Files
dtf-system/tests/observability_web_test.mjs
Cauê Faleiros f2b825b652
Some checks failed
Build and deploy / Validate source (push) Successful in 6s
Build and deploy / Publish images (push) Has been cancelled
Build and deploy / Secret scan and release gate (push) Has been cancelled
Build and deploy / Integration suite on a real stack (push) Has been cancelled
feat: every error shown in red reaches Sentry, also the ones the page handled itself
Until now only failed API calls and uncaught exceptions reached Sentry. An
error the page caught and showed, like the folders' "A última tentativa
falhou" Marcus photographed on 16/11, or an upload that failed on the Site,
never did. Now every red message in the Kanban, the Site's checkout, the
customer's account page and every failed upload is sent, one issue per kind
of message; the same text an API error already sent, or a 401 / restart 503
it skips on purpose, is not sent twice.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-16 13:40:59 -03:00

66 lines
3.4 KiB
JavaScript

// The browser's API error grouping (note Sentry): one issue per kind of error.
// Dependency-free: Node loads web/observability.js with a stub Sentry.
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import vm from 'node:vm';
let captured;
globalThis.window = globalThis;
globalThis.location = {pathname: '/carrinho'};
globalThis.Sentry = {
init() {},
setTag() {},
captureMessage(message, options) { captured = {message, options}; },
};
vm.runInThisContext(readFileSync('web/observability.js', 'utf8'));
window.dtfObservability.start('https://key@o1.ingest.sentry.io/1', 'site');
const apiError = (path, status, message) => {
captured = null;
window.dtfObservability.apiError(path, status, message);
assert.ok(captured, 'apiError sends one event');
return captured;
};
const expired = 'Este arquivo expirou. Escolha o arquivo de novo.';
const first = apiError('/uploads/ba39b432-b0d1-4b1b-8971-09123e067067', 410, expired);
const second = apiError('/uploads/5f2be7e0-1111-2222-3333-444455556666', 410, expired);
assert.deepEqual(first.options.fingerprint, second.options.fingerprint);
assert.equal(first.message, 'API 410 /uploads/:id: ' + expired);
assert.deepEqual(first.options.fingerprint, ['api-error', '410', '/uploads/:id', expired]);
const otherMessage = apiError('/uploads/ba39b432-b0d1-4b1b-8971-09123e067067', 410,
'Arquivo não encontrado. Escolha o arquivo de novo.');
assert.notDeepEqual(first.options.fingerprint, otherMessage.options.fingerprint);
// The numbers and the file names in the server message change per occurrence;
// the kind does not.
const png = apiError('/quotes', 422, 'Item 1: layout.png abaixo do mínimo de 96 DPI.');
const jpg = apiError('/quotes', 422, 'Item 2: final.jpg abaixo do mínimo de 300 DPI.');
assert.deepEqual(png.options.fingerprint, jpg.options.fingerprint);
assert.equal(png.message, 'API 422 /quotes: Item :id: :arquivo abaixo do mínimo de :id DPI.');
// The status and the path are part of the kind.
assert.notDeepEqual(png.options.fingerprint,
apiError('/quotes', 500, 'Item 1: layout.png abaixo do mínimo de 96 DPI.').options.fingerprint);
assert.notDeepEqual(
apiError('/quotes/ba39b432-b0d1-4b1b-8971-09123e067067', 422, expired).options.fingerprint,
apiError('/orders/ba39b432-b0d1-4b1b-8971-09123e067067', 422, expired).options.fingerprint);
// Every red message reaches Sentry, also one the page caught itself; the
// same text an API error already sent (or deliberately skipped) is not doubled.
captured = null;
window.dtfObservability.uiError('Pastas automáticas: A última tentativa falhou: DTF-26-item-1.png bloqueado.', 'kanban');
assert.ok(captured, 'uiError sends a caught error');
assert.equal(captured.message, 'Aviso kanban: Pastas automáticas: A última tentativa falhou: :arquivo bloqueado.');
assert.deepEqual(captured.options.fingerprint, ['ui-error', 'kanban', 'Pastas automáticas: A última tentativa falhou: :arquivo bloqueado.']);
apiError('/orders', 409, 'Falta a foto da revisão.');
captured = null;
window.dtfObservability.uiError('Falta a foto da revisão.', 'kanban');
assert.equal(captured, null, 'an API error shown in red is sent once');
window.dtfObservability.apiError('/board', 503, 'O sistema está reiniciando.');
window.dtfObservability.uiError('O sistema está reiniciando.', 'kanban');
assert.equal(captured, null, 'a skipped restart 503 stays skipped');
console.log('observability web checks passed');