import { db, initializeDatabase } from '../../src/db'; import { describe, test, expect, beforeEach } from 'bun:test'; describe('Database', () => { beforeEach(() => { // Reset database between tests db.exec('DROP TABLE IF EXISTS tasks'); db.exec('DROP TABLE IF EXISTS task_assignments'); db.exec('DROP TABLE IF EXISTS users'); db.exec('DROP TABLE IF EXISTS groups'); initializeDatabase(); }); test('should create all required tables with correct schema', () => { // Verify tables exist const tables = db .query("SELECT name FROM sqlite_master WHERE type='table'") .all() .map((t: any) => t.name); const expectedTables = ['groups', 'task_assignments', 'tasks', 'users']; // Filter out system tables like sqlite_sequence const userTables = tables.filter(t => !t.startsWith('sqlite_')); expect(userTables.sort()).toEqual(expectedTables.sort()); // Verify tasks table schema const tasksColumns = db .query("PRAGMA table_info(tasks)") .all() .map((c: any) => c.name); expect(tasksColumns).toContain('description'); expect(tasksColumns).toContain('due_date'); // Verify groups table columns const groupsColumns = db .query("PRAGMA table_info(groups)") .all() .map((c: any) => c.name); expect(groupsColumns).toContain('last_verified'); expect(groupsColumns).toContain('active'); // Verify foreign key constraint const fkInfo = db .query("PRAGMA foreign_key_list(task_assignments)") .all(); expect(fkInfo.length).toBe(1); expect(fkInfo[0].from).toBe('task_id'); expect(fkInfo[0].to).toBe('id'); expect(fkInfo[0].table).toBe('tasks'); // Verify default active status db.exec(` INSERT INTO groups (id, community_id, name) VALUES ('test-group', 'test-community', 'Test Group') `); const group = db.query("SELECT active FROM groups WHERE id = 'test-group'").get(); expect(group.active).toBe(1); // SQLite stores TRUE as 1 }); });