@virtocommerce/test-automation-framework 0.0.0-stage → 0.1.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.biome/formatter.json +11 -0
- package/.biome/imports.json +52 -0
- package/.biome/preset.json +4 -0
- package/.biome/rules.json +45 -0
- package/.claude/agents/qa-automation-expert.md +119 -0
- package/.claude/agents/qa-coverage.md +96 -0
- package/.claude/agents/qa-explorer.md +112 -0
- package/.claude/agents/qa-healer.md +88 -0
- package/.claude/agents/qa-judge.md +102 -0
- package/.claude/agents/qa-planner.md +104 -0
- package/.claude/agents/qa-reviewer.md +121 -0
- package/.claude/checks/browser-secrets.mts +107 -0
- package/.claude/checks/contract.mts +469 -0
- package/.claude/checks/expectation-guard.mts +665 -0
- package/.claude/checks/explore-extractors.mts +4 -0
- package/.claude/checks/judge-verdict.mts +165 -0
- package/.claude/checks/project-facts.mts +491 -0
- package/.claude/checks/prove-fails.mts +770 -0
- package/.claude/checks/rank-gaps.mts +177 -0
- package/.claude/checks/readonly-shell.mts +740 -0
- package/.claude/checks/reconcile-run.mts +270 -0
- package/.claude/checks/scan-prompts.mts +351 -0
- package/.claude/checks/stand.mts +45 -0
- package/.claude/checks/test-duration.mts +134 -0
- package/.claude/checks/test-shape.mts +224 -0
- package/.claude/checks/ticket-card.mts +195 -0
- package/.claude/commands/qa-coverage.md +13 -0
- package/.claude/commands/qa-explore.md +17 -0
- package/.claude/commands/qa-heal.md +44 -0
- package/.claude/commands/qa-implement.md +60 -0
- package/.claude/commands/qa-plan.md +55 -0
- package/.claude/commands/qa-review.md +90 -0
- package/.claude/commands/qa.md +156 -0
- package/.claude/hooks/readonly-shell.mts +65 -0
- package/.claude/settings.json +15 -0
- package/.claude/skills/coverage-analysis/SKILL.md +107 -0
- package/.claude/skills/create-graphql-layer/SKILL.md +159 -0
- package/.claude/skills/create-ui-layer/SKILL.md +189 -0
- package/.claude/skills/explore-api/SKILL.md +74 -0
- package/.claude/skills/explore-frontend/SKILL.md +85 -0
- package/.claude/skills/framework-rules/SKILL.md +189 -0
- package/.claude/skills/heal-tests/SKILL.md +197 -0
- package/.claude/skills/heal-tests/guard-recipe.md +53 -0
- package/.claude/skills/judge-criteria/SKILL.md +40 -0
- package/.claude/skills/judge-criteria/coverage.md +9 -0
- package/.claude/skills/judge-criteria/expert.md +23 -0
- package/.claude/skills/judge-criteria/explorer.md +9 -0
- package/.claude/skills/judge-criteria/healer.md +13 -0
- package/.claude/skills/judge-criteria/planner.md +12 -0
- package/.claude/skills/plan-implementation/SKILL.md +82 -0
- package/.claude/skills/plan-implementation/contract-template.md +72 -0
- package/.claude/skills/plan-test-strategy/SKILL.md +100 -0
- package/.claude/skills/project-facts/SKILL.md +130 -0
- package/.claude/skills/read-change/SKILL.md +64 -0
- package/.claude/skills/read-ticket/SKILL.md +63 -0
- package/.claude/skills/read-ticket/providers.md +58 -0
- package/.claude/skills/review-pr/SKILL.md +152 -0
- package/.claude/skills/review-test-code/SKILL.md +208 -0
- package/.claude/skills/write-e2e-test/SKILL.md +175 -0
- package/.claude/skills/write-graphql-test/SKILL.md +158 -0
- package/.claude/skills/write-rest-api-test/SKILL.md +182 -0
- package/.claude/skills/write-security-test/SKILL.md +253 -0
- package/.qa/project.example.json +71 -0
- package/CHANGELOG.md +1 -0
- package/LICENSE +12 -0
- package/README.md +53 -2
- package/bin/install-qa-agents.mjs +94 -0
- package/dist/auth/credentials.d.ts +10 -0
- package/dist/auth/credentials.js +2 -0
- package/dist/auth/credentials.js.map +1 -0
- package/dist/auth/frontend-browser-sessions.d.ts +71 -0
- package/dist/auth/frontend-browser-sessions.js +130 -0
- package/dist/auth/frontend-browser-sessions.js.map +1 -0
- package/dist/auth/index.d.ts +9 -0
- package/dist/auth/index.js +5 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/platform-browser-sessions.d.ts +47 -0
- package/dist/auth/platform-browser-sessions.js +92 -0
- package/dist/auth/platform-browser-sessions.js.map +1 -0
- package/dist/auth/sign-in-lock.d.ts +14 -0
- package/dist/auth/sign-in-lock.js +70 -0
- package/dist/auth/sign-in-lock.js.map +1 -0
- package/dist/auth/storage-state.d.ts +3 -0
- package/dist/auth/storage-state.js +2 -0
- package/dist/auth/storage-state.js.map +1 -0
- package/dist/auth/token-client.d.ts +82 -0
- package/dist/auth/token-client.js +111 -0
- package/dist/auth/token-client.js.map +1 -0
- package/dist/auth/token-manager.d.ts +67 -0
- package/dist/auth/token-manager.js +138 -0
- package/dist/auth/token-manager.js.map +1 -0
- package/dist/core/default-reporters.d.ts +31 -0
- package/dist/core/default-reporters.js +36 -0
- package/dist/core/default-reporters.js.map +1 -0
- package/dist/core/default-use.d.ts +28 -0
- package/dist/core/default-use.js +25 -0
- package/dist/core/default-use.js.map +1 -0
- package/dist/core/env.d.ts +24 -0
- package/dist/core/env.js +33 -0
- package/dist/core/env.js.map +1 -0
- package/dist/core/error-message.d.ts +10 -0
- package/dist/core/error-message.js +13 -0
- package/dist/core/error-message.js.map +1 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.js +11 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/is-ci.d.ts +14 -0
- package/dist/core/is-ci.js +18 -0
- package/dist/core/is-ci.js.map +1 -0
- package/dist/core/parse-env.d.ts +13 -0
- package/dist/core/parse-env.js +21 -0
- package/dist/core/parse-env.js.map +1 -0
- package/dist/core/projects.d.ts +24 -0
- package/dist/core/projects.js +33 -0
- package/dist/core/projects.js.map +1 -0
- package/dist/core/run-options.d.ts +31 -0
- package/dist/core/run-options.js +43 -0
- package/dist/core/run-options.js.map +1 -0
- package/dist/core/secret.d.ts +105 -0
- package/dist/core/secret.js +167 -0
- package/dist/core/secret.js.map +1 -0
- package/dist/core/string-schemas.d.ts +39 -0
- package/dist/core/string-schemas.js +56 -0
- package/dist/core/string-schemas.js.map +1 -0
- package/dist/core/url-path.d.ts +7 -0
- package/dist/core/url-path.js +2 -0
- package/dist/core/url-path.js.map +1 -0
- package/dist/dataset/collection.d.ts +44 -0
- package/dist/dataset/collection.js +76 -0
- package/dist/dataset/collection.js.map +1 -0
- package/dist/dataset/index.d.ts +11 -0
- package/dist/dataset/index.js +6 -0
- package/dist/dataset/index.js.map +1 -0
- package/dist/dataset/load-dataset.d.ts +39 -0
- package/dist/dataset/load-dataset.js +62 -0
- package/dist/dataset/load-dataset.js.map +1 -0
- package/dist/dataset/manifest.d.ts +77 -0
- package/dist/dataset/manifest.js +10 -0
- package/dist/dataset/manifest.js.map +1 -0
- package/dist/dataset/placeholders.d.ts +66 -0
- package/dist/dataset/placeholders.js +87 -0
- package/dist/dataset/placeholders.js.map +1 -0
- package/dist/dataset/seed-collection.d.ts +53 -0
- package/dist/dataset/seed-collection.js +115 -0
- package/dist/dataset/seed-collection.js.map +1 -0
- package/dist/dataset/seed-dataset.d.ts +70 -0
- package/dist/dataset/seed-dataset.js +138 -0
- package/dist/dataset/seed-dataset.js.map +1 -0
- package/dist/dataset/seed-events.d.ts +81 -0
- package/dist/dataset/seed-events.js +2 -0
- package/dist/dataset/seed-events.js.map +1 -0
- package/dist/dataset/seed-requests.d.ts +102 -0
- package/dist/dataset/seed-requests.js +160 -0
- package/dist/dataset/seed-requests.js.map +1 -0
- package/dist/dataset/seed-scope.d.ts +39 -0
- package/dist/dataset/seed-scope.js +67 -0
- package/dist/dataset/seed-scope.js.map +1 -0
- package/dist/fixtures/default-fixtures.d.ts +93 -0
- package/dist/fixtures/default-fixtures.js +105 -0
- package/dist/fixtures/default-fixtures.js.map +1 -0
- package/dist/fixtures/index.d.ts +2 -0
- package/dist/fixtures/index.js +2 -0
- package/dist/fixtures/index.js.map +1 -0
- package/dist/graphql/graphql-client.d.ts +78 -0
- package/dist/graphql/graphql-client.js +83 -0
- package/dist/graphql/graphql-client.js.map +1 -0
- package/dist/graphql/graphql-codegen.d.ts +56 -0
- package/dist/graphql/graphql-codegen.js +42 -0
- package/dist/graphql/graphql-codegen.js.map +1 -0
- package/dist/graphql/graphql-error.d.ts +54 -0
- package/dist/graphql/graphql-error.js +74 -0
- package/dist/graphql/graphql-error.js.map +1 -0
- package/dist/graphql/index.d.ts +6 -0
- package/dist/graphql/index.js +4 -0
- package/dist/graphql/index.js.map +1 -0
- package/dist/http/authentication.d.ts +16 -0
- package/dist/http/authentication.js +2 -0
- package/dist/http/authentication.js.map +1 -0
- package/dist/http/http-client.d.ts +110 -0
- package/dist/http/http-client.js +237 -0
- package/dist/http/http-client.js.map +1 -0
- package/dist/http/http-error.d.ts +26 -0
- package/dist/http/http-error.js +34 -0
- package/dist/http/http-error.js.map +1 -0
- package/dist/http/http-method.d.ts +2 -0
- package/dist/http/http-method.js +2 -0
- package/dist/http/http-method.js.map +1 -0
- package/dist/http/http-response.d.ts +137 -0
- package/dist/http/http-response.js +166 -0
- package/dist/http/http-response.js.map +1 -0
- package/dist/http/index.d.ts +7 -0
- package/dist/http/index.js +4 -0
- package/dist/http/index.js.map +1 -0
- package/dist/http/request-options.d.ts +62 -0
- package/dist/http/request-options.js +2 -0
- package/dist/http/request-options.js.map +1 -0
- package/dist/modules/index.d.ts +2 -0
- package/dist/modules/index.js +2 -0
- package/dist/modules/index.js.map +1 -0
- package/dist/modules/installed-modules.d.ts +86 -0
- package/dist/modules/installed-modules.js +135 -0
- package/dist/modules/installed-modules.js.map +1 -0
- package/docs/README.md +43 -0
- package/docs/concepts/areas.md +38 -0
- package/docs/concepts/auth.md +92 -0
- package/docs/concepts/config.md +102 -0
- package/docs/concepts/dataset.md +183 -0
- package/docs/concepts/environment.md +54 -0
- package/docs/concepts/fixtures.md +109 -0
- package/docs/concepts/graphql.md +76 -0
- package/docs/concepts/http.md +88 -0
- package/docs/concepts/modules.md +63 -0
- package/docs/concepts/secrets.md +50 -0
- package/docs/contributing/architecture.md +67 -0
- package/docs/contributing/conventions.md +39 -0
- package/docs/contributing/documentation.md +37 -0
- package/docs/contributing/releasing.md +76 -0
- package/docs/examples/auth.ts +20 -0
- package/docs/examples/config.ts +36 -0
- package/docs/examples/dataset.ts +70 -0
- package/docs/examples/environment.ts +8 -0
- package/docs/examples/fixtures.ts +41 -0
- package/docs/examples/getting-started.ts +22 -0
- package/docs/examples/graphql-codegen.ts +9 -0
- package/docs/examples/graphql.ts +32 -0
- package/docs/examples/http.ts +18 -0
- package/docs/examples/modules.ts +19 -0
- package/docs/examples/secrets.ts +11 -0
- package/docs/examples/tsconfig.json +10 -0
- package/docs/getting-started.md +106 -0
- package/docs/guides/generating-types.md +75 -0
- package/docs/guides/qa-agents.md +66 -0
- package/package.json +156 -4
|
@@ -0,0 +1,770 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import {
|
|
3
|
+
copyFileSync,
|
|
4
|
+
existsSync,
|
|
5
|
+
lstatSync,
|
|
6
|
+
mkdirSync,
|
|
7
|
+
mkdtempSync,
|
|
8
|
+
readdirSync,
|
|
9
|
+
readFileSync,
|
|
10
|
+
rmSync,
|
|
11
|
+
statSync,
|
|
12
|
+
symlinkSync,
|
|
13
|
+
unlinkSync,
|
|
14
|
+
writeFileSync,
|
|
15
|
+
} from "node:fs";
|
|
16
|
+
import { tmpdir } from "node:os";
|
|
17
|
+
import { basename, dirname, isAbsolute, join, posix, relative, resolve } from "node:path";
|
|
18
|
+
|
|
19
|
+
import { parse } from "@babel/parser";
|
|
20
|
+
|
|
21
|
+
import { playwrightConfigArgs, ROOT_SUITE_DIR, readProjectFactsIn } from "./project-facts.mts";
|
|
22
|
+
|
|
23
|
+
export type ProveFailsResult = { failed: boolean; output: string; mutated: string };
|
|
24
|
+
|
|
25
|
+
type TestRef = { project: string; title: string; line: number; expectedStatus: string };
|
|
26
|
+
|
|
27
|
+
type Outcome = { status: string; expectedStatus: string; messages: string[] };
|
|
28
|
+
|
|
29
|
+
type AstNode = Record<string, unknown> & { type: string; start: number; end: number };
|
|
30
|
+
|
|
31
|
+
type Edit = { start: number; end: number; text: string };
|
|
32
|
+
|
|
33
|
+
const CLI_FILE_NAME = "prove-fails.mts";
|
|
34
|
+
const TEMP_PREFIX = "prove-fails-";
|
|
35
|
+
const PLAYWRIGHT_CLI = ["node_modules", "@playwright", "test", "cli.js"];
|
|
36
|
+
const RUN_TIMEOUT_MS = 300_000;
|
|
37
|
+
const MAX_BUFFER = 64 * 1024 * 1024;
|
|
38
|
+
const MAX_OUTPUT = 3000;
|
|
39
|
+
const MAX_PROBLEM = 300;
|
|
40
|
+
const PROJECT_FLAG = /^--project=([\w.-]+)$/;
|
|
41
|
+
const ANSI = new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, "g");
|
|
42
|
+
const REGEX_SPECIALS = /[.*+?^${}()|[\]\\]/g;
|
|
43
|
+
const ENV_FILE = /^\.env(\..+)?$/;
|
|
44
|
+
const ENV_EXAMPLE = ".env.example";
|
|
45
|
+
const MUTATION_SUFFIX = "__mutated__";
|
|
46
|
+
const MATCHERS = [
|
|
47
|
+
"toBe",
|
|
48
|
+
"toEqual",
|
|
49
|
+
"toStrictEqual",
|
|
50
|
+
"toMatch",
|
|
51
|
+
"toContain",
|
|
52
|
+
"toHaveLength",
|
|
53
|
+
"toHaveCount",
|
|
54
|
+
"toHaveText",
|
|
55
|
+
"toContainText",
|
|
56
|
+
"toHaveValue",
|
|
57
|
+
"toHaveURL",
|
|
58
|
+
"toHaveTitle",
|
|
59
|
+
];
|
|
60
|
+
const MATCHER_CHAIN = ["resolves", "rejects"];
|
|
61
|
+
const NEGATION = "not";
|
|
62
|
+
const LINE_BREAK = new RegExp(`\\r\\n|[\\n\\r${String.fromCharCode(0x2028)}${String.fromCharCode(0x2029)}]`);
|
|
63
|
+
const QA_DIR = ".qa";
|
|
64
|
+
const EXPECT_VARIANTS = ["soft", "poll"];
|
|
65
|
+
const TEST_FUNCTIONS = ["ArrowFunctionExpression", "FunctionExpression"];
|
|
66
|
+
const CALLS = ["CallExpression", "OptionalCallExpression"];
|
|
67
|
+
const MEMBERS = ["MemberExpression", "OptionalMemberExpression"];
|
|
68
|
+
const TYPE_WRAPPERS = ["TSNonNullExpression", "TSAsExpression", "TSSatisfiesExpression", "TSTypeAssertion"];
|
|
69
|
+
const SKIPPED_KEYS = new Set([
|
|
70
|
+
"loc",
|
|
71
|
+
"extra",
|
|
72
|
+
"leadingComments",
|
|
73
|
+
"trailingComments",
|
|
74
|
+
"innerComments",
|
|
75
|
+
"comments",
|
|
76
|
+
"tokens",
|
|
77
|
+
]);
|
|
78
|
+
const INHERITED_PLAYWRIGHT = /^(PW_|TEST_WORKER_INDEX$|TEST_PARALLEL_INDEX$|FORCE_COLOR$)/;
|
|
79
|
+
|
|
80
|
+
function problem(text: string): Error {
|
|
81
|
+
return new Error(`prove-fails: ${text}`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
85
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function isNode(value: unknown): value is AstNode {
|
|
89
|
+
return (
|
|
90
|
+
isRecord(value) &&
|
|
91
|
+
typeof value["type"] === "string" &&
|
|
92
|
+
typeof value["start"] === "number" &&
|
|
93
|
+
typeof value["end"] === "number"
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function firstLine(text: string): string {
|
|
98
|
+
return (text.trim().split("\n")[0] ?? "").trim().slice(0, MAX_PROBLEM);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function normalize(path: string): string {
|
|
102
|
+
return path.replaceAll("\\", "/");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function isInside(parent: string, candidate: string): boolean {
|
|
106
|
+
const path = relative(parent, candidate);
|
|
107
|
+
return path !== "" && !path.startsWith("..") && !isAbsolute(path);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function quietly(action: () => void): void {
|
|
111
|
+
try {
|
|
112
|
+
action();
|
|
113
|
+
} catch {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function exitCodeOf(error: { code?: number | string | null | undefined } | null): number {
|
|
119
|
+
if (error === null) {
|
|
120
|
+
return 0;
|
|
121
|
+
}
|
|
122
|
+
return typeof error.code === "number" ? error.code : -1;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function run(
|
|
126
|
+
file: string,
|
|
127
|
+
args: readonly string[],
|
|
128
|
+
cwd: string,
|
|
129
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
130
|
+
): Promise<{ stdout: string; stderr: string; failed: boolean; code: number }> {
|
|
131
|
+
return new Promise((done) => {
|
|
132
|
+
const child = execFile(
|
|
133
|
+
file,
|
|
134
|
+
[...args],
|
|
135
|
+
{ cwd, env, timeout: RUN_TIMEOUT_MS, maxBuffer: MAX_BUFFER, encoding: "utf8", windowsHide: true },
|
|
136
|
+
(error, stdout, stderr) => {
|
|
137
|
+
done({ stdout, stderr, failed: error !== null, code: exitCodeOf(error) });
|
|
138
|
+
},
|
|
139
|
+
);
|
|
140
|
+
child.stdin?.end();
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function git(args: readonly string[], cwd: string): Promise<string> {
|
|
145
|
+
const result = await run("git", args, cwd);
|
|
146
|
+
if (result.failed) {
|
|
147
|
+
throw problem(`git ${args[0] ?? ""} failed: ${firstLine(result.stderr)}`);
|
|
148
|
+
}
|
|
149
|
+
return result.stdout;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function childrenOf(node: AstNode): AstNode[] {
|
|
153
|
+
const children: AstNode[] = [];
|
|
154
|
+
for (const [key, value] of Object.entries(node)) {
|
|
155
|
+
if (!SKIPPED_KEYS.has(key)) {
|
|
156
|
+
children.push(...(Array.isArray(value) ? value : [value]).filter(isNode));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return children;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function collect(node: AstNode, wanted: (node: AstNode) => boolean, found: AstNode[] = []): AstNode[] {
|
|
163
|
+
if (wanted(node)) {
|
|
164
|
+
found.push(node);
|
|
165
|
+
}
|
|
166
|
+
for (const child of childrenOf(node)) {
|
|
167
|
+
collect(child, wanted, found);
|
|
168
|
+
}
|
|
169
|
+
return found;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function lineOf(node: unknown, which: "start" | "end" = "start"): number {
|
|
173
|
+
const loc = isRecord(node) ? node["loc"] : undefined;
|
|
174
|
+
const position = isRecord(loc) ? loc[which] : undefined;
|
|
175
|
+
const line = isRecord(position) ? position["line"] : undefined;
|
|
176
|
+
return typeof line === "number" ? line : 0;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function nameOf(node: unknown): string | undefined {
|
|
180
|
+
return isNode(node) && node.type === "Identifier" && typeof node["name"] === "string" ? node["name"] : undefined;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function isMember(node: unknown): node is AstNode {
|
|
184
|
+
return isNode(node) && node.type === "MemberExpression" && node["computed"] !== true;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function unwrapped(node: unknown): unknown {
|
|
188
|
+
let current = node;
|
|
189
|
+
while (isNode(current) && TYPE_WRAPPERS.includes(current.type)) {
|
|
190
|
+
current = current["expression"];
|
|
191
|
+
}
|
|
192
|
+
return current;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function isExpectCall(node: unknown): boolean {
|
|
196
|
+
if (!isNode(node) || !CALLS.includes(node.type)) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
const callee = unwrapped(node["callee"]);
|
|
200
|
+
if (nameOf(callee) === "expect") {
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
return (
|
|
204
|
+
isMember(callee) &&
|
|
205
|
+
nameOf(callee["object"]) === "expect" &&
|
|
206
|
+
EXPECT_VARIANTS.includes(nameOf(callee["property"]) ?? "")
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function isAssertionCall(node: AstNode): boolean {
|
|
211
|
+
const callee = unwrapped(node["callee"]);
|
|
212
|
+
if (!CALLS.includes(node.type) || !isNode(callee) || !MEMBERS.includes(callee.type)) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
let current = unwrapped(callee["object"]);
|
|
216
|
+
while (isNode(current) && MEMBERS.includes(current.type)) {
|
|
217
|
+
current = unwrapped(current["object"]);
|
|
218
|
+
}
|
|
219
|
+
return isExpectCall(current);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function argumentsOf(call: AstNode): unknown[] {
|
|
223
|
+
return Array.isArray(call["arguments"]) ? call["arguments"] : [];
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function matcherOf(node: AstNode): { negated: boolean } | undefined {
|
|
227
|
+
if (node.type !== "CallExpression") {
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
const callee = node["callee"];
|
|
231
|
+
if (!isMember(callee) || !MATCHERS.includes(nameOf(callee["property"]) ?? "")) {
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
let negated = false;
|
|
235
|
+
let current: unknown = callee["object"];
|
|
236
|
+
while (isMember(current) && [NEGATION, ...MATCHER_CHAIN].includes(nameOf(current["property"]) ?? "")) {
|
|
237
|
+
negated = negated || nameOf(current["property"]) === NEGATION;
|
|
238
|
+
current = current["object"];
|
|
239
|
+
}
|
|
240
|
+
return isExpectCall(current) ? { negated } : undefined;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function jsonString(text: string): string {
|
|
244
|
+
return JSON.stringify(text).replaceAll("\u2028", "\\u2028").replaceAll("\u2029", "\\u2029");
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function plainTemplateText(argument: AstNode): string | undefined {
|
|
248
|
+
const quasis = argument["quasis"];
|
|
249
|
+
const first = Array.isArray(quasis) ? quasis[0] : undefined;
|
|
250
|
+
const cooked = isRecord(first) && isRecord(first["value"]) ? first["value"]["cooked"] : undefined;
|
|
251
|
+
const expressions = argument["expressions"];
|
|
252
|
+
return Array.isArray(expressions) && expressions.length === 0 && typeof cooked === "string" ? cooked : undefined;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function mutationOf(argument: AstNode): string | undefined {
|
|
256
|
+
const value = argument["value"];
|
|
257
|
+
if (argument.type === "NumericLiteral" && typeof value === "number") {
|
|
258
|
+
return Number.isFinite(value) && value + 1 !== value ? String(value + 1) : undefined;
|
|
259
|
+
}
|
|
260
|
+
if (argument.type === "StringLiteral" && typeof value === "string") {
|
|
261
|
+
return jsonString(`${value}${MUTATION_SUFFIX}`);
|
|
262
|
+
}
|
|
263
|
+
const text = argument.type === "TemplateLiteral" ? plainTemplateText(argument) : undefined;
|
|
264
|
+
return text === undefined ? undefined : jsonString(`${text}${MUTATION_SUFFIX}`);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function parseSource(source: string): AstNode {
|
|
268
|
+
try {
|
|
269
|
+
const file: unknown = parse(source, { sourceType: "module", plugins: ["typescript"] });
|
|
270
|
+
if (isRecord(file) && isNode(file["program"])) {
|
|
271
|
+
return file["program"];
|
|
272
|
+
}
|
|
273
|
+
} catch (error) {
|
|
274
|
+
throw problem(`the spec cannot be parsed: ${firstLine(error instanceof Error ? error.message : String(error))}`);
|
|
275
|
+
}
|
|
276
|
+
throw problem("the spec cannot be parsed");
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function isTestCall(callee: unknown): boolean {
|
|
280
|
+
return (
|
|
281
|
+
nameOf(callee) === "test" ||
|
|
282
|
+
(isMember(callee) && nameOf(callee["object"]) === "test" && nameOf(callee["property"]) === "only")
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function checkInsideTest(program: AstNode, testLine: number, line: number, edit: Edit): void {
|
|
287
|
+
const calls = collect(
|
|
288
|
+
program,
|
|
289
|
+
(node) =>
|
|
290
|
+
node.type === "CallExpression" &&
|
|
291
|
+
isTestCall(node["callee"]) &&
|
|
292
|
+
lineOf(node["callee"]) <= testLine &&
|
|
293
|
+
testLine <= lineOf(node["callee"], "end"),
|
|
294
|
+
);
|
|
295
|
+
if (calls.length > 1) {
|
|
296
|
+
throw problem(`more than one test starts on line ${testLine}; put each test on its own line`);
|
|
297
|
+
}
|
|
298
|
+
const inside = calls.some((call) =>
|
|
299
|
+
argumentsOf(call).some(
|
|
300
|
+
(argument) =>
|
|
301
|
+
isNode(argument) &&
|
|
302
|
+
TEST_FUNCTIONS.includes(argument.type) &&
|
|
303
|
+
argument.start <= edit.start &&
|
|
304
|
+
edit.end <= argument.end,
|
|
305
|
+
),
|
|
306
|
+
);
|
|
307
|
+
if (!inside) {
|
|
308
|
+
throw problem(`line ${line} is not inside the test that starts on line ${testLine}`);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function assertionOn(program: AstNode, line: number): AstNode | undefined {
|
|
313
|
+
const onLine = collect(program, isAssertionCall).filter(
|
|
314
|
+
(call) => lineOf(call) === line || argumentsOf(call).some((argument) => lineOf(argument) === line),
|
|
315
|
+
);
|
|
316
|
+
if (onLine.length > 1) {
|
|
317
|
+
throw problem(`line ${line}: more than one assertion on the line; put each assertion on its own line`);
|
|
318
|
+
}
|
|
319
|
+
return onLine[0];
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function editOf(call: AstNode, line: number): Edit | undefined {
|
|
323
|
+
const args = argumentsOf(call);
|
|
324
|
+
const [argument] = args;
|
|
325
|
+
if (args.length !== 1 || !isNode(argument) || lineOf(argument) !== line || matcherOf(call) === undefined) {
|
|
326
|
+
return undefined;
|
|
327
|
+
}
|
|
328
|
+
const text = mutationOf(argument);
|
|
329
|
+
return text === undefined ? undefined : { start: argument.start, end: argument.end, text };
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Makes the mutation `proveFails` runs, and nothing else: it changes exactly one literal, the expected value of an
|
|
334
|
+
* `expect(...).<matcher>(<literal>)` on `line`, where the matcher is one of the single-expected-value matchers
|
|
335
|
+
* (`toBe`, `toEqual`, `toHaveText`, ...), is called with exactly one argument, and is not under `.not`. The line must
|
|
336
|
+
* hold that assertion and no other: every `expect` chain that starts on the line or has an argument there counts,
|
|
337
|
+
* whatever its matcher, so an assertion that cannot be proved is never credited by its neighbour. A bound
|
|
338
|
+
* (`toBeGreaterThan`, `toBeLessThan`, ...) is not changed, because one more rarely breaks it. A number becomes its
|
|
339
|
+
* value plus one, a string or plain template becomes itself with `__mutated__` appended. The source is parsed, so a
|
|
340
|
+
* literal in a comment, a regular expression or a template with substitutions is never a candidate, and no text of
|
|
341
|
+
* the caller enters the result. A name, a key, a file name or a second argument (`toHaveAttribute("href", ...)`,
|
|
342
|
+
* `toHaveScreenshot("a.png")`, `toBeCloseTo(1, 2)`) is never changed. With `testLine` the changed literal must also
|
|
343
|
+
* lie, by position, inside the function of the one `test(...)` or `test.only(...)` that starts there.
|
|
344
|
+
*
|
|
345
|
+
* @returns the mutated source and the mutated line.
|
|
346
|
+
* @throws Error when the source cannot be parsed, the line holds no such assertion, more than one assertion, only a
|
|
347
|
+
* `.not` one, or the literal is outside the test, or more than one test starts on `testLine`.
|
|
348
|
+
*/
|
|
349
|
+
export function mutateAssertion(source: string, line: number, testLine?: number): { source: string; mutated: string } {
|
|
350
|
+
const program = parseSource(source);
|
|
351
|
+
const call = assertionOn(program, line);
|
|
352
|
+
const edit = call === undefined ? undefined : editOf(call, line);
|
|
353
|
+
if (call !== undefined && edit !== undefined && matcherOf(call)?.negated) {
|
|
354
|
+
throw problem(`line ${line} has only a negated assertion: a .not assertion cannot be proved this way`);
|
|
355
|
+
}
|
|
356
|
+
if (edit === undefined) {
|
|
357
|
+
throw problem(
|
|
358
|
+
`line ${line} has no assertion it can change: it needs one expect(...).<matcher>(<one string or number literal>) with a matcher from ${MATCHERS.join(", ")}`,
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
if (testLine !== undefined) {
|
|
362
|
+
checkInsideTest(program, testLine, line, edit);
|
|
363
|
+
}
|
|
364
|
+
const mutated = `${source.slice(0, edit.start)}${edit.text}${source.slice(edit.end)}`;
|
|
365
|
+
return { source: mutated, mutated: mutated.split(LINE_BREAK)[line - 1] ?? "" };
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async function checkedSpec(spec: string, root: string): Promise<string> {
|
|
369
|
+
const path = resolve(root, spec);
|
|
370
|
+
if (isAbsolute(spec) || !isInside(root, path)) {
|
|
371
|
+
throw problem(`${spec} is not a path inside the repository`);
|
|
372
|
+
}
|
|
373
|
+
const specPath = normalize(relative(root, path));
|
|
374
|
+
if (specPath.split("/")[0]?.toLowerCase() === QA_DIR) {
|
|
375
|
+
throw problem(`${spec} is under .qa/ and cannot be proved`);
|
|
376
|
+
}
|
|
377
|
+
if (ENV_FILE.test(basename(specPath).toLowerCase())) {
|
|
378
|
+
throw problem(`${spec} is a .env file and cannot be proved`);
|
|
379
|
+
}
|
|
380
|
+
if (!existsSync(path) || !statSync(path).isFile()) {
|
|
381
|
+
throw problem(`${spec} is not a file`);
|
|
382
|
+
}
|
|
383
|
+
const ignored = await run("git", ["check-ignore", "-q", "--", specPath], root);
|
|
384
|
+
if (ignored.code !== 0 && ignored.code !== 1) {
|
|
385
|
+
throw problem(`git check-ignore failed: ${firstLine(ignored.stderr)}`);
|
|
386
|
+
}
|
|
387
|
+
if (ignored.code === 0) {
|
|
388
|
+
throw problem(`${spec} is ignored by git and cannot be proved`);
|
|
389
|
+
}
|
|
390
|
+
return specPath;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** The spec's path as Playwright reports it: relative to the suite directory, the directory of its config. */
|
|
394
|
+
function suitePathOf(specPath: string, suiteDir: string): string {
|
|
395
|
+
if (suiteDir === ROOT_SUITE_DIR) {
|
|
396
|
+
return specPath;
|
|
397
|
+
}
|
|
398
|
+
const path = posix.relative(suiteDir, specPath);
|
|
399
|
+
if (path === "" || path.startsWith("..") || posix.isAbsolute(path)) {
|
|
400
|
+
throw problem(`${specPath} is not inside the suite directory ${suiteDir}`);
|
|
401
|
+
}
|
|
402
|
+
return path;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function checkedLine(line: number): void {
|
|
406
|
+
if (!Number.isInteger(line) || line < 1) {
|
|
407
|
+
throw problem("the line must be a positive whole number");
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function copyInto(root: string, worktree: string, path: string): void {
|
|
412
|
+
const source = join(root, path);
|
|
413
|
+
const target = join(worktree, path);
|
|
414
|
+
if (existsSync(source) && statSync(source).isFile()) {
|
|
415
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
416
|
+
copyFileSync(source, target);
|
|
417
|
+
} else {
|
|
418
|
+
rmSync(target, { force: true });
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
async function overlayWorkingTree(root: string, worktree: string, spec: string): Promise<void> {
|
|
423
|
+
const changed = (await git(["diff", "--name-only", "--no-renames", "-z", "HEAD"], root)).split("\0");
|
|
424
|
+
const untracked = (await git(["ls-files", "-z", "--others", "--exclude-standard"], root)).split("\0");
|
|
425
|
+
for (const path of [...changed, ...untracked].filter((entry) => entry !== "")) {
|
|
426
|
+
copyInto(root, worktree, path);
|
|
427
|
+
}
|
|
428
|
+
copyInto(root, worktree, spec);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function copyEnvFiles(root: string, worktree: string, suiteDir: string): void {
|
|
432
|
+
const from = join(root, suiteDir);
|
|
433
|
+
const to = join(worktree, suiteDir);
|
|
434
|
+
for (const name of readdirSync(from)) {
|
|
435
|
+
if (ENV_FILE.test(name) && name !== ENV_EXAMPLE && statSync(join(from, name)).isFile()) {
|
|
436
|
+
copyFileSync(join(from, name), join(to, name));
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function linkDependencies(root: string, worktree: string): void {
|
|
442
|
+
const modules = join(root, "node_modules");
|
|
443
|
+
if (existsSync(modules)) {
|
|
444
|
+
symlinkSync(modules, join(worktree, "node_modules"), "junction");
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function isLinkAt(path: string): boolean {
|
|
449
|
+
try {
|
|
450
|
+
return lstatSync(path).isSymbolicLink();
|
|
451
|
+
} catch {
|
|
452
|
+
return false;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function unlinkDependencies(worktree: string): boolean {
|
|
457
|
+
const link = join(worktree, "node_modules");
|
|
458
|
+
if (isLinkAt(link)) {
|
|
459
|
+
quietly(() => {
|
|
460
|
+
unlinkSync(link);
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
return !isLinkAt(link);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Removes a throw-away worktree: the link to `node_modules` first (the target stays), then the worktree itself.
|
|
468
|
+
* Every step is separate and none throws, so a cleanup problem never hides the result or error of the run. When the
|
|
469
|
+
* link cannot be removed nothing else is deleted.
|
|
470
|
+
*/
|
|
471
|
+
export async function removeWorktree(root: string, worktree: string): Promise<void> {
|
|
472
|
+
if (!unlinkDependencies(worktree)) {
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
const removed = await run("git", ["worktree", "remove", "--force", worktree], root);
|
|
476
|
+
if (removed.failed) {
|
|
477
|
+
quietly(() => {
|
|
478
|
+
rmSync(worktree, { recursive: true, force: true });
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
await run("git", ["worktree", "prune"], root);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function escapeRegExp(text: string): string {
|
|
485
|
+
return text.replace(REGEX_SPECIALS, "\\$&");
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function stripAnsi(text: string): string {
|
|
489
|
+
return text.replace(ANSI, "");
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function textOf(value: unknown): string {
|
|
493
|
+
return typeof value === "string" ? value : "";
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function walkSpecs(suite: unknown, visit: (spec: Record<string, unknown>) => void): void {
|
|
497
|
+
if (!isRecord(suite)) {
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
for (const spec of Array.isArray(suite["specs"]) ? suite["specs"] : []) {
|
|
501
|
+
if (isRecord(spec)) {
|
|
502
|
+
visit(spec);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
for (const child of Array.isArray(suite["suites"]) ? suite["suites"] : []) {
|
|
506
|
+
walkSpecs(child, visit);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
function entriesOf(
|
|
511
|
+
report: unknown,
|
|
512
|
+
visit: (spec: Record<string, unknown>, entry: Record<string, unknown>) => void,
|
|
513
|
+
): void {
|
|
514
|
+
if (!isRecord(report)) {
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
for (const suite of Array.isArray(report["suites"]) ? report["suites"] : []) {
|
|
518
|
+
walkSpecs(suite, (spec) => {
|
|
519
|
+
for (const entry of Array.isArray(spec["tests"]) ? spec["tests"] : []) {
|
|
520
|
+
if (isRecord(entry)) {
|
|
521
|
+
visit(spec, entry);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
function errorMessages(report: unknown): string[] {
|
|
529
|
+
const messages: string[] = [];
|
|
530
|
+
if (isRecord(report) && Array.isArray(report["errors"])) {
|
|
531
|
+
for (const error of report["errors"]) {
|
|
532
|
+
messages.push(stripAnsi(isRecord(error) ? textOf(error["message"]) : ""));
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
return messages.filter((message) => message.trim() !== "");
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* The environment of a nested Playwright run: the caller's, without what a Playwright worker sets for its own
|
|
540
|
+
* children (`PW_*`, worker indexes, a `NODE_OPTIONS` that names Playwright), plus the report file.
|
|
541
|
+
*/
|
|
542
|
+
export function childEnv(base: NodeJS.ProcessEnv, reportFile: string): NodeJS.ProcessEnv {
|
|
543
|
+
const kept = Object.fromEntries(
|
|
544
|
+
Object.entries(base).filter(
|
|
545
|
+
([key, value]) => !INHERITED_PLAYWRIGHT.test(key) && !(key === "NODE_OPTIONS" && /playwright/i.test(value ?? "")),
|
|
546
|
+
),
|
|
547
|
+
);
|
|
548
|
+
return { ...kept, PLAYWRIGHT_JSON_OUTPUT_FILE: reportFile };
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
async function playwright(
|
|
552
|
+
worktree: string,
|
|
553
|
+
config: readonly string[],
|
|
554
|
+
args: readonly string[],
|
|
555
|
+
reportFile: string,
|
|
556
|
+
): Promise<{ report: unknown; stderr: string }> {
|
|
557
|
+
rmSync(reportFile, { force: true });
|
|
558
|
+
const result = await run(
|
|
559
|
+
process.execPath,
|
|
560
|
+
[join(worktree, ...PLAYWRIGHT_CLI), "test", ...config, ...args, "--reporter=json"],
|
|
561
|
+
worktree,
|
|
562
|
+
childEnv(process.env, reportFile),
|
|
563
|
+
);
|
|
564
|
+
if (!existsSync(reportFile)) {
|
|
565
|
+
throw problem(`Playwright wrote no report: ${firstLine(stripAnsi(result.stderr))}`);
|
|
566
|
+
}
|
|
567
|
+
try {
|
|
568
|
+
return { report: JSON.parse(readFileSync(reportFile, "utf8")), stderr: result.stderr };
|
|
569
|
+
} catch {
|
|
570
|
+
throw problem("Playwright's report is not valid JSON");
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
function listedTests(report: unknown, spec: string): TestRef[] {
|
|
575
|
+
const refs: TestRef[] = [];
|
|
576
|
+
entriesOf(report, (specEntry, entry) => {
|
|
577
|
+
if (normalize(textOf(specEntry["file"])) === spec && typeof specEntry["line"] === "number") {
|
|
578
|
+
refs.push({
|
|
579
|
+
project: textOf(entry["projectName"]),
|
|
580
|
+
title: textOf(specEntry["title"]),
|
|
581
|
+
line: specEntry["line"],
|
|
582
|
+
expectedStatus: textOf(entry["expectedStatus"]),
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
return refs;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
function describeNone(spec: string, title: string, project: string | undefined): Error {
|
|
590
|
+
const where = project === undefined ? "" : ` in project ${project}`;
|
|
591
|
+
return problem(`no test titled "${title}" in ${spec}${where}`);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
function resolveTest(
|
|
595
|
+
report: unknown,
|
|
596
|
+
spec: string,
|
|
597
|
+
suiteSpec: string,
|
|
598
|
+
title: string,
|
|
599
|
+
project: string | undefined,
|
|
600
|
+
): TestRef {
|
|
601
|
+
const refs = listedTests(report, suiteSpec).filter((ref) => project === undefined || ref.project === project);
|
|
602
|
+
const exact = refs.filter((ref) => ref.title === title);
|
|
603
|
+
const matches = exact.length > 0 ? exact : refs.filter((ref) => ref.title.includes(title));
|
|
604
|
+
const [first] = matches;
|
|
605
|
+
if (first === undefined) {
|
|
606
|
+
throw describeNone(spec, title, project);
|
|
607
|
+
}
|
|
608
|
+
const titles = [...new Set(matches.map((ref) => ref.title))];
|
|
609
|
+
if (titles.length > 1) {
|
|
610
|
+
throw problem(`"${title}" matches more than one test: ${titles.map((found) => `"${found}"`).join(", ")}`);
|
|
611
|
+
}
|
|
612
|
+
const projects = [...new Set(matches.map((ref) => ref.project))];
|
|
613
|
+
if (projects.length > 1) {
|
|
614
|
+
throw problem(`"${first.title}" is in projects ${projects.join(", ")}; pass the project`);
|
|
615
|
+
}
|
|
616
|
+
if (new Set(matches.map((ref) => ref.line)).size > 1) {
|
|
617
|
+
throw problem(`"${first.title}" appears more than once in ${spec}`);
|
|
618
|
+
}
|
|
619
|
+
return first;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
function refuseExpectedFailure(title: string, expectedStatus: string): void {
|
|
623
|
+
if (expectedStatus !== "" && expectedStatus !== "passed") {
|
|
624
|
+
const expected = expectedStatus === "failed" ? "fail" : expectedStatus;
|
|
625
|
+
throw problem(`the test "${title}" is expected to ${expected}, so a failure after the change proves nothing`);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
function outcomeOf(report: unknown, target: TestRef): Outcome {
|
|
630
|
+
const found: Outcome[] = [];
|
|
631
|
+
entriesOf(report, (specEntry, entry) => {
|
|
632
|
+
if (specEntry["title"] !== target.title || entry["projectName"] !== target.project) {
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
const messages: string[] = [];
|
|
636
|
+
for (const result of Array.isArray(entry["results"]) ? entry["results"] : []) {
|
|
637
|
+
for (const error of isRecord(result) && Array.isArray(result["errors"]) ? result["errors"] : []) {
|
|
638
|
+
messages.push(stripAnsi(isRecord(error) ? textOf(error["message"]) : ""));
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
found.push({
|
|
642
|
+
status: textOf(entry["status"]),
|
|
643
|
+
expectedStatus: textOf(entry["expectedStatus"]),
|
|
644
|
+
messages: messages.filter((message) => message.trim() !== ""),
|
|
645
|
+
});
|
|
646
|
+
});
|
|
647
|
+
const [only] = found;
|
|
648
|
+
if (only === undefined || found.length > 1) {
|
|
649
|
+
const why = found.length > 1 ? "ran more than once" : "did not run";
|
|
650
|
+
throw problem(`the test "${target.title}" ${why}: ${firstLine(errorMessages(report).join(" "))}`);
|
|
651
|
+
}
|
|
652
|
+
return only;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
async function runOnce(
|
|
656
|
+
worktree: string,
|
|
657
|
+
config: readonly string[],
|
|
658
|
+
spec: string,
|
|
659
|
+
target: TestRef,
|
|
660
|
+
reportFile: string,
|
|
661
|
+
): Promise<{ failed: boolean; output: string }> {
|
|
662
|
+
const args = [
|
|
663
|
+
"--no-deps",
|
|
664
|
+
"--retries=0",
|
|
665
|
+
`--project=${target.project}`,
|
|
666
|
+
`--grep=${escapeRegExp(target.title)}$`,
|
|
667
|
+
`${spec}:${target.line}`,
|
|
668
|
+
];
|
|
669
|
+
const { report } = await playwright(worktree, config, args, reportFile);
|
|
670
|
+
const outcome = outcomeOf(report, target);
|
|
671
|
+
refuseExpectedFailure(target.title, outcome.expectedStatus);
|
|
672
|
+
if (outcome.status === "expected") {
|
|
673
|
+
return { failed: false, output: "the test passed" };
|
|
674
|
+
}
|
|
675
|
+
if (outcome.status === "unexpected") {
|
|
676
|
+
return { failed: true, output: outcome.messages.join("\n---\n").slice(0, MAX_OUTPUT) };
|
|
677
|
+
}
|
|
678
|
+
const detail = firstLine(errorMessages(report).join(" "));
|
|
679
|
+
throw problem(`the test "${target.title}" is ${outcome.status}, not a clean pass or failure: ${detail}`);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Proves that one test fails when a value it checks is changed. The script makes the change itself
|
|
684
|
+
* ({@link mutateAssertion}): nothing the caller writes reaches the copy. It works on a throw-away git worktree in the
|
|
685
|
+
* system temporary directory, outside the repository (the working tree is never touched): the worktree gets the
|
|
686
|
+
* repository's `HEAD`, the working tree's uncommitted changes and the spec as it is on disk, a link to `node_modules`
|
|
687
|
+
* and the `.env*` files of the suite directory (`suiteDir` of the project facts). Playwright runs with the suite's
|
|
688
|
+
* config (`playwrightConfigArgs`). The test is run once as it is, with no retries, and must pass; then the literal on
|
|
689
|
+
* `line` is changed and the same test run again.
|
|
690
|
+
*
|
|
691
|
+
* `failed` is true only when the test passed before the change and failed after it. A flaky or skipped result, and a
|
|
692
|
+
* test that is expected to fail, are errors, not proofs. The worktree is removed on every path, and the removal never
|
|
693
|
+
* hides the result or the error.
|
|
694
|
+
*
|
|
695
|
+
* @param spec repository-relative path of the spec file, inside the suite directory.
|
|
696
|
+
* @param title the test's own title, or a part of it that matches exactly one test in the file.
|
|
697
|
+
* @param line the line of the assertion (`expect(...).toXxx(...)`) inside that test whose literal is changed.
|
|
698
|
+
* @param project the Playwright project to run; resolved from the spec when omitted and the test is in one project.
|
|
699
|
+
* @throws Error when an input is wrong, the line holds no changeable literal, the test fails before the change, is
|
|
700
|
+
* flaky, expected to fail, or does not run.
|
|
701
|
+
*/
|
|
702
|
+
export async function proveFails(
|
|
703
|
+
spec: string,
|
|
704
|
+
title: string,
|
|
705
|
+
line: number,
|
|
706
|
+
project?: string,
|
|
707
|
+
): Promise<ProveFailsResult> {
|
|
708
|
+
checkedLine(line);
|
|
709
|
+
const root = resolve((await git(["rev-parse", "--show-toplevel"], process.cwd())).trim());
|
|
710
|
+
const { suiteDir } = readProjectFactsIn(root);
|
|
711
|
+
const config = playwrightConfigArgs(suiteDir);
|
|
712
|
+
const specPath = await checkedSpec(spec, root);
|
|
713
|
+
const suiteSpec = suitePathOf(specPath, suiteDir);
|
|
714
|
+
const source = readFileSync(join(root, specPath), "utf8");
|
|
715
|
+
mutateAssertion(source, line);
|
|
716
|
+
const worktree = mkdtempSync(join(tmpdir(), TEMP_PREFIX));
|
|
717
|
+
try {
|
|
718
|
+
await git(["worktree", "add", "--detach", worktree, "HEAD"], root);
|
|
719
|
+
await overlayWorkingTree(root, worktree, specPath);
|
|
720
|
+
copyEnvFiles(root, worktree, suiteDir);
|
|
721
|
+
linkDependencies(root, worktree);
|
|
722
|
+
const reportFile = join(worktree, ".prove-fails-report.json");
|
|
723
|
+
const listed = await playwright(
|
|
724
|
+
worktree,
|
|
725
|
+
config,
|
|
726
|
+
["--list", "--no-deps", ...(project === undefined ? [] : [`--project=${project}`]), specPath],
|
|
727
|
+
reportFile,
|
|
728
|
+
);
|
|
729
|
+
const target = resolveTest(listed.report, specPath, suiteSpec, title, project);
|
|
730
|
+
refuseExpectedFailure(target.title, target.expectedStatus);
|
|
731
|
+
const change = mutateAssertion(source, line, target.line);
|
|
732
|
+
const before = await runOnce(worktree, config, specPath, target, reportFile);
|
|
733
|
+
if (before.failed) {
|
|
734
|
+
throw problem(`the test "${target.title}" fails before the change: ${firstLine(before.output)}`);
|
|
735
|
+
}
|
|
736
|
+
writeFileSync(join(worktree, specPath), change.source);
|
|
737
|
+
const after = await runOnce(worktree, config, specPath, target, reportFile);
|
|
738
|
+
return { ...after, mutated: change.mutated };
|
|
739
|
+
} finally {
|
|
740
|
+
await removeWorktree(root, worktree);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
function usage(): Error {
|
|
745
|
+
return problem("usage is node .claude/checks/prove-fails.mts <spec> <line> <title words> [--project=<name>]");
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
function parseArguments(args: readonly string[]): { spec: string; line: number; title: string; project?: string } {
|
|
749
|
+
const project = args.map((arg) => PROJECT_FLAG.exec(arg)?.[1]).find((name) => name !== undefined);
|
|
750
|
+
const [spec, lineText, ...words] = args.filter((arg) => !PROJECT_FLAG.test(arg));
|
|
751
|
+
if (spec === undefined || lineText === undefined || !/^\d+$/.test(lineText) || words.length === 0) {
|
|
752
|
+
throw usage();
|
|
753
|
+
}
|
|
754
|
+
return { spec, line: Number(lineText), title: words.join(" "), ...(project === undefined ? {} : { project }) };
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
async function main(args: readonly string[]): Promise<void> {
|
|
758
|
+
const { spec, line, title, project } = parseArguments(args);
|
|
759
|
+
const result = await proveFails(spec, title, line, project);
|
|
760
|
+
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
function reportFailure(error: unknown): void {
|
|
764
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
765
|
+
process.exitCode = 1;
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
if (basename(process.argv[1] ?? "") === CLI_FILE_NAME) {
|
|
769
|
+
main(process.argv.slice(2)).catch(reportFailure);
|
|
770
|
+
}
|