Compare commits
1 Commits
doc-drift/
...
metrics-co
| Author | SHA1 | Date | |
|---|---|---|---|
| a1d7f55772 |
@@ -77,12 +77,6 @@ cd client && npm run test:watch
|
||||
- Export pure functions (validators, formatters, etc.) for direct testing
|
||||
- Run `npm test` in both `server/` and `client/` before committing
|
||||
|
||||
**Doc drift check:**
|
||||
```bash
|
||||
node scripts/doc-drift.js
|
||||
```
|
||||
Scans `CLAUDE.md` and `PRD.md` for verifiable code references (file paths, API routes, component names) and cross-checks each against the filesystem and source tree. Prints a PASS/FAIL report with doc name and line number. Exits non-zero on any failure — suitable for CI gating.
|
||||
|
||||
## Application Structure
|
||||
|
||||
The default route `/` renders the paycheck-centric main view (`client/src/pages/PaycheckView.jsx`). It shows the current month's two paychecks side-by-side with bills, paid status, one-time expenses, and remaining balance. Month navigation (prev/next) fetches data via `GET /api/paychecks?year=&month=`.
|
||||
|
||||
@@ -1,203 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* doc-drift.js — detects documentation drift by cross-checking verifiable
|
||||
* code references in CLAUDE.md and PRD.md against the filesystem and source tree.
|
||||
*
|
||||
* Usage: node scripts/doc-drift.js
|
||||
* Exits non-zero if any drift is found.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { execSync } = require('child_process');
|
||||
|
||||
const ROOT = path.resolve(__dirname, '..');
|
||||
const DOCS = ['CLAUDE.md', 'PRD.md'].map(f => path.join(ROOT, f));
|
||||
|
||||
// ── Result tracking ──────────────────────────────────────────────────────────
|
||||
|
||||
const results = [];
|
||||
|
||||
function record(doc, line, kind, ref, pass, reason) {
|
||||
results.push({ doc: path.basename(doc), line, kind, ref, pass, reason });
|
||||
}
|
||||
|
||||
// ── Extraction helpers ───────────────────────────────────────────────────────
|
||||
|
||||
/** Extract all backtick spans from a line (may be multiple). */
|
||||
function backtickSpans(line) {
|
||||
const spans = [];
|
||||
const re = /`([^`]+)`/g;
|
||||
let m;
|
||||
while ((m = re.exec(line)) !== null) spans.push(m[1]);
|
||||
return spans;
|
||||
}
|
||||
|
||||
/** Return true if a span looks like a file/dir path we can verify. */
|
||||
function isFilePath(span) {
|
||||
// Must contain a slash and start with a recognisable project prefix.
|
||||
return (
|
||||
/[/\\]/.test(span) &&
|
||||
/^(client|server|db|scripts|docker-compose)/.test(span) &&
|
||||
// Exclude shell commands, URLs, SQL snippets, etc.
|
||||
!/\s/.test(span) &&
|
||||
!span.includes('=') &&
|
||||
!span.startsWith('http')
|
||||
);
|
||||
}
|
||||
|
||||
/** Return true if a span looks like a component/page reference (*.jsx). */
|
||||
function isJsxRef(span) {
|
||||
return /\w+\.jsx$/.test(span) && !/[/]/.test(span); // bare name, no path
|
||||
}
|
||||
|
||||
/** Extract HTTP API route patterns like `GET /api/paychecks`. */
|
||||
function extractApiRoutes(line) {
|
||||
const routes = [];
|
||||
const re = /\b(GET|POST|PUT|DELETE|PATCH)\s+(\/api\/[^\s,`'")\]]+)/g;
|
||||
let m;
|
||||
while ((m = re.exec(line)) !== null) routes.push({ method: m[1], path: m[2] });
|
||||
return routes;
|
||||
}
|
||||
|
||||
// ── Verification helpers ─────────────────────────────────────────────────────
|
||||
|
||||
function fileExists(relPath) {
|
||||
return fs.existsSync(path.join(ROOT, relPath));
|
||||
}
|
||||
|
||||
/**
|
||||
* For API routes: grep server/src/routes/ for the route path string.
|
||||
* We look for the path fragment (everything after /api) as a string literal.
|
||||
*/
|
||||
function apiRouteExists(routePath) {
|
||||
// Strip query-string placeholders like ?year=&month=
|
||||
const clean = routePath.replace(/\?.*$/, '').replace(/:id/g, ':id');
|
||||
// Build a grep-friendly pattern: look for the path minus leading /api
|
||||
const fragment = clean.replace(/^\/api/, '');
|
||||
try {
|
||||
const out = execSync(
|
||||
`grep -rE --include="*.js" -l "${clean}|${fragment}" "${path.join(ROOT, 'server/src/routes')}"`,
|
||||
{ stdio: ['pipe', 'pipe', 'pipe'] }
|
||||
).toString().trim();
|
||||
return out.length > 0;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* For bare *.jsx component names: check that a file with that name exists
|
||||
* somewhere under client/src/.
|
||||
*/
|
||||
function jsxComponentExists(name) {
|
||||
try {
|
||||
const out = execSync(
|
||||
`find "${path.join(ROOT, 'client/src')}" -name "${name}" -type f`,
|
||||
{ stdio: ['pipe', 'pipe', 'pipe'] }
|
||||
).toString().trim();
|
||||
return out.length > 0;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ── Main ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
for (const docPath of DOCS) {
|
||||
if (!fs.existsSync(docPath)) {
|
||||
console.error(`WARN: doc not found: ${docPath}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const lines = fs.readFileSync(docPath, 'utf8').split('\n');
|
||||
|
||||
lines.forEach((rawLine, idx) => {
|
||||
const lineNo = idx + 1;
|
||||
|
||||
// 1. Backtick file paths
|
||||
for (const span of backtickSpans(rawLine)) {
|
||||
if (isFilePath(span)) {
|
||||
const exists = fileExists(span);
|
||||
record(
|
||||
docPath,
|
||||
lineNo,
|
||||
'file-path',
|
||||
span,
|
||||
exists,
|
||||
exists ? 'found on filesystem' : `not found: ${span}`
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isJsxRef(span)) {
|
||||
const exists = jsxComponentExists(span);
|
||||
record(
|
||||
docPath,
|
||||
lineNo,
|
||||
'component',
|
||||
span,
|
||||
exists,
|
||||
exists ? 'found under client/src' : `no file named ${span} in client/src`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. API routes (inside or outside backticks)
|
||||
for (const { method, path: routePath } of extractApiRoutes(rawLine)) {
|
||||
const ref = `${method} ${routePath}`;
|
||||
const exists = apiRouteExists(routePath);
|
||||
record(
|
||||
docPath,
|
||||
lineNo,
|
||||
'api-route',
|
||||
ref,
|
||||
exists,
|
||||
exists ? 'found in server/src/routes' : `route not found in server/src/routes`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ── Report ───────────────────────────────────────────────────────────────────
|
||||
|
||||
const padDoc = Math.max(...results.map(r => r.doc.length), 9);
|
||||
const padKind = Math.max(...results.map(r => r.kind.length), 9);
|
||||
const padRef = Math.min(60, Math.max(...results.map(r => r.ref.length), 10));
|
||||
|
||||
const header = [
|
||||
'STATUS'.padEnd(6),
|
||||
'DOC'.padEnd(padDoc),
|
||||
'LINE'.padStart(4),
|
||||
'KIND'.padEnd(padKind),
|
||||
'REFERENCE',
|
||||
].join(' ');
|
||||
|
||||
console.log('\n' + header);
|
||||
console.log('─'.repeat(header.length + 10));
|
||||
|
||||
let failures = 0;
|
||||
|
||||
for (const r of results) {
|
||||
const status = r.pass ? 'PASS' : 'FAIL';
|
||||
const ref = r.ref.length > padRef ? r.ref.slice(0, padRef - 1) + '…' : r.ref;
|
||||
const line = [
|
||||
(r.pass ? '\x1b[32m' : '\x1b[31m') + status.padEnd(6) + '\x1b[0m',
|
||||
r.doc.padEnd(padDoc),
|
||||
String(r.line).padStart(4),
|
||||
r.kind.padEnd(padKind),
|
||||
ref,
|
||||
].join(' ');
|
||||
console.log(line);
|
||||
if (!r.pass) {
|
||||
console.log(` \x1b[33m↳ ${r.reason}\x1b[0m`);
|
||||
failures++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log('─'.repeat(header.length + 10));
|
||||
console.log(`\n${results.length} references checked — ${failures} failure(s)\n`);
|
||||
|
||||
process.exit(failures > 0 ? 1 : 0);
|
||||
251
scripts/metrics-coverage.js
Normal file
251
scripts/metrics-coverage.js
Normal file
@@ -0,0 +1,251 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* metrics-coverage.js — Static analysis script for metrics/logging instrumentation coverage.
|
||||
*
|
||||
* Scans all Express route files in server/src/routes/*.js and app.js to measure
|
||||
* how many route handlers contain logging calls (console.error/console.warn/console.log).
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/metrics-coverage.js # JSON output (default)
|
||||
* node scripts/metrics-coverage.js --format=text # Human-readable table
|
||||
*
|
||||
* Sample output (captured 2026-03-20):
|
||||
* {
|
||||
* "files": [
|
||||
* { "file": "actuals.js", "total": 5, "logged": 5, "unlogged": 0, "coverage": 100 },
|
||||
* { "file": "bills.js", "total": 6, "logged": 6, "unlogged": 0, "coverage": 100 },
|
||||
* { "file": "config.js", "total": 2, "logged": 2, "unlogged": 0, "coverage": 100 },
|
||||
* { "file": "financing.js", "total": 6, "logged": 6, "unlogged": 0, "coverage": 100 },
|
||||
* { "file": "health.js", "total": 1, "logged": 0, "unlogged": 1, "coverage": 0 },
|
||||
* { "file": "one-time-expenses.js", "total": 3, "logged": 3, "unlogged": 0, "coverage": 100 },
|
||||
* { "file": "paychecks.js", "total": 6, "logged": 6, "unlogged": 0, "coverage": 100 },
|
||||
* { "file": "summary.js", "total": 2, "logged": 2, "unlogged": 0, "coverage": 100 }
|
||||
* ],
|
||||
* "app": {
|
||||
* "has_request_timing_middleware": false,
|
||||
* "has_error_handling_middleware": false,
|
||||
* "middleware_count": 11
|
||||
* },
|
||||
* "aggregate": {
|
||||
* "total_handlers": 31,
|
||||
* "logged_handlers": 30,
|
||||
* "unlogged_handlers": 1,
|
||||
* "coverage_pct": 96.77
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const ROUTES_DIR = path.resolve(__dirname, '../server/src/routes');
|
||||
const APP_FILE = path.resolve(__dirname, '../server/src/app.js');
|
||||
|
||||
// Regex patterns for route handler definitions.
|
||||
// Matches: router.get/post/put/patch/delete( and app.get/post/put/patch/delete(
|
||||
const ROUTE_DEF_RE = /\b(?:router|app)\.(get|post|put|patch|delete)\s*\(/g;
|
||||
|
||||
// Logging call patterns
|
||||
const LOG_RE = /\bconsole\.(error|warn|log)\s*\(/;
|
||||
|
||||
/**
|
||||
* Extract individual route handler bodies from source.
|
||||
* Strategy: find each route definition, then walk forward counting
|
||||
* braces to find the closing of the outermost async/function callback.
|
||||
*/
|
||||
function extractHandlerBodies(src) {
|
||||
const handlers = [];
|
||||
let match;
|
||||
ROUTE_DEF_RE.lastIndex = 0;
|
||||
|
||||
while ((match = ROUTE_DEF_RE.exec(src)) !== null) {
|
||||
const startIdx = match.index;
|
||||
// Find the opening paren of the route call
|
||||
const parenOpen = src.indexOf('(', startIdx);
|
||||
if (parenOpen === -1) continue;
|
||||
|
||||
// Walk from the paren open, tracking paren depth to find the matching close.
|
||||
// The handler callback body will be inside the outer parens.
|
||||
let depth = 0;
|
||||
let bodyStart = -1;
|
||||
let bodyEnd = -1;
|
||||
let inString = false;
|
||||
let stringChar = '';
|
||||
let i = parenOpen;
|
||||
|
||||
while (i < src.length) {
|
||||
const ch = src[i];
|
||||
|
||||
// Basic string tracking (skip contents of string literals)
|
||||
if (!inString && (ch === '"' || ch === "'" || ch === '`')) {
|
||||
inString = true;
|
||||
stringChar = ch;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if (inString) {
|
||||
if (ch === '\\') { i += 2; continue; } // skip escape
|
||||
if (ch === stringChar) inString = false;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch === '(') {
|
||||
depth++;
|
||||
if (depth === 1) {
|
||||
// This is the opening of the route call args
|
||||
}
|
||||
} else if (ch === ')') {
|
||||
depth--;
|
||||
if (depth === 0) {
|
||||
bodyEnd = i;
|
||||
break;
|
||||
}
|
||||
} else if (ch === '{' && depth >= 1 && bodyStart === -1) {
|
||||
// First brace inside the outer parens — start of the handler body
|
||||
bodyStart = i;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
if (bodyStart !== -1 && bodyEnd !== -1) {
|
||||
handlers.push(src.slice(bodyStart, bodyEnd));
|
||||
}
|
||||
}
|
||||
|
||||
return handlers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Analyse a single route file.
|
||||
*/
|
||||
function analyseRouteFile(filePath) {
|
||||
const src = fs.readFileSync(filePath, 'utf8');
|
||||
const handlers = extractHandlerBodies(src);
|
||||
|
||||
const logged = handlers.filter(body => LOG_RE.test(body));
|
||||
|
||||
return {
|
||||
file: path.basename(filePath),
|
||||
total: handlers.length,
|
||||
logged: logged.length,
|
||||
unlogged: handlers.length - logged.length,
|
||||
coverage: handlers.length === 0
|
||||
? null
|
||||
: Math.round((logged.length / handlers.length) * 10000) / 100,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Analyse app.js for middleware-level instrumentation.
|
||||
*/
|
||||
function analyseApp(filePath) {
|
||||
const src = fs.readFileSync(filePath, 'utf8');
|
||||
|
||||
// Request timing: morgan, custom middleware checking req.method, Date.now() at top-level use()
|
||||
const hasRequestTiming =
|
||||
/\brequire\s*\(\s*['"]morgan['"]\s*\)/.test(src) ||
|
||||
/app\.use\s*\(.*Date\.now\(\)/.test(src) ||
|
||||
/app\.use\s*\(.*req,\s*res,\s*next/.test(src) && /Date\.now|performance\.now/.test(src);
|
||||
|
||||
// Error handling middleware: app.use((err, req, res, next) => ...)
|
||||
const hasErrorHandling = /app\.use\s*\(\s*(?:\S+\s*,\s*)?\(\s*err\s*,/.test(src);
|
||||
|
||||
// Count top-level app.use() calls (middleware registrations)
|
||||
const middlewareMatches = src.match(/app\.use\s*\(/g) || [];
|
||||
|
||||
return {
|
||||
has_request_timing_middleware: hasRequestTiming,
|
||||
has_error_handling_middleware: hasErrorHandling,
|
||||
middleware_count: middlewareMatches.length,
|
||||
};
|
||||
}
|
||||
|
||||
function run() {
|
||||
const format = process.argv.includes('--format=text') ? 'text' : 'json';
|
||||
|
||||
// Analyse all route files
|
||||
const routeFiles = fs.readdirSync(ROUTES_DIR)
|
||||
.filter(f => f.endsWith('.js'))
|
||||
.sort();
|
||||
|
||||
const fileResults = routeFiles.map(f =>
|
||||
analyseRouteFile(path.join(ROUTES_DIR, f))
|
||||
);
|
||||
|
||||
// Aggregate
|
||||
const totalHandlers = fileResults.reduce((s, r) => s + r.total, 0);
|
||||
const loggedHandlers = fileResults.reduce((s, r) => s + r.logged, 0);
|
||||
|
||||
const aggregate = {
|
||||
total_handlers: totalHandlers,
|
||||
logged_handlers: loggedHandlers,
|
||||
unlogged_handlers: totalHandlers - loggedHandlers,
|
||||
coverage_pct: totalHandlers === 0
|
||||
? null
|
||||
: Math.round((loggedHandlers / totalHandlers) * 10000) / 100,
|
||||
};
|
||||
|
||||
const appInfo = analyseApp(APP_FILE);
|
||||
|
||||
const result = {
|
||||
files: fileResults,
|
||||
app: appInfo,
|
||||
aggregate,
|
||||
};
|
||||
|
||||
if (format === 'json') {
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
return;
|
||||
}
|
||||
|
||||
// Text table
|
||||
const COL_FILE = 28;
|
||||
const COL_TOTAL = 7;
|
||||
const COL_LOGGED = 8;
|
||||
const COL_COVER = 10;
|
||||
|
||||
const pad = (s, n) => String(s).padEnd(n);
|
||||
const lpad = (s, n) => String(s).padStart(n);
|
||||
|
||||
const hr = '-'.repeat(COL_FILE + COL_TOTAL + COL_LOGGED + COL_COVER + 6);
|
||||
|
||||
console.log('\nMetrics Instrumentation Coverage\n');
|
||||
console.log(
|
||||
pad('Route File', COL_FILE) +
|
||||
lpad('Handlers', COL_TOTAL) +
|
||||
lpad('Logged', COL_LOGGED) +
|
||||
lpad('Coverage', COL_COVER)
|
||||
);
|
||||
console.log(hr);
|
||||
|
||||
for (const r of fileResults) {
|
||||
const cov = r.coverage === null ? 'N/A' : `${r.coverage}%`;
|
||||
console.log(
|
||||
pad(r.file, COL_FILE) +
|
||||
lpad(r.total, COL_TOTAL) +
|
||||
lpad(r.logged, COL_LOGGED) +
|
||||
lpad(cov, COL_COVER)
|
||||
);
|
||||
}
|
||||
|
||||
console.log(hr);
|
||||
const aggCov = aggregate.coverage_pct === null ? 'N/A' : `${aggregate.coverage_pct}%`;
|
||||
console.log(
|
||||
pad('TOTAL', COL_FILE) +
|
||||
lpad(aggregate.total_handlers, COL_TOTAL) +
|
||||
lpad(aggregate.logged_handlers, COL_LOGGED) +
|
||||
lpad(aggCov, COL_COVER)
|
||||
);
|
||||
|
||||
console.log('\napp.js middleware:');
|
||||
console.log(` Request timing middleware : ${appInfo.has_request_timing_middleware}`);
|
||||
console.log(` Error handling middleware : ${appInfo.has_error_handling_middleware}`);
|
||||
console.log(` app.use() call count : ${appInfo.middleware_count}`);
|
||||
console.log('');
|
||||
}
|
||||
|
||||
run();
|
||||
Reference in New Issue
Block a user