Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(engine): make the parity gate see delegated unsupported-throws
Yudhi Armyndharisa0f4f13fc9a1
diff --git a/src/engine/engine-parity.spec.ts b/src/engine/engine-parity.spec.tsindex 3744f5640..639ddfc50 100644--- a/src/engine/engine-parity.spec.ts+++ b/src/engine/engine-parity.spec.ts@@ -1,4 +1,4 @@-import { readFileSync } from 'node:fs';+import { readdirSync, readFileSync } from 'node:fs';import { join } from 'node:path';import { BaileysAdapter } from './adapters/baileys.adapter';import { WhatsAppWebJsAdapter } from './adapters/whatsapp-web-js.adapter';@@ -40,7 +40,37 @@ const ADAPTERS: ReadonlyArray<[AdapterKey, AdapterCtor]> = [['baileys', BaileysAdapter as unknown as AdapterCtor],];-function liveThrows(adapter: AdapterCtor, method: string): boolean {+/**+ * Unsupported-throws that live in DELEGATE modules rather than in the adapter prototype.+ *+ * The prototype scan reads a method's own body text, so once an adapter method forwards to a+ * delegate its `EngineNotSupportedError` becomes invisible and BOTH invariants stop applying to it:+ * a genuinely unsupported method could be marked `supported` and the gate would say nothing. That is+ * not hypothetical — every wwjs unsupported-throw except a handful now lives in a `wwebjs-*` module.+ *+ * Every throw site names its own method as a string literal, so the registry is derived from those+ * literals instead of by following delegation, which would mean resolving call graphs in a spec.+ * Bucketed by filename because that is what identifies the engine: `baileys*` is Baileys, the wwjs+ * adapter and its `wwebjs-*` delegates are wwjs.+ */+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';+ 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]);+ }+ }+ return registry;+}++const DELEGATE_THROWS = readDelegateThrows();++function liveThrows(adapter: AdapterCtor, method: string, engine: string): boolean {+ if (DELEGATE_THROWS[engine].has(method)) return true;const fn = adapter.prototype[method];if (typeof fn !== 'function') return true; // missing method = effectively unavailablereturn UNSUPPORTED_RE.test(String(fn));@@ -50,6 +80,18 @@ describe('engine capability matrix — drift invariants', () => {const methods = readInterfaceMethods();const matrixKeys = Object.keys(ENGINE_CAPABILITY_MATRIX).sort();+ // Guard against a pattern that silently stops matching: an empty registry would make the widened+ // scan collapse back to the prototype-only one without a single test turning red.+ it('the delegate throw registry actually found the delegated throws', () => {+ expect(DELEGATE_THROWS.wwjs.size).toBeGreaterThanOrEqual(5);+ // Known-positive: getCatalog's throw lives in wwebjs-catalog.ts, not in the adapter prototype.+ expect(DELEGATE_THROWS.wwjs.has('getCatalog')).toBe(true);+ // Indexed rather than dotted so the unbound-method rule does not fire: this reads the body TEXT,+ // it never calls the method.+ const body = String((WhatsAppWebJsAdapter.prototype as unknown as Record<string, unknown>)['getCatalog']);+ expect(body).not.toMatch(UNSUPPORTED_RE);+ });+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));@@ -59,7 +101,7 @@ describe('engine capability matrix — drift invariants', () => {it.each(methods)('%s: throws ⇒ not-available, supported ⇒ not-throws', method => {const entry = ENGINE_CAPABILITY_MATRIX[method];for (const [adapter, ctor] of ADAPTERS) {- const throws = liveThrows(ctor, method);+ const throws = liveThrows(ctor, method, adapter);const status = entry[adapter].status;if (throws) {expect({ method, adapter, status }).toEqual({ method, adapter, status: 'not-available' });