diff --git a/src/services/group-sync.ts b/src/services/group-sync.ts index 834434d..1165b4f 100644 --- a/src/services/group-sync.ts +++ b/src/services/group-sync.ts @@ -3,7 +3,7 @@ import { db } from '../db'; const env = process.env; // In-memory cache for active groups -const activeGroupsCache = new Map(); // groupId -> groupName +// const activeGroupsCache = new Map(); // groupId -> groupName /** * Represents a group from the Evolution API response @@ -36,6 +36,9 @@ export class GroupSyncService { // Static property for DB instance injection (defaults to global db) static dbInstance: Database = db; + // In-memory cache for active groups (made public for tests) + public static readonly activeGroupsCache = new Map(); // groupId -> groupName + /** * Gets the sync interval duration in milliseconds. * @@ -199,11 +202,11 @@ export class GroupSyncService { private static cacheActiveGroups(): void { const groups = this.dbInstance.prepare('SELECT id, name FROM groups WHERE active = TRUE').all(); - activeGroupsCache.clear(); + this.activeGroupsCache.clear(); for (const group of groups) { - activeGroupsCache.set(group.id, group.name); + this.activeGroupsCache.set(group.id, group.name); } - console.log(`Cached ${activeGroupsCache.size} active groups`); + console.log(`Cached ${this.activeGroupsCache.size} active groups`); } private static getActiveGroupsCount(): number { @@ -330,6 +333,6 @@ export class GroupSyncService { * @returns True if the group is active, false otherwise. */ static isGroupActive(groupId: string): boolean { - return activeGroupsCache.has(groupId); + return this.activeGroupsCache.has(groupId); } } diff --git a/tests/unit/services/group-sync.test.ts b/tests/unit/services/group-sync.test.ts index 3701a1e..44ee12c 100644 --- a/tests/unit/services/group-sync.test.ts +++ b/tests/unit/services/group-sync.test.ts @@ -149,7 +149,7 @@ describe('GroupSyncService', () => { describe('isGroupActive', () => { beforeEach(() => { // Clear cache and add test groups - GroupSyncService['activeGroupsCache'].clear(); + GroupSyncService.activeGroupsCache.clear(); db.exec('DELETE FROM groups'); db.exec("INSERT INTO groups (id, name, active) VALUES ('active-group', 'Active Group', 1)"); db.exec("INSERT INTO groups (id, name, active) VALUES ('inactive-group', 'Inactive Group', 0)");