Files
tiny-webhook/test/n8n-service.test.ts
Cauê Faleiros 4fe5830f8f Stop losing notifications on Olist/n8n hiccups and redeploys
- Olist lookups: 20s timeout so one hung request can't stall the serial
  queue; retry temporary failures (codigo_erro 6/11/35/99, timeouts,
  5xx) with backoff instead of forwarding orders without phone or
  forma_envio. Log "[Notification at risk]" when that still happens.
- n8n forwarding: timeout plus retries, only when n8n certainly didn't
  run the workflow (connection refused, 502/503, 404 while restarting),
  so retries can't send a customer the same WhatsApp twice. Stop logging
  the start of N8N_AUTH_TOKEN.
- On SIGTERM, stop accepting requests and finish webhooks already acked
  to Tiny (up to SHUTDOWN_GRACE_MS, default 50s).
- Keep stock_memory.json and stock_log.csv on the data-runtime volume so
  stock stats survive redeploys.
- Add a node:test suite (npm test) covering the payload n8n reads, the
  status cache, burst queueing, retries, stock files and shutdown drain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 15:12:09 -03:00

52 lines
2.0 KiB
TypeScript

import { test, before, after, beforeEach } from 'node:test';
import assert from 'node:assert/strict';
import { setTestEnv, mockAxiosPost, axiosError, silenceConsole } from './helpers';
setTestEnv({});
const { sendToN8n } = require('../src/services/n8n.service');
let responses: Array<() => any> = [];
const calls = mockAxiosPost(async () => {
const next = responses.shift();
return next ? next() : { status: 200, data: {} };
});
let capture: ReturnType<typeof silenceConsole>;
before(() => { capture = silenceConsole(); });
after(() => capture.restore());
beforeEach(() => { calls.length = 0; responses = []; });
const fail = (code: string, status?: number) => () => { throw axiosError(code, status); };
test('retries when n8n refuses the connection (n8n restarting)', async () => {
responses = [fail('ECONNREFUSED')];
await sendToN8n({ id: 1 }, 'http://n8n.test/status');
assert.equal(calls.length, 2);
});
test('retries when the proxy says n8n is down (502/503)', async () => {
responses = [fail('ERR_BAD_RESPONSE', 502), fail('ERR_BAD_RESPONSE', 503)];
await sendToN8n({ id: 1 }, 'http://n8n.test/status');
assert.equal(calls.length, 3);
});
test('retries 404 while n8n has not registered the webhook yet, up to the max attempts', async () => {
responses = [1, 2, 3, 4, 5, 6].map(() => fail('ERR_BAD_REQUEST', 404));
await sendToN8n({ id: 1 }, 'http://n8n.test/status');
assert.equal(calls.length, 5);
});
test('does not retry when n8n may already have received the request', async () => {
for (const failure of [fail('ECONNABORTED'), fail('ECONNRESET'), fail('ERR_BAD_RESPONSE', 504), fail('ERR_BAD_RESPONSE', 500), fail('ERR_BAD_REQUEST', 400)]) {
calls.length = 0;
responses = [failure];
await sendToN8n({ id: 1 }, 'http://n8n.test/status');
assert.equal(calls.length, 1);
}
});
test('sends once when n8n answers', async () => {
await sendToN8n({ id: 1 }, 'http://n8n.test/status');
assert.equal(calls.length, 1);
});