/** * SPACES 1 rule 2 (coordinator, 28 Nov 2026): no route of a shared space reads a whole table per request. A space of * 200k record rows (99k live, near the most a space holds, half still at the old epoch, and 101k deleted) answers each * member route by reading a handful of rows: SQLite's own count of rows read for the call is asserted, the way * test/read-cost.test.ts does for a person's object. Every number is invented. */import { afterEach, beforeEach, describe, expect, it, vi } from"vitest";import { fakes } from"./calling.ts";import { accept, found, inSpace, person, record, wrap } from"./shared_space_people.ts";vi.setConfig({ testTimeout: 600_000 });beforeEach(() => { fakes();});afterEach(() => { vi.restoreAllMocks();});const LIVE = 99_000;const DELETED = 101_000;typeCounted = { cursors: SqlStorageCursor<Record<string, SqlStorageValue>>[]; said: string[] };typePatched = { exec: (...a: unknown[]) => SqlStorageCursor<Record<string, SqlStorageValue>>; __exec?: (...a: unknown[]) => SqlStorageCursor<Record<string, SqlStorageValue>>; __counted?: Counted };describe("a shared space's routes read a handful of rows, whatever its size (SPACES 1)", () => { it("answers read, list, stale, put, upload and roster on a space of 200k record rows", async () => {const a = await person();const b = await person();await accept(a, b);const { space, id } = await found(a, [b], { app: "words" });const me = id[b.number]!;// Planted straight into the space: half the live records at epoch 1, half at 2 (after the move below), and tombstones.await inSpace(space, (sql) => { sql.exec(`WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < ?) INSERT INTO record (id, kind, album, adder, epoch, key, body, sig, files, bytes, seq, at, deleted) SELECT 'r' || i, 'part', 'note', ?, CASE WHEN i % 2 = 0 THEN 1 ELSE 2 END, 'g1:1:AAAA', 'r1:AAAA', 's1:AAAA', '[]', 500, 1000 + i, 0, CASE WHEN i <= ? THEN 1 ELSE 0 END FROM n`, LIVE + DELETED, me, DELETED, ); sql.exec("UPDATE meta SET v = ? WHERE k = 'seq'", String(1000 + LIVE + DELETED)); });const moved = await a.ask("POST", `/v1/spaces/${space}/epoch`, { epoch: 2, remove: [], wraps: { [id[a.number]!]: wrap(), [me]: wrap() } }); expect(moved.status, JSON.stringify(moved.body)).toBe(200);// A warm call: the object's once-per-instance work is not a request's. expect((await b.ask("GET", `/v1/spaces/${space}`)).status).toBe(200);const costOf = async (method: string, path: string, body?: unknown): Promise<{ status: number; rowsRead: number; heavy: { n: number; sql: string }[] }> => {await inSpace(space, (sql) => {const s = sql as unknown asPatched;const exec = (s.__exec ??= s.exec.bind(sql));const counted: Counted = { cursors: [], said: [] }; s.__counted = counted; s.exec = (...x: unknown[]) => {const cursor = exec(...x); counted.cursors.push(cursor); counted.said.push(String(x[0]).replace(/\s+/g, " ").slice(0, 200));return cursor; }; });const res = await b.ask(method, path, body);const { rowsRead, heavy } = await inSpace(space, (sql) => {const s = sql as unknown asPatched;const counted = s.__counted!; s.exec = s.__exec!;return { rowsRead: counted.cursors.reduce((n, c) => n + c.rowsRead, 0), heavy: counted.cursors.map((c, i) => ({ n: c.rowsRead, sql: counted.said[i]! })).filter((x) => x.n > 2_000) }; });for (const h of heavy) console.log(`SPACES read cost: ${h.n} rows: ${h.sql}`);return { status: res.status, rowsRead, heavy }; };const routes: [string, string, unknown?][] = [ ["GET", `/v1/spaces/${space}`], ["GET", `/v1/spaces/${space}/records?after=${1000 + LIVE + DELETED - 10}`], ["GET", `/v1/spaces/${space}/records?after=${1000 + LIVE + DELETED - 10}&album=note`], ["GET", `/v1/spaces/${space}/stale`], ["GET", `/v1/spaces/${space}/roster?after=-1`], ["POST", `/v1/spaces/${space}/records`, { epoch: 2, records: [record("fresh-1", "part", "note", [], 2)] }], ["POST", `/v1/spaces/${space}/upload`, { epoch: 2, size: 128 }], ["GET", "/v1/spaces"], ];for (const [method, path, body] of routes) {const cost = await costOf(method, path, body); expect(cost.status, `${method} ${path}`).toBe(200); expect(cost.heavy, `${method} ${path}`).toEqual([]);// A page is at most LIMITS.listPage (500) records; nothing beyond a page and its few lookups. expect(cost.rowsRead, `${method} ${path}`).toBeLessThan(3_000); } });});