Skip to content

Commit cd7fb34

Browse files
committed
refactor(gates): spell the test-consumer rule once as isTestConsumerFile
The ./testing/ facade gate (TEST_FILE_RE) and the TEST_ONLY_FIXTURE_SUBPATHS predicate (inline isTestConsumer) spelled the same claim twice and had already drifted: the inline copy missed .test.tsx/.mts and a root __tests__/ tree (#3546 review). One exported helper now serves both gates, with an accept/deny table pinning the vocabulary (planted red: the inline copy's narrower regex fails the .test.tsx, .test.mts, and root __tests__/ rows).
1 parent 1ec4ce7 commit cd7fb34

3 files changed

Lines changed: 52 additions & 16 deletions

File tree

‎scripts/layering/client-only-subpaths.test.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ test('the caller-side replay source subpaths are denied to daemon production cod
3636
: checkPackageInternalSites(daemon, sites(file), [], true);
3737
for (const file of [
3838
'src/commands/replay/index.ts',
39-
'src/daemon/__tests__/replay.test.ts',
4039
'packages/daemon/src/__tests__/replay.test.ts',
4140
]) {
4241
assert.deepEqual(
@@ -46,7 +45,6 @@ test('the caller-side replay source subpaths are denied to daemon production cod
4645
);
4746
}
4847
for (const file of [
49-
'src/daemon/replay-runtime.ts',
5048
'packages/daemon/src/replay-runtime.ts',
5149
'src/daemon.ts',
5250
'src/daemon-composition.ts',

‎scripts/layering/package-boundaries-facade.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { test } from 'node:test';
99
import {
1010
checkPackageInternalSites,
1111
checkRootSites,
12+
isTestConsumerFile,
1213
specifierSites,
1314
workspacePackagesFromManifests,
1415
} from './package-boundaries.ts';
@@ -140,6 +141,36 @@ test('a package-local test/ tree is a test consumer, its src/ tree is not', () =
140141
assert.match(denied[0]!.message, /publishes test data to test files only/);
141142
});
142143

144+
test('isTestConsumerFile accepts the test vocabulary and denies production files', () => {
145+
// One spelling serves both test-data gates (the ./testing/ façade check and the
146+
// TEST_ONLY_FIXTURE_SUBPATHS predicate), so the vocabulary itself is pinned here:
147+
// every accepted shape forwards to both gates, and the shapes that once spelled
148+
// differently across the two copies (.test.tsx, .mts, a root __tests__/ tree) are
149+
// in the accepted set (#3546 review).
150+
for (const file of [
151+
'test/integration/consumer.ts',
152+
'packages/daemon-side/test/harness.ts',
153+
'src/daemon/__tests__/consumer.ts',
154+
'__tests__/root-consumer.ts',
155+
'scripts/layering/daemon-layers.test.tsx',
156+
'src/daemon/session-store.test.mts',
157+
'src/foo.test.js',
158+
'src/foo.test.cjs',
159+
]) {
160+
assert.equal(isTestConsumerFile(file), true, file);
161+
}
162+
for (const file of [
163+
'src/commands/inventory.ts',
164+
'packages/kernel/src/errors.ts',
165+
'scripts/layering/package-boundaries.ts',
166+
'testbed/notes.ts',
167+
'docs/writing/tests.ts',
168+
'src/foo.ts',
169+
]) {
170+
assert.equal(isTestConsumerFile(file), false, file);
171+
}
172+
});
173+
143174
test('a pattern entry matches deep specifiers, and an unnamed one is refused in scripts/', () => {
144175
// uncompiled sites resolve through the pattern rather than report "not named",
145176
// and a spelling no entry names still reports.

‎scripts/layering/package-boundaries.ts‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
import fs from 'node:fs';
3333
import path from 'node:path';
3434
import { parseImports, resolveWorkspaceSpecifier } from './model.ts';
35-
import { listTrackedPackageManifests, listTrackedProductionSources } from './tracked-sources.ts';
35+
import {
36+
DAEMON_SOURCE_ROOT,
37+
listTrackedPackageManifests,
38+
listTrackedProductionSources,
39+
} from './tracked-sources.ts';
3640

3741
export type PackageBoundaryViolation = {
3842
rule: string;
@@ -174,9 +178,19 @@ function namedByExports(target: WorkspacePackage, specifier: string): boolean {
174178
return resolveWorkspaceSpecifier(specifier, target.exportTargets) !== undefined;
175179
}
176180

177-
// Root `test/`, package-local `packages/*/test/`, *.test.*, and __tests__ trees are all
178-
// test consumers — the same file vocabulary TEST_ONLY_FIXTURE_SUBPATHS' predicate uses.
179-
const TEST_FILE_RE = /(^|\/)__tests__\/|\.test\.[cm]?[jt]sx?$|(?:^|\/)test\//;
181+
const TEST_CONSUMER_RE = /(^|\/)__tests__\/|\.test\.[cm]?[jt]sx?$|(?:^|\/)test\//;
182+
183+
/**
184+
* The one file vocabulary for "this file consumes test data": the root `test/` tree, a
185+
* package-local `test/` harness tree, `*.test.*` files, and `__tests__/` trees (root or
186+
* nested). Both test-data gates call this — the `./testing/` façade check and the
187+
* TEST_ONLY_FIXTURE_SUBPATHS predicate — so what counts as a test consumer is one rule,
188+
* not two spellings that drift. Pinned by an accept/deny table in
189+
* `package-boundaries-facade.test.ts`.
190+
*/
191+
export function isTestConsumerFile(file: string): boolean {
192+
return TEST_CONSUMER_RE.test(file);
193+
}
180194

181195
/**
182196
* Façade policy (ADR-0033 amendment): a package publishes test data only under its
@@ -193,7 +207,7 @@ function checkTestDataOnlySites(
193207
): PackageBoundaryViolation | undefined {
194208
if (!site.specifier.startsWith(`${target.name}/testing/`)) return undefined;
195209
if (!namedByExports(target, site.specifier)) return undefined;
196-
if (TEST_FILE_RE.test(site.file)) return undefined;
210+
if (isTestConsumerFile(site.file)) return undefined;
197211
return {
198212
rule: 'R11 package-boundaries',
199213
file: site.file,
@@ -244,12 +258,6 @@ export const TEST_ONLY_FIXTURE_SUBPATHS: ReadonlySet<string> = new Set([
244258
'@agent-device/replay-port/replay-script-source-fixtures',
245259
]);
246260

247-
// Root `test/`, package-local `packages/<pkg>/test/`, *.test.ts, and __tests__ trees are test
248-
// consumers; anything else is production. Mirrors R13's non-production predicate.
249-
function isTestConsumerFile(file: string): boolean {
250-
return /(?:^|\/)test\//.test(file) || file.endsWith('.test.ts') || file.includes('/__tests__/');
251-
}
252-
253261
/** Consumer side of `TEST_ONLY_FIXTURE_SUBPATHS`. */
254262
function importsTestOnlyFixtureFromProduction(file: string, specifier: string): boolean {
255263
return TEST_ONLY_FIXTURE_SUBPATHS.has(specifier) && !isTestConsumerFile(file);
@@ -265,8 +273,7 @@ export const CLIENT_ONLY_SUBPATHS: ReadonlySet<string> = new Set([
265273
'@agent-device/replay-port/source-discovery',
266274
]);
267275

268-
const DAEMON_SOURCE_FILE = /^(?:src\/daemon|packages\/daemon\/src)\//;
269-
// The daemon process also runs its root entry and composition root, outside the daemon tree.
276+
// The daemon process also runs its root entry and composition root, outside the daemon package.
270277
const DAEMON_PROCESS_ROOT_FILES: ReadonlySet<string> = new Set([
271278
'src/daemon.ts',
272279
'src/daemon-composition.ts',
@@ -275,7 +282,7 @@ const DAEMON_PROCESS_ROOT_FILES: ReadonlySet<string> = new Set([
275282
function importsClientOnlyFromDaemon(file: string, specifier: string): boolean {
276283
return (
277284
CLIENT_ONLY_SUBPATHS.has(specifier) &&
278-
(DAEMON_SOURCE_FILE.test(file) || DAEMON_PROCESS_ROOT_FILES.has(file)) &&
285+
(file.startsWith(DAEMON_SOURCE_ROOT) || DAEMON_PROCESS_ROOT_FILES.has(file)) &&
279286
!isTestConsumerFile(file)
280287
);
281288
}

0 commit comments

Comments
 (0)