session.mjs 1.4 KB

1234567891011121314151617181920212223242526272829303132
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { createVideoLiveSession } from '../../src/modules/cockpit/services/videoLiveSession.js'
  4. const data = (code) => ({ deviceCode: code, sessionId: `session-${code}`, sessionToken: `owner-${code}`, mode: 'LIVE', media: { native: true, url: `https://media.invalid/${code}.mp4` } })
  5. test('simultaneous open commands never create a video for a superseded device', async () => {
  6. const requests = [], updates = []
  7. const live = createVideoLiveSession({
  8. start: async (code) => { requests.push(code); return data(code) },
  9. heartbeat: async () => {}, stop: async () => {},
  10. }, (patch) => updates.push(patch))
  11. try {
  12. await Promise.all([live.start('A'), live.start('B')])
  13. assert.deepEqual(requests, ['B'])
  14. assert.equal(updates.some((patch) => patch.media?.url.endsWith('/A.mp4')), false)
  15. } finally { await live.dispose() }
  16. })
  17. test('closing in the same turn cancels an open before it can start server media', async () => {
  18. const requests = []
  19. const live = createVideoLiveSession({
  20. start: async (code) => { requests.push(code); return data(code) },
  21. heartbeat: async () => {}, stop: async () => {},
  22. }, () => {})
  23. try {
  24. const opening = live.start('A')
  25. await live.stop()
  26. await opening
  27. assert.deepEqual(requests, [])
  28. } finally { await live.dispose() }
  29. })