|
| 1 | +import { expect, test } from "bun:test" |
| 2 | +import type { Claim, Degraded, PlacementKind, Slotted, Suppressed } from "../src/plugin/structure" |
| 3 | + |
| 4 | +function claim(kind: PlacementKind, target: string, id: string): Claim<string> { |
| 5 | + return { key: `p/${id}`, plugin: "p", placement: { kind, target }, render: id } |
| 6 | +} |
| 7 | + |
| 8 | +type SerializedResolution = { |
| 9 | + readonly slotted: ReadonlyArray<[string, Slotted<string>]> |
| 10 | + readonly suppressed: ReadonlyArray<Suppressed<string>> |
| 11 | + readonly degraded: ReadonlyArray<Degraded<string>> |
| 12 | +} |
| 13 | + |
| 14 | +// Run resolveSlots off the main thread so a synchronous infinite loop fails |
| 15 | +// via the timeout instead of blocking the test runner. |
| 16 | +async function resolveWithTimeout( |
| 17 | + paths: ReadonlyArray<string>, |
| 18 | + claims: ReadonlyArray<Claim<string>>, |
| 19 | + timeoutMs = 5_000, |
| 20 | +): Promise<SerializedResolution> { |
| 21 | + const moduleUrl = new URL("../src/plugin/structure.ts", import.meta.url).href |
| 22 | + const source = ` |
| 23 | + import { resolveSlots } from ${JSON.stringify(moduleUrl)} |
| 24 | + self.onmessage = (event) => { |
| 25 | + const result = resolveSlots({ paths: new Set(event.data.paths), claims: event.data.claims }) |
| 26 | + self.postMessage({ |
| 27 | + slotted: [...result.slotted.entries()], |
| 28 | + suppressed: result.suppressed, |
| 29 | + degraded: result.degraded, |
| 30 | + }) |
| 31 | + } |
| 32 | + ` |
| 33 | + const blobUrl = URL.createObjectURL(new Blob([source], { type: "text/javascript" })) |
| 34 | + const worker = new Worker(blobUrl) |
| 35 | + let timer: ReturnType<typeof setTimeout> | undefined |
| 36 | + try { |
| 37 | + return await new Promise<SerializedResolution>((resolve, reject) => { |
| 38 | + timer = setTimeout(() => reject(new Error(`resolveSlots did not return within ${timeoutMs}ms`)), timeoutMs) |
| 39 | + worker.onmessage = (event) => resolve(event.data as SerializedResolution) |
| 40 | + worker.onerror = (event) => reject(event.error ?? new Error(event.message)) |
| 41 | + worker.postMessage({ paths, claims }) |
| 42 | + }) |
| 43 | + } finally { |
| 44 | + clearTimeout(timer) |
| 45 | + worker.terminate() |
| 46 | + URL.revokeObjectURL(blobUrl) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +test("suppresses slot claims targeting paths with empty segments without hanging", async () => { |
| 51 | + const invalid = [ |
| 52 | + claim("append", ".x", "leading"), |
| 53 | + claim("after", ".", "dot"), |
| 54 | + claim("before", ".side.child", "leading-nested"), |
| 55 | + claim("prepend", "", "empty"), |
| 56 | + claim("append", "side.", "trailing"), |
| 57 | + claim("append", "side..child", "double-dot"), |
| 58 | + claim("replace", "side..child", "replace-double-dot"), |
| 59 | + claim("replace", ".x", "replace-leading"), |
| 60 | + ] |
| 61 | + const direct = claim("append", "side", "direct") |
| 62 | + const degraded = claim("after", "side.missing", "degraded") |
| 63 | + |
| 64 | + const result = await resolveWithTimeout(["side", "side.child"], [...invalid, direct, degraded]) |
| 65 | + |
| 66 | + expect(result.slotted).toEqual([ |
| 67 | + [ |
| 68 | + "side", |
| 69 | + { |
| 70 | + before: [], |
| 71 | + prepend: [], |
| 72 | + append: [direct, degraded], |
| 73 | + after: [], |
| 74 | + }, |
| 75 | + ], |
| 76 | + ]) |
| 77 | + expect(result.degraded).toEqual([{ claim: degraded, to: "side" }]) |
| 78 | + expect(result.suppressed).toEqual([...invalid.slice(6), ...invalid.slice(0, 6)].map((item) => ({ claim: item }))) |
| 79 | +}) |
0 commit comments