import { describe, expect, it } from "vitest";
import { parseICalendarBytes } from "@/worker/caldav/ical/parse";
import { parseVCardBytes } from "@/worker/carddav/vcard";
import { utf8ArrayBuffer } from "@tests/worker/helpers/bytes";
import { extractDeadPropertyFragment } from "@/worker/xml/dead-props";
import { parseSafeXml } from "@/worker/xml/parser";
describe("parser compatibility checks", () => {
it("preserves namespace URI and local name for dead property fragments", () => {
const fragment = extractDeadPropertyFragment('blue');
expect(fragment).toEqual({
nsUri: "urn:example:test",
localName: "favorite",
xmlValue: 'blue',
});
});
it("rejects unsafe entity declarations", () => {
expect(() => parseSafeXml("]>&y;")).toThrow(
"Unsafe XML entity declarations are not accepted",
);
});
it("rejects structurally hostile XML below the byte limit", () => {
const deep = `${"".repeat(129)}${"".repeat(129)}`;
expect(() => parseSafeXml(deep)).toThrow();
const many = `${"".repeat(50_001)}`;
expect(() => parseSafeXml(many, 1_000_000)).toThrow("element count limit");
});
it("imports bounded calendar and contact parsers in the Worker runtime", () => {
const calendar = [
"BEGIN:VCALENDAR",
"VERSION:2.0",
"PRODID:-//dab//parser-check//EN",
"BEGIN:VEVENT",
"UID:calendar-parser-check",
"DTSTAMP:20260521T000000Z",
"DTSTART:20260521T000000Z",
"END:VEVENT",
"END:VCALENDAR",
].join("\r\n");
const contact = ["BEGIN:VCARD", "VERSION:4.0", "UID:contact-parser-check", "FN:Parser Check", "END:VCARD"].join(
"\r\n",
);
expect(parseICalendarBytes(utf8ArrayBuffer(calendar), 4096, { maxYears: 1, maxInstances: 10 }).uid).toBe(
"calendar-parser-check",
);
expect(parseVCardBytes(utf8ArrayBuffer(contact), 4096).uid).toBe("contact-parser-check");
});
});