|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { authMockFns } from '@sim/testing' |
| 5 | +import { NextRequest } from 'next/server' |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +const mocks = vi.hoisted(() => ({ |
| 9 | + getSlice: vi.fn(), |
| 10 | + readFile: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('@/lib/file-parsers/csv-preview-slice', () => ({ |
| 14 | + getCsvPreviewSlice: mocks.getSlice, |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock('@/lib/workspace-files/application/read-workspace-file-record', () => ({ |
| 18 | + readWorkspaceFileContentRecord: { |
| 19 | + operation: { id: 'files.read_content', minimumRole: 'read', workspaceApiKey: 'allow' }, |
| 20 | + execute: mocks.readFile, |
| 21 | + }, |
| 22 | +})) |
| 23 | + |
| 24 | +import { GET } from '@/app/api/workspaces/[id]/files/[fileId]/csv-preview/route' |
| 25 | + |
| 26 | +const WORKSPACE_ID = '7727ef3f-8cf6-4686-b063-2bb006a10785' |
| 27 | +const FILE_ID = 'wf_csv' |
| 28 | +const KEY = `workspace/${WORKSPACE_ID}/large.csv` |
| 29 | +const USER = { id: 'user-1' } |
| 30 | +const context = { params: Promise.resolve({ id: WORKSPACE_ID, fileId: FILE_ID }) } |
| 31 | + |
| 32 | +function request(signal?: AbortSignal): NextRequest { |
| 33 | + return new NextRequest( |
| 34 | + `http://localhost/api/workspaces/${WORKSPACE_ID}/files/${FILE_ID}/csv-preview?key=${encodeURIComponent(KEY)}`, |
| 35 | + { signal } |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +describe('GET /api/workspaces/[id]/files/[fileId]/csv-preview', () => { |
| 40 | + beforeEach(() => { |
| 41 | + vi.clearAllMocks() |
| 42 | + authMockFns.mockGetSession.mockResolvedValue({ user: USER, session: { id: 'session-1' } }) |
| 43 | + mocks.readFile.mockResolvedValue({ file: { id: FILE_ID, key: KEY } }) |
| 44 | + mocks.getSlice.mockResolvedValue({ |
| 45 | + headers: ['name'], |
| 46 | + rows: [['Ada']], |
| 47 | + truncated: false, |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + it('propagates client cancellation to the storage preview read', async () => { |
| 52 | + const req = request() |
| 53 | + const response = await GET(req, context) |
| 54 | + |
| 55 | + expect(response.status).toBe(200) |
| 56 | + expect(mocks.getSlice).toHaveBeenCalledWith({ |
| 57 | + key: KEY, |
| 58 | + context: 'workspace', |
| 59 | + signal: req.signal, |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + it('returns a cancellation response when the client disconnects during the storage read', async () => { |
| 64 | + const controller = new AbortController() |
| 65 | + const req = request(controller.signal) |
| 66 | + mocks.getSlice.mockImplementation(async () => { |
| 67 | + controller.abort() |
| 68 | + throw Object.assign(new Error('Premature close'), { |
| 69 | + code: 'ERR_STREAM_PREMATURE_CLOSE', |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + const response = await GET(req, context) |
| 74 | + |
| 75 | + expect(response.status).toBe(499) |
| 76 | + await expect(response.json()).resolves.toMatchObject({ |
| 77 | + error: 'Client cancelled request', |
| 78 | + requestId: expect.any(String), |
| 79 | + }) |
| 80 | + }) |
| 81 | +}) |
0 commit comments