Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| The docs service answers folio_page with a folio and what its page shows around it, a space can let its editors share what is in it, and someone asking for access to an artifact is told to wait when they already asked for it in the last day. | 1 | import assert from "node:assert/strict"; |
| 2 | import { readFileSync, readdirSync } from "node:fs"; | |
| 3 | import { DatabaseSync } from "node:sqlite"; | |
| 4 | import { test } from "node:test"; | |
| 5 | ||
| 6 | import { REQUEST_EVERY_MS, claimAccessRequest } from "./requests.ts"; | |
| 7 | ||
| 8 | /** D1, as far as requests use it, over node's SQLite with the service's migrations. */ | |
| 9 | function fakeD1(): D1Database { | |
| 10 | const db = new DatabaseSync(":memory:"); | |
| 11 | const dir = new URL("../../migrations/", import.meta.url); | |
| 12 | for (const file of readdirSync(dir).sort()) db.exec(readFileSync(new URL(file, dir), "utf8")); | |
| 13 | db.exec("PRAGMA foreign_keys = OFF"); | |
| 14 | const statement = (sql: string, params: unknown[] = []) => ({ | |
| 15 | bind: (...values: unknown[]) => statement(sql, values), | |
| 16 | first: async () => (db.prepare(sql).get(...(params as never[])) as unknown) ?? null, | |
| 17 | run: async () => db.prepare(sql).run(...(params as never[])), | |
| 18 | }); | |
| 19 | return { prepare: (sql: string) => statement(sql) } as unknown as D1Database; | |
| 20 | } | |
| 21 | ||
| 22 | const at = new Date("2026-10-09T12:00:00.000Z"); | |
| 23 | const later = (ms: number) => new Date(at.getTime() + ms); | |
| 24 | ||
| 25 | test("a person's first request for a folio goes", async () => { | |
| 26 | const db = fakeD1(); | |
| 27 | assert.equal(await claimAccessRequest(db, "fol_a", "ana", at), true); | |
| 28 | }); | |
| 29 | ||
| 30 | test("asking again within a day is refused, for that folio only", async () => { | |
| 31 | const db = fakeD1(); | |
| 32 | await claimAccessRequest(db, "fol_a", "ana", at); | |
| 33 | assert.equal(await claimAccessRequest(db, "fol_a", "ana", later(60_000)), false); | |
| 34 | assert.equal(await claimAccessRequest(db, "fol_a", "ana", later(REQUEST_EVERY_MS - 1)), false); | |
| 35 | // Someone else, or another folio, is a request of its own. | |
| 36 | assert.equal(await claimAccessRequest(db, "fol_a", "bo", later(60_000)), true); | |
| 37 | assert.equal(await claimAccessRequest(db, "fol_b", "ana", later(60_000)), true); | |
| 38 | }); | |
| 39 | ||
| 40 | test("a day later they may ask again, and the day starts over", async () => { | |
| 41 | const db = fakeD1(); | |
| 42 | await claimAccessRequest(db, "fol_a", "ana", at); | |
| 43 | assert.equal(await claimAccessRequest(db, "fol_a", "ana", later(REQUEST_EVERY_MS)), true); | |
| 44 | assert.equal(await claimAccessRequest(db, "fol_a", "ana", later(REQUEST_EVERY_MS + 60_000)), false); | |
| 45 | assert.equal(await claimAccessRequest(db, "fol_a", "ana", later(2 * REQUEST_EVERY_MS)), true); | |
| 46 | }); | |
| 47 | ||
| 48 | test("a refused press doesn't push the next allowed time back", async () => { | |
| 49 | const db = fakeD1(); | |
| 50 | await claimAccessRequest(db, "fol_a", "ana", at); | |
| 51 | await claimAccessRequest(db, "fol_a", "ana", later(REQUEST_EVERY_MS - 1)); | |
| 52 | assert.equal(await claimAccessRequest(db, "fol_a", "ana", later(REQUEST_EVERY_MS)), true); | |
| 53 | }); |