|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + CROSS_SERVICE_CONTRACT_MAP, |
| 4 | + getContractDefinition, |
| 5 | + getContractsByConsumer, |
| 6 | + getContractsByProducer, |
| 7 | + listAllContracts, |
| 8 | + validateEventContractPayload, |
| 9 | + verifyCrossServiceContractMap |
| 10 | +} from "@/core/contract/contract-map"; |
| 11 | + |
| 12 | +describe("Cross-Service Contract Map (#219)", () => { |
| 13 | + it("verifies the integrity and completeness of all registered contracts", () => { |
| 14 | + const verification = verifyCrossServiceContractMap(); |
| 15 | + expect(verification.ok).toBe(true); |
| 16 | + if (!verification.ok) return; |
| 17 | + |
| 18 | + expect(verification.value.valid).toBe(true); |
| 19 | + expect(verification.value.totalContracts).toBeGreaterThanOrEqual(10); |
| 20 | + expect(verification.value.producersCount).toBeGreaterThanOrEqual(5); |
| 21 | + expect(verification.value.consumersCount).toBeGreaterThanOrEqual(5); |
| 22 | + }); |
| 23 | + |
| 24 | + it("retrieves contract definition by eventId", () => { |
| 25 | + const telemetryContract = getContractDefinition("telemetry.event"); |
| 26 | + expect(telemetryContract).toBeDefined(); |
| 27 | + expect(telemetryContract?.producer).toBe("core/telemetry"); |
| 28 | + expect(telemetryContract?.versionRules.currentVersion).toBe("1.0"); |
| 29 | + expect(telemetryContract?.failurePolicy.allowedFailureBehavior).toBe("fail_silent"); |
| 30 | + |
| 31 | + const nonExistent = getContractDefinition("non.existent.event"); |
| 32 | + expect(nonExistent).toBeUndefined(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("filters contracts by producer and consumer", () => { |
| 36 | + const telemetryContracts = getContractsByProducer("core/telemetry"); |
| 37 | + expect(telemetryContracts.some((c) => c.eventId === "telemetry.event")).toBe(true); |
| 38 | + |
| 39 | + const auditConsumers = getContractsByConsumer("core/audit"); |
| 40 | + expect(auditConsumers.length).toBeGreaterThanOrEqual(2); |
| 41 | + expect(auditConsumers.some((c) => c.eventId === "telemetry.event" || c.eventId === "export.envelope")).toBe(true); |
| 42 | + }); |
| 43 | + |
| 44 | + describe("Payload Validation against Contract Schemas", () => { |
| 45 | + it("validates compliant telemetry.event payload", () => { |
| 46 | + const validPayload = { |
| 47 | + op: "horizon.request", |
| 48 | + actorType: "client", |
| 49 | + result: "success", |
| 50 | + latencyMs: 125, |
| 51 | + correlationId: "corr-12345", |
| 52 | + timestamp: "2026-10-19T10:00:00.000Z" |
| 53 | + }; |
| 54 | + |
| 55 | + const result = validateEventContractPayload("telemetry.event", validPayload); |
| 56 | + expect(result.ok).toBe(true); |
| 57 | + }); |
| 58 | + |
| 59 | + it("rejects telemetry.event payload with missing required field", () => { |
| 60 | + const invalidPayload = { |
| 61 | + op: "horizon.request", |
| 62 | + result: "success", |
| 63 | + // missing actorType, latencyMs, correlationId, timestamp |
| 64 | + }; |
| 65 | + |
| 66 | + const result = validateEventContractPayload("telemetry.event", invalidPayload); |
| 67 | + expect(result.ok).toBe(false); |
| 68 | + if (result.ok) return; |
| 69 | + expect(result.code).toBe("missing_required_field"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("rejects telemetry.event payload with wrong field type", () => { |
| 73 | + const wrongTypePayload = { |
| 74 | + op: "horizon.request", |
| 75 | + actorType: "client", |
| 76 | + result: "success", |
| 77 | + latencyMs: "125ms", // should be number |
| 78 | + correlationId: "corr-12345", |
| 79 | + timestamp: "2026-10-19T10:00:00.000Z" |
| 80 | + }; |
| 81 | + |
| 82 | + const result = validateEventContractPayload("telemetry.event", wrongTypePayload); |
| 83 | + expect(result.ok).toBe(false); |
| 84 | + if (result.ok) return; |
| 85 | + expect(result.code).toBe("wrong_type"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("validates compliant worker.job payload with retry and idempotency policies", () => { |
| 89 | + const jobContract = getContractDefinition("worker.job"); |
| 90 | + expect(jobContract?.idempotency.required).toBe(true); |
| 91 | + expect(jobContract?.failurePolicy.allowedFailureBehavior).toBe("retry_and_dead_letter"); |
| 92 | + expect(jobContract?.failurePolicy.deadLetterQueue).toBe(true); |
| 93 | + |
| 94 | + const validJob = { |
| 95 | + id: "job-991", |
| 96 | + operation: "maintenance.cleanup", |
| 97 | + status: "queued", |
| 98 | + attempts: 0, |
| 99 | + enqueuedAt: "2026-10-19T10:00:00.000Z", |
| 100 | + correlationId: "corr-991" |
| 101 | + }; |
| 102 | + |
| 103 | + const result = validateEventContractPayload("worker.job", validJob); |
| 104 | + expect(result.ok).toBe(true); |
| 105 | + }); |
| 106 | + |
| 107 | + it("validates compliant export.envelope payload", () => { |
| 108 | + const validEnvelope = { |
| 109 | + schemaVersion: "1.0", |
| 110 | + scope: "own", |
| 111 | + generatedAt: "2026-10-19T10:00:00.000Z", |
| 112 | + expiresAt: "2026-10-20T10:00:00.000Z", |
| 113 | + generator: "utix-export@1", |
| 114 | + correlationId: "corr-exp-1", |
| 115 | + recordCount: 1, |
| 116 | + page: 1, |
| 117 | + totalRecords: 1, |
| 118 | + records: [{ op: "wallet.detect", result: "success" }] |
| 119 | + }; |
| 120 | + |
| 121 | + const result = validateEventContractPayload("export.envelope", validEnvelope); |
| 122 | + expect(result.ok).toBe(true); |
| 123 | + }); |
| 124 | + |
| 125 | + it("rejects unknown event contracts with contract_drift", () => { |
| 126 | + const result = validateEventContractPayload("unknown.event", {}); |
| 127 | + expect(result.ok).toBe(false); |
| 128 | + if (result.ok) return; |
| 129 | + expect(result.code).toBe("contract_drift"); |
| 130 | + }); |
| 131 | + |
| 132 | + it("handles version negotiation and rejects future unsupported versions", () => { |
| 133 | + const validPayload = { |
| 134 | + op: "horizon.request", |
| 135 | + actorType: "client", |
| 136 | + result: "success", |
| 137 | + latencyMs: 125, |
| 138 | + correlationId: "corr-12345", |
| 139 | + timestamp: "2026-10-19T10:00:00.000Z" |
| 140 | + }; |
| 141 | + |
| 142 | + const result = validateEventContractPayload("telemetry.event", validPayload, { |
| 143 | + requestedVersion: "99.0" // future version |
| 144 | + }); |
| 145 | + |
| 146 | + expect(result.ok).toBe(false); |
| 147 | + if (result.ok) return; |
| 148 | + expect(result.code).toBe("schema_future"); |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + describe("Failure Behavior and Policy Consistency", () => { |
| 153 | + it("ensures every contract declares a defined failure behavior and default error code", () => { |
| 154 | + for (const contract of listAllContracts()) { |
| 155 | + expect(["fail_silent", "retry_and_dead_letter", "revert_state", "safe_fallback", "reject_with_code"]).toContain( |
| 156 | + contract.failurePolicy.allowedFailureBehavior |
| 157 | + ); |
| 158 | + expect(contract.failurePolicy.defaultErrorCode).toBeTruthy(); |
| 159 | + expect(["halt", "skip", "quarantine"]).toContain(contract.failurePolicy.poisonHandling); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + it("ensures high-risk write operations enforce idempotency", () => { |
| 164 | + const writeEvents = ["worker.job", "export.envelope", "idempotency.record", "ownership.transfer", "webhook.delivery"]; |
| 165 | + for (const eventId of writeEvents) { |
| 166 | + const contract = getContractDefinition(eventId); |
| 167 | + expect(contract?.idempotency.required).toBe(true); |
| 168 | + expect(contract?.idempotency.keyField).toBeDefined(); |
| 169 | + } |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments