|
| 1 | +/** |
| 2 | + * End-to-end tests for the `run402 wallets` command family and wallet |
| 3 | + * selection (named profiles). Spawns the real CLI as a subprocess. Wallet |
| 4 | + * management is local (no network), so no fetch mocking is needed. |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, beforeEach, afterEach } from "node:test"; |
| 8 | +import assert from "node:assert/strict"; |
| 9 | +import { spawnSync } from "node:child_process"; |
| 10 | +import { mkdtempSync, mkdirSync, rmSync, writeFileSync, existsSync } from "node:fs"; |
| 11 | +import { join } from "node:path"; |
| 12 | +import { tmpdir } from "node:os"; |
| 13 | +import { fileURLToPath } from "node:url"; |
| 14 | + |
| 15 | +const repoRoot = fileURLToPath(new URL(".", import.meta.url)); |
| 16 | +const CLI = join(repoRoot, "cli/cli.mjs"); |
| 17 | +const API = "https://test-api.run402.com"; |
| 18 | + |
| 19 | +let configDir; |
| 20 | +let workDir; |
| 21 | + |
| 22 | +beforeEach(() => { |
| 23 | + configDir = mkdtempSync(join(tmpdir(), "run402-wallets-cfg-")); |
| 24 | + workDir = mkdtempSync(join(tmpdir(), "run402-wallets-cwd-")); |
| 25 | +}); |
| 26 | + |
| 27 | +afterEach(() => { |
| 28 | + rmSync(configDir, { recursive: true, force: true }); |
| 29 | + rmSync(workDir, { recursive: true, force: true }); |
| 30 | +}); |
| 31 | + |
| 32 | +function run(args, { cwd = workDir, env = {} } = {}) { |
| 33 | + // RUN402_WALLET_LABEL_SYNC=0 keeps these tests offline/hermetic — the |
| 34 | + // server-side label push is on by default in real use. |
| 35 | + const base = { ...process.env, RUN402_CONFIG_DIR: configDir, RUN402_API_BASE: API, RUN402_WALLET_LABEL_SYNC: "0" }; |
| 36 | + delete base.RUN402_WALLET; |
| 37 | + delete base.RUN402_PROFILE; |
| 38 | + const result = spawnSync(process.execPath, [CLI, ...args], { |
| 39 | + cwd, |
| 40 | + env: { ...base, ...env }, |
| 41 | + encoding: "utf-8", |
| 42 | + }); |
| 43 | + return result; |
| 44 | +} |
| 45 | + |
| 46 | +function jsonOut(result) { |
| 47 | + try { |
| 48 | + return JSON.parse(result.stdout); |
| 49 | + } catch { |
| 50 | + throw new Error(`stdout not JSON: ${result.stdout}\nstderr: ${result.stderr}`); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function errEnvelope(result) { |
| 55 | + const line = result.stderr.trim().split("\n").filter(Boolean).pop(); |
| 56 | + return JSON.parse(line); |
| 57 | +} |
| 58 | + |
| 59 | +describe("wallets — lifecycle", () => { |
| 60 | + it("new → list → rm", () => { |
| 61 | + assert.equal(run(["wallets", "new", "kychon"]).status, 0); |
| 62 | + const list = jsonOut(run(["wallets", "list"])); |
| 63 | + assert.deepEqual(list.map((w) => w.name).sort(), ["kychon"]); |
| 64 | + assert.equal(list[0].label, "kychon"); |
| 65 | + // rm requires --yes |
| 66 | + const noConfirm = run(["wallets", "rm", "kychon"]); |
| 67 | + assert.notEqual(noConfirm.status, 0); |
| 68 | + assert.equal(errEnvelope(noConfirm).code, "CONFIRMATION_REQUIRED"); |
| 69 | + assert.equal(run(["wallets", "rm", "kychon", "--yes"]).status, 0); |
| 70 | + assert.deepEqual(jsonOut(run(["wallets", "list"])), []); |
| 71 | + }); |
| 72 | + |
| 73 | + it("rename default migrates the root wallet into profiles/", () => { |
| 74 | + assert.equal(run(["allowance", "create"]).status, 0); // creates the default wallet locally |
| 75 | + assert.ok(existsSync(join(configDir, "allowance.json"))); |
| 76 | + assert.equal(run(["wallets", "rename", "default", "kychon"]).status, 0); |
| 77 | + assert.ok(!existsSync(join(configDir, "allowance.json")), "root allowance.json migrated away"); |
| 78 | + assert.ok(existsSync(join(configDir, "profiles", "kychon", "allowance.json"))); |
| 79 | + assert.deepEqual(jsonOut(run(["wallets", "list"])).map((w) => w.name), ["kychon"]); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe("wallets — selection precedence", () => { |
| 84 | + beforeEach(() => { |
| 85 | + run(["wallets", "new", "kychon"]); |
| 86 | + run(["wallets", "new", "client-a"]); |
| 87 | + }); |
| 88 | + |
| 89 | + it("flag wins and reports source=flag", () => { |
| 90 | + const cur = jsonOut(run(["--wallet", "kychon", "wallets", "current"])); |
| 91 | + assert.equal(cur.name, "kychon"); |
| 92 | + assert.equal(cur.source, "flag"); |
| 93 | + }); |
| 94 | + |
| 95 | + it("env selects when no flag", () => { |
| 96 | + const cur = jsonOut(run(["wallets", "current"], { env: { RUN402_WALLET: "client-a" } })); |
| 97 | + assert.equal(cur.name, "client-a"); |
| 98 | + assert.equal(cur.source, "env"); |
| 99 | + }); |
| 100 | + |
| 101 | + it("directory binding selects when no flag/env", () => { |
| 102 | + writeFileSync(join(workDir, ".run402.json"), JSON.stringify({ wallet: "client-a" })); |
| 103 | + const cur = jsonOut(run(["wallets", "current"])); |
| 104 | + assert.equal(cur.name, "client-a"); |
| 105 | + assert.equal(cur.source, "binding"); |
| 106 | + }); |
| 107 | + |
| 108 | + it(".run402.local.json overrides .run402.json and walks up from a subdir", () => { |
| 109 | + writeFileSync(join(workDir, ".run402.json"), JSON.stringify({ wallet: "client-a" })); |
| 110 | + writeFileSync(join(workDir, ".run402.local.json"), JSON.stringify({ wallet: "kychon" })); |
| 111 | + const sub = join(workDir, "api"); |
| 112 | + mkdirSync(sub); |
| 113 | + const cur = jsonOut(run(["wallets", "current"], { cwd: sub })); |
| 114 | + assert.equal(cur.name, "kychon"); |
| 115 | + }); |
| 116 | + |
| 117 | + it("global `wallets use` applies when no flag/env/binding", () => { |
| 118 | + assert.equal(run(["wallets", "use", "kychon"]).status, 0); |
| 119 | + const cur = jsonOut(run(["wallets", "current"])); |
| 120 | + assert.equal(cur.name, "kychon"); |
| 121 | + assert.equal(cur.source, "config"); |
| 122 | + }); |
| 123 | + |
| 124 | + it("falls back to default when nothing selects", () => { |
| 125 | + const cur = jsonOut(run(["wallets", "current"])); |
| 126 | + assert.equal(cur.name, "default"); |
| 127 | + assert.equal(cur.source, "default"); |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +describe("wallets — conflict + fail-closed", () => { |
| 132 | + beforeEach(() => { |
| 133 | + run(["wallets", "new", "kychon"]); |
| 134 | + run(["wallets", "new", "client-a"]); |
| 135 | + }); |
| 136 | + |
| 137 | + it("env vs binding mismatch is a hard error on a normal command", () => { |
| 138 | + writeFileSync(join(workDir, ".run402.json"), JSON.stringify({ wallet: "client-a" })); |
| 139 | + const r = run(["allowance", "export"], { env: { RUN402_WALLET: "kychon" } }); |
| 140 | + assert.notEqual(r.status, 0); |
| 141 | + const env = errEnvelope(r); |
| 142 | + assert.equal(env.code, "WALLET_SELECTION_CONFLICT"); |
| 143 | + assert.match(env.message, /kychon/); |
| 144 | + assert.match(env.message, /client-a/); |
| 145 | + }); |
| 146 | + |
| 147 | + it("--wallet resolves the conflict", () => { |
| 148 | + writeFileSync(join(workDir, ".run402.json"), JSON.stringify({ wallet: "client-a" })); |
| 149 | + const r = run(["--wallet", "kychon", "allowance", "export"], { env: { RUN402_WALLET: "client-a" } }); |
| 150 | + assert.equal(r.status, 0, r.stderr); |
| 151 | + }); |
| 152 | + |
| 153 | + it("selecting an unknown wallet fails closed on a normal command", () => { |
| 154 | + const r = run(["--wallet", "ghost", "allowance", "export"]); |
| 155 | + assert.notEqual(r.status, 0); |
| 156 | + assert.equal(errEnvelope(r).code, "WALLET_NOT_FOUND"); |
| 157 | + }); |
| 158 | + |
| 159 | + it("the wallets group itself is never blocked by a conflict (can unbind)", () => { |
| 160 | + writeFileSync(join(workDir, ".run402.json"), JSON.stringify({ wallet: "client-a" })); |
| 161 | + const r = run(["wallets", "unbind"], { env: { RUN402_WALLET: "kychon" } }); |
| 162 | + assert.equal(r.status, 0, r.stderr); |
| 163 | + assert.ok(!existsSync(join(workDir, ".run402.json"))); |
| 164 | + }); |
| 165 | +}); |
| 166 | + |
| 167 | +describe("wallets — provenance", () => { |
| 168 | + it("emits a provenance line for a non-default selection", () => { |
| 169 | + run(["wallets", "new", "kychon"]); |
| 170 | + const r = run(["--wallet", "kychon", "allowance", "export"]); |
| 171 | + assert.equal(r.status, 0, r.stderr); |
| 172 | + assert.match(r.stderr, /wallet: kychon/); |
| 173 | + }); |
| 174 | + |
| 175 | + it("stays silent for the default wallet", () => { |
| 176 | + run(["allowance", "create"]); // default wallet |
| 177 | + const r = run(["allowance", "export"]); |
| 178 | + assert.equal(r.status, 0, r.stderr); |
| 179 | + assert.ok(!/↪ wallet:/.test(r.stderr), `expected no provenance line, got: ${r.stderr}`); |
| 180 | + }); |
| 181 | +}); |
0 commit comments