Cotton code
108 lines · 7.3 KB · TypeScriptRaw
import { describe, expect, it } from "vitest";import { answer, personalObject, refusal } from "./personal.ts";const STORY_OPEN = ["story_story", "story_scene", "story_character", "story_place", "story_note", "story_line", "story_answer", "story_sequence"];interface Written {  scene: { id: string; story: string; heading: string; setting: string; place: string | null; time: string | null; words: number; pages: number; speakers: string };  made: { characters: string[]; places: string[] };}interface Listed {  records: { id: string; data: Record<string, unknown> }[];}/** * RULE (Cotton Story contract, 17 Nov 2026): an assistant connected to * Cotton lists and reads stories and scenes with the generic tools, and keeps * the story's rules with Story's own verbs: story_scene_write writes a body * and a heading, works out the figures and makes the characters and places * it names; story_scene_move reorders; story_copy copies a story for * translating; story_fountain_write reads the whole story as Fountain, and * story_fountain_read brings a Fountain file back the same. Story is sealed: * with no window, a write is refused. */describe("Cotton Story over MCP", () => {  it("refuses a sealed story write with no window open", async () => {    const slug = "story-sealed";    const { owner } = await personalObject(slug);    const refused = await refusal(slug, owner, "story_scene_write", { story: "no-window", body: "MEERA\nHi." });    // Your Claude, not yet allowed: the one account card (the grant is on Worker-wide since 21 Nov 2026).    expect(refused).toContain("Let your Claude read your data");  });  /**   * RULE (Story's new scene list, 18 Nov 2026): an assistant reordering scenes   * leaves the interval and a sequence's start between the same scenes, and a   * copy for translating carries them, the answers and what each scene grew from.   */  it("keeps the interval and sequences in place through a move and a copy", async () => {    const slug = "story-marks";    const { owner } = await personalObject(slug, { open: STORY_OPEN });    const made = await answer<{ record: { id: string } }>(slug, owner, "create_record", { table: "story_story", data: { title: "The Last Letter", interval_at: 2.5 } });    const story = made.record.id;    const ids: string[] = [];    for (const place of ["KITCHEN", "STATION", "WEDDING"]) ids.push((await answer<Written>(slug, owner, "story_scene_write", { story, heading: `INT. ${place} - DAY`, body: "She waits." })).scene.id);    await answer(slug, owner, "create_record", { table: "story_sequence", data: { story, title: "The wedding", position: 1.5 } });    const asked = await answer<{ record: { id: string } }>(slug, owner, "create_record", { table: "story_answer", data: { story, question: "who", answer: "Maya, a nurse." } });    await answer(slug, owner, "update_record", { table: "story_scene", id: ids[0], patch: { answer: asked.record.id } });    await answer(slug, owner, "story_scene_move", { id: ids[2], to: 1 });    await answer<Written>(slug, owner, "story_scene_write", { after: ids[2], heading: "INT. ROOF - DAWN", body: "Wind." });    const stories = await answer<Listed>(slug, owner, "list_records", { table: "story_story" });    expect(stories.records.find((row) => row.id === story)?.data.interval_at).toBe(3.5);    const sequences = await answer<Listed>(slug, owner, "list_records", { table: "story_sequence", where: { story } });    expect(sequences.records[0]?.data.position).toBe(1.5);    const copy = await answer<{ story: string }>(slug, owner, "story_copy", { story, language: "Telugu" });    const copied = await answer<Listed>(slug, owner, "list_records", { table: "story_sequence", where: { story: copy.story } });    expect(copied.records.map((row) => row.data.title)).toEqual(["The wedding"]);    const answers = await answer<Listed>(slug, owner, "list_records", { table: "story_answer", where: { story: copy.story } });    expect(answers.records.map((row) => row.data.answer)).toEqual(["Maya, a nurse."]);    const scenes = await answer<Listed>(slug, owner, "list_records", { table: "story_scene", where: { story: copy.story } });    expect(scenes.records.filter((row) => row.data.answer === answers.records[0]?.id)).toHaveLength(1);  });  it("writes scenes by the story's rules, reorders, copies and reads them as Fountain", async () => {    const slug = "story-verbs";    const { owner } = await personalObject(slug, { open: STORY_OPEN });    const made = await answer<{ record: { id: string } }>(slug, owner, "create_record", { table: "story_story", data: { title: "The Last Bus", written_by: "A Cotton sample", made_at: "2026-11-10" } });    const story = made.record.id;    const first = await answer<Written>(slug, owner, "story_scene_write", { story, heading: "ext. bus depot - night", summary: "Meera finds the boy.", body: "Buses hum.\n\nMEERA\nYou waiting for someone?\n\nARJUN\n(holding up a ticket)\nThe bus on this ticket." });    expect(first.scene.setting).toBe("EXT.");    expect(first.scene.time).toBe("night");    expect(first.scene.speakers).toBe("MEERA\nARJUN");    expect(first.scene.words).toBeGreaterThan(10);    expect(first.made.characters).toHaveLength(2);    expect(first.made.places).toHaveLength(1);    const moved = await answer<Written>(slug, owner, "story_scene_write", { id: first.scene.id, time: "DAWN" });    expect(moved.scene.heading).toBe("EXT. BUS DEPOT - DAWN");    const refusedFigure = await refusal(slug, owner, "update_record", { table: "story_scene", id: first.scene.id, patch: { words: 3 } });    expect(refusedFigure).toContain("read only");    const second = await answer<Written>(slug, owner, "story_scene_write", { story, heading: "INT. TEA STALL - NIGHT", body: "DEVI\nTea?" });    const order = await answer<{ positions: { id: string; position: number }[] }>(slug, owner, "story_scene_move", { id: second.scene.id, by: -1 });    expect(order.positions.map((p) => p.id)).toEqual([second.scene.id, first.scene.id]);    const characters = await answer<Listed>(slug, owner, "list_records", { table: "story_character", where: { story } });    const meera = characters.records.find((c) => c.data.name === "MEERA");    expect(meera?.data.lines).toBe(1);    const out = await answer<{ text: string; name: string }>(slug, owner, "story_fountain_write", { story });    expect(out.name).toBe("The Last Bus.fountain");    expect(out.text).toContain("Title: The Last Bus\n");    expect(out.text).toContain("\nINT. TEA STALL - NIGHT\n\nDEVI\nTea?\n");    expect(out.text).toContain("\nEXT. BUS DEPOT - DAWN\n\n= Meera finds the boy.\n\nBuses hum.");    const back = await answer<{ story: string; scenes: number; characters: number; places: number }>(slug, owner, "story_fountain_read", { text: out.text });    expect([back.scenes, back.characters, back.places]).toEqual([2, 3, 2]);    const again = await answer<{ text: string }>(slug, owner, "story_fountain_write", { story: back.story });    expect(again.text).toBe(out.text);    const copy = await answer<{ story: string; scenes: number }>(slug, owner, "story_copy", { story, language: "Telugu" });    expect(copy.scenes).toBe(2);    const stories = await answer<Listed>(slug, owner, "list_records", { table: "story_story" });    const copied = stories.records.find((s) => s.id === copy.story)!;    expect(copied.data.translation_of).toBe(story);    expect(copied.data.title).toBe("The Last Bus (Telugu)");  });});