architecture-harness 0.4.2 → 0.6.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 +1 -1
- package/src/cli/check.mjs +32 -6
- package/src/cli/cli.test.mjs +44 -0
- package/src/cli/lease-file.mjs +33 -0
- package/src/cli/lease.mjs +136 -0
- package/src/cli/lease.test.mjs +140 -0
- package/src/cli/post-edit-gate.test.mjs +75 -0
- package/src/cli/qc.mjs +22 -10
- package/src/cli/run-dispatch.mjs +52 -0
- package/src/cli/run.mjs +75 -0
- package/src/cli/run.test.mjs +71 -0
- package/src/cli/work-order-guard.mjs +0 -0
- package/src/cli/work-order-guard.test.mjs +127 -2
- package/src/cli/worktree-isolation.mjs +48 -14
- package/src/cli/worktree-isolation.test.mjs +57 -0
- package/src/config.mjs +34 -0
- package/src/descriptions.mjs +2 -1
- package/src/gates/eight-blocks.mjs +5 -1
- package/src/gates/eight-blocks.test.mjs +21 -0
- package/src/gates/feature-cli.mjs +198 -0
- package/src/gates/feature-cli.test.mjs +172 -0
- package/src/gates/test-mirror.mjs +57 -37
- package/src/gates/test-mirror.test.mjs +67 -4
- package/templates/githooks/pre-push +18 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// A feature is reachable two ways, and only one of them is usually built. Routes need a server,
|
|
2
|
+
// a session and a client; that is the path a person exercises by hand and a suite mocks away.
|
|
3
|
+
// This names the second one: a command block that drives the same pipeline with no server at all,
|
|
4
|
+
// so a test, a script or a person can run the feature and see what it really does.
|
|
5
|
+
//
|
|
6
|
+
// headless-sagas proves a pipeline *could* run without a view. This proves something *does*.
|
|
7
|
+
//
|
|
8
|
+
// The check that carries the gate is agreement, not existence: every saga a feature declares is
|
|
9
|
+
// named by a command, and every command names a saga the feature declares. A gate asserting only
|
|
10
|
+
// that an entrypoint file exists passes on day one and never fails again; this one fails every
|
|
11
|
+
// time a workflow is added and left undrivable -- the shape public-routes and internal-routes use.
|
|
12
|
+
//
|
|
13
|
+
// Off by default in the shipped config: a repository names its own command block and the roots
|
|
14
|
+
// that carry one, or the gate would red every tree the moment it upgraded the harness.
|
|
15
|
+
|
|
16
|
+
import { declaredSagas } from "./saga-tests.mjs";
|
|
17
|
+
|
|
18
|
+
const DEFAULT_BLOCK = "cli";
|
|
19
|
+
const DEFAULT_VIEW_MODULES = ["react", "react-dom"];
|
|
20
|
+
const DEFAULT_SERVER_MODULES = ["fastify", "express", "koa", "@nestjs/core"];
|
|
21
|
+
const DEFAULT_FACTORY = "defineCommands";
|
|
22
|
+
const DEFAULT_PUBLISHER = "index";
|
|
23
|
+
// What a command entry names as the workflow it drives. One key, so the table stays readable.
|
|
24
|
+
const DEFAULT_DRIVES = "saga";
|
|
25
|
+
// Kept beside saga-tests' own default: the two gates must read the same declarations.
|
|
26
|
+
const DEFAULT_SAGA_FACTORIES = ["definePipeline", "defineSaga"];
|
|
27
|
+
|
|
28
|
+
function escape(text) {
|
|
29
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// An empty list would silently match nothing, disabling the check instead of failing it.
|
|
33
|
+
function names(value, label) {
|
|
34
|
+
const list = [value].flat();
|
|
35
|
+
if (list.length === 0 || list.some((name) => typeof name !== "string" || name === "")) {
|
|
36
|
+
throw new Error(`cli.${label} must be a name or a non-empty list of them, got ${JSON.stringify(value)}`);
|
|
37
|
+
}
|
|
38
|
+
return list;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function blockPattern(block) {
|
|
42
|
+
return new RegExp(`(?:^|/)${escape(block)}\\.[cm]?[jt]sx?$`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function importPattern(modules) {
|
|
46
|
+
return new RegExp(`\\bfrom\\s+["'](?:${modules.map(escape).join("|")})["']`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Every feature directory the roots cover, with the block file it carries and that file's text. */
|
|
50
|
+
function blocksOf(features, contents, block, roots) {
|
|
51
|
+
const matches = blockPattern(block);
|
|
52
|
+
const byPath = new Map(contents.map((file) => [file.path, file.contents]));
|
|
53
|
+
const found = [];
|
|
54
|
+
for (const { feature, files } of features) {
|
|
55
|
+
if (roots.length > 0 && !roots.some((root) => feature.includes(root))) continue;
|
|
56
|
+
const file = files.find((name) => matches.test(name));
|
|
57
|
+
found.push({
|
|
58
|
+
feature,
|
|
59
|
+
files,
|
|
60
|
+
path: file === undefined ? undefined : `${feature}/${file}`,
|
|
61
|
+
contents: file === undefined ? undefined : (byPath.get(`${feature}/${file}`) ?? ""),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return found;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
/** Every workflow the command table names, read from the `drives` key of each entry. */
|
|
69
|
+
export function commandedSagas(contents, options = {}) {
|
|
70
|
+
const factories = names(options.factory ?? DEFAULT_FACTORY, "factory");
|
|
71
|
+
const drives = options.drives ?? DEFAULT_DRIVES;
|
|
72
|
+
const table = new RegExp(`\\b(?:${factories.map(escape).join("|")})\\s*[<(]`);
|
|
73
|
+
const opens = table.exec(contents);
|
|
74
|
+
if (opens === null) return [];
|
|
75
|
+
const body = contents.slice(opens.index);
|
|
76
|
+
const entry = new RegExp(`\\b${escape(drives)}\\s*:\\s*([A-Za-z_$][\\w$]*)`, "g");
|
|
77
|
+
const found = [];
|
|
78
|
+
for (const hit of body.matchAll(entry)) found.push(hit[1]);
|
|
79
|
+
return found;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @param {{feature: string, files: string[]}[]} features Feature folders, relative to the root.
|
|
84
|
+
* @param {{path: string, contents: string}[]} contents Every source file, relative to the root.
|
|
85
|
+
* @param {{block?: string, roots?: string[], viewModules?: string[], serverModules?: string[], factory?: string | string[], publisher?: string, tests?: {path: string, contents: string}[]}} [options]
|
|
86
|
+
* @returns {{path?: string, feature?: string, rule: string, detail: string}[]}
|
|
87
|
+
*/
|
|
88
|
+
export function checkFeatureCommands(features, contents, options = {}) {
|
|
89
|
+
return check(features, contents, options).problems;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The problems, and how many features the roots actually cover — a summary line must not overcount. */
|
|
93
|
+
export function governedFeatures(features, options = {}) {
|
|
94
|
+
const roots = options.roots ?? [];
|
|
95
|
+
return features.filter((feature) => roots.length === 0 || roots.some((root) => feature.feature.includes(root))).length;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function check(features, contents, options) {
|
|
99
|
+
const block = names(options.block ?? DEFAULT_BLOCK, "block")[0];
|
|
100
|
+
const roots = options.roots ?? [];
|
|
101
|
+
const viewModules = options.viewModules ?? DEFAULT_VIEW_MODULES;
|
|
102
|
+
const serverModules = options.serverModules ?? DEFAULT_SERVER_MODULES;
|
|
103
|
+
const factories = names(options.factory ?? DEFAULT_FACTORY, "factory");
|
|
104
|
+
const publisher = options.publisher ?? DEFAULT_PUBLISHER;
|
|
105
|
+
const tests = options.tests ?? [];
|
|
106
|
+
const drives = options.drives ?? DEFAULT_DRIVES;
|
|
107
|
+
const sagaFactories = names(options.sagaFactories ?? DEFAULT_SAGA_FACTORIES, "sagaFactories");
|
|
108
|
+
|
|
109
|
+
const view = importPattern(viewModules);
|
|
110
|
+
const server = importPattern(serverModules);
|
|
111
|
+
const declares = new RegExp(`\\b(?:${factories.map(escape).join("|")})\\s*[<(]`);
|
|
112
|
+
const publishes = new RegExp(`\\bfrom\\s+["']\\.\\/${escape(block)}\\.(?:[cm]?[jt]sx?)["']`);
|
|
113
|
+
|
|
114
|
+
const problems = [];
|
|
115
|
+
const governed = blocksOf(features, contents, block, roots);
|
|
116
|
+
for (const found of governed) {
|
|
117
|
+
if (found.path === undefined) {
|
|
118
|
+
problems.push({
|
|
119
|
+
feature: found.feature,
|
|
120
|
+
rule: "feature-cli-missing",
|
|
121
|
+
detail: `${found.feature} declares no ${block} block: a feature that can only be driven over its routes cannot be run without a server`,
|
|
122
|
+
});
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (view.test(found.contents)) {
|
|
126
|
+
problems.push({
|
|
127
|
+
path: found.path,
|
|
128
|
+
rule: "feature-cli-no-view",
|
|
129
|
+
detail: `${block} is the headless entrypoint and cannot import ${viewModules.join(" or ")}`,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
if (server.test(found.contents)) {
|
|
133
|
+
problems.push({
|
|
134
|
+
path: found.path,
|
|
135
|
+
rule: "feature-cli-no-server",
|
|
136
|
+
detail: `${block} drives the pipeline directly and cannot import ${serverModules.join(" or ")} — a command that needs a running server is a route, not a command`,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
if (!declares.test(found.contents)) {
|
|
140
|
+
problems.push({
|
|
141
|
+
path: found.path,
|
|
142
|
+
rule: "feature-cli-undeclared",
|
|
143
|
+
detail: `${block} declares no command through ${factories.join(" or ")}, so nothing can dispatch it`,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
const index = `${found.feature}/${publisher}`;
|
|
147
|
+
const republished = contents.find((file) => file.path.replace(/\.[cm]?[jt]sx?$/, "") === index);
|
|
148
|
+
if (republished === undefined || !publishes.test(republished.contents)) {
|
|
149
|
+
problems.push({
|
|
150
|
+
path: found.path,
|
|
151
|
+
rule: "feature-cli-unpublished",
|
|
152
|
+
detail: `${publisher} does not re-export ${block}: only ${publisher} crosses a feature boundary, so the registry cannot reach these commands`,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
// The command block is the test seam. A block nothing imports is a claim, exactly as an
|
|
156
|
+
// untested saga is -- saga-tests makes the same demand of a workflow.
|
|
157
|
+
const named = tests.some((file) => publishes.test(file.contents) && file.path.startsWith(`${found.feature}/`));
|
|
158
|
+
if (tests.length > 0 && !named) {
|
|
159
|
+
problems.push({
|
|
160
|
+
path: found.path,
|
|
161
|
+
rule: "feature-cli-untested",
|
|
162
|
+
detail: `no test under ${found.feature} imports ${block}: the command block is what makes the feature testable without a server, and nothing runs it`,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// The gate's real work: the dispatch table and the feature's declared workflows agree, both
|
|
167
|
+
// ways. Existence alone would pass forever; this fails the next time a saga is added.
|
|
168
|
+
const owned = found.files
|
|
169
|
+
.filter((file) => /\.[cm]?tsx?$/.test(file) && !/\.test\.[cm]?tsx?$/.test(file))
|
|
170
|
+
.map((file) => `${found.feature}/${file}`);
|
|
171
|
+
const declared = new Set(
|
|
172
|
+
declaredSagas(
|
|
173
|
+
contents.filter((file) => owned.includes(file.path)),
|
|
174
|
+
{ factories: sagaFactories },
|
|
175
|
+
).map((saga) => saga.name),
|
|
176
|
+
);
|
|
177
|
+
const commanded = new Set(commandedSagas(found.contents, { factory: factories, drives }));
|
|
178
|
+
for (const saga of declared) {
|
|
179
|
+
if (!commanded.has(saga)) {
|
|
180
|
+
problems.push({
|
|
181
|
+
path: found.path,
|
|
182
|
+
rule: "feature-cli-undrivable-saga",
|
|
183
|
+
detail: `${saga} is declared in ${found.feature} but no command drives it: a workflow reachable only over a route cannot be run without a server`,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
for (const saga of commanded) {
|
|
188
|
+
if (!declared.has(saga)) {
|
|
189
|
+
problems.push({
|
|
190
|
+
path: found.path,
|
|
191
|
+
rule: "feature-cli-unknown-saga",
|
|
192
|
+
detail: `a command names ${saga}, which ${found.feature} does not declare through ${sagaFactories.join(" or ")}`,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return { problems, governed: governed.length };
|
|
198
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { checkFeatureCommands, governedFeatures } from "./feature-cli.mjs";
|
|
4
|
+
|
|
5
|
+
const FEATURE = "backend/src/features/progress";
|
|
6
|
+
|
|
7
|
+
function tree({ cli, index, test: testFile } = {}) {
|
|
8
|
+
const files = [];
|
|
9
|
+
if (cli !== undefined) files.push({ path: `${FEATURE}/cli.ts`, contents: cli });
|
|
10
|
+
if (index !== undefined) files.push({ path: `${FEATURE}/index.ts`, contents: index });
|
|
11
|
+
if (testFile !== undefined) files.push({ path: `${FEATURE}/cli.test.ts`, contents: testFile });
|
|
12
|
+
return files;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const GOOD_CLI = 'import { defineCommands } from "@qc/kernel";\nexport const commands = defineCommands("progress", {});';
|
|
16
|
+
const SAGA = 'export const createEntry = definePipeline({});';
|
|
17
|
+
const GOOD_INDEX = 'export { commands } from "./cli.js";';
|
|
18
|
+
const GOOD_TEST = 'import { commands } from "./cli.js";';
|
|
19
|
+
|
|
20
|
+
function features(files) {
|
|
21
|
+
return [{ feature: FEATURE, files: files.map((file) => file.path.slice(FEATURE.length + 1)) }];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
test("a feature with a declared, published, tested command block passes", () => {
|
|
25
|
+
const files = tree({ cli: GOOD_CLI, index: GOOD_INDEX, test: GOOD_TEST });
|
|
26
|
+
assert.deepEqual(checkFeatureCommands(features(files), files, { tests: [files[2]] }), []);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("a feature carrying no cli block fails and names feature-cli-missing", () => {
|
|
30
|
+
const files = tree({ index: GOOD_INDEX });
|
|
31
|
+
const problems = checkFeatureCommands(features(files), files);
|
|
32
|
+
assert.equal(problems.length, 1);
|
|
33
|
+
assert.equal(problems[0].rule, "feature-cli-missing");
|
|
34
|
+
assert.equal(problems[0].feature, FEATURE);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("a cli block importing react fails and names feature-cli-no-view", () => {
|
|
38
|
+
const files = tree({ cli: `import * as React from "react";\n${GOOD_CLI}`, index: GOOD_INDEX });
|
|
39
|
+
const problems = checkFeatureCommands(features(files), files);
|
|
40
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["feature-cli-no-view"]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("a cli block importing a server framework fails and names feature-cli-no-server", () => {
|
|
44
|
+
const files = tree({ cli: `import Fastify from "fastify";\n${GOOD_CLI}`, index: GOOD_INDEX });
|
|
45
|
+
const problems = checkFeatureCommands(features(files), files);
|
|
46
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["feature-cli-no-server"]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("a cli block declaring no command through the factory fails", () => {
|
|
50
|
+
const files = tree({ cli: "export const commands = {};", index: GOOD_INDEX });
|
|
51
|
+
const problems = checkFeatureCommands(features(files), files);
|
|
52
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["feature-cli-undeclared"]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("a cli block the index does not re-export fails and names feature-cli-unpublished", () => {
|
|
56
|
+
const files = tree({ cli: GOOD_CLI, index: 'export { trigger } from "./trigger.js";' });
|
|
57
|
+
const problems = checkFeatureCommands(features(files), files);
|
|
58
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["feature-cli-unpublished"]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("a feature with no index at all fails as unpublished rather than throwing", () => {
|
|
62
|
+
const files = tree({ cli: GOOD_CLI });
|
|
63
|
+
const problems = checkFeatureCommands(features(files), files);
|
|
64
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["feature-cli-unpublished"]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("a command block no test imports fails and names feature-cli-untested", () => {
|
|
68
|
+
const files = tree({ cli: GOOD_CLI, index: GOOD_INDEX });
|
|
69
|
+
const elsewhere = { path: "backend/src/features/photos/cli.test.ts", contents: GOOD_TEST };
|
|
70
|
+
const problems = checkFeatureCommands(features(files), files, { tests: [elsewhere] });
|
|
71
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["feature-cli-untested"]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("a feature outside the configured roots is not asked for a command block", () => {
|
|
75
|
+
const files = tree({ index: GOOD_INDEX });
|
|
76
|
+
assert.deepEqual(checkFeatureCommands(features(files), files, { roots: ["frontend/src/features"] }), []);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("the block name, the factory and the publisher come from options", () => {
|
|
80
|
+
const feature = "api/features/pins";
|
|
81
|
+
const files = [
|
|
82
|
+
{ path: `${feature}/command.ts`, contents: 'export const run = defineTasks("pins", {});' },
|
|
83
|
+
{ path: `${feature}/barrel.ts`, contents: 'export { run } from "./command.js";' },
|
|
84
|
+
];
|
|
85
|
+
const found = [{ feature, files: ["command.ts", "barrel.ts"] }];
|
|
86
|
+
const options = { block: "command", factory: "defineTasks", publisher: "barrel" };
|
|
87
|
+
assert.deepEqual(checkFeatureCommands(found, files, options), []);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("an empty block name fails loudly rather than matching nothing", () => {
|
|
91
|
+
assert.throws(() => checkFeatureCommands([], [], { block: "" }), /cli\.block/);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("an empty factory list fails loudly rather than matching nothing", () => {
|
|
95
|
+
assert.throws(() => checkFeatureCommands([], [], { factory: [] }), /cli\.factory/);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("a saga the command table does not drive fails and names feature-cli-undrivable-saga", () => {
|
|
99
|
+
const files = [
|
|
100
|
+
{ path: `${FEATURE}/cli.ts`, contents: GOOD_CLI },
|
|
101
|
+
{ path: `${FEATURE}/index.ts`, contents: GOOD_INDEX },
|
|
102
|
+
{ path: `${FEATURE}/slices/create-entry.ts`, contents: SAGA },
|
|
103
|
+
];
|
|
104
|
+
const found = [{ feature: FEATURE, files: ["cli.ts", "index.ts", "slices/create-entry.ts"] }];
|
|
105
|
+
const problems = checkFeatureCommands(found, files, {});
|
|
106
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["feature-cli-undrivable-saga"]);
|
|
107
|
+
assert.match(problems[0].detail, /createEntry/);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("a command table driving every declared saga passes", () => {
|
|
111
|
+
const cli = 'export const commands = defineCommands("progress", { "create-entry": { saga: createEntry } });';
|
|
112
|
+
const files = [
|
|
113
|
+
{ path: `${FEATURE}/cli.ts`, contents: cli },
|
|
114
|
+
{ path: `${FEATURE}/index.ts`, contents: GOOD_INDEX },
|
|
115
|
+
{ path: `${FEATURE}/slices/create-entry.ts`, contents: SAGA },
|
|
116
|
+
];
|
|
117
|
+
const found = [{ feature: FEATURE, files: ["cli.ts", "index.ts", "slices/create-entry.ts"] }];
|
|
118
|
+
assert.deepEqual(checkFeatureCommands(found, files, {}), []);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("a command naming a saga the feature does not declare fails", () => {
|
|
122
|
+
const cli = 'export const commands = defineCommands("progress", { "nope": { saga: notHere } });';
|
|
123
|
+
const files = [
|
|
124
|
+
{ path: `${FEATURE}/cli.ts`, contents: cli },
|
|
125
|
+
{ path: `${FEATURE}/index.ts`, contents: GOOD_INDEX },
|
|
126
|
+
];
|
|
127
|
+
const found = [{ feature: FEATURE, files: ["cli.ts", "index.ts"] }];
|
|
128
|
+
const problems = checkFeatureCommands(found, files, {});
|
|
129
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["feature-cli-unknown-saga"]);
|
|
130
|
+
assert.match(problems[0].detail, /notHere/);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("a saga declared in a sibling feature is not the one this command table owes", () => {
|
|
134
|
+
const cli = 'export const commands = defineCommands("progress", {});';
|
|
135
|
+
const files = [
|
|
136
|
+
{ path: `${FEATURE}/cli.ts`, contents: cli },
|
|
137
|
+
{ path: `${FEATURE}/index.ts`, contents: GOOD_INDEX },
|
|
138
|
+
{ path: "backend/src/features/photos/slices/upload.ts", contents: "export const upload = definePipeline({});" },
|
|
139
|
+
];
|
|
140
|
+
const found = [{ feature: FEATURE, files: ["cli.ts", "index.ts"] }];
|
|
141
|
+
assert.deepEqual(checkFeatureCommands(found, files, {}), []);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("a saga declared only in a test file is not owed a command", () => {
|
|
145
|
+
const files = [
|
|
146
|
+
{ path: `${FEATURE}/cli.ts`, contents: GOOD_CLI },
|
|
147
|
+
{ path: `${FEATURE}/index.ts`, contents: GOOD_INDEX },
|
|
148
|
+
{ path: `${FEATURE}/pipeline.test.ts`, contents: SAGA },
|
|
149
|
+
];
|
|
150
|
+
const found = [{ feature: FEATURE, files: ["cli.ts", "index.ts", "pipeline.test.ts"] }];
|
|
151
|
+
assert.deepEqual(checkFeatureCommands(found, files, {}), []);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("the key a command entry names its workflow with comes from options", () => {
|
|
155
|
+
const cli = 'export const commands = defineCommands("progress", { "create-entry": { runs: createEntry } });';
|
|
156
|
+
const files = [
|
|
157
|
+
{ path: `${FEATURE}/cli.ts`, contents: cli },
|
|
158
|
+
{ path: `${FEATURE}/index.ts`, contents: GOOD_INDEX },
|
|
159
|
+
{ path: `${FEATURE}/slices/create-entry.ts`, contents: SAGA },
|
|
160
|
+
];
|
|
161
|
+
const found = [{ feature: FEATURE, files: ["cli.ts", "index.ts", "slices/create-entry.ts"] }];
|
|
162
|
+
assert.deepEqual(checkFeatureCommands(found, files, { drives: "runs" }), []);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("the governed count is the features the roots cover, not every feature in the tree", () => {
|
|
166
|
+
const found = [
|
|
167
|
+
{ feature: "backend/src/features/progress", files: [] },
|
|
168
|
+
{ feature: "frontend/src/features/progress", files: [] },
|
|
169
|
+
];
|
|
170
|
+
assert.equal(governedFeatures(found, { roots: ["backend/src/features"] }), 1);
|
|
171
|
+
assert.equal(governedFeatures(found, {}), 2);
|
|
172
|
+
});
|
|
@@ -1,47 +1,67 @@
|
|
|
1
|
-
// Test path mirror: every
|
|
2
|
-
//
|
|
3
|
-
// Tests in frontend/tests/ must not be orphaned (a matching source file must exist).
|
|
1
|
+
// Test path mirror: in every configured root, a test lives in the tests folder at its source's path,
|
|
2
|
+
// never beside it in src. A testOnly root holds test support, so its tests name no source file.
|
|
4
3
|
|
|
5
|
-
const
|
|
4
|
+
const TEST_SUFFIX = /\.test\.[cm]?[jt]sx?$/;
|
|
5
|
+
const SOURCE_EXTENSION = /\.[cm]?[jt]sx?$/;
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* @param {string[]} testFiles Relative paths of all test files (e.g. frontend/tests/...)
|
|
11
|
-
* @param {string[]} srcFiles Relative paths of all source files (e.g. frontend/src/...)
|
|
12
|
-
* @returns {{path: string, rule: string, detail: string}[]}
|
|
13
|
-
*/
|
|
14
|
-
export function checkTestMirror(testFiles, srcFiles) {
|
|
15
|
-
const problems = [];
|
|
16
|
-
const srcStems = new Set(
|
|
17
|
-
srcFiles.map((f) => f.replace(/\.(tsx?)$/, ""))
|
|
18
|
-
);
|
|
7
|
+
const folder = (value) => value.replace(/\/+$/, "");
|
|
8
|
+
const holds = (dir, file) => file.startsWith(`${dir}/`);
|
|
9
|
+
const named = (value) => typeof value === "string" && folder(value) !== "";
|
|
19
10
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
11
|
+
/** The roots as clean folders, or the problems that name each malformed one. */
|
|
12
|
+
function readRoots(roots) {
|
|
13
|
+
if (!Array.isArray(roots)) {
|
|
14
|
+
const detail = `testMirror.roots must be a list of { src, tests, testOnly? }, got ${JSON.stringify(roots)}`;
|
|
15
|
+
return { valid: [], problems: [{ path: "qc.config.json", rule: "invalid-mirror-root", detail }] };
|
|
16
|
+
}
|
|
17
|
+
const valid = [];
|
|
18
|
+
const problems = [];
|
|
19
|
+
roots.forEach((root, index) => {
|
|
20
|
+
if (named(root?.src) && named(root?.tests)) {
|
|
21
|
+
valid.push({ src: folder(root.src), tests: folder(root.tests), testOnly: root.testOnly === true });
|
|
22
|
+
return;
|
|
28
23
|
}
|
|
24
|
+
const detail = `testMirror.roots[${index}] needs a src folder and a tests folder, got ${JSON.stringify(root)}`;
|
|
25
|
+
problems.push({ path: "qc.config.json", rule: "invalid-mirror-root", detail });
|
|
26
|
+
});
|
|
27
|
+
return { valid, problems };
|
|
28
|
+
}
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
/** Every folder the well-formed roots name, once each: the folders a caller walks for this gate. */
|
|
31
|
+
export function mirrorFolders(roots) {
|
|
32
|
+
const { valid } = readRoots(roots);
|
|
33
|
+
return [...new Set(valid.flatMap((root) => [root.src, root.tests]))];
|
|
34
|
+
}
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
detail: `${testFile}: no matching source file at frontend/src/${stem}.{ts,tsx}`,
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
}
|
|
36
|
+
function judge(testFile, root, srcStems) {
|
|
37
|
+
if (!holds(root.tests, testFile)) {
|
|
38
|
+
const detail = `${testFile}: a test in ${root.src}/ lives in ${root.tests}/, at the path that mirrors its source`;
|
|
39
|
+
return { path: testFile, rule: "unmirrored-test", detail };
|
|
44
40
|
}
|
|
41
|
+
if (root.testOnly) return null;
|
|
42
|
+
const stem = testFile.slice(root.tests.length + 1).replace(TEST_SUFFIX, "");
|
|
43
|
+
const source = `${root.src}/${stem}`;
|
|
44
|
+
if (srcStems.has(source) || srcStems.has(`${source}/index`)) return null;
|
|
45
|
+
const detail = `${testFile}: no source at ${source} or ${source}/index for this test in ${root.tests}/`;
|
|
46
|
+
return { path: testFile, rule: "orphaned-test", detail };
|
|
47
|
+
}
|
|
45
48
|
|
|
49
|
+
/**
|
|
50
|
+
* A test is judged by the first root whose tests folder holds it, else by the first whose src
|
|
51
|
+
* folder does. A test in no root is not this gate's concern.
|
|
52
|
+
*
|
|
53
|
+
* @param {string[]} testFiles repository-relative paths of the test files
|
|
54
|
+
* @param {string[]} srcFiles repository-relative paths of the source files
|
|
55
|
+
* @param {{src: string, tests: string, testOnly?: boolean}[]} roots `testMirror.roots`
|
|
56
|
+
* @returns {{path: string, rule: string, detail: string}[]}
|
|
57
|
+
*/
|
|
58
|
+
export function checkTestMirror(testFiles, srcFiles, roots) {
|
|
59
|
+
const { valid, problems } = readRoots(roots);
|
|
60
|
+
const srcStems = new Set(srcFiles.map((file) => file.replace(SOURCE_EXTENSION, "")));
|
|
61
|
+
for (const testFile of testFiles) {
|
|
62
|
+
const root = valid.find((each) => holds(each.tests, testFile)) ?? valid.find((each) => holds(each.src, testFile));
|
|
63
|
+
const problem = root ? judge(testFile, root, srcStems) : null;
|
|
64
|
+
if (problem) problems.push(problem);
|
|
65
|
+
}
|
|
46
66
|
return problems;
|
|
47
67
|
}
|
|
@@ -1,23 +1,28 @@
|
|
|
1
1
|
import test from "node:test";
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
|
+
import { defaults } from "../config.mjs";
|
|
3
4
|
import { checkTestMirror } from "./test-mirror.mjs";
|
|
4
5
|
|
|
6
|
+
const FRONTEND = [{ src: "frontend/src", tests: "frontend/tests" }];
|
|
7
|
+
const TWO_ROOTS = [...FRONTEND, { src: "backend/src", tests: "backend/tests" }];
|
|
8
|
+
const verdict = (problems) => problems.map(({ path, rule }) => ({ path, rule }));
|
|
9
|
+
|
|
5
10
|
test("a test perfectly mirroring a source file passes", () => {
|
|
6
11
|
const tests = ["frontend/tests/features/photos/slices/get-photo.test.ts"];
|
|
7
12
|
const src = ["frontend/src/features/photos/slices/get-photo.ts"];
|
|
8
|
-
assert.deepEqual(checkTestMirror(tests, src), []);
|
|
13
|
+
assert.deepEqual(checkTestMirror(tests, src, FRONTEND), []);
|
|
9
14
|
});
|
|
10
15
|
|
|
11
16
|
test("a test mirroring a view folder index passes", () => {
|
|
12
17
|
const tests = ["frontend/tests/features/photos/components/photo-viewer.test.tsx"];
|
|
13
18
|
const src = ["frontend/src/features/photos/components/photo-viewer/index.ts"];
|
|
14
|
-
assert.deepEqual(checkTestMirror(tests, src), []);
|
|
19
|
+
assert.deepEqual(checkTestMirror(tests, src, FRONTEND), []);
|
|
15
20
|
});
|
|
16
21
|
|
|
17
22
|
test("a test file remaining in frontend/src is flagged as unmirrored", () => {
|
|
18
23
|
const tests = ["frontend/src/features/photos/slices/get-photo.test.ts"];
|
|
19
24
|
const src = ["frontend/src/features/photos/slices/get-photo.ts"];
|
|
20
|
-
const problems = checkTestMirror(tests, src);
|
|
25
|
+
const problems = checkTestMirror(tests, src, FRONTEND);
|
|
21
26
|
assert.equal(problems.length, 1);
|
|
22
27
|
assert.equal(problems[0].rule, "unmirrored-test");
|
|
23
28
|
});
|
|
@@ -25,7 +30,65 @@ test("a test file remaining in frontend/src is flagged as unmirrored", () => {
|
|
|
25
30
|
test("an orphaned test with no matching source file is flagged", () => {
|
|
26
31
|
const tests = ["frontend/tests/features/photos/slices/unknown-slice.test.ts"];
|
|
27
32
|
const src = ["frontend/src/features/photos/slices/get-photo.ts"];
|
|
28
|
-
const problems = checkTestMirror(tests, src);
|
|
33
|
+
const problems = checkTestMirror(tests, src, FRONTEND);
|
|
29
34
|
assert.equal(problems.length, 1);
|
|
30
35
|
assert.equal(problems[0].rule, "orphaned-test");
|
|
31
36
|
});
|
|
37
|
+
|
|
38
|
+
test("with two roots, a colocated backend test fails and a mirrored one passes", () => {
|
|
39
|
+
const tests = ["backend/src/features/tasks/slices/assign.test.ts", "backend/tests/features/tasks/slices/close.test.ts"];
|
|
40
|
+
const src = ["backend/src/features/tasks/slices/assign.ts", "backend/src/features/tasks/slices/close.ts"];
|
|
41
|
+
const problems = checkTestMirror(tests, src, TWO_ROOTS);
|
|
42
|
+
assert.deepEqual(verdict(problems), [{ path: "backend/src/features/tasks/slices/assign.test.ts", rule: "unmirrored-test" }]);
|
|
43
|
+
assert.ok(problems[0].detail.includes("backend/src/") && problems[0].detail.includes("backend/tests/"), problems[0].detail);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("an orphaned test names the root it belongs to", () => {
|
|
47
|
+
const problems = checkTestMirror(["backend/tests/features/tasks/gone.test.ts"], ["frontend/src/features/tasks/gone.ts"], TWO_ROOTS);
|
|
48
|
+
assert.deepEqual(verdict(problems), [{ path: "backend/tests/features/tasks/gone.test.ts", rule: "orphaned-test" }]);
|
|
49
|
+
assert.ok(problems[0].detail.includes("backend/src/features/tasks/gone"), problems[0].detail);
|
|
50
|
+
assert.ok(problems[0].detail.includes("backend/tests/"), problems[0].detail);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("the default config is the one frontend root, and gives today's frontend-only verdict", () => {
|
|
54
|
+
assert.deepEqual(defaults.testMirror.roots, FRONTEND);
|
|
55
|
+
const tests = [
|
|
56
|
+
"frontend/src/features/photos/slices/get-photo.test.ts",
|
|
57
|
+
"frontend/tests/features/photos/slices/get-photo.test.ts",
|
|
58
|
+
"frontend/tests/features/photos/slices/unknown-slice.test.ts",
|
|
59
|
+
"backend/src/features/tasks/slices/assign.test.ts",
|
|
60
|
+
];
|
|
61
|
+
const src = ["frontend/src/features/photos/slices/get-photo.ts", "backend/src/features/tasks/slices/assign.ts"];
|
|
62
|
+
assert.deepEqual(verdict(checkTestMirror(tests, src, defaults.testMirror.roots)), [
|
|
63
|
+
{ path: "frontend/src/features/photos/slices/get-photo.test.ts", rule: "unmirrored-test" },
|
|
64
|
+
{ path: "frontend/tests/features/photos/slices/unknown-slice.test.ts", rule: "orphaned-test" },
|
|
65
|
+
]);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("a testOnly root accepts a test with no source stem", () => {
|
|
69
|
+
const tests = ["packages/testing/tests/interop/cross-tenant.test.ts"];
|
|
70
|
+
const root = { src: "packages/testing/src", tests: "packages/testing/tests" };
|
|
71
|
+
assert.deepEqual(checkTestMirror(tests, [], [{ ...root, testOnly: true }]), []);
|
|
72
|
+
assert.deepEqual(verdict(checkTestMirror(tests, [], [root])), [{ path: tests[0], rule: "orphaned-test" }]);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("a root whose tests folder is its src folder judges a test as a test, so testOnly covers it", () => {
|
|
76
|
+
const root = { src: "packages/testing/src", tests: "packages/testing/src", testOnly: true };
|
|
77
|
+
assert.deepEqual(checkTestMirror(["packages/testing/src/interop.test.ts"], [], [root]), []);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("a root claims its own folder only, not a sibling that shares its prefix", () => {
|
|
81
|
+
assert.deepEqual(checkTestMirror(["frontend/src-legacy/old.test.ts"], [], FRONTEND), []);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("a test and its source match whatever script extension each one carries", () => {
|
|
85
|
+
const tests = ["frontend/tests/platform/format.test.mjs", "frontend/tests/platform/parse.test.js"];
|
|
86
|
+
const src = ["frontend/src/platform/format.mjs", "frontend/src/platform/parse.ts"];
|
|
87
|
+
assert.deepEqual(checkTestMirror(tests, src, FRONTEND), []);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("a root without a src and a tests folder is a problem that names its key, not a silent pass", () => {
|
|
91
|
+
const problems = checkTestMirror(["backend/src/a.test.ts"], [], [{ src: "backend/src", test: "backend/tests" }]);
|
|
92
|
+
assert.deepEqual(verdict(problems), [{ path: "qc.config.json", rule: "invalid-mirror-root" }]);
|
|
93
|
+
assert.ok(problems[0].detail.includes("testMirror.roots[0]"), problems[0].detail);
|
|
94
|
+
});
|
|
@@ -3,4 +3,22 @@
|
|
|
3
3
|
# tenant predicate, route agreement, sagas. Once per push, not once per commit.
|
|
4
4
|
set -euo pipefail
|
|
5
5
|
cd "$(git rev-parse --show-toplevel)"
|
|
6
|
+
|
|
7
|
+
# A deletion pushes no commits, so there is nothing to gate. git sends one line per ref as
|
|
8
|
+
# `<local ref> <local oid> <remote ref> <remote oid>`, and a delete carries an all-zero local oid.
|
|
9
|
+
# Only skip when refs were named and every one of them was a delete: no refs on stdin means an
|
|
10
|
+
# invocation this cannot reason about, which is a reason to run the gates, not to skip them.
|
|
11
|
+
named=""
|
|
12
|
+
carries_commits=""
|
|
13
|
+
while read -r _local_ref local_oid _remote_ref _remote_oid; do
|
|
14
|
+
named=yes
|
|
15
|
+
case "$local_oid" in
|
|
16
|
+
*[!0]*) carries_commits=yes ;;
|
|
17
|
+
esac
|
|
18
|
+
done
|
|
19
|
+
if [ -n "$named" ] && [ -z "$carries_commits" ]; then
|
|
20
|
+
echo "pre-push: every ref is a delete, nothing to check"
|
|
21
|
+
exit 0
|
|
22
|
+
fi
|
|
23
|
+
|
|
6
24
|
npx qc check
|