Files
raea/sdk/ui/test/host.ts
shinyobjectz 6d8a15ac55 A scroll event no longer puts back an offset the page wrote past (sdk/ui's DOM layer)
The host's scroll event carries the offset it had when it committed, and the DOM layer took it over as scrollTop. A page that had written another one since (each write is sent and shown at once) was put back a step. Two scrollers kept in step, the app's timeline tracks and their heads, then each put the other back, every frame, for good: after a quick drag of the tracks' scroll thumb in raea's window they cycled through five offsets and never stopped (logged event by event in the feed's timeline-nav N1).

Now each scroller keeps the values it wrote that the host hasn't told back. An event carrying one of them retires it and those before it, and if newer ones are still out, the newest written stands: as in Chrome, where a scroll handler reads the offset as it is now. An event carrying a value the page never wrote is the host's own (a wheel, a clamp) and stands.

sdk/ui/test/scroll.test.tsx (the scroll tests moved out of dom.test.tsx, which was over 500 lines; its snapshot helpers shared from test/host.ts): a stale event leaves the newer offset, the host's own stands; two scrollers mirrored both ways, with the host's events a frame late, settle. Both fail without the change (the pair still had 10 events due after 20 frames). bun test test: 62 pass.
2026-11-15 20:57:02 -08:00

141 lines
5.7 KiB
TypeScript

// A stand-in for the host module (apps/legacy/shell-bevy/src/tsx.rs) in TypeScript: it takes each batch as the host does
// (`{v, ops}`, the version checked), applies the ops to a tree of nodes as the host's entities would stand, and is
// strict where the host is lenient (an op on an id it doesn't know, a second create of an id, a listen twice, an
// update with nothing in it all throw), so a driver's mistake fails a test instead of passing quietly. Events go back
// through the wire's own `__ui_deliver`, as the host delivers them.
import { layout } from "../src/dom";
import { OPS_VERSION, restart, type Ev, type Op } from "../src/wire";
export type N = { kind: string; props: Record<string, unknown>; text?: string; on: Set<Ev>; children: number[]; parent?: number };
export class Host {
nodes = new Map<number, N>([[0, { kind: "#root", props: {}, on: new Set(), children: [] }]]);
batches: Op[][] = [];
emits: [string, unknown][] = [];
calls: [number, string, unknown][] = [];
constructor() {
const g = globalThis as any;
g.__ui_flush = (json: string) => {
const b = JSON.parse(json);
if (b.v !== OPS_VERSION) throw new Error(`a batch of op version ${b.v}; the host speaks ${OPS_VERSION}`);
this.batches.push(b.ops);
for (const op of b.ops) this.apply(op);
};
g.__ui_emit = (name: string, json: string) => this.emits.push([name, JSON.parse(json)]);
g.__ui_call = (id: number, name: string, json: string) => this.calls.push([id, name, JSON.parse(json)]);
}
private get(id: number): N {
const n = this.nodes.get(id);
if (!n) throw new Error(`no element ${id}`);
return n;
}
private detach(c: number) {
const n = this.get(c);
if (n.parent === undefined) return;
const k = this.get(n.parent).children;
k.splice(k.indexOf(c), 1);
n.parent = undefined;
}
private drop(c: number) {
for (const k of this.get(c).children) this.drop(k);
this.nodes.delete(c);
}
apply(op: Op) {
switch (op.op) {
case "clear":
for (const c of [...this.get(0).children]) {
this.detach(c);
this.drop(c);
}
break;
case "create":
if (this.nodes.has(op.id)) throw new Error(`element ${op.id} made twice`);
this.nodes.set(op.id, { kind: op.kind, props: { ...op.props }, text: op.text, on: new Set(), children: [] });
break;
case "append":
case "insert": {
const p = this.get(op.parent);
this.detach(op.child);
const at = op.op === "insert" ? p.children.indexOf(op.before) : -1;
if (op.op === "insert" && at < 0) throw new Error(`insert before ${op.before}, which isn't under ${op.parent}`);
if (at < 0) p.children.push(op.child);
else p.children.splice(at, 0, op.child);
this.get(op.child).parent = op.parent;
break;
}
case "remove":
this.detach(op.id);
this.drop(op.id);
break;
case "update": {
const n = this.get(op.id);
if (!op.set && !op.unset && op.text === undefined) throw new Error(`an empty update of ${op.id}`);
Object.assign(n.props, op.set ?? {});
for (const k of op.unset ?? []) delete n.props[k];
if (op.text !== undefined) n.text = op.text;
break;
}
case "listen":
for (const e of op.events) {
if (this.get(op.id).on.has(e)) throw new Error(`${op.id} listens for ${e} twice`);
this.get(op.id).on.add(e);
}
break;
case "unlisten":
for (const e of op.events) {
if (!this.get(op.id).on.delete(e)) throw new Error(`${op.id} unlistens ${e}, which it didn't listen for`);
}
break;
}
}
/** The tree as the host's entities stand, ids left out (two drivers number their elements differently). */
shape(id = 0): unknown {
const x = this.get(id);
const props = Object.fromEntries(Object.entries(x.props).sort(([a], [b]) => (a < b ? -1 : 1)));
return [x.kind, props, x.text ?? null, [...x.on].sort(), x.children.map((c) => this.shape(c))];
}
/** The element with this data-testid. */
tid(t: string): number {
for (const [id, x] of this.nodes) if (x.props["data-testid"] === t) return id;
throw new Error(`no element ${t}`);
}
/** An element's event, as the host sends it (only to an element that listens for it, as the host does). */
send(t: string | number, event: Ev, payload: unknown = null) {
const id = typeof t === "number" ? t : this.tid(t);
if (!this.get(id).on.has(event)) throw new Error(`${t} doesn't listen for ${event}`);
(globalThis as any).__ui_deliver(JSON.stringify({ t: "event", id, event, payload }));
}
named(name: string, value: unknown) {
(globalThis as any).__ui_deliver(JSON.stringify({ t: "named", name, value }));
}
answer(id: number, value: unknown) {
(globalThis as any).__ui_deliver(JSON.stringify({ t: "answer", id, ok: true, value }));
}
/** The ops since batch `from`. */
since(from: number): Op[] {
return this.batches.slice(from).flat();
}
}
export const kinds = (ops: Op[]) => ops.reduce((a: Record<string, number>, o) => ((a[o.op] = (a[o.op] ?? 0) + 1), a), {});
/** A wire started over and a new stand-in host for it. */
export function fresh() {
restart();
return new Host();
}
/** A layout snapshot with these boxes (by data-testid): [x, y, w, h] and optional scroll [sl, st, sw, sh]. */
export function snap(host: Host, boxes: Record<string, number[]>) {
const n: number[] = [];
for (const [t, b] of Object.entries(boxes)) {
const id = host.tid(t);
const [x, y, w, h, sl = 0, st = 0, sw = w, sh = h] = b;
n.push(id, x, y, w, h, sl, st, sw, sh, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
layout(JSON.stringify({ vw: 800, vh: 600, dpr: 2, n }));
}