Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(engine): attribute every adapter file to an engine in the parity gate
Yudhi Armyndharisaea63d37529a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f8a0ea099..35c2ef2f7 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- `check:sdk-routes` scanned the JavaScript client for backtick-delimited paths only, while its PHP and Python rules already accepted every quote style, so the nine routes that client writes as single-quoted strings were never compared to the contract — `/api/health/ready` among them, which is also the container healthcheck and Kubernetes readiness path. Renaming one of those server-side passed every gate and the JS suite, and the published client would have 404'd with CI green.- The JavaScript, Python, Go and Java SDKs omitted `contact`, `call` and `ephemeralDuration` from their chat-history message type, so a typed client had to cast to read three fields the endpoint returns; all four now mirror the engine payload.- `POST /api/infra/import-data` bound its body to an inline type, which erases at runtime, so on the replace-all restore the global ValidationPipe's `whitelist` and `forbidNonWhitelisted` never ran: a body carrying no `tables` failed as a 500 from inside the import, and a misspelled key was accepted in silence. It now takes a DTO — a missing `tables` answers 400 naming the field, an unknown key is refused, and `force`/`stopOrphans` accept only a real boolean or an exact `'true'`/`'false'`, so an ambiguous spelling cannot open the orphan-engine escape.+- The engine parity gate took an adapter file's engine from its filename prefix and credited the rest to whatsapp-web.js, so a `501` added to one of the five unprefixed modules would be blamed on the wrong engine — and the invariant would then demand that the engine which really refuses be marked `supported`. Those files are now attributed explicitly, and one that is neither prefixed nor attributed fails the spec by name.### Documentationdiff --git a/src/engine/engine-parity.spec.ts b/src/engine/engine-parity.spec.tsindex 589d95a3a..c215375bd 100644--- a/src/engine/engine-parity.spec.ts+++ b/src/engine/engine-parity.spec.ts@@ -72,18 +72,54 @@ const ADAPTERS: ReadonlyArray<[AdapterKey, AdapterCtor]> = [function readDelegateThrows(): Record<string, Set<string>> {const dir = join(__dirname, 'adapters');const registry: Record<string, Set<string>> = { wwjs: new Set(), baileys: new Set() };- for (const file of readdirSync(dir)) {- if (!file.endsWith('.ts') || file.endsWith('.spec.ts')) continue;- const engine = file.startsWith('baileys') ? 'baileys' : 'wwjs';+ for (const file of adapterFiles()) {const src = readFileSync(join(dir, file), 'utf8');for (const m of src.matchAll(/(?:new EngineNotSupportedError|this\.unsupported)\(\s*'([a-zA-Z][a-zA-Z0-9]*)'/g)) {- registry[engine].add(m[1]);+ for (const engine of enginesForFile(file)) registry[engine].add(m[1]);}}return registry;}+/** Adapter modules, spec files excluded — the same set both the scan and its guard below read. */+function adapterFiles(): string[] {+ return readdirSync(join(__dirname, 'adapters')).filter(f => f.endsWith('.ts') && !f.endsWith('.spec.ts'));+}++/** The engine a filename names, or undefined when it carries no engine prefix at all. */+function prefixEngine(file: string): AdapterKey | undefined {+ if (file.startsWith('baileys')) return 'baileys';+ if (file.startsWith('wwebjs-') || file.startsWith('whatsapp-web-js')) return 'wwjs';+ return undefined;+}++/**+ * Adapter modules whose filename names no engine, mapped to the engines that actually import them.+ * Read off the import graph, not the name: safe-link-preview.ts is reached only from+ * baileys-messaging.ts, so the `else wwjs` default this replaces already credited it to the wrong+ * engine, and inbound-media-cap/message-mapper/vcard are reached from both.+ *+ * None of these files throws today, so the registry is byte-identical either way. The list exists+ * for the DIRECTION the old default failed in: a misattributed throw does not merely go unseen, it+ * makes `liveThrows` report false for the engine that really refuses, and the invariant below then+ * demands the matrix mark that cell `supported` to stay green. A file added here without an+ * engine — or added to `adapters/` under no prefix at all — is a red test naming itself instead.+ */+const UNPREFIXED_FILE_ENGINES: Record<string, readonly AdapterKey[]> = {+ 'chromium-profile-hygiene.ts': ['wwjs'],+ 'inbound-media-cap.ts': ['wwjs', 'baileys'],+ 'message-mapper.ts': ['wwjs', 'baileys'],+ 'safe-link-preview.ts': ['baileys'],+ 'vcard.ts': ['wwjs', 'baileys'],+};++function enginesForFile(file: string): readonly AdapterKey[] {+ const prefix = prefixEngine(file);+ return prefix ? [prefix] : (UNPREFIXED_FILE_ENGINES[file] ?? []);+}+const DELEGATE_THROWS = readDelegateThrows();+const UNPREFIXED_FILES = adapterFiles().filter(f => prefixEngine(f) === undefined);function liveThrows(adapter: AdapterCtor, method: string, engine: string): boolean {if (DELEGATE_THROWS[engine].has(method)) return true;@@ -108,6 +144,15 @@ describe('engine capability matrix — drift invariants', () => {expect(body).not.toMatch(UNSUPPORTED_RE);});+ // Guard the guard, in both directions. The right-hand side is the five files that carry no engine+ // prefix today, so a scan that stopped seeing the directory would report an empty left side and+ // fail here rather than pass vacuously. A NEW unprefixed adapter file appears on the left and names+ // itself — attribute it in UNPREFIXED_FILE_ENGINES or give it an engine prefix — and an entry left+ // behind by a rename or deletion is caught the same way from the other side.+ it('every adapter file is attributed to an engine', () => {+ expect([...UNPREFIXED_FILES].sort()).toEqual(Object.keys(UNPREFIXED_FILE_ENGINES).sort());+ });+it('matrix keys exactly match the interface methods (no missing, no stale)', () => {const missing = methods.filter(m => !(m in ENGINE_CAPABILITY_MATRIX));const stale = matrixKeys.filter(k => !methods.includes(k));