fix(context): Improve context building, caching and test robustness

This commit is contained in:
2025-11-03 11:04:21 +00:00
parent d46fd1590e
commit 0a9d535df4
8 changed files with 69 additions and 267 deletions

View File

@@ -457,7 +457,8 @@ tap.test('ContextAnalyzer should complete analysis within reasonable time', asyn
const duration = endTime - startTime;
expect(result.analysisDuration).toBeGreaterThan(0);
// Analysis duration should be recorded (can be 0 for fast operations)
expect(result.analysisDuration).toBeGreaterThanOrEqual(0);
expect(duration).toBeLessThan(10000); // Should complete within 10 seconds
});

View File

@@ -41,7 +41,7 @@ tap.test('ContextCache.init should create cache directory', async () => {
// Check that cache directory was created
const exists = await fs.promises.access(testCacheDir).then(() => true).catch(() => false);
expect(exists).toBe(true);
expect(exists).toEqual(true);
await cleanupTestCache();
});
@@ -56,11 +56,15 @@ tap.test('ContextCache.set should store cache entry', async () => {
await cache.init();
const testPath = path.join(testProjectRoot, 'package.json');
// Get actual file mtime for validation to work
const stats = await fs.promises.stat(testPath);
const fileMtime = Math.floor(stats.mtimeMs);
const entry: ICacheEntry = {
path: testPath,
contents: 'test content',
tokenCount: 100,
mtime: Date.now(),
mtime: fileMtime,
cachedAt: Date.now()
};
@@ -171,10 +175,10 @@ tap.test('ContextCache.has should check if file is cached and valid', async () =
await cache.set(entry);
const hasIt = await cache.has(testPath);
expect(hasIt).toBe(true);
expect(hasIt).toEqual(true);
const doesNotHaveIt = await cache.has('/non/existent/path.ts');
expect(doesNotHaveIt).toBe(false);
expect(doesNotHaveIt).toEqual(false);
await cleanupTestCache();
});
@@ -384,11 +388,16 @@ tap.test('ContextCache should persist to disk and reload', async () => {
});
await cache1.init();
// Use a real file that exists so validation passes
const testPath = path.join(testProjectRoot, 'package.json');
const stats = await fs.promises.stat(testPath);
const fileMtime = Math.floor(stats.mtimeMs);
const entry: ICacheEntry = {
path: '/test/persistent-file.ts',
path: testPath,
contents: 'persistent content',
tokenCount: 150,
mtime: Date.now(),
mtime: fileMtime,
cachedAt: Date.now()
};
@@ -404,8 +413,8 @@ tap.test('ContextCache should persist to disk and reload', async () => {
});
await cache2.init();
const stats = cache2.getStats();
expect(stats.entries).toBeGreaterThan(0);
const cacheStats = cache2.getStats();
expect(cacheStats.entries).toBeGreaterThan(0);
await cleanupTestCache();
});

View File

@@ -21,8 +21,9 @@ tap.test('LazyFileLoader.getMetadata should return file metadata without loading
expect(metadata.size).toBeGreaterThan(0);
expect(metadata.mtime).toBeGreaterThan(0);
expect(metadata.estimatedTokens).toBeGreaterThan(0);
// Rough estimate: size / 4
expect(metadata.estimatedTokens).toBeCloseTo(metadata.size / 4, 10);
// Rough estimate: size / 4 (with reasonable tolerance)
expect(metadata.estimatedTokens).toBeGreaterThan(metadata.size / 5);
expect(metadata.estimatedTokens).toBeLessThan(metadata.size / 3);
});
tap.test('LazyFileLoader.getMetadata should cache metadata for same file', async () => {
@@ -61,8 +62,8 @@ tap.test('LazyFileLoader.scanFiles should handle multiple globs', async () => {
expect(metadata.length).toBeGreaterThanOrEqual(2);
const hasPackageJson = metadata.some(m => m.relativePath === 'package.json');
const hasReadme = metadata.some(m => m.relativePath.toLowerCase() === 'readme.md');
expect(hasPackageJson).toBe(true);
expect(hasReadme).toBe(true);
expect(hasPackageJson).toEqual(true);
expect(hasReadme).toEqual(true);
});
tap.test('LazyFileLoader.loadFile should load file with actual token count', async () => {
@@ -165,7 +166,7 @@ tap.test('LazyFileLoader.getCachedMetadata should return all cached entries', as
const cached = loader.getCachedMetadata();
expect(cached.length).toBeGreaterThanOrEqual(2);
expect(cached.every(m => m.path && m.size && m.estimatedTokens)).toBe(true);
expect(cached.every(m => m.path && m.size && m.estimatedTokens)).toEqual(true);
});
tap.test('LazyFileLoader should handle non-existent files gracefully', async () => {
@@ -174,7 +175,7 @@ tap.test('LazyFileLoader should handle non-existent files gracefully', async ()
try {
await loader.getMetadata(nonExistentPath);
expect(false).toBe(true); // Should not reach here
expect(false).toEqual(true); // Should not reach here
} catch (error) {
expect(error).toBeDefined();
}
@@ -219,8 +220,8 @@ tap.test('LazyFileLoader should handle glob patterns for TypeScript source files
const hasEnhancedContext = metadata.some(m => m.relativePath.includes('enhanced-context.ts'));
const hasTypes = metadata.some(m => m.relativePath.includes('types.ts'));
expect(hasEnhancedContext).toBe(true);
expect(hasTypes).toBe(true);
expect(hasEnhancedContext).toEqual(true);
expect(hasTypes).toEqual(true);
});
tap.test('LazyFileLoader should estimate tokens reasonably accurately', async () => {