Versions 201 to 318
new file, 60 lines
1+// An init or join that fails against the server leaves the folder as it was, so the same command can run again: 2+// no starting .absoignore or abso.json from init, no .abso/ from join (but a key already here stays). 4+import assert from "node:assert/strict"; 5+import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs"; 6+import { tmpdir } from "node:os"; 7+import { join } from "node:path"; 8+import { after, describe, it } from "node:test"; 9+import { main } from "../src/main.ts"; 11+// Nothing listens on port 1, so every call fails to connect. 12+const DOWN = "http://127.0.0.1:1"; 13+const ENV = { ABSO_TOKEN: "test-token-not-secret", ABSO_SERVERS_FILE: "no-servers.json" }; 15+const dirs: string[] = []; 17+ for (const d of dirs) rmSync(d, { recursive: true, force: true, maxRetries: 5 }); 20+function temp(): string { 21+ const d = mkdtempSync(join(tmpdir(), "abso-failed-start-")); 26+async function run(cwd: string, ...argv: string[]): Promise<number> { 27+ return (await main(argv, { cwd, env: ENV, write: () => {} })).exit; 30+describe("a failed start", () => { 31+ it("init takes back the .absoignore and abso.json it wrote", async () => { 33+ writeFileSync(join(dir, "package.json"), `${JSON.stringify({ scripts: { test: "node --test" } })}\n`); 34+ assert.notEqual(await run(dir, "init", "--server", DOWN, "--project", "p", "--agent", "a"), 0); 35+ assert.deepEqual(readdirSync(dir), ["package.json"]); 38+ it("join leaves no .abso folder", async () => { 40+ assert.notEqual(await run(dir, "join", "p", "--server", DOWN, "--agent", "a", "--dir", dir), 0); 41+ assert.equal(existsSync(join(dir, ".abso")), false); 44+ it("join keeps a key that was already here", async () => { 46+ mkdirSync(join(dir, ".abso")); 47+ const key = { server: DOWN, key: "abso_sk_not-a-real-key", id: "k_0123abcd" }; 48+ writeFileSync(join(dir, ".abso", "key"), `${JSON.stringify(key)}\n`); 49+ assert.notEqual(await run(dir, "join", "p", "--server", DOWN, "--agent", "a", "--dir", dir), 0); 50+ assert.deepEqual(readdirSync(join(dir, ".abso")), ["key"]); 53+ it("join with a key it can't read leaves only that key", async () => { 55+ mkdirSync(join(dir, ".abso")); 56+ writeFileSync(join(dir, ".abso", "key"), "{}\n"); 57+ assert.notEqual(await run(dir, "join", "p", "--server", DOWN, "--agent", "a", "--dir", dir), 0); 58+ assert.deepEqual(readdirSync(join(dir, ".abso")), ["key"]);