Files
dtf-system/tests/observability_web_test.mjs
Cauê Faleiros fe5e9c4f4a
All checks were successful
Build and deploy / Validate source (push) Successful in 8s
Build and deploy / Integration suite on a real stack (push) Successful in 8m14s
Build and deploy / Secret scan and release gate (push) Successful in 7s
Build and deploy / Publish images (push) Successful in 1m22s
fix: a file the phone cannot read tells the customer to save it on the phone first, and Sentry learns its kind (DTF-WEB-2D)
The same Android 10 phone failed three times on 2026-12-02 reading a file
seven seconds after picking it: not a permission lost over time, but a file
the phone does not have yet (Google Drive or WhatsApp). "Escolha o arquivo de
novo" sent the customer back to the same file. The message now says "Se ele
está no Google Drive ou no WhatsApp, salve no aparelho e escolha de novo".
The Sentry notice carries the browser's error name, the file's type,
extension and size in MB, never its name, so the next one says which case
it is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 14:23:11 -03:00

96 lines
5.2 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.']);
// Extra detail (the kind of file a phone could not read, DTF-WEB-2D) rides along.
captured = null;
window.dtfObservability.uiError('Envio de arquivo falhou: não conseguimos ler este arquivo no aparelho.', 'site',
{erro: 'NotReadableError', extensao: 'png', mb: 12.5});
assert.equal(captured.options.extra.extensao, 'png');
assert.equal(captured.options.extra.mb, 12.5);
assert.ok(captured.options.extra.page);
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');
// A refusal the server explains (a 4xx with a sentence) is no bug: on the
// Kanban a warning, on the Site nothing; a field list or a 5xx is an error
// (2026-11-30).
const refused = (app, status, detail) => {
// A fresh copy per app: a page starts Sentry once, for its own app.
vm.runInThisContext(readFileSync('web/observability.js', 'utf8'));
window.dtfObservability.start('https://key@o1.ingest.sentry.io/1', app);
captured = null;
window.dtfObservability.apiError('/orders/1/move', status, 'Puxe o pedido na mesma máquina.', detail);
return captured;
};
assert.equal(refused('kanban', 409, 'Puxe o pedido na mesma máquina.').options.level, 'warning');
assert.equal(refused('kanban', 409, 'Puxe o pedido na mesma máquina.').options.tags.recusa, 'sim');
assert.equal(refused('site', 409, 'Cupom não encontrado.'), null, 'a refusal on the Site is not sent');
assert.equal(refused('site', 422, [{loc: ['body', 'items'], msg: 'bad'}]).options.level, 'error');
assert.equal(refused('site', 500, 'Internal Server Error').options.level, 'error');
assert.equal(refused('kanban', 422, [{loc: ['body'], msg: 'bad'}]).options.level, 'error');
// Shown in red after it, the refusal is not sent again as a page error.
refused('site', 409, 'Cupom não encontrado.');
captured = null;
window.dtfObservability.uiError('Puxe o pedido na mesma máquina.', 'site');
assert.equal(captured, null, 'a refusal shown in red is not sent as an error');
console.log('observability web checks passed');