Skip to content
File

Blob: worker/tests/helpers/sfu.mjs

javascript48 lines
1import fs from 'node:fs/promises';
2import { Miniflare, convertV4MiniflareOptions } from 'miniflare';
3import { bindings, createRadioClient, createSfu } from './sfu-fixture.ts';
4 
5export { offer, answer, track, nowPlaying, device } from './sfu-fixture.ts';
6 
7// The production bundle runs with its production compatibility flags, without
8// the Node compatibility added by the source-test runner.
9export async function createHarness(t, respond) {
10 const sfu = createSfu(respond);
11 const mf = new Miniflare(
12 convertV4MiniflareOptions({
13 workers: [
14 {
15 name: 'radio-test',
16 modules: [
17 {
18 type: 'ESModule',
19 path: 'worker.js',
20 contents: await fs.readFile(
21 new URL('../../dist/esp32_radio/index.js', import.meta.url),
22 'utf8',
23 ),
24 },
25 ],
26 compatibilityDate: '2026-09-12',
27 bindings,
28 durableObjects: { ROBOTS: { className: 'RobotRoom', useSQLite: true } },
29 outboundService: (request) => sfu.fetch(request),
30 },
31 ],
32 }),
33 );
34 t.after(() => mf.dispose());
35 return {
36 ...createRadioClient(async (request) =>
37 mf.dispatchFetch(request.url, {
38 method: request.method,
39 headers: Object.fromEntries(request.headers),
40 body: request.body ? await request.arrayBuffer() : undefined,
41 }),
42 ),
43 calls: sfu.calls,
44 allocations: sfu.allocations,
45 evict: () => mf.unsafeEvictDurableObject('radio-test', 'RobotRoom', { name: 'test-board' }),
46 };
47}