- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/test/server-flag.test.ts?format=json
267 linesspeed 100%Last change speed in version 156, 2 versions changed it
1// --server on every command that talks to a server (abso-core issue 10). The rules, each a test:
2// - every command is sorted: one that works on this machine only takes no --server and says so; every other one
3// takes it, and help says the same words about it for each kind;
4// - a missing, empty or malformed value is refused before anything is sent, for every family of command;
5// - in a joined folder, --server naming the folder's own server reaches it; another server is refused, never
6// swapped in, and nothing is sent; outside a joined folder a folder command with --server is refused too;
7// - an explicit --server goes to that server's own Issues Worker, which ABSO_ISSUES_SERVER doesn't redirect;
8// - a command that picks its server (whoami, box, logout) uses the one named, even inside a folder on another;
9// - with nothing named (abso-core issue 9) the server is the joined folder's, else ABSO_SERVER, else prod, whatever
10// this box is signed in to (doctor.test.ts has whoami with staging alone and with staging and prod both).
12import assert from "node:assert/strict";
13import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
14import { createServer, type Server } from "node:http";
15import type { AddressInfo } from "node:net";
16import { tmpdir } from "node:os";
17import { join } from "node:path";
18import { after, before, describe, it } from "node:test";
19import { writeConfig } from "../src/config.ts";
20import { commandTable, main } from "../src/main.ts";
21import { chooseServer, PROD_SERVER, SERVER_ORDER, sameServer, serverHelp, serverUse } from "../src/server-choice.ts";
23/** A made-up token: the tests never read the real ~/.abso/token. */
24const TOKEN = "abso_test-token-for-server-flag";
26interface Listening {
27 readonly server: Server;
28 readonly url: string;
29 readonly paths: string[];
30}
32/** A server that notes each path it is asked for and answers 500, so a command stops after its first call. */
33async function listen(): Promise<Listening> {
34 const paths: string[] = [];
35 const server = createServer((req, res) => {
36 paths.push(req.url ?? "");
37 res.writeHead(500, { "content-type": "application/json" });
38 res.end(JSON.stringify({ error: "a test server" }));
39 });
40 await new Promise<void>((done) => server.listen(0, "127.0.0.1", done));
41 return { server, url: `http://127.0.0.1:${(server.address() as AddressInfo).port}`, paths };
42}
44const temps: string[] = [];
45function temp(name: string): string {
46 const dir = mkdtempSync(join(tmpdir(), `abso-server-flag-${name}-`));
47 temps.push(dir);
48 return dir;
49}
51let home: Listening;
52let other: Listening;
53let issuesElsewhere: Listening;
54let env: Record<string, string>;
56before(async () => {
57 home = await listen();
58 other = await listen();
59 issuesElsewhere = await listen();
60 const servers = join(temp("servers"), "servers.json");
61 writeFileSync(servers, "{}");
62 env = { ABSO_TOKEN: TOKEN, ABSO_SERVERS_FILE: servers };
63});
65after(() => {
66 for (const l of [home, other, issuesElsewhere]) l.server.close();
67 for (const dir of temps) rmSync(dir, { recursive: true, force: true });
68});
70function folder(server = home.url): string {
71 const root = temp("folder");
72 writeConfig(root, { server, project: "demo", agent: "alice" });
73 return root;
74}
76const run = (cwd: string, argv: string[], extra: Record<string, string> = {}) =>
77 main(argv, { cwd, env: { ...env, ...extra }, write: () => {} });
79function calls(): number {
80 return home.paths.length + other.paths.length + issuesElsewhere.paths.length;
81}
83function clear(): void {
84 for (const l of [home, other, issuesElsewhere]) l.paths.length = 0;
85}
87describe("--server on every command", () => {
88 it("sorts every command: local ones take no --server, every other one does, and help says so alike", async () => {
89 const table = commandTable();
90 const local = Object.keys(table).filter((name) => serverUse(name) === "local");
91 assert.deepEqual(local.sort(), ["check-ignore", "fix-eol", "pause", "resume", "skill"]);
92 for (const [name, def] of Object.entries(table)) {
93 if (serverUse(name) === "local") {
94 assert.equal(def.flags.server, undefined, name);
95 assert.equal(serverHelp(name), undefined, name);
96 continue;
97 }
98 assert.equal(def.flags.server, "string", name);
99 const help = await run(process.cwd(), [name, "--help"]);
100 assert.ok(help.stdout.includes(` ${serverHelp(name)}\n`), `abso ${name} --help names --server`);
101 }
102 const data = JSON.parse((await run(process.cwd(), ["help", "--json"])).stdout) as {
103 commands: { name: string; server?: string; flags: Record<string, string> }[];
104 };
105 for (const c of data.commands.filter((c) => c.name in table)) assert.equal(c.server, serverHelp(c.name), c.name);
106 const text = (await run(process.cwd(), ["help"])).stdout;
107 assert.match(text, /Every command that talks to a server takes --server <url>/);
108 });
110 it("refuses --server on a command that works on this machine only", async () => {
111 for (const argv of [
112 ["skill", "show", "--server", home.url],
113 ["check-ignore", "a.txt", `--server=${home.url}`],
114 ["pause", "--server", home.url],
115 ]) {
116 const r = await run(folder(), argv);
117 assert.equal(r.exit, 1, argv.join(" "));
118 assert.match(r.stderr, /works on this machine only and talks to no server, so it takes no --server/);
119 }
120 const words = await run(folder(), ["check-ignore", "--", "--server"]);
121 assert.doesNotMatch(words.stderr, /takes no --server/, "a path after -- is a path");
122 });
124 it("refuses a missing, empty or malformed value before anything is sent, in every family", async () => {
125 const root = folder();
126 const outside = temp("outside");
127 clear();
128 const families: [string, string[]][] = [
129 [root, ["status"]],
130 [root, ["issue", "new", "A title"]],
131 [root, ["ask", "bob", "Why?"]],
132 [root, ["news"]],
133 [root, ["pipeline", "runs"]],
134 [outside, ["whoami"]],
135 [outside, ["login"]],
136 [outside, ["join", "demo"]],
137 [outside, ["org", "acme"]],
138 [outside, ["key", "list"]],
139 ];
140 for (const [cwd, argv] of families) {
141 const missing = await run(cwd, [...argv, "--server"]);
142 assert.match(missing.stderr, /--server needs a value/, argv.join(" "));
143 const empty = await run(cwd, [...argv, "--server="]);
144 assert.match(empty.stderr, /--server needs a server's URL, like https:\/\/api\.abso\.dev/, argv.join(" "));
145 for (const bad of ["api.abso.dev", "ftp://api.abso.dev", "https://me@api.abso.dev", "https://x.example/?a=1"]) {
146 const r = await run(cwd, [...argv, "--server", bad]);
147 assert.equal(r.exit, 1, `${argv.join(" ")} --server ${bad}`);
148 assert.match(r.stderr, /--server takes an http or https URL with no user name, query or fragment/);
149 }
150 }
151 assert.equal(calls(), 0, "nothing was sent");
152 });
154 it("in a joined folder, reaches the folder's own server and refuses another without sending anything", async () => {
155 const root = folder();
156 for (const argv of [["status"], ["log"], ["issues"], ["issue", "1"], ["news"], ["waiting"], ["ci"]]) {
157 clear();
158 const same = await run(root, [...argv, "--server", `${home.url.toUpperCase().replace("HTTP", "http")}/`]);
159 assert.doesNotMatch(same.stderr, /joined on/, argv.join(" "));
160 assert.ok(home.paths.length > 0, `abso ${argv.join(" ")} reached the folder's server`);
161 assert.equal(other.paths.length, 0);
162 clear();
163 const refused = await run(root, [...argv, "--server", other.url]);
164 assert.equal(refused.exit, 1);
165 assert.match(
166 refused.stderr,
167 new RegExp(
168 `this folder is joined on ${home.url}, not ${other.url}, so abso ${argv[0]} won't send its work or key there`,
169 ),
170 );
171 assert.equal(calls(), 0, `abso ${argv.join(" ")} sent nothing`);
172 }
173 });
175 it("outside a joined folder, a folder command with --server says how to join one there", async () => {
176 const outside = temp("outside");
177 for (const argv of [["status"], ["issue", "new", "A title"], ["ask", "bob", "Why?"], ["news"]]) {
178 const r = await run(outside, [...argv, "--server", home.url]);
179 assert.equal(r.exit, 1);
180 assert.match(r.stderr, new RegExp(`abso join <project> --server ${home.url}`));
181 }
182 assert.equal(calls(), 0);
183 });
185 it("an explicit --server takes issues to that server's own Issues Worker, past ABSO_ISSUES_SERVER", async () => {
186 const root = folder();
187 const elsewhere = { ABSO_ISSUES_SERVER: issuesElsewhere.url };
188 clear();
189 await run(root, ["issue", "1"], elsewhere);
190 assert.ok(issuesElsewhere.paths.length > 0, "without --server, ABSO_ISSUES_SERVER names the Issues Worker");
191 assert.ok(home.paths.every((p) => !p.startsWith("/issues/")));
192 clear();
193 await run(root, ["issue", "1", "--server", home.url], elsewhere);
194 assert.equal(issuesElsewhere.paths.length, 0, "--server isn't redirected");
195 assert.ok(home.paths.length > 0);
196 assert.ok(
197 home.paths.some((p) => p.startsWith("/issues/p/demo/")),
198 `the open server keeps issues at /issues/p/<project>: ${home.paths}`,
199 );
200 });
202 it("a command that picks its server uses the one named, even in a folder on another", async () => {
203 const root = folder(other.url);
204 for (const argv of [["whoami"], ["box"], ["logout"]]) {
205 const r = await run(root, [...argv, "--server", home.url]);
206 assert.match(r.stdout, new RegExp(home.url.replace(/\./g, "\\.")), argv.join(" "));
207 }
208 clear();
209 const doctor = await run(root, ["doctor", "--server", home.url]);
210 assert.match(doctor.stdout, new RegExp(`${home.url.replace(/\./g, "\\.")} answered 500`));
211 assert.equal(other.paths.filter((p) => p === "/version").length, 0, "doctor didn't swap in the folder's server");
212 });
214 it("counts two spellings of one server as the same, and different ports or paths as different", () => {
215 assert.ok(sameServer("https://API.abso.dev/", "https://api.abso.dev"));
216 assert.ok(!sameServer("http://127.0.0.1:8787", "http://127.0.0.1:8788"));
217 assert.ok(!sameServer("http://127.0.0.1:8787", "http://localhost:8787"));
218 assert.ok(!sameServer("https://x.example/a", "https://x.example/b"));
219 });
220});
222describe("the server when nothing names one", () => {
223 const STAGING = "https://staging.abso.example";
225 it("is --server, else the joined folder's, else ABSO_SERVER, else prod", () => {
226 const outside = temp("outside");
227 const staging = folder(STAGING);
228 assert.equal(PROD_SERVER, "https://api.abso.dev");
229 assert.equal(chooseServer(undefined, outside, {}), PROD_SERVER, "nothing named: prod");
230 assert.equal(chooseServer(undefined, outside, { ABSO_SERVER: STAGING }), STAGING, "the environment");
231 assert.equal(chooseServer(undefined, outside, { AVCS_SERVER: STAGING }), STAGING, "its older name");
232 assert.equal(chooseServer(undefined, staging, {}), STAGING, "a joined staging folder");
233 assert.equal(chooseServer(undefined, staging, { ABSO_SERVER: "https://env.example" }), STAGING, "folder first");
234 assert.equal(chooseServer(PROD_SERVER, staging, { ABSO_SERVER: STAGING }), PROD_SERVER, "explicit prod");
235 assert.equal(chooseServer(STAGING, outside, {}), STAGING, "explicit staging");
236 assert.throws(
237 () => chooseServer(undefined, outside, { ABSO_SERVER: "staging" }),
238 /ABSO_SERVER takes an http or https URL/,
239 );
240 });
242 it("help names the order once for every command that picks its server", async () => {
243 assert.equal(
244 SERVER_ORDER,
245 "--server <url>, else the server of the abso folder you're in, else ABSO_SERVER, else prod (https://api.abso.dev)",
246 );
247 for (const name of ["join", "init", "login", "whoami", "doctor", "box", "logout", "orgs", "org"])
248 assert.ok(serverHelp(name)?.includes(SERVER_ORDER), name);
249 assert.ok((await run(process.cwd(), ["help"])).stdout.includes(SERVER_ORDER));
250 });
252 it("a joined folder's commands go to its server with nothing named, and ABSO_SERVER doesn't move them", async () => {
253 const root = folder();
254 clear();
255 await run(root, ["status"], { ABSO_SERVER: other.url });
256 assert.ok(home.paths.length > 0);
257 assert.equal(other.paths.length, 0);
258 });
260 it("whoami in a joined folder looks at the folder's server, and another named there is used as given", async () => {
261 const root = folder();
262 const here = await run(root, ["whoami"], { ABSO_SERVER: other.url });
263 assert.ok(here.stdout.includes(`isn't signed in to ${home.url}`), here.stdout);
264 const named = await run(root, ["box", "--server", other.url]);
265 assert.ok(named.stdout.includes(`isn't signed in to ${other.url}`), named.stdout);
266 });
267});