import { afterEach, describe, expect, test } from "bun:test"; import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { runCli } from "../runner.js"; const ORIGINAL_ENV = { enterprises: process.env.SEAL_USE_ENTERPRISES_DIR, config: process.env.SEAL_USE_CONFIG_FILE, tokenStore: process.env.SEAL_USE_TOKEN_STORE, usageFile: process.env.SEAL_USE_USAGE_FILE }; const ORIGINAL_FETCH = globalThis.fetch; const tempDirs: string[] = []; afterEach(() => { restoreEnv("SEAL_USE_ENTERPRISES_DIR", ORIGINAL_ENV.enterprises); restoreEnv("SEAL_USE_CONFIG_FILE", ORIGINAL_ENV.config); restoreEnv("SEAL_USE_TOKEN_STORE", ORIGINAL_ENV.tokenStore); restoreEnv("SEAL_USE_USAGE_FILE", ORIGINAL_ENV.usageFile); globalThis.fetch = ORIGINAL_FETCH; for (const dir of tempDirs.splice(0)) { rmSync(dir, { recursive: true, force: true }); } }); function restoreEnv(name: string, value: string | undefined) { if (value === undefined) delete process.env[name]; else process.env[name] = value; } function setupMockEnv() { const root = mkdtempSync(join(tmpdir(), "seal-use-users-cli-")); const enterprises = join(root, "enterprises"); tempDirs.push(root); process.env.SEAL_USE_ENTERPRISES_DIR = enterprises; process.env.SEAL_USE_CONFIG_FILE = join(root, "config.json"); process.env.SEAL_USE_TOKEN_STORE = join(root, "tokens.json"); process.env.SEAL_USE_USAGE_FILE = join(root, "usage.json"); mkdirSync(enterprises, { recursive: true }); writeFileSync( join(enterprises, "users-corp.json"), JSON.stringify( { id: "users-corp", name: "Users Corp", environment: "test", seal: { url: "https://users-corp.sealai.cc", endpoints: { approvalStylePreferences: "api/v1/agent/ai-approval/config" } }, source: { type: "seal", token: "mock-token", sealUrl: "https://users-corp.sealai.cc" } }, null, 2 ) ); writeFileSync( join(root, "config.json"), JSON.stringify({ defaultCorpId: "users-corp", stateful: true }, null, 2) ); } /** Counts requests that would actually delete or reset a user. */ function trackUserMutationRequests() { const mutations: string[] = []; globalThis.fetch = (async (input: RequestInfo | URL) => { const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; mutations.push(url); // Endpoint-shaped payloads: reset-password is validated against // SealUserResetPasswordResultSchema ({ password }). const payload = url.includes("reset-password") ? { data: { password: "mock-reset-password" } } : { data: { success: true } }; return new Response(JSON.stringify(payload), { status: 200, headers: { "Content-Type": "application/json" } }); }) as typeof fetch; return mutations; } describe("users command confirmation gates", () => { test("users delete refuses without --yes and never reaches the API", async () => { setupMockEnv(); const mutations = trackUserMutationRequests(); const result = await runCli(["users", "delete", "user-123"]); expect(result.isError).toBe(true); expect(result.exitCode).toBe(10); expect(result.stderr).toContain("Refusing to delete user 'user-123' without --yes."); expect(result.stderr).toContain("seal-use users delete user-123"); expect(mutations).toEqual([]); }); test("users delete runs with --yes", async () => { setupMockEnv(); trackUserMutationRequests(); const result = await runCli(["users", "delete", "user-123", "--yes"]); expect(result.isError).toBe(false); expect(result.exitCode).toBe(0); }); test("users reset-password refuses without --yes and never reaches the API", async () => { setupMockEnv(); const mutations = trackUserMutationRequests(); const result = await runCli(["users", "reset-password", "user-123"]); expect(result.isError).toBe(true); expect(result.exitCode).toBe(10); expect(result.stderr).toContain("Refusing to reset password of user 'user-123' without --yes."); expect(mutations).toEqual([]); }); test("users reset-password runs with --confirm", async () => { setupMockEnv(); trackUserMutationRequests(); const result = await runCli(["users", "reset-password", "user-123", "--confirm"]); expect(result.isError).toBe(false); expect(result.exitCode).toBe(0); }); test("users activate refuses without --yes and never reaches the API", async () => { setupMockEnv(); const mutations = trackUserMutationRequests(); const result = await runCli(["users", "activate", "user-123"]); expect(result.isError).toBe(true); expect(result.exitCode).toBe(10); expect(result.stderr).toContain("Refusing to activate user 'user-123' without --yes."); expect(mutations).toEqual([]); }); test("users activate runs with --yes", async () => { setupMockEnv(); trackUserMutationRequests(); const result = await runCli(["users", "activate", "user-123", "--yes"]); expect(result.isError).toBe(false); expect(result.exitCode).toBe(0); }); test("usage line mentions --yes when the id is missing", async () => { setupMockEnv(); const result = await runCli(["users", "delete"]); expect(result.isError).toBe(true); expect(result.stderr).toContain("--yes"); }); });