Files
TREK/server/scripts/test-new-service-ratchet.mjs
T
Maurice 4427c435a8 ci(server): module mocks, hand-built services and test file length in tests/ may only shrink
lint:test-mocks counts vi.mock/vi.doMock of src/config, src/nest/realtime and src/db per file under tests/, lint:test-new-service counts new XService(/new XRepository( there with every class whose constructor takes nothing allowed (derived from src/ and tests/helpers/), and lint:size holds files under tests/ to 2000 lines against their own baseline. Both counters share scripts/lib/count-ratchet.mjs, which gains a prepare hook for a check that reads the tree first. The three run as steps of the existing server lint job; the CLAUDE.md files and the module guide describe the test helpers they point to.
2026-10-10 20:16:12 +02:00

152 lines
5.7 KiB
JavaScript

#!/usr/bin/env node
/*
* lint:test-new-service: hand-built services and repositories in tests/ may
* only shrink.
*
* `new XService(a, b, c, ...)` in a test pins the constructor's parameter
* order: every dependency a service gains is a change to every suite that
* builds it (and to every suite that builds one of its consumers), which is
* why the harness files are among the most churned in the repository. A suite
* takes its subject from a Nest testing module instead (tests/helpers/
* test-module.ts: createTestModule with overrides), and a booted app hands
* the rest out through `app.get(...)`.
*
* Every `new <Name>Service(` and `new <Name>Repository(` under tests/ counts,
* per file, against scripts/test-new-service-baseline.json; a file without an
* entry may hold none (scripts/lib/count-ratchet.mjs has the rules). The
* allowlist is derived, not kept by hand: a class whose constructor takes no
* parameters (no constructor of its own, or an empty one, down its chain of
* `extends` inside src/ and tests/helpers/) has no wiring to pin, so building
* it with `new` does not count. RealtimeService and FakeRealtimeService are
* the common case.
*
* npm run lint:test-new-service check against the baseline (CI)
* npm run lint:test-new-service -- --update lower the counts; never raises or adds an entry
*/
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { cliMain } from './lib/count-ratchet.mjs';
const NEW_CALL = /\bnew\s+([A-Z][A-Za-z0-9_]*(?:Service|Repository))\s*[(<]/g;
const CLASS_DECL = /\bclass\s+([A-Za-z_$][\w$]*)(?:\s*<[^{]*?>)?(?:\s+extends\s+([A-Za-z_$][\w$.]*))?[^{]*\{/g;
/** The trees whose classes the allowlist is derived from, relative to the server root. */
const CLASS_ROOTS = ['src', 'tests/helpers'];
function walk(dir, files = []) {
for (const name of readdirSync(dir)) {
if (name === 'node_modules') continue;
const path = join(dir, name);
if (statSync(path).isDirectory()) walk(path, files);
else if (/\.[cm]?ts$/.test(name)) files.push(path);
}
return files;
}
/** The index just past the bracket that closes the one at `open`, skipping strings and comments. */
function closeOf(text, open) {
const pairs = { '(': ')', '{': '}', '[': ']' };
const stack = [];
for (let i = open; i < text.length; i++) {
const c = text[i];
if (c === '/' && text[i + 1] === '/') {
i = text.indexOf('\n', i);
if (i < 0) return text.length;
continue;
}
if (c === '/' && text[i + 1] === '*') {
i = text.indexOf('*/', i + 2) + 1;
if (i <= 0) return text.length;
continue;
}
if (c === '"' || c === "'" || c === '`') {
for (i++; i < text.length && text[i] !== c; i++) if (text[i] === '\\') i++;
continue;
}
if (pairs[c]) stack.push(pairs[c]);
else if (c === stack[stack.length - 1]) {
stack.pop();
if (!stack.length) return i + 1;
}
}
return text.length;
}
/**
* Every class declared in `text`: its parent, and whether its own constructor
* takes parameters (`null` when it declares none).
*/
export function parseClasses(text) {
const classes = [];
for (const match of text.matchAll(CLASS_DECL)) {
const bodyOpen = match.index + match[0].length - 1;
const body = text.slice(bodyOpen, closeOf(text, bodyOpen));
const ctor = /\bconstructor\s*\(/.exec(body);
let ctorHasParams = null;
if (ctor) {
const open = ctor.index + ctor[0].length - 1;
const params = body
.slice(open + 1, closeOf(body, open) - 1)
.replace(/\/\*[\s\S]*?\*\//g, '')
.replace(/\/\/.*$/gm, '')
.trim();
ctorHasParams = params.length > 0;
}
classes.push({ name: match[1], parent: match[2] ?? null, ctorHasParams });
}
return classes;
}
/**
* The names whose constructor takes no parameters, resolved down the extends
* chain. A parent declared outside the scanned trees (a library class) is
* taken to need arguments, so a subclass without a constructor of its own counts.
*/
export function zeroArgClasses(classes) {
const byName = new Map();
for (const c of classes) if (!byName.has(c.name)) byName.set(c.name, c);
const memo = new Map();
const resolve = (name, seen = new Set()) => {
if (memo.has(name)) return memo.get(name);
const c = byName.get(name);
let zero;
if (!c || seen.has(name)) zero = false;
else if (c.ctorHasParams !== null) zero = !c.ctorHasParams;
else zero = c.parent === null ? true : resolve(c.parent, new Set([...seen, name]));
memo.set(name, zero);
return zero;
};
return new Set([...byName.keys()].filter((name) => resolve(name)));
}
/** The hand-built services and repositories in one file's text, `allowed` left out. */
export function countNewServices(text, allowed = new Set()) {
let n = 0;
for (const match of text.matchAll(NEW_CALL)) if (!allowed.has(match[1])) n++;
return n;
}
export const check = {
name: 'test-new-service',
script: 'lint:test-new-service',
root: 'tests',
baseline: 'scripts/test-new-service-baseline.json',
unit: 'hand-built service(s) or repository(ies)',
advice:
'Take the subject from createTestModule (tests/helpers/test-module.ts) with overrides for its ' +
'collaborators, or from a booted app with app.get(), instead of calling its constructor.',
prepare: (serverDir) => {
const classes = [];
for (const root of CLASS_ROOTS) {
const dir = join(serverDir, root);
if (!existsSync(dir)) continue;
for (const path of walk(dir)) classes.push(...parseClasses(readFileSync(path, 'utf8')));
}
return zeroArgClasses(classes);
},
include: (file) => /\.[cm]?[jt]s$/.test(file),
count: (text, _file, allowed) => countNewServices(text, allowed),
};
cliMain(check, import.meta.url);