Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/client/test/box.test.ts?draft=login-timeout&format=json
215 linesFirst import 88% · pinch 11% · review 1%Last change pinch in version 1, 1 version changed it
1// Signing in once per box (abso issue 108): login waits for approval and keeps
2// the credential outside every project folder; it never shows up in output and
3// only goes to the server that issued it.
4
5import assert from "node:assert/strict";
6import { mkdtempSync, readdirSync, readFileSync } from "node:fs";
7import { tmpdir } from "node:os";
8import { join } from "node:path";
9import { test } from "node:test";
10import { boxPublic, boxText, forgetBox, login, logout, mintKey, readBox, writeBox } from "../src/box.ts";
12const SERVER = "https://staging.abso.example";
13const CREDENTIAL = "abso_bx_the-secret-credential";
14const grant = {
15 projects: [
16 { project: "abso", role: "writer" },
17 { project: "game", role: "reader" },
18 ],
19 max_agents: 5,
20};
21const EXPIRES = "2026-10-15T00:00:00.000Z";
22const record = { server: SERVER, id: "b_0123abcd", credential: CREDENTIAL, grant, expiresAt: Date.parse(EXPIRES) };
24const home = () => mkdtempSync(join(tmpdir(), "abso-box-"));
26type Seen = { url: string; auth: string | null; body: unknown }[];
27function server(answers: [number, unknown][], seen: Seen): typeof fetch {
28 return (async (url, init) => {
29 seen.push({
30 url: String(url),
31 auth: new Headers(init?.headers).get("authorization"),
32 body: JSON.parse(String(init?.body ?? "null")),
33 });
34 const [status, body] = answers.shift() ?? [500, { error: "no more answers" }];
35 return new Response(JSON.stringify(body), { status });
36 }) as typeof fetch;
39const started = {
40 device_code: "device-code-long-enough",
41 user_code: "WXYZ-BCDF",
42 verification_uri_complete: "https://abso.dev/login/box?code=WXYZ-BCDF",
43 expires_in: 60,
44 interval: 2,
45};
47function clock() {
48 let t = 0;
49 return {
50 sleep: async (ms: number) => {
51 t += ms;
52 },
53 now: () => t,
54 slept: () => t,
55 };
58test("login shows the code, waits while it's pending, and keeps the credential in the user folder", async () => {
59 const h = home();
60 const seen: Seen = [];
61 const said: string[] = [];
62 const c = clock();
63 const got = await login(
64 `${SERVER}/some/path`,
65 {
66 fetch: server(
67 [
68 [200, started],
69 [400, { error: "authorization_pending" }],
70 [400, { error: "slow_down" }],
71 [200, { box_id: "b_0123abcd", box_token: CREDENTIAL, expires_at: EXPIRES, grant }],
72 ],
73 seen,
74 ),
75 sleep: c.sleep,
76 now: c.now,
77 say: (line) => said.push(line),
78 },
79 h,
80 );
81 assert.equal(got.server, SERVER);
82 assert.deepEqual(
83 seen.map((s) => s.url),
84 [`${SERVER}/login/device`, `${SERVER}/login/token`, `${SERVER}/login/token`, `${SERVER}/login/token`],
85 );
86 assert.deepEqual(seen[1]?.body, { device_code: "device-code-long-enough" });
87 assert.equal(c.slept(), 2000 + 2000 + 7000, "slow_down adds 5 s to the wait");
88 assert.ok(
89 seen.every((s) => s.auth === null),
90 "nothing is sent with login's calls",
91 );
92 assert.deepEqual(said, [
93 "To sign this box in, open https://abso.dev/login/box?code=WXYZ-BCDF and approve the code WXYZ-BCDF.",
94 "Waiting for approval. Ctrl-C stops waiting, and abso stops by itself when the code runs out.",
95 ]);
96 assert.equal(readBox(SERVER, h)?.credential, CREDENTIAL);
97 const files = readdirSync(join(h, ".abso", "boxes"));
98 assert.equal(files.length, 1);
99 assert.ok(readFileSync(join(h, ".abso", "boxes", files[0]!), "utf8").includes(CREDENTIAL));
100 assert.equal(boxText(got, SERVER, 0).includes(CREDENTIAL), false);
101 assert.match(boxText(got, SERVER, 0), /abso \(writer\), game \(reader\)\. Up to 5 agents\./);
102 assert.equal(JSON.stringify(boxPublic(got)).includes(CREDENTIAL), false);
103});
105test("a code that's turned down or runs out says so and keeps nothing", async () => {
106 const h = home();
107 const c = clock();
108 const io = { sleep: c.sleep, now: c.now, say: () => {} };
109 await assert.rejects(
110 login(
111 SERVER,
112 {
113 ...io,
114 fetch: server(
115 [
116 [200, started],
117 [400, { error: "access_denied" }],
118 ],
119 [],
120 ),
121 },
122 h,
123 ),
124 /was turned down/,
125 );
126 await assert.rejects(
127 login(
128 SERVER,
129 {
130 ...io,
131 fetch: server(
132 [
133 [200, started],
134 [400, { error: "expired_token" }],
135 ],
136 [],
137 ),
138 },
139 h,
140 ),
141 /ran out\. Run abso login again/,
142 );
143 await assert.rejects(
144 login(
145 SERVER,
146 {
147 ...io,
148 fetch: server(
149 [
150 [200, { ...started, expires_in: 3 }],
151 [400, { error: "authorization_pending" }],
152 [400, { error: "authorization_pending" }],
153 ],
154 [],
155 ),
156 },
157 h,
158 ),
159 /in time\. Run abso login again/,
160 );
161 assert.equal(readBox(SERVER, h), undefined);
162});
164test("login says it's still waiting about once a minute, and stops in the end whatever the server says", async () => {
165 const c = clock();
166 const said: string[] = [];
167 const pending = Array.from({ length: 2000 }, (): [number, unknown] => [400, { error: "authorization_pending" }]);
168 await assert.rejects(
169 login(
170 SERVER,
171 {
172 fetch: server([[200, { ...started, expires_in: 10 ** 9, interval: 5 }], ...pending], []),
173 sleep: c.sleep,
174 now: c.now,
175 say: (line) => said.push(line),
176 },
177 home(),
178 ),
179 /nobody approved the code WXYZ-BCDF in time\. Run abso login again\./,
180 );
181 assert.equal(c.slept(), 30 * 60 * 1000, "a server that says never still gets an end");
182 const still = said.filter((line) => line === "Still waiting for the code WXYZ-BCDF to be approved.");
183 assert.equal(still.length, 30);
184 assert.equal(said.length, 2 + 30);
185});
187test("a key is minted with the box credential, sent only to the box's own server", async () => {
188 const seen: Seen = [];
189 const minted = await mintKey(
190 record,
191 "abso",
192 "tester-1",
193 server([[200, { key: "abso_sk_x", id: "k_0123abcd" }]], seen),
194 );
195 assert.deepEqual(minted, { key: "abso_sk_x", id: "k_0123abcd" });
196 assert.equal(seen[0]?.url.startsWith(`${SERVER}/`), true);
197 assert.equal(seen[0]?.auth, `Box ${CREDENTIAL}`);
198 await assert.rejects(
199 mintKey(record, "secret-project", "a", server([[403, { error: `outside the grant ${CREDENTIAL}` }]], [])),
200 (e: Error) => /refused \(403\)/.test(e.message) && !e.message.includes(CREDENTIAL),
201 );
202});
204test("a box's sign-in is kept per server, and logout forgets it even when the server is down", async () => {
205 const h = home();
206 writeBox(record, h);
207 assert.equal(readBox("https://abso.example", h), undefined, "another server's box isn't this one");
208 assert.match(boxText(undefined, SERVER), /isn't signed in/);
209 const down = (async () => {
210 throw new Error("offline");
211 }) as typeof fetch;
212 assert.equal(await logout(readBox(SERVER, h)!, down, h), false);
213 assert.equal(readBox(SERVER, h), undefined);
214 assert.equal(forgetBox(SERVER, h), false);
215});