// 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); console.log('observability web checks passed');