/** * Profile, config, and session-agent tests: the full unlock lifecycle with * no SEEDPASS_MNEMONIC in the environment — the agent is the only seed * source, exactly how an agent-driven workflow would run. */ import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { mkdtemp, readFile } from "node:fs/promises"; import { existsSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import process from "node:process"; import { mnemonics } from "@seedpass/test-vectors"; import { generateFingerprint } from "@seedpass/core"; import { buildProgram, AgentDaemon, agentSocketPath, type ProgramIo } from "../src/index.js"; const MNEMONIC = mnemonics["abandon12"]!; const FINGERPRINT = generateFingerprint(MNEMONIC); const PASSWORD = "profile-test-password"; let appDir: string; let daemon: AgentDaemon; interface RunResult { stdout: string; error?: unknown; } async function run(env: Record, ...argv: string[]): Promise { const saved: Record = {}; for (const [k, v] of Object.entries(env)) { saved[k] = process.env[k]; if (v === undefined) delete process.env[k]; else process.env[k] = v; } const out: string[] = []; const io: ProgramIo = { out: (l: string) => out.push(l), err: () => {} }; let error: unknown; try { await buildProgram(io).parseAsync(["node", "seedpass-js", "--app-dir", appDir, ...argv]); } catch (e) { error = e; } finally { for (const [k, v] of Object.entries(saved)) { if (v === undefined) delete process.env[k]; else process.env[k] = v; } } return { stdout: out.join("\n"), error }; } beforeAll(async () => { appDir = await mkdtemp(join(tmpdir(), "seedpass-appdir-")); process.env["SEEDPASS_AGENT_SOCK"] = join(appDir, "agent.sock"); daemon = new AgentDaemon(agentSocketPath(appDir), 900); await daemon.start(); }); afterAll(async () => { await daemon.stop(); delete process.env["SEEDPASS_AGENT_SOCK"]; }); describe("profile lifecycle", () => { it("creates a Python-layout profile from env secrets", async () => { const r = await run( { SEEDPASS_MNEMONIC: MNEMONIC, SEEDPASS_PASSWORD: PASSWORD }, "fingerprint", "add", "--name", "main", ); expect(JSON.parse(r.stdout).fingerprint).toBe(FINGERPRINT); expect(existsSync(join(appDir, FINGERPRINT, "parent_seed.enc"))).toBe(true); expect(existsSync(join(appDir, FINGERPRINT, "seedpass_entries_db.json.enc"))).toBe(true); const meta = JSON.parse(await readFile(join(appDir, "fingerprints.json"), "utf8")); expect(meta).toEqual({ fingerprints: [FINGERPRINT], last_used: FINGERPRINT, names: { [FINGERPRINT]: "main" }, }); }); it("lists and switches profiles", async () => { const r = await run({}, "fingerprint", "list"); expect(JSON.parse(r.stdout)).toEqual([ { fingerprint: FINGERPRINT, name: "main", current: true }, ]); }); }); describe("agent unlock lifecycle (no mnemonic in env)", () => { it("locked vault refuses access with a helpful error", async () => { const r = await run({ SEEDPASS_MNEMONIC: undefined }, "entry", "list"); expect(String((r.error as Error).message)).toContain("vault is locked"); }); it("vault unlock hands the seed to the agent using only the password", async () => { const r = await run( { SEEDPASS_MNEMONIC: undefined, SEEDPASS_PASSWORD: PASSWORD }, "vault", "unlock", "--ttl", "60", ); const row = JSON.parse(r.stdout); expect(row.unlocked).toBe(FINGERPRINT); expect(row.expires_at).toBeGreaterThan(Date.now() / 1000); }); it("profile vault opens via the agent, no env mnemonic, no --vault", async () => { const list = await run({ SEEDPASS_MNEMONIC: undefined }, "entry", "list"); expect(JSON.parse(list.stdout)).toEqual([]); const add = await run( { SEEDPASS_MNEMONIC: undefined }, "entry", "add", "key-value", "agent-made", "k", "agent-value", ); expect(JSON.parse(add.stdout).ref).toBe("sp://entry/0"); expect(add.stdout).not.toContain("agent-value"); const reveal = await run( { SEEDPASS_MNEMONIC: undefined }, "entry", "reveal", "agent-made", ); expect(reveal.stdout).toBe("agent-value"); }); it("agent status shows the unlocked profile; lock drops it", async () => { const status = await run({}, "agent", "status"); expect(JSON.parse(status.stdout).map((p: { fingerprint: string }) => p.fingerprint)).toEqual([ FINGERPRINT, ]); const lock = await run({ SEEDPASS_MNEMONIC: undefined }, "vault", "lock"); expect(JSON.parse(lock.stdout).locked).toBe(1); const denied = await run({ SEEDPASS_MNEMONIC: undefined }, "entry", "list"); expect(String((denied.error as Error).message)).toContain("vault is locked"); }); }); describe("config", () => { it("returns defaults, persists sets, and redacts hashes", async () => { const env = { SEEDPASS_MNEMONIC: MNEMONIC }; const defaults = JSON.parse((await run(env, "config", "get")).stdout); expect(defaults.relays).toHaveLength(3); expect(defaults.kdf_iterations).toBe(200000); await run(env, "config", "set", "clipboard_clear_delay", "10"); await run(env, "config", "set", "relays", '["wss://relay.example"]'); const after = JSON.parse((await run(env, "config", "get")).stdout); expect(after.clipboard_clear_delay).toBe(10); expect(after.relays).toEqual(["wss://relay.example"]); await run(env, "config", "set", "pin_hash", "supersecret"); const redacted = JSON.parse((await run(env, "config", "get", "pin_hash")).stdout); expect(redacted.pin_hash).toBe(""); }); });