Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(engine): refuse an unsupported-throw in a shared adapter module
Yudhi Armyndharis6dac63769c52
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 1ad376149..d54735452 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -23,7 +23,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 instead, naming the field outside production, where field-level detail is suppressed unless `VALIDATION_ERROR_DETAIL=true`; 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 export file still posts back verbatim: the five metadata fields it wraps `tables` in are accepted and ignored, and a spec probes every field the export publishes so a new one cannot start failing the restore.-- 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.+- 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 four 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. The three modules both adapters import are marked shared rather than attributed, because no attribution is correct for them: crediting both engines demands `not-available` from the one that never refuses, crediting neither demands `supported` from the one that does. A refusal added to a shared module is now refused by name, pointing at the engine delegate it belongs in.- Replying with an attachment in the dashboard composer silently dropped the quote: the media branch never carried the quoted id, the optimistic bubble's metadata took the same either/or, and the reply banner was cleared regardless — so it looked exactly like a quoted reply had been sent. Both payloads now carry the quote, and the two builders moved to a tested utility module.### Documentationdiff --git a/src/engine/engine-parity.spec.ts b/src/engine/engine-parity.spec.tsindex c215375bd..b601eb29a 100644--- a/src/engine/engine-parity.spec.ts+++ b/src/engine/engine-parity.spec.ts@@ -69,13 +69,13 @@ const ADAPTERS: ReadonlyArray<[AdapterKey, AdapterCtor]> = [* Bucketed by filename because that is what identifies the engine: `baileys*` is Baileys, the wwjs* adapter and its `wwebjs-*` delegates are wwjs.*/+const THROW_SITE_RE = /(?:new EngineNotSupportedError|this\.unsupported)\(\s*'([a-zA-Z][a-zA-Z0-9]*)'/;+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 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)) {- for (const engine of enginesForFile(file)) registry[engine].add(m[1]);+ for (const method of throwsIn(file)) {+ for (const engine of enginesForFile(file)) registry[engine].add(method);}}return registry;@@ -94,28 +94,47 @@ function prefixEngine(file: string): AdapterKey | 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.+ * Adapter modules whose filename names no engine, read off the import graph rather than 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; `chromium-profile-hygiene.ts` is reached only+ * from the wwjs adapter, so for that one the old default happened to be right.+ *+ * `'shared'` marks a module BOTH adapters import. It is not an attribution — it is the statement+ * that no correct attribution exists. Crediting such a file to both engines would make the invariant+ * demand `not-available` on the engine that never refuses; crediting it to neither would make the+ * invariant demand `supported` on the engine that does. Both directions press a false cell into the+ * matrix, which is the failure this whole scan exists to prevent, so a throw in a shared module is+ * refused outright by `shared modules carry no unsupported-throw` below.*- * 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.+ * None of these files throws today, so the registry is byte-identical to the prefix-only scan.*/-const UNPREFIXED_FILE_ENGINES: Record<string, readonly AdapterKey[]> = {+const SHARED = 'shared' as const;+const UNPREFIXED_FILE_ENGINES: Record<string, readonly AdapterKey[] | typeof SHARED> = {'chromium-profile-hygiene.ts': ['wwjs'],- 'inbound-media-cap.ts': ['wwjs', 'baileys'],- 'message-mapper.ts': ['wwjs', 'baileys'],+ 'inbound-media-cap.ts': SHARED,+ 'message-mapper.ts': SHARED,'safe-link-preview.ts': ['baileys'],- 'vcard.ts': ['wwjs', 'baileys'],+ 'vcard.ts': SHARED,};function enginesForFile(file: string): readonly AdapterKey[] {const prefix = prefixEngine(file);- return prefix ? [prefix] : (UNPREFIXED_FILE_ENGINES[file] ?? []);+ if (prefix) return [prefix];+ const declared = UNPREFIXED_FILE_ENGINES[file];+ return declared === undefined || declared === SHARED ? [] : declared;+}++/** Modules both adapters import, where a refusal cannot be attributed to one engine. */+const SHARED_FILES = Object.entries(UNPREFIXED_FILE_ENGINES)+ .filter(([, engines]) => engines === SHARED)+ .map(([file]) => file);++/** Method names an adapter file refuses by literal, in file order. */+function throwsIn(file: string): string[] {+ const src = readFileSync(join(__dirname, 'adapters', file), 'utf8');+ // A fresh regex per call: THROW_SITE_RE carries /g, and a shared /g regex keeps lastIndex between+ // calls, so reusing the instance would silently skip matches on every other file.+ return [...src.matchAll(new RegExp(THROW_SITE_RE.source, 'g'))].map(m => m[1]);}const DELEGATE_THROWS = readDelegateThrows();@@ -153,6 +172,20 @@ describe('engine capability matrix — drift invariants', () => {expect([...UNPREFIXED_FILES].sort()).toEqual(Object.keys(UNPREFIXED_FILE_ENGINES).sort());});+ // The other half of the same problem. Attribution fixes files the prefix rule could not name; this+ // fixes files that CANNOT be named, because both adapters import them. There is no safe default:+ // credit a shared module's refusal to both engines and the invariant demands `not-available` from+ // the engine that never refuses; credit it to neither and it demands `supported` from the engine+ // that does. Either way a false cell gets pressed into the matrix — the exact outcome this scan+ // exists to prevent — so the throw is refused where it stands, with the fix named.+ it('shared modules carry no unsupported-throw', () => {+ expect(SHARED_FILES.length).toBeGreaterThanOrEqual(3); // non-vacuous: the shared set was read+ const offenders = SHARED_FILES.filter(file => throwsIn(file).length > 0);+ // Fix: move the throw into that engine's own delegate (`baileys-*.ts` / `wwebjs-*.ts`) and call+ // the shared helper from there, so the refusal carries an engine and this scan can see it.+ expect(offenders).toEqual([]);+ });+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));