@vicacha-devs/medusa-plugin-barcode 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.medusa/server/src/admin/index.cjs +3960 -0
- package/.medusa/server/src/admin/index.js +3938 -0
- package/.medusa/server/src/api/admin/barcodes/[barcode]/route.js +43 -0
- package/.medusa/server/src/api/admin/barcodes/batch/route.js +9 -0
- package/.medusa/server/src/api/admin/barcodes/bulk-generate/route.js +9 -0
- package/.medusa/server/src/api/admin/barcodes/cache/route.js +25 -0
- package/.medusa/server/src/api/admin/barcodes/count-sessions/[id]/apply/route.js +13 -0
- package/.medusa/server/src/api/admin/barcodes/count-sessions/[id]/scan/route.js +13 -0
- package/.medusa/server/src/api/admin/barcodes/count-sessions/route.js +27 -0
- package/.medusa/server/src/api/admin/barcodes/create-from-barcode/route.js +9 -0
- package/.medusa/server/src/api/admin/barcodes/lookup/route.js +12 -0
- package/.medusa/server/src/api/admin/barcodes/print/route.js +11 -0
- package/.medusa/server/src/api/admin/barcodes/route.js +27 -0
- package/.medusa/server/src/api/admin/barcodes/scan/route.js +23 -0
- package/.medusa/server/src/api/admin/barcodes/tags/[id]/route.js +28 -0
- package/.medusa/server/src/api/admin/barcodes/tags/route.js +25 -0
- package/.medusa/server/src/api/admin/barcodes/templates/route.js +40 -0
- package/.medusa/server/src/api/admin/plugin/route.js +4 -0
- package/.medusa/server/src/api/admin/variants/barcode/[code]/route.js +28 -0
- package/.medusa/server/src/api/middlewares.js +73 -0
- package/.medusa/server/src/api/store/barcodes/[code]/route.js +28 -0
- package/.medusa/server/src/api/store/barcodes/[code]/verify/route.js +20 -0
- package/.medusa/server/src/api/store/plugin/route.js +4 -0
- package/.medusa/server/src/api/store/variants/barcode/[code]/route.js +28 -0
- package/.medusa/server/src/api/validators.js +173 -0
- package/.medusa/server/src/hooks/index.js +202 -0
- package/.medusa/server/src/index.js +5 -0
- package/.medusa/server/src/jobs/purge-expired-barcode-cache.js +10 -0
- package/.medusa/server/src/modules/barcode/index.js +16 -0
- package/.medusa/server/src/modules/barcode/migrations/Migration20260522000000.js +165 -0
- package/.medusa/server/src/modules/barcode/models/index.js +162 -0
- package/.medusa/server/src/modules/barcode/operations/context.js +2 -0
- package/.medusa/server/src/modules/barcode/operations/data-service.js +10 -0
- package/.medusa/server/src/modules/barcode/operations/generation.js +33 -0
- package/.medusa/server/src/modules/barcode/operations/index.js +9 -0
- package/.medusa/server/src/modules/barcode/operations/labels.js +57 -0
- package/.medusa/server/src/modules/barcode/operations/lookup.js +135 -0
- package/.medusa/server/src/modules/barcode/operations/maintenance.js +37 -0
- package/.medusa/server/src/modules/barcode/operations/stock-count.js +129 -0
- package/.medusa/server/src/modules/barcode/operations/tags.js +298 -0
- package/.medusa/server/src/modules/barcode/providers/index.js +721 -0
- package/.medusa/server/src/modules/barcode/service.js +161 -0
- package/.medusa/server/src/modules/barcode/types.js +21 -0
- package/.medusa/server/src/modules/barcode/utils/barcode-parser.js +399 -0
- package/.medusa/server/src/modules/barcode/utils/label-printer.js +533 -0
- package/.medusa/server/src/modules/barcode/utils/normalize-product.js +443 -0
- package/.medusa/server/src/modules/barcode/utils/security.js +114 -0
- package/.medusa/server/src/modules/barcode/utils/svg-renderer.js +492 -0
- package/.medusa/server/src/rbac.js +130 -0
- package/.medusa/server/src/sdk/index.js +164 -0
- package/.medusa/server/src/subscribers/product-created.js +34 -0
- package/.medusa/server/src/types/index.js +2 -0
- package/.medusa/server/src/ui/index.js +138 -0
- package/.medusa/server/src/workflows/apply-inventory-count.js +68 -0
- package/.medusa/server/src/workflows/batch-barcode-import.js +153 -0
- package/.medusa/server/src/workflows/bulk-generate-variant-barcodes.js +143 -0
- package/.medusa/server/src/workflows/generate-barcode-tag.js +44 -0
- package/.medusa/server/src/workflows/helpers.js +15 -0
- package/.medusa/server/src/workflows/index.js +7 -0
- package/.medusa/server/src/workflows/lookup-and-create-product.js +215 -0
- package/.medusa/server/src/workflows/scan-inventory-count.js +15 -0
- package/LICENSE +21 -0
- package/README.md +844 -2
- package/package.json +129 -3
- package/src/__tests__/barcode.spec.ts +836 -0
- package/src/__tests__/medusa-shim.mjs +331 -0
- package/src/__tests__/register-ts-resolver.mjs +135 -0
- package/src/admin/README.md +134 -0
- package/src/admin/components/barcode-studio-header.tsx +73 -0
- package/src/admin/components/batch-import-section.tsx +99 -0
- package/src/admin/components/dynamic-qr-tag-drawer-form.tsx +230 -0
- package/src/admin/components/dynamic-qr-tag-form.tsx +129 -0
- package/src/admin/components/dynamic-qr-tags-table.tsx +136 -0
- package/src/admin/components/gtin-lookup-section.tsx +155 -0
- package/src/admin/components/index.tsx +14 -0
- package/src/admin/components/label-designer-section.tsx +151 -0
- package/src/admin/components/label-output-section.tsx +63 -0
- package/src/admin/components/overview-cards.tsx +98 -0
- package/src/admin/components/product-barcode-card.tsx +127 -0
- package/src/admin/components/providers-cache-table.tsx +61 -0
- package/src/admin/components/providers-overview-section.tsx +49 -0
- package/src/admin/components/rbac-guard.tsx +153 -0
- package/src/admin/components/stock-take-scanner-section.tsx +116 -0
- package/src/admin/components/stock-take-sessions-section.tsx +109 -0
- package/src/admin/hooks/use-barcode-studio.ts +466 -0
- package/src/admin/hooks/use-product-barcodes.ts +139 -0
- package/src/admin/i18n/README.md +130 -0
- package/src/admin/i18n/index.ts +36 -0
- package/src/admin/i18n/json/ar.json +157 -0
- package/src/admin/i18n/json/de.json +157 -0
- package/src/admin/i18n/json/en.json +157 -0
- package/src/admin/i18n/json/es.json +157 -0
- package/src/admin/i18n/json/fr.json +157 -0
- package/src/admin/i18n/json/it.json +157 -0
- package/src/admin/i18n/json/nl.json +157 -0
- package/src/admin/i18n/json/pl.json +157 -0
- package/src/admin/i18n/json/pt.json +157 -0
- package/src/admin/i18n/json/tr.json +157 -0
- package/src/admin/routes/barcodes/lookup/page.tsx +75 -0
- package/src/admin/routes/barcodes/page.tsx +212 -0
- package/src/admin/routes/barcodes/print/page.tsx +67 -0
- package/src/admin/routes/barcodes/providers/page.tsx +66 -0
- package/src/admin/routes/barcodes/stock-take/page.tsx +74 -0
- package/src/admin/routes/barcodes/tags/@create/page.tsx +56 -0
- package/src/admin/routes/barcodes/tags/page.tsx +72 -0
- package/src/admin/tsconfig.json +20 -0
- package/src/admin/vite-env.d.ts +1 -0
- package/src/admin/widgets/product-barcode-widget.tsx +43 -0
- package/src/api/README.md +117 -0
- package/src/api/admin/barcodes/[barcode]/route.ts +64 -0
- package/src/api/admin/barcodes/batch/route.ts +12 -0
- package/src/api/admin/barcodes/bulk-generate/route.ts +12 -0
- package/src/api/admin/barcodes/cache/route.ts +35 -0
- package/src/api/admin/barcodes/count-sessions/[id]/apply/route.ts +17 -0
- package/src/api/admin/barcodes/count-sessions/[id]/scan/route.ts +17 -0
- package/src/api/admin/barcodes/count-sessions/route.ts +43 -0
- package/src/api/admin/barcodes/create-from-barcode/route.ts +16 -0
- package/src/api/admin/barcodes/lookup/route.ts +19 -0
- package/src/api/admin/barcodes/print/route.ts +18 -0
- package/src/api/admin/barcodes/route.ts +36 -0
- package/src/api/admin/barcodes/scan/route.ts +32 -0
- package/src/api/admin/barcodes/tags/[id]/route.ts +41 -0
- package/src/api/admin/barcodes/tags/route.ts +36 -0
- package/src/api/admin/barcodes/templates/route.ts +51 -0
- package/src/api/admin/plugin/route.ts +5 -0
- package/src/api/admin/variants/barcode/[code]/route.ts +36 -0
- package/src/api/middlewares.ts +91 -0
- package/src/api/store/barcodes/[code]/route.ts +43 -0
- package/src/api/store/barcodes/[code]/verify/route.ts +35 -0
- package/src/api/store/plugin/route.ts +5 -0
- package/src/api/store/variants/barcode/[code]/route.ts +36 -0
- package/src/api/validators.ts +217 -0
- package/src/hooks/index.ts +270 -0
- package/src/index.ts +4 -0
- package/src/jobs/README.md +54 -0
- package/src/jobs/purge-expired-barcode-cache.ts +18 -0
- package/src/links/README.md +44 -0
- package/src/modules/README.md +85 -0
- package/src/modules/barcode/index.ts +18 -0
- package/src/modules/barcode/migrations/Migration20260522000000.ts +225 -0
- package/src/modules/barcode/models/index.ts +168 -0
- package/src/modules/barcode/operations/context.ts +14 -0
- package/src/modules/barcode/operations/data-service.ts +10 -0
- package/src/modules/barcode/operations/generation.ts +57 -0
- package/src/modules/barcode/operations/index.ts +8 -0
- package/src/modules/barcode/operations/labels.ts +90 -0
- package/src/modules/barcode/operations/lookup.ts +202 -0
- package/src/modules/barcode/operations/maintenance.ts +53 -0
- package/src/modules/barcode/operations/stock-count.ts +217 -0
- package/src/modules/barcode/operations/tags.ts +425 -0
- package/src/modules/barcode/providers/index.ts +958 -0
- package/src/modules/barcode/service.ts +243 -0
- package/src/modules/barcode/types.ts +297 -0
- package/src/modules/barcode/utils/barcode-parser.ts +453 -0
- package/src/modules/barcode/utils/label-printer.ts +641 -0
- package/src/modules/barcode/utils/normalize-product.ts +524 -0
- package/src/modules/barcode/utils/security.ts +136 -0
- package/src/modules/barcode/utils/svg-renderer.ts +574 -0
- package/src/providers/README.md +105 -0
- package/src/rbac.ts +144 -0
- package/src/sdk/index.ts +253 -0
- package/src/subscribers/README.md +73 -0
- package/src/subscribers/product-created.ts +45 -0
- package/src/types/index.ts +1 -0
- package/src/types/medusa-ui.d.ts +118 -0
- package/src/ui/index.tsx +311 -0
- package/src/workflows/README.md +56 -0
- package/src/workflows/apply-inventory-count.ts +115 -0
- package/src/workflows/batch-barcode-import.ts +218 -0
- package/src/workflows/bulk-generate-variant-barcodes.ts +214 -0
- package/src/workflows/generate-barcode-tag.ts +85 -0
- package/src/workflows/helpers.ts +21 -0
- package/src/workflows/index.ts +6 -0
- package/src/workflows/lookup-and-create-product.ts +298 -0
- package/src/workflows/scan-inventory-count.ts +45 -0
- package/tsconfig.json +40 -0
|
@@ -0,0 +1,836 @@
|
|
|
1
|
+
import assert from "node:assert/strict"
|
|
2
|
+
import { describe, it } from "node:test"
|
|
3
|
+
import {
|
|
4
|
+
BARCODE_EVENTS,
|
|
5
|
+
BARCODE_MODULE,
|
|
6
|
+
BarcodeModuleService,
|
|
7
|
+
BaseBarcodeProvider,
|
|
8
|
+
buildGs1DigitalLink,
|
|
9
|
+
BUILTIN_LABEL_TEMPLATES,
|
|
10
|
+
CATEGORY_ALIASES,
|
|
11
|
+
computeEan13CheckDigit,
|
|
12
|
+
computeGs1CheckDigit,
|
|
13
|
+
escapeHtml,
|
|
14
|
+
generateInternalEan13,
|
|
15
|
+
mapToMedusaCreateProduct,
|
|
16
|
+
mergeNormalizedProducts,
|
|
17
|
+
normalizeCategory,
|
|
18
|
+
type NormalizedBarcodeProduct,
|
|
19
|
+
parseBarcode,
|
|
20
|
+
parseDimensionMm,
|
|
21
|
+
parseGs1ApplicationIdentifiers,
|
|
22
|
+
parseWeightGrams,
|
|
23
|
+
ProviderRegistry,
|
|
24
|
+
renderBarcodeSvg,
|
|
25
|
+
renderPrintableLabels,
|
|
26
|
+
runProviderLookupChain,
|
|
27
|
+
upcToEan13,
|
|
28
|
+
validateExternalImageUrl,
|
|
29
|
+
validateGs1Checksum,
|
|
30
|
+
} from "../modules/barcode"
|
|
31
|
+
import {
|
|
32
|
+
applyInventoryCountWorkflow,
|
|
33
|
+
batchBarcodeImportWorkflow,
|
|
34
|
+
bulkGenerateVariantBarcodesWorkflow,
|
|
35
|
+
generateBarcodeTagWorkflow,
|
|
36
|
+
lookupAndCreateProductWorkflow,
|
|
37
|
+
scanInventoryCountWorkflow,
|
|
38
|
+
} from "../workflows"
|
|
39
|
+
import middlewares from "../api/middlewares"
|
|
40
|
+
import adminI18n, { SUPPORTED_UI_LOCALES } from "../admin/i18n"
|
|
41
|
+
import { createBarcodeSdk } from "../sdk"
|
|
42
|
+
|
|
43
|
+
class MockProvider extends BaseBarcodeProvider {
|
|
44
|
+
readonly name: string
|
|
45
|
+
private readonly mockData: Partial<NormalizedBarcodeProduct> | null
|
|
46
|
+
|
|
47
|
+
constructor(
|
|
48
|
+
name: string,
|
|
49
|
+
priority: number,
|
|
50
|
+
mockData: Partial<NormalizedBarcodeProduct> | null
|
|
51
|
+
) {
|
|
52
|
+
super({ priority })
|
|
53
|
+
this.name = name
|
|
54
|
+
this.mockData = mockData
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async lookup(barcode: string): Promise<NormalizedBarcodeProduct | null> {
|
|
58
|
+
if (!this.mockData) return null
|
|
59
|
+
return {
|
|
60
|
+
barcode,
|
|
61
|
+
barcode_type: "ean13",
|
|
62
|
+
name: this.mockData.name || "Mock Product",
|
|
63
|
+
description: this.mockData.description ?? null,
|
|
64
|
+
brand: this.mockData.brand ?? null,
|
|
65
|
+
manufacturer: this.mockData.manufacturer ?? null,
|
|
66
|
+
categories: this.mockData.categories || [],
|
|
67
|
+
images: this.mockData.images || [],
|
|
68
|
+
weight: this.mockData.weight ?? null,
|
|
69
|
+
weight_unit: this.mockData.weight_unit ?? null,
|
|
70
|
+
dimensions: this.mockData.dimensions ?? null,
|
|
71
|
+
quantity_label: this.mockData.quantity_label ?? null,
|
|
72
|
+
ingredients: this.mockData.ingredients ?? null,
|
|
73
|
+
nutrition: this.mockData.nutrition ?? null,
|
|
74
|
+
country_of_origin: this.mockData.country_of_origin ?? null,
|
|
75
|
+
suggested_price: this.mockData.suggested_price ?? null,
|
|
76
|
+
currency_code: this.mockData.currency_code ?? "USD",
|
|
77
|
+
isbn: this.mockData.isbn ?? null,
|
|
78
|
+
authors: this.mockData.authors || [],
|
|
79
|
+
publisher: this.mockData.publisher ?? null,
|
|
80
|
+
published_date: this.mockData.published_date ?? null,
|
|
81
|
+
page_count: this.mockData.page_count ?? null,
|
|
82
|
+
provider: this.name,
|
|
83
|
+
raw_data: {},
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
describe("medusa-plugin-barcode — GS1 Parser, Checksum & Digital Link", () => {
|
|
89
|
+
it("validates GS1 Modulo-10 check digits right-to-left across odd and even lengths", () => {
|
|
90
|
+
// EAN-13 (Nutella 3017620422003)
|
|
91
|
+
assert.equal(computeEan13CheckDigit("301762042200"), 3)
|
|
92
|
+
assert.equal(validateGs1Checksum("3017620422003"), true)
|
|
93
|
+
assert.equal(validateGs1Checksum("3017620422004"), false)
|
|
94
|
+
|
|
95
|
+
// UPC-A (12 digits - even length! Fixes @bambiste/medusa-barcode bug)
|
|
96
|
+
assert.equal(computeGs1CheckDigit("01234567890"), 5)
|
|
97
|
+
assert.equal(validateGs1Checksum("012345678905"), true)
|
|
98
|
+
assert.equal(upcToEan13("012345678905"), "0012345678905")
|
|
99
|
+
|
|
100
|
+
// EAN-8 (8 digits - even length)
|
|
101
|
+
assert.equal(computeGs1CheckDigit("9638507"), 4)
|
|
102
|
+
assert.equal(validateGs1Checksum("96385074"), true)
|
|
103
|
+
|
|
104
|
+
// GTIN-14 (14 digits - even length)
|
|
105
|
+
assert.equal(computeGs1CheckDigit("1061414100041"), 5)
|
|
106
|
+
assert.equal(validateGs1Checksum("10614141000415"), true)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it("identifies ISBN-13 (978/979) and GS1-128 Application Identifiers", () => {
|
|
110
|
+
const isbn = parseBarcode("9780140328721")
|
|
111
|
+
assert.equal(isbn.type, "isbn13")
|
|
112
|
+
assert.equal(isbn.isIsbn, true)
|
|
113
|
+
assert.equal(isbn.isValid, true)
|
|
114
|
+
|
|
115
|
+
const gs1 = parseBarcode(
|
|
116
|
+
"(01)09506000134352(10)LOT2026(17)261231(21)SN9988(3102)001250"
|
|
117
|
+
)
|
|
118
|
+
assert.equal(gs1.type, "gs1-128")
|
|
119
|
+
assert.equal(gs1.isValid, true)
|
|
120
|
+
assert.equal(gs1.gs1?.gtin, "09506000134352")
|
|
121
|
+
assert.equal(gs1.gs1?.batchLot, "LOT2026")
|
|
122
|
+
assert.equal(gs1.gs1?.expiryDate, "2026-12-31")
|
|
123
|
+
assert.equal(gs1.gs1?.serialNumber, "SN9988")
|
|
124
|
+
assert.equal(gs1.gs1?.netWeightKg, 12.5)
|
|
125
|
+
|
|
126
|
+
const directAi = parseGs1ApplicationIdentifiers("(01)04006381333931(10)B42")
|
|
127
|
+
assert.equal(directAi?.gtin, "04006381333931")
|
|
128
|
+
assert.equal(directAi?.batchLot, "B42")
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it("generates valid GS1 in-store EAN-13 (prefix 200-299) and GS1 Digital Link URIs", () => {
|
|
132
|
+
const internal1 = generateInternalEan13("variant_01", "200")
|
|
133
|
+
const internal2 = generateInternalEan13("variant_01", "200")
|
|
134
|
+
assert.equal(internal1, internal2)
|
|
135
|
+
assert.equal(internal1.length, 13)
|
|
136
|
+
assert.equal(internal1.startsWith("200"), true)
|
|
137
|
+
assert.equal(validateGs1Checksum(internal1), true)
|
|
138
|
+
|
|
139
|
+
const digitalLink = buildGs1DigitalLink({
|
|
140
|
+
baseUrl: "https://id.store.com",
|
|
141
|
+
gtin: "3017620422003",
|
|
142
|
+
batchLot: "LOT-7",
|
|
143
|
+
serial: "SN-42",
|
|
144
|
+
expiry: "261231",
|
|
145
|
+
})
|
|
146
|
+
assert.equal(
|
|
147
|
+
digitalLink,
|
|
148
|
+
"https://id.store.com/01/03017620422003/10/LOT-7/21/SN-42?17=261231"
|
|
149
|
+
)
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
describe("medusa-plugin-barcode — Multi-Provider Chain & Deep-Merge Enrichment", () => {
|
|
154
|
+
it("supports both first-hit and deep-merge enrichment strategies", async () => {
|
|
155
|
+
const registry = new ProviderRegistry()
|
|
156
|
+
registry.register(
|
|
157
|
+
new MockProvider("openfoodfacts", 10, {
|
|
158
|
+
name: "Artisan Hazelnut Spread",
|
|
159
|
+
brand: "Ferrero",
|
|
160
|
+
images: ["https://images.example.com/front.jpg"],
|
|
161
|
+
description: null,
|
|
162
|
+
ingredients: "Sugar, Palm Oil, Hazelnuts",
|
|
163
|
+
})
|
|
164
|
+
)
|
|
165
|
+
registry.register(
|
|
166
|
+
new MockProvider("upcitemdb", 20, {
|
|
167
|
+
name: "Hazelnut Spread 400g",
|
|
168
|
+
brand: "Ferrero",
|
|
169
|
+
description: "Rich chocolate hazelnut spread imported from Italy.",
|
|
170
|
+
images: ["https://images.example.com/side.jpg"],
|
|
171
|
+
suggested_price: 6.99,
|
|
172
|
+
})
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
const firstHit = await runProviderLookupChain("3017620422003", registry, {
|
|
176
|
+
strategy: "first-hit",
|
|
177
|
+
})
|
|
178
|
+
assert.equal(firstHit.found, true)
|
|
179
|
+
assert.equal(firstHit.outcomes.length, 1)
|
|
180
|
+
assert.equal(firstHit.product?.description, null)
|
|
181
|
+
|
|
182
|
+
const deepMerge = await runProviderLookupChain("3017620422003", registry, {
|
|
183
|
+
strategy: "deep-merge",
|
|
184
|
+
})
|
|
185
|
+
assert.equal(deepMerge.found, true)
|
|
186
|
+
assert.equal(deepMerge.outcomes.length, 2)
|
|
187
|
+
assert.equal(deepMerge.product?.name, "Artisan Hazelnut Spread")
|
|
188
|
+
assert.equal(
|
|
189
|
+
deepMerge.product?.description,
|
|
190
|
+
"Rich chocolate hazelnut spread imported from Italy."
|
|
191
|
+
)
|
|
192
|
+
assert.equal(deepMerge.product?.images.length, 2)
|
|
193
|
+
assert.equal(deepMerge.product?.suggested_price, 6.99)
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
it("prioritizes GoogleBooks and OpenLibrary automatically for 978/979 ISBNs", () => {
|
|
197
|
+
const registry = new ProviderRegistry()
|
|
198
|
+
registry.register(new MockProvider("openfoodfacts", 10, null))
|
|
199
|
+
registry.register(new MockProvider("googlebooks", 50, null))
|
|
200
|
+
registry.register(new MockProvider("openlibrary", 60, null))
|
|
201
|
+
|
|
202
|
+
const ordered = registry.getOrderedForBarcode("9780140328721", "isbn13")
|
|
203
|
+
assert.equal(ordered[0]?.name, "googlebooks")
|
|
204
|
+
assert.equal(ordered[1]?.name, "openlibrary")
|
|
205
|
+
assert.equal(ordered[2]?.name, "openfoodfacts")
|
|
206
|
+
})
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
describe("medusa-plugin-barcode — SVG Renderer, Thermal/Sheet Labels & ZPL II", () => {
|
|
210
|
+
it("renders 1D and 2D barcodes as standalone SVGs and escapes XSS payloads", () => {
|
|
211
|
+
const eanSvg = renderBarcodeSvg({
|
|
212
|
+
code: "3017620422003",
|
|
213
|
+
symbology: "ean13",
|
|
214
|
+
title: 'Nutella <script>alert("xss")</script>',
|
|
215
|
+
})
|
|
216
|
+
assert.equal(eanSvg.startsWith("<svg"), true)
|
|
217
|
+
assert.equal(eanSvg.includes("<script>"), false)
|
|
218
|
+
assert.equal(eanSvg.includes("<script>"), true)
|
|
219
|
+
|
|
220
|
+
const qrSvg = renderBarcodeSvg({
|
|
221
|
+
code: "https://store.example.com/products/nutella",
|
|
222
|
+
symbology: "qr",
|
|
223
|
+
})
|
|
224
|
+
assert.equal(qrSvg.startsWith("<svg"), true)
|
|
225
|
+
assert.equal(qrSvg.includes("<path"), true)
|
|
226
|
+
|
|
227
|
+
const dmSvg = renderBarcodeSvg({
|
|
228
|
+
code: "010301762042200310LOT1",
|
|
229
|
+
symbology: "datamatrix",
|
|
230
|
+
})
|
|
231
|
+
assert.equal(dmSvg.startsWith("<svg"), true)
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
it("renders print-ready HTML+SVG and Zebra ZPL II across thermal & A4 presets", () => {
|
|
235
|
+
assert.equal(Object.keys(BUILTIN_LABEL_TEMPLATES).length, 7)
|
|
236
|
+
|
|
237
|
+
const output = renderPrintableLabels(
|
|
238
|
+
[
|
|
239
|
+
{
|
|
240
|
+
barcode: "3017620422003",
|
|
241
|
+
title: "Organic Roast <img src=x onerror=alert(1)>",
|
|
242
|
+
variant_title: "250g Bag",
|
|
243
|
+
sku: "ORG-250",
|
|
244
|
+
brand: "Medusa Coffee",
|
|
245
|
+
price: 12.5,
|
|
246
|
+
compare_at_price: 16.0,
|
|
247
|
+
currency_code: "USD",
|
|
248
|
+
quantity: 3,
|
|
249
|
+
},
|
|
250
|
+
],
|
|
251
|
+
"thermal_57x32"
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
assert.equal(output.total_labels, 3)
|
|
255
|
+
assert.equal(output.html.includes("<!DOCTYPE html>"), true)
|
|
256
|
+
assert.equal(output.html.includes("$12.50"), true)
|
|
257
|
+
assert.equal(output.html.includes("$16.00"), true)
|
|
258
|
+
assert.equal(output.html.includes("<img src=x"), false)
|
|
259
|
+
assert.equal(output.html.includes("<img src=x"), true)
|
|
260
|
+
assert.equal(output.zpl.includes("^XA"), true)
|
|
261
|
+
assert.equal(output.zpl.includes("^XZ"), true)
|
|
262
|
+
assert.equal(output.zpl.includes("^BEN"), true)
|
|
263
|
+
})
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
describe("medusa-plugin-barcode — BarcodeModuleService, HMAC Security & Workflows", () => {
|
|
267
|
+
it("verifies anti-counterfeit HMAC-SHA256 signatures and rejects tampered tokens", async () => {
|
|
268
|
+
const service = new BarcodeModuleService(
|
|
269
|
+
{},
|
|
270
|
+
{ hmacSecret: "unit-test-super-secret-hmac-key-2026" }
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
const tag = await service.createOrUpdateBarcodeTag({
|
|
274
|
+
code: "MPB-AUTH-001",
|
|
275
|
+
target_type: "product",
|
|
276
|
+
target_id: "prod_luxury_watch",
|
|
277
|
+
symbology: "qr",
|
|
278
|
+
scan_action: "verify",
|
|
279
|
+
serial_number: "SN-9001",
|
|
280
|
+
batch_lot: "BATCH-26",
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
assert.equal(typeof tag.hmac_signature, "string")
|
|
284
|
+
const sig = tag.hmac_signature ?? ""
|
|
285
|
+
assert.equal(sig.length, 64)
|
|
286
|
+
|
|
287
|
+
const validCheck = await service.verifyAuthenticityToken(
|
|
288
|
+
"MPB-AUTH-001",
|
|
289
|
+
sig
|
|
290
|
+
)
|
|
291
|
+
assert.equal(validCheck.valid, true)
|
|
292
|
+
assert.equal(validCheck.tag?.scan_count, 1)
|
|
293
|
+
|
|
294
|
+
const tamperedCheck = await service.verifyAuthenticityToken(
|
|
295
|
+
"MPB-AUTH-001",
|
|
296
|
+
"0000000000000000000000000000000000000000000000000000000000000000"
|
|
297
|
+
)
|
|
298
|
+
assert.equal(tamperedCheck.valid, false)
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
it("blocks SSRF private/loopback/non-HTTPS URLs in validateExternalImageUrl", () => {
|
|
302
|
+
assert.equal(
|
|
303
|
+
validateExternalImageUrl("https://images.openfoodfacts.org/front.jpg"),
|
|
304
|
+
"https://images.openfoodfacts.org/front.jpg"
|
|
305
|
+
)
|
|
306
|
+
assert.equal(validateExternalImageUrl("http://localhost:9000/secret"), null)
|
|
307
|
+
assert.equal(validateExternalImageUrl("https://127.0.0.1/admin"), null)
|
|
308
|
+
assert.equal(
|
|
309
|
+
validateExternalImageUrl("https://169.254.169.254/latest/meta-data/"),
|
|
310
|
+
null
|
|
311
|
+
)
|
|
312
|
+
assert.equal(validateExternalImageUrl("https://192.168.1.1/router"), null)
|
|
313
|
+
assert.equal(validateExternalImageUrl("https://10.0.0.1/internal"), null)
|
|
314
|
+
assert.equal(validateExternalImageUrl("javascript:alert(1)"), null)
|
|
315
|
+
assert.equal(
|
|
316
|
+
escapeHtml('<svg onload="alert(1)">'),
|
|
317
|
+
"<svg onload="alert(1)">"
|
|
318
|
+
)
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
it("executes product creation, batch import, bulk variant barcode generation, and stock-take workflows with compensation", async () => {
|
|
322
|
+
const barcodeService = new BarcodeModuleService()
|
|
323
|
+
const registry = await barcodeService.getProviderRegistry()
|
|
324
|
+
registry.register(
|
|
325
|
+
new MockProvider("mock_catalog", 1, {
|
|
326
|
+
name: "Sparkling Mineral Water",
|
|
327
|
+
brand: "Alpine",
|
|
328
|
+
suggested_price: 2.5,
|
|
329
|
+
})
|
|
330
|
+
)
|
|
331
|
+
|
|
332
|
+
const createdMedusaProducts: Array<Record<string, unknown>> = []
|
|
333
|
+
const mockVariants: Array<Record<string, unknown>> = [
|
|
334
|
+
{
|
|
335
|
+
id: "var_101",
|
|
336
|
+
product_id: "prod_101",
|
|
337
|
+
title: "Default",
|
|
338
|
+
barcode: null,
|
|
339
|
+
},
|
|
340
|
+
{ id: "var_102", product_id: "prod_102", title: "Large", barcode: null },
|
|
341
|
+
]
|
|
342
|
+
|
|
343
|
+
const mockProductService = {
|
|
344
|
+
async createProducts(payloads: Array<Record<string, unknown>>) {
|
|
345
|
+
const created = payloads.map((p, i) => ({
|
|
346
|
+
id: `medusa_prod_${createdMedusaProducts.length + i + 1}`,
|
|
347
|
+
...p,
|
|
348
|
+
variants: [
|
|
349
|
+
{ id: `medusa_var_${createdMedusaProducts.length + i + 1}` },
|
|
350
|
+
],
|
|
351
|
+
}))
|
|
352
|
+
createdMedusaProducts.push(...created)
|
|
353
|
+
return created
|
|
354
|
+
},
|
|
355
|
+
async deleteProducts(ids: string[]) {
|
|
356
|
+
const idSet = new Set(ids)
|
|
357
|
+
for (let i = createdMedusaProducts.length - 1; i >= 0; i--) {
|
|
358
|
+
if (idSet.has(String(createdMedusaProducts[i]?.id))) {
|
|
359
|
+
createdMedusaProducts.splice(i, 1)
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
},
|
|
363
|
+
async listProductVariants() {
|
|
364
|
+
return mockVariants
|
|
365
|
+
},
|
|
366
|
+
async upsertProductVariants(updates: Array<Record<string, unknown>>) {
|
|
367
|
+
for (const u of updates) {
|
|
368
|
+
const target = mockVariants.find((v) => v.id === u.id)
|
|
369
|
+
if (target) Object.assign(target, u)
|
|
370
|
+
}
|
|
371
|
+
},
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const container = {
|
|
375
|
+
resolve(key: string) {
|
|
376
|
+
if (key === BARCODE_MODULE) return barcodeService
|
|
377
|
+
if (key === "product") return mockProductService
|
|
378
|
+
throw new Error(`Unknown key ${key}`)
|
|
379
|
+
},
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// 1. lookupAndCreateProductWorkflow
|
|
383
|
+
const singleRun = (await lookupAndCreateProductWorkflow(
|
|
384
|
+
container as never
|
|
385
|
+
).run({
|
|
386
|
+
input: {
|
|
387
|
+
barcode: "3017620422003",
|
|
388
|
+
status: "published",
|
|
389
|
+
},
|
|
390
|
+
})) as unknown as {
|
|
391
|
+
result: {
|
|
392
|
+
product_id: string
|
|
393
|
+
lookup: { product: NormalizedBarcodeProduct | null }
|
|
394
|
+
}
|
|
395
|
+
compensate?: () => Promise<void>
|
|
396
|
+
}
|
|
397
|
+
assert.equal(singleRun.result.product_id, "medusa_prod_1")
|
|
398
|
+
assert.equal(createdMedusaProducts.length, 1)
|
|
399
|
+
|
|
400
|
+
// Test rollback/compensation
|
|
401
|
+
await singleRun.compensate?.()
|
|
402
|
+
assert.equal(createdMedusaProducts.length, 0)
|
|
403
|
+
|
|
404
|
+
// 2. batchBarcodeImportWorkflow (genuinely creates Medusa products!)
|
|
405
|
+
const batchRun = await batchBarcodeImportWorkflow(container as never).run({
|
|
406
|
+
input: {
|
|
407
|
+
barcodes: ["3017620422003", "4006381333931"],
|
|
408
|
+
status: "draft",
|
|
409
|
+
},
|
|
410
|
+
})
|
|
411
|
+
assert.equal(batchRun.result.imported_count, 2)
|
|
412
|
+
assert.equal(createdMedusaProducts.length, 2)
|
|
413
|
+
|
|
414
|
+
// 3. bulkGenerateVariantBarcodesWorkflow
|
|
415
|
+
const bulkGen = await bulkGenerateVariantBarcodesWorkflow(
|
|
416
|
+
container as never
|
|
417
|
+
).run({
|
|
418
|
+
input: { prefix: "205" },
|
|
419
|
+
})
|
|
420
|
+
assert.equal(bulkGen.result.generated_count, 2)
|
|
421
|
+
assert.equal(String(mockVariants[0]?.barcode).startsWith("205"), true)
|
|
422
|
+
assert.equal(validateGs1Checksum(String(mockVariants[0]?.barcode)), true)
|
|
423
|
+
|
|
424
|
+
// 4. Stock-Take Count Session & Scan + Apply Workflows
|
|
425
|
+
const sessionCreated = await barcodeService.createStockCountSession({
|
|
426
|
+
name: "Monthly Warehouse Count",
|
|
427
|
+
location_id: "loc_01",
|
|
428
|
+
expected_items: [
|
|
429
|
+
{
|
|
430
|
+
barcode: "3017620422003",
|
|
431
|
+
title: "Sparkling Mineral Water",
|
|
432
|
+
sku: "GTIN-3017620422003",
|
|
433
|
+
expected_quantity: 10,
|
|
434
|
+
},
|
|
435
|
+
],
|
|
436
|
+
})
|
|
437
|
+
|
|
438
|
+
const scanRun = await scanInventoryCountWorkflow(container as never).run({
|
|
439
|
+
input: {
|
|
440
|
+
session_id: sessionCreated.session.id,
|
|
441
|
+
barcode: "3017620422003",
|
|
442
|
+
set_quantity: 12,
|
|
443
|
+
},
|
|
444
|
+
})
|
|
445
|
+
assert.equal(scanRun.result.item.counted_quantity, 12)
|
|
446
|
+
assert.equal(scanRun.result.item.variance, 2)
|
|
447
|
+
|
|
448
|
+
const applyRun = await applyInventoryCountWorkflow(container as never).run({
|
|
449
|
+
input: {
|
|
450
|
+
session_id: sessionCreated.session.id,
|
|
451
|
+
},
|
|
452
|
+
})
|
|
453
|
+
assert.equal(applyRun.result.session.status, "completed")
|
|
454
|
+
assert.equal(applyRun.result.adjustments[0]?.variance, 2)
|
|
455
|
+
|
|
456
|
+
// 5. generateBarcodeTagWorkflow
|
|
457
|
+
const tagWf = await generateBarcodeTagWorkflow(container as never).run({
|
|
458
|
+
input: {
|
|
459
|
+
target_id: "prod_101",
|
|
460
|
+
symbology: "qr",
|
|
461
|
+
scan_action: "gs1_digital_link",
|
|
462
|
+
gs1_gtin: "3017620422003",
|
|
463
|
+
},
|
|
464
|
+
})
|
|
465
|
+
assert.equal(
|
|
466
|
+
String(tagWf.result.target_url).includes("/01/03017620422003"),
|
|
467
|
+
true
|
|
468
|
+
)
|
|
469
|
+
|
|
470
|
+
// Verify mapToMedusaCreateProduct, mergeNormalizedProducts, middlewares & SDK export
|
|
471
|
+
const merged = mergeNormalizedProducts([singleRun.result.lookup.product!])
|
|
472
|
+
const mapped = mapToMedusaCreateProduct(merged!)
|
|
473
|
+
assert.equal(mapped.variants[0]?.barcode, "3017620422003")
|
|
474
|
+
assert.equal(Array.isArray(middlewares.routes), true)
|
|
475
|
+
assert.equal((middlewares.routes?.length ?? 0) >= 10, true)
|
|
476
|
+
|
|
477
|
+
const sdk = createBarcodeSdk({ baseUrl: "https://api.store.com" })
|
|
478
|
+
assert.equal(
|
|
479
|
+
sdk.store.getSvgUrl("3017620422003", "ean13"),
|
|
480
|
+
"https://api.store.com/store/barcodes/3017620422003?format=svg&symbology=ean13"
|
|
481
|
+
)
|
|
482
|
+
})
|
|
483
|
+
})
|
|
484
|
+
|
|
485
|
+
describe("medusa-plugin-barcode — Admin UI Localization (10 Locales Key Parity)", () => {
|
|
486
|
+
it("exports all 10 locales (ar, de, en, es, fr, it, nl, pl, pt, tr) with 100% key parity", () => {
|
|
487
|
+
const bundles = adminI18n
|
|
488
|
+
const locales = SUPPORTED_UI_LOCALES.map((l) => l.code)
|
|
489
|
+
|
|
490
|
+
assert.equal(locales.length, 10)
|
|
491
|
+
|
|
492
|
+
const flattenKeys = (obj: Record<string, unknown>, prefix = ""): string[] =>
|
|
493
|
+
Object.entries(obj).flatMap(([k, v]) => {
|
|
494
|
+
const nextKey = prefix ? `${prefix}.${k}` : k
|
|
495
|
+
if (v && typeof v === "object" && !Array.isArray(v)) {
|
|
496
|
+
return flattenKeys(v as Record<string, unknown>, nextKey)
|
|
497
|
+
}
|
|
498
|
+
return [nextKey]
|
|
499
|
+
})
|
|
500
|
+
|
|
501
|
+
const enKeys = flattenKeys(
|
|
502
|
+
bundles.en.translation as unknown as Record<string, unknown>
|
|
503
|
+
).toSorted()
|
|
504
|
+
assert.ok(enKeys.length >= 120)
|
|
505
|
+
|
|
506
|
+
for (const localeCode of locales) {
|
|
507
|
+
const entry = bundles[localeCode]
|
|
508
|
+
assert.ok(entry, `Missing locale bundle for ${localeCode}`)
|
|
509
|
+
assert.equal(entry.barcodes, entry.translation)
|
|
510
|
+
const localeKeys = flattenKeys(
|
|
511
|
+
entry.translation as unknown as Record<string, unknown>
|
|
512
|
+
).toSorted()
|
|
513
|
+
assert.deepEqual(
|
|
514
|
+
localeKeys,
|
|
515
|
+
enKeys,
|
|
516
|
+
`Locale "${localeCode}" keys do not match "en"`
|
|
517
|
+
)
|
|
518
|
+
}
|
|
519
|
+
})
|
|
520
|
+
|
|
521
|
+
it("exports Medusa v2 Admin multi-route pages, breadcrumbs, drawer modal, widget, and modular components", async () => {
|
|
522
|
+
const overviewRoute = await import("../admin/routes/barcodes/page")
|
|
523
|
+
const lookupRoute = await import("../admin/routes/barcodes/lookup/page")
|
|
524
|
+
const tagsRoute = await import("../admin/routes/barcodes/tags/page")
|
|
525
|
+
const tagsCreateRoute =
|
|
526
|
+
await import("../admin/routes/barcodes/tags/@create/page")
|
|
527
|
+
const printRoute = await import("../admin/routes/barcodes/print/page")
|
|
528
|
+
const stockTakeRoute =
|
|
529
|
+
await import("../admin/routes/barcodes/stock-take/page")
|
|
530
|
+
const providersRoute =
|
|
531
|
+
await import("../admin/routes/barcodes/providers/page")
|
|
532
|
+
const widget = await import("../admin/widgets/product-barcode-widget")
|
|
533
|
+
const components = await import("../admin/components")
|
|
534
|
+
|
|
535
|
+
for (const route of [
|
|
536
|
+
overviewRoute,
|
|
537
|
+
lookupRoute,
|
|
538
|
+
tagsRoute,
|
|
539
|
+
printRoute,
|
|
540
|
+
stockTakeRoute,
|
|
541
|
+
providersRoute,
|
|
542
|
+
]) {
|
|
543
|
+
assert.equal(typeof route.default, "function")
|
|
544
|
+
assert.equal(route.config.translationNs, "barcodes")
|
|
545
|
+
assert.equal(typeof route.handle.breadcrumb, "function")
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
assert.equal(typeof tagsCreateRoute.default, "function")
|
|
549
|
+
assert.equal(typeof widget.default, "function")
|
|
550
|
+
assert.equal(widget.config.zone, "product.details.after")
|
|
551
|
+
assert.equal(typeof components.BarcodeStudioHeader, "function")
|
|
552
|
+
assert.equal(typeof components.GtinLookupTab, "function")
|
|
553
|
+
assert.equal(typeof components.DynamicQrTagsTab, "function")
|
|
554
|
+
assert.equal(typeof components.LabelPrintCenterTab, "function")
|
|
555
|
+
assert.equal(typeof components.WarehouseStockTakeTab, "function")
|
|
556
|
+
assert.equal(typeof components.ProvidersCacheTab, "function")
|
|
557
|
+
assert.equal(typeof components.ProductBarcodeCard, "function")
|
|
558
|
+
})
|
|
559
|
+
|
|
560
|
+
it("parses weights, dimensions, and category aliases and maps extended Medusa product options", () => {
|
|
561
|
+
assert.equal(parseWeightGrams(250), 250)
|
|
562
|
+
assert.equal(parseWeightGrams("1.5 kg"), 1500)
|
|
563
|
+
assert.equal(parseWeightGrams("16 oz"), 454)
|
|
564
|
+
assert.equal(parseWeightGrams("2 lb"), 907)
|
|
565
|
+
assert.equal(parseWeightGrams("500 ml"), 500)
|
|
566
|
+
assert.equal(parseWeightGrams("75 cl"), 750)
|
|
567
|
+
assert.equal(parseWeightGrams("invalid"), null)
|
|
568
|
+
|
|
569
|
+
const cmDims = parseDimensionMm("10 x 20 x 5 cm")
|
|
570
|
+
assert.deepEqual(cmDims, { length: 100, width: 200, height: 50 })
|
|
571
|
+
|
|
572
|
+
const inDims = parseDimensionMm("4 x 2 x 1 in")
|
|
573
|
+
assert.deepEqual(inDims, { length: 102, width: 51, height: 25 })
|
|
574
|
+
|
|
575
|
+
assert.equal(normalizeCategory("en:beverages"), "Food & Beverages")
|
|
576
|
+
assert.equal(normalizeCategory("snacks"), "Snacks")
|
|
577
|
+
assert.equal(
|
|
578
|
+
normalizeCategory("en:organic-dark-chocolates"),
|
|
579
|
+
"Organic Dark Chocolates"
|
|
580
|
+
)
|
|
581
|
+
assert.equal(typeof CATEGORY_ALIASES.beverages, "string")
|
|
582
|
+
|
|
583
|
+
const mapped = mapToMedusaCreateProduct(
|
|
584
|
+
{
|
|
585
|
+
barcode: "3017620422003",
|
|
586
|
+
barcode_type: "ean13",
|
|
587
|
+
name: "Hazelnut Spread",
|
|
588
|
+
description: "Rich cocoa spread",
|
|
589
|
+
brand: "Ferrero",
|
|
590
|
+
manufacturer: "Ferrero",
|
|
591
|
+
categories: ["en:snacks", "en:sweet-spreads"],
|
|
592
|
+
labels: ["Gluten-free"],
|
|
593
|
+
images: [],
|
|
594
|
+
weight: null,
|
|
595
|
+
weight_unit: null,
|
|
596
|
+
dimensions: "8 x 8 x 12 cm",
|
|
597
|
+
quantity_label: "400 g",
|
|
598
|
+
ingredients: null,
|
|
599
|
+
nutrition: null,
|
|
600
|
+
country_of_origin: "France",
|
|
601
|
+
suggested_price: 4.5,
|
|
602
|
+
currency_code: "EUR",
|
|
603
|
+
isbn: null,
|
|
604
|
+
authors: [],
|
|
605
|
+
publisher: null,
|
|
606
|
+
published_date: null,
|
|
607
|
+
page_count: null,
|
|
608
|
+
provider: "openfoodfacts",
|
|
609
|
+
raw_data: {},
|
|
610
|
+
},
|
|
611
|
+
{
|
|
612
|
+
publishImmediately: true,
|
|
613
|
+
prices: [
|
|
614
|
+
{ amount: 499, currency_code: "eur" },
|
|
615
|
+
{ amount: 549, currency_code: "usd" },
|
|
616
|
+
],
|
|
617
|
+
salesChannelIds: ["sc_web", "sc_pos"],
|
|
618
|
+
categoryIds: ["pcat_food"],
|
|
619
|
+
collectionId: "pcol_featured",
|
|
620
|
+
}
|
|
621
|
+
)
|
|
622
|
+
|
|
623
|
+
assert.equal(mapped.status, "published")
|
|
624
|
+
assert.equal(mapped.weight, 400)
|
|
625
|
+
assert.equal(mapped.length, 80)
|
|
626
|
+
assert.equal(mapped.width, 80)
|
|
627
|
+
assert.equal(mapped.height, 120)
|
|
628
|
+
assert.equal(mapped.collection_id, "pcol_featured")
|
|
629
|
+
assert.deepEqual(mapped.categories, [{ id: "pcat_food" }])
|
|
630
|
+
assert.deepEqual(mapped.sales_channels, [
|
|
631
|
+
{ id: "sc_web" },
|
|
632
|
+
{ id: "sc_pos" },
|
|
633
|
+
])
|
|
634
|
+
assert.deepEqual(mapped.variants[0]?.prices, [
|
|
635
|
+
{ amount: 499, currency_code: "eur" },
|
|
636
|
+
{ amount: 549, currency_code: "usd" },
|
|
637
|
+
])
|
|
638
|
+
})
|
|
639
|
+
|
|
640
|
+
it("supports self-healing existing product checks, event emission, subscriber events, POS variant routes, and migration", async () => {
|
|
641
|
+
const barcodeService = new BarcodeModuleService()
|
|
642
|
+
const registry = await barcodeService.getProviderRegistry()
|
|
643
|
+
registry.register(
|
|
644
|
+
new MockProvider("mock_catalog", 1, {
|
|
645
|
+
name: "Artisan Olive Oil",
|
|
646
|
+
brand: "Mediterranean",
|
|
647
|
+
suggested_price: 12.0,
|
|
648
|
+
})
|
|
649
|
+
)
|
|
650
|
+
|
|
651
|
+
const medusaProducts: Array<Record<string, unknown>> = []
|
|
652
|
+
const medusaVariants: Array<Record<string, unknown>> = [
|
|
653
|
+
{
|
|
654
|
+
id: "var_pos_1",
|
|
655
|
+
product_id: "prod_pos_1",
|
|
656
|
+
title: "500ml Bottle",
|
|
657
|
+
barcode: "4006381333931",
|
|
658
|
+
ean: "4006381333931",
|
|
659
|
+
upc: null,
|
|
660
|
+
},
|
|
661
|
+
{
|
|
662
|
+
id: "var_missing_bc",
|
|
663
|
+
product_id: "prod_pos_1",
|
|
664
|
+
title: "1L Bottle",
|
|
665
|
+
barcode: null,
|
|
666
|
+
ean: null,
|
|
667
|
+
upc: null,
|
|
668
|
+
},
|
|
669
|
+
]
|
|
670
|
+
const emittedEvents: Array<{
|
|
671
|
+
name: string
|
|
672
|
+
data: Record<string, unknown>
|
|
673
|
+
}> = []
|
|
674
|
+
|
|
675
|
+
const mockProductService = {
|
|
676
|
+
async createProducts(payloads: Array<Record<string, unknown>>) {
|
|
677
|
+
const created = payloads.map((p, i) => {
|
|
678
|
+
const prod = {
|
|
679
|
+
id: `medusa_prod_${medusaProducts.length + i + 1}`,
|
|
680
|
+
...p,
|
|
681
|
+
variants: [{ id: `medusa_var_${medusaProducts.length + i + 1}` }],
|
|
682
|
+
}
|
|
683
|
+
return prod
|
|
684
|
+
})
|
|
685
|
+
medusaProducts.push(...created)
|
|
686
|
+
return created
|
|
687
|
+
},
|
|
688
|
+
async listProducts(filter: { id?: string[] }) {
|
|
689
|
+
if (filter?.id) {
|
|
690
|
+
return medusaProducts.filter((p) => filter.id!.includes(String(p.id)))
|
|
691
|
+
}
|
|
692
|
+
return medusaProducts
|
|
693
|
+
},
|
|
694
|
+
async listProductVariants(filter: Record<string, unknown>) {
|
|
695
|
+
if (Array.isArray(filter?.id)) {
|
|
696
|
+
return medusaVariants.filter((v) =>
|
|
697
|
+
(filter.id as string[]).includes(String(v.id))
|
|
698
|
+
)
|
|
699
|
+
}
|
|
700
|
+
return medusaVariants
|
|
701
|
+
},
|
|
702
|
+
async upsertProductVariants(updates: Array<Record<string, unknown>>) {
|
|
703
|
+
for (const u of updates) {
|
|
704
|
+
const target = medusaVariants.find((v) => v.id === u.id)
|
|
705
|
+
if (target) Object.assign(target, u)
|
|
706
|
+
}
|
|
707
|
+
},
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
const mockEventBus = {
|
|
711
|
+
async emit(evt: { name: string; data: Record<string, unknown> }) {
|
|
712
|
+
emittedEvents.push(evt)
|
|
713
|
+
},
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
const container = {
|
|
717
|
+
resolve(key: string) {
|
|
718
|
+
if (key === BARCODE_MODULE) return barcodeService
|
|
719
|
+
if (key === "product") return mockProductService
|
|
720
|
+
if (key === "event_bus") return mockEventBus
|
|
721
|
+
throw new Error(`Unknown key ${key}`)
|
|
722
|
+
},
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
// 1. First run creates the product and emits BARCODE_EVENTS.PRODUCT_CREATED
|
|
726
|
+
const firstCreate = await lookupAndCreateProductWorkflow(
|
|
727
|
+
container as never
|
|
728
|
+
).run({
|
|
729
|
+
input: {
|
|
730
|
+
barcode: "3017620422003",
|
|
731
|
+
publishImmediately: true,
|
|
732
|
+
},
|
|
733
|
+
})
|
|
734
|
+
assert.equal(firstCreate.result.created, true)
|
|
735
|
+
assert.equal(firstCreate.result.existing, false)
|
|
736
|
+
assert.equal(firstCreate.result.product_id, "medusa_prod_1")
|
|
737
|
+
assert.equal(emittedEvents.length, 1)
|
|
738
|
+
assert.equal(emittedEvents[0]?.name, BARCODE_EVENTS.PRODUCT_CREATED)
|
|
739
|
+
|
|
740
|
+
// 2. Second run returns the existing product without duplicating
|
|
741
|
+
const secondCreate = await lookupAndCreateProductWorkflow(
|
|
742
|
+
container as never
|
|
743
|
+
).run({
|
|
744
|
+
input: {
|
|
745
|
+
barcode: "3017620422003",
|
|
746
|
+
},
|
|
747
|
+
})
|
|
748
|
+
assert.equal(secondCreate.result.created, false)
|
|
749
|
+
assert.equal(secondCreate.result.existing, true)
|
|
750
|
+
assert.equal(secondCreate.result.product_id, "medusa_prod_1")
|
|
751
|
+
assert.equal(medusaProducts.length, 1)
|
|
752
|
+
|
|
753
|
+
// 3. Simulate deleting the product in Medusa -> self-heals stale link and recreates
|
|
754
|
+
medusaProducts.length = 0
|
|
755
|
+
const thirdCreate = await lookupAndCreateProductWorkflow(
|
|
756
|
+
container as never
|
|
757
|
+
).run({
|
|
758
|
+
input: {
|
|
759
|
+
barcode: "3017620422003",
|
|
760
|
+
},
|
|
761
|
+
})
|
|
762
|
+
assert.equal(thirdCreate.result.created, true)
|
|
763
|
+
assert.equal(thirdCreate.result.existing, false)
|
|
764
|
+
assert.equal(medusaProducts.length, 1)
|
|
765
|
+
|
|
766
|
+
// 4. Subscriber handles product.created (product_ids) and creates BarcodeTag for existing barcode too
|
|
767
|
+
const subscriberMod = await import("../subscribers/product-created")
|
|
768
|
+
await subscriberMod.default({
|
|
769
|
+
event: { name: "product.created", data: { id: "prod_pos_1" } },
|
|
770
|
+
container: container as never,
|
|
771
|
+
pluginOptions: {},
|
|
772
|
+
})
|
|
773
|
+
// var_missing_bc now has a generated 200-prefix barcode
|
|
774
|
+
assert.equal(String(medusaVariants[1]?.barcode).startsWith("200"), true)
|
|
775
|
+
// var_pos_1 already had barcode 4006381333931 and now has a BarcodeTag created for it
|
|
776
|
+
const existingTag = await barcodeService.listBarcodeTags({
|
|
777
|
+
code: "4006381333931",
|
|
778
|
+
})
|
|
779
|
+
assert.equal(existingTag.length, 1)
|
|
780
|
+
|
|
781
|
+
// 5. POS/Storefront variant barcode lookup routes
|
|
782
|
+
const adminVariantRoute =
|
|
783
|
+
await import("../api/admin/variants/barcode/[code]/route")
|
|
784
|
+
const storeVariantRoute =
|
|
785
|
+
await import("../api/store/variants/barcode/[code]/route")
|
|
786
|
+
|
|
787
|
+
let statusCode = 200
|
|
788
|
+
let jsonPayload: Record<string, unknown> | null = null
|
|
789
|
+
const mockRes = {
|
|
790
|
+
status(code: number) {
|
|
791
|
+
statusCode = code
|
|
792
|
+
return this
|
|
793
|
+
},
|
|
794
|
+
json(body: Record<string, unknown>) {
|
|
795
|
+
jsonPayload = body
|
|
796
|
+
return this
|
|
797
|
+
},
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
await adminVariantRoute.GET(
|
|
801
|
+
{
|
|
802
|
+
scope: container,
|
|
803
|
+
params: { code: "4006381333931" },
|
|
804
|
+
} as never,
|
|
805
|
+
mockRes as never
|
|
806
|
+
)
|
|
807
|
+
assert.equal(statusCode, 200)
|
|
808
|
+
assert.equal(jsonPayload?.found, true)
|
|
809
|
+
assert.equal(
|
|
810
|
+
(jsonPayload?.variant as { id?: string } | null)?.id,
|
|
811
|
+
"var_pos_1"
|
|
812
|
+
)
|
|
813
|
+
|
|
814
|
+
await storeVariantRoute.GET(
|
|
815
|
+
{
|
|
816
|
+
scope: container,
|
|
817
|
+
params: { code: "9999999999999" },
|
|
818
|
+
} as never,
|
|
819
|
+
mockRes as never
|
|
820
|
+
)
|
|
821
|
+
assert.equal(statusCode, 404)
|
|
822
|
+
|
|
823
|
+
// 6. Verify Migration20260522000000
|
|
824
|
+
const { Migration20260522000000 } =
|
|
825
|
+
await import("../modules/barcode/migrations/Migration20260522000000")
|
|
826
|
+
const migration = new Migration20260522000000() as unknown as {
|
|
827
|
+
up: () => Promise<void>
|
|
828
|
+
down: () => Promise<void>
|
|
829
|
+
queries: string[]
|
|
830
|
+
}
|
|
831
|
+
await migration.up()
|
|
832
|
+
assert.equal(migration.queries.length >= 18, true)
|
|
833
|
+
await migration.down()
|
|
834
|
+
assert.equal(migration.queries.length >= 24, true)
|
|
835
|
+
})
|
|
836
|
+
})
|