|
| 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, mockQueryRows } = vi.hoisted(() => ({ |
| 10 | + mockCheckAccess: vi.fn(), |
| 11 | + mockQueryRows: 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({ error: 'Access denied' }, { status: result.status }), |
| 20 | + } |
| 21 | +}) |
| 22 | + |
| 23 | +vi.mock('@/lib/table/service', () => ({ |
| 24 | + queryRows: mockQueryRows, |
| 25 | +})) |
| 26 | + |
| 27 | +import { GET } from '@/app/api/table/[tableId]/export/route' |
| 28 | + |
| 29 | +/** Table with an id-native column whose stable id (`col_email`) differs from its display name. */ |
| 30 | +function buildTable(): TableDefinition { |
| 31 | + return { |
| 32 | + id: 'tbl_1', |
| 33 | + name: 'People', |
| 34 | + description: null, |
| 35 | + schema: { |
| 36 | + columns: [ |
| 37 | + { id: 'col_email', name: 'email', type: 'string' }, |
| 38 | + { name: 'legacy', type: 'string' }, // legacy: id == name |
| 39 | + ], |
| 40 | + }, |
| 41 | + metadata: null, |
| 42 | + rowCount: 1, |
| 43 | + maxRows: 100, |
| 44 | + workspaceId: 'workspace-1', |
| 45 | + createdBy: 'user-1', |
| 46 | + archivedAt: null, |
| 47 | + createdAt: new Date('2024-01-01'), |
| 48 | + updatedAt: new Date('2024-01-01'), |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function callGet(format: string) { |
| 53 | + const req = new NextRequest(`http://localhost:3000/api/table/tbl_1/export?format=${format}`, { |
| 54 | + method: 'GET', |
| 55 | + }) |
| 56 | + return GET(req, { params: Promise.resolve({ tableId: 'tbl_1' }) }) |
| 57 | +} |
| 58 | + |
| 59 | +describe('table export route — id→name translation', () => { |
| 60 | + beforeEach(() => { |
| 61 | + vi.clearAllMocks() |
| 62 | + hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({ |
| 63 | + success: true, |
| 64 | + userId: 'user-1', |
| 65 | + authType: 'session', |
| 66 | + }) |
| 67 | + mockCheckAccess.mockResolvedValue({ ok: true, table: buildTable() }) |
| 68 | + // Row data is keyed by stable column id (`col_email`), not the display name. |
| 69 | + mockQueryRows.mockResolvedValue({ |
| 70 | + rows: [{ id: 'r1', data: { col_email: 'a@b.c', legacy: 'x' }, executions: {}, position: 0 }], |
| 71 | + rowCount: 1, |
| 72 | + totalCount: 1, |
| 73 | + limit: 1000, |
| 74 | + offset: 0, |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + it('CSV: header uses display names and cell values resolve from id-keyed data', async () => { |
| 79 | + const res = await callGet('csv') |
| 80 | + expect(res.status).toBe(200) |
| 81 | + const body = await res.text() |
| 82 | + const [header, firstRow] = body.trim().split('\n') |
| 83 | + expect(header).toBe('email,legacy') |
| 84 | + // Without id→name resolution the email cell would be blank. |
| 85 | + expect(firstRow).toBe('a@b.c,x') |
| 86 | + }) |
| 87 | + |
| 88 | + it('JSON: keys are display names, never the stable column id', async () => { |
| 89 | + const res = await callGet('json') |
| 90 | + expect(res.status).toBe(200) |
| 91 | + const parsed = JSON.parse(await res.text()) |
| 92 | + expect(parsed).toEqual([{ email: 'a@b.c', legacy: 'x' }]) |
| 93 | + expect(JSON.stringify(parsed)).not.toContain('col_email') |
| 94 | + }) |
| 95 | +}) |
0 commit comments