Cotton code
69 lines · 4.8 KB · TypeScriptRaw
/** * Cotton Days over MCP (assets/design/days/CONTRACT.md): a to-do's mark, a * Google event's time and title refused with the sentence a phone shows, and * the person's own additions on it kept through the next Google pull. * Invented data only. */import { env, runDurableObjectAlarm, runInDurableObject } from "cloudflare:test";import { afterEach, describe, expect, it } from "vitest";import { macRowId } from "../src/google_calendar.ts";import { Store } from "../src/store.ts";import { connectAccount, EMAIL, FakeCalendar, google, primary, quiet, revoked, tokens } from "./google.ts";import { answer, EVENTS_OPEN, personalObject, refusal, TODOS_OPEN } from "./personal.ts";afterEach(() => {  google(tokens, primary, quiet, revoked);});const DAY = 86_400_000;const day = (offset: number) => new Date(Date.now() + offset * DAY + 5.5 * 3_600_000).toISOString().slice(0, 10);const stub = (slug: string) => env.BUSINESS.get(env.BUSINESS.idFromName(slug));interface Rec {  id: string;  data: Record<string, unknown>;}describe("Cotton Days over MCP", () => {  /** RULE (DECISIONS.md, Marks): the person's Claude gives a to-do its mark, takes it off with null, and a mark outside the catalogue is refused; a repeating to-do's next occurrence wears its mark. */  it("marks a to-do, clears the mark, refuses an unknown one, and carries it to the next occurrence", async () => {    const slug = "daysmark";    const { owner } = await personalObject(slug, { open: TODOS_OPEN });    const made = await answer<{ id: string; mark: string }>(slug, owner, "create_todo", { title: "Dentist forms", dueDate: day(2), mark: "tooth" });    expect(made.mark).toBe("tooth");    const held = await answer<Rec>(slug, owner, "get_record_by_table", { table: "todo", id: made.id });    expect(held.data.mark).toBe("tooth");    expect((await answer<{ mark: unknown }>(slug, owner, "update_todo", { id: made.id, mark: null })).mark).toBeNull();    expect(await refusal(slug, owner, "update_todo", { id: made.id, mark: "unicorn" })).toMatch(/^mark must be one of the marks \(cake, plane, tooth/);    const walk = await answer<{ id: string; recurrenceId: string }>(slug, owner, "create_todo", { title: "Walk the dog", dueDate: day(1), dueTime: "08:00", rrule: "FREQ=DAILY", mark: "dog" });    await answer(slug, owner, "update_todo", { id: walk.id, status: "done" });    const all = await answer<{ records: Rec[] }>(slug, owner, "list_records", { table: "todo", limit: 500 });    const series = all.records.filter((r) => r.data.recurrence_id === walk.recurrenceId).sort((a, b) => String(a.data.occurrence_at).localeCompare(String(b.data.occurrence_at)));    expect(series.map((r) => [r.data.status, r.data.mark])).toEqual([["done", "dog"], ["open", "dog"]]);  });  /** RULE (DECISIONS.md: Google events are read-only for now): a Google event's time and title are refused with the plain reason; the person's mark and note are written and survive the next pull, while Google's own words still arrive. */  it("refuses a Google event's time and title and keeps the person's mark and note through the next pull", async () => {    const slug = "daysgoogle";    const { owner } = await personalObject(slug, EVENTS_OPEN);    const cal = new FakeCalendar();    cal.put({ id: "sync", iCalUID: "sync@google.com", summary: "Team sync", start: { dateTime: "2026-12-01T10:00:00Z" }, end: { dateTime: "2026-12-01T10:30:00Z" } });    google(tokens, primary, cal.route, revoked);    await connectAccount(slug, owner);    for (let i = 0; i < 6; i++) await runDurableObjectAlarm(stub(slug));    const id = await macRowId("calendar", "sync@google.com");    expect(await refusal(slug, owner, "update_record", { table: "event", id, patch: { title: "Renamed here" } })).toBe("This event is from Google Calendar. Change its time and title in Google.");    expect(await refusal(slug, owner, "update_record", { table: "event", id, patch: { starts_at: "2026-12-02T10:00:00.000Z" } })).toBe("This event is from Google Calendar. Change its time and title in Google.");    await answer(slug, owner, "update_record", { table: "event", id, patch: { mark: "laptop", your_note: "Bring the figures", alert: 30 } });    const asked = cal.asked.length;    cal.put({ id: "sync", iCalUID: "sync@google.com", summary: "Team sync (moved)", start: { dateTime: "2026-12-01T11:00:00Z" }, end: { dateTime: "2026-12-01T11:30:00Z" } });    for (let i = 0; i < 12 && cal.asked.length <= asked; i++) await runDurableObjectAlarm(stub(slug));    await runDurableObjectAlarm(stub(slug));    const row = await runInDurableObject(stub(slug), (_instance, state) => new Store(state.storage.sql, () => new Date()).get("event", id));    expect(row!.data).toMatchObject({ title: "Team sync (moved)", starts_at: "2026-12-01T11:00:00.000Z", mark: "laptop", your_note: "Bring the figures", alert: 30, account: EMAIL });  });});