Cotton code
61 lines · 4.1 KB · TypeScriptRaw
/** * A place a person picks over Cotton MCP is copied from Cotton's published * places into their own record (src/mcp/tools/records/picks.ts): its name in * the place column, its id, country and zone in the read-only siblings. * Synthetic places and people only. */import { env, runInDurableObject } from "cloudflare:test";import { describe, expect, it } from "vitest";import { answer, personalObject, refusal, PEOPLE_OPEN } from "./personal.ts";import { bearer, open } from "./calling.ts";const SLUG = "picks";const COTTON = bearer("test-publisher-token-cotton-0123456789abcdef");async function publishPlaces(version: string, name: string): Promise<void> {  const base = `/v1/published/cotton/places/versions/${version}`;  expect((await open("PUT", base, undefined, COTTON)).status).toBe(200);  const rows = [{ id: "geonames:1269843", row: { name, admin1: "Telangana", country: "IN", country_name: "India", zone: "Asia/Kolkata", population: 6800000 }, names: [name], lat: 17.38, lng: 78.47, rank: 6800000, group: "IN" }];  expect((await open("POST", `${base}/rows`, { table: "places", rows }, COTTON)).status).toBe(200);  expect((await open("POST", `${base}/commit`, undefined, COTTON)).status).toBe(200);}type Written = { record: { id: string; data: Record<string, unknown> } };describe("a picked place", () => {  it("is copied whole into the person's record, and a later version never changes it", async () => {    const { owner } = await personalObject(SLUG, PEOPLE_OPEN);    await publishPlaces("p1", "Hyderabad");    const made = await answer<Written>(SLUG, owner, "create_record", { table: "person", data: { full_name: "Asha Rao", city: { place: "geonames:1269843" } } });    expect(made.record.data).toMatchObject({ city: "Hyderabad", city_place: "geonames:1269843", country: "IN", timezone: "Asia/Kolkata" });    await publishPlaces("p2", "Hyderabad (renamed)");    const later = await answer<{ data: Record<string, unknown> }>(SLUG, owner, "get_record_by_table", { table: "person", id: made.record.id });    expect(later.data.city).toBe("Hyderabad");    const moved = await answer<Written>(SLUG, owner, "update_record", { table: "person", id: made.record.id, patch: { city: { place: "geonames:1269843" } } });    expect(moved.record.data.city).toBe("Hyderabad (renamed)");  });  it("refuses a place the published set does not hold, and a sibling written by hand", async () => {    const { owner } = await personalObject(`${SLUG}-b`, PEOPLE_OPEN);    await publishPlaces("p3", "Hyderabad");    expect(await refusal(`${SLUG}-b`, owner, "create_record", { table: "person", data: { full_name: "Ravi", city: { place: "geonames:42" } } })).toMatch(/not one of Cotton's published places/);    expect(await refusal(`${SLUG}-b`, owner, "create_record", { table: "person", data: { full_name: "Ravi", city: { place: "paris" } } })).toMatch(/choose a place/);    expect(await refusal(`${SLUG}-b`, owner, "create_record", { table: "person", data: { full_name: "Ravi", city_place: "geonames:1269843" } })).toMatch(/read only/);  });  // Locations are sealed (e2e rulings R1): a saved location's name is read only inside the owner's window for this assistant.  // Saved locations are retired (TABLE DROP 1): a pick naming one is refused by name, whatever the row holds.  it("refuses a saved location's pick by name: saved locations are retired", async () => {    const slug = `${SLUG}-sealed-location`;    const { owner } = await personalObject(slug, { ...PEOPLE_OPEN, windows: true });    const id = "00000000-0000-4000-8000-00000000a1c0";    await runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slug)), (_i, state) => {      const at = new Date().toISOString();      state.storage.sql.exec("INSERT INTO rows (table_id, id, data, version, created_at, updated_at) VALUES ('location', ?, ?, 1, ?, ?)", id, JSON.stringify({ sealed_enc: "s1:synthetic", dek_enc: "d1:synthetic", seal_v: 1 }), at, at);    });    const refused = await refusal(slug, owner, "create_record", { table: "person", data: { full_name: "Ravi Iyer", city: { place: `location:${id}` } } });    expect(refused).toContain("saved locations are retired");    expect(refused).toContain("geonames:");  });});