|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * The PATCH handler performs several writes, each in its own locked |
| 5 | + * transaction. `renameColumn` is the first, so any rejection raised after it |
| 6 | + * returns an error with the rename already committed — a partial update the |
| 7 | + * caller cannot see or undo. These pin the pre-flight guards ahead of it. |
| 8 | + */ |
| 9 | +import { hybridAuthMockFns } from '@sim/testing' |
| 10 | +import { NextRequest } from 'next/server' |
| 11 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 12 | + |
| 13 | +const { |
| 14 | + mockCheckAccess, |
| 15 | + mockRenameColumn, |
| 16 | + mockUpdateColumnType, |
| 17 | + mockUpdateColumnCurrency, |
| 18 | + mockUpdateColumnOptions, |
| 19 | + mockUpdateColumnConstraints, |
| 20 | + mockAddTableColumn, |
| 21 | + mockDeleteColumn, |
| 22 | +} = vi.hoisted(() => ({ |
| 23 | + mockCheckAccess: vi.fn(), |
| 24 | + mockRenameColumn: vi.fn(), |
| 25 | + mockUpdateColumnType: vi.fn(), |
| 26 | + mockUpdateColumnCurrency: vi.fn(), |
| 27 | + mockUpdateColumnOptions: vi.fn(), |
| 28 | + mockUpdateColumnConstraints: vi.fn(), |
| 29 | + mockAddTableColumn: vi.fn(), |
| 30 | + mockDeleteColumn: vi.fn(), |
| 31 | +})) |
| 32 | + |
| 33 | +vi.mock('@/lib/table', () => ({ |
| 34 | + addTableColumn: mockAddTableColumn, |
| 35 | + deleteColumn: mockDeleteColumn, |
| 36 | + renameColumn: mockRenameColumn, |
| 37 | + updateColumnConstraints: mockUpdateColumnConstraints, |
| 38 | + updateColumnCurrency: mockUpdateColumnCurrency, |
| 39 | + updateColumnOptions: mockUpdateColumnOptions, |
| 40 | + updateColumnType: mockUpdateColumnType, |
| 41 | +})) |
| 42 | +vi.mock('@/app/api/table/utils', () => ({ |
| 43 | + accessError: () => new Response('denied', { status: 403 }), |
| 44 | + checkAccess: mockCheckAccess, |
| 45 | + normalizeColumn: (c: unknown) => c, |
| 46 | + rootErrorMessage: (e: unknown) => (e instanceof Error ? e.message : String(e)), |
| 47 | + tableLockErrorResponse: () => null, |
| 48 | +})) |
| 49 | + |
| 50 | +import { PATCH } from '@/app/api/table/[tableId]/columns/route' |
| 51 | + |
| 52 | +const WORKSPACE_ID = '11111111-1111-4111-8111-111111111111' |
| 53 | + |
| 54 | +function patch(updates: Record<string, unknown>) { |
| 55 | + return PATCH( |
| 56 | + new NextRequest('http://localhost/api/table/t1/columns', { |
| 57 | + method: 'PATCH', |
| 58 | + body: JSON.stringify({ workspaceId: WORKSPACE_ID, columnName: 'amount', updates }), |
| 59 | + headers: { 'content-type': 'application/json' }, |
| 60 | + }), |
| 61 | + { params: Promise.resolve({ tableId: 't1' }) } |
| 62 | + ) |
| 63 | +} |
| 64 | + |
| 65 | +describe('PATCH /api/table/[tableId]/columns — pre-flight guards', () => { |
| 66 | + beforeEach(() => { |
| 67 | + vi.clearAllMocks() |
| 68 | + hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({ |
| 69 | + success: true, |
| 70 | + userId: 'user-1', |
| 71 | + authType: 'session', |
| 72 | + }) |
| 73 | + mockCheckAccess.mockResolvedValue({ |
| 74 | + ok: true, |
| 75 | + table: { |
| 76 | + workspaceId: WORKSPACE_ID, |
| 77 | + schema: { columns: [{ id: 'col_a', name: 'amount', type: 'number' }] }, |
| 78 | + }, |
| 79 | + }) |
| 80 | + mockRenameColumn.mockResolvedValue({ schema: { columns: [] } }) |
| 81 | + }) |
| 82 | + |
| 83 | + it('rejects a currency code on a non-currency column without renaming first', async () => { |
| 84 | + const response = await patch({ name: 'renamed', currencyCode: 'USD' }) |
| 85 | + |
| 86 | + expect(response.status).toBe(400) |
| 87 | + expect(await response.json()).toMatchObject({ |
| 88 | + error: expect.stringContaining('Cannot set currency'), |
| 89 | + }) |
| 90 | + // The whole point: the rename must not have been committed. |
| 91 | + expect(mockRenameColumn).not.toHaveBeenCalled() |
| 92 | + expect(mockUpdateColumnCurrency).not.toHaveBeenCalled() |
| 93 | + }) |
| 94 | + |
| 95 | + it('rejects an unsupported currency code without renaming first', async () => { |
| 96 | + mockCheckAccess.mockResolvedValue({ |
| 97 | + ok: true, |
| 98 | + table: { |
| 99 | + workspaceId: WORKSPACE_ID, |
| 100 | + schema: { columns: [{ id: 'col_a', name: 'amount', type: 'currency' }] }, |
| 101 | + }, |
| 102 | + }) |
| 103 | + |
| 104 | + const response = await patch({ name: 'renamed', currencyCode: 'ZZZ' }) |
| 105 | + |
| 106 | + expect(response.status).toBe(400) |
| 107 | + expect(await response.json()).toMatchObject({ |
| 108 | + error: expect.stringContaining('Invalid currency code'), |
| 109 | + }) |
| 110 | + expect(mockRenameColumn).not.toHaveBeenCalled() |
| 111 | + }) |
| 112 | + |
| 113 | + it('rejects unique on a type that cannot carry it without renaming first', async () => { |
| 114 | + mockCheckAccess.mockResolvedValue({ |
| 115 | + ok: true, |
| 116 | + table: { |
| 117 | + workspaceId: WORKSPACE_ID, |
| 118 | + schema: { |
| 119 | + columns: [ |
| 120 | + { id: 'col_a', name: 'amount', type: 'select', options: [{ id: 'o', name: 'O' }] }, |
| 121 | + ], |
| 122 | + }, |
| 123 | + }, |
| 124 | + }) |
| 125 | + |
| 126 | + const response = await patch({ name: 'renamed', unique: true }) |
| 127 | + |
| 128 | + expect(response.status).toBe(400) |
| 129 | + expect(mockRenameColumn).not.toHaveBeenCalled() |
| 130 | + }) |
| 131 | + |
| 132 | + it('still performs a valid combined rename + currency change', async () => { |
| 133 | + mockCheckAccess.mockResolvedValue({ |
| 134 | + ok: true, |
| 135 | + table: { |
| 136 | + workspaceId: WORKSPACE_ID, |
| 137 | + schema: { columns: [{ id: 'col_a', name: 'amount', type: 'currency' }] }, |
| 138 | + }, |
| 139 | + }) |
| 140 | + mockUpdateColumnCurrency.mockResolvedValue({ schema: { columns: [] } }) |
| 141 | + |
| 142 | + const response = await patch({ name: 'renamed', currencyCode: 'eur' }) |
| 143 | + |
| 144 | + expect(response.status).toBe(200) |
| 145 | + expect(mockRenameColumn).toHaveBeenCalledTimes(1) |
| 146 | + expect(mockUpdateColumnCurrency).toHaveBeenCalledWith( |
| 147 | + // The contract upper-cases on the way in, and the rename means the |
| 148 | + // currency write must target the NEW name. |
| 149 | + expect.objectContaining({ columnName: 'renamed', currencyCode: 'EUR' }), |
| 150 | + expect.any(String) |
| 151 | + ) |
| 152 | + }) |
| 153 | +}) |
0 commit comments