feat: add project index and exact patch review
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fixture, compatibleFixture, completion, call, until, assertToolPairs, launchWorkbench } from './helpers.mjs';
|
||||
import { Runtime, validateToolArgs } from '../src/server/runtime.mjs';
|
||||
import { Provider } from '../src/server/provider.mjs';
|
||||
import { Store } from '../src/server/store.mjs';
|
||||
import { applyChange, hash } from '../src/server/files.mjs';
|
||||
|
||||
test('model code tools keep Task/Run ownership, index evidence and exact proposals across storage recovery', async t => {
|
||||
const f = fixture(t), target = path.join(f.projectRoot, 'compute.ts'), original = '\uFEFF// protected banner\r\nexport function compute() { return 1; }\r\n';
|
||||
fs.writeFileSync(target, original); let rounds = 0, fileSha = '';
|
||||
const model = await compatibleFixture(t, (_url, input) => {
|
||||
assertToolPairs(input.messages); rounds++;
|
||||
const output = id => JSON.parse(input.messages.find(value => value.tool_call_id === id).content);
|
||||
if (rounds === 1) {
|
||||
for (const name of ['index_project', 'query_code', 'propose_patch']) assert.ok(input.tools.some(value => value.function.name === name));
|
||||
return completion([call('index', 'index_project', {}), call('query', 'query_code', { query: 'compute', kind: 'symbols', limit: 5 })]);
|
||||
}
|
||||
if (rounds === 2) {
|
||||
assert.equal(output('index').projectId, f.project.id); assert.equal(output('query').results[0].line, 2);
|
||||
assert.ok(output('index').artifact_id); assert.equal(output('query').results[0].evidenceMethod, 'javascript-typescript-ast');
|
||||
return completion([call('read', 'read_file', { path: 'compute.ts', start_line: 2, end_line: 2 })]);
|
||||
}
|
||||
if (rounds === 3) {
|
||||
fileSha = output('read').sha256; assert.equal(output('read').bom, true);
|
||||
return completion([call('patch', 'propose_patch', { path: 'compute.ts', base_sha256: fileSha, reason: 'Repair precise implementation', edits: [{ old_text: 'return 1;', new_text: 'return 2;' }] })]);
|
||||
}
|
||||
assert.equal(output('patch').status, 'awaiting_review'); assert.equal(output('patch').patch.lineEnding, 'crlf');
|
||||
return completion([], '精确补丁已准备,请审核当前项目的差异。');
|
||||
});
|
||||
f.store.saveConfig({ ...f.store.config, mode: 'compatible', baseUrl: model.baseUrl, model: 'index-tool-fixture' });
|
||||
const provider = new Provider(); provider.setKey('synthetic-index-key'); const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
|
||||
runtime.start(f.session.id, 'Use project index and exact patches'); await until(() => !runtime.isActive(f.session.id), 15000);
|
||||
assert.equal(rounds, 4); assert.equal(f.session.changes.length, 1); assert.equal(fs.readFileSync(target, 'utf8'), original);
|
||||
const change = f.session.changes[0], run = f.store.executionSnapshot(f.session).run;
|
||||
assert.equal(change.editKind, 'exact-patch'); assert.equal(change.runId, run.id); assert.equal(change.taskId, run.taskId); assert.equal(change.baseSha256, fileSha);
|
||||
const artifact = f.store.executionSnapshot(f.session).artifacts.find(value => value.kind === 'project-index');
|
||||
const owned = f.store.owned('artifact', artifact.id, f.session.id), content = f.store.artifactStore.read(owned), index = JSON.parse(content);
|
||||
assert.equal(owned.runId, run.id); assert.equal(owned.taskId, run.taskId); assert.equal(owned.sha256, hash(Buffer.from(content))); assert.equal(index.projectId, f.project.id);
|
||||
assert.ok(!content.includes('synthetic-index-key')); assert.equal(index.symbols.find(value => value.name === 'compute').sha256, fileSha);
|
||||
const other = f.store.createSession(f.project.id); assert.throws(() => f.store.owned('artifact', artifact.id, other.id));
|
||||
applyChange(f.store, f.session, change.id); assert.equal(fs.readFileSync(target, 'utf8'), original.replace('return 1;', 'return 2;'));
|
||||
f.store.close(); const recovered = new Store(f.data); f.onCleanup(() => recovered.close());
|
||||
assert.deepEqual(recovered.session(f.session.id).changes[0].patch, change.patch);
|
||||
assert.equal(recovered.artifactStore.read(recovered.owned('artifact', artifact.id, f.session.id)), content);
|
||||
});
|
||||
|
||||
test('bundled code APIs isolate project results, validate queries and create review-only patches with conflicts', async t => {
|
||||
const f = fixture(t); fs.writeFileSync(path.join(f.projectRoot, 'code.mjs'), 'export const currentOnly = 1;\n');
|
||||
const otherRoot = path.join(f.root, 'other'); fs.mkdirSync(otherRoot); fs.writeFileSync(path.join(otherRoot, 'code.mjs'), 'export const otherOnly = 1;\n');
|
||||
const other = f.store.addProject(otherRoot), server = await launchWorkbench(t, f.data);
|
||||
assert.equal(server.bootstrap.capabilities.codeIndex, 'bounded-ast-and-candidates'); assert.equal(server.bootstrap.capabilities.exactPatches, 'sha256-unique-anchors');
|
||||
assert.equal((await fetch(server.url + '/api/projects/' + f.project.id + '/code-index')).status, 403);
|
||||
const index = await server.api('/projects/' + f.project.id + '/code-index'); assert.equal(index.projectId, f.project.id); assert.equal(index.coverage.astFiles, 1);
|
||||
const own = await server.api('/projects/' + f.project.id + '/code-query?query=currentOnly&kind=symbols'); assert.equal(own.results[0].path, 'code.mjs'); assert.equal(own.results[0].line, 1);
|
||||
const separate = await server.api('/projects/' + other.id + '/code-query?query=currentOnly'); assert.equal(separate.results.length, 0); assert.equal(separate.index.projectId, other.id);
|
||||
for (const query of ['kind=references', 'limit=0', 'limit=61', 'limit=abc', 'query=' + 'a'.repeat(201)]) await assert.rejects(server.api('/projects/' + f.project.id + '/code-query?' + query), { status: 400 });
|
||||
const args = { path: 'code.mjs', base_sha256: own.results[0].sha256, reason: 'Exact API edit', edits: [{ old_text: 'currentOnly = 1', new_text: 'currentOnly = 2' }] };
|
||||
const proposed = await server.api('/sessions/' + f.session.id + '/patch-proposals', args); assert.equal(proposed.changes[0].editKind, 'exact-patch');
|
||||
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'code.mjs'), 'utf8'), 'export const currentOnly = 1;\n');
|
||||
fs.writeFileSync(path.join(f.projectRoot, 'code.mjs'), 'export const owner = 3;\n');
|
||||
await assert.rejects(server.api('/sessions/' + f.session.id + '/patch-proposals', args), { status: 409 });
|
||||
await assert.rejects(server.api('/sessions/' + f.session.id + '/changes/' + proposed.changes[0].id + '/apply', {}), { status: 409 });
|
||||
const refreshed = await server.api('/projects/' + f.project.id + '/code-query?query=owner'); assert.equal(refreshed.results[0].name, 'owner'); assert.notEqual(refreshed.index.indexFingerprint, own.index.indexFingerprint);
|
||||
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'code.mjs'), 'utf8'), 'export const owner = 3;\n');
|
||||
});
|
||||
|
||||
test('code tool arguments reject malformed ownership-independent inputs before side effects', () => {
|
||||
for (const [name, args] of [['query_code', { query: 'q', kind: 'references' }], ['query_code', { query: [] }], ['query_code', { query: 'q', limit: 1.5 }],
|
||||
['propose_patch', { path: 'a', base_sha256: 'invalid', reason: 'x', edits: [] }], ['propose_patch', { path: 'a', base_sha256: 'a'.repeat(64), reason: 'x', edits: [] }]]) assert.throws(() => validateToolArgs(name, args));
|
||||
validateToolArgs('query_code', { query: 'compute', kind: 'symbols', limit: 5 });
|
||||
validateToolArgs('propose_patch', { path: 'a', base_sha256: 'a'.repeat(64), reason: 'x', edits: [{ old_text: 'a', new_text: 'b' }] });
|
||||
});
|
||||
+11
-5
@@ -90,12 +90,18 @@ export async function launchWorkbench(t, data, extraEnv = {}) {
|
||||
child.stderr.on('data', bytes => { errors += bytes.toString('utf8'); });
|
||||
await until(() => url || child.exitCode !== null);
|
||||
assert.ok(url, 'Workbench launch failed: ' + errors);
|
||||
const bootstrap = await (await fetch(url + '/api/bootstrap')).json();
|
||||
const bootstrap = await (await fetch(url + '/api/bootstrap', { headers: { Connection: 'close' } })).json();
|
||||
const api = async (route, input) => {
|
||||
const response = await fetch(url + '/api' + route, { method: input === undefined ? 'GET' : 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'X-Workbench-Token': bootstrap.token },
|
||||
...(input === undefined ? {} : { body: JSON.stringify(input) }) });
|
||||
const result = await response.json();
|
||||
const method = input === undefined ? 'GET' : 'POST';
|
||||
let response;
|
||||
try {
|
||||
response = await fetch(url + '/api' + route, { method,
|
||||
headers: { 'Content-Type': 'application/json', 'X-Workbench-Token': bootstrap.token, Connection: 'close' },
|
||||
...(input === undefined ? {} : { body: JSON.stringify(input) }) });
|
||||
} catch (cause) { throw Object.assign(new Error('QA API transport failed before response headers: ' + method + ' ' + route, { cause }), { stage: 'response_headers', method, route }); }
|
||||
let result;
|
||||
try { result = await response.json(); }
|
||||
catch (cause) { throw Object.assign(new Error('QA API transport failed while reading response: ' + method + ' ' + route, { cause }), { stage: 'response_body', method, route }); }
|
||||
if (!response.ok) throw Object.assign(new Error(result.error), { status: response.status });
|
||||
return result;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fixture } from './helpers.mjs';
|
||||
import { proposePatch, proposeFile, readText, applyChange, hash } from '../src/server/files.mjs';
|
||||
|
||||
test('exact patches preserve BOM/CRLF, unicode and all unselected bytes and back up the original on apply', t => {
|
||||
const f = fixture(t), target = path.join(f.projectRoot, 'code.ts');
|
||||
const before = Buffer.from('\uFEFF// 保留🙂\r\nexport const value = 1;\r\n\r\nexport function keep() { return "原文"; }\r\n'); fs.writeFileSync(target, before);
|
||||
const file = readText(f.project, 'code.ts'); assert.equal(file.bom, true);
|
||||
const result = proposePatch(f.store, f.session, { path: file.path, base_sha256: file.sha256.toUpperCase(), reason: 'precise replacement',
|
||||
edits: [{ old_text: 'export const value = 1;\n', new_text: 'export const value = 2;\nexport const added = true;\n' }] });
|
||||
assert.equal(result.status, 'awaiting_review'); assert.equal(result.patch.lineEnding, 'crlf'); assert.equal(result.patch.preservedBom, true);
|
||||
assert.deepEqual(fs.readFileSync(target), before);
|
||||
const change = f.session.changes[0]; assert.equal(change.editKind, 'exact-patch'); assert.equal(change.baseContent, before.toString('utf8')); assert.equal(change.baseSha256, hash(before));
|
||||
assert.equal(change.patch.ranges[0].startLine, 2); assert.equal(change.patch.ranges[0].anchorSha256, hash(Buffer.from('export const value = 1;\r\n')));
|
||||
applyChange(f.store, f.session, result.change_id);
|
||||
const expected = Buffer.from(before.toString('utf8').replace('export const value = 1;\r\n', 'export const value = 2;\r\nexport const added = true;\r\n'));
|
||||
assert.deepEqual(fs.readFileSync(target), expected); assert.deepEqual(fs.readFileSync(path.join(f.data, 'backups', f.session.id, change.id + '.txt')), before);
|
||||
});
|
||||
|
||||
test('several patches are matched against one original snapshot in any input order and keep LF', t => {
|
||||
const f = fixture(t), target = path.join(f.projectRoot, 'code.js'); fs.writeFileSync(target, 'const first = 1;\nconst second = 2;\n// trailing\n');
|
||||
const file = readText(f.project, 'code.js');
|
||||
const result = proposePatch(f.store, f.session, { path: file.path, base_sha256: file.sha256, reason: 'two edits', edits: [
|
||||
{ old_text: 'const second = 2;\r\n', new_text: 'const second = 20;\r\n' }, { old_text: 'const first = 1;', new_text: 'const first = 10;' }] });
|
||||
assert.equal(result.patch.edits, 2); assert.deepEqual(result.patch.ranges.map(value => value.startLine), [1, 2]);
|
||||
applyChange(f.store, f.session, result.change_id); assert.equal(fs.readFileSync(target, 'utf8'), 'const first = 10;\nconst second = 20;\n// trailing\n');
|
||||
});
|
||||
|
||||
test('mixed line endings require exact anchors and preserve untouched formatting', t => {
|
||||
const f = fixture(t), target = path.join(f.projectRoot, 'mixed.txt'); fs.writeFileSync(target, 'first\r\nsecond\nthird\r\n');
|
||||
const file = readText(f.project, 'mixed.txt'), args = { path: file.path, base_sha256: file.sha256, reason: 'mixed' };
|
||||
assert.throws(() => proposePatch(f.store, f.session, { ...args, edits: [{ old_text: 'first\nsecond', new_text: 'changed' }] }), { status: 409 });
|
||||
const result = proposePatch(f.store, f.session, { ...args, edits: [{ old_text: 'second\n', new_text: 'replacement\n' }] });
|
||||
assert.equal(result.patch.lineEnding, 'mixed'); applyChange(f.store, f.session, result.change_id);
|
||||
assert.equal(fs.readFileSync(target, 'utf8'), 'first\r\nreplacement\nthird\r\n');
|
||||
});
|
||||
|
||||
test('ambiguous, overlapping, missing and malformed patch anchors never create a proposal or write', t => {
|
||||
const f = fixture(t), target = path.join(f.projectRoot, 'anchors.txt'); const original = 'repeat repeat\nabcdef\n'; fs.writeFileSync(target, original);
|
||||
const file = readText(f.project, 'anchors.txt'), args = { path: file.path, base_sha256: file.sha256, reason: 'reject invalid' };
|
||||
const invalid = [[], [{ old_text: '', new_text: 'x' }], [{ old_text: 'repeat', new_text: 'x' }], [{ old_text: 'missing', new_text: 'x' }],
|
||||
[{ old_text: 'abc', new_text: 'x' }, { old_text: 'bcd', new_text: 'y' }], [null], [{ old_text: 1, new_text: 'x' }],
|
||||
[{ old_text: 'abc', new_text: '\0' }], [{ old_text: 'abc', new_text: 42 }], Array.from({ length: 33 }, () => ({ old_text: 'abc', new_text: 'x' }))];
|
||||
for (const edits of invalid) assert.throws(() => proposePatch(f.store, f.session, { ...args, edits }));
|
||||
assert.throws(() => proposePatch(f.store, f.session, { ...args, base_sha256: 'invalid', edits: [{ old_text: 'abc', new_text: 'x' }] }));
|
||||
assert.throws(() => proposePatch(f.store, f.session, { ...args, path: '../anchors.txt', edits: [{ old_text: 'abc', new_text: 'x' }] }));
|
||||
assert.equal(f.session.changes.length, 0); assert.equal(fs.readFileSync(target, 'utf8'), original);
|
||||
});
|
||||
|
||||
test('stale original hashes and changes before apply preserve owner edits and mark conflicts', t => {
|
||||
const f = fixture(t), target = path.join(f.projectRoot, 'value.txt'); fs.writeFileSync(target, 'value = 1\n');
|
||||
const file = readText(f.project, 'value.txt'), args = { path: file.path, base_sha256: file.sha256, reason: 'fix', edits: [{ old_text: 'value = 1', new_text: 'value = 2' }] };
|
||||
const result = proposePatch(f.store, f.session, args); fs.writeFileSync(target, 'owner = 3\n');
|
||||
assert.throws(() => proposePatch(f.store, f.session, args), { status: 409 });
|
||||
assert.throws(() => applyChange(f.store, f.session, result.change_id), { status: 409 }); assert.equal(f.session.changes[0].status, 'conflict'); assert.equal(fs.readFileSync(target, 'utf8'), 'owner = 3\n');
|
||||
});
|
||||
|
||||
test('no-op BOM patches/full content produce no change and replacements cannot split Unicode bytes', t => {
|
||||
const f = fixture(t), target = path.join(f.projectRoot, 'unicode.txt'); const contents = '\uFEFFkeep🙂\n'; fs.writeFileSync(target, contents);
|
||||
const file = readText(f.project, 'unicode.txt'), args = { path: file.path, base_sha256: file.sha256, reason: 'noop' };
|
||||
assert.equal(proposePatch(f.store, f.session, { ...args, edits: [{ old_text: 'keep', new_text: 'keep' }] }).status, 'unchanged');
|
||||
assert.equal(proposeFile(f.store, f.session, { path: file.path, content: contents }).status, 'unchanged');
|
||||
assert.throws(() => proposePatch(f.store, f.session, { ...args, edits: [{ old_text: '\ud83d', new_text: 'x' }] }));
|
||||
assert.throws(() => proposePatch(f.store, f.session, { ...args, edits: [{ old_text: 'keep', new_text: '\ud83d' }] }));
|
||||
assert.equal(f.session.changes.length, 0); assert.equal(fs.readFileSync(target, 'utf8'), contents);
|
||||
});
|
||||
@@ -0,0 +1,174 @@
|
||||
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');
|
||||
});
|
||||
Reference in New Issue
Block a user