fix(metrics): enforce exact runtime inventory

This commit is contained in:
Tam Nhu Tran committed 2026-07-26 09:36:00 -04:00
1 parent 426dc541a9
commit 306a8276b3
7 files changed
+258 -160

No files matched your search

+20 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'bun:test';
const { collectSyncCallSites } = require('../../../scripts/hardening-inventory.js');
const { collectSyncCallSites, isTestFile } = require('../../../scripts/hardening-inventory.js');
describe('hardening-inventory sync call scanning', () => {
test('ignores sync-call names inside regex literals after else', () => {
@@ -34,3 +34,22 @@ describe('hardening-inventory sync call scanning', () => {
expect(result.calls).toEqual(['readFileSync']);
});
});
describe('hardening-inventory runtime file classification', () => {
test.each([
'src/cliproxy/__tests__/routing.test.ts',
'src/commands/fixtures/help-output.ts',
'src/auth/profile.spec.ts',
'tests/unit/commands/profile.test.ts',
])('excludes %s from runtime hotpaths', (filePath) => {
expect(isTestFile(filePath)).toBe(true);
});
test.each([
'src/cliproxy/routing/retry-settings.ts',
'src/commands/help-command.ts',
'src/utils/browser/mcp-installer.ts',
])('keeps %s eligible for runtime hotpaths', (filePath) => {
expect(isTestFile(filePath)).toBe(false);
});
});
@@ -8,10 +8,31 @@ const {
classifyThrows,
countConsoleErrors,
hasCreateLogger,
isTestFile,
countLoc,
collectMaintainabilityMetrics,
} = require('../../../scripts/maintainability-metrics.js');
describe('maintainability-metrics runtime file classification', () => {
test.each([
'src/cliproxy/__tests__/routing.test.ts',
'src/commands/fixtures/help-output.ts',
'src/auth/profile.spec.ts',
'tests/unit/commands/profile.test.ts',
'src\\auth\\__mocks__\\profile.ts',
])('excludes %s from runtime metrics', (filePath) => {
expect(isTestFile(filePath)).toBe(true);
});
test.each([
'src/cliproxy/routing/retry-settings.ts',
'src/commands/help-command.ts',
'src/utils/browser/mcp-installer.ts',
])('keeps %s in runtime metrics', (filePath) => {
expect(isTestFile(filePath)).toBe(false);
});
});
describe('maintainability-metrics.classifyThrows', () => {
test('counts plain Error, typed, and other throws; ignores re-throws', () => {
const source = [
@@ -94,14 +115,18 @@ describe('maintainability-metrics.collectMaintainabilityMetrics (fixtures tree)'
fs.mkdirSync(path.join(root, 'src', 'commands'), { recursive: true });
fs.writeFileSync(
path.join(root, 'src', 'commands', 'b.ts'),
['console.error(\'cli print\');', "throw new Error('plain');"].join('\n')
["console.error('cli print');", "throw new Error('plain');"].join('\n')
);
// src/cliproxy/quota/q.ts: createLogger present, 1 plain throw, 0 console.error
fs.mkdirSync(path.join(root, 'src', 'cliproxy', 'quota'), { recursive: true });
fs.writeFileSync(
path.join(root, 'src', 'cliproxy', 'quota', 'q.ts'),
["import { createLogger } from '../../../services/logging';", 'const logger = createLogger();', "throw new Error('quota plain');"].join('\n')
[
"import { createLogger } from '../../../services/logging';",
'const logger = createLogger();',
"throw new Error('quota plain');",
].join('\n')
);
// non-source file is ignored