fix: persistencia dinamica em configs e filtro de tabelas ativas status publish

This commit is contained in:
Andre RDS
2026-11-27 09:00:08 -03:00
parent 9e6c38ff37
commit cc6a77612b
16 changed files with 554 additions and 144 deletions
+17
View File
@@ -158,4 +158,21 @@ describe("SQLite Repositories", () => {
// Mais recente primeiro
expect(new Date(history[0].checked_at).getTime()).toBeGreaterThanOrEqual(new Date(history[1].checked_at).getTime());
});
test("TableRepository: deleteNotIn remove tabelas desativadas/removidas", () => {
const t1: TableInfo = { id: "active-1", title: "Ativo 1", operadora: "Op", administradora: null, revision_date: "Out/2027", download_pdf: null, vigencia: null, produtos_json: null, status: "vigente", extracted_at: null, last_checked: "2026-01-01" };
const t2: TableInfo = { id: "deactivated-2", title: "Desativado 2", operadora: "Op", administradora: null, revision_date: "Out/2026", download_pdf: null, vigencia: null, produtos_json: null, status: "vencida", extracted_at: null, last_checked: "2026-01-01" };
tableRepo.save(t1);
tableRepo.save(t2);
expect(tableRepo.getAll().length).toBe(2);
// Sync do CMS traz apenas active-1
const deletedCount = tableRepo.deleteNotIn(["active-1"]);
expect(deletedCount).toBe(1);
const remaining = tableRepo.getAll();
expect(remaining.length).toBe(1);
expect(remaining[0].id).toBe("active-1");
});
});
+2 -2
View File
@@ -15,7 +15,7 @@ describe("AstraCallsClient", () => {
const c = new AstraCallsClient({
mcpUrl: "https://astra.test/mcp",
bearerToken: "my-secret-token",
}, mockFetch);
}, mockFetch as any);
await c.sendText("5527999998888", "teste");
expect(capturedHeaders?.Authorization).toBe("Bearer my-secret-token");
@@ -30,7 +30,7 @@ describe("AstraCallsClient", () => {
}), { status: 200, headers: { "content-type": "application/json" } });
};
const c = new AstraCallsClient({ mcpUrl: "https://astra.test/mcp" }, mockFetch);
const c = new AstraCallsClient({ mcpUrl: "https://astra.test/mcp" }, mockFetch as any);
await c.sendText("5527999998888", "teste");
});
});
+70
View File
@@ -0,0 +1,70 @@
import { test, expect, describe, beforeEach } from "bun:test";
import { DynamicConfigService } from "../../src/infrastructure/config/dynamic-config-service.js";
import type { SettingsRepository } from "../../src/domain/repositories/settings-repository.js";
import type { SettingValue } from "../../src/domain/models/setting.js";
class InMemorySettingsRepo implements SettingsRepository {
private map = new Map<string, string>();
get(key: string): SettingValue | null {
const val = this.map.get(key);
return val !== undefined ? { key, value: val, updated_at: "now" } : null;
}
getAll(): SettingValue[] {
return Array.from(this.map.entries()).map(([key, value]) => ({ key, value, updated_at: "now" }));
}
set(key: string, value: string): void {
this.map.set(key, value);
}
}
describe("DynamicConfigService", () => {
let repo: InMemorySettingsRepo;
let dynamicConfig: DynamicConfigService;
beforeEach(() => {
repo = new InMemorySettingsRepo();
dynamicConfig = new DynamicConfigService(repo);
delete process.env.TEST_DYNAMIC_KEY;
});
test("Prioridade: SQLite > process.env > default", () => {
process.env.CMS_API_URL = "https://env.example.com";
expect(dynamicConfig.get("CMS_API_URL")).toBe("https://env.example.com");
repo.set("CMS_API_URL", "https://sqlite.example.com");
expect(dynamicConfig.get("CMS_API_URL")).toBe("https://sqlite.example.com");
});
test("Não sobrescreve segredo se valor for máscara", () => {
repo.set("CMS_API_KEY", "real-super-secret");
dynamicConfig.set("CMS_API_KEY", "••••••••");
expect(dynamicConfig.get("CMS_API_KEY")).toBe("real-super-secret");
dynamicConfig.set("CMS_API_KEY", "*****");
expect(dynamicConfig.get("CMS_API_KEY")).toBe("real-super-secret");
dynamicConfig.set("CMS_API_KEY", "new-real-secret");
expect(dynamicConfig.get("CMS_API_KEY")).toBe("new-real-secret");
});
test("getNumber e getBoolean convertem tipos corretamente", () => {
repo.set("TABLE_EXPIRY_WARNING_DAYS", "45");
expect(dynamicConfig.getNumber("TABLE_EXPIRY_WARNING_DAYS", 30)).toBe(45);
repo.set("CUSTOM_BOOL", "true");
expect(dynamicConfig.getBoolean("CUSTOM_BOOL", false)).toBe(true);
repo.set("CUSTOM_BOOL", "false");
expect(dynamicConfig.getBoolean("CUSTOM_BOOL", true)).toBe(false);
});
test("getEffectiveSettings mascara segredos", () => {
repo.set("CMS_API_KEY", "my-secret-key");
const settings = dynamicConfig.getEffectiveSettings(true);
const cmsKeySetting = settings.find(s => s.key === "CMS_API_KEY");
expect(cmsKeySetting).toBeDefined();
expect(cmsKeySetting?.value).toBe("••••••••");
expect(cmsKeySetting?.isSecret).toBe(true);
});
});
+100
View File
@@ -0,0 +1,100 @@
import { test, expect, describe, beforeEach } from "bun:test";
import { TableService, calculateTableStatus } from "../../src/application/services/table-service.js";
import type { TableInfo } from "../../src/domain/models/table.js";
describe("Table Service Status Evaluation & Publish Filtering", () => {
test("Tabela em dia (como exemplo do usuario) e avaliada como vigente", () => {
const table: TableInfo = {
id: "55c02939-aadd-4403-8766-f5da1a506454",
title: "SBS Up Health – Empresarial – SINTRAMASSAS",
operadora: "São Bernardo Samp",
administradora: "Up Health",
revision_date: "Setembro/2027",
download_pdf: "https://media.rmscorretora.com.br/uploads/pdf.pdf",
vigencia: null,
produtos_json: {
_meta: {
extracted_at: "2026-11-26T20:04:10.700782+00:00",
},
produtos: [{ nome: "Plano 1", tipo: "Ambulatorial", faixas: [] }],
},
status: "sem_data",
extracted_at: "2026-11-26T20:04:10.700782+00:00",
last_checked: "2026-11-27T00:00:00.000Z",
vesting_status: "em_dia",
is_stale: false,
modified_gmt: "2026-11-26 20:06:09",
};
const status = calculateTableStatus(table, 30, 7);
expect(status).toBe("vigente");
});
test("Tabela com vesting_status vencido ou is_stale e avaliada como vencida", () => {
const table: TableInfo = {
id: "stale-1",
title: "Tabela Vencida por Stale",
operadora: "Op",
administradora: null,
revision_date: "Setembro/2027",
download_pdf: null,
vigencia: null,
produtos_json: null,
status: "sem_data",
extracted_at: null,
last_checked: "2026-11-27T00:00:00.000Z",
vesting_status: "vencido",
is_stale: true,
modified_gmt: "2026-11-26 20:06:09",
};
expect(calculateTableStatus(table, 30, 7)).toBe("vencida");
});
test("Tabela sem revision_date ou com * e avaliada como sem_data (falta)", () => {
const tableStar: TableInfo = {
id: "star-1",
title: "Tabela com Asterisco",
operadora: "Op",
administradora: null,
revision_date: "*",
download_pdf: null,
vigencia: null,
produtos_json: null,
status: "sem_data",
extracted_at: null,
last_checked: "2026-11-27T00:00:00.000Z",
vesting_status: "em_dia",
is_stale: false,
modified_gmt: null,
};
expect(calculateTableStatus(tableStar, 30, 7)).toBe("sem_data");
const tableEmpty: TableInfo = {
...tableStar,
id: "empty-1",
revision_date: null,
};
expect(calculateTableStatus(tableEmpty, 30, 7)).toBe("sem_data");
});
test("Tabela com vesting_status a_vencer e classificada como prestes_a_vencer", () => {
const table: TableInfo = {
id: "avencer-1",
title: "Tabela A Vencer",
operadora: "Op",
administradora: null,
revision_date: "Dezembro/2027",
download_pdf: null,
vigencia: null,
produtos_json: null,
status: "sem_data",
extracted_at: null,
last_checked: "2026-11-27T00:00:00.000Z",
vesting_status: "a_vencer",
is_stale: false,
modified_gmt: null,
};
expect(calculateTableStatus(table, 30, 7)).toBe("prestes_a_vencer");
});
});
+5 -4
View File
@@ -1,6 +1,7 @@
import { test, expect, describe, beforeEach, afterEach } from "bun:test";
import { calculateTableStatus, parseRevisionDate, calculateSeverity } from "../../src/application/services/table-service.ts";
import { TableAlertFormatter } from "../../src/domain/services/table-alert-formatter.ts";
import type { ExpiryAlert } from "../../src/domain/models/table.ts";
describe("Table Status Calculation", () => {
const originalWarningDays = process.env.TABLE_EXPIRY_WARNING_DAYS;
@@ -124,7 +125,7 @@ describe("TableAlertFormatter", () => {
const formatter = new TableAlertFormatter();
test("formatExpiryAlert: expired", () => {
const alert = {
const alert: ExpiryAlert = {
table_id: "test-1",
table_title: "Plano Teste",
operadora: "Operadora X",
@@ -145,7 +146,7 @@ describe("TableAlertFormatter", () => {
});
test("formatExpiryAlert: critical", () => {
const alert = {
const alert: ExpiryAlert = {
table_id: "test-2",
table_title: "Plano Crítico",
operadora: "Operadora Z",
@@ -163,7 +164,7 @@ describe("TableAlertFormatter", () => {
});
test("formatExpiryAlert: warning", () => {
const alert = {
const alert: ExpiryAlert = {
table_id: "test-3",
table_title: "Plano Warning",
operadora: "Operadora W",
@@ -181,7 +182,7 @@ describe("TableAlertFormatter", () => {
});
test("escapeHtml: sanitiza caracteres perigosos", () => {
const alert = {
const alert: ExpiryAlert = {
table_id: "test-4",
table_title: "Plano <script>alert(1)</script>",
operadora: "Op & Co",
+5 -5
View File
@@ -3,7 +3,7 @@ import { TelegramNotifier } from "../../src/infrastructure/telegram/telegram-not
describe("TelegramNotifier", () => {
let notifier: TelegramNotifier;
let fetchMock: typeof fetch;
let fetchMock: any;
beforeEach(() => {
fetchMock = async (url: string, options: any) => {
@@ -47,7 +47,7 @@ describe("TelegramNotifier", () => {
const n = new TelegramNotifier({
botToken: "123456:TEST",
chatId: "-1001111111111",
fetchFn: mockFetch,
fetchFn: mockFetch as any,
delayMs: 0,
});
@@ -74,7 +74,7 @@ describe("TelegramNotifier", () => {
const n = new TelegramNotifier({
botToken: "123456:TEST",
chatId: "-1001234567890",
fetchFn: mockFetch,
fetchFn: mockFetch as any,
delayMs: 0,
maxRetries: 3,
});
@@ -98,7 +98,7 @@ describe("TelegramNotifier", () => {
const n = new TelegramNotifier({
botToken: "123456:TEST",
chatId: "-1001234567890",
fetchFn: mockFetch,
fetchFn: mockFetch as any,
delayMs: 0,
maxRetries: 3,
});
@@ -121,7 +121,7 @@ describe("TelegramNotifier", () => {
const n = new TelegramNotifier({
botToken: "123456:TEST",
chatId: "-1001234567890",
fetchFn: mockFetch,
fetchFn: mockFetch as any,
delayMs: 0,
});