test: aislar base de datos en pruebas de group-sync

Co-authored-by: aider (openrouter/x-ai/grok-code-fast-1) <aider@aider.chat>
pull/1/head
borja 3 months ago
parent 378b3f1895
commit 837a9f5cc5

@ -1,6 +1,8 @@
import { describe, it, expect, beforeEach, afterAll, mock } from 'bun:test'; import { describe, it, expect, beforeEach, afterAll, mock } from 'bun:test';
import { GroupSyncService } from '../../../src/services/group-sync'; import { GroupSyncService } from '../../../src/services/group-sync';
import { db } from '../../../src/db'; import { db } from '../../../src/db';
import { Database } from 'bun:sqlite';
import { initializeDatabase } from '../../../src/db';
// Store original globals // Store original globals
const originalFetch = globalThis.fetch; const originalFetch = globalThis.fetch;
@ -8,11 +10,19 @@ const originalConsoleError = console.error;
describe('GroupSyncService', () => { describe('GroupSyncService', () => {
let fetchMock: any; let fetchMock: any;
let testDb: Database;
beforeEach(() => { beforeEach(() => {
// Create a new in-memory database for each test
testDb = new Database(':memory:');
initializeDatabase(testDb);
// Assign the isolated DB to the service
GroupSyncService.dbInstance = testDb;
// Clear and reset test data // Clear and reset test data
db.exec('DELETE FROM groups'); testDb.exec('DELETE FROM groups');
db.exec('DELETE FROM sqlite_sequence WHERE name="groups"'); testDb.exec('DELETE FROM sqlite_sequence WHERE name="groups"');
GroupSyncService['lastSyncAttempt'] = 0; GroupSyncService['lastSyncAttempt'] = 0;
// Setup mock fetch to return proper Response object // Setup mock fetch to return proper Response object
@ -72,7 +82,7 @@ describe('GroupSyncService', () => {
}); });
// Verify database state // Verify database state
const groups = db.query('SELECT * FROM groups').all(); const groups = testDb.query('SELECT * FROM groups').all();
expect(groups).toHaveLength(1); expect(groups).toHaveLength(1);
const group = groups[0]; const group = groups[0];
@ -88,7 +98,7 @@ describe('GroupSyncService', () => {
it('should update existing groups', async () => { it('should update existing groups', async () => {
// Add initial group // Add initial group
db.exec( testDb.exec(
"INSERT INTO groups (id, community_id, name, active) VALUES ('group1', 'test-community', 'Old Name', 1)" "INSERT INTO groups (id, community_id, name, active) VALUES ('group1', 'test-community', 'Old Name', 1)"
); );
@ -98,7 +108,7 @@ describe('GroupSyncService', () => {
updated: 1 updated: 1
}); });
const group = db.query('SELECT * FROM groups WHERE id = ?').get('group1'); const group = testDb.query('SELECT * FROM groups WHERE id = ?').get('group1');
expect(group).toEqual({ expect(group).toEqual({
id: 'group1', id: 'group1',
community_id: 'test-community', community_id: 'test-community',
@ -110,12 +120,12 @@ describe('GroupSyncService', () => {
it('should mark non-matching groups as inactive', async () => { it('should mark non-matching groups as inactive', async () => {
// Add initial group not in current sync // Add initial group not in current sync
db.exec( testDb.exec(
"INSERT INTO groups (id, community_id, name, active, last_verified) VALUES ('old-group', 'test-community', 'Old Group', 1, '2023-01-01')" "INSERT INTO groups (id, community_id, name, active, last_verified) VALUES ('old-group', 'test-community', 'Old Group', 1, '2023-01-01')"
); );
await GroupSyncService.syncGroups(); await GroupSyncService.syncGroups();
const group = db.query('SELECT * FROM groups WHERE id = ?').get('old-group'); const group = testDb.query('SELECT * FROM groups WHERE id = ?').get('old-group');
expect(group).toEqual({ expect(group).toEqual({
id: 'old-group', id: 'old-group',
community_id: 'test-community', community_id: 'test-community',
@ -150,9 +160,9 @@ describe('GroupSyncService', () => {
beforeEach(() => { beforeEach(() => {
// Clear cache and add test groups // Clear cache and add test groups
GroupSyncService.activeGroupsCache.clear(); GroupSyncService.activeGroupsCache.clear();
db.exec('DELETE FROM groups'); testDb.exec('DELETE FROM groups');
db.exec("INSERT INTO groups (id, community_id, name, active) VALUES ('active-group', 'test-community', 'Active Group', 1)"); testDb.exec("INSERT INTO groups (id, community_id, name, active) VALUES ('active-group', 'test-community', 'Active Group', 1)");
db.exec("INSERT INTO groups (id, community_id, name, active) VALUES ('inactive-group', 'test-community', 'Inactive Group', 0)"); testDb.exec("INSERT INTO groups (id, community_id, name, active) VALUES ('inactive-group', 'test-community', 'Inactive Group', 0)");
// Populate cache // Populate cache
GroupSyncService['cacheActiveGroups'](); GroupSyncService['cacheActiveGroups']();
}); });

Loading…
Cancel
Save