@jeffawe/understudy 0.0.0-stage → 0.2.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/LICENSE +201 -0
- package/README.md +166 -2
- package/REASONER.md +67 -0
- package/build/extension/firefox/background.js +186 -0
- package/build/extension/firefox/icons/icon-128.png +0 -0
- package/build/extension/firefox/icons/icon-16.png +0 -0
- package/build/extension/firefox/icons/icon-32.png +0 -0
- package/build/extension/firefox/icons/icon-48.png +0 -0
- package/build/extension/firefox/manifest.json +54 -0
- package/build/extension/firefox/popup.html +52 -0
- package/build/extension/firefox/popup.js +106 -0
- package/build/extension/firefox/recorder.js +362 -0
- package/dist/adapters/recorder/accname-bundle.d.ts +12 -0
- package/dist/adapters/recorder/accname-bundle.js +12 -0
- package/dist/adapters/recorder/extension.d.ts +57 -0
- package/dist/adapters/recorder/extension.js +146 -0
- package/dist/adapters/recorder/injected.d.ts +21 -0
- package/dist/adapters/recorder/injected.js +352 -0
- package/dist/adapters/recorder/live.d.ts +105 -0
- package/dist/adapters/recorder/live.js +317 -0
- package/dist/adapters/recorder/script.d.ts +43 -0
- package/dist/adapters/recorder/script.js +530 -0
- package/dist/adapters/recorder/wire.d.ts +42 -0
- package/dist/adapters/recorder/wire.js +51 -0
- package/dist/core/auth-cache.d.ts +119 -0
- package/dist/core/auth-cache.js +216 -0
- package/dist/core/browser.d.ts +47 -0
- package/dist/core/browser.js +82 -0
- package/dist/core/distill.d.ts +118 -0
- package/dist/core/distill.js +197 -0
- package/dist/core/fingerprint.d.ts +15 -0
- package/dist/core/fingerprint.js +21 -0
- package/dist/core/recording-store.d.ts +34 -0
- package/dist/core/recording-store.js +64 -0
- package/dist/core/recording.d.ts +185 -0
- package/dist/core/recording.js +100 -0
- package/dist/core/replay.d.ts +307 -0
- package/dist/core/replay.js +804 -0
- package/dist/core/sig.d.ts +70 -0
- package/dist/core/sig.js +0 -0
- package/dist/core/trigger.d.ts +18 -0
- package/dist/core/trigger.js +35 -0
- package/dist/core/types.d.ts +28 -0
- package/dist/core/types.js +8 -0
- package/dist/core/visual.d.ts +80 -0
- package/dist/core/visual.js +111 -0
- package/dist/entry/cli.d.ts +9 -0
- package/dist/entry/cli.js +1016 -0
- package/dist/entry/mcp.d.ts +20 -0
- package/dist/entry/mcp.js +288 -0
- package/dist/flows/destructive.d.ts +19 -0
- package/dist/flows/destructive.js +48 -0
- package/dist/flows/distill.d.ts +28 -0
- package/dist/flows/distill.js +109 -0
- package/dist/flows/drift.d.ts +43 -0
- package/dist/flows/drift.js +116 -0
- package/dist/flows/emit.d.ts +23 -0
- package/dist/flows/emit.js +133 -0
- package/dist/flows/execute.d.ts +75 -0
- package/dist/flows/execute.js +133 -0
- package/dist/flows/findings.d.ts +38 -0
- package/dist/flows/findings.js +136 -0
- package/dist/flows/runs.d.ts +42 -0
- package/dist/flows/runs.js +93 -0
- package/dist/flows/save.d.ts +74 -0
- package/dist/flows/save.js +171 -0
- package/dist/memory/notes.d.ts +89 -0
- package/dist/memory/notes.js +163 -0
- package/dist/memory/placeholders.d.ts +41 -0
- package/dist/memory/placeholders.js +139 -0
- package/dist/memory/recall.d.ts +118 -0
- package/dist/memory/recall.js +147 -0
- package/dist/memory/vocabulary.d.ts +9 -0
- package/dist/memory/vocabulary.js +24 -0
- package/dist/plan/planner.d.ts +69 -0
- package/dist/plan/planner.js +211 -0
- package/dist/plan/seams.d.ts +58 -0
- package/dist/plan/seams.js +99 -0
- package/dist/store/db.d.ts +62 -0
- package/dist/store/db.js +183 -0
- package/dist/store/embed.d.ts +46 -0
- package/dist/store/embed.js +100 -0
- package/dist/store/reembed.d.ts +13 -0
- package/dist/store/reembed.js +32 -0
- package/dist/store/settings.d.ts +67 -0
- package/dist/store/settings.js +151 -0
- package/dist/store/types.d.ts +220 -0
- package/dist/store/types.js +14 -0
- package/extension/README.md +76 -0
- package/extension/background.js +186 -0
- package/extension/icons/icon-128.png +0 -0
- package/extension/icons/icon-16.png +0 -0
- package/extension/icons/icon-32.png +0 -0
- package/extension/icons/icon-48.png +0 -0
- package/extension/manifest.json +38 -0
- package/extension/popup.html +52 -0
- package/extension/popup.js +106 -0
- package/extension/recorder.js +362 -0
- package/package.json +68 -4
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sig() — a structural fingerprint of a page state.
|
|
3
|
+
*
|
|
4
|
+
* NOT visual. No screenshot, no pixels, no vision model. Visual drift is
|
|
5
|
+
* explicitly out of scope; this exists so the agent can answer "where am I?"
|
|
6
|
+
* by comparing two strings, which is what lets it follow a map instead of
|
|
7
|
+
* replaying a script.
|
|
8
|
+
*
|
|
9
|
+
* Four ingredients, and saucedemo demonstrates why each is needed:
|
|
10
|
+
*
|
|
11
|
+
* url pattern — `/inventory.html` served the LOGIN page when unauthenticated.
|
|
12
|
+
* URL alone would have said "arrived" when it hadn't.
|
|
13
|
+
* title — `Swag Labs` on every page. Useless alone, useful combined.
|
|
14
|
+
* landmarks — coarse page shape; survives copy changes.
|
|
15
|
+
* top names — the accessible names of the first N interactive controls.
|
|
16
|
+
* This is what actually separates two pages that share a title.
|
|
17
|
+
*
|
|
18
|
+
* The output is `<url_pattern>#<8 hex>` — readable enough that a flow-drift
|
|
19
|
+
* diff reads as `/login#1a2b3c4d → /inventory#c3d4e5f6`, structural enough that
|
|
20
|
+
* a real change to the page moves the hash.
|
|
21
|
+
*/
|
|
22
|
+
import type { Page } from 'playwright';
|
|
23
|
+
/**
|
|
24
|
+
* Normalize a URL to a route shape: origin dropped (envs differ), numeric and
|
|
25
|
+
* uuid segments replaced, query and hash dropped, trailing slash trimmed.
|
|
26
|
+
*
|
|
27
|
+
* Pure and exported so it can be unit-tested without a browser.
|
|
28
|
+
*/
|
|
29
|
+
export declare function urlPattern(raw: string): string;
|
|
30
|
+
export interface PageSig {
|
|
31
|
+
sig: string;
|
|
32
|
+
urlPattern: string;
|
|
33
|
+
title: string;
|
|
34
|
+
landmarks: string[];
|
|
35
|
+
names: string[];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Parse Playwright's aria snapshot into (role, name) pairs.
|
|
39
|
+
*
|
|
40
|
+
* The snapshot is YAML-ish: `- button "Login"`, `- textbox "Username"`,
|
|
41
|
+
* `- heading "..." [level=4]`. Names may be single- or double-quoted because
|
|
42
|
+
* YAML re-quotes strings that contain quotes.
|
|
43
|
+
*
|
|
44
|
+
* A line with a role but NO quoted name is an element with no accessible name.
|
|
45
|
+
* Those are kept deliberately — they're the `unnamed` refusal class, and the
|
|
46
|
+
* dismiss button in saucedemo's error banner is exactly one.
|
|
47
|
+
*/
|
|
48
|
+
export declare function parseAria(snapshot: string): Array<{
|
|
49
|
+
role: string;
|
|
50
|
+
name: string | null;
|
|
51
|
+
}>;
|
|
52
|
+
/**
|
|
53
|
+
* Block until the accessibility tree stops changing, or `maxMs` elapses.
|
|
54
|
+
*
|
|
55
|
+
* In-page reveals resolve no navigation, so `waitForLoadState` returns
|
|
56
|
+
* instantly and a sig taken straight after matches the PRE-click state — the
|
|
57
|
+
* reveal then looks like it changed nothing and gets discarded. That race is
|
|
58
|
+
* why saucedemo's burger menu was never captured and `Logout` never entered
|
|
59
|
+
* the corpus.
|
|
60
|
+
*
|
|
61
|
+
* Polling to stability rather than sleeping a fixed interval: a click that
|
|
62
|
+
* changes nothing costs one poll, while a slow drawer gets as long as it needs.
|
|
63
|
+
* A fixed delay has to be sized for the worst case and then pays it every time.
|
|
64
|
+
*/
|
|
65
|
+
export declare function waitForAriaStable(page: Page, maxMs?: number, pollMs?: number, requireContent?: boolean): Promise<string>;
|
|
66
|
+
/**
|
|
67
|
+
* Fingerprint the page's CURRENT state. Call after the page has settled — a
|
|
68
|
+
* sig computed mid-navigation describes a state that never really existed.
|
|
69
|
+
*/
|
|
70
|
+
export declare function computeSig(page: Page, known?: string): Promise<PageSig>;
|
package/dist/core/sig.js
ADDED
|
Binary file
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Is this lesson trigger one that can fire as written?
|
|
3
|
+
*
|
|
4
|
+
* Lessons match by JSONB containment against a `StepContext` (lessons.ts), and
|
|
5
|
+
* containment makes two mistakes silent. An empty trigger is contained by
|
|
6
|
+
* EVERY context, so it fires on every step. A key the context never carries
|
|
7
|
+
* (`urlPattern` for `url_pattern`) is contained by NO context, so the lesson
|
|
8
|
+
* never fires and still looks exactly like one whose step simply has not come
|
|
9
|
+
* up yet — observed on a real corpus, on the one lesson that explained why a
|
|
10
|
+
* page rendered empty.
|
|
11
|
+
*
|
|
12
|
+
* Every writer of `lessons` runs this: remember, distill, triage. Lives in its
|
|
13
|
+
* own module so `distill.ts` can validate without importing the database.
|
|
14
|
+
*/
|
|
15
|
+
/** The only keys a step context ever carries — see `StepContext` in lessons.ts. */
|
|
16
|
+
export declare const TRIGGER_KEYS: readonly ["url_pattern", "action", "role", "name"];
|
|
17
|
+
/** Everything wrong with a trigger, or [] when it is usable. */
|
|
18
|
+
export declare function triggerProblems(trigger: unknown): string[];
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Is this lesson trigger one that can fire as written?
|
|
3
|
+
*
|
|
4
|
+
* Lessons match by JSONB containment against a `StepContext` (lessons.ts), and
|
|
5
|
+
* containment makes two mistakes silent. An empty trigger is contained by
|
|
6
|
+
* EVERY context, so it fires on every step. A key the context never carries
|
|
7
|
+
* (`urlPattern` for `url_pattern`) is contained by NO context, so the lesson
|
|
8
|
+
* never fires and still looks exactly like one whose step simply has not come
|
|
9
|
+
* up yet — observed on a real corpus, on the one lesson that explained why a
|
|
10
|
+
* page rendered empty.
|
|
11
|
+
*
|
|
12
|
+
* Every writer of `lessons` runs this: remember, distill, triage. Lives in its
|
|
13
|
+
* own module so `distill.ts` can validate without importing the database.
|
|
14
|
+
*/
|
|
15
|
+
/** The only keys a step context ever carries — see `StepContext` in lessons.ts. */
|
|
16
|
+
export const TRIGGER_KEYS = ['url_pattern', 'action', 'role', 'name'];
|
|
17
|
+
/** Everything wrong with a trigger, or [] when it is usable. */
|
|
18
|
+
export function triggerProblems(trigger) {
|
|
19
|
+
if (typeof trigger !== 'object' || trigger === null || Array.isArray(trigger)) {
|
|
20
|
+
return ['trigger must be an object'];
|
|
21
|
+
}
|
|
22
|
+
const keys = Object.keys(trigger);
|
|
23
|
+
if (!keys.length)
|
|
24
|
+
return ['trigger is empty — it would match every step'];
|
|
25
|
+
const problems = [];
|
|
26
|
+
for (const k of keys) {
|
|
27
|
+
if (!TRIGGER_KEYS.includes(k)) {
|
|
28
|
+
problems.push(`trigger key "${k}" is never in a step context, so the lesson could never fire — use ${TRIGGER_KEYS.join(', ')}`);
|
|
29
|
+
}
|
|
30
|
+
else if (typeof trigger[k] !== 'string') {
|
|
31
|
+
problems.push(`trigger.${k} must be a string`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return problems;
|
|
35
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pause-and-ask contract. Deterministic code never calls a model: when it
|
|
3
|
+
* reaches a judgement it can't make alone, it suspends with a PendingDecision,
|
|
4
|
+
* and whoever is consulted (an agent over MCP) answers it.
|
|
5
|
+
*
|
|
6
|
+
* Embeddings live in src/store/embed.ts; the distill contract in distill.ts.
|
|
7
|
+
*/
|
|
8
|
+
export interface PendingDecision {
|
|
9
|
+
kind: 'gap' | 'seam' | 'unexpected_page' | 'step_failed' | 'visual_diff' | 'finding_judgment' | 'parameter'
|
|
10
|
+
/**
|
|
11
|
+
* The executor is stuck and wants a human to RECORD the missing piece.
|
|
12
|
+
*
|
|
13
|
+
* Deliberately not 'gap': that word already means a RETRIEVAL gap — top
|
|
14
|
+
* distance at or above GAP_DISTANCE, a statement about the corpus made at
|
|
15
|
+
* planning time (see REASONER.md). This is an execution-time statement
|
|
16
|
+
* about a live browser sitting on a known page, and overloading the word
|
|
17
|
+
* would make the one document that matters ambiguous.
|
|
18
|
+
*/
|
|
19
|
+
| 'needs_capture';
|
|
20
|
+
context: Record<string, unknown>;
|
|
21
|
+
}
|
|
22
|
+
export interface Reasoner {
|
|
23
|
+
readonly id: string;
|
|
24
|
+
/** Break a goal into sub-goals, using the app's own vocabulary. */
|
|
25
|
+
decompose(goal: string, vocabulary: string[]): Promise<string[]>;
|
|
26
|
+
/** Answer a judgment call the deterministic executor can't make alone. */
|
|
27
|
+
resolve(decision: PendingDecision): Promise<Record<string, unknown>>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pause-and-ask contract. Deterministic code never calls a model: when it
|
|
3
|
+
* reaches a judgement it can't make alone, it suspends with a PendingDecision,
|
|
4
|
+
* and whoever is consulted (an agent over MCP) answers it.
|
|
5
|
+
*
|
|
6
|
+
* Embeddings live in src/store/embed.ts; the distill contract in distill.ts.
|
|
7
|
+
*/
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Visual checkpoints — the one class of defect the rest of this system is blind to.
|
|
3
|
+
*
|
|
4
|
+
* Everything else Understudy notices is structural or behavioural: a console
|
|
5
|
+
* error, a non-2xx body, a locator that matched nothing, a page fingerprint
|
|
6
|
+
* that moved. None of it can see that the Submit button is now white on white,
|
|
7
|
+
* that a panel collapsed to zero height, or that the price renders as NaN.
|
|
8
|
+
* `sig()` deliberately fingerprints STRUCTURE — url pattern, title, landmarks,
|
|
9
|
+
* control names — so a page can be visually destroyed while its sig is
|
|
10
|
+
* unchanged.
|
|
11
|
+
*
|
|
12
|
+
* WHY THE JUDGE IS A MODEL AND THE FILTER IS NOT.
|
|
13
|
+
*
|
|
14
|
+
* Pixel diffing alone is famously unusable: a changed timestamp, an animation
|
|
15
|
+
* frame mid-flight, font hinting on a different machine — all produce diffs,
|
|
16
|
+
* and none of them are bugs. A threshold cannot tell "the date moved" from
|
|
17
|
+
* "the button is gone", so a threshold-only system trains people to ignore it.
|
|
18
|
+
*
|
|
19
|
+
* So this file does only the cheap, deterministic half: capture, compare,
|
|
20
|
+
* produce a ratio. Anything above the floor is handed to the reasoner WITH THE
|
|
21
|
+
* IMAGES, and it decides what the change means. Same ladder as everywhere else
|
|
22
|
+
* — free mechanical checks first, the model last, and only on what survived.
|
|
23
|
+
*
|
|
24
|
+
* WHERE THE CHECKPOINTS COME FROM. Not every step: a run would produce sixty
|
|
25
|
+
* images and drown the judge. The imported spec already says where visuals
|
|
26
|
+
* matter — its `checkpoint(...)` / `toHaveScreenshot(...)` calls are a human's
|
|
27
|
+
* own judgement about which moments are worth a picture. That is far better
|
|
28
|
+
* signal than any heuristic this file could invent, and it was being discarded.
|
|
29
|
+
*/
|
|
30
|
+
import type { Page } from 'playwright';
|
|
31
|
+
/** Gitignored: these are large, machine-local, and regenerable. */
|
|
32
|
+
export declare const SHOTS_DIR: string;
|
|
33
|
+
/**
|
|
34
|
+
* Below this fraction of changed pixels, say nothing.
|
|
35
|
+
*
|
|
36
|
+
* Antialiasing and subpixel text rendering alone move a few hundred pixels on
|
|
37
|
+
* a full-page shot. Escalating those would make every run produce a question.
|
|
38
|
+
*/
|
|
39
|
+
export declare const VISUAL_MIN = 0.002;
|
|
40
|
+
/**
|
|
41
|
+
* Above this, stop the run rather than drive forty more steps into it.
|
|
42
|
+
*
|
|
43
|
+
* A third of the page changing is not a moved button — it is a blank render,
|
|
44
|
+
* an error screen, or a layout that collapsed. `sig()` can miss all three,
|
|
45
|
+
* because landmarks and control names survive a page that looks destroyed.
|
|
46
|
+
*/
|
|
47
|
+
export declare const VISUAL_SEVERE = 0.35;
|
|
48
|
+
export interface VisualCheck {
|
|
49
|
+
seq: number;
|
|
50
|
+
/** The name the spec gave this checkpoint, e.g. "02-review-summary". */
|
|
51
|
+
label: string;
|
|
52
|
+
currentPath: string;
|
|
53
|
+
baselinePath?: string;
|
|
54
|
+
/** Where the highlighted difference was written, when there was one. */
|
|
55
|
+
diffPath?: string;
|
|
56
|
+
/** Fraction of pixels that differ. Undefined when there was no baseline. */
|
|
57
|
+
ratio?: number;
|
|
58
|
+
/** First sighting of this label — it BECOMES the baseline, nothing to judge. */
|
|
59
|
+
isNew: boolean;
|
|
60
|
+
/** Dimensions changed, so a pixel comparison is meaningless. */
|
|
61
|
+
resized?: boolean;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Take the shot, compare it to the baseline, and report.
|
|
65
|
+
*
|
|
66
|
+
* `fullPage` and the toast mask are both copied from the spec helper this
|
|
67
|
+
* replaces — a run-to-run popup on a timer is exactly the kind of thing that
|
|
68
|
+
* makes pixel comparison worthless, and whoever wrote that helper had already
|
|
69
|
+
* been bitten by it.
|
|
70
|
+
*/
|
|
71
|
+
export declare function captureCheckpoint(page: Page, opts: {
|
|
72
|
+
appSlug: string;
|
|
73
|
+
runId: string;
|
|
74
|
+
seq: number;
|
|
75
|
+
label: string;
|
|
76
|
+
}): Promise<VisualCheck>;
|
|
77
|
+
/** Checks worth a judgement — everything below the floor is dropped. */
|
|
78
|
+
export declare const worthJudging: (checks: VisualCheck[]) => VisualCheck[];
|
|
79
|
+
/** Accept the current shot as the new truth, once a change is judged expected. */
|
|
80
|
+
export declare function acceptBaseline(check: VisualCheck): Promise<void>;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Visual checkpoints — the one class of defect the rest of this system is blind to.
|
|
3
|
+
*
|
|
4
|
+
* Everything else Understudy notices is structural or behavioural: a console
|
|
5
|
+
* error, a non-2xx body, a locator that matched nothing, a page fingerprint
|
|
6
|
+
* that moved. None of it can see that the Submit button is now white on white,
|
|
7
|
+
* that a panel collapsed to zero height, or that the price renders as NaN.
|
|
8
|
+
* `sig()` deliberately fingerprints STRUCTURE — url pattern, title, landmarks,
|
|
9
|
+
* control names — so a page can be visually destroyed while its sig is
|
|
10
|
+
* unchanged.
|
|
11
|
+
*
|
|
12
|
+
* WHY THE JUDGE IS A MODEL AND THE FILTER IS NOT.
|
|
13
|
+
*
|
|
14
|
+
* Pixel diffing alone is famously unusable: a changed timestamp, an animation
|
|
15
|
+
* frame mid-flight, font hinting on a different machine — all produce diffs,
|
|
16
|
+
* and none of them are bugs. A threshold cannot tell "the date moved" from
|
|
17
|
+
* "the button is gone", so a threshold-only system trains people to ignore it.
|
|
18
|
+
*
|
|
19
|
+
* So this file does only the cheap, deterministic half: capture, compare,
|
|
20
|
+
* produce a ratio. Anything above the floor is handed to the reasoner WITH THE
|
|
21
|
+
* IMAGES, and it decides what the change means. Same ladder as everywhere else
|
|
22
|
+
* — free mechanical checks first, the model last, and only on what survived.
|
|
23
|
+
*
|
|
24
|
+
* WHERE THE CHECKPOINTS COME FROM. Not every step: a run would produce sixty
|
|
25
|
+
* images and drown the judge. The imported spec already says where visuals
|
|
26
|
+
* matter — its `checkpoint(...)` / `toHaveScreenshot(...)` calls are a human's
|
|
27
|
+
* own judgement about which moments are worth a picture. That is far better
|
|
28
|
+
* signal than any heuristic this file could invent, and it was being discarded.
|
|
29
|
+
*/
|
|
30
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
31
|
+
import { existsSync } from 'node:fs';
|
|
32
|
+
import { dirname, join, resolve } from 'node:path';
|
|
33
|
+
import { PNG } from 'pngjs';
|
|
34
|
+
import pixelmatch from 'pixelmatch';
|
|
35
|
+
/** Gitignored: these are large, machine-local, and regenerable. */
|
|
36
|
+
export const SHOTS_DIR = process.env.UNDERSTUDY_SHOTS_DIR ?? resolve('.understudy/shots');
|
|
37
|
+
/**
|
|
38
|
+
* Below this fraction of changed pixels, say nothing.
|
|
39
|
+
*
|
|
40
|
+
* Antialiasing and subpixel text rendering alone move a few hundred pixels on
|
|
41
|
+
* a full-page shot. Escalating those would make every run produce a question.
|
|
42
|
+
*/
|
|
43
|
+
export const VISUAL_MIN = 0.002;
|
|
44
|
+
/**
|
|
45
|
+
* Above this, stop the run rather than drive forty more steps into it.
|
|
46
|
+
*
|
|
47
|
+
* A third of the page changing is not a moved button — it is a blank render,
|
|
48
|
+
* an error screen, or a layout that collapsed. `sig()` can miss all three,
|
|
49
|
+
* because landmarks and control names survive a page that looks destroyed.
|
|
50
|
+
*/
|
|
51
|
+
export const VISUAL_SEVERE = 0.35;
|
|
52
|
+
const baselinePathFor = (appSlug, label) => join(SHOTS_DIR, appSlug, 'baseline', `${label}.png`);
|
|
53
|
+
const currentPathFor = (appSlug, runId, label) => join(SHOTS_DIR, appSlug, 'runs', runId, `${label}.png`);
|
|
54
|
+
async function write(path, data) {
|
|
55
|
+
await mkdir(dirname(path), { recursive: true });
|
|
56
|
+
await writeFile(path, data);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Take the shot, compare it to the baseline, and report.
|
|
60
|
+
*
|
|
61
|
+
* `fullPage` and the toast mask are both copied from the spec helper this
|
|
62
|
+
* replaces — a run-to-run popup on a timer is exactly the kind of thing that
|
|
63
|
+
* makes pixel comparison worthless, and whoever wrote that helper had already
|
|
64
|
+
* been bitten by it.
|
|
65
|
+
*/
|
|
66
|
+
export async function captureCheckpoint(page, opts) {
|
|
67
|
+
const { appSlug, runId, seq, label } = opts;
|
|
68
|
+
const shot = await page.screenshot({
|
|
69
|
+
fullPage: true,
|
|
70
|
+
mask: [page.getByRole('status'), page.getByRole('alert')],
|
|
71
|
+
});
|
|
72
|
+
const currentPath = currentPathFor(appSlug, runId, label);
|
|
73
|
+
await write(currentPath, shot);
|
|
74
|
+
const baselinePath = baselinePathFor(appSlug, label);
|
|
75
|
+
if (!existsSync(baselinePath)) {
|
|
76
|
+
// FIRST SIGHTING IS THE BASELINE. There is nothing to judge against, and
|
|
77
|
+
// inventing a verdict here would be pure noise on every new checkpoint.
|
|
78
|
+
await write(baselinePath, shot);
|
|
79
|
+
return { seq, label, currentPath, baselinePath, isNew: true };
|
|
80
|
+
}
|
|
81
|
+
const baseline = PNG.sync.read(await readFile(baselinePath));
|
|
82
|
+
const current = PNG.sync.read(shot);
|
|
83
|
+
if (baseline.width !== current.width || baseline.height !== current.height) {
|
|
84
|
+
// A full-page shot changes height whenever content does, so this is common
|
|
85
|
+
// and NOT automatically a defect — but a pixel ratio would be nonsense.
|
|
86
|
+
return {
|
|
87
|
+
seq, label, currentPath, baselinePath, isNew: false, resized: true,
|
|
88
|
+
ratio: 1,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
const diff = new PNG({ width: baseline.width, height: baseline.height });
|
|
92
|
+
const changed = pixelmatch(baseline.data, current.data, diff.data, baseline.width, baseline.height,
|
|
93
|
+
// Slightly forgiving: below this, a pixel counts as unchanged. Text
|
|
94
|
+
// rendering differs by a shade or two between runs on the same machine.
|
|
95
|
+
{ threshold: 0.15, includeAA: false });
|
|
96
|
+
const ratio = changed / (baseline.width * baseline.height);
|
|
97
|
+
if (ratio < VISUAL_MIN) {
|
|
98
|
+
return { seq, label, currentPath, baselinePath, ratio, isNew: false };
|
|
99
|
+
}
|
|
100
|
+
const diffPath = currentPath.replace(/\.png$/, '.diff.png');
|
|
101
|
+
await write(diffPath, PNG.sync.write(diff));
|
|
102
|
+
return { seq, label, currentPath, baselinePath, diffPath, ratio, isNew: false };
|
|
103
|
+
}
|
|
104
|
+
/** Checks worth a judgement — everything below the floor is dropped. */
|
|
105
|
+
export const worthJudging = (checks) => checks.filter((c) => !c.isNew && (c.ratio ?? 0) >= VISUAL_MIN);
|
|
106
|
+
/** Accept the current shot as the new truth, once a change is judged expected. */
|
|
107
|
+
export async function acceptBaseline(check) {
|
|
108
|
+
if (!check.baselinePath)
|
|
109
|
+
return;
|
|
110
|
+
await write(check.baselinePath, await readFile(check.currentPath));
|
|
111
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* understudy — command line. Agents use the MCP server (entry/mcp.ts) instead;
|
|
4
|
+
* both call the same functions underneath.
|
|
5
|
+
*
|
|
6
|
+
* Argument parsing is hand-rolled on purpose: the surface is small, and a
|
|
7
|
+
* parser library would be more code than it saves.
|
|
8
|
+
*/
|
|
9
|
+
export {};
|