// The seeder tests' fixture (controller/seeder.test.ts, webrtcScenario.ts): a // tracker on 127.0.0.1 tracking one synthetic playable torrent, and WebTorrent // clients that find peers through it and nowhere else. import { mkdir, mkdtemp, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import WebTorrent from "webtorrent"; import { DEFAULT_SEEDER_SETTINGS, type SeederSettings } from "../../lib/seederSettings"; import { makePlayableTorrent } from "../../controller/preparePlayable"; import { startTracker, type SeedTarget } from "../../controller/seeder"; export const SEED_ID = "aaaaaaaaaaa"; export async function until(pred: () => boolean, ms: number, what: string): Promise { const t0 = Date.now(); while (!pred()) { if (Date.now() - t0 > ms) throw new Error(`timed out waiting for ${what}`); await new Promise((r) => setTimeout(r, 100)); } } export const newClient = () => new WebTorrent({ dht: false, lsd: false, natUpnp: false, natPmp: false, utp: false, webSeeds: false, tracker: { rtcConfig: { iceServers: [] } }, }); export const destroyClient = (c: WebTorrent) => c.destroyed ? Promise.resolve() : new Promise((resolve) => c.destroy(() => resolve())); export async function seederFixture(trackerUrl: (port: number) => string) { const dir = await mkdtemp(path.join(tmpdir(), "ttb-seeder-")); const seedDir = path.join(dir, "playable", "demo", SEED_ID); await mkdir(seedDir, { recursive: true }); const bytes = Buffer.from(Array.from({ length: 600_000 }, (_, i) => (i * 13 + 7) & 0xff)); const file = path.join(seedDir, `${SEED_ID}.mp4`); await writeFile(file, bytes); let allowed = new Set(); const tracker = await startTracker({ host: "127.0.0.1", port: 0, allowed: () => allowed }); const made = await makePlayableTorrent({ file, name: `${SEED_ID}.mp4`, bytes: bytes.length, trackers: [trackerUrl(tracker.port)] }); allowed = new Set([made.infoHash]); const torrentPath = path.join(seedDir, `${SEED_ID}.torrent`); await writeFile(torrentPath, made.torrent); const target: SeedTarget = { slug: "demo", id: SEED_ID, infoHash: made.infoHash, torrentPath, file, dir: seedDir, bytes: bytes.length }; return { dir, bytes, tracker, made, target }; } export function seederSettings(): SeederSettings { return { ...DEFAULT_SEEDER_SETTINGS, sites: ["demo-site"], maxConnections: 10 }; } // The flags a child of a test file should run with: this process's (the tsx // loader), less the test runner's own — `--test-name-pattern=x` and // `--test-name-pattern x` alike (the value too). The flags that take no value // are named; every other `--test-*` written without `=` takes the next word. const TEST_FLAGS_WITHOUT_VALUE = new Set(["--test", "--test-only", "--test-force-exit", "--test-update-snapshots"]); export function childExecArgv(execArgv: readonly string[] = process.execArgv): string[] { const out: string[] = []; for (let i = 0; i < execArgv.length; i += 1) { const a = execArgv[i]; if (!a.startsWith("--test") && !a.startsWith("--experimental-test")) { out.push(a); continue; } if (!a.includes("=") && !TEST_FLAGS_WITHOUT_VALUE.has(a) && !a.startsWith("--experimental-test") && i + 1 < execArgv.length && !execArgv[i + 1].startsWith("-")) i += 1; } return out; }