144 lines · 7.9 KB · TypeScriptRaw
import { describe, expect, it } from "vitest";import { ACTS } from "../src/acts.ts";import { isGoneAct } from "../src/retired.ts";import { ADMIN, answer, http, mint, personalObject, put, refusal, tool } from "./personal.ts";/** A synthetic club: members by person, slots with a reserve act, seats the reservation owns, and a set act on a member. */const CLUB_TABLES = [ { id: "club_member", name: "Club member", visibility: "mine(person)", title: "name", columns: [ { key: "name", type: "text", required: true }, { key: "person", type: "link", link_to: "person" }, { key: "paused", type: "bool" }, ], acts: [{ id: "pause", label: "Pause", kind: "update", verb: "set", data: { column: "paused", value: true }, when: { paused: false } }], }, { id: "club_slot", name: "Club slot", visibility: "owner", title: "name", date: "on", columns: [ { key: "name", type: "text", required: true }, { key: "on", type: "date" }, { key: "capacity", type: "number" }, ], acts: [ { id: "book", label: "Book", kind: "create", verb: "reserve_record", bindings: { "data.name": "name", "data.on": "on" }, picks: [ { argument: "data.member", kind: "record", table: "club_member", label: "Member" }, { argument: "data.quantity", kind: "number", label: "Places", min: 1, default: 1 }, ], }, ], }, { id: "club_seat", name: "Club seat", visibility: "owner", title: "name", columns: [ { key: "name", type: "text" }, { key: "member", type: "link", link_to: "club_member" }, { key: "slot", type: "link", link_to: "club_slot" }, { key: "quantity", type: "number" }, { key: "on", type: "date" }, ], },];const CLUB_TOOL = { id: "club", name: "Club", tables: ["club_member", "club_slot", "club_seat"], reserve: { slot_table: "club_slot", capacity_column: "capacity", claim_table: "club_seat", slot_column: "slot", quantity_column: "quantity" } };interface ActAnswer { version: number; changed: { table: string; id: string; data: Record<string, unknown> }[]; made?: { table: string; id: string }; notices: string[]; token?: string;}/** RULE (owner, 26 Oct 2026): Cotton MCP reaches every act the object dispatches, through the object's own dispatch, as the caller. */describe("run_act", () => { const slug = "actsclub"; let credential = ""; let owner = ""; let writer = ""; let ids: Record<string, string> = {}; it("is set up: the personal object with a club tool beside the package", async () => { const made = await personalObject(slug); ({ owner, credential } = made); writer = await mint(slug, credential, "a writer", ["read", "write"]); expect((await http(slug, "POST", "/v1/admin/definitions/tables", { tables: CLUB_TABLES }, ADMIN)).status).toBe(200); expect((await http(slug, "POST", "/v1/admin/definitions/tools", { tools: [CLUB_TOOL] }, ADMIN)).status).toBe(200); ids = await put(slug, [ { ref: "asha", table: "club_member", data: { name: "Asha" } }, { ref: "slot", table: "club_slot", data: { name: "Tuesday swim", on: "2099-01-06", capacity: 2 } }, ]); }); it("lists exactly the acts the dispatch has, each with its arguments and meaning", async () => { const listed = await http<{ result: { tools: { name: string; description: string; inputSchema: { properties: { act: { enum: string[] } } } }[] } }>(slug, "POST", "/mcp", { jsonrpc: "2.0", id: 1, method: "tools/list" }, { authorization: `Bearer ${owner}` }); const runAct = listed.body.result.tools.find((t) => t.name === "run_act")!; // A retired app's act is never offered (TABLE DROP 1). expect(runAct.inputSchema.properties.act.enum).toEqual(Object.keys(ACTS).filter((name) => !isGoneAct(name))); for (const [name, act] of Object.entries(ACTS)) { const line = `- ${name} ${act.args.length ? `{ ${act.args.join(", ")} }` : "{}"}: ${act.meaning}`; if (isGoneAct(name)) expect(runAct.description).not.toContain(line); else expect(runAct.description).toContain(line); } }); it("books and cancels through the reservation, and the claim table refuses a direct write by pointing here", async () => { const booked = await answer<ActAnswer>(slug, writer, "run_act", { act: "book", args: { slot: ids.slot, member: ids.asha, quantity: 2 } }); expect(booked.made?.table).toBe("club_seat"); const seat = booked.changed.find((r) => r.id === booked.made!.id)!; expect(seat.data).toMatchObject({ member: ids.asha, slot: ids.slot, quantity: 2, name: "Tuesday swim", on: "2099-01-06" }); const full = await tool(slug, writer, "run_act", { act: "book", args: { slot: ids.slot, member: ids.asha } }); expect(full).toEqual({ isError: true, content: [{ type: "text", text: "this session is full" }], structuredContent: { code: "full" } }); expect(await refusal(slug, owner, "create_record", { table: "club_seat", data: { name: "sneak" } })).toBe('club_seat is a claim table its reservation owns: book with run_act { act: "book" } and cancel with run_act { act: "cancel" }'); const cancelled = await answer<ActAnswer>(slug, writer, "run_act", { act: "cancel", args: { id: booked.made!.id } }); expect(cancelled.changed.map((r) => r.id)).toContain(booked.made!.id); expect(await refusal(slug, owner, "get_record_by_table", { table: "club_seat", id: booked.made!.id })).toBe("That club seat is no longer here"); }); it("runs a record act a definition declares, and says the act's own refusal when it is not available", async () => { const paused = await answer<ActAnswer>(slug, writer, "run_act", { act: "record_act", args: { table: "club_member", id: ids.asha, act: "pause" } }); expect(paused.changed.find((r) => r.id === ids.asha)?.data.paused).toBe(true); const again = await tool(slug, writer, "run_act", { act: "record_act", args: { table: "club_member", id: ids.asha, act: "pause" } }); expect(again.content[0]!.text).toBe("Pause is not available for this club_member"); expect(again.structuredContent).toEqual({ code: "act_not_available" }); }); // ADMIN KEYS 1b: who may do what is never changed by a key, a write token's or an owner token's alike; the owner's // own device does it (test/access-acts.test.ts). it("refuses an act that changes who may do what to a write token and to an owner token", async () => { for (const token of [writer, owner]) { const role = await tool(slug, token, "run_act", { act: "set_role", args: { phone: "+919000000001", role: "staff", name: "Ravi" } }); expect(role.content[0]!.text).toBe("set_role changes who may do what: the owner does it from their own device, never a key"); const minted = await tool(slug, token, "run_act", { act: "mint_token", args: { name: "a script", scopes: ["read"], expires_in_days: 1 } }); // Not a lane token, so refused to every key (the one mint a key makes: test/access-acts.test.ts). expect(minted.content[0]!.text).toMatch(/^mint_token changes who may do what: the owner does it from their own device, never a key/); } const byPhone = await http<{ version: number }>(slug, "POST", "/v1/act", { act: "set_role", args: { phone: "+919000000001", role: "staff", name: "Ravi" } }, { authorization: `Bearer ${credential}` }); expect(byPhone.status, JSON.stringify(byPhone.body)).toBe(200); }); it("refuses an act there is none of, args that are not an object, and a reader", async () => { expect(await refusal(slug, owner, "run_act", { act: "undo" })).toBe(`there is no act "undo": the acts are ${Object.keys(ACTS).join(", ")}`); expect(await refusal(slug, owner, "run_act", { act: "book", args: "slot" })).toBe("args must be an object: the act's own arguments"); const reader = await mint(slug, credential, "a reader", ["read"]); expect(await refusal(slug, reader, "run_act", { act: "book", args: {} })).toBe('this token does not have "write" access. Ask the owner to reconnect with write enabled.'); });});