Files
Agent-Workbench/tests/project-index.test.mjs
2026-10-29 17:59:35 +08:00

175 lines
15 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { execFileSync } from 'node:child_process';
import { ProjectIndex } from '../src/core/context/project-index.mjs';
import { inspectProject } from '../src/core/project-inspector.mjs';
import { readText } from '../src/server/files.mjs';
import { fixture } from './helpers.mjs';
function write(f, relative, contents) {
const target = path.join(f.projectRoot, relative);
fs.mkdirSync(path.dirname(target), { recursive: true }); fs.writeFileSync(target, contents);
}
const gitFor = f => args => execFileSync('git', ['-C', f.projectRoot, '-c', 'core.autocrlf=false', ...args], { windowsHide: true, encoding: 'utf8' });
function initGit(f) {
const git = gitFor(f); git(['init', '--initial-branch=main']); git(['add', '.']);
git(['-c', 'user.name=Workbench QA', '-c', 'user.email=qa@workbench.local', 'commit', '-m', 'Index fixture']); return git;
}
test('JS/TS/React AST indexes actual declarations, exports, imports and verified source lines', async t => {
const f = fixture(t);
write(f, 'src/shape.ts', ['// function Fake() {}', 'const text = "class Imaginary {}";', 'import type { Point } from "./point.js";',
'export interface Shape { area(): number; }', 'export type Size = number;', 'export enum Mode { Active }',
'export function area(size: Size) { function square() { return size * size; } return square(); }',
'export class Box { area() { return 1; } privateFn = () => 2; }', 'const alias = (x: number) => x;', 'export { alias };',
'export * from "./point.js";', 'const later = import("./point.js");', 'const required = require("./point.js");', ''].join('\n'));
write(f, 'src/point.ts', 'export interface Point { x: number; }\n');
write(f, 'src/Card.tsx', 'import React from "react";\nexport const Card = () => <button>查看</button>;\n');
const indexer = new ProjectIndex(), index = await indexer.refresh(f.project);
assert.equal(index.coverage.astFiles, 3); assert.equal(index.coverage.truncated, false);
const symbols = index.symbols.filter(value => value.path === 'src/shape.ts');
for (const name of ['Shape', 'Size', 'Mode', 'area', 'Box', 'alias', 'Box.area', 'Box.privateFn', 'area.square']) assert.ok(symbols.some(value => value.qualifiedName === name), name);
assert.ok(!symbols.some(value => ['Fake', 'Imaginary'].includes(value.name)));
assert.equal(symbols.find(value => value.name === 'Shape').line, 4);
assert.equal(symbols.find(value => value.name === 'alias').exported, true);
assert.equal(symbols.find(value => value.name === 'area' && value.kind === 'function').sha256, readText(f.project, 'src/shape.ts').sha256);
assert.ok(index.symbols.find(value => value.name === 'Card').exported);
const local = index.imports.filter(value => value.from === 'src/shape.ts'); assert.equal(local.length, 4);
assert.deepEqual(local.map(value => value.kind), ['type-import', 're-export', 'literal-dynamic-import', 'literal-require-call']);
for (const value of local) { assert.equal(value.target, 'src/point.ts'); assert.equal(value.runtimeResolution, 'not_verified'); }
const external = index.imports.find(value => value.source === 'react'); assert.equal(external.status, 'external-or-alias'); assert.equal(external.target, null);
const found = indexer.query(index, { query: 'Box area', kind: 'symbols' }); assert.equal(found.results[0].qualifiedName, 'Box.area');
assert.match(index.indexFingerprint, /^[a-f0-9]{64}$/); assert.equal(indexer.summary(index).sourceFingerprint, index.sourceFingerprint);
});
test('default expressions, named default exports and declared functions retain their AST evidence', async t => {
const f = fixture(t);
write(f, 'arrow.mjs', 'export default (value) => value + 1;\n');
write(f, 'named.mjs', 'function calculate() { return 1; }\nexport default calculate;\n');
write(f, 'class.mjs', 'export default class { run() { return 1; } }\n');
write(f, 'overload.ts', 'export declare function convert(value: string): number;\n');
const index = await new ProjectIndex().refresh(f.project);
assert.ok(index.symbols.find(value => value.path === 'arrow.mjs' && value.name === 'default' && value.exported && value.kind === 'function'));
assert.ok(index.symbols.find(value => value.name === 'calculate' && value.exported));
assert.ok(index.symbols.find(value => value.path === 'class.mjs' && value.qualifiedName === 'default.run'));
assert.ok(index.symbols.find(value => value.name === 'convert' && value.kind === 'function'));
});
test('incremental refresh invalidates changed/deleted symbols and recalculates reused import targets', async t => {
const f = fixture(t); write(f, 'src/use.mjs', 'import { value } from "./new.mjs";\nexport function read() { return value; }\n');
const indexer = new ProjectIndex(), first = await indexer.refresh(f.project);
assert.equal(first.imports[0].status, 'unresolved-local');
const unchanged = await indexer.refresh(f.project); assert.equal(unchanged.refresh.updatedFiles, 0); assert.equal(unchanged.indexFingerprint, first.indexFingerprint);
write(f, 'src/new.mjs', 'export const value = 1;\n');
const added = await indexer.refresh(f.project); assert.equal(added.imports[0].target, 'src/new.mjs'); assert.equal(added.refresh.updatedFiles, 1); assert.ok(added.refresh.reusedFiles >= 2);
write(f, 'src/new.mjs', 'export const different = 2;\n');
const changed = await indexer.refresh(f.project); assert.ok(changed.symbols.some(value => value.name === 'different')); assert.ok(!changed.symbols.some(value => value.name === 'value'));
assert.notEqual(changed.imports[0].targetSha256, added.imports[0].targetSha256);
fs.unlinkSync(path.join(f.projectRoot, 'src/new.mjs'));
const removed = await indexer.refresh(f.project); assert.equal(removed.refresh.removedFiles, 1); assert.equal(removed.imports[0].target, null); assert.ok(!removed.symbols.some(value => value.name === 'different'));
assert.equal(first.imports[0].target, null, 'refresh must not mutate previously returned evidence');
});
test('Python candidates exclude comments and multiline strings and declare limited evidence', async t => {
const f = fixture(t);
write(f, 'main.py', ['"""', 'def fake_doc():', 'class FakeDoc:', '"""', '# def fake_comment():', 'text = "def fake_text():"',
'class Service:', ' def run(self):', ' return 1', 'if True:', ' def conditional():', ' return 2',
"continued = 'example\\", 'def fake_continued():\\', "end'", 'async def actual():', ' pass', 'from .tools import helper', ''].join('\n'));
write(f, 'tools.py', 'def helper():\n return 1\n');
const index = await new ProjectIndex().refresh(f.project);
assert.equal(index.coverage.pythonCandidateFiles, 2);
const symbols = index.symbols.filter(value => value.path === 'main.py');
assert.deepEqual(symbols.map(value => value.qualifiedName), ['Service', 'Service.run', 'conditional', 'actual']);
assert.ok(symbols.every(value => value.evidenceMethod === 'python-declaration-candidate'));
assert.equal(symbols.find(value => value.name === 'actual').line, 16);
assert.equal(index.imports.find(value => value.source === '.tools').runtimeResolution, 'not_verified');
assert.match(index.notes.join(' '), /候选/);
});
test('indexes isolate project identity and root and evict bounded caches without sharing results', async t => {
const f = fixture(t), otherRoot = path.join(f.root, 'other'); fs.mkdirSync(otherRoot); fs.writeFileSync(path.join(otherRoot, 'README.md'), 'other');
fs.writeFileSync(path.join(otherRoot, 'unique.ts'), 'export const otherOnly = true;\n'); write(f, 'unique.ts', 'export const hereOnly = true;\n');
const other = { ...f.project, root: fs.realpathSync.native(otherRoot) };
const indexer = new ProjectIndex({ maximumProjects: 1 });
const firstPromise = indexer.refresh(f.project); assert.equal(indexer.refresh(f.project), firstPromise);
const first = await firstPromise, second = await indexer.refresh(other);
assert.ok(first.symbols.some(value => value.name === 'hereOnly')); assert.ok(!second.symbols.some(value => value.name === 'hereOnly'));
assert.ok(second.symbols.some(value => value.name === 'otherOnly')); assert.notEqual(first.indexFingerprint, second.indexFingerprint);
assert.equal(indexer.projects.size, 1); const again = await indexer.refresh(f.project); assert.ok(again.refresh.updatedFiles > 0); assert.equal(indexer.pending.size, 0);
});
test('Git ignore, credential paths and links stay outside the index and no project files are executed', async t => {
const f = fixture(t); write(f, '.gitignore', 'ignored/\n.env\n'); write(f, 'main.mjs', 'export function safe() { throw new Error("do not execute"); }\n');
initGit(f); write(f, 'ignored/hidden.mjs', 'export const hidden = true;\n'); write(f, '.env', 'credential-sentinel');
write(f, 'credentials.json', 'credential-sentinel'); write(f, 'node_modules/dependency/index.js', 'export const dependencyHidden = true;\n');
const outside = path.join(f.root, 'outside'); fs.mkdirSync(outside); fs.writeFileSync(path.join(outside, 'private.js'), 'export const linkedHidden = true;');
fs.symlinkSync(outside, path.join(f.projectRoot, 'linked'), process.platform === 'win32' ? 'junction' : 'dir');
const index = await new ProjectIndex().refresh(f.project), text = JSON.stringify(index);
for (const sentinel of ['credential-sentinel', 'dependencyHidden', 'linkedHidden', 'hidden =']) assert.ok(!text.includes(sentinel));
assert.ok(!index.files.some(value => /^(ignored|node_modules|linked)\//.test(value.path)));
assert.equal(index.symbols.find(value => value.name === 'safe').path, 'main.mjs');
});
test('malformed, binary, non-UTF8 and bounded files expose partial coverage while good files remain searchable', async t => {
const f = fixture(t); write(f, 'good.ts', 'export const valid = true;\n'); write(f, 'broken.ts', 'export function broken( {');
write(f, 'binary.js', Buffer.from([0, 1, 2])); write(f, 'encoding.ts', Buffer.from([0xff])); write(f, 'large.js', '// padding\n'.repeat(25));
const indexer = new ProjectIndex({ maximumFileBytes: 128 }), index = await indexer.refresh(f.project);
assert.equal(index.coverage.truncated, true); assert.equal(index.coverage.unindexedFiles, 4);
assert.ok(index.warnings.find(value => value.path === 'broken.ts').line > 0);
assert.equal(index.files.find(value => value.path === 'large.js').status, 'outside-index-budget');
assert.equal(indexer.query(index, { query: 'valid' }).results[0].name, 'valid');
assert.equal(indexer.query(index, { query: 'not found' }).truncated, true);
const limited = await new ProjectIndex({ maximumSymbolsPerFile: 1, maximumSymbols: 1 }).refresh(f.project);
assert.ok(limited.symbols.length <= 1); assert.equal(limited.coverage.truncated, true);
});
test('index query rejects invalid limits/types and does not imply reference or alias resolution', async t => {
const f = fixture(t); write(f, 'app.ts', 'import { worker } from "@app/worker";\nconst a = 1;\nconst b = 2;\n');
const indexer = new ProjectIndex(), index = await indexer.refresh(f.project);
for (const args of [{ query: null }, { query: 'x'.repeat(201) }, { kind: 'references' }, { limit: 0 }, { limit: 61 }, { limit: 1.5 }]) assert.throws(() => indexer.query(index, args));
const result = indexer.query(index, { query: '', kind: 'symbols', limit: 1 }); assert.equal(result.results.length, 1); assert.equal(result.totalMatches, 2); assert.equal(result.truncated, true);
assert.equal(index.imports[0].status, 'external-or-alias'); assert.equal(index.imports[0].target, null);
});
test('read-only engineering inspection disables configured Git clean filters and preserves index bytes', async t => {
const f = fixture(t), marker = path.join(f.root, 'filter-executed.txt'), script = path.join(f.root, 'filter.mjs');
write(f, '.gitattributes', '*.mjs filter=fixture\n'); write(f, 'main.mjs', 'export const value = 1;\n');
const git = initGit(f);
fs.writeFileSync(script, "import fs from 'node:fs'; fs.writeFileSync(" + JSON.stringify(marker) + ", 'filter executed'); process.stdin.pipe(process.stdout);\n");
git(['config', 'filter.fixture.clean', '"' + process.execPath.replaceAll('\\', '/') + '" "' + script.replaceAll('\\', '/') + '"']);
git(['config', 'filter.fixture.required', 'true']);
write(f, 'main.mjs', 'export const value = 2;\n');
git(['status', '--porcelain=v1']); assert.equal(fs.existsSync(marker), true, 'the fixture must prove ordinary Git status executes the custom filter'); fs.unlinkSync(marker);
const indexBytes = fs.readFileSync(path.join(f.projectRoot, '.git/index'));
const inspection = await inspectProject(f.project);
assert.equal(fs.existsSync(marker), false, 'read-only inspection must not execute project filter commands');
assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, '.git/index')), indexBytes);
assert.equal(inspection.git.available, true); assert.equal(inspection.git.dirty, true);
const index = await new ProjectIndex().refresh(f.project); assert.equal(index.symbols.find(value => value.name === 'value').path, 'main.mjs');
assert.equal(fs.existsSync(marker), false);
});
test('Python multiline declarations retain candidate scope without interpreting expressions as declarations', async t => {
const f = fixture(t); write(f, 'multiline.py', 'class Worker:\n def run(\n self,\n value\n ):\n def inner():\n return value\n return inner()\n');
const index = await new ProjectIndex().refresh(f.project);
assert.deepEqual(index.symbols.map(value => value.qualifiedName), ['Worker', 'Worker.run', 'Worker.run.inner']);
write(f, 'one.py', 'def one():\n pass\n');
const bounded = await new ProjectIndex({ maximumSymbolsPerFile: 1 }).refresh(f.project);
assert.equal(bounded.files.find(value => value.path === 'one.py').truncated, false);
assert.equal(bounded.files.find(value => value.path === 'multiline.py').truncated, true);
});
test('TypeScript module candidates support import-equals and MTS/CTS while opaque imports stay explicit', async t => {
const f = fixture(t);
write(f, 'main.ts', 'import helper = require("./helper.cjs");\nimport { value } from "./module.mjs";\nconst pathName = "./module.mjs";\nconst dynamic = import(pathName);\n');
write(f, 'helper.cts', 'export const helper = true;\n'); write(f, 'module.mts', 'export const value = 1;\n');
const index = await new ProjectIndex().refresh(f.project), imports = index.imports.filter(value => value.from === 'main.ts');
assert.equal(imports.length, 2); assert.equal(imports[0].kind, 'import-equals'); assert.equal(imports[0].target, 'helper.cts'); assert.deepEqual(imports[0].bindings, ['helper']);
assert.equal(imports[1].target, 'module.mts'); assert.equal(imports[1].runtimeResolution, 'not_verified');
assert.equal(index.coverage.truncated, true); assert.match(index.warnings.find(value => value.path === 'main.ts').message, /动态导入/);
write(f, 'module.mjs', 'export const value = 2;\n');
const exact = await new ProjectIndex().refresh(f.project); assert.equal(exact.imports.find(value => value.source === './module.mjs').target, 'module.mjs');
});