|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * Public v2 column update: the guards that keep a schema change from |
| 5 | + * half-applying, and the options/multiple support the shared contract already |
| 6 | + * accepts. |
| 7 | + */ |
| 8 | +import { NextRequest } from 'next/server' |
| 9 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 10 | + |
| 11 | +const { |
| 12 | + mockCheckRateLimit, |
| 13 | + mockResolveWorkspaceScope, |
| 14 | + mockCheckAccess, |
| 15 | + mockRenameColumn, |
| 16 | + mockUpdateColumnType, |
| 17 | + mockUpdateColumnOptions, |
| 18 | + mockUpdateColumnConstraints, |
| 19 | +} = vi.hoisted(() => ({ |
| 20 | + mockCheckRateLimit: vi.fn(), |
| 21 | + mockResolveWorkspaceScope: vi.fn(), |
| 22 | + mockCheckAccess: vi.fn(), |
| 23 | + mockRenameColumn: vi.fn(), |
| 24 | + mockUpdateColumnType: vi.fn(), |
| 25 | + mockUpdateColumnOptions: vi.fn(), |
| 26 | + mockUpdateColumnConstraints: vi.fn(), |
| 27 | +})) |
| 28 | + |
| 29 | +vi.mock('@/app/api/v1/middleware', () => ({ |
| 30 | + checkRateLimit: mockCheckRateLimit, |
| 31 | + resolveWorkspaceScope: mockResolveWorkspaceScope, |
| 32 | +})) |
| 33 | + |
| 34 | +vi.mock('@/app/api/table/utils', () => ({ |
| 35 | + checkAccess: mockCheckAccess, |
| 36 | + normalizeColumn: (col: Record<string, unknown>) => col, |
| 37 | + rootErrorMessage: (error: unknown) => String(error), |
| 38 | + rowWriteErrorResponse: () => null, |
| 39 | +})) |
| 40 | + |
| 41 | +vi.mock('@/lib/table', async () => { |
| 42 | + const actual = await import('@/lib/table/column-keys') |
| 43 | + return { |
| 44 | + ...actual, |
| 45 | + addTableColumn: vi.fn(), |
| 46 | + deleteColumn: vi.fn(), |
| 47 | + renameColumn: mockRenameColumn, |
| 48 | + updateColumnConstraints: mockUpdateColumnConstraints, |
| 49 | + updateColumnOptions: mockUpdateColumnOptions, |
| 50 | + updateColumnType: mockUpdateColumnType, |
| 51 | + } |
| 52 | +}) |
| 53 | + |
| 54 | +vi.mock('@/app/api/v2/lib/gate', () => ({ |
| 55 | + v2ApiGateError: vi.fn().mockResolvedValue(null), |
| 56 | +})) |
| 57 | + |
| 58 | +import { PATCH } from '@/app/api/v2/tables/[tableId]/columns/route' |
| 59 | + |
| 60 | +const SELECT_COLUMN = { |
| 61 | + id: 'col-1', |
| 62 | + name: 'Status', |
| 63 | + type: 'select', |
| 64 | + options: [{ id: 'o1', name: 'Open' }], |
| 65 | +} |
| 66 | +const TEXT_COLUMN = { id: 'col-2', name: 'Priority', type: 'text' } |
| 67 | +const TABLE = { |
| 68 | + id: 'table-1', |
| 69 | + name: 'Tasks', |
| 70 | + workspaceId: 'ws-1', |
| 71 | + schema: { columns: [SELECT_COLUMN, TEXT_COLUMN] }, |
| 72 | +} |
| 73 | +const UPDATED = { schema: { columns: [SELECT_COLUMN] } } |
| 74 | + |
| 75 | +function patch(updates: Record<string, unknown>, columnName = 'Status') { |
| 76 | + const req = new NextRequest('http://localhost:3000/api/v2/tables/table-1/columns', { |
| 77 | + method: 'PATCH', |
| 78 | + headers: { 'content-type': 'application/json' }, |
| 79 | + body: JSON.stringify({ workspaceId: 'ws-1', columnName, updates }), |
| 80 | + }) |
| 81 | + return PATCH(req, { params: Promise.resolve({ tableId: 'table-1' }) }) |
| 82 | +} |
| 83 | + |
| 84 | +describe('PATCH /api/v2/tables/[tableId]/columns', () => { |
| 85 | + beforeEach(() => { |
| 86 | + vi.clearAllMocks() |
| 87 | + mockCheckRateLimit.mockResolvedValue({ |
| 88 | + allowed: true, |
| 89 | + userId: 'user-1', |
| 90 | + keyType: 'workspace', |
| 91 | + workspaceId: 'ws-1', |
| 92 | + limit: 100, |
| 93 | + remaining: 99, |
| 94 | + resetAt: new Date('2026-01-01T01:00:00Z'), |
| 95 | + }) |
| 96 | + mockResolveWorkspaceScope.mockResolvedValue(null) |
| 97 | + mockCheckAccess.mockResolvedValue({ ok: true, table: TABLE }) |
| 98 | + mockUpdateColumnOptions.mockResolvedValue(UPDATED) |
| 99 | + mockUpdateColumnType.mockResolvedValue(UPDATED) |
| 100 | + mockUpdateColumnConstraints.mockResolvedValue(UPDATED) |
| 101 | + }) |
| 102 | + |
| 103 | + it('rejects making a select column unique before writing anything', async () => { |
| 104 | + const res = await patch({ unique: true }) |
| 105 | + |
| 106 | + expect(res.status).toBe(400) |
| 107 | + expect((await res.json()).error.code).toBe('BAD_REQUEST') |
| 108 | + // Each write is its own transaction, so an un-gated constraint write would |
| 109 | + // commit the earlier writes and then throw, half-applying the change. |
| 110 | + expect(mockUpdateColumnConstraints).not.toHaveBeenCalled() |
| 111 | + }) |
| 112 | + |
| 113 | + it('routes an unchanged type with options to the options update, not the type update', async () => { |
| 114 | + await patch({ type: 'select', options: [{ id: 'o1', name: 'Open' }] }) |
| 115 | + |
| 116 | + // updateColumnType early-returns on an unchanged type and would drop the options. |
| 117 | + expect(mockUpdateColumnType).not.toHaveBeenCalled() |
| 118 | + expect(mockUpdateColumnOptions).toHaveBeenCalledWith( |
| 119 | + expect.objectContaining({ options: [{ id: 'o1', name: 'Open' }] }), |
| 120 | + expect.any(String) |
| 121 | + ) |
| 122 | + }) |
| 123 | + |
| 124 | + it('applies options on a real type change instead of silently dropping them', async () => { |
| 125 | + await patch( |
| 126 | + { type: 'select', options: [{ id: 'o2', name: 'Done' }], required: true }, |
| 127 | + 'Priority' |
| 128 | + ) |
| 129 | + |
| 130 | + expect(mockUpdateColumnOptions).not.toHaveBeenCalled() |
| 131 | + expect(mockUpdateColumnType).toHaveBeenCalledWith( |
| 132 | + expect.objectContaining({ |
| 133 | + newType: 'select', |
| 134 | + options: [{ id: 'o2', name: 'Done' }], |
| 135 | + required: true, |
| 136 | + }), |
| 137 | + expect.any(String) |
| 138 | + ) |
| 139 | + }) |
| 140 | + |
| 141 | + it('rejects converting a column to select and making it unique in one request', async () => { |
| 142 | + const res = await patch( |
| 143 | + { type: 'select', options: [{ id: 'o2', name: 'Done' }], unique: true }, |
| 144 | + 'Priority' |
| 145 | + ) |
| 146 | + |
| 147 | + expect(res.status).toBe(400) |
| 148 | + expect(mockUpdateColumnType).not.toHaveBeenCalled() |
| 149 | + }) |
| 150 | + |
| 151 | + it('renames without touching the type when only a name is sent', async () => { |
| 152 | + mockRenameColumn.mockResolvedValue(UPDATED) |
| 153 | + |
| 154 | + const res = await patch({ name: 'State' }) |
| 155 | + |
| 156 | + expect(res.status).toBe(200) |
| 157 | + expect(mockRenameColumn).toHaveBeenCalled() |
| 158 | + expect(mockUpdateColumnType).not.toHaveBeenCalled() |
| 159 | + }) |
| 160 | +}) |
0 commit comments