111 lines
5.0 KiB
TypeScript
111 lines
5.0 KiB
TypeScript
import { createLogger } from "@hivegrid/logger";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import {
|
|
createSignedDomainCommand,
|
|
verifySignedDomainResult,
|
|
type DomainResultPayload,
|
|
type SignedDomainEnvelope,
|
|
} from "./contracts.js";
|
|
import { DomainControllerFailure, type PortalDomainController } from "./controller.js";
|
|
import { createControllerMetrics } from "./metrics.js";
|
|
import { createDomainCommandProcessor } from "./queue.js";
|
|
|
|
const SECRET = "test-command-secret-that-is-at-least-thirty-two-bytes";
|
|
const payload = {
|
|
claimId: "claim_12345678",
|
|
tenantId: "tenant_12345678",
|
|
generation: 1,
|
|
hostname: "portal.example.com",
|
|
action: "provision" as const,
|
|
};
|
|
|
|
describe("portal-domain queue processor", () => {
|
|
it("verifies commands and publishes an independently signed result", async () => {
|
|
const published: SignedDomainEnvelope<DomainResultPayload>[] = [];
|
|
const controller: PortalDomainController = {
|
|
handle: vi.fn(async (command, attempt) => ({
|
|
...command,
|
|
status: "active",
|
|
attempt,
|
|
completedAt: "2026-07-16T10:00:00.000Z",
|
|
configHash: "a".repeat(64),
|
|
certificateExpiresAt: "2026-10-01T00:00:00.000Z",
|
|
})),
|
|
};
|
|
const processor = createProcessor(controller, published);
|
|
await processor({ data: createSignedDomainCommand(payload, SECRET), attemptsMade: 0 });
|
|
expect(published).toHaveLength(1);
|
|
expect(verifySignedDomainResult(published[0], SECRET)).toMatchObject({ status: "active", attempt: 1 });
|
|
});
|
|
|
|
it("rejects unsigned or tampered commands without emitting a reflected result", async () => {
|
|
const published: SignedDomainEnvelope<DomainResultPayload>[] = [];
|
|
const controller: PortalDomainController = { handle: vi.fn() };
|
|
const processor = createProcessor(controller, published);
|
|
const signed = createSignedDomainCommand(payload, SECRET);
|
|
await expect(processor({ data: { ...signed, payload: { ...signed.payload, hostname: "evil.example.com" } }, attemptsMade: 0 })).rejects.toThrow("invalid_domain_command");
|
|
expect(published).toEqual([]);
|
|
expect(controller.handle).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("publishes signed retry and terminal outcomes around the four-attempt ceiling", async () => {
|
|
const published: SignedDomainEnvelope<DomainResultPayload>[] = [];
|
|
const controller: PortalDomainController = {
|
|
handle: vi.fn().mockRejectedValue(new DomainControllerFailure("https_health_failed", true, "transient")),
|
|
};
|
|
const processor = createProcessor(controller, published);
|
|
const signed = createSignedDomainCommand(payload, SECRET);
|
|
await expect(processor({ data: signed, attemptsMade: 0 })).rejects.toThrow("https_health_failed");
|
|
expect(verifySignedDomainResult(published[0], SECRET)).toMatchObject({ status: "retrying", retryAfterMs: 900_000 });
|
|
|
|
await expect(processor({ data: signed, attemptsMade: 3 })).resolves.toBeUndefined();
|
|
expect(verifySignedDomainResult(published[1], SECRET)).toMatchObject({ status: "failed", attempt: 4, reasonCode: "https_health_failed" });
|
|
});
|
|
|
|
it("keeps release mismatches inside the same four-attempt safety budget", async () => {
|
|
const published: SignedDomainEnvelope<DomainResultPayload>[] = [];
|
|
const controller: PortalDomainController = {
|
|
handle: vi.fn().mockRejectedValue(new DomainControllerFailure("release_revision_mismatch", true, "release overlap")),
|
|
};
|
|
const processor = createProcessor(controller, published);
|
|
|
|
await expect(processor({ data: createSignedDomainCommand(payload, SECRET), attemptsMade: 0 })).rejects.toThrow("release_revision_mismatch");
|
|
expect(verifySignedDomainResult(published[0], SECRET)).toMatchObject({
|
|
status: "retrying",
|
|
reasonCode: "release_revision_mismatch",
|
|
retryAfterMs: 900_000,
|
|
});
|
|
|
|
await expect(processor({ data: createSignedDomainCommand(payload, SECRET), attemptsMade: 3 })).resolves.toBeUndefined();
|
|
expect(verifySignedDomainResult(published[1], SECRET)).toMatchObject({
|
|
status: "failed",
|
|
attempt: 4,
|
|
reasonCode: "release_revision_mismatch",
|
|
});
|
|
});
|
|
|
|
it("does not retry terminal controller failures", async () => {
|
|
const published: SignedDomainEnvelope<DomainResultPayload>[] = [];
|
|
const controller: PortalDomainController = {
|
|
handle: vi.fn().mockRejectedValue(new DomainControllerFailure("unsafe_config_path", false, "terminal")),
|
|
};
|
|
const processor = createProcessor(controller, published);
|
|
await expect(processor({ data: createSignedDomainCommand(payload, SECRET), attemptsMade: 0 })).resolves.toBeUndefined();
|
|
expect(verifySignedDomainResult(published[0], SECRET)).toMatchObject({ status: "failed", attempt: 1 });
|
|
});
|
|
});
|
|
|
|
function createProcessor(
|
|
controller: PortalDomainController,
|
|
published: SignedDomainEnvelope<DomainResultPayload>[]
|
|
) {
|
|
return createDomainCommandProcessor({
|
|
controller,
|
|
commandSecret: SECRET,
|
|
resultPublisher: async (result) => { published.push(result); },
|
|
logger: createLogger({ level: "silent" }),
|
|
metrics: createControllerMetrics("test", false),
|
|
now: () => new Date("2026-07-16T10:00:00.000Z"),
|
|
});
|
|
}
|