|
| 1 | +// SPDX-License-Identifier: MPL-2.0 |
| 2 | +// e2e host for dom_drive.wasm — Int-handle DOM, mutation log, assertions. |
| 3 | +import assert from 'node:assert/strict'; |
| 4 | +import { readFile } from 'node:fs/promises'; |
| 5 | + |
| 6 | +let inst = null; |
| 7 | +const readString = (ptr) => { |
| 8 | + const dv = new DataView(inst.exports.memory.buffer); |
| 9 | + const len = dv.getUint32(ptr, true); |
| 10 | + return new TextDecoder('utf-8').decode( |
| 11 | + new Uint8Array(inst.exports.memory.buffer, ptr + 4, len)); |
| 12 | +}; |
| 13 | + |
| 14 | +// handle 1 = #root |
| 15 | +const nodes = new Map([[1, { tag: '#root', attrs: new Map(), children: [], text: null }]]); |
| 16 | +let next = 2; |
| 17 | +const log = []; |
| 18 | +const mk = (n) => { const h = next++; nodes.set(h, n); return h; }; |
| 19 | + |
| 20 | +const env = { |
| 21 | + dom_query_selector: (p) => { const s = readString(p); log.push(`query(${s})`); return s === '#root' ? 1 : 0; }, |
| 22 | + dom_create_element: (p) => { const t = readString(p); const h = mk({ tag: t, attrs: new Map(), children: [], text: null }); log.push(`createElement(${t})=#${h}`); return h; }, |
| 23 | + dom_create_text_node: (p) => { const s = readString(p); const h = mk({ tag: '#text', attrs: new Map(), children: [], text: s }); log.push(`createText("${s}")=#${h}`); return h; }, |
| 24 | + dom_append_child: (p, c) => { nodes.get(p).children.push(c); log.push(`append(#${p},#${c})`); }, |
| 25 | + dom_replace_child: (p, o, n) => { const cs = nodes.get(p).children; const i = cs.indexOf(o); assert.ok(i >= 0, `replace: #${o} not under #${p}`); cs[i] = n; log.push(`replace(#${p},#${o}->#${n})`); }, |
| 26 | + dom_remove_child: (p, c) => { const cs = nodes.get(p).children; const i = cs.indexOf(c); assert.ok(i >= 0, `remove: #${c} not under #${p}`); cs.splice(i, 1); log.push(`remove(#${p},#${c})`); }, |
| 27 | + dom_child_at: (p, i) => { const c = nodes.get(p).children[i] ?? 0; log.push(`childAt(#${p},${i})=#${c}`); return c; }, |
| 28 | + dom_set_attribute: (el, n, v) => { const name = readString(n), val = readString(v); nodes.get(el).attrs.set(name, val); log.push(`setAttr(#${el},${name}=${val})`); }, |
| 29 | + dom_remove_attribute: (el, n) => { const name = readString(n); nodes.get(el).attrs.delete(name); log.push(`removeAttr(#${el},${name})`); }, |
| 30 | + dom_set_text: (el, p) => { const s = readString(p); nodes.get(el).text = s; log.push(`setText(#${el},"${s}")`); }, |
| 31 | + dom_str_eq: (a, b) => (readString(a) === readString(b) ? 1 : 0), |
| 32 | +}; |
| 33 | + |
| 34 | +const dump = (h, d = 0) => { |
| 35 | + const n = nodes.get(h); |
| 36 | + const attrs = [...n.attrs].map(([k, v]) => ` ${k}="${v}"`).join(''); |
| 37 | + const line = n.tag === '#text' ? `"${n.text}"` : `<${n.tag}${attrs}> #${h}`; |
| 38 | + return [' '.repeat(d) + line, ...n.children.flatMap((c) => dump(c, d + 1))].join('\n'); |
| 39 | +}; |
| 40 | + |
| 41 | +const bytes = await readFile(process.argv[2]); |
| 42 | +const { instance } = await WebAssembly.instantiate(bytes, { |
| 43 | + env, wasi_snapshot_preview1: { fd_write: () => 0 }, |
| 44 | +}); |
| 45 | +inst = instance; |
| 46 | + |
| 47 | +const ret = inst.exports.main(); |
| 48 | +console.log('main() =', ret); |
| 49 | +console.log('--- mutation log ---'); console.log(log.join('\n')); |
| 50 | +console.log('--- final DOM under #root ---'); console.log(dump(1)); |
| 51 | + |
| 52 | +// assertions: reconcile patched in place |
| 53 | +const root = nodes.get(1); |
| 54 | +assert.equal(root.children.length, 1, 'root has exactly one child'); |
| 55 | +const div = nodes.get(root.children[0]); |
| 56 | +assert.equal(div.tag, 'div'); |
| 57 | +assert.equal(div.attrs.get('id'), 'app'); |
| 58 | +assert.equal(div.attrs.get('title'), 't2', 'title added by patch_attrs'); |
| 59 | +assert.ok(!div.attrs.has('class'), 'class removed by patch_attrs'); |
| 60 | +assert.equal(div.children.length, 1, 'span child removed by while-loop reconcile'); |
| 61 | +const t = nodes.get(div.children[0]); |
| 62 | +assert.equal(t.tag, '#text'); |
| 63 | +assert.equal(t.text, 'world', 'text updated in place'); |
| 64 | +assert.equal(ret, root.children[0], 'reconcile returned the in-place div handle'); |
| 65 | +console.log('ALL ASSERTIONS PASS — reconciler ran end-to-end (loops executed)'); |
0 commit comments