Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

define.test.ts

114 lines3.2 KB

1import { describe, expect, it } from 'vitest'
2import { z } from 'zod'
3import { defineAgent, toolDefinition } from '@tanstack/ai'
4import { memoryPersistence } from '@tanstack/ai-persistence'
5import { createHarnessHost, defineHarness, isHarnessDefinition } from '../src'
6import { mockAdapter, text, toolCall } from './helpers'
7​
8const agent = (name: string) =>
9 defineAgent({
10 name,
11 description: name,
12 inputSchema: z.object({ q: z.string() }),
13 run: async () => 'ok',
14 })
15​
16describe('defineHarness', () => {
17 const { adapter } = mockAdapter([])
18​
19 it('returns a frozen, branded definition', () => {
20 const harness = defineHarness({ name: 'acme/test', adapter })
21 expect(isHarnessDefinition(harness)).toBe(true)
22 expect(Object.isFrozen(harness)).toBe(true)
23 expect(isHarnessDefinition({ name: 'x' })).toBe(false)
24 })
25​
26 it('rejects an empty name', () => {
27 expect(() => defineHarness({ name: ' ', adapter })).toThrow(
28 'non-empty name',
29 )
30 })
31​
32 it('rejects two different agents with one name', () => {
33 expect(() =>
34 defineHarness({
35 name: 'acme/test',
36 adapter,
37 agents: [agent('same')],
38 subagents: { agents: [agent('same')] },
39 }),
40 ).toThrow('two different agents are named "same"')
41 })
42​
43 it('allows one agent in both agents and subagents', () => {
44 const shared = agent('shared')
45 expect(() =>
46 defineHarness({
47 name: 'acme/test',
48 adapter,
49 agents: [shared],
50 subagents: { agents: [shared] },
51 }),
52 ).not.toThrow()
53 })
54​
55 it('rejects expose names that are not agents', () => {
56 expect(() =>
57 defineHarness({
58 name: 'acme/test',
59 adapter,
60 agents: [agent('real')],
61 expose: { agents: ['missing' as 'real'] },
62 }),
63 ).toThrow('expose.agents names "missing"')
64 })
65})
66​
67describe('toolExecution', () => {
68 it("runs the tools of one model call one at a time with 'sequential'", async () => {
69 const log: Array<string> = []
70 const tool = (name: string) =>
71 toolDefinition({
72 name,
73 description: name,
74 inputSchema: z.object({}),
75 }).server(async () => {
76 log.push(`start:${name}`)
77 // Wait one macrotask. Parallel tools all start before it ends.
78 await new Promise((resolve) => setTimeout(resolve, 0))
79 log.push(`end:${name}`)
80 return {}
81 })
82 // One model call with two tool calls: drop the RUN_FINISHED of the
83 // first call and the RUN_STARTED of the second.
84 const bothTools = () => [
85 ...toolCall('first', {}, 'call-1').slice(0, -1),
86 ...toolCall('second', {}, 'call-2').slice(1),
87 ]
88 const { adapter } = mockAdapter([bothTools, () => text('done')])
89 const host = createHarnessHost({ persistence: memoryPersistence() })
90 const session = await host.open(
91 defineHarness({
92 name: 'test/sequential',
93 adapter,
94 tools: [tool('first'), tool('second')],
95 toolExecution: 'sequential',
96 }),
97 {
98 threadId: 't1',
99 },
100 )
101​
102 await expect(session.send('go')).resolves.toEqual({
103 text: 'done',
104 media: [],
105 })
106 expect(log).toEqual([
107 'start:first',
108 'end:first',
109 'start:second',
110 'end:second',
111 ])
112 await host.close()
113 })
114})