Debian Codegraph @

addons/pos_glory_cash/static/tests/glory_service.test.js

raw
1 import { beforeEach, describe, expect, test, waitUntil } from "@odoo/hoot";
2 import { patchWithCleanup } from "@web/../tests/web_test_helpers";
3 import { GloryService } from "@pos_glory_cash/glory_service";
4 import { SocketIoService } from "@pos_glory_cash/utils/socket_io";
5
6 const sentMessages = [];
7
8 const mockSettings = {
9 FunctionSetting: {
10 OccupyEnable: "0",
11 SessionEnable: "0",
12 SessionMinute: "60",
13 SoapDuplicateCheck: "0",
14 SoapUserCheck: "0",
15 },
16 };
17
18 const parsedMockSettings = {
19 OccupyEnable: 0,
20 SessionEnable: 0,
21 SessionMinute: 60,
22 SoapDuplicateCheck: 0,
23 SoapUserCheck: 0,
24 };
25
26 const expectedDateTimeMessage =
27 '<AdjustTimeRequest><Id>OdooPos</Id><SeqNo>00000000000</SeqNo><Date year="2019" month="3" day="11"/><Time hour="10" minute="30" second="0"/></AdjustTimeRequest>\0';
28 const expectedGetStatusMessage =
29 '<StatusRequest><Id>OdooPos</Id><SeqNo>00000000000</SeqNo><RequireVerification type="1"/></StatusRequest>\0';
30 const expectedGetInventoryMessage =
31 '<InventoryRequest><Id>OdooPos</Id><SeqNo>00000000000</SeqNo><Option type="2"/></InventoryRequest>\0';
32
33 beforeEach(() => {
34 sentMessages.splice(0, sentMessages.length);
35 patchWithCleanup(SocketIoService.prototype, {
36 _responses: {},
37 _xmlResponses: {},
38 connect() {
39 this.callbacks.onConnect();
40 },
41 _respondToMessageWith(message, response) {
42 this._responses[message] = response;
43 },
44 _respondToXmlMessageWith(message, response) {
45 this._xmlResponses[message] = response;
46 },
47 sendMessage(message) {
48 sentMessages.push(message);
49 if (message[0] in this._responses) {
50 this.callbacks.onEvent(this._responses[message[0]]);
51 } else if (message[0] === "xml send") {
52 const xmlMessage = message[1].split("><")[0].slice(1);
53 if (xmlMessage in this._xmlResponses) {
54 const strResponse = `<BbxEventRequest>${this._xmlResponses[xmlMessage]}</BbxEventRequest>`;
55 this.callbacks.onBinaryEvent(new Blob([strResponse]));
56 }
57 }
58 },
59 });
60 });
61
62 describe("on connecting", () => {
63 test("checks credentials", async () => {
64 const gloryService = new GloryService(() => {});
65 gloryService.connect("mockIp");
66
67 expect(sentMessages).toHaveLength(1);
68 expect(sentMessages[0]).toEqual(["check credential", undefined]);
69 });
70
71 test("retrieves device settings", async () => {
72 const gloryService = new GloryService(() => {});
73 gloryService.socketIo._respondToMessageWith("check credential", ["credential ok"]);
74 gloryService.socketIo._respondToMessageWith("getFunctionSetting", [
75 "sendFunctionSetting",
76 mockSettings,
77 ]);
78 gloryService.connect("mockIp");
79
80 await waitUntil(() => sentMessages.length >= 2);
81
82 expect(sentMessages[1]).toEqual(["getFunctionSetting"]);
83 expect(gloryService.settings).toEqual(parsedMockSettings);
84 });
85
86 test("sets date and time", async () => {
87 const gloryService = new GloryService(() => {});
88 gloryService.socketIo._respondToMessageWith("check credential", ["credential ok"]);
89 gloryService.socketIo._respondToMessageWith("getFunctionSetting", [
90 "sendFunctionSetting",
91 mockSettings,
92 ]);
93 gloryService.connect("mockIp");
94
95 await waitUntil(() => sentMessages.length >= 3);
96
97 expect(sentMessages[2]).toEqual(["xml send", expectedDateTimeMessage]);
98 });
99
100 test("retrieves device status", async () => {
101 const gloryService = new GloryService(() => {});
102 gloryService.socketIo._respondToMessageWith("check credential", ["credential ok"]);
103 gloryService.socketIo._respondToMessageWith("getFunctionSetting", [
104 "sendFunctionSetting",
105 mockSettings,
106 ]);
107 gloryService.socketIo._respondToXmlMessageWith(
108 "AdjustTimeRequest",
109 '<AdjustTimeResponse result="0"/>'
110 );
111 gloryService.socketIo._respondToXmlMessageWith(
112 "StatusRequest",
113 '<StatusResponse result="0"><Code>0</Code></StatusResponse>'
114 );
115 gloryService.connect("mockIp");
116
117 await waitUntil(() => sentMessages.length >= 4);
118
119 expect(sentMessages[3]).toEqual(["xml send", expectedGetStatusMessage]);
120 expect(gloryService.status).toBe("INITIALIZING");
121 });
122
123 test("retrieves initial inventory", async () => {
124 const gloryService = new GloryService(() => {});
125 gloryService.socketIo._respondToMessageWith("check credential", ["credential ok"]);
126 gloryService.socketIo._respondToMessageWith("getFunctionSetting", [
127 "sendFunctionSetting",
128 mockSettings,
129 ]);
130 gloryService.socketIo._respondToXmlMessageWith(
131 "AdjustTimeRequest",
132 '<AdjustTimeResponse result="0"/>'
133 );
134 gloryService.socketIo._respondToXmlMessageWith(
135 "StatusRequest",
136 '<StatusResponse result="0"><Code>0</Code></StatusResponse>'
137 );
138 gloryService.socketIo._respondToXmlMessageWith(
139 "InventoryRequest",
140 '<InventoryResponse result="0"><Cash type="4"><Denomination fv="10"><Piece>5</Piece><Status>1</Status></Denomination></Cash></InventoryResponse>'
141 );
142 gloryService.connect("mockIp");
143
144 await waitUntil(() => sentMessages.length >= 5);
145
146 expect(sentMessages[4]).toEqual(["xml send", expectedGetInventoryMessage]);
147 expect(gloryService.state.inventory).toHaveLength(1);
148 expect(gloryService.state.inventory[0]).toEqual({
149 value: 10,
150 amount: 5,
151 status: "NEAR_EMPTY",
152 });
153 });
154 });