Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135/** * A Worker the tests can hold. * * jsdom has no `Worker`, which is why the analyzer is an injected dependency and why almost * nothing in the suite needs this. What it does need a worker for is the half of the story * the inline fallback cannot tell: the message that crosses, the answer that comes back * *later*, and the answer that comes back for a question nobody is asking any more. Those * are the parts with the races in them, and a synchronous fallback cannot exercise any of * them. * * So this is a worker with the transport pulled out: `postMessage` answers through the real * `runDetect` unless the test says otherwise, and a test that wants to watch *searching* sit * on screen holds its replies (`auto: false`) and lets them go when it is ready to see what * arrives. */import { runDetect, type DetectReply, type DetectRequest } from "../lib/player/detect.worker";import { count } from "../lib/print";
export class FakeWorker { static instances: FakeWorker[] = []; /** * What a worker built from here on starts out at. The analyzer builds its worker lazily, * when the first analysis is asked for, which is after a booted test has run — so a test * that wants the wire held from the first request holds it here, on the class. */ static auto = true; /** every worker built since the last reset() — the live page has exactly one */ static reset(): void { FakeWorker.instances = []; FakeWorker.auto = true; }
/** what the main thread posted, in order — the protocol, as evidence */ readonly requests: DetectRequest[] = []; /** the last thing posted, before its reply was let go */ lastRequest(): DetectRequest | undefined { return this.requests[this.requests.length - 1]; } onmessage: ((event: { data?: DetectReply }) => void) | null = null; onerror: ((event: unknown) => void) | null = null; /** answer as soon as it is asked, like a worker on a real thread */ auto: boolean; terminates = 0;
private answered = new Set<number | undefined>();
constructor( /** the URL and options the analyzer asked for; kept so a test can assert which file */ readonly url: string | URL, readonly options?: unknown, ) { this.auto = FakeWorker.auto; FakeWorker.instances.push(this); }
postMessage(message: DetectRequest): void { this.requests.push(message); if (this.auto) this.answer(message); }
/** * Answer with something the real search would never say — a refusal, or regions that are * the test's own — as if the worker had produced it. Delivered at once, which is how a test * gets the *second* question answered before the first. */ reply(reply: DetectReply): void { this.answered.add(reply.token); this.deliver(reply); }
/** * Let every request still on the wire have the answer the real search would have given, in * the order they were asked. This is the lever for *searching*: hold the wire with * `auto = false`, look at the bar, then let the answers land. */ flush(): void { for (const request of this.requests) { if (!this.answered.has(request.token)) this.answer(request); } }
/** How many requests are still on the wire. */ get outstanding(): number { return this.requests.filter((r) => !this.answered.has(r.token)).length; }
/** The worker died. `onerror`, which is what the analyzer's fallback hangs off. */ fail(): void { this.onerror?.(new Error("the worker died")); }
terminate(): void { this.terminates++; this.onmessage = null; this.onerror = null; }
private answer(request: DetectRequest): void { this.answered.add(request.token); this.deliver(runDetect(request)); }
private deliver(reply: DetectReply): void { // Synchronous, which is the opposite of a real worker and the only way a test can be // deterministic about it: the answer arrives on the next microtask either way, so the // awaiting code cannot tell the difference, and nothing here is allowed to depend on // *when* the other thread finished. this.onmessage?.({ data: reply }); }}
/** * The structured clone, approximated: what would actually survive being posted. * * Not a clone implementation — a *reader* for the assertion that matters, which is that the * message is the bins and the decisions and nothing else. A `Float32Array` the length of a * track appearing anywhere in here means the samples were posted, which is the mistake this * file's whole reason for existing is written down to prevent: 42 MB copied on the thread * the feature was built to keep free. */export function postedShape(value: unknown, depth = 0): string { if (depth > 4) return "…"; if (ArrayBuffer.isView(value)) return `${value.constructor.name}<${count(value.byteLength, "byte")}>`; if (Array.isArray(value)) return `[${value.map((v) => postedShape(v, depth + 1)).join(",")}]`; if (value instanceof ArrayBuffer) return `ArrayBuffer[${value.byteLength}]`; if (value && typeof value === "object") { const entries = Object.entries(value as Record<string, unknown>) .filter(([, v]) => typeof v !== "function") .map(([k, v]) => `${k}: ${postedShape(v, depth + 1)}`); return `{${entries.join(", ")}}`; } return typeof value;}