@mizchi/vlmkit-core 0.8.1 → 0.9.1
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/README.md +1 -1
- package/dist/a11y-semantic.d.mts +1 -6
- package/dist/a11y-semantic.mjs +1 -6
- package/dist/arg-reader.d.mts +54 -0
- package/dist/arg-reader.mjs +136 -0
- package/dist/auth-state.d.mts +30 -0
- package/dist/auth-state.mjs +82 -0
- package/dist/cli-args.d.mts +24 -5
- package/dist/cli-args.mjs +49 -12
- package/dist/cli-error.d.mts +15 -1
- package/dist/cli-error.mjs +52 -4
- package/dist/dom-equivalence.d.mts +2 -7
- package/dist/dom-equivalence.mjs +1 -6
- package/dist/element-compare.mjs +8 -8
- package/dist/gate-config.d.mts +116 -0
- package/dist/gate-config.mjs +169 -0
- package/dist/gate-exit.d.mts +45 -0
- package/dist/gate-exit.mjs +25 -0
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +3 -3
- package/dist/navigation-redirect.d.mts +28 -0
- package/dist/navigation-redirect.mjs +47 -0
- package/dist/page-open.d.mts +102 -0
- package/dist/page-open.mjs +133 -0
- package/dist/project-config.d.mts +12 -0
- package/dist/project-config.mjs +28 -0
- package/package.json +4 -2
|
@@ -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 };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/project-config.d.ts
|
|
2
|
+
declare const STATE_DIR = ".vlmkit";
|
|
3
|
+
declare const CONFIG_FILE = "vlmkit.config.json";
|
|
4
|
+
declare const CONFIG_CANDIDATES: readonly ["vlmkit.config.json", "vlmkit.config.toml"];
|
|
5
|
+
/** Resolve a project-owned state entry below the canonical state directory. */
|
|
6
|
+
declare function resolveStatePath(cwd: string, entry: string): string;
|
|
7
|
+
/** Read a canonical VLMKIT_* environment variable. */
|
|
8
|
+
declare function readEnv(suffix: string): string | undefined;
|
|
9
|
+
declare function debugEnabled(): boolean;
|
|
10
|
+
declare const ENV_SUFFIXES: readonly ["BASE_URL", "CAPTURE_BACKEND", "CONFIG_FILE", "CONFIG_PATH", "LLM_MODEL", "LLM_PROVIDER", "PROJECT_ROOT", "VLM_MODEL"];
|
|
11
|
+
//#endregion
|
|
12
|
+
export { CONFIG_CANDIDATES, CONFIG_FILE, ENV_SUFFIXES, STATE_DIR, debugEnabled, readEnv, resolveStatePath };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
//#region src/project-config.ts
|
|
3
|
+
const STATE_DIR = ".vlmkit";
|
|
4
|
+
const CONFIG_FILE = "vlmkit.config.json";
|
|
5
|
+
const CONFIG_CANDIDATES = [CONFIG_FILE, "vlmkit.config.toml"];
|
|
6
|
+
/** Resolve a project-owned state entry below the canonical state directory. */
|
|
7
|
+
function resolveStatePath(cwd, entry) {
|
|
8
|
+
return resolve(cwd, STATE_DIR, entry);
|
|
9
|
+
}
|
|
10
|
+
/** Read a canonical VLMKIT_* environment variable. */
|
|
11
|
+
function readEnv(suffix) {
|
|
12
|
+
return process.env[`VLMKIT_${suffix}`] || void 0;
|
|
13
|
+
}
|
|
14
|
+
function debugEnabled() {
|
|
15
|
+
return Boolean(process.env.DEBUG_VLMKIT);
|
|
16
|
+
}
|
|
17
|
+
const ENV_SUFFIXES = [
|
|
18
|
+
"BASE_URL",
|
|
19
|
+
"CAPTURE_BACKEND",
|
|
20
|
+
"CONFIG_FILE",
|
|
21
|
+
"CONFIG_PATH",
|
|
22
|
+
"LLM_MODEL",
|
|
23
|
+
"LLM_PROVIDER",
|
|
24
|
+
"PROJECT_ROOT",
|
|
25
|
+
"VLM_MODEL"
|
|
26
|
+
];
|
|
27
|
+
//#endregion
|
|
28
|
+
export { CONFIG_CANDIDATES, CONFIG_FILE, ENV_SUFFIXES, STATE_DIR, debugEnabled, readEnv, resolveStatePath };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mizchi/vlmkit-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"description": "VRT diff engine + shared types — pure image / DOM / a11y / visual comparison primitives.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "mizchi",
|
|
@@ -37,9 +37,11 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"pixelmatch": "^7.1.0",
|
|
40
|
-
"playwright": "^1.61.1",
|
|
41
40
|
"pngjs": "^7.0.0"
|
|
42
41
|
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"playwright": ">=1.61 <2"
|
|
44
|
+
},
|
|
43
45
|
"scripts": {
|
|
44
46
|
"build": "pnpm --dir ../.. exec tsdown --config tsdown.packages.config.ts --filter @mizchi/vlmkit-core",
|
|
45
47
|
"test": "node --test 'src/**/*.test.ts'"
|