GitNexus/gitnexus/test/helpers/detect-changes-stderr-hooks.mjs
Gergő Magyar a3eb34b27c
Some checks are pending
Devcontainer Smoke / Config-transform unit tests (push) Waiting to run
Devcontainer Smoke / Host importer tests (macos-latest) (push) Waiting to run
Devcontainer Smoke / Host importer tests (windows-latest) (push) Waiting to run
Devcontainer Smoke / Build devcontainer image (default) (push) Waiting to run
Devcontainer Smoke / Build devcontainer image (ui) (push) Waiting to run
CodeQL / Analyze (javascript-typescript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
Publish Development Images / publish (core) (push) Waiting to run
Publish Development Images / publish (ui) (push) Waiting to run
Gitleaks / gitleaks (push) Waiting to run
Publish / ci (push) Blocked by required conditions
Publish / Publish to npm (push) Blocked by required conditions
Publish / Classify release event (push) Waiting to run
Publish / RC guard (marker + release-PR skip) (push) Blocked by required conditions
Publish / Build & Push RC Docker images (push) Blocked by required conditions
Scorecard / Scorecard analysis (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-cli) (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-web) (push) Waiting to run
fix(mcp): contain detect_changes git stderr (#3841)
2026-11-08 05:50:10 +00:00

91 lines
3.5 KiB
JavaScript

/**
* Lets detect-changes-stderr-child.ts run the real `detect_changes` path
* without a LadybugDB index.
*
* Loaded with `node --import <tsx> --import <this file>` BEFORE the child.
* The child then imports LocalBackend, which would open the index and read
* the global registry. Those two modules are swapped for thin wrappers: git
* diff still runs for real, and the graph query returns no rows.
*
* The wrappers re-export the real modules (imported once, with `?bypass=1`
* so this hook does not recurse) and override only the calls the tool makes
* before and after `git diff`.
*/
import { registerHooks } from 'node:module';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
const srcRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..', 'src');
const poolReal = pathToFileURL(path.join(srcRoot, 'core/lbug/pool-adapter.ts')).href;
const repoReal = pathToFileURL(path.join(srcRoot, 'storage/repo-manager.ts')).href;
const POOL_OVERRIDES = new Set(['initLbug', 'executeParameterized', 'executeQuery', 'isLbugReady']);
const REPO_OVERRIDES = new Set(['listRegisteredRepos']);
function reexportLines(names, overridden) {
const lines = [];
for (const name of names) {
if (overridden.has(name) || name === 'default') continue;
if (!/^[A-Za-z_$][\w$]*$/.test(name)) continue;
lines.push(`export const ${name} = real[${JSON.stringify(name)}];`);
}
if (names.includes('default') && !overridden.has('default')) {
lines.push('export default real.default;');
}
return lines.join('\n');
}
let poolNames = [];
let repoNames = [];
registerHooks({
load(url, context, nextLoad) {
if (url.includes('bypass=1')) return nextLoad(url, context);
const isPool = url.includes('/core/lbug/pool-adapter.');
const isRepo = url.includes('/storage/repo-manager.');
if (!isPool && !isRepo) return nextLoad(url, context);
if ((isPool && poolNames.length === 0) || (isRepo && repoNames.length === 0)) {
throw new Error(`detect-changes stderr hook ran before the real module was indexed (${url})`);
}
const real = (isPool ? poolReal : repoReal) + '?bypass=1';
const override = isPool
? `
export const initLbug = async () => {};
export const executeParameterized = async () => [];
export const executeQuery = async () => [];
export const isLbugReady = () => true;
`
: `
export const listRegisteredRepos = async () => {
const { join } = await import('node:path');
const repo = process.env.DETECT_CHANGES_REPO;
if (!repo) throw new Error('DETECT_CHANGES_REPO is not set');
return [{
name: 'autocrlf-repo',
path: repo,
storagePath: join(repo, '.gitnexus'),
indexedAt: '2026-01-01T00:00:00.000Z',
lastCommit: 'abc1234',
stats: { files: 1, nodes: 1, edges: 0, communities: 0, processes: 0 },
}];
};
`;
return {
format: 'module',
shortCircuit: true,
source: `import * as real from ${JSON.stringify(real)};\n${override}\n${reexportLines(
isPool ? poolNames : repoNames,
isPool ? POOL_OVERRIDES : REPO_OVERRIDES,
)}\n`,
};
},
});
poolNames = Object.keys(await import(poolReal + '?bypass=1'));
repoNames = Object.keys(await import(repoReal + '?bypass=1'));
if (poolNames.length === 0 || repoNames.length === 0) {
throw new Error('detect-changes stderr hook could not read the real module exports');
}