132 lines
3.9 KiB
TypeScript
132 lines
3.9 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
barcodePrintGuidance,
|
|
createPrintSizedSvg,
|
|
createBarcodeZip,
|
|
parseBatch,
|
|
renderBarcodeSvg,
|
|
} from "../../src/barcode/generate";
|
|
import {
|
|
buildStructuredPayload,
|
|
gtinCheckDigit,
|
|
validateGtin,
|
|
} from "../../src/barcode/payloads";
|
|
import { inspectGs1 } from "../../src/barcode/gs1";
|
|
|
|
describe("barcode generation", () => {
|
|
it("renders inert scalable QR output", () => {
|
|
const result = renderBarcodeSvg({
|
|
format: "qrcode",
|
|
text: "https://example.com",
|
|
scale: 3,
|
|
padding: 8,
|
|
includeText: false,
|
|
});
|
|
expect(result.svg).toMatch(/^<svg/u);
|
|
expect(result.svg).not.toMatch(/<script|\bon\w+=|href=/iu);
|
|
expect(result.width).toBeGreaterThan(0);
|
|
});
|
|
|
|
it("creates deterministic named batch archives", () => {
|
|
const rows = parseBatch('first,hello\n"second",world');
|
|
const first = createBarcodeZip(rows, {
|
|
format: "qrcode",
|
|
scale: 2,
|
|
padding: 8,
|
|
includeText: false,
|
|
});
|
|
const second = createBarcodeZip(rows, {
|
|
format: "qrcode",
|
|
scale: 2,
|
|
padding: 8,
|
|
includeText: false,
|
|
});
|
|
expect(first).toEqual(second);
|
|
});
|
|
|
|
it("exports physical SVG dimensions and reports honest quiet-zone guidance", () => {
|
|
const barcode = renderBarcodeSvg({
|
|
format: "qrcode",
|
|
text: "print plan",
|
|
scale: 3,
|
|
padding: 2,
|
|
includeText: false,
|
|
});
|
|
const plan = barcodePrintGuidance(
|
|
barcode,
|
|
{ format: "qrcode", padding: 2 },
|
|
50,
|
|
300,
|
|
);
|
|
expect(plan.quietZoneAssessment).toBe("manual-measurement-required");
|
|
expect(plan.suppliedPaddingPoints).toBe(2);
|
|
expect(plan.minimumQuietZoneModules).toBe(4);
|
|
expect(plan.rasterWidthPixels).toBe(591);
|
|
expect(createPrintSizedSvg(barcode, 50)).toMatch(
|
|
/^<svg width="50mm" height="[\d.]+mm" viewBox=/u,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("structured payloads", () => {
|
|
it("escapes Wi-Fi delimiter characters", () =>
|
|
expect(
|
|
buildStructuredPayload("wifi", {
|
|
primary: "lab;guest",
|
|
secondary: "p:a\\ss",
|
|
tertiary: "WPA",
|
|
hidden: true,
|
|
}),
|
|
).toBe("WIFI:T:WPA;S:lab\\;guest;P:p\\:a\\\\ss;H:true;;"));
|
|
it("rejects injected or unsupported Wi-Fi security fields", () => {
|
|
expect(() =>
|
|
buildStructuredPayload("wifi", {
|
|
primary: "lab",
|
|
tertiary: "WPA;S:other",
|
|
}),
|
|
).toThrow(/WPA, WEP, or nopass/u);
|
|
});
|
|
it("calculates and validates GTIN check digits", () => {
|
|
expect(gtinCheckDigit("400638133393")).toBe(1);
|
|
expect(validateGtin("4006381333931")).toEqual({
|
|
valid: true,
|
|
expected: "1",
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("GS1 element inspection", () => {
|
|
it("parses parenthesized HRI with checks, dates and variable fields", () => {
|
|
const result = inspectGs1(
|
|
"(01)04006381333931(17)271231(10)LOT-42(21)SERIAL-7",
|
|
);
|
|
expect(result.errors).toEqual([]);
|
|
expect(result.elements.map((item) => item.ai)).toEqual([
|
|
"01",
|
|
"17",
|
|
"10",
|
|
"21",
|
|
]);
|
|
expect(result.normalizedElementString).toContain("LOT-42\u001d21");
|
|
});
|
|
|
|
it("parses raw fields using GS separators and reports a bad check digit", () => {
|
|
const result = inspectGs1("]C10104006381333930\u001d10LOT\u001d21SERIAL");
|
|
expect(result.symbologyIdentifier).toBe("]C1");
|
|
expect(result.elements[0]?.valid).toBe(false);
|
|
expect(result.errors.join(" ")).toMatch(/Check digit should be 1/u);
|
|
});
|
|
|
|
it("interprets decimal indicator AIs without changing the source", () => {
|
|
const result = inspectGs1("(3102)001234(3932)978001234");
|
|
expect(result.elements[0]?.interpretation).toBe("12.34");
|
|
expect(result.elements[1]?.interpretation).toBe("978 12.34");
|
|
});
|
|
|
|
it("stops rather than guessing an unknown raw AI boundary", () => {
|
|
const result = inspectGs1("8912345");
|
|
expect(result.elements).toEqual([]);
|
|
expect(result.errors.join(" ")).toMatch(/Unknown AI/u);
|
|
});
|
|
});
|