@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,469 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { basename } from "node:path";
|
|
3
|
+
|
|
4
|
+
import { readProjectFacts } from "./project-facts.mts";
|
|
5
|
+
import { CREDENTIAL_SHAPE } from "./readonly-shell.mts";
|
|
6
|
+
|
|
7
|
+
const CLI_FILE_NAME = "contract.mts";
|
|
8
|
+
const APPROVED_FLAG = "--approved";
|
|
9
|
+
const IMPLEMENTED_FLAG = "--implemented";
|
|
10
|
+
const FLAGS = [APPROVED_FLAG, IMPLEMENTED_FLAG];
|
|
11
|
+
const SOURCE_KINDS = ["ticket", "doc", "area", "gap"] as const;
|
|
12
|
+
const ORACLE_SOURCES = ["ticket", "doc", "schema", "observed"] as const;
|
|
13
|
+
const REQUIRED_SECTIONS = [
|
|
14
|
+
"Source",
|
|
15
|
+
"Scope",
|
|
16
|
+
"Out of scope",
|
|
17
|
+
"Cases",
|
|
18
|
+
"Stand needs",
|
|
19
|
+
"Building blocks",
|
|
20
|
+
"Platform defects",
|
|
21
|
+
"Verification",
|
|
22
|
+
] as const;
|
|
23
|
+
const CASE_COLUMNS = ["Case", "Technique", "Project", "Data role", "Oracle"] as const;
|
|
24
|
+
const AMENDMENTS_HEADING = /^amendment/i;
|
|
25
|
+
const NO_ROLE = "none";
|
|
26
|
+
const APPROVAL_LINE = /^Approved:\s*([^,\s][^,]*),\s*\d{4}-\d{2}-\d{2}(?:\s+\(relayed: "[^\n]+"\))?\s*$/;
|
|
27
|
+
const APPROVAL = new RegExp(APPROVAL_LINE.source, "m");
|
|
28
|
+
const APPROVAL_SECTION = "Approval";
|
|
29
|
+
const CONFIRMATION = /^(characterization|confirmed by \S.*)$/;
|
|
30
|
+
const OBSERVED = "observed:";
|
|
31
|
+
const PENDING = /^pending\b/;
|
|
32
|
+
const AT_FIRST_RUN = /^at first run\s+\S/;
|
|
33
|
+
const FIRST_RUN_LINE = /\(first run\)$/;
|
|
34
|
+
const REGEX_SPECIALS = /[.*+?^${}()|[\]\\]/g;
|
|
35
|
+
const RECORDED = /^(.*?)\s*\(([^()]*)\)$/;
|
|
36
|
+
const ESCAPED_PIPE = /\\\|/g;
|
|
37
|
+
const REPORTS_DIR = ".qa/";
|
|
38
|
+
const REPORT_PATH = /^\.qa\/reports\/explore-[a-z0-9]+(-[a-z0-9]+)*\.md$/;
|
|
39
|
+
const OBSERVED_VALUES = "Observed values";
|
|
40
|
+
|
|
41
|
+
type CaseRow = { name: string; dataRole: string; oracle: string };
|
|
42
|
+
|
|
43
|
+
function problem(text: string): string {
|
|
44
|
+
return `contract: ${text}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
type Section = { heading: string; lines: string[] };
|
|
48
|
+
|
|
49
|
+
function parseSections(markdown: string): Section[] {
|
|
50
|
+
const sections: Section[] = [];
|
|
51
|
+
for (const line of markdown.split(/\r?\n/)) {
|
|
52
|
+
const heading = /^## (\S.*?)\s*$/.exec(line);
|
|
53
|
+
if (heading?.[1] !== undefined) {
|
|
54
|
+
sections.push({ heading: heading[1], lines: [] });
|
|
55
|
+
} else {
|
|
56
|
+
sections.at(-1)?.lines.push(line);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return sections;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function hasText(lines: string[]): boolean {
|
|
63
|
+
return lines.some((line) => line.trim() !== "");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function checkSource(lines: string[]): string[] {
|
|
67
|
+
const first = lines.find((line) => line.trim() !== "")?.trim() ?? "";
|
|
68
|
+
const accepted = SOURCE_KINDS.some((kind) => new RegExp(`^${kind}:\\s*\\S`).test(first));
|
|
69
|
+
return accepted ? [] : [problem("## Source must start with ticket:, doc:, area: or gap:")];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Splits the Oracle cell on `separator` outside double quotes, so a quoted observed value may hold `;`. */
|
|
73
|
+
function splitOutsideQuotes(text: string, separator: string): string[] {
|
|
74
|
+
const parts = [""];
|
|
75
|
+
let quoted = false;
|
|
76
|
+
for (const character of text) {
|
|
77
|
+
if (character === '"') {
|
|
78
|
+
quoted = !quoted;
|
|
79
|
+
}
|
|
80
|
+
if (character === separator && !quoted) {
|
|
81
|
+
parts.push("");
|
|
82
|
+
} else {
|
|
83
|
+
parts[parts.length - 1] += character;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return parts;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Splits a table row on every `|` that is not escaped as `\|`, the Markdown way to write a pipe in a cell. */
|
|
90
|
+
function splitRow(line: string): string[] {
|
|
91
|
+
return line
|
|
92
|
+
.trim()
|
|
93
|
+
.replace(/^\|/, "")
|
|
94
|
+
.replace(/(?<!\\)\|$/, "")
|
|
95
|
+
.split(/(?<!\\)\|/)
|
|
96
|
+
.map((cell) => cell.trim());
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function isSeparator(cells: string[]): boolean {
|
|
100
|
+
return cells.every((cell) => /^:?-{3,}:?$/.test(cell));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function oracleParts(oracle: string): string[] {
|
|
104
|
+
return splitOutsideQuotes(oracle, ";").map((part) => part.trim());
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function oracleSources(oracle: string): string[] {
|
|
108
|
+
return oracleParts(oracle)
|
|
109
|
+
.map((part) => ORACLE_SOURCES.find((source) => part.trim().startsWith(`${source}:`)))
|
|
110
|
+
.filter((source) => source !== undefined);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function isObservedOnly(oracle: string): boolean {
|
|
114
|
+
const sources = oracleSources(oracle);
|
|
115
|
+
return sources.length > 0 && sources.every((source) => source === "observed");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function checkCases(lines: string[], roles: string[] | undefined): { problems: string[]; rows: CaseRow[] } {
|
|
119
|
+
const problems: string[] = [];
|
|
120
|
+
const rows: CaseRow[] = [];
|
|
121
|
+
const tableLines = lines.filter((line) => line.trim().startsWith("|"));
|
|
122
|
+
const header = splitRow(tableLines[0] ?? "");
|
|
123
|
+
const index = new Map(CASE_COLUMNS.map((column) => [column, header.indexOf(column)]));
|
|
124
|
+
for (const column of CASE_COLUMNS) {
|
|
125
|
+
if (index.get(column) === -1) {
|
|
126
|
+
problems.push(problem(`## Cases table has no ${column} column`));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const body = tableLines
|
|
130
|
+
.slice(1)
|
|
131
|
+
.map(splitRow)
|
|
132
|
+
.filter((cells) => !isSeparator(cells));
|
|
133
|
+
if (body.length === 0) {
|
|
134
|
+
problems.push(problem("## Cases table has no case rows"));
|
|
135
|
+
}
|
|
136
|
+
const cell = (cells: string[], column: (typeof CASE_COLUMNS)[number]): string => cells[index.get(column) ?? -1] ?? "";
|
|
137
|
+
for (const [position, cells] of body.entries()) {
|
|
138
|
+
if (cells.length !== header.length) {
|
|
139
|
+
problems.push(problem(`## Cases row ${position + 1} has ${cells.length} cells, the header has ${header.length}`));
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const name = cell(cells, "Case");
|
|
143
|
+
const label = name === "" ? `row ${position + 1}` : `case "${name}"`;
|
|
144
|
+
for (const column of CASE_COLUMNS) {
|
|
145
|
+
if (index.get(column) !== -1 && cell(cells, column) === "") {
|
|
146
|
+
problems.push(problem(`${label} has an empty ${column} cell`));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const oracle = cell(cells, "Oracle");
|
|
150
|
+
if (
|
|
151
|
+
index.get("Oracle") !== -1 &&
|
|
152
|
+
oracle !== "" &&
|
|
153
|
+
!ORACLE_SOURCES.some((source) => oracle.startsWith(`${source}:`))
|
|
154
|
+
) {
|
|
155
|
+
problems.push(problem(`${label} oracle must start with ticket:, doc:, schema: or observed:`));
|
|
156
|
+
}
|
|
157
|
+
if ((oracle.match(/"/g) ?? []).length % 2 === 1) {
|
|
158
|
+
problems.push(problem(`${label} oracle has an unbalanced double quote, which hides the parts after it`));
|
|
159
|
+
}
|
|
160
|
+
const dataRole = cell(cells, "Data role");
|
|
161
|
+
for (const role of dataRole.split(",").map((item) => item.trim())) {
|
|
162
|
+
if (roles !== undefined && role !== "" && role !== NO_ROLE && !roles.includes(role)) {
|
|
163
|
+
problems.push(problem(`${label} data role "${role}" is not in the facts roles`));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
rows.push({ name, dataRole, oracle });
|
|
167
|
+
}
|
|
168
|
+
return { problems, rows };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** The value as the explorer writes it in the report: raw, never quoted, with `\|` unescaped. */
|
|
172
|
+
function rawValue(value: string): string {
|
|
173
|
+
const unquoted = value.length >= 2 && value.startsWith('"') && value.endsWith('"') ? value.slice(1, -1) : value;
|
|
174
|
+
return unquoted.replace(ESCAPED_PIPE, "|");
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function recordedLine(report: string, caseId: string, value: string): string | undefined {
|
|
178
|
+
const recorded = parseSections(report).find((section) => section.heading === OBSERVED_VALUES)?.lines ?? [];
|
|
179
|
+
return recorded.map((line) => line.trim()).find((line) => line.startsWith(`- ${caseId}: ${value} (`));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function namesCase(text: string, caseId: string): boolean {
|
|
183
|
+
return new RegExp(`(^|[^\\w-])${caseId.replace(REGEX_SPECIALS, "\\$&")}([^\\w-]|$)`).test(text);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
type OraclePart = { text: string; splitOnSemicolon: boolean };
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* What an observed part is checked against: the report reader, whether the contract has its approval line, the text
|
|
190
|
+
* of its approved amendment entries, and whether the implementation must be finished (`--implemented`).
|
|
191
|
+
*/
|
|
192
|
+
type ObservedContext = {
|
|
193
|
+
readReport: ((path: string) => string | undefined) | undefined;
|
|
194
|
+
approved: boolean;
|
|
195
|
+
approvedAmendments: string;
|
|
196
|
+
implemented: boolean;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
/** The Oracle's parts; a part that starts with no source prefix is joined back to the part before it. */
|
|
200
|
+
function sourceParts(oracle: string): OraclePart[] {
|
|
201
|
+
const parts: OraclePart[] = [];
|
|
202
|
+
for (const text of oracleParts(oracle)) {
|
|
203
|
+
const last = parts.at(-1);
|
|
204
|
+
if (last !== undefined && !ORACLE_SOURCES.some((source) => text.startsWith(`${source}:`))) {
|
|
205
|
+
last.text = `${last.text}; ${text}`;
|
|
206
|
+
last.splitOnSemicolon = true;
|
|
207
|
+
} else {
|
|
208
|
+
parts.push({ text, splitOnSemicolon: false });
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return parts;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function checkObservedPart(
|
|
215
|
+
label: string,
|
|
216
|
+
caseId: string,
|
|
217
|
+
part: OraclePart,
|
|
218
|
+
context: ObservedContext,
|
|
219
|
+
): string | undefined {
|
|
220
|
+
const { readReport, approved, approvedAmendments, implemented } = context;
|
|
221
|
+
const rest = part.text.slice(OBSERVED.length).trim();
|
|
222
|
+
if (part.splitOnSemicolon) {
|
|
223
|
+
return `${label} observed: the value holds an unquoted ;: write the value in double quotes`;
|
|
224
|
+
}
|
|
225
|
+
if (AT_FIRST_RUN.test(rest)) {
|
|
226
|
+
if (implemented) {
|
|
227
|
+
return `${label} observed: at first run is unresolved: the expert has not recorded and pinned its value`;
|
|
228
|
+
}
|
|
229
|
+
return approved ? undefined : `${label} observed: at first run awaits the person's approval (## Approval)`;
|
|
230
|
+
}
|
|
231
|
+
const match = RECORDED.exec(rest);
|
|
232
|
+
const value = match?.[1]?.trim();
|
|
233
|
+
const path = match?.[2]?.trim();
|
|
234
|
+
if (value === undefined || path === undefined || !(path.startsWith(REPORTS_DIR) || path.endsWith(".md"))) {
|
|
235
|
+
return PENDING.test(rest)
|
|
236
|
+
? `${label} observed: oracle has no recorded value: observe it first, then write observed: <value> (.qa/reports/explore-<slug>-observed.md)`
|
|
237
|
+
: `${label} observed: this contract predates observe-before-approval: re-plan its observed cases (observed: pending <what to observe>)`;
|
|
238
|
+
}
|
|
239
|
+
if (!REPORT_PATH.test(path)) {
|
|
240
|
+
return `${label} observed: report path must be .qa/reports/explore-<slug>[-observed].md, not "${path}"`;
|
|
241
|
+
}
|
|
242
|
+
if (value === "") {
|
|
243
|
+
return `${label} observed: oracle names a report but no value before it`;
|
|
244
|
+
}
|
|
245
|
+
if (CREDENTIAL_SHAPE.test(value)) {
|
|
246
|
+
return `${label} observed: the value looks like a credential (a token, a password field or a Bearer header); never record one`;
|
|
247
|
+
}
|
|
248
|
+
if (readReport === undefined) {
|
|
249
|
+
return undefined;
|
|
250
|
+
}
|
|
251
|
+
const report = readReport(path);
|
|
252
|
+
if (report === undefined) {
|
|
253
|
+
return `${label} observed: oracle names ${path}, which does not exist`;
|
|
254
|
+
}
|
|
255
|
+
const line = recordedLine(report, caseId, rawValue(value));
|
|
256
|
+
if (line === undefined) {
|
|
257
|
+
return `${label} observed: ${path} has no line "- ${caseId}: ${rawValue(value)} (" under ## ${OBSERVED_VALUES}`;
|
|
258
|
+
}
|
|
259
|
+
return FIRST_RUN_LINE.test(line) && !namesCase(approvedAmendments, caseId)
|
|
260
|
+
? `${label} observed: the first-run value of ${caseId} needs an approved amendment naming it (A-n first-run values: ${caseId} = <value> (<report>))`
|
|
261
|
+
: undefined;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function checkObservedValues(rows: CaseRow[], context: ObservedContext): string[] {
|
|
265
|
+
const problems: string[] = [];
|
|
266
|
+
for (const row of rows) {
|
|
267
|
+
const label = row.name === "" ? "a case" : `case "${row.name}"`;
|
|
268
|
+
const caseId = row.name.split(/\s+/)[0] ?? "";
|
|
269
|
+
for (const part of sourceParts(row.oracle).filter((item) => item.text.startsWith(OBSERVED))) {
|
|
270
|
+
const found = checkObservedPart(label, caseId, part, context);
|
|
271
|
+
if (found !== undefined) {
|
|
272
|
+
problems.push(problem(found));
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return problems;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function checkObservedOnly(lines: string[] | undefined, rows: CaseRow[]): string[] {
|
|
280
|
+
const observed = rows.filter((row) => row.name !== "" && isObservedOnly(row.oracle));
|
|
281
|
+
if (observed.length === 0) {
|
|
282
|
+
return [];
|
|
283
|
+
}
|
|
284
|
+
if (lines === undefined) {
|
|
285
|
+
return [problem("## Observed only is missing")];
|
|
286
|
+
}
|
|
287
|
+
const problems: string[] = [];
|
|
288
|
+
for (const row of observed) {
|
|
289
|
+
const entry = lines
|
|
290
|
+
.map((line) => line.trim())
|
|
291
|
+
.find((line) => /^[-*]\s/.test(line) && line.slice(2).startsWith(`${row.name}:`));
|
|
292
|
+
if (entry === undefined) {
|
|
293
|
+
problems.push(problem(`## Observed only does not list case "${row.name}"`));
|
|
294
|
+
} else if (!CONFIRMATION.test(entry.slice(2 + row.name.length + 1).trim())) {
|
|
295
|
+
problems.push(
|
|
296
|
+
problem(`## Observed only case "${row.name}" must be marked confirmed by <who> or characterization`),
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return problems;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function checkApproval(lines: string[]): string[] {
|
|
304
|
+
const written = lines.map((line) => line.trim()).filter((line) => line !== "");
|
|
305
|
+
return written.length === 1 && APPROVAL_LINE.test(written[0] ?? "")
|
|
306
|
+
? []
|
|
307
|
+
: [
|
|
308
|
+
problem(
|
|
309
|
+
`## ${APPROVAL_SECTION} must hold one line: Approved: <who>, <date>, optionally ending (relayed: "<the person's words>")`,
|
|
310
|
+
),
|
|
311
|
+
];
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
type AmendmentEntry = { title: string; body: string[] };
|
|
315
|
+
|
|
316
|
+
function amendmentEntries(lines: string[]): { entries: AmendmentEntry[]; outside: string[] } {
|
|
317
|
+
const entries: AmendmentEntry[] = [];
|
|
318
|
+
const outside: string[] = [];
|
|
319
|
+
for (const line of lines) {
|
|
320
|
+
const heading = /^### (\S.*?)\s*$/.exec(line);
|
|
321
|
+
if (heading?.[1] !== undefined) {
|
|
322
|
+
entries.push({ title: heading[1], body: [] });
|
|
323
|
+
} else {
|
|
324
|
+
(entries.at(-1)?.body ?? outside).push(line);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return { entries, outside };
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** The title and text of every amendment entry that carries its approval line, for the first-run values it names. */
|
|
331
|
+
function approvedAmendmentText(sections: Section[]): string {
|
|
332
|
+
return sections
|
|
333
|
+
.filter(({ heading }) => AMENDMENTS_HEADING.test(heading))
|
|
334
|
+
.flatMap(({ lines }) => amendmentEntries(lines).entries)
|
|
335
|
+
.filter((entry) => APPROVAL.test(entry.body.join("\n")))
|
|
336
|
+
.map((entry) => [entry.title, ...entry.body].join("\n"))
|
|
337
|
+
.join("\n");
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function checkAmendments(heading: string, lines: string[]): string[] {
|
|
341
|
+
const { entries, outside } = amendmentEntries(lines);
|
|
342
|
+
const problems: string[] = [];
|
|
343
|
+
if (hasText(outside)) {
|
|
344
|
+
problems.push(problem(`## ${heading} has text outside a ### entry`));
|
|
345
|
+
}
|
|
346
|
+
for (const entry of entries) {
|
|
347
|
+
if (!APPROVAL.test(entry.body.join("\n"))) {
|
|
348
|
+
problems.push(problem(`## ${heading} entry "${entry.title}" has no Approved: <who>, <date> line`));
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return problems;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Checks the shape of a contract. Only the shape: it does not judge whether the cases are good.
|
|
356
|
+
*
|
|
357
|
+
* @param roles when given, every data role in the cases table must be one of them (or `none`).
|
|
358
|
+
* @param readReport returns the text of a report an `observed:` oracle names, or `undefined` when it does not exist;
|
|
359
|
+
* when given, the report must hold the oracle's value for the case. The CLI reads from disk; omitted, only the shape
|
|
360
|
+
* is checked.
|
|
361
|
+
* @param requireApproval when true, the contract must also carry its `## Approval` line (`check --approved`).
|
|
362
|
+
* @param requireImplemented when true, no `observed: at first run` part may be left (`check --implemented`).
|
|
363
|
+
* @returns one line per problem, empty when the contract is valid.
|
|
364
|
+
*/
|
|
365
|
+
export function checkContract(
|
|
366
|
+
markdown: string,
|
|
367
|
+
roles?: string[],
|
|
368
|
+
readReport?: (path: string) => string | undefined,
|
|
369
|
+
requireApproval = false,
|
|
370
|
+
requireImplemented = false,
|
|
371
|
+
): string[] {
|
|
372
|
+
const parsed = parseSections(markdown);
|
|
373
|
+
const sections = new Map<string, string[]>();
|
|
374
|
+
const problems: string[] = [];
|
|
375
|
+
for (const { heading, lines } of parsed) {
|
|
376
|
+
if (sections.has(heading)) {
|
|
377
|
+
if (!problems.includes(problem(`## ${heading} appears more than once`))) {
|
|
378
|
+
problems.push(problem(`## ${heading} appears more than once`));
|
|
379
|
+
}
|
|
380
|
+
} else {
|
|
381
|
+
sections.set(heading, lines);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
for (const heading of REQUIRED_SECTIONS) {
|
|
385
|
+
const lines = sections.get(heading);
|
|
386
|
+
if (lines === undefined) {
|
|
387
|
+
problems.push(problem(`## ${heading} is missing`));
|
|
388
|
+
} else if (!hasText(lines)) {
|
|
389
|
+
problems.push(problem(`## ${heading} is empty`));
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
const source = sections.get("Source");
|
|
393
|
+
if (source !== undefined && hasText(source)) {
|
|
394
|
+
problems.push(...checkSource(source));
|
|
395
|
+
}
|
|
396
|
+
const cases = sections.get("Cases");
|
|
397
|
+
const checked = cases !== undefined && hasText(cases) ? checkCases(cases, roles) : { problems: [], rows: [] };
|
|
398
|
+
problems.push(
|
|
399
|
+
...checked.problems,
|
|
400
|
+
...checkObservedValues(checked.rows, {
|
|
401
|
+
readReport,
|
|
402
|
+
approved: sections.has(APPROVAL_SECTION),
|
|
403
|
+
approvedAmendments: approvedAmendmentText(parsed),
|
|
404
|
+
implemented: requireImplemented,
|
|
405
|
+
}),
|
|
406
|
+
...checkObservedOnly(sections.get("Observed only"), checked.rows),
|
|
407
|
+
);
|
|
408
|
+
const approval = sections.get(APPROVAL_SECTION);
|
|
409
|
+
if (approval !== undefined) {
|
|
410
|
+
problems.push(...checkApproval(approval));
|
|
411
|
+
} else if (requireApproval) {
|
|
412
|
+
problems.push(problem(`## ${APPROVAL_SECTION} is missing: a person has not approved this contract`));
|
|
413
|
+
}
|
|
414
|
+
for (const { heading, lines } of parsed) {
|
|
415
|
+
if (AMENDMENTS_HEADING.test(heading)) {
|
|
416
|
+
problems.push(...checkAmendments(heading, lines));
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return problems;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function readReportFile(path: string): string | undefined {
|
|
423
|
+
try {
|
|
424
|
+
return readFileSync(path, "utf8");
|
|
425
|
+
} catch {
|
|
426
|
+
return undefined;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if (basename(process.argv[1] ?? "") === CLI_FILE_NAME) {
|
|
431
|
+
try {
|
|
432
|
+
const [command, ...rest] = process.argv.slice(2);
|
|
433
|
+
const flags = rest.filter((argument) => argument.startsWith("--"));
|
|
434
|
+
const [file, extra] = rest.filter((argument) => !argument.startsWith("--"));
|
|
435
|
+
if (
|
|
436
|
+
command !== "check" ||
|
|
437
|
+
file === undefined ||
|
|
438
|
+
extra !== undefined ||
|
|
439
|
+
flags.some((flag) => !FLAGS.includes(flag)) ||
|
|
440
|
+
new Set(flags).size !== flags.length
|
|
441
|
+
) {
|
|
442
|
+
throw new Error(
|
|
443
|
+
`contract: usage is node .claude/checks/contract.mts check [${APPROVED_FLAG}] [${IMPLEMENTED_FLAG}] <file>, run from the repository root (report paths resolve from it)`,
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
let text: string;
|
|
447
|
+
try {
|
|
448
|
+
text = readFileSync(file, "utf8");
|
|
449
|
+
} catch {
|
|
450
|
+
throw new Error(`contract: ${file} cannot be read`);
|
|
451
|
+
}
|
|
452
|
+
const problems = checkContract(
|
|
453
|
+
text,
|
|
454
|
+
Object.keys(readProjectFacts().roles),
|
|
455
|
+
readReportFile,
|
|
456
|
+
flags.includes(APPROVED_FLAG),
|
|
457
|
+
flags.includes(IMPLEMENTED_FLAG),
|
|
458
|
+
);
|
|
459
|
+
if (problems.length > 0) {
|
|
460
|
+
process.stderr.write(`${problems.join("\n")}\n`);
|
|
461
|
+
process.exitCode = 1;
|
|
462
|
+
} else {
|
|
463
|
+
process.stdout.write(`ok ${file}\n`);
|
|
464
|
+
}
|
|
465
|
+
} catch (error) {
|
|
466
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
467
|
+
process.exitCode = 1;
|
|
468
|
+
}
|
|
469
|
+
}
|