71 lines
1.8 KiB
TypeScript
71 lines
1.8 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
createBarcodeZip,
|
|
parseBatch,
|
|
renderBarcodeSvg,
|
|
} from "../../src/barcode/generate";
|
|
import {
|
|
buildStructuredPayload,
|
|
gtinCheckDigit,
|
|
validateGtin,
|
|
} from "../../src/barcode/payloads";
|
|
|
|
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);
|
|
});
|
|
});
|
|
|
|
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",
|
|
});
|
|
});
|
|
});
|