- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/hooks/register.test.ts?format=json
152 linesFirst import 49% · pinch 28% · box-grants 19% · review 3% · rename 1%Last change pinch in version 293, 8 versions changed it
1// The abso mod's news poll, against the engine (claude plugin test client):
2// an agent that isn't editing still gets announcements, within 30 seconds.
4import type { On } from "claude-code";
5import { expect, mock, test } from "claude-code/testing";
6import { HOOK_COMMAND } from "./stamp.ts";
8/** The world beneath the mod: a chat in /w/p, an abso folder when `inFolder`, and a server with `news` waiting. */
9function world(on: On, inFolder: boolean, news: string[][], server = "https://avcs.test") {
10 mock.env(on, { AVCS_TOKEN: "test-token" });
11 const clock = mock.clock(on, { now: 1_000_000 });
12 const asked: string[] = [];
13 const auth: string[] = [];
14 const clients: string[] = [];
15 const appended: string[] = [];
16 const written = new Map<string, string>();
17 // The engine hands hooks paths resolved for this machine (C:\w\p on Windows).
18 const norm = (p: string) => p.replace(/\\/g, "/").replace(/^[A-Za-z]:/, "");
19 on("session.start", (_$, e) => ({ cwd: (e as { cwd?: string }).cwd ?? "/w/p" }));
20 on("session.cwd", () => ({ value: "/w/p" }));
21 on("fs.exists", (_$, e) => ({ value: inFolder && norm(e.path) === "/w/p/.abso/config.json" }));
22 on("fs.read", (_$, e) => {
23 const path = norm(e.path);
24 if (inFolder && path === "/w/p/.abso/config.json")
25 return { value: JSON.stringify({ server, project: "p", agent: "bob" }) };
26 if (written.has(path)) return { value: written.get(path)! };
27 return { deny: `ENOENT: ${path}` };
28 });
29 on("fs.write", (_$, e) => {
30 written.set(norm(e.path), e.text);
31 return { value: undefined };
32 });
33 on("http.fetch", (_$, e) => {
34 asked.push(e.url);
35 auth.push((e.init?.headers as Record<string, string> | undefined)?.authorization ?? "none");
36 clients.push(String((e.init?.headers as Record<string, string> | undefined)?.["x-abso-client"]));
37 return { value: { status: 200, ok: true, headers: {}, text: JSON.stringify({ news: news.shift() ?? [] }) } };
38 });
39 // The engine stores the row itself: a session.append hook passes it on and answers what next answered.
40 on("session.append", (_$, e, next) => {
41 appended.push((e.message.content as { text: string }[])[0]!.text);
42 return next(e);
43 });
44 // Tools the chat calls run as asked.
45 on("tool.call", () => ({ result: { isError: false, text: "ok" } }) as never);
46 return { clock, asked, auth, clients, appended, written };
47}
49test("the news poll asks every 5 minutes in an abso folder, and keeps each item once", async ($, on) => {
50 // The kit has no conversation to append to, so the append itself is checked in a live session (#25);
51 // here: the poll's timing, the call, and the news log the view mod reads.
52 const w = world(on, true, [["Announcement from lead: stop pushing until v12 lands"], []]);
53 await $.session.start({ cwd: "/w/p" });
54 await w.clock.advance(299_000);
55 expect(w.asked).toEqual([]);
56 await w.clock.advance(1_000);
57 expect(w.asked).toEqual(["https://avcs.test/p/p/a/bob/news"]);
58 // avcs.test isn't a server the token goes to (#30): the call goes out without it.
59 expect(w.auth[0]).toBe("none");
60 // Which mod asked: avcs-mod/<version> once installed, avcs-mod/checkout from a checkout (#29).
61 expect(w.clients[0]).toMatch(/^avcs-mod\/(checkout|\d+\.\d+\.\d+(\+[\w.]+)?)$/);
62 await $.tool.call({ tool: "Bash", command: "ls" } as never);
63 await w.clock.advance(300_000);
64 expect(w.asked).toHaveLength(2);
65 const log = w.written.get("/w/p/.abso/news.log") ?? "";
66 expect(log.split("\n").filter((l) => l.includes("Announcement from lead"))).toHaveLength(1);
67});
69test("while the seat holds its news stream (abso 690), the poll stands down, and asks again once the mark runs out", async ($, on) => {
70 const w = world(on, true, [[], []]);
71 w.written.set("/w/p/.abso/news-stream.json", JSON.stringify({ until: 1_000_000 + 400_000 }));
72 await $.session.start({ cwd: "/w/p" });
73 await w.clock.advance(300_000);
74 expect(w.asked).toEqual([]);
75 await $.tool.call({ tool: "Bash", command: "ls" } as never);
76 await w.clock.advance(300_000);
77 expect(w.asked).toEqual(["https://avcs.test/p/p/a/bob/news"]);
78});
80test("a chat with no tool call for 10 minutes stops asking for news; its next tool call starts it again", async ($, on) => {
81 const w = world(on, true, [[], [], []]);
82 await $.session.start({ cwd: "/w/p" });
83 await w.clock.advance(300_000);
84 expect(w.asked).toHaveLength(1);
85 // Idle since the start: at 10 and 15 minutes it doesn't ask.
86 await w.clock.advance(600_000);
87 expect(w.asked).toHaveLength(1);
88 await $.tool.call({ tool: "Read", file_path: "/w/p/a.ts" } as never);
89 await w.clock.advance(300_000);
90 expect(w.asked).toHaveLength(2);
91});
93test("outside an abso folder it never asks", async ($, on) => {
94 const w = world(on, false, [["should not arrive"]]);
95 await $.session.start({ cwd: "/w/p" });
96 await w.clock.advance(120_000);
97 expect(w.asked).toEqual([]);
98 expect(w.appended).toEqual([]);
99});
101test("an edit that leaves a NUL byte in a text file is not sent, and the log says where", async ($, on) => {
102 const w = world(on, true, []);
103 w.written.set("/w/p/.abso/known/fuzz.ts", "one\ntwo\nthree\n");
104 w.written.set("/w/p/fuzz.ts", "one\ntwo\nth\u0000ree\n");
105 on("fs.stat", () => ({ value: { realPath: "/w/p/fuzz.ts" } }) as never);
106 on("session.id", () => ({ value: "sess" }));
107 await $.session.start({ cwd: "/w/p" });
108 await $.tool.call({
109 tool: "Write",
110 tool_use_id: "t1",
111 file_path: "/w/p/fuzz.ts",
112 content: "one\ntwo\nth\u0000ree\n",
113 } as never);
114 await w.clock.advance(1_000);
115 expect(w.written.get("/w/p/.abso/hook.log") ?? "").toContain("not sent: a NUL byte at line 3");
116 expect(w.asked.filter((u) => !u.endsWith("/news"))).toEqual([]);
117});
119test("a local project's edit goes to abso hook as Claude Code's command hook would get it, and its news to the chat", async ($, on) => {
120 const w = world(on, true, [], "local");
121 w.written.set("/w/p/notes.md", "hello\n");
122 on("fs.stat", () => ({ value: { realPath: "/w/p/notes.md" } }) as never);
123 on("session.id", () => ({ value: "sess" }));
124 const ran: { argv: readonly string[]; stdin?: string; env?: Record<string, string> }[] = [];
125 const context = "abso: Announcement from lead: hold pushes";
126 on("process.run", (_$, e) => {
127 ran.push({ argv: e.argv, stdin: e.init?.stdin, env: e.init?.env });
128 const stdout = JSON.stringify({ hookSpecificOutput: { hookEventName: "PostToolUse", additionalContext: context } });
129 return { value: { exitCode: 0, stdout, stderr: "" } } as never;
130 });
131 await $.session.start({ cwd: "/w/p" });
132 await $.tool.call({ tool: "Write", tool_use_id: "t1", file_path: "/w/p/notes.md", content: "hello\n" } as never);
133 await w.clock.advance(1_000);
134 // A local project has no server to call: nothing goes over the network.
135 expect(w.asked.filter((u) => !u.endsWith("/news"))).toEqual([]);
136 const log = w.written.get("/w/p/.abso/hook.log") ?? "";
137 if (HOOK_COMMAND === undefined) {
138 // Loaded from a checkout, the mod has no client to hand it to.
139 expect(ran).toEqual([]);
140 expect(log).toContain("skipped: a local project");
141 return;
142 }
143 expect(ran).toHaveLength(1);
144 expect(ran[0]!.argv).toEqual(HOOK_COMMAND);
145 expect(ran[0]!.env).toEqual({ ABSO_HOOK_FORCE: "1" });
146 const input = JSON.parse(ran[0]!.stdin ?? "{}") as Record<string, unknown>;
147 expect(input.session_id).toBe("sess");
148 expect(input.tool_name).toBe("Write");
149 expect((input.tool_input as Record<string, unknown>).file_path).toBe("/w/p/notes.md");
150 expect(log).toContain("handed to abso hook");
151 expect(w.appended).toEqual([context]);
152});