agentpath 0.0.0-stage → 0.1.0-alpha.2
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.md +105 -0
- package/README.md +101 -2
- package/THIRD_PARTY_NOTICES +12 -0
- package/apps/demo-site/dist/site.js +92 -0
- package/apps/studio/dist/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
- package/apps/studio/dist/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
- package/apps/studio/dist/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
- package/apps/studio/dist/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-cyrillic-ext-wght-normal-X_5orZeX.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-cyrillic-wght-normal-DiZS0aHC.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-latin-ext-wght-normal-Bwz-egvJ.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-latin-wght-normal-XN7g48iV.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-symbols2-wght-normal-CO5SzqOn.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-vietnamese-wght-normal-DadHysG0.woff2 +0 -0
- package/apps/studio/dist/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
- package/apps/studio/dist/assets/index-DH4wJPHm.js +67 -0
- package/apps/studio/dist/assets/index-DrnNh2ip.css +1 -0
- package/apps/studio/dist/index.html +14 -0
- package/audit.md +173 -0
- package/autonomous-mode.md +104 -0
- package/ci.md +118 -0
- package/coding-agents.md +143 -0
- package/concepts.md +145 -0
- package/contracts.md +356 -0
- package/examples/tasks/ambiguous.json +98 -0
- package/examples/tasks/assertion-failure.json +98 -0
- package/examples/tasks/broken.json +98 -0
- package/examples/tasks/fixed.json +98 -0
- package/examples/tasks/recovery.json +19 -0
- package/images/compare-recovery-desktop.jpg +0 -0
- package/images/compare-variants-desktop.jpg +0 -0
- package/images/overview-desktop.jpg +0 -0
- package/images/run-autonomous-passed-desktop.jpg +0 -0
- package/images/run-failed-desktop.jpg +0 -0
- package/images/run-failed-mobile.jpg +0 -0
- package/images/run-missing-artifact-desktop.jpg +0 -0
- package/images/task-editor-laptop.jpg +0 -0
- package/installation.md +102 -0
- package/ledger.md +62 -0
- package/licenses/-fontsource-variable-geist-mono.txt +93 -0
- package/licenses/-fontsource-variable-geist.txt +93 -0
- package/licenses/lucide-react.txt +43 -0
- package/licenses/react-dom.txt +21 -0
- package/licenses/react.txt +21 -0
- package/licenses/scheduler.txt +21 -0
- package/licenses/tailwindcss.txt +21 -0
- package/licenses/zod.txt +21 -0
- package/package.json +165 -4
- package/packages/cli/bin/agentpath.js +39 -0
- package/packages/cli/dist/audit-rerun.js +384 -0
- package/packages/cli/dist/audit.js +387 -0
- package/packages/cli/dist/change-summaries.js +87 -0
- package/packages/cli/dist/ci-report.js +88 -0
- package/packages/cli/dist/demo.js +88 -0
- package/packages/cli/dist/doctor.js +265 -0
- package/packages/cli/dist/explain.js +89 -0
- package/packages/cli/dist/git.js +7 -0
- package/packages/cli/dist/inspect.js +118 -0
- package/packages/cli/dist/ledger.js +509 -0
- package/packages/cli/dist/local.js +102 -0
- package/packages/cli/dist/main.js +708 -0
- package/packages/cli/dist/mcp.js +231 -0
- package/packages/cli/dist/paths.js +6 -0
- package/packages/cli/dist/playwright-fixture.js +185 -0
- package/packages/cli/dist/playwright-reporter.js +49 -0
- package/packages/cli/dist/tasks.js +37 -0
- package/packages/cli/dist/test-source.js +283 -0
- package/packages/cli/dist/watch.js +96 -0
- package/packages/contracts/dist/agent-settings.js +18 -0
- package/packages/contracts/dist/agent.js +175 -0
- package/packages/contracts/dist/audit.js +115 -0
- package/packages/contracts/dist/guidance.js +339 -0
- package/packages/contracts/dist/index.js +14 -0
- package/packages/contracts/dist/inspect.js +63 -0
- package/packages/contracts/dist/ledger.js +80 -0
- package/packages/contracts/dist/local.js +171 -0
- package/packages/contracts/dist/outcomes.js +94 -0
- package/packages/contracts/dist/platform.js +105 -0
- package/packages/contracts/dist/playwright-live.js +76 -0
- package/packages/contracts/dist/policy.js +93 -0
- package/packages/contracts/dist/run.js +178 -0
- package/packages/contracts/dist/task-issues.js +39 -0
- package/packages/contracts/dist/task.js +185 -0
- package/packages/contracts/dist/watch.js +57 -0
- package/packages/core/dist/action-feedback.js +103 -0
- package/packages/core/dist/actions.js +160 -0
- package/packages/core/dist/agent-loop.js +122 -0
- package/packages/core/dist/artifacts.js +156 -0
- package/packages/core/dist/assertions.js +120 -0
- package/packages/core/dist/autonomous.js +1 -0
- package/packages/core/dist/browser.js +196 -0
- package/packages/core/dist/compare.js +141 -0
- package/packages/core/dist/control-state.js +62 -0
- package/packages/core/dist/diagnostics.js +134 -0
- package/packages/core/dist/evidence-diff.js +93 -0
- package/packages/core/dist/evidence.js +203 -0
- package/packages/core/dist/index.js +22 -0
- package/packages/core/dist/inspect.js +257 -0
- package/packages/core/dist/integrity.js +176 -0
- package/packages/core/dist/liveness.js +114 -0
- package/packages/core/dist/local-files.js +61 -0
- package/packages/core/dist/locators.js +214 -0
- package/packages/core/dist/model-provider.js +88 -0
- package/packages/core/dist/observation.js +257 -0
- package/packages/core/dist/playwright-live.js +519 -0
- package/packages/core/dist/playwright-results.js +543 -0
- package/packages/core/dist/policy.js +182 -0
- package/packages/core/dist/privacy.js +93 -0
- package/packages/core/dist/recovery.js +54 -0
- package/packages/core/dist/retention.js +204 -0
- package/packages/core/dist/run-index.js +177 -0
- package/packages/core/dist/run-plan.js +28 -0
- package/packages/core/dist/run-repository.js +518 -0
- package/packages/core/dist/runner.js +913 -0
- package/packages/core/dist/storage.js +63 -0
- package/packages/core/dist/task-fingerprint.js +36 -0
- package/packages/core/dist/test-change-rerun.js +84 -0
- package/packages/core/dist/test-changes.js +896 -0
- package/packages/core/dist/timing.js +41 -0
- package/packages/core/dist/watch.js +165 -0
- package/packages/server/dist/index.js +265 -0
- package/packages/server/dist/task-store.js +103 -0
- package/quickstart.md +85 -0
- package/security.md +136 -0
- package/try-on-your-repo.md +54 -0
- package/understanding-results.md +159 -0
- package/writing-tasks.md +196 -0
|
@@ -0,0 +1,896 @@
|
|
|
1
|
+
import { messageKey, testChangeEffect } from "#agentpath/contracts";
|
|
2
|
+
// Static reading of Playwright test source for `agentpath audit` (docs/audit.md). It never executes, imports or
|
|
3
|
+
// evaluates the source: a linear-time tokenizer, then fixed patterns over the tokens. Anything it cannot read
|
|
4
|
+
// fails closed as unreadable instead of being guessed.
|
|
5
|
+
export const MAX_SOURCE_BYTES = 1024 * 1024;
|
|
6
|
+
const MAX_LIST = 1000;
|
|
7
|
+
const DETAIL_LIMIT = 300;
|
|
8
|
+
export class UnreadableSource extends Error {
|
|
9
|
+
}
|
|
10
|
+
const REGEX_KEYWORDS = new Set(["return", "typeof", "case", "do", "else", "in", "of", "new", "delete", "void", "throw", "yield", "await", "instanceof"]);
|
|
11
|
+
const isIdentStart = (code) => (code >= 65 && code <= 90) || (code >= 97 && code <= 122) || code === 36 || code === 95 || code > 127;
|
|
12
|
+
const isIdentPart = (code) => isIdentStart(code) || (code >= 48 && code <= 57);
|
|
13
|
+
const isDigit = (code) => code >= 48 && code <= 57;
|
|
14
|
+
const ESCAPES = { n: "\n", t: "\t", r: "\r", b: "\b", f: "\f", v: "\v", "0": "\0" };
|
|
15
|
+
/** Tokenizes JavaScript or TypeScript source in one pass. Throws UnreadableSource on anything unbalanced. */
|
|
16
|
+
export function tokenize(source) {
|
|
17
|
+
if (Buffer.byteLength(source, "utf8") > MAX_SOURCE_BYTES)
|
|
18
|
+
throw new UnreadableSource("The file is larger than 1 MiB.");
|
|
19
|
+
const tokens = [];
|
|
20
|
+
// Each entry is the brace depth inside a template substitution; the template resumes when its depth closes.
|
|
21
|
+
const templates = [];
|
|
22
|
+
let index = 0;
|
|
23
|
+
let line = 1;
|
|
24
|
+
const length = source.length;
|
|
25
|
+
const regexAllowed = () => {
|
|
26
|
+
const previous = tokens.at(-1);
|
|
27
|
+
if (!previous)
|
|
28
|
+
return true;
|
|
29
|
+
if (previous.type === "punct")
|
|
30
|
+
return ![")", "]", "}"].includes(previous.value);
|
|
31
|
+
if (previous.type === "ident")
|
|
32
|
+
return REGEX_KEYWORDS.has(previous.value);
|
|
33
|
+
return false;
|
|
34
|
+
};
|
|
35
|
+
// Scans template text from `index` (just after "`" or a closing substitution "}") up to "`" or "${".
|
|
36
|
+
const scanTemplate = (startLine, continued) => {
|
|
37
|
+
let raw = "";
|
|
38
|
+
while (index < length) {
|
|
39
|
+
const char = source[index];
|
|
40
|
+
if (char === "\\") {
|
|
41
|
+
raw += source.slice(index, index + 2);
|
|
42
|
+
if (source[index + 1] === "\n")
|
|
43
|
+
line++;
|
|
44
|
+
index += 2;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (char === "`") {
|
|
48
|
+
index++;
|
|
49
|
+
tokens.push({ type: "template", value: raw, line: startLine, ...(continued ? { continued } : {}) });
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (char === "$" && source[index + 1] === "{") {
|
|
53
|
+
index += 2;
|
|
54
|
+
tokens.push({ type: "template", value: raw, line: startLine, dynamic: true, ...(continued ? { continued } : {}) });
|
|
55
|
+
templates.push(0);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (char === "\n")
|
|
59
|
+
line++;
|
|
60
|
+
raw += char;
|
|
61
|
+
index++;
|
|
62
|
+
}
|
|
63
|
+
throw new UnreadableSource(`Unterminated template literal from line ${startLine}.`);
|
|
64
|
+
};
|
|
65
|
+
if (source.startsWith("#!")) {
|
|
66
|
+
while (index < length && source[index] !== "\n")
|
|
67
|
+
index++;
|
|
68
|
+
}
|
|
69
|
+
while (index < length) {
|
|
70
|
+
const char = source[index];
|
|
71
|
+
const code = source.charCodeAt(index);
|
|
72
|
+
if (char === "\n") {
|
|
73
|
+
line++;
|
|
74
|
+
index++;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (char === " " || char === "\t" || char === "\r" || char === "\uFEFF" || char === "\u00A0" || char === "\u2028" || char === "\u2029" || char === "\f" || char === "\v") {
|
|
78
|
+
index++;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (char === "/" && source[index + 1] === "/") {
|
|
82
|
+
while (index < length && source[index] !== "\n")
|
|
83
|
+
index++;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (char === "/" && source[index + 1] === "*") {
|
|
87
|
+
const end = source.indexOf("*/", index + 2);
|
|
88
|
+
if (end < 0)
|
|
89
|
+
throw new UnreadableSource(`Unterminated comment on line ${line}.`);
|
|
90
|
+
for (let at = index; at < end; at++)
|
|
91
|
+
if (source[at] === "\n")
|
|
92
|
+
line++;
|
|
93
|
+
index = end + 2;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (char === "'" || char === '"') {
|
|
97
|
+
const startLine = line;
|
|
98
|
+
let value = "";
|
|
99
|
+
index++;
|
|
100
|
+
for (;;) {
|
|
101
|
+
if (index >= length || source[index] === "\n")
|
|
102
|
+
throw new UnreadableSource(`Unterminated string on line ${startLine}.`);
|
|
103
|
+
const next = source[index];
|
|
104
|
+
if (next === char) {
|
|
105
|
+
index++;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
if (next === "\\") {
|
|
109
|
+
const escaped = source[index + 1] ?? "";
|
|
110
|
+
if (escaped === "\n") {
|
|
111
|
+
line++;
|
|
112
|
+
index += 2;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
value += ESCAPES[escaped] ?? escaped;
|
|
116
|
+
index += 2;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
value += next;
|
|
120
|
+
index++;
|
|
121
|
+
}
|
|
122
|
+
tokens.push({ type: "string", value, line: startLine });
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (char === "`") {
|
|
126
|
+
index++;
|
|
127
|
+
scanTemplate(line, false);
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (isDigit(code) || (char === "." && isDigit(source.charCodeAt(index + 1)))) {
|
|
131
|
+
const start = index;
|
|
132
|
+
while (index < length && (isIdentPart(source.charCodeAt(index)) || source[index] === "."))
|
|
133
|
+
index++;
|
|
134
|
+
tokens.push({ type: "number", value: source.slice(start, index).replace(/_/g, ""), line });
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (isIdentStart(code) || char === "#") {
|
|
138
|
+
const start = index;
|
|
139
|
+
index++;
|
|
140
|
+
while (index < length && isIdentPart(source.charCodeAt(index)))
|
|
141
|
+
index++;
|
|
142
|
+
tokens.push({ type: "ident", value: source.slice(start, index), line });
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (char === "/" && regexAllowed()) {
|
|
146
|
+
const start = index;
|
|
147
|
+
let inClass = false;
|
|
148
|
+
index++;
|
|
149
|
+
for (;;) {
|
|
150
|
+
if (index >= length || source[index] === "\n")
|
|
151
|
+
throw new UnreadableSource(`Unterminated regular expression on line ${line}.`);
|
|
152
|
+
const next = source[index];
|
|
153
|
+
if (next === "\\") {
|
|
154
|
+
index += 2;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (next === "[")
|
|
158
|
+
inClass = true;
|
|
159
|
+
else if (next === "]")
|
|
160
|
+
inClass = false;
|
|
161
|
+
else if (next === "/" && !inClass) {
|
|
162
|
+
index++;
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
index++;
|
|
166
|
+
}
|
|
167
|
+
while (index < length && isIdentPart(source.charCodeAt(index)))
|
|
168
|
+
index++;
|
|
169
|
+
tokens.push({ type: "regex", value: source.slice(start, index), line });
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (templates.length) {
|
|
173
|
+
if (char === "{")
|
|
174
|
+
templates[templates.length - 1]++;
|
|
175
|
+
else if (char === "}") {
|
|
176
|
+
if (templates[templates.length - 1] === 0) {
|
|
177
|
+
templates.pop();
|
|
178
|
+
index++;
|
|
179
|
+
scanTemplate(line, true);
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
templates[templates.length - 1]--;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (char === "?" && source[index + 1] === "." && !isDigit(source.charCodeAt(index + 2))) {
|
|
186
|
+
tokens.push({ type: "punct", value: ".", line });
|
|
187
|
+
index += 2;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (char === "=" && source[index + 1] === ">") {
|
|
191
|
+
tokens.push({ type: "punct", value: "=>", line });
|
|
192
|
+
index += 2;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (char === "." && source[index + 1] === "." && source[index + 2] === ".") {
|
|
196
|
+
tokens.push({ type: "punct", value: "...", line });
|
|
197
|
+
index += 3;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
tokens.push({ type: "punct", value: char, line });
|
|
201
|
+
index++;
|
|
202
|
+
}
|
|
203
|
+
if (templates.length)
|
|
204
|
+
throw new UnreadableSource("Unterminated template substitution.");
|
|
205
|
+
return tokens;
|
|
206
|
+
}
|
|
207
|
+
/** For each opening bracket token, the index of its closing token (and the reverse). Throws when unbalanced. */
|
|
208
|
+
export function matchBrackets(tokens) {
|
|
209
|
+
const match = new Int32Array(tokens.length).fill(-1);
|
|
210
|
+
const stack = [];
|
|
211
|
+
const pairs = { ")": "(", "]": "[", "}": "{" };
|
|
212
|
+
tokens.forEach((token, index) => {
|
|
213
|
+
if (token.type !== "punct")
|
|
214
|
+
return;
|
|
215
|
+
if (token.value === "(" || token.value === "[" || token.value === "{")
|
|
216
|
+
stack.push(index);
|
|
217
|
+
else if (token.value in pairs) {
|
|
218
|
+
const open = stack.pop();
|
|
219
|
+
if (open === undefined || tokens[open].value !== pairs[token.value])
|
|
220
|
+
throw new UnreadableSource(`Unbalanced "${token.value}" on line ${token.line}.`);
|
|
221
|
+
match[open] = index;
|
|
222
|
+
match[index] = open;
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
if (stack.length)
|
|
226
|
+
throw new UnreadableSource(`Unclosed "${tokens[stack.at(-1)].value}" from line ${tokens[stack.at(-1)].line}.`);
|
|
227
|
+
return match;
|
|
228
|
+
}
|
|
229
|
+
// A template piece shows its own delimiters, so `a${x}b` reads back as written.
|
|
230
|
+
const display = (token) => token.type === "string" ? JSON.stringify(token.value)
|
|
231
|
+
: token.type === "template" ? `${token.continued ? "}" : "`"}${token.value}${token.dynamic ? "${" : "`"}` : token.value;
|
|
232
|
+
/** Compact code text for a token range, for keys and for details. */
|
|
233
|
+
function code(tokens, from, to) {
|
|
234
|
+
let text = "";
|
|
235
|
+
for (let index = from; index < to; index++) {
|
|
236
|
+
const token = tokens[index];
|
|
237
|
+
const previous = tokens[index - 1];
|
|
238
|
+
const word = (candidate) => candidate && (candidate.type === "ident" || candidate.type === "number");
|
|
239
|
+
if (index > from && word(previous) && word(token))
|
|
240
|
+
text += " ";
|
|
241
|
+
else if (index > from && token.type === "punct" && token.value === "=>")
|
|
242
|
+
text += " ";
|
|
243
|
+
text += display(token);
|
|
244
|
+
if (token.type === "punct" && (token.value === "," || token.value === "=>"))
|
|
245
|
+
text += " ";
|
|
246
|
+
}
|
|
247
|
+
return text.trim();
|
|
248
|
+
}
|
|
249
|
+
/** Repository text made safe to print: control, format and line-separator characters become spaces. */
|
|
250
|
+
export const plainText = (text) => text.replace(/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/gu, " ");
|
|
251
|
+
const clip = (text) => { const plain = plainText(text); return plain.length > DETAIL_LIMIT ? `${plain.slice(0, DETAIL_LIMIT - 1)}…` : plain; };
|
|
252
|
+
const TITLE_LIMIT = 2000;
|
|
253
|
+
const plainTitle = (title) => title.map((part) => { const plain = plainText(part); return plain.length > TITLE_LIMIT ? `${plain.slice(0, TITLE_LIMIT - 1)}…` : plain; });
|
|
254
|
+
const DECLARATION_MODIFIERS = new Set(["skip", "fixme", "fail", "only", "slow"]);
|
|
255
|
+
const HOOKS = new Set(["beforeEach", "afterEach", "beforeAll", "afterAll"]);
|
|
256
|
+
// Generic matchers take no options object: an object literal passed to them is the expected value.
|
|
257
|
+
const VALUE_MATCHERS = new Set(["toBe", "toEqual", "toStrictEqual", "toMatchObject", "toContain", "toContainEqual", "toHaveProperty", "toHaveLength", "toMatch", "toThrow", "toThrowError"]);
|
|
258
|
+
const NOT_A_TARGET = /^(?:\[|(?:new\s*)?(?:Array|[A-Z][A-Za-z0-9]*Array|Buffer)\b)/;
|
|
259
|
+
const ACTIONS = new Set(["click", "dblclick", "fill", "press", "pressSequentially", "type", "check", "uncheck", "setChecked", "selectOption", "hover", "tap", "setInputFiles", "dragTo", "goto", "focus", "clear", "selectText", "dispatchEvent"]);
|
|
260
|
+
const LOCATORS = new Set(["getByRole", "getByLabel", "getByText", "getByTestId", "getByPlaceholder", "getByAltText", "getByTitle", "locator", "frameLocator"]);
|
|
261
|
+
/** Locators that find an element by what a user reads: a name shaped like a message key there may be a missing translation. */
|
|
262
|
+
const NAMED = new Set(["getByRole", "getByLabel", "getByText", "getByPlaceholder", "getByAltText", "getByTitle"]);
|
|
263
|
+
const NARROWING = new Set(["first", "last", "nth"]);
|
|
264
|
+
const STATEMENT_START = new Set([";", "{", "}", ",", "(", "[", "=", "=>", "?", ":", "&", "|", "!"]);
|
|
265
|
+
const literalTitle = (token) => token && (token.type === "string" || (token.type === "template" && !token.dynamic)) ? token.value : undefined;
|
|
266
|
+
const isMember = (tokens, index) => tokens[index - 1]?.type === "punct" && tokens[index - 1].value === ".";
|
|
267
|
+
/** Marks every token that runs only conditionally: inside if/else/try/catch blocks or single-statement branches. */
|
|
268
|
+
function conditionalTokens(tokens, match, from, to) {
|
|
269
|
+
const conditional = new Uint8Array(tokens.length);
|
|
270
|
+
const conditionalOpen = new Set();
|
|
271
|
+
let depth = 0;
|
|
272
|
+
const closeAt = [];
|
|
273
|
+
let singleUntil = -1;
|
|
274
|
+
// The end of a single statement starting at `start`: the next ";" outside brackets, or the block's end.
|
|
275
|
+
const statementEnd = (start) => {
|
|
276
|
+
for (let index = start; index < to; index++) {
|
|
277
|
+
const token = tokens[index];
|
|
278
|
+
if (token.type === "punct" && ["(", "[", "{"].includes(token.value)) {
|
|
279
|
+
index = match[index];
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (token.type === "punct" && (token.value === ";" || token.value === "}"))
|
|
283
|
+
return index;
|
|
284
|
+
}
|
|
285
|
+
return to;
|
|
286
|
+
};
|
|
287
|
+
const branch = (next) => {
|
|
288
|
+
const token = tokens[next];
|
|
289
|
+
if (token?.type === "punct" && token.value === "{")
|
|
290
|
+
conditionalOpen.add(next);
|
|
291
|
+
else if (token && !(token.type === "ident" && token.value === "if"))
|
|
292
|
+
singleUntil = Math.max(singleUntil, statementEnd(next));
|
|
293
|
+
};
|
|
294
|
+
for (let index = from; index < to; index++) {
|
|
295
|
+
const token = tokens[index];
|
|
296
|
+
if (token.type === "ident" && !isMember(tokens, index)) {
|
|
297
|
+
if ((token.value === "if" || token.value === "catch") && tokens[index + 1]?.value === "(")
|
|
298
|
+
branch(match[index + 1] + 1);
|
|
299
|
+
else if (token.value === "catch" && tokens[index + 1]?.value === "{")
|
|
300
|
+
branch(index + 1);
|
|
301
|
+
else if (token.value === "else" || token.value === "try")
|
|
302
|
+
branch(index + 1);
|
|
303
|
+
}
|
|
304
|
+
if (token.type === "punct" && token.value === "{" && conditionalOpen.has(index)) {
|
|
305
|
+
depth++;
|
|
306
|
+
closeAt.push(match[index]);
|
|
307
|
+
}
|
|
308
|
+
while (closeAt.length && closeAt.at(-1) < index) {
|
|
309
|
+
closeAt.pop();
|
|
310
|
+
depth--;
|
|
311
|
+
}
|
|
312
|
+
if (depth > 0 || index <= singleUntil)
|
|
313
|
+
conditional[index] = 1;
|
|
314
|
+
}
|
|
315
|
+
return conditional;
|
|
316
|
+
}
|
|
317
|
+
function hashTokens(tokens, from, to) {
|
|
318
|
+
// FNV-1a over token values: a pairing aid for renamed tests, not a security digest.
|
|
319
|
+
let hash = 0x811c9dc5;
|
|
320
|
+
for (let index = from; index < to; index++) {
|
|
321
|
+
const value = `${tokens[index].type}:${tokens[index].value}\u0000`;
|
|
322
|
+
for (let at = 0; at < value.length; at++) {
|
|
323
|
+
hash ^= value.charCodeAt(at);
|
|
324
|
+
hash = Math.imul(hash, 0x01000193) >>> 0;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return hash.toString(16);
|
|
328
|
+
}
|
|
329
|
+
function firstNumber(tokens, from, to) {
|
|
330
|
+
const token = tokens[from];
|
|
331
|
+
return from < to && token?.type === "number" && /^\d+(\.\d+)?$/.test(token.value) ? Number(token.value) : undefined;
|
|
332
|
+
}
|
|
333
|
+
/** The facts of one test or hook body, from its tokens only. */
|
|
334
|
+
function readBody(tokens, match, from, to) {
|
|
335
|
+
const conditional = conditionalTokens(tokens, match, from, to);
|
|
336
|
+
const expectations = [];
|
|
337
|
+
const steps = [];
|
|
338
|
+
const locators = [];
|
|
339
|
+
const names = [];
|
|
340
|
+
const runtime = [];
|
|
341
|
+
let narrowing = 0;
|
|
342
|
+
let timeout = 0;
|
|
343
|
+
// The argument ranges of assertion matchers: a call such as `Array(5).fill(x)` inside an expected value computes
|
|
344
|
+
// a value and is never a browser step. The assertion's subject (a toPass or poll callback) is still read.
|
|
345
|
+
const values = [];
|
|
346
|
+
for (let index = from; index < to; index++) {
|
|
347
|
+
const token = tokens[index];
|
|
348
|
+
if (token.type !== "ident")
|
|
349
|
+
continue;
|
|
350
|
+
const next = tokens[index + 1];
|
|
351
|
+
const member = isMember(tokens, index);
|
|
352
|
+
if (!member && (token.value === "test" || token.value === "it") && next?.value === "." && tokens[index + 3]?.value === "(") {
|
|
353
|
+
const modifier = tokens[index + 2].value;
|
|
354
|
+
const open = index + 3;
|
|
355
|
+
if (modifier === "skip" || modifier === "fixme" || modifier === "fail" || modifier === "slow") {
|
|
356
|
+
const close = match[open];
|
|
357
|
+
const first = tokens[open + 1];
|
|
358
|
+
const unconditional = close === open + 1 || (first?.type === "ident" && first.value === "true" && [",", ")"].includes(tokens[open + 2]?.value ?? ""));
|
|
359
|
+
// A call inside an if, else, try or catch branch runs only sometimes, like one with a condition argument.
|
|
360
|
+
runtime.push({ modifier, conditional: !unconditional || conditional[index] === 1, line: token.line });
|
|
361
|
+
index = close;
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
if (modifier === "setTimeout") {
|
|
365
|
+
const value = firstNumber(tokens, open + 1, match[open]);
|
|
366
|
+
if (value !== undefined)
|
|
367
|
+
timeout = Math.max(timeout, value);
|
|
368
|
+
}
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
if (!member && token.value === "expect") {
|
|
372
|
+
let cursor = index + 1;
|
|
373
|
+
let soft = false;
|
|
374
|
+
let poll = false;
|
|
375
|
+
if (tokens[cursor]?.value === "." && (tokens[cursor + 1]?.value === "soft" || tokens[cursor + 1]?.value === "poll")) {
|
|
376
|
+
soft = tokens[cursor + 1].value === "soft";
|
|
377
|
+
poll = !soft;
|
|
378
|
+
cursor += 2;
|
|
379
|
+
}
|
|
380
|
+
if (tokens[cursor]?.value !== "(")
|
|
381
|
+
continue;
|
|
382
|
+
const subjectClose = match[cursor];
|
|
383
|
+
const subject = code(tokens, cursor + 1, subjectClose);
|
|
384
|
+
let not = false;
|
|
385
|
+
let matcher;
|
|
386
|
+
let args = "";
|
|
387
|
+
cursor = subjectClose + 1;
|
|
388
|
+
while (tokens[cursor]?.value === "." && tokens[cursor + 1]?.type === "ident") {
|
|
389
|
+
const name = tokens[cursor + 1].value;
|
|
390
|
+
if (name === "not") {
|
|
391
|
+
not = !not;
|
|
392
|
+
cursor += 2;
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
if (name === "resolves" || name === "rejects") {
|
|
396
|
+
cursor += 2;
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
if (tokens[cursor + 2]?.value === "(") {
|
|
400
|
+
matcher = name;
|
|
401
|
+
// A trailing options object ({ timeout }, { exact }) is not the expected value; its timeout counts separately.
|
|
402
|
+
const argsClose = match[cursor + 2];
|
|
403
|
+
let argsEnd = argsClose;
|
|
404
|
+
let lastComma = -1;
|
|
405
|
+
for (let at = cursor + 3; at < argsClose; at++) {
|
|
406
|
+
const inner = tokens[at];
|
|
407
|
+
if (inner.type === "punct" && ["(", "[", "{"].includes(inner.value)) {
|
|
408
|
+
at = match[at];
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
if (inner.type === "punct" && inner.value === ",")
|
|
412
|
+
lastComma = at;
|
|
413
|
+
}
|
|
414
|
+
const optionsStart = lastComma >= 0 ? lastComma + 1 : cursor + 3;
|
|
415
|
+
if (!VALUE_MATCHERS.has(name) && tokens[optionsStart]?.value === "{" && match[optionsStart] === argsClose - 1 - (tokens[argsClose - 1]?.value === "," ? 1 : 0))
|
|
416
|
+
argsEnd = lastComma >= 0 ? lastComma : optionsStart;
|
|
417
|
+
args = code(tokens, cursor + 3, argsEnd);
|
|
418
|
+
values.push([cursor + 2, argsClose]);
|
|
419
|
+
cursor = argsClose + 1;
|
|
420
|
+
}
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
if (!matcher)
|
|
424
|
+
continue;
|
|
425
|
+
// A `.catch(` later in the same statement swallows the assertion's failure.
|
|
426
|
+
let swallowed = false;
|
|
427
|
+
for (let at = cursor; at < to; at++) {
|
|
428
|
+
const after = tokens[at];
|
|
429
|
+
if (after.type === "punct" && ["(", "[", "{"].includes(after.value)) {
|
|
430
|
+
at = match[at];
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
if (after.type === "punct" && (after.value === ";" || after.value === "}"))
|
|
434
|
+
break;
|
|
435
|
+
if (after.type === "ident" && after.value === "catch" && isMember(tokens, at)) {
|
|
436
|
+
swallowed = true;
|
|
437
|
+
break;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
expectations.push({ key: `${subject}.${matcher}`, subject, matcher, args, not, soft, poll, conditional: swallowed || conditional[index] === 1, line: token.line });
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
if (member && next?.value === "(") {
|
|
444
|
+
const close = match[index + 1];
|
|
445
|
+
if (ACTIONS.has(token.value) && values.some(([open, close]) => index > open && index < close)) {
|
|
446
|
+
// Inside an assertion's expected value: not a step.
|
|
447
|
+
}
|
|
448
|
+
else if (ACTIONS.has(token.value)) {
|
|
449
|
+
// The receiver: back from the "." to the start of the expression, stepping over bracketed groups.
|
|
450
|
+
let start = index - 2;
|
|
451
|
+
while (start >= from) {
|
|
452
|
+
const back = tokens[start];
|
|
453
|
+
if (back.type === "punct" && (back.value === ")" || back.value === "]")) {
|
|
454
|
+
start = match[start] - 1;
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
if ((back.type === "punct" && STATEMENT_START.has(back.value)) || (back.type === "ident" && ["await", "return", "void"].includes(back.value)))
|
|
458
|
+
break;
|
|
459
|
+
start--;
|
|
460
|
+
}
|
|
461
|
+
const receiver = code(tokens, start + 1, index - 1);
|
|
462
|
+
// `fill` on an array or buffer (`Array(5).fill(0)`, `new Uint8Array(4).fill(1)`, `[a, b].fill(c)`) is not a step.
|
|
463
|
+
if (token.value === "fill" && NOT_A_TARGET.test(receiver))
|
|
464
|
+
continue;
|
|
465
|
+
// Only a navigation target is part of the step; typed values, keys and options are never kept.
|
|
466
|
+
let urlEnd = index + 2;
|
|
467
|
+
while (urlEnd < close && !(tokens[urlEnd].type === "punct" && tokens[urlEnd].value === ","))
|
|
468
|
+
urlEnd = match[urlEnd] > urlEnd ? match[urlEnd] + 1 : urlEnd + 1;
|
|
469
|
+
const target = token.value === "goto" ? `(${clip(code(tokens, index + 2, urlEnd))})` : "()";
|
|
470
|
+
steps.push({ key: `${receiver}.${token.value}${target}`, method: token.value, text: `${receiver}.${token.value}${target}`, line: token.line });
|
|
471
|
+
}
|
|
472
|
+
else if (LOCATORS.has(token.value)) {
|
|
473
|
+
locators.push(`${token.value}(${code(tokens, index + 2, close)})`);
|
|
474
|
+
if (NAMED.has(token.value)) {
|
|
475
|
+
// Only direct literals: the first argument, and the value of a `name:` option. A string passed to a call
|
|
476
|
+
// inside the locator, such as a translation lookup t("nav.save"), is not the name the page shows.
|
|
477
|
+
if (tokens[index + 2]?.type === "string")
|
|
478
|
+
names.push(tokens[index + 2].value);
|
|
479
|
+
for (let at = index + 3; at + 2 < close; at++) {
|
|
480
|
+
if (tokens[at].type === "ident" && tokens[at].value === "name" && tokens[at + 1].value === ":" && tokens[at + 2].type === "string")
|
|
481
|
+
names.push(tokens[at + 2].value);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
else if (NARROWING.has(token.value)) {
|
|
486
|
+
narrowing++;
|
|
487
|
+
}
|
|
488
|
+
else if (token.value === "waitForTimeout" || token.value === "setTimeout") {
|
|
489
|
+
const value = firstNumber(tokens, index + 2, close);
|
|
490
|
+
if (value !== undefined)
|
|
491
|
+
timeout = Math.max(timeout, value);
|
|
492
|
+
}
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
if (token.value === "timeout" && next?.value === ":") {
|
|
496
|
+
const value = firstNumber(tokens, index + 2, to);
|
|
497
|
+
if (value !== undefined)
|
|
498
|
+
timeout = Math.max(timeout, value);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
if (expectations.length > MAX_LIST || steps.length > MAX_LIST)
|
|
502
|
+
throw new UnreadableSource("A test has more than 1000 assertions or steps.");
|
|
503
|
+
return { runtime, expectations, steps, locators, names, narrowing, timeout, bodyHash: hashTokens(tokens, from, to) };
|
|
504
|
+
}
|
|
505
|
+
/** Reads the tests, hooks and describe blocks of one file. Throws UnreadableSource when the file cannot be read. */
|
|
506
|
+
export function readTests(source) {
|
|
507
|
+
const tokens = tokenize(source);
|
|
508
|
+
const match = matchBrackets(tokens);
|
|
509
|
+
const tests = [];
|
|
510
|
+
const unreadableTests = [];
|
|
511
|
+
const parameterised = [];
|
|
512
|
+
const coveredLines = [];
|
|
513
|
+
// Token ranges [from, to) left out of the skeleton.
|
|
514
|
+
const excluded = [];
|
|
515
|
+
const withSemicolon = (close) => tokens[close + 1]?.value === ";" ? close + 2 : close + 1;
|
|
516
|
+
// The end of a call's first argument: the first comma at its own depth, or undefined.
|
|
517
|
+
const firstComma = (open, close) => {
|
|
518
|
+
for (let at = open + 1; at < close; at++) {
|
|
519
|
+
const value = tokens[at].type === "punct" ? tokens[at].value : "";
|
|
520
|
+
if (value === "(" || value === "[" || value === "{")
|
|
521
|
+
at = match[at];
|
|
522
|
+
else if (value === ",")
|
|
523
|
+
return at;
|
|
524
|
+
}
|
|
525
|
+
return undefined;
|
|
526
|
+
};
|
|
527
|
+
const seen = new Map();
|
|
528
|
+
const uniqueTitle = (title) => {
|
|
529
|
+
const key = title.join("\u0000");
|
|
530
|
+
const count = (seen.get(key) ?? 0) + 1;
|
|
531
|
+
seen.set(key, count);
|
|
532
|
+
return count === 1 ? title : [...title.slice(0, -1), `${title.at(-1)} #${count}`];
|
|
533
|
+
};
|
|
534
|
+
const scan = (from, to, path, inherited, dynamic = false) => {
|
|
535
|
+
const firstTest = tests.length;
|
|
536
|
+
const firstParameterised = parameterised.length;
|
|
537
|
+
const into = dynamic ? parameterised : tests;
|
|
538
|
+
const groupRuntime = [];
|
|
539
|
+
const hookCounts = new Map();
|
|
540
|
+
for (let index = from; index < to; index++) {
|
|
541
|
+
const token = tokens[index];
|
|
542
|
+
if (token.type !== "ident" || isMember(tokens, index))
|
|
543
|
+
continue;
|
|
544
|
+
const isTestName = token.value === "test" || token.value === "it";
|
|
545
|
+
if (!isTestName && token.value !== "describe" && !HOOKS.has(token.value))
|
|
546
|
+
continue;
|
|
547
|
+
const names = [token.value];
|
|
548
|
+
let cursor = index + 1;
|
|
549
|
+
while (tokens[cursor]?.value === "." && tokens[cursor + 1]?.type === "ident") {
|
|
550
|
+
names.push(tokens[cursor + 1].value);
|
|
551
|
+
cursor += 2;
|
|
552
|
+
}
|
|
553
|
+
if (tokens[cursor]?.value !== "(")
|
|
554
|
+
continue;
|
|
555
|
+
const open = cursor;
|
|
556
|
+
const close = match[open];
|
|
557
|
+
const members = names.slice(1);
|
|
558
|
+
if (members.includes("configure") || members.includes("use") || members.includes("extend") || members.includes("step") || members.includes("info")) {
|
|
559
|
+
index = close;
|
|
560
|
+
continue;
|
|
561
|
+
}
|
|
562
|
+
const hook = HOOKS.has(names[0]) ? names[0] : members.find((name) => HOOKS.has(name));
|
|
563
|
+
if (hook) {
|
|
564
|
+
const count = (hookCounts.get(hook) ?? 0) + 1;
|
|
565
|
+
hookCounts.set(hook, count);
|
|
566
|
+
into.push({ title: [...path, `[${hook} ${count}]`], line: token.line, modifiers: new Set(inherited), ...readBody(tokens, match, open + 1, close) });
|
|
567
|
+
excluded.push(dynamic ? [open + 1, close] : [index, withSemicolon(close)]);
|
|
568
|
+
index = close;
|
|
569
|
+
continue;
|
|
570
|
+
}
|
|
571
|
+
const isDescribe = names[0] === "describe" || members.includes("describe");
|
|
572
|
+
const modifiers = new Set(members.filter((name) => DECLARATION_MODIFIERS.has(name)));
|
|
573
|
+
const title = literalTitle(tokens[open + 1]);
|
|
574
|
+
if (isDescribe) {
|
|
575
|
+
const anonymous = tokens[open + 1]?.type === "ident" || tokens[open + 1]?.value === "(";
|
|
576
|
+
if (title === undefined && !anonymous) {
|
|
577
|
+
unreadableTests.push(token.line);
|
|
578
|
+
const comma = firstComma(open, close);
|
|
579
|
+
if (comma !== undefined) {
|
|
580
|
+
coveredLines.push(token.line);
|
|
581
|
+
scan(comma + 1, close, [...path, code(tokens, open + 1, comma)], new Set([...inherited, ...modifiers]), true);
|
|
582
|
+
}
|
|
583
|
+
index = close;
|
|
584
|
+
continue;
|
|
585
|
+
}
|
|
586
|
+
scan(open + 1, close, title === undefined ? path : [...path, title], new Set([...inherited, ...modifiers]), dynamic);
|
|
587
|
+
index = close;
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
if (!isTestName)
|
|
591
|
+
continue;
|
|
592
|
+
if (title === undefined) {
|
|
593
|
+
// test.skip(condition) and friends at group level apply to every test in the group.
|
|
594
|
+
const modifier = members[0];
|
|
595
|
+
// A template literal first is a title built at run time (test.skip(`a ${b}`, ...)), never a condition.
|
|
596
|
+
if (members.length === 1 && (modifier === "skip" || modifier === "fixme" || modifier === "fail" || modifier === "slow") && tokens[open + 1]?.type !== "template") {
|
|
597
|
+
const first = tokens[open + 1];
|
|
598
|
+
const unconditional = close === open + 1 || (first?.type === "ident" && first.value === "true" && [",", ")"].includes(tokens[open + 2]?.value ?? ""));
|
|
599
|
+
groupRuntime.push({ modifier, conditional: !unconditional, line: token.line, group: true });
|
|
600
|
+
}
|
|
601
|
+
else if (members.length === 0 || members.every((name) => DECLARATION_MODIFIERS.has(name))) {
|
|
602
|
+
unreadableTests.push(token.line);
|
|
603
|
+
const comma = firstComma(open, close);
|
|
604
|
+
if (comma !== undefined) {
|
|
605
|
+
coveredLines.push(token.line);
|
|
606
|
+
parameterised.push({ title: [...path, code(tokens, open + 1, comma)], line: token.line, modifiers: new Set([...inherited, ...modifiers]), ownModifiers: modifiers, ...readBody(tokens, match, comma + 1, close) });
|
|
607
|
+
excluded.push([comma + 1, close]);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
index = close;
|
|
611
|
+
continue;
|
|
612
|
+
}
|
|
613
|
+
if (dynamic) {
|
|
614
|
+
// A literal title inside a parameterised describe still stands for one test per parameter.
|
|
615
|
+
parameterised.push({ title: [...path, title], line: token.line, modifiers: new Set([...inherited, ...modifiers]), ownModifiers: modifiers, ...readBody(tokens, match, open + 2, close) });
|
|
616
|
+
excluded.push([open + 2, close]);
|
|
617
|
+
}
|
|
618
|
+
else {
|
|
619
|
+
tests.push({ title: uniqueTitle([...path, title]), line: token.line, modifiers: new Set([...inherited, ...modifiers]), ownModifiers: modifiers, ...readBody(tokens, match, open + 2, close) });
|
|
620
|
+
excluded.push([index, withSemicolon(close)]);
|
|
621
|
+
}
|
|
622
|
+
index = close;
|
|
623
|
+
}
|
|
624
|
+
if (groupRuntime.length)
|
|
625
|
+
for (const test of [...tests.slice(firstTest), ...parameterised.slice(firstParameterised)])
|
|
626
|
+
test.runtime.push(...groupRuntime);
|
|
627
|
+
};
|
|
628
|
+
scan(0, tokens.length, [], new Set());
|
|
629
|
+
if (tests.length > 2000 || parameterised.length > 2000)
|
|
630
|
+
throw new UnreadableSource("The file declares more than 2000 tests.");
|
|
631
|
+
excluded.sort((a, b) => a[0] - b[0]);
|
|
632
|
+
const kept = [];
|
|
633
|
+
let at = 0;
|
|
634
|
+
for (const [from, to] of excluded) {
|
|
635
|
+
for (; at < from; at++)
|
|
636
|
+
kept.push(`${tokens[at].type}:${tokens[at].value}`);
|
|
637
|
+
at = Math.max(at, to);
|
|
638
|
+
}
|
|
639
|
+
for (; at < tokens.length; at++)
|
|
640
|
+
kept.push(`${tokens[at].type}:${tokens[at].value}`);
|
|
641
|
+
const imports = [];
|
|
642
|
+
tokens.forEach((token, index) => {
|
|
643
|
+
if (token.type !== "ident" || isMember(tokens, index) || imports.length >= MAX_LIST)
|
|
644
|
+
return;
|
|
645
|
+
const next = tokens[index + 1];
|
|
646
|
+
if (token.value === "from" && next?.type === "string")
|
|
647
|
+
imports.push(next.value);
|
|
648
|
+
else if (token.value === "import" && next?.type === "string")
|
|
649
|
+
imports.push(next.value);
|
|
650
|
+
else if ((token.value === "import" || token.value === "require") && next?.value === "(" && tokens[index + 2]?.type === "string")
|
|
651
|
+
imports.push(tokens[index + 2].value);
|
|
652
|
+
});
|
|
653
|
+
return { tests, unreadableTests, parameterised, coveredLines, imports, skeleton: kept.join("\u0000") };
|
|
654
|
+
}
|
|
655
|
+
/** Longest common subsequence of two key lists: the aligned index pairs, in order. */
|
|
656
|
+
function align(base, head) {
|
|
657
|
+
const rows = base.length + 1;
|
|
658
|
+
const columns = head.length + 1;
|
|
659
|
+
const table = new Uint16Array(rows * columns);
|
|
660
|
+
for (let i = base.length - 1; i >= 0; i--) {
|
|
661
|
+
for (let j = head.length - 1; j >= 0; j--) {
|
|
662
|
+
table[i * columns + j] = base[i].key === head[j].key ? table[(i + 1) * columns + j + 1] + 1 : Math.max(table[(i + 1) * columns + j], table[i * columns + j + 1]);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
const pairs = [];
|
|
666
|
+
let i = 0;
|
|
667
|
+
let j = 0;
|
|
668
|
+
while (i < base.length && j < head.length) {
|
|
669
|
+
if (base[i].key === head[j].key) {
|
|
670
|
+
pairs.push([i, j]);
|
|
671
|
+
i++;
|
|
672
|
+
j++;
|
|
673
|
+
}
|
|
674
|
+
else if (table[(i + 1) * columns + j] >= table[i * columns + j + 1])
|
|
675
|
+
i++;
|
|
676
|
+
else
|
|
677
|
+
j++;
|
|
678
|
+
}
|
|
679
|
+
return pairs;
|
|
680
|
+
}
|
|
681
|
+
const finding = (kind, detail, baseLine, headLine) => ({ kind, effect: testChangeEffect(kind), detail: clip(detail), ...(baseLine ? { baseLine } : {}), ...(headLine ? { headLine } : {}) });
|
|
682
|
+
const assertionText = (entry) => `expect${entry.soft ? ".soft" : entry.poll ? ".poll" : ""}(${entry.subject})${entry.not ? ".not" : ""}.${entry.matcher}(${entry.args})`;
|
|
683
|
+
const MODIFIER_KIND = { skip: "skip-added", fixme: "fixme-added", fail: "fail-added", only: "only-added" };
|
|
684
|
+
const MODIFIER_TEXT = {
|
|
685
|
+
skip: "The test is now skipped (test.skip).",
|
|
686
|
+
fixme: "The test is now marked fixme, so it is skipped.",
|
|
687
|
+
fail: "The test is now expected to fail (test.fail): it passes when it fails.",
|
|
688
|
+
only: "test.only was added: other tests in the run stop running.",
|
|
689
|
+
};
|
|
690
|
+
/** The findings for one test present at both base and head. */
|
|
691
|
+
export function compareTest(base, head) {
|
|
692
|
+
const findings = [];
|
|
693
|
+
for (const modifier of ["skip", "fixme", "fail", "only"]) {
|
|
694
|
+
if (head.modifiers.has(modifier) && !base.modifiers.has(modifier))
|
|
695
|
+
findings.push(finding(MODIFIER_KIND[modifier], MODIFIER_TEXT[modifier], base.line, head.line));
|
|
696
|
+
}
|
|
697
|
+
if (head.modifiers.has("slow") && !base.modifiers.has("slow"))
|
|
698
|
+
findings.push(finding("timeout-raised", "test.slow() was added: the timeout is tripled.", base.line, head.line));
|
|
699
|
+
const runtimeKey = (call) => `${call.modifier}:${call.conditional}`;
|
|
700
|
+
const baseRuntime = new Map();
|
|
701
|
+
for (const call of base.runtime)
|
|
702
|
+
baseRuntime.set(runtimeKey(call), (baseRuntime.get(runtimeKey(call)) ?? 0) + 1);
|
|
703
|
+
for (const call of head.runtime) {
|
|
704
|
+
const key = runtimeKey(call);
|
|
705
|
+
const left = baseRuntime.get(key) ?? 0;
|
|
706
|
+
if (left > 0) {
|
|
707
|
+
baseRuntime.set(key, left - 1);
|
|
708
|
+
continue;
|
|
709
|
+
}
|
|
710
|
+
if (call.modifier === "slow")
|
|
711
|
+
findings.push(finding("timeout-raised", "test.slow() was added: the timeout is tripled.", undefined, call.line));
|
|
712
|
+
else if (call.conditional && call.modifier !== "fail")
|
|
713
|
+
findings.push(finding("conditional-skip-added", `A conditional test.${call.modifier}(…) was added.`, undefined, call.line));
|
|
714
|
+
else if (call.modifier === "fail")
|
|
715
|
+
findings.push(finding("fail-added", "test.fail() was added: the test passes when it fails.", undefined, call.line));
|
|
716
|
+
else
|
|
717
|
+
findings.push(finding(MODIFIER_KIND[call.modifier], `test.${call.modifier}() was added in the test body: the test is skipped.`, undefined, call.line));
|
|
718
|
+
}
|
|
719
|
+
const pairs = align(base.expectations, head.expectations);
|
|
720
|
+
const matchedBase = new Set(pairs.map(([i]) => i));
|
|
721
|
+
const matchedHead = new Set(pairs.map(([, j]) => j));
|
|
722
|
+
base.expectations.forEach((entry, i) => { if (!matchedBase.has(i))
|
|
723
|
+
findings.push(finding("assertion-removed", `The assertion ${assertionText(entry)} was removed.`, entry.line)); });
|
|
724
|
+
for (const [i, j] of pairs) {
|
|
725
|
+
const before = base.expectations[i];
|
|
726
|
+
const after = head.expectations[j];
|
|
727
|
+
if (before.args !== after.args)
|
|
728
|
+
findings.push(finding("expected-changed", `expect(${after.subject}).${after.matcher}: the expected value changed from (${before.args}) to (${after.args}).`, before.line, after.line));
|
|
729
|
+
if (before.not !== after.not)
|
|
730
|
+
findings.push(finding("negation-changed", `expect(${after.subject}).${after.matcher}: .not was ${after.not ? "added" : "removed"}.`, before.line, after.line));
|
|
731
|
+
if (!before.soft && after.soft)
|
|
732
|
+
findings.push(finding("assertion-soft", `expect(${after.subject}).${after.matcher} is now a soft assertion.`, before.line, after.line));
|
|
733
|
+
if (!before.conditional && after.conditional)
|
|
734
|
+
findings.push(finding("assertion-made-conditional", `The assertion ${assertionText(after)} now runs only conditionally (inside if, else, try or catch, or before .catch).`, before.line, after.line));
|
|
735
|
+
}
|
|
736
|
+
head.expectations.forEach((entry, j) => { if (!matchedHead.has(j))
|
|
737
|
+
findings.push(finding("assertion-added", `The assertion ${assertionText(entry)} was added.`, undefined, entry.line)); });
|
|
738
|
+
const stepPairs = align(base.steps, head.steps);
|
|
739
|
+
const stepBase = new Set(stepPairs.map(([i]) => i));
|
|
740
|
+
const stepHead = new Set(stepPairs.map(([, j]) => j));
|
|
741
|
+
// Between two aligned steps, a step whose receiver changed but whose method did not is a retarget, reported once
|
|
742
|
+
// as target-changed below, not as one step removed and another added.
|
|
743
|
+
const anchors = [[-1, -1], ...stepPairs, [base.steps.length, head.steps.length]];
|
|
744
|
+
for (let gap = 0; gap + 1 < anchors.length; gap++) {
|
|
745
|
+
let j = anchors[gap][1] + 1;
|
|
746
|
+
for (let i = anchors[gap][0] + 1; i < anchors[gap + 1][0]; i++) {
|
|
747
|
+
for (let candidate = j; candidate < anchors[gap + 1][1]; candidate++) {
|
|
748
|
+
if (base.steps[i].method === head.steps[candidate].method && base.steps[i].method !== "goto") {
|
|
749
|
+
stepBase.add(i);
|
|
750
|
+
stepHead.add(candidate);
|
|
751
|
+
j = candidate + 1;
|
|
752
|
+
break;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
base.steps.forEach((step, i) => { if (!stepBase.has(i))
|
|
758
|
+
findings.push(finding("action-removed", `The step ${step.text} was removed.`, step.line)); });
|
|
759
|
+
head.steps.forEach((step, j) => { if (!stepHead.has(j))
|
|
760
|
+
findings.push(finding("action-added", `The step ${step.text} was added.`, undefined, step.line)); });
|
|
761
|
+
const removed = [...new Set(base.locators.filter((locator) => !head.locators.includes(locator)))];
|
|
762
|
+
const added = [...new Set(head.locators.filter((locator) => !base.locators.includes(locator)))];
|
|
763
|
+
if (removed.length && added.length)
|
|
764
|
+
findings.push(finding("target-changed", `Locators removed: ${removed.join(", ")}; added: ${added.join(", ")}.`, base.line, head.line));
|
|
765
|
+
// A readable name swapped for one shaped like an untranslated message key: if the page now shows that key, the
|
|
766
|
+
// test was changed to accept a missing translation.
|
|
767
|
+
const keyNames = [...new Set(head.names.filter((name) => messageKey(name) && !base.names.includes(name)))];
|
|
768
|
+
if (removed.length && added.length && keyNames.length && base.names.some((name) => !messageKey(name) && !head.names.includes(name))) {
|
|
769
|
+
findings.push(finding("target-message-key", `A locator now asks for ${keyNames.map((name) => JSON.stringify(name)).join(", ")}, shaped like an untranslated message key, where it asked for a readable name.`, base.line, head.line));
|
|
770
|
+
}
|
|
771
|
+
if (head.narrowing > base.narrowing)
|
|
772
|
+
findings.push(finding("first-added", ".first(), .last() or .nth() was added: a duplicated element would no longer fail the test.", base.line, head.line));
|
|
773
|
+
if (head.timeout > base.timeout)
|
|
774
|
+
findings.push(finding("timeout-raised", `The largest timeout in the test went from ${base.timeout || "the default"} to ${head.timeout} ms.`, base.line, head.line));
|
|
775
|
+
return findings;
|
|
776
|
+
}
|
|
777
|
+
/**
|
|
778
|
+
* Pairs the tests of two versions of one file and lists what changed. A missing side means the file was added or
|
|
779
|
+
* deleted. Tests are paired by describe path and title; a renamed test only when its body is token-identical.
|
|
780
|
+
*/
|
|
781
|
+
export function compareTestFiles(base, head) {
|
|
782
|
+
const key = (test) => test.title.join("\u0000");
|
|
783
|
+
const baseTests = base?.tests ?? [];
|
|
784
|
+
const headTests = head?.tests ?? [];
|
|
785
|
+
const headByKey = new Map(headTests.map((test) => [key(test), test]));
|
|
786
|
+
const pairedHead = new Set();
|
|
787
|
+
const unpairedBase = [];
|
|
788
|
+
const results = [];
|
|
789
|
+
for (const test of baseTests) {
|
|
790
|
+
const match = headByKey.get(key(test));
|
|
791
|
+
if (match) {
|
|
792
|
+
pairedHead.add(match);
|
|
793
|
+
const findings = compareTest(test, match);
|
|
794
|
+
if (findings.length)
|
|
795
|
+
results.push({ title: plainTitle(match.title), baseLine: test.line, headLine: match.line, findings });
|
|
796
|
+
}
|
|
797
|
+
else
|
|
798
|
+
unpairedBase.push(test);
|
|
799
|
+
}
|
|
800
|
+
const unpairedHead = headTests.filter((test) => !pairedHead.has(test));
|
|
801
|
+
for (const test of unpairedBase) {
|
|
802
|
+
const renamed = unpairedHead.find((candidate) => candidate.bodyHash === test.bodyHash && !pairedHead.has(candidate));
|
|
803
|
+
if (renamed) {
|
|
804
|
+
pairedHead.add(renamed);
|
|
805
|
+
const findings = compareTest(test, renamed);
|
|
806
|
+
if (findings.length)
|
|
807
|
+
results.push({ title: plainTitle(renamed.title), baseLine: test.line, headLine: renamed.line, findings });
|
|
808
|
+
continue;
|
|
809
|
+
}
|
|
810
|
+
results.push({ title: plainTitle(test.title), baseLine: test.line, findings: [finding("test-removed", "The test was removed.", test.line)] });
|
|
811
|
+
}
|
|
812
|
+
for (const test of unpairedHead) {
|
|
813
|
+
if (pairedHead.has(test))
|
|
814
|
+
continue;
|
|
815
|
+
const findings = [finding("test-added", "The test was added.", undefined, test.line)];
|
|
816
|
+
const empty = { title: test.title, line: test.line, modifiers: new Set(), runtime: [], expectations: [], steps: [], locators: [], names: [], narrowing: 0, timeout: 0, bodyHash: "" };
|
|
817
|
+
// A new test can still be born skipped, fixme'd, failing or only; a conditional skip on a new test is ordinary.
|
|
818
|
+
findings.push(...compareTest(empty, test).filter((entry) => ["skip-added", "fixme-added", "fail-added", "only-added"].includes(entry.kind)));
|
|
819
|
+
results.push({ title: plainTitle(test.title), headLine: test.line, findings });
|
|
820
|
+
}
|
|
821
|
+
return results;
|
|
822
|
+
}
|
|
823
|
+
/**
|
|
824
|
+
* Compares the parameterised tests of a file read on both sides, or returns undefined when they must stay unread.
|
|
825
|
+
* One declaration with a title built at run time stands for as many tests as its parameters, which are set outside
|
|
826
|
+
* the test call, so a pull request that shortens a parameter list removes tests without touching any test body.
|
|
827
|
+
* They are compared only when every unread title is one this reading covers, the skeleton (everything outside the
|
|
828
|
+
* compared bodies, parameter lists and title expressions included) is identical, and each title's source text is
|
|
829
|
+
* unique within the file on each side. The caller adds the guard that no helper file changed beside the tests.
|
|
830
|
+
*/
|
|
831
|
+
export function compareParameterised(base, head) {
|
|
832
|
+
if (!base.unreadableTests.length && !head.unreadableTests.length)
|
|
833
|
+
return [];
|
|
834
|
+
const covered = (read) => { const lines = new Set(read.coveredLines); return read.unreadableTests.every((line) => lines.has(line)); };
|
|
835
|
+
if (!covered(base) || !covered(head) || base.skeleton !== head.skeleton)
|
|
836
|
+
return undefined;
|
|
837
|
+
const key = (test) => test.title.join("\u0000");
|
|
838
|
+
const unique = (read) => new Map(read.parameterised.map((test) => [key(test), test]));
|
|
839
|
+
const baseByKey = unique(base);
|
|
840
|
+
const headByKey = unique(head);
|
|
841
|
+
if (baseByKey.size !== base.parameterised.length || headByKey.size !== head.parameterised.length || baseByKey.size !== headByKey.size)
|
|
842
|
+
return undefined;
|
|
843
|
+
const results = [];
|
|
844
|
+
for (const [title, test] of baseByKey) {
|
|
845
|
+
const match = headByKey.get(title);
|
|
846
|
+
if (!match)
|
|
847
|
+
return undefined;
|
|
848
|
+
const findings = compareTest(test, match);
|
|
849
|
+
if (findings.length)
|
|
850
|
+
results.push({ title: plainTitle(match.title), baseLine: test.line, headLine: match.line, findings });
|
|
851
|
+
}
|
|
852
|
+
return results;
|
|
853
|
+
}
|
|
854
|
+
/** Whether the test declared at this line is parameterised: one line may stand for several tests. */
|
|
855
|
+
export function isParameterised(read, line) {
|
|
856
|
+
return read.parameterised.some((test) => test.line === line);
|
|
857
|
+
}
|
|
858
|
+
const SNAPSHOT_MATCHERS = new Set(["toHaveScreenshot", "toMatchSnapshot"]);
|
|
859
|
+
/** Whether the source runs tests in serial mode, where one test cannot run on its own. */
|
|
860
|
+
export function runsSerially(tokens) {
|
|
861
|
+
return tokens.some((token, index) => (token.type === "ident" && token.value === "serial" && tokens[index - 1]?.value === ".")
|
|
862
|
+
|| (token.type === "ident" && token.value === "mode" && tokens[index + 1]?.value === ":" && tokens[index + 2]?.type === "string" && tokens[index + 2]?.value === "serial"));
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* An unchanged test to run beside flagged ones as a control: not listed in `reported` (lines with findings), not a
|
|
866
|
+
* hook, not skipped, fixme'd or expected to fail, without runtime skip calls or snapshot files.
|
|
867
|
+
*/
|
|
868
|
+
export function controlTestLine(read, reported) {
|
|
869
|
+
return read.tests.find((test) => !reported.has(test.line) && !test.title.at(-1).startsWith("[")
|
|
870
|
+
&& !["skip", "fixme", "fail"].some((modifier) => test.modifiers.has(modifier)) && !test.runtime.length
|
|
871
|
+
&& !test.expectations.some((entry) => SNAPSHOT_MATCHERS.has(entry.matcher)))?.line;
|
|
872
|
+
}
|
|
873
|
+
/** Whether the test declared at `line` compares snapshot files, which are found by the test file's name. */
|
|
874
|
+
export function usesSnapshotFiles(read, line) {
|
|
875
|
+
return read.tests.some((test) => test.line === line && test.expectations.some((entry) => SNAPSHOT_MATCHERS.has(entry.matcher)));
|
|
876
|
+
}
|
|
877
|
+
/**
|
|
878
|
+
* How a test is turned off for every run: a skip, fixme or fail modifier on the test or an enclosing describe, or an
|
|
879
|
+
* unconditional test.skip()/fixme()/fail() call in its body or group. Conditional calls do not count. Hooks are never disabled.
|
|
880
|
+
*/
|
|
881
|
+
export function disabledState(test) {
|
|
882
|
+
if (!test.ownModifiers)
|
|
883
|
+
return undefined; // a hook
|
|
884
|
+
for (const how of ["skip", "fixme", "fail"]) {
|
|
885
|
+
if (test.modifiers.has(how))
|
|
886
|
+
return { how, scope: test.ownModifiers?.has(how) ? "test" : "group" };
|
|
887
|
+
const call = test.runtime.find((entry) => entry.modifier === how && !entry.conditional);
|
|
888
|
+
if (call)
|
|
889
|
+
return { how, scope: call.group ? "group" : "test" };
|
|
890
|
+
}
|
|
891
|
+
return undefined;
|
|
892
|
+
}
|
|
893
|
+
/** A conditional skip or fixme (for example, one browser only), on a test that is not otherwise turned off. */
|
|
894
|
+
export function conditionallySkipped(test) {
|
|
895
|
+
return !disabledState(test) && test.runtime.some((entry) => (entry.modifier === "skip" || entry.modifier === "fixme") && entry.conditional);
|
|
896
|
+
}
|