// MK43: an edit is written back where each task is written (src/source.ts), however the brief composes its plan: // inline, a plan.ts it imports, or modules of its own; what code makes is refused, naming its file and line. import { describe, expect, test } from "vitest" import { applyEdits } from "../src/edit.ts" import type { PlanFile } from "../src/doc.ts" import { registerPlan, writeBack, type Source } from "../src/source.ts" const NOW = new Date(2026, 10, 29, 9, 30) const opts = { now: NOW, newId: () => "t-new00001" } /** the edits on `plan` (as the brief's run gives it), written back into `sources` */ function edit(sources: Source[], plan: PlanFile | undefined, edits: Parameters[1]) { const r = applyEdits(plan, edits, opts) return { ...writeBack(sources, plan, r.file), file: r.file } } describe("MK43: an edit written back where the task is written", () => { test("a plan.ts of plain values: the plan's literal rewritten, every line around it kept", () => { const text = `// the shots, by hand import { plan } from "raea/task" export default plan({ tasks: [{ id: "t-door", keyword: "TODO", title: "Light the door" }], }) // after ` const plan: PlanFile = { tasks: [{ id: "t-door", keyword: "TODO", title: "Light the door" }] } const r = edit([{ file: "plan.ts", text }], plan, [{ op: "state", ref: "t-door", state: "done" }]) expect(r.changed).toHaveLength(1) expect(r.changed[0].text.startsWith('// the shots, by hand\nimport { plan } from "raea/task"\n\nexport default plan({\n tasks: [\n {\n id: "t-door",\n keyword: "DONE",\n title: "Light the door",\n closed: "2026-11-29 09:30",')).toBe(true) expect(r.changed[0].text).toEndWith("})\n// after\n") }) test("a plan inline in brief.ts, beside a contract made by calls: only the plan's literal changes", () => { const text = `import { brief, cut } from "raea/brief" export default brief({ title: "Spring", goal: cut({ deliverables: ["spring_9x16.mp4"] }), plan: plan({ tasks: [{ id: "t-cut", keyword: "TODO", title: "First cut" }] }), }) ` const plan: PlanFile = { tasks: [{ id: "t-cut", keyword: "TODO", title: "First cut" }] } const r = edit([{ file: "brief.ts", text }], plan, [{ op: "add", title: "Sound pass" }]) const out = r.changed[0].text expect(out).toContain('goal: cut({ deliverables: ["spring_9x16.mp4"] }),') expect(out).toContain(' plan: plan({\n tasks: [\n {\n id: "t-cut",') expect(out).toContain('title: "Sound pass"') }) test("a plan composed by code: a task written as plain values in another module changes there; the list itself can't", () => { const plan = `import { plan } from "raea/task" import { shots } from "./shots.ts" export default plan({ tasks: [...shots, { id: "t-grade", keyword: "TODO", title: "Grade" }] }) ` const shots = `export const shots = [ { id: "t-s1", keyword: "TODO", title: "Shot 1" }, { id: "t-s2", keyword: "TODO", title: "Shot 2" }, ] ` const sources = [{ file: "plan.ts", text: plan }, { file: "shots.ts", text: shots }] const value: PlanFile = { tasks: [{ id: "t-s1", keyword: "TODO", title: "Shot 1" }, { id: "t-s2", keyword: "TODO", title: "Shot 2" }, { id: "t-grade", keyword: "TODO", title: "Grade" }] } const r = edit(sources, value, [{ op: "state", ref: "t-s2", state: "done" }]) expect(r.changed.map((c) => c.file)).toEqual(["shots.ts"]) expect(r.changed[0].text).toContain('{ id: "t-s1", keyword: "TODO", title: "Shot 1" },\n {\n id: "t-s2",\n keyword: "DONE",') const g = edit(sources, value, [{ op: "set", ref: "t-grade", title: "Grade it warm" }]) expect(g.changed.map((c) => c.file)).toEqual(["plan.ts"]) expect(g.changed[0].text).toContain("...shots, {") expect(() => edit(sources, value, [{ op: "add", title: "Deliver" }])).toThrow(/the plan's own list is made by code at plan\.ts:4: add, move or remove its top tasks there by hand/) }) test("a task made by code is refused, naming where; nothing is written", () => { const shots = `const names = ["Shot 1", "Shot 2"] export const shots = names.map((title, i) => ({ id: \`t-s\${i}\`, keyword: "TODO", title })) ` const sources = [{ file: "plan.ts", text: `export default plan({ tasks: [{ title: "Shots", tasks: shots }] })` }, { file: "shots.ts", text: shots }] const value: PlanFile = { tasks: [{ title: "Shots", tasks: [{ id: "t-s0", keyword: "TODO", title: "Shot 1" }] }] } expect(() => edit(sources, value, [{ op: "state", ref: "t-s0", state: "done" }])).toThrow(/t-s0 isn't written in any file the brief reaches: it's made by code/) }) test("a task written without an id is given one where it's written, found by its words", () => { const text = `export default plan({ tasks: [...more, { keyword: "TODO", title: "By hand" }] })` const value: PlanFile = { tasks: [{ id: "t-a", keyword: "TODO", title: "A" }, { keyword: "TODO", title: "By hand" }] } const r = edit([{ file: "plan.ts", text }, { file: "more.ts", text: 'export const more = [{ id: "t-a", keyword: "TODO", title: "A" }]' }], value, []) expect(r.changed.map((c) => c.file)).toEqual(["plan.ts"]) expect(r.changed[0].text).toBe('export default plan({ tasks: [...more, {\n id: "t-new00001",\n keyword: "TODO",\n title: "By hand",\n}] })') }) test("a project with no plan: a plan.ts to make, and the brief given it", () => { const r = edit([{ file: "brief.ts", text: 'import { brief } from "raea/brief"\n\nexport default brief({\n title: "Spring",\n})\n' }], undefined, [{ op: "add", title: "First cut" }]) expect(r.changed).toEqual([]) expect(r.created!.file).toBe("plan.ts") expect(r.created!.text.startsWith('import { plan } from "raea/task"\n\nexport default plan({\n')).toBe(true) expect(registerPlan('import { brief } from "raea/brief"\n\nexport default brief({\n title: "Spring",\n})\n')).toBe('import { brief } from "raea/brief"\nimport plan from "./plan.ts"\n\nexport default brief({\n plan,\n title: "Spring",\n})\n') expect(() => registerPlan('export default brief({ title: "x", plan: makePlan() })')).toThrow(/already names a plan/) }) })