Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

51 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { build } from "esbuild";
4import { fileURLToPath } from "node:url";
5
6import type { WhoEnv } from "./who.ts";
7
8// Resolve the contracts package's bundler-style imports as production does.
9const bundle = await build({ entryPoints: [fileURLToPath(new URL("./who.ts", import.meta.url))], bundle: true, write: false, format: "esm", platform: "node" });
10const { Who, forgetKept }: typeof import("./who.ts") = await import(`data:text/javascript;base64,${Buffer.from(bundle.outputFiles[0].text).toString("base64")}`);
11
12function deferred<T>() {
13 let resolve!: (value: T) => void;
14 const promise = new Promise<T>((done) => { resolve = done; });
15 return { promise, resolve };
16}
17
18function environment(read: () => Promise<Response>): WhoEnv {
19 const statement = { bind: () => statement, run: async () => ({ success: true }) };
20 return { IDENTITY: { fetch: read }, DB: { prepare: () => statement } as unknown as D1Database };
21}
22
23test("a cold request cannot make another request wait on its workspace I/O", async () => {
24 forgetKept();
25 const first = deferred<Response>();
26 let calls = 0;
27 const workspace = { id: "ws_cold", slug: "cold" };
28 const env = environment(async () => ++calls === 1 ? first.promise : Response.json(workspace));
29 const one = new Who(env);
30 const pending = one.workspace("cold");
31 assert.equal(one.workspace("cold"), pending, "deduplicate within one request");
32 const two = new Who(env).workspace("cold");
33 assert.equal(calls, 2, "independent requests must start independent I/O on a miss");
34 assert.deepEqual(await two, workspace, "request two completes while request one is still pending");
35 first.resolve(Response.json(null));
36 assert.equal(await pending, null);
37 assert.deepEqual(await new Who(env).workspace("cold"), workspace);
38 assert.equal(calls, 2, "a late miss must not evict a successful cached answer");
39});
40
41test("failed workspace reads are retried by the next request", async () => {
42 forgetKept();
43 let calls = 0;
44 const env = environment(async () => {
45 if (++calls === 1) throw new Error("connection lost");
46 return Response.json({ id: "ws_retry", slug: "retry" });
47 });
48 assert.equal(await new Who(env).workspace("retry"), null);
49 assert.equal((await new Who(env).workspace("retry"))?.id, "ws_retry");
50 assert.equal(calls, 2);
51});