// drafted.mjs (release 28, M0): the marks, the one draft writer, the // operator's edit, orphans, and the validators — over the S0 kit's `full` // manifest (every media field a draft task writes). import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { applyDraft, clearEditedDrafts, draftedFields, draftedOf, fieldKey, makeProvenance, orphanDrafts, ownerOf, parseReviewAddress, pruneDrafted, reviewAddress, validateDrafted, publishWarnings, validatePublish, } from "./drafted.mjs"; const full = () => JSON.parse(readFileSync(new URL("../../testing/draft/manifests/full.video.manifest.json", import.meta.url), "utf8")); const P = (o: Partial<{ task: string; attempt: number }> = {}) => makeProvenance({ task: o.task ?? "onscreen", model: "qwen3:8b", at: "2026-10-13T10:00:00.000Z", iteration: 2, attempt: o.attempt ?? 1 }); const entry = (m: { timeline: Record[] }, id: string) => m.timeline.find((e) => e.id === id)!; test("applyDraft: an empty field is drafted and marked; the operator's is never replaced, force or not", () => { const m = full(); // c02 has no on-screen text: a draft fills it and says so. assert.deepEqual(applyDraft(m, { entry: "c02", field: "onscreen", value: { title: "The oven" }, provenance: P() }), { applied: true }); assert.deepEqual(entry(m, "c02").onscreen, { title: "The oven" }); assert.deepEqual(entry(m, "c02").drafted, { onscreen: { task: "onscreen", model: "qwen3:8b", at: "2026-10-13T10:00:00.000Z", iteration: 2, attempt: 1 } }); assert.equal(ownerOf(m, { entry: "c02", field: "onscreen" }), "drafted"); // c01's was written by hand: kept, with or without force. const before = JSON.stringify(entry(m, "c01")); assert.deepEqual(applyDraft(m, { entry: "c01", field: "onscreen", value: { title: "X" }, provenance: P() }), { applied: false, reason: "operator" }); assert.deepEqual(applyDraft(m, { entry: "c01", field: "onscreen", value: { title: "X" }, provenance: P(), force: true }), { applied: false, reason: "operator" }); assert.equal(JSON.stringify(entry(m, "c01")), before); // A draft is re-drafted only with force. assert.deepEqual(applyDraft(m, { entry: "c02", field: "onscreen", value: { title: "Again" }, provenance: P({ attempt: 2 }) }), { applied: false, reason: "drafted" }); assert.deepEqual(applyDraft(m, { entry: "c02", field: "onscreen", value: { title: "Again" }, provenance: P({ attempt: 2 }), force: true }), { applied: true }); assert.equal(entry(m, "c02").onscreen.title, "Again"); assert.equal(entry(m, "c02").drafted.onscreen.attempt, 2); // No such entry; flips with no deck to hold them. assert.deepEqual(applyDraft(m, { entry: "nope", field: "onscreen", value: { title: "x" }, provenance: P() }), { applied: false, reason: "missing" }); const bare = { timeline: [] as unknown[] }; assert.deepEqual(applyDraft(bare, { field: "render.chrome.flips", value: { pairs: [{}] }, provenance: P({ task: "flips" }) }), { applied: false, reason: "missing" }); // A field in the wrong place is the task's bug, thrown. assert.throws(() => applyDraft(m, { entry: "c01", field: "narration", value: { text: "x" }, provenance: P() }), /narration is drafted only on a card/); assert.throws(() => applyDraft(m, { field: "title", value: "x", provenance: P() }), /not a manifest field/); assert.throws(() => applyDraft(m, { entry: "c03", field: "onscreen", value: { title: "x" }, provenance: { ...P(), attempt: 3 } as never }), /attempt must be 1 or 2/); }); test("applyDraft: manifest fields — the teaser's lines on the entry, publish.youtube made, the cut's order; marks in the top-level map", () => { const m = full(); // The fixture's teaser lines are hand-written: the operator's. assert.deepEqual(applyDraft(m, { entry: "fin", field: "lines", value: ["a"], provenance: P({ task: "teaser" }), force: true }), { applied: false, reason: "operator" }); assert.deepEqual(applyDraft(m, { field: "publish.youtube", value: { title: "T", description: "D" }, provenance: P({ task: "youtube" }) }), { applied: true }); assert.deepEqual(m.publish, { youtube: { title: "T", description: "D" } }); assert.equal(m.drafted["publish.youtube"].task, "youtube"); // The threads exist and are the operator's. assert.deepEqual(applyDraft(m, { field: "render.chrome.threads", value: { list: [] }, provenance: P({ task: "threads" }), force: true }), { applied: false, reason: "operator" }); // A cut's timeline with no mark is the operator's (M1 decides what the converter's order counts as). assert.equal(ownerOf(m, { field: "timeline" }), "operator"); assert.deepEqual(validateDrafted(m), []); assert.deepEqual(validatePublish(m), []); assert.deepEqual(draftedFields(m).map((d) => [d.entry, d.field]), [[null, "publish.youtube"]]); }); test("an operator's edit clears the mark; an untouched field keeps it; each field by its own signature", () => { const m = full(); applyDraft(m, { entry: "c02", field: "onscreen", value: { title: "The oven" }, provenance: P() }); m.drafted = { timeline: P({ task: "cut" }), "render.chrome.threads": P({ task: "threads" }), "publish.youtube": P({ task: "youtube" }), }; m.publish = { youtube: { title: "T", description: "D" } }; entry(m, "the-contest").drafted = { narration: P({ task: "narrate" }) }; entry(m, "fin").drafted = { lines: P({ task: "teaser" }) }; const base = JSON.parse(JSON.stringify(m)); // Each field edited alone clears only its own mark. const edits: [string, (x: any) => void, string | null, string][] = [ ["onscreen", (x) => (entry(x, "c02").onscreen = { title: "Hand" }), "c02", "onscreen"], ["narration", (x) => (entry(x, "the-contest").narration.text = "Mine now."), "the-contest", "narration"], ["lines", (x) => (entry(x, "fin").lines = ["One", "Two", "Three"]), "fin", "lines"], ["order", (x) => x.timeline.splice(3, 0, x.timeline.splice(2, 1)[0]), null, "timeline"], ["a clip's thread", (x) => (entry(x, "c02").thread = "contest"), null, "render.chrome.threads"], ["youtube", (x) => (x.publish.youtube.title = "Mine"), null, "publish.youtube"], ]; for (const [what, edit, id, field] of edits) { const after = JSON.parse(JSON.stringify(base)); edit(after); const cleared = clearEditedDrafts(base, after); assert.deepEqual(cleared, [{ entry: id, field }], what); assert.equal(draftedOf(after, { entry: id, field }), null, what); assert.equal(ownerOf(after, { entry: id, field }), "operator", what); assert.equal(draftedFields(after).length, draftedFields(base).length - 1, `${what}: the others keep theirs`); } // An edit elsewhere in the entry (its on-screen text) leaves the cut's order and the narration drafted. const same = JSON.parse(JSON.stringify(base)); entry(same, "c01").onscreen.title = "Edited"; assert.deepEqual(clearEditedDrafts(base, same), []); // A mark the write itself made (a draft applied in it) is the writer's, kept. const fresh = JSON.parse(JSON.stringify(base)); applyDraft(fresh, { entry: "c03", field: "onscreen", value: { title: "New" }, provenance: P() }); assert.deepEqual(clearEditedDrafts(base, fresh), []); assert.equal(draftedOf(fresh, { entry: "c03", field: "onscreen" })?.task, "onscreen"); }); test("orphans: a mark whose field is gone is listed, then dropped with a sentence; validation does not refuse it", () => { const m = full(); entry(m, "c03").drafted = { onscreen: P() }; m.drafted = { "render.chrome.flips": P({ task: "flips" }) }; assert.deepEqual(validateDrafted(m), []); assert.deepEqual(orphanDrafts(m).map((o) => o.where), ["timeline[4] (c03).drafted.onscreen", 'drafted["render.chrome.flips"]']); const said = pruneDrafted(m); assert.equal(said.length, 2); assert.match(said[0]!, /its field is gone, so the drafted mark is dropped/); assert.equal(entry(m, "c03").drafted, undefined); assert.equal(m.drafted, undefined); assert.deepEqual(orphanDrafts(m), []); }); test("validateDrafted: a mark where no field may be drafted, or not a provenance, is refused", () => { const m = full(); entry(m, "c01").drafted = { onscreen: P(), quote: P() }; entry(m, "title").drafted = { lines: P() }; entry(m, "c02").drafted = { narration: P() }; entry(m, "c04").drafted = { onscreen: { task: "", model: "m", at: "yesterday", attempt: 1, extra: true } }; entry(m, "c05").onscreen = { title: "x", drafted: P() } as never; m.drafted = { title: P(), timeline: P({ task: "cut" }) }; m.render.drafted = {}; const errors = validateDrafted(m); for (const re of [ /\(c01\)\.drafted\.quote is not an entry field a draft may set/, /\(title\)\.drafted\.lines: a card has no lines to draft \(only a teaser\)/, /\(c02\)\.drafted\.narration: a clip has no narration to draft \(only a card\)/, /\(c04\)\.drafted\.onscreen\.extra is not a provenance key/, /\(c04\)\.drafted\.onscreen\.task must be a name/, /\(c04\)\.drafted\.onscreen\.at must be an ISO date and time/, /\(c05\)\.onscreen\.drafted: the mark goes on the entry/, /drafted\["title"\] is not a manifest field a draft may set/, /render\.drafted: a manifest field's mark goes in the top-level drafted map/, ]) { assert.ok(errors.some((e) => re.test(e)), `${re}\n${errors.join("\n")}`); } assert.equal(errors.length, 9, errors.join("\n")); }); test("validatePublish: publish.youtube is two strings, YouTube's limits; nothing else under publish", () => { assert.deepEqual(validatePublish({}), []); assert.deepEqual(validatePublish({ publish: { youtube: { title: "T", description: "" } } }), []); const bad = validatePublish({ publish: { youtube: { title: "a\nb", description: 3, tags: [] }, tiktok: 1 } }); assert.deepEqual(bad, [ "publish.tiktok is not a publish key (youtube, x, bluesky, videoUrl)", "publish.youtube.tags is not a YouTube field (title, description)", "publish.youtube.description must be a string", "publish.youtube.title must be one line", ]); assert.match(validatePublish({ publish: { youtube: { title: "t".repeat(101), description: "" } } })[0]!, /101 characters \(at most 100\)/); assert.deepEqual(validatePublish({ publish: [] }), ["publish must be an object ({ youtube, x, bluesky, videoUrl })"]); }); test("publishWarnings (release 29 G2): the social posts within their platform's count, a thread of at most 4, the video's URL — warnings, never validatePublish's errors", () => { const link = "https://archilyzer.example/articles/synth-basic/"; const fine = { publish: { x: { post: `A post.\n\n${link}` }, bluesky: { post: "A post.", thread: ["Two.", "Three."] }, videoUrl: "https://video.example/v/1" } }; assert.deepEqual(publishWarnings(fine), []); assert.deepEqual(validatePublish(fine), []); // X: a URL weighs 23 whatever its length, so 255 characters and a long link is 280. assert.deepEqual(publishWarnings({ publish: { x: { post: `${"a".repeat(255)}\n\n${link}${"z".repeat(80)}` } } }), []); const long = { publish: { x: { post: `${"a".repeat(256)}\n\n${link}` } } }; assert.deepEqual(publishWarnings(long), ["publish.x.post is 281 characters as X counts them (at most 280)"]); assert.deepEqual(validatePublish(long), [], "a post past its count does not refuse the build"); // Bluesky counts graphemes: 300 flags are 300, not 600 code points. assert.deepEqual(publishWarnings({ publish: { bluesky: { post: "🇫🇷".repeat(300) } } }), []); assert.deepEqual(publishWarnings({ publish: { bluesky: { post: "🇫🇷".repeat(301) } } }), ["publish.bluesky.post is 301 graphemes (at most 300)"]); const bad = { publish: { x: { post: "", thread: ["a", "b", "c", "d", "e"], tags: 1 }, videoUrl: "video" } }; assert.deepEqual(publishWarnings(bad), [ "publish.videoUrl must be the video's http(s) URL", "publish.x.tags is not a social post field (post, thread)", "publish.x.post must be a post (a string)", "publish.x.thread has 5 posts (at most 4)", ]); assert.deepEqual(publishWarnings(bad, { only: "videoUrl" }), ["publish.videoUrl must be the video's http(s) URL"]); assert.deepEqual(validatePublish(bad), []); }); test("review addresses and keys: one address per field, a key that moves with the value", () => { assert.equal(reviewAddress({ entry: "k1", field: "narration" }), "narrate/k1/narration"); assert.equal(reviewAddress({ field: "render.chrome.flips" }), "flips/*/render.chrome.flips"); assert.equal(reviewAddress({ entry: "c01", field: "onscreen" }), "onscreen/c01/onscreen"); assert.deepEqual(parseReviewAddress("teaser/fin/lines"), { task: "teaser", entry: "fin", field: "lines" }); assert.deepEqual(parseReviewAddress("cut/*/timeline"), { task: "cut", entry: null, field: "timeline" }); assert.equal(parseReviewAddress("no"), null); const m = full(); const k = fieldKey(m, { entry: "c01", field: "onscreen" }); assert.equal(fieldKey(JSON.parse(JSON.stringify(m)), { entry: "c01", field: "onscreen" }), k, "same value, same key"); // Key order in the object does not matter. entry(m, "c01").onscreen = { subtitle: entry(m, "c01").onscreen.subtitle, title: entry(m, "c01").onscreen.title }; assert.equal(fieldKey(m, { entry: "c01", field: "onscreen" }), k); entry(m, "c01").onscreen.title = "Edited"; assert.notEqual(fieldKey(m, { entry: "c01", field: "onscreen" }), k); });