|
| 1 | +import { describe, it, expect } from 'bun:test' |
| 2 | + |
| 3 | +import { handleReadSubtree } from '../tool/read-subtree' |
| 4 | +import { getStubProjectFileContext } from '@codebuff/common/util/file' |
| 5 | + |
| 6 | +import type { CodebuffToolCall } from '@codebuff/common/tools/list' |
| 7 | +import type { Logger } from '@codebuff/common/types/contracts/logger' |
| 8 | + |
| 9 | +function createLogger(): Logger { |
| 10 | + return { |
| 11 | + debug: () => {}, |
| 12 | + info: () => {}, |
| 13 | + warn: () => {}, |
| 14 | + error: () => {}, |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +function buildMockFileContext() { |
| 19 | + const ctx = getStubProjectFileContext() |
| 20 | + ctx.fileTree = [ |
| 21 | + { |
| 22 | + name: 'src', |
| 23 | + type: 'directory', |
| 24 | + filePath: 'src', |
| 25 | + children: [ |
| 26 | + { |
| 27 | + name: 'index.ts', |
| 28 | + type: 'file', |
| 29 | + filePath: 'src/index.ts', |
| 30 | + lastReadTime: 0, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: 'util.ts', |
| 34 | + type: 'file', |
| 35 | + filePath: 'src/util.ts', |
| 36 | + lastReadTime: 0, |
| 37 | + }, |
| 38 | + ], |
| 39 | + }, |
| 40 | + { |
| 41 | + name: 'package.json', |
| 42 | + type: 'file', |
| 43 | + filePath: 'package.json', |
| 44 | + lastReadTime: 0, |
| 45 | + }, |
| 46 | + ] |
| 47 | + ctx.fileTokenScores = { |
| 48 | + 'src/index.ts': { beta: 2.0, alpha: 1.0 }, |
| 49 | + 'src/util.ts': { helper: 3.0 }, |
| 50 | + 'package.json': {}, |
| 51 | + } |
| 52 | + return ctx |
| 53 | +} |
| 54 | + |
| 55 | +describe('handleReadSubtree', () => { |
| 56 | + it('returns a directory subtree blob with tokens for a directory path', async () => { |
| 57 | + const fileContext = buildMockFileContext() |
| 58 | + const logger = createLogger() |
| 59 | + |
| 60 | + const toolCall: CodebuffToolCall<'read_subtree'> = { |
| 61 | + toolName: 'read_subtree', |
| 62 | + toolCallId: 'tc-1', |
| 63 | + input: { paths: ['src'], maxTokens: 50000 }, |
| 64 | + } |
| 65 | + |
| 66 | + const { result } = handleReadSubtree({ |
| 67 | + previousToolCallFinished: Promise.resolve(), |
| 68 | + toolCall, |
| 69 | + fileContext, |
| 70 | + logger, |
| 71 | + }) |
| 72 | + |
| 73 | + const output = await result |
| 74 | + expect(Array.isArray(output)).toBe(true) |
| 75 | + expect(output[0].type).toBe('json') |
| 76 | + const value = output[0].value as any[] |
| 77 | + const dirEntry = value.find((v) => v.type === 'directory' && v.path === 'src') |
| 78 | + expect(dirEntry).toBeTruthy() |
| 79 | + expect(typeof dirEntry.printedTree).toBe('string') |
| 80 | + expect(dirEntry.printedTree).toContain('src/') |
| 81 | + expect(dirEntry.printedTree).toContain('index.ts') |
| 82 | + expect(typeof dirEntry.tokenCount).toBe('number') |
| 83 | + expect(['none', 'unimportant-files', 'tokens', 'depth-based']).toContain( |
| 84 | + dirEntry.truncationLevel, |
| 85 | + ) |
| 86 | + }) |
| 87 | + |
| 88 | + it('returns parsed variable names for a file path', async () => { |
| 89 | + const fileContext = buildMockFileContext() |
| 90 | + const logger = createLogger() |
| 91 | + |
| 92 | + const toolCall: CodebuffToolCall<'read_subtree'> = { |
| 93 | + toolName: 'read_subtree', |
| 94 | + toolCallId: 'tc-2', |
| 95 | + input: { paths: ['src/index.ts'], maxTokens: 50000 }, |
| 96 | + } |
| 97 | + |
| 98 | + const { result } = handleReadSubtree({ |
| 99 | + previousToolCallFinished: Promise.resolve(), |
| 100 | + toolCall, |
| 101 | + fileContext, |
| 102 | + logger, |
| 103 | + }) |
| 104 | + |
| 105 | + const output = await result |
| 106 | + expect(output[0].type).toBe('json') |
| 107 | + const value = output[0].value as any[] |
| 108 | + const fileEntry = value.find( |
| 109 | + (v) => v.type === 'file' && v.path === 'src/index.ts', |
| 110 | + ) |
| 111 | + expect(fileEntry).toBeTruthy() |
| 112 | + expect(Array.isArray(fileEntry.variables)).toBe(true) |
| 113 | + // Sorted by descending score: beta (2.0) before alpha (1.0) |
| 114 | + expect(fileEntry.variables[0]).toBe('beta') |
| 115 | + expect(fileEntry.variables).toContain('alpha') |
| 116 | + }) |
| 117 | + |
| 118 | + it('returns an error object for a missing path', async () => { |
| 119 | + const fileContext = buildMockFileContext() |
| 120 | + const logger = createLogger() |
| 121 | + |
| 122 | + const toolCall: CodebuffToolCall<'read_subtree'> = { |
| 123 | + toolName: 'read_subtree', |
| 124 | + toolCallId: 'tc-3', |
| 125 | + input: { paths: ['does-not-exist'], maxTokens: 50000 }, |
| 126 | + } |
| 127 | + |
| 128 | + const { result } = handleReadSubtree({ |
| 129 | + previousToolCallFinished: Promise.resolve(), |
| 130 | + toolCall, |
| 131 | + fileContext, |
| 132 | + logger, |
| 133 | + }) |
| 134 | + |
| 135 | + const output = await result |
| 136 | + expect(output[0].type).toBe('json') |
| 137 | + const value = output[0].value as any[] |
| 138 | + const errEntry = value.find((v) => v.path === 'does-not-exist' && v.errorMessage) |
| 139 | + expect(errEntry).toBeTruthy() |
| 140 | + expect(String(errEntry.errorMessage)).toContain('Path not found or ignored') |
| 141 | + }) |
| 142 | + |
| 143 | + it('honors maxTokens by reducing token count under a tiny budget', async () => { |
| 144 | + const fileContext = buildMockFileContext() |
| 145 | + const logger = createLogger() |
| 146 | + |
| 147 | + // Large budget (baseline) |
| 148 | + const largeToolCall: CodebuffToolCall<'read_subtree'> = { |
| 149 | + toolName: 'read_subtree', |
| 150 | + toolCallId: 'tc-4a', |
| 151 | + input: { paths: ['src'], maxTokens: 50000 }, |
| 152 | + } |
| 153 | + const { result: largeResultPromise } = handleReadSubtree({ |
| 154 | + previousToolCallFinished: Promise.resolve(), |
| 155 | + toolCall: largeToolCall, |
| 156 | + fileContext, |
| 157 | + logger, |
| 158 | + }) |
| 159 | + const largeOutput = await largeResultPromise |
| 160 | + expect(largeOutput[0].type).toBe('json') |
| 161 | + const largeValue = largeOutput[0].value as any[] |
| 162 | + const largeDirEntry = largeValue.find((v) => v.type === 'directory' && v.path === 'src') |
| 163 | + expect(largeDirEntry).toBeTruthy() |
| 164 | + |
| 165 | + // Tiny budget |
| 166 | + const tinyBudget = 5 |
| 167 | + const smallToolCall: CodebuffToolCall<'read_subtree'> = { |
| 168 | + toolName: 'read_subtree', |
| 169 | + toolCallId: 'tc-4b', |
| 170 | + input: { paths: ['src'], maxTokens: tinyBudget }, |
| 171 | + } |
| 172 | + const { result: smallResultPromise } = handleReadSubtree({ |
| 173 | + previousToolCallFinished: Promise.resolve(), |
| 174 | + toolCall: smallToolCall, |
| 175 | + fileContext, |
| 176 | + logger, |
| 177 | + }) |
| 178 | + const smallOutput = await smallResultPromise |
| 179 | + expect(smallOutput[0].type).toBe('json') |
| 180 | + const smallValue = smallOutput[0].value as any[] |
| 181 | + const smallDirEntry = smallValue.find((v) => v.type === 'directory' && v.path === 'src') |
| 182 | + expect(smallDirEntry).toBeTruthy() |
| 183 | + |
| 184 | + // Must honor the tiny budget |
| 185 | + expect(typeof smallDirEntry.tokenCount).toBe('number') |
| 186 | + expect(smallDirEntry.tokenCount).toBeLessThanOrEqual(tinyBudget) |
| 187 | + |
| 188 | + // Typically, token count under tiny budget should be <= baseline |
| 189 | + expect(smallDirEntry.tokenCount).toBeLessThanOrEqual(largeDirEntry.tokenCount) |
| 190 | + }) |
| 191 | +}) |
0 commit comments