Release Label Tools 0.1.0

This commit is contained in:
2026-09-01 13:05:57 +02:00
commit 4e3df8d4a4
64 changed files with 10953 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
import { describe, expect, it } from "vitest";
import { augmentRecords, parseMergeData } from "../../src/labels/data";
describe("label merge data", () => {
it("parses quoted CSV and bounded JSON records", () => {
expect(
parseMergeData('name,address\n"Ada, A.","Line 1\nLine 2"', "csv"),
).toEqual([{ name: "Ada, A.", address: "Line 1\nLine 2" }]);
expect(
parseMergeData('[{"name":"Grace","meta":{"team":"Compiler"}}]', "json"),
).toEqual([{ name: "Grace", "meta.team": "Compiler" }]);
});
it("rejects duplicate CSV headers and dangerous JSON keys", () => {
expect(() => parseMergeData("name,name\na,b", "csv")).toThrow(/unique/iu);
expect(() => parseMergeData('[{"__proto__":{"x":1}}]', "json")).toThrow(
/Dangerous JSON/iu,
);
});
it("adds deterministic serials and injected secure random bytes", () => {
const records = augmentRecords(
[{ name: "Ada" }],
{
count: 2,
serialPrefix: "A-",
serialStart: 7,
serialPadding: 3,
randomLength: 4,
},
(length) => new Uint8Array(length).fill(0),
);
expect(records).toEqual([
{ name: "Ada", __row: "1", __serial: "A-007", __random: "AAAA" },
{ __row: "2", __serial: "A-008", __random: "AAAA" },
]);
});
});
+70
View File
@@ -0,0 +1,70 @@
import { describe, expect, it } from "vitest";
import { renderLabelPages } from "../../src/labels/render";
import {
STOCKS,
fromMillimetres,
toMillimetres,
} from "../../src/labels/stocks";
import type { RenderOptions } from "../../src/labels/types";
function options(): RenderOptions {
return {
stock: STOCKS[0]!,
calibration: { offsetXmm: 0, offsetYmm: 0, scaleX: 1, scaleY: 1 },
layout: "asset",
mapping: {
title: "name",
subtitle: "serial",
body: "body",
barcode: "code",
image: "image",
},
barcodeFormat: "qrcode",
includeBarcodeText: false,
fontFamily: "Arial, Helvetica, sans-serif",
records: [
{
name: "<script>alert(1)</script>",
serial: "A-1",
body: "local",
code: "https://example.test/inert",
image: "",
},
],
images: [],
defaultImage: "",
cutMarks: true,
registrationMarks: true,
startPosition: 2,
};
}
describe("label SVG renderer", () => {
it("creates a physical-size inert SVG and escapes merged text", () => {
const result = renderLabelPages(options());
expect(result.pages).toHaveLength(1);
expect(result.pages[0]).toContain('width="210mm"');
expect(result.pages[0]).toContain("&lt;script&gt;alert");
expect(result.pages[0]).not.toContain("<script>");
expect(result.pages[0]).toContain("clip-path");
});
it("paginates with a start position and caps preview rendering", () => {
const input = options();
input.records = Array.from({ length: 60 }, (_, index) => ({
name: String(index),
code: String(index),
}));
expect(renderLabelPages(input, 2)).toMatchObject({
totalPages: 3,
totalLabels: 60,
});
expect(renderLabelPages(input, 2).pages).toHaveLength(2);
});
it("converts mm, inches and points round-trip", () => {
expect(toMillimetres(1, "in")).toBeCloseTo(25.4);
expect(fromMillimetres(25.4, "pt")).toBeCloseTo(72);
expect(toMillimetres(fromMillimetres(42, "in"), "in")).toBeCloseTo(42);
});
});