From b00fa4ac2334b2b3aace419ce0d84185a69f553b Mon Sep 17 00:00:00 2001 From: "borja (aider)" Date: Mon, 31 Mar 2025 14:01:11 +0200 Subject: [PATCH] test: update db tests for new schema with users table --- tests/unit/db.test.ts | 62 ++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/tests/unit/db.test.ts b/tests/unit/db.test.ts index 91ca8c3..8d16565 100644 --- a/tests/unit/db.test.ts +++ b/tests/unit/db.test.ts @@ -23,6 +23,17 @@ describe('Database', () => { const userTables = tables.filter(t => !t.startsWith('sqlite_')); expect(userTables.sort()).toEqual(expectedTables.sort()); + // Verify users table schema + const usersColumns = db + .query("PRAGMA table_info(users)") + .all() + .map((c: any) => c.name); + expect(usersColumns).toEqual([ + 'id', + 'first_seen', + 'last_seen' + ]); + // Verify tasks table schema const tasksColumns = db .query("PRAGMA table_info(tasks)") @@ -30,30 +41,49 @@ describe('Database', () => { .map((c: any) => c.name); expect(tasksColumns).toContain('description'); expect(tasksColumns).toContain('due_date'); + expect(tasksColumns).toContain('group_id'); + expect(tasksColumns).toContain('created_by'); - // 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 + // Verify foreign key constraints + const taskAssignmentsFk = 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'); + expect(taskAssignmentsFk.length).toBe(1); + expect(taskAssignmentsFk[0].from).toBe('task_id'); + expect(taskAssignmentsFk[0].to).toBe('id'); + expect(taskAssignmentsFk[0].table).toBe('tasks'); - // Verify default active status + const tasksFk = db + .query("PRAGMA foreign_key_list(tasks)") + .all(); + expect(tasksFk.length).toBe(1); + expect(tasksFk[0].from).toBe('created_by'); + expect(tasksFk[0].to).toBe('id'); + expect(tasksFk[0].table).toBe('users'); + + // Test data operations db.exec(` + INSERT INTO users (id) VALUES ('34650112233'); INSERT INTO groups (id, community_id, name) VALUES ('test-group', 'test-community', 'Test Group') `); + + // Verify group defaults const group = db.query("SELECT active FROM groups WHERE id = 'test-group'").get(); - expect(group.active).toBe(1); // SQLite stores TRUE as 1 + expect(group.active).toBe(1); + + // Verify task insertion with proper user reference + const taskResult = db.prepare(` + INSERT INTO tasks (description, created_by, group_id) + VALUES ('Test task', '34650112233', 'test-group') + `).run(); + expect(taskResult.changes).toBe(1); + + // Verify private task insertion (no group_id) + const privateTaskResult = db.prepare(` + INSERT INTO tasks (description, created_by) + VALUES ('Private task', '34650112233') + `).run(); + expect(privateTaskResult.changes).toBe(1); }); });