Files
SeedPass/js/packages/cli/test/profiles.test.ts
T
thePR0M3TH3ANandClaude Fable 5 841d0984b3 TS port slice 8: profiles, config, and the session agent
- Python-compatible ~/.seedpass layout: fingerprints.json (same shape,
  indent=4), per-profile dirs with parent_seed.enc (kdf/ct wrapper; unlock
  honors argon2id metadata and falls back across pbkdf2 iteration counts),
  index-key-encrypted seedpass_config.json.enc with ConfigManager defaults.
- CLI: fingerprint list/add/switch/remove (remove requires --yes),
  config get/set (JSON-parsed values, pin/password hashes redacted on read),
  profile-aware vault resolution (--vault override, else active profile).
- Session agent (the chosen unlock model): unix-socket daemon holding
  unlocked seeds in memory with TTL, 0600 socket; agent start/status/stop,
  vault unlock (password -> parent seed -> agent, never argv) and vault
  lock. Seed resolution order: SEEDPASS_MNEMONIC env, then agent. This
  daemon is the designed enforcement point for the section-9.3 lease/token
  layer.
- Tests cover the full no-env-mnemonic lifecycle: locked error -> unlock
  with password only -> provision/reveal through the agent -> status ->
  lock -> locked again. 148 core + 29 CLI tests green.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-14 14:15:51 -04:00

155 lines
5.6 KiB
TypeScript

/**
* 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<string, string | undefined>, ...argv: string[]): Promise<RunResult> {
const saved: Record<string, string | undefined> = {};
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("<redacted>");
});
});