import { describe, it, expect, beforeEach } from 'vitest' import { createSession, getLiveSession, updateLiveSession, deleteLiveSession, getActiveSessions, getActiveSessionByUserId, clearSessions, } from '../../../server/utils/liveSessions.js' describe('liveSessions', () => { let sessionId beforeEach(() => { clearSessions() sessionId = createSession('test-user', 'Test Session').id }) it('creates a session with WebRTC fields', () => { const session = getLiveSession(sessionId) expect(session).toBeDefined() expect(session.id).toBe(sessionId) expect(session.userId).toBe('test-user') expect(session.label).toBe('Test Session') expect(session.routerId).toBeNull() expect(session.producerId).toBeNull() expect(session.transportId).toBeNull() }) it('updates location', () => { updateLiveSession(sessionId, { lat: 37.7, lng: -122.4 }) const session = getLiveSession(sessionId) expect(session.lat).toBe(37.7) expect(session.lng).toBe(-122.4) }) it('updates WebRTC fields', () => { updateLiveSession(sessionId, { routerId: 'router-1', producerId: 'producer-1', transportId: 'transport-1' }) const session = getLiveSession(sessionId) expect(session.routerId).toBe('router-1') expect(session.producerId).toBe('producer-1') expect(session.transportId).toBe('transport-1') }) it('returns hasStream instead of hasSnapshot', async () => { updateLiveSession(sessionId, { producerId: 'producer-1' }) const active = await getActiveSessions() const session = active.find(s => s.id === sessionId) expect(session).toBeDefined() expect(session.hasStream).toBe(true) }) it('returns hasStream false when no producer', async () => { const active = await getActiveSessions() const session = active.find(s => s.id === sessionId) expect(session).toBeDefined() expect(session.hasStream).toBe(false) }) it('deletes a session', () => { deleteLiveSession(sessionId) const session = getLiveSession(sessionId) expect(session).toBeUndefined() }) it('getActiveSessionByUserId returns session for same user when active', () => { const found = getActiveSessionByUserId('test-user') expect(found).toBeDefined() expect(found.id).toBe(sessionId) }) it('getActiveSessionByUserId returns undefined for unknown user', () => { const found = getActiveSessionByUserId('other-user') expect(found).toBeUndefined() }) it('getActiveSessionByUserId returns undefined for expired session', () => { const session = getLiveSession(sessionId) session.updatedAt = Date.now() - 120_000 const found = getActiveSessionByUserId('test-user') expect(found).toBeUndefined() }) it('getActiveSessions removes expired sessions', async () => { const session = getLiveSession(sessionId) session.updatedAt = Date.now() - 120_000 const active = await getActiveSessions() expect(active.find(s => s.id === sessionId)).toBeUndefined() expect(getLiveSession(sessionId)).toBeUndefined() }) })