|
| 1 | +import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +import { handler } from './handler' |
| 4 | + |
| 5 | +const createMockResponse = (responseFn: () => Promise<Response>): typeof fetch => { |
| 6 | + return responseFn |
| 7 | +} |
| 8 | + |
| 9 | +const mockResponse = { |
| 10 | + id: 'gsheet_dataflow', |
| 11 | + name: 'GSheet data flow', |
| 12 | + last_successful_execution_id: '11' |
| 13 | +} |
| 14 | + |
| 15 | +// Response mocks |
| 16 | +const mockGetDataflow = createMockResponse( |
| 17 | + async () => new Response( |
| 18 | + JSON.stringify(mockResponse) |
| 19 | + ) |
| 20 | +) |
| 21 | + |
| 22 | +const mockGetDataflowError = createMockResponse( |
| 23 | + async () => new Response('Error when getting dataflow', { status: 500 }) |
| 24 | +) |
| 25 | + |
| 26 | +const mockFetch = vi.spyOn(globalThis, 'fetch') |
| 27 | + |
| 28 | +describe('get-dataflow', () => { |
| 29 | + beforeEach(async () => { |
| 30 | + mockFetch.mockReset() |
| 31 | + }) |
| 32 | + |
| 33 | + afterAll(async () => { |
| 34 | + mockFetch.mockRestore() |
| 35 | + }) |
| 36 | + |
| 37 | + it('returns data flow', async () => { |
| 38 | + mockFetch |
| 39 | + .mockImplementationOnce(mockGetDataflow) |
| 40 | + |
| 41 | + const toolResult = await handler({ dataflowId: 'gsheet_dataflow' }) |
| 42 | + |
| 43 | + expect(toolResult).toEqual({ |
| 44 | + isError: false, |
| 45 | + content: [{ |
| 46 | + type: 'text', |
| 47 | + text: JSON.stringify(mockResponse, null, 2), |
| 48 | + }], |
| 49 | + structuredContent: { |
| 50 | + dataflow: mockResponse |
| 51 | + }, |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + describe('with any error', () => { |
| 56 | + it('returns error message', async () => { |
| 57 | + mockFetch |
| 58 | + .mockImplementationOnce(mockGetDataflowError) |
| 59 | + |
| 60 | + const toolResult = await handler({ dataflowId: 'gsheet_dataflow' }) |
| 61 | + |
| 62 | + expect(toolResult).toEqual({ |
| 63 | + isError: true, |
| 64 | + content: [{ |
| 65 | + type: 'text', |
| 66 | + text: 'Failed to get data flow gsheet_dataflow. Response status: 500', |
| 67 | + }] |
| 68 | + }) |
| 69 | + }) |
| 70 | + }) |
| 71 | +}) |
| 72 | + |
| 73 | +describe('with invalid params', () => { |
| 74 | + it('returns errors on missing parameters', async () => { |
| 75 | + const toolResult = await handler() |
| 76 | + |
| 77 | + expect(toolResult).toEqual({ |
| 78 | + isError: true, |
| 79 | + content: [{ |
| 80 | + type: 'text', |
| 81 | + text: 'Invalid parameters for get-dataflow tool. Validation error: Required', |
| 82 | + }] |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + it('returns error on missing dataflowId', async () => { |
| 87 | + const toolResult = await handler({}) |
| 88 | + |
| 89 | + expect(toolResult).toEqual({ |
| 90 | + isError: true, |
| 91 | + content: [{ |
| 92 | + type: 'text', |
| 93 | + text: 'Invalid parameters for get-dataflow tool. Validation error: Required at "dataflowId"', |
| 94 | + }] |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + it('returns error on invalid dataflowId or extraneous parameters', async () => { |
| 99 | + const toolResult = await handler({ dataflowId: 123, executionId: true }) |
| 100 | + |
| 101 | + expect(toolResult).toEqual({ |
| 102 | + isError: true, |
| 103 | + content: [{ |
| 104 | + type: 'text', |
| 105 | + text: "Invalid parameters for get-dataflow tool. Validation error: Expected string, received number at \"dataflowId\"; Unrecognized key(s) in object: 'executionId'" |
| 106 | + }] |
| 107 | + }) |
| 108 | + }) |
| 109 | +}) |
0 commit comments