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.

55 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(
11 `data:text/javascript;base64,${Buffer.from(bundle.outputFiles[0].text).toString("base64")}`
12);
13
14function deferred<T>() {
15 let resolve!: (value: T) => void;
16 const promise = new Promise<T>((done) => {
17 resolve = done;
18 });
19 return { promise, resolve };
20}
21
22function environment(read: () => Promise<Response>): WhoEnv {
23 const statement = { bind: () => statement, run: async () => ({ success: true }) };
24 return { IDENTITY: { fetch: read }, DB: { prepare: () => statement } as unknown as D1Database };
25}
26
27test("a cold request cannot make another request wait on its workspace I/O", async () => {
28 forgetKept();
29 const first = deferred<Response>();
30 let calls = 0;
31 const workspace = { id: "ws_cold", slug: "cold" };
32 const env = environment(async () => (++calls === 1 ? first.promise : Response.json(workspace)));
33 const one = new Who(env);
34 const pending = one.workspace("cold");
35 assert.equal(one.workspace("cold"), pending, "deduplicate within one request");
36 const two = new Who(env).workspace("cold");
37 assert.equal(calls, 2, "independent requests must start independent I/O on a miss");
38 assert.deepEqual(await two, workspace, "request two completes while request one is still pending");
39 first.resolve(Response.json(null));
40 assert.equal(await pending, null);
41 assert.deepEqual(await new Who(env).workspace("cold"), workspace);
42 assert.equal(calls, 2, "a late miss must not evict a successful cached answer");
43});
44
45test("failed workspace reads are retried by the next request", async () => {
46 forgetKept();
47 let calls = 0;
48 const env = environment(async () => {
49 if (++calls === 1) throw new Error("connection lost");
50 return Response.json({ id: "ws_retry", slug: "retry" });
51 });
52 assert.equal(await new Who(env).workspace("retry"), null);
53 assert.equal((await new Who(env).workspace("retry"))?.id, "ws_retry");
54 assert.equal(calls, 2);
55});