import { expect, spyOn, test } from "bun:test"; import * as service from "../service.js"; import { parseArgs } from "../parser.js"; import { handleServiceCommand, handleAiProServiceCommand, handleApprovalRedirectCommand, } from "./service.js"; test("service lifecycle previews never dispatch process or state mutations", async () => { const dispatch = spyOn(service, "runServiceCommand").mockResolvedValue({ running: false } as any); try { for (const action of ["start", "stop", "restart", "run"]) { const result = await handleServiceCommand(parseArgs(["service", action, "--dry-run", "--yes"]), { jsonMode: true } as any); expect(JSON.parse(result.stdout).dryRun).toBe(true); expect(JSON.parse(result.stdout).changes.action).toBe(action); } expect(dispatch).not.toHaveBeenCalled(); await handleServiceCommand(parseArgs(["service", "status"]), { jsonMode: true } as any); expect(dispatch).toHaveBeenCalledTimes(1); // hose-use aipro status await handleAiProServiceCommand(parseArgs(["aipro", "status"]), { jsonMode: true } as any); expect(dispatch).toHaveBeenCalledTimes(2); // hose-use aipro bind 重定向阻断提示 const bindRedirect = await handleAiProServiceCommand(parseArgs(["aipro", "bind", "S123"]), { jsonMode: true } as any); expect(bindRedirect.exitCode).toBe(1); expect(bindRedirect.stderr).toContain("hose-aipro"); // hose-use approval 重定向提示 const approvalRedirect = await handleApprovalRedirectCommand(parseArgs(["approval", "decide", "S123"]), { jsonMode: true } as any); expect(approvalRedirect.exitCode).toBe(1); expect(approvalRedirect.stderr).toContain("hose-aipro"); } finally { dispatch.mockRestore(); } });