Cotton code
82 lines · 4.7 KB · TypeScriptRaw
/** * SPACES 1 (coordinator, 28 Nov 2026; src/space/space.ts `APPS`): a shared space names the app it belongs to, fixed at * founding, so Words, Sites, Days and Cinemas share through the same space as Albums. Each app's records are its own * kinds: Albums `album`/`item`, every other app `thing` (its own container) and `part`. A space founded with no app is * an Albums space, as every space before SPACES 1 is. Every number is invented. */import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import { fakes } from "./calling.ts";import { accept, found, person, record, type SpaceView } from "./shared_space_people.ts";vi.setConfig({ testTimeout: 240_000 });beforeEach(() => {  fakes();});afterEach(() => {  vi.restoreAllMocks();});describe("a space names its app (SPACES 1)", () => {  it("founds a Words space whose records are things and parts, listed under Words for each member", async () => {    const a = await person();    const b = await person();    await accept(a, b);    const { space } = await found(a, [b], { app: "words" });    const read = await b.ask<SpaceView & { app: string }>("GET", `/v1/spaces/${space}`);    expect(read.body.app).toBe("words");    for (const p of [a, b]) expect((await p.ask<{ spaces: { space: string; app: string }[] }>("GET", "/v1/spaces")).body.spaces).toContainEqual(expect.objectContaining({ space, app: "words" }));    const put = (records: unknown[]) => b.ask("POST", `/v1/spaces/${space}/records`, { epoch: 1, records });    expect((await put([record("note-1", "thing", "note-1", [], 1), record("para-1", "part", "note-1", [], 1)])).status).toBe(200);    // Albums' kinds are not a Words space's, and a thing is its own container.    const wrongKind = await put([record("alb-1", "album", "alb-1", [], 1)]);    expect(wrongKind.status).toBe(400);    expect(JSON.stringify(wrongKind.body)).toContain("thing, part");    expect((await put([record("note-2", "thing", "note-1", [], 1)])).status).toBe(400);  });  it("founds an Albums space when no app is named, and refuses an app it does not know", async () => {    const a = await person();    const b = await person();    await accept(a, b);    const { space } = await found(a, [b]);    expect((await a.ask<{ app: string }>("GET", `/v1/spaces/${space}`)).body.app).toBe("albums");    expect((await a.ask("POST", `/v1/spaces/${space}/records`, { epoch: 1, records: [record("alb-1", "album", "alb-1", [], 1)] })).status).toBe(200);    expect((await a.ask("POST", `/v1/spaces/${space}/records`, { epoch: 1, records: [record("t-1", "thing", "t-1", [], 1)] })).status).toBe(400);    const refused = await a.ask<{ error: string }>("POST", "/v1/spaces", { members: [b.number], app: "mail" });    expect(refused.status).toBe(400);    expect(JSON.stringify(refused.body)).toContain("bad_app");  });});describe("roles in a space (SPACES 1)", () => {  it("lets a member who only sees read but not add, and the owner change what a member may do", async () => {    const a = await person();    const b = await person();    const c = await person();    await accept(a, b);    await accept(a, c);    const { space, id } = await found(a, [b, c], { app: "days", see: [c.number] });    const read = await a.ask<{ members: { member: string; role: string }[] }>("GET", `/v1/spaces/${space}`);    const roles = Object.fromEntries(read.body.members.map((m) => [m.member, m.role]));    expect([roles[id[a.number]!], roles[id[b.number]!], roles[id[c.number]!]]).toEqual(["owner", "add", "see"]);    const put = (who: typeof a, rid: string) => who.ask<{ code?: string }>("POST", `/v1/spaces/${space}/records`, { epoch: 1, records: [record(rid, "thing", rid, [], 1)] });    const refused = await put(c, "ev-c");    expect(refused.status).toBe(403);    expect(JSON.stringify(refused.body)).toContain("see_only");    expect((await c.ask("POST", `/v1/spaces/${space}/upload`, { epoch: 1, size: 128 })).status).toBe(403);    expect((await c.ask("GET", `/v1/spaces/${space}/records?after=0`)).status).toBe(200);    // Only the owner changes a role, and never the owner's own.    expect((await b.ask("POST", `/v1/spaces/${space}/roles`, { member: id[c.number], role: "add" })).status).toBe(403);    expect((await a.ask("POST", `/v1/spaces/${space}/roles`, { member: id[a.number], role: "see" })).status).toBe(409);    expect((await a.ask("POST", `/v1/spaces/${space}/roles`, { member: id[c.number], role: "add" })).status).toBe(200);    expect((await put(c, "ev-c")).status).toBe(200);    expect((await a.ask("POST", `/v1/spaces/${space}/roles`, { member: id[b.number], role: "see" })).status).toBe(200);    expect((await put(b, "ev-b")).status).toBe(403);    expect((await a.ask("POST", `/v1/spaces/${space}/roles`, { member: id[b.number], role: "boss" })).status).toBe(400);  });});