66 lines
2.6 KiB
TypeScript
66 lines
2.6 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { checkFactory, formatFactoryCheck } from "./factory-status.js";
|
|
|
|
const ok = (json: unknown, status = 200) => ({ ok: true, status, json: async () => json });
|
|
const fail = (status: number) => ({ ok: false, status, json: async () => ({}) });
|
|
|
|
describe("factory check", () => {
|
|
it("prints live counts from response data", () => {
|
|
const out = formatFactoryCheck(
|
|
{ status: "ok", phase: 4, containers: 24, agents: 6, infra: 14, monitoring: 5, skills: 63 },
|
|
{ status: "ok", service: "git-proxy-deploy", phase: 4 },
|
|
);
|
|
|
|
expect(out).toContain("Containers: 24");
|
|
expect(out).toContain("Deploy route: ok (git-proxy-deploy)");
|
|
});
|
|
|
|
it("accepts factory-status 200", async () => {
|
|
const result = await checkFactory(async (url) =>
|
|
url.includes("/factory-status")
|
|
? ok({ status: "ok", containers: 25 })
|
|
: fail(401),
|
|
);
|
|
|
|
expect(result.factoryAvailable).toBe(true);
|
|
expect(result.deployAvailable).toBe(true);
|
|
expect(result.factory.containers).toBe(25);
|
|
});
|
|
|
|
it("degrades factory-status 404 to unknown while preserving deploy health", async () => {
|
|
const result = await checkFactory(async (url) =>
|
|
url.includes("/factory-status") ? fail(404) : fail(401),
|
|
);
|
|
|
|
expect(result.factoryAvailable).toBe(false);
|
|
expect(result.deploy.status).toBe("ok");
|
|
const out = formatFactoryCheck(result.factory, result.deploy, result.errors);
|
|
expect(out).toContain("Factory API: unknown (returned 404)");
|
|
expect(out).toContain("Factory: unknown");
|
|
expect(result.errors[0]).toContain("returned 404");
|
|
});
|
|
|
|
it("degrades malformed factory-status JSON to unknown", async () => {
|
|
const result = await checkFactory(async (url) =>
|
|
url.includes("/factory-status") ? ok(null) : fail(401),
|
|
);
|
|
|
|
expect(result.factoryAvailable).toBe(false);
|
|
expect(result.factory.status).toBeUndefined();
|
|
expect(result.errors[0]).toContain("malformed JSON");
|
|
});
|
|
|
|
it("degrades factory-status timeout without using stale memory", async () => {
|
|
const result = await checkFactory(async (url, init) => {
|
|
if (!url.includes("/factory-status")) return fail(401);
|
|
await new Promise((_resolve, reject) => init?.signal?.addEventListener("abort", () => reject(Object.assign(new Error("aborted"), { name: "AbortError" }))));
|
|
return ok({ status: "stale-memory-should-not-appear" });
|
|
}, 1);
|
|
|
|
expect(result.factoryAvailable).toBe(false);
|
|
expect(result.factory.status).toBeUndefined();
|
|
expect(result.deploy.status).toBe("ok");
|
|
expect(result.errors[0]).toContain("timed out");
|
|
});
|
|
});
|