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/caller.test.ts?format=json
104 linesgroups 87% · pinch 13%Last change pinch in version 282, 2 versions changed it
1// Who is calling a seat: one header with the chat's session, the computer and the folder, read back exactly or not at all.
2import assert from "node:assert/strict";
3import { test } from "node:test";
4import { CALLER_HEADER, CALLER_MAX, callerHeader, callerPlace, parseCaller } from "../../src/caller.ts";
5import { Api } from "../src/api.ts";
6import { callerFor } from "../src/command-env.ts";
7
8const session = "0da5c2b7-ab07-4249-b868-e8f09b6ec7b6";
9
10test("the client sends a hash of the session and only the folder's last part", () => {
11 const header = callerFor(session, "DESKTOP-1", "C:\\Users\\sam\\repos\\abso\\groups")!;
12 const caller = parseCaller(header)!;
13 assert.equal(caller.folder, "groups");
14 assert.match(caller.session!, /^[0-9a-f]{16}$/);
15 assert.notEqual(caller.session, session);
16 assert.doesNotMatch(Buffer.from(header, "base64url").toString(), /sam|0da5c2b7/);
17 // The same chat hashes the same way, so the server still tells one chat from another.
18 assert.equal(parseCaller(callerFor(session, "DESKTOP-1", "/x/groups"))!.session, caller.session);
19 assert.equal(parseCaller(callerFor(undefined, "box", "/work/über"))!.session, undefined);
20 assert.equal(callerPlace(caller), "groups on DESKTOP-1");
21});
23test("a caller goes out and comes back as it was", () => {
24 const header = callerHeader({ session, machine: "DESKTOP-1", folder: "C:\\Users\\sam\\repos\\abso\\groups" })!;
25 assert.match(header, /^[A-Za-z0-9_-]+$/);
26 assert.deepEqual(parseCaller(header), {
27 session,
28 machine: "DESKTOP-1",
29 folder: "C:\\Users\\sam\\repos\\abso\\groups",
30 });
31 assert.equal(callerPlace(parseCaller(header)!), "groups on DESKTOP-1");
32 // Outside a chat there's no session, and that's still a caller.
33 assert.deepEqual(parseCaller(callerHeader({ machine: "box", folder: "/work/über" })), {
34 machine: "box",
35 folder: "/work/über",
36 });
37});
39test("a session that isn't an id is left out, and a long folder keeps its end", () => {
40 assert.deepEqual(parseCaller(callerHeader({ session: "not a session!", machine: "m", folder: "/a" })), {
41 machine: "m",
42 folder: "/a",
43 });
44 const deep = `/${"very-long-folder-name/".repeat(40)}groups`;
45 const header = callerHeader({ session, machine: "m", folder: deep })!;
46 assert.ok(header.length <= CALLER_MAX);
47 assert.ok(parseCaller(header)!.folder.endsWith("/groups"));
48});
50test("a header that isn't a caller is refused, never guessed at", () => {
51 const enc = (v: unknown) => Buffer.from(JSON.stringify(v)).toString("base64url");
52 for (const bad of [
53 undefined,
54 "",
55 "not base64!",
56 enc("text"),
57 enc([1]),
58 enc({ machine: "m" }),
59 enc({ machine: "has space", folder: "/a" }),
60 enc({ machine: "m", folder: "/a", session: "bad id!" }),
61 enc({ machine: "m", folder: "/a", token: "x" }),
62 enc({ machine: "m", folder: "/a\nb" }),
63 "A".repeat(CALLER_MAX + 1),
64 ])
65 assert.equal(parseCaller(bad), null, String(bad));
66});
68test("the Api sends the caller with its calls, except from a visitor folder", () => {
69 const config = {
70 server: "https://abso.test",
71 project: "p",
72 agent: "ann",
73 token: "abso_sk_x",
74 } as ConstructorParameters<typeof Api>[0];
75 const api = new Api(config);
76 assert.equal(api.headers()[CALLER_HEADER], undefined);
77 api.caller = callerHeader({ session, machine: "m", folder: "/a" });
78 assert.equal(api.headers()[CALLER_HEADER], api.caller);
79 const visitor = new Api({ ...config, visitor: true } as ConstructorParameters<typeof Api>[0]);
80 visitor.caller = api.caller;
81 assert.equal(visitor.headers()[CALLER_HEADER], undefined);
82});
84test("a seat notice on any answer is printed once with the news", async () => {
85 const { takeNews } = await import("../src/command-env.ts");
86 takeNews();
87 const real = globalThis.fetch;
88 const line = "ann was used from another chat (groups on DESKTOP-2) in the last 30 minutes";
89 globalThis.fetch = (async () => Response.json({ ok: true, seatNotice: line })) as typeof fetch;
90 try {
91 const api = new Api({
92 server: "https://abso.test",
93 project: "p",
94 agent: "ann",
95 token: "abso_sk_x",
96 } as ConstructorParameters<typeof Api>[0]);
97 await api.json("GET", "/status");
98 await api.json("GET", "/status");
99 assert.deepEqual(takeNews(), [line]);
100 assert.deepEqual(takeNews(), []);
101 } finally {
102 globalThis.fetch = real;
103 }
104});