feat: add maintenance toggle command

This commit is contained in:
2026-09-18 15:21:28 +00:00 Unverified
parent e3df2f4d76
commit dcdacd5c04
12 changed files with 712 additions and 7 deletions
+64
View File
@@ -0,0 +1,64 @@
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 <T>(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 <T>(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();
}
});
});