import test from 'node:test'; import assert from 'node:assert/strict'; import { openDb, getSetting, setSetting } from '../src/db.mjs'; import { listWabaTemplateLibrary, syncWabaTemplateLibrary } from '../src/outreach/waba-template-library.mjs'; test('listWabaTemplateLibrary retorna modelos padrao', () => { const db = openDb(':memory:'); const templates = listWabaTemplateLibrary(db); assert.ok(templates.some((template) => template.name === 'gti_inicio_atendimento_solicitado')); db.close(); }); test('syncWabaTemplateLibrary importa templates de resposta do GChat', async () => { const db = openDb(':memory:'); setSetting(db, 'gchat.templatesUrl', 'https://gchat.example/templates'); const originalFetch = global.fetch; global.fetch = async () => ({ ok: true, status: 200, text: async () => JSON.stringify({ data: [{ name: 'novo_template', language: 'pt_BR', category: 'UTILITY', components: [{ type: 'BODY', text: 'Ola {{1}}, assunto {{2}}' }] }] }) }); try { const result = await syncWabaTemplateLibrary(db); assert.equal(result.ok, true); assert.equal(result.total, 1); } finally { global.fetch = originalFetch; } const saved = JSON.parse(getSetting(db, 'gchat.wabaTemplatesJson')); assert.deepEqual(saved[0].variables, ['{empresa}', '{produto}']); db.close(); });