MCPcopy Index your code
hub / github.com/simstudioai/sim / ToolTester

Class ToolTester

packages/testing/src/builders/tool-tester.builder.ts:113–416  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

111 * Helper class for testing tools with controllable mock responses
112 */
113export class ToolTester<P = unknown, R = unknown> {
114 tool: TestToolConfig<P, R>
115 private mockFetch: MockFetch
116 private originalFetch: typeof fetch
117 private mockResponse: unknown
118 private mockResponseOptions: { ok: boolean; status: number; headers: Record<string, string> }
119 private error: Error | null = null
120
121 constructor(tool: TestToolConfig<P, R>) {
122 this.tool = tool
123 this.mockResponse = { success: true, output: {} }
124 this.mockResponseOptions = {
125 ok: true,
126 status: 200,
127 headers: { 'content-type': 'application/json' },
128 }
129 this.mockFetch = createToolMockFetch(this.mockResponse, this.mockResponseOptions)
130 this.originalFetch = global.fetch
131 }
132
133 /**
134 * Setup mock responses for this tool
135 */
136 setup(
137 response: unknown,
138 options: { ok?: boolean; status?: number; headers?: Record<string, string> } = {}
139 ) {
140 this.mockResponse = response
141 this.mockResponseOptions = {
142 ok: options.ok ?? true,
143 status: options.status ?? 200,
144 headers: options.headers ?? { 'content-type': 'application/json' },
145 }
146 this.mockFetch = createToolMockFetch(this.mockResponse, this.mockResponseOptions)
147 global.fetch = Object.assign(this.mockFetch, { preconnect: vi.fn() }) as typeof fetch
148 return this
149 }
150
151 /**
152 * Setup error responses for this tool
153 */
154 setupError(errorMessage: string, status = 400) {
155 this.mockFetch = createErrorFetch(errorMessage, status)
156 global.fetch = Object.assign(this.mockFetch, { preconnect: vi.fn() }) as typeof fetch
157
158 this.error = new Error(errorMessage)
159 ;(this.error as Error & { status: number }).status = status
160
161 if (status > 0) {
162 ;(this.error as Error & { response: unknown }).response = {
163 ok: false,
164 status,
165 statusText: errorMessage,
166 json: () => Promise.resolve({ error: errorMessage, message: errorMessage }),
167 }
168 }
169
170 return this

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected