import { describe, expect, spyOn, test } from "bun:test"; import { runMaintenance } from "../../command/maintenance"; import type { ApiRequestInit } from "../../lib/api"; describe("maintenance command", () => { test("first call reads status and asks for confirmation without mutation", async () => { const calls: string[] = []; const output = spyOn(console, "log").mockImplementation(() => {}); try { await runMaintenance( "a.example.com", async (path: string) => { calls.push(path); return { host: "a.example.com", enabled: false, hosts: [] } as T; }, async () => false, ); expect(calls).toEqual(["/maintenance/a.example.com"]); expect(output).toHaveBeenCalledWith( expect.stringContaining("currently \x1b[32mOFF\x1b[0m"), ); } finally { output.mockRestore(); } }); test("confirmed call posts an idempotent desired state and prints result", async () => { const calls: Array<{ path: string; init?: { method?: string; json?: unknown }; }> = []; const output = spyOn(console, "log").mockImplementation(() => {}); try { await runMaintenance( "a.example.com", async (path: string, init?: ApiRequestInit) => { calls.push({ path, init }); return ( calls.length === 1 ? { host: "a.example.com", enabled: false, hosts: [] } : { host: "a.example.com", enabled: true, hosts: ["a.example.com"], } ) as T; }, async () => true, ); expect(calls).toEqual([ { path: "/maintenance/a.example.com", init: undefined }, { path: "/maintenance/a.example.com", init: { method: "POST", json: { enabled: true } }, }, ]); expect(output).toHaveBeenCalledWith( "a.example.com maintenance is now \x1b[31mON\x1b[0m", ); } finally { output.mockRestore(); } }); });