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.
This commit is contained in:
Maurice
2026-10-10 18:49:30 +02:00
parent 9bb5854dbb
commit 4427c435a8
14 changed files with 1056 additions and 39 deletions
+6 -2
View File
@@ -1,5 +1,6 @@
/*
* The per-file count ratchet shared by lint:mcp-zod and lint:service-http.
* The per-file count ratchet shared by lint:mcp-zod, lint:service-http,
* lint:test-mocks and lint:test-new-service.
*
* A check names the tree it walks, which files in it count and how to count one
* file's text. Every file is held to its entry in the check's baseline JSON
@@ -31,11 +32,14 @@ export function scan(serverDir, check) {
`${check.root}/ does not exist under ${serverDir}: the check would pass without looking at anything`,
);
}
// A check that needs to read the tree first (a list of class names, say)
// builds that context once here; count() gets it with every file.
const context = check.prepare ? check.prepare(serverDir) : undefined;
const counts = {};
for (const path of walk(dir)) {
const file = relative(serverDir, path).split('\\').join('/');
if (!check.include(file)) continue;
const n = check.count(readFileSync(path, 'utf8'), file);
const n = check.count(readFileSync(path, 'utf8'), file, context);
if (n > 0) counts[file] = n;
}
return counts;
+64 -29
View File
@@ -15,6 +15,11 @@
* --update lowers it: otherwise the file could grow back unseen, and a new
* file at a deleted path would inherit its allowance.
*
* Tests are held the same way under their own, wider limit: a file under
* tests/ (helpers and fixtures included) may hold TEST_LIMIT lines, the longer
* ones are listed in scripts/test-size-baseline.json, and a suite that needs
* to grow is split by the behaviour it covers.
*
* Lines are counted the way they read, not the way they are stored: a line
* longer than LINE_WIDTH (prettier's printWidth) counts once per LINE_WIDTH
* columns it spans, so joining lines does not buy room under the limit.
@@ -42,22 +47,42 @@ import {
writeBaseline,
} from '../../scripts/lib/ratchet.mjs';
/** The most lines a file without a baseline entry may hold. */
/** The most lines a source file without a baseline entry may hold. */
const LIMIT = 1000;
/** The most lines a test file without a baseline entry may hold. */
const TEST_LIMIT = 2000;
/** prettier's printWidth for the server: a line past it counts once per width it spans. */
const LINE_WIDTH = 120;
/** The trees the check walks, relative to the server root. Each one must exist. */
const ROOTS = ['src', 'scripts'];
const SOURCE = /\.(?:[cm]?[jt]s)$/;
const TEST = /\.(?:test|spec)\./;
const accepts = (key) => {
const name = key.slice(key.lastIndexOf('/') + 1);
return SOURCE.test(name) && !TEST.test(name);
};
const nameOf = (key) => key.slice(key.lastIndexOf('/') + 1);
/**
* What the check holds. Each scope walks its trees (relative to the server
* root; each one must exist) against its own limit and baseline file.
*/
const SCOPES = [
{
label: 'size',
roots: ['src', 'scripts'],
limit: LIMIT,
baseline: 'size-baseline.json',
accepts: (key) => SOURCE.test(nameOf(key)) && !TEST.test(nameOf(key)),
advice: 'Split a concern into a service, helper or module of its own instead of growing the file.',
},
{
label: 'size (tests)',
roots: ['tests'],
limit: TEST_LIMIT,
baseline: 'test-size-baseline.json',
accepts: (key) => SOURCE.test(nameOf(key)),
advice: 'Split the suite by the behaviour it covers instead of growing the file.',
},
];
/**
* Lines as an editor numbers them (a final newline does not start another),
@@ -73,48 +98,48 @@ function lineCount(text) {
}
/** Weighted line counts keyed by the server-relative POSIX path. A missing root is an error. */
function scan(serverDir) {
function scan(serverDir, scope) {
const counts = {};
for (const path of listFiles(serverDir, ROOTS, accepts)) counts[toKey(serverDir, path)] = lineCount(readText(path));
for (const path of listFiles(serverDir, scope.roots, scope.accepts)) {
counts[toKey(serverDir, path)] = lineCount(readText(path));
}
return counts;
}
/** Every entry must be a whole number above the limit: anything else is a hand edit gone wrong. */
function sizeEntries(value) {
const sizeEntries = (limit) => (value) => {
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
return 'it must be an object of file paths to line counts';
}
for (const [file, n] of Object.entries(value)) {
if (!Number.isInteger(n) || n <= LIMIT) return `${file} holds ${JSON.stringify(n)}, expected an integer above ${LIMIT}`;
if (!Number.isInteger(n) || n <= limit) return `${file} holds ${JSON.stringify(n)}, expected an integer above ${limit}`;
}
return null;
}
};
function check(argv) {
const dirArg = argv.find((a) => a.startsWith('--dir='));
const serverDir = dirArg ? resolve(dirArg.slice('--dir='.length)) : fileURLToPath(new URL('..', import.meta.url));
const baselinePath = join(serverDir, 'scripts', 'size-baseline.json');
/** One scope against its baseline; answers whether it failed. */
function checkScope(serverDir, scope, update) {
const { limit } = scope;
const baselinePath = join(serverDir, 'scripts', scope.baseline);
const counts = scan(serverDir);
let baseline = readBaseline(baselinePath, sizeEntries);
if (argv.includes('--update')) {
baseline = lowerCounts(baseline, counts, LIMIT);
const counts = scan(serverDir, scope);
let baseline = readBaseline(baselinePath, sizeEntries(limit));
if (update) {
baseline = lowerCounts(baseline, counts, limit);
writeBaseline(baselinePath, baseline);
}
const grown = Object.entries(counts).filter(([file, n]) => n > Math.max(baseline[file] ?? 0, LIMIT));
const stale = staleCounts(baseline, counts, LIMIT);
const grown = Object.entries(counts).filter(([file, n]) => n > Math.max(baseline[file] ?? 0, limit));
const stale = staleCounts(baseline, counts, limit);
for (const [file, n] of grown) {
const entry = baseline[file];
console.error(
`FAIL ${file}: ${n} lines, ` +
(entry ? `its baseline is ${entry}. ` : `the limit is ${LIMIT}. `) +
'Split a concern into a service, helper or module of its own instead of growing the file.',
`FAIL ${file}: ${n} lines, ` + (entry ? `its baseline is ${entry}. ` : `the limit is ${limit}. `) + scope.advice,
);
}
for (const { key, entry } of stale) {
console.error(
`FAIL ${key} is held at ${entry} in scripts/size-baseline.json, ` +
`FAIL ${key} is held at ${entry} in scripts/${scope.baseline}, ` +
(key in counts ? `but it has ${counts[key]} lines now.` : 'but the file is gone.'),
);
}
@@ -125,9 +150,19 @@ function check(argv) {
);
}
console.log(
`size: ${Object.keys(counts).length} file(s), ${Object.keys(baseline).length} over ${LIMIT} lines held at their baseline`,
`${scope.label}: ${Object.keys(counts).length} file(s), ` +
`${Object.keys(baseline).length} over ${limit} lines held at their baseline`,
);
return grown.length || stale.length ? 1 : 0;
return grown.length > 0 || stale.length > 0;
}
function check(argv) {
const dirArg = argv.find((a) => a.startsWith('--dir='));
const serverDir = dirArg ? resolve(dirArg.slice('--dir='.length)) : fileURLToPath(new URL('..', import.meta.url));
const update = argv.includes('--update');
let failed = false;
for (const scope of SCOPES) failed = checkScope(serverDir, scope, update) || failed;
return failed ? 1 : 0;
}
await runCli('lint:size', (argv) => {
+283
View File
@@ -0,0 +1,283 @@
{
"counts": {
"tests/e2e/addons.e2e.test.ts": 1,
"tests/e2e/admin.e2e.test.ts": 1,
"tests/e2e/airports.e2e.test.ts": 1,
"tests/e2e/assignments.e2e.test.ts": 1,
"tests/e2e/atlas.e2e.test.ts": 1,
"tests/e2e/auth.e2e.test.ts": 1,
"tests/e2e/backup.e2e.test.ts": 1,
"tests/e2e/booking-import.e2e.test.ts": 1,
"tests/e2e/budget.e2e.test.ts": 1,
"tests/e2e/calendar-feed-visibility.e2e.test.ts": 1,
"tests/e2e/categories.e2e.test.ts": 1,
"tests/e2e/collab.e2e.test.ts": 1,
"tests/e2e/collections.e2e.test.ts": 1,
"tests/e2e/dawarich.e2e.test.ts": 1,
"tests/e2e/days.e2e.test.ts": 1,
"tests/e2e/doc-sync.e2e.test.ts": 1,
"tests/e2e/feeds.e2e.test.ts": 1,
"tests/e2e/files.e2e.test.ts": 1,
"tests/e2e/google-quota.e2e.test.ts": 1,
"tests/e2e/harness.ts": 1,
"tests/e2e/journey.e2e.test.ts": 1,
"tests/e2e/mailer.e2e.test.ts": 1,
"tests/e2e/maps.e2e.test.ts": 1,
"tests/e2e/memories.e2e.test.ts": 1,
"tests/e2e/notifications.e2e.test.ts": 1,
"tests/e2e/oauth.e2e.test.ts": 1,
"tests/e2e/oidc.e2e.test.ts": 1,
"tests/e2e/packing.e2e.test.ts": 1,
"tests/e2e/place-enrichment.e2e.test.ts": 1,
"tests/e2e/place-shadow.e2e.test.ts": 1,
"tests/e2e/places.e2e.test.ts": 1,
"tests/e2e/plugin-pois.e2e.test.ts": 1,
"tests/e2e/plugin-search.e2e.test.ts": 1,
"tests/e2e/public-api.e2e.test.ts": 1,
"tests/e2e/push.e2e.test.ts": 1,
"tests/e2e/receipt-scan.e2e.test.ts": 1,
"tests/e2e/reservations.e2e.test.ts": 1,
"tests/e2e/roadtrip.e2e.test.ts": 1,
"tests/e2e/school-holidays.e2e.test.ts": 1,
"tests/e2e/session-revocation.e2e.test.ts": 1,
"tests/e2e/sessions.e2e.test.ts": 1,
"tests/e2e/settings.e2e.test.ts": 1,
"tests/e2e/share.e2e.test.ts": 1,
"tests/e2e/storage-admin.e2e.test.ts": 1,
"tests/e2e/system-notices.e2e.test.ts": 1,
"tests/e2e/tags.e2e.test.ts": 1,
"tests/e2e/todo.e2e.test.ts": 1,
"tests/e2e/tours.e2e.test.ts": 1,
"tests/e2e/transit-provider.e2e.test.ts": 1,
"tests/e2e/transit.e2e.test.ts": 1,
"tests/e2e/trip-invite.e2e.test.ts": 1,
"tests/e2e/trips.e2e.test.ts": 1,
"tests/e2e/vacay.e2e.test.ts": 1,
"tests/e2e/weather.e2e.test.ts": 1,
"tests/helpers/db-mock.ts": 3,
"tests/helpers/test-db.ts": 1,
"tests/integration/admin.test.ts": 1,
"tests/integration/airtrail-sync-request-context.test.ts": 1,
"tests/integration/api-docs.test.ts": 1,
"tests/integration/app-config.test.ts": 1,
"tests/integration/assignments.test.ts": 1,
"tests/integration/async-closure-truthiness-guards.test.ts": 1,
"tests/integration/atlas-geocode-request-context.test.ts": 1,
"tests/integration/atlas.test.ts": 1,
"tests/integration/auth.test.ts": 1,
"tests/integration/backup.test.ts": 1,
"tests/integration/boot-sweeps-request-context.test.ts": 1,
"tests/integration/bootstrap.test.ts": 1,
"tests/integration/budget.test.ts": 1,
"tests/integration/categories.test.ts": 1,
"tests/integration/collab.test.ts": 1,
"tests/integration/collections.test.ts": 1,
"tests/integration/dayNotes.test.ts": 1,
"tests/integration/dayReorder.test.ts": 1,
"tests/integration/days.test.ts": 1,
"tests/integration/demo-seed-request-context.test.ts": 1,
"tests/integration/doc-sync-webhook-request-context.test.ts": 1,
"tests/integration/feeds-request-context.test.ts": 1,
"tests/integration/files.test.ts": 1,
"tests/integration/help.test.ts": 1,
"tests/integration/immich.test.ts": 1,
"tests/integration/journey-place-order.test.ts": 1,
"tests/integration/journey.test.ts": 1,
"tests/integration/leg-mode-incoming.test.ts": 1,
"tests/integration/legacy-upgrade-v066.test.ts": 1,
"tests/integration/legacy-upgrade-v082.test.ts": 1,
"tests/integration/legacy-upgrade-v205.test.ts": 1,
"tests/integration/legacy-upgrade-v242.test.ts": 1,
"tests/integration/legacy-upgrade-v244.test.ts": 1,
"tests/integration/maps.test.ts": 1,
"tests/integration/mcp.test.ts": 1,
"tests/integration/memories-immich.test.ts": 1,
"tests/integration/memories-synology.test.ts": 1,
"tests/integration/memories-unified.test.ts": 1,
"tests/integration/misc.test.ts": 1,
"tests/integration/notifications.test.ts": 1,
"tests/integration/oauth-authorize.test.ts": 1,
"tests/integration/oauth.test.ts": 1,
"tests/integration/oidc-local-provider.test.ts": 1,
"tests/integration/oidc.test.ts": 1,
"tests/integration/orm-request-context-seams.test.ts": 1,
"tests/integration/packing.test.ts": 1,
"tests/integration/passkey.test.ts": 1,
"tests/integration/places.test.ts": 1,
"tests/integration/plugins/boot-registry-order.test.ts": 1,
"tests/integration/plugins/plugin-runtime.test.ts": 1,
"tests/integration/profile.test.ts": 1,
"tests/integration/reservations.test.ts": 1,
"tests/integration/scheduler-gate.test.ts": 1,
"tests/integration/security.test.ts": 1,
"tests/integration/settings.test.ts": 1,
"tests/integration/share.test.ts": 1,
"tests/integration/storage-health-notifier-request-context.test.ts": 1,
"tests/integration/systemNotices.test.ts": 1,
"tests/integration/tags.test.ts": 1,
"tests/integration/todo.test.ts": 1,
"tests/integration/trip-access-primitives.test.ts": 1,
"tests/integration/trips.test.ts": 1,
"tests/integration/uploads-photo-request-context.test.ts": 1,
"tests/integration/uploads-static.test.ts": 1,
"tests/integration/vacay.test.ts": 1,
"tests/setup.ts": 1,
"tests/unit/auto-backup.test.ts": 1,
"tests/unit/db/database-lazy-open.test.ts": 1,
"tests/unit/mcp/demo-tool-gate.test.ts": 1,
"tests/unit/mcp/tools-addon-gating.test.ts": 1,
"tests/unit/mcp/tools-airtrail.test.ts": 1,
"tests/unit/mcp/tools-assignments-reservations-extra.test.ts": 1,
"tests/unit/mcp/tools-assignments.test.ts": 1,
"tests/unit/mcp/tools-atlas-expanded.test.ts": 1,
"tests/unit/mcp/tools-atlas.test.ts": 1,
"tests/unit/mcp/tools-budget-advanced.test.ts": 1,
"tests/unit/mcp/tools-budget.test.ts": 1,
"tests/unit/mcp/tools-categories.test.ts": 1,
"tests/unit/mcp/tools-collab-polls-chat.test.ts": 1,
"tests/unit/mcp/tools-collections.test.ts": 1,
"tests/unit/mcp/tools-days-accommodations.test.ts": 1,
"tests/unit/mcp/tools-days.test.ts": 1,
"tests/unit/mcp/tools-feeds-invite.test.ts": 1,
"tests/unit/mcp/tools-files.test.ts": 1,
"tests/unit/mcp/tools-help-addons.test.ts": 1,
"tests/unit/mcp/tools-journey.test.ts": 1,
"tests/unit/mcp/tools-leg-transport-mode.test.ts": 1,
"tests/unit/mcp/tools-memories.test.ts": 1,
"tests/unit/mcp/tools-notes.test.ts": 1,
"tests/unit/mcp/tools-notifications.test.ts": 1,
"tests/unit/mcp/tools-packing-advanced.test.ts": 1,
"tests/unit/mcp/tools-packing.test.ts": 1,
"tests/unit/mcp/tools-places.test.ts": 1,
"tests/unit/mcp/tools-plugin-pois.test.ts": 1,
"tests/unit/mcp/tools-plugin-search.test.ts": 1,
"tests/unit/mcp/tools-prompts.test.ts": 1,
"tests/unit/mcp/tools-reservations.test.ts": 1,
"tests/unit/mcp/tools-roadtrip.test.ts": 1,
"tests/unit/mcp/tools-settings.test.ts": 1,
"tests/unit/mcp/tools-share.test.ts": 1,
"tests/unit/mcp/tools-tags-maps-weather.test.ts": 1,
"tests/unit/mcp/tools-todos.test.ts": 1,
"tests/unit/mcp/tools-transit.test.ts": 1,
"tests/unit/mcp/tools-transports.test.ts": 1,
"tests/unit/mcp/tools-trip-members.test.ts": 1,
"tests/unit/mcp/tools-trip-warnings.test.ts": 1,
"tests/unit/mcp/tools-trips.test.ts": 1,
"tests/unit/mcp/tools-vacay.test.ts": 1,
"tests/unit/nest/accommodations.service.test.ts": 1,
"tests/unit/nest/admin.service.test.ts": 1,
"tests/unit/nest/admin.version-notification.test.ts": 1,
"tests/unit/nest/assignments.service.test.ts": 1,
"tests/unit/nest/atlas.service.test.ts": 1,
"tests/unit/nest/auth.service.test.ts": 1,
"tests/unit/nest/backup.impl.test.ts": 1,
"tests/unit/nest/booking-import/booking-import.service.test.ts": 1,
"tests/unit/nest/budget.service.calc.test.ts": 1,
"tests/unit/nest/budget.service.db.test.ts": 1,
"tests/unit/nest/budget.service.test.ts": 1,
"tests/unit/nest/calendar.service.test.ts": 1,
"tests/unit/nest/channel-override.test.ts": 1,
"tests/unit/nest/collab.service.test.ts": 1,
"tests/unit/nest/collections.service.test.ts": 1,
"tests/unit/nest/common/conflictUpdate.test.ts": 1,
"tests/unit/nest/database/database-lifecycle.service.test.ts": 2,
"tests/unit/nest/dawarich-sync.service.test.ts": 1,
"tests/unit/nest/day-notes.service.test.ts": 1,
"tests/unit/nest/day-removal.service.test.ts": 1,
"tests/unit/nest/days.service.test.ts": 1,
"tests/unit/nest/doc-sync/doc-sync-config.service.test.ts": 1,
"tests/unit/nest/doc-sync/doc-sync-webhook.controller.test.ts": 1,
"tests/unit/nest/doc-sync/doc-sync.controller.test.ts": 1,
"tests/unit/nest/doc-sync/doc-sync.service.test.ts": 1,
"tests/unit/nest/files.service.test.ts": 1,
"tests/unit/nest/google-quota.service.test.ts": 1,
"tests/unit/nest/immich.service.test.ts": 1,
"tests/unit/nest/journey-book.service.test.ts": 1,
"tests/unit/nest/journey-domain.service.test.ts": 1,
"tests/unit/nest/journey-share.service.test.ts": 1,
"tests/unit/nest/journey-stats.service.test.ts": 1,
"tests/unit/nest/mailer.service.test.ts": 1,
"tests/unit/nest/maps.amap.test.ts": 1,
"tests/unit/nest/maps.curated-images.test.ts": 1,
"tests/unit/nest/maps.google-quota.test.ts": 1,
"tests/unit/nest/maps.identity.test.ts": 1,
"tests/unit/nest/maps.service.test.ts": 1,
"tests/unit/nest/mcp-shared.service.test.ts": 1,
"tests/unit/nest/mcp-transport.service.test.ts": 1,
"tests/unit/nest/memories-access.service.test.ts": 1,
"tests/unit/nest/notification-preferences.service.test.ts": 1,
"tests/unit/nest/notification-transports.test.ts": 1,
"tests/unit/nest/notifications.inapp-prefs.test.ts": 1,
"tests/unit/nest/notifications.service.test.ts": 1,
"tests/unit/nest/notifications/push-subscriptions.service.test.ts": 1,
"tests/unit/nest/notifications/vapid-keys.service.test.ts": 1,
"tests/unit/nest/notifications/web-push-dispatch.test.ts": 1,
"tests/unit/nest/notifications/web-push.service.test.ts": 1,
"tests/unit/nest/oauth.service.test.ts": 1,
"tests/unit/nest/oidc.service.test.ts": 1,
"tests/unit/nest/packing.service.test.ts": 1,
"tests/unit/nest/passkey.service.test.ts": 1,
"tests/unit/nest/permissions.service.test.ts": 1,
"tests/unit/nest/photo-resolver.service.test.ts": 1,
"tests/unit/nest/photos/trekPhotoMedia.test.ts": 1,
"tests/unit/nest/place-photos/place-photo-cache.service.test.ts": 1,
"tests/unit/nest/place-regions-repair.test.ts": 1,
"tests/unit/nest/places.controller.test.ts": 1,
"tests/unit/nest/places.directions.test.ts": 1,
"tests/unit/nest/places.helpers.test.ts": 1,
"tests/unit/nest/places.service.test.ts": 1,
"tests/unit/nest/platform.controller.test.ts": 1,
"tests/unit/nest/public-api-scopes.test.ts": 1,
"tests/unit/nest/registration-invites.service.test.ts": 1,
"tests/unit/nest/reminder-jobs.service.test.ts": 1,
"tests/unit/nest/reservations.service.test.ts": 1,
"tests/unit/nest/route-inventory.test.ts": 1,
"tests/unit/nest/settings.service.test.ts": 1,
"tests/unit/nest/share.service.test.ts": 1,
"tests/unit/nest/storage/storage-admin.service.test.ts": 1,
"tests/unit/nest/storage/storage-jobs.service.test.ts": 1,
"tests/unit/nest/storage/storage-registry.service.test.ts": 1,
"tests/unit/nest/storage/storage-stats.service.test.ts": 1,
"tests/unit/nest/synology.service.test.ts": 1,
"tests/unit/nest/todo.service.test.ts": 1,
"tests/unit/nest/token.service.test.ts": 1,
"tests/unit/nest/tours.controller.test.ts": 1,
"tests/unit/nest/tours.gpx.atomic.test.ts": 1,
"tests/unit/nest/tours.permissions.boundaries.mock-only.test.ts": 1,
"tests/unit/nest/tours.permissions.test.ts": 1,
"tests/unit/nest/tours.service.permissions.test.ts": 1,
"tests/unit/nest/tours.service.test.ts": 1,
"tests/unit/nest/transit-itinerary.helpers.test.ts": 1,
"tests/unit/nest/travel-stats.service.test.ts": 1,
"tests/unit/nest/trip-invite.service.test.ts": 1,
"tests/unit/nest/trip-members.service.test.ts": 1,
"tests/unit/nest/trip-read-model.service.test.ts": 1,
"tests/unit/nest/trips.service.test.ts": 1,
"tests/unit/nest/unified-memories.service.test.ts": 1,
"tests/unit/nest/user-cleanup.service.test.ts": 1,
"tests/unit/nest/user-profile.service.test.ts": 1,
"tests/unit/nest/vacay.service.test.ts": 1,
"tests/unit/plugins/atlas-layers.controller.test.ts": 1,
"tests/unit/plugins/day-schedule.controller.test.ts": 1,
"tests/unit/plugins/day-tints.controller.test.ts": 1,
"tests/unit/plugins/host-rpc.test.ts": 1,
"tests/unit/plugins/map-layers.controller.test.ts": 1,
"tests/unit/plugins/map-markers.controller.test.ts": 1,
"tests/unit/plugins/operator-egress.test.ts": 1,
"tests/unit/plugins/pdf-sections.controller.test.ts": 1,
"tests/unit/plugins/place-details.controller.test.ts": 1,
"tests/unit/plugins/plugin-instance-settings.test.ts": 1,
"tests/unit/plugins/plugin-oauth.service.test.ts": 1,
"tests/unit/plugins/plugin-pois.test.ts": 1,
"tests/unit/plugins/plugin-retrust.controller.test.ts": 1,
"tests/unit/plugins/plugin-routes.controller.test.ts": 1,
"tests/unit/plugins/plugin-user-settings.test.ts": 1,
"tests/unit/plugins/plugins-service.test.ts": 1,
"tests/unit/plugins/settings-isolation.test.ts": 1,
"tests/unit/plugins/trip-card-contributions.controller.test.ts": 1,
"tests/unit/plugins/trip-warnings.controller.test.ts": 1,
"tests/unit/plugins/view-contributions.controller.test.ts": 1,
"tests/websocket/connection.test.ts": 1,
"tests/websocket/wire-envelope.test.ts": 1
}
}
+56
View File
@@ -0,0 +1,56 @@
#!/usr/bin/env node
/*
* lint:test-mocks: module mocks of the config, the realtime transport and the
* database modules in tests/ may only shrink.
*
* A `vi.mock` of one of those paths replaces a module every domain imports,
* so a suite stays green for reasons its cases do not show (a factory that
* happens to export the names the import order reaches first) and every
* constructor or import change ripples through hundreds of factories. The
* test infrastructure covers each of them instead:
*
* src/config the setup file fixes its values once
* (tests/helpers/test-config.ts, overrideTestConfig)
* src/nest/realtime/** a FakeRealtimeService through DI, or spyOnRealtime(app)
* src/db/** a snapshot test db (tests/helpers/db-mock.ts) handed
* to the code under test; the remaining `src/db/database`
* mocks are the legacy this counts down
*
* Every `vi.mock(` / `vi.doMock(` whose path ends in one of those modules
* counts, per file, against scripts/test-mocks-baseline.json; a file without
* an entry may hold none (scripts/lib/count-ratchet.mjs has the rules).
*
* npm run lint:test-mocks check against the baseline (CI)
* npm run lint:test-mocks -- --update lower the counts; never raises or adds an entry
*/
import { cliMain } from './lib/count-ratchet.mjs';
const MOCK_CALL = /\bvi\.(?:mock|doMock)\(\s*(['"`])([^'"`]+)\1/g;
/** A mocked path that names one of the held modules, with or without an extension. */
const HELD_TARGET = /(?:^|\/)src\/(?:config|websocket|db(?:\/[^'"`]*)?|nest\/realtime(?:\/[^'"`]*)?)$/;
/** The module mocks of held targets in one file's text. */
export function countHeldMocks(text) {
let n = 0;
for (const match of text.matchAll(MOCK_CALL)) {
const target = match[2].replace(/\.(?:[cm]?[jt]s)$/, '');
if (HELD_TARGET.test(target)) n++;
}
return n;
}
export const check = {
name: 'test-mocks',
script: 'lint:test-mocks',
root: 'tests',
baseline: 'scripts/test-mocks-baseline.json',
unit: 'module mock(s) of config, realtime or db',
advice:
'Use the test config (overrideTestConfig), a FakeRealtimeService or spyOnRealtime(app), ' +
'and a snapshot test db handed to the code under test instead of mocking the module.',
include: (file) => /\.[cm]?[jt]s$/.test(file),
count: (text) => countHeldMocks(text),
};
cliMain(check, import.meta.url);
@@ -0,0 +1,209 @@
{
"counts": {
"tests/helpers/accommodations-service.ts": 8,
"tests/helpers/budget-repos.ts": 1,
"tests/helpers/fake-realtime.ts": 1,
"tests/helpers/maps-service.ts": 1,
"tests/helpers/notifications.ts": 10,
"tests/helpers/place-import.ts": 1,
"tests/helpers/plugin-host.ts": 45,
"tests/helpers/sessions.ts": 1,
"tests/helpers/share-repos.ts": 1,
"tests/helpers/storage-fixture.ts": 1,
"tests/helpers/test-addons.ts": 2,
"tests/helpers/test-app.ts": 1,
"tests/helpers/test-uow.ts": 3,
"tests/integration/dayReorder.test.ts": 3,
"tests/integration/mcp.test.ts": 2,
"tests/integration/oauth-authorize.test.ts": 2,
"tests/integration/oauth.test.ts": 2,
"tests/integration/plugins/boot-registry-order.test.ts": 7,
"tests/integration/plugins/plugin-runtime.task2-conversion.test.ts": 17,
"tests/integration/plugins/plugin-scheduler-sweep.test.ts": 5,
"tests/integration/plugins/registry.test.ts": 2,
"tests/integration/share.test.ts": 2,
"tests/integration/storage-restore-reload.test.ts": 2,
"tests/integration/storage-stream-abort.test.ts": 3,
"tests/unit/budget/costs.rpc.test.ts": 1,
"tests/unit/db/connection.test.ts": 1,
"tests/unit/db/repositories/DemoRepository.test.ts": 1,
"tests/unit/db/repositories/MaintenanceRepository.test.ts": 2,
"tests/unit/db/repositories/UserConnections.repository.test.ts": 3,
"tests/unit/demo/demo-reset-sessions.test.ts": 3,
"tests/unit/mcp/tools-airtrail.test.ts": 1,
"tests/unit/mcp/tools-budget-advanced.test.ts": 4,
"tests/unit/mcp/tools-categories.test.ts": 4,
"tests/unit/mcp/tools-prompts.test.ts": 7,
"tests/unit/mcp/tools-roadtrip.test.ts": 1,
"tests/unit/mcp/tools-transit.test.ts": 1,
"tests/unit/nest-rpc/plugin-guards.test.ts": 1,
"tests/unit/nest-rpc/rpc-kit/registry.service.test.ts": 3,
"tests/unit/nest/accommodations.event-parity.test.ts": 1,
"tests/unit/nest/accommodations.service.test.ts": 1,
"tests/unit/nest/addons.service.test.ts": 2,
"tests/unit/nest/admin.service.test.ts": 11,
"tests/unit/nest/admin.version-notification.test.ts": 11,
"tests/unit/nest/airports/airports.service.test.ts": 7,
"tests/unit/nest/airtrail.service.test.ts": 3,
"tests/unit/nest/assignments.service.test.ts": 6,
"tests/unit/nest/atlas.service.test.ts": 2,
"tests/unit/nest/audit.service.test.ts": 1,
"tests/unit/nest/auth-core/auth-guard.test.ts": 3,
"tests/unit/nest/auth.controller.test.ts": 1,
"tests/unit/nest/auth.service.test.ts": 9,
"tests/unit/nest/backup.controller.test.ts": 1,
"tests/unit/nest/booking-import/booking-import.service.test.ts": 3,
"tests/unit/nest/booking-import/import-jobs.service.test.ts": 1,
"tests/unit/nest/booking-import/kitinerary-extractor.service.test.ts": 1,
"tests/unit/nest/budget.service.calc.test.ts": 1,
"tests/unit/nest/budget.service.db.test.ts": 6,
"tests/unit/nest/budget.service.test.ts": 1,
"tests/unit/nest/calendar.service.test.ts": 6,
"tests/unit/nest/categories.service.test.ts": 2,
"tests/unit/nest/channel-override.test.ts": 2,
"tests/unit/nest/charging.test.ts": 6,
"tests/unit/nest/collab.service.test.ts": 6,
"tests/unit/nest/collections.service.test.ts": 3,
"tests/unit/nest/common/conflictUpdate.test.ts": 9,
"tests/unit/nest/cron-registrar.service.test.ts": 18,
"tests/unit/nest/dawarich-suggestions.service.test.ts": 1,
"tests/unit/nest/dawarich-sync.service.test.ts": 1,
"tests/unit/nest/dawarich-tracks.service.test.ts": 1,
"tests/unit/nest/dawarich.service.test.ts": 1,
"tests/unit/nest/day-boundaries.test.ts": 1,
"tests/unit/nest/demo.service.test.ts": 1,
"tests/unit/nest/doc-sync/doc-sync-config.service.test.ts": 1,
"tests/unit/nest/doc-sync/doc-sync.controller.test.ts": 1,
"tests/unit/nest/doc-sync/doc-sync.job.test.ts": 3,
"tests/unit/nest/doc-sync/doc-sync.service.test.ts": 3,
"tests/unit/nest/features.controller.test.ts": 2,
"tests/unit/nest/feeds.service.test.ts": 1,
"tests/unit/nest/files.service.test.ts": 7,
"tests/unit/nest/google-quota.service.test.ts": 1,
"tests/unit/nest/google-route.test.ts": 1,
"tests/unit/nest/google-transit.provider.test.ts": 7,
"tests/unit/nest/immich.service.test.ts": 1,
"tests/unit/nest/integrations/airtrailImport.test.ts": 5,
"tests/unit/nest/integrations/airtrailTestConnection.test.ts": 3,
"tests/unit/nest/integrations/airtrailWriteback.test.ts": 3,
"tests/unit/nest/integrations/airtrailWriteGate.test.ts": 2,
"tests/unit/nest/journey-book.service.test.ts": 3,
"tests/unit/nest/journey-domain.service.test.ts": 2,
"tests/unit/nest/journey-photo-capture.service.test.ts": 3,
"tests/unit/nest/journey-share.service.test.ts": 4,
"tests/unit/nest/journey-stats.service.test.ts": 2,
"tests/unit/nest/llm-parse/llm-parse.service.test.ts": 1,
"tests/unit/nest/mailer.service.test.ts": 1,
"tests/unit/nest/mcp-shared.service.test.ts": 2,
"tests/unit/nest/mcp-transport.service.test.ts": 1,
"tests/unit/nest/memories-access.service.test.ts": 1,
"tests/unit/nest/memories.service.test.ts": 1,
"tests/unit/nest/memories/journey-thumbs.job.test.ts": 2,
"tests/unit/nest/memories/trek-photo-cache.job.test.ts": 1,
"tests/unit/nest/notification-preferences.service.test.ts": 4,
"tests/unit/nest/notification-transports.test.ts": 3,
"tests/unit/nest/notifications/storage-health-notifier.service.test.ts": 2,
"tests/unit/nest/notifications/vapid-keys.service.test.ts": 2,
"tests/unit/nest/notifications/web-push.service.test.ts": 1,
"tests/unit/nest/oauth.controller.test.ts": 1,
"tests/unit/nest/oauth.service.test.ts": 4,
"tests/unit/nest/oidc.service.test.ts": 13,
"tests/unit/nest/packing-writes.service.test.ts": 2,
"tests/unit/nest/packing.controller.test.ts": 1,
"tests/unit/nest/packing.service.test.ts": 1,
"tests/unit/nest/passkey.service.test.ts": 10,
"tests/unit/nest/permissions.service.test.ts": 2,
"tests/unit/nest/photo-capture-backfill.exif.test.ts": 1,
"tests/unit/nest/photo-capture-backfill.service.test.ts": 5,
"tests/unit/nest/photo-resolver.service.test.ts": 2,
"tests/unit/nest/photos/trekPhotoMedia.test.ts": 1,
"tests/unit/nest/place-enrichment.brand.test.ts": 1,
"tests/unit/nest/place-enrichment.controller.test.ts": 2,
"tests/unit/nest/place-enrichment.description.test.ts": 1,
"tests/unit/nest/place-enrichment.service.test.ts": 1,
"tests/unit/nest/place-photos/place-photo-cache.job.test.ts": 2,
"tests/unit/nest/place-photos/place-photo-cache.service.test.ts": 2,
"tests/unit/nest/place-regions-repair.test.ts": 1,
"tests/unit/nest/place-shadow.service.test.ts": 1,
"tests/unit/nest/places.directions.test.ts": 6,
"tests/unit/nest/places.service.test.ts": 6,
"tests/unit/nest/process-state-ports.test.ts": 9,
"tests/unit/nest/public-api-scopes.test.ts": 4,
"tests/unit/nest/public-api.service.test.ts": 1,
"tests/unit/nest/public-stats.controller.test.ts": 2,
"tests/unit/nest/query-helpers.service.test.ts": 1,
"tests/unit/nest/rate-limit.service.test.ts": 6,
"tests/unit/nest/realtime.gateway.test.ts": 16,
"tests/unit/nest/receipt-scan.controller.test.ts": 1,
"tests/unit/nest/registration-invites.service.test.ts": 1,
"tests/unit/nest/reminder-jobs.service.test.ts": 2,
"tests/unit/nest/reservations.service.test.ts": 2,
"tests/unit/nest/roadtrip-plan.service.test.ts": 5,
"tests/unit/nest/roadtrip-planning.test.ts": 2,
"tests/unit/nest/roadtrip-router.test.ts": 1,
"tests/unit/nest/roadtrip-search.test.ts": 3,
"tests/unit/nest/roadtrip.service.test.ts": 1,
"tests/unit/nest/route-usage.service.test.ts": 1,
"tests/unit/nest/school-holidays.service.test.ts": 2,
"tests/unit/nest/sessions/sessions.service.test.ts": 1,
"tests/unit/nest/settings.service.test.ts": 1,
"tests/unit/nest/share.service.test.ts": 3,
"tests/unit/nest/sqlite-database-backup.test.ts": 2,
"tests/unit/nest/storage/storage-admin.service.test.ts": 5,
"tests/unit/nest/storage/storage-jobs.service.test.ts": 15,
"tests/unit/nest/storage/storage-registry.service.test.ts": 3,
"tests/unit/nest/storage/storage-stats.service.test.ts": 3,
"tests/unit/nest/storage/storage.service.test.ts": 5,
"tests/unit/nest/synology.service.test.ts": 1,
"tests/unit/nest/system-notices.branch-coverage.test.ts": 1,
"tests/unit/nest/system-notices.service.test.ts": 1,
"tests/unit/nest/tags.service.test.ts": 2,
"tests/unit/nest/thumbnail.service.test.ts": 1,
"tests/unit/nest/todo.service.test.ts": 5,
"tests/unit/nest/token.service.test.ts": 1,
"tests/unit/nest/tours.gpx.atomic.test.ts": 7,
"tests/unit/nest/tours.permissions.boundaries.mock-only.test.ts": 1,
"tests/unit/nest/tours.service.permissions.test.ts": 1,
"tests/unit/nest/tours.service.test.ts": 1,
"tests/unit/nest/transit.service.test.ts": 2,
"tests/unit/nest/travel-stats.service.test.ts": 1,
"tests/unit/nest/trek-photo-cache.service.test.ts": 2,
"tests/unit/nest/trek-photo-registration.service.test.ts": 1,
"tests/unit/nest/trip-invite.service.test.ts": 4,
"tests/unit/nest/trip-members.service.test.ts": 6,
"tests/unit/nest/trip-membership/trip-membership.service.test.ts": 1,
"tests/unit/nest/trip-read-model.service.test.ts": 31,
"tests/unit/nest/trips.service.test.ts": 38,
"tests/unit/nest/unified-memories.service.test.ts": 3,
"tests/unit/nest/unsplash/unsplash.service.test.ts": 1,
"tests/unit/nest/user-cleanup.service.test.ts": 8,
"tests/unit/nest/user-profile.service.test.ts": 1,
"tests/unit/nest/vacay.service.test.ts": 2,
"tests/unit/nest/weather/weather-cache-lifecycle.test.ts": 1,
"tests/unit/nest/webauthn-config.service.test.ts": 1,
"tests/unit/packing/packing.rpc.test.ts": 1,
"tests/unit/plugins/addon-gates.rpc.test.ts": 1,
"tests/unit/plugins/day-schedule.controller.test.ts": 1,
"tests/unit/plugins/day-tints.controller.test.ts": 1,
"tests/unit/plugins/map-layers.controller.test.ts": 1,
"tests/unit/plugins/map-markers.controller.test.ts": 1,
"tests/unit/plugins/operator-egress.test.ts": 1,
"tests/unit/plugins/pdf-sections.controller.test.ts": 1,
"tests/unit/plugins/place-details.controller.test.ts": 1,
"tests/unit/plugins/plugin-instance-settings.test.ts": 1,
"tests/unit/plugins/plugin-mcp-tools.test.ts": 1,
"tests/unit/plugins/plugin-oauth.service.test.ts": 1,
"tests/unit/plugins/plugin-pois.test.ts": 1,
"tests/unit/plugins/plugin-routes.controller.test.ts": 1,
"tests/unit/plugins/plugin-runtime.boot-no-orm.test.ts": 3,
"tests/unit/plugins/plugin-user-settings.test.ts": 2,
"tests/unit/plugins/plugins-service.test.ts": 1,
"tests/unit/plugins/request-context.test.ts": 1,
"tests/unit/plugins/rpc-coverage.test.ts": 1,
"tests/unit/plugins/settings-isolation.test.ts": 3,
"tests/unit/plugins/trip-card-contributions.controller.test.ts": 1,
"tests/unit/plugins/trip-warnings.controller.test.ts": 1,
"tests/unit/plugins/view-contributions.controller.test.ts": 1,
"tests/websocket/connection.test.ts": 1
}
}
+151
View File
@@ -0,0 +1,151 @@
#!/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);
+9
View File
@@ -0,0 +1,9 @@
{
"tests/unit/nest/atlas.service.test.ts": 2193,
"tests/unit/nest/budget.service.db.test.ts": 2220,
"tests/unit/nest/doc-sync/doc-sync.service.test.ts": 2511,
"tests/unit/nest/journey-domain.service.test.ts": 3876,
"tests/unit/nest/maps.service.test.ts": 3751,
"tests/unit/nest/places.service.test.ts": 2561,
"tests/unit/nest/trips.service.test.ts": 2404
}