fix: arreglar cache de grupos en tests unitarios

Co-authored-by: aider (openrouter/x-ai/grok-code-fast-1) <aider@aider.chat>
pull/1/head
borja 2 months ago
parent 8177a8eaa9
commit 69a5747cf9

@ -33,6 +33,9 @@ type EvolutionGroup = {
};
export class GroupSyncService {
// Static property for DB instance injection (defaults to global db)
static dbInstance: Database = db;
/**
* Gets the sync interval duration in milliseconds.
*
@ -92,12 +95,12 @@ export class GroupSyncService {
parent: g.linkedParent
})));
const dbGroupsBefore = db.prepare('SELECT id, active FROM groups').all();
const dbGroupsBefore = this.dbInstance.prepare('SELECT id, active FROM groups').all();
console.log(' Grupos en DB antes de upsert:', dbGroupsBefore);
const result = await this.upsertGroups(communityGroups);
const dbGroupsAfter = db.prepare('SELECT id, active FROM groups').all();
const dbGroupsAfter = this.dbInstance.prepare('SELECT id, active FROM groups').all();
console.log(' Grupos en DB después de upsert:', dbGroupsAfter);
return result;
@ -187,7 +190,7 @@ export class GroupSyncService {
return groups;
} catch (error) {
console.error('❌ Failed to fetch groups:', {
error: error instanceof Error ? error.message : String(error),
error: error instanceof Error ? error.message : String(e),
stack: error instanceof Error ? error.stack : undefined
});
throw error;
@ -195,7 +198,7 @@ export class GroupSyncService {
}
private static cacheActiveGroups(): void {
const groups = db.prepare('SELECT id, name FROM groups WHERE active = TRUE').all();
const groups = this.dbInstance.prepare('SELECT id, name FROM groups WHERE active = TRUE').all();
activeGroupsCache.clear();
for (const group of groups) {
activeGroupsCache.set(group.id, group.name);
@ -204,7 +207,7 @@ export class GroupSyncService {
}
private static getActiveGroupsCount(): number {
const result = db.prepare('SELECT COUNT(*) as count FROM groups WHERE active = TRUE').get();
const result = this.dbInstance.prepare('SELECT COUNT(*) as count FROM groups WHERE active = TRUE').get();
return result?.count || 0;
}
@ -275,9 +278,9 @@ export class GroupSyncService {
let added = 0;
let updated = 0;
const transactionResult = db.transaction(() => {
const transactionResult = this.dbInstance.transaction(() => {
// First mark all groups as inactive and update verification timestamp
const inactiveResult = db.prepare(`
const inactiveResult = this.dbInstance.prepare(`
UPDATE groups
SET active = FALSE,
last_verified = CURRENT_TIMESTAMP
@ -289,17 +292,17 @@ export class GroupSyncService {
});
for (const group of groups) {
const existing = db.prepare('SELECT 1 FROM groups WHERE id = ?').get(group.id);
const existing = this.dbInstance.prepare('SELECT 1 FROM groups WHERE id = ?').get(group.id);
console.log('Checking group:', group.id, 'exists:', !!existing);
if (existing) {
const updateResult = db.prepare(
const updateResult = this.dbInstance.prepare(
'UPDATE groups SET name = ?, active = TRUE, last_verified = CURRENT_TIMESTAMP WHERE id = ?'
).run(group.subject, group.id);
console.log('Updated group:', group.id, 'result:', updateResult);
updated++;
} else {
const insertResult = db.prepare(
const insertResult = this.dbInstance.prepare(
'INSERT INTO groups (id, community_id, name, active) VALUES (?, ?, ?, TRUE)'
).run(group.id, env.WHATSAPP_COMMUNITY_ID, group.subject);
console.log('Added group:', group.id, 'result:', insertResult);

@ -47,6 +47,9 @@ beforeEach(() => {
// Inject testDb for WebhookServer to use
WebhookServer.dbInstance = testDb;
// Inject testDb for GroupSyncService to use
GroupSyncService.dbInstance = testDb;
// Ensure database is initialized (recreates tables if dropped)
initializeDatabase(testDb);
@ -62,7 +65,7 @@ beforeEach(() => {
VALUES ('group-id@g.us', 'test-community', 'Test Group', 1)
`);
// Populate active groups cache
// Populate active groups cache with test data
GroupSyncService['cacheActiveGroups']();
});

Loading…
Cancel
Save