@highalch/story 0.0.0 → 0.0.2
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.
- package/LICENSE +21 -0
- package/README.md +343 -0
- package/dist/index.d.mts +42 -0
- package/dist/index.mjs +321 -0
- package/dist/run-C2SdDPWE.d.mts +75 -0
- package/dist/run-DMO8Dcmq.mjs +131 -0
- package/dist/screen.d.mts +71 -0
- package/dist/screen.mjs +505 -0
- package/dist/steps-CvFOQHum.d.mts +599 -0
- package/dist/steps-DZBFsrQr.mjs +950 -0
- package/dist/xstate.d.mts +145 -0
- package/dist/xstate.mjs +405 -0
- package/package.json +96 -1
package/dist/screen.mjs
ADDED
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
import { L as Mismatch, M as macrotask, w as staged } from "./steps-DZBFsrQr.mjs";
|
|
2
|
+
import { Option, Predicate, Schema } from "effect";
|
|
3
|
+
import { mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import { dirname, join, relative } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { act, createElement } from "react";
|
|
7
|
+
import { createRoot } from "react-dom/client";
|
|
8
|
+
import { render } from "takumi-js";
|
|
9
|
+
import wasm from "takumi-js/wasm-url";
|
|
10
|
+
import { Provider } from "@highalch/db/react";
|
|
11
|
+
import { Window } from "happy-dom";
|
|
12
|
+
import { deflateSync, inflateSync } from "node:zlib";
|
|
13
|
+
// src/screen/dom.ts
|
|
14
|
+
/** Globals React DOM and components read; installed only while a screen is mounted. */
|
|
15
|
+
const always = [
|
|
16
|
+
"window",
|
|
17
|
+
"document",
|
|
18
|
+
"navigator",
|
|
19
|
+
"location",
|
|
20
|
+
"getComputedStyle"
|
|
21
|
+
];
|
|
22
|
+
/**
|
|
23
|
+
* Installs a happy-dom window as the global DOM: `window`, `document` and every window property
|
|
24
|
+
* the global scope lacks (`HTMLElement`, `Node`, `MutationObserver`, …). Properties the runtime
|
|
25
|
+
* already has (timers, `fetch`, `Event`, …) stay the runtime's, except the few in `always`.
|
|
26
|
+
* `remove` restores the global scope exactly and closes the window, so other tests keep their
|
|
27
|
+
* environment.
|
|
28
|
+
*/
|
|
29
|
+
const install = (width, height) => {
|
|
30
|
+
const window = new Window({
|
|
31
|
+
url: "http://localhost/",
|
|
32
|
+
width,
|
|
33
|
+
height
|
|
34
|
+
});
|
|
35
|
+
const previous = /* @__PURE__ */ new Map();
|
|
36
|
+
const names = Object.getOwnPropertyNames(window).filter((name) => !always.includes(name) && !(name in globalThis));
|
|
37
|
+
for (const name of [...always, ...names]) {
|
|
38
|
+
previous.set(name, Object.getOwnPropertyDescriptor(globalThis, name));
|
|
39
|
+
Object.defineProperty(globalThis, name, bound(window, name));
|
|
40
|
+
}
|
|
41
|
+
const remove = async () => {
|
|
42
|
+
for (const [name, descriptor] of previous) if (descriptor) Object.defineProperty(globalThis, name, descriptor);
|
|
43
|
+
else Reflect.deleteProperty(globalThis, name);
|
|
44
|
+
await window.happyDOM.abort();
|
|
45
|
+
window.close();
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
window,
|
|
49
|
+
remove
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
/** A window property's descriptor, found on the window or its prototypes. */
|
|
53
|
+
const descriptorOf = (window, name) => {
|
|
54
|
+
for (let at = window; at !== null; at = Object.getPrototypeOf(at)) {
|
|
55
|
+
const found = Object.getOwnPropertyDescriptor(at, name);
|
|
56
|
+
if (found) return found;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
/** A global that reads the window's property with the window as `this` (getters, methods). */
|
|
60
|
+
const bound = (window, name) => {
|
|
61
|
+
if (name === "window") return {
|
|
62
|
+
value: window,
|
|
63
|
+
configurable: true,
|
|
64
|
+
writable: true
|
|
65
|
+
};
|
|
66
|
+
const descriptor = descriptorOf(window, name);
|
|
67
|
+
if (descriptor?.get) return {
|
|
68
|
+
get: () => descriptor.get?.call(window),
|
|
69
|
+
configurable: true
|
|
70
|
+
};
|
|
71
|
+
const value = descriptor?.value;
|
|
72
|
+
if (Predicate.isFunction(value) && name === "getComputedStyle") return {
|
|
73
|
+
value: value.bind(window),
|
|
74
|
+
configurable: true,
|
|
75
|
+
writable: true
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
value,
|
|
79
|
+
configurable: true,
|
|
80
|
+
writable: true
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
const escape = (text) => text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
84
|
+
/**
|
|
85
|
+
* The HTML of a mounted tree as it looks now, for a renderer that runs no script: an `input`,
|
|
86
|
+
* `textarea` or `select` becomes a `span` with the same attributes (and `data-control` naming
|
|
87
|
+
* the control) whose text is its current value (a password masked, a checkbox ☑/☐), since the
|
|
88
|
+
* renderer paints no form controls and the attributes hold stale values. Scripts are dropped.
|
|
89
|
+
*/
|
|
90
|
+
const serialize = (root) => {
|
|
91
|
+
const copy = root.cloneNode(true);
|
|
92
|
+
if (!(copy instanceof Element)) return "";
|
|
93
|
+
const live = [...root.querySelectorAll("input, textarea, select")];
|
|
94
|
+
[...copy.querySelectorAll("input, textarea, select")].forEach((control, index) => {
|
|
95
|
+
const source = live[index];
|
|
96
|
+
const span = control.ownerDocument.createElement("span");
|
|
97
|
+
for (const attribute of control.attributes) if (attribute.name !== "value") span.setAttribute(attribute.name, attribute.value);
|
|
98
|
+
span.setAttribute("data-control", control.tagName.toLowerCase());
|
|
99
|
+
span.textContent = valueOf(source);
|
|
100
|
+
control.replaceWith(span);
|
|
101
|
+
});
|
|
102
|
+
for (const script of copy.querySelectorAll("script")) script.remove();
|
|
103
|
+
return copy.innerHTML;
|
|
104
|
+
};
|
|
105
|
+
const valueOf = (control) => {
|
|
106
|
+
if (control instanceof window.HTMLInputElement) {
|
|
107
|
+
if (control.type === "checkbox" || control.type === "radio") return control.checked ? "☑" : "☐";
|
|
108
|
+
if (control.type === "password") return "•".repeat(control.value.length);
|
|
109
|
+
return control.value;
|
|
110
|
+
}
|
|
111
|
+
if (control instanceof window.HTMLTextAreaElement) return control.value;
|
|
112
|
+
if (control instanceof window.HTMLSelectElement) return [...control.selectedOptions].map((option) => option.text).join(", ");
|
|
113
|
+
return "";
|
|
114
|
+
};
|
|
115
|
+
// src/screen/png.ts
|
|
116
|
+
const signature = Uint8Array.from([
|
|
117
|
+
137,
|
|
118
|
+
80,
|
|
119
|
+
78,
|
|
120
|
+
71,
|
|
121
|
+
13,
|
|
122
|
+
10,
|
|
123
|
+
26,
|
|
124
|
+
10
|
|
125
|
+
]);
|
|
126
|
+
const table = Uint32Array.from({ length: 256 }, (_, index) => {
|
|
127
|
+
let value = index;
|
|
128
|
+
for (let bit = 0; bit < 8; bit += 1) value = value & 1 ? 3988292384 ^ value >>> 1 : value >>> 1;
|
|
129
|
+
return value >>> 0;
|
|
130
|
+
});
|
|
131
|
+
const crc = (bytes) => {
|
|
132
|
+
let value = 4294967295;
|
|
133
|
+
for (const byte of bytes) value = (table[(value ^ byte) & 255] ?? 0) ^ value >>> 8;
|
|
134
|
+
return (value ^ 4294967295) >>> 0;
|
|
135
|
+
};
|
|
136
|
+
const chunk = (type, body) => {
|
|
137
|
+
const bytes = new Uint8Array(12 + body.length);
|
|
138
|
+
const view = new DataView(bytes.buffer);
|
|
139
|
+
view.setUint32(0, body.length);
|
|
140
|
+
bytes.set(new TextEncoder().encode(type), 4);
|
|
141
|
+
bytes.set(body, 8);
|
|
142
|
+
view.setUint32(8 + body.length, crc(bytes.subarray(4, 8 + body.length)));
|
|
143
|
+
return bytes;
|
|
144
|
+
};
|
|
145
|
+
/** Encodes RGBA pixels as a PNG (8-bit RGBA, no filter): what baselines and frames are stored as. */
|
|
146
|
+
const encode = (pixels) => {
|
|
147
|
+
const header = /* @__PURE__ */ new Uint8Array(13);
|
|
148
|
+
const view = new DataView(header.buffer);
|
|
149
|
+
view.setUint32(0, pixels.width);
|
|
150
|
+
view.setUint32(4, pixels.height);
|
|
151
|
+
header.set([
|
|
152
|
+
8,
|
|
153
|
+
6,
|
|
154
|
+
0,
|
|
155
|
+
0,
|
|
156
|
+
0
|
|
157
|
+
], 8);
|
|
158
|
+
const stride = pixels.width * 4;
|
|
159
|
+
const raw = new Uint8Array((stride + 1) * pixels.height);
|
|
160
|
+
for (let row = 0; row < pixels.height; row += 1) raw.set(pixels.data.subarray(row * stride, (row + 1) * stride), row * (stride + 1) + 1);
|
|
161
|
+
const parts = [
|
|
162
|
+
signature,
|
|
163
|
+
chunk("IHDR", header),
|
|
164
|
+
chunk("IDAT", deflateSync(raw)),
|
|
165
|
+
chunk("IEND", /* @__PURE__ */ new Uint8Array())
|
|
166
|
+
];
|
|
167
|
+
const out = new Uint8Array(parts.reduce((total, part) => total + part.length, 0));
|
|
168
|
+
let offset = 0;
|
|
169
|
+
for (const part of parts) {
|
|
170
|
+
out.set(part, offset);
|
|
171
|
+
offset += part.length;
|
|
172
|
+
}
|
|
173
|
+
return out;
|
|
174
|
+
};
|
|
175
|
+
/** Channels per pixel of a PNG colour type this decoder reads: RGBA, RGB, else none. */
|
|
176
|
+
const channelsOf = (color) => {
|
|
177
|
+
if (color === 6) return 4;
|
|
178
|
+
if (color === 2) return 3;
|
|
179
|
+
return 0;
|
|
180
|
+
};
|
|
181
|
+
const paeth = (left, up, corner) => {
|
|
182
|
+
const estimate = left + up - corner;
|
|
183
|
+
const toLeft = Math.abs(estimate - left);
|
|
184
|
+
const toUp = Math.abs(estimate - up);
|
|
185
|
+
const toCorner = Math.abs(estimate - corner);
|
|
186
|
+
if (toLeft <= toUp && toLeft <= toCorner) return left;
|
|
187
|
+
if (toUp <= toCorner) return up;
|
|
188
|
+
return corner;
|
|
189
|
+
};
|
|
190
|
+
/**
|
|
191
|
+
* Decodes a PNG to RGBA pixels: 8-bit RGB or RGBA, not interlaced, any filter (what `encode`
|
|
192
|
+
* writes, and what common tools re-save it as). Anything else is an error.
|
|
193
|
+
*/
|
|
194
|
+
const decode = (png) => {
|
|
195
|
+
if (!signature.every((byte, index) => png[index] === byte)) throw new Error("not a PNG");
|
|
196
|
+
const view = new DataView(png.buffer, png.byteOffset, png.byteLength);
|
|
197
|
+
const data = [];
|
|
198
|
+
let width = 0;
|
|
199
|
+
let height = 0;
|
|
200
|
+
let channels = 0;
|
|
201
|
+
for (let offset = 8; offset < png.length;) {
|
|
202
|
+
const length = view.getUint32(offset);
|
|
203
|
+
const type = new TextDecoder().decode(png.subarray(offset + 4, offset + 8));
|
|
204
|
+
const body = png.subarray(offset + 8, offset + 8 + length);
|
|
205
|
+
if (type === "IHDR") {
|
|
206
|
+
const header = new DataView(body.buffer, body.byteOffset, body.byteLength);
|
|
207
|
+
const [depth, color, , , interlace] = body.subarray(8, 13);
|
|
208
|
+
width = header.getUint32(0);
|
|
209
|
+
height = header.getUint32(4);
|
|
210
|
+
channels = channelsOf(color);
|
|
211
|
+
if (depth !== 8 || channels === 0 || interlace !== 0) throw new Error("only 8-bit RGB or RGBA PNGs without interlacing are compared");
|
|
212
|
+
}
|
|
213
|
+
if (type === "IDAT") data.push(body);
|
|
214
|
+
offset += 12 + length;
|
|
215
|
+
}
|
|
216
|
+
const joined = new Uint8Array(data.reduce((total, part) => total + part.length, 0));
|
|
217
|
+
let at = 0;
|
|
218
|
+
for (const part of data) {
|
|
219
|
+
joined.set(part, at);
|
|
220
|
+
at += part.length;
|
|
221
|
+
}
|
|
222
|
+
const raw = inflateSync(joined);
|
|
223
|
+
const stride = width * channels;
|
|
224
|
+
const rows = new Uint8Array(stride * height);
|
|
225
|
+
for (let row = 0; row < height; row += 1) {
|
|
226
|
+
const filter = raw[row * (stride + 1)];
|
|
227
|
+
const line = raw.subarray(row * (stride + 1) + 1, (row + 1) * (stride + 1));
|
|
228
|
+
for (let index = 0; index < stride; index += 1) {
|
|
229
|
+
const left = index >= channels ? rows[row * stride + index - channels] ?? 0 : 0;
|
|
230
|
+
const up = row > 0 ? rows[(row - 1) * stride + index] ?? 0 : 0;
|
|
231
|
+
const corner = row > 0 && index >= channels ? rows[(row - 1) * stride + index - channels] ?? 0 : 0;
|
|
232
|
+
const value = line[index] ?? 0;
|
|
233
|
+
const predicted = [
|
|
234
|
+
0,
|
|
235
|
+
left,
|
|
236
|
+
up,
|
|
237
|
+
left + up >> 1,
|
|
238
|
+
paeth(left, up, corner)
|
|
239
|
+
][filter ?? 0];
|
|
240
|
+
if (predicted === void 0) throw new Error(`unknown PNG filter ${filter}`);
|
|
241
|
+
rows[row * stride + index] = value + predicted & 255;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (channels === 4) return {
|
|
245
|
+
width,
|
|
246
|
+
height,
|
|
247
|
+
data: rows
|
|
248
|
+
};
|
|
249
|
+
const rgba = new Uint8Array(width * height * 4);
|
|
250
|
+
for (let pixel = 0; pixel < width * height; pixel += 1) {
|
|
251
|
+
rgba.set(rows.subarray(pixel * 3, pixel * 3 + 3), pixel * 4);
|
|
252
|
+
rgba[pixel * 4 + 3] = 255;
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
width,
|
|
256
|
+
height,
|
|
257
|
+
data: rgba
|
|
258
|
+
};
|
|
259
|
+
};
|
|
260
|
+
/**
|
|
261
|
+
* Compares decoded pixels exactly (not PNG bytes). Images of different sizes differ everywhere;
|
|
262
|
+
* the difference image then has the actual image's size.
|
|
263
|
+
*/
|
|
264
|
+
const compare = (actual, expected) => {
|
|
265
|
+
const data = new Uint8Array(actual.width * actual.height * 4);
|
|
266
|
+
const same = actual.width === expected.width && actual.height === expected.height;
|
|
267
|
+
let pixels = 0;
|
|
268
|
+
for (let pixel = 0; pixel < actual.width * actual.height; pixel += 1) {
|
|
269
|
+
const at = pixel * 4;
|
|
270
|
+
if (!same || [
|
|
271
|
+
0,
|
|
272
|
+
1,
|
|
273
|
+
2,
|
|
274
|
+
3
|
|
275
|
+
].some((channel) => actual.data[at + channel] !== expected.data[at + channel])) {
|
|
276
|
+
pixels += 1;
|
|
277
|
+
data.set([
|
|
278
|
+
220,
|
|
279
|
+
30,
|
|
280
|
+
30,
|
|
281
|
+
255
|
|
282
|
+
], at);
|
|
283
|
+
} else {
|
|
284
|
+
const grey = Math.round(((actual.data[at] ?? 0) + (actual.data[at + 1] ?? 0) + (actual.data[at + 2] ?? 0)) / 3);
|
|
285
|
+
const faded = 255 - Math.round((255 - grey) * .25);
|
|
286
|
+
data.set([
|
|
287
|
+
faded,
|
|
288
|
+
faded,
|
|
289
|
+
faded,
|
|
290
|
+
255
|
|
291
|
+
], at);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return {
|
|
295
|
+
pixels,
|
|
296
|
+
image: {
|
|
297
|
+
width: actual.width,
|
|
298
|
+
height: actual.height,
|
|
299
|
+
data
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
};
|
|
303
|
+
// src/screen/screen.ts
|
|
304
|
+
const screensKey = Symbol("@highalch/story/screen");
|
|
305
|
+
const pathOf = (place) => {
|
|
306
|
+
if (place instanceof URL) return fileURLToPath(place);
|
|
307
|
+
return place;
|
|
308
|
+
};
|
|
309
|
+
const slug = (text) => text.toLowerCase().replaceAll(/[^a-z0-9]+/g, "-").replaceAll(/^-+|-+$/g, "").slice(0, 60) || "step";
|
|
310
|
+
const pad = (step) => String(step).padStart(2, "0");
|
|
311
|
+
/** The wasm renderer's module bytes, loaded once: the same renderer binary on every platform. */
|
|
312
|
+
let module;
|
|
313
|
+
const moduleOf = () => module ??= readFile(wasm);
|
|
314
|
+
/**
|
|
315
|
+
* Lets React finish: `act` flushes renders and effects scheduled by store changes. React's act
|
|
316
|
+
* environment flag is on only inside, so updates a story causes between steps (a live query's
|
|
317
|
+
* store changing) are scheduled normally and flushed here.
|
|
318
|
+
*/
|
|
319
|
+
const flush = async (run = () => {}) => {
|
|
320
|
+
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
|
|
321
|
+
try {
|
|
322
|
+
await act(async () => {
|
|
323
|
+
run();
|
|
324
|
+
await macrotask();
|
|
325
|
+
});
|
|
326
|
+
} finally {
|
|
327
|
+
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: false });
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
/** Waits until the world is quiet without letting command replies through, then lets React finish. */
|
|
331
|
+
const quiet = async (world) => {
|
|
332
|
+
await flush();
|
|
333
|
+
await world.settle(false);
|
|
334
|
+
await flush();
|
|
335
|
+
};
|
|
336
|
+
const nameOf = (stage, label) => Option.getOrThrowWith(stage.name, () => new Mismatch(`"${label}" needs a story name, since frames are named by it: story.with({ name: "…" })(…)`));
|
|
337
|
+
const write = async (file, bytes) => {
|
|
338
|
+
await mkdir(dirname(file), { recursive: true });
|
|
339
|
+
await writeFile(file, bytes);
|
|
340
|
+
};
|
|
341
|
+
const readable = async (file) => readFile(file).then((bytes) => Option.some(new Uint8Array(bytes)), () => Option.none());
|
|
342
|
+
/** A filmstrip page of one story's frames. */
|
|
343
|
+
const page = (story, frames) => {
|
|
344
|
+
const figures = frames.map((frame) => {
|
|
345
|
+
const failed = frame.status === "mismatch" || frame.status === "missing";
|
|
346
|
+
const base = frame.file.replace(/\.png$/, "");
|
|
347
|
+
const extra = Option.match(Option.liftPredicate(frame, () => frame.status === "mismatch"), {
|
|
348
|
+
onNone: () => "",
|
|
349
|
+
onSome: () => `<a href="${escape(base)}.expected.png">expected</a> · <a href="${escape(base)}.diff.png">diff</a> (${frame.differing ?? 0} px)`
|
|
350
|
+
});
|
|
351
|
+
return `<figure class="${failed ? "failed" : ""}"><img src="${escape(frame.file)}" alt=""><figcaption><b>${frame.step}.</b> ${escape(frame.label)}<br><span>${frame.status}</span> ${extra}</figcaption></figure>`;
|
|
352
|
+
});
|
|
353
|
+
return `<!doctype html><meta charset="utf-8"><title>${escape(story)}</title><style>body{font:14px system-ui;margin:16px;background:#f6f6f4}h1{font-size:18px}.strip{display:flex;gap:12px;overflow-x:auto}figure{margin:0;background:#fff;border:1px solid #ddd;padding:6px}figure.failed{border-color:#d22}img{display:block;border:1px solid #eee}figcaption{max-width:320px;margin-top:6px}span{color:#666}</style><p><a href="../index.html">all stories</a></p><h1>${escape(story)}</h1><div class="strip">${figures.join("")}</div>`;
|
|
354
|
+
};
|
|
355
|
+
/** What the index reads of a story's manifest. */
|
|
356
|
+
const Manifest = Schema.Struct({
|
|
357
|
+
story: Schema.String,
|
|
358
|
+
frames: Schema.Array(Schema.Struct({ status: Schema.String }))
|
|
359
|
+
});
|
|
360
|
+
/** Rewrites the run's index from every story's manifest under `output`. */
|
|
361
|
+
const index = async (output) => {
|
|
362
|
+
const entries = await readdir(output, { withFileTypes: true }).catch(() => []);
|
|
363
|
+
const stories = [];
|
|
364
|
+
for (const entry of entries.filter((each) => each.isDirectory()).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
365
|
+
const manifest = await readFile(join(output, entry.name, "manifest.json"), "utf8").then(Schema.decodeUnknownOption(Schema.fromJsonString(Manifest)), () => Option.none());
|
|
366
|
+
if (Option.isNone(manifest)) continue;
|
|
367
|
+
stories.push({
|
|
368
|
+
dir: entry.name,
|
|
369
|
+
name: manifest.value.story,
|
|
370
|
+
failed: manifest.value.frames.some((frame) => frame.status === "mismatch" || frame.status === "missing")
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
const items = stories.map((story) => `<li><a href="${escape(story.dir)}/index.html">${escape(story.name)}</a>${story.failed ? " ✗" : ""}</li>`);
|
|
374
|
+
await write(join(output, "index.html"), `<!doctype html><meta charset="utf-8"><title>Story screens</title><style>body{font:14px system-ui;margin:16px}</style><h1>Story screens</h1><ul>${items.join("")}</ul>`);
|
|
375
|
+
};
|
|
376
|
+
/**
|
|
377
|
+
* Screens for stories: `make(options)` gives `render(element)`, a step that mounts a React
|
|
378
|
+
* element under the package's `Provider` with the story's client (stock `useLiveQuery` reads the
|
|
379
|
+
* story's data) in a happy-dom document, and captures a frame after it and after every later
|
|
380
|
+
* step: once the world is quiet (command replies still held) and React has flushed, the DOM is
|
|
381
|
+
* serialised (current control values) and painted by takumi's WASM renderer with `css`, then
|
|
382
|
+
* compared pixel by pixel with its baseline. `shot(label?)` captures a frame on demand (with
|
|
383
|
+
* `render(element, { every: false })`, only there).
|
|
384
|
+
*/
|
|
385
|
+
const make = (options) => {
|
|
386
|
+
const baselines = pathOf(options.baselines);
|
|
387
|
+
const output = pathOf(options.output);
|
|
388
|
+
const css = [options.css ?? []].flat();
|
|
389
|
+
const capture = async (stage, mounted, done, label) => {
|
|
390
|
+
const story = slug(nameOf(stage, label));
|
|
391
|
+
await quiet(stage.hosted(label));
|
|
392
|
+
const html = `<div data-screen style="width:${options.width}px;height:${options.height}px;display:flex;flex-direction:column">${serialize(mounted.container)}</div>`;
|
|
393
|
+
const raw = await render(html, {
|
|
394
|
+
module: await moduleOf(),
|
|
395
|
+
width: options.width,
|
|
396
|
+
height: options.height,
|
|
397
|
+
css,
|
|
398
|
+
fonts: options.fonts ? [...options.fonts] : void 0,
|
|
399
|
+
format: "raw"
|
|
400
|
+
});
|
|
401
|
+
const actual = {
|
|
402
|
+
width: options.width,
|
|
403
|
+
height: options.height,
|
|
404
|
+
data: raw
|
|
405
|
+
};
|
|
406
|
+
const name = `${pad(done.step)}-${slug(done.label)}`;
|
|
407
|
+
const file = `${name}.png`;
|
|
408
|
+
const baseline = join(baselines, story, file);
|
|
409
|
+
const target = join(output, story);
|
|
410
|
+
const png = encode(actual);
|
|
411
|
+
await write(join(target, file), png);
|
|
412
|
+
await write(join(target, `${name}.html`), html);
|
|
413
|
+
const known = await readable(baseline);
|
|
414
|
+
const record = (status, differing) => mounted.frames.push({
|
|
415
|
+
step: done.step,
|
|
416
|
+
label: done.label,
|
|
417
|
+
file,
|
|
418
|
+
status,
|
|
419
|
+
differing
|
|
420
|
+
});
|
|
421
|
+
if (options.update) {
|
|
422
|
+
await write(baseline, png);
|
|
423
|
+
return record(Option.isSome(known) ? "updated" : "created");
|
|
424
|
+
}
|
|
425
|
+
if (Option.isNone(known)) {
|
|
426
|
+
record("missing");
|
|
427
|
+
throw new Mismatch(`frame ${story}/${file} has no baseline`, [`actual: ${relative(process.cwd(), join(target, file))}`, "review it, then write baselines with the screen's `update` option"]);
|
|
428
|
+
}
|
|
429
|
+
const expected = decode(known.value);
|
|
430
|
+
const difference = compare(actual, expected);
|
|
431
|
+
if (difference.pixels === 0) return record("matched");
|
|
432
|
+
record("mismatch", difference.pixels);
|
|
433
|
+
await write(join(target, `${name}.expected.png`), known.value);
|
|
434
|
+
await write(join(target, `${name}.diff.png`), encode(difference.image));
|
|
435
|
+
throw new Mismatch(`frame ${story}/${file} differs from its baseline in ${difference.pixels} pixel(s)`, [
|
|
436
|
+
`actual: ${relative(process.cwd(), join(target, file))}`,
|
|
437
|
+
`expected: ${relative(process.cwd(), baseline)}`,
|
|
438
|
+
`diff: ${relative(process.cwd(), join(target, `${name}.diff.png`))}`
|
|
439
|
+
]);
|
|
440
|
+
};
|
|
441
|
+
/** Unmounts, removes the DOM globals and writes the story's filmstrip and manifest. */
|
|
442
|
+
const finish = (stage, mounted, label) => async () => {
|
|
443
|
+
await flush(() => mounted.root.unmount());
|
|
444
|
+
await mounted.dom.remove();
|
|
445
|
+
const story = nameOf(stage, label);
|
|
446
|
+
const target = join(output, slug(story));
|
|
447
|
+
await write(join(target, "manifest.json"), `${JSON.stringify({
|
|
448
|
+
story,
|
|
449
|
+
width: options.width,
|
|
450
|
+
height: options.height,
|
|
451
|
+
frames: mounted.frames
|
|
452
|
+
}, null, 2)}\n`);
|
|
453
|
+
await write(join(target, "index.html"), page(story, mounted.frames));
|
|
454
|
+
await index(output);
|
|
455
|
+
};
|
|
456
|
+
/**
|
|
457
|
+
* Mounts `element` against the story's client (of the step's scope) and captures a frame after
|
|
458
|
+
* this step and every later one (`every: false`: only at `shot` steps). Needs a hosted story
|
|
459
|
+
* with a name; one screen per story.
|
|
460
|
+
*/
|
|
461
|
+
const render$1 = (element, settings = {}) => {
|
|
462
|
+
const label = "render";
|
|
463
|
+
return staged(label, async (stage) => {
|
|
464
|
+
const world = stage.hosted(label);
|
|
465
|
+
const screens = stage.slot(screensKey, () => ({ mounted: Option.none() }));
|
|
466
|
+
const story = nameOf(stage, label);
|
|
467
|
+
if (Option.isSome(screens.mounted)) throw new Mismatch("this story already rendered a screen");
|
|
468
|
+
await rm(join(output, slug(story)), {
|
|
469
|
+
recursive: true,
|
|
470
|
+
force: true
|
|
471
|
+
});
|
|
472
|
+
const client = await world.connect();
|
|
473
|
+
const dom = install(options.width, options.height);
|
|
474
|
+
const container = document.createElement("div");
|
|
475
|
+
document.body.append(container);
|
|
476
|
+
const root = createRoot(container);
|
|
477
|
+
const mounted = {
|
|
478
|
+
root,
|
|
479
|
+
container,
|
|
480
|
+
dom,
|
|
481
|
+
every: settings.every ?? true,
|
|
482
|
+
frames: []
|
|
483
|
+
};
|
|
484
|
+
screens.mounted = Option.some(mounted);
|
|
485
|
+
stage.end(finish(stage, mounted, label));
|
|
486
|
+
await flush(() => root.render(createElement(Provider, { client: client.db }, element)));
|
|
487
|
+
stage.after(async (done) => {
|
|
488
|
+
if (mounted.every) await capture(stage, mounted, done, label);
|
|
489
|
+
});
|
|
490
|
+
});
|
|
491
|
+
};
|
|
492
|
+
/** Captures a frame now, labelled `label` (for `render(element, { every: false })`). */
|
|
493
|
+
const shot = (label = "shot") => staged(`shot ${label}`, async (stage) => {
|
|
494
|
+
const mounted = Option.getOrThrowWith(stage.slot(screensKey, () => ({ mounted: Option.none() })).mounted, () => new Mismatch("nothing is rendered yet: render(element) first"));
|
|
495
|
+
if (!mounted.every) await capture(stage, mounted, {
|
|
496
|
+
step: stage.step,
|
|
497
|
+
label
|
|
498
|
+
}, label);
|
|
499
|
+
});
|
|
500
|
+
return {
|
|
501
|
+
render: render$1,
|
|
502
|
+
shot
|
|
503
|
+
};
|
|
504
|
+
};
|
|
505
|
+
export { compare, decode, encode, make };
|