Cotton code
43 lines · 2.6 KB · TypeScriptRaw
/** * RULE (owner, 18 Nov 2026): two films sending thousands of files stopped the object answering for seven minutes. * One object answers one request at a time, so a file's request must not read the kit's definitions (megabytes of JSON). */import { env, runInDurableObject, SELF } from "cloudflare:test";import { expect, it } from "vitest";import { ADMIN, DATABASE_ID, http, OWNER_PHONE } from "./personal.ts";const SLUG = "media-load";it("a signed-in device's file requests read no definitions", async () => {  const stub = env.BUSINESS.get(env.BUSINESS.idFromName(SLUG));  await runInDurableObject(stub, async (instance: unknown) => {    await (instance as { provision(a: { databaseId: string; slug: string; phone: string }): Promise<void> }).provision({ databaseId: DATABASE_ID, slug: SLUG, phone: OWNER_PHONE });  });  // Made in the object directly, so the directory learns the slug from the admin's first request (AUDIT FIX B1, B-6).  expect((await http(SLUG, "GET", "/v1/admin/open-tables-lock", undefined, ADMIN)).status).toBe(200);  const code = await http<{ challenge_id: string }>(SLUG, "POST", "/v1/code", { phone: OWNER_PHONE, device_id: "load" });  const signed = await http<{ credential: string }>(SLUG, "POST", "/v1/sign-in", { challenge_id: code.body.challenge_id, code: "123456", device_id: "load" });  const headers = { authorization: `Bearer ${signed.body.credential}` };  const url = `https://example.test/b/${SLUG}/v1/media/${DATABASE_ID}/films/film-load/video/seg00000.m4s`;  // The first request after sign-in links the owner; the ones this rule is about come after it.  await (await SELF.fetch(url, { method: "HEAD", headers })).arrayBuffer();  await runInDurableObject(stub, (instance: unknown, state) => {    const sql = state.storage.sql as unknown as { exec: (...args: unknown[]) => unknown };    const exec = sql.exec.bind(sql);    const seen: string[] = [];    (instance as { seen?: string[] }).seen = seen;    sql.exec = (...args: unknown[]) => {      seen.push(String(args[0]));      return exec(...args);    };  });  const body = new Uint8Array(512).fill(7);  expect((await SELF.fetch(url, { method: "HEAD", headers })).status).toBe(404);  const put = await SELF.fetch(url, { method: "PUT", headers: { ...headers, "content-type": "application/vnd.cotton.f1", "content-length": String(body.length) }, body });  expect(put.status).toBe(201);  const seen = await runInDurableObject(stub, (instance: unknown) => (instance as { seen: string[] }).seen);  expect(seen.length).toBeGreaterThan(0);  expect(seen.filter((statement) => /SELECT json FROM definitions/.test(statement))).toEqual([]);});