Debian Codegraph @

addons/barcodes_gs1_nomenclature/static/src/js/tests/barcode_parser.test.js

raw
1 import { barcodeService } from "@barcodes/barcode_service";
2 import { BarcodeParser } from "@barcodes/js/barcode_parser";
3 import { expect, test } from "@odoo/hoot";
4
5 function buildBarcodeParser(diff = {}) {
6 const nomenclature = {
7 id: 2,
8 name: "GS1",
9 upc_ean_conv: "always",
10 is_gs1_nomenclature: true,
11 gs1_separator_fnc1: "",
12 rules: [
13 {
14 id: 2,
15 name: "Global Trade Item Number (GTIN)",
16 barcode_nomenclature_id: 2,
17 sequence: 100,
18 encoding: "gs1-128",
19 pattern: "(01)(\\d{14})",
20 type: "product",
21 gs1_content_type: "identifier",
22 gs1_decimal_usage: false,
23 },
24 {
25 id: 3,
26 name: "GTIN of contained trade items",
27 barcode_nomenclature_id: 2,
28 sequence: 101,
29 encoding: "gs1-128",
30 pattern: "(02)(\\d{14})",
31 type: "product",
32 gs1_content_type: "identifier",
33 gs1_decimal_usage: false,
34 },
35 {
36 id: 4,
37 name: "Batch or lot number",
38 barcode_nomenclature_id: 2,
39 sequence: 102,
40 encoding: "gs1-128",
41 pattern: '(10)([!"%/0-9:?A-Za-z]{0,20})',
42 type: "product",
43 gs1_content_type: "alpha",
44 gs1_decimal_usage: false,
45 },
46 {
47 id: 5,
48 name: "Serial number",
49 barcode_nomenclature_id: 2,
50 sequence: 105,
51 encoding: "gs1-128",
52 pattern: '(21)([!"%/0-9:?A-Za-z]{0,20})',
53 type: "product",
54 gs1_content_type: "alpha",
55 gs1_decimal_usage: false,
56 },
57 {
58 id: 6,
59 name: "Pack date (YYMMDD)",
60 barcode_nomenclature_id: 2,
61 sequence: 103,
62 encoding: "gs1-128",
63 pattern: "(13)(\\d{6})",
64 type: "pack_date",
65 gs1_content_type: "date",
66 gs1_decimal_usage: false,
67 },
68 {
69 id: 7,
70 name: "Best before date (YYMMDD)",
71 barcode_nomenclature_id: 2,
72 sequence: 104,
73 encoding: "gs1-128",
74 pattern: "(15)(\\d{6})",
75 type: "use_date",
76 gs1_content_type: "date",
77 gs1_decimal_usage: false,
78 },
79 {
80 id: 8,
81 name: "Expiration date (YYMMDD)",
82 barcode_nomenclature_id: 2,
83 sequence: 105,
84 encoding: "gs1-128",
85 pattern: "(16)(\\d{6})",
86 type: "expiration_date",
87 gs1_content_type: "date",
88 gs1_decimal_usage: false,
89 },
90 {
91 id: 9,
92 name: "Variable count of items (variabl",
93 barcode_nomenclature_id: 2,
94 sequence: 105,
95 encoding: "gs1-128",
96 pattern: "(30)(\\d{0,8})",
97 type: "product",
98 gs1_content_type: "measure",
99 gs1_decimal_usage: false,
100 },
101 {
102 id: 10,
103 name: "Count of trade items or trade it",
104 barcode_nomenclature_id: 2,
105 sequence: 105,
106 encoding: "gs1-128",
107 pattern: "(37)(\\d{0,8})",
108 type: "product",
109 gs1_content_type: "measure",
110 gs1_decimal_usage: false,
111 },
112 {
113 id: 11,
114 name: "Net weight, kilograms (variable",
115 barcode_nomenclature_id: 2,
116 sequence: 105,
117 encoding: "gs1-128",
118 pattern: "(310[0-5])(\\d{6})",
119 type: "product",
120 gs1_content_type: "measure",
121 gs1_decimal_usage: true,
122 },
123 {
124 id: 12,
125 name: "Length or first dimension, metre",
126 barcode_nomenclature_id: 2,
127 sequence: 105,
128 encoding: "gs1-128",
129 pattern: "(311[0-5])(\\d{6})",
130 type: "product",
131 gs1_content_type: "measure",
132 gs1_decimal_usage: true,
133 },
134 {
135 id: 13,
136 name: "Net volume, litres (variable mea",
137 barcode_nomenclature_id: 2,
138 sequence: 105,
139 encoding: "gs1-128",
140 pattern: "(315[0-5])(\\d{6})",
141 type: "product",
142 gs1_content_type: "measure",
143 gs1_decimal_usage: true,
144 },
145 {
146 id: 14,
147 name: "Length or first dimension, inche",
148 barcode_nomenclature_id: 2,
149 sequence: 105,
150 encoding: "gs1-128",
151 pattern: "(321[0-5])(\\d{6})",
152 type: "product",
153 gs1_content_type: "measure",
154 gs1_decimal_usage: true,
155 },
156 {
157 id: 15,
158 name: "Net weight (or volume), ounces (",
159 barcode_nomenclature_id: 2,
160 sequence: 105,
161 encoding: "gs1-128",
162 pattern: "(357[0-5])(\\d{6})",
163 type: "product",
164 gs1_content_type: "measure",
165 gs1_decimal_usage: true,
166 },
167 ],
168 };
169 return new BarcodeParser({ nomenclature: { ...nomenclature, ...diff } });
170 }
171
172 test("Test gs1 date barcode", async () => {
173 const barcodeNomenclature = buildBarcodeParser();
174
175 // 20/10/2015 -> 151020
176 let dateGS1 = "151020";
177 let date = barcodeNomenclature.gs1_date_to_date(dateGS1);
178 expect(date.getDate()).toBe(20);
179 expect(date.getMonth() + 1).toBe(10);
180 expect(date.getFullYear()).toBe(2015);
181
182 // XX/03/2052 -> 520300 -> (if day no set take last day of the month -> 31)
183 dateGS1 = "520300";
184 date = barcodeNomenclature.gs1_date_to_date(dateGS1);
185 expect(date.getDate()).toBe(31);
186 expect(date.getMonth() + 1).toBe(3);
187 expect(date.getFullYear()).toBe(2052);
188
189 // XX/02/2020 -> 520200 -> (if day no set take last day of the month -> 29)
190 dateGS1 = "200200";
191 date = barcodeNomenclature.gs1_date_to_date(dateGS1);
192 expect(date.getDate()).toBe(29);
193 expect(date.getMonth() + 1).toBe(2);
194 expect(date.getFullYear()).toBe(2020);
195 });
196
197 test("Test gs1 decompose extended", async () => {
198 const barcodeNomenclature = buildBarcodeParser();
199
200 // (01)94019097685457(10)33650100138(3102)002004(15)131018
201 const code128 = "01940190976854571033650100138\x1D310200200415131018";
202 let res = barcodeNomenclature.gs1_decompose_extended(code128);
203 expect(res.length).toBe(4);
204 expect(res[0].ai).toBe("01");
205
206 expect(res[1].ai).toBe("10");
207
208 expect(res[2].ai).toBe("3102");
209 expect(res[2].value).toBe(20.04);
210
211 expect(res[3].ai).toBe("15");
212 expect(res[3].value.getFullYear()).toBe(2013);
213 expect(res[3].value.getDate()).toBe(18);
214 expect(res[3].value.getMonth() + 1).toBe(10);
215
216 // Check multiple variants of the same GS1, the result should be always the same.
217 // (01)94019097685457(30)17(13)170119
218 const gs1Barcodes = [
219 "0194019097685457300000001713170119",
220 "\x1D0194019097685457300000001713170119",
221 "01940190976854573017\x1D13170119",
222 ];
223 for (const gs1Barcode of gs1Barcodes) {
224 res = barcodeNomenclature.gs1_decompose_extended(gs1Barcode);
225 expect(res.length).toBe(3);
226 expect(res[0].ai).toBe("01");
227
228 expect(res[1].ai).toBe("30");
229 expect(res[1].value).toBe(17);
230
231 expect(res[2].ai).toBe("13");
232 expect(res[2].value.getFullYear()).toBe(2017);
233 expect(res[2].value.getDate()).toBe(19);
234 expect(res[2].value.getMonth() + 1).toBe(1);
235 }
236 });
237
238 test("Test Alternative GS1 Separator (fnc1)", async () => {
239 let barcodeNomenclature = buildBarcodeParser();
240
241 // (21)12345(15)090101(16)100101
242 const code128 = "2112345#1509010116100101";
243 expect(() => {
244 barcodeNomenclature.gs1_decompose_extended(barcodeService.cleanBarcode(code128));
245 }).toThrow();
246
247 // Reload the nomenclature but this time using '#' as separator.
248 barcodeNomenclature = buildBarcodeParser({ gs1_separator_fnc1: "#" });
249 const res = barcodeNomenclature.gs1_decompose_extended(barcodeService.cleanBarcode(code128));
250 expect(res.length).toBe(3);
251 expect(res[0].ai).toBe("21");
252 expect(res[0].value).toBe("12345");
253 expect(res[1].ai).toBe("15");
254 expect(res[2].ai).toBe("16");
255 });