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

Versions 149 to 150

Compare

+224 −0 in 1 place
new file, 224 lines
1+import assert from "node:assert/strict";
2+import { describe, it } from "node:test";
3+import type { Env } from "../src/env.ts";
4+import { authorize, handle, ISSUES_ROUTE, PROJECT_ROUTE, sameSecret } from "../src/router.ts";
5+
6+const TOKEN = "test-token-for-the-router";
7+
8+/** A project namespace whose objects record every call and answer what each test needs. */
9+function fakeEnv(extra: Partial<Env> = {}) {
10+ const calls: { project: string; method: string; args: unknown[] }[] = [];
11+ const object = (project: string) =>
12+ new Proxy(
13+ {},
14+ {
15+ get:
16+ (_t, method: string) =>
17+ async (...args: unknown[]) => {
18+ calls.push({ project, method, args });
19+ if (method === "call") return { status: 200, body: { op: args[1], agent: args[2], body: args[3] } };
20+ if (method === "putBlob") return { status: 200, body: { stored: true } };
21+ if (method === "head") return { version: 3, at: "now" };
22+ if (method === "checkAgentKey")
23+ return { ok: true, id: "k_1", as: "key k_1", role: "writer", agent: "carol", dirty: false };
24+ if (method === "issueAgentKey") return { id: "k_2", key: "abso_sk_new", agent: args[0] };
25+ return null;
26+ },
27+ },
28+ );
29+ const env = {
30+ PROJECT: { idFromName: (n: string) => n, get: (id: string) => object(id) },
31+ ISSUES_DO: { idFromName: (n: string) => n, get: () => ({}) },
32+ ABSO_TOKEN: TOKEN,
33+ ...extra,
34+ } as unknown as Env;
35+ return { env, calls };
36+}
38+const ctx = {} as ExecutionContext;
39+const noIssues = async () => new Response("issues", { status: 299 });
40+const request = (path: string, init: RequestInit & { token?: string | null } = {}) => {
41+ const { token = TOKEN, ...rest } = init;
42+ const headers = new Headers(rest.headers);
43+ if (token) headers.set("authorization", `Bearer ${token}`);
44+ return new Request(`http://abso.test${path}`, { ...rest, headers });
45+};
47+describe("routes", () => {
48+ it("matches the project's top routes and an agent's ops", () => {
49+ assert.deepEqual(PROJECT_ROUTE.exec("/p/game/status")?.slice(1, 3), ["game", "status"]);
50+ assert.deepEqual(PROJECT_ROUTE.exec("/p/game/a/alice/publish")?.slice(1), [
51+ "game",
52+ "a/alice/publish",
53+ "alice",
54+ "publish",
55+ ]);
56+ assert.equal(PROJECT_ROUTE.exec("/p/game/change/12")?.[2], "change/12");
57+ assert.equal(PROJECT_ROUTE.exec("/p/game/a/alice/run"), null, "boxes are not on the open server");
58+ assert.equal(PROJECT_ROUTE.exec("/p/game/ci/log"), null, "nor CI's output");
59+ assert.equal(PROJECT_ROUTE.exec("/p/game/members"), null);
60+ assert.deepEqual(ISSUES_ROUTE.exec("/issues/p/game/issues/4")?.slice(1), ["game", "/issues/4"]);
61+ assert.deepEqual(ISSUES_ROUTE.exec("/issues/p/game")?.slice(1), ["game", undefined]);
62+ });
64+ it("sends an agent's op to the project's object, with the JSON body", async () => {
65+ const { env, calls } = fakeEnv();
66+ const res = await handle(
67+ request("/p/game/a/alice/publish", { method: "POST", body: JSON.stringify({ message: "why" }) }),
68+ env,
69+ ctx,
70+ noIssues,
71+ );
72+ assert.equal(res.status, 200);
73+ assert.deepEqual(await res.json(), { op: "publish", agent: "alice", body: { message: "why" } });
74+ assert.equal(calls[0]?.project, "game");
75+ });
77+ it("reads take their query as the body, and a read refuses a POST-only op as a GET", async () => {
78+ const { env } = fakeEnv();
79+ const log = await handle(request("/p/game/log?limit=5"), env, ctx, noIssues);
80+ assert.deepEqual(await log.json(), { op: "log", body: { limit: "5" } });
81+ const publish = await handle(request("/p/game/a/alice/publish"), env, ctx, noIssues);
82+ assert.equal(publish.status, 405);
83+ });
85+ it("refuses a project name the engine wouldn't take", async () => {
86+ const { env, calls } = fakeEnv();
87+ const res = await handle(request("/p/..bad/status"), env, ctx, noIssues);
88+ assert.equal(res.status, 400);
89+ assert.equal(calls.length, 0);
90+ });
92+ it("answers head from the object, and ci and queue as empty", async () => {
93+ const { env } = fakeEnv();
94+ assert.deepEqual(await (await handle(request("/p/game/head"), env, ctx, noIssues)).json(), {
95+ version: 3,
96+ at: "now",
97+ });
98+ const ci = (await (await handle(request("/p/game/ci"), env, ctx, noIssues)).json()) as Record<string, unknown>;
99+ assert.equal(ci.red, null);
100+ assert.deepEqual(await (await handle(request("/p/game/queue"), env, ctx, noIssues)).json(), {
101+ items: [],
102+ paused: null,
103+ });
104+ });
106+ it("checks a blob's name before its bytes go anywhere", async () => {
107+ const { env, calls } = fakeEnv();
108+ const bad = await handle(request("/p/game/blobs/not-a-hash", { method: "PUT", body: "x" }), env, ctx, noIssues);
109+ assert.equal(bad.status, 400);
110+ const upper = await handle(
111+ request(`/p/game/blobs/${"A".repeat(64)}`, { method: "PUT", body: "x" }),
112+ env,
113+ ctx,
114+ noIssues,
115+ );
116+ assert.equal(upper.status, 400);
117+ assert.equal(calls.length, 0);
118+ const good = await handle(
119+ request(`/p/game/blobs/${"a".repeat(64)}`, { method: "PUT", body: "x" }),
120+ env,
121+ ctx,
122+ noIssues,
123+ );
124+ assert.equal(good.status, 200);
125+ assert.equal(calls[0]?.method, "putBlob");
126+ });
128+ it("hands /issues to the issues code with the rest of the path and who is calling", async () => {
129+ const { env } = fakeEnv();
130+ let seen: unknown;
131+ const res = await handle(request("/issues/p/game/issues?open=1"), env, ctx, async (_r, _e, _c, where) => {
132+ seen = where;
133+ return new Response("ok");
134+ });
135+ assert.equal(await res.text(), "ok");
136+ assert.deepEqual(seen, { project: "game", rest: "/issues", auth: { kind: "token" } });
137+ });
139+ it("gives the issues code a key's agent and role", async () => {
140+ const { env } = fakeEnv({ AGENT_KEYS: "on", KEY_HMAC_SECRET: "s" });
141+ let seen: unknown;
142+ await handle(request("/issues/p/game/issues", { token: "abso_sk_abc" }), env, ctx, async (_r, _e, _c, where) => {
143+ seen = where;
144+ return new Response("ok");
145+ });
146+ assert.deepEqual(seen, { project: "game", rest: "/issues", auth: { kind: "key", agent: "carol", role: "writer" } });
147+ });
149+ it("answers /version without a token", async () => {
150+ const { env } = fakeEnv({ ABSO_TOKEN: undefined });
151+ const res = await handle(request("/version", { token: null }), env, ctx, noIssues);
152+ assert.equal(res.status, 200);
153+ });
154+});
156+describe("auth", () => {
157+ it("refuses everything when no shared token is set", async () => {
158+ const { env, calls } = fakeEnv({ ABSO_TOKEN: undefined });
159+ const res = await handle(request("/p/game/status", { token: "anything" }), env, ctx, noIssues);
160+ assert.equal(res.status, 503);
161+ assert.match(((await res.json()) as { error: string }).error, /no ABSO_TOKEN/);
162+ const empty = await authorize(request("/p/game/status", { token: null }), { ...env, ABSO_TOKEN: "" });
163+ assert.ok(empty instanceof Response && empty.status === 503);
164+ assert.equal(calls.length, 0);
165+ });
167+ it("refuses a missing or wrong token, and takes the right one", async () => {
168+ const { env, calls } = fakeEnv();
169+ assert.equal((await handle(request("/p/game/status", { token: null }), env, ctx, noIssues)).status, 401);
170+ assert.equal((await handle(request("/p/game/status", { token: "wrong" }), env, ctx, noIssues)).status, 401);
171+ assert.equal((await handle(request("/p/game/status", { token: `${TOKEN}x` }), env, ctx, noIssues)).status, 401);
172+ assert.equal((await handle(request("/", { token: "wrong" }), env, ctx, noIssues)).status, 401);
173+ assert.equal(calls.length, 0);
174+ assert.equal((await handle(request("/p/game/status"), env, ctx, noIssues)).status, 200);
175+ });
177+ it("takes the token under its old name, AVCS_TOKEN", async () => {
178+ const { env } = fakeEnv({ ABSO_TOKEN: undefined, AVCS_TOKEN: TOKEN });
179+ assert.deepEqual(await authorize(request("/p/game/status"), env), { kind: "token" });
180+ });
182+ it("treats an agent key as a key only when keys are on", async () => {
183+ const off = fakeEnv();
184+ const refused = await authorize(request("/p/game/status", { token: "abso_sk_abc" }), off.env);
185+ assert.ok(refused instanceof Response && refused.status === 401);
186+ const on = fakeEnv({ AGENT_KEYS: "on", KEY_HMAC_SECRET: "s" });
187+ assert.deepEqual(await authorize(request("/p/game/status", { token: "abso_sk_abc" }), on.env), {
188+ kind: "key",
189+ presented: "abso_sk_abc",
190+ });
191+ });
193+ it("compares secrets whatever their lengths", async () => {
194+ assert.equal(await sameSecret("abc", "abc"), true);
195+ assert.equal(await sameSecret("abc", "abcd"), false);
196+ assert.equal(await sameSecret("", "abc"), false);
197+ });
199+ it("issues agent keys only with the admin token beside the shared one", async () => {
200+ const { env, calls } = fakeEnv({ AGENT_KEYS: "on", KEY_HMAC_SECRET: "s", ABSO_ADMIN_TOKEN: "admin-secret" });
201+ const path = "/admin/agent-keys?project=game&agent=carol";
202+ assert.equal((await handle(request(path, { method: "POST" }), env, ctx, noIssues)).status, 403);
203+ const wrong = request(path, { method: "POST", headers: { "x-abso-admin": "nope" } });
204+ assert.equal((await handle(wrong, env, ctx, noIssues)).status, 403);
205+ const key = request(path, { method: "POST", token: "abso_sk_abc", headers: { "x-abso-admin": "admin-secret" } });
206+ assert.equal((await handle(key, env, ctx, noIssues)).status, 401);
207+ assert.equal(calls.length, 0);
208+ const ok = request(path, { method: "POST", headers: { "x-abso-admin": "admin-secret" } });
209+ const res = await handle(ok, env, ctx, noIssues);
210+ assert.equal(res.status, 200);
211+ assert.deepEqual(await res.json(), { id: "k_2", key: "abso_sk_new", agent: "carol" });
212+ });
214+ it("has no agent keys routes while keys are off", async () => {
215+ const { env } = fakeEnv({ ABSO_ADMIN_TOKEN: "admin-secret" });
216+ const res = await handle(
217+ request("/admin/agent-keys?project=game", { headers: { "x-abso-admin": "admin-secret" } }),
218+ env,
219+ ctx,
220+ noIssues,
221+ );
222+ assert.equal(res.status, 404);
223+ });
224+});
MCP
compare on this site's /mcp
CLI
abso diff 149 150 server/test/router.test.ts --json
JSON
/christopher/abso-core/compare/149..150?file=server%2Ftest%2Frouter.test.ts&format=json