Files
Agent-Workbench/tests/project-cache.test.mjs
2026-11-01 02:29:58 +08:00

164 lines
13 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 { createHash } from 'node:crypto';
import { ProjectIndex } from '../src/core/context/project-index.mjs';
import { fixture } from './helpers.mjs';
const setup = t => {
const f = fixture(t), cacheDirectory = path.join(f.root, 'derived');
const write = (name, text) => { const filename = path.join(f.projectRoot, name); fs.mkdirSync(path.dirname(filename), { recursive: true }); fs.writeFileSync(filename, text); return filename; };
write('main.ts', 'import { value } from "./value.js";\nexport function read() { return value; }\n');
write('value.ts', 'export const value = 1;\n');
const create = (options = {}) => new ProjectIndex({ cacheDirectory, ...options });
const records = () => fs.readdirSync(cacheDirectory).filter(name => /^[a-f0-9]{64}\.json$/.test(name));
return { ...f, write, create, records, cacheDirectory };
};
test('persistent parsing is project/SHA/config bound and produces identical semantic indexes after restart', async t => {
const f = setup(t), firstIndexer = f.create(), first = await firstIndexer.refresh(f.project);
assert.equal(first.cache.parsedFiles, 2); assert.equal(first.cache.stored, true); assert.equal(f.records().length, 1);
const second = await f.create().refresh(f.project);
assert.equal(second.cache.origin, 'disk'); assert.equal(second.cache.parsedFiles, 0); assert.equal(second.cache.reusedFiles, 3);
assert.equal(second.cache.sourceVerified, true); assert.equal(second.cache.verificationPasses, 2);
assert.equal(second.indexFingerprint, first.indexFingerprint); assert.deepEqual(second.symbols, first.symbols); assert.deepEqual(second.imports, first.imports);
assert.equal((await firstIndexer.refresh(f.project)).cache.origin, 'memory');
const otherId = await f.create().refresh({ ...f.project, id: 'different-project' });
assert.equal(otherId.cache.parsedFiles, 2); assert.notEqual(otherId.indexFingerprint, first.indexFingerprint);
const options = await f.create({ maximumSymbolsPerFile: 1 }).refresh(f.project);
assert.equal(options.cache.origin, 'identity-changed'); assert.equal(options.cache.parsedFiles, 2);
const revision = await f.create({ parserRevision: 'next-parser' }).refresh(f.project);
assert.equal(revision.cache.origin, 'identity-changed'); assert.equal(revision.cache.parsedFiles, 2);
});
test('same-size restored-mtime changes, additions, renames and deletions cannot reuse stale entries or import targets', async t => {
const f = setup(t), indexer = f.create(), original = await indexer.refresh(f.project);
const filename = path.join(f.projectRoot, 'value.ts'), stat = fs.statSync(filename);
f.write('value.ts', 'export const other = 2;\n'); fs.utimesSync(filename, stat.atime, stat.mtime);
assert.equal(fs.statSync(filename).size, stat.size); assert.ok(Math.abs(fs.statSync(filename).mtimeMs - stat.mtimeMs) < 1);
const changed = await indexer.refresh(f.project);
assert.equal(changed.cache.invalidatedFiles, 1); assert.equal(changed.cache.parsedFiles, 1);
assert.equal(changed.cache.invalidation, 'source-or-access-changed'); assert.notEqual(changed.sourceFingerprint, original.sourceFingerprint);
assert.ok(changed.symbols.some(value => value.name === 'other')); assert.ok(!changed.symbols.some(value => value.name === 'value'));
fs.renameSync(filename, path.join(f.projectRoot, 'moved.ts'));
const renamed = await f.create().refresh(f.project);
assert.equal(renamed.cache.removedFiles, 1); assert.equal(renamed.imports[0].target, null); assert.equal(renamed.cache.parsedFiles, 1);
f.write('value.ts', 'export const value = 9;\n');
const added = await indexer.refresh(f.project); assert.equal(added.imports[0].target, 'value.ts');
fs.unlinkSync(path.join(f.projectRoot, 'moved.ts'));
assert.equal((await indexer.refresh(f.project)).cache.removedFiles, 1);
assert.equal(original.imports[0].target, 'value.ts'); assert.ok(original.symbols.some(value => value.name === 'value'));
});
test('branch and commit changes invalidate even byte-identical sources', async t => {
const f = setup(t), git = args => execFileSync('git', ['-C', f.projectRoot, ...args], { windowsHide: true, encoding: 'utf8' });
git(['init', '--initial-branch=main']); git(['add', '.']);
git(['-c', 'user.name=QA', '-c', 'user.email=qa@local.invalid', 'commit', '-m', 'cache baseline']);
const indexer = f.create(), first = await indexer.refresh(f.project);
git(['switch', '-c', 'other']);
const branch = await indexer.refresh(f.project);
assert.equal(branch.cache.invalidation, 'identity-changed'); assert.equal(branch.cache.parsedFiles, 2);
assert.equal(branch.indexFingerprint, first.indexFingerprint, 'semantic index can match while branch-specific reuse is invalidated');
git(['-c', 'user.name=QA', '-c', 'user.email=qa@local.invalid', 'commit', '--allow-empty', '-m', 'new identity']);
const commit = await f.create().refresh(f.project); assert.equal(commit.cache.origin, 'identity-changed'); assert.equal(commit.cache.parsedFiles, 2);
});
test('corrupt, malformed and schema-invalid records fall back to fresh parsing', async t => {
const f = setup(t), first = await f.create().refresh(f.project), filename = path.join(f.cacheDirectory, f.records()[0]);
fs.writeFileSync(filename, '{bad json');
const corrupt = await f.create().refresh(f.project); assert.equal(corrupt.cache.parsedFiles, 2); assert.equal(corrupt.indexFingerprint, first.indexFingerprint);
const envelope = JSON.parse(fs.readFileSync(filename, 'utf8')); envelope.payload.entries.find(value => value.path === 'main.ts').symbols = [null];
envelope.sha256 = createHash('sha256').update(JSON.stringify(envelope.payload)).digest('hex'); fs.writeFileSync(filename, JSON.stringify(envelope));
const malformed = await f.create().refresh(f.project); assert.equal(malformed.cache.parsedFiles, 1); assert.equal(malformed.indexFingerprint, first.indexFingerprint);
const changed = JSON.parse(fs.readFileSync(filename, 'utf8')); changed.payload.entries[0].sha256 = '0'.repeat(64); fs.writeFileSync(filename, JSON.stringify(changed));
const digest = await f.create().refresh(f.project); assert.equal(digest.cache.origin, 'corrupt'); assert.equal(digest.cache.parsedFiles, 2);
});
test('disable and clear persist without touching sources, sessions, configuration or original artifacts', async t => {
const f = setup(t), indexer = f.create(); await indexer.refresh(f.project);
const before = fs.readFileSync(path.join(f.projectRoot, 'main.ts')), count = f.store.sessions.size, configuration = JSON.stringify(f.store.config);
indexer.configure(false); assert.equal(f.create().status().enabled, false);
const disabled = await indexer.refresh(f.project); assert.equal(disabled.cache.origin, 'disabled'); assert.equal(disabled.cache.parsedFiles, 2);
assert.equal(indexer.projects.size, 0); indexer.configure(true);
assert.equal((await indexer.refresh(f.project)).cache.origin, 'disk');
assert.equal(indexer.clear(f.project).removedProjects, 1); assert.equal(f.records().length, 0);
assert.equal((await f.create().refresh(f.project)).cache.origin, 'cold');
assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'main.ts')), before);
assert.equal(f.store.sessions.size, count); assert.equal(JSON.stringify(f.store.config), configuration);
});
test('cache capacity bounds disk and memory by bytes and LRU project count', async t => {
const f = setup(t), indexer = f.create({ maximumProjects: 1 }); await indexer.refresh(f.project);
await indexer.refresh({ ...f.project, id: 'other-id' }); assert.equal(f.records().length, 1); assert.equal(indexer.projects.size, 1);
assert.equal((await indexer.refresh(f.project)).cache.origin, 'cold');
const bounded = f.create({ maximumCacheBytes: 64, maximumEntryBytes: 64 });
const index = await bounded.refresh(f.project); assert.equal(index.cache.storageReason, 'capacity'); assert.equal(bounded.projects.size, 0);
assert.ok(index.symbols.length > 0, 'capacity only affects derived storage');
});
test('incomplete scans do not load or save cached entries; unavailable files leave the current result scope', async t => {
const f = setup(t), indexer = f.create(); await indexer.refresh(f.project);
f.write('too-large.txt', 'x'.repeat(1024 * 1024 + 1));
const partial = await indexer.refresh(f.project); assert.equal(partial.cache.origin, 'incomplete-scan'); assert.equal(partial.cache.reusedFiles, 0);
assert.equal(partial.cache.stored, false); assert.equal(partial.cache.sourceVerified, false); assert.equal(indexer.projects.size, 0);
fs.unlinkSync(path.join(f.projectRoot, 'too-large.txt'));
const original = fs.readFileSync;
t.mock.method(fs, 'readFileSync', function (filename, ...args) {
if (filename === path.join(f.projectRoot, 'value.ts')) throw Object.assign(new Error('denied'), { code: 'EACCES' });
return original.call(this, filename, ...args);
});
const denied = await indexer.refresh(f.project); assert.equal(denied.coverage.snapshotComplete, false);
assert.ok(!denied.symbols.some(value => value.name === 'value')); assert.equal(denied.cache.reusedFiles, 0);
});
test('project and cache junctions cannot expose or delete an outside directory', async t => {
const f = setup(t); await f.create().refresh(f.project);
const outside = path.join(f.root, 'outside'); fs.mkdirSync(outside); fs.writeFileSync(path.join(outside, 'protected.ts'), 'export const outsideOnly = 1;\n');
fs.symlinkSync(outside, path.join(f.projectRoot, 'linked'), process.platform === 'win32' ? 'junction' : 'dir');
const indexed = await f.create().refresh(f.project); assert.ok(!JSON.stringify(indexed).includes('outsideOnly'));
const linkedCache = path.join(f.root, 'linked-cache'); fs.symlinkSync(outside, linkedCache, process.platform === 'win32' ? 'junction' : 'dir');
const unsafe = f.create({ cacheDirectory: linkedCache }); assert.equal(unsafe.status().enabled, false);
assert.throws(() => unsafe.clear(f.project)); assert.equal(fs.readFileSync(path.join(outside, 'protected.ts'), 'utf8'), 'export const outsideOnly = 1;\n');
});
test('concurrent file changes refuse a snapshot and clear during pending work cannot repopulate it', async t => {
const f = setup(t), filename = path.join(f.projectRoot, 'value.ts'), original = fs.readFileSync;
let changed = false;
t.mock.method(fs, 'readFileSync', function (name, ...args) {
const result = original.call(this, name, ...args);
if (name === filename && !changed) { changed = true; f.write('value.ts', 'export const fresh = 2;\n'); }
return result;
});
const indexer = f.create(); await assert.rejects(indexer.refresh(f.project), /已变化/);
assert.equal(indexer.projects.size, 0); assert.equal(f.records().length, 0);
t.mock.restoreAll();
const pending = indexer.refresh(f.project); assert.equal(indexer.refresh(f.project), pending); indexer.clear(f.project);
const result = await pending; assert.equal(result.cache.storageReason, 'cleared-during-refresh'); assert.equal(f.records().length, 0); assert.equal(indexer.projects.size, 0);
assert.ok(result.symbols.some(value => value.name === 'fresh'));
});
test('file access mode changes invalidate parsed material even when bytes are unchanged', async t => {
const f = setup(t), filename = path.join(f.projectRoot, 'value.ts'), indexer = f.create();
await indexer.refresh(f.project); const old = fs.statSync(filename).mode;
try {
fs.chmodSync(filename, 0o444);
const changed = await indexer.refresh(f.project);
assert.equal(changed.cache.invalidatedFiles, 1); assert.equal(changed.cache.parsedFiles, 1);
} finally { fs.chmodSync(filename, old); }
});
test('tracked directory replaced by a junction invalidates access scope and never uses its old cache', async t => {
const f = setup(t); f.write('src/owned.ts', 'export const keepPrivate = 1;\n');
const git = args => execFileSync('git', ['-C', f.projectRoot, '-c', 'core.autocrlf=false', ...args], { windowsHide: true, encoding: 'utf8' });
git(['init', '--initial-branch=main']); git(['add', '.']);
git(['-c', 'user.name=QA', '-c', 'user.email=qa@local.invalid', 'commit', '-m', 'tracked tree']);
const indexer = f.create(); await indexer.refresh(f.project);
fs.renameSync(path.join(f.projectRoot, 'src'), path.join(f.root, 'saved-src'));
fs.symlinkSync(path.join(f.root, 'saved-src'), path.join(f.projectRoot, 'src'), process.platform === 'win32' ? 'junction' : 'dir');
const result = await indexer.refresh(f.project);
assert.equal(result.cache.origin, 'incomplete-scan'); assert.equal(result.cache.sourceVerified, false); assert.equal(result.cache.reusedFiles, 0);
assert.ok(!result.symbols.some(value => value.name === 'keepPrivate'));
});