Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { z } from 'zod'
3import { toolDefinition } from '@tanstack/ai'
4import { createDiscoveryTool } from '../src/create-discovery-tool'
5
6const lazyA = toolDefinition({
7 name: 'fetchStocks',
8 description: 'Get stock prices.',
9 inputSchema: z.object({ ticker: z.string() }),
10 outputSchema: z.object({ price: z.number() }),
11 lazy: true,
12}).server(async () => ({ price: 0 }))
13
14describe('createDiscoveryTool', () => {
15 it('names the tool discover_tools and lists discoverable names in its description', () => {
16 const tool = createDiscoveryTool([lazyA])
17 expect(tool.name).toBe('discover_tools')
18 expect(tool.description).toContain('fetchStocks')
19 })
20
21 it('returns a TypeScript type stub + description for a known lazy tool', async () => {
22 const tool = createDiscoveryTool([lazyA])
23 const result = await tool.execute!({ toolNames: ['fetchStocks'] })
24 expect(result.tools).toHaveLength(1)
25 expect(result.tools[0]!.name).toBe('external_fetchStocks')
26 expect(result.tools[0]!.description).toBe('Get stock prices.')
27 expect(result.tools[0]!.typeStub).toContain(
28 'declare function external_fetchStocks',
29 )
30 expect(result.errors).toBeUndefined()
31 })
32
33 it('returns an error entry for an unknown name', async () => {
34 const tool = createDiscoveryTool([lazyA])
35 const result = await tool.execute!({ toolNames: ['nope'] })
36 expect(result.tools).toHaveLength(0)
37 expect(result.errors?.[0]).toContain("Unknown tool: 'nope'")
38 expect(result.errors?.[0]).toContain('fetchStocks')
39 })
40
41 it('resolves names passed with the external_ prefix (the catalog form)', async () => {
42 const tool = createDiscoveryTool([lazyA])
43 const result = await tool.execute!({ toolNames: ['external_fetchStocks'] })
44 expect(result.tools).toHaveLength(1)
45 expect(result.tools[0]!.name).toBe('external_fetchStocks')
46 expect(result.errors).toBeUndefined()
47 })
48
49 it('honors lazyToolsConfig.includeDescription in its own catalog', () => {
50 const namesOnly = createDiscoveryTool([lazyA])
51 expect(namesOnly.description).toContain('external_fetchStocks')
52 expect(namesOnly.description).not.toContain(
53 'external_fetchStocks — Get stock prices.',
54 )
55
56 const withDesc = createDiscoveryTool([lazyA], {
57 includeDescription: 'first-sentence',
58 })
59 expect(withDesc.description).toContain(
60 'external_fetchStocks — Get stock prices.',
61 )
62 })
63})