architecture-harness 0.6.0 → 0.7.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "architecture-harness",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "type": "module",
5
5
  "description": "Architecture gates, lint rules and scaffolding for the bounded-vertical-slice architecture",
6
6
  "license": "MIT",
@@ -1,5 +1,5 @@
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.
1
+ // Test path mirror: a test lives in its root's tests folder at its source's path, never in src. A folder
2
+ // root matches a source folder, for a block test that spans files; a testOnly root names no source.
3
3
 
4
4
  const TEST_SUFFIX = /\.test\.[cm]?[jt]sx?$/;
5
5
  const SOURCE_EXTENSION = /\.[cm]?[jt]sx?$/;
@@ -7,21 +7,23 @@ const SOURCE_EXTENSION = /\.[cm]?[jt]sx?$/;
7
7
  const folder = (value) => value.replace(/\/+$/, "");
8
8
  const holds = (dir, file) => file.startsWith(`${dir}/`);
9
9
  const named = (value) => typeof value === "string" && folder(value) !== "";
10
+ const MATCHES = ["file", "folder"];
10
11
 
11
12
  /** The roots as clean folders, or the problems that name each malformed one. */
12
13
  function readRoots(roots) {
13
14
  if (!Array.isArray(roots)) {
14
- const detail = `testMirror.roots must be a list of { src, tests, testOnly? }, got ${JSON.stringify(roots)}`;
15
+ const detail = `testMirror.roots must be a list of { src, tests, match?, testOnly? }, got ${JSON.stringify(roots)}`;
15
16
  return { valid: [], problems: [{ path: "qc.config.json", rule: "invalid-mirror-root", detail }] };
16
17
  }
17
18
  const valid = [];
18
19
  const problems = [];
19
20
  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 });
21
+ const match = root?.match ?? "file";
22
+ if (named(root?.src) && named(root?.tests) && MATCHES.includes(match)) {
23
+ valid.push({ src: folder(root.src), tests: folder(root.tests), match, testOnly: root.testOnly === true });
22
24
  return;
23
25
  }
24
- const detail = `testMirror.roots[${index}] needs a src folder and a tests folder, got ${JSON.stringify(root)}`;
26
+ const detail = `testMirror.roots[${index}] needs a src folder, a tests folder, and a match of ${MATCHES.join(" or ")}, got ${JSON.stringify(root)}`;
25
27
  problems.push({ path: "qc.config.json", rule: "invalid-mirror-root", detail });
26
28
  });
27
29
  return { valid, problems };
@@ -33,13 +35,20 @@ export function mirrorFolders(roots) {
33
35
  return [...new Set(valid.flatMap((root) => [root.src, root.tests]))];
34
36
  }
35
37
 
36
- function judge(testFile, root, srcStems) {
38
+ function judge(testFile, root, srcStems, srcFolders) {
37
39
  if (!holds(root.tests, testFile)) {
38
40
  const detail = `${testFile}: a test in ${root.src}/ lives in ${root.tests}/, at the path that mirrors its source`;
39
41
  return { path: testFile, rule: "unmirrored-test", detail };
40
42
  }
41
43
  if (root.testOnly) return null;
42
44
  const stem = testFile.slice(root.tests.length + 1).replace(TEST_SUFFIX, "");
45
+ if (root.match === "folder") {
46
+ const within = stem.includes("/") ? stem.slice(0, stem.lastIndexOf("/")) : "";
47
+ const sourceFolder = within ? `${root.src}/${within}` : root.src;
48
+ if (srcFolders.has(sourceFolder)) return null;
49
+ const detail = `${testFile}: no source folder at ${sourceFolder} for this test in ${root.tests}/`;
50
+ return { path: testFile, rule: "orphaned-test", detail };
51
+ }
43
52
  const source = `${root.src}/${stem}`;
44
53
  if (srcStems.has(source) || srcStems.has(`${source}/index`)) return null;
45
54
  const detail = `${testFile}: no source at ${source} or ${source}/index for this test in ${root.tests}/`;
@@ -52,15 +61,19 @@ function judge(testFile, root, srcStems) {
52
61
  *
53
62
  * @param {string[]} testFiles repository-relative paths of the test files
54
63
  * @param {string[]} srcFiles repository-relative paths of the source files
55
- * @param {{src: string, tests: string, testOnly?: boolean}[]} roots `testMirror.roots`
64
+ * @param {{src: string, tests: string, match?: "file" | "folder", testOnly?: boolean}[]} roots `testMirror.roots`
56
65
  * @returns {{path: string, rule: string, detail: string}[]}
57
66
  */
58
67
  export function checkTestMirror(testFiles, srcFiles, roots) {
59
68
  const { valid, problems } = readRoots(roots);
60
69
  const srcStems = new Set(srcFiles.map((file) => file.replace(SOURCE_EXTENSION, "")));
70
+ const srcFolders = new Set();
71
+ for (const file of srcFiles) {
72
+ for (let at = file.lastIndexOf("/"); at > 0; at = file.lastIndexOf("/", at - 1)) srcFolders.add(file.slice(0, at));
73
+ }
61
74
  for (const testFile of testFiles) {
62
75
  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;
76
+ const problem = root ? judge(testFile, root, srcStems, srcFolders) : null;
64
77
  if (problem) problems.push(problem);
65
78
  }
66
79
  return problems;
@@ -92,3 +92,37 @@ test("a root without a src and a tests folder is a problem that names its key, n
92
92
  assert.deepEqual(verdict(problems), [{ path: "qc.config.json", rule: "invalid-mirror-root" }]);
93
93
  assert.ok(problems[0].detail.includes("testMirror.roots[0]"), problems[0].detail);
94
94
  });
95
+
96
+ const BACKEND_BY_FOLDER = [{ src: "backend/src", tests: "backend/tests", match: "folder" }];
97
+ const BACKEND_SOURCES = ["backend/src/features/audit-log/schema.ts", "backend/src/features/audit-log/shared/queries.ts"];
98
+
99
+ test("a folder root accepts a block test whose folder mirrors a source folder", () => {
100
+ const problems = checkTestMirror(["backend/tests/features/audit-log/record.test.ts"], BACKEND_SOURCES, BACKEND_BY_FOLDER);
101
+ assert.deepEqual(problems, []);
102
+ });
103
+
104
+ test("a folder root refuses a test whose folder mirrors no source folder", () => {
105
+ const problems = checkTestMirror(["backend/tests/features/gone/record.test.ts"], BACKEND_SOURCES, BACKEND_BY_FOLDER);
106
+ assert.equal(problems.length, 1);
107
+ assert.equal(problems[0].rule, "orphaned-test");
108
+ assert.match(problems[0].detail, /backend\/src\/features\/gone/);
109
+ });
110
+
111
+ test("a folder root still refuses a test left beside its source in src", () => {
112
+ const problems = checkTestMirror(["backend/src/features/audit-log/record.test.ts"], BACKEND_SOURCES, BACKEND_BY_FOLDER);
113
+ assert.equal(problems[0].rule, "unmirrored-test");
114
+ });
115
+
116
+ test("a file root still refuses the same block test, and file is the default", () => {
117
+ for (const roots of [[{ src: "backend/src", tests: "backend/tests", match: "file" }], [{ src: "backend/src", tests: "backend/tests" }]]) {
118
+ const problems = checkTestMirror(["backend/tests/features/audit-log/record.test.ts"], BACKEND_SOURCES, roots);
119
+ assert.equal(problems.length, 1);
120
+ assert.equal(problems[0].rule, "orphaned-test");
121
+ }
122
+ });
123
+
124
+ test("a match value other than file or folder is an invalid root", () => {
125
+ const problems = checkTestMirror([], BACKEND_SOURCES, [{ src: "backend/src", tests: "backend/tests", match: "stem" }]);
126
+ assert.equal(problems.length, 1);
127
+ assert.equal(problems[0].rule, "invalid-mirror-root");
128
+ });