|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockBuildAPIUrl, mockBuildAuthHeaders, mockExtractAPIErrorMessage } = vi.hoisted(() => ({ |
| 7 | + mockBuildAPIUrl: vi.fn((path: string, params?: Record<string, string>) => { |
| 8 | + const url = new URL(path, 'http://localhost:3000') |
| 9 | + for (const [key, value] of Object.entries(params ?? {})) { |
| 10 | + url.searchParams.set(key, value) |
| 11 | + } |
| 12 | + return url |
| 13 | + }), |
| 14 | + mockBuildAuthHeaders: vi.fn(), |
| 15 | + mockExtractAPIErrorMessage: vi.fn(), |
| 16 | +})) |
| 17 | + |
| 18 | +vi.mock('@/executor/utils/http', () => ({ |
| 19 | + buildAPIUrl: mockBuildAPIUrl, |
| 20 | + buildAuthHeaders: mockBuildAuthHeaders, |
| 21 | + extractAPIErrorMessage: mockExtractAPIErrorMessage, |
| 22 | +})) |
| 23 | + |
| 24 | +import { enrichTableToolSchema } from '@/tools/schema-enrichers' |
| 25 | + |
| 26 | +const ORIGINAL_SCHEMA = { |
| 27 | + type: 'object' as const, |
| 28 | + properties: { |
| 29 | + filter: { type: 'object' }, |
| 30 | + sort: { type: 'object' }, |
| 31 | + }, |
| 32 | + required: [], |
| 33 | +} |
| 34 | + |
| 35 | +describe('enrichTableToolSchema', () => { |
| 36 | + beforeEach(() => { |
| 37 | + vi.clearAllMocks() |
| 38 | + mockBuildAuthHeaders.mockResolvedValue({ Authorization: 'Bearer internal-token' }) |
| 39 | + }) |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + vi.unstubAllGlobals() |
| 43 | + }) |
| 44 | + |
| 45 | + it('fetches the table through the authenticated detail route and enriches the schema', async () => { |
| 46 | + const mockFetch = vi.fn().mockResolvedValue( |
| 47 | + new Response( |
| 48 | + JSON.stringify({ |
| 49 | + success: true, |
| 50 | + data: { |
| 51 | + table: { |
| 52 | + name: 'Customers', |
| 53 | + schema: { |
| 54 | + columns: [ |
| 55 | + { name: 'email', type: 'string' }, |
| 56 | + { name: 'score', type: 'number' }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }), |
| 62 | + { status: 200, headers: { 'Content-Type': 'application/json' } } |
| 63 | + ) |
| 64 | + ) |
| 65 | + vi.stubGlobal('fetch', mockFetch) |
| 66 | + |
| 67 | + const result = await enrichTableToolSchema( |
| 68 | + 'table-1', |
| 69 | + 'table_query_rows', |
| 70 | + ORIGINAL_SCHEMA, |
| 71 | + 'Query rows', |
| 72 | + { workspaceId: 'workspace-1', userId: 'user-1' } |
| 73 | + ) |
| 74 | + |
| 75 | + expect(mockBuildAuthHeaders).toHaveBeenCalledWith('user-1') |
| 76 | + expect(mockFetch).toHaveBeenCalledWith( |
| 77 | + 'http://localhost:3000/api/table/table-1?workspaceId=workspace-1', |
| 78 | + { headers: { Authorization: 'Bearer internal-token' } } |
| 79 | + ) |
| 80 | + expect(result.description).toContain('Table "Customers" columns:') |
| 81 | + expect(result.parameters.required).toContain('filter') |
| 82 | + expect(result.parameters.properties.filter).toMatchObject({ |
| 83 | + description: expect.stringContaining('email, score'), |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + it('fails when the table detail request fails', async () => { |
| 88 | + vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(null, { status: 404 }))) |
| 89 | + mockExtractAPIErrorMessage.mockResolvedValue('Table not found') |
| 90 | + |
| 91 | + await expect( |
| 92 | + enrichTableToolSchema('missing-table', 'table_query_rows', ORIGINAL_SCHEMA, 'Query rows', { |
| 93 | + workspaceId: 'workspace-1', |
| 94 | + userId: 'user-1', |
| 95 | + }) |
| 96 | + ).rejects.toThrow('Failed to fetch table schema for missing-table: Table not found') |
| 97 | + }) |
| 98 | + |
| 99 | + it('fails when trusted execution identity is missing', async () => { |
| 100 | + await expect( |
| 101 | + enrichTableToolSchema('table-1', 'table_query_rows', ORIGINAL_SCHEMA, 'Query rows', {}) |
| 102 | + ).rejects.toThrow('Workspace ID is required to enrich table tool schema for table-1') |
| 103 | + }) |
| 104 | +}) |
0 commit comments