d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
124 lines
4.2 KiB
TypeScript
124 lines
4.2 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { hybridAuthMockFns, permissionsMock, permissionsMockFns } from '@sim/testing'
|
|
import { NextRequest } from 'next/server'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const {
|
|
mockCreateTable,
|
|
mockGetLimits,
|
|
mockListTables,
|
|
mockRunTableImport,
|
|
mockRunDetached,
|
|
MockTableConflictError,
|
|
} = vi.hoisted(() => ({
|
|
mockCreateTable: vi.fn(),
|
|
mockGetLimits: vi.fn(),
|
|
mockListTables: vi.fn(),
|
|
mockRunTableImport: vi.fn(),
|
|
mockRunDetached: vi.fn(),
|
|
MockTableConflictError: class extends Error {
|
|
readonly code = 'TABLE_EXISTS' as const
|
|
},
|
|
}))
|
|
|
|
vi.mock('@sim/utils/id', () => ({
|
|
generateId: vi.fn().mockReturnValue('import-id-123'),
|
|
generateShortId: vi.fn().mockReturnValue('short-id'),
|
|
}))
|
|
|
|
vi.mock('@/lib/table', () => ({
|
|
createTable: mockCreateTable,
|
|
getWorkspaceTableLimits: mockGetLimits,
|
|
listTables: mockListTables,
|
|
sanitizeName: (name: string) => name.replace(/[^a-zA-Z0-9_]/g, '_'),
|
|
TABLE_LIMITS: { MAX_TABLE_NAME_LENGTH: 128 },
|
|
TableConflictError: MockTableConflictError,
|
|
}))
|
|
vi.mock('@/lib/table/import-runner', () => ({ runTableImport: mockRunTableImport }))
|
|
vi.mock('@/lib/core/utils/background', () => ({
|
|
runDetached: mockRunDetached.mockImplementation(
|
|
(_label: string, work: () => Promise<unknown>) => {
|
|
void work()
|
|
}
|
|
),
|
|
}))
|
|
vi.mock('@/lib/workspaces/permissions/utils', () => permissionsMock)
|
|
|
|
import { POST } from '@/app/api/table/import-async/route'
|
|
|
|
function makeRequest(body: unknown): NextRequest {
|
|
return new NextRequest('http://localhost:3000/api/table/import-async', {
|
|
method: 'POST',
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify(body),
|
|
})
|
|
}
|
|
|
|
const validBody = {
|
|
workspaceId: 'workspace-1',
|
|
fileKey: 'workspace/workspace-1/123-data.csv',
|
|
fileName: 'data.csv',
|
|
}
|
|
|
|
describe('POST /api/table/import-async', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({
|
|
success: true,
|
|
userId: 'user-1',
|
|
authType: 'session',
|
|
})
|
|
permissionsMockFns.mockGetUserEntityPermissions.mockResolvedValue('write')
|
|
mockGetLimits.mockResolvedValue({ maxRowsPerTable: 1_000_000, maxTables: 50 })
|
|
mockListTables.mockResolvedValue([])
|
|
mockCreateTable.mockResolvedValue({ id: 'tbl_async', name: 'data' })
|
|
mockRunTableImport.mockResolvedValue(undefined)
|
|
})
|
|
|
|
it('creates an importing table and kicks off the background import', async () => {
|
|
const response = await POST(makeRequest(validBody))
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(data.data).toEqual({ tableId: 'tbl_async', importId: 'import-id-123' })
|
|
expect(mockCreateTable).toHaveBeenCalledWith(
|
|
expect.objectContaining({ jobStatus: 'running', jobType: 'import', jobId: 'import-id-123' }),
|
|
expect.any(String)
|
|
)
|
|
expect(mockRunTableImport).toHaveBeenCalledWith(
|
|
expect.objectContaining({ tableId: 'tbl_async', mode: 'create', delimiter: ',' })
|
|
)
|
|
})
|
|
|
|
it('uses a tab delimiter for .tsv files', async () => {
|
|
await POST(makeRequest({ ...validBody, fileName: 'data.tsv' }))
|
|
expect(mockRunTableImport).toHaveBeenCalledWith(expect.objectContaining({ delimiter: '\t' }))
|
|
})
|
|
|
|
it('returns 400 for unsupported extensions', async () => {
|
|
const response = await POST(makeRequest({ ...validBody, fileName: 'data.json' }))
|
|
expect(response.status).toBe(400)
|
|
expect(mockCreateTable).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns 401 when unauthenticated', async () => {
|
|
hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({ success: false })
|
|
const response = await POST(makeRequest(validBody))
|
|
expect(response.status).toBe(401)
|
|
})
|
|
|
|
it('returns 403 without write permission', async () => {
|
|
permissionsMockFns.mockGetUserEntityPermissions.mockResolvedValue('read')
|
|
const response = await POST(makeRequest(validBody))
|
|
expect(response.status).toBe(403)
|
|
expect(mockCreateTable).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns 400 when the body is missing required fields', async () => {
|
|
const response = await POST(makeRequest({ workspaceId: 'workspace-1' }))
|
|
expect(response.status).toBe(400)
|
|
})
|
|
})
|