Root cause found for why the real Slack message showed "→ Validar e
executar" for nearly every ACTIONABLE_NOW ticket: logic/message.js's
ACTION_LABELS map had a hardcoded entry for ACTIONABLE_NOW that always
won over the AI's actual next_action field in shortActionLabel(). The
AI's real, specific, already-validated-as-non-generic next_action (e.g.
"Realizar inventário das máquinas") was being silently discarded for
display purposes on every single message. Fixed by removing the
ACTIONABLE_NOW entry -- it now falls through to the existing
next_action-based fallback (already there, just unreachable for that
class before). UNASSIGNED_ACTIONABLE intentionally keeps its fixed
"Atribuir técnico" label -- that IS the correct display for that class.
logic/classification.js: expanded GENERIC_NEXT_ACTION_PHRASES with the
exact phrase that leaked through ("validar e executar") plus the
mission's additional list (validar chamado, prosseguir atendimento,
executar chamado, tratar chamado, analisar chamado). Bumped
AI_CLASSIFIER_VERSION 3->4 since this changes what next_action values
validate as non-generic -- forces reclassification of any cached
candidate that only passed under the older, narrower blacklist.
n8n/nodes/validate-ai-response.node.js: generic-next-action rejections
now get their own distinct fallback_reason ('generic_or_unsupported_
next_action') instead of being lumped into 'invalid_json', matching the
same pattern already used for the technician-vendor conflict.
logic/slack.js + logic/config.js: added FORCE_SEND (default false).
Manual-triggered runs now never send for real even with SEND_ENABLED=
true in production, unless FORCE_SEND is also explicitly set -- protects
against someone clicking "Execute Workflow" in production and
accidentally re-sending today's digest.
Added format-safety regression tests (no literal backslash-n, URL never
bleeds into the next item's id, no markdown-wrapped URLs) -- verified
these were never actually triggered in a real send (the reported
corruption was n8n's raw JSON data-inspector view, not a delivered
Slack message; both manual runs in question had shouldSend:false).
169/169 tests passing.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
107 lines
5.7 KiB
JavaScript
107 lines
5.7 KiB
JavaScript
const { test } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { buildTopMessage, buildCoordinationDigest, shortActionLabel, tituloCurto } = require('./message');
|
|
|
|
function selectedItem(overrides) {
|
|
return Object.assign({
|
|
ticket: {
|
|
id: 38611, titulo: 'Colocar etiqueta sala 03', tecnico_atribuido: 'Sem tecnico atribuido',
|
|
sem_tecnico: true, sla_vencido: false, dias_sem_interacao: 2, link_glpi: 'https://glpi/x?id=38611',
|
|
},
|
|
aiClassification: { classification: 'UNASSIGNED_ACTIONABLE', next_action: 'Atribuir e executar', is_fallback: false },
|
|
}, overrides);
|
|
}
|
|
|
|
test('mensagem final contem cabecalho, id, link e acao curta', () => {
|
|
const msg = buildTopMessage([selectedItem({})], '24/08', {});
|
|
assert.ok(msg.includes('#38611'));
|
|
assert.ok(msg.includes('https://glpi/x?id=38611'));
|
|
assert.ok(msg.includes('→'));
|
|
assert.ok(msg.includes('24/08'));
|
|
});
|
|
|
|
test('mensagem NAO contem score, confianca ou justificativa longa', () => {
|
|
const msg = buildTopMessage([selectedItem({})], '24/08', {});
|
|
assert.ok(!/confian(c|ç)a/i.test(msg));
|
|
assert.ok(!/score/i.test(msg));
|
|
});
|
|
|
|
test('linha de acao e curta (limite de tamanho)', () => {
|
|
const label = shortActionLabel({ classification: 'ACTIONABLE_NOW', next_action: 'x'.repeat(100), is_fallback: false });
|
|
assert.ok(label.length <= 40);
|
|
});
|
|
|
|
test('tituloCurto limita a 60 caracteres', () => {
|
|
const curto = tituloCurto('a'.repeat(100));
|
|
assert.ok(curto.length <= 60);
|
|
});
|
|
|
|
test('digest de coordenacao vazio retorna null (nao manda lista igual todo dia)', () => {
|
|
assert.equal(buildCoordinationDigest([], '24/08'), null);
|
|
});
|
|
|
|
test('mensagem nao tem linhas em branco duplicadas entre cabecalho e blocos', () => {
|
|
const msg = buildTopMessage([selectedItem({}), selectedItem({ ticket: { ...selectedItem({}).ticket, id: 38612 } })], '24/08', {});
|
|
assert.ok(!msg.includes('\n\n\n'), 'nao deveria haver 3+ quebras de linha seguidas');
|
|
});
|
|
|
|
test('digest de coordenacao lista apenas os itens passados (novos/alterados)', () => {
|
|
const digest = buildCoordinationDigest([{ ticket: { id: 37691 }, aiClassification: { reason: 'aguardando definicao' }, exclusionReason: 'needs_coordination' }], '24/08');
|
|
assert.ok(digest.includes('#37691'));
|
|
});
|
|
|
|
test('BUG REAL corrigido: ACTIONABLE_NOW mostra o next_action real da IA, nao o rotulo fixo "Validar e executar" -- missao "correcao final" secao 2/5', () => {
|
|
const item = selectedItem({
|
|
aiClassification: { classification: 'ACTIONABLE_NOW', next_action: 'Realizar inventário das máquinas', is_fallback: false },
|
|
});
|
|
const msg = buildTopMessage([item], '24/08', {});
|
|
assert.ok(msg.includes('Realizar inventário das máquinas'));
|
|
assert.ok(!msg.includes('Validar e executar'));
|
|
});
|
|
|
|
test('UNASSIGNED_ACTIONABLE continua mostrando "Atribuir técnico" (rotulo fixo correto, nao e o bug)', () => {
|
|
const item = selectedItem({
|
|
aiClassification: { classification: 'UNASSIGNED_ACTIONABLE', next_action: 'Levar patch cord e etiquetar', is_fallback: false },
|
|
});
|
|
const msg = buildTopMessage([item], '24/08', {});
|
|
assert.ok(msg.includes('Atribuir técnico'));
|
|
});
|
|
|
|
test('mensagem nunca contem a sequencia literal de dois caracteres barra-n (bug reportado: \\\\n aparecendo como texto) -- teste obrigatorio #1', () => {
|
|
const dezItens = Array.from({ length: 10 }, (_, i) => selectedItem({
|
|
ticket: { ...selectedItem({}).ticket, id: 38600 + i, titulo: `Chamado ${i}` },
|
|
aiClassification: { classification: 'ACTIONABLE_NOW', next_action: `Diagnosticar item ${i}`, is_fallback: false },
|
|
}));
|
|
const msg = buildTopMessage(dezItens, '24/08', {});
|
|
assert.ok(!msg.includes('\\n'), 'mensagem nao pode conter a sequencia literal barra-n');
|
|
});
|
|
|
|
test('cada URL termina exatamente no ticket_id -- o proximo item nunca vaza pra dentro do link anterior -- teste obrigatorio #3', () => {
|
|
const dois = [
|
|
selectedItem({ ticket: { ...selectedItem({}).ticket, id: 38413, link_glpi: 'https://glpi.gtigrupo.com.br/front/ticket.form.php?id=38413' } }),
|
|
selectedItem({ ticket: { ...selectedItem({}).ticket, id: 38477, link_glpi: 'https://glpi.gtigrupo.com.br/front/ticket.form.php?id=38477' } }),
|
|
];
|
|
const msg = buildTopMessage(dois, '24/08', {});
|
|
assert.ok(msg.includes('id=38413\n'), 'a URL do primeiro chamado deve terminar em id=38413 seguido de quebra de linha real, nao emendar no proximo item');
|
|
assert.ok(!/id=38413\d/.test(msg), 'nenhum digito deveria colar direto depois do id do primeiro chamado (URL corrompida)');
|
|
assert.ok(msg.includes('https://glpi.gtigrupo.com.br/front/ticket.form.php?id=38413'));
|
|
assert.ok(msg.includes('https://glpi.gtigrupo.com.br/front/ticket.form.php?id=38477'));
|
|
});
|
|
|
|
test('URL fica pura (sem colchetes markdown [url](url)) -- Slack ja auto-linka texto puro -- teste obrigatorio #4', () => {
|
|
const msg = buildTopMessage([selectedItem({})], '24/08', {});
|
|
assert.ok(!/\[https?:\/\//.test(msg), 'nao deveria embrulhar a URL em markdown [texto](url)');
|
|
});
|
|
|
|
test('mensagem com 10 chamados continua curta (teste obrigatorio #19 -- nao vira relatorio longo)', () => {
|
|
const dezItens = Array.from({ length: 10 }, (_, i) => selectedItem({
|
|
ticket: { ...selectedItem({}).ticket, id: 38600 + i, titulo: `Titulo do chamado numero ${i} com texto razoavelmente longo` },
|
|
}));
|
|
const msg = buildTopMessage(dezItens, '25/08', {});
|
|
// 10 blocos de ~4 linhas curtas cada + cabecalho: bem abaixo de um "relatorio". Limite
|
|
// generoso o suficiente pra nao quebrar por titulos longos, mas que pegaria regressao real
|
|
// (ex.: reason/evidence/JSON vazando pra mensagem).
|
|
assert.ok(msg.length < 2000, `mensagem ficou com ${msg.length} caracteres, esperado < 2000`);
|
|
assert.ok(!/"classification"|"evidence"|"confidence"/.test(msg), 'mensagem nao pode vazar campos internos/JSON');
|
|
});
|