You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			90 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			TypeScript
		
	
			
		
		
	
	
			90 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			TypeScript
		
	
| 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 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)")
 | |
|       .all()
 | |
|       .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 foreign key constraints
 | |
|     const taskAssignmentsFk = db
 | |
|       .query("PRAGMA foreign_key_list(task_assignments)")
 | |
|       .all();
 | |
|     expect(taskAssignmentsFk.length).toBe(1);
 | |
|     expect(taskAssignmentsFk[0].from).toBe('task_id');
 | |
|     expect(taskAssignmentsFk[0].to).toBe('id');
 | |
|     expect(taskAssignmentsFk[0].table).toBe('tasks');
 | |
| 
 | |
|     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);
 | |
| 
 | |
|     // 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);
 | |
|   });
 | |
| });
 |