/** * The shared spaces' off switch (src/space/switch.ts): built off (the live Worker, whose config defines nothing, * guarded by scripts/__tests__/wrangler-envs.test.ts), every space path gets exactly the answer a route nobody has * gets, signed in or not, and no person's object founds, joins or is charged for a space. The test pool is built on, * so each check turns the switch off for its own moment. */ import { env, runInDurableObject, SELF } from "cloudflare:test"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import type { Business } from "../src/business.ts"; import { SWITCH } from "../src/space/switch.ts"; import { fakes } from "./calling.ts"; import { opaque, person } from "./shared_space_people.ts"; vi.setConfig({ testTimeout: 180_000 }); beforeEach(() => { fakes(); SWITCH.on = false; }); afterEach(() => { SWITCH.on = true; vi.restoreAllMocks(); }); async function asked(method: string, path: string, auth?: string): Promise<{ status: number; text: string }> { const headers: Record = { "content-type": "application/json" }; if (auth) headers.authorization = `Bearer ${auth}`; const res = await SELF.fetch(`https://cottondb.com${path}`, { method, headers, body: method === "GET" || method === "HEAD" ? undefined : "{}" }); return { status: res.status, text: (await res.text()).split(path).join("") }; } describe("the shared spaces, built off", () => { it("answers every space path exactly as a route nobody has, signed in or not, with a link token or a media URL", async () => { const a = await person(); const space = opaque(16); const paths: [string, string][] = [ ["GET", "/v1/spaces"], ["POST", "/v1/spaces"], ["GET", `/v1/spaces/${space}`], ["POST", `/v1/spaces/${space}/records`], ["POST", `/v1/space-link/${space}/${opaque(12)}`], ["GET", `/v1/space-media/${opaque(49)}/${space}/${opaque(16)}`], ["PUT", `/v1/space-media/${opaque(49)}/${space}/${opaque(16)}`], ]; for (const [method, path] of paths) { for (const auth of [undefined, a.credential, opaque(32)]) { const unknown = await asked(method, `/v1/no-such-route-${opaque(6)}`, auth); const got = await asked(method, path, auth); expect({ status: got.status, text: got.text }, `${method} ${path} ${auth ? "with a bearer" : "with none"}`).toEqual({ status: unknown.status, text: unknown.text.replace(/\/v1\/no-such-route-[A-Za-z0-9_-]+/, "") }); } } // The person's own space for files is not a shared space's path, and still answers. expect((await asked("GET", "/v1/space", a.credential)).status).toBe(200); }); it("no person's object founds, joins or is charged for a space; leaving and refunds still work", async () => { const a = await person(); const answers = await runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(a.slug)), async (instance: Business) => { const held = instance as unknown as { spaceJoin(s: string, f: string, fo: boolean, serial: number): Promise; spaceLeft(s: string, serial: number): Promise; spaceCharge(s: string, d: number): Promise }; return { found: await held.spaceJoin(opaque(16), a.number, true, 1), charged: await held.spaceCharge(opaque(16), 100), left: await held.spaceLeft(opaque(16), 1), refunded: await held.spaceCharge(opaque(16), -100), }; }); expect(answers).toEqual({ found: { refused: "not_your_person" }, charged: false, left: true, refunded: true }); }); });