@virtocommerce/test-automation-framework 0.0.0-stage → 0.2.0-alpha.20261126085150
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/vc-qa-automation-expert.md +122 -0
- package/.claude/agents/vc-qa-coverage.md +99 -0
- package/.claude/agents/vc-qa-explorer.md +117 -0
- package/.claude/agents/vc-qa-healer.md +90 -0
- package/.claude/agents/vc-qa-judge.md +102 -0
- package/.claude/agents/vc-qa-planner.md +117 -0
- package/.claude/agents/vc-qa-reviewer.md +128 -0
- package/.claude/checks/browser-secrets.mts +150 -0
- package/.claude/checks/contract.mts +694 -0
- package/.claude/checks/expectation-guard.mts +665 -0
- package/.claude/checks/explore-extractors.mts +4 -0
- package/.claude/checks/facts-consistency.mts +335 -0
- package/.claude/checks/introspect.mts +157 -0
- package/.claude/checks/judge-verdict.mts +206 -0
- package/.claude/checks/probe.mts +91 -0
- package/.claude/checks/project-facts.mts +570 -0
- package/.claude/checks/prove-fails.mts +965 -0
- package/.claude/checks/prune.mts +584 -0
- package/.claude/checks/rank-gaps.mts +196 -0
- package/.claude/checks/readonly-shell.mts +819 -0
- package/.claude/checks/reconcile-run.mts +327 -0
- package/.claude/checks/run-record.mts +338 -0
- package/.claude/checks/scan-prompts.mts +351 -0
- package/.claude/checks/stand-request.mts +76 -0
- package/.claude/checks/stand.mts +45 -0
- package/.claude/checks/test-duration.mts +134 -0
- package/.claude/checks/test-shape.mts +288 -0
- package/.claude/checks/ticket-card.mts +195 -0
- package/.claude/commands/vc-qa-coverage.md +20 -0
- package/.claude/commands/vc-qa-explore.md +17 -0
- package/.claude/commands/vc-qa-heal.md +44 -0
- package/.claude/commands/vc-qa-implement.md +60 -0
- package/.claude/commands/vc-qa-plan.md +64 -0
- package/.claude/commands/vc-qa-review.md +90 -0
- package/.claude/commands/vc-qa.md +165 -0
- package/.claude/hooks/readonly-shell.mts +65 -0
- package/.claude/settings.json +15 -0
- package/.claude/skills/vc-coverage-analysis/SKILL.md +116 -0
- package/.claude/skills/vc-create-graphql-layer/SKILL.md +161 -0
- package/.claude/skills/vc-create-ui-layer/SKILL.md +191 -0
- package/.claude/skills/vc-explore-api/SKILL.md +93 -0
- package/.claude/skills/vc-explore-frontend/SKILL.md +91 -0
- package/.claude/skills/vc-framework-rules/SKILL.md +212 -0
- package/.claude/skills/vc-heal-tests/SKILL.md +197 -0
- package/.claude/skills/vc-heal-tests/guard-recipe.md +53 -0
- package/.claude/skills/vc-judge-criteria/SKILL.md +42 -0
- package/.claude/skills/vc-judge-criteria/coverage.md +9 -0
- package/.claude/skills/vc-judge-criteria/expert.md +28 -0
- package/.claude/skills/vc-judge-criteria/explorer.md +9 -0
- package/.claude/skills/vc-judge-criteria/healer.md +13 -0
- package/.claude/skills/vc-judge-criteria/planner.md +12 -0
- package/.claude/skills/vc-plan-implementation/SKILL.md +94 -0
- package/.claude/skills/vc-plan-implementation/contract-template.md +86 -0
- package/.claude/skills/vc-plan-test-strategy/SKILL.md +110 -0
- package/.claude/skills/vc-project-facts/SKILL.md +165 -0
- package/.claude/skills/vc-read-change/SKILL.md +64 -0
- package/.claude/skills/vc-read-ticket/SKILL.md +63 -0
- package/.claude/skills/vc-read-ticket/providers.md +58 -0
- package/.claude/skills/vc-review-pr/SKILL.md +152 -0
- package/.claude/skills/vc-review-test-code/SKILL.md +222 -0
- package/.claude/skills/vc-write-e2e-test/SKILL.md +179 -0
- package/.claude/skills/vc-write-graphql-test/SKILL.md +162 -0
- package/.claude/skills/vc-write-rest-api-test/SKILL.md +187 -0
- package/.claude/skills/vc-write-security-test/SKILL.md +257 -0
- package/.qa/project.example.json +71 -0
- package/CHANGELOG.md +118 -0
- package/LICENSE +12 -0
- package/README.md +70 -2
- package/bin/vc-qa-agents.mjs +595 -0
- package/dist/auth/browser-sessions.d.ts +88 -0
- package/dist/auth/browser-sessions.js +126 -0
- package/dist/auth/browser-sessions.js.map +1 -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/index.d.ts +9 -0
- package/dist/auth/index.js +5 -0
- package/dist/auth/index.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/sign-in-strategies.d.ts +67 -0
- package/dist/auth/sign-in-strategies.js +121 -0
- package/dist/auth/sign-in-strategies.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 +90 -0
- package/dist/auth/token-client.js +116 -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 +96 -0
- package/dist/fixtures/default-fixtures.js +109 -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 +144 -0
- package/docs/concepts/config.md +103 -0
- package/docs/concepts/dataset.md +184 -0
- package/docs/concepts/environment.md +59 -0
- package/docs/concepts/fixtures.md +116 -0
- package/docs/concepts/graphql.md +76 -0
- package/docs/concepts/http.md +88 -0
- package/docs/concepts/modules.md +64 -0
- package/docs/concepts/secrets.md +50 -0
- package/docs/contributing/architecture.md +74 -0
- package/docs/contributing/conventions.md +39 -0
- package/docs/contributing/documentation.md +26 -0
- package/docs/contributing/releasing.md +90 -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/sign-in-strategies.ts +22 -0
- package/docs/examples/tsconfig.json +10 -0
- package/docs/getting-started.md +167 -0
- package/docs/guides/generating-types.md +76 -0
- package/docs/guides/qa-agents.md +224 -0
- package/package.json +156 -4
- package/templates/suite/.mcp.json +16 -0
- package/templates/suite/.qa/project.json +70 -0
- package/templates/suite/CLAUDE.template.md +62 -0
- package/templates/suite/allurerc.mjs +13 -0
- package/templates/suite/arrange/context.ts +15 -0
- package/templates/suite/arrange/index.ts +69 -0
- package/templates/suite/arrange/security.ts +78 -0
- package/templates/suite/biome.template.json +12 -0
- package/templates/suite/builders/security.ts +46 -0
- package/templates/suite/codegen.ts +27 -0
- package/templates/suite/env.example +10 -0
- package/templates/suite/env.ts +25 -0
- package/templates/suite/fixtures/index.ts +96 -0
- package/templates/suite/gitignore +13 -0
- package/templates/suite/pages/README.md +10 -0
- package/templates/suite/playwright.config.ts +48 -0
- package/templates/suite/scripts/generate-rest-types.ts +16 -0
- package/templates/suite/support/require-fields.ts +53 -0
- package/templates/suite/support/shopper.ts +51 -0
- package/templates/suite/support/store.ts +33 -0
- package/templates/suite/support/unique-id.ts +11 -0
- package/templates/suite/support/users.ts +6 -0
- package/templates/suite/tests/restapi/security/sign-in.spec.ts +23 -0
- package/templates/suite/tsconfig.json +33 -0
|
@@ -0,0 +1,694 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { readFileSync, statSync } from "node:fs";
|
|
3
|
+
import { basename, join } from "node:path";
|
|
4
|
+
|
|
5
|
+
import { PLAYWRIGHT_CLI, playwrightConfigArgs, readProjectFacts, suitePackageDir } from "./project-facts.mts";
|
|
6
|
+
import { CREDENTIAL_SHAPE } from "./readonly-shell.mts";
|
|
7
|
+
import { walkRoot } from "./reconcile-run.mts";
|
|
8
|
+
import { contractSeries, sameSpec } from "./run-record.mts";
|
|
9
|
+
|
|
10
|
+
const CLI_FILE_NAME = "contract.mts";
|
|
11
|
+
const APPROVED_FLAG = "--approved";
|
|
12
|
+
const IMPLEMENTED_FLAG = "--implemented";
|
|
13
|
+
const FLAGS = [APPROVED_FLAG, IMPLEMENTED_FLAG];
|
|
14
|
+
const SOURCE_KINDS = ["ticket", "doc", "area", "gap"] as const;
|
|
15
|
+
const ORACLE_SOURCES = ["ticket", "doc", "schema", "observed"] as const;
|
|
16
|
+
const REQUIRED_SECTIONS = [
|
|
17
|
+
"Source",
|
|
18
|
+
"Scope",
|
|
19
|
+
"Out of scope",
|
|
20
|
+
"Existing coverage",
|
|
21
|
+
"Cases",
|
|
22
|
+
"Stand needs",
|
|
23
|
+
"Building blocks",
|
|
24
|
+
"Platform defects",
|
|
25
|
+
"Verification",
|
|
26
|
+
] as const;
|
|
27
|
+
const CASE_COLUMNS = ["Case", "Technique", "Project", "Data role", "Critical flow", "Parameters", "Oracle"] as const;
|
|
28
|
+
const AMENDMENTS_HEADING = /^amendment/i;
|
|
29
|
+
const NO_ROLE = "none";
|
|
30
|
+
const NOT_CRITICAL = "no";
|
|
31
|
+
const COMMENT = /<!--[\s\S]*?-->/g;
|
|
32
|
+
const LOCAL_STATE = /\.qa\/(?:reports|kb)\/[^\s`'"()<>,;]*/g;
|
|
33
|
+
const PATH_END_PUNCTUATION = /[.:!?\]]+$/;
|
|
34
|
+
const FILE_NAME = /[^/]\.[A-Za-z0-9]+$/;
|
|
35
|
+
const GLOB = /[*?]/;
|
|
36
|
+
const STATED_DATE = /\((\d{4}-\d{2}-\d{2})\)/;
|
|
37
|
+
const AMENDMENT_FIELDS = ["Cases", "Changes"] as const;
|
|
38
|
+
const DROPS_LINE = /^Drops:\s*(.+)$/im;
|
|
39
|
+
const TITLE_SEPARATOR = " › ";
|
|
40
|
+
const LIST_BUFFER = 256 * 1024 * 1024;
|
|
41
|
+
const MAX_REASON = 300;
|
|
42
|
+
const APPROVAL_LINE = /^Approved:\s*([^,\s][^,]*),\s*\d{4}-\d{2}-\d{2}(?:\s+\(relayed: "[^\n]+"\))?\s*$/;
|
|
43
|
+
const APPROVAL = new RegExp(APPROVAL_LINE.source, "m");
|
|
44
|
+
const APPROVAL_SECTION = "Approval";
|
|
45
|
+
const CONFIRMATION = /^(characterization|confirmed by \S.*)$/;
|
|
46
|
+
const OBSERVED = "observed:";
|
|
47
|
+
const PENDING = /^pending\b/;
|
|
48
|
+
const AT_FIRST_RUN = /^at first run\s+\S/;
|
|
49
|
+
const FIRST_RUN_LINE = /\(first run\)$/;
|
|
50
|
+
const REGEX_SPECIALS = /[.*+?^${}()|[\]\\]/g;
|
|
51
|
+
const RECORDED = /^(.*?)\s*\(([^()]*)\)$/;
|
|
52
|
+
const ESCAPED_PIPE = /\\\|/g;
|
|
53
|
+
const REPORTS_DIR = ".qa/";
|
|
54
|
+
const REPORT_PATH = /^\.qa\/reports\/explore-[a-z0-9]+(-[a-z0-9]+)*\.md$/;
|
|
55
|
+
const OBSERVED_VALUES = "Observed values";
|
|
56
|
+
|
|
57
|
+
type CaseRow = { name: string; dataRole: string; oracle: string };
|
|
58
|
+
|
|
59
|
+
function problem(text: string): string {
|
|
60
|
+
return `contract: ${text}`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
type Section = { heading: string; lines: string[] };
|
|
64
|
+
|
|
65
|
+
function parseSections(markdown: string): Section[] {
|
|
66
|
+
const sections: Section[] = [];
|
|
67
|
+
for (const line of markdown.split(/\r?\n/)) {
|
|
68
|
+
const heading = /^## (\S.*?)\s*$/.exec(line);
|
|
69
|
+
if (heading?.[1] !== undefined) {
|
|
70
|
+
sections.push({ heading: heading[1], lines: [] });
|
|
71
|
+
} else {
|
|
72
|
+
sections.at(-1)?.lines.push(line);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return sections;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function hasText(lines: string[]): boolean {
|
|
79
|
+
return lines.some((line) => line.trim() !== "");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The files a line names under `.qa/reports/` or `.qa/kb/`. A path ends at whitespace, a backtick, a quote or a
|
|
84
|
+
* bracket, and the sentence's closing punctuation is not part of it. A directory (`.qa/reports/`), a name with no
|
|
85
|
+
* extension and a pattern (`coverage-*.md`) name no file, as in "none exists under `.qa/reports/`".
|
|
86
|
+
*/
|
|
87
|
+
function localStateFiles(line: string): string[] {
|
|
88
|
+
return [...line.matchAll(LOCAL_STATE)]
|
|
89
|
+
.map((match) => match[0].replace(PATH_END_PUNCTUATION, ""))
|
|
90
|
+
.filter((path) => FILE_NAME.test(path) && !GLOB.test(path));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* A line of `## Source` that names a report or a knowledge snapshot (`.qa/reports/…`, `.qa/kb/…`) must carry the date
|
|
95
|
+
* the planner read it, `(<yyyy-mm-dd>)`, and the file must not have changed after that date (`modifiedOn`, when given).
|
|
96
|
+
*/
|
|
97
|
+
function checkLocalState(lines: string[], modifiedOn: ((path: string) => string | undefined) | undefined): string[] {
|
|
98
|
+
const problems: string[] = [];
|
|
99
|
+
for (const line of lines) {
|
|
100
|
+
const date = STATED_DATE.exec(line)?.[1];
|
|
101
|
+
for (const path of localStateFiles(line)) {
|
|
102
|
+
const changed = date === undefined ? undefined : modifiedOn?.(path);
|
|
103
|
+
if (date === undefined) {
|
|
104
|
+
problems.push(
|
|
105
|
+
problem(`## Source names ${path} with no date: write (<yyyy-mm-dd>) after it, the day it was read`),
|
|
106
|
+
);
|
|
107
|
+
} else if (changed !== undefined && changed > date) {
|
|
108
|
+
problems.push(
|
|
109
|
+
problem(
|
|
110
|
+
`## Source: ${path} changed on ${changed}, after the ${date} the contract states: read it again and update the date`,
|
|
111
|
+
),
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return problems;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function checkSource(lines: string[], modifiedOn: ((path: string) => string | undefined) | undefined): string[] {
|
|
120
|
+
const first = lines.find((line) => line.trim() !== "")?.trim() ?? "";
|
|
121
|
+
const accepted = SOURCE_KINDS.some((kind) => new RegExp(`^${kind}:\\s*\\S`).test(first));
|
|
122
|
+
return [
|
|
123
|
+
...(accepted ? [] : [problem("## Source must start with ticket:, doc:, area: or gap:")]),
|
|
124
|
+
...checkLocalState(lines, modifiedOn),
|
|
125
|
+
];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Splits the Oracle cell on `separator` outside double quotes, so a quoted observed value may hold `;`. */
|
|
129
|
+
function splitOutsideQuotes(text: string, separator: string): string[] {
|
|
130
|
+
const parts = [""];
|
|
131
|
+
let quoted = false;
|
|
132
|
+
for (const character of text) {
|
|
133
|
+
if (character === '"') {
|
|
134
|
+
quoted = !quoted;
|
|
135
|
+
}
|
|
136
|
+
if (character === separator && !quoted) {
|
|
137
|
+
parts.push("");
|
|
138
|
+
} else {
|
|
139
|
+
parts[parts.length - 1] += character;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return parts;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Splits a table row on every `|` that is not escaped as `\|`, the Markdown way to write a pipe in a cell. */
|
|
146
|
+
function splitRow(line: string): string[] {
|
|
147
|
+
return line
|
|
148
|
+
.trim()
|
|
149
|
+
.replace(/^\|/, "")
|
|
150
|
+
.replace(/(?<!\\)\|$/, "")
|
|
151
|
+
.split(/(?<!\\)\|/)
|
|
152
|
+
.map((cell) => cell.trim());
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function isSeparator(cells: string[]): boolean {
|
|
156
|
+
return cells.every((cell) => /^:?-{3,}:?$/.test(cell));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function oracleParts(oracle: string): string[] {
|
|
160
|
+
return splitOutsideQuotes(oracle, ";").map((part) => part.trim());
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function oracleSources(oracle: string): string[] {
|
|
164
|
+
return oracleParts(oracle)
|
|
165
|
+
.map((part) => ORACLE_SOURCES.find((source) => part.trim().startsWith(`${source}:`)))
|
|
166
|
+
.filter((source) => source !== undefined);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function isObservedOnly(oracle: string): boolean {
|
|
170
|
+
const sources = oracleSources(oracle);
|
|
171
|
+
return sources.length > 0 && sources.every((source) => source === "observed");
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** The text without `<!-- -->` comments, which hold the template's instructions and its commented-out sections. */
|
|
175
|
+
function withoutComments(markdown: string): string {
|
|
176
|
+
return markdown.replace(COMMENT, "");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function checkCases(
|
|
180
|
+
lines: string[],
|
|
181
|
+
roles: string[] | undefined,
|
|
182
|
+
critical: string[] | undefined,
|
|
183
|
+
): { problems: string[]; rows: CaseRow[] } {
|
|
184
|
+
const problems: string[] = [];
|
|
185
|
+
const rows: CaseRow[] = [];
|
|
186
|
+
const tableLines = lines.filter((line) => line.trim().startsWith("|"));
|
|
187
|
+
const header = splitRow(tableLines[0] ?? "");
|
|
188
|
+
const index = new Map(CASE_COLUMNS.map((column) => [column, header.indexOf(column)]));
|
|
189
|
+
for (const column of CASE_COLUMNS) {
|
|
190
|
+
if (index.get(column) === -1) {
|
|
191
|
+
problems.push(problem(`## Cases table has no ${column} column`));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
const body = tableLines
|
|
195
|
+
.slice(1)
|
|
196
|
+
.map(splitRow)
|
|
197
|
+
.filter((cells) => !isSeparator(cells));
|
|
198
|
+
if (body.length === 0) {
|
|
199
|
+
problems.push(problem("## Cases table has no case rows"));
|
|
200
|
+
}
|
|
201
|
+
const cell = (cells: string[], column: (typeof CASE_COLUMNS)[number]): string => cells[index.get(column) ?? -1] ?? "";
|
|
202
|
+
for (const [position, cells] of body.entries()) {
|
|
203
|
+
if (cells.length !== header.length) {
|
|
204
|
+
problems.push(problem(`## Cases row ${position + 1} has ${cells.length} cells, the header has ${header.length}`));
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
const name = cell(cells, "Case");
|
|
208
|
+
const label = name === "" ? `row ${position + 1}` : `case "${name}"`;
|
|
209
|
+
for (const column of CASE_COLUMNS) {
|
|
210
|
+
if (index.get(column) !== -1 && cell(cells, column) === "") {
|
|
211
|
+
problems.push(problem(`${label} has an empty ${column} cell`));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const oracle = cell(cells, "Oracle");
|
|
215
|
+
if (
|
|
216
|
+
index.get("Oracle") !== -1 &&
|
|
217
|
+
oracle !== "" &&
|
|
218
|
+
!ORACLE_SOURCES.some((source) => oracle.startsWith(`${source}:`))
|
|
219
|
+
) {
|
|
220
|
+
problems.push(problem(`${label} oracle must start with ticket:, doc:, schema: or observed:`));
|
|
221
|
+
}
|
|
222
|
+
if ((oracle.match(/"/g) ?? []).length % 2 === 1) {
|
|
223
|
+
problems.push(problem(`${label} oracle has an unbalanced double quote, which hides the parts after it`));
|
|
224
|
+
}
|
|
225
|
+
const dataRole = cell(cells, "Data role");
|
|
226
|
+
for (const role of dataRole.split(",").map((item) => item.trim())) {
|
|
227
|
+
if (roles !== undefined && role !== "" && role !== NO_ROLE && !roles.includes(role)) {
|
|
228
|
+
problems.push(problem(`${label} data role "${role}" is not in the facts roles`));
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
const flow = cell(cells, "Critical flow");
|
|
232
|
+
for (const item of flow.split(",").map((value) => value.trim())) {
|
|
233
|
+
if (critical !== undefined && item !== "" && item !== NOT_CRITICAL && !critical.includes(item)) {
|
|
234
|
+
problems.push(
|
|
235
|
+
problem(`${label} critical flow "${item}" is not in the facts critical list (or ${NOT_CRITICAL})`),
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
rows.push({ name, dataRole, oracle });
|
|
240
|
+
}
|
|
241
|
+
return { problems, rows };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** The value as the explorer writes it in the report: raw, never quoted, with `\|` unescaped. */
|
|
245
|
+
function rawValue(value: string): string {
|
|
246
|
+
const unquoted = value.length >= 2 && value.startsWith('"') && value.endsWith('"') ? value.slice(1, -1) : value;
|
|
247
|
+
return unquoted.replace(ESCAPED_PIPE, "|");
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function recordedLine(report: string, caseId: string, value: string): string | undefined {
|
|
251
|
+
const recorded = parseSections(report).find((section) => section.heading === OBSERVED_VALUES)?.lines ?? [];
|
|
252
|
+
return recorded.map((line) => line.trim()).find((line) => line.startsWith(`- ${caseId}: ${value} (`));
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** True when `text` (a test title) names `caseId` as a whole word: `C-1` is not named by `C-10`. */
|
|
256
|
+
export function namesCase(text: string, caseId: string): boolean {
|
|
257
|
+
return new RegExp(`(^|[^\\w-])${caseId.replace(REGEX_SPECIALS, "\\$&")}([^\\w-]|$)`).test(text);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
type OraclePart = { text: string; splitOnSemicolon: boolean };
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* What an observed part is checked against: the report reader, whether the contract has its approval line, the text
|
|
264
|
+
* of its approved amendment entries, and whether the implementation must be finished (`--implemented`).
|
|
265
|
+
*/
|
|
266
|
+
type ObservedContext = {
|
|
267
|
+
readReport: ((path: string) => string | undefined) | undefined;
|
|
268
|
+
approved: boolean;
|
|
269
|
+
approvedAmendments: string;
|
|
270
|
+
implemented: boolean;
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
/** The Oracle's parts; a part that starts with no source prefix is joined back to the part before it. */
|
|
274
|
+
function sourceParts(oracle: string): OraclePart[] {
|
|
275
|
+
const parts: OraclePart[] = [];
|
|
276
|
+
for (const text of oracleParts(oracle)) {
|
|
277
|
+
const last = parts.at(-1);
|
|
278
|
+
if (last !== undefined && !ORACLE_SOURCES.some((source) => text.startsWith(`${source}:`))) {
|
|
279
|
+
last.text = `${last.text}; ${text}`;
|
|
280
|
+
last.splitOnSemicolon = true;
|
|
281
|
+
} else {
|
|
282
|
+
parts.push({ text, splitOnSemicolon: false });
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return parts;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function checkObservedPart(
|
|
289
|
+
label: string,
|
|
290
|
+
caseId: string,
|
|
291
|
+
part: OraclePart,
|
|
292
|
+
context: ObservedContext,
|
|
293
|
+
): string | undefined {
|
|
294
|
+
const { readReport, approved, approvedAmendments, implemented } = context;
|
|
295
|
+
const rest = part.text.slice(OBSERVED.length).trim();
|
|
296
|
+
if (part.splitOnSemicolon) {
|
|
297
|
+
return `${label} observed: the value holds an unquoted ;: write the value in double quotes`;
|
|
298
|
+
}
|
|
299
|
+
if (AT_FIRST_RUN.test(rest)) {
|
|
300
|
+
if (implemented) {
|
|
301
|
+
return `${label} observed: at first run is unresolved: the expert has not recorded and pinned its value`;
|
|
302
|
+
}
|
|
303
|
+
return approved ? undefined : `${label} observed: at first run awaits the person's approval (## Approval)`;
|
|
304
|
+
}
|
|
305
|
+
const match = RECORDED.exec(rest);
|
|
306
|
+
const value = match?.[1]?.trim();
|
|
307
|
+
const path = match?.[2]?.trim();
|
|
308
|
+
if (value === undefined || path === undefined || !(path.startsWith(REPORTS_DIR) || path.endsWith(".md"))) {
|
|
309
|
+
return PENDING.test(rest)
|
|
310
|
+
? `${label} observed: oracle has no recorded value: observe it first, then write observed: <value> (.qa/reports/explore-<slug>-observed.md)`
|
|
311
|
+
: `${label} observed: this contract predates observe-before-approval: re-plan its observed cases (observed: pending <what to observe>)`;
|
|
312
|
+
}
|
|
313
|
+
if (!REPORT_PATH.test(path)) {
|
|
314
|
+
return `${label} observed: report path must be .qa/reports/explore-<slug>[-observed].md, not "${path}"`;
|
|
315
|
+
}
|
|
316
|
+
if (value === "") {
|
|
317
|
+
return `${label} observed: oracle names a report but no value before it`;
|
|
318
|
+
}
|
|
319
|
+
if (CREDENTIAL_SHAPE.test(value)) {
|
|
320
|
+
return `${label} observed: the value looks like a credential (a token, a password field or a Bearer header); never record one`;
|
|
321
|
+
}
|
|
322
|
+
if (readReport === undefined) {
|
|
323
|
+
return undefined;
|
|
324
|
+
}
|
|
325
|
+
const report = readReport(path);
|
|
326
|
+
if (report === undefined) {
|
|
327
|
+
return `${label} observed: oracle names ${path}, which does not exist`;
|
|
328
|
+
}
|
|
329
|
+
const line = recordedLine(report, caseId, rawValue(value));
|
|
330
|
+
if (line === undefined) {
|
|
331
|
+
return `${label} observed: ${path} has no line "- ${caseId}: ${rawValue(value)} (" under ## ${OBSERVED_VALUES}`;
|
|
332
|
+
}
|
|
333
|
+
return FIRST_RUN_LINE.test(line) && !namesCase(approvedAmendments, caseId)
|
|
334
|
+
? `${label} observed: the first-run value of ${caseId} needs an approved amendment naming it (A-n first-run values: ${caseId} = <value> (<report>))`
|
|
335
|
+
: undefined;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function checkObservedValues(rows: CaseRow[], context: ObservedContext): string[] {
|
|
339
|
+
const problems: string[] = [];
|
|
340
|
+
for (const row of rows) {
|
|
341
|
+
const label = row.name === "" ? "a case" : `case "${row.name}"`;
|
|
342
|
+
const caseId = row.name.split(/\s+/)[0] ?? "";
|
|
343
|
+
for (const part of sourceParts(row.oracle).filter((item) => item.text.startsWith(OBSERVED))) {
|
|
344
|
+
const found = checkObservedPart(label, caseId, part, context);
|
|
345
|
+
if (found !== undefined) {
|
|
346
|
+
problems.push(problem(found));
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
return problems;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function checkObservedOnly(lines: string[] | undefined, rows: CaseRow[]): string[] {
|
|
354
|
+
const observed = rows.filter((row) => row.name !== "" && isObservedOnly(row.oracle));
|
|
355
|
+
if (observed.length === 0) {
|
|
356
|
+
return [];
|
|
357
|
+
}
|
|
358
|
+
if (lines === undefined) {
|
|
359
|
+
return [problem("## Observed only is missing")];
|
|
360
|
+
}
|
|
361
|
+
const problems: string[] = [];
|
|
362
|
+
for (const row of observed) {
|
|
363
|
+
const entry = lines
|
|
364
|
+
.map((line) => line.trim())
|
|
365
|
+
.find((line) => /^[-*]\s/.test(line) && line.slice(2).startsWith(`${row.name}:`));
|
|
366
|
+
if (entry === undefined) {
|
|
367
|
+
problems.push(problem(`## Observed only does not list case "${row.name}"`));
|
|
368
|
+
} else if (!CONFIRMATION.test(entry.slice(2 + row.name.length + 1).trim())) {
|
|
369
|
+
problems.push(
|
|
370
|
+
problem(`## Observed only case "${row.name}" must be marked confirmed by <who> or characterization`),
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return problems;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function checkApproval(lines: string[]): string[] {
|
|
378
|
+
const written = lines.map((line) => line.trim()).filter((line) => line !== "");
|
|
379
|
+
return written.length === 1 && APPROVAL_LINE.test(written[0] ?? "")
|
|
380
|
+
? []
|
|
381
|
+
: [
|
|
382
|
+
problem(
|
|
383
|
+
`## ${APPROVAL_SECTION} must hold one line: Approved: <who>, <date>, optionally ending (relayed: "<the person's words>")`,
|
|
384
|
+
),
|
|
385
|
+
];
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
type AmendmentEntry = { title: string; body: string[] };
|
|
389
|
+
|
|
390
|
+
function amendmentEntries(lines: string[]): { entries: AmendmentEntry[]; outside: string[] } {
|
|
391
|
+
const entries: AmendmentEntry[] = [];
|
|
392
|
+
const outside: string[] = [];
|
|
393
|
+
for (const line of lines) {
|
|
394
|
+
const heading = /^### (\S.*?)\s*$/.exec(line);
|
|
395
|
+
if (heading?.[1] !== undefined) {
|
|
396
|
+
entries.push({ title: heading[1], body: [] });
|
|
397
|
+
} else {
|
|
398
|
+
(entries.at(-1)?.body ?? outside).push(line);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return { entries, outside };
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** The title and text of every amendment entry that carries its approval line, for the first-run values it names. */
|
|
405
|
+
function approvedAmendmentText(sections: Section[]): string {
|
|
406
|
+
return sections
|
|
407
|
+
.filter(({ heading }) => AMENDMENTS_HEADING.test(heading))
|
|
408
|
+
.flatMap(({ lines }) => amendmentEntries(lines).entries)
|
|
409
|
+
.filter((entry) => APPROVAL.test(entry.body.join("\n")))
|
|
410
|
+
.map((entry) => [entry.title, ...entry.body].join("\n"))
|
|
411
|
+
.join("\n");
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** The case ids an approved amendment drops (`Drops: C-04, C-05`): they need no test. */
|
|
415
|
+
function droppedCases(sections: Section[]): string[] {
|
|
416
|
+
return sections
|
|
417
|
+
.filter(({ heading }) => AMENDMENTS_HEADING.test(heading))
|
|
418
|
+
.flatMap(({ lines }) => amendmentEntries(lines).entries)
|
|
419
|
+
.filter((entry) => APPROVAL.test(entry.body.join("\n")))
|
|
420
|
+
.flatMap((entry) => (DROPS_LINE.exec(entry.body.join("\n"))?.[1] ?? "").split(/[\s,]+/))
|
|
421
|
+
.filter((id) => id !== "");
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function checkAmendments(heading: string, lines: string[]): string[] {
|
|
425
|
+
const { entries, outside } = amendmentEntries(lines);
|
|
426
|
+
const problems: string[] = [];
|
|
427
|
+
if (hasText(outside)) {
|
|
428
|
+
problems.push(problem(`## ${heading} has text outside a ### entry`));
|
|
429
|
+
}
|
|
430
|
+
for (const entry of entries) {
|
|
431
|
+
const body = entry.body.join("\n");
|
|
432
|
+
if (!APPROVAL.test(body)) {
|
|
433
|
+
problems.push(problem(`## ${heading} entry "${entry.title}" has no Approved: <who>, <date> line`));
|
|
434
|
+
}
|
|
435
|
+
for (const field of AMENDMENT_FIELDS) {
|
|
436
|
+
if (!new RegExp(`^${field}:\\s*\\S`, "m").test(body)) {
|
|
437
|
+
problems.push(problem(`## ${heading} entry "${entry.title}" has no ${field}: line naming what it changes`));
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return problems;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** A test the suite lists: its spec file, relative to the config's root directory, and its own title. */
|
|
445
|
+
export type ListedTest = { file: string; title: string };
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Every case id of the contract, but one an approved amendment drops, appears in the title of a listed test of a spec
|
|
449
|
+
* file the contract's `## Verification` runs. Case ids (`C-01`) repeat from one contract to the next, so a test of
|
|
450
|
+
* another contract's spec never counts.
|
|
451
|
+
*/
|
|
452
|
+
function checkTestsExist(rows: CaseRow[], tests: readonly ListedTest[], markdown: string, dropped: string[]): string[] {
|
|
453
|
+
const specs = contractSeries(markdown).flatMap((series) => series.specs);
|
|
454
|
+
if (specs.length === 0) {
|
|
455
|
+
return [
|
|
456
|
+
problem(
|
|
457
|
+
"## Verification names no spec file: write the command that runs the new specs, so --implemented can find this contract's tests",
|
|
458
|
+
),
|
|
459
|
+
];
|
|
460
|
+
}
|
|
461
|
+
const titles = tests.filter((test) => specs.some((spec) => sameSpec(spec, test.file))).map((test) => test.title);
|
|
462
|
+
return rows
|
|
463
|
+
.map((row) => row.name.split(/\s+/)[0] ?? "")
|
|
464
|
+
.filter((caseId) => caseId !== "" && !dropped.includes(caseId))
|
|
465
|
+
.filter((caseId) => !titles.some((title) => namesCase(title, caseId)))
|
|
466
|
+
.map((caseId) =>
|
|
467
|
+
problem(
|
|
468
|
+
`case ${caseId} has no test: no title of the tests in the spec files of ## Verification names it (the case id comes first in the title)`,
|
|
469
|
+
),
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/** What the contract is checked against besides its own text; every part is optional and the CLI fills them from disk. */
|
|
474
|
+
export type ContractChecks = {
|
|
475
|
+
/** Every data role in the cases table must be one of these (or `none`). */
|
|
476
|
+
roles?: string[];
|
|
477
|
+
/** Every critical flow in the cases table must be one of these (or `no`): the facts' `critical`. */
|
|
478
|
+
critical?: string[];
|
|
479
|
+
/** The text of a report an `observed:` oracle names, or `undefined` when it does not exist: the report must hold the value. */
|
|
480
|
+
readReport?: (path: string) => string | undefined;
|
|
481
|
+
/** The day (`yyyy-mm-dd`) a local file last changed, or `undefined` when it does not exist: for the dates in `## Source`. */
|
|
482
|
+
modifiedOn?: (path: string) => string | undefined;
|
|
483
|
+
/** The contract must also carry its `## Approval` line (`check --approved`). */
|
|
484
|
+
requireApproval?: boolean;
|
|
485
|
+
/**
|
|
486
|
+
* No `observed: at first run` part may be left and, when `listedTests` is given, every case id appears in the title of
|
|
487
|
+
* one of them in a spec file of `## Verification` (`check --implemented`).
|
|
488
|
+
*/
|
|
489
|
+
requireImplemented?: boolean;
|
|
490
|
+
/** The tests the suite lists (`--list`): spec file and own title, without describe titles. */
|
|
491
|
+
listedTests?: ListedTest[];
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* Checks the shape of a contract. Only the shape: it does not judge whether the cases are good. `<!-- -->` comments are
|
|
496
|
+
* ignored.
|
|
497
|
+
*
|
|
498
|
+
* @param checks what to check besides the text, see {@link ContractChecks}; omitted, only the shape is checked.
|
|
499
|
+
* @returns one line per problem, empty when the contract is valid.
|
|
500
|
+
*/
|
|
501
|
+
export function checkContract(markdown: string, checks: ContractChecks = {}): string[] {
|
|
502
|
+
const { roles, critical, readReport, modifiedOn, requireApproval = false, requireImplemented = false } = checks;
|
|
503
|
+
const parsed = parseSections(withoutComments(markdown));
|
|
504
|
+
const sections = new Map<string, string[]>();
|
|
505
|
+
const problems: string[] = [];
|
|
506
|
+
for (const { heading, lines } of parsed) {
|
|
507
|
+
if (sections.has(heading)) {
|
|
508
|
+
if (!problems.includes(problem(`## ${heading} appears more than once`))) {
|
|
509
|
+
problems.push(problem(`## ${heading} appears more than once`));
|
|
510
|
+
}
|
|
511
|
+
} else {
|
|
512
|
+
sections.set(heading, lines);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
for (const heading of REQUIRED_SECTIONS) {
|
|
516
|
+
const lines = sections.get(heading);
|
|
517
|
+
if (lines === undefined) {
|
|
518
|
+
problems.push(problem(`## ${heading} is missing`));
|
|
519
|
+
} else if (!hasText(lines)) {
|
|
520
|
+
problems.push(problem(`## ${heading} is empty`));
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
const source = sections.get("Source");
|
|
524
|
+
if (source !== undefined && hasText(source)) {
|
|
525
|
+
problems.push(...checkSource(source, modifiedOn));
|
|
526
|
+
}
|
|
527
|
+
const cases = sections.get("Cases");
|
|
528
|
+
const checked =
|
|
529
|
+
cases !== undefined && hasText(cases) ? checkCases(cases, roles, critical) : { problems: [], rows: [] };
|
|
530
|
+
problems.push(
|
|
531
|
+
...checked.problems,
|
|
532
|
+
...checkObservedValues(checked.rows, {
|
|
533
|
+
readReport,
|
|
534
|
+
approved: sections.has(APPROVAL_SECTION),
|
|
535
|
+
approvedAmendments: approvedAmendmentText(parsed),
|
|
536
|
+
implemented: requireImplemented,
|
|
537
|
+
}),
|
|
538
|
+
...checkObservedOnly(sections.get("Observed only"), checked.rows),
|
|
539
|
+
...(requireImplemented && checks.listedTests !== undefined
|
|
540
|
+
? checkTestsExist(checked.rows, checks.listedTests, markdown, droppedCases(parsed))
|
|
541
|
+
: []),
|
|
542
|
+
);
|
|
543
|
+
const approval = sections.get(APPROVAL_SECTION);
|
|
544
|
+
if (approval !== undefined) {
|
|
545
|
+
problems.push(...checkApproval(approval));
|
|
546
|
+
} else if (requireApproval) {
|
|
547
|
+
problems.push(problem(`## ${APPROVAL_SECTION} is missing: a person has not approved this contract`));
|
|
548
|
+
}
|
|
549
|
+
for (const { heading, lines } of parsed) {
|
|
550
|
+
if (AMENDMENTS_HEADING.test(heading)) {
|
|
551
|
+
problems.push(...checkAmendments(heading, lines));
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
return problems;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/** What another check reads from a contract besides its shape. */
|
|
558
|
+
export type ContractReferences = {
|
|
559
|
+
/** The id of every row of `## Cases`, the first word of its Case cell. */
|
|
560
|
+
caseIds: string[];
|
|
561
|
+
/** The case ids an approved amendment drops (`Drops:`). */
|
|
562
|
+
dropped: string[];
|
|
563
|
+
/** The `.qa/reports/explore-<slug>[-observed].md` reports its `observed:` oracles name. */
|
|
564
|
+
observedReports: string[];
|
|
565
|
+
/** The files under `.qa/reports/` and `.qa/kb/` its `## Source` names. */
|
|
566
|
+
sourceFiles: string[];
|
|
567
|
+
/** Whether it has an `## Approval` section. */
|
|
568
|
+
approved: boolean;
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* Reads the case ids, the dropped cases, the reports and the approval of a contract, without checking its shape.
|
|
573
|
+
* `<!-- -->` comments are ignored.
|
|
574
|
+
*
|
|
575
|
+
* @returns what the contract names; a missing section gives an empty list.
|
|
576
|
+
*/
|
|
577
|
+
function sectionLines(parsed: Section[], heading: string): string[] {
|
|
578
|
+
return parsed.find((section) => section.heading === heading)?.lines ?? [];
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
export function contractReferences(markdown: string): ContractReferences {
|
|
582
|
+
const parsed = parseSections(withoutComments(markdown));
|
|
583
|
+
const cases = sectionLines(parsed, "Cases");
|
|
584
|
+
const rows = hasText(cases) ? checkCases(cases, undefined, undefined).rows : [];
|
|
585
|
+
const observedReports = rows
|
|
586
|
+
.flatMap((row) => sourceParts(row.oracle))
|
|
587
|
+
.filter((part) => part.text.startsWith(OBSERVED))
|
|
588
|
+
.map((part) => RECORDED.exec(part.text.slice(OBSERVED.length).trim())?.[2]?.trim() ?? "")
|
|
589
|
+
.filter((path) => REPORT_PATH.test(path));
|
|
590
|
+
return {
|
|
591
|
+
caseIds: rows.map((row) => row.name.split(/\s+/)[0] ?? "").filter((caseId) => caseId !== ""),
|
|
592
|
+
dropped: droppedCases(parsed),
|
|
593
|
+
observedReports: [...new Set(observedReports)],
|
|
594
|
+
sourceFiles: [...new Set(sectionLines(parsed, "Source").flatMap(localStateFiles))],
|
|
595
|
+
approved: parsed.some((section) => section.heading === APPROVAL_SECTION),
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
function readReportFile(path: string): string | undefined {
|
|
600
|
+
try {
|
|
601
|
+
return readFileSync(path, "utf8");
|
|
602
|
+
} catch {
|
|
603
|
+
return undefined;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
function twoDigits(value: number): string {
|
|
608
|
+
return String(value).padStart(2, "0");
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
function modifiedOnDay(path: string): string | undefined {
|
|
612
|
+
try {
|
|
613
|
+
const time = statSync(path).mtime;
|
|
614
|
+
return `${time.getFullYear()}-${twoDigits(time.getMonth() + 1)}-${twoDigits(time.getDate())}`;
|
|
615
|
+
} catch {
|
|
616
|
+
return undefined;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* Every test the suite lists (`playwright test --list --reporter=json` with the suite's config, Playwright from the
|
|
622
|
+
* suite's own `node_modules` when it is its own npm project): its spec file and its own title, without describe titles.
|
|
623
|
+
*
|
|
624
|
+
* @throws Error when the list cannot be made or is not Playwright JSON.
|
|
625
|
+
*/
|
|
626
|
+
function listedTests(suiteDir: string): ListedTest[] {
|
|
627
|
+
const cli = join(process.cwd(), suitePackageDir(process.cwd(), suiteDir), ...PLAYWRIGHT_CLI);
|
|
628
|
+
const { PLAYWRIGHT_JSON_OUTPUT_FILE: _output, ...environment } = process.env;
|
|
629
|
+
const listed = spawnSync(
|
|
630
|
+
process.execPath,
|
|
631
|
+
[cli, "test", ...playwrightConfigArgs(suiteDir), "--list", "--reporter=json"],
|
|
632
|
+
{ cwd: process.cwd(), env: environment, encoding: "utf8", maxBuffer: LIST_BUFFER, windowsHide: true },
|
|
633
|
+
);
|
|
634
|
+
if (listed.status !== 0) {
|
|
635
|
+
const reason = ((listed.stderr || listed.stdout).trim().split("\n")[0] ?? "").trim().slice(0, MAX_REASON);
|
|
636
|
+
throw new Error(`contract: --implemented could not list the tests: ${reason}`);
|
|
637
|
+
}
|
|
638
|
+
const tests: ListedTest[] = [];
|
|
639
|
+
try {
|
|
640
|
+
walkRoot(JSON.parse(listed.stdout), "list", (test) => {
|
|
641
|
+
const parts = test.split(TITLE_SEPARATOR);
|
|
642
|
+
tests.push({ file: parts[0] ?? "", title: parts.at(-1) ?? "" });
|
|
643
|
+
});
|
|
644
|
+
} catch (error) {
|
|
645
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
646
|
+
throw new Error(`contract: --implemented could not read the list of tests: ${reason}`);
|
|
647
|
+
}
|
|
648
|
+
return tests;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
if (basename(process.argv[1] ?? "") === CLI_FILE_NAME) {
|
|
652
|
+
try {
|
|
653
|
+
const [command, ...rest] = process.argv.slice(2);
|
|
654
|
+
const flags = rest.filter((argument) => argument.startsWith("--"));
|
|
655
|
+
const [file, extra] = rest.filter((argument) => !argument.startsWith("--"));
|
|
656
|
+
if (
|
|
657
|
+
command !== "check" ||
|
|
658
|
+
file === undefined ||
|
|
659
|
+
extra !== undefined ||
|
|
660
|
+
flags.some((flag) => !FLAGS.includes(flag)) ||
|
|
661
|
+
new Set(flags).size !== flags.length
|
|
662
|
+
) {
|
|
663
|
+
throw new Error(
|
|
664
|
+
`contract: usage is node .claude/checks/contract.mts check [${APPROVED_FLAG}] [${IMPLEMENTED_FLAG}] <file>, run from the repository root (report paths resolve from it)`,
|
|
665
|
+
);
|
|
666
|
+
}
|
|
667
|
+
let text: string;
|
|
668
|
+
try {
|
|
669
|
+
text = readFileSync(file, "utf8");
|
|
670
|
+
} catch {
|
|
671
|
+
throw new Error(`contract: ${file} cannot be read`);
|
|
672
|
+
}
|
|
673
|
+
const facts = readProjectFacts();
|
|
674
|
+
const implemented = flags.includes(IMPLEMENTED_FLAG);
|
|
675
|
+
const problems = checkContract(text, {
|
|
676
|
+
roles: Object.keys(facts.roles),
|
|
677
|
+
critical: facts.critical,
|
|
678
|
+
readReport: readReportFile,
|
|
679
|
+
modifiedOn: modifiedOnDay,
|
|
680
|
+
requireApproval: flags.includes(APPROVED_FLAG),
|
|
681
|
+
requireImplemented: implemented,
|
|
682
|
+
...(implemented ? { listedTests: listedTests(facts.suiteDir) } : {}),
|
|
683
|
+
});
|
|
684
|
+
if (problems.length > 0) {
|
|
685
|
+
process.stderr.write(`${problems.join("\n")}\n`);
|
|
686
|
+
process.exitCode = 1;
|
|
687
|
+
} else {
|
|
688
|
+
process.stdout.write(`ok ${file}\n`);
|
|
689
|
+
}
|
|
690
|
+
} catch (error) {
|
|
691
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
692
|
+
process.exitCode = 1;
|
|
693
|
+
}
|
|
694
|
+
}
|