architecture-harness 0.4.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 +80 -0
- package/src/cli/check.mjs +535 -0
- package/src/cli/cli.test.mjs +120 -0
- package/src/cli/commit-attribution.mjs +68 -0
- package/src/cli/commit-attribution.test.mjs +47 -0
- package/src/cli/decisions.mjs +223 -0
- package/src/cli/doctor.mjs +135 -0
- package/src/cli/doctor.test.mjs +57 -0
- package/src/cli/init.mjs +98 -0
- package/src/cli/install-hooks.mjs +26 -0
- package/src/cli/qc.mjs +89 -0
- package/src/cli/scaffold.mjs +194 -0
- package/src/cli/sync-enforcement-map.mjs +28 -0
- package/src/cli/work-order-guard.mjs +0 -0
- package/src/cli/work-order-guard.test.mjs +79 -0
- package/src/cli/worktree-isolation.mjs +94 -0
- package/src/cli/worktree-isolation.test.mjs +82 -0
- package/src/config.mjs +338 -0
- package/src/config.test.mjs +59 -0
- package/src/decisions.mjs +198 -0
- package/src/decisions.test.mjs +183 -0
- package/src/descriptions.mjs +28 -0
- package/src/descriptions.test.mjs +27 -0
- package/src/enforcement-map.mjs +46 -0
- package/src/enforcement-map.test.mjs +60 -0
- package/src/eslint/index.mjs +37 -0
- package/src/eslint/options.mjs +53 -0
- package/src/eslint/preset.mjs +192 -0
- package/src/eslint/preset.test.mjs +96 -0
- package/src/eslint/rules/durable-idempotency-key.mjs +120 -0
- package/src/eslint/rules/external-service-only-in-adapter.mjs +52 -0
- package/src/eslint/rules/module-boundary.mjs +12 -0
- package/src/eslint/rules/name-the-pattern.mjs +111 -0
- package/src/eslint/rules/no-comment-paragraph.mjs +101 -0
- package/src/eslint/rules/no-cross-feature-internals.mjs +57 -0
- package/src/eslint/rules/no-number-in-comment.mjs +89 -0
- package/src/eslint/rules/no-offset-pagination.mjs +29 -0
- package/src/eslint/rules/no-orchestration-in-trigger.mjs +56 -0
- package/src/eslint/rules/no-promise-then.mjs +43 -0
- package/src/eslint/rules/no-promise-then.test.mjs +63 -0
- package/src/eslint/rules/no-raw-fetch.mjs +27 -0
- package/src/eslint/rules/no-status-literal.mjs +34 -0
- package/src/eslint/rules/no-supersession-trail.mjs +51 -0
- package/src/eslint/rules/scoped-repository.mjs +43 -0
- package/src/eslint/rules/signal-last-param.mjs +43 -0
- package/src/eslint/rules/storage-only-in-resource.mjs +52 -0
- package/src/eslint/rules/tenant-scoped-table.mjs +71 -0
- package/src/eslint/rules.test.mjs +627 -0
- package/src/gates/adr-format.mjs +87 -0
- package/src/gates/adr-format.test.mjs +54 -0
- package/src/gates/audit-append-only.mjs +34 -0
- package/src/gates/audit-append-only.test.mjs +36 -0
- package/src/gates/citations.mjs +93 -0
- package/src/gates/citations.test.mjs +115 -0
- package/src/gates/claimed-requirements.mjs +53 -0
- package/src/gates/claimed-requirements.test.mjs +60 -0
- package/src/gates/comment-style.mjs +76 -0
- package/src/gates/comment-style.test.mjs +46 -0
- package/src/gates/config-floor.mjs +68 -0
- package/src/gates/config-floor.test.mjs +52 -0
- package/src/gates/contract-compose.mjs +38 -0
- package/src/gates/contract-compose.test.mjs +40 -0
- package/src/gates/eight-blocks.mjs +223 -0
- package/src/gates/eight-blocks.test.mjs +359 -0
- package/src/gates/enforcement-map.mjs +62 -0
- package/src/gates/enforcement-map.test.mjs +41 -0
- package/src/gates/english-source.mjs +176 -0
- package/src/gates/english-source.test.mjs +122 -0
- package/src/gates/frontend-boundaries.mjs +154 -0
- package/src/gates/frontend-boundaries.test.mjs +72 -0
- package/src/gates/gate-tests.mjs +44 -0
- package/src/gates/gate-tests.test.mjs +41 -0
- package/src/gates/headless-sagas.mjs +54 -0
- package/src/gates/headless-sagas.test.mjs +71 -0
- package/src/gates/internal-routes.mjs +76 -0
- package/src/gates/internal-routes.test.mjs +53 -0
- package/src/gates/public-routes.mjs +67 -0
- package/src/gates/public-routes.test.mjs +38 -0
- package/src/gates/registry-agreement.mjs +37 -0
- package/src/gates/registry-agreement.test.mjs +36 -0
- package/src/gates/saga-tests.mjs +94 -0
- package/src/gates/saga-tests.test.mjs +92 -0
- package/src/gates/sql-identifiers.mjs +244 -0
- package/src/gates/sql-identifiers.test.mjs +197 -0
- package/src/gates/tenant-predicate.mjs +175 -0
- package/src/gates/tenant-predicate.test.mjs +161 -0
- package/src/gates/tenant-tables.mjs +65 -0
- package/src/gates/tenant-tables.test.mjs +56 -0
- package/src/gates/test-mirror.mjs +47 -0
- package/src/gates/test-mirror.test.mjs +31 -0
- package/src/index.mjs +9 -0
- package/templates/.jscpd.json +15 -0
- package/templates/CLAUDE.md +54 -0
- package/templates/decisions.examples.md +25 -0
- package/templates/docs/adr-template.md +58 -0
- package/templates/docs/architecture.md +179 -0
- package/templates/docs/decisions.md +72 -0
- package/templates/docs/enforcement.md +94 -0
- package/templates/docs/glossary.md +16 -0
- package/templates/docs/guards.md +23 -0
- package/templates/docs/pagination.md +113 -0
- package/templates/docs/performance.md +35 -0
- package/templates/docs/ui.md +32 -0
- package/templates/githooks/commit-msg +4 -0
- package/templates/githooks/pre-commit +18 -0
- package/templates/githooks/pre-push +6 -0
- package/templates/github/workflows/ci.yml +21 -0
- package/templates/qc.config.json +6 -0
- package/templates/quality-thresholds.json +114 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { checkEnglishSource, checkTranslationPairs, commentLines } from "./english-source.mjs";
|
|
4
|
+
|
|
5
|
+
const file = (path, contents) => [{ path, contents }];
|
|
6
|
+
|
|
7
|
+
test("an English comment beside Japanese data passes", () => {
|
|
8
|
+
const files = file("backend/src/features/delivery/shared/guards.ts", 'const agency = "国土交通省"; // The issuing ministry.');
|
|
9
|
+
assert.deepEqual(checkEnglishSource(files, { allowNonEnglish: ["backend/src/features/delivery/**"] }), []);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("a Japanese comment fails wherever it is, even in a declared path", () => {
|
|
13
|
+
const files = file("frontend/src/platform/i18n/dictionaries/ja.ts", "// 図面 keeps its card for the sheet preview.");
|
|
14
|
+
const problems = checkEnglishSource(files, { allowNonEnglish: ["frontend/src/platform/i18n/**"] });
|
|
15
|
+
assert.equal(problems.length, 1);
|
|
16
|
+
assert.equal(problems[0].rule, "english-comment");
|
|
17
|
+
assert.match(problems[0].detail, /line 1/);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("a Japanese doc comment fails, since JSDoc is prose too", () => {
|
|
21
|
+
const files = file("frontend/src/f/gallery.tsx", "/** What a row of 図面 is. */\nexport const rows = [];");
|
|
22
|
+
assert.equal(checkEnglishSource(files)[0].rule, "english-comment");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("Japanese in an undeclared string is a finding that names the config key", () => {
|
|
26
|
+
const files = file("backend/src/features/x/trigger.ts", 'export const label = "検査対象";');
|
|
27
|
+
const [problem] = checkEnglishSource(files);
|
|
28
|
+
assert.equal(problem.rule, "english-source");
|
|
29
|
+
assert.match(problem.detail, /language\.allowNonEnglish/);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("Japanese in a declared string passes, because data is not prose", () => {
|
|
33
|
+
const files = file("frontend/src/platform/i18n/dictionaries/ja.json", '{"inspection":"検査"}');
|
|
34
|
+
assert.deepEqual(checkEnglishSource(files, { allowNonEnglish: ["frontend/src/platform/i18n/dictionaries/**"] }), []);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("a purely English file is never inspected line by line", () => {
|
|
38
|
+
assert.deepEqual(checkEnglishSource(file("a/b.ts", "// ordinary\nexport const x = 1;")), []);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("a block comment's interior counts as comment, and code after it does not", () => {
|
|
42
|
+
const lines = commentLines("/*\n * 検査\n */\nconst x = 1;");
|
|
43
|
+
assert.deepEqual(lines.map((entry) => entry.line), [1, 2, 3]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("a declared translation without its English variant fails", () => {
|
|
47
|
+
const paths = ["src/i18n/ja.json", "src/i18n/vi.json"];
|
|
48
|
+
const problems = checkTranslationPairs(paths, [{ pattern: "**/i18n/*.json", english: "en.json" }]);
|
|
49
|
+
assert.equal(problems.length, 2);
|
|
50
|
+
assert.match(problems[0].detail, /src\/i18n\/en\.json/);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("any language is welcome once English is beside it", () => {
|
|
54
|
+
const paths = ["src/i18n/ja.json", "src/i18n/vi.json", "src/i18n/en.json"];
|
|
55
|
+
assert.deepEqual(checkTranslationPairs(paths, [{ pattern: "**/i18n/*.json", english: "en.json" }]), []);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("an empty script list is a named config error, not a gate that matches nothing", () => {
|
|
59
|
+
assert.throws(() => checkEnglishSource(file("a.ts", "x"), { scripts: [] }), /language\.scripts/);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("a markdown heading is not a comment, so a declared document is not scanned line by line", () => {
|
|
63
|
+
const files = [{ path: "docs/design-tokens.md", contents: "# ピンのステータス色\n\nbody\n" }];
|
|
64
|
+
assert.deepEqual(checkEnglishSource(files, { allowNonEnglish: ["docs/design-tokens.md"] }), []);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("an undeclared markdown document with Japanese still fails, at file level", () => {
|
|
68
|
+
const files = [{ path: "docs/notes.md", contents: "# 検査\n" }];
|
|
69
|
+
assert.equal(checkEnglishSource(files)[0].rule, "english-source");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("a legacy entry allows the file it names, up to the count recorded", () => {
|
|
73
|
+
const files = [{ path: "a/old.tsx", contents: 'const label = "検査";' }];
|
|
74
|
+
assert.deepEqual(checkEnglishSource(files, { legacyNonEnglish: { "a/old.tsx": 2 } }), []);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("a listed file that gains more non-English fails, so the debt cannot grow", () => {
|
|
78
|
+
const files = [{ path: "a/old.tsx", contents: 'const a = "検査"; const b = "是正指示";' }];
|
|
79
|
+
const [problem] = checkEnglishSource(files, { legacyNonEnglish: { "a/old.tsx": 2 } });
|
|
80
|
+
assert.equal(problem.rule, "legacy-entry-grew");
|
|
81
|
+
assert.match(problem.detail, /over the 2 recorded/);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("a legacy entry for a file that is already clean fails, so the list can only shrink", () => {
|
|
85
|
+
const files = [{ path: "a/old.tsx", contents: 'const label = "Inspection";' }];
|
|
86
|
+
const [problem] = checkEnglishSource(files, { legacyNonEnglish: { "a/old.tsx": 2 } });
|
|
87
|
+
assert.equal(problem.rule, "stale-legacy-entry");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("a bare path is refused, because an uncounted entry is an unlimited pass", () => {
|
|
91
|
+
const files = [{ path: "a/old.tsx", contents: 'const label = "検査";' }];
|
|
92
|
+
const [problem] = checkEnglishSource(files, { legacyNonEnglish: ["a/old.tsx"] });
|
|
93
|
+
assert.equal(problem.rule, "uncounted-legacy-entry");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("fullwidth punctuation is typography, not language, so it is not a finding by default", () => {
|
|
97
|
+
const files = [{ path: "a/count.tsx", contents: "<span>({count})</span>" }];
|
|
98
|
+
assert.deepEqual(checkEnglishSource(files), []);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("a repository that wants ASCII punctuation asks for the fullwidth script by name", () => {
|
|
102
|
+
const files = [{ path: "a/count.tsx", contents: "<span>({count})</span>" }];
|
|
103
|
+
assert.equal(checkEnglishSource(files, { scripts: ["cjk", "fullwidth"] })[0].rule, "english-source");
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const ledger = [{ path: "a/one.ts", contents: 'const a = "検査";' }, { path: "a/two.ts", contents: 'const b = "是正";' }];
|
|
107
|
+
const counted = { legacyNonEnglish: { "a/one.ts": 2, "a/two.ts": 2 } };
|
|
108
|
+
|
|
109
|
+
test("a ledger inside its budget passes", () => {
|
|
110
|
+
assert.deepEqual(checkEnglishSource(ledger, { ...counted, legacyBudget: { files: 2, occurrences: 4 } }), []);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("adding a file to the ledger without raising the budget fails", () => {
|
|
114
|
+
const [problem] = checkEnglishSource(ledger, { ...counted, legacyBudget: { files: 1, occurrences: 4 } });
|
|
115
|
+
assert.equal(problem.rule, "legacy-over-budget");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("a budget above what is left fails, so the ledger cannot sit there unnoticed", () => {
|
|
119
|
+
const [problem] = checkEnglishSource(ledger, { ...counted, legacyBudget: { files: 5, occurrences: 40 } });
|
|
120
|
+
assert.equal(problem.rule, "legacy-budget-stale");
|
|
121
|
+
assert.match(problem.detail, /Lower the budget to what is left/);
|
|
122
|
+
});
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// Platform remains a thin substrate and never holds feature domain logic or junk drawer directories.
|
|
2
|
+
// docs/architecture.md, docs/ui.md.
|
|
3
|
+
|
|
4
|
+
const DEFAULT_MAX_FOLDER_FILES = 10;
|
|
5
|
+
const DEFAULT_EXEMPT_DIRS = [
|
|
6
|
+
"platform/ui/primitives",
|
|
7
|
+
"platform/ui/patterns",
|
|
8
|
+
"platform/ui/layout",
|
|
9
|
+
"platform/ui/domain",
|
|
10
|
+
"platform/i18n/dictionaries",
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
const DOMAIN_LEAK_PATTERNS = [
|
|
14
|
+
{
|
|
15
|
+
pattern: /(?:^|\/)platform\/shell\/(?:chrome\/)?(photo-viewer[^/]*\.[cm]?[jt]sx?)/,
|
|
16
|
+
domain: "photos",
|
|
17
|
+
suggestion: "Relocate photo-viewer components to features/photos/components/photo-viewer",
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
pattern: /(?:^|\/)platform\/shell\/(drawing-viewer(?:\/.*)?)/,
|
|
21
|
+
domain: "blueprints",
|
|
22
|
+
suggestion: "Relocate drawing-viewer components to features/blueprints/components/drawing-viewer",
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
pattern: /(?:^|\/)platform\/shell\/(inspections(?:\/.*)?)/,
|
|
26
|
+
domain: "inspections",
|
|
27
|
+
suggestion: "Relocate inspections panels and modals to features/inspections/components/",
|
|
28
|
+
},
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
const INVERTED_IMPORT_PATTERNS = [
|
|
32
|
+
{
|
|
33
|
+
pattern: /from\s+["'].*\/platform\/shell\/chrome\/photo-viewer(?:\.js)?["']/,
|
|
34
|
+
domain: "photo-viewer",
|
|
35
|
+
suggestion: "Import photo-viewer from features/photos/components/photo-viewer",
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
pattern: /from\s+["'].*\/platform\/shell\/drawing-viewer\/[^"']+["']/,
|
|
39
|
+
domain: "drawing-viewer",
|
|
40
|
+
suggestion: "Import drawing-viewer from features/blueprints/components/drawing-viewer",
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
pattern: /from\s+["'].*\/platform\/shell\/inspections\/[^"']+["']/,
|
|
44
|
+
domain: "inspections",
|
|
45
|
+
suggestion: "Import inspection components from features/inspections/components/",
|
|
46
|
+
},
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
function classifyStemDomain(basename) {
|
|
50
|
+
const stem = basename.replace(/\.test\.[cm]?[jt]sx?$/, "").replace(/\.[cm]?[jt]sx?$/, "");
|
|
51
|
+
if (/^(?:use-)?session|auth-gate/.test(stem)) return "auth";
|
|
52
|
+
if (/^theme-/.test(stem)) return "theme";
|
|
53
|
+
if (/^language-/.test(stem)) return "i18n";
|
|
54
|
+
if (/preference/.test(stem)) return "preferences";
|
|
55
|
+
if (/^photo-viewer/.test(stem)) return "photos";
|
|
56
|
+
if (/^account-|personal-settings|ai-quota|settings-menu/.test(stem)) return "account";
|
|
57
|
+
if (/^app-|project-.*-chrome|error-boundary|home-screen/.test(stem)) return "chrome";
|
|
58
|
+
return "other";
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @param {{path: string, contents?: string}[]} platformFiles
|
|
63
|
+
* @param {{path: string, contents: string}[]} featureFiles
|
|
64
|
+
* @param {{maxFolderFiles?: number, exemptDirs?: string[]}} [options]
|
|
65
|
+
* @returns {{path: string, rule: string, detail: string}[]}
|
|
66
|
+
*/
|
|
67
|
+
export function checkFrontendBoundaries(platformFiles, featureFiles = [], options = {}) {
|
|
68
|
+
// Only platformFiles' paths are ever split on "/" below — featureFiles' paths are matched
|
|
69
|
+
// by content and only ever displayed, so an absolute OS path there is not this bug. A
|
|
70
|
+
// backslash in a platformFiles path means a caller's path helper broke cross-platform
|
|
71
|
+
// normalization — fail loud rather than silently bucket every file under one fake
|
|
72
|
+
// directory, the way the junk-drawer-folder gate once did on Windows.
|
|
73
|
+
for (const file of platformFiles) {
|
|
74
|
+
if (file.path.includes("\\")) {
|
|
75
|
+
throw new Error(`frontend-boundaries: "${file.path}" carries a backslash — pass forward-slash paths`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const problems = [];
|
|
80
|
+
const maxFiles = options.maxFolderFiles ?? DEFAULT_MAX_FOLDER_FILES;
|
|
81
|
+
const exemptDirs = new Set(options.exemptDirs ?? DEFAULT_EXEMPT_DIRS);
|
|
82
|
+
|
|
83
|
+
// 1. Check for domain leaks into platform/
|
|
84
|
+
const reportedLeakDirs = new Set();
|
|
85
|
+
for (const file of platformFiles) {
|
|
86
|
+
for (const { pattern, domain, suggestion } of DOMAIN_LEAK_PATTERNS) {
|
|
87
|
+
const match = pattern.exec(file.path);
|
|
88
|
+
if (match) {
|
|
89
|
+
// Group by directory if it's a folder leak, or report specific file
|
|
90
|
+
const leakKey = file.path.includes("platform/shell/drawing-viewer")
|
|
91
|
+
? "platform/shell/drawing-viewer"
|
|
92
|
+
: file.path.includes("platform/shell/inspections")
|
|
93
|
+
? "platform/shell/inspections"
|
|
94
|
+
: file.path;
|
|
95
|
+
|
|
96
|
+
if (!reportedLeakDirs.has(leakKey)) {
|
|
97
|
+
reportedLeakDirs.add(leakKey);
|
|
98
|
+
problems.push({
|
|
99
|
+
path: leakKey,
|
|
100
|
+
rule: "domain-in-platform",
|
|
101
|
+
detail: `Feature domain logic '${domain}' found in platform substrate. Suggestion: ${suggestion}`,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// 2. Check for junk drawer folders in platform/
|
|
110
|
+
const filesByDir = new Map();
|
|
111
|
+
for (const file of platformFiles) {
|
|
112
|
+
const parts = file.path.split("/");
|
|
113
|
+
const dir = parts.slice(0, -1).join("/");
|
|
114
|
+
const filename = parts[parts.length - 1];
|
|
115
|
+
if (!filesByDir.has(dir)) filesByDir.set(dir, []);
|
|
116
|
+
filesByDir.get(dir).push(filename);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
for (const [dir, files] of filesByDir) {
|
|
120
|
+
if ([...exemptDirs].some((exempt) => dir.includes(exempt))) continue;
|
|
121
|
+
// Only count source files (exclude tests from raw count threshold)
|
|
122
|
+
const sourceFiles = files.filter((f) => /\.[cm]?[jt]sx?$/.test(f) && !f.includes(".test."));
|
|
123
|
+
if (sourceFiles.length > maxFiles) {
|
|
124
|
+
const domains = new Set();
|
|
125
|
+
for (const f of sourceFiles) {
|
|
126
|
+
domains.add(classifyStemDomain(f));
|
|
127
|
+
}
|
|
128
|
+
domains.delete("other");
|
|
129
|
+
if (domains.size >= 3) {
|
|
130
|
+
problems.push({
|
|
131
|
+
path: dir,
|
|
132
|
+
rule: "junk-drawer-folder",
|
|
133
|
+
detail: `Folder contains ${sourceFiles.length} files mixing ${domains.size} unrelated domains (${[...domains].join(", ")}). Suggestion: decompose into focused platform modules (platform/auth, platform/preferences, platform/account) and move feature components to features/.`,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// 3. Check for inverted feature imports
|
|
140
|
+
for (const file of featureFiles) {
|
|
141
|
+
if (!file.contents) continue;
|
|
142
|
+
for (const { pattern, domain, suggestion } of INVERTED_IMPORT_PATTERNS) {
|
|
143
|
+
if (pattern.test(file.contents)) {
|
|
144
|
+
problems.push({
|
|
145
|
+
path: file.path,
|
|
146
|
+
rule: "inverted-feature-import",
|
|
147
|
+
detail: `Feature imports '${domain}' from platform shell. Suggestion: ${suggestion}`,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return problems;
|
|
154
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { checkFrontendBoundaries } from "./frontend-boundaries.mjs";
|
|
4
|
+
|
|
5
|
+
test("a clean platform directory with cohesive files passes", () => {
|
|
6
|
+
const platformFiles = [
|
|
7
|
+
{ path: "platform/auth/use-session.ts" },
|
|
8
|
+
{ path: "platform/auth/auth-gate.tsx" },
|
|
9
|
+
{ path: "platform/preferences/hand-preference.tsx" },
|
|
10
|
+
];
|
|
11
|
+
const problems = checkFrontendBoundaries(platformFiles, []);
|
|
12
|
+
assert.deepEqual(problems, []);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("a domain leak in platform is detected and flagged", () => {
|
|
16
|
+
const platformFiles = [
|
|
17
|
+
{ path: "platform/shell/drawing-viewer/drawing-viewer-modal.tsx" },
|
|
18
|
+
{ path: "platform/shell/drawing-viewer/drawing-viewer-canvas.tsx" },
|
|
19
|
+
{ path: "platform/shell/inspections/inspection-checklist-panel.tsx" },
|
|
20
|
+
{ path: "platform/shell/chrome/photo-viewer.tsx" },
|
|
21
|
+
];
|
|
22
|
+
const problems = checkFrontendBoundaries(platformFiles, []);
|
|
23
|
+
assert.equal(problems.length, 3);
|
|
24
|
+
assert.ok(problems.some((p) => p.path === "platform/shell/drawing-viewer" && p.rule === "domain-in-platform"));
|
|
25
|
+
assert.ok(problems.some((p) => p.path === "platform/shell/inspections" && p.rule === "domain-in-platform"));
|
|
26
|
+
assert.ok(problems.some((p) => p.path === "platform/shell/chrome/photo-viewer.tsx" && p.rule === "domain-in-platform"));
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("a junk drawer folder mixing multiple domains is detected", () => {
|
|
30
|
+
const platformFiles = [
|
|
31
|
+
{ path: "platform/shell/chrome/account-menu.tsx" },
|
|
32
|
+
{ path: "platform/shell/chrome/ai-quota-badge.tsx" },
|
|
33
|
+
{ path: "platform/shell/chrome/app-header.tsx" },
|
|
34
|
+
{ path: "platform/shell/chrome/app-layout.tsx" },
|
|
35
|
+
{ path: "platform/shell/chrome/auth-gate.tsx" },
|
|
36
|
+
{ path: "platform/shell/chrome/hand-preference.tsx" },
|
|
37
|
+
{ path: "platform/shell/chrome/language-menu.tsx" },
|
|
38
|
+
{ path: "platform/shell/chrome/layout-preference.tsx" },
|
|
39
|
+
{ path: "platform/shell/chrome/personal-settings-modal.tsx" },
|
|
40
|
+
{ path: "platform/shell/chrome/theme-toggle.tsx" },
|
|
41
|
+
{ path: "platform/shell/chrome/use-session.ts" },
|
|
42
|
+
{ path: "platform/shell/chrome/view-mode-preference.ts" },
|
|
43
|
+
];
|
|
44
|
+
const problems = checkFrontendBoundaries(platformFiles, []);
|
|
45
|
+
const junkDrawer = problems.find((p) => p.rule === "junk-drawer-folder");
|
|
46
|
+
assert.ok(junkDrawer);
|
|
47
|
+
assert.equal(junkDrawer.path, "platform/shell/chrome");
|
|
48
|
+
assert.ok(junkDrawer.detail.includes("Suggestion:"));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("a backslash path fails loud instead of bucketing every file as one folder", () => {
|
|
52
|
+
const platformFiles = [{ path: "platform\\shell\\chrome\\app-header.tsx" }];
|
|
53
|
+
assert.throws(() => checkFrontendBoundaries(platformFiles, []), /backslash/);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("an absolute, OS-native featureFiles path is not the same bug and does not throw", () => {
|
|
57
|
+
const featureFiles = [{ path: "C:\\repo\\backend\\src\\features\\orders\\index.ts", contents: "" }];
|
|
58
|
+
assert.doesNotThrow(() => checkFrontendBoundaries([], featureFiles));
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("an inverted feature import is detected", () => {
|
|
62
|
+
const featureFiles = [
|
|
63
|
+
{
|
|
64
|
+
path: "features/photos/slices/view-photo-run.tsx",
|
|
65
|
+
contents: 'import { PhotoViewerModalDialog } from "../../../platform/shell/chrome/photo-viewer.js";',
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
const problems = checkFrontendBoundaries([], featureFiles);
|
|
69
|
+
assert.equal(problems.length, 1);
|
|
70
|
+
assert.equal(problems[0].rule, "inverted-feature-import");
|
|
71
|
+
assert.ok(problems[0].detail.includes("Suggestion:"));
|
|
72
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// A gate is tested code: every gate ships a case that must fail. A warning is not a
|
|
2
|
+
// check, and a gate that has never failed has never been tested. docs/decisions.md.
|
|
3
|
+
//
|
|
4
|
+
// The kit's own gates are proven by its suite. This one covers the gates a repository
|
|
5
|
+
// writes for itself, which nothing else is watching.
|
|
6
|
+
|
|
7
|
+
function escape(text) {
|
|
8
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const NAME = /([^/]+?)\.[cm]?[jt]sx?$/;
|
|
12
|
+
|
|
13
|
+
function stemOf(path) {
|
|
14
|
+
const found = NAME.exec(path);
|
|
15
|
+
return found === null ? path : found[1];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {{path: string, contents: string}[]} checks a repository's own gate sources
|
|
20
|
+
* @param {{path: string, contents: string}[]} tests
|
|
21
|
+
* @returns {{path: string, rule: string, detail: string}[]}
|
|
22
|
+
*/
|
|
23
|
+
export function checkGatesAreTested(checks, tests) {
|
|
24
|
+
const problems = [];
|
|
25
|
+
for (const { path, contents } of checks) {
|
|
26
|
+
const stem = stemOf(path);
|
|
27
|
+
const exported = [...contents.matchAll(/export\s+(?:async\s+)?(?:function|const)\s+(\w+)/g)].map((m) => m[1]);
|
|
28
|
+
// The stem counts only where a test imports the file. A bare word matches English:
|
|
29
|
+
// a gate called `nothing` read as proven because a test said "yields nothing".
|
|
30
|
+
const imported = new RegExp(`${escape(stem)}\\.[cm]?[jt]sx?`);
|
|
31
|
+
const named =
|
|
32
|
+
exported.length > 0
|
|
33
|
+
? new RegExp(`${imported.source}|\\b(?:${exported.map(escape).join("|")})\\b`)
|
|
34
|
+
: imported;
|
|
35
|
+
if (!tests.some((test) => named.test(test.contents))) {
|
|
36
|
+
problems.push({
|
|
37
|
+
path,
|
|
38
|
+
rule: "unproven-gate",
|
|
39
|
+
detail: `no test names ${stem} or anything it exports — a gate that has never failed has never been tested`,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return problems;
|
|
44
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { checkGatesAreTested } from "./gate-tests.mjs";
|
|
4
|
+
|
|
5
|
+
const checks = [{ path: "scripts/gate/tenant-rules.mjs", contents: "export function checkTenantRules() {}" }];
|
|
6
|
+
|
|
7
|
+
test("a gate no test names fails", () => {
|
|
8
|
+
const problems = checkGatesAreTested(checks, []);
|
|
9
|
+
assert.equal(problems.length, 1);
|
|
10
|
+
assert.equal(problems[0].rule, "unproven-gate");
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("a test naming the file stem is enough", () => {
|
|
14
|
+
const tests = [{ path: "a.test.mjs", contents: 'import x from "./tenant-rules.mjs";' }];
|
|
15
|
+
assert.deepEqual(checkGatesAreTested(checks, tests), []);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("a test naming an exported function is enough", () => {
|
|
19
|
+
const tests = [{ path: "a.test.mjs", contents: "checkTenantRules([], []);" }];
|
|
20
|
+
assert.deepEqual(checkGatesAreTested(checks, tests), []);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("a repository with no gates of its own has nothing to prove", () => {
|
|
24
|
+
assert.deepEqual(checkGatesAreTested([], []), []);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// A stem is an English word as often as it is a filename. Matching it bare read a
|
|
28
|
+
// gate as proven because an unrelated test happened to contain the word.
|
|
29
|
+
test("a stem that appears only as prose does not prove a gate", () => {
|
|
30
|
+
const checks = [{ path: "scripts/gate/nothing.mjs", contents: "export function checkNothing() { return []; }" }];
|
|
31
|
+
const tests = [{ path: "other.test.mjs", contents: 'test("a source with no table yields nothing", () => {});' }];
|
|
32
|
+
const problems = checkGatesAreTested(checks, tests);
|
|
33
|
+
assert.equal(problems.length, 1);
|
|
34
|
+
assert.equal(problems[0].rule, "unproven-gate");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("a stem in an import path still proves it", () => {
|
|
38
|
+
const checks = [{ path: "scripts/gate/nothing.mjs", contents: "export function checkNothing() { return []; }" }];
|
|
39
|
+
const tests = [{ path: "a.test.mjs", contents: 'import { checkNothing } from "./nothing.mjs";' }];
|
|
40
|
+
assert.deepEqual(checkGatesAreTested(checks, tests), []);
|
|
41
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// The named block stays callable without a view. `block` takes a list: anatomies name it differently.
|
|
2
|
+
|
|
3
|
+
const DEFAULT_BLOCK = "pipeline";
|
|
4
|
+
const DEFAULT_VIEW_MODULES = ["react", "react-dom"];
|
|
5
|
+
const HOOK_IMPORT = /\bimport\s+{[^}]*\b(?:use[A-Z]\w+)[^}]*}\s+from/;
|
|
6
|
+
|
|
7
|
+
function escape(text) {
|
|
8
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// An empty or non-string list would silently match nothing, disabling the gate instead of failing.
|
|
12
|
+
function blockNames(block) {
|
|
13
|
+
const names = [block].flat();
|
|
14
|
+
if (names.length === 0 || names.some((name) => typeof name !== "string" || name === "")) {
|
|
15
|
+
throw new Error(`saga.headlessBlock must be a block name or a non-empty list of them, got ${JSON.stringify(block)}`);
|
|
16
|
+
}
|
|
17
|
+
return names;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {{path: string, contents: string}[]} files
|
|
22
|
+
* @param {{block?: string | string[], viewModules?: string[]}} [options]
|
|
23
|
+
*/
|
|
24
|
+
export function checkHeadlessPipelines(files, options = {}) {
|
|
25
|
+
const blocks = blockNames(options.block ?? DEFAULT_BLOCK);
|
|
26
|
+
const viewModules = options.viewModules ?? DEFAULT_VIEW_MODULES;
|
|
27
|
+
const viewImport = new RegExp(`\\bfrom\\s+["'](?:${viewModules.map(escape).join("|")})["']`);
|
|
28
|
+
const named = blocks.map((name) => ({
|
|
29
|
+
name,
|
|
30
|
+
matches: new RegExp(`(?:^|/)${escape(name)}\\.[cm]?[jt]sx?$`),
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
const problems = [];
|
|
34
|
+
for (const { path, contents } of files) {
|
|
35
|
+
const hit = named.find(({ matches }) => matches.test(path));
|
|
36
|
+
if (hit === undefined) continue;
|
|
37
|
+
const block = hit.name;
|
|
38
|
+
if (viewImport.test(contents)) {
|
|
39
|
+
problems.push({
|
|
40
|
+
path,
|
|
41
|
+
rule: "headless-pipeline-no-view",
|
|
42
|
+
detail: `${block} must stay headless and cannot import ${viewModules.join(" or ")}`,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (HOOK_IMPORT.test(contents)) {
|
|
46
|
+
problems.push({
|
|
47
|
+
path,
|
|
48
|
+
rule: "headless-pipeline-no-hooks",
|
|
49
|
+
detail: `${block} must stay headless and cannot import view hooks`,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return problems;
|
|
54
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { checkHeadlessPipelines } from "./headless-sagas.mjs";
|
|
4
|
+
|
|
5
|
+
test("a pure headless pipeline with no React imports passes", () => {
|
|
6
|
+
const pure = [{ path: "frontend/src/features/photos/pipeline.ts", contents: "export function upload() {}" }];
|
|
7
|
+
assert.deepEqual(checkHeadlessPipelines(pure), []);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test("a pipeline importing react fails and names headless-pipeline-no-view", () => {
|
|
11
|
+
const withReact = [{ path: "frontend/src/features/photos/pipeline.ts", contents: "import * as React from 'react';" }];
|
|
12
|
+
const problems = checkHeadlessPipelines(withReact);
|
|
13
|
+
assert.equal(problems.length, 1);
|
|
14
|
+
assert.equal(problems[0].rule, "headless-pipeline-no-view");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("a pipeline importing React hooks fails and names the rule", () => {
|
|
18
|
+
const withHook = [{ path: "frontend/src/features/photos/pipeline.ts", contents: "import { useState } from 'preact';" }];
|
|
19
|
+
const problems = checkHeadlessPipelines(withHook);
|
|
20
|
+
assert.equal(problems.length, 1);
|
|
21
|
+
assert.equal(problems[0].rule, "headless-pipeline-no-hooks");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("a UI trigger file importing React is allowed and passes", () => {
|
|
25
|
+
const trigger = [{ path: "frontend/src/features/photos/trigger.tsx", contents: "import * as React from 'react';" }];
|
|
26
|
+
assert.deepEqual(checkHeadlessPipelines(trigger), []);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("the headless block and its banned view modules come from options", () => {
|
|
30
|
+
const files = [{ path: "a/features/pins/flow.ts", contents: 'import { h } from "vue";' }];
|
|
31
|
+
const problems = checkHeadlessPipelines(files, { block: "flow", viewModules: ["vue"] });
|
|
32
|
+
assert.equal(problems.length, 1);
|
|
33
|
+
assert.equal(problems[0].rule, "headless-pipeline-no-view");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("a file that is not the configured block is not inspected", () => {
|
|
37
|
+
const files = [{ path: "a/features/pins/pipeline.ts", contents: 'import { h } from "vue";' }];
|
|
38
|
+
assert.deepEqual(checkHeadlessPipelines(files, { block: "flow", viewModules: ["vue"] }), []);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("a list of blocks inspects every name in it", () => {
|
|
42
|
+
const files = [
|
|
43
|
+
{ path: "a/features/pins/pipeline.ts", contents: 'import * as React from "react";' },
|
|
44
|
+
{ path: "a/features/pins/shared/runner.ts", contents: 'import * as React from "react";' },
|
|
45
|
+
];
|
|
46
|
+
const problems = checkHeadlessPipelines(files, { block: ["pipeline", "shared/runner"] });
|
|
47
|
+
assert.equal(problems.length, 2);
|
|
48
|
+
assert.deepEqual(
|
|
49
|
+
problems.map(({ path }) => path).sort(),
|
|
50
|
+
["a/features/pins/pipeline.ts", "a/features/pins/shared/runner.ts"],
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("a problem names the block that matched, not the whole list", () => {
|
|
55
|
+
const files = [{ path: "a/features/pins/shared/runner.ts", contents: 'import { useState } from "preact";' }];
|
|
56
|
+
const [problem] = checkHeadlessPipelines(files, { block: ["pipeline", "shared/runner"] });
|
|
57
|
+
assert.equal(problem.rule, "headless-pipeline-no-hooks");
|
|
58
|
+
assert.match(problem.detail, /^shared\/runner must stay headless/);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("a sibling of a listed block is not inspected", () => {
|
|
62
|
+
const files = [{ path: "a/features/pins/shared/queries.ts", contents: 'import * as React from "react";' }];
|
|
63
|
+
assert.deepEqual(checkHeadlessPipelines(files, { block: ["pipeline", "shared/runner"] }), []);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("a misconfigured block is a named config error, never a silently disabled gate", () => {
|
|
67
|
+
const files = [{ path: "a/pipeline.ts", contents: 'import * as React from "react";' }];
|
|
68
|
+
for (const block of [[], [7], "", [""], ["pipeline", null]]) {
|
|
69
|
+
assert.throws(() => checkHeadlessPipelines(files, { block }), /saga\.headlessBlock/, `block: ${JSON.stringify(block)}`);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// A worker calls the api by building a path string. Nothing type-checks that
|
|
2
|
+
// string against the route table, so renaming a route leaves the worker posting
|
|
3
|
+
// into a not-found response that looks, from the queue's side, like an outcome
|
|
4
|
+
// nobody recorded.
|
|
5
|
+
//
|
|
6
|
+
// Both sides are read as text and matched shape by shape: a template segment in
|
|
7
|
+
// the worker's url stands where a `:name` stands in the route. QC-007 — where a
|
|
8
|
+
// value is written twice, assert agreement rather than forbid it.
|
|
9
|
+
|
|
10
|
+
const DEFAULT_PREFIX = "/v1/internal/";
|
|
11
|
+
|
|
12
|
+
function escape(text) {
|
|
13
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function patterns(prefix) {
|
|
17
|
+
const literal = escape(prefix);
|
|
18
|
+
return {
|
|
19
|
+
route: new RegExp(`path:\\s*"(${literal}[^"]*)"`, "g"),
|
|
20
|
+
worker: new RegExp("`(?:\\$\\{[A-Za-z0-9_.]+\\})?(" + literal + "[^`]*)`", "g"),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function collect(source, pattern) {
|
|
25
|
+
const found = new Set();
|
|
26
|
+
pattern.lastIndex = 0;
|
|
27
|
+
let match = pattern.exec(source);
|
|
28
|
+
while (match !== null) {
|
|
29
|
+
found.add(match[1]);
|
|
30
|
+
match = pattern.exec(source);
|
|
31
|
+
}
|
|
32
|
+
return found;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A route's `:name` and a worker's `${expression}` are both one segment. */
|
|
36
|
+
function normalise(path) {
|
|
37
|
+
return path
|
|
38
|
+
.split("/")
|
|
39
|
+
.map((segment) => (segment.startsWith(":") || /^\$\{.*\}$/.test(segment) ? ":" : segment))
|
|
40
|
+
.join("/");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function declaredInternalRoutes(triggerSources, options = {}) {
|
|
44
|
+
const { route } = patterns(options.prefix ?? DEFAULT_PREFIX);
|
|
45
|
+
const found = new Set();
|
|
46
|
+
for (const source of triggerSources) {
|
|
47
|
+
for (const path of collect(source, route)) found.add(normalise(path));
|
|
48
|
+
}
|
|
49
|
+
return found;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function calledInternalRoutes(workerSources, options = {}) {
|
|
53
|
+
const { worker } = patterns(options.prefix ?? DEFAULT_PREFIX);
|
|
54
|
+
const found = new Map();
|
|
55
|
+
for (const { path, source } of workerSources) {
|
|
56
|
+
for (const called of collect(source, worker)) {
|
|
57
|
+
found.set(normalise(called), path);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return found;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** @returns {{path: string, rule: string, detail: string}[]} */
|
|
64
|
+
export function checkInternalRoutes(declared, called) {
|
|
65
|
+
const problems = [];
|
|
66
|
+
for (const [route, where] of called) {
|
|
67
|
+
if (!declared.has(route)) {
|
|
68
|
+
problems.push({
|
|
69
|
+
path: where,
|
|
70
|
+
rule: "unserved-internal-route",
|
|
71
|
+
detail: `posts to '${route.replace(/\/:/g, "/{id}")}', which no api route declares`,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return problems;
|
|
76
|
+
}
|