|
| 1 | +/** |
| 2 | + * @module tests/contract/pending-envelope.conformance |
| 3 | + * |
| 4 | + * THE CONTRACT-CONFORMANCE SPINE (issue #118, B1 slice 1 — sdk side). |
| 5 | + * |
| 6 | + * This is the cross-repo, cross-license bridge. The sdk cannot import core's zod |
| 7 | + * schema (core is BUSL-1.1/private, the sdk is MIT/public). Instead: |
| 8 | + * |
| 9 | + * 1. core emits `dist/wire-contract.json` (a JSON-Schema dump of its exported |
| 10 | + * `pendingEnvelopeSchema`) via `npm run build:contract`. |
| 11 | + * 2. the sdk VENDORS a copy at `tests/fixtures/wire-contract.json`. |
| 12 | + * 3. THIS test asserts the sdk's hand-declared `PendingEnvelopeWire` |
| 13 | + * (via its runtime descriptor `PENDING_ENVELOPE_WIRE_FIELDS`) is structurally |
| 14 | + * equal to core's snapshot definition — same field set, same primitive types, |
| 15 | + * and core's `.strict()` (`additionalProperties:false`) is honored. |
| 16 | + * |
| 17 | + * When core renames/adds/removes a pending-envelope field, regenerating the |
| 18 | + * snapshot and re-vendoring it makes this test FAIL until the sdk mirror is |
| 19 | + * updated in lock-step. That CI failure IS the regression shield for #118 — drift |
| 20 | + * is caught at build time, never in production. |
| 21 | + * |
| 22 | + * To refresh the snapshot after an intentional core change: |
| 23 | + * (in paybot-core) npm run build && npm run build:contract |
| 24 | + * then copy core's dist/wire-contract.json → this repo's tests/fixtures/. |
| 25 | + * |
| 26 | + * Test naming convention: `[CONTRACT] subject — should [behavior] when [condition]`. |
| 27 | + */ |
| 28 | + |
| 29 | +import { describe, it, expect } from 'vitest'; |
| 30 | +import wireContract from '../fixtures/wire-contract.json'; |
| 31 | +import { |
| 32 | + PENDING_ENVELOPE_WIRE_FIELDS, |
| 33 | + type PendingEnvelopeWire, |
| 34 | + type WireFieldType, |
| 35 | +} from '../../src/wire-contract.js'; |
| 36 | + |
| 37 | +/** The shape of one JSON-Schema property entry we care about. */ |
| 38 | +interface JsonSchemaProperty { |
| 39 | + type?: string; |
| 40 | + const?: unknown; |
| 41 | +} |
| 42 | + |
| 43 | +/** The shape of one contract definition in the vendored snapshot. */ |
| 44 | +interface JsonSchemaDefinition { |
| 45 | + type: string; |
| 46 | + properties: Record<string, JsonSchemaProperty>; |
| 47 | + required: string[]; |
| 48 | + additionalProperties: boolean; |
| 49 | +} |
| 50 | + |
| 51 | +const CONTRACT_NAME = 'PendingEnvelope'; |
| 52 | + |
| 53 | +function getDefinition(name: string): JsonSchemaDefinition { |
| 54 | + const defs = (wireContract as { definitions: Record<string, JsonSchemaDefinition> }).definitions; |
| 55 | + const def = defs[name]; |
| 56 | + if (def === undefined) { |
| 57 | + throw new Error( |
| 58 | + `Vendored snapshot has no "${name}" definition. Re-run core \`build:contract\` and re-vendor tests/fixtures/wire-contract.json.` |
| 59 | + ); |
| 60 | + } |
| 61 | + return def; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Map a JSON-Schema property to the sdk's primitive type tag, so the two sides |
| 66 | + * are comparable. A `const` literal (e.g. core's `z.literal('pending_approval')`) |
| 67 | + * still carries `type:'string'` in the dump, so this stays simple. |
| 68 | + */ |
| 69 | +function jsonSchemaTypeToWireType(prop: JsonSchemaProperty): WireFieldType { |
| 70 | + switch (prop.type) { |
| 71 | + case 'string': |
| 72 | + return 'string'; |
| 73 | + case 'number': |
| 74 | + case 'integer': |
| 75 | + return 'number'; |
| 76 | + case 'boolean': |
| 77 | + return 'boolean'; |
| 78 | + default: |
| 79 | + throw new Error(`Unhandled JSON-Schema type "${String(prop.type)}" in snapshot.`); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +describe('pending-envelope wire conformance (sdk ⊇⊆ core snapshot)', () => { |
| 84 | + const def = getDefinition(CONTRACT_NAME); |
| 85 | + const snapshotFields = Object.keys(def.properties).sort(); |
| 86 | + const sdkFields = Object.keys(PENDING_ENVELOPE_WIRE_FIELDS).sort(); |
| 87 | + |
| 88 | + it('[CONTRACT] PendingEnvelopeWire — should declare EXACTLY core’s field set (happy)', () => { |
| 89 | + // Bidirectional: no field the sdk forgot, no field the sdk invented. |
| 90 | + expect(sdkFields).toEqual(snapshotFields); |
| 91 | + }); |
| 92 | + |
| 93 | + it('[CONTRACT] PendingEnvelopeWire — should match core’s primitive type for every field (happy)', () => { |
| 94 | + for (const field of snapshotFields) { |
| 95 | + const expected = jsonSchemaTypeToWireType(def.properties[field]); |
| 96 | + const actual = PENDING_ENVELOPE_WIRE_FIELDS[field as keyof typeof PENDING_ENVELOPE_WIRE_FIELDS]; |
| 97 | + expect(actual, `field "${field}" type mismatch`).toBe(expected); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + it('[CONTRACT] snapshot — should mark every field required AND forbid extras (.strict) (edge)', () => { |
| 102 | + // core uses `.strict()`; the dump must carry that through so drift fails closed. |
| 103 | + expect(def.additionalProperties).toBe(false); |
| 104 | + expect([...def.required].sort()).toEqual(snapshotFields); |
| 105 | + }); |
| 106 | + |
| 107 | + it('[CONTRACT] snapshot — should be a versioned paybot-core artifact (edge)', () => { |
| 108 | + // Guard against vendoring an unrelated/garbage JSON by accident. |
| 109 | + const meta = wireContract as { $contract?: string; version?: number; issue?: number }; |
| 110 | + expect(meta.$contract).toBe('paybot-core wire contract'); |
| 111 | + expect(typeof meta.version).toBe('number'); |
| 112 | + expect(meta.issue).toBe(118); |
| 113 | + }); |
| 114 | + |
| 115 | + it('[CONTRACT] PendingEnvelopeWire — should keep status as the pending discriminator (happy)', () => { |
| 116 | + // The one field the sdk semantically depends on at the `pay()` branch. |
| 117 | + const statusProp = def.properties.status; |
| 118 | + expect(statusProp.type).toBe('string'); |
| 119 | + expect(statusProp.const).toBe('pending_approval'); |
| 120 | + // Compile-time anchor: a literal value still typed against the wire interface. |
| 121 | + const probe: Pick<PendingEnvelopeWire, 'status'> = { status: 'pending_approval' }; |
| 122 | + expect(probe.status).toBe('pending_approval'); |
| 123 | + }); |
| 124 | +}); |
0 commit comments