summaryrefslogtreecommitdiff
path: root/test/web_harness.mjs
blob: 087dd91bee885d73ec0561088b70764a7f1ad496 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env node
// Dependency-free JS harness: instantiate the real freestanding core in Node,
// then render its packed frame through app.mjs against a deliberately tiny DOM
// double. Chrome is reserved for the separate browser/e2e snapshot harness.

import assert from "node:assert/strict";
import fs from "node:fs/promises";
import path from "node:path";
import { pathToFileURL } from "node:url";

if (process.argv.length !== 3) {
  console.error("usage: node test/web_harness.mjs <zig-out/web>");
  process.exit(2);
}

async function main() {
  const webDir = path.resolve(process.argv[2]);
  const wasmBytes = await fs.readFile(path.join(webDir, "pardes.wasm"));
  const module = await WebAssembly.compile(wasmBytes);
  assert.deepEqual(WebAssembly.Module.imports(module), [], "the freestanding core must have no JS or libc imports");
  const { exports: wasm } = await WebAssembly.instantiate(module, {});

  for (const name of [
    "memory", "pardes_init", "pardes_frame", "pardes_frame_ptr", "pardes_key",
    "pardes_mouse", "pardes_pointer_leave", "pardes_gui_tagline_font_percent", "pardes_output",
    "pardes_lsp_response", "pardes_pipe_response", "pardes_file_changed",
    "pardes_effect_next", "pardes_deinit",
  ]) assert.ok(wasm[name], `missing wasm export ${name}`);

  assert.equal(wasm.pardes_init(80, 24), 0, `wasm init error ${wasm.pardes_last_error()}`);
  assert.equal(wasm.pardes_frame(), 80 * 24);
  assert.ok(wasm.pardes_frame_ptr() > 0);

  const { DomRenderer, animationTicks } = await import(pathToFileURL(path.join(webDir, "app.mjs")));
  const document = new FakeDocument();
  const root = document.createElement("main");
  root.ownerDocument = document;
  const renderer = new DomRenderer(root, wasm);
  const first = renderer.render();

  assert.equal(first.cols, 80);
  assert.equal(first.rows, 24);
  assert.equal(root.children.length, 24);
  assert.equal(root.children[0].children.length, 80);
  assert.ok(first.cells.some((cell) => cell.text !== " "), "dump rendered an empty frame");
  assert.ok(root.walk().every((node) => node.tagName !== "CANVAS"), "renderer created a canvas");
  const taglineIndex = first.cells.findIndex((cell) => cell.attrs & (1 << 7));
  const bodyIndex = first.cells.findIndex((cell) => !(cell.attrs & (1 << 7)) && cell.text !== " ");
  assert.ok(taglineIndex >= 0, "frame ABI did not mark a tagline font role");
  assert.ok(bodyIndex >= 0, "fixture has no body glyph to compare with tagline");
  assert.ok(renderer.cells[taglineIndex].classList.names.has("is-tagline"), "DOM tagline class was not applied");
  assert.ok(!renderer.cells[bodyIndex].classList.names.has("is-tagline"), "DOM inferred tagline role for body text");
  assert.equal(
    root.style["--pardes-tagline-font-size"],
    `${wasm.pardes_gui_tagline_font_percent()}%`,
    "DOM did not use the build-time tagline percentage",
  );

  for (const hz of [60, 120, 144]) {
    const clock = { last: null, lag: 0 };
    let ticks = 0;
    for (let frame = 0; frame <= hz; frame += 1) {
      ticks += animationTicks(clock, frame * 1000 / hz);
    }
    assert.equal(ticks, 60, `${hz} Hz rAF did not produce 60 core ticks per second`);
  }
  const stalled = { last: null, lag: 0 };
  assert.equal(animationTicks(stalled, 0), 0);
  assert.equal(animationTicks(stalled, 60_000), 4, "suspended tab catch-up was not bounded");

  const firstCell = renderer.cells[0];
  const second = renderer.render();
  assert.equal(renderer.cells[0], firstCell, "stable frames should reuse DOM nodes");
  assert.equal(second.cells.length, 80 * 24);

  // A replay follows the terminal cursor at the bottom. Deliberately
  // over-scroll (the core clamps at row zero) so adding more source files does
  // not silently move the hard-coded build.zig click onto some later path.
  for (let tick = 0; tick < 128; tick += 1) wasm.pardes_mouse(3, 0, 5, 3, 0);
  const listingTop = renderer.render();
  assert.match(
    listingTop.cells.map((cell) => cell.text).join(""),
    /git ls-files --cached --others --exclude-standard.*build\.zig/,
    "working-tree source listing did not scroll to its first row",
  );
  wasm.pardes_mouse(2, 0, 5, 3, 0);
  wasm.pardes_mouse(2, 1, 5, 3, 0);
  const looked = renderer.render();
  assert.match(looked.cells.map((cell) => cell.text).join(""), /build\.zig Save New Del/, "right-click LOOK did not open the embedded source");
  const sourceRows = Array.from({ length: looked.rows }, (_, row) =>
    looked.cells.slice(row * looked.cols, (row + 1) * looked.cols));
  const codeRows = sourceRows.filter((row) => row.map((cell) => cell.text).join("").match(/^\s*\d+\s/));
  const codeColors = new Set(codeRows.flatMap((row) => row.slice(5).map((cell) => `${cell.fg}:${cell.attrs & 1}`)));
  assert.ok(codeColors.size >= 3, "Tree-sitter did not apply distinct syntax styles to build.zig");
  let sawWatch = false;
  for (let index = 0; index < 32; index += 1) {
    const kind = wasm.pardes_effect_next();
    if (!kind) break;
    if (kind === 9) sawWatch = true;
  }
  assert.ok(sawWatch, "JS effect drain did not receive the opened file watch request");

  wasm.pardes_resize(41, 13);
  const resized = renderer.render();
  assert.equal(resized.cols, 41);
  assert.equal(resized.rows, 13);
  assert.equal(root.children.length, 13);
  assert.equal(root.children[0].children.length, 41);

  wasm.pardes_deinit();
  console.error("web harness ok: freestanding wasm + Tree-sitter + packed DOM renderer");
}

class FakeDocument {
  constructor() {
    this.body = new FakeElement("body", this);
  }

  createElement(tagName) {
    return new FakeElement(tagName, this);
  }
}

class FakeElement {
  constructor(tagName, ownerDocument) {
    this.tagName = tagName.toUpperCase();
    this.ownerDocument = ownerDocument;
    this.children = [];
    this.attributes = new Map();
    this.dataset = {};
    this.style = new FakeStyle();
    this.classList = new FakeClassList(this);
    this.className = "";
    this.textContent = "";
  }

  setAttribute(name, value) {
    this.attributes.set(name, String(value));
  }

  replaceChildren(...children) {
    this.children = children;
  }

  *walk() {
    yield this;
    for (const child of this.children) yield* child.walk();
  }
}

class FakeStyle {
  setProperty(name, value) {
    this[name] = value;
  }
}

class FakeClassList {
  constructor(element) {
    this.element = element;
    this.names = new Set();
  }

  toggle(name, force) {
    if (force) this.names.add(name);
    else this.names.delete(name);
    this.element.className = [...this.names].join(" ");
  }
}

await main();