Cotton code
94 lines · 6.9 KB · TypeScriptRaw
import { describe, expect, it } from "vitest";import { answer, personalObject } from "./personal.ts";const STORY_OPEN = ["story_story", "story_scene", "story_character", "story_place", "story_note", "story_line", "story_answer", "story_sequence", "story_picture", "story_thing", "story_costume", "story_use", "story_draft", "story_draft_scene"];interface Listed {  records: { id: string; data: Record<string, unknown> }[];}interface Lines {  line: string;  lines: { id: string; kind: string; who: string | null; text: string }[];}async function linesOf(slug: string, owner: string, scene: string): Promise<{ id: string; text: string }[]> {  const rows = await answer<Listed>(slug, owner, "list_records", { table: "story_line", where: { scene }, limit: 200 });  return rows.records.map((row) => ({ id: row.id, text: String(row.data.text), position: Number(row.data.position) })).sort((a, b) => a.position - b.position).map(({ id, text }) => ({ id, text }));}/** * RULE (Story's redesign, rings one to four, 20 Nov 2026): everything a * person adds by hand their Claude can write: a scene's lines that keep their * ids through any rewrite, rename, split and merge; characters and places * merged; a picture on a person; a draft marked, compared by identity and * gone back to; the story out as Final Draft. */describe("Cotton Story's rings over MCP", () => {  it("keeps every line's id through a rewrite, a rename, a split and a merge", async () => {    const slug = "story-ink-lines";    const { owner } = await personalObject(slug, { open: STORY_OPEN });    const story = (await answer<{ record: { id: string } }>(slug, owner, "create_record", { table: "story_story", data: { title: "The Night Bus" } })).record.id;    const scene = (await answer<{ scene: { id: string } }>(slug, owner, "story_scene_write", { story, heading: "EXT. BUS STOP - NIGHT", day: 1, body: "Rain.\n\nASHA\nIs it late?\n\nRAVI\nNot yet.\n\nA bus comes." })).scene.id;    const first = await linesOf(slug, owner, scene);    expect(first.map((line) => line.text)).toEqual(["Rain.", "Is it late?", "Not yet.", "A bus comes."]);    await answer(slug, owner, "story_scene_write", { id: scene, body: "Rain.\n\nASHA\nIs it very late?\n\nA dog barks.\n\nRAVI\nNot yet.\n\nA bus comes." });    const second = await linesOf(slug, owner, scene);    expect(second.map((line) => line.id)).toEqual([first[0]!.id, first[1]!.id, second[2]!.id, first[2]!.id, first[3]!.id]);    expect(first.some((line) => line.id === second[2]!.id)).toBe(false);    const characters = await answer<Listed>(slug, owner, "list_records", { table: "story_character", where: { story } });    const ravi = characters.records.find((row) => row.data.name === "RAVI")!.id;    const asha = characters.records.find((row) => row.data.name === "ASHA")!.id;    await answer(slug, owner, "story_rename", { table: "story_character", id: ravi, name: "Ravi Kumar" });    expect((await linesOf(slug, owner, scene)).map((line) => line.id)).toEqual(second.map((line) => line.id));    const out = await answer<{ text: string }>(slug, owner, "story_fountain_write", { story });    expect(out.text).toContain("RAVI KUMAR\nNot yet.");    const added = await answer<Lines>(slug, owner, "story_line_write", { scene, after: second[3]!.id, kind: "dialogue", character: asha, text: "Good." });    expect(added.lines.map((line) => line.text)).toEqual(["Rain.", "Is it very late?", "A dog barks.", "Not yet.", "Good.", "A bus comes."]);    const split = await answer<{ scene: string }>(slug, owner, "story_scene_split", { id: scene, at: second[3]!.id });    expect((await linesOf(slug, owner, split.scene)).map((line) => line.text)).toEqual(["Not yet.", "Good.", "A bus comes."]);    await answer(slug, owner, "story_scene_merge", { id: split.scene, into: scene });    expect((await linesOf(slug, owner, scene)).map((line) => line.id)).toEqual(added.lines.map((line) => line.id));    await answer(slug, owner, "story_merge", { table: "story_character", id: asha, into: ravi });    const after = await answer<Listed>(slug, owner, "list_records", { table: "story_character", where: { story } });    expect(after.records.map((row) => row.data.name)).toEqual(["Ravi Kumar"]);    expect(after.records[0]!.data.lines).toBe(3);  });  it("marks a draft, says what changed since by identity, goes back to it, and writes Final Draft", async () => {    const slug = "story-ink-drafts";    // A picture is a photo, sealed at arrival (AVATAR SEAL 4 (2)): the object holds the arrival key a phone handed over.    const { owner } = await personalObject(slug, { open: STORY_OPEN, arrival: true });    const story = (await answer<{ record: { id: string } }>(slug, owner, "create_record", { table: "story_story", data: { title: "The Night Bus" } })).record.id;    const scene = (await answer<{ scene: { id: string } }>(slug, owner, "story_scene_write", { story, heading: "EXT. BUS STOP - NIGHT", summary: "Asha waits", body: "Rain.\n\nASHA\nIs it late?" })).scene.id;    const before = await linesOf(slug, owner, scene);    const asha = (await answer<Listed>(slug, owner, "list_records", { table: "story_character", where: { story } })).records[0]!.id;    const picture = await answer<{ picture: { id: string; picture: string } }>(slug, owner, "story_picture_add", { of: { character: asha }, day: 2, caption: "Yellow raincoat", bytes_base64: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==", name: "coat.png" });    expect(picture.picture.picture).toMatch(/\/photos\/[0-9a-f]{64}\.png$/);    const draft = (await answer<{ draft: string }>(slug, owner, "story_draft_mark", { story, name: "First draft" })).draft;    await answer(slug, owner, "story_scene_write", { id: scene, body: "Rain.\n\nASHA\nIs it very late?" });    const added = (await answer<{ scene: { id: string } }>(slug, owner, "story_scene_write", { story, heading: "INT. BUS - NIGHT", body: "Empty seats." })).scene.id;    const since = await answer<{ since: Record<string, number>; scenes: { scene: string; change: string }[] }>(slug, owner, "story_draft_changes", { draft });    expect(since.since).toMatchObject({ scenes_added: 1, scenes_changed: 1, lines_changed: 1 });    expect(since.scenes.find((one) => one.scene === added)?.change).toBe("added");    const read = await answer<{ text: string }>(slug, owner, "story_draft_read", { draft });    expect(read.text).toContain("Is it late?");    await answer(slug, owner, "story_draft_restore", { draft });    expect(await linesOf(slug, owner, scene)).toEqual(before);    const scenes = await answer<Listed>(slug, owner, "list_records", { table: "story_scene", where: { story } });    expect(scenes.records.map((row) => row.id)).toEqual([scene]);    const fdx = await answer<{ text: string; name: string }>(slug, owner, "story_fdx_write", { story });    expect(fdx.name).toBe("The Night Bus.fdx");    expect(fdx.text).toContain("<Paragraph Type=\"Character\">\n      <Text>ASHA</Text>");    expect(fdx.text).toContain("<Text>Asha waits</Text>");  });});