-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathQueryEngine.test.ts
More file actions
99 lines (89 loc) · 2.68 KB
/
Copy pathQueryEngine.test.ts
File metadata and controls
99 lines (89 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { describe, expect, it } from 'bun:test'
import {
QueryEngine,
inferInitialToolChoiceFromPrompt,
type QueryEngineReplEvent,
} from './QueryEngine.js'
import type { QueryParams } from './query.js'
import type { Tools } from './Tool.js'
async function collectTurnEvents<TTerminal>(
stream: AsyncGenerator<QueryEngineReplEvent, TTerminal>,
): Promise<{
events: QueryEngineReplEvent[]
terminal: TTerminal
}> {
const events: QueryEngineReplEvent[] = []
while (true) {
const next = await stream.next()
if (next.done) {
return {
events,
terminal: next.value,
}
}
events.push(next.value)
}
}
describe('QueryEngine prepared turn seams', () => {
it('runPreparedTurn forwards the prepared params, events, and terminal value', async () => {
const params = { sentinel: 'prepared-turn' } as unknown as QueryParams
const expectedEvents: QueryEngineReplEvent[] = [
{ type: 'stream_request_start' } as QueryEngineReplEvent,
{
type: 'assistant',
message: { role: 'assistant', content: [] },
uuid: 'assistant-1',
timestamp: '2026-04-13T00:00:00.000Z',
} as QueryEngineReplEvent,
]
const terminal = { kind: 'terminal-success' } as const
let seenParams: QueryParams | undefined
const result = await collectTurnEvents(
QueryEngine.runPreparedTurn(params, {
async *runQuery(receivedParams) {
seenParams = receivedParams
for (const event of expectedEvents) {
yield event
}
return terminal as never
},
}),
)
expect(seenParams).toBe(params)
expect(result.events).toEqual(expectedEvents)
expect(result.terminal).toEqual(terminal)
})
})
describe('inferInitialToolChoiceFromPrompt', () => {
const tools = [
{ name: 'Bash' },
{ name: 'Read' },
{ name: 'Grep' },
] as unknown as Tools
it('requires a first tool call for repository review prompts', () => {
expect(
inferInitialToolChoiceFromPrompt('Please review this repository.', tools),
).toEqual({ type: 'any' })
expect(
inferInitialToolChoiceFromPrompt(
'Use available tools to inspect actual files before answering.',
tools,
),
).toEqual({ type: 'any' })
})
it('does not force tools for pure answer prompts', () => {
expect(
inferInitialToolChoiceFromPrompt(
'Reply with exactly this marker: K26_NCODE_OK',
tools,
),
).toBeUndefined()
})
it('preserves explicit caller tool choice', () => {
expect(
inferInitialToolChoiceFromPrompt('Please review this repository.', tools, {
type: 'auto',
}),
).toEqual({ type: 'auto' })
})
})