// @vitest-environment node import { describe, expect, it } from "vitest"; import type { CustomerPortalCatalogPlan } from "./catalog"; import { assessCatalogAuditRecovery, type CatalogAuditRecoveryRow } from "./provision-catalog"; function plan(providerAction: "create" | "update" | "noop" = "noop"): CustomerPortalCatalogPlan { return { provider: [ { resource: "plan", code: "customer_portal_monthly", action: providerAction, driftFields: [] }, { resource: "add_on", code: "customer_portal_domain_setup", action: "noop", driftFields: [] }, ], database: { action: "noop", reconcileIssues: [], blockingIssues: [] }, }; } function row(index: number, action: CatalogAuditRecoveryRow["action"], targetId: string | null): CatalogAuditRecoveryRow { return { id: String(index).padStart(3, "0"), action, targetId, createdAt: new Date(1_700_000_000_000 + index) }; } describe("customer-portal catalog audit recovery", () => { it("reconciles a canonical provider resource after a committed POST timed out or its created audit failed", () => { const recovery = assessCatalogAuditRecovery( [ row(1, "customer_portal.catalog.provider_requested", "customer_portal_monthly"), row(2, "customer_portal.catalog.provider_failed", "customer_portal_monthly"), ], plan() ); expect(recovery.providerCodesNeedingReconciliation).toEqual(["customer_portal_monthly"]); }); it("does not mark an unresolved request as reconciled while the provider resource is still missing", () => { const recovery = assessCatalogAuditRecovery([row(1, "customer_portal.catalog.provider_requested", "customer_portal_monthly")], plan("create")); expect(recovery.providerCodesNeedingReconciliation).toEqual([]); }); it("repairs a missing final provisioned audit after provider or database success", () => { const providerRecovery = assessCatalogAuditRecovery([row(1, "customer_portal.catalog.provider_created", "customer_portal_monthly")], plan()); expect(providerRecovery).toMatchObject({ providerCodesNeedingReconciliation: [], providerCodesNeedingProvisionedAudit: ["customer_portal_monthly"], needsProvisionedAudit: true, }); const databaseRecovery = assessCatalogAuditRecovery([row(1, "customer_portal.catalog.database_reconciled", null)], plan()); expect(databaseRecovery).toMatchObject({ databaseNeedsProvisionedAudit: true, needsProvisionedAudit: true }); }); it("is quiescent once a later canonical provisioned audit closes the recovery window", () => { const recovery = assessCatalogAuditRecovery( [ row(1, "customer_portal.catalog.provider_requested", "customer_portal_monthly"), row(2, "customer_portal.catalog.provider_reconciled", "customer_portal_monthly"), row(3, "customer_portal.catalog.database_reconciled", null), row(4, "customer_portal.catalog.provisioned", null), ], plan() ); expect(recovery).toEqual({ providerCodesNeedingReconciliation: [], providerCodesNeedingProvisionedAudit: [], databaseNeedsProvisionedAudit: false, needsProvisionedAudit: false, }); }); });