@isoloop/dom 0.0.0 → 0.1.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,699 @@
1
+ import { h as WParentNode, o as WElement, t as WCharacterData, v as createDom } from "./dom-yLsf0Z4e.js";
2
+ import { t as createApplier } from "./applier-D5wb4gz9.js";
3
+ import { afterEach, describe, expect, it } from "vitest";
4
+ import { commands } from "vitest/browser";
5
+ // src/testing/conformance.ts
6
+ /** Cases a bridge may declare as known gaps; they run as expected failures. */
7
+ const CASES = {
8
+ create: "creates elements, attributes and text",
9
+ update: "updates attributes and text, and removes attributes",
10
+ move: "moves a node",
11
+ remove: "removes nodes",
12
+ keyed: "keyed list reorders keep node identity and focus",
13
+ sanitise: "sanitises tags, attributes and URLs (I9)",
14
+ ids: "maps node ids both ways",
15
+ events: "registers listeners and round-trips events",
16
+ noListener: "sends nothing for a node without a listener",
17
+ inputs: "host-owned inputs keep the live value while focused",
18
+ styles: "applies inline styles",
19
+ malformed: "rejects a malformed batch and keeps the tree",
20
+ classList: "classList reads and writes the class attribute",
21
+ dataset: "dataset reads and writes data-* attributes",
22
+ getElementById: "getElementById finds rendered elements by id",
23
+ contains: "contains walks the isolate tree",
24
+ fragment: "inserting a fragment inserts its children in order and empties it",
25
+ comment: "comment nodes round-trip, and their data updates",
26
+ forced: "a forced value replaces a focused input’s value, caret at the end"
27
+ };
28
+ /**
29
+ * The suite every render bridge must pass, with no known gaps for the
30
+ * isoloop-dom bridge. Known gaps can be marked per bridge. Batches go through
31
+ * `structuredClone`, as they would through `postMessage`.
32
+ */
33
+ function runBridgeConformance(name, makePair, opts = {}) {
34
+ const gaps = new Set(opts.knownGaps ?? []);
35
+ const cleanups = [];
36
+ afterEach(() => {
37
+ for (const off of cleanups.splice(0)) off();
38
+ });
39
+ const setup = () => {
40
+ const { host, isolate, internalAttr = () => false } = makePair();
41
+ const shadowHost = document.createElement("div");
42
+ document.body.append(shadowHost);
43
+ const shadow = shadowHost.attachShadow({ mode: "open" });
44
+ const sent = [];
45
+ host.mount(shadow, (e) => sent.push(e));
46
+ cleanups.push(() => {
47
+ host.unmount();
48
+ shadowHost.remove();
49
+ });
50
+ const sync = () => {
51
+ for (const batch of isolate.flush()) expect(host.apply(structuredClone(batch))).toBe(true);
52
+ };
53
+ sync();
54
+ const root = () => {
55
+ const el = shadow.querySelector(".cf-root");
56
+ if (!el) throw new Error("no .cf-root on the host");
57
+ return el;
58
+ };
59
+ const html = () => serialize(root(), internalAttr);
60
+ const find = (selector) => {
61
+ const el = root().querySelector(selector);
62
+ if (!el) throw new Error(`no ${selector} on the host`);
63
+ return el;
64
+ };
65
+ return {
66
+ host,
67
+ isolate,
68
+ shadow,
69
+ sent,
70
+ sync,
71
+ root,
72
+ html,
73
+ find
74
+ };
75
+ };
76
+ const test = (key, fn) => gaps.has(key) ? it.fails(`${CASES[key]} (known gap)`, fn) : it(CASES[key], fn);
77
+ describe(`render bridge conformance: ${name}`, () => {
78
+ test("create", () => {
79
+ const { isolate: d, sync, html } = setup();
80
+ const p = build(d, "p", ["hello ", build(d, "strong", ["world"])]);
81
+ d.setAttr(p, "class", "greeting");
82
+ d.setAttr(p, "title", "Hi");
83
+ d.insert(d.root, p);
84
+ d.insert(d.root, build(d, "ul", [build(d, "li", ["a"]), build(d, "li", ["b"])]));
85
+ sync();
86
+ expect(html()).toBe("<p class=\"greeting\" title=\"Hi\">hello <strong>world</strong></p><ul><li>a</li><li>b</li></ul>");
87
+ });
88
+ test("update", () => {
89
+ const { isolate: d, sync, html } = setup();
90
+ const t = d.text("one");
91
+ const span = build(d, "span", [t]);
92
+ d.setAttr(span, "class", "a");
93
+ d.setAttr(span, "title", "x");
94
+ d.insert(d.root, span);
95
+ sync();
96
+ d.setText(t, "two");
97
+ d.setAttr(span, "class", "b");
98
+ d.setAttr(span, "title", null);
99
+ sync();
100
+ expect(html()).toBe("<span class=\"b\">two</span>");
101
+ });
102
+ test("move", () => {
103
+ const { isolate: d, sync, html } = setup();
104
+ const a = build(d, "span", ["a"]);
105
+ const b = build(d, "span", ["b"]);
106
+ const c = build(d, "span", ["c"]);
107
+ for (const x of [
108
+ a,
109
+ b,
110
+ c
111
+ ]) d.insert(d.root, x);
112
+ sync();
113
+ d.insert(d.root, c, a);
114
+ sync();
115
+ expect(html()).toBe("<span>c</span><span>a</span><span>b</span>");
116
+ });
117
+ test("remove", () => {
118
+ const { isolate: d, sync, html } = setup();
119
+ const t = d.text("gone");
120
+ const keep = build(d, "p", ["kept", t]);
121
+ const drop = build(d, "p", ["dropped"]);
122
+ d.insert(d.root, keep);
123
+ d.insert(d.root, drop);
124
+ sync();
125
+ d.remove(drop);
126
+ d.remove(t);
127
+ sync();
128
+ expect(html()).toBe("<p>kept</p>");
129
+ });
130
+ test("keyed", () => {
131
+ const { isolate: d, sync, root, shadow } = setup();
132
+ const ul = d.el("ul");
133
+ const items = [
134
+ "a",
135
+ "b",
136
+ "c"
137
+ ].map((k) => {
138
+ const input = d.el("input");
139
+ d.setAttr(input, "title", k);
140
+ return build(d, "li", [k, input]);
141
+ });
142
+ for (const li of items) d.insert(ul, li);
143
+ d.insert(d.root, ul);
144
+ sync();
145
+ const before = [...root().querySelectorAll("li")];
146
+ const input = before[2]?.querySelector("input");
147
+ if (!(input instanceof HTMLInputElement)) throw new Error("no input");
148
+ input.focus();
149
+ expect(shadow.activeElement).toBe(input);
150
+ const [first, , last] = items;
151
+ if (!first || !last) throw new Error("no items");
152
+ d.insert(ul, last, first);
153
+ sync();
154
+ const after = [...root().querySelectorAll("li")];
155
+ expect(after.map((li) => li.textContent)).toEqual([
156
+ "c",
157
+ "a",
158
+ "b"
159
+ ]);
160
+ expect(after[0]).toBe(before[2]);
161
+ expect(shadow.activeElement).toBe(input);
162
+ });
163
+ test("sanitise", () => {
164
+ const { isolate: d, sync, root } = setup();
165
+ const tags = [
166
+ "script",
167
+ "iframe",
168
+ "object",
169
+ "embed",
170
+ "style",
171
+ "made-up"
172
+ ];
173
+ for (const tag of tags) d.insert(d.root, build(d, tag, [tag]));
174
+ const a = build(d, "a", ["link"]);
175
+ d.setAttr(a, "href", "javascript:alert(1)");
176
+ d.setAttr(a, "onclick", "alert(1)");
177
+ d.insert(d.root, a);
178
+ const div = d.el("div");
179
+ d.setAttr(div, "srcdoc", "<script>alert(1)<\/script>");
180
+ d.setAttr(div, "onmouseover", "alert(1)");
181
+ d.insert(d.root, div);
182
+ sync();
183
+ const all = [...root().querySelectorAll("*")];
184
+ for (const tag of [
185
+ "script",
186
+ "iframe",
187
+ "object",
188
+ "embed",
189
+ "style",
190
+ "made-up"
191
+ ]) expect(root().querySelector(tag)).toBeNull();
192
+ for (const el of all) for (const attr of el.attributes) {
193
+ expect(attr.name.startsWith("on"), `${el.localName}[${attr.name}]`).toBe(false);
194
+ expect(attr.name).not.toBe("srcdoc");
195
+ expect(attr.value.trim().toLowerCase().startsWith("javascript:")).toBe(false);
196
+ }
197
+ expect([...root().children].map((el) => el.textContent).slice(0, tags.length)).toEqual(tags);
198
+ });
199
+ test("ids", () => {
200
+ const { host, isolate: d, sync, find } = setup();
201
+ const button = build(d, "button", ["ok"]);
202
+ d.insert(d.root, button);
203
+ sync();
204
+ if (!host.idFor || !host.nodeFor) throw new Error("bridge has no idFor/nodeFor");
205
+ const el = find("button");
206
+ const id = d.idOf(button);
207
+ expect(host.nodeFor(id)).toBe(el);
208
+ expect(host.idFor(el)).toBe(id);
209
+ expect(host.nodeFor("no-such-id")).toBeUndefined();
210
+ });
211
+ test("events", () => {
212
+ const { isolate: d, sync, sent, find } = setup();
213
+ const button = build(d, "button", [build(d, "span", ["go"])]);
214
+ d.listen(button, "click");
215
+ d.insert(d.root, button);
216
+ const input = d.el("input");
217
+ d.listen(input, "input");
218
+ d.insert(d.root, input);
219
+ sync();
220
+ find("button span").dispatchEvent(new MouseEvent("click", {
221
+ bubbles: true,
222
+ composed: true,
223
+ shiftKey: true
224
+ }));
225
+ const el = find("input");
226
+ if (!(el instanceof HTMLInputElement)) throw new Error("no input");
227
+ el.value = "hi";
228
+ el.dispatchEvent(new Event("input", {
229
+ bubbles: true,
230
+ composed: true
231
+ }));
232
+ const [click, typed] = sent;
233
+ expect(sent).toHaveLength(2);
234
+ expect(click?.type).toBe("click");
235
+ expect(click?.mods.shift).toBe(true);
236
+ expect(click?.currentTargets.map((id) => d.handleFor(id))).toContain(button);
237
+ expect(typed?.type).toBe("input");
238
+ expect(typed?.value).toBe("hi");
239
+ expect(d.handleFor(typed?.target ?? "")).toBe(input);
240
+ });
241
+ test("noListener", () => {
242
+ const { isolate: d, sync, sent, find } = setup();
243
+ d.insert(d.root, build(d, "button", ["nothing"]));
244
+ sync();
245
+ find("button").dispatchEvent(new MouseEvent("click", {
246
+ bubbles: true,
247
+ composed: true
248
+ }));
249
+ expect(sent).toEqual([]);
250
+ });
251
+ test("inputs", () => {
252
+ const { isolate: d, sync, find } = setup();
253
+ const input = d.el("input");
254
+ d.listen(input, "input");
255
+ d.insert(d.root, input);
256
+ sync();
257
+ const el = find("input");
258
+ if (!(el instanceof HTMLInputElement)) throw new Error("no input");
259
+ el.focus();
260
+ el.value = "typed";
261
+ el.dispatchEvent(new Event("input", {
262
+ bubbles: true,
263
+ composed: true
264
+ }));
265
+ d.setValue(input, "stale");
266
+ sync();
267
+ expect(el.value).toBe("typed");
268
+ el.blur();
269
+ d.setValue(input, "next");
270
+ sync();
271
+ expect(el.value).toBe("next");
272
+ });
273
+ test("styles", () => {
274
+ const { isolate: d, sync, find } = setup();
275
+ const p = build(d, "p", ["red"]);
276
+ d.setAttr(p, "style", "color: red");
277
+ d.insert(d.root, p);
278
+ sync();
279
+ const el = find("p");
280
+ if (!(el instanceof HTMLElement)) throw new Error("no p");
281
+ expect(el.style.color).toBe("red");
282
+ });
283
+ test("classList", () => {
284
+ const { isolate: d, sync, find } = setup();
285
+ const p = build(d, "p", ["x"]);
286
+ d.setAttr(p, "class", "a b");
287
+ d.insert(d.root, p);
288
+ sync();
289
+ expect(d.classList(p, "contains", "b")).toBe(true);
290
+ d.classList(p, "add", "c");
291
+ d.classList(p, "add", "a");
292
+ d.classList(p, "remove", "b");
293
+ expect(d.classList(p, "toggle", "on")).toBe(true);
294
+ expect(d.classList(p, "toggle", "a")).toBe(false);
295
+ expect(d.classList(p, "toggle", "c", true)).toBe(true);
296
+ sync();
297
+ expect(find("p").getAttribute("class")).toBe("c on");
298
+ d.classList(p, "toggle", "on");
299
+ d.classList(p, "remove", "c");
300
+ sync();
301
+ expect(find("p").getAttribute("class")).toBe("");
302
+ });
303
+ test("dataset", () => {
304
+ const { isolate: d, sync, find } = setup();
305
+ const p = build(d, "p", ["x"]);
306
+ d.setAttr(p, "data-user-id", "7");
307
+ d.insert(d.root, p);
308
+ sync();
309
+ expect(d.dataset(p, "userId")).toBe("7");
310
+ expect(d.dataset(p, "rowIndex", "3")).toBe("3");
311
+ expect(d.dataset(p, "userId", null)).toBeUndefined();
312
+ sync();
313
+ const el = find("p");
314
+ expect(el.getAttribute("data-row-index")).toBe("3");
315
+ expect(el.hasAttribute("data-user-id")).toBe(false);
316
+ });
317
+ test("getElementById", () => {
318
+ const { isolate: d, sync } = setup();
319
+ const inner = build(d, "span", ["in"]);
320
+ d.setAttr(inner, "id", "target");
321
+ const outer = build(d, "div", [build(d, "p", [inner])]);
322
+ d.insert(d.root, outer);
323
+ const detached = d.el("span");
324
+ d.setAttr(detached, "id", "loose");
325
+ sync();
326
+ expect(d.byId("target")).toBe(inner);
327
+ expect(d.byId("loose")).toBeNull();
328
+ expect(d.byId("none")).toBeNull();
329
+ d.remove(outer);
330
+ expect(d.byId("target")).toBeNull();
331
+ });
332
+ test("contains", () => {
333
+ const { isolate: d } = setup();
334
+ const leaf = d.text("leaf");
335
+ const mid = build(d, "p", [leaf]);
336
+ const top = build(d, "div", [mid]);
337
+ const other = d.el("div");
338
+ d.insert(d.root, top);
339
+ expect(d.contains(top, leaf)).toBe(true);
340
+ expect(d.contains(top, top)).toBe(true);
341
+ expect(d.contains(mid, top)).toBe(false);
342
+ expect(d.contains(top, other)).toBe(false);
343
+ expect(d.contains(top, null)).toBe(false);
344
+ });
345
+ test("fragment", () => {
346
+ const { isolate: d, sync, html } = setup();
347
+ const last = build(d, "p", ["end"]);
348
+ d.insert(d.root, last);
349
+ sync();
350
+ const frag = d.fragment();
351
+ for (const t of [
352
+ "a",
353
+ "b",
354
+ "c"
355
+ ]) d.insert(frag, build(d, "span", [t]));
356
+ expect(d.childCount(frag)).toBe(3);
357
+ d.insert(d.root, frag, last);
358
+ expect(d.childCount(frag)).toBe(0);
359
+ sync();
360
+ expect(html()).toBe("<span>a</span><span>b</span><span>c</span><p>end</p>");
361
+ d.insert(d.root, frag);
362
+ d.insert(frag, build(d, "strong", ["again"]));
363
+ d.insert(d.root, frag);
364
+ sync();
365
+ expect(html()).toBe("<span>a</span><span>b</span><span>c</span><p>end</p><strong>again</strong>");
366
+ });
367
+ test("comment", () => {
368
+ const { isolate: d, sync, html } = setup();
369
+ const c = d.comment("marker");
370
+ d.insert(d.root, build(d, "p", [c, "text"]));
371
+ sync();
372
+ expect(html()).toBe("<p><!--marker-->text</p>");
373
+ d.setText(c, "moved");
374
+ sync();
375
+ expect(html()).toBe("<p><!--moved-->text</p>");
376
+ d.remove(c);
377
+ sync();
378
+ expect(html()).toBe("<p>text</p>");
379
+ });
380
+ test("forced", () => {
381
+ const { isolate: d, sync, find } = setup();
382
+ const input = d.el("input");
383
+ d.listen(input, "input");
384
+ d.insert(d.root, input);
385
+ sync();
386
+ const el = find("input");
387
+ if (!(el instanceof HTMLInputElement)) throw new Error("no input");
388
+ el.focus();
389
+ el.value = "hello";
390
+ el.dispatchEvent(new Event("input", {
391
+ bubbles: true,
392
+ composed: true
393
+ }));
394
+ d.setValue(input, "stale");
395
+ sync();
396
+ expect(el.value).toBe("hello");
397
+ d.setValue(input, "done", true);
398
+ sync();
399
+ expect(el.value).toBe("done");
400
+ expect([el.selectionStart, el.selectionEnd]).toEqual([4, 4]);
401
+ expect(el.matches(":focus")).toBe(true);
402
+ });
403
+ test("malformed", () => {
404
+ const { host, isolate: d, sync, html } = setup();
405
+ d.insert(d.root, build(d, "p", ["ok"]));
406
+ sync();
407
+ for (const bad of [
408
+ null,
409
+ "x",
410
+ 42,
411
+ { nonsense: true },
412
+ [[99, "nope"]]
413
+ ]) expect(host.apply(bad)).toBe(false);
414
+ expect(html()).toBe("<p>ok</p>");
415
+ });
416
+ });
417
+ }
418
+ /** An element with text and element children, built through the driver. */
419
+ function build(d, tag, children = []) {
420
+ const el = d.el(tag);
421
+ for (const c of children) d.insert(el, typeof c === "string" ? d.text(c) : c);
422
+ return el;
423
+ }
424
+ /** Children of `el` as HTML, with attributes sorted and internal ones left out. */
425
+ function serialize(el, internal) {
426
+ let out = "";
427
+ for (const n of el.childNodes) if (n instanceof Text) out += escape(n.data);
428
+ else if (n instanceof Comment) out += `<!--${n.data}-->`;
429
+ else if (n instanceof Element) {
430
+ const attrs = [...n.attributes].filter((a) => !internal(a.name)).map((a) => ` ${a.name}="${escape(a.value)}"`);
431
+ attrs.sort();
432
+ out += `<${n.localName}${attrs.join("")}>${serialize(n, internal)}</${n.localName}>`;
433
+ }
434
+ return out;
435
+ }
436
+ const escape = (s) => s.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll("\"", "&quot;");
437
+ // src/testing/driver.ts
438
+ const noop = () => {};
439
+ /**
440
+ * The isolate side of isoloop-dom for the conformance suite: the real
441
+ * worker-side subset, run in the test page without installing globals.
442
+ * Batches are collected and handed over on `flush()`.
443
+ */
444
+ function createIsoloopDomDriver() {
445
+ const batches = [];
446
+ const dom = createDom({
447
+ onBatch: (b) => batches.push(b),
448
+ schedule: () => {}
449
+ });
450
+ const handles = /* @__PURE__ */ new WeakMap();
451
+ const byId = /* @__PURE__ */ new Map();
452
+ const handleOf = /* @__PURE__ */ new WeakMap();
453
+ const wrap = (n) => {
454
+ const h = { __handle: true };
455
+ handles.set(h, n);
456
+ handleOf.set(n, h);
457
+ byId.set(String(dom.idOf(n)), h);
458
+ return h;
459
+ };
460
+ const node = (h) => {
461
+ const n = handles.get(h);
462
+ if (!n) throw new Error("not a handle from this driver");
463
+ return n;
464
+ };
465
+ const el = (h) => {
466
+ const n = node(h);
467
+ if (!(n instanceof WElement)) throw new Error("not an element");
468
+ return n;
469
+ };
470
+ const cfRoot = dom.document.createElement("div");
471
+ cfRoot.setAttribute("class", "cf-root");
472
+ dom.root.appendChild(cfRoot);
473
+ return {
474
+ dom,
475
+ root: wrap(cfRoot),
476
+ el: (tag) => wrap(dom.document.createElement(tag)),
477
+ text: (data) => wrap(dom.document.createTextNode(data)),
478
+ comment: (data) => wrap(dom.document.createComment(data)),
479
+ fragment: () => wrap(dom.document.createDocumentFragment()),
480
+ childCount: (h) => node(h).childNodes.length,
481
+ insert(parent, child, before) {
482
+ const p = node(parent);
483
+ if (!(p instanceof WParentNode)) throw new Error("not a parent");
484
+ p.insertBefore(node(child), before ? node(before) : null);
485
+ },
486
+ remove: (child) => node(child).remove(),
487
+ setAttr(h, name, value) {
488
+ if (value === null) el(h).removeAttribute(name);
489
+ else el(h).setAttribute(name, value);
490
+ },
491
+ setText(h, data) {
492
+ const n = node(h);
493
+ if (!(n instanceof WCharacterData)) throw new Error("not a text or comment node");
494
+ n.data = data;
495
+ },
496
+ setValue(h, value, force) {
497
+ if (force) dom.setValue(el(h), value, { force: true });
498
+ else Object.assign(el(h), { value });
499
+ },
500
+ listen: (h, type) => el(h).addEventListener(type, noop),
501
+ classList(h, op, token, force) {
502
+ const list = el(h).classList;
503
+ if (op === "toggle") return force === void 0 ? list.toggle(token) : list.toggle(token, force);
504
+ if (op === "contains") return list.contains(token);
505
+ list[op](token);
506
+ return list.contains(token);
507
+ },
508
+ dataset(h, key, value) {
509
+ const data = el(h).dataset;
510
+ if (value === null) delete data[key];
511
+ else if (value !== void 0) data[key] = value;
512
+ return data[key];
513
+ },
514
+ byId(id) {
515
+ const n = dom.document.getElementById(id);
516
+ return n ? handleOf.get(n) ?? null : null;
517
+ },
518
+ contains: (a, b) => node(a).contains(b ? node(b) : null),
519
+ flush() {
520
+ dom.flush();
521
+ return batches.splice(0);
522
+ },
523
+ idOf: (h) => String(dom.idOf(node(h))),
524
+ handleFor: (id) => byId.get(id)
525
+ };
526
+ }
527
+ // src/testing/check-usage.ts
528
+ /**
529
+ * Reads the subset doesn't serve, each with why that's fine. A read of an
530
+ * absent member returns `undefined`, which these call sites handle.
531
+ */
532
+ const TOLERATED_MISSING = {
533
+ moveBefore: "Preact 11 uses it when present and falls back to insertBefore, which already keeps node identity on the host (a `move` record).",
534
+ contentEditable: "React checks `=== \"true\"` when it saves and restores the selection around a commit; nothing in an isolate is editable, so undefined reads right.",
535
+ documentMode: "React checks for old IE’s document mode; absent means a modern browser.",
536
+ createEvent: "React 18 in development replays errors through a synthetic event when it can; without one it uses try/catch.",
537
+ getRootNode: "React 19 uses it, when present, to find a shadow root to hoist into, and falls back to ownerDocument; the isolate tree has no shadow root of its own.",
538
+ correspondingUseElement: "React reads it on event targets for old IE’s SVG <use> instances and falls back to the target itself."
539
+ };
540
+ async function checkDomUsage(worker, logPath, write) {
541
+ const log = await new Promise((resolve, reject) => {
542
+ worker.addEventListener("message", (e) => {
543
+ if (e.data.t === "error") reject(/* @__PURE__ */ new Error(`usage fixture threw: ${e.data.error}`));
544
+ else if (e.data.t === "usage") resolve(e.data.log);
545
+ });
546
+ worker.addEventListener("error", (e) => {
547
+ e.preventDefault();
548
+ reject(/* @__PURE__ */ new Error(`usage fixture failed: ${e.message}`));
549
+ });
550
+ }).finally(() => worker.terminate());
551
+ const text = `${JSON.stringify(log, null, 2)}\n`;
552
+ if (write) await commands.writeFile(logPath, text);
553
+ const unserved = log.missing.filter((m) => !Object.hasOwn(TOLERATED_MISSING, m));
554
+ expect(unserved, `${log.framework} uses members the subset lacks`).toEqual([]);
555
+ const committed = await commands.readFile(logPath).then((t) => JSON.parse(t)).catch(() => null);
556
+ expect(log, `${logPath} is out of date: run pnpm dom:usage`).toEqual(committed);
557
+ return log;
558
+ }
559
+ // src/testing/fixture.ts
560
+ /** Lets everything through: the smoke tests compare markup, not policy. */
561
+ const OPEN = {
562
+ element: (tag, ns) => ({
563
+ tag,
564
+ ns
565
+ }),
566
+ attribute: (_tag, _ns, _name, value) => value,
567
+ property: () => true
568
+ };
569
+ const EVENTS = [
570
+ "click",
571
+ "input",
572
+ "change",
573
+ "keydown"
574
+ ];
575
+ const isMessage = (x) => typeof x === "object" && x !== null && typeof Reflect.get(x, "t") === "string";
576
+ function mountFixture(worker) {
577
+ const container = document.createElement("div");
578
+ document.body.append(container);
579
+ const applier = createApplier(container, OPEN);
580
+ const started = performance.now();
581
+ let last = -1;
582
+ let failed = null;
583
+ worker.addEventListener("message", (e) => {
584
+ if (!isMessage(e.data)) return;
585
+ if (e.data.t === "batch") {
586
+ applier.apply(e.data.batch);
587
+ last = performance.now();
588
+ } else if (e.data.t === "error") failed = /* @__PURE__ */ new Error(`fixture worker threw: ${e.data.error}`);
589
+ });
590
+ worker.addEventListener("error", (e) => {
591
+ e.preventDefault();
592
+ failed = /* @__PURE__ */ new Error(`fixture worker failed: ${e.message}`);
593
+ });
594
+ for (const type of EVENTS) container.addEventListener(type, (e) => {
595
+ const event = applier.eventFor(e);
596
+ if (event) worker.postMessage({
597
+ t: "event",
598
+ event
599
+ });
600
+ });
601
+ return {
602
+ container,
603
+ async settled(quietMs = 30) {
604
+ for (;;) {
605
+ if (failed) throw failed;
606
+ if (last < 0 && performance.now() - started > 5e3) throw new Error("fixture worker rendered nothing within 5 s");
607
+ const wait = last < 0 ? quietMs : last + quietMs - performance.now();
608
+ if (wait <= 0) return;
609
+ await new Promise((r) => setTimeout(r, wait));
610
+ }
611
+ },
612
+ dispose() {
613
+ worker.terminate();
614
+ container.remove();
615
+ }
616
+ };
617
+ }
618
+ /**
619
+ * The page side of `servePoolFixture(n)`: one container per member, each
620
+ * with its own applier, getting only that member's batches and sending its
621
+ * events back for that member (ADR 0015, M2d task 2).
622
+ */
623
+ function mountMembers(worker, n) {
624
+ const surfaces = Array.from({ length: n }, (_, member) => {
625
+ const container = document.createElement("div");
626
+ document.body.append(container);
627
+ const applier = createApplier(container, OPEN);
628
+ for (const type of EVENTS) container.addEventListener(type, (e) => {
629
+ const event = applier.eventFor(e);
630
+ if (event) worker.postMessage({
631
+ t: "event",
632
+ event,
633
+ member
634
+ });
635
+ });
636
+ return {
637
+ container,
638
+ applier
639
+ };
640
+ });
641
+ worker.addEventListener("message", (e) => {
642
+ if (isMessage(e.data) && e.data.t === "batch") surfaces[e.data.member ?? 0]?.applier.apply(e.data.batch);
643
+ });
644
+ return surfaces.map((s) => s.container);
645
+ }
646
+ /** Sets a control's value the way typing does: through the prototype's setter, past React's value tracker. */
647
+ function setNative(el, value) {
648
+ const proto = el instanceof HTMLInputElement ? HTMLInputElement.prototype : HTMLSelectElement.prototype;
649
+ Object.getOwnPropertyDescriptor(proto, "value")?.set?.call(el, value);
650
+ }
651
+ const one = (root, selector, type) => {
652
+ const el = root.querySelector(selector);
653
+ if (!(el instanceof type)) throw new Error(`smoke: no ${selector} rendered`);
654
+ return el;
655
+ };
656
+ /**
657
+ * The user actions the smoke tests take on the fixture app (the same ones
658
+ * `driveUsageFixture` sends as events), for a container either way: the
659
+ * fixture host's or a real-DOM render's.
660
+ */
661
+ const SMOKE_STEPS = [
662
+ {
663
+ name: "toggle",
664
+ act: (r) => one(r, "button[name=toggle]", HTMLButtonElement).click()
665
+ },
666
+ {
667
+ name: "reorder",
668
+ act: (r) => one(r, "button[name=reorder]", HTMLButtonElement).click()
669
+ },
670
+ {
671
+ name: "type",
672
+ act(r) {
673
+ const input = one(r, "input", HTMLInputElement);
674
+ setNative(input, "typed");
675
+ input.dispatchEvent(new Event("input", { bubbles: true }));
676
+ }
677
+ },
678
+ {
679
+ name: "pick",
680
+ act(r) {
681
+ const select = one(r, "select", HTMLSelectElement);
682
+ setNative(select, "b");
683
+ select.dispatchEvent(new Event("change", { bubbles: true }));
684
+ }
685
+ },
686
+ {
687
+ name: "toggle back",
688
+ act: (r) => one(r, "button[name=toggle]", HTMLButtonElement).click()
689
+ }
690
+ ];
691
+ /** What the smoke tests compare: the markup, and the controls' live values. */
692
+ function snapshot(root) {
693
+ return {
694
+ html: root.innerHTML,
695
+ input: one(root, "input", HTMLInputElement).value,
696
+ select: one(root, "select", HTMLSelectElement).value
697
+ };
698
+ }
699
+ export { CASES, SMOKE_STEPS, TOLERATED_MISSING, checkDomUsage, createIsoloopDomDriver, mountFixture, mountMembers, runBridgeConformance, snapshot };