|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { hybridAuthMockFns } from '@sim/testing' |
| 5 | +import { NextRequest } from 'next/server' |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import type { TableDefinition } from '@/lib/table' |
| 8 | + |
| 9 | +const { mockCheckAccess, mockFindRowMatches } = vi.hoisted(() => ({ |
| 10 | + mockCheckAccess: vi.fn(), |
| 11 | + mockFindRowMatches: vi.fn(), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/app/api/table/utils', async () => { |
| 15 | + const { NextResponse } = await import('next/server') |
| 16 | + return { |
| 17 | + checkAccess: mockCheckAccess, |
| 18 | + accessError: (result: { status: number }) => |
| 19 | + NextResponse.json( |
| 20 | + { error: result.status === 404 ? 'Table not found' : 'Access denied' }, |
| 21 | + { status: result.status } |
| 22 | + ), |
| 23 | + } |
| 24 | +}) |
| 25 | + |
| 26 | +vi.mock('@/lib/table/service', () => ({ |
| 27 | + findRowMatches: mockFindRowMatches, |
| 28 | +})) |
| 29 | + |
| 30 | +import { GET } from '@/app/api/table/[tableId]/rows/find/route' |
| 31 | + |
| 32 | +function buildTable(overrides: Partial<TableDefinition> = {}): TableDefinition { |
| 33 | + return { |
| 34 | + id: 'tbl_1', |
| 35 | + name: 'People', |
| 36 | + description: null, |
| 37 | + schema: { columns: [{ name: 'name', type: 'string' }] }, |
| 38 | + metadata: null, |
| 39 | + rowCount: 0, |
| 40 | + maxRows: 100, |
| 41 | + workspaceId: 'workspace-1', |
| 42 | + createdBy: 'user-1', |
| 43 | + archivedAt: null, |
| 44 | + createdAt: new Date('2024-01-01'), |
| 45 | + updatedAt: new Date('2024-01-01'), |
| 46 | + ...overrides, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function callGet( |
| 51 | + query: Record<string, string>, |
| 52 | + { tableId }: { tableId: string } = { tableId: 'tbl_1' } |
| 53 | +) { |
| 54 | + const params = new URLSearchParams(query) |
| 55 | + const req = new NextRequest(`http://localhost:3000/api/table/${tableId}/rows/find?${params}`, { |
| 56 | + method: 'GET', |
| 57 | + }) |
| 58 | + return GET(req, { params: Promise.resolve({ tableId }) }) |
| 59 | +} |
| 60 | + |
| 61 | +describe('GET /api/table/[tableId]/rows/find', () => { |
| 62 | + beforeEach(() => { |
| 63 | + vi.clearAllMocks() |
| 64 | + hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({ |
| 65 | + success: true, |
| 66 | + userId: 'user-1', |
| 67 | + authType: 'session', |
| 68 | + }) |
| 69 | + mockCheckAccess.mockResolvedValue({ ok: true, table: buildTable() }) |
| 70 | + mockFindRowMatches.mockResolvedValue({ |
| 71 | + matches: [{ ordinal: 4, rowId: 'row_4', column: 'name' }], |
| 72 | + truncated: false, |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + it('returns 401 when unauthenticated', async () => { |
| 77 | + hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValueOnce({ |
| 78 | + success: false, |
| 79 | + error: 'Authentication required', |
| 80 | + }) |
| 81 | + const res = await callGet({ workspaceId: 'workspace-1', q: 'foo' }) |
| 82 | + expect(res.status).toBe(401) |
| 83 | + expect(mockFindRowMatches).not.toHaveBeenCalled() |
| 84 | + }) |
| 85 | + |
| 86 | + it('returns 400 when q is missing', async () => { |
| 87 | + const res = await callGet({ workspaceId: 'workspace-1' }) |
| 88 | + expect(res.status).toBe(400) |
| 89 | + expect(mockFindRowMatches).not.toHaveBeenCalled() |
| 90 | + }) |
| 91 | + |
| 92 | + it('returns 400 on a workspace mismatch', async () => { |
| 93 | + const res = await callGet({ workspaceId: 'other-ws', q: 'foo' }) |
| 94 | + expect(res.status).toBe(400) |
| 95 | + expect(mockFindRowMatches).not.toHaveBeenCalled() |
| 96 | + }) |
| 97 | + |
| 98 | + it('returns 400 on invalid filter JSON', async () => { |
| 99 | + const res = await callGet({ workspaceId: 'workspace-1', q: 'foo', filter: '{not json' }) |
| 100 | + expect(res.status).toBe(400) |
| 101 | + }) |
| 102 | + |
| 103 | + it('returns matches and forwards q/filter/sort to the service', async () => { |
| 104 | + const res = await callGet({ |
| 105 | + workspaceId: 'workspace-1', |
| 106 | + q: 'alice', |
| 107 | + filter: JSON.stringify({ name: { $contains: 'a' } }), |
| 108 | + sort: JSON.stringify({ name: 'asc' }), |
| 109 | + }) |
| 110 | + expect(res.status).toBe(200) |
| 111 | + const body = await res.json() |
| 112 | + expect(body).toEqual({ |
| 113 | + success: true, |
| 114 | + data: { matches: [{ ordinal: 4, rowId: 'row_4', column: 'name' }], truncated: false }, |
| 115 | + }) |
| 116 | + expect(mockFindRowMatches).toHaveBeenCalledWith( |
| 117 | + expect.objectContaining({ id: 'tbl_1' }), |
| 118 | + { q: 'alice', filter: { name: { $contains: 'a' } }, sort: { name: 'asc' } }, |
| 119 | + expect.any(String) |
| 120 | + ) |
| 121 | + }) |
| 122 | + |
| 123 | + it('maps a TableQueryValidationError to 400', async () => { |
| 124 | + const { TableQueryValidationError } = await import('@/lib/table/sql') |
| 125 | + mockFindRowMatches.mockRejectedValueOnce(new TableQueryValidationError('Invalid field name')) |
| 126 | + const res = await callGet({ workspaceId: 'workspace-1', q: 'foo' }) |
| 127 | + expect(res.status).toBe(400) |
| 128 | + const body = await res.json() |
| 129 | + expect(body.error).toBe('Invalid field name') |
| 130 | + }) |
| 131 | + |
| 132 | + it('returns 404 when the table is not accessible', async () => { |
| 133 | + mockCheckAccess.mockResolvedValueOnce({ ok: false, status: 404 }) |
| 134 | + const res = await callGet({ workspaceId: 'workspace-1', q: 'foo' }) |
| 135 | + expect(res.status).toBe(404) |
| 136 | + }) |
| 137 | +}) |
0 commit comments