|
| 1 | +import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test' |
| 2 | + |
| 3 | +mock.module('src/utils/log.ts', () => ({ |
| 4 | + logError: () => {}, |
| 5 | + logToFile: () => {}, |
| 6 | + getLogDisplayTitle: () => '', |
| 7 | + logEvent: () => {}, |
| 8 | + logMCPError: () => {}, |
| 9 | + logMCPDebug: () => {}, |
| 10 | + dateToFilename: (d: Date) => d.toISOString().replace(/[:.]/g, '-'), |
| 11 | + getLogFilePath: () => '/tmp/mock-log', |
| 12 | + attachErrorLogSink: () => {}, |
| 13 | + getInMemoryErrors: () => [], |
| 14 | + loadErrorLogs: async () => [], |
| 15 | + getErrorLogByIndex: async () => null, |
| 16 | + captureAPIRequest: () => {}, |
| 17 | + _resetErrorLogForTesting: () => {}, |
| 18 | +})) |
| 19 | + |
| 20 | +mock.module('src/services/tokenEstimation.ts', () => ({ |
| 21 | + roughTokenCountEstimation: (text: string) => Math.ceil(text.length / 4), |
| 22 | + roughTokenCountEstimationForMessages: (msgs: unknown[]) => msgs.length * 64, |
| 23 | + roughTokenCountEstimationForMessage: () => 64, |
| 24 | + roughTokenCountEstimationForFileType: () => 64, |
| 25 | + bytesPerTokenForFileType: () => 4, |
| 26 | + countTokensWithAPI: async () => 0, |
| 27 | + countMessagesTokensWithAPI: async () => 0, |
| 28 | + countTokensViaHaikuFallback: async () => 0, |
| 29 | +})) |
| 30 | + |
| 31 | +let sessionMemoryInitialized = false |
| 32 | +mock.module('src/services/SessionMemory/sessionMemoryUtils.ts', () => ({ |
| 33 | + isSessionMemoryInitialized: () => sessionMemoryInitialized, |
| 34 | + waitForSessionMemoryExtraction: async () => {}, |
| 35 | + getLastSummarizedMessageId: () => undefined, |
| 36 | + getSessionMemoryContent: async () => null, |
| 37 | + setLastSummarizedMessageId: () => {}, |
| 38 | + markExtractionStarted: () => {}, |
| 39 | + markExtractionCompleted: () => {}, |
| 40 | + setSessionMemoryConfig: () => {}, |
| 41 | + getSessionMemoryConfig: () => ({}), |
| 42 | + recordExtractionTokenCount: () => {}, |
| 43 | + markSessionMemoryInitialized: () => {}, |
| 44 | + hasMetInitializationThreshold: () => false, |
| 45 | + hasMetUpdateThreshold: () => false, |
| 46 | + getToolCallsBetweenUpdates: () => 0, |
| 47 | + resetSessionMemoryState: () => {}, |
| 48 | + DEFAULT_SESSION_MEMORY_CONFIG: {}, |
| 49 | +})) |
| 50 | + |
| 51 | +mock.module('src/utils/slowOperations.ts', () => ({ |
| 52 | + jsonStringify: JSON.stringify, |
| 53 | + jsonParse: JSON.parse, |
| 54 | + slowLogging: { enabled: false }, |
| 55 | + clone: (value: unknown) => structuredClone(value), |
| 56 | + cloneDeep: (value: unknown) => structuredClone(value), |
| 57 | + callerFrame: () => '', |
| 58 | + SLOW_OPERATION_THRESHOLD_MS: 100, |
| 59 | + writeFileSync_DEPRECATED: () => {}, |
| 60 | +})) |
| 61 | + |
| 62 | +const { initContextCollapse, resetContextCollapse } = await import( |
| 63 | + 'src/services/contextCollapse/index.js' |
| 64 | +) |
| 65 | +const { tokenCountWithEstimation } = await import('src/utils/tokens.js') |
| 66 | +const { CtxInspectTool } = await import('../CtxInspectTool.js') |
| 67 | + |
| 68 | +function makeUserMessage(text: string) { |
| 69 | + return { |
| 70 | + type: 'user' as const, |
| 71 | + uuid: `user-${text}`, |
| 72 | + message: { role: 'user' as const, content: text }, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +function makeAssistantMessage(text: string) { |
| 77 | + return { |
| 78 | + type: 'assistant' as const, |
| 79 | + uuid: `assistant-${text}`, |
| 80 | + message: { |
| 81 | + role: 'assistant' as const, |
| 82 | + content: [{ type: 'text' as const, text }], |
| 83 | + }, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +function makeContext(messages: unknown[], mainLoopModel = 'claude-sonnet-4-6') { |
| 88 | + return { |
| 89 | + messages, |
| 90 | + options: { |
| 91 | + mainLoopModel, |
| 92 | + }, |
| 93 | + getAppState: () => ({}), |
| 94 | + } as any |
| 95 | +} |
| 96 | + |
| 97 | +const allowTool = async (input: Record<string, unknown>) => ({ |
| 98 | + behavior: 'allow' as const, |
| 99 | + updatedInput: input, |
| 100 | +}) |
| 101 | + |
| 102 | +const parentMessage = makeAssistantMessage('Parent tool call') |
| 103 | + |
| 104 | +beforeEach(() => { |
| 105 | + resetContextCollapse() |
| 106 | + sessionMemoryInitialized = false |
| 107 | +}) |
| 108 | + |
| 109 | +afterEach(() => { |
| 110 | + resetContextCollapse() |
| 111 | + sessionMemoryInitialized = false |
| 112 | +}) |
| 113 | + |
| 114 | +describe('CtxInspectTool', () => { |
| 115 | + test('tool exports and metadata remain stable', async () => { |
| 116 | + expect(CtxInspectTool).toBeDefined() |
| 117 | + expect(CtxInspectTool.name).toBe('CtxInspect') |
| 118 | + expect(typeof CtxInspectTool.call).toBe('function') |
| 119 | + expect(await CtxInspectTool.description()).toContain('context') |
| 120 | + expect(CtxInspectTool.userFacingName()).toBe('CtxInspect') |
| 121 | + expect(CtxInspectTool.isReadOnly()).toBe(true) |
| 122 | + expect(CtxInspectTool.isConcurrencySafe()).toBe(true) |
| 123 | + }) |
| 124 | + |
| 125 | + test('formats tool results for transcript rendering', () => { |
| 126 | + const block = CtxInspectTool.mapToolResultToToolResultBlockParam( |
| 127 | + { |
| 128 | + total_tokens: 192, |
| 129 | + message_count: 3, |
| 130 | + context_window_model: 'claude-sonnet-4-6', |
| 131 | + prompt_caching_enabled: true, |
| 132 | + session_memory_enabled: true, |
| 133 | + context_collapse_enabled: false, |
| 134 | + summary: 'Context collapse: disabled', |
| 135 | + }, |
| 136 | + 'tool-use-id', |
| 137 | + ) |
| 138 | + |
| 139 | + expect(block.tool_use_id).toBe('tool-use-id') |
| 140 | + expect(block.content).toContain('192 tokens') |
| 141 | + expect(block.content).toContain('3 messages') |
| 142 | + expect(block.content).toContain('Context collapse: disabled') |
| 143 | + }) |
| 144 | + |
| 145 | + test('returns live context counts and mechanism state', async () => { |
| 146 | + const messages = [ |
| 147 | + makeUserMessage('Inspect the current context budget.'), |
| 148 | + makeAssistantMessage('Looking at the current conversation state.'), |
| 149 | + ] |
| 150 | + const context = makeContext(messages, 'claude-sonnet-4-6') |
| 151 | + |
| 152 | + const result = await (CtxInspectTool as any).call( |
| 153 | + {}, |
| 154 | + context, |
| 155 | + allowTool, |
| 156 | + parentMessage, |
| 157 | + ) |
| 158 | + |
| 159 | + expect(Object.keys(result.data).sort()).toEqual([ |
| 160 | + 'context_collapse_enabled', |
| 161 | + 'context_window_model', |
| 162 | + 'message_count', |
| 163 | + 'prompt_caching_enabled', |
| 164 | + 'session_memory_enabled', |
| 165 | + 'summary', |
| 166 | + 'total_tokens', |
| 167 | + ]) |
| 168 | + expect(result.data.message_count).toBe(messages.length) |
| 169 | + expect(result.data.total_tokens).toBe(tokenCountWithEstimation(messages as any)) |
| 170 | + expect(result.data.context_window_model).toBe('claude-sonnet-4-6') |
| 171 | + expect(result.data.prompt_caching_enabled).toBe(true) |
| 172 | + expect(result.data.session_memory_enabled).toBe(false) |
| 173 | + expect(result.data.context_collapse_enabled).toBe(false) |
| 174 | + expect(result.data.summary).toContain('Overall context summary') |
| 175 | + expect(result.data.summary).toContain('Session memory: disabled') |
| 176 | + expect(result.data.summary).toContain('Context collapse: disabled') |
| 177 | + }) |
| 178 | + |
| 179 | + test('query input focuses summary and collapse runtime changes the reported state', async () => { |
| 180 | + const messages = [ |
| 181 | + makeUserMessage('Show me tool usage pressure in this thread.'), |
| 182 | + makeAssistantMessage('Summarizing tool-heavy context now.'), |
| 183 | + ] |
| 184 | + const context = makeContext(messages, 'claude-sonnet-4-6') |
| 185 | + |
| 186 | + const disabledResult = await (CtxInspectTool as any).call( |
| 187 | + { query: 'tool usage' }, |
| 188 | + context, |
| 189 | + allowTool, |
| 190 | + parentMessage, |
| 191 | + ) |
| 192 | + |
| 193 | + initContextCollapse() |
| 194 | + |
| 195 | + const enabledResult = await (CtxInspectTool as any).call( |
| 196 | + { query: 'tool usage' }, |
| 197 | + context, |
| 198 | + allowTool, |
| 199 | + parentMessage, |
| 200 | + ) |
| 201 | + |
| 202 | + expect(disabledResult.data.message_count).toBe(messages.length) |
| 203 | + expect(enabledResult.data.message_count).toBe(messages.length) |
| 204 | + expect(disabledResult.data.total_tokens).toBe( |
| 205 | + tokenCountWithEstimation(messages as any), |
| 206 | + ) |
| 207 | + expect(enabledResult.data.total_tokens).toBe( |
| 208 | + tokenCountWithEstimation(messages as any), |
| 209 | + ) |
| 210 | + expect(disabledResult.data.summary).toContain('Focus: tool usage') |
| 211 | + expect(disabledResult.data.context_collapse_enabled).toBe(false) |
| 212 | + expect(enabledResult.data.context_collapse_enabled).toBe(true) |
| 213 | + expect(enabledResult.data.summary).toContain('Context collapse: enabled') |
| 214 | + expect(enabledResult.data.summary).toContain('Collapse spans:') |
| 215 | + }) |
| 216 | +}) |
0 commit comments