@mizchi/vlmkit-core 0.8.1 → 0.9.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,133 @@
1
+ import { withAuthState } from "./auth-state.mjs";
2
+ import { describeRedirect } from "./navigation-redirect.mjs";
3
+ import { resolve } from "node:path";
4
+ import { pathToFileURL } from "node:url";
5
+ //#region src/page-open.ts
6
+ /**
7
+ * One way to open the page under test.
8
+ *
9
+ * Two mechanisms had grown up side by side, and they do not measure the same
10
+ * document:
11
+ *
12
+ * - `page.goto(pathToFileURL(file))` — the document has an origin, so
13
+ * `<link rel="stylesheet" href="style.css">`, `<img src="hero.png">` and
14
+ * relative scripts all resolve.
15
+ * - `page.setContent(await readFile(file))` — the document's base URL is
16
+ * `about:blank`, so every relative reference silently fails to load.
17
+ *
18
+ * Measured consequence (2026-08-02): a page with `p { color: #bbbbbb }` in an
19
+ * external stylesheet — 1.92:1 on white, a clear WCAG failure — was reported by
20
+ * `check a11y contrast` as **0 failures**, because the gate measured unstyled
21
+ * markup. Inlining the same CSS in a `<style>` block made the same gate report
22
+ * the failure correctly. Ten gates read a user's file and pushed it through
23
+ * `setContent` with no base URL.
24
+ *
25
+ * So: load by navigation whenever the bytes on disk are what we want, and when
26
+ * a gate must mutate the HTML first (inflate text for i18n, force a theme,
27
+ * delete a CSS rule), navigate to the source and *then* replace the markup, so
28
+ * the document keeps a base URL its siblings resolve against.
29
+ */
30
+ const isUrlSource = (source) => /^https?:\/\//.test(source);
31
+ /**
32
+ * Normalize a source for storage and display: an absolute path for a file, the
33
+ * URL untouched for a URL.
34
+ *
35
+ * `resolve()` alone mangles a URL into a path — `resolve("http://x/p.html")`
36
+ * yields `<cwd>/http:/x/p.html`, which then fails as "file not found" and tells
37
+ * the caller nothing about what actually went wrong.
38
+ */
39
+ function resolveSource(source) {
40
+ return isUrlSource(source) ? source : resolve(source);
41
+ }
42
+ /** File path or URL → a URL Playwright can navigate to. */
43
+ function sourceToUrl(source) {
44
+ return isUrlSource(source) ? source : pathToFileURL(resolve(source)).href;
45
+ }
46
+ /**
47
+ * Wait for the page to stop moving: bounded network idle, then webfonts, then
48
+ * one frame. Every part is best-effort — a page with a long-poll connection
49
+ * never reaches network idle, and refusing to measure it would be worse than
50
+ * measuring it slightly early.
51
+ *
52
+ * **A gate that navigates and does not call this measures the wrong document.**
53
+ * `load` fires before a client-rendered view paints, and the failure is never
54
+ * reported as "I looked too early" — it is reported as a defect in the page:
55
+ *
56
+ * - 2026-08-01: `check interactions` said "interactive elements: 0" on a React
57
+ * page with a button, two links and a scroller. It measured the "Loading…"
58
+ * placeholder.
59
+ * - 2026-08-02: `verify flow` failed `count .card expected 2, measured 0` on a
60
+ * page where `check layout` measured 2 at the same instant, and
61
+ * `build page` screenshotted a candidate at 5.3% of its settled ink — so
62
+ * every component came back missing. Both blamed the markup.
63
+ *
64
+ * Playwright *actions* auto-wait, which is why this stayed hidden: a click on a
65
+ * late-rendered element is safe. Reads are not — `page.evaluate`,
66
+ * `page.screenshot` and `getBoundingClientRect` all sample the DOM at that
67
+ * instant.
68
+ *
69
+ * `waitUntil` is not the axis. `goto(load)` followed by this settle waits for
70
+ * network idle anyway, so the 8 `load` and 2 `domcontentloaded` call sites are
71
+ * equivalent to the 71 `networkidle` ones **provided they settle**. The
72
+ * difference that mattered was always the settle, never the load state.
73
+ */
74
+ async function settlePage(page, settleMs = 250) {
75
+ await page.waitForLoadState("networkidle", { timeout: 5e3 }).catch(() => {});
76
+ await page.evaluate(() => document.fonts ? document.fonts.ready.then(() => void 0) : void 0).catch(() => {});
77
+ if (settleMs > 0) await page.waitForTimeout(settleMs);
78
+ }
79
+ function pageOptions(options) {
80
+ return withAuthState({
81
+ ...options.viewport ? { viewport: options.viewport } : {},
82
+ ...options.deviceScaleFactor !== void 0 ? { deviceScaleFactor: options.deviceScaleFactor } : {},
83
+ ...options.colorScheme ? { colorScheme: options.colorScheme } : {}
84
+ }, options.storageState);
85
+ }
86
+ /**
87
+ * Open a file or URL by navigation, so relative assets resolve.
88
+ */
89
+ async function openSource(browser, source, options = {}) {
90
+ const page = await browser.newPage(pageOptions(options));
91
+ await page.goto(sourceToUrl(source), {
92
+ waitUntil: options.waitUntil ?? "networkidle",
93
+ timeout: options.timeout ?? 3e4
94
+ });
95
+ if (!options.skipSettle) await settlePage(page, options.settleMs ?? 250);
96
+ return {
97
+ page,
98
+ redirect: isUrlSource(source) ? describeRedirect(source, page.url()) : null
99
+ };
100
+ }
101
+ /**
102
+ * Open HTML a gate has rewritten in memory (inflated text, forced theme,
103
+ * deleted rule).
104
+ *
105
+ * Prefer `openSource` when the bytes on disk are what should be measured.
106
+ *
107
+ * Navigate-then-replace, because the obvious alternative does not work.
108
+ * Measured on the same one-line fixture (external `style.css` setting
109
+ * `p { color: rgb(4,5,6) }`):
110
+ *
111
+ * setContent + injected `<base href="file:///dir/">` -> rgb(0, 0, 0)
112
+ * goto(file) then setContent -> rgb(4, 5, 6)
113
+ *
114
+ * A `<base>` cannot rescue it: the `setContent` document has an opaque origin,
115
+ * and Chromium blocks a `file://` subresource from one. Navigating first gives
116
+ * the document a real base URL, and `setContent` replaces the markup without
117
+ * discarding it.
118
+ */
119
+ async function openHtml(browser, html, options = {}) {
120
+ const page = await browser.newPage(pageOptions(options));
121
+ if (options.baseSource) await page.goto(sourceToUrl(options.baseSource), {
122
+ waitUntil: "domcontentloaded",
123
+ timeout: options.timeout ?? 3e4
124
+ });
125
+ await page.setContent(html, {
126
+ waitUntil: options.waitUntil ?? "networkidle",
127
+ timeout: options.timeout ?? 3e4
128
+ });
129
+ if (!options.skipSettle) await settlePage(page, options.settleMs ?? 250);
130
+ return page;
131
+ }
132
+ //#endregion
133
+ export { isUrlSource, openHtml, openSource, resolveSource, settlePage, sourceToUrl };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mizchi/vlmkit-core",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "description": "VRT diff engine + shared types — pure image / DOM / a11y / visual comparison primitives.",
5
5
  "license": "MIT",
6
6
  "author": "mizchi",