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/install.test.ts?version=2&format=json
389 linesFirst import 100%Last change First import in version 0, 1 version changed it

You're reading version 2. The newest is 318, and 9 versions since then changed this file. Go to the newestCompare with 318

1// Installing a mod (src/install.ts, #29): a staged copy that passed its check
2// goes in file by file with renames, plugin.json last; what it replaces is
3// kept for a rollback; a failed check or a failed rename leaves the mod as it
4// was. Every install here goes into a scratch folder. And which client this
5// is (src/version.ts): the commit, read from .git the way git reads it.
6
7import assert from "node:assert/strict";
8import { spawnSync } from "node:child_process";
9import {
10 copyFileSync,
11 existsSync,
12 mkdirSync,
13 mkdtempSync,
14 readdirSync,
15 readFileSync,
16 renameSync,
17 rmSync,
18 statSync,
19 utimesSync,
20 writeFileSync,
21} from "node:fs";
22import { tmpdir } from "node:os";
23import { dirname, join, relative } from "node:path";
24import { after, describe, it } from "node:test";
25import { Api } from "../src/api.ts";
26import { clientVersion, headCommit } from "../src/client-version.ts";
27import { installMod, type ModSpec, modsBehindText, rollbackMod, versionFor, versionReport } from "../src/install.ts";
28import { main } from "../src/main.ts";
30const hasGit = spawnSync("git", ["--version"]).status === 0;
31const dirs: string[] = [];
32after(() => {
33 for (const d of dirs) rmSync(d, { recursive: true, force: true, maxRetries: 5 });
34});
36function temp(): string {
37 const d = mkdtempSync(join(tmpdir(), "avcs-install-test-"));
38 dirs.push(d);
39 return d;
42const FILES = ["src/core.ts", "hooks/stamp.ts", "hooks/register.ts", "hooks/hooks.json", ".claude-plugin/plugin.json"];
43const pass = () => undefined;
44const at = (dir: string, rel: string) => join(dir, ...rel.split("/"));
45const read = (dir: string, rel: string) => readFileSync(at(dir, rel), "utf8");
46const write = (dir: string, rel: string, text: string) => {
47 mkdirSync(dirname(at(dir, rel)), { recursive: true });
48 writeFileSync(at(dir, rel), text);
49};
51/** A mod's source folder: a small mod shaped like avcs, with these texts in place of the defaults. */
52function source(texts: Record<string, string> = {}): ModSpec {
53 const from = temp();
54 const all: Record<string, string> = {
55 "src/core.ts": "export const core = 1\n",
56 "hooks/stamp.ts": "// Which mod this is.\nexport const STAMP: string | undefined = undefined\n",
57 "hooks/register.ts": "import { core } from '../src/core.ts'\n",
58 "hooks/hooks.json": '{ "modules": ["./register.ts"] }\n',
59 ".claude-plugin/plugin.json": '{ "name": "avcs", "version": "0.1.0", "description": "a mod" }\n',
60 ...texts,
61 };
62 for (const [rel, text] of Object.entries(all)) write(from, rel, text);
63 return { name: "avcs", from, files: FILES };
66/** Every file under dir, relative and sorted. */
67function tree(dir: string): string[] {
68 return readdirSync(dir, { recursive: true, withFileTypes: true })
69 .filter((e) => e.isFile())
70 .map((e) => relative(dir, join(e.parentPath, e.name)).replace(/\\/g, "/"))
71 .sort();
74const contents = (dir: string) => Object.fromEntries(tree(dir).map((rel) => [rel, read(dir, rel)]));
75const record = (dir: string) =>
76 JSON.parse(read(dir, ".avcs-install.json")) as {
77 version: string;
78 files: string[];
79 prev?: { version: string; files: string[] };
80 moving?: string;
81 };
83describe("installing a mod", () => {
84 it("stamps the version into plugin.json and stamp.ts, and leaves nothing staged behind", () => {
85 const spec = source();
86 const target = join(temp(), "avcs");
87 const r = installMod(spec, { target, check: pass, commit: "702c616" });
88 assert.deepEqual(r, { changed: true, version: "0.1.0+702c616", previous: undefined, files: 5 });
89 assert.deepEqual(JSON.parse(read(target, ".claude-plugin/plugin.json")), {
90 name: "avcs",
91 version: "0.1.0+702c616",
92 description: "a mod",
93 });
94 assert.equal(
95 read(target, "hooks/stamp.ts"),
96 "// Which mod this is.\nexport const STAMP: string | undefined = '0.1.0+702c616'\n",
97 );
98 assert.equal(read(target, "src/core.ts"), "export const core = 1\n");
99 assert.deepEqual(tree(target), [".avcs-install.json", ...FILES].sort());
100 assert.deepEqual(
101 { ...record(target), at: undefined, from: undefined },
102 { mod: "avcs", version: "0.1.0+702c616", files: FILES, at: undefined, from: undefined },
103 );
104 });
106 it("moves the files in by rename, what the hook imports first and plugin.json last", () => {
107 const spec = source();
108 const target = join(temp(), "avcs");
109 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
110 write(spec.from, "src/core.ts", "export const core = 2\n");
111 const order: string[] = [];
112 installMod(spec, {
113 target,
114 check: pass,
115 commit: "bbbbbbb",
116 rename: (from, to) => {
117 assert.ok(from.endsWith(".next"));
118 order.push(relative(target, to).replace(/\\/g, "/"));
119 renameSync(from, to);
120 },
121 });
122 assert.deepEqual(order, FILES);
123 });
125 it("checks the staged copy, stamped, and when the check fails changes nothing", () => {
126 const spec = source();
127 const target = join(temp(), "avcs");
128 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
129 const before = contents(target);
130 write(spec.from, "src/core.ts", "export const core = 2\n");
131 let staged = "";
132 const failing = (dir: string) => {
133 staged = read(dir, "hooks/stamp.ts") + read(dir, "src/core.ts");
134 return "2 tests failed";
135 };
136 assert.throws(
137 () => installMod(spec, { target, check: failing, commit: "bbbbbbb" }),
138 /failed its check, so nothing was changed:\n2 tests failed/,
139 );
140 assert.match(staged, /'0\.1\.0\+bbbbbbb'[\s\S]*core = 2/);
141 assert.deepEqual(contents(target), before);
142 });
144 it("leaves no folder behind when a first install fails its check", () => {
145 const target = join(temp(), "avcs");
146 assert.throws(() => installMod(source(), { target, check: () => "no", commit: "aaaaaaa" }), /nothing was changed/);
147 assert.equal(existsSync(target), false);
148 });
150 it("changes nothing, and checks nothing, when the same files are installed again", () => {
151 const spec = source();
152 const target = join(temp(), "avcs");
153 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
154 const mtime = statSync(at(target, "hooks/register.ts")).mtimeMs;
155 let checked = false;
156 const again = installMod(spec, { target, check: () => ((checked = true), undefined), commit: "aaaaaaa" });
157 assert.equal(again.changed, false);
158 assert.equal(checked, false);
159 assert.equal(statSync(at(target, "hooks/register.ts")).mtimeMs, mtime);
160 });
162 it("keeps what it replaces; a rollback puts it back, and a second rollback undoes the first", () => {
163 const spec = source();
164 const target = join(temp(), "avcs");
165 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
166 write(spec.from, "src/core.ts", "export const core = 2\n");
167 const r = installMod(spec, { target, check: pass, commit: "bbbbbbb" });
168 assert.equal(r.previous, "0.1.0+aaaaaaa");
169 assert.equal(read(target, "src/core.ts.prev"), "export const core = 1\n");
171 const back = rollbackMod(spec, { target });
172 assert.deepEqual(back, { changed: true, version: "0.1.0+aaaaaaa", previous: "0.1.0+bbbbbbb", files: 5 });
173 assert.equal(read(target, "src/core.ts"), "export const core = 1\n");
174 assert.equal(JSON.parse(read(target, ".claude-plugin/plugin.json")).version, "0.1.0+aaaaaaa");
175 assert.match(read(target, "hooks/stamp.ts"), /'0\.1\.0\+aaaaaaa'/);
177 const forward = rollbackMod(spec, { target });
178 assert.equal(forward.version, "0.1.0+bbbbbbb");
179 assert.equal(read(target, "src/core.ts"), "export const core = 2\n");
180 assert.equal(record(target).prev?.version, "0.1.0+aaaaaaa");
181 });
183 it("puts back the files already moved when a rename fails midway", () => {
184 const spec = source();
185 const target = join(temp(), "avcs");
186 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
187 const before = Object.fromEntries(FILES.map((rel) => [rel, read(target, rel)]));
188 write(spec.from, "src/core.ts", "export const core = 2\n");
189 write(spec.from, "hooks/register.ts", "import { core } from '../src/core.ts'\nexport const two = core\n");
190 let n = 0;
191 const flaky = (from: string, to: string) => {
192 if (++n === 3) throw Object.assign(new Error("EPERM: operation not permitted, rename"), { code: "EPERM" });
193 renameSync(from, to);
194 };
195 assert.throws(() => installMod(spec, { target, check: pass, commit: "bbbbbbb", rename: flaky }), /EPERM/);
196 assert.deepEqual(Object.fromEntries(FILES.map((rel) => [rel, read(target, rel)])), before);
197 assert.deepEqual(
198 tree(target).filter((f) => f.endsWith(".next")),
199 [],
200 );
201 assert.equal(record(target).version, "0.1.0+aaaaaaa");
202 assert.equal(record(target).moving, undefined);
203 });
205 it("keeps a mod installed before .avcs-install.json for a rollback", () => {
206 const spec = source();
207 const target = join(temp(), "avcs");
208 // The old installer copied four files and wrote nothing else.
209 for (const rel of FILES.filter((f) => f !== "hooks/stamp.ts")) {
210 mkdirSync(dirname(at(target, rel)), { recursive: true });
211 copyFileSync(at(spec.from, rel), at(target, rel));
212 }
213 const r = installMod(spec, { target, check: pass, commit: "bbbbbbb" });
214 assert.equal(r.previous, "0.1.0");
215 const back = rollbackMod(spec, { target });
216 assert.equal(back.version, "0.1.0");
217 assert.equal(JSON.parse(read(target, ".claude-plugin/plugin.json")).version, "0.1.0");
218 assert.equal(existsSync(at(target, "hooks/stamp.ts")), false);
219 });
221 it("after an install that stopped midway, keeps the last whole install for a rollback", () => {
222 const spec = source();
223 const target = join(temp(), "avcs");
224 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
225 write(spec.from, "src/core.ts", "export const core = 2\n");
226 installMod(spec, { target, check: pass, commit: "bbbbbbb" });
227 // An install of ccccccc that died after its first rename: core.ts is new, the rest is bbbbbbb.
228 for (const rel of FILES) copyFileSync(at(target, rel), at(target, `${rel}.prev`));
229 writeFileSync(
230 at(target, ".avcs-install.json"),
231 JSON.stringify({ ...record(target), prev: { ...record(target), prev: undefined }, moving: "0.1.0+ccccccc" }),
232 );
233 write(target, "src/core.ts", "export const core = 3\n");
235 write(spec.from, "src/core.ts", "export const core = 4\n");
236 const r = installMod(spec, { target, check: pass, commit: "ddddddd" });
237 assert.equal(r.previous, "0.1.0+bbbbbbb");
238 assert.equal(read(target, "src/core.ts.prev"), "export const core = 2\n");
239 rollbackMod(spec, { target });
240 assert.equal(read(target, "src/core.ts"), "export const core = 2\n");
241 });
243 it("refuses while another install holds the lock, and takes over a lock left by one that died", () => {
244 const spec = source();
245 const target = join(temp(), "avcs");
246 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
247 write(spec.from, "src/core.ts", "export const core = 2\n");
248 const lock = at(target, ".avcs-install.lock");
249 writeFileSync(lock, "");
250 assert.throws(() => installMod(spec, { target, check: pass, commit: "bbbbbbb" }), /another install is running/);
251 assert.throws(() => rollbackMod(spec, { target }), /another install is running/);
252 const old = new Date(Date.now() - 11 * 60_000);
253 utimesSync(lock, old, old);
254 assert.equal(installMod(spec, { target, check: pass, commit: "bbbbbbb" }).changed, true);
255 assert.equal(existsSync(lock), false);
256 });
258 it("says so when there is nothing to roll back to", () => {
259 const spec = source();
260 const target = join(temp(), "avcs");
261 assert.throws(() => rollbackMod(spec, { target }), /nothing is installed/);
262 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
263 assert.throws(() => rollbackMod(spec, { target }), /no earlier install is kept/);
264 });
266 it("stamps plugin.json's version plus the last commit that changed the mod's files, and .dirty when they differ from it", {
267 skip: !hasGit,
268 }, () => {
269 const spec = source();
270 const git = (...args: string[]) =>
271 spawnSync("git", ["-c", "user.name=t", "-c", "user.email=t@t", "-C", spec.from, ...args], {
272 encoding: "utf8",
273 }).stdout.trim();
274 git("init", "-q");
275 git("add", ".");
276 git("commit", "-q", "-m", "mod");
277 const mod = git("rev-parse", "--short=7", "HEAD");
278 assert.equal(versionFor(spec), `0.1.0+${mod}`);
279 // A commit that changes none of the mod's files leaves the stamp, so a reinstall changes nothing.
280 write(spec.from, "README.md", "not part of the mod\n");
281 assert.equal(versionFor(spec), `0.1.0+${mod}`);
282 git("add", ".");
283 git("commit", "-q", "-m", "docs");
284 assert.equal(versionFor(spec), `0.1.0+${mod}`);
285 const target = join(temp(), "avcs");
286 installMod(spec, { target, check: pass });
287 assert.equal(installMod(spec, { target, check: pass }).changed, false);
288 write(spec.from, "src/core.ts", "export const core = 2\n");
289 assert.equal(versionFor(spec), `0.1.0+${mod}.dirty`);
290 git("commit", "-q", "-am", "core");
291 assert.equal(versionFor(spec), `0.1.0+${git("rev-parse", "--short=7", "HEAD")}`);
292 // Outside a git checkout there is no commit to add.
293 assert.equal(versionFor(spec, null), "0.1.0");
294 });
295});
297describe("which client this is", () => {
298 const git = (cwd: string, ...args: string[]) =>
299 spawnSync(
300 "git",
301 ["-c", "user.name=t", "-c", "user.email=t@t", "-c", "init.defaultBranch=main", "-C", cwd, ...args],
302 { encoding: "utf8" },
303 ).stdout.trim();
305 it("reads the commit from .git as git does: a branch, a packed branch, a detached head, a worktree", {
306 skip: !hasGit,
307 }, () => {
308 const repo = temp();
309 git(repo, "init", "-q");
310 write(repo, "a/b/file.txt", "one\n");
311 git(repo, "add", ".");
312 git(repo, "commit", "-q", "-m", "one");
313 assert.equal(headCommit(join(repo, "a", "b")), git(repo, "rev-parse", "--short=7", "HEAD"));
314 write(repo, "a/b/file.txt", "two\n");
315 git(repo, "commit", "-q", "-am", "two");
316 git(repo, "pack-refs", "--all");
317 assert.equal(existsSync(join(repo, ".git", "refs", "heads", "main")), false);
318 assert.equal(headCommit(repo), git(repo, "rev-parse", "--short=7", "HEAD"));
319 const first = git(repo, "rev-parse", "--short=7", "HEAD~1");
320 git(repo, "checkout", "-q", "--detach", "HEAD~1");
321 assert.equal(headCommit(repo), first);
322 const wt = join(temp(), "wt");
323 git(repo, "worktree", "add", "-q", "-b", "side", wt, "main");
324 write(wt, "a/b/file.txt", "three\n");
325 git(wt, "commit", "-q", "-am", "three");
326 assert.equal(headCommit(join(wt, "a")), git(wt, "rev-parse", "--short=7", "HEAD"));
327 assert.notEqual(headCommit(wt), headCommit(repo));
328 });
330 it("has no commit outside a repository", () => {
331 assert.equal(headCommit(temp()), undefined);
332 });
334 it("avcs --version names this client's version and the mods installed, and which are from another commit", async () => {
335 const config = temp();
336 const spec = source();
337 installMod(spec, {
338 target: join(config, "skills", "avcs"),
339 check: pass,
340 commit: "aaaaaaa",
341 now: new Date("2026-10-05T04:30:00Z"),
342 });
343 const out = await main(["--version"], { cwd: config, env: { CLAUDE_CONFIG_DIR: config }, write: () => {} });
344 const [client, mod, view] = out.stdout.trimEnd().split("\n");
345 assert.equal(out.exit, 0);
346 assert.equal(client, `abso ${clientVersion()} (${dirname(import.meta.dirname)})`);
347 assert.match(
348 mod!,
349 /^the avcs mod: 0\.1\.0\+aaaaaaa in .*skills[\\/]avcs, installed 2026-10-05 04:30 UTC; this client has 0\.1\.0(\+[0-9a-f]{7}(\.dirty)?)?: node client\/install-mod\.ts installs it$/,
350 );
351 assert.equal(view, "the avcs-view mod: not installed (node client/install-view.ts)");
352 const json = JSON.parse(
353 (await main(["--version", "--json"], { cwd: config, env: { CLAUDE_CONFIG_DIR: config }, write: () => {} }))
354 .stdout,
355 );
356 assert.equal(json.client, clientVersion());
357 assert.equal(json.mods[0].version, "0.1.0+aaaaaaa");
358 });
360 it("status says when an installed mod is behind this client, where the client knows its commit (abso issue 41)", () => {
361 const config = temp();
362 const env = { CLAUDE_CONFIG_DIR: config };
363 assert.deepEqual(modsBehindText(env), [], "nothing installed, nothing to say");
364 installMod(source(), {
365 target: join(config, "skills", "avcs"),
366 check: pass,
367 commit: "aaaaaaa",
368 now: new Date("2026-10-05T04:30:00Z"),
369 });
370 const current = (versionReport(env).json.mods as { current: string }[])[0]!.current;
371 if (/\+[0-9a-f]{7}$/.test(current))
372 assert.deepEqual(modsBehindText(env), [
373 `The avcs mod installed on this machine is 0.1.0+aaaaaaa, and this client has ${current}: node client/install-mod.ts installs it (new chats load it).`,
374 ]);
375 // A client outside git (an abso folder, a box) can't tell, so it says nothing.
376 else assert.deepEqual(modsBehindText(env), []);
377 });
379 it("the command-line client says which client it is with every call", () => {
380 const headers = new Api({
381 server: "https://avcs.test",
382 project: "p",
383 agent: "a",
384 token: "t",
385 } as ConstructorParameters<typeof Api>[0]).headers("application/json");
386 assert.equal(headers["x-avcs-client"], `abso-cli/${clientVersion()}`);
387 assert.match(clientVersion(), /^\d+\.\d+\.\d+(\+[0-9a-f]{7})?$/);
388 });
389});