Cotton code
75 lines · 4.3 KB · TypeScriptRaw
// A Cotton app's kit on a personal object: Journal and Story (each took a retired app's place, Recipes and then Calling, 22 Nov 2026: an app with tables of its own).//// RULE (owner, 12 Nov 2026): every object gets its whole kit, and an app's// tool is a definition the object holds — so an app signed in to its object// draws its tool from there. A calling walk (13 Nov, ~04:30 IST) saw the// object answer the calling tables and no tool; this pins that a fresh// object, and one made before those apps' kits, both hold each app's tool// and its tables after the first request.import { env, runInDurableObject } from "cloudflare:test";import { describe, expect, it } from "vitest";import { KIT_GIVEN, KIT_VERSION, givenBy, type Kit } from "../src/kit_keep.ts";import { KITS, PERSONAL } from "../src/kits.ts";import type { Business } from "../src/business.ts";import { ADMIN, DATABASE_ID, OWNER_PHONE, http } from "./personal.ts";type Doc = Record<string, unknown>;type Defs = { tools: Doc[]; boards: Doc[]; tables: Doc[] };// Journal is held as retired (TABLE DROP 1): only Story's tool and tables are laid now.const APPS = ["story"] as const;const stub = (slug: string) => env.BUSINESS.get(env.BUSINESS.idFromName(slug));const ids = (docs: Doc[]): string[] => docs.map((d) => String(d.id));/** Each app's own tables: the ones its kit ships that no other app's kit in the personal kit ships too. */function ownTables(app: string): string[] {  const others = new Set(Object.entries(KITS).filter(([id]) => id !== app && id !== "personal" && id !== "shell").flatMap(([, kit]) => kit.tables.map((t) => String(t.id))));  return KITS[app]!.tables.map((t) => String(t.id)).filter((id) => !others.has(id));}function expectEachApp(defs: Defs): void {  for (const app of APPS) {    const kit = KITS[app]!;    for (const tool of kit.tools) expect(defs.tools.find((t) => t.id === tool.id), `${app}'s tool ${String(tool.id)}`).toEqual(tool);    for (const table of kit.tables) expect(ids(defs.tables), `${app}'s table ${String(table.id)}`).toContain(String(table.id));  }}describe("Journal's and Story's tools on a personal object", () => {  it("are the personal kit's: each app's tool and tables ship in it", () => {    for (const app of APPS) {      for (const tool of KITS[app]!.tools) expect(ids(PERSONAL.tools), app).toContain(String(tool.id));      for (const table of KITS[app]!.tables) expect(ids(PERSONAL.tables), app).toContain(String(table.id));    }  });  it("are on a new number's object from its first sign-in", async () => {    const slug = "kit-apps-fresh";    await runInDurableObject(stub(slug), (instance: Business) => instance.provision({ databaseId: DATABASE_ID, slug, phone: OWNER_PHONE }));    expectEachApp((await http<Defs>(slug, "GET", "/v1/definitions", undefined, ADMIN)).body);  });  it("are brought to an object made from a kit before either app, on its next request", async () => {    const slug = "kit-apps-older";    const appTools = new Set(APPS.flatMap((app) => ids(KITS[app]!.tools)));    const appTables = new Set(APPS.flatMap(ownTables));    const older: Kit = {      own: PERSONAL.own,      tools: PERSONAL.tools.filter((t) => !appTools.has(String(t.id))),      boards: PERSONAL.boards,      tables: PERSONAL.tables.filter((t) => !appTables.has(String(t.id))),      shell: KITS["shell"]!.boards.map((b) => String(b.id)),    };    expect(appTables.size, "each app has a table of its own to bring").toBeGreaterThan(0);    expect((await http(slug, "PUT", "/v1/admin/business", { name: "Personal", kind: "personal", owner_phone: OWNER_PHONE, database_id: DATABASE_ID, timezone: "Asia/Kolkata" }, ADMIN)).status).toBe(200);    const laid = await http(slug, "PUT", "/v1/admin/definitions", { tools: [...older.own, ...older.tools], tables: older.tables, flavour: { flavour: "personal" } }, ADMIN);    expect(laid.status, JSON.stringify(laid.body)).toBe(200);    await runInDurableObject(stub(slug), (_instance, state) => {      const set = (key: string, value: string) => state.storage.sql.exec("INSERT INTO meta (key, value) VALUES (?, ?) ON CONFLICT (key) DO UPDATE SET value = excluded.value", key, value);      set(KIT_GIVEN, JSON.stringify(givenBy(older)));      set(KIT_VERSION, "a-kit-before-calling-and-recipes");    });    expectEachApp((await http<Defs>(slug, "GET", "/v1/definitions", undefined, ADMIN)).body);  });});