From a7004d5ef1f0faaa7b855386dc7ea9c6e4ab9338 Mon Sep 17 00:00:00 2001 From: borja Date: Mon, 29 Sep 2025 14:34:24 +0200 Subject: [PATCH] test: usar seedGroup para poblar grupos en gating Co-authored-by: aider (openrouter/openai/gpt-5) --- tests/unit/services/reminders.gating.test.ts | 36 ++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/tests/unit/services/reminders.gating.test.ts b/tests/unit/services/reminders.gating.test.ts index 7e3314c..61df16f 100644 --- a/tests/unit/services/reminders.gating.test.ts +++ b/tests/unit/services/reminders.gating.test.ts @@ -6,6 +6,38 @@ import { RemindersService } from '../../../src/services/reminders'; import { AllowedGroups } from '../../../src/services/allowed-groups'; import { ResponseQueue } from '../../../src/services/response-queue'; +function seedGroup(db: Database, groupId: string) { + const cols = db.query(`PRAGMA table_info(groups)`).all() as any[]; + const values: Record = {}; + const nowIso = new Date().toISOString().replace('T', ' ').replace('Z', ''); + + for (const c of cols) { + const name = String(c.name); + const type = String(c.type || '').toUpperCase(); + const notnull = Number(c.notnull || 0) === 1; + const hasDefault = c.dflt_value != null; + + if (name === 'id') { values[name] = groupId; continue; } + if (name === 'name' || name === 'title' || name === 'subject') { values[name] = 'Test Group'; continue; } + if (name === 'created_by') { values[name] = 'tester'; continue; } + if (name.endsWith('_at')) { values[name] = nowIso; continue; } + if (name === 'is_active' || name === 'active') { values[name] = 1; continue; } + + if (notnull && !hasDefault) { + if (type.includes('INT')) values[name] = 1; + else if (type.includes('REAL')) values[name] = 0; + else values[name] = 'N/A'; + } + } + + if (!('id' in values)) values['id'] = groupId; + + const colsList = Object.keys(values); + const placeholders = colsList.map(() => '?').join(', '); + const sql = `INSERT OR REPLACE INTO groups (${colsList.join(', ')}) VALUES (${placeholders})`; + db.prepare(sql).run(...colsList.map(k => values[k])); +} + describe('RemindersService - gating por grupos en modo enforce', () => { const envBackup = process.env; let memdb: Database; @@ -45,8 +77,8 @@ describe('RemindersService - gating por grupos en modo enforce', () => { `); // Sembrar grupos y estados - memdb.exec(`INSERT OR IGNORE INTO groups (id) VALUES ('ok@g.us')`); - memdb.exec(`INSERT OR IGNORE INTO groups (id) VALUES ('na@g.us')`); + seedGroup(memdb, 'ok@g.us'); + seedGroup(memdb, 'na@g.us'); AllowedGroups.setStatus('ok@g.us', 'allowed', 'OK'); AllowedGroups.setStatus('na@g.us', 'allowed', 'NA'); // inicialmente allowed para que las tareas se creen con group_id