- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/test/local.test.ts?format=json
222 linespinch 100%Last change pinch in version 305, 6 versions changed it
1// Local projects (abso init --local): a project whose history is in a store on this machine, answered by the open
2// server's own code in the client's process. Every test here runs with fetch replaced by one that throws, so any
3// call that would leave the machine fails the test.
5import assert from "node:assert/strict";
6import { spawnSync } from "node:child_process";
7import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
8import { tmpdir } from "node:os";
9import { join } from "node:path";
10import { after, before, describe, it } from "node:test";
11import { fileURLToPath } from "node:url";
12import { closeLocalStores, LOCAL_BASE, localFetch } from "../src/local-store.ts";
13import { main } from "../src/main.ts";
14import { stateDir } from "../src/state-dir.ts";
16const dirs: string[] = [];
17const temp = (label: string) => {
18 const d = mkdtempSync(join(tmpdir(), `abso-local-${label}-`));
19 dirs.push(d);
20 return d;
21};
23const outside: string[] = [];
24const realFetch = globalThis.fetch;
25before(() => {
26 globalThis.fetch = (async (input: unknown) => {
27 outside.push(String(input instanceof Request ? input.url : input));
28 throw new TypeError("network is off in this test");
29 }) as typeof fetch;
30});
31after(() => {
32 globalThis.fetch = realFetch;
33 for (const d of dirs) rmSync(d, { recursive: true, force: true, maxRetries: 5 });
34 assert.deepEqual(outside, [], "nothing reached for the network");
35});
37let store: string;
38const env = () => ({ ABSO_STORE: store, ABSO_SERVERS_FILE: "no-servers.json", ABSO_HOOK_FORCE: "1" });
39async function run(cwd: string, ...argv: string[]): Promise<{ exit: number; out: string; err: string }> {
40 const r = await main(argv, { cwd, env: env(), write: () => {} });
41 return { exit: r.exit, out: r.stdout, err: r.stderr };
42}
43const text = (dir: string, rel: string) => readFileSync(join(dir, rel), "utf8");
45/** A copy of the client without the open server beside it (a platform that pins only client/) has no local projects to test. */
46const noServer = existsSync(fileURLToPath(new URL("../../server/src/router.ts", import.meta.url)))
47 ? false
48 : "no server/src beside this client, so no local projects";
50describe("a local project, with the network off", { skip: noServer }, () => {
51 let alice: string;
52 let bob: string;
53 before(() => {
54 store = temp("store");
55 });
57 it("init --local makes the project in the store, with no sign-in", async () => {
58 alice = temp("alice");
59 writeFileSync(join(alice, "a.txt"), "one\ntwo\nthree\n");
60 const r = await run(alice, "init", "--local", "--project", "demo", "--agent", "alice");
61 assert.equal(r.exit, 0, r.out + r.err);
62 assert.match(r.out, /Created project demo at version 0 from 2 text files/, "a.txt and the starting .absoignore");
63 assert.doesNotMatch(r.out, /CI has nothing to run/);
64 assert.match(r.out, /It's a local project: its history is in .*demo, on this machine/);
65 assert.doesNotMatch(r.out + r.err, /Signing in/);
66 assert.ok(existsSync(join(store, "demo", "project.sqlite")));
67 const config = JSON.parse(readFileSync(join(stateDir(alice), "config.json"), "utf8"));
68 assert.equal(config.server, "local");
69 assert.equal(config.store, undefined, "the store came from ABSO_STORE, so the folder doesn't name it");
70 });
72 it("record and push make version 1, and log says so", async () => {
73 writeFileSync(join(alice, "a.txt"), "one\nTWO\nthree\n");
74 writeFileSync(join(alice, "b.txt"), "new\n");
75 assert.equal((await run(alice, "record")).exit, 0);
76 const pushed = await run(alice, "push", "-m", "Change two, add b");
77 assert.equal(pushed.exit, 0, pushed.out + pushed.err);
78 assert.match(pushed.out, /Pushed version 1/);
79 const log = await run(alice, "log");
80 assert.equal(log.exit, 0, log.err);
81 assert.match(log.out, /Change two, add b/);
82 });
84 it("a second folder joins, and pulls what the first pushes", async () => {
85 bob = temp("bob");
86 const joined = await run(bob, "join", "demo", "--local", "--agent", "bob", "--dir", bob);
87 assert.equal(joined.exit, 0, joined.out + joined.err);
88 assert.equal(text(bob, "a.txt"), "one\nTWO\nthree\n");
89 assert.equal(text(bob, "b.txt"), "new\n");
90 writeFileSync(join(alice, "b.txt"), "new\nmore\n");
91 assert.equal((await run(alice, "push", "-m", "More in b")).exit, 0);
92 const pulled = await run(bob, "pull");
93 assert.equal(pulled.exit, 0, pulled.out + pulled.err);
94 assert.equal(text(bob, "b.txt"), "new\nmore\n");
95 });
97 it("blame names who wrote each line", async () => {
98 const r = await run(bob, "blame", "a.txt");
99 assert.equal(r.exit, 0, r.err);
100 assert.match(r.out, /alice/);
101 assert.match(r.out, /TWO/);
102 });
104 it("a push that collides with work pushed since is held", async () => {
105 assert.equal((await run(bob, "pull")).exit, 0);
106 writeFileSync(join(alice, "a.txt"), "one\nalice's\nthree\n");
107 assert.equal((await run(alice, "push", "-m", "Alice's two")).exit, 0);
108 writeFileSync(join(bob, "a.txt"), "one\nbob's\nthree\n");
109 const held = await run(bob, "push", "-m", "Bob's two");
110 assert.notEqual(held.exit, 0);
111 assert.match(held.out + held.err, /collision/);
112 });
114 it("issues, asks and news work too, kept in issues.sqlite beside the project", async () => {
115 const opened = await run(alice, "issue", "new", "Fix the parser", "--agent", "bob");
116 assert.equal(opened.exit, 0, opened.out + opened.err);
117 assert.ok(existsSync(join(store, "demo", "issues.sqlite")));
118 const listed = await run(bob, "issues");
119 assert.equal(listed.exit, 0, listed.err);
120 assert.match(listed.out, /Fix the parser/);
121 const started = await run(bob, "start", "Parser fix", "--issue", "1");
122 assert.equal(started.exit, 0, started.out + started.err);
123 const asked = await run(alice, "ask", "bob", "Which grammar?", "--issue", "1");
124 assert.equal(asked.exit, 0, asked.out + asked.err);
125 const waiting = await run(bob, "waiting", "--mine");
126 assert.equal(waiting.exit, 0, waiting.err);
127 assert.match(waiting.out, /Which grammar\?/);
128 const answered = await run(bob, "answer", "1", "The new one");
129 assert.equal(answered.exit, 0, answered.out + answered.err);
130 const news = await run(alice, "news");
131 assert.equal(news.exit, 0, news.err);
132 assert.match(news.out, /The new one/);
133 const issue = await run(alice, "issue", "1");
134 assert.match(issue.out, /In progress|bob/);
135 });
137 it("two calls at once on one project are both answered, one after the other", async () => {
138 // As abso ci and waiting --mine make them, with Promise.all.
139 const local = localFetch(store);
140 const answers = await Promise.all([local(`${LOCAL_BASE}/p/demo/queue`), local(`${LOCAL_BASE}/p/demo/queue`)]);
141 closeLocalStores();
142 for (const a of answers) assert.equal(a.status, 200, await a.text());
143 });
145 it("a project's name can't start with a dot, so its folder stays inside the store", async () => {
146 const dir = temp("dots");
147 writeFileSync(join(dir, "a.txt"), "x\n");
148 for (const name of ["..", ".hidden"]) {
149 const r = await run(dir, "init", "--local", "--project", name, "--agent", "a");
150 assert.equal(r.exit, 1, r.out + r.err);
151 // The name rule says why, before any call is made (a ".." would collapse the call's path).
152 assert.match(r.err, /project names are letters, digits, dot, dash and underscore/);
153 }
154 assert.ok(!existsSync(join(store, "..", "project.sqlite")));
155 assert.deepEqual(readdirSync(dir), ["a.txt"], "init left nothing behind");
156 });
157});
159describe("where a local project's store is", { skip: noServer }, () => {
160 it("--store inside the folder is kept in its settings and left out of its files", async () => {
161 store = temp("unused");
162 const dir = temp("venv");
163 writeFileSync(join(dir, "a.txt"), "x\n");
164 const r = await run(dir, "init", "--local", "--project", "beside", "--agent", "a", "--store", ".abso-store");
165 assert.equal(r.exit, 0, r.out + r.err);
166 assert.ok(existsSync(join(dir, ".abso-store", "beside", "project.sqlite")));
167 assert.equal(JSON.parse(readFileSync(join(stateDir(dir), "config.json"), "utf8")).store, join(dir, ".abso-store"));
168 assert.match(text(dir, ".absoignore"), /^\/\.abso-store\/$/m);
169 assert.match(r.out, /from 2 text files/, "a.txt and .absoignore, not the store");
170 // Every later command finds the store from the folder's settings, whatever ABSO_STORE says.
171 writeFileSync(join(dir, "a.txt"), "y\n");
172 assert.equal((await run(dir, "push", "-m", "y")).exit, 0);
173 });
175 it("--local refuses --server, and --store needs --local", async () => {
176 const dir = temp("refuse");
177 mkdirSync(join(dir, "x"));
178 const both = await run(dir, "init", "--local", "--server", "https://abso.test", "--project", "p");
179 assert.match(both.out + both.err, /--local or --server, not both/);
180 const alone = await run(dir, "init", "--store", "s", "--project", "p");
181 assert.match(alone.out + alone.err, /--store goes with --local/);
182 });
184 it("joining a project the store doesn't have leaves nothing behind", async () => {
185 store = temp("empty");
186 const dir = temp("nojoin");
187 const r = await run(dir, "join", "nothing", "--local", "--agent", "a", "--dir", dir);
188 assert.notEqual(r.exit, 0);
189 assert.match(r.out + r.err, /no local project nothing/);
190 assert.equal(existsSync(join(dir, ".abso")) || existsSync(join(dir, ".avcs")), false);
191 });
192});
194describe("local commands as their own processes", { skip: noServer }, () => {
195 // The way people and agents run them: client/abso.ts, a process per command, Node's compile cache as it is.
196 const entry = fileURLToPath(new URL("../abso.ts", import.meta.url));
197 const cli = (cwd: string, processStore: string, ...argv: string[]) =>
198 spawnSync(process.execPath, [entry, ...argv], {
199 cwd,
200 env: { ...process.env, ABSO_STORE: processStore, ABSO_SERVERS_FILE: "no-servers.json" },
201 encoding: "utf8",
202 });
204 it("init, push, status and log each exit cleanly", () => {
205 const processStore = temp("process-store");
206 const dir = temp("process");
207 writeFileSync(join(dir, "a.txt"), "one\n");
208 const steps: [string, string[]][] = [
209 ["init", ["init", "--local", "--project", "proc", "--agent", "p"]],
210 ["push", ["push", "-m", "Two"]],
211 ["status", ["status"]],
212 ["log", ["log"]],
213 ];
214 for (const [name, argv] of steps) {
215 if (name === "push") writeFileSync(join(dir, "a.txt"), "one\ntwo\n");
216 const r = cli(dir, processStore, ...argv);
217 assert.equal(r.status, 0, `${name}: exit ${r.status} ${r.signal ?? ""}\n${r.stdout}${r.stderr}`);
218 if (name === "status") assert.match(r.stdout, /It's a local project, in /);
219 if (name === "log") assert.match(r.stdout, /Two/);
220 }
221 });
222});