import { afterEach, describe, expect, test } from "bun:test"; import { mkdirSync, mkdtempSync, 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-sim-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, "sim-corp.json"), JSON.stringify( { id: "sim-corp", name: "Sim Corp", environment: "test", seal: { url: "https://sim-corp.sealai.cc", endpoints: { approvalStylePreferences: "api/v1/agent/ai-approval/config" } }, source: { type: "seal", token: "mock-token", sealUrl: "https://sim-corp.sealai.cc" } }, null, 2 ) ); writeFileSync( join(root, "config.json"), JSON.stringify({ defaultCorpId: "sim-corp", stateful: true }, null, 2) ); } function trackMutationRequests() { 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); return new Response(JSON.stringify({ data: { success: true } }), { status: 200, headers: { "Content-Type": "application/json" } }); }) as typeof fetch; return mutations; } function mockSimulationBatches() { const requested: string[] = []; globalThis.fetch = (async (input: RequestInfo | URL) => { const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; requested.push(url); const body = url.includes("/api/v1/simulation/batches") ? { data: [ { id: "01a12488-a69d-74c4-88b1-118bfe1993e4", status: "completed", approvalCode: "hosecloud", sourceSystem: "hosecloud", createdAt: "2026-10-10T06:38:17.757Z" }, { id: "01a123be-0742-743a-ac16-87f7c4e121c7", status: "completed", approvalCode: "hosecloud", sourceSystem: "hosecloud", createdAt: "2026-10-09T02:56:58.000Z" } ] } : { data: {} }; return new Response(JSON.stringify(body), { status: 200, headers: { "Content-Type": "application/json" } }); }) as typeof fetch; return requested; } describe("simulation local search", () => { test("search matches the console「任务 fe1993e4」label locally", async () => { setupMockEnv(); const requested = mockSimulationBatches(); const result = await runCli(["simulation", "search", "任务 fe1993e4"]); expect(result.isError).toBe(false); expect(result.exitCode).toBe(0); const payload = JSON.parse(result.stdout) as { count: number; scanned: number; retrieval: { mode: string; serverSideSearch: boolean }; batches: Array<{ id: string }>; }; expect(payload.count).toBe(1); expect(payload.retrieval).toMatchObject({ mode: "local_scan", serverSideSearch: false }); expect(payload.batches[0]?.id).toBe("01a12488-a69d-74c4-88b1-118bfe1993e4"); expect(result.stderr).toContain("本地聚合检索"); expect(requested.every((url) => url.includes("/api/v1/simulation/batches"))).toBe(true); }); test("search respects a local time window", async () => { setupMockEnv(); mockSimulationBatches(); const result = await runCli(["simulation", "search", "任务", "--since", "2026-10-09", "--until", "2026-10-09"]); expect(result.exitCode).toBe(0); const payload = JSON.parse(result.stdout) as { count: number; batches: Array<{ id: string }> }; expect(payload.count).toBe(1); expect(payload.batches[0]?.id).toBe("01a123be-0742-743a-ac16-87f7c4e121c7"); }); test("search without a keyword is a usage error", async () => { setupMockEnv(); const requested = mockSimulationBatches(); const result = await runCli(["simulation", "search"]); expect(result.isError).toBe(true); expect(result.exitCode).toBe(2); expect(requested).toEqual([]); }); }); describe("simulation command confirmation gates", () => { test("simulation clean (legacy alias) refuses without --yes and never reaches the API", async () => { setupMockEnv(); const mutations = trackMutationRequests(); const result = await runCli(["simulation", "clean", "batch-123"]); expect(result.isError).toBe(true); expect(result.exitCode).toBe(10); expect(result.stderr).toContain("Refusing to delete simulation batch 'batch-123' without --yes."); expect(result.stderr).toContain("seal-use simulation delete batch-123"); expect(mutations).toEqual([]); }); test("simulation delete alias also requires --yes", async () => { setupMockEnv(); const mutations = trackMutationRequests(); const result = await runCli(["simulation", "delete", "batch-123"]); expect(result.isError).toBe(true); expect(result.exitCode).toBe(10); expect(mutations).toEqual([]); }); test("simulation clean runs with --yes", async () => { setupMockEnv(); const mutations = trackMutationRequests(); const result = await runCli(["simulation", "clean", "batch-123", "--yes"]); expect(result.isError).toBe(false); expect(result.exitCode).toBe(0); expect(mutations.some((url) => url.includes("batch-123"))).toBe(true); }); });