claude-code-best/tests/integration/context-build.test.ts
James Feng 76c47956dd fix: resolve ~50 CodeQL alerts — unused, injection, type issues
unused-local-variable / useless-assignment (~30 alerts):
- mcp-client: manager.ts (11), connection.ts (3)
- acp-link: server.ts (4), rcs-upstream.ts
- remote-control-server: 10 files (SessionDetail, acp/client, transports, etc.)
- src: constants/tools.ts, attribution.ts, win32.ts, main.tsx, attachments.ts

log-injection (2):
- chromeNativeHost.ts, rcs-upstream.ts — apply sanitizeLog()

indirect-command-line-injection (4):
- imagePaste.ts: convert shell:true to explicit sh -c pattern

use-before-declaration (4):
- query.ts, claude.ts, tools.ts, attachments.ts — move feature() import up

comparison-between-incompatible-types (2):
- ExitPlanModePermissionRequest.tsx, useTerminalNotification.ts

shared: sensitive.ts — sanitizeLog() helper
2026-06-08 17:15:22 +08:00

110 lines
3.9 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import {
stripHtmlComments,
isMemoryFilePath,
getLargeMemoryFiles,
} from '../../src/utils/claudemd'
import { buildEffectiveSystemPrompt } from '../../src/utils/systemPrompt'
import {
createTempDir,
cleanupTempDir,
writeTempFile,
} from '../mocks/file-system'
// ─── CLAUDE.md Integration with System Prompt ─────────────────────────
describe('Context build: CLAUDE.md + system prompt integration', () => {
test('buildEffectiveSystemPrompt passes through default prompt', () => {
const result = buildEffectiveSystemPrompt({
defaultSystemPrompt: 'You are Claude.',
})
// Result is an array of strings (may be split differently)
const joined = Array.from(result).join('')
expect(joined).toBe('You are Claude.')
})
test('buildEffectiveSystemPrompt handles empty prompts', () => {
const result = buildEffectiveSystemPrompt({
defaultSystemPrompt: '',
})
const joined = Array.from(result).join('')
expect(joined).toBe('')
})
test('buildEffectiveSystemPrompt with overrideSystemPrompt replaces everything', () => {
const result = buildEffectiveSystemPrompt({
defaultSystemPrompt: 'Default',
overrideSystemPrompt: 'Override',
})
const joined = Array.from(result).join('')
expect(joined).toBe('Override')
})
test('buildEffectiveSystemPrompt with customSystemPrompt replaces default', () => {
const result = buildEffectiveSystemPrompt({
defaultSystemPrompt: 'Default',
customSystemPrompt: 'Custom',
})
const joined = Array.from(result).join('')
expect(joined).toBe('Custom')
})
test('buildEffectiveSystemPrompt with appendSystemPrompt includes both', () => {
const result = buildEffectiveSystemPrompt({
defaultSystemPrompt: 'Main prompt',
appendSystemPrompt: 'Appended',
})
const joined = Array.from(result).join('')
expect(joined).toContain('Main prompt')
expect(joined).toContain('Appended')
// Appended should come after main
expect(joined.indexOf('Main prompt')).toBeLessThan(
joined.indexOf('Appended'),
)
})
})
// ─── CLAUDE.md Discovery with Real File System ───────────────────────
describe('Context build: CLAUDE.md file system integration', () => {
let tempDir: string
test('strips HTML comments from CLAUDE.md content', () => {
const input = '<!-- this is a comment -->Actual content'
const { content, stripped } = stripHtmlComments(input)
expect(content).toBe('Actual content')
expect(stripped).toBe(true)
})
test('preserves code blocks when stripping HTML comments', () => {
const input = '```\n<!-- not a real comment -->\n```\nReal text'
const { content } = stripHtmlComments(input)
expect(content).toContain('<!-- not a real comment -->')
expect(content).toContain('Real text')
})
test('isMemoryFilePath correctly identifies CLAUDE.md paths', () => {
expect(isMemoryFilePath('/project/CLAUDE.md')).toBe(true)
expect(isMemoryFilePath('/project/CLAUDE.local.md')).toBe(true)
expect(isMemoryFilePath('/project/.claude/rules/file.md')).toBe(true)
expect(isMemoryFilePath('/project/README.md')).toBe(false)
expect(isMemoryFilePath('/project/src/index.ts')).toBe(false)
})
})
// ─── Large Memory File Filtering ──────────────────────────────────────
describe('Context build: large memory file filtering', () => {
test('getLargeMemoryFiles returns empty for empty input', () => {
expect(getLargeMemoryFiles([])).toEqual([])
})
test('getLargeMemoryFiles returns empty when all files are small', () => {
const files = [
{ path: '/a/CLAUDE.md', content: 'small' },
{ path: '/b/CLAUDE.md', content: 'also small' },
]
expect(getLargeMemoryFiles(files)).toEqual([])
})
})