This commit is contained in:
@@ -17,18 +17,20 @@ vi.mock('../../../server/utils/mediasoup.js', () => ({
|
||||
}))
|
||||
|
||||
describe('liveSessions', () => {
|
||||
let sessionId
|
||||
const testState = {
|
||||
sessionId: null,
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
clearSessions()
|
||||
const session = await createSession('test-user', 'Test Session')
|
||||
sessionId = session.id
|
||||
testState.sessionId = session.id
|
||||
})
|
||||
|
||||
it('creates a session with WebRTC fields', () => {
|
||||
const session = getLiveSession(sessionId)
|
||||
const session = getLiveSession(testState.sessionId)
|
||||
expect(session).toBeDefined()
|
||||
expect(session.id).toBe(sessionId)
|
||||
expect(session.id).toBe(testState.sessionId)
|
||||
expect(session.userId).toBe('test-user')
|
||||
expect(session.label).toBe('Test Session')
|
||||
expect(session.routerId).toBeNull()
|
||||
@@ -37,45 +39,45 @@ describe('liveSessions', () => {
|
||||
})
|
||||
|
||||
it('updates location', async () => {
|
||||
await updateLiveSession(sessionId, { lat: 37.7, lng: -122.4 })
|
||||
const session = getLiveSession(sessionId)
|
||||
await updateLiveSession(testState.sessionId, { lat: 37.7, lng: -122.4 })
|
||||
const session = getLiveSession(testState.sessionId)
|
||||
expect(session.lat).toBe(37.7)
|
||||
expect(session.lng).toBe(-122.4)
|
||||
})
|
||||
|
||||
it('updates WebRTC fields', async () => {
|
||||
await updateLiveSession(sessionId, { routerId: 'router-1', producerId: 'producer-1', transportId: 'transport-1' })
|
||||
const session = getLiveSession(sessionId)
|
||||
await updateLiveSession(testState.sessionId, { routerId: 'router-1', producerId: 'producer-1', transportId: 'transport-1' })
|
||||
const session = getLiveSession(testState.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 () => {
|
||||
await updateLiveSession(sessionId, { producerId: 'producer-1' })
|
||||
await updateLiveSession(testState.sessionId, { producerId: 'producer-1' })
|
||||
const active = await getActiveSessions()
|
||||
const session = active.find(s => s.id === sessionId)
|
||||
const session = active.find(s => s.id === testState.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)
|
||||
const session = active.find(s => s.id === testState.sessionId)
|
||||
expect(session).toBeDefined()
|
||||
expect(session.hasStream).toBe(false)
|
||||
})
|
||||
|
||||
it('deletes a session', async () => {
|
||||
await deleteLiveSession(sessionId)
|
||||
const session = getLiveSession(sessionId)
|
||||
await deleteLiveSession(testState.sessionId)
|
||||
const session = getLiveSession(testState.sessionId)
|
||||
expect(session).toBeUndefined()
|
||||
})
|
||||
|
||||
it('getActiveSessionByUserId returns session for same user when active', async () => {
|
||||
const found = await getActiveSessionByUserId('test-user')
|
||||
expect(found).toBeDefined()
|
||||
expect(found.id).toBe(sessionId)
|
||||
expect(found.id).toBe(testState.sessionId)
|
||||
})
|
||||
|
||||
it('getActiveSessionByUserId returns undefined for unknown user', async () => {
|
||||
@@ -84,18 +86,18 @@ describe('liveSessions', () => {
|
||||
})
|
||||
|
||||
it('getActiveSessionByUserId returns undefined for expired session', async () => {
|
||||
const session = getLiveSession(sessionId)
|
||||
const session = getLiveSession(testState.sessionId)
|
||||
session.updatedAt = Date.now() - 120_000
|
||||
const found = await getActiveSessionByUserId('test-user')
|
||||
expect(found).toBeUndefined()
|
||||
})
|
||||
|
||||
it('getActiveSessions removes expired sessions', async () => {
|
||||
const session = getLiveSession(sessionId)
|
||||
const session = getLiveSession(testState.sessionId)
|
||||
session.updatedAt = Date.now() - 120_000
|
||||
const active = await getActiveSessions()
|
||||
expect(active.find(s => s.id === sessionId)).toBeUndefined()
|
||||
expect(getLiveSession(sessionId)).toBeUndefined()
|
||||
expect(active.find(s => s.id === testState.sessionId)).toBeUndefined()
|
||||
expect(getLiveSession(testState.sessionId)).toBeUndefined()
|
||||
})
|
||||
|
||||
it('getActiveSessions runs cleanup for expired session with producer and transport', async () => {
|
||||
@@ -105,19 +107,19 @@ describe('liveSessions', () => {
|
||||
getProducer.mockReturnValue(mockProducer)
|
||||
getTransport.mockReturnValue(mockTransport)
|
||||
closeRouter.mockResolvedValue(undefined)
|
||||
await updateLiveSession(sessionId, { producerId: 'p1', transportId: 't1', routerId: 'r1' })
|
||||
const session = getLiveSession(sessionId)
|
||||
await updateLiveSession(testState.sessionId, { producerId: 'p1', transportId: 't1', routerId: 'r1' })
|
||||
const session = getLiveSession(testState.sessionId)
|
||||
session.updatedAt = Date.now() - 120_000
|
||||
const active = await getActiveSessions()
|
||||
expect(active.find(s => s.id === sessionId)).toBeUndefined()
|
||||
expect(active.find(s => s.id === testState.sessionId)).toBeUndefined()
|
||||
expect(mockProducer.close).toHaveBeenCalled()
|
||||
expect(mockTransport.close).toHaveBeenCalled()
|
||||
expect(closeRouter).toHaveBeenCalledWith(sessionId)
|
||||
expect(closeRouter).toHaveBeenCalledWith(testState.sessionId)
|
||||
})
|
||||
|
||||
it('getOrCreateSession returns existing active session', async () => {
|
||||
const session = await getOrCreateSession('test-user', 'New Label')
|
||||
expect(session.id).toBe(sessionId)
|
||||
expect(session.id).toBe(testState.sessionId)
|
||||
expect(session.userId).toBe('test-user')
|
||||
})
|
||||
|
||||
@@ -128,10 +130,9 @@ describe('liveSessions', () => {
|
||||
})
|
||||
|
||||
it('getOrCreateSession handles concurrent calls atomically', async () => {
|
||||
const promises = []
|
||||
for (let i = 0; i < 5; i++) {
|
||||
promises.push(getOrCreateSession('concurrent-user', 'Concurrent'))
|
||||
}
|
||||
const promises = Array.from({ length: 5 }, () =>
|
||||
getOrCreateSession('concurrent-user', 'Concurrent'),
|
||||
)
|
||||
const sessions = await Promise.all(promises)
|
||||
const uniqueIds = new Set(sessions.map(s => s.id))
|
||||
expect(uniqueIds.size).toBe(1)
|
||||
|
||||
Reference in New Issue
Block a user