|
|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
import { describe, it, expect, beforeEach, afterAll, mock } from 'bun:test';
|
|
|
|
|
import { GroupSyncService } from '../../../src/services/group-sync';
|
|
|
|
|
import { db } from '../../../src/db';
|
|
|
|
|
import { Database } from 'bun:sqlite';
|
|
|
|
|
import { initializeDatabase } from '../../../src/db';
|
|
|
|
|
|
|
|
|
|
// Store original globals
|
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
|
|
|
@ -8,11 +10,19 @@ const originalConsoleError = console.error;
|
|
|
|
|
|
|
|
|
|
describe('GroupSyncService', () => {
|
|
|
|
|
let fetchMock: any;
|
|
|
|
|
let testDb: Database;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
db.exec('DELETE FROM groups');
|
|
|
|
|
db.exec('DELETE FROM sqlite_sequence WHERE name="groups"');
|
|
|
|
|
testDb.exec('DELETE FROM groups');
|
|
|
|
|
testDb.exec('DELETE FROM sqlite_sequence WHERE name="groups"');
|
|
|
|
|
GroupSyncService['lastSyncAttempt'] = 0;
|
|
|
|
|
|
|
|
|
|
// Setup mock fetch to return proper Response object
|
|
|
|
|
@ -72,7 +82,7 @@ describe('GroupSyncService', () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Verify database state
|
|
|
|
|
const groups = db.query('SELECT * FROM groups').all();
|
|
|
|
|
const groups = testDb.query('SELECT * FROM groups').all();
|
|
|
|
|
expect(groups).toHaveLength(1);
|
|
|
|
|
|
|
|
|
|
const group = groups[0];
|
|
|
|
|
@ -88,7 +98,7 @@ describe('GroupSyncService', () => {
|
|
|
|
|
|
|
|
|
|
it('should update existing groups', async () => {
|
|
|
|
|
// Add initial group
|
|
|
|
|
db.exec(
|
|
|
|
|
testDb.exec(
|
|
|
|
|
"INSERT INTO groups (id, community_id, name, active) VALUES ('group1', 'test-community', 'Old Name', 1)"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
@ -98,7 +108,7 @@ describe('GroupSyncService', () => {
|
|
|
|
|
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({
|
|
|
|
|
id: 'group1',
|
|
|
|
|
community_id: 'test-community',
|
|
|
|
|
@ -110,12 +120,12 @@ describe('GroupSyncService', () => {
|
|
|
|
|
|
|
|
|
|
it('should mark non-matching groups as inactive', async () => {
|
|
|
|
|
// 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')"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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({
|
|
|
|
|
id: 'old-group',
|
|
|
|
|
community_id: 'test-community',
|
|
|
|
|
@ -150,9 +160,9 @@ describe('GroupSyncService', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
// Clear cache and add test groups
|
|
|
|
|
GroupSyncService.activeGroupsCache.clear();
|
|
|
|
|
db.exec('DELETE FROM groups');
|
|
|
|
|
db.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('DELETE FROM groups');
|
|
|
|
|
testDb.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 ('inactive-group', 'test-community', 'Inactive Group', 0)");
|
|
|
|
|
// Populate cache
|
|
|
|
|
GroupSyncService['cacheActiveGroups']();
|
|
|
|
|
});
|
|
|
|
|
|