architecture-harness 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +14 -4
- package/src/cli/bash-command-guard.mjs +25 -8
- package/src/cli/bash-command-guard.test.mjs +44 -0
- package/src/cli/branch-name.test.mjs +31 -0
- package/src/cli/check.mjs +132 -5
- package/src/cli/commit-msg.mjs +94 -0
- package/src/cli/commit-msg.test.mjs +60 -0
- package/src/cli/doctor.mjs +39 -5
- package/src/cli/doctor.test.mjs +18 -1
- package/src/cli/git-hooks.mjs +67 -0
- package/src/cli/git-hooks.test.mjs +85 -0
- package/src/cli/post-edit-gate.test.mjs +10 -0
- package/src/cli/powershell-command.mjs +3 -0
- package/src/cli/pr-check.mjs +74 -0
- package/src/cli/pr-check.test.mjs +59 -0
- package/src/cli/qc.mjs +8 -6
- package/src/cli/stop-gate.test.mjs +113 -0
- package/src/cli/templates.test.mjs +9 -0
- package/src/cli/work-order-guard.mjs +25 -1
- package/src/config.mjs +48 -7
- package/src/descriptions.mjs +4 -0
- package/src/gates/closed-set-writers.mjs +36 -0
- package/src/gates/closed-set-writers.test.mjs +46 -0
- package/src/gates/config-floor.mjs +23 -8
- package/src/gates/config-floor.test.mjs +10 -0
- package/src/gates/doc-claims.mjs +45 -0
- package/src/gates/doc-claims.test.mjs +33 -0
- package/src/gates/english-source.mjs +1 -1
- package/src/gates/integration-imports.mjs +49 -0
- package/src/gates/integration-imports.test.mjs +35 -0
- package/src/gates/parity.mjs +163 -0
- package/src/gates/parity.test.mjs +108 -0
- package/src/gates/sql-identifiers.mjs +44 -0
- package/src/gates/sql-identifiers.test.mjs +43 -1
- package/src/gates/tenant-predicate.mjs +108 -58
- package/src/gates/tenant-predicate.test.mjs +61 -39
- package/templates/githooks/commit-msg +2 -1
- package/templates/github/workflows/ci.yml +7 -0
- package/src/cli/commit-attribution.mjs +0 -68
- package/src/cli/commit-attribution.test.mjs +0 -47
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// A closed set is a value, and no type asks whether a declared key is ever written. So a key
|
|
2
|
+
// either has a writer or names the mutation it waits for, and the waiting list can only shrink.
|
|
3
|
+
|
|
4
|
+
/** The first group of each match of `pattern` in `source`. */
|
|
5
|
+
export function matchedKeys(source, pattern) {
|
|
6
|
+
return [...source.matchAll(new RegExp(pattern, "g"))].map((match) => match[1]);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {string[]} declared the keys the set's source declares
|
|
11
|
+
* @param {Set<string>} written the keys some writer writes
|
|
12
|
+
* @param {{name: string, source: string, declaredAhead?: Record<string, string>}} set
|
|
13
|
+
* @returns {{path: string, rule: string, detail: string}[]}
|
|
14
|
+
*/
|
|
15
|
+
export function checkClosedSetWriters(declared, written, set) {
|
|
16
|
+
const ahead = set.declaredAhead ?? {};
|
|
17
|
+
const path = set.source;
|
|
18
|
+
const problems = [];
|
|
19
|
+
const push = (rule, detail) => problems.push({ path, rule, detail });
|
|
20
|
+
if (declared.length === 0) {
|
|
21
|
+
push("empty-closed-set", `${set.name}: the source declares no key, so this set checks nothing`);
|
|
22
|
+
return problems;
|
|
23
|
+
}
|
|
24
|
+
for (const key of declared) {
|
|
25
|
+
if (written.has(key) || key in ahead) continue;
|
|
26
|
+
push("unwritten-key", `${key} is declared in ${set.name}, never written, and not listed as waiting for a mutation`);
|
|
27
|
+
}
|
|
28
|
+
for (const key of Object.keys(ahead)) {
|
|
29
|
+
if (!declared.includes(key)) push("stale-declared-ahead", `${key} is listed as waiting but is no longer in ${set.name} — delete the entry`);
|
|
30
|
+
if (written.has(key)) push("stale-declared-ahead", `${key} is listed as waiting but something writes it now — delete the entry`);
|
|
31
|
+
}
|
|
32
|
+
for (const key of written) {
|
|
33
|
+
if (!declared.includes(key)) push("undeclared-write", `${key} is written but not declared in ${set.name}`);
|
|
34
|
+
}
|
|
35
|
+
return problems;
|
|
36
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { checkClosedSetWriters, matchedKeys } from "./closed-set-writers.mjs";
|
|
4
|
+
|
|
5
|
+
const set = {
|
|
6
|
+
name: "AUDIT_ACTIONS",
|
|
7
|
+
source: "audit/types.ts",
|
|
8
|
+
key: '\\{ key: "([a-z_.]+)", category:',
|
|
9
|
+
writer: 'record\\([\\s\\S]{0,400}?action: "([a-z_.]+)"',
|
|
10
|
+
declaredAhead: { "role.rename": "roles are fixed" },
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const declared = matchedKeys('[{ key: "pin.create", category: "pin" }, { key: "role.rename", category: "role" }]', set.key);
|
|
14
|
+
|
|
15
|
+
test("a key and its writer are read by the configured patterns", () => {
|
|
16
|
+
assert.deepEqual(declared, ["pin.create", "role.rename"]);
|
|
17
|
+
assert.deepEqual(matchedKeys('await record(tx, {\n action: "pin.create",\n});', set.writer), ["pin.create"]);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("every key written, or waiting with its reason, passes", () => {
|
|
21
|
+
assert.deepEqual(checkClosedSetWriters(declared, new Set(["pin.create"]), set), []);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("a key with no writer and no reason fails", () => {
|
|
25
|
+
const problems = checkClosedSetWriters(declared, new Set(), set);
|
|
26
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["unwritten-key"]);
|
|
27
|
+
assert.equal(problems[0].path, "audit/types.ts");
|
|
28
|
+
assert.match(problems[0].detail, /pin\.create is declared in AUDIT_ACTIONS/);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("a waiting entry that is written now, or no longer declared, fails", () => {
|
|
32
|
+
const written = checkClosedSetWriters(declared, new Set(["pin.create", "role.rename"]), set);
|
|
33
|
+
assert.deepEqual(written.map((problem) => problem.rule), ["stale-declared-ahead"]);
|
|
34
|
+
const gone = checkClosedSetWriters(["pin.create"], new Set(["pin.create"]), set);
|
|
35
|
+
assert.deepEqual(gone.map((problem) => problem.rule), ["stale-declared-ahead"]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("a writer that writes a key outside the set fails", () => {
|
|
39
|
+
const problems = checkClosedSetWriters(declared, new Set(["pin.create", "pin.explode"]), set);
|
|
40
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["undeclared-write"]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("a set whose source declares no key fails, since it checks nothing", () => {
|
|
44
|
+
const problems = checkClosedSetWriters([], new Set(), { ...set, declaredAhead: {} });
|
|
45
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["empty-closed-set"]);
|
|
46
|
+
});
|
|
@@ -13,6 +13,22 @@ export function switchedOff(shipped, configured) {
|
|
|
13
13
|
.sort();
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
/** @returns each hook whose required calls lack one the kit ships. */
|
|
17
|
+
export function weakenedHooks(shipped = {}, configured = {}) {
|
|
18
|
+
return Object.keys(shipped)
|
|
19
|
+
.filter((name) => shipped[name].some((call) => !(configured[name] ?? []).includes(call)))
|
|
20
|
+
.sort();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Every shipped check this repository has switched off, by kind. A hook's exemption key is `hooks.<name>`. */
|
|
24
|
+
function offByKind(shipped, configured) {
|
|
25
|
+
return {
|
|
26
|
+
gates: switchedOff(shipped.gates ?? {}, configured.gates ?? {}),
|
|
27
|
+
rules: switchedOff(shipped.rules ?? {}, configured.rules ?? {}),
|
|
28
|
+
hooks: weakenedHooks(shipped.hooks?.required, configured.hooks?.required).map((name) => `hooks.${name}`),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
16
32
|
/**
|
|
17
33
|
* @param {{gates: object, rules: object}} shipped the kit's own defaults
|
|
18
34
|
* @param {{gates: object, rules: object}} configured this repository's resolved config
|
|
@@ -23,15 +39,17 @@ export function checkConfigFloor(shipped, configured, options = {}) {
|
|
|
23
39
|
const pattern = options.pattern ?? /^[A-Z]{2,5}-\d{3,4}$/;
|
|
24
40
|
const problems = [];
|
|
25
41
|
|
|
26
|
-
|
|
27
|
-
|
|
42
|
+
const off = offByKind(shipped, configured);
|
|
43
|
+
for (const kind of ["gates", "rules", "hooks"]) {
|
|
44
|
+
for (const name of off[kind]) {
|
|
45
|
+
const shown = kind === "hooks" ? name : `${kind}.${name}`;
|
|
28
46
|
const cited = exemptions[name];
|
|
29
47
|
if (cited === undefined) {
|
|
30
48
|
problems.push({
|
|
31
49
|
path: "qc.config.json",
|
|
32
50
|
rule: "unexempted-opt-out",
|
|
33
51
|
detail:
|
|
34
|
-
`${
|
|
52
|
+
`${shown} is switched off, and the kit ships it on. Name the decision that says ` +
|
|
35
53
|
`why under floor.exemptions, or "${OFF_BY_DESIGN}" when the rule cannot apply to this repository's shape.`,
|
|
36
54
|
});
|
|
37
55
|
continue;
|
|
@@ -46,12 +64,9 @@ export function checkConfigFloor(shipped, configured, options = {}) {
|
|
|
46
64
|
}
|
|
47
65
|
}
|
|
48
66
|
// An exemption for something that is not off is a stale reason nobody will notice going wrong.
|
|
49
|
-
const
|
|
50
|
-
...switchedOff(shipped.gates ?? {}, configured.gates ?? {}),
|
|
51
|
-
...switchedOff(shipped.rules ?? {}, configured.rules ?? {}),
|
|
52
|
-
]);
|
|
67
|
+
const inForce = new Set([...off.gates, ...off.rules, ...off.hooks]);
|
|
53
68
|
for (const name of Object.keys(exemptions).sort()) {
|
|
54
|
-
if (!
|
|
69
|
+
if (!inForce.has(name)) {
|
|
55
70
|
problems.push({
|
|
56
71
|
path: "qc.config.json",
|
|
57
72
|
rule: "stale-exemption",
|
|
@@ -50,3 +50,13 @@ test("a check the kit does not ship on is not held to the floor", () => {
|
|
|
50
50
|
test("cited ids are collected for the citations gate, and off-by-design is not one", () => {
|
|
51
51
|
assert.deepEqual(exemptionIds({ a: "QC-011", b: "off-by-design", c: "ADR-0031" }), ["QC-011", "ADR-0031"]);
|
|
52
52
|
});
|
|
53
|
+
|
|
54
|
+
test("a weakened hooks.required entry is an opt-out, held to the same floor", () => {
|
|
55
|
+
const shipped = { gates: {}, rules: {}, hooks: { required: { "pre-commit": ["qc work-order-check"], "pre-push": ["qc check"] } } };
|
|
56
|
+
const weakened = { gates: {}, rules: {}, hooks: { required: { "pre-commit": ["qc work-order-check"], "pre-push": [] } } };
|
|
57
|
+
const problems = checkConfigFloor(shipped, weakened);
|
|
58
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["unexempted-opt-out"]);
|
|
59
|
+
assert.match(problems[0].detail, /^hooks\.pre-push is switched off/);
|
|
60
|
+
assert.deepEqual(checkConfigFloor(shipped, weakened, { exemptions: { "hooks.pre-push": "off-by-design" } }), []);
|
|
61
|
+
assert.deepEqual(checkConfigFloor(shipped, shipped, { exemptions: { "hooks.pre-push": "off-by-design" } }).map((p) => p.rule), ["stale-exemption"]);
|
|
62
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// A true claim in a document can go stale and still read as current. A fenced state-claim
|
|
2
|
+
// block pairs a file with its line count, so the claim is checked instead of trusted.
|
|
3
|
+
|
|
4
|
+
const CLAIM_BLOCK = /```state-claim\r?\n([\s\S]*?)```/g;
|
|
5
|
+
|
|
6
|
+
/** @returns {{file: string, lines: number}[]} each block that names both a file and a count */
|
|
7
|
+
export function parseDocClaims(contents) {
|
|
8
|
+
const claims = [];
|
|
9
|
+
for (const [, block] of contents.matchAll(CLAIM_BLOCK)) {
|
|
10
|
+
const file = /file:\s*(\S+)/.exec(block)?.[1];
|
|
11
|
+
const lines = /lines:\s*(\d+)/.exec(block)?.[1];
|
|
12
|
+
if (file && lines) claims.push({ file, lines: Number(lines) });
|
|
13
|
+
}
|
|
14
|
+
return claims;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Newline-terminated lines, as `wc -l` counts them, with CRLF read as LF. */
|
|
18
|
+
export function lineCount(contents) {
|
|
19
|
+
const normalized = contents.replace(/\r\n/g, "\n");
|
|
20
|
+
const parts = normalized.split("\n");
|
|
21
|
+
return normalized.endsWith("\n") ? parts.length - 1 : parts.length;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} doc the document that makes the claims
|
|
26
|
+
* @param {{file: string, lines: number}[]} claims
|
|
27
|
+
* @param {Map<string, string | null>} targets each claimed file's contents, null when it is missing
|
|
28
|
+
* @returns {{path: string, rule: string, detail: string}[]}
|
|
29
|
+
*/
|
|
30
|
+
export function checkDocClaims(doc, claims, targets) {
|
|
31
|
+
const problems = [];
|
|
32
|
+
for (const claim of claims) {
|
|
33
|
+
const contents = targets.get(claim.file) ?? null;
|
|
34
|
+
const actual = contents === null ? null : lineCount(contents);
|
|
35
|
+
if (actual === claim.lines) continue;
|
|
36
|
+
problems.push({
|
|
37
|
+
path: claim.file,
|
|
38
|
+
rule: "stale-doc-claim",
|
|
39
|
+
detail: actual === null
|
|
40
|
+
? `${doc} claims ${claim.lines} lines; the file no longer exists`
|
|
41
|
+
: `${doc} claims ${claim.lines} lines; the file now has ${actual}`,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return problems;
|
|
45
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { checkDocClaims, lineCount, parseDocClaims } from "./doc-claims.mjs";
|
|
4
|
+
|
|
5
|
+
const doc = "# State\n\n```state-claim\nfile: src/big.ts\nlines: 3\n```\n\n```state-claim\r\nfile: src/gone.ts\r\nlines: 9\r\n```\n";
|
|
6
|
+
|
|
7
|
+
test("each fenced block yields its file and line count, and a block missing one is skipped", () => {
|
|
8
|
+
assert.deepEqual(parseDocClaims(doc), [{ file: "src/big.ts", lines: 3 }, { file: "src/gone.ts", lines: 9 }]);
|
|
9
|
+
assert.deepEqual(parseDocClaims("```state-claim\nfile: a.ts\n```\n"), []);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("lines are counted the way wc -l counts them, for LF and CRLF alike", () => {
|
|
13
|
+
assert.equal(lineCount("a\nb\nc\n"), 3);
|
|
14
|
+
assert.equal(lineCount("a\r\nb\r\nc\r\n"), 3);
|
|
15
|
+
assert.equal(lineCount("a\nb\nc"), 3);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("a claim that is still true passes", () => {
|
|
19
|
+
const found = checkDocClaims("STATE.md", [{ file: "src/big.ts", lines: 3 }], new Map([["src/big.ts", "1\n2\n3\n"]]));
|
|
20
|
+
assert.deepEqual(found, []);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("a claim whose count changed, or whose file is gone, fails", () => {
|
|
24
|
+
const found = checkDocClaims(
|
|
25
|
+
"STATE.md",
|
|
26
|
+
parseDocClaims(doc),
|
|
27
|
+
new Map([["src/big.ts", "1\n2\n3\n4\n"], ["src/gone.ts", null]]),
|
|
28
|
+
);
|
|
29
|
+
assert.deepEqual(found.map((problem) => problem.rule), ["stale-doc-claim", "stale-doc-claim"]);
|
|
30
|
+
assert.equal(found[0].path, "src/big.ts");
|
|
31
|
+
assert.equal(found[0].detail, "STATE.md claims 3 lines; the file now has 4");
|
|
32
|
+
assert.equal(found[1].detail, "STATE.md claims 9 lines; the file no longer exists");
|
|
33
|
+
});
|
|
@@ -15,7 +15,7 @@ const SCRIPTS = {
|
|
|
15
15
|
fullwidth: "\\uff00-\\uffef",
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
function scriptPattern(scripts) {
|
|
18
|
+
export function scriptPattern(scripts) {
|
|
19
19
|
const ranges = scripts.map((name) => SCRIPTS[name] ?? name).join("");
|
|
20
20
|
if (ranges === "") throw new Error("language.scripts must name at least one script range");
|
|
21
21
|
return new RegExp(`[${ranges}]`);
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// A test named for integration has to reach the thing it integrates with. One that imports
|
|
2
|
+
// only a test runner and the kernel builds its own subject and asserts about it, so it passes
|
|
3
|
+
// for every failure a user would meet.
|
|
4
|
+
|
|
5
|
+
/** Each `import ... from "<specifier>"`, so a name in a comment or a string is not one. */
|
|
6
|
+
const IMPORT_SPECIFIER = /^\s*import\s[\s\S]*?from\s+"([^"]+)"/gm;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A test inside a product root reaches its neighbours with `./`, so any relative import counts.
|
|
10
|
+
* One outside has to name the product root it climbs into, or import a product package.
|
|
11
|
+
* @param {string} source
|
|
12
|
+
* @param {string} file the repository-relative path
|
|
13
|
+
* @param {{productRoots: string[], productPackages: string[]}} options
|
|
14
|
+
* @returns {number}
|
|
15
|
+
*/
|
|
16
|
+
export function productImportCount(source, file, { productRoots, productPackages }) {
|
|
17
|
+
const inProduct = productRoots.some((root) => file.startsWith(`${root}/`));
|
|
18
|
+
const productPackage = productPackages.length > 0 ? new RegExp(productPackages.join("|")) : null;
|
|
19
|
+
let count = 0;
|
|
20
|
+
for (const [, specifier] of source.matchAll(IMPORT_SPECIFIER)) {
|
|
21
|
+
if (productPackage?.test(specifier)) count += 1;
|
|
22
|
+
else if (!specifier.startsWith(".")) continue;
|
|
23
|
+
else if (inProduct) count += 1;
|
|
24
|
+
else if (productRoots.some((root) => specifier.split("/").includes(root))) count += 1;
|
|
25
|
+
}
|
|
26
|
+
return count;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @param {{path: string, contents: string}[]} subjects every file the subject pattern matched
|
|
31
|
+
* @param {{productRoots: string[], productPackages: string[]}} options
|
|
32
|
+
* @returns {{path: string, rule: string, detail: string}[]}
|
|
33
|
+
*/
|
|
34
|
+
export function checkIntegrationImports(subjects, options) {
|
|
35
|
+
if (subjects.length === 0) {
|
|
36
|
+
return [{
|
|
37
|
+
path: "qc.config.json",
|
|
38
|
+
rule: "no-integration-tests",
|
|
39
|
+
detail: "no file matches integrationImports.subject, so this gate is checking nothing",
|
|
40
|
+
}];
|
|
41
|
+
}
|
|
42
|
+
return subjects
|
|
43
|
+
.filter(({ path, contents }) => productImportCount(contents, path, options) === 0)
|
|
44
|
+
.map(({ path }) => ({
|
|
45
|
+
path,
|
|
46
|
+
rule: "integration-without-product",
|
|
47
|
+
detail: "imports no product module, so it can only assert about subjects it declares itself",
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { checkIntegrationImports, productImportCount } from "./integration-imports.mjs";
|
|
4
|
+
|
|
5
|
+
const options = { productRoots: ["backend", "frontend"], productPackages: ["^@app/(?:domain|contracts)(?:/|$)"] };
|
|
6
|
+
|
|
7
|
+
test("an integration test that imports only the runner and the kernel fails", () => {
|
|
8
|
+
const contents = 'import { test } from "vitest";\nimport { ok } from "@app/kernel";\ntest("x", () => {});\n';
|
|
9
|
+
const problems = checkIntegrationImports([{ path: "packages/journeys/src/flow.interop.test.ts", contents }], options);
|
|
10
|
+
assert.equal(problems.length, 1);
|
|
11
|
+
assert.equal(problems[0].rule, "integration-without-product");
|
|
12
|
+
assert.equal(problems[0].path, "packages/journeys/src/flow.interop.test.ts");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("a product package, a climb into a product root, or any relative import inside one counts", () => {
|
|
16
|
+
assert.equal(productImportCount('import { Pin } from "@app/domain/pin";', "packages/x/a.live.test.ts", options), 1);
|
|
17
|
+
assert.equal(productImportCount('import { app } from "../../backend/src/main.js";', "packages/x/a.live.test.ts", options), 1);
|
|
18
|
+
assert.equal(productImportCount('import { route } from "./route.js";', "backend/src/pins/a.live.test.ts", options), 1);
|
|
19
|
+
assert.equal(productImportCount('import { helper } from "./helper.js";', "packages/x/a.live.test.ts", options), 0);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("a product root named in a comment or a string is not an import", () => {
|
|
23
|
+
const contents = '// see backend/src/main.ts\nimport { test } from "vitest";\nconst path = "@app/domain";\n';
|
|
24
|
+
assert.equal(checkIntegrationImports([{ path: "packages/x/a.interop.test.ts", contents }], options).length, 1);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("with no integration test at all the gate says it checks nothing", () => {
|
|
28
|
+
const problems = checkIntegrationImports([], options);
|
|
29
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["no-integration-tests"]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("a test that reaches the product passes", () => {
|
|
33
|
+
const contents = 'import { test } from "vitest";\nimport { createPin } from "@app/domain";\n';
|
|
34
|
+
assert.deepEqual(checkIntegrationImports([{ path: "packages/x/a.interop.test.ts", contents }], options), []);
|
|
35
|
+
});
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// The two surfaces carry one architecture: a feature exists on both under one name, and a
|
|
2
|
+
// server slice has a client slice of the same stem. A divergence is legal only when the
|
|
3
|
+
// registry names it with its reason, and an entry that stops being true fails like a gap.
|
|
4
|
+
// A slice that only exists to satisfy this pairing is refused too, or the pairing proves nothing.
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {{
|
|
8
|
+
* server: {name: string, slices: string[], routes: string[]}[],
|
|
9
|
+
* client: {name: string, slices: string[]}[],
|
|
10
|
+
* registry: {backendOnlyFeatures?: {name: string}[], frontendOnlyFeatures?: {name: string}[], slices?: Record<string, {stem: string, why?: string}[]>},
|
|
11
|
+
* }} surfaces
|
|
12
|
+
* @param {{server: string, client: string, registry: string, trigger?: string}} roots
|
|
13
|
+
* @returns {{path: string, rule: string, detail: string}[]}
|
|
14
|
+
*/
|
|
15
|
+
export function checkParity({ server, client, registry }, roots) {
|
|
16
|
+
const where = roots.registry;
|
|
17
|
+
const trigger = roots.trigger ?? "trigger.ts";
|
|
18
|
+
const backend = server.map((feature) => feature.name);
|
|
19
|
+
const frontend = client.map((feature) => feature.name);
|
|
20
|
+
const backendOnly = new Set((registry.backendOnlyFeatures ?? []).map((entry) => entry.name));
|
|
21
|
+
const frontendOnly = new Set((registry.frontendOnlyFeatures ?? []).map((entry) => entry.name));
|
|
22
|
+
const problems = [];
|
|
23
|
+
const push = (path, rule, detail) => problems.push({ path, rule, detail });
|
|
24
|
+
|
|
25
|
+
for (const name of backend) {
|
|
26
|
+
if (frontend.includes(name) || backendOnly.has(name)) continue;
|
|
27
|
+
push(`${roots.server}/${name}`, "one-sided-feature", `${name}: served by the api with no client feature, and not named in ${where}`);
|
|
28
|
+
}
|
|
29
|
+
for (const name of frontend) {
|
|
30
|
+
if (backend.includes(name) || frontendOnly.has(name)) continue;
|
|
31
|
+
push(`${roots.client}/${name}`, "one-sided-feature", `${name}: a client feature the api does not serve, and not named in ${where}`);
|
|
32
|
+
}
|
|
33
|
+
for (const name of [...backendOnly, ...frontendOnly]) {
|
|
34
|
+
if (backend.includes(name) && frontend.includes(name)) {
|
|
35
|
+
push(where, "stale-divergence", `${name}: listed as one-sided in ${where}, but both surfaces have it — delete the entry`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const drawnBy = new Map(client.map((feature) => [feature.name, new Set(feature.slices)]));
|
|
40
|
+
for (const feature of server.filter(({ name }) => drawnBy.has(name))) {
|
|
41
|
+
const { name } = feature;
|
|
42
|
+
const served = new Set(feature.slices);
|
|
43
|
+
const drawn = drawnBy.get(name);
|
|
44
|
+
const routes = [...new Set(feature.routes)].sort();
|
|
45
|
+
const excuses = new Map((registry.slices?.[name] ?? []).map((entry) => [entry.stem, entry.why ?? ""]));
|
|
46
|
+
const serverPath = `${roots.server}/${name}`;
|
|
47
|
+
const clientPath = `${roots.client}/${name}`;
|
|
48
|
+
|
|
49
|
+
for (const stem of routes) {
|
|
50
|
+
if (!served.has(stem)) push(serverPath, "one-sided-slice", `${name}/${stem}: route in ${trigger} has no backend slice implementation`);
|
|
51
|
+
}
|
|
52
|
+
for (const stem of routes) {
|
|
53
|
+
if (!drawn.has(stem) && !excuses.has(stem)) push(clientPath, "one-sided-slice", `${name}/${stem}: backend route has no frontend slice`);
|
|
54
|
+
}
|
|
55
|
+
for (const stem of [...served].sort()) {
|
|
56
|
+
if (drawn.has(stem) || excuses.has(stem)) continue;
|
|
57
|
+
push(clientPath, "one-sided-slice", `${name}/${stem}: a backend slice with no frontend slice of that name`);
|
|
58
|
+
}
|
|
59
|
+
for (const stem of [...drawn].sort()) {
|
|
60
|
+
if (served.has(stem) || excuses.has(stem)) continue;
|
|
61
|
+
push(serverPath, "one-sided-slice", `${name}/${stem}: a frontend slice with no backend slice/route of that name`);
|
|
62
|
+
}
|
|
63
|
+
for (const [stem, why] of excuses) {
|
|
64
|
+
const stale = (detail) => push(where, "stale-divergence", `${name}/${stem}: ${detail}`);
|
|
65
|
+
if (served.has(stem) && drawn.has(stem)) stale(`excused in ${where}, but both surfaces have it — delete the entry`);
|
|
66
|
+
else if (!served.has(stem) && !drawn.has(stem)) stale(`excused in ${where}, but neither surface has a slice of that name — delete the entry`);
|
|
67
|
+
else if (why.startsWith("Client-only") && !drawn.has(stem)) stale(`excused as client-only in ${where}, but no frontend slice exists`);
|
|
68
|
+
else if (why.startsWith("Backend-only") && !served.has(stem)) stale(`excused as backend-only in ${where}, but no backend slice exists`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return problems;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// A string can hold "//" or a brace, so a comment is removed only outside a string.
|
|
75
|
+
const COMMENT_OR_STRING = /("(?:\\.|[^"\\\n])*"|'(?:\\.|[^'\\\n])*'|`(?:\\.|[^`\\])*`)|\/\*[\s\S]*?\*\/|\/\/[^\n]*/g;
|
|
76
|
+
const NO_OP_STATEMENT = /^(?:void 0|return|return undefined|return void 0)$/;
|
|
77
|
+
|
|
78
|
+
/** The index just past the bracket that closes the one at `open`, skipping strings. */
|
|
79
|
+
function closing(source, open) {
|
|
80
|
+
const pair = { "(": ")", "{": "}", "<": ">" }[source[open]];
|
|
81
|
+
let depth = 0;
|
|
82
|
+
for (let i = open; i < source.length; i += 1) {
|
|
83
|
+
const ch = source[i];
|
|
84
|
+
if (ch === '"' || ch === "'" || ch === "`") {
|
|
85
|
+
const end = source.indexOf(ch, i + 1);
|
|
86
|
+
if (end === -1) return source.length;
|
|
87
|
+
i = end;
|
|
88
|
+
} else if (ch === source[open]) depth += 1;
|
|
89
|
+
else if (ch === pair && (depth -= 1) === 0) return i + 1;
|
|
90
|
+
}
|
|
91
|
+
return source.length;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function isNoOpBody(body) {
|
|
95
|
+
return body
|
|
96
|
+
.split(/[;\n]/)
|
|
97
|
+
.map((statement) => statement.trim())
|
|
98
|
+
.filter(Boolean)
|
|
99
|
+
.every((statement) => NO_OP_STATEMENT.test(statement));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** True for a function whose parameters open at `open` and whose body does nothing. */
|
|
103
|
+
function noOpFunctionAt(source, open, { arrow }) {
|
|
104
|
+
const after = source.slice(closing(source, open));
|
|
105
|
+
if (arrow) {
|
|
106
|
+
const head = /^\s*(?::[^=]*?)?=>\s*/.exec(after);
|
|
107
|
+
if (head === null) return false;
|
|
108
|
+
const rest = after.slice(head[0].length);
|
|
109
|
+
if (rest.startsWith("{")) return isNoOpBody(rest.slice(1, closing(rest, 0) - 1));
|
|
110
|
+
return /^(?:void 0|undefined)\s*(?:;|$|\n)/.test(rest);
|
|
111
|
+
}
|
|
112
|
+
const brace = after.indexOf("{");
|
|
113
|
+
return brace !== -1 && isNoOpBody(after.slice(brace + 1, closing(after, brace) - 1));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Each value export: true when it is a function that does nothing. A type export carries no
|
|
118
|
+
* behavior and is skipped; anything else, a constant, a class or a re-export, is real.
|
|
119
|
+
*/
|
|
120
|
+
function valueExports(source) {
|
|
121
|
+
const found = [];
|
|
122
|
+
for (const match of source.matchAll(/(?:^|[;\n])\s*export\s+/g)) {
|
|
123
|
+
const rest = source.slice(match.index + match[0].length);
|
|
124
|
+
if (/^(?:type|interface|declare)\b/.test(rest)) continue;
|
|
125
|
+
const declared = /^(?:default\s+)?(?:async\s+)?function\b\s*\*?\s*[\w$]*\s*(?:<[^>]*>\s*)?(?=\()/.exec(rest);
|
|
126
|
+
if (declared) {
|
|
127
|
+
found.push(noOpFunctionAt(rest, declared[0].length, { arrow: false }));
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const bound = /^(?:const|let|var)\s+[\w$]+\s*(?::[^=]+)?=\s*(?:async\s+)?/.exec(rest);
|
|
131
|
+
if (bound) {
|
|
132
|
+
const value = rest.slice(bound[0].length);
|
|
133
|
+
const expression = /^function\b\s*[\w$]*\s*(?=\()/.exec(value);
|
|
134
|
+
if (expression) found.push(noOpFunctionAt(value, expression[0].length, { arrow: false }));
|
|
135
|
+
else if (value.startsWith("(")) found.push(noOpFunctionAt(value, 0, { arrow: true }));
|
|
136
|
+
else if (/^[\w$]+\s*=>/.test(value)) found.push(noOpFunctionAt(`(${value.replace(/^([\w$]+)/, "$1)")}`, 0, { arrow: true }));
|
|
137
|
+
else found.push(false);
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
found.push(false);
|
|
141
|
+
}
|
|
142
|
+
return found;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* A slice file whose every value export is a function that does nothing: it pairs a name and
|
|
147
|
+
* carries no behavior. A file with no value export is left to the empty-block check.
|
|
148
|
+
* @param {{path: string, contents: string}[]} files
|
|
149
|
+
* @returns {{path: string, rule: string, detail: string}[]}
|
|
150
|
+
*/
|
|
151
|
+
export function placeholderSlices(files) {
|
|
152
|
+
const problems = [];
|
|
153
|
+
for (const { path, contents } of files) {
|
|
154
|
+
const exports = valueExports(contents.replace(COMMENT_OR_STRING, (whole, string) => string ?? ""));
|
|
155
|
+
if (exports.length === 0 || !exports.every(Boolean)) continue;
|
|
156
|
+
problems.push({
|
|
157
|
+
path,
|
|
158
|
+
rule: "placeholder-slice",
|
|
159
|
+
detail: "every export is a function that does nothing, so the slice pairs a name and carries no behavior — build the slice, or name the divergence in the parity registry",
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return problems;
|
|
163
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { checkParity, placeholderSlices } from "./parity.mjs";
|
|
4
|
+
|
|
5
|
+
const roots = { server: "backend/src/features", client: "frontend/src/features", registry: "contracts/parity.json" };
|
|
6
|
+
|
|
7
|
+
function run({ server = [], client = [], registry = {} }) {
|
|
8
|
+
return checkParity({ server, client, registry }, roots);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const rules = (problems) => problems.map((problem) => problem.rule);
|
|
12
|
+
|
|
13
|
+
test("two surfaces that carry the same features and slices pass", () => {
|
|
14
|
+
const server = [{ name: "pins", slices: ["create-pin"], routes: ["create-pin"] }];
|
|
15
|
+
const client = [{ name: "pins", slices: ["create-pin"] }];
|
|
16
|
+
assert.deepEqual(run({ server, client }), []);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("a feature on one surface only fails in both directions", () => {
|
|
20
|
+
const problems = run({ server: [{ name: "billing", slices: [], routes: [] }], client: [{ name: "outbox", slices: [] }] });
|
|
21
|
+
assert.deepEqual(rules(problems), ["one-sided-feature", "one-sided-feature"]);
|
|
22
|
+
assert.equal(problems[0].path, "backend/src/features/billing");
|
|
23
|
+
assert.equal(problems[1].path, "frontend/src/features/outbox");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("a one-sided feature the registry names passes, and an entry both surfaces now have fails", () => {
|
|
27
|
+
const registry = {
|
|
28
|
+
backendOnlyFeatures: [{ name: "billing", why: "server only" }],
|
|
29
|
+
frontendOnlyFeatures: [{ name: "pins", why: "stale" }],
|
|
30
|
+
};
|
|
31
|
+
const server = [{ name: "billing", slices: [], routes: [] }, { name: "pins", slices: [], routes: [] }];
|
|
32
|
+
const problems = run({ server, client: [{ name: "pins", slices: [] }], registry });
|
|
33
|
+
assert.deepEqual(rules(problems), ["stale-divergence"]);
|
|
34
|
+
assert.equal(problems[0].path, "contracts/parity.json");
|
|
35
|
+
assert.match(problems[0].detail, /pins: listed as one-sided/);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("a slice or a route with no counterpart fails, in both directions", () => {
|
|
39
|
+
const server = [{ name: "pins", slices: ["create-pin", "move-pin"], routes: ["create-pin", "drop-pin"] }];
|
|
40
|
+
const client = [{ name: "pins", slices: ["create-pin", "draw-pin"] }];
|
|
41
|
+
const details = run({ server, client }).map((problem) => problem.detail);
|
|
42
|
+
assert.deepEqual(details.sort(), [
|
|
43
|
+
"pins/draw-pin: a frontend slice with no backend slice/route of that name",
|
|
44
|
+
"pins/drop-pin: backend route has no frontend slice",
|
|
45
|
+
"pins/drop-pin: route in trigger.ts has no backend slice implementation",
|
|
46
|
+
"pins/move-pin: a backend slice with no frontend slice of that name",
|
|
47
|
+
]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("a registry excuse covers a one-sided slice, and fails once it stops being true", () => {
|
|
51
|
+
const server = [{ name: "pins", slices: ["render", "both"], routes: [] }];
|
|
52
|
+
const client = [{ name: "pins", slices: ["both", "local"] }];
|
|
53
|
+
const registry = {
|
|
54
|
+
slices: {
|
|
55
|
+
pins: [
|
|
56
|
+
{ stem: "render", why: "Backend-only: a worker route." },
|
|
57
|
+
{ stem: "local", why: "Client-only: a draft." },
|
|
58
|
+
{ stem: "both", why: "Backend-only: stale." },
|
|
59
|
+
{ stem: "gone", why: "Backend-only: deleted." },
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
const details = run({ server, client, registry }).map((problem) => problem.detail);
|
|
64
|
+
assert.deepEqual(details, [
|
|
65
|
+
"pins/both: excused in contracts/parity.json, but both surfaces have it — delete the entry",
|
|
66
|
+
"pins/gone: excused in contracts/parity.json, but neither surface has a slice of that name — delete the entry",
|
|
67
|
+
]);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("an excuse whose reason names the wrong side fails", () => {
|
|
71
|
+
const server = [{ name: "pins", slices: ["render"], routes: [] }];
|
|
72
|
+
const client = [{ name: "pins", slices: [] }];
|
|
73
|
+
const registry = { slices: { pins: [{ stem: "render", why: "Client-only: wrong side." }] } };
|
|
74
|
+
const problems = run({ server, client, registry });
|
|
75
|
+
assert.deepEqual(rules(problems), ["stale-divergence"]);
|
|
76
|
+
assert.match(problems[0].detail, /excused as client-only/);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const slice = (contents, path = "frontend/src/features/tasks/slices/get-task.tsx") => [{ path, contents }];
|
|
80
|
+
|
|
81
|
+
test("a slice whose only export is a no-op fails as a placeholder, whatever its name", () => {
|
|
82
|
+
const problems = placeholderSlices(slice("// placeholder for parity\nexport function PLACEHOLDER(): void { void 0; }"));
|
|
83
|
+
assert.equal(problems.length, 1);
|
|
84
|
+
assert.equal(problems[0].rule, "placeholder-slice");
|
|
85
|
+
assert.equal(problems[0].path, "frontend/src/features/tasks/slices/get-task.tsx");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("an empty body, a bare return, and return undefined are no-ops too", () => {
|
|
89
|
+
for (const body of ["", "return;", "return undefined;", "void 0"]) {
|
|
90
|
+
assert.equal(placeholderSlices(slice(`export function stub() { ${body} }`)).length, 1, body);
|
|
91
|
+
}
|
|
92
|
+
assert.equal(placeholderSlices(slice("export const stub = () => {};")).length, 1);
|
|
93
|
+
assert.equal(placeholderSlices(slice("export const stub = (): void => void 0;")).length, 1);
|
|
94
|
+
assert.equal(placeholderSlices(slice("export async function stub(a: string): Promise<void> {\n return;\n}")).length, 1);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("a slice with one real export passes, even beside a no-op", () => {
|
|
98
|
+
const real = "export function GetTask(props: Props) {\n return <Task id={props.id} />;\n}";
|
|
99
|
+
assert.deepEqual(placeholderSlices(slice(real)), []);
|
|
100
|
+
assert.deepEqual(placeholderSlices(slice(`${real}\nexport function noop(): void { void 0; }`)), []);
|
|
101
|
+
assert.deepEqual(placeholderSlices(slice("export const LIMIT = 20;\nexport function noop() {}")), []);
|
|
102
|
+
assert.deepEqual(placeholderSlices(slice("export { getTask } from './get';\nexport function noop() {}")), []);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("a file with no value export is not a placeholder, and a type export does not make one real", () => {
|
|
106
|
+
assert.deepEqual(placeholderSlices(slice("const local = 1;\n")), []);
|
|
107
|
+
assert.equal(placeholderSlices(slice("export type Row = { id: string };\nexport function noop() {}")).length, 1);
|
|
108
|
+
});
|
|
@@ -261,3 +261,47 @@ export function checkSqlIdentifiers(tables, resources, owners = new Map()) {
|
|
|
261
261
|
}
|
|
262
262
|
return problems;
|
|
263
263
|
}
|
|
264
|
+
|
|
265
|
+
// The other direction. A schema declaration with no migration behind it typechecks, passes every
|
|
266
|
+
// statement check, and fails at runtime. This is the text-level half: it cannot see a type or a
|
|
267
|
+
// default that differs, and a column a later migration dropped still matches the one that added it.
|
|
268
|
+
const SCHEMA_COLUMN = /\b(?:uuid|text|integer|bigint|boolean|jsonb|timestamp|numeric|real|doublePrecision|date|smallint)\(\s*"([a-z0-9_]+)"/g;
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Each table a schema file declares, with the columns under it. A column belongs to the table
|
|
272
|
+
* whose factory call precedes it: one table, then its columns.
|
|
273
|
+
* @param {string} source
|
|
274
|
+
* @param {{tableFactory?: string}} [options]
|
|
275
|
+
* @returns {{table: string, columns: string[]}[]}
|
|
276
|
+
*/
|
|
277
|
+
export function schemaTables(source, { tableFactory = "pgTable" } = {}) {
|
|
278
|
+
const factory = tableFactory.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
279
|
+
const starts = [...source.matchAll(new RegExp(`${factory}\\(\\s*"([a-z0-9_]+)"`, "g"))].map((match) => ({ table: match[1], at: match.index }));
|
|
280
|
+
return starts.map((start, index) => {
|
|
281
|
+
const body = source.slice(start.at, starts[index + 1]?.at ?? source.length);
|
|
282
|
+
return { table: start.table, columns: [...new Set([...body.matchAll(SCHEMA_COLUMN)].map((match) => match[1]))] };
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Every name the migrations write down. A name anywhere in the SQL is enough: the question is whether it was ever written. */
|
|
287
|
+
export function migrationNames(sources) {
|
|
288
|
+
const names = new Set();
|
|
289
|
+
for (const sql of sources) for (const [name] of sql.matchAll(/[a-z0-9_]+/g)) names.add(name);
|
|
290
|
+
return names;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* @param {{path: string, table: string, columns: string[]}[]} declared
|
|
295
|
+
* @param {Set<string>} names
|
|
296
|
+
* @returns {{path: string, rule: string, detail: string}[]}
|
|
297
|
+
*/
|
|
298
|
+
export function checkSchemaInMigrations(declared, names) {
|
|
299
|
+
const problems = [];
|
|
300
|
+
for (const { path, table, columns } of declared) {
|
|
301
|
+
const missing = names.has(table) ? columns.filter((column) => !names.has(column)).map((column) => `${table}.${column}`) : [`table ${table}`];
|
|
302
|
+
for (const name of missing) {
|
|
303
|
+
problems.push({ path, rule: "schema-without-migration", detail: `${name} is declared here and named by no migration` });
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return problems;
|
|
307
|
+
}
|