- 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?version=7&format=json
89 linesFirst import 100%Last change First import in version 0, 1 version changed it
You're reading version 7. The newest is 318, and 7 versions since then changed this file.
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";
7/** The world beneath the mod: a chat in /w/p, an abso folder when `inFolder`, and a server with `news` waiting. */
8function world(on: On, inFolder: boolean, news: string[][]) {
9 mock.env(on, { AVCS_TOKEN: "test-token" });
10 const clock = mock.clock(on, { now: 1_000_000 });
11 const asked: string[] = [];
12 const auth: string[] = [];
13 const clients: string[] = [];
14 const appended: string[] = [];
15 const written = new Map<string, string>();
16 // The engine hands hooks paths resolved for this machine (C:\w\p on Windows).
17 const norm = (p: string) => p.replace(/\\/g, "/").replace(/^[A-Za-z]:/, "");
18 on("session.start", (_$, e) => ({ cwd: (e as { cwd?: string }).cwd ?? "/w/p" }));
19 on("session.cwd", () => ({ value: "/w/p" }));
20 on("fs.exists", (_$, e) => ({ value: inFolder && norm(e.path) === "/w/p/.avcs/config.json" }));
21 on("fs.read", (_$, e) => {
22 const path = norm(e.path);
23 if (inFolder && path === "/w/p/.avcs/config.json")
24 return { value: JSON.stringify({ server: "https://avcs.test", project: "p", agent: "bob" }) };
25 if (written.has(path)) return { value: written.get(path)! };
26 return { deny: `ENOENT: ${path}` };
27 });
28 on("fs.write", (_$, e) => {
29 written.set(norm(e.path), e.text);
30 return { value: undefined };
31 });
32 on("http.fetch", (_$, e) => {
33 asked.push(e.url);
34 auth.push((e.init?.headers as Record<string, string> | undefined)?.authorization ?? "none");
35 clients.push(String((e.init?.headers as Record<string, string> | undefined)?.["x-avcs-client"]));
36 return { value: { status: 200, ok: true, headers: {}, text: JSON.stringify({ news: news.shift() ?? [] }) } };
37 });
38 on("session.append", (_$, e) => {
39 appended.push((e.message.content as { text: string }[])[0]!.text);
40 return { value: { message: e.message, uuid: `u${appended.length}` } } as never;
41 });
42 return { clock, asked, auth, clients, appended, written };
43}
45test("the news poll asks every 30 seconds in an abso folder, and keeps each item once", async ($, on) => {
46 // The kit has no conversation to append to, so the append itself is checked in a live session (#25);
47 // here: the poll's timing, the call, and the news log the view mod reads.
48 const w = world(on, true, [["Announcement from lead: stop publishing until v12 lands"], []]);
49 await $.session.start({ cwd: "/w/p" });
50 await w.clock.advance(29_000);
51 expect(w.asked).toEqual([]);
52 await w.clock.advance(1_000);
53 expect(w.asked).toEqual(["https://avcs.test/p/p/a/bob/news"]);
54 // avcs.test isn't a server the token goes to (#30): the call goes out without it.
55 expect(w.auth[0]).toBe("none");
56 // Which mod asked: avcs-mod/<version> once installed, avcs-mod/checkout from a checkout (#29).
57 expect(w.clients[0]).toMatch(/^avcs-mod\/(checkout|\d+\.\d+\.\d+(\+[\w.]+)?)$/);
58 await w.clock.advance(30_000);
59 expect(w.asked).toHaveLength(2);
60 const log = w.written.get("/w/p/.avcs/news.log") ?? "";
61 expect(log.split("\n").filter((l) => l.includes("Announcement from lead"))).toHaveLength(1);
62});
64test("outside an abso folder it never asks", async ($, on) => {
65 const w = world(on, false, [["should not arrive"]]);
66 await $.session.start({ cwd: "/w/p" });
67 await w.clock.advance(120_000);
68 expect(w.asked).toEqual([]);
69 expect(w.appended).toEqual([]);
70});
72test("an edit that leaves a NUL byte in a text file is not sent, and the log says where", async ($, on) => {
73 const w = world(on, true, []);
74 w.written.set("/w/p/.avcs/known/fuzz.ts", "one\ntwo\nthree\n");
75 w.written.set("/w/p/fuzz.ts", "one\ntwo\nth\u0000ree\n");
76 on("fs.stat", () => ({ value: { realPath: "/w/p/fuzz.ts" } }) as never);
77 on("tool.call", () => ({ result: { isError: false, text: "ok" } }) as never);
78 on("session.id", () => ({ value: "sess" }));
79 await $.session.start({ cwd: "/w/p" });
80 await $.tool.call({
81 tool: "Write",
82 tool_use_id: "t1",
83 file_path: "/w/p/fuzz.ts",
84 content: "one\ntwo\nth\u0000ree\n",
85 } as never);
86 await w.clock.advance(1_000);
87 expect(w.written.get("/w/p/.avcs/hook.log") ?? "").toContain("not sent: a NUL byte at line 3");
88 expect(w.asked.filter((u) => !u.endsWith("/news"))).toEqual([]);
89});