Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(dashboard): lock the api client wiring the refusal predicate depends on
Yudhi Armyndharis5bf2032f995e
diff --git a/dashboard/src/hooks/useDataBackup.ts b/dashboard/src/hooks/useDataBackup.tsindex bc7dcdf1e..1156c15c0 100644--- a/dashboard/src/hooks/useDataBackup.ts+++ b/dashboard/src/hooks/useDataBackup.ts@@ -50,8 +50,8 @@ export function useDataBackup(): DataBackup {// which stops them inside the request, so that one is offered as a confirm. Every other 409 leaves// nothing to decide (another restore in flight, another transaction holding the connection), and// retrying one with stopOrphans would run a second destructive replace the operator never asked- // for. force=true is deliberately not offered (the api- // client does not even send it): it leaves the engines running until a restart.+ // for. force=true is deliberately not offered (the api client does not even send it): it leaves+ // the engines running until a restart.const runImport = async (tables: Record<string, unknown[]>, stopOrphans = false): Promise<void> => {try {const res = await infraApi.importData(tables, stopOrphans ? { stopOrphans: true } : undefined);diff --git a/dashboard/src/services/api.ts b/dashboard/src/services/api.tsindex 872e7e516..0a43396d9 100644--- a/dashboard/src/services/api.ts+++ b/dashboard/src/services/api.ts@@ -1078,9 +1078,8 @@ export const infraApi = {// message lists them) is the only one the caller retries with stopOrphans=true to stop those// engines inside the request. IMPORT_ALREADY_RUNNING (another restore is in flight) and// IMPORT_NESTED_TRANSACTION (another transaction holds the connection) leave nothing to retry.- // force is- // deliberately NOT exposed: it leaves the engines writing into the restored tables until a- // restart — the window stopOrphans exists to close.+ // force is deliberately NOT exposed: it leaves the engines writing into the restored tables until+ // a restart — the window stopOrphans exists to close.importData: (tables: Record<string, unknown[]>, options?: { stopOrphans?: boolean }) =>request<{imported: boolean;diff --git a/dashboard/src/utils/importRefusal.test.ts b/dashboard/src/utils/importRefusal.test.tsindex f1abfe1c4..a5d4381ec 100644--- a/dashboard/src/utils/importRefusal.test.ts+++ b/dashboard/src/utils/importRefusal.test.ts@@ -1,5 +1,6 @@import { test } from 'node:test';import assert from 'node:assert/strict';+import { readFileSync } from 'node:fs';import { shouldOfferStopOrphansRetry } from './importRefusal.ts';// The retry this predicate gates is DESTRUCTIVE: it re-POSTs the replace-all with stopOrphans=true,@@ -45,3 +46,19 @@ test('an unknown future 409 code does not get the destructive retry', () => {test('only the 409 status can offer it, whatever the code says', () => {assert.equal(shouldOfferStopOrphansRetry(500, 'IMPORT_WOULD_ORPHAN_ENGINES', false), false);});++// The assertion below guards the WIRING, which is this design's single point of failure. Matching the+// code positively means "no code" withholds the confirm — so if the api client ever stops copying the+// body's code onto the Error, every 409 arrives code-less, the predicate returns false for all of+// them, and the stop-orphans confirm becomes unreachable. It is the only control in the dashboard+// that can send stopOrphans, and nothing else in the suite would notice its loss: every test above+// hands the predicate a code directly.+const read = (path: string): string => readFileSync(new URL(path, import.meta.url), 'utf8');++test('the api client carries the machine code onto the error this predicate reads', () => {+ assert.match(+ read('../services/api.ts'),+ /err\.code = error\.code/,+ 'api.ts must copy the response body `code` onto the thrown Error — update this guard, do not delete it',+ );+});diff --git a/dashboard/src/utils/importRefusal.ts b/dashboard/src/utils/importRefusal.tsindex 577f6b797..7011a20fb 100644--- a/dashboard/src/utils/importRefusal.ts+++ b/dashboard/src/utils/importRefusal.ts@@ -12,9 +12,14 @@* Matching positively rather than excluding known codes is load-bearing. A 409 with no code is the* DEFAULT state, not evidence of the orphan case: a reverse proxy, a gateway error page or a body* that never parsed all produce one, and so would any refusal added later by an author who does not- * know a destructive confirm keys on this. Withholding costs nothing — the refusal is still shown- * as an error toast carrying the server's own message. The backend pins its half of this contract- * in the infra-data controller spec.+ * know a destructive confirm keys on this. Withholding costs only the retry button: where the body+ * survived, the error toast still carries the server's own message; where it did not, there was no+ * message to lose and the alternative was offering a destructive OK under a bare "HTTP 409".+ *+ * A gateway older than this code refuses orphans without the code, so a split-origin dashboard+ * (VITE_API_URL) pointed at one withholds the confirm for a legitimate orphan refusal. That+ * degradation is deliberate and recoverable — the message still says to stop the sessions first.+ * The backend pins its half of the contract in the infra-data controller spec.*/export function shouldOfferStopOrphansRetry(status: number | undefined,diff --git a/src/modules/infra/infra-data.controller.spec.ts b/src/modules/infra/infra-data.controller.spec.tsindex 9500fde7f..cb1e469f1 100644--- a/src/modules/infra/infra-data.controller.spec.ts+++ b/src/modules/infra/infra-data.controller.spec.ts@@ -181,6 +181,9 @@ describe('InfraDataController.importData round-trips export-data (no silent mess// The dashboard decides whether to offer the destructive stop-orphans retry by matching this// code positively, so which code this refusal carries is a cross-tier contract, not a detail.expect((refusal as ConflictException).getResponse()).toMatchObject({ code: 'IMPORT_NESTED_TRANSACTION' });+ // Asserted alongside the code because `message` is the field the dashboard renders when it+ // withholds the retry — dropping it degrades the operator's toast to a bare "HTTP 409".+ expect((refusal as ConflictException).message).toContain('Another database transaction');// The decisive assertion: nothing was destroyed on the way to the refusal.const survived = await ds.getRepository(Session).findOneBy({ id: 's1' });@@ -222,6 +225,7 @@ describe('InfraDataController.importData round-trips export-data (no silent messconst refusal = await second.catch((e: unknown) => e);expect((refusal as ConflictException).getStatus()).toBe(409);expect((refusal as ConflictException).getResponse()).toMatchObject({ code: 'IMPORT_ALREADY_RUNNING' });+ expect((refusal as ConflictException).message).toContain('already running');await expect(first).resolves.toMatchObject({ imported: true });});diff --git a/src/modules/infra/infra-data.controller.ts b/src/modules/infra/infra-data.controller.tsindex 192cbb7a5..f5f55e994 100644--- a/src/modules/infra/infra-data.controller.ts+++ b/src/modules/infra/infra-data.controller.ts@@ -424,8 +424,10 @@ export class InfraDataController {} else if (orphanedEngines.length > 0 && !data.force) {// Carries a code like the other two refusals, and for a stronger reason: this is the ONLY 409// on this route whose documented retry (stopOrphans=true) is destructive — it stops live- // engines — and the dashboard decides whether to offer that retry by matching this code. Were- // it identified by the ABSENCE of a code instead, every unrecognised 409 (a proxy's, a body+ // engines — and dashboard/src/utils/importRefusal.ts decides whether to offer that retry by+ // matching this code. Renaming it there and here together is required; renaming it here alone+ // leaves both suites green and withdraws the operator's only route to stopOrphans. Were the+ // case identified by the ABSENCE of a code instead, every unrecognised 409 (a proxy's, a body// that never parsed) would open a confirm whose OK tears down engines and replaces every table.throw new ConflictException({statusCode: 409,