autotel-message-contract 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":["path","defaultSerializer"],"sources":["../src/diff.ts","../src/reader.ts","../src/snapshot-storage.ts","../src/contract.ts"],"sourcesContent":["/**\n * Minimal line diff for snapshot failures. The goal is a message that points\n * at *what moved* — a renamed field, a switched date format, a dropped value —\n * not a full diff engine. Lines only in the approved file are marked `-`, lines\n * only in the actual output are marked `+`, and a little surrounding context is\n * kept so the change is readable in a terminal.\n */\nexport function lineDiff(approved: string, actual: string): string {\n const a = approved.split('\\n');\n const b = actual.split('\\n');\n const lcs = longestCommonSubsequence(a, b);\n\n const out: string[] = [];\n let i = 0;\n let j = 0;\n for (const [ai, bj] of lcs) {\n while (i < ai) out.push(`- ${a[i++]}`);\n while (j < bj) out.push(`+ ${b[j++]}`);\n out.push(` ${a[i++]}`);\n j++;\n }\n while (i < a.length) out.push(`- ${a[i++]}`);\n while (j < b.length) out.push(`+ ${b[j++]}`);\n\n return out.join('\\n');\n}\n\n/** Indices `[i, j]` of lines common to both, longest such subsequence. */\nfunction longestCommonSubsequence(\n a: string[],\n b: string[],\n): Array<[number, number]> {\n const n = a.length;\n const m = b.length;\n const table: number[][] = Array.from({ length: n + 1 }, () =>\n Array.from({ length: m + 1 }, () => 0),\n );\n for (let i = n - 1; i >= 0; i--) {\n for (let j = m - 1; j >= 0; j--) {\n table[i][j] =\n a[i] === b[j]\n ? table[i + 1][j + 1] + 1\n : Math.max(table[i + 1][j], table[i][j + 1]);\n }\n }\n const pairs: Array<[number, number]> = [];\n let i = 0;\n let j = 0;\n while (i < n && j < m) {\n if (a[i] === b[j]) {\n pairs.push([i, j]);\n i++;\n j++;\n } else if (table[i + 1][j] >= table[i][j + 1]) {\n i++;\n } else {\n j++;\n }\n }\n return pairs;\n}\n","/**\n * Readers — the \"as this version\" side of a compatibility check.\n *\n * In a JVM contract library you write `whenDeserializedAs(NewType.class)` and\n * reflection does the rest. TypeScript types are erased at runtime, so there is\n * no class to hand over. Instead you describe the *reader*: the thing that\n * accepts a deserialized value and either produces a typed result or rejects\n * it. Two shapes are accepted, in order of how most TS codebases already model\n * a message version:\n *\n * 1. A **Standard Schema** (Zod ≥3.24, Valibot, ArkType, …) — anything exposing\n * the `~standard` interface. This is the recommended form: the schema is the\n * version, and it already lives next to your message type.\n * 2. A plain **parse function** `(value) => T` that throws on incompatible input.\n *\n * A reader that accepts the value proves compatibility; a reader that throws or\n * reports issues proves the versions have drifted apart.\n */\n\n/** The subset of the Standard Schema v1 interface we rely on. */\nexport interface StandardSchemaLike<Output = unknown> {\n readonly '~standard': {\n readonly version: 1;\n readonly vendor: string;\n readonly validate: (value: unknown) =>\n | StandardResult<Output>\n | Promise<StandardResult<Output>>;\n };\n}\n\ninterface StandardResult<Output> {\n value?: Output;\n issues?: ReadonlyArray<{ readonly message: string; readonly path?: unknown }>;\n}\n\n/** A bare parse function: returns the typed value or throws. */\nexport type ParseFn<Output = unknown> = (value: unknown) => Output;\n\n/** Either accepted reader form. */\nexport type Reader<Output = unknown> =\n | StandardSchemaLike<Output>\n | ParseFn<Output>;\n\nfunction isStandardSchema(reader: Reader): reader is StandardSchemaLike {\n return (\n typeof reader === 'object' &&\n reader !== null &&\n '~standard' in reader &&\n typeof (reader as StandardSchemaLike)['~standard']?.validate === 'function'\n );\n}\n\nexport interface ReadOutcome<Output = unknown> {\n ok: boolean;\n /** Present when `ok`. */\n value?: Output;\n /** Human-readable reasons the reader rejected the value. */\n issues: string[];\n}\n\n/**\n * Run a reader against a deserialized value. Never throws — a thrown parse\n * error or reported issues become `{ ok: false, issues }` so the caller can\n * build a single, legible failure message.\n */\nexport async function read<Output>(\n reader: Reader<Output>,\n value: unknown,\n): Promise<ReadOutcome<Output>> {\n if (isStandardSchema(reader)) {\n try {\n const result = await reader['~standard'].validate(value);\n if (result.issues && result.issues.length > 0) {\n return {\n ok: false,\n issues: result.issues.map(\n (issue) => formatIssue(issue.message, issue.path),\n ),\n };\n }\n return { ok: true, value: result.value as Output, issues: [] };\n } catch (error) {\n return { ok: false, issues: [errorMessage(error)] };\n }\n }\n\n try {\n const parsed = (reader as ParseFn<Output>)(value);\n return { ok: true, value: parsed, issues: [] };\n } catch (error) {\n return { ok: false, issues: [errorMessage(error)] };\n }\n}\n\nfunction formatIssue(message: string, path: unknown): string {\n if (Array.isArray(path) && path.length > 0) {\n return `${path.map(String).join('.')}: ${message}`;\n }\n return message;\n}\n\nfunction errorMessage(error: unknown): string {\n if (error instanceof Error) return error.message;\n return String(error);\n}\n","import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';\nimport path from 'node:path';\n\n/**\n * Snapshot storage.\n *\n * A snapshot is just a file you commit: the serialized shape of a message,\n * reviewed once and from then on compared on every run. There is no broker, no\n * registry, no service to start — a format change shows up in a normal diff,\n * reviewed like any other code. This mirrors the \"approved file\" convention\n * (`<name>.approved.txt`) familiar from approval testing.\n *\n * By default the file lives in a `__contracts__` directory beside the test that\n * created it, resolved from the call stack so you do not have to thread paths\n * around. Override `dir` / `path` when you keep snapshots elsewhere.\n */\n\n/** Set any of these (e.g. `AUTOTEL_CONTRACT_UPDATE=1`) to (re)write approved files. */\nconst UPDATE_ENV_VARS = [\n 'AUTOTEL_CONTRACT_UPDATE',\n 'UPDATE_CONTRACTS',\n 'UPDATE_SNAPSHOTS',\n] as const;\n\nexport function isUpdateMode(env: NodeJS.ProcessEnv = process.env): boolean {\n return UPDATE_ENV_VARS.some((name) => {\n const value = env[name];\n return value === '1' || value === 'true';\n });\n}\n\nexport interface SnapshotLocation {\n /** Directory holding the approved file. */\n dir?: string;\n /** Logical name; the file becomes `<name>.approved.txt`. */\n name: string;\n /** Fully-qualified path; when set, `dir`/`name` are ignored for placement. */\n path?: string;\n}\n\nconst DEFAULT_DIR_NAME = '__contracts__';\nconst APPROVED_SUFFIX = '.approved.txt';\n\n/** Resolve the absolute file path for a snapshot. */\nexport function resolveSnapshotPath(location: SnapshotLocation): string {\n if (location.path) {\n return path.isAbsolute(location.path)\n ? location.path\n : path.resolve(process.cwd(), location.path);\n }\n const dir = location.dir ?? defaultSnapshotDir();\n return path.join(dir, `${sanitize(location.name)}${APPROVED_SUFFIX}`);\n}\n\nfunction sanitize(name: string): string {\n // Keep it filesystem-safe while preserving readable type names.\n return name.replaceAll(/[^\\w.@-]+/g, '_');\n}\n\nexport interface ReadSnapshotResult {\n exists: boolean;\n content?: string;\n path: string;\n}\n\nexport function readSnapshot(location: SnapshotLocation): ReadSnapshotResult {\n const filePath = resolveSnapshotPath(location);\n if (!existsSync(filePath)) return { exists: false, path: filePath };\n return { exists: true, content: readFileSync(filePath, 'utf8'), path: filePath };\n}\n\nexport function writeSnapshot(\n location: SnapshotLocation,\n content: string,\n): string {\n const filePath = resolveSnapshotPath(location);\n mkdirSync(path.dirname(filePath), { recursive: true });\n writeFileSync(filePath, content, 'utf8');\n return filePath;\n}\n\n/**\n * Best-effort `__contracts__` directory beside the calling test file, found by\n * walking the stack past this module and the contract internals. Falls back to\n * `<cwd>/__contracts__` when the caller cannot be determined (e.g. bundled).\n */\nfunction defaultSnapshotDir(): string {\n const callerFile = callerOutsidePackage();\n const base = callerFile ? path.dirname(callerFile) : process.cwd();\n return path.join(base, DEFAULT_DIR_NAME);\n}\n\nfunction callerOutsidePackage(): string | undefined {\n const stack = new Error('stack probe').stack;\n if (!stack) return undefined;\n const lines = stack.split('\\n').slice(1);\n for (const line of lines) {\n const match = line.match(/\\((.*?):\\d+:\\d+\\)/) ?? line.match(/at (.*?):\\d+:\\d+/);\n const file = match?.[1];\n if (!file) continue;\n if (file.includes('node:')) continue;\n // Skip frames inside this package's own source/dist.\n if (file.includes(`${path.join('autotel-message-contract', 'src')}`)) continue;\n if (file.includes(`${path.join('autotel-message-contract', 'dist')}`)) continue;\n if (file.includes('node_modules')) continue;\n return file.startsWith('file://') ? fileUrlToPath(file) : file;\n }\n return undefined;\n}\n\nfunction fileUrlToPath(url: string): string {\n return decodeURIComponent(url.replace(/^file:\\/\\//, ''));\n}\n","/**\n * The message contract DSL.\n *\n * Every check starts from {@link messageContract} and reads as a sentence:\n *\n * ```ts\n * // Pin the serialized shape — fail when it drifts.\n * await messageContract()\n * .given(new OrderPlaced(orderId, 'Alice', placedAt))\n * .whenSerialized()\n * .thenContractIsUnchanged();\n *\n * // Prove a newer reader still reads what an older writer produced.\n * await messageContract()\n * .given(orderPlacedV1)\n * .whenDeserializedAs(OrderPlacedV2) // a Zod schema or parse fn\n * .thenBackwardCompatible((v2) => expect(v2.coupon).toBeUndefined());\n * ```\n *\n * A **snapshot check** confirms a message still serializes exactly as approved,\n * so nothing reading it downstream breaks. A **compatibility check** confirms an\n * older and a newer version can still read each other's data. Both are ordinary\n * unit tests — no broker, no registry, no running service.\n */\nimport { lineDiff } from './diff.js';\nimport { read, type Reader } from './reader.js';\nimport {\n defaultSerializer,\n type MessageSerializer,\n} from './serializer.js';\nimport {\n isUpdateMode,\n readSnapshot,\n type SnapshotLocation,\n writeSnapshot,\n} from './snapshot-storage.js';\n\n/** Thrown when a contract check fails. Message is pre-formatted for a test runner. */\nexport class ContractViolationError extends Error {\n override readonly name = 'ContractViolationError';\n constructor(message: string) {\n super(message);\n }\n}\n\nexport interface MessageContractOptions {\n /** Serializer producing the bytes you ship. Defaults to deterministic JSON. */\n serializer?: MessageSerializer<string>;\n /**\n * Where approved files live and what they are named. A bare string is used as\n * the logical name; an object gives full control over `dir`/`path`.\n */\n snapshot?: string | SnapshotLocation;\n /** Override update-mode detection (default reads env, e.g. AUTOTEL_CONTRACT_UPDATE=1). */\n update?: boolean;\n}\n\nconst SNAPSHOT_SOURCE = Symbol('autotel-message-contract.snapshot-source');\n\nexport interface ApprovedSnapshotSource {\n readonly [SNAPSHOT_SOURCE]: true;\n readonly location?: string | SnapshotLocation;\n}\n\n/**\n * Point a compatibility check at a previously approved snapshot instead of a\n * live in-memory message instance.\n */\nexport function approvedSnapshot(\n location?: string | SnapshotLocation,\n): ApprovedSnapshotSource {\n return {\n [SNAPSHOT_SOURCE]: true,\n location,\n };\n}\n\nfunction isApprovedSnapshotSource(value: unknown): value is ApprovedSnapshotSource {\n return (\n typeof value === 'object' &&\n value !== null &&\n SNAPSHOT_SOURCE in value &&\n (value as ApprovedSnapshotSource)[SNAPSHOT_SOURCE] === true\n );\n}\n\n/** Start a contract check. */\nexport function messageContract(options: MessageContractOptions = {}): GivenStep {\n return new GivenStep(options);\n}\n\nclass GivenStep {\n constructor(private readonly options: MessageContractOptions) {}\n\n /** The message under contract. */\n given<T>(message: T | ApprovedSnapshotSource): WhenStep<T> {\n return new WhenStep(message, this.options);\n }\n}\n\nclass WhenStep<T> {\n constructor(\n private readonly message: T | ApprovedSnapshotSource,\n private readonly options: MessageContractOptions,\n ) {}\n\n /** Serialize the message; the next step pins or inspects the result. */\n whenSerialized(): SnapshotStep {\n if (isApprovedSnapshotSource(this.message)) {\n throw new ContractViolationError(\n 'Cannot serialize an approved snapshot source. ' +\n 'Use .whenDeserializedAs(...) to run a compatibility check, or ' +\n 'pass a live message instance to .given(...).',\n );\n }\n const serializer = this.options.serializer ?? defaultSerializer;\n const serialized = serializer.serialize(this.message);\n return new SnapshotStep(serialized, serializer, this.options);\n }\n\n /**\n * Round-trip the message through a reader that models a *different version*\n * (a Standard Schema such as Zod/Valibot, or a parse function). The next step\n * asserts the versions stay compatible.\n */\n whenDeserializedAs<Output>(reader: Reader<Output>): CompatibilityStep<Output> {\n const serializer = this.options.serializer ?? defaultSerializer;\n return new CompatibilityStep(this.message, reader, serializer, this.options);\n }\n}\n\nclass SnapshotStep {\n constructor(\n private readonly serialized: string,\n private readonly serializer: MessageSerializer<string>,\n private readonly options: MessageContractOptions,\n ) {}\n\n /** The serialized bytes, for ad-hoc assertions outside the snapshot flow. */\n get output(): string {\n return this.serialized;\n }\n\n /**\n * Compare the serialized output against the approved snapshot. On first run\n * (or in update mode) it writes the approved file and passes; afterwards it\n * fails with a diff when the shape drifts.\n */\n thenContractIsUnchanged(snapshotName?: string): void {\n const location = this.resolveLocation(snapshotName);\n const existing = readSnapshot(location);\n const update = this.options.update ?? isUpdateMode();\n\n if (!existing.exists || update) {\n const path = writeSnapshot(location, this.serialized);\n if (!existing.exists) {\n // First run: record and pass, leaving the file to be reviewed/committed.\n return;\n }\n // Update mode: rewrite and pass even if it changed.\n void path;\n return;\n }\n\n if (existing.content !== this.serialized) {\n throw new ContractViolationError(\n `Message contract drifted from its approved snapshot.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` snapshot: ${existing.path}\\n\\n` +\n `${lineDiff(existing.content ?? '', this.serialized)}\\n\\n` +\n `If this change is intentional, re-run with AUTOTEL_CONTRACT_UPDATE=1 ` +\n `to update the approved file, then review and commit it.`,\n );\n }\n }\n\n private resolveLocation(snapshotName?: string): SnapshotLocation {\n if (snapshotName) return { name: snapshotName };\n const configured = this.options.snapshot;\n if (typeof configured === 'string') return { name: configured };\n if (configured) return configured;\n throw new ContractViolationError(\n `A snapshot name is required. Pass one to messageContract({ snapshot: 'OrderPlaced' }) ` +\n `or to thenContractIsUnchanged('OrderPlaced').`,\n );\n }\n}\n\nclass CompatibilityStep<Output> {\n constructor(\n private readonly source: unknown,\n private readonly reader: Reader<Output>,\n private readonly serializer: MessageSerializer<string>,\n private readonly options: MessageContractOptions,\n ) {}\n\n /**\n * The reader models a **newer** version; confirm it still reads what an older\n * writer produced (stored events, in-flight messages). Optionally assert on\n * the upgraded value — e.g. that a newly-added field defaults sensibly.\n */\n async thenBackwardCompatible(\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n return this.check('backward', assert);\n }\n\n /**\n * The reader models an **older** version; confirm a consumer that has not\n * upgraded yet still reads what the newer writer produces, so you can ship the\n * new shape before every reader has caught up.\n */\n async thenForwardCompatible(\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n return this.check('forward', assert);\n }\n\n private async check(\n direction: 'backward' | 'forward',\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n const serialized = this.resolveSourceSerialized();\n const deserializedSource = this.serializer.deserialize(serialized);\n const outcome = await read(this.reader, deserializedSource);\n\n if (!outcome.ok) {\n const writer = direction === 'backward' ? 'an older writer' : 'a newer writer';\n const readerLabel =\n direction === 'backward' ? 'the newer reader' : 'the older reader';\n throw new ContractViolationError(\n `Not ${direction}-compatible: ${readerLabel} rejected a message ${writer} produced.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` serialized: ${truncate(serialized)}\\n` +\n ` issues:\\n${outcome.issues.map((m) => ` - ${m}`).join('\\n')}`,\n );\n }\n\n this.assertStructuralCompatibility(\n deserializedSource,\n this.serializer.deserialize(this.serializer.serialize(outcome.value)),\n direction,\n serialized,\n );\n\n if (assert) await assert(outcome.value as Output);\n return outcome.value as Output;\n }\n\n private resolveSourceSerialized(): string {\n if (isApprovedSnapshotSource(this.source)) {\n const location = this.resolveSnapshotLocation(this.source.location);\n const existing = readSnapshot(location);\n if (!existing.exists || existing.content === undefined) {\n throw new ContractViolationError(\n `Cannot read approved snapshot for compatibility check.\\n` +\n ` snapshot: ${existing.path}\\n\\n` +\n `Record it first with .whenSerialized().thenContractIsUnchanged(), ` +\n `or point approvedSnapshot(...) at an existing file.`,\n );\n }\n return existing.content;\n }\n return this.serializer.serialize(this.source);\n }\n\n private resolveSnapshotLocation(\n location?: string | SnapshotLocation,\n ): SnapshotLocation {\n if (typeof location === 'string') return { name: location };\n if (location) return location;\n\n const configured = this.options.snapshot;\n if (typeof configured === 'string') return { name: configured };\n if (configured) return configured;\n\n throw new ContractViolationError(\n 'A snapshot location is required for approvedSnapshot(). ' +\n `Pass approvedSnapshot('OrderPlaced_v1') or configure messageContract({ snapshot: 'OrderPlaced_v1' }).`,\n );\n }\n\n private assertStructuralCompatibility(\n sourceValue: unknown,\n targetValue: unknown,\n direction: 'backward' | 'forward',\n serialized: string,\n ): void {\n const mismatches: string[] = [];\n compareSharedStructure(sourceValue, targetValue, '$', mismatches);\n\n if (mismatches.length === 0) return;\n\n throw new ContractViolationError(\n `Not ${direction}-compatible: shared fields changed meaning across versions.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` serialized: ${truncate(serialized)}\\n` +\n ` mismatches:\\n${mismatches.map((issue) => ` - ${issue}`).join('\\n')}`,\n );\n }\n}\n\nfunction truncate(value: string, max = 400): string {\n return value.length > max ? `${value.slice(0, max)}… (${value.length} chars)` : value;\n}\n\nfunction compareSharedStructure(\n sourceValue: unknown,\n targetValue: unknown,\n path: string,\n mismatches: string[],\n): void {\n if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {\n const sourceKeys = Object.keys(sourceValue);\n const targetKeys = Object.keys(targetValue);\n const sourceOnly = sourceKeys.filter((key) => !(key in targetValue));\n const targetOnly = targetKeys.filter((key) => !(key in sourceValue));\n\n if (sourceOnly.length > 0 && targetOnly.length > 0) {\n mismatches.push(\n `${path}: structural incompatibility ` +\n `[source-only: ${sourceOnly.join(', ')}, target-only: ${targetOnly.join(', ')}]`,\n );\n return;\n }\n\n for (const key of sourceKeys.filter((candidate) => candidate in targetValue).toSorted()) {\n compareSharedStructure(\n sourceValue[key],\n targetValue[key],\n joinPath(path, key),\n mismatches,\n );\n }\n return;\n }\n\n if (Array.isArray(sourceValue) && Array.isArray(targetValue)) {\n if (sourceValue.length !== targetValue.length) {\n mismatches.push(\n `${path}: array length differs (${sourceValue.length} vs ${targetValue.length})`,\n );\n return;\n }\n\n for (const [index, sourceItem] of sourceValue.entries()) {\n compareSharedStructure(sourceItem, targetValue[index], `${path}[${index}]`, mismatches);\n }\n return;\n }\n\n if (!deepEqual(sourceValue, targetValue)) {\n mismatches.push(\n `${path}: value differs (${formatValue(sourceValue)} vs ${formatValue(targetValue)})`,\n );\n }\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;\n const proto = Object.getPrototypeOf(value);\n return proto === Object.prototype || proto === null;\n}\n\nfunction deepEqual(left: unknown, right: unknown): boolean {\n if (Object.is(left, right)) return true;\n\n if (Array.isArray(left) && Array.isArray(right)) {\n return (\n left.length === right.length &&\n left.every((value, index) => deepEqual(value, right[index]))\n );\n }\n\n if (isPlainObject(left) && isPlainObject(right)) {\n const leftKeys = Object.keys(left);\n const rightKeys = Object.keys(right);\n return (\n leftKeys.length === rightKeys.length &&\n leftKeys.every((key) => key in right && deepEqual(left[key], right[key]))\n );\n }\n\n return false;\n}\n\nfunction formatValue(value: unknown): string {\n if (typeof value === 'string') return JSON.stringify(value);\n if (\n value === null ||\n typeof value === 'number' ||\n typeof value === 'boolean' ||\n value === undefined\n ) {\n return String(value);\n }\n return JSON.stringify(value);\n}\n\nfunction joinPath(path: string, key: string): string {\n return path === '$' ? `$.${key}` : `${path}.${key}`;\n}\n\nexport type {\n GivenStep,\n WhenStep,\n SnapshotStep,\n CompatibilityStep,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,SAAgB,SAAS,UAAkB,QAAwB;CACjE,MAAM,IAAI,SAAS,MAAM,IAAI;CAC7B,MAAM,IAAI,OAAO,MAAM,IAAI;CAC3B,MAAM,MAAM,yBAAyB,GAAG,CAAC;CAEzC,MAAM,MAAgB,CAAC;CACvB,IAAI,IAAI;CACR,IAAI,IAAI;CACR,KAAK,MAAM,CAAC,IAAI,OAAO,KAAK;EAC1B,OAAO,IAAI,IAAI,IAAI,KAAK,KAAK,EAAE,MAAM;EACrC,OAAO,IAAI,IAAI,IAAI,KAAK,KAAK,EAAE,MAAM;EACrC,IAAI,KAAK,KAAK,EAAE,MAAM;EACtB;CACF;CACA,OAAO,IAAI,EAAE,QAAQ,IAAI,KAAK,KAAK,EAAE,MAAM;CAC3C,OAAO,IAAI,EAAE,QAAQ,IAAI,KAAK,KAAK,EAAE,MAAM;CAE3C,OAAO,IAAI,KAAK,IAAI;AACtB;;AAGA,SAAS,yBACP,GACA,GACyB;CACzB,M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AAc,KAAK,GAAG;EAC/C,MAAM,WAAW,OAAO,KAAK,IAAI;EACjC,MAAM,YAAY,OAAO,KAAK,KAAK;EACnC,OACE,SAAS,WAAW,UAAU,UAC9B,SAAS,OAAO,QAAQ,OAAO,SAAS,UAAU,KAAK,MAAM,MAAM,IAAI,CAAC;CAE5E;CAEA,OAAO;AACT;AAEA,SAAS,YAAY,OAAwB;CAC3C,IAAI,OAAO,UAAU,UAAU,OAAO,KAAK,UAAU,KAAK;CAC1D,IACE,UAAU,QACV,OAAO,UAAU,YACjB,OAAO,UAAU,aACjB,UAAU,QAEV,OAAO,OAAO,KAAK;CAErB,OAAO,KAAK,UAAU,KAAK;AAC7B;AAEA,SAAS,SAAS,MAAc,KAAqB;CACnD,OAAO,SAAS,MAAM,KAAK,QAAQ,GAAG,KAAK,GAAG;AAChD"}
@@ -0,0 +1,173 @@
1
+ import { JsonSerializerOptions, MessageSerializer, defaultSerializer, jsonSerializer } from "./serializer.cjs";
2
+
3
+ //#region src/reader.d.ts
4
+ /**
5
+ * Readers — the "as this version" side of a compatibility check.
6
+ *
7
+ * In a JVM contract library you write `whenDeserializedAs(NewType.class)` and
8
+ * reflection does the rest. TypeScript types are erased at runtime, so there is
9
+ * no class to hand over. Instead you describe the *reader*: the thing that
10
+ * accepts a deserialized value and either produces a typed result or rejects
11
+ * it. Two shapes are accepted, in order of how most TS codebases already model
12
+ * a message version:
13
+ *
14
+ * 1. A **Standard Schema** (Zod ≥3.24, Valibot, ArkType, …) — anything exposing
15
+ * the `~standard` interface. This is the recommended form: the schema is the
16
+ * version, and it already lives next to your message type.
17
+ * 2. A plain **parse function** `(value) => T` that throws on incompatible input.
18
+ *
19
+ * A reader that accepts the value proves compatibility; a reader that throws or
20
+ * reports issues proves the versions have drifted apart.
21
+ */
22
+ /** The subset of the Standard Schema v1 interface we rely on. */
23
+ interface StandardSchemaLike<Output = unknown> {
24
+ readonly '~standard': {
25
+ readonly version: 1;
26
+ readonly vendor: string;
27
+ readonly validate: (value: unknown) => StandardResult<Output> | Promise<StandardResult<Output>>;
28
+ };
29
+ }
30
+ interface StandardResult<Output> {
31
+ value?: Output;
32
+ issues?: ReadonlyArray<{
33
+ readonly message: string;
34
+ readonly path?: unknown;
35
+ }>;
36
+ }
37
+ /** A bare parse function: returns the typed value or throws. */
38
+ type ParseFn<Output = unknown> = (value: unknown) => Output;
39
+ /** Either accepted reader form. */
40
+ type Reader<Output = unknown> = StandardSchemaLike<Output> | ParseFn<Output>;
41
+ interface ReadOutcome<Output = unknown> {
42
+ ok: boolean;
43
+ /** Present when `ok`. */
44
+ value?: Output;
45
+ /** Human-readable reasons the reader rejected the value. */
46
+ issues: string[];
47
+ }
48
+ /**
49
+ * Run a reader against a deserialized value. Never throws — a thrown parse
50
+ * error or reported issues become `{ ok: false, issues }` so the caller can
51
+ * build a single, legible failure message.
52
+ */
53
+ declare function read<Output>(reader: Reader<Output>, value: unknown): Promise<ReadOutcome<Output>>;
54
+ //#endregion
55
+ //#region src/snapshot-storage.d.ts
56
+ declare function isUpdateMode(env?: NodeJS.ProcessEnv): boolean;
57
+ interface SnapshotLocation {
58
+ /** Directory holding the approved file. */
59
+ dir?: string;
60
+ /** Logical name; the file becomes `<name>.approved.txt`. */
61
+ name: string;
62
+ /** Fully-qualified path; when set, `dir`/`name` are ignored for placement. */
63
+ path?: string;
64
+ }
65
+ /** Resolve the absolute file path for a snapshot. */
66
+ declare function resolveSnapshotPath(location: SnapshotLocation): string;
67
+ interface ReadSnapshotResult {
68
+ exists: boolean;
69
+ content?: string;
70
+ path: string;
71
+ }
72
+ declare function readSnapshot(location: SnapshotLocation): ReadSnapshotResult;
73
+ declare function writeSnapshot(location: SnapshotLocation, content: string): string;
74
+ //#endregion
75
+ //#region src/contract.d.ts
76
+ /** Thrown when a contract check fails. Message is pre-formatted for a test runner. */
77
+ declare class ContractViolationError extends Error {
78
+ readonly name = "ContractViolationError";
79
+ constructor(message: string);
80
+ }
81
+ interface MessageContractOptions {
82
+ /** Serializer producing the bytes you ship. Defaults to deterministic JSON. */
83
+ serializer?: MessageSerializer<string>;
84
+ /**
85
+ * Where approved files live and what they are named. A bare string is used as
86
+ * the logical name; an object gives full control over `dir`/`path`.
87
+ */
88
+ snapshot?: string | SnapshotLocation;
89
+ /** Override update-mode detection (default reads env, e.g. AUTOTEL_CONTRACT_UPDATE=1). */
90
+ update?: boolean;
91
+ }
92
+ declare const SNAPSHOT_SOURCE: unique symbol;
93
+ interface ApprovedSnapshotSource {
94
+ readonly [SNAPSHOT_SOURCE]: true;
95
+ readonly location?: string | SnapshotLocation;
96
+ }
97
+ /**
98
+ * Point a compatibility check at a previously approved snapshot instead of a
99
+ * live in-memory message instance.
100
+ */
101
+ declare function approvedSnapshot(location?: string | SnapshotLocation): ApprovedSnapshotSource;
102
+ /** Start a contract check. */
103
+ declare function messageContract(options?: MessageContractOptions): GivenStep;
104
+ declare class GivenStep {
105
+ private readonly options;
106
+ constructor(options: MessageContractOptions);
107
+ /** The message under contract. */
108
+ given<T>(message: T | ApprovedSnapshotSource): WhenStep<T>;
109
+ }
110
+ declare class WhenStep<T> {
111
+ private readonly message;
112
+ private readonly options;
113
+ constructor(message: T | ApprovedSnapshotSource, options: MessageContractOptions);
114
+ /** Serialize the message; the next step pins or inspects the result. */
115
+ whenSerialized(): SnapshotStep;
116
+ /**
117
+ * Round-trip the message through a reader that models a *different version*
118
+ * (a Standard Schema such as Zod/Valibot, or a parse function). The next step
119
+ * asserts the versions stay compatible.
120
+ */
121
+ whenDeserializedAs<Output>(reader: Reader<Output>): CompatibilityStep<Output>;
122
+ }
123
+ declare class SnapshotStep {
124
+ private readonly serialized;
125
+ private readonly serializer;
126
+ private readonly options;
127
+ constructor(serialized: string, serializer: MessageSerializer<string>, options: MessageContractOptions);
128
+ /** The serialized bytes, for ad-hoc assertions outside the snapshot flow. */
129
+ get output(): string;
130
+ /**
131
+ * Compare the serialized output against the approved snapshot. On first run
132
+ * (or in update mode) it writes the approved file and passes; afterwards it
133
+ * fails with a diff when the shape drifts.
134
+ */
135
+ thenContractIsUnchanged(snapshotName?: string): void;
136
+ private resolveLocation;
137
+ }
138
+ declare class CompatibilityStep<Output> {
139
+ private readonly source;
140
+ private readonly reader;
141
+ private readonly serializer;
142
+ private readonly options;
143
+ constructor(source: unknown, reader: Reader<Output>, serializer: MessageSerializer<string>, options: MessageContractOptions);
144
+ /**
145
+ * The reader models a **newer** version; confirm it still reads what an older
146
+ * writer produced (stored events, in-flight messages). Optionally assert on
147
+ * the upgraded value — e.g. that a newly-added field defaults sensibly.
148
+ */
149
+ thenBackwardCompatible(assert?: (value: Output) => void | Promise<void>): Promise<Output>;
150
+ /**
151
+ * The reader models an **older** version; confirm a consumer that has not
152
+ * upgraded yet still reads what the newer writer produces, so you can ship the
153
+ * new shape before every reader has caught up.
154
+ */
155
+ thenForwardCompatible(assert?: (value: Output) => void | Promise<void>): Promise<Output>;
156
+ private check;
157
+ private resolveSourceSerialized;
158
+ private resolveSnapshotLocation;
159
+ private assertStructuralCompatibility;
160
+ }
161
+ //#endregion
162
+ //#region src/diff.d.ts
163
+ /**
164
+ * Minimal line diff for snapshot failures. The goal is a message that points
165
+ * at *what moved* — a renamed field, a switched date format, a dropped value —
166
+ * not a full diff engine. Lines only in the approved file are marked `-`, lines
167
+ * only in the actual output are marked `+`, and a little surrounding context is
168
+ * kept so the change is readable in a terminal.
169
+ */
170
+ declare function lineDiff(approved: string, actual: string): string;
171
+ //#endregion
172
+ export { type ApprovedSnapshotSource, type CompatibilityStep, ContractViolationError, type GivenStep, type JsonSerializerOptions, type MessageContractOptions, type MessageSerializer, type ParseFn, type ReadOutcome, type ReadSnapshotResult, type Reader, type SnapshotLocation, type SnapshotStep, type StandardSchemaLike, type WhenStep, approvedSnapshot, defaultSerializer, isUpdateMode, jsonSerializer, lineDiff, messageContract, read, readSnapshot, resolveSnapshotPath, writeSnapshot };
173
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],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,KAAA,EAAO,MAAA,YAAkB,OAAA,SAClC,OAAA,CAAQ,MAAA;EAAA,QAIG,KAAA;EAAA,QA+BN,uBAAA;EAAA,QAiBA,uBAAA;EAAA,QAgBA,6BAAA;AAAA;;;;;;AFtQV;;;;iBGbgB,QAAA,CAAS,QAAA,UAAkB,MAAc"}
@@ -0,0 +1,173 @@
1
+ import { JsonSerializerOptions, MessageSerializer, defaultSerializer, jsonSerializer } from "./serializer.js";
2
+
3
+ //#region src/reader.d.ts
4
+ /**
5
+ * Readers — the "as this version" side of a compatibility check.
6
+ *
7
+ * In a JVM contract library you write `whenDeserializedAs(NewType.class)` and
8
+ * reflection does the rest. TypeScript types are erased at runtime, so there is
9
+ * no class to hand over. Instead you describe the *reader*: the thing that
10
+ * accepts a deserialized value and either produces a typed result or rejects
11
+ * it. Two shapes are accepted, in order of how most TS codebases already model
12
+ * a message version:
13
+ *
14
+ * 1. A **Standard Schema** (Zod ≥3.24, Valibot, ArkType, …) — anything exposing
15
+ * the `~standard` interface. This is the recommended form: the schema is the
16
+ * version, and it already lives next to your message type.
17
+ * 2. A plain **parse function** `(value) => T` that throws on incompatible input.
18
+ *
19
+ * A reader that accepts the value proves compatibility; a reader that throws or
20
+ * reports issues proves the versions have drifted apart.
21
+ */
22
+ /** The subset of the Standard Schema v1 interface we rely on. */
23
+ interface StandardSchemaLike<Output = unknown> {
24
+ readonly '~standard': {
25
+ readonly version: 1;
26
+ readonly vendor: string;
27
+ readonly validate: (value: unknown) => StandardResult<Output> | Promise<StandardResult<Output>>;
28
+ };
29
+ }
30
+ interface StandardResult<Output> {
31
+ value?: Output;
32
+ issues?: ReadonlyArray<{
33
+ readonly message: string;
34
+ readonly path?: unknown;
35
+ }>;
36
+ }
37
+ /** A bare parse function: returns the typed value or throws. */
38
+ type ParseFn<Output = unknown> = (value: unknown) => Output;
39
+ /** Either accepted reader form. */
40
+ type Reader<Output = unknown> = StandardSchemaLike<Output> | ParseFn<Output>;
41
+ interface ReadOutcome<Output = unknown> {
42
+ ok: boolean;
43
+ /** Present when `ok`. */
44
+ value?: Output;
45
+ /** Human-readable reasons the reader rejected the value. */
46
+ issues: string[];
47
+ }
48
+ /**
49
+ * Run a reader against a deserialized value. Never throws — a thrown parse
50
+ * error or reported issues become `{ ok: false, issues }` so the caller can
51
+ * build a single, legible failure message.
52
+ */
53
+ declare function read<Output>(reader: Reader<Output>, value: unknown): Promise<ReadOutcome<Output>>;
54
+ //#endregion
55
+ //#region src/snapshot-storage.d.ts
56
+ declare function isUpdateMode(env?: NodeJS.ProcessEnv): boolean;
57
+ interface SnapshotLocation {
58
+ /** Directory holding the approved file. */
59
+ dir?: string;
60
+ /** Logical name; the file becomes `<name>.approved.txt`. */
61
+ name: string;
62
+ /** Fully-qualified path; when set, `dir`/`name` are ignored for placement. */
63
+ path?: string;
64
+ }
65
+ /** Resolve the absolute file path for a snapshot. */
66
+ declare function resolveSnapshotPath(location: SnapshotLocation): string;
67
+ interface ReadSnapshotResult {
68
+ exists: boolean;
69
+ content?: string;
70
+ path: string;
71
+ }
72
+ declare function readSnapshot(location: SnapshotLocation): ReadSnapshotResult;
73
+ declare function writeSnapshot(location: SnapshotLocation, content: string): string;
74
+ //#endregion
75
+ //#region src/contract.d.ts
76
+ /** Thrown when a contract check fails. Message is pre-formatted for a test runner. */
77
+ declare class ContractViolationError extends Error {
78
+ readonly name = "ContractViolationError";
79
+ constructor(message: string);
80
+ }
81
+ interface MessageContractOptions {
82
+ /** Serializer producing the bytes you ship. Defaults to deterministic JSON. */
83
+ serializer?: MessageSerializer<string>;
84
+ /**
85
+ * Where approved files live and what they are named. A bare string is used as
86
+ * the logical name; an object gives full control over `dir`/`path`.
87
+ */
88
+ snapshot?: string | SnapshotLocation;
89
+ /** Override update-mode detection (default reads env, e.g. AUTOTEL_CONTRACT_UPDATE=1). */
90
+ update?: boolean;
91
+ }
92
+ declare const SNAPSHOT_SOURCE: unique symbol;
93
+ interface ApprovedSnapshotSource {
94
+ readonly [SNAPSHOT_SOURCE]: true;
95
+ readonly location?: string | SnapshotLocation;
96
+ }
97
+ /**
98
+ * Point a compatibility check at a previously approved snapshot instead of a
99
+ * live in-memory message instance.
100
+ */
101
+ declare function approvedSnapshot(location?: string | SnapshotLocation): ApprovedSnapshotSource;
102
+ /** Start a contract check. */
103
+ declare function messageContract(options?: MessageContractOptions): GivenStep;
104
+ declare class GivenStep {
105
+ private readonly options;
106
+ constructor(options: MessageContractOptions);
107
+ /** The message under contract. */
108
+ given<T>(message: T | ApprovedSnapshotSource): WhenStep<T>;
109
+ }
110
+ declare class WhenStep<T> {
111
+ private readonly message;
112
+ private readonly options;
113
+ constructor(message: T | ApprovedSnapshotSource, options: MessageContractOptions);
114
+ /** Serialize the message; the next step pins or inspects the result. */
115
+ whenSerialized(): SnapshotStep;
116
+ /**
117
+ * Round-trip the message through a reader that models a *different version*
118
+ * (a Standard Schema such as Zod/Valibot, or a parse function). The next step
119
+ * asserts the versions stay compatible.
120
+ */
121
+ whenDeserializedAs<Output>(reader: Reader<Output>): CompatibilityStep<Output>;
122
+ }
123
+ declare class SnapshotStep {
124
+ private readonly serialized;
125
+ private readonly serializer;
126
+ private readonly options;
127
+ constructor(serialized: string, serializer: MessageSerializer<string>, options: MessageContractOptions);
128
+ /** The serialized bytes, for ad-hoc assertions outside the snapshot flow. */
129
+ get output(): string;
130
+ /**
131
+ * Compare the serialized output against the approved snapshot. On first run
132
+ * (or in update mode) it writes the approved file and passes; afterwards it
133
+ * fails with a diff when the shape drifts.
134
+ */
135
+ thenContractIsUnchanged(snapshotName?: string): void;
136
+ private resolveLocation;
137
+ }
138
+ declare class CompatibilityStep<Output> {
139
+ private readonly source;
140
+ private readonly reader;
141
+ private readonly serializer;
142
+ private readonly options;
143
+ constructor(source: unknown, reader: Reader<Output>, serializer: MessageSerializer<string>, options: MessageContractOptions);
144
+ /**
145
+ * The reader models a **newer** version; confirm it still reads what an older
146
+ * writer produced (stored events, in-flight messages). Optionally assert on
147
+ * the upgraded value — e.g. that a newly-added field defaults sensibly.
148
+ */
149
+ thenBackwardCompatible(assert?: (value: Output) => void | Promise<void>): Promise<Output>;
150
+ /**
151
+ * The reader models an **older** version; confirm a consumer that has not
152
+ * upgraded yet still reads what the newer writer produces, so you can ship the
153
+ * new shape before every reader has caught up.
154
+ */
155
+ thenForwardCompatible(assert?: (value: Output) => void | Promise<void>): Promise<Output>;
156
+ private check;
157
+ private resolveSourceSerialized;
158
+ private resolveSnapshotLocation;
159
+ private assertStructuralCompatibility;
160
+ }
161
+ //#endregion
162
+ //#region src/diff.d.ts
163
+ /**
164
+ * Minimal line diff for snapshot failures. The goal is a message that points
165
+ * at *what moved* — a renamed field, a switched date format, a dropped value —
166
+ * not a full diff engine. Lines only in the approved file are marked `-`, lines
167
+ * only in the actual output are marked `+`, and a little surrounding context is
168
+ * kept so the change is readable in a terminal.
169
+ */
170
+ declare function lineDiff(approved: string, actual: string): string;
171
+ //#endregion
172
+ export { type ApprovedSnapshotSource, type CompatibilityStep, ContractViolationError, type GivenStep, type JsonSerializerOptions, type MessageContractOptions, type MessageSerializer, type ParseFn, type ReadOutcome, type ReadSnapshotResult, type Reader, type SnapshotLocation, type SnapshotStep, type StandardSchemaLike, type WhenStep, approvedSnapshot, defaultSerializer, isUpdateMode, jsonSerializer, lineDiff, messageContract, read, readSnapshot, resolveSnapshotPath, writeSnapshot };
173
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/reader.ts","../src/snapshot-storage.ts","../src/contract.ts","../src/diff.ts"],"mappings":";;;;;;AAoBA;;;;;;;;;;;;;;;;UAAiB,kBAAA;EAAA,SACN,WAAA;IAAA,SACE,OAAA;IAAA,SACA,MAAA;IAAA,SACA,QAAA,GAAW,KAAA,cAChB,cAAA,CAAe,MAAA,IACf,OAAA,CAAQ,cAAA,CAAe,MAAA;EAAA;AAAA;AAAA,UAIrB,cAAA;EACR,KAAA,GAAQ,MAAA;EACR,MAAA,GAAS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