/** * src/connectors/conformance.ts * ============================== * P061 DoD: "two heterogeneous connectors pass the same conformance suite." * * This module IS that suite. It is a plain async function returning a report * — not a vitest file — so it can be run three ways without duplication: * 1. from tests (tests/p0/connector-conformance.test.ts runs it per connector), * 2. from an operator script against a real configured source, * 3. from a future capability route as a pre-activation check. * * Each check is a named assertion about the CONTRACT, not about any one * connector's data. A connector that cannot serve a check honestly (e.g. it * has no partitions under the given config) FAILS it — there is no "skip" * verdict, because a skip that reads as a pass is how a conformance suite * becomes decoration. */ import type { BackfillRequest, Connector, ConnectorConfig, ConnectorContext, } from "./connector"; export interface ConformanceCheck { name: string; passed: boolean; detail: string; } export interface ConformanceReport { connector_id: string; connector_version: string; passed: boolean; checks: ConformanceCheck[]; } export interface ConformanceFixture { /** A config the connector must accept. */ validConfig: ConnectorConfig; /** A config the connector must REJECT in validate() without doing IO. */ invalidConfig: ConnectorConfig; ctx: ConnectorContext; /** * A bounded backfill range that overlaps data the connector can serve. * `from`/`to` are connector-defined (documented per connector). */ backfill: Omit & { partition_key?: string }; /** Total number of records the fixture's first partition holds, so paging can be checked exactly. */ expectedRecordCount: number; } async function check( checks: ConformanceCheck[], name: string, fn: () => Promise | string, ): Promise { try { const detail = await fn(); checks.push({ name, passed: true, detail }); } catch (err) { checks.push({ name, passed: false, detail: err instanceof Error ? err.message : String(err), }); } } function assert(condition: boolean, message: string): void { if (!condition) throw new Error(message); } /** * Run the full conformance suite against one connector instance. * The connector is left disconnected on return, pass or fail. */ export async function runConnectorConformance( connector: Connector, fixture: ConformanceFixture, ): Promise { const checks: ConformanceCheck[] = []; await check(checks, "identity: non-empty id and version", () => { assert(connector.connector_id.length > 0, "connector_id is empty"); assert(connector.connector_version.length > 0, "connector_version is empty"); return `${connector.connector_id}@${connector.connector_version}`; }); await check(checks, "validate: accepts a valid config without IO", () => { const res = connector.validate(fixture.validConfig); assert(res.valid, `valid config rejected: ${res.errors.join("; ")}`); assert(res.errors.length === 0, "valid config produced errors"); return "valid config accepted"; }); await check(checks, "validate: rejects an invalid config and says why", () => { const res = connector.validate(fixture.invalidConfig); assert(!res.valid, "invalid config was accepted (fail-open)"); assert(res.errors.length > 0, "rejection carried no reason"); return `rejected with ${res.errors.length} reason(s)`; }); await check(checks, "operations before connect() fail closed", async () => { let threw = false; try { await connector.read({ partition_key: "anything", limit: 1 }); } catch { threw = true; } assert(threw, "read() succeeded without a session"); return "read() refused without a session"; }); await check(checks, "connect: idempotent", async () => { await connector.connect(fixture.validConfig, fixture.ctx); await connector.connect(fixture.validConfig, fixture.ctx); return "connect() called twice without error"; }); let partitionKey = fixture.backfill.partition_key ?? ""; await check(checks, "discover: reports at least one partition with a schema", async () => { const discovery = await connector.discover(); assert(discovery.partitions.length > 0, "no partitions discovered"); assert(discovery.modes.length > 0, "no ingestion modes declared"); const first = discovery.partitions[0]; assert(first.partition_key.length > 0, "partition_key is empty"); assert( Object.keys(first.schema).length > 0, `partition "${first.partition_key}" reported an empty schema`, ); if (!partitionKey) partitionKey = first.partition_key; return `${discovery.partitions.length} partition(s), modes: ${discovery.modes.join(",")}`; }); await check(checks, "health: answers with a status instead of throwing", async () => { const health = await connector.health(); assert( ["up", "degraded", "down"].includes(health.status), `unexpected health status "${health.status}"`, ); return `status=${health.status}`; }); await check(checks, "read: honours the limit and advances the cursor", async () => { const first = await connector.read({ partition_key: partitionKey, limit: 1 }); assert(first.records.length <= 1, `limit 1 returned ${first.records.length} records`); assert(first.records.length === 1, "limit 1 returned nothing — fixture has no data"); assert(first.next_cursor !== undefined, "no cursor returned after a non-empty read"); const second = await connector.read({ partition_key: partitionKey, cursor: first.next_cursor, limit: 1, }); if (second.records.length > 0) { assert( second.records[0].record_key !== first.records[0].record_key, "reading from the returned cursor re-delivered the same record (cursor is not exclusive)", ); } return `cursor advanced past ${first.records[0].record_key}`; }); await check(checks, "read: paging covers every record exactly once", async () => { const seen: string[] = []; let cursor: string | undefined; for (let page = 0; page < 100; page++) { const res = await connector.read({ partition_key: partitionKey, cursor, limit: 2 }); for (const r of res.records) seen.push(r.record_key); cursor = res.next_cursor; if (res.exhausted || res.records.length === 0) break; } assert( seen.length === fixture.expectedRecordCount, `paged ${seen.length} records, fixture declares ${fixture.expectedRecordCount}`, ); assert(new Set(seen).size === seen.length, "paging delivered a duplicate record_key"); return `${seen.length} records, no duplicates`; }); await check(checks, "read: an exhausted partition reports exhausted", async () => { const everything = await connector.read({ partition_key: partitionKey, limit: 1000 }); const past = await connector.read({ partition_key: partitionKey, cursor: everything.next_cursor, limit: 1000, }); assert(past.records.length === 0, "reading past the end returned records"); assert(past.exhausted, "reading past the end did not report exhausted"); return "end of partition reported honestly"; }); await check(checks, "checkpoint: reflects the session position", async () => { const cp = await connector.checkpoint(partitionKey); assert(cp.partition_key === partitionKey, "checkpoint returned a different partition"); return `cursor=${cp.cursor ?? ""}`; }); await check(checks, "resume: continues from a committed checkpoint without re-delivery", async () => { const firstPage = await connector.read({ partition_key: partitionKey, limit: 2 }); assert(firstPage.records.length > 0, "fixture partition is empty"); const committed = { source_id: fixture.ctx.source_id, tenant_id: fixture.ctx.tenant_id, partition_key: partitionKey, cursor: firstPage.next_cursor, sequence: firstPage.records.length, records_committed: firstPage.records.length, committed_at: new Date().toISOString(), }; const resumed = await connector.resume(committed, 1000); const firstKeys = new Set(firstPage.records.map((r) => r.record_key)); for (const r of resumed.records) { assert( !firstKeys.has(r.record_key), `resume re-delivered already-committed record "${r.record_key}"`, ); } assert( firstPage.records.length + resumed.records.length === fixture.expectedRecordCount, `resume covered ${firstPage.records.length + resumed.records.length} of ${fixture.expectedRecordCount} records — a gap`, ); return `resumed ${resumed.records.length} remaining records with no gap and no overlap`; }); await check(checks, "backfill: serves a bounded range", async () => { const res = await connector.backfill({ partition_key: partitionKey, from: fixture.backfill.from, to: fixture.backfill.to, limit: 1000, }); assert(res.records.length > 0, "bounded backfill range returned nothing"); return `${res.records.length} record(s) in range`; }); await check(checks, "backfill: refuses an unbounded range", async () => { let threw = false; try { await connector.backfill({ partition_key: partitionKey, from: fixture.backfill.from, to: "", limit: 1000, }); } catch { threw = true; } assert(threw, "an unbounded backfill was accepted"); return "unbounded backfill refused"; }); await check(checks, "disconnect: idempotent and safe", async () => { await connector.disconnect(); await connector.disconnect(); return "disconnect() called twice without error"; }); await check(checks, "operations after disconnect() fail closed", async () => { let threw = false; try { await connector.read({ partition_key: partitionKey, limit: 1 }); } catch { threw = true; } assert(threw, "read() succeeded after disconnect()"); return "read() refused after disconnect()"; }); return { connector_id: connector.connector_id, connector_version: connector.connector_version, passed: checks.every((c) => c.passed), checks, }; }