screw-bugs 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +906 -2
- package/bin/screw-bugs.js +29 -0
- package/dist/browser-playwright/annotate.d.ts +9 -0
- package/dist/browser-playwright/annotate.js +86 -0
- package/dist/browser-playwright/capture.d.ts +232 -0
- package/dist/browser-playwright/capture.js +1049 -0
- package/dist/browser-playwright/frames.d.ts +77 -0
- package/dist/browser-playwright/frames.js +191 -0
- package/dist/browser-playwright/index.d.ts +2 -0
- package/dist/browser-playwright/index.js +2 -0
- package/dist/browser-playwright/input.d.ts +44 -0
- package/dist/browser-playwright/input.js +116 -0
- package/dist/browser-playwright/launch.d.ts +21 -0
- package/dist/browser-playwright/launch.js +86 -0
- package/dist/browser-playwright/page.d.ts +387 -0
- package/dist/browser-playwright/page.js +1930 -0
- package/dist/build-info.json +5 -0
- package/dist/cli/app.d.ts +19 -0
- package/dist/cli/app.js +70 -0
- package/dist/cli/build-info.d.ts +52 -0
- package/dist/cli/build-info.js +82 -0
- package/dist/cli/commands.d.ts +14 -0
- package/dist/cli/commands.js +308 -0
- package/dist/cli/config.d.ts +16 -0
- package/dist/cli/config.js +71 -0
- package/dist/cli/doctor.d.ts +19 -0
- package/dist/cli/doctor.js +113 -0
- package/dist/cli/environment.d.ts +20 -0
- package/dist/cli/environment.js +34 -0
- package/dist/cli/exit-codes.d.ts +29 -0
- package/dist/cli/exit-codes.js +28 -0
- package/dist/cli/failure.d.ts +20 -0
- package/dist/cli/failure.js +15 -0
- package/dist/cli/index.d.ts +6 -0
- package/dist/cli/index.js +6 -0
- package/dist/cli/install-method.d.ts +96 -0
- package/dist/cli/install-method.js +148 -0
- package/dist/cli/main.d.ts +1 -0
- package/dist/cli/main.js +10 -0
- package/dist/cli/mcp.d.ts +30 -0
- package/dist/cli/mcp.js +309 -0
- package/dist/cli/node-version.d.ts +3 -0
- package/dist/cli/node-version.js +22 -0
- package/dist/cli/operations.d.ts +127 -0
- package/dist/cli/operations.js +337 -0
- package/dist/cli/package-info.d.ts +22 -0
- package/dist/cli/package-info.js +25 -0
- package/dist/cli/report-server.d.ts +170 -0
- package/dist/cli/report-server.js +387 -0
- package/dist/cli/signals.d.ts +37 -0
- package/dist/cli/signals.js +61 -0
- package/dist/cli/stamp-build.d.ts +1 -0
- package/dist/cli/stamp-build.js +9 -0
- package/dist/cli/update-check.d.ts +140 -0
- package/dist/cli/update-check.js +316 -0
- package/dist/cli/uploads.d.ts +16 -0
- package/dist/cli/uploads.js +80 -0
- package/dist/core/css.d.ts +95 -0
- package/dist/core/css.js +436 -0
- package/dist/core/engine.d.ts +65 -0
- package/dist/core/engine.js +436 -0
- package/dist/core/errors.d.ts +66 -0
- package/dist/core/errors.js +30 -0
- package/dist/core/evidence.d.ts +14 -0
- package/dist/core/evidence.js +127 -0
- package/dist/core/index.d.ts +12 -0
- package/dist/core/index.js +12 -0
- package/dist/core/labels.d.ts +47 -0
- package/dist/core/labels.js +199 -0
- package/dist/core/liveness.d.ts +45 -0
- package/dist/core/liveness.js +64 -0
- package/dist/core/marks.d.ts +63 -0
- package/dist/core/marks.js +410 -0
- package/dist/core/services.d.ts +444 -0
- package/dist/core/services.js +22 -0
- package/dist/core/steps.d.ts +96 -0
- package/dist/core/steps.js +656 -0
- package/dist/core/uploads.d.ts +49 -0
- package/dist/core/uploads.js +133 -0
- package/dist/core/video.d.ts +16 -0
- package/dist/core/video.js +101 -0
- package/dist/core/viewport.d.ts +28 -0
- package/dist/core/viewport.js +50 -0
- package/dist/report/html.d.ts +37 -0
- package/dist/report/html.js +762 -0
- package/dist/report/index.d.ts +5 -0
- package/dist/report/index.js +5 -0
- package/dist/report/markup.d.ts +27 -0
- package/dist/report/markup.js +75 -0
- package/dist/report/pr-summary.d.ts +11 -0
- package/dist/report/pr-summary.js +110 -0
- package/dist/report/sha256.d.ts +9 -0
- package/dist/report/sha256.js +97 -0
- package/dist/report/styles.d.ts +1 -0
- package/dist/report/styles.js +328 -0
- package/dist/report/view.d.ts +141 -0
- package/dist/report/view.js +368 -0
- package/dist/report/walkthrough.d.ts +75 -0
- package/dist/report/walkthrough.js +458 -0
- package/dist/schema/config.d.ts +7 -0
- package/dist/schema/config.js +6 -0
- package/dist/schema/doctor.d.ts +46 -0
- package/dist/schema/doctor.js +23 -0
- package/dist/schema/evidence.d.ts +994 -0
- package/dist/schema/evidence.js +521 -0
- package/dist/schema/findings.d.ts +159 -0
- package/dist/schema/findings.js +80 -0
- package/dist/schema/index.d.ts +7 -0
- package/dist/schema/index.js +7 -0
- package/dist/schema/job.d.ts +1724 -0
- package/dist/schema/job.js +505 -0
- package/dist/schema/ledger.d.ts +951 -0
- package/dist/schema/ledger.js +193 -0
- package/dist/schema/status.d.ts +7 -0
- package/dist/schema/status.js +11 -0
- package/dist/store-local/accounts.d.ts +7 -0
- package/dist/store-local/accounts.js +9 -0
- package/dist/store-local/index.d.ts +3 -0
- package/dist/store-local/index.js +3 -0
- package/dist/store-local/paths.d.ts +10 -0
- package/dist/store-local/paths.js +17 -0
- package/dist/store-local/storage.d.ts +20 -0
- package/dist/store-local/storage.js +170 -0
- package/package.json +73 -4
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { baseName, casesOf, uploadFileOf, } from '../schema/index.js';
|
|
2
|
+
import { describeStep, withStartPage } from './steps.js';
|
|
3
|
+
/** What every report and evidence pack says once about how cases ran. */
|
|
4
|
+
export const SESSION_NOTE = 'Cases ran one after another, in job order, in one browser session: cookies and storage ' +
|
|
5
|
+
'carried over from one case to the next.';
|
|
6
|
+
const stepState = (entry, index, ran) => {
|
|
7
|
+
if (entry.status === 'passed')
|
|
8
|
+
return 'ran';
|
|
9
|
+
if (entry.step !== undefined && (entry.status === 'failed' || entry.status === 'blocked'))
|
|
10
|
+
return index < entry.step ? 'ran' : index === entry.step ? 'decided' : 'not-run';
|
|
11
|
+
return ran ? 'ran' : 'not-run';
|
|
12
|
+
};
|
|
13
|
+
/** The timecodes key moments of a case may use: its start, each step's start, and its end. */
|
|
14
|
+
const timecodesOf = (entry) => {
|
|
15
|
+
const all = [entry.startMs, ...(entry.steps ?? []).map((s) => s.startMs), entry.endMs].flatMap((ms) => (ms === undefined ? [] : [Math.round(ms)]));
|
|
16
|
+
return [...new Set(all)].toSorted((a, b) => a - b);
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* The evidence pack of a run: per case what an agent needs to write it up (status, deciding step,
|
|
20
|
+
* reason, step timecodes, screenshot IDs, page facts), and the IDs its findings may use. Built
|
|
21
|
+
* from the job and ledger alone, so it is the same whoever builds it.
|
|
22
|
+
*/
|
|
23
|
+
export const evidencePackOf = (job, ledger) => {
|
|
24
|
+
const specs = new Map(casesOf(job.spec).map((spec) => [spec.id, spec]));
|
|
25
|
+
const cases = ledger.cases.map((entry) => {
|
|
26
|
+
const spec = specs.get(entry.id);
|
|
27
|
+
const steps = spec ? withStartPage(spec, job.spec.url) : [];
|
|
28
|
+
const offset = spec ? steps.length - spec.steps.length : 0;
|
|
29
|
+
const timings = new Map((entry.steps ?? []).map((s) => [s.index, s]));
|
|
30
|
+
return {
|
|
31
|
+
id: entry.id,
|
|
32
|
+
title: entry.title,
|
|
33
|
+
status: entry.status,
|
|
34
|
+
...(entry.step !== undefined ? { step: entry.step } : {}),
|
|
35
|
+
...(entry.reason !== undefined ? { reason: entry.reason } : {}),
|
|
36
|
+
...(spec?.success?.trim() ? { success: spec.success } : {}),
|
|
37
|
+
...(entry.continuedFrom !== undefined ? { continuedFrom: entry.continuedFrom } : {}),
|
|
38
|
+
...(entry.session !== undefined ? { session: entry.session } : {}),
|
|
39
|
+
...(entry.startMs !== undefined ? { startMs: Math.round(entry.startMs) } : {}),
|
|
40
|
+
...(entry.endMs !== undefined ? { endMs: Math.round(entry.endMs) } : {}),
|
|
41
|
+
steps: steps.map((step, i) => {
|
|
42
|
+
const index = i + 1 - offset;
|
|
43
|
+
const timing = timings.get(index);
|
|
44
|
+
return {
|
|
45
|
+
index,
|
|
46
|
+
text: describeStep(step),
|
|
47
|
+
state: stepState(entry, index, timing !== undefined),
|
|
48
|
+
...(step.action === 'upload'
|
|
49
|
+
? { files: step.files.map((file) => baseName(uploadFileOf(file).path)) }
|
|
50
|
+
: {}),
|
|
51
|
+
...(timing
|
|
52
|
+
? { startMs: Math.round(timing.startMs), endMs: Math.round(timing.endMs) }
|
|
53
|
+
: {}),
|
|
54
|
+
};
|
|
55
|
+
}),
|
|
56
|
+
timecodes: timecodesOf(entry),
|
|
57
|
+
shots: (entry.shots ?? []).map((shot) => ({
|
|
58
|
+
id: shot.id,
|
|
59
|
+
name: shot.name,
|
|
60
|
+
kind: shot.kind,
|
|
61
|
+
step: shot.step,
|
|
62
|
+
path: shot.path,
|
|
63
|
+
...(shot.page !== undefined ? { page: shot.page } : {}),
|
|
64
|
+
})),
|
|
65
|
+
...(entry.facts !== undefined ? { facts: entry.facts } : {}),
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
return {
|
|
69
|
+
version: 1,
|
|
70
|
+
jobId: ledger.jobId,
|
|
71
|
+
state: ledger.state,
|
|
72
|
+
sessionNote: SESSION_NOTE,
|
|
73
|
+
videos: (ledger.sessions ?? []).map((session) => ({
|
|
74
|
+
session: session.number,
|
|
75
|
+
...(session.video !== undefined ? { path: session.video } : {}),
|
|
76
|
+
...(session.videoDurationMs !== undefined ? { durationMs: session.videoDurationMs } : {}),
|
|
77
|
+
...(session.videoProblem !== undefined ? { problem: session.videoProblem } : {}),
|
|
78
|
+
})),
|
|
79
|
+
cases,
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
const list = (values, max = 12) => {
|
|
83
|
+
const shown = values.slice(0, max).join(', ');
|
|
84
|
+
return values.length > max ? `${shown}, …` : shown;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Everything in `findings` that does not fit the run, one plain line each, saying what to change:
|
|
88
|
+
* case IDs, screenshot IDs and timecodes it does not have. Empty when the findings fit.
|
|
89
|
+
*/
|
|
90
|
+
export const checkFindings = (findings, pack) => {
|
|
91
|
+
const problems = [];
|
|
92
|
+
const cases = new Map(pack.cases.map((c) => [c.id, c]));
|
|
93
|
+
const ids = list(pack.cases.map((c) => c.id));
|
|
94
|
+
const unknownCase = (where, id) => problems.push(`${where}: "${id}" is not a case of this run; use one of: ${ids}.`);
|
|
95
|
+
findings.fixes.forEach((fix, i) => fix.caseIds.forEach((id, j) => {
|
|
96
|
+
if (!cases.has(id))
|
|
97
|
+
unknownCase(`fixes[${i}].caseIds[${j}]`, id);
|
|
98
|
+
}));
|
|
99
|
+
const seen = new Set();
|
|
100
|
+
findings.cases.forEach((entry, i) => {
|
|
101
|
+
const evidence = cases.get(entry.id);
|
|
102
|
+
if (evidence === undefined)
|
|
103
|
+
return unknownCase(`cases[${i}].id`, entry.id);
|
|
104
|
+
if (seen.has(entry.id))
|
|
105
|
+
problems.push(`cases[${i}].id: case "${entry.id}" is written up twice; merge the two.`);
|
|
106
|
+
seen.add(entry.id);
|
|
107
|
+
const shots = evidence.shots.map((s) => s.id);
|
|
108
|
+
(entry.highlights ?? []).forEach((highlight, j) => {
|
|
109
|
+
if (shots.includes(highlight.shot))
|
|
110
|
+
return;
|
|
111
|
+
problems.push(`cases[${i}].highlights[${j}].shot: "${highlight.shot}" is not a screenshot of case ` +
|
|
112
|
+
`"${entry.id}"; ` +
|
|
113
|
+
(shots.length > 0
|
|
114
|
+
? `use one of: ${list(shots)}.`
|
|
115
|
+
: 'it has none, so leave its highlights out.'));
|
|
116
|
+
});
|
|
117
|
+
(entry.keyMoments ?? []).forEach((moment, j) => {
|
|
118
|
+
if (evidence.timecodes.includes(moment.ms))
|
|
119
|
+
return;
|
|
120
|
+
problems.push(`cases[${i}].keyMoments[${j}].ms: ${moment.ms} is not a timecode of case "${entry.id}"; ` +
|
|
121
|
+
(evidence.timecodes.length > 0
|
|
122
|
+
? `use one of: ${list(evidence.timecodes)}.`
|
|
123
|
+
: 'it has none, so leave its key moments out.'));
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
return problems;
|
|
127
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from './engine.js';
|
|
2
|
+
export * from './errors.js';
|
|
3
|
+
export * from './evidence.js';
|
|
4
|
+
export * from './labels.js';
|
|
5
|
+
export * from './css.js';
|
|
6
|
+
export * from './marks.js';
|
|
7
|
+
export * from './services.js';
|
|
8
|
+
export * from './steps.js';
|
|
9
|
+
export * from './liveness.js';
|
|
10
|
+
export * from './video.js';
|
|
11
|
+
export * from './uploads.js';
|
|
12
|
+
export * from './viewport.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from './engine.js';
|
|
2
|
+
export * from './errors.js';
|
|
3
|
+
export * from './evidence.js';
|
|
4
|
+
export * from './labels.js';
|
|
5
|
+
export * from './css.js';
|
|
6
|
+
export * from './marks.js';
|
|
7
|
+
export * from './services.js';
|
|
8
|
+
export * from './steps.js';
|
|
9
|
+
export * from './liveness.js';
|
|
10
|
+
export * from './video.js';
|
|
11
|
+
export * from './uploads.js';
|
|
12
|
+
export * from './viewport.js';
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Box, ShotPage } from '../schema/index.js';
|
|
2
|
+
/** Above or below the box, lined up with its start or end; beside it; or, with no room at all, in it. */
|
|
3
|
+
export type LabelSpot = 'above-start' | 'above-end' | 'below-start' | 'below-end' | 'right' | 'left' | 'inside';
|
|
4
|
+
export interface Size {
|
|
5
|
+
readonly width: number;
|
|
6
|
+
readonly height: number;
|
|
7
|
+
}
|
|
8
|
+
/** Where a label goes: a spot, moved `shift` image pixels to the right to sit after another label. */
|
|
9
|
+
export interface LabelPlace {
|
|
10
|
+
readonly spot: LabelSpot;
|
|
11
|
+
readonly shift: number;
|
|
12
|
+
}
|
|
13
|
+
/** The area a label of `size` takes at `place`, `gap` away from the edge of `box`. */
|
|
14
|
+
export declare const labelArea: (box: Box, size: Size, { spot, shift }: LabelPlace, gap: number) => Box;
|
|
15
|
+
/** The part of `box` inside `frame`: a box drawn past the image's edge would be cut off there. */
|
|
16
|
+
export declare const clipBox: (box: Box, frame: Box) => Box;
|
|
17
|
+
/**
|
|
18
|
+
* `box` grown by `by` on every side: the area a line drawn `by` outside an element takes, so the
|
|
19
|
+
* line goes around the element and never over it.
|
|
20
|
+
*/
|
|
21
|
+
export declare const outsetBox: (box: Box, by: number) => Box;
|
|
22
|
+
/**
|
|
23
|
+
* The boxes of the elements saved with a screenshot that labels should keep off: the innermost
|
|
24
|
+
* ones (elements with no saved element inside them), visible and on the image, rounded to whole
|
|
25
|
+
* pixels. Outer ones such as a form or the page's main area would leave a label nowhere to go.
|
|
26
|
+
*/
|
|
27
|
+
export declare const elementBoxes: (page: ShotPage) => ReadonlyArray<Box>;
|
|
28
|
+
/**
|
|
29
|
+
* A place for each box's label, in the boxes' order. Preferred: above the box, then below it (or
|
|
30
|
+
* the other way round), lined up with its start, its end, or right after a label already there
|
|
31
|
+
* (when two boxes mark one element); then beside it. The first that stays in the image, covers no
|
|
32
|
+
* label placed before it and no other box, and has room: not on a side where another box is too
|
|
33
|
+
* close for the label to fit between them (it goes to the far side from that box, or beside its
|
|
34
|
+
* own box), and not touching another box. When none does, the one that does least harm: leaving
|
|
35
|
+
* the image is worst (the label would be cut off), then sitting in its own box, then covering
|
|
36
|
+
* another label, then covering other boxes (the less of them the better), then covering other
|
|
37
|
+
* elements of the page (`obstacles`, such as a button next to the marked text: the less the
|
|
38
|
+
* better), then being squeezed against one. Obstacles that hold the box, or are its own element,
|
|
39
|
+
* do not count. Last, lined up with the end of a box much wider than the label, far from its start.
|
|
40
|
+
* Sizes and `gap` are in the image's pixels.
|
|
41
|
+
*/
|
|
42
|
+
export declare const placeLabels: (frame: Size, boxes: ReadonlyArray<{
|
|
43
|
+
readonly box: Box;
|
|
44
|
+
readonly label: Size;
|
|
45
|
+
/** Try below the box first, as for a field, whose own label is usually just above it. */
|
|
46
|
+
readonly prefer?: 'above' | 'below';
|
|
47
|
+
}>, gap: number, obstacles?: ReadonlyArray<Box>) => ReadonlyArray<LabelPlace>;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/** The area a label of `size` takes at `place`, `gap` away from the edge of `box`. */
|
|
2
|
+
export const labelArea = (box, size, { spot, shift }, gap) => {
|
|
3
|
+
const start = box.x + shift;
|
|
4
|
+
const end = box.x + box.width - size.width;
|
|
5
|
+
const above = box.y - gap - size.height;
|
|
6
|
+
const below = box.y + box.height + gap;
|
|
7
|
+
switch (spot) {
|
|
8
|
+
case 'above-start':
|
|
9
|
+
return { x: start, y: above, ...size };
|
|
10
|
+
case 'above-end':
|
|
11
|
+
return { x: end, y: above, ...size };
|
|
12
|
+
case 'below-start':
|
|
13
|
+
return { x: start, y: below, ...size };
|
|
14
|
+
case 'below-end':
|
|
15
|
+
return { x: end, y: below, ...size };
|
|
16
|
+
case 'right':
|
|
17
|
+
return { x: box.x + box.width + gap, y: box.y, ...size };
|
|
18
|
+
case 'left':
|
|
19
|
+
return { x: box.x - gap - size.width, y: box.y, ...size };
|
|
20
|
+
case 'inside':
|
|
21
|
+
return { x: box.x, y: box.y, ...size };
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
/** The part of `box` inside `frame`: a box drawn past the image's edge would be cut off there. */
|
|
25
|
+
export const clipBox = (box, frame) => {
|
|
26
|
+
const x = Math.max(box.x, frame.x);
|
|
27
|
+
const y = Math.max(box.y, frame.y);
|
|
28
|
+
return {
|
|
29
|
+
x,
|
|
30
|
+
y,
|
|
31
|
+
width: Math.max(0, Math.min(box.x + box.width, frame.x + frame.width) - x),
|
|
32
|
+
height: Math.max(0, Math.min(box.y + box.height, frame.y + frame.height) - y),
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
const overlap = (a, b) => a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height;
|
|
36
|
+
/** The area two boxes share. */
|
|
37
|
+
const common = (a, b) => Math.max(0, Math.min(a.x + a.width, b.x + b.width) - Math.max(a.x, b.x)) *
|
|
38
|
+
Math.max(0, Math.min(a.y + a.height, b.y + b.height) - Math.max(a.y, b.y));
|
|
39
|
+
const within = (area, frame) => area.x >= 0 &&
|
|
40
|
+
area.y >= 0 &&
|
|
41
|
+
area.x + area.width <= frame.width &&
|
|
42
|
+
area.y + area.height <= frame.height;
|
|
43
|
+
/**
|
|
44
|
+
* `box` grown by `by` on every side: the area a line drawn `by` outside an element takes, so the
|
|
45
|
+
* line goes around the element and never over it.
|
|
46
|
+
*/
|
|
47
|
+
export const outsetBox = (box, by) => ({
|
|
48
|
+
x: box.x - by,
|
|
49
|
+
y: box.y - by,
|
|
50
|
+
width: box.width + 2 * by,
|
|
51
|
+
height: box.height + 2 * by,
|
|
52
|
+
});
|
|
53
|
+
const grow = outsetBox;
|
|
54
|
+
const contains = (outer, inner) => outer.x <= inner.x &&
|
|
55
|
+
outer.y <= inner.y &&
|
|
56
|
+
outer.x + outer.width >= inner.x + inner.width &&
|
|
57
|
+
outer.y + outer.height >= inner.y + inner.height;
|
|
58
|
+
/**
|
|
59
|
+
* The boxes of the elements saved with a screenshot that labels should keep off: the innermost
|
|
60
|
+
* ones (elements with no saved element inside them), visible and on the image, rounded to whole
|
|
61
|
+
* pixels. Outer ones such as a form or the page's main area would leave a label nowhere to go.
|
|
62
|
+
*/
|
|
63
|
+
export const elementBoxes = (page) => {
|
|
64
|
+
const holders = new Set(page.candidates.flatMap((c) => (c.parent === undefined ? [] : [c.parent])));
|
|
65
|
+
const seen = new Set();
|
|
66
|
+
return page.candidates.flatMap((c, i) => {
|
|
67
|
+
if (holders.has(i) || c.visible === false)
|
|
68
|
+
return [];
|
|
69
|
+
// An element in a frame shows only where its frame does.
|
|
70
|
+
const shown = c.frame === undefined ? undefined : page.frames?.[c.frame]?.box;
|
|
71
|
+
const box = shown === undefined ? c.box : clipBox(c.box, shown);
|
|
72
|
+
const x = Math.max(0, Math.floor(box.x));
|
|
73
|
+
const y = Math.max(0, Math.floor(box.y));
|
|
74
|
+
const right = Math.min(page.width, Math.ceil(box.x + box.width));
|
|
75
|
+
const bottom = Math.min(page.height, Math.ceil(box.y + box.height));
|
|
76
|
+
const key = `${x},${y},${right},${bottom}`;
|
|
77
|
+
if (right <= x || bottom <= y || seen.has(key))
|
|
78
|
+
return [];
|
|
79
|
+
seen.add(key);
|
|
80
|
+
return [{ x, y, width: right - x, height: bottom - y }];
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
/** Whether two boxes mark one element (or nearly): they share at least half of the smaller one. */
|
|
84
|
+
const sameElement = (a, b) => common(a, b) >= 0.5 * Math.min(a.width * a.height, b.width * b.height);
|
|
85
|
+
const sideOf = (spot) => spot === 'inside'
|
|
86
|
+
? undefined
|
|
87
|
+
: spot === 'right' || spot === 'left'
|
|
88
|
+
? spot
|
|
89
|
+
: spot.startsWith('above')
|
|
90
|
+
? 'above'
|
|
91
|
+
: 'below';
|
|
92
|
+
/**
|
|
93
|
+
* The sides of `box` with another box too close for a label to fit between them with room: such as
|
|
94
|
+
* a field's own label, marked just above the field. A label on that side would sit squeezed in the
|
|
95
|
+
* gap, so each box's label goes to its far side from the other.
|
|
96
|
+
*/
|
|
97
|
+
const crowdedSides = (box, others, label, gap) => {
|
|
98
|
+
const sides = new Set();
|
|
99
|
+
const [top, bottom, left, right] = [box.y, box.y + box.height, box.x, box.x + box.width];
|
|
100
|
+
for (const other of others) {
|
|
101
|
+
if (sameElement(box, other))
|
|
102
|
+
continue;
|
|
103
|
+
const [oTop, oBottom, oLeft, oRight] = [
|
|
104
|
+
other.y,
|
|
105
|
+
other.y + other.height,
|
|
106
|
+
other.x,
|
|
107
|
+
other.x + other.width,
|
|
108
|
+
];
|
|
109
|
+
const [midY, midX] = [(oTop + oBottom) / 2, (oLeft + oRight) / 2];
|
|
110
|
+
const across = oLeft < right && left < oRight;
|
|
111
|
+
const along = oTop < bottom && top < oBottom;
|
|
112
|
+
const tall = label.height + 2 * gap;
|
|
113
|
+
const wide = label.width + 2 * gap;
|
|
114
|
+
if (across && midY < top && top - oBottom < tall)
|
|
115
|
+
sides.add('above');
|
|
116
|
+
if (across && midY > bottom && oTop - bottom < tall)
|
|
117
|
+
sides.add('below');
|
|
118
|
+
if (along && midX < left && left - oRight < wide)
|
|
119
|
+
sides.add('left');
|
|
120
|
+
if (along && midX > right && oLeft - right < wide)
|
|
121
|
+
sides.add('right');
|
|
122
|
+
}
|
|
123
|
+
return sides;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* A place for each box's label, in the boxes' order. Preferred: above the box, then below it (or
|
|
127
|
+
* the other way round), lined up with its start, its end, or right after a label already there
|
|
128
|
+
* (when two boxes mark one element); then beside it. The first that stays in the image, covers no
|
|
129
|
+
* label placed before it and no other box, and has room: not on a side where another box is too
|
|
130
|
+
* close for the label to fit between them (it goes to the far side from that box, or beside its
|
|
131
|
+
* own box), and not touching another box. When none does, the one that does least harm: leaving
|
|
132
|
+
* the image is worst (the label would be cut off), then sitting in its own box, then covering
|
|
133
|
+
* another label, then covering other boxes (the less of them the better), then covering other
|
|
134
|
+
* elements of the page (`obstacles`, such as a button next to the marked text: the less the
|
|
135
|
+
* better), then being squeezed against one. Obstacles that hold the box, or are its own element,
|
|
136
|
+
* do not count. Last, lined up with the end of a box much wider than the label, far from its start.
|
|
137
|
+
* Sizes and `gap` are in the image's pixels.
|
|
138
|
+
*/
|
|
139
|
+
export const placeLabels = (frame, boxes, gap, obstacles = []) => {
|
|
140
|
+
const taken = [];
|
|
141
|
+
return boxes.map(({ box, label, prefer }, i) => {
|
|
142
|
+
const others = boxes.filter((_, j) => j !== i).map((other) => other.box);
|
|
143
|
+
const around = obstacles.filter((o) => !sameElement(box, o) && !contains(o, box));
|
|
144
|
+
const crowded = crowdedSides(box, others, label, gap);
|
|
145
|
+
// Other boxes a label may not touch: not those of this box's own element.
|
|
146
|
+
const apart = others.filter((other) => !sameElement(box, other));
|
|
147
|
+
// After a label already placed that starts over this box (the other box on the element).
|
|
148
|
+
const after = (spot) => taken
|
|
149
|
+
.filter((other) => other.x >= box.x - gap && other.x <= box.x + box.width)
|
|
150
|
+
.map((other) => ({ spot, shift: other.x + other.width + gap - box.x }));
|
|
151
|
+
const above = [
|
|
152
|
+
{ spot: 'above-start', shift: 0 },
|
|
153
|
+
{ spot: 'above-end', shift: 0 },
|
|
154
|
+
...after('above-start'),
|
|
155
|
+
];
|
|
156
|
+
const below = [
|
|
157
|
+
{ spot: 'below-start', shift: 0 },
|
|
158
|
+
{ spot: 'below-end', shift: 0 },
|
|
159
|
+
...after('below-start'),
|
|
160
|
+
];
|
|
161
|
+
const places = [
|
|
162
|
+
...(prefer === 'below' ? [...below, ...above] : [...above, ...below]),
|
|
163
|
+
{ spot: 'right', shift: 0 },
|
|
164
|
+
{ spot: 'left', shift: 0 },
|
|
165
|
+
{ spot: 'inside', shift: 0 },
|
|
166
|
+
];
|
|
167
|
+
let best = places.at(-1);
|
|
168
|
+
let least = Infinity;
|
|
169
|
+
for (const place of places) {
|
|
170
|
+
const area = labelArea(box, label, place, gap);
|
|
171
|
+
// How much of the label lies over other boxes, as a share of it: less is better.
|
|
172
|
+
const covered = Math.min(1, others.reduce((sum, other) => sum + common(area, other), 0) /
|
|
173
|
+
Math.max(1, area.width * area.height));
|
|
174
|
+
// How much of it lies over the page's other elements: covering one is avoided, not barred.
|
|
175
|
+
const hidden = Math.min(1, around.reduce((sum, other) => sum + common(area, other), 0) /
|
|
176
|
+
Math.max(1, area.width * area.height));
|
|
177
|
+
const side = sideOf(place.spot);
|
|
178
|
+
// Within a gap of another box (its edge is less than `gap` away), or on a crowded side.
|
|
179
|
+
const squeezed = (side !== undefined && crowded.has(side)) ||
|
|
180
|
+
apart.some((other) => overlap(grow(area, gap * 0.9), other));
|
|
181
|
+
// Lined up with the end of a box much wider than it, a label is far from where the box's
|
|
182
|
+
// content starts (the text of a line across the page): fine, but worse than next to it.
|
|
183
|
+
const far = place.spot.endsWith('-end') && box.width > 4 * label.width;
|
|
184
|
+
const harm = (within(area, frame) ? 0 : 16) +
|
|
185
|
+
(place.spot === 'inside' ? 8 : 0) +
|
|
186
|
+
(taken.some((other) => overlap(area, other)) ? 4 : 0) +
|
|
187
|
+
(covered > 0 ? 1 + covered / 2 : 0) +
|
|
188
|
+
(hidden > 0 ? 0.25 + hidden / 2 : 0) +
|
|
189
|
+
(squeezed ? 0.5 : 0) +
|
|
190
|
+
(far ? 0.1 : 0);
|
|
191
|
+
if (harm < least)
|
|
192
|
+
[best, least] = [place, harm];
|
|
193
|
+
if (harm === 0)
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
taken.push(labelArea(box, label, best, gap));
|
|
197
|
+
return best;
|
|
198
|
+
});
|
|
199
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Context } from 'effect';
|
|
2
|
+
import type { CaseLedger } from '../schema/index.js';
|
|
3
|
+
/** How often a running job rewrites `heartbeatAt` in its ledger. */
|
|
4
|
+
export declare const HEARTBEAT_INTERVAL_MS = 5000;
|
|
5
|
+
/**
|
|
6
|
+
* A queued or running ledger whose heartbeat is older than this belongs to a process that is gone
|
|
7
|
+
* (killed, crashed, or the machine lost power). Several intervals, so a busy process is not
|
|
8
|
+
* mistaken for a dead one.
|
|
9
|
+
*/
|
|
10
|
+
export declare const HEARTBEAT_STALE_MS: number;
|
|
11
|
+
/**
|
|
12
|
+
* How long a run's heartbeat must have been stale before a status read writes its
|
|
13
|
+
* `report-interrupted.html`, showing it errored. Far beyond `HEARTBEAT_STALE_MS`: a process that
|
|
14
|
+
* is only paused (a laptop asleep) is still alive, and may resume and write its own report.
|
|
15
|
+
*/
|
|
16
|
+
export declare const REPORT_STALE_MS: number;
|
|
17
|
+
/** The job error of a run whose process disappeared without finishing it. */
|
|
18
|
+
export declare const PROCESS_GONE = "The process running this job stopped unexpectedly.";
|
|
19
|
+
/** The job error of a run that was interrupted, such as by Ctrl+C or SIGTERM. */
|
|
20
|
+
export declare const RUN_INTERRUPTED = "The run was stopped before it finished.";
|
|
21
|
+
/** The reason a case gets when its run ended before reaching it. */
|
|
22
|
+
export declare const STOPPED_BEFORE_CASE = "The run stopped before this case.";
|
|
23
|
+
/**
|
|
24
|
+
* ID of the operating-system process running jobs, recorded in each ledger it writes. Core stays
|
|
25
|
+
* platform-free, so the CLI provides it; undefined leaves it out of the ledger.
|
|
26
|
+
*/
|
|
27
|
+
export declare const ProcessId: Context.Reference<number | undefined>;
|
|
28
|
+
/**
|
|
29
|
+
* The version of screw-bugs running jobs, recorded in each ledger. The CLI provides it from its
|
|
30
|
+
* package.json; undefined leaves it out.
|
|
31
|
+
*/
|
|
32
|
+
export declare const AppVersion: Context.Reference<string | undefined>;
|
|
33
|
+
/** Marks every case that has no result yet as not verified because the run stopped first. */
|
|
34
|
+
export declare const markUnreached: (cases: CaseLedger['cases'], reason?: string) => CaseLedger['cases'];
|
|
35
|
+
/**
|
|
36
|
+
* The ledger as it should be reported at `nowMs`: a queued or running job whose heartbeat is
|
|
37
|
+
* stale (or missing) is reported errored, with its unreached cases not verified. Any other ledger
|
|
38
|
+
* is returned unchanged.
|
|
39
|
+
*/
|
|
40
|
+
export declare const withLiveness: (ledger: CaseLedger, nowMs: number) => CaseLedger;
|
|
41
|
+
/**
|
|
42
|
+
* How long ago the heartbeat of a ledger that `withLiveness` reports errored was written; its
|
|
43
|
+
* process may only be paused. Undefined for a ledger that its own process ended (or never ended).
|
|
44
|
+
*/
|
|
45
|
+
export declare const staleFor: (ledger: CaseLedger, nowMs: number) => number | undefined;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Context } from 'effect';
|
|
2
|
+
/** How often a running job rewrites `heartbeatAt` in its ledger. */
|
|
3
|
+
export const HEARTBEAT_INTERVAL_MS = 5_000;
|
|
4
|
+
/**
|
|
5
|
+
* A queued or running ledger whose heartbeat is older than this belongs to a process that is gone
|
|
6
|
+
* (killed, crashed, or the machine lost power). Several intervals, so a busy process is not
|
|
7
|
+
* mistaken for a dead one.
|
|
8
|
+
*/
|
|
9
|
+
export const HEARTBEAT_STALE_MS = 6 * HEARTBEAT_INTERVAL_MS;
|
|
10
|
+
/**
|
|
11
|
+
* How long a run's heartbeat must have been stale before a status read writes its
|
|
12
|
+
* `report-interrupted.html`, showing it errored. Far beyond `HEARTBEAT_STALE_MS`: a process that
|
|
13
|
+
* is only paused (a laptop asleep) is still alive, and may resume and write its own report.
|
|
14
|
+
*/
|
|
15
|
+
export const REPORT_STALE_MS = 10 * 60_000;
|
|
16
|
+
/** The job error of a run whose process disappeared without finishing it. */
|
|
17
|
+
export const PROCESS_GONE = 'The process running this job stopped unexpectedly.';
|
|
18
|
+
/** The job error of a run that was interrupted, such as by Ctrl+C or SIGTERM. */
|
|
19
|
+
export const RUN_INTERRUPTED = 'The run was stopped before it finished.';
|
|
20
|
+
/** The reason a case gets when its run ended before reaching it. */
|
|
21
|
+
export const STOPPED_BEFORE_CASE = 'The run stopped before this case.';
|
|
22
|
+
/**
|
|
23
|
+
* ID of the operating-system process running jobs, recorded in each ledger it writes. Core stays
|
|
24
|
+
* platform-free, so the CLI provides it; undefined leaves it out of the ledger.
|
|
25
|
+
*/
|
|
26
|
+
export const ProcessId = Context.Reference('screw-bugs/ProcessId', {
|
|
27
|
+
defaultValue: () => undefined,
|
|
28
|
+
});
|
|
29
|
+
/**
|
|
30
|
+
* The version of screw-bugs running jobs, recorded in each ledger. The CLI provides it from its
|
|
31
|
+
* package.json; undefined leaves it out.
|
|
32
|
+
*/
|
|
33
|
+
export const AppVersion = Context.Reference('screw-bugs/AppVersion', {
|
|
34
|
+
defaultValue: () => undefined,
|
|
35
|
+
});
|
|
36
|
+
/** Marks every case that has no result yet as not verified because the run stopped first. */
|
|
37
|
+
export const markUnreached = (cases, reason = STOPPED_BEFORE_CASE) => cases.map((c) => (c.status === 'not_verified' && c.reason === undefined ? { ...c, reason } : c));
|
|
38
|
+
/**
|
|
39
|
+
* The ledger as it should be reported at `nowMs`: a queued or running job whose heartbeat is
|
|
40
|
+
* stale (or missing) is reported errored, with its unreached cases not verified. Any other ledger
|
|
41
|
+
* is returned unchanged.
|
|
42
|
+
*/
|
|
43
|
+
export const withLiveness = (ledger, nowMs) => {
|
|
44
|
+
if (ledger.state !== 'queued' && ledger.state !== 'running')
|
|
45
|
+
return ledger;
|
|
46
|
+
const beat = ledger.heartbeatAt === undefined ? Number.NaN : Date.parse(ledger.heartbeatAt);
|
|
47
|
+
if (nowMs - beat <= HEARTBEAT_STALE_MS)
|
|
48
|
+
return ledger;
|
|
49
|
+
return { ...ledger, state: 'errored', error: PROCESS_GONE, cases: markUnreached(ledger.cases) };
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* How long ago the heartbeat of a ledger that `withLiveness` reports errored was written; its
|
|
53
|
+
* process may only be paused. Undefined for a ledger that its own process ended (or never ended).
|
|
54
|
+
*/
|
|
55
|
+
export const staleFor = (ledger, nowMs) => {
|
|
56
|
+
if (ledger.state !== 'errored' ||
|
|
57
|
+
ledger.error !== PROCESS_GONE ||
|
|
58
|
+
ledger.finishedAt !== undefined)
|
|
59
|
+
return undefined;
|
|
60
|
+
// A ledger without a heartbeat counts as stale forever, as `withLiveness` treats it.
|
|
61
|
+
return ledger.heartbeatAt === undefined
|
|
62
|
+
? Number.POSITIVE_INFINITY
|
|
63
|
+
: nowMs - Date.parse(ledger.heartbeatAt);
|
|
64
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { type Box, type ElementTarget, type Findings, type PlacedMark, type Shot, type ShotPage, type Target } from '../schema/index.js';
|
|
2
|
+
/** Where a highlight's element is on a screenshot, or why it has no box. */
|
|
3
|
+
export type Resolution = {
|
|
4
|
+
readonly _tag: 'found';
|
|
5
|
+
readonly box: Box;
|
|
6
|
+
readonly content?: Box;
|
|
7
|
+
/** Other matches left out as not on the screenshot, in words (see `PlacedMark.ignored`). */
|
|
8
|
+
readonly ignored?: string;
|
|
9
|
+
} | {
|
|
10
|
+
readonly _tag: 'missing';
|
|
11
|
+
readonly reason: string;
|
|
12
|
+
} | {
|
|
13
|
+
readonly _tag: 'ambiguous';
|
|
14
|
+
readonly reason: string;
|
|
15
|
+
};
|
|
16
|
+
/** The part of `box` on an image of `width` × `height`, or undefined when none of it is. */
|
|
17
|
+
export declare const clampBox: (box: Box, width: number, height: number) => Box | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* The part of an element's `content` (its text and images) inside its `box`, which is already on
|
|
20
|
+
* the image; undefined when it has none there, or none was saved.
|
|
21
|
+
*/
|
|
22
|
+
export declare const contentWithin: (content: Box | undefined, box: Box) => Box | undefined;
|
|
23
|
+
/** Why a mark is refused on an element that was in the page but not seen on the screenshot. */
|
|
24
|
+
export declare const NOT_VISIBLE = "the element was not visible when the screenshot was taken";
|
|
25
|
+
/**
|
|
26
|
+
* The part of a screenshot one document shows in: the box on the image where it shows (for a
|
|
27
|
+
* frame, the part of it that shows), and how to say "outside" it.
|
|
28
|
+
*/
|
|
29
|
+
export interface ShownPart {
|
|
30
|
+
readonly box: Box;
|
|
31
|
+
/** Such as `the part of its frame css "iframe.preview" that shows on this screenshot`. */
|
|
32
|
+
readonly where: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Finds `target` among the elements saved with a screenshot. When several match, an element
|
|
36
|
+
* inside another match wins over that one (text is in every ancestor too), then an exact name or
|
|
37
|
+
* text over a partial one. Then, when some of those left are on the screenshot (on its image, in
|
|
38
|
+
* `part` when given, and visible when it was taken), the others are left out and said so: a copy
|
|
39
|
+
* far below a viewport screenshot or below where its frame shows, or one that is transparent, is
|
|
40
|
+
* not what the agent saw. More than one left, unless they share one box, is ambiguous; an element
|
|
41
|
+
* the page had but did not show gets no mark. The box is cut to `part`.
|
|
42
|
+
*/
|
|
43
|
+
export declare const resolveTarget: (target: ElementTarget, page: ShotPage, part?: ShownPart) => Resolution;
|
|
44
|
+
/**
|
|
45
|
+
* Finds a highlight's element on a screenshot: in the frames its `frame` names, outermost first,
|
|
46
|
+
* then among that document's elements (`resolveTarget`). An element in a frame gets only the part
|
|
47
|
+
* of its box where the frame shows, and a match outside that part counts as outside the
|
|
48
|
+
* screenshot, as a copy below a viewport screenshot does. The frame's own `truncated` decides
|
|
49
|
+
* whether a match is unique there. One without `frame` is looked for in the page itself only,
|
|
50
|
+
* as a step would; when it is missing there but in a frame, the reason says so.
|
|
51
|
+
*/
|
|
52
|
+
export declare const resolveHighlight: (target: Target, page: ShotPage) => Resolution;
|
|
53
|
+
/**
|
|
54
|
+
* Places every highlight of `findings` on its screenshot. `shots` has every screenshot of the run
|
|
55
|
+
* by ID, with its saved page state when it has one, and why its boxes cannot be placed on the
|
|
56
|
+
* image when they cannot (`unplaced`, see `pageUnplaced`). Highlights on unknown screenshots are
|
|
57
|
+
* left out: `checkFindings` rejects them before findings are saved.
|
|
58
|
+
*/
|
|
59
|
+
export declare const placeMarks: (findings: Findings, shots: ReadonlyMap<string, {
|
|
60
|
+
readonly shot: Shot;
|
|
61
|
+
readonly page: ShotPage | undefined;
|
|
62
|
+
readonly unplaced?: string | undefined;
|
|
63
|
+
}>) => Record<string, ReadonlyArray<PlacedMark>>;
|