web/test/offline-stub.ts25 lines · 819 bytes
| 1 | // First import of offline-selftest.ts: ?mode=offline, and every way out of the machine is a trap that records the |
| 2 | // attempt (fetch, WebSocket, EventSource, XMLHttpRequest). Offline playback must never touch any of them. |
| 3 | const g = globalThis as unknown as Record<string, unknown> |
| 4 | g.location = { search: '?mode=offline', href: 'http://localhost/?mode=offline' } |
| 5 | g.window = globalThis |
| 6 | export const attempts: string[] = [] |
| 7 | g.fetch = (url: unknown) => { |
| 8 | attempts.push(`fetch ${String(url)}`) |
| 9 | return Promise.reject(new Error('offline: no network')) |
| 10 | } |
| 11 | class Trap { |
| 12 | constructor(url: unknown) { |
| 13 | attempts.push(`socket ${String(url)}`) |
| 14 | } |
| 15 | send() {} |
| 16 | close() {} |
| 17 | } |
| 18 | g.WebSocket = Trap |
| 19 | g.EventSource = Trap |
| 20 | g.XMLHttpRequest = class { |
| 21 | open(_m: string, url: string) { |
| 22 | attempts.push(`xhr ${url}`) |
| 23 | } |
| 24 | send() {} |
| 25 | } |