Files
SLACK_COORD_RELATORIO/coordenador/logic/slack.test.js
T
Gustavo Santos e322cec92b chore: import coordenador (subtree)
git-subtree-dir: coordenador
git-subtree-mainline: facbe2e340
git-subtree-split: ce7af94d55
2026-10-01 14:25:55 -03:00

101 lines
5.5 KiB
JavaScript

const { test } = require('node:test');
const assert = require('node:assert/strict');
const {
computeMessageHash, shouldSendByConfig, alreadySentSuccessfully, evaluateSendGuard, buildEnvioDigestRecord,
} = require('./slack');
test('computeMessageHash e deterministico para o mesmo texto', () => {
assert.equal(computeMessageHash('ola mundo'), computeMessageHash('ola mundo'));
});
test('computeMessageHash muda quando o texto muda', () => {
assert.notEqual(computeMessageHash('ola mundo'), computeMessageHash('ola mundo!'));
});
test('SEND_ENABLED=false nunca deixa enviar -- missao secao 22/13', () => {
assert.equal(shouldSendByConfig({ sendEnabled: false }), false);
});
test('SEND_ENABLED=true libera o envio (sujeito ainda a idempotencia)', () => {
assert.equal(shouldSendByConfig({ sendEnabled: true }), true);
});
test('alreadySentSuccessfully detecta envio de sucesso identico (mesma data/tipo/hash)', () => {
const existing = [{ report_date: '2026-08-25', run_type: 'scheduled', message_hash: 'abc', status: 'success' }];
assert.equal(alreadySentSuccessfully(existing, { reportDate: '2026-08-25', runType: 'scheduled', messageHash: 'abc' }), true);
});
test('alreadySentSuccessfully ignora registros de falha -- retry apos erro nao fica bloqueado', () => {
const existing = [{ report_date: '2026-08-25', run_type: 'scheduled', message_hash: 'abc', status: 'failed' }];
assert.equal(alreadySentSuccessfully(existing, { reportDate: '2026-08-25', runType: 'scheduled', messageHash: 'abc' }), false);
});
test('alreadySentSuccessfully ignora envios de outro dia ou outro hash', () => {
const existing = [{ report_date: '2026-08-24', run_type: 'scheduled', message_hash: 'abc', status: 'success' }];
assert.equal(alreadySentSuccessfully(existing, { reportDate: '2026-08-25', runType: 'scheduled', messageHash: 'abc' }), false);
const existing2 = [{ report_date: '2026-08-25', run_type: 'scheduled', message_hash: 'xyz', status: 'success' }];
assert.equal(alreadySentSuccessfully(existing2, { reportDate: '2026-08-25', runType: 'scheduled', messageHash: 'abc' }), false);
});
test('evaluateSendGuard: Slack NAO envia com SEND_ENABLED=false -- teste obrigatorio #13', () => {
const result = evaluateSendGuard({ config: { sendEnabled: false }, existingRecords: [], reportDate: '2026-08-25', runType: 'scheduled', messageHash: 'abc' });
assert.equal(result.shouldSend, false);
assert.equal(result.reason, 'send_disabled');
});
test('evaluateSendGuard: Slack envia com SEND_ENABLED=true e nenhum envio anterior -- teste obrigatorio #14', () => {
const result = evaluateSendGuard({ config: { sendEnabled: true }, existingRecords: [], reportDate: '2026-08-25', runType: 'scheduled', messageHash: 'abc' });
assert.equal(result.shouldSend, true);
assert.equal(result.reason, 'ok');
});
test('evaluateSendGuard: envio duplicado e bloqueado -- teste obrigatorio #15', () => {
const existing = [{ report_date: '2026-08-25', run_type: 'scheduled', message_hash: 'abc', status: 'success' }];
const result = evaluateSendGuard({ config: { sendEnabled: true }, existingRecords: existing, reportDate: '2026-08-25', runType: 'scheduled', messageHash: 'abc' });
assert.equal(result.shouldSend, false);
assert.equal(result.reason, 'already_sent');
});
test('runType manual NAO envia mesmo com SEND_ENABLED=true, a menos que FORCE_SEND tambem esteja ligado -- missao "correcao final" secao 16', () => {
const result = evaluateSendGuard({
config: { sendEnabled: true, forceSend: false }, existingRecords: [], reportDate: '2026-08-25', runType: 'manual', messageHash: 'abc',
});
assert.equal(result.shouldSend, false);
assert.equal(result.reason, 'manual_run_needs_force_send');
});
test('runType manual envia quando FORCE_SEND esta explicitamente ligado -- missao "correcao final" secao 17', () => {
const result = evaluateSendGuard({
config: { sendEnabled: true, forceSend: true }, existingRecords: [], reportDate: '2026-08-25', runType: 'manual', messageHash: 'abc',
});
assert.equal(result.shouldSend, true);
});
test('runType scheduled envia normalmente com SEND_ENABLED=true, independente de FORCE_SEND', () => {
const result = evaluateSendGuard({
config: { sendEnabled: true, forceSend: false }, existingRecords: [], reportDate: '2026-08-25', runType: 'scheduled', messageHash: 'abc',
});
assert.equal(result.shouldSend, true);
});
test('evaluateSendGuard: retry (mensagem diferente) apos falha anterior nao e bloqueado', () => {
const existing = [{ report_date: '2026-08-25', run_type: 'scheduled', message_hash: 'abc', status: 'failed' }];
const result = evaluateSendGuard({ config: { sendEnabled: true }, existingRecords: existing, reportDate: '2026-08-25', runType: 'scheduled', messageHash: 'abc' });
assert.equal(result.shouldSend, true);
});
test('buildEnvioDigestRecord monta o registro de sucesso corretamente', () => {
const record = buildEnvioDigestRecord({ reportDate: '2026-08-25', runType: 'scheduled', messageHash: 'abc', sentAt: 12345, status: 'success' });
assert.deepEqual(record, {
report_date: '2026-08-25', run_type: 'scheduled', message_hash: 'abc', sent_at: 12345, status: 'success', error_detail: null,
});
});
test('buildEnvioDigestRecord monta o registro de falha com error_detail -- falha nunca registra sucesso (teste obrigatorio #16)', () => {
const record = buildEnvioDigestRecord({
reportDate: '2026-08-25', runType: 'scheduled', messageHash: 'abc', sentAt: 12345, status: 'failed', errorDetail: 'HTTP 500',
});
assert.equal(record.status, 'failed');
assert.equal(record.error_detail, 'HTTP 500');
});