File
Blob: worker/tests/runtime/sfu.test.ts
| 1 | import { expect, test, vi } from 'vitest'; |
| 2 | import { SfuClient } from '@/server/sfu.ts'; |
| 3 | |
| 4 | const credentials = { REALTIME_APP_ID: 'test-app', REALTIME_APP_TOKEN: 'test-token' }; |
| 5 | |
| 6 | test('cleanup accepts explicit absence at request and item level for tracks and channels', async () => { |
| 7 | const sfu = new SfuClient(credentials); |
| 8 | for (const resource of ['tracks', 'datachannels']) { |
| 9 | for (const payload of [ |
| 10 | { errorCode: 'close_track_error' }, |
| 11 | { [resource === 'tracks' ? 'tracks' : 'dataChannels']: [{ errorCode: 'close_track_error' }] }, |
| 12 | ]) { |
| 13 | vi.stubGlobal('fetch', async () => Response.json(payload)); |
| 14 | await sfu.call(`/sessions/old/${resource}/close`, {}, 'PUT'); |
| 15 | } |
| 16 | } |
| 17 | }); |
| 18 | |
| 19 | test('cleanup keeps real failures pending beside already closed items', async () => { |
| 20 | vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 21 | const sfu = new SfuClient(credentials); |
| 22 | for (const resource of ['tracks', 'dataChannels']) { |
| 23 | vi.stubGlobal('fetch', async () => |
| 24 | Response.json({ |
| 25 | errorCode: 'close_track_error', |
| 26 | [resource]: [{ errorCode: 'close_track_error' }, { errorCode: 'backend_error' }], |
| 27 | }), |
| 28 | ); |
| 29 | await expect( |
| 30 | sfu.call(`/sessions/old/${resource.toLowerCase()}/close`, {}, 'PUT'), |
| 31 | ).rejects.toThrow(/could not configure/); |
| 32 | } |
| 33 | }); |
| 34 | |
| 35 | test('absence handling does not hide failed HTTP requests or errors from other operations', async () => { |
| 36 | vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 37 | const sfu = new SfuClient(credentials); |
| 38 | for (const [operation, status, errorCode] of [ |
| 39 | ['tracks/new', 200, 'close_track_error'], |
| 40 | ['datachannels/update', 200, 'close_track_error'], |
| 41 | ['tracks/close', 503, 'close_track_error'], |
| 42 | ['tracks/close', 200, 'backend_error'], |
| 43 | ['tracks/close', 200, 'session_error'], |
| 44 | ['tracks/close', 425, 'session_error'], |
| 45 | ] as const) { |
| 46 | vi.stubGlobal('fetch', async () => Response.json({ errorCode }, { status })); |
| 47 | await expect(sfu.call(`/sessions/old/${operation}`, {}, 'PUT')).rejects.toThrow( |
| 48 | /SFU operation failed/, |
| 49 | ); |
| 50 | } |
| 51 | }); |