|
| 1 | +import { BuiltinToolsEditor } from '../index'; |
| 2 | +import type { BuiltinToolOption } from '../index'; |
| 3 | +import { render, fireEvent, act } from '@testing-library/react'; |
| 4 | +import { IntlProvider } from 'react-intl'; |
| 5 | +import renderer from 'react-test-renderer'; |
| 6 | +import { describe, vi, beforeEach, it, expect } from 'vitest'; |
| 7 | +import { createLiteralValueSegment } from '../../editor/base/utils/helper'; |
| 8 | + |
| 9 | +const TestWrapper = ({ children }: { children: React.ReactNode }) => ( |
| 10 | + <IntlProvider locale="en" messages={{}}> |
| 11 | + {children} |
| 12 | + </IntlProvider> |
| 13 | +); |
| 14 | + |
| 15 | +describe('lib/builtintools', () => { |
| 16 | + const defaultOptions: BuiltinToolOption[] = [ |
| 17 | + { |
| 18 | + value: 'code_interpreter', |
| 19 | + displayName: 'Code Interpreter', |
| 20 | + description: 'Enable the agent to write and execute JavaScript code for calculations, data analysis, and file processing.', |
| 21 | + }, |
| 22 | + ]; |
| 23 | + |
| 24 | + const defaultProps = { |
| 25 | + initialValue: [], |
| 26 | + options: defaultOptions, |
| 27 | + onChange: vi.fn(), |
| 28 | + }; |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + vi.clearAllMocks(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should render with basic props', () => { |
| 35 | + const tree = renderer |
| 36 | + .create( |
| 37 | + <TestWrapper> |
| 38 | + <BuiltinToolsEditor {...defaultProps} /> |
| 39 | + </TestWrapper> |
| 40 | + ) |
| 41 | + .toJSON(); |
| 42 | + expect(tree).toMatchSnapshot(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should render header text', () => { |
| 46 | + const { getByText } = render( |
| 47 | + <TestWrapper> |
| 48 | + <BuiltinToolsEditor {...defaultProps} /> |
| 49 | + </TestWrapper> |
| 50 | + ); |
| 51 | + |
| 52 | + expect(getByText('Built-in Tools')).toBeTruthy(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should render tool display name and description', () => { |
| 56 | + const { getByText } = render( |
| 57 | + <TestWrapper> |
| 58 | + <BuiltinToolsEditor {...defaultProps} /> |
| 59 | + </TestWrapper> |
| 60 | + ); |
| 61 | + |
| 62 | + expect(getByText('Code Interpreter')).toBeTruthy(); |
| 63 | + expect( |
| 64 | + getByText('Enable the agent to write and execute JavaScript code for calculations, data analysis, and file processing.') |
| 65 | + ).toBeTruthy(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should render switch as unchecked when initialValue is empty', () => { |
| 69 | + const { getByRole } = render( |
| 70 | + <TestWrapper> |
| 71 | + <BuiltinToolsEditor {...defaultProps} initialValue={[]} /> |
| 72 | + </TestWrapper> |
| 73 | + ); |
| 74 | + |
| 75 | + const switchEl = getByRole('switch') as HTMLInputElement; |
| 76 | + expect(switchEl.checked).toBe(false); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should render switch as unchecked when initialValue is empty array string', () => { |
| 80 | + const { getByRole } = render( |
| 81 | + <TestWrapper> |
| 82 | + <BuiltinToolsEditor {...defaultProps} initialValue={[createLiteralValueSegment('[]')]} /> |
| 83 | + </TestWrapper> |
| 84 | + ); |
| 85 | + |
| 86 | + const switchEl = getByRole('switch') as HTMLInputElement; |
| 87 | + expect(switchEl.checked).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should render switch as checked when initialValue contains the tool', () => { |
| 91 | + const { getByRole } = render( |
| 92 | + <TestWrapper> |
| 93 | + <BuiltinToolsEditor {...defaultProps} initialValue={[createLiteralValueSegment('["code_interpreter"]')]} /> |
| 94 | + </TestWrapper> |
| 95 | + ); |
| 96 | + |
| 97 | + const switchEl = getByRole('switch') as HTMLInputElement; |
| 98 | + expect(switchEl.checked).toBe(true); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should call onChange with tool added when toggling on', () => { |
| 102 | + const onChange = vi.fn(); |
| 103 | + const { getByRole } = render( |
| 104 | + <TestWrapper> |
| 105 | + <BuiltinToolsEditor {...defaultProps} initialValue={[]} onChange={onChange} /> |
| 106 | + </TestWrapper> |
| 107 | + ); |
| 108 | + |
| 109 | + const switchEl = getByRole('switch'); |
| 110 | + act(() => { |
| 111 | + fireEvent.click(switchEl); |
| 112 | + }); |
| 113 | + |
| 114 | + expect(onChange).toHaveBeenCalledTimes(1); |
| 115 | + const callArg = onChange.mock.calls[0][0]; |
| 116 | + expect(callArg.value).toHaveLength(1); |
| 117 | + expect(callArg.value[0].value).toBe('["code_interpreter"]'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should call onChange with tool removed when toggling off', () => { |
| 121 | + const onChange = vi.fn(); |
| 122 | + const { getByRole } = render( |
| 123 | + <TestWrapper> |
| 124 | + <BuiltinToolsEditor {...defaultProps} initialValue={[createLiteralValueSegment('["code_interpreter"]')]} onChange={onChange} /> |
| 125 | + </TestWrapper> |
| 126 | + ); |
| 127 | + |
| 128 | + const switchEl = getByRole('switch'); |
| 129 | + act(() => { |
| 130 | + fireEvent.click(switchEl); |
| 131 | + }); |
| 132 | + |
| 133 | + expect(onChange).toHaveBeenCalledTimes(1); |
| 134 | + const callArg = onChange.mock.calls[0][0]; |
| 135 | + expect(callArg.value).toHaveLength(1); |
| 136 | + expect(callArg.value[0].value).toBe('[]'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should disable switch when readonly is true', () => { |
| 140 | + const { getByRole } = render( |
| 141 | + <TestWrapper> |
| 142 | + <BuiltinToolsEditor {...defaultProps} readonly={true} /> |
| 143 | + </TestWrapper> |
| 144 | + ); |
| 145 | + |
| 146 | + const switchEl = getByRole('switch'); |
| 147 | + expect(switchEl).toBeDisabled(); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should not call onChange when readonly and clicked', () => { |
| 151 | + const onChange = vi.fn(); |
| 152 | + const { getByRole } = render( |
| 153 | + <TestWrapper> |
| 154 | + <BuiltinToolsEditor {...defaultProps} readonly={true} onChange={onChange} /> |
| 155 | + </TestWrapper> |
| 156 | + ); |
| 157 | + |
| 158 | + const switchEl = getByRole('switch'); |
| 159 | + act(() => { |
| 160 | + fireEvent.click(switchEl); |
| 161 | + }); |
| 162 | + |
| 163 | + expect(onChange).not.toHaveBeenCalled(); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should render multiple tool options', () => { |
| 167 | + const multipleOptions: BuiltinToolOption[] = [ |
| 168 | + { |
| 169 | + value: 'code_interpreter', |
| 170 | + displayName: 'Code Interpreter', |
| 171 | + description: 'Execute code.', |
| 172 | + }, |
| 173 | + { |
| 174 | + value: 'file_search', |
| 175 | + displayName: 'File Search', |
| 176 | + description: 'Search files.', |
| 177 | + }, |
| 178 | + ]; |
| 179 | + |
| 180 | + const { getAllByRole, getByText } = render( |
| 181 | + <TestWrapper> |
| 182 | + <BuiltinToolsEditor {...defaultProps} options={multipleOptions} /> |
| 183 | + </TestWrapper> |
| 184 | + ); |
| 185 | + |
| 186 | + const switches = getAllByRole('switch'); |
| 187 | + expect(switches).toHaveLength(2); |
| 188 | + expect(getByText('Code Interpreter')).toBeTruthy(); |
| 189 | + expect(getByText('File Search')).toBeTruthy(); |
| 190 | + }); |
| 191 | + |
| 192 | + it('should handle invalid JSON in initialValue gracefully', () => { |
| 193 | + const { getByRole } = render( |
| 194 | + <TestWrapper> |
| 195 | + <BuiltinToolsEditor {...defaultProps} initialValue={[createLiteralValueSegment('not-valid-json')]} /> |
| 196 | + </TestWrapper> |
| 197 | + ); |
| 198 | + |
| 199 | + const switchEl = getByRole('switch') as HTMLInputElement; |
| 200 | + expect(switchEl.checked).toBe(false); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should handle non-array JSON in initialValue gracefully', () => { |
| 204 | + const { getByRole } = render( |
| 205 | + <TestWrapper> |
| 206 | + <BuiltinToolsEditor {...defaultProps} initialValue={[createLiteralValueSegment('"string_value"')]} /> |
| 207 | + </TestWrapper> |
| 208 | + ); |
| 209 | + |
| 210 | + const switchEl = getByRole('switch') as HTMLInputElement; |
| 211 | + expect(switchEl.checked).toBe(false); |
| 212 | + }); |
| 213 | + |
| 214 | + it('should render with empty options array', () => { |
| 215 | + const { queryByRole } = render( |
| 216 | + <TestWrapper> |
| 217 | + <BuiltinToolsEditor {...defaultProps} options={[]} /> |
| 218 | + </TestWrapper> |
| 219 | + ); |
| 220 | + |
| 221 | + expect(queryByRole('switch')).toBeNull(); |
| 222 | + }); |
| 223 | +}); |
0 commit comments