39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
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" },
|
|
]);
|
|
});
|
|
});
|