@carecard/validate 3.17.0 → 3.19.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/.agents/skills/carecard-workspace-standards/SKILL.md +49 -20
- package/.agents/skills/github-pr-create-update/SKILL.md +22 -1
- package/.agents/skills/github-pr-merge-cleanup/SKILL.md +22 -1
- package/.agents/skills/logged-in-user-profile-page/SKILL.md +22 -1
- package/.agents/skills/pkg-publish/SKILL.md +22 -1
- package/.agents/skills/pkg-validate-coding-standards-and-best-practices/SKILL.md +47 -9
- package/.agents/skills/pkg-validate-validation-library/SKILL.md +45 -14
- package/.agents/skills/software-design-patterns-and-clean-code/SKILL.md +23 -3
- package/.codex/AGENTS.md +41 -10
- package/.github/workflows/auto-draft-pr.yml +173 -151
- package/.github/workflows/ci.yml +35 -32
- package/.husky/pre-commit +4 -4
- package/.prettierrc.js +10 -9
- package/AGENTS.md +19 -0
- package/eslint.config.mjs +60 -8
- package/index.d.ts +108 -107
- package/index.js +6 -6
- package/lib/validate.js +230 -157
- package/lib/validateNewUserRoleRequest.js +68 -60
- package/lib/validateProperties.js +326 -326
- package/lib/validateWhitelistProperties.js +182 -142
- package/lint-staged.config.mjs +36 -0
- package/package.json +66 -60
- package/readme.md +22 -1
- package/scripts/canonicalTestCommand.test.mjs +32 -0
- package/scripts/packageTaskRunner.audit.mjs +37 -0
- package/scripts/packageTaskRunner.test.mjs +64 -0
- package/scripts/runPackageTask.mjs +135 -0
- package/scripts/testOrder/randomizeTestOrder.cjs +35 -25
- package/scripts/testOrder/randomizeTestOrder.test.mjs +19 -19
- package/scripts/testOrder/testOrderPolicy.audit.mjs +54 -0
- package/scripts/testParallel/parallelTestPolicy.audit.mjs +63 -0
- package/scripts/testParallel/runIndexedMochaTests.cjs +45 -43
- package/scripts/testParallel/runIndexedMochaTests.test.mjs +13 -7
- package/scripts/testOrder/testOrderPolicy.test.mjs +0 -48
- package/scripts/testParallel/parallelTestPolicy.test.mjs +0 -39
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import test from 'node:test';
|
|
4
|
+
|
|
5
|
+
const packageJson = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
|
|
6
|
+
const composedCommandPattern = /&&|\|\||\bsh -c\b|\bnode -e\b/u;
|
|
7
|
+
const runnerCommandPattern = /^node scripts\/runPackageTask\.mjs ([A-Za-z0-9:_-]+)$/u;
|
|
8
|
+
|
|
9
|
+
async function loadPackageTaskRunner() {
|
|
10
|
+
return import(new URL('./runPackageTask.mjs', import.meta.url));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
test('delegates composed package tasks to the repository runner', async () => {
|
|
14
|
+
const runnerCommands = Object.entries(packageJson.scripts ?? {}).filter(([, command]) =>
|
|
15
|
+
runnerCommandPattern.test(command),
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
for (const [scriptName, command] of Object.entries(packageJson.scripts ?? {})) {
|
|
19
|
+
assert.doesNotMatch(
|
|
20
|
+
command,
|
|
21
|
+
composedCommandPattern,
|
|
22
|
+
`${scriptName} must delegate composition to runPackageTask.mjs`,
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
assert.ok(runnerCommands.length > 0, 'at least one package task must use the runner');
|
|
27
|
+
const { packageTasks } = await loadPackageTaskRunner();
|
|
28
|
+
for (const [scriptName] of runnerCommands) {
|
|
29
|
+
assert.ok(packageTasks[scriptName], `${scriptName} must have a runner task`);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('does not suppress command execution errors at the CLI boundary', () => {
|
|
34
|
+
const runnerSource = readFileSync(new URL('./runPackageTask.mjs', import.meta.url), 'utf8');
|
|
35
|
+
|
|
36
|
+
assert.doesNotMatch(runnerSource, /catch\s*\{/u);
|
|
37
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
|
|
4
|
+
async function loadPackageTaskRunner() {
|
|
5
|
+
return import(new URL('./runPackageTask.mjs', import.meta.url));
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
test('runs task steps in order and stops at the first failure', async () => {
|
|
9
|
+
const { runPackageTask } = await loadPackageTaskRunner();
|
|
10
|
+
const executedCommands = [];
|
|
11
|
+
const fixtureTasks = {
|
|
12
|
+
fixture: [{ command: 'first' }, { command: 'second' }, { command: 'third' }],
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const exitCode = runPackageTask(
|
|
16
|
+
'fixture',
|
|
17
|
+
step => {
|
|
18
|
+
executedCommands.push(step.command);
|
|
19
|
+
return step.command === 'second' ? 7 : 0;
|
|
20
|
+
},
|
|
21
|
+
fixtureTasks,
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
assert.equal(exitCode, 7);
|
|
25
|
+
assert.deepEqual(executedCommands, ['first', 'second']);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('runs conditional steps only when their required output is missing', async () => {
|
|
29
|
+
const { runPackageTask } = await loadPackageTaskRunner();
|
|
30
|
+
const executedCommands = [];
|
|
31
|
+
const fixtureTasks = {
|
|
32
|
+
fixture: [{ command: 'build', whenMissing: 'dist/runtime.js' }, { command: 'execute' }],
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const exitCode = runPackageTask(
|
|
36
|
+
'fixture',
|
|
37
|
+
step => {
|
|
38
|
+
executedCommands.push(step.command);
|
|
39
|
+
return 0;
|
|
40
|
+
},
|
|
41
|
+
fixtureTasks,
|
|
42
|
+
() => true,
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
assert.equal(exitCode, 0);
|
|
46
|
+
assert.deepEqual(executedCommands, ['execute']);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('merges task environment overrides without mutating inherited values', async () => {
|
|
50
|
+
const { createTaskEnvironment } = await loadPackageTaskRunner();
|
|
51
|
+
const inheritedEnvironment = { NODE_ENV: 'test', PATH: '/bin' };
|
|
52
|
+
|
|
53
|
+
const environment = createTaskEnvironment(
|
|
54
|
+
{ NODE_ENV: 'production', DB_ENV: 'privileged' },
|
|
55
|
+
inheritedEnvironment,
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
assert.deepEqual(environment, {
|
|
59
|
+
NODE_ENV: 'production',
|
|
60
|
+
PATH: '/bin',
|
|
61
|
+
DB_ENV: 'privileged',
|
|
62
|
+
});
|
|
63
|
+
assert.deepEqual(inheritedEnvironment, { NODE_ENV: 'test', PATH: '/bin' });
|
|
64
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, rmSync } from 'node:fs';
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
|
|
6
|
+
export const packageTasks = Object.freeze({
|
|
7
|
+
'validate:audits': [
|
|
8
|
+
{
|
|
9
|
+
command: 'node',
|
|
10
|
+
arguments: [
|
|
11
|
+
'--test',
|
|
12
|
+
'scripts/packageTaskRunner.audit.mjs',
|
|
13
|
+
'scripts/testOrder/testOrderPolicy.audit.mjs',
|
|
14
|
+
'scripts/testParallel/parallelTestPolicy.audit.mjs',
|
|
15
|
+
],
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
command: 'mocha',
|
|
19
|
+
arguments: [
|
|
20
|
+
'--reporter',
|
|
21
|
+
'spec',
|
|
22
|
+
'--timeout',
|
|
23
|
+
'5000',
|
|
24
|
+
'test/config/repositoryIsolation.audit.js',
|
|
25
|
+
'test/config/tddGuidanceDocs.audit.js',
|
|
26
|
+
'test/dependencyOverrides.audit.js',
|
|
27
|
+
],
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
test: [
|
|
31
|
+
{ command: 'npm', arguments: ['run', 'validate:audits'] },
|
|
32
|
+
{ command: 'npm', arguments: ['run', 'test:order'] },
|
|
33
|
+
{ command: 'tsc', arguments: ['--noEmit'] },
|
|
34
|
+
{ command: 'nyc', arguments: ['node', 'test/index.test.js'] },
|
|
35
|
+
],
|
|
36
|
+
'test:types': [
|
|
37
|
+
{ command: 'tsc', arguments: ['--noEmit'] },
|
|
38
|
+
{
|
|
39
|
+
command: 'node',
|
|
40
|
+
arguments: [
|
|
41
|
+
'-e',
|
|
42
|
+
"process.stdout.write('\\n ✔ Type tests passed: tsc --noEmit reported 0 errors across index.d.ts and test/**/*.ts\\n\\n')",
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
'test:coverage': [
|
|
47
|
+
{ command: 'npm', arguments: ['run', 'test:order'] },
|
|
48
|
+
{ command: 'tsc', arguments: ['--noEmit'] },
|
|
49
|
+
{ command: 'nyc', arguments: ['node', 'test/index.test.js'] },
|
|
50
|
+
],
|
|
51
|
+
'test:All': [{ command: 'npm', arguments: ['test'] }],
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
export function createTaskEnvironment(overrides = {}, inheritedEnvironment = process.env) {
|
|
55
|
+
return { ...inheritedEnvironment, ...overrides };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function getTaskExitCode(result) {
|
|
59
|
+
if (result.error) {
|
|
60
|
+
throw result.error;
|
|
61
|
+
}
|
|
62
|
+
if (typeof result.status === 'number') {
|
|
63
|
+
return result.status;
|
|
64
|
+
}
|
|
65
|
+
if (result.signal === 'SIGINT') {
|
|
66
|
+
return 130;
|
|
67
|
+
}
|
|
68
|
+
if (result.signal === 'SIGTERM') {
|
|
69
|
+
return 143;
|
|
70
|
+
}
|
|
71
|
+
return 1;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function executeTaskStep(taskStep) {
|
|
75
|
+
if (taskStep.removePath) {
|
|
76
|
+
rmSync(taskStep.removePath, { recursive: true, force: true });
|
|
77
|
+
return 0;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const result = spawnSync(taskStep.command, taskStep.arguments ?? [], {
|
|
81
|
+
env: createTaskEnvironment(taskStep.environment),
|
|
82
|
+
shell: false,
|
|
83
|
+
stdio: 'inherit',
|
|
84
|
+
});
|
|
85
|
+
return getTaskExitCode(result);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function shouldRunTaskStep(taskStep, pathExists) {
|
|
89
|
+
return !taskStep.whenMissing || !pathExists(taskStep.whenMissing);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function runPackageTask(
|
|
93
|
+
taskName,
|
|
94
|
+
executeTask = executeTaskStep,
|
|
95
|
+
taskDefinitions = packageTasks,
|
|
96
|
+
pathExists = existsSync,
|
|
97
|
+
) {
|
|
98
|
+
const taskSteps = taskDefinitions[taskName];
|
|
99
|
+
if (!Array.isArray(taskSteps)) {
|
|
100
|
+
throw new Error('Unknown package task.');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
for (const taskStep of taskSteps) {
|
|
104
|
+
if (!shouldRunTaskStep(taskStep, pathExists)) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const exitCode = executeTask(taskStep);
|
|
108
|
+
if (exitCode !== 0) {
|
|
109
|
+
return exitCode;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return 0;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function isDirectExecution() {
|
|
116
|
+
if (!process.argv[1]) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
return resolve(process.argv[1]) === fileURLToPath(import.meta.url);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function runCommandLineTask() {
|
|
123
|
+
const taskName = process.argv[2];
|
|
124
|
+
if (!taskName || !packageTasks[taskName]) {
|
|
125
|
+
process.stderr.write('[PACKAGE_TASK_CONFIG] Unknown package task.\n');
|
|
126
|
+
process.exitCode = 2;
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
process.exitCode = runPackageTask(taskName);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (isDirectExecution()) {
|
|
134
|
+
runCommandLineTask();
|
|
135
|
+
}
|
|
@@ -3,38 +3,48 @@
|
|
|
3
3
|
const MAX_TEST_ORDER_SEED = 2_147_483_647;
|
|
4
4
|
|
|
5
5
|
function resolveTestOrderSeed(configuredSeed) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
if (configuredSeed === undefined) {
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
if (!/^[1-9]\d*$/.test(configuredSeed)) {
|
|
10
|
+
throw new Error('TEST_ORDER_SEED must be a positive 32-bit integer.');
|
|
11
|
+
}
|
|
12
|
+
const seed = Number(configuredSeed);
|
|
13
|
+
if (!Number.isSafeInteger(seed) || seed > MAX_TEST_ORDER_SEED) {
|
|
14
|
+
throw new Error('TEST_ORDER_SEED must be a positive 32-bit integer.');
|
|
15
|
+
}
|
|
16
|
+
return seed;
|
|
11
17
|
}
|
|
12
18
|
function createSeededRandom(seed) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
let state = seed;
|
|
20
|
+
return function nextRandomValue() {
|
|
21
|
+
state = (state + 0x6d2b79f5) | 0;
|
|
22
|
+
let value = Math.imul(state ^ (state >>> 15), 1 | state);
|
|
23
|
+
value = (value + Math.imul(value ^ (value >>> 7), 61 | value)) ^ value;
|
|
24
|
+
return ((value ^ (value >>> 14)) >>> 0) / 4_294_967_296;
|
|
25
|
+
};
|
|
20
26
|
}
|
|
21
27
|
function shuffleValues(values, random) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
for (let index = values.length - 1; index > 0; index -= 1) {
|
|
29
|
+
const replacementIndex = Math.floor(random() * (index + 1));
|
|
30
|
+
[values[index], values[replacementIndex]] = [values[replacementIndex], values[index]];
|
|
31
|
+
}
|
|
26
32
|
}
|
|
27
33
|
function shuffleSuiteTree(suite, random) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
for (const childSuite of suite.suites) {
|
|
35
|
+
shuffleSuiteTree(childSuite, random);
|
|
36
|
+
}
|
|
37
|
+
shuffleValues(suite.tests, random);
|
|
38
|
+
shuffleValues(suite.suites, random);
|
|
31
39
|
}
|
|
32
40
|
const mochaHooks = {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
41
|
+
beforeAll() {
|
|
42
|
+
const seed = resolveTestOrderSeed(process.env.TEST_ORDER_SEED);
|
|
43
|
+
if (seed === undefined) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
console.log(`Test order seed: ${seed} (reproduce with TEST_ORDER_SEED=${seed})`);
|
|
47
|
+
shuffleSuiteTree(this.test.parent, createSeededRandom(seed));
|
|
48
|
+
},
|
|
39
49
|
};
|
|
40
50
|
module.exports = { createSeededRandom, mochaHooks, resolveTestOrderSeed, shuffleSuiteTree };
|
|
@@ -6,31 +6,31 @@ import testOrderRandomizer from './randomizeTestOrder.cjs';
|
|
|
6
6
|
const { createSeededRandom, resolveTestOrderSeed, shuffleSuiteTree } = testOrderRandomizer;
|
|
7
7
|
|
|
8
8
|
function createSuiteTree() {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
return {
|
|
10
|
+
suites: [
|
|
11
|
+
{ title: 'alpha', suites: [], tests: [{ title: 'one' }, { title: 'two' }] },
|
|
12
|
+
{ title: 'beta', suites: [], tests: [{ title: 'three' }, { title: 'four' }] },
|
|
13
|
+
{ title: 'gamma', suites: [], tests: [{ title: 'five' }, { title: 'six' }] },
|
|
14
|
+
],
|
|
15
|
+
tests: [{ title: 'root one' }, { title: 'root two' }, { title: 'root three' }],
|
|
16
|
+
};
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
test('uses ordinary ordering unless TEST_ORDER_SEED is explicitly supplied', () => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
assert.strictEqual(resolveTestOrderSeed(undefined), undefined);
|
|
21
|
+
assert.strictEqual(resolveTestOrderSeed('314159'), 314159);
|
|
22
|
+
for (const invalidSeed of ['', '0', '-1', '1.5', 'seed', '2147483648']) {
|
|
23
|
+
assert.throws(() => resolveTestOrderSeed(invalidSeed), /TEST_ORDER_SEED/);
|
|
24
|
+
}
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
test('shuffles nested suites and tests reproducibly', () => {
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
const firstTree = createSuiteTree();
|
|
29
|
+
const secondTree = createSuiteTree();
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
shuffleSuiteTree(firstTree, createSeededRandom(314159));
|
|
32
|
+
shuffleSuiteTree(secondTree, createSeededRandom(314159));
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
assert.deepStrictEqual(firstTree, secondTree);
|
|
35
|
+
assert.notDeepStrictEqual(firstTree, createSuiteTree());
|
|
36
36
|
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { execFileSync } from 'node:child_process';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
import { test } from 'node:test';
|
|
5
|
+
|
|
6
|
+
const TEST_ORDER_INVARIANCE_RULE =
|
|
7
|
+
"Non-negotiable test order invariance rule: Every test must pass independently of which tests run before or after it, and the suite must pass in every execution order. Each test must establish the state it needs, isolate mutable state, and clean up state it owns; it must never rely on another test's setup, mutations, or cleanup. Default test, CI, and Husky commands must use the test framework's ordinary ordering and must not force randomized ordering. Random-order execution is an explicit diagnostic only, and every failure it exposes must be fixed at the root cause.";
|
|
8
|
+
|
|
9
|
+
function listRepositoryFiles() {
|
|
10
|
+
return execFileSync('git', ['ls-files', '--cached', '--others', '--exclude-standard'], {
|
|
11
|
+
encoding: 'utf8',
|
|
12
|
+
})
|
|
13
|
+
.trim()
|
|
14
|
+
.split('\n')
|
|
15
|
+
.filter(Boolean);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function isRequiredTestGuidance(filePath) {
|
|
19
|
+
return (
|
|
20
|
+
/^readme\.md$/i.test(filePath) ||
|
|
21
|
+
filePath === '.codex/AGENTS.md' ||
|
|
22
|
+
filePath === '.junie/guidelines.md' ||
|
|
23
|
+
filePath === '.agents/skills/carecard-workspace-standards/SKILL.md' ||
|
|
24
|
+
/^\.agents\/skills\/[^/]*(?:test|testing)[^/]*\/(?:SKILL\.md|references\/[^/]*(?:test|testing|coding-principles)[^/]*\.md)$/i.test(
|
|
25
|
+
filePath,
|
|
26
|
+
)
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
test('keeps the non-negotiable test order rule in repository guidance', () => {
|
|
31
|
+
const guidanceFiles = listRepositoryFiles().filter(isRequiredTestGuidance);
|
|
32
|
+
assert.ok(guidanceFiles.length > 0, 'No repository test guidance was found.');
|
|
33
|
+
|
|
34
|
+
for (const guidanceFile of guidanceFiles) {
|
|
35
|
+
const normalizedGuidance = readFileSync(guidanceFile, 'utf8').replace(/\s+/g, ' ').trim();
|
|
36
|
+
assert.ok(
|
|
37
|
+
normalizedGuidance.includes(TEST_ORDER_INVARIANCE_RULE),
|
|
38
|
+
`${guidanceFile} must document the non-negotiable test order invariance rule.`,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('keeps default package scripts on the test framework ordinary ordering', () => {
|
|
44
|
+
const packageJson = JSON.parse(readFileSync('package.json', 'utf8'));
|
|
45
|
+
|
|
46
|
+
for (const [scriptName, command] of Object.entries(packageJson.scripts ?? {})) {
|
|
47
|
+
assert.equal(typeof command, 'string', `${scriptName} must be a string command.`);
|
|
48
|
+
assert.doesNotMatch(
|
|
49
|
+
command,
|
|
50
|
+
/--test-randomize|--test-random-seed/,
|
|
51
|
+
`${scriptName} must not force randomized test ordering.`,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { readdirSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { join, relative, resolve } from 'node:path';
|
|
5
|
+
import test from 'node:test';
|
|
6
|
+
|
|
7
|
+
const require = createRequire(import.meta.url);
|
|
8
|
+
const repositoryRoot = resolve(import.meta.dirname, '../..');
|
|
9
|
+
const packageJson = JSON.parse(
|
|
10
|
+
readFileSync(new URL('../../package.json', import.meta.url), 'utf8'),
|
|
11
|
+
);
|
|
12
|
+
const packageTaskRunnerSource = readFileSync(
|
|
13
|
+
new URL('../runPackageTask.mjs', import.meta.url),
|
|
14
|
+
'utf8',
|
|
15
|
+
);
|
|
16
|
+
const testIndexSource = readFileSync(new URL('../../test/index.test.js', import.meta.url), 'utf8');
|
|
17
|
+
const { parallelTestFiles } = require('../../test/index.test.js');
|
|
18
|
+
|
|
19
|
+
function listRuntimeTestFiles(directoryPath) {
|
|
20
|
+
return readdirSync(directoryPath, { withFileTypes: true }).flatMap(entry => {
|
|
21
|
+
const entryPath = join(directoryPath, entry.name);
|
|
22
|
+
if (entry.isDirectory()) {
|
|
23
|
+
return listRuntimeTestFiles(entryPath);
|
|
24
|
+
}
|
|
25
|
+
if (!/\.test\.(?:js|mjs)$/.test(entry.name) || entry.name === 'index.test.js') {
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
return [relative(repositoryRoot, entryPath)];
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
test('keeps runtime test selection in the index and package scripts short', () => {
|
|
33
|
+
assert.equal(packageJson.scripts.test, 'node scripts/runPackageTask.mjs test');
|
|
34
|
+
assert.equal(
|
|
35
|
+
packageJson.scripts['test:coverage'],
|
|
36
|
+
'node scripts/runPackageTask.mjs test:coverage',
|
|
37
|
+
);
|
|
38
|
+
assert.match(packageTaskRunnerSource, /arguments: \['run', 'test:order'\]/);
|
|
39
|
+
assert.match(packageTaskRunnerSource, /arguments: \['node', 'test\/index\.test\.js'\]/);
|
|
40
|
+
assert.match(packageTaskRunnerSource, /command: 'nyc'/);
|
|
41
|
+
assert.match(testIndexSource, /parallelTestFiles/);
|
|
42
|
+
assert.match(testIndexSource, /runIndexedMochaTests/);
|
|
43
|
+
assert.match(testIndexSource, /if \(require\.main === module\)/);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('runs static parallel policy in the audit gate and callable runner behavior in the test-order gate', () => {
|
|
47
|
+
assert.equal(
|
|
48
|
+
packageJson.scripts['validate:audits'],
|
|
49
|
+
'node scripts/runPackageTask.mjs validate:audits',
|
|
50
|
+
);
|
|
51
|
+
assert.match(packageTaskRunnerSource, /scripts\/testParallel\/parallelTestPolicy\.audit\.mjs/);
|
|
52
|
+
assert.match(
|
|
53
|
+
packageJson.scripts['test:order'],
|
|
54
|
+
/scripts\/testParallel\/runIndexedMochaTests\.test\.mjs/,
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('selects every runtime test file exactly once', () => {
|
|
59
|
+
assert.deepEqual(
|
|
60
|
+
[...parallelTestFiles].sort(),
|
|
61
|
+
listRuntimeTestFiles(resolve(repositoryRoot, 'test')).sort(),
|
|
62
|
+
);
|
|
63
|
+
});
|
|
@@ -9,63 +9,65 @@ const DEFAULT_MAX_PARALLEL_JOBS = 4;
|
|
|
9
9
|
|
|
10
10
|
// Pattern: Configuration Boundary - bounds workers without accepting invalid input.
|
|
11
11
|
function resolveParallelJobCount(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
configuredJobCount,
|
|
13
|
+
testFileCount,
|
|
14
|
+
defaultMaximum = DEFAULT_MAX_PARALLEL_JOBS,
|
|
15
|
+
availableJobCount = availableParallelism(),
|
|
16
16
|
) {
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
const requestedJobCount =
|
|
18
|
+
configuredJobCount === undefined
|
|
19
|
+
? Math.min(availableJobCount, defaultMaximum)
|
|
20
|
+
: Number.parseInt(configuredJobCount, 10);
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
if (!Number.isInteger(requestedJobCount) || requestedJobCount < 1) {
|
|
23
|
+
throw new Error('TEST_PARALLEL_JOBS must be a positive integer.');
|
|
24
|
+
}
|
|
25
|
+
return Math.min(requestedJobCount, testFileCount);
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
// Pattern: Command Builder - keeps Mocha worker details out of package metadata.
|
|
27
29
|
function buildMochaArguments(testFiles, jobCount) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
30
|
+
const requireFromRunner = createRequire(__filename);
|
|
31
|
+
return [
|
|
32
|
+
requireFromRunner.resolve('mocha/bin/mocha.js'),
|
|
33
|
+
'--parallel',
|
|
34
|
+
'--jobs',
|
|
35
|
+
String(jobCount),
|
|
36
|
+
'--require',
|
|
37
|
+
resolve('scripts/testOrder/randomizeTestOrder.cjs'),
|
|
38
|
+
...testFiles,
|
|
39
|
+
];
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
// Pattern: Process Adapter - returns the exact test process result to the index.
|
|
41
43
|
function runIndexedMochaTests(testFiles) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
44
|
+
if (testFiles.length === 0) {
|
|
45
|
+
throw new Error('The package test index must select at least one test file.');
|
|
46
|
+
}
|
|
45
47
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
const jobCount = resolveParallelJobCount(process.env.TEST_PARALLEL_JOBS, testFiles.length);
|
|
49
|
+
const child = spawn(process.execPath, buildMochaArguments(testFiles, jobCount), {
|
|
50
|
+
env: {
|
|
51
|
+
...process.env,
|
|
52
|
+
NODE_ENV: 'test',
|
|
53
|
+
},
|
|
54
|
+
stdio: 'inherit',
|
|
55
|
+
});
|
|
54
56
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
});
|
|
57
|
+
return new Promise((resolveExit, rejectExit) => {
|
|
58
|
+
child.once('error', rejectExit);
|
|
59
|
+
child.once('exit', (code, signal) => {
|
|
60
|
+
if (signal) {
|
|
61
|
+
rejectExit(new Error(`Mocha exited from signal ${signal}.`));
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
resolveExit(code ?? 1);
|
|
64
65
|
});
|
|
66
|
+
});
|
|
65
67
|
}
|
|
66
68
|
|
|
67
69
|
module.exports = {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
buildMochaArguments,
|
|
71
|
+
resolveParallelJobCount,
|
|
72
|
+
runIndexedMochaTests,
|
|
71
73
|
};
|
|
@@ -6,16 +6,22 @@ const require = createRequire(import.meta.url);
|
|
|
6
6
|
const { buildMochaArguments, resolveParallelJobCount } = require('./runIndexedMochaTests.cjs');
|
|
7
7
|
|
|
8
8
|
test('uses bounded Mocha file workers without randomized default ordering', () => {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
assert.equal(resolveParallelJobCount(undefined, 8, 4, 12), 4);
|
|
10
|
+
assert.equal(resolveParallelJobCount('2', 8, 4, 12), 2);
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
const argumentsList = buildMochaArguments(['test/example.test.js'], 2);
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
assert.ok(argumentsList.includes('--parallel'));
|
|
15
|
+
assert.deepEqual(
|
|
16
|
+
argumentsList.slice(argumentsList.indexOf('--jobs'), argumentsList.indexOf('--jobs') + 2),
|
|
17
|
+
['--jobs', '2'],
|
|
18
|
+
);
|
|
19
|
+
assert.ok(argumentsList.includes('test/example.test.js'));
|
|
17
20
|
});
|
|
18
21
|
|
|
19
22
|
test('rejects invalid worker configuration instead of changing execution silently', () => {
|
|
20
|
-
|
|
23
|
+
assert.throws(
|
|
24
|
+
() => resolveParallelJobCount('0', 8, 4, 12),
|
|
25
|
+
/TEST_PARALLEL_JOBS must be a positive integer/,
|
|
26
|
+
);
|
|
21
27
|
});
|