Files
insights/services/mcp/tests/unit/workflows-run-handler.test.ts
zeekay e552a2f764 Point the MCP tools at /v1/
The generator already resolves operations under /v1/ (40d120dbf7), and
the Python docstrings and prompts it copies say /v1/ too, but the
checked-in output still carried upstream's /api/ paths, so 771 tool
calls went to a prefix insights no longer serves.  Bring the generated
tools, types and schema in line with what the generator emits for the
/v1/ mount, and move the tests that assert those paths.

Left as they are: POST /api/orders and /api/feed are customer span
names in tool examples, and the connection tool's `api/projects/2/...`
mirrors insights_connection.py's help text.

Claude-Session: https://claude.ai/code/session_01TUxvfuta1Z44KVNYuAz71F
2026-09-24 15:00:46 -07:00

77 lines
2.5 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import { GENERATED_TOOLS } from '@/tools/generated/workflows'
import type { Context } from '@/tools/types'
interface RequestArgs {
method: string
path: string
body?: Record<string, unknown>
}
function createMockContext(): { context: Context; request: ReturnType<typeof vi.fn> } {
const request = vi.fn().mockResolvedValue({ status: 'success' })
const context = {
api: {
request,
getProjectBaseUrl: vi.fn().mockReturnValue('https://us.hanzo.ai/project/1'),
},
stateManager: {
getProjectId: vi.fn().mockResolvedValue('1'),
getOrgID: vi.fn(),
getRegion: vi.fn().mockResolvedValue('us'),
},
env: { INSIGHTS_BASE_URL: 'https://us.hanzo.ai' },
sessionManager: {},
cache: {},
getDistinctId: async () => 'test',
} as unknown as Context
return { context, request }
}
const runTool = GENERATED_TOOLS['workflows-test-run']!()
describe('workflows-test-run handler', () => {
it('posts to the invocations endpoint with the workflow id in the path', async () => {
const { context, request } = createMockContext()
await runTool.handler(context, {
id: 'wf-123',
globals: { event: { event: '$pageview', distinct_id: 'd1' } },
mock_async_functions: true,
})
const call = request.mock.calls[0]![0] as RequestArgs
expect(call.method).toBe('POST')
expect(call.path).toBe('/v1/projects/1/script_flows/wf-123/invocations/')
})
it('forwards globals, mock_async_functions, and current_action_id in the body', async () => {
const { context, request } = createMockContext()
await runTool.handler(context, {
id: 'wf-123',
globals: { event: { event: '$pageview', distinct_id: 'd1' } },
mock_async_functions: false,
current_action_id: 'action_42',
})
const call = request.mock.calls[0]![0] as RequestArgs
expect(call.body).toEqual({
globals: { event: { event: '$pageview', distinct_id: 'd1' } },
mock_async_functions: false,
current_action_id: 'action_42',
})
})
it('omits unset optional fields from the body', async () => {
const { context, request } = createMockContext()
await runTool.handler(context, { id: 'wf-123' })
const call = request.mock.calls[0]![0] as RequestArgs
expect(call.body).toEqual({})
})
})