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

tool-input-normalizer.test.ts

146 lines4.2 KB

1import { describe, expect, it } from 'vitest'
2import { createToolInputNormalizer } from './tool-input-normalizer'
3import type { Tool } from '@tanstack/ai'
4​
5describe('createToolInputNormalizer', () => {
6 it('removes a null synthesized for an optional enum', () => {
7 const tool: Tool = {
8 name: 'optional_enum',
9 description: 'Uses an optional literal value',
10 inputSchema: {
11 type: 'object',
12 properties: {
13 value: { type: 'string', enum: ['canary'] },
14 },
15 required: [],
16 },
17 }
18 const normalize = createToolInputNormalizer([tool])
19​
20 expect(normalize(tool.name, { value: null })).toEqual({})
21 })
22​
23 it('leaves non-strict tool inputs unchanged', () => {
24 const tool: Tool = {
25 name: 'non_strict',
26 description: 'Uses a schema outside the strict subset',
27 inputSchema: {
28 type: 'object',
29 properties: { optional: { type: 'string' } },
30 oneOf: [{ required: ['optional'] }, { required: [] }],
31 },
32 }
33 const normalize = createToolInputNormalizer([tool])
34 const input = { optional: null }
35​
36 expect(normalize(tool.name, input)).toBe(input)
37 })
38​
39 it('does not guess when public tool names are ambiguous', () => {
40 const tools: Array<Tool> = [
41 {
42 name: 'duplicate',
43 description: 'First tool',
44 inputSchema: {
45 type: 'object',
46 properties: { firstOptional: { type: 'string' } },
47 },
48 },
49 {
50 name: 'duplicate',
51 description: 'Second tool',
52 inputSchema: {
53 type: 'object',
54 properties: { secondOptional: { type: 'string' } },
55 },
56 },
57 ]
58 const normalize = createToolInputNormalizer(tools)
59 const input = { firstOptional: null, secondOptional: null }
60​
61 expect(normalize('duplicate', input)).toBe(input)
62 })
63​
64 it('leaves anyOf inputs unchanged when variant widening is ambiguous', () => {
65 const tool: Tool = {
66 name: 'union',
67 description: 'Uses variant-specific nullability',
68 inputSchema: {
69 type: 'object',
70 properties: {
71 value: {
72 anyOf: [
73 {
74 type: 'object',
75 properties: {
76 kind: { const: 'optional' },
77 note: { type: 'string' },
78 },
79 required: ['kind'],
80 },
81 {
82 type: 'object',
83 properties: {
84 kind: { const: 'nullable' },
85 note: { type: ['string', 'null'] },
86 },
87 required: ['kind', 'note'],
88 },
89 ],
90 },
91 },
92 required: ['value'],
93 },
94 }
95 const normalize = createToolInputNormalizer([tool])
96 const input = { value: { kind: 'nullable', note: null } }
97​
98 expect(normalize(tool.name, input)).toBe(input)
99 })
100​
101 it.each([
102 ['const', { anyOf: [{ type: 'string' }, { const: null }] }],
103 ['enum', { anyOf: [{ type: 'string' }, { enum: [null] }] }],
104 ])('preserves genuine null accepted by an anyOf %s branch', (_, value) => {
105 const tool: Tool = {
106 name: 'nullable_union',
107 description: 'Uses an optional union that genuinely accepts null',
108 inputSchema: {
109 type: 'object',
110 properties: { value },
111 required: [],
112 },
113 }
114 const normalize = createToolInputNormalizer([tool])
115 const input = { value: null }
116​
117 expect(normalize(tool.name, input)).toBe(input)
118 })
119​
120 it('builds the inverse map from the supplied converter', () => {
121 const tool: Tool = {
122 name: 'ask',
123 description: 'Ask',
124 inputSchema: {
125 type: 'object',
126 properties: {
127 note: { type: 'string' },
128 },
129 required: ['note'],
130 },
131 }
132​
133 const defaultNormalize = createToolInputNormalizer([tool])
134 expect(defaultNormalize(tool.name, { note: null })).toEqual({ note: null })
135​
136 const normalize = createToolInputNormalizer([tool], () => ({
137 schema: {
138 type: 'object',
139 properties: { note: { type: ['string', 'null'] } },
140 required: ['note'],
141 },
142 nullWideningMap: { properties: { note: { widened: true } } },
143 }))
144 expect(normalize(tool.name, { note: null })).toEqual({})
145 })
146})