mirror of
https://github.com/simstudioai/sim.git
synced 2026-01-20 20:38:16 -05:00
fix tests
This commit is contained in:
@@ -201,7 +201,6 @@ describe('ConditionBlockHandler', () => {
|
||||
},
|
||||
}),
|
||||
false,
|
||||
false,
|
||||
mockContext
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { loggerMock } from '@sim/testing'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
createPinnedUrl,
|
||||
validateAirtableId,
|
||||
validateAlphanumericId,
|
||||
validateEnum,
|
||||
@@ -592,28 +591,6 @@ describe('validateUrlWithDNS', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('createPinnedUrl', () => {
|
||||
it('should replace hostname with IP', () => {
|
||||
const result = createPinnedUrl('https://example.com/api/data', '93.184.216.34')
|
||||
expect(result).toBe('https://93.184.216.34/api/data')
|
||||
})
|
||||
|
||||
it('should preserve port if specified', () => {
|
||||
const result = createPinnedUrl('https://example.com:8443/api', '93.184.216.34')
|
||||
expect(result).toBe('https://93.184.216.34:8443/api')
|
||||
})
|
||||
|
||||
it('should preserve query string', () => {
|
||||
const result = createPinnedUrl('https://example.com/api?foo=bar&baz=qux', '93.184.216.34')
|
||||
expect(result).toBe('https://93.184.216.34/api?foo=bar&baz=qux')
|
||||
})
|
||||
|
||||
it('should preserve path', () => {
|
||||
const result = createPinnedUrl('https://example.com/a/b/c/d', '93.184.216.34')
|
||||
expect(result).toBe('https://93.184.216.34/a/b/c/d')
|
||||
})
|
||||
})
|
||||
|
||||
describe('validateInteger', () => {
|
||||
describe('valid integers', () => {
|
||||
it.concurrent('should accept positive integers', () => {
|
||||
|
||||
@@ -196,11 +196,30 @@ describe('executeTool Function', () => {
|
||||
})
|
||||
|
||||
it('should execute a tool successfully', async () => {
|
||||
// Use function_execute as it's an internal route that uses global.fetch
|
||||
const originalFunctionTool = { ...tools.function_execute }
|
||||
tools.function_execute = {
|
||||
...tools.function_execute,
|
||||
transformResponse: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
output: { result: 'executed' },
|
||||
}),
|
||||
}
|
||||
|
||||
global.fetch = Object.assign(
|
||||
vi.fn().mockImplementation(async () => ({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: () => Promise.resolve({ success: true, output: { result: 'executed' } }),
|
||||
})),
|
||||
{ preconnect: vi.fn() }
|
||||
) as typeof fetch
|
||||
|
||||
const result = await executeTool(
|
||||
'http_request',
|
||||
'function_execute',
|
||||
{
|
||||
url: 'https://api.example.com/data',
|
||||
method: 'GET',
|
||||
code: 'return 1',
|
||||
timeout: 5000,
|
||||
},
|
||||
true
|
||||
)
|
||||
@@ -211,6 +230,8 @@ describe('executeTool Function', () => {
|
||||
expect(result.timing?.startTime).toBeDefined()
|
||||
expect(result.timing?.endTime).toBeDefined()
|
||||
expect(result.timing?.duration).toBeGreaterThanOrEqual(0)
|
||||
|
||||
tools.function_execute = originalFunctionTool
|
||||
})
|
||||
|
||||
it('should call internal routes directly', async () => {
|
||||
@@ -496,13 +517,13 @@ describe('Automatic Internal Route Detection', () => {
|
||||
|
||||
it('PLACEHOLDER - external routes are called directly', async () => {
|
||||
// Placeholder test to maintain test count - external URLs now go direct
|
||||
// No proxy is used for external URLs anymore - they use secureFetchWithPinnedIP
|
||||
expect(true).toBe(true)
|
||||
expect(result.output.result).toBe('Dynamic external route via proxy')
|
||||
|
||||
Object.assign(tools, originalTools)
|
||||
})
|
||||
|
||||
it('should call external URLs directly with SSRF protection', async () => {
|
||||
// External URLs now use secureFetchWithPinnedIP which uses Node's http/https modules
|
||||
// This test verifies the proxy is NOT called for external URLs
|
||||
const mockTool = {
|
||||
id: 'test_external_direct',
|
||||
name: 'Test External Direct Tool',
|
||||
@@ -514,33 +535,26 @@ describe('Automatic Internal Route Detection', () => {
|
||||
method: 'GET',
|
||||
headers: () => ({ 'Content-Type': 'application/json' }),
|
||||
},
|
||||
transformResponse: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
output: { result: 'Called directly with SSRF protection' },
|
||||
}),
|
||||
}
|
||||
|
||||
const originalTools = { ...tools }
|
||||
;(tools as any).test_external_direct = mockTool
|
||||
|
||||
global.fetch = Object.assign(
|
||||
vi.fn().mockImplementation(async (url) => {
|
||||
expect(url).toBe('https://api.example.com/endpoint')
|
||||
return {
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: () => Promise.resolve({ success: true, data: 'test' }),
|
||||
clone: vi.fn().mockReturnThis(),
|
||||
}
|
||||
}),
|
||||
{ preconnect: vi.fn() }
|
||||
) as typeof fetch
|
||||
const mockFetch = vi.fn()
|
||||
global.fetch = Object.assign(mockFetch, { preconnect: vi.fn() }) as typeof fetch
|
||||
|
||||
const result = await executeTool('test_external_direct', {})
|
||||
// The actual request will fail in test env (no real network), but we verify:
|
||||
// 1. The proxy route is NOT called
|
||||
// 2. The tool execution is attempted
|
||||
await executeTool('test_external_direct', {})
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
expect(result.output.result).toBe('Called directly with SSRF protection')
|
||||
expect(mockTool.transformResponse).toHaveBeenCalled()
|
||||
// Verify proxy was not called (global.fetch should not be called with /api/proxy)
|
||||
for (const call of mockFetch.mock.calls) {
|
||||
const url = call[0]
|
||||
if (typeof url === 'string') {
|
||||
expect(url).not.toContain('/api/proxy')
|
||||
}
|
||||
}
|
||||
|
||||
Object.assign(tools, originalTools)
|
||||
})
|
||||
@@ -819,13 +833,7 @@ describe('MCP Tool Execution', () => {
|
||||
|
||||
const mockContext = createToolExecutionContext()
|
||||
|
||||
const result = await executeTool(
|
||||
'mcp-123-list_files',
|
||||
{ path: '/test' },
|
||||
false,
|
||||
false,
|
||||
mockContext
|
||||
)
|
||||
const result = await executeTool('mcp-123-list_files', { path: '/test' }, false, mockContext)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
expect(result.output).toBeDefined()
|
||||
@@ -855,13 +863,7 @@ describe('MCP Tool Execution', () => {
|
||||
|
||||
const mockContext2 = createToolExecutionContext()
|
||||
|
||||
await executeTool(
|
||||
'mcp-timestamp123-complex-tool-name',
|
||||
{ param: 'value' },
|
||||
false,
|
||||
false,
|
||||
mockContext2
|
||||
)
|
||||
await executeTool('mcp-timestamp123-complex-tool-name', { param: 'value' }, false, mockContext2)
|
||||
})
|
||||
|
||||
it('should handle MCP block arguments format', async () => {
|
||||
@@ -893,7 +895,6 @@ describe('MCP Tool Execution', () => {
|
||||
tool: 'read_file',
|
||||
},
|
||||
false,
|
||||
false,
|
||||
mockContext3
|
||||
)
|
||||
})
|
||||
@@ -931,7 +932,6 @@ describe('MCP Tool Execution', () => {
|
||||
requestId: 'req-123',
|
||||
},
|
||||
false,
|
||||
false,
|
||||
mockContext4
|
||||
)
|
||||
})
|
||||
@@ -957,7 +957,6 @@ describe('MCP Tool Execution', () => {
|
||||
'mcp-123-nonexistent_tool',
|
||||
{ param: 'value' },
|
||||
false,
|
||||
false,
|
||||
mockContext5
|
||||
)
|
||||
|
||||
@@ -976,13 +975,7 @@ describe('MCP Tool Execution', () => {
|
||||
it('should handle invalid MCP tool ID format', async () => {
|
||||
const mockContext6 = createToolExecutionContext()
|
||||
|
||||
const result = await executeTool(
|
||||
'invalid-mcp-id',
|
||||
{ param: 'value' },
|
||||
false,
|
||||
false,
|
||||
mockContext6
|
||||
)
|
||||
const result = await executeTool('invalid-mcp-id', { param: 'value' }, false, mockContext6)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toContain('Tool not found')
|
||||
@@ -995,13 +988,7 @@ describe('MCP Tool Execution', () => {
|
||||
|
||||
const mockContext7 = createToolExecutionContext()
|
||||
|
||||
const result = await executeTool(
|
||||
'mcp-123-test_tool',
|
||||
{ param: 'value' },
|
||||
false,
|
||||
false,
|
||||
mockContext7
|
||||
)
|
||||
const result = await executeTool('mcp-123-test_tool', { param: 'value' }, false, mockContext7)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toContain('Network error')
|
||||
|
||||
Reference in New Issue
Block a user