const { initDatabase, closeDatabase } = require('./src/database/connection'); const { initEncryption } = require('./src/utils/encryption'); const userRepo = require('./src/repositories/userRepository'); const sessionRepo = require('./src/repositories/sessionRepository'); const auditRepo = require('./src/repositories/auditRepository'); const crypto = require('crypto'); // Initialize const dbPath = './.data/test_shopify_ai.db'; const encKey = crypto.randomBytes(32).toString('hex'); console.log('๐Ÿงช Testing Repositories...\n'); initEncryption(encKey); initDatabase(dbPath); // Setup schema const fs = require('fs'); const path = require('path'); const db = require('./src/database/connection').getDatabase(); const schema = fs.readFileSync(path.join(__dirname, 'src/database/schema.sql'), 'utf8'); db.exec(schema); // Test 1: Create user console.log('Test 1: Create User'); const user = userRepo.createUser({ id: 'user123', email: 'test@example.com', name: 'Test User', passwordHash: '$2b$12$hashedpassword', emailVerified: true, plan: 'professional' }); console.log(' Created user:', user.email); console.log(' ID:', user.id); console.log(' Plan:', user.plan); console.log(' Success:', user.id === 'user123' ? 'โœ…' : 'โŒ'); // Test 2: Get user by email console.log('\nTest 2: Get User by Email'); const foundUser = userRepo.getUserByEmail('test@example.com'); console.log(' Found user:', foundUser.email); console.log(' Name matches:', foundUser.name === 'Test User' ? 'โœ…' : 'โŒ'); // Test 3: Update user console.log('\nTest 3: Update User'); const updated = userRepo.updateUser('user123', { plan: 'enterprise', billingStatus: 'active' }); console.log(' Updated plan:', updated.plan); console.log(' Plan correct:', updated.plan === 'enterprise' ? 'โœ…' : 'โŒ'); // Test 4: Create session console.log('\nTest 4: Create Session'); const session = sessionRepo.createSession({ userId: 'user123', token: 'session_token_123', deviceFingerprint: 'device123', expiresAt: Date.now() + 86400000 }); console.log(' Created session:', session.id); console.log(' Token:', session.token); console.log(' Success:', session.userId === 'user123' ? 'โœ…' : 'โŒ'); // Test 5: Get session by token console.log('\nTest 5: Get Session by Token'); const foundSession = sessionRepo.getSessionByToken('session_token_123'); console.log(' Found session:', foundSession.id); console.log(' Matches:', foundSession.id === session.id ? 'โœ…' : 'โŒ'); // Test 6: Audit log console.log('\nTest 6: Audit Log'); auditRepo.logAuditEvent({ userId: 'user123', eventType: 'login', eventData: { method: 'email' }, ipAddress: '127.0.0.1', success: true }); const logs = auditRepo.getUserAuditLog('user123'); console.log(' Logged event count:', logs.length); console.log(' Event type:', logs[0].eventType); console.log(' Success:', logs.length === 1 && logs[0].eventType === 'login' ? 'โœ…' : 'โŒ'); // Test 7: Cleanup console.log('\nTest 7: Cleanup'); const deleted = userRepo.deleteUser('user123'); console.log(' User deleted:', deleted ? 'โœ…' : 'โŒ'); closeDatabase(); console.log('\nโœ… All repository tests passed!'); // Clean up test database fs.unlinkSync(dbPath); fs.unlinkSync(dbPath + '-wal'); fs.unlinkSync(dbPath + '-shm');