@mizchi/vlmkit-core 0.8.0 → 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.
- package/README.md +1 -1
- package/dist/a11y-semantic.d.mts +34 -0
- package/dist/a11y-semantic.mjs +261 -0
- package/dist/arg-reader.d.mts +52 -0
- package/dist/arg-reader.mjs +129 -0
- package/dist/auth-state.d.mts +30 -0
- package/dist/auth-state.mjs +82 -0
- package/dist/authored-style-capture.d.mts +42 -0
- package/dist/authored-style-capture.mjs +117 -0
- package/dist/authored-style-diff.d.mts +15 -0
- package/dist/authored-style-diff.mjs +16 -0
- package/dist/cli-args.d.mts +35 -0
- package/dist/cli-args.mjs +63 -0
- package/dist/cli-error.d.mts +12 -0
- package/dist/cli-error.mjs +71 -0
- package/dist/computed-style-capture.d.mts +41 -0
- package/dist/computed-style-capture.mjs +589 -0
- package/dist/computed-style-diff.d.mts +83 -0
- package/dist/computed-style-diff.mjs +123 -0
- package/dist/diff-regions.d.mts +8 -0
- package/dist/diff-regions.mjs +52 -0
- package/dist/dom-equivalence.d.mts +51 -0
- package/dist/dom-equivalence.mjs +91 -0
- package/dist/dom-position-styles.d.mts +166 -0
- package/dist/dom-position-styles.mjs +296 -0
- package/dist/element-compare.d.mts +71 -0
- package/dist/element-compare.mjs +269 -0
- 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/heatmap-regions.d.mts +57 -0
- package/dist/heatmap-regions.mjs +158 -0
- package/dist/heatmap.d.mts +86 -0
- package/dist/heatmap.mjs +510 -0
- package/dist/image-resize.d.mts +34 -0
- package/dist/image-resize.mjs +92 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.mjs +14 -0
- package/dist/legacy-names.d.mts +48 -0
- package/dist/legacy-names.mjs +134 -0
- package/dist/mask.d.mts +18 -0
- package/dist/mask.mjs +27 -0
- 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/png-utils.d.mts +30 -0
- package/dist/png-utils.mjs +80 -0
- package/dist/region-shift.d.mts +20 -0
- package/dist/region-shift.mjs +128 -0
- package/dist/run-ledger.d.mts +14 -0
- package/dist/run-ledger.mjs +31 -0
- package/dist/terminal-colors.d.mts +12 -0
- package/dist/terminal-colors.mjs +14 -0
- package/dist/text-rows.d.mts +121 -0
- package/dist/text-rows.mjs +246 -0
- package/{src/types.ts → dist/types.d.mts} +88 -206
- package/dist/types.mjs +1 -0
- package/package.json +17 -5
- package/src/a11y-semantic.ts +0 -384
- package/src/authored-style-capture.ts +0 -185
- package/src/authored-style-diff.ts +0 -32
- package/src/cli-args.ts +0 -29
- package/src/cli-error.ts +0 -85
- package/src/computed-style-capture.ts +0 -717
- package/src/computed-style-diff.ts +0 -192
- package/src/diff-regions.ts +0 -70
- package/src/dom-equivalence.ts +0 -158
- package/src/dom-position-styles.ts +0 -457
- package/src/element-compare.ts +0 -354
- package/src/heatmap-regions.ts +0 -245
- package/src/heatmap.ts +0 -670
- package/src/image-resize.ts +0 -112
- package/src/index.ts +0 -105
- package/src/mask.ts +0 -41
- package/src/png-utils.ts +0 -86
- package/src/region-shift.ts +0 -173
- package/src/run-ledger.ts +0 -39
- package/src/terminal-colors.ts +0 -13
- package/src/text-rows.ts +0 -412
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
VRT diff engine and shared types. Pure-TypeScript image / DOM / a11y / visual
|
|
4
4
|
comparison primitives — no Playwright required for the lightweight surface.
|
|
5
5
|
|
|
6
|
-
Part of the [`
|
|
6
|
+
Part of the [`vlmkit`](https://github.com/mizchi/vrt) monorepo.
|
|
7
7
|
|
|
8
8
|
## Install
|
|
9
9
|
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { A11yDiff, A11yNode, A11ySnapshot } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/a11y-semantic.d.ts
|
|
4
|
+
declare const LANDMARK_ROLES: Set<string>;
|
|
5
|
+
declare const INTERACTIVE_ROLES: Set<string>;
|
|
6
|
+
/**
|
|
7
|
+
* Compare two a11y tree snapshots and return the semantic diff.
|
|
8
|
+
*/
|
|
9
|
+
declare function diffA11yTrees(baseline: A11ySnapshot, current: A11ySnapshot): A11yDiff;
|
|
10
|
+
/**
|
|
11
|
+
* A11y tree quality checks (standalone, no diff).
|
|
12
|
+
*
|
|
13
|
+
* Walks the tree and returns issues such as missing labels on
|
|
14
|
+
* interactive elements and missing alt text on images.
|
|
15
|
+
*
|
|
16
|
+
* @since 0.5.0 — replaces `checkA11yTree`.
|
|
17
|
+
*/
|
|
18
|
+
declare function verifyA11yTree(tree: A11yNode): A11yIssue[];
|
|
19
|
+
/**
|
|
20
|
+
* @deprecated since 0.5.0 — use `verifyA11yTree` instead. Removed in 1.0.0.
|
|
21
|
+
*/
|
|
22
|
+
declare const checkA11yTree: typeof verifyA11yTree;
|
|
23
|
+
interface A11yIssue {
|
|
24
|
+
path: string;
|
|
25
|
+
rule: string;
|
|
26
|
+
severity: "error" | "warning";
|
|
27
|
+
message: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Convert Playwright page.accessibility.snapshot() output to A11ySnapshot.
|
|
31
|
+
*/
|
|
32
|
+
declare function parsePlaywrightA11ySnapshot(testId: string, testTitle: string, raw: Record<string, unknown>): A11ySnapshot;
|
|
33
|
+
//#endregion
|
|
34
|
+
export { A11yIssue, INTERACTIVE_ROLES, LANDMARK_ROLES, checkA11yTree, diffA11yTrees, parsePlaywrightA11ySnapshot, verifyA11yTree };
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
//#region src/a11y-semantic.ts
|
|
2
|
+
const LANDMARK_ROLES = new Set([
|
|
3
|
+
"banner",
|
|
4
|
+
"complementary",
|
|
5
|
+
"contentinfo",
|
|
6
|
+
"form",
|
|
7
|
+
"main",
|
|
8
|
+
"navigation",
|
|
9
|
+
"region",
|
|
10
|
+
"search"
|
|
11
|
+
]);
|
|
12
|
+
const INTERACTIVE_ROLES = new Set([
|
|
13
|
+
"button",
|
|
14
|
+
"checkbox",
|
|
15
|
+
"combobox",
|
|
16
|
+
"link",
|
|
17
|
+
"listbox",
|
|
18
|
+
"menu",
|
|
19
|
+
"menubar",
|
|
20
|
+
"menuitem",
|
|
21
|
+
"option",
|
|
22
|
+
"radio",
|
|
23
|
+
"scrollbar",
|
|
24
|
+
"searchbox",
|
|
25
|
+
"slider",
|
|
26
|
+
"spinbutton",
|
|
27
|
+
"switch",
|
|
28
|
+
"tab",
|
|
29
|
+
"tabpanel",
|
|
30
|
+
"textbox",
|
|
31
|
+
"treeitem"
|
|
32
|
+
]);
|
|
33
|
+
/**
|
|
34
|
+
* Compare two a11y tree snapshots and return the semantic diff.
|
|
35
|
+
*/
|
|
36
|
+
function diffA11yTrees(baseline, current) {
|
|
37
|
+
const changes = [];
|
|
38
|
+
diffNodes(baseline.tree, current.tree, "", changes);
|
|
39
|
+
const landmarkChanges = changes.filter((c) => c.type === "landmark-changed");
|
|
40
|
+
const hasRegression = changes.some((c) => c.severity === "error");
|
|
41
|
+
return {
|
|
42
|
+
testId: current.testId,
|
|
43
|
+
changes,
|
|
44
|
+
hasRegression,
|
|
45
|
+
landmarkChanges,
|
|
46
|
+
stats: {
|
|
47
|
+
added: changes.filter((c) => c.type === "node-added").length,
|
|
48
|
+
removed: changes.filter((c) => c.type === "node-removed").length,
|
|
49
|
+
modified: changes.filter((c) => c.type !== "node-added" && c.type !== "node-removed").length
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function diffNodes(before, after, path, changes) {
|
|
54
|
+
const nodePath = path ? `${path} > ${nodeLabel(after ?? before)}` : nodeLabel(after ?? before);
|
|
55
|
+
if (!before && after) {
|
|
56
|
+
changes.push({
|
|
57
|
+
type: isLandmark(after) ? "landmark-changed" : "node-added",
|
|
58
|
+
path: nodePath,
|
|
59
|
+
after: nodeSnapshot(after),
|
|
60
|
+
severity: "info",
|
|
61
|
+
description: `Added ${after.role}${after.name ? ` "${after.name}"` : ""}`
|
|
62
|
+
});
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (before && !after) {
|
|
66
|
+
const severity = isLandmark(before) ? "error" : isInteractive(before) ? "error" : "warning";
|
|
67
|
+
changes.push({
|
|
68
|
+
type: isLandmark(before) ? "landmark-changed" : "node-removed",
|
|
69
|
+
path: nodePath,
|
|
70
|
+
before: nodeSnapshot(before),
|
|
71
|
+
severity,
|
|
72
|
+
description: `Removed ${before.role}${before.name ? ` "${before.name}"` : ""}`
|
|
73
|
+
});
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (!before || !after) return;
|
|
77
|
+
if (before.role !== after.role) changes.push({
|
|
78
|
+
type: "role-changed",
|
|
79
|
+
path: nodePath,
|
|
80
|
+
before: { role: before.role },
|
|
81
|
+
after: { role: after.role },
|
|
82
|
+
severity: isInteractive(before) || isInteractive(after) ? "error" : "warning",
|
|
83
|
+
description: `Role changed from "${before.role}" to "${after.role}"`
|
|
84
|
+
});
|
|
85
|
+
if (before.name !== after.name) {
|
|
86
|
+
const severity = isInteractive(before) && !after.name ? "error" : "info";
|
|
87
|
+
changes.push({
|
|
88
|
+
type: "name-changed",
|
|
89
|
+
path: nodePath,
|
|
90
|
+
before: { name: before.name },
|
|
91
|
+
after: { name: after.name },
|
|
92
|
+
severity,
|
|
93
|
+
description: after.name ? `Name changed from "${before.name}" to "${after.name}"` : `Name removed (was "${before.name}")`
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
for (const key of [
|
|
97
|
+
"checked",
|
|
98
|
+
"disabled",
|
|
99
|
+
"expanded",
|
|
100
|
+
"pressed",
|
|
101
|
+
"selected"
|
|
102
|
+
]) if (before[key] !== after[key]) changes.push({
|
|
103
|
+
type: "state-changed",
|
|
104
|
+
path: nodePath,
|
|
105
|
+
before: { [key]: before[key] },
|
|
106
|
+
after: { [key]: after[key] },
|
|
107
|
+
severity: "info",
|
|
108
|
+
description: `${key} changed from ${before[key]} to ${after[key]}`
|
|
109
|
+
});
|
|
110
|
+
diffChildren(before.children ?? [], after.children ?? [], nodePath, changes);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Diff child node lists.
|
|
114
|
+
* 1) Exact match by role+name
|
|
115
|
+
* 2) Position-based matching for remaining -> detect role/name changes
|
|
116
|
+
* 3) Remainder is additions/removals
|
|
117
|
+
*/
|
|
118
|
+
function diffChildren(beforeChildren, afterChildren, parentPath, changes) {
|
|
119
|
+
const matched = /* @__PURE__ */ new Set();
|
|
120
|
+
const afterMatched = /* @__PURE__ */ new Set();
|
|
121
|
+
const beforeByKey = /* @__PURE__ */ new Map();
|
|
122
|
+
for (let i = 0; i < beforeChildren.length; i++) {
|
|
123
|
+
const key = childKey(beforeChildren[i]);
|
|
124
|
+
const list = beforeByKey.get(key) ?? [];
|
|
125
|
+
list.push({
|
|
126
|
+
node: beforeChildren[i],
|
|
127
|
+
index: i
|
|
128
|
+
});
|
|
129
|
+
beforeByKey.set(key, list);
|
|
130
|
+
}
|
|
131
|
+
for (let ai = 0; ai < afterChildren.length; ai++) {
|
|
132
|
+
const key = childKey(afterChildren[ai]);
|
|
133
|
+
const candidates = beforeByKey.get(key);
|
|
134
|
+
if (candidates && candidates.length > 0) {
|
|
135
|
+
const match = candidates.shift();
|
|
136
|
+
matched.add(match.index);
|
|
137
|
+
afterMatched.add(ai);
|
|
138
|
+
diffNodes(match.node, afterChildren[ai], parentPath, changes);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const unmatchedBefore = [];
|
|
142
|
+
for (let i = 0; i < beforeChildren.length; i++) if (!matched.has(i)) unmatchedBefore.push({
|
|
143
|
+
node: beforeChildren[i],
|
|
144
|
+
index: i
|
|
145
|
+
});
|
|
146
|
+
const unmatchedAfter = [];
|
|
147
|
+
for (let i = 0; i < afterChildren.length; i++) if (!afterMatched.has(i)) unmatchedAfter.push({
|
|
148
|
+
node: afterChildren[i],
|
|
149
|
+
index: i
|
|
150
|
+
});
|
|
151
|
+
const stillUnmatchedBefore = [];
|
|
152
|
+
const stillUnmatchedAfter = new Set(unmatchedAfter.map((_, i) => i));
|
|
153
|
+
for (const b of unmatchedBefore) {
|
|
154
|
+
let found = false;
|
|
155
|
+
for (let j = 0; j < unmatchedAfter.length; j++) {
|
|
156
|
+
if (!stillUnmatchedAfter.has(j)) continue;
|
|
157
|
+
const a = unmatchedAfter[j];
|
|
158
|
+
if (b.node.role === a.node.role || b.node.name && b.node.name === a.node.name) {
|
|
159
|
+
stillUnmatchedAfter.delete(j);
|
|
160
|
+
diffNodes(b.node, a.node, parentPath, changes);
|
|
161
|
+
found = true;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (!found) stillUnmatchedBefore.push(b);
|
|
166
|
+
}
|
|
167
|
+
for (const b of stillUnmatchedBefore) diffNodes(b.node, void 0, parentPath, changes);
|
|
168
|
+
for (const j of stillUnmatchedAfter) diffNodes(void 0, unmatchedAfter[j].node, parentPath, changes);
|
|
169
|
+
const countDiff = Math.abs(afterChildren.length - beforeChildren.length);
|
|
170
|
+
if (countDiff > 3 && countDiff / Math.max(beforeChildren.length, 1) > .3) changes.push({
|
|
171
|
+
type: "structure-changed",
|
|
172
|
+
path: parentPath,
|
|
173
|
+
before: { name: `${beforeChildren.length} children` },
|
|
174
|
+
after: { name: `${afterChildren.length} children` },
|
|
175
|
+
severity: "warning",
|
|
176
|
+
description: `Structure changed significantly: ${beforeChildren.length} → ${afterChildren.length} children`
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
function childKey(node) {
|
|
180
|
+
return `${node.role}:${node.name ?? ""}`;
|
|
181
|
+
}
|
|
182
|
+
function nodeLabel(node) {
|
|
183
|
+
const name = node.name ? `[${node.name}]` : "";
|
|
184
|
+
return `${node.role}${name}`;
|
|
185
|
+
}
|
|
186
|
+
function nodeSnapshot(node) {
|
|
187
|
+
return {
|
|
188
|
+
role: node.role,
|
|
189
|
+
name: node.name,
|
|
190
|
+
...node.level !== void 0 && { level: node.level },
|
|
191
|
+
...node.checked !== void 0 && { checked: node.checked },
|
|
192
|
+
...node.disabled !== void 0 && { disabled: node.disabled },
|
|
193
|
+
...node.expanded !== void 0 && { expanded: node.expanded }
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function isLandmark(node) {
|
|
197
|
+
return LANDMARK_ROLES.has(node.role);
|
|
198
|
+
}
|
|
199
|
+
function isInteractive(node) {
|
|
200
|
+
return INTERACTIVE_ROLES.has(node.role);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* A11y tree quality checks (standalone, no diff).
|
|
204
|
+
*
|
|
205
|
+
* Walks the tree and returns issues such as missing labels on
|
|
206
|
+
* interactive elements and missing alt text on images.
|
|
207
|
+
*
|
|
208
|
+
* @since 0.5.0 — replaces `checkA11yTree`.
|
|
209
|
+
*/
|
|
210
|
+
function verifyA11yTree(tree) {
|
|
211
|
+
const issues = [];
|
|
212
|
+
walkTree(tree, "", issues);
|
|
213
|
+
return issues;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* @deprecated since 0.5.0 — use `verifyA11yTree` instead. Removed in 1.0.0.
|
|
217
|
+
*/
|
|
218
|
+
const checkA11yTree = verifyA11yTree;
|
|
219
|
+
function walkTree(node, path, issues) {
|
|
220
|
+
const nodePath = path ? `${path} > ${nodeLabel(node)}` : nodeLabel(node);
|
|
221
|
+
if (isInteractive(node) && !node.name) issues.push({
|
|
222
|
+
path: nodePath,
|
|
223
|
+
rule: "label-missing",
|
|
224
|
+
severity: "error",
|
|
225
|
+
message: `Interactive element "${node.role}" has no accessible name`
|
|
226
|
+
});
|
|
227
|
+
if (node.role === "img" && !node.name) issues.push({
|
|
228
|
+
path: nodePath,
|
|
229
|
+
rule: "img-alt-missing",
|
|
230
|
+
severity: "error",
|
|
231
|
+
message: "Image has no accessible name (missing alt text)"
|
|
232
|
+
});
|
|
233
|
+
for (const child of node.children ?? []) walkTree(child, nodePath, issues);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Convert Playwright page.accessibility.snapshot() output to A11ySnapshot.
|
|
237
|
+
*/
|
|
238
|
+
function parsePlaywrightA11ySnapshot(testId, testTitle, raw) {
|
|
239
|
+
return {
|
|
240
|
+
testId,
|
|
241
|
+
testTitle,
|
|
242
|
+
tree: normalizeA11yNode(raw)
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
function normalizeA11yNode(raw) {
|
|
246
|
+
return {
|
|
247
|
+
role: raw.role ?? "none",
|
|
248
|
+
name: raw.name ?? "",
|
|
249
|
+
...raw.level !== void 0 && { level: raw.level },
|
|
250
|
+
...raw.value !== void 0 && { value: raw.value },
|
|
251
|
+
...raw.description !== void 0 && { description: raw.description },
|
|
252
|
+
...raw.checked !== void 0 && { checked: raw.checked },
|
|
253
|
+
...raw.disabled !== void 0 && { disabled: raw.disabled },
|
|
254
|
+
...raw.expanded !== void 0 && { expanded: raw.expanded },
|
|
255
|
+
...raw.pressed !== void 0 && { pressed: raw.pressed },
|
|
256
|
+
...raw.selected !== void 0 && { selected: raw.selected },
|
|
257
|
+
...raw.children ? { children: raw.children.map(normalizeA11yNode) } : {}
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
//#endregion
|
|
261
|
+
export { INTERACTIVE_ROLES, LANDMARK_ROLES, checkA11yTree, diffA11yTrees, parsePlaywrightA11ySnapshot, verifyA11yTree };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
//#region src/arg-reader.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Argv readers that take the array explicitly, and refuse to guess.
|
|
4
|
+
*
|
|
5
|
+
* `cli-args.ts` binds to `process.argv` at import time, which makes it
|
|
6
|
+
* untestable and unusable from a dispatcher-driven leaf. The hand-rolled
|
|
7
|
+
* readers that grew in its place all shared two silent failure modes:
|
|
8
|
+
*
|
|
9
|
+
* - `--output --json` consumed `--json` as the output directory;
|
|
10
|
+
* - `--concurrency abc` produced `NaN`, and `NaN` does not fail loudly. It
|
|
11
|
+
* made `runPool` build zero lanes and "successfully" run nothing.
|
|
12
|
+
*
|
|
13
|
+
* So a missing value is an error, a value that looks like another flag is an
|
|
14
|
+
* error, and a numeric flag is validated where it is read rather than wherever
|
|
15
|
+
* the NaN eventually lands.
|
|
16
|
+
*/
|
|
17
|
+
declare function hasFlag(argv: readonly string[], name: string): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Last occurrence wins, matching how shells and most CLIs behave when a flag is
|
|
20
|
+
* repeated. Throws when the value is missing or is another flag.
|
|
21
|
+
*/
|
|
22
|
+
declare function readFlag(argv: readonly string[], name: string): string | undefined;
|
|
23
|
+
/** Every occurrence, for repeatable flags (`--gate`, `--only`, `--pages`). */
|
|
24
|
+
declare function readAll(argv: readonly string[], name: string): string[];
|
|
25
|
+
interface NumberFlagOptions {
|
|
26
|
+
min?: number;
|
|
27
|
+
max?: number;
|
|
28
|
+
integer?: boolean;
|
|
29
|
+
}
|
|
30
|
+
declare function readNumber(argv: readonly string[], name: string, options?: NumberFlagOptions): number | undefined;
|
|
31
|
+
declare function readInt(argv: readonly string[], name: string, options?: NumberFlagOptions): number | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Arguments that are neither a flag nor a flag's value.
|
|
34
|
+
*
|
|
35
|
+
* The caller has to name the flags that take values, because argv alone cannot
|
|
36
|
+
* tell `--concurrency 4` (a value) from `--quiet page.html` (a positional). The
|
|
37
|
+
* hand-rolled version of this was a for-loop per CLI that advanced its own
|
|
38
|
+
* index, i.e. the same knowledge encoded three times.
|
|
39
|
+
*/
|
|
40
|
+
declare function readPositionals(argv: readonly string[], valueFlags?: readonly string[]): string[];
|
|
41
|
+
/**
|
|
42
|
+
* Split a command string into argv, honouring quotes.
|
|
43
|
+
*
|
|
44
|
+
* Needed because gate commands are authored as single strings — in a config
|
|
45
|
+
* file (`"check copy --manifest copy/press kit.txt"`) or on the command line
|
|
46
|
+
* (`--gate 'check breakpoints --mask ".hero, .promo"'`). Splitting on
|
|
47
|
+
* whitespace would hand the gate `kit.txt` and `.promo"` as separate
|
|
48
|
+
* arguments, which fails in a way that looks like the gate's fault.
|
|
49
|
+
*/
|
|
50
|
+
declare function tokenizeCommand(command: string): string[];
|
|
51
|
+
//#endregion
|
|
52
|
+
export { NumberFlagOptions, hasFlag, readAll, readFlag, readInt, readNumber, readPositionals, tokenizeCommand };
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { UsageError } from "./cli-error.mjs";
|
|
2
|
+
//#region src/arg-reader.ts
|
|
3
|
+
/**
|
|
4
|
+
* Argv readers that take the array explicitly, and refuse to guess.
|
|
5
|
+
*
|
|
6
|
+
* `cli-args.ts` binds to `process.argv` at import time, which makes it
|
|
7
|
+
* untestable and unusable from a dispatcher-driven leaf. The hand-rolled
|
|
8
|
+
* readers that grew in its place all shared two silent failure modes:
|
|
9
|
+
*
|
|
10
|
+
* - `--output --json` consumed `--json` as the output directory;
|
|
11
|
+
* - `--concurrency abc` produced `NaN`, and `NaN` does not fail loudly. It
|
|
12
|
+
* made `runPool` build zero lanes and "successfully" run nothing.
|
|
13
|
+
*
|
|
14
|
+
* So a missing value is an error, a value that looks like another flag is an
|
|
15
|
+
* error, and a numeric flag is validated where it is read rather than wherever
|
|
16
|
+
* the NaN eventually lands.
|
|
17
|
+
*/
|
|
18
|
+
const flagName = (name) => name.startsWith("-") ? name : `--${name}`;
|
|
19
|
+
/** A value must not itself look like a flag — `-1` is a value, `--json` is not. */
|
|
20
|
+
const looksLikeFlag = (value) => /^--/.test(value) || /^-[a-zA-Z]/.test(value);
|
|
21
|
+
function hasFlag(argv, name) {
|
|
22
|
+
return argv.includes(flagName(name));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Last occurrence wins, matching how shells and most CLIs behave when a flag is
|
|
26
|
+
* repeated. Throws when the value is missing or is another flag.
|
|
27
|
+
*/
|
|
28
|
+
function readFlag(argv, name) {
|
|
29
|
+
const flag = flagName(name);
|
|
30
|
+
const index = argv.lastIndexOf(flag);
|
|
31
|
+
if (index < 0) return void 0;
|
|
32
|
+
const value = argv[index + 1];
|
|
33
|
+
if (value === void 0) throw new UsageError(`${flag} needs a value`);
|
|
34
|
+
if (looksLikeFlag(value)) throw new UsageError(`${flag} needs a value, got the next flag ${value}`);
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
/** Every occurrence, for repeatable flags (`--gate`, `--only`, `--pages`). */
|
|
38
|
+
function readAll(argv, name) {
|
|
39
|
+
const flag = flagName(name);
|
|
40
|
+
const out = [];
|
|
41
|
+
for (let i = 0; i < argv.length; i++) {
|
|
42
|
+
if (argv[i] !== flag) continue;
|
|
43
|
+
const value = argv[i + 1];
|
|
44
|
+
if (value === void 0) throw new UsageError(`${flag} needs a value`);
|
|
45
|
+
if (looksLikeFlag(value)) throw new UsageError(`${flag} needs a value, got the next flag ${value}`);
|
|
46
|
+
out.push(value);
|
|
47
|
+
i++;
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
function readNumber(argv, name, options = {}) {
|
|
52
|
+
const raw = readFlag(argv, name);
|
|
53
|
+
if (raw === void 0) return void 0;
|
|
54
|
+
const flag = flagName(name);
|
|
55
|
+
const value = Number.parseFloat(raw);
|
|
56
|
+
if (!Number.isFinite(value) || !/^-?\d+(\.\d+)?$/.test(raw.trim())) throw new UsageError(`${flag} must be a number, got "${raw}"`);
|
|
57
|
+
if (options.integer && !Number.isInteger(value)) throw new UsageError(`${flag} must be a whole number, got "${raw}"`);
|
|
58
|
+
if (options.min !== void 0 && value < options.min) throw new UsageError(`${flag} must be >= ${options.min}, got ${value}`);
|
|
59
|
+
if (options.max !== void 0 && value > options.max) throw new UsageError(`${flag} must be <= ${options.max}, got ${value}`);
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
function readInt(argv, name, options = {}) {
|
|
63
|
+
return readNumber(argv, name, {
|
|
64
|
+
...options,
|
|
65
|
+
integer: true
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Arguments that are neither a flag nor a flag's value.
|
|
70
|
+
*
|
|
71
|
+
* The caller has to name the flags that take values, because argv alone cannot
|
|
72
|
+
* tell `--concurrency 4` (a value) from `--quiet page.html` (a positional). The
|
|
73
|
+
* hand-rolled version of this was a for-loop per CLI that advanced its own
|
|
74
|
+
* index, i.e. the same knowledge encoded three times.
|
|
75
|
+
*/
|
|
76
|
+
function readPositionals(argv, valueFlags = []) {
|
|
77
|
+
const takesValue = new Set(valueFlags.map(flagName));
|
|
78
|
+
const out = [];
|
|
79
|
+
for (let i = 0; i < argv.length; i++) {
|
|
80
|
+
const arg = argv[i];
|
|
81
|
+
if (takesValue.has(arg)) {
|
|
82
|
+
i++;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (arg.startsWith("-")) continue;
|
|
86
|
+
out.push(arg);
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Split a command string into argv, honouring quotes.
|
|
92
|
+
*
|
|
93
|
+
* Needed because gate commands are authored as single strings — in a config
|
|
94
|
+
* file (`"check copy --manifest copy/press kit.txt"`) or on the command line
|
|
95
|
+
* (`--gate 'check breakpoints --mask ".hero, .promo"'`). Splitting on
|
|
96
|
+
* whitespace would hand the gate `kit.txt` and `.promo"` as separate
|
|
97
|
+
* arguments, which fails in a way that looks like the gate's fault.
|
|
98
|
+
*/
|
|
99
|
+
function tokenizeCommand(command) {
|
|
100
|
+
const out = [];
|
|
101
|
+
let current = "";
|
|
102
|
+
let quote = null;
|
|
103
|
+
let started = false;
|
|
104
|
+
for (const char of command) {
|
|
105
|
+
if (quote) {
|
|
106
|
+
if (char === quote) quote = null;
|
|
107
|
+
else current += char;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (char === "\"" || char === "'") {
|
|
111
|
+
quote = char;
|
|
112
|
+
started = true;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (/\s/.test(char)) {
|
|
116
|
+
if (started) out.push(current);
|
|
117
|
+
current = "";
|
|
118
|
+
started = false;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
current += char;
|
|
122
|
+
started = true;
|
|
123
|
+
}
|
|
124
|
+
if (quote) throw new UsageError(`Unterminated ${quote} quote in command: ${command}`);
|
|
125
|
+
if (started) out.push(current);
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
export { hasFlag, readAll, readFlag, readInt, readNumber, readPositionals, tokenizeCommand };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//#region src/auth-state.d.ts
|
|
2
|
+
declare const STORAGE_STATE_ENV = "VLMKIT_STORAGE_STATE";
|
|
3
|
+
/**
|
|
4
|
+
* Explicit flag beats env; returns undefined when neither is set. A blank
|
|
5
|
+
* explicit value counts as "not passed" and falls through to the
|
|
6
|
+
* environment, so `--storage-state "$MAYBE_UNSET"` in a script does not
|
|
7
|
+
* silently disable an env-configured session.
|
|
8
|
+
*/
|
|
9
|
+
declare function storageStatePath(explicit?: string): string | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Validate eagerly and loudly. A silently ignored auth file would send the
|
|
12
|
+
* caller straight back to the failure this feature exists to fix — an
|
|
13
|
+
* unauthenticated measurement that looks like a real result.
|
|
14
|
+
*/
|
|
15
|
+
declare function readStorageState(path: string): {
|
|
16
|
+
cookies: unknown[];
|
|
17
|
+
origins: unknown[];
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Merge `storageState` into Playwright page options when configured.
|
|
21
|
+
* `browser.newPage(options)` forwards context options, so every gate can
|
|
22
|
+
* adopt this by wrapping its existing options object.
|
|
23
|
+
*/
|
|
24
|
+
declare function withAuthState<T extends object>(base: T, explicit?: string): T & {
|
|
25
|
+
storageState?: string;
|
|
26
|
+
};
|
|
27
|
+
/** One-line notice so a run that used auth says so in its output. */
|
|
28
|
+
declare function authStateNotice(explicit?: string): string | null;
|
|
29
|
+
//#endregion
|
|
30
|
+
export { STORAGE_STATE_ENV, authStateNotice, readStorageState, storageStatePath, withAuthState };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
//#region src/auth-state.ts
|
|
4
|
+
/**
|
|
5
|
+
* Authenticated-page support for the gates.
|
|
6
|
+
*
|
|
7
|
+
* The 2026-08-01 hard-target audit made the auth gap loud (a redirect to a
|
|
8
|
+
* login wall is now a reported defect instead of a silent CLEAN), but loud
|
|
9
|
+
* failure is not access: for most product apps the interesting pages —
|
|
10
|
+
* dashboard, checkout, settings — are all behind a session. This closes
|
|
11
|
+
* that by accepting a Playwright storage-state file, which is the format
|
|
12
|
+
* `playwright codegen --save-storage` and `context.storageState()` already
|
|
13
|
+
* produce, so teams reuse the artifact their e2e suite has.
|
|
14
|
+
*
|
|
15
|
+
* Two ways in, because gate invocations live in CI yaml / npm scripts and
|
|
16
|
+
* threading a flag through every one of them is the exact config sprawl
|
|
17
|
+
* this project is trying not to create:
|
|
18
|
+
* - `--storage-state <path>` on any URL-capable gate
|
|
19
|
+
* - `VLMKIT_STORAGE_STATE=<path>` for all of them at once
|
|
20
|
+
*
|
|
21
|
+
* An explicit flag always wins over the environment.
|
|
22
|
+
*/
|
|
23
|
+
const STORAGE_STATE_ENV = "VLMKIT_STORAGE_STATE";
|
|
24
|
+
/**
|
|
25
|
+
* Explicit flag beats env; returns undefined when neither is set. A blank
|
|
26
|
+
* explicit value counts as "not passed" and falls through to the
|
|
27
|
+
* environment, so `--storage-state "$MAYBE_UNSET"` in a script does not
|
|
28
|
+
* silently disable an env-configured session.
|
|
29
|
+
*/
|
|
30
|
+
function storageStatePath(explicit) {
|
|
31
|
+
for (const candidate of [explicit, process.env[STORAGE_STATE_ENV]]) if (candidate && candidate.trim() !== "") return candidate;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Validate eagerly and loudly. A silently ignored auth file would send the
|
|
35
|
+
* caller straight back to the failure this feature exists to fix — an
|
|
36
|
+
* unauthenticated measurement that looks like a real result.
|
|
37
|
+
*/
|
|
38
|
+
function readStorageState(path) {
|
|
39
|
+
const abs = resolve(path);
|
|
40
|
+
let raw;
|
|
41
|
+
try {
|
|
42
|
+
raw = readFileSync(abs, "utf8");
|
|
43
|
+
} catch {
|
|
44
|
+
throw new Error(`storage state file not found: ${abs}\nCreate one with: npx playwright codegen --save-storage=auth.json <your-login-url>`);
|
|
45
|
+
}
|
|
46
|
+
let parsed;
|
|
47
|
+
try {
|
|
48
|
+
parsed = JSON.parse(raw);
|
|
49
|
+
} catch {
|
|
50
|
+
throw new Error(`storage state file is not valid JSON: ${abs}`);
|
|
51
|
+
}
|
|
52
|
+
const obj = parsed;
|
|
53
|
+
if (!obj || typeof obj !== "object" || !Array.isArray(obj.cookies) && !Array.isArray(obj.origins)) throw new Error(`storage state file has no "cookies" or "origins" array: ${abs}\nExpected a Playwright storage-state file (context.storageState() output).`);
|
|
54
|
+
const cookies = Array.isArray(obj.cookies) ? obj.cookies : [];
|
|
55
|
+
const origins = Array.isArray(obj.origins) ? obj.origins : [];
|
|
56
|
+
if (cookies.length === 0 && origins.length === 0) throw new Error(`storage state file carries no cookies and no origins: ${abs}\nIt would authenticate nothing — re-capture it after logging in.`);
|
|
57
|
+
return {
|
|
58
|
+
cookies,
|
|
59
|
+
origins
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Merge `storageState` into Playwright page options when configured.
|
|
64
|
+
* `browser.newPage(options)` forwards context options, so every gate can
|
|
65
|
+
* adopt this by wrapping its existing options object.
|
|
66
|
+
*/
|
|
67
|
+
function withAuthState(base, explicit) {
|
|
68
|
+
const path = storageStatePath(explicit);
|
|
69
|
+
if (!path) return base;
|
|
70
|
+
readStorageState(path);
|
|
71
|
+
return {
|
|
72
|
+
...base,
|
|
73
|
+
storageState: resolve(path)
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/** One-line notice so a run that used auth says so in its output. */
|
|
77
|
+
function authStateNotice(explicit) {
|
|
78
|
+
const path = storageStatePath(explicit);
|
|
79
|
+
return path ? `auth: storage state from ${resolve(path)}` : null;
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
82
|
+
export { STORAGE_STATE_ENV, authStateNotice, readStorageState, storageStatePath, withAuthState };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//#region src/authored-style-capture.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Authored CSS capture for properties whose computed form lies about the
|
|
4
|
+
* authored shape.
|
|
5
|
+
*
|
|
6
|
+
* Motivating case (2026-05-23 dogfood, app-shell `.shell`):
|
|
7
|
+
* `getComputedStyle().getPropertyValue("grid-template-columns")` resolves
|
|
8
|
+
* `minmax(0, 1fr) 4px minmax(0, 1fr)` to a three px-tuple — but writing
|
|
9
|
+
* those numbers back as CSS would corrupt the responsive layout. The fix is
|
|
10
|
+
* not to track grid-template-* in computed-style at all; instead, walk
|
|
11
|
+
* `document.styleSheets.cssRules` to emit the AUTHORED strings and diff
|
|
12
|
+
* THEM. Same approach scales to `flex` shorthand, `transform`, and any
|
|
13
|
+
* other property whose computed form diverges from authored.
|
|
14
|
+
*
|
|
15
|
+
* Surface mirrors `computed-style-capture` so consumers can reuse
|
|
16
|
+
* `diffComputedStyles` (it's just `Record<selector, Record<prop, value>>`
|
|
17
|
+
* → `Record<selector, Record<prop, value>>` either way).
|
|
18
|
+
*/
|
|
19
|
+
type AuthoredStyleSnapshot = Record<string, Record<string, string>>;
|
|
20
|
+
/**
|
|
21
|
+
* Properties tracked by default. These are the ones whose computed
|
|
22
|
+
* representation differs structurally from the authored source — anything
|
|
23
|
+
* already faithful in `getComputedStyle` does not need this channel.
|
|
24
|
+
*/
|
|
25
|
+
declare const AUTHORED_PROPERTIES: string[];
|
|
26
|
+
/**
|
|
27
|
+
* Browser-context capture. Walks all same-origin stylesheets and nested
|
|
28
|
+
* `@media` / `@supports` rules, collecting authored `value` strings for
|
|
29
|
+
* each (scoped-selector, property) pair listed in `props`. Last
|
|
30
|
+
* declaration wins per pair (mirrors authoring order; sufficient for
|
|
31
|
+
* static diff use cases).
|
|
32
|
+
*
|
|
33
|
+
* Selectors are comma-split so `.a, .b { grid-template-columns: ... }`
|
|
34
|
+
* fans out into both `.a` and `.b` entries.
|
|
35
|
+
*/
|
|
36
|
+
declare function captureAuthoredStyleSnapshotInDom(props: string[]): AuthoredStyleSnapshot;
|
|
37
|
+
declare function buildAuthoredStyleCaptureExpression(props?: string[]): string;
|
|
38
|
+
declare function buildAuthoredStyleCaptureJsonExpression(props?: string[]): string;
|
|
39
|
+
declare function parseAuthoredStyleSnapshot(value: unknown): AuthoredStyleSnapshot;
|
|
40
|
+
declare function hasMeaningfulAuthoredStyleSnapshot(snapshot: AuthoredStyleSnapshot): boolean;
|
|
41
|
+
//#endregion
|
|
42
|
+
export { AUTHORED_PROPERTIES, AuthoredStyleSnapshot, buildAuthoredStyleCaptureExpression, buildAuthoredStyleCaptureJsonExpression, captureAuthoredStyleSnapshotInDom, hasMeaningfulAuthoredStyleSnapshot, parseAuthoredStyleSnapshot };
|