Skip to content

Commit cafdb95

Browse files
committed
fix: address PR review feedback
- Make EXCLUDED_GLOB_PATTERNS readonly to prevent accidental mutation by consumers (spreads at call sites are now intentional, creating mutable copies for APIs that require string[]) - Throw on unrecognized index format in test instead of silently defaulting to empty array (prevents polluter assertions from passing vacuously) - Move analyzerRegistry.register into test body — only one test, no need for beforeEach ceremony
1 parent 31fdda5 commit cafdb95

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

src/constants/codebase-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const EXCLUDED_DIRECTORY_NAMES = [
4949
] as const;
5050

5151
/** Glob patterns that match excluded directories at any nesting depth. */
52-
export const EXCLUDED_GLOB_PATTERNS: string[] = EXCLUDED_DIRECTORY_NAMES.map(
52+
export const EXCLUDED_GLOB_PATTERNS: readonly string[] = EXCLUDED_DIRECTORY_NAMES.map(
5353
(dir) => `**/${dir}/**`
5454
);
5555

tests/indexer-exclude-patterns.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
1+
import { afterEach, describe, expect, it } from 'vitest';
22
import { promises as fs } from 'fs';
33
import os from 'os';
44
import path from 'path';
@@ -13,16 +13,14 @@ import {
1313
describe('Indexer exclude patterns — nested directories', () => {
1414
let tempDir: string;
1515

16-
beforeEach(async () => {
17-
analyzerRegistry.register(new GenericAnalyzer());
18-
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'indexer-exclude-patterns-'));
19-
});
20-
2116
afterEach(async () => {
22-
await fs.rm(tempDir, { recursive: true, force: true });
17+
if (tempDir) await fs.rm(tempDir, { recursive: true, force: true });
2318
});
2419

2520
it('excludes nested coverage, worktrees, .claude, and dist directories', async () => {
21+
analyzerRegistry.register(new GenericAnalyzer());
22+
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'indexer-exclude-patterns-'));
23+
2624
// Legitimate source file
2725
await fs.mkdir(path.join(tempDir, 'src'), { recursive: true });
2826
await fs.writeFile(
@@ -41,10 +39,7 @@ describe('Indexer exclude patterns — nested directories', () => {
4139
for (const segments of polluters) {
4240
const dir = path.join(tempDir, ...segments.slice(0, -1));
4341
await fs.mkdir(dir, { recursive: true });
44-
await fs.writeFile(
45-
path.join(tempDir, ...segments),
46-
'export const polluter = true;\n'
47-
);
42+
await fs.writeFile(path.join(tempDir, ...segments), 'export const polluter = true;\n');
4843
}
4944

5045
const indexer = new CodebaseIndexer({
@@ -65,13 +60,18 @@ describe('Indexer exclude patterns — nested directories', () => {
6560

6661
const indexPath = path.join(tempDir, CODEBASE_CONTEXT_DIRNAME, KEYWORD_INDEX_FILENAME);
6762
const indexRaw = JSON.parse(await fs.readFile(indexPath, 'utf-8')) as Record<string, unknown>;
68-
const chunks = (
69-
Array.isArray(indexRaw)
70-
? indexRaw
71-
: Array.isArray(indexRaw?.chunks)
72-
? indexRaw.chunks
73-
: []
74-
) as Array<{ filePath: string }>;
63+
64+
let chunks: Array<{ filePath: string }>;
65+
if (Array.isArray(indexRaw)) {
66+
chunks = indexRaw;
67+
} else if (Array.isArray(indexRaw?.chunks)) {
68+
chunks = indexRaw.chunks as Array<{ filePath: string }>;
69+
} else {
70+
throw new Error(
71+
`Unexpected index format: keys=${JSON.stringify(Object.keys(indexRaw ?? {}))}`
72+
);
73+
}
74+
7575
const indexedPaths = chunks.map((chunk) => chunk.filePath);
7676

7777
// The legitimate file must be indexed

0 commit comments

Comments
 (0)