Files
hose-use/src/cli/approval-dry-run.test.ts
T

34 lines
2.2 KiB
TypeScript

import { afterEach, expect, test } from "bun:test";
import { mkdtempSync, mkdirSync, writeFileSync, existsSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { runAiProCli } from "../aipro-cli.js";
import { runCli } from "./runner.js";
import { closeDatabase } from "../core/db/index.js";
const keys = ["HOSE_APPROVAL_DB", "HOSE_ENTERPRISES_DIR", "HOSE_CONFIG_FILE"] as const;
const original = keys.map(key => process.env[key]);
afterEach(() => { closeDatabase(); keys.forEach((key, i) => { if (original[i] === undefined) delete process.env[key]; else process.env[key] = original[i]; }); });
test("approval and aipro dry-runs override apply/yes without requests or database creation", async () => {
const root = mkdtempSync(join(tmpdir(), "hose-approval-preview-"));
const corps = join(root, "corps"); mkdirSync(corps);
writeFileSync(join(corps, "corp.json"), JSON.stringify({ id: "corp", name: "Preview", source: "manual", credentials: { accessToken: "fixture", corpId: "corp", staffId: "staff" } }));
process.env.HOSE_ENTERPRISES_DIR = corps;
process.env.HOSE_CONFIG_FILE = join(root, "config.json");
process.env.HOSE_APPROVAL_DB = join(root, "approval.db");
const originalFetch = globalThis.fetch;
let requests = 0;
globalThis.fetch = (async () => { requests++; throw new Error("No network during preview"); }) as any;
try {
for (const action of ["points", "review", "decide", "approve", "reject"]) {
const result = await runCli(["approval", action, "SN", "--apply", "--yes", "--dry-run", "--json"]);
expect(result.exitCode).toBe(0); expect(JSON.parse(result.stdout).dryRun).toBe(true);
}
for (const action of ["init", "bind", "review", "decide", "approve", "reject"]) {
const result = await runAiProCli([action, "approve", "--run", "ABC123", "--apply", "--yes", "--dry-run", "--json"]);
expect(result.exitCode).toBe(0); expect(JSON.parse(result.stdout).dryRun).toBe(true);
expect(JSON.parse(result.stdout).nextAction).toStartWith("hose-use aipro ");
}
expect(requests).toBe(0); expect(existsSync(process.env.HOSE_APPROVAL_DB)).toBe(false);
} finally { globalThis.fetch = originalFetch; }
});