import test from "node:test"; import assert from "node:assert/strict"; import { parseChannelForm, CHANNEL_FORM_FIELDS } from "./parseChannelForm"; import { applyChannelFormPatch, channelConfigToFormData, validateChannelFormPatch, } from "./channelConfigToForm"; // Run with: // pnpm -C editor exec tsx --test "app/channels/components/parseChannelForm.test.ts" // // The Configure form's recorded-date field (release 20 D1): parsed into // `recordedDate.titlePattern`, refused when it could not be a rule, round-tripped // through the ops patch, and cleared by an empty value. const ISO = String.raw`(?\d{4})-(?\d{2})-(?\d{2})`; function form(fields: Record): FormData { const fd = new FormData(); fd.set("name", "Example VODs"); fd.set("handling", "youtube"); for (const [k, v] of Object.entries(fields)) fd.set(k, v); return fd; } test("the form writes recordedDate.titlePattern, trimmed", () => { const { config } = parseChannelForm(form({ recordedDateTitlePattern: ` ${ISO} ` })); assert.deepEqual(config.recordedDate, { titlePattern: ISO }); assert.equal("recordedDate" in parseChannelForm(form({})).config, false); // A form save clears a stored rule the form left blank. assert.ok((CHANNEL_FORM_FIELDS as readonly string[]).includes("recordedDate")); }); test("a pattern that cannot be a rule is refused with why", () => { assert.throws( () => parseChannelForm(form({ recordedDateTitlePattern: String.raw`(?\d{4})` })), /Recorded-date pattern must name the groups year, month and day \(missing: month, day\)/, ); assert.throws( () => parseChannelForm(form({ recordedDateTitlePattern: "(?[" })), /Recorded-date pattern is not a valid regular expression/, ); assert.throws( () => parseChannelForm( form({ recordedDateTitlePattern: String.raw`(?(\d+)+)-(?\d+)-(?\d+)` }), ), /Recorded-date pattern contains a nested quantifier/, ); }); test("the ops patch sets and clears it over the channel's current form", () => { validateChannelFormPatch({ recordedDateTitlePattern: ISO }); const fd = channelConfigToFormData({ handling: "youtube", name: "Example VODs" }); applyChannelFormPatch(fd, { recordedDateTitlePattern: ISO }); const set = parseChannelForm(fd).config; assert.deepEqual(set.recordedDate, { titlePattern: ISO }); const back = channelConfigToFormData(set); assert.equal(back.get("recordedDateTitlePattern"), ISO); applyChannelFormPatch(back, { recordedDateTitlePattern: "" }); assert.equal("recordedDate" in parseChannelForm(back).config, false); });