Files
simstudioai--sim/apps/sim/lib/file-parsers/csv-preview-slice.test.ts
T
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

104 lines
3.6 KiB
TypeScript

/**
* @vitest-environment node
*/
import { Readable } from 'node:stream'
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { mockDownloadFileStream } = vi.hoisted(() => ({
mockDownloadFileStream: vi.fn(),
}))
vi.mock('@/lib/uploads/core/storage-service', () => ({
downloadFileStream: mockDownloadFileStream,
}))
import { CSV_PREVIEW_MAX_ROWS } from '@/lib/api/contracts/workspace-file-table'
import { getCsvPreviewSlice } from '@/lib/file-parsers/csv-preview-slice'
function streamOf(text: string): Readable {
// Array-wrapped so the whole text is one chunk (a bare Buffer/string is iterated element-wise).
return Readable.from([Buffer.from(text, 'utf-8')])
}
const args = { key: 'workspace/ws_1/file.csv', context: 'workspace' as const }
function csvWithRows(dataRows: number): string {
const lines = ['h1,h2']
for (let i = 0; i < dataRows; i++) lines.push(`${i},x`)
return lines.join('\n')
}
describe('getCsvPreviewSlice', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('returns headers and every row when under the cap', async () => {
mockDownloadFileStream.mockResolvedValue(streamOf('a,b\n1,2\n3,4\n'))
const slice = await getCsvPreviewSlice(args)
expect(slice.headers).toEqual(['a', 'b'])
expect(slice.rows).toEqual([
['1', '2'],
['3', '4'],
])
expect(slice.truncated).toBe(false)
})
it('caps at CSV_PREVIEW_MAX_ROWS and flags truncated', async () => {
mockDownloadFileStream.mockResolvedValue(streamOf(csvWithRows(CSV_PREVIEW_MAX_ROWS + 500)))
const slice = await getCsvPreviewSlice(args)
expect(slice.rows).toHaveLength(CSV_PREVIEW_MAX_ROWS)
expect(slice.truncated).toBe(true)
})
it('is not truncated at exactly the cap', async () => {
mockDownloadFileStream.mockResolvedValue(streamOf(csvWithRows(CSV_PREVIEW_MAX_ROWS)))
const slice = await getCsvPreviewSlice(args)
expect(slice.rows).toHaveLength(CSV_PREVIEW_MAX_ROWS)
expect(slice.truncated).toBe(false)
})
it('detects a semicolon delimiter', async () => {
mockDownloadFileStream.mockResolvedValue(streamOf('a;b;c\n1;2;3\n'))
const slice = await getCsvPreviewSlice(args)
expect(slice.headers).toEqual(['a', 'b', 'c'])
expect(slice.rows).toEqual([['1', '2', '3']])
})
it('detects a tab delimiter', async () => {
mockDownloadFileStream.mockResolvedValue(streamOf('a\tb\n1\t2\n'))
const slice = await getCsvPreviewSlice(args)
expect(slice.headers).toEqual(['a', 'b'])
expect(slice.rows).toEqual([['1', '2']])
})
it('returns empty for an empty file', async () => {
mockDownloadFileStream.mockResolvedValue(streamOf(''))
const slice = await getCsvPreviewSlice(args)
expect(slice).toEqual({ headers: [], rows: [], truncated: false })
})
it('tolerates ragged rows', async () => {
mockDownloadFileStream.mockResolvedValue(streamOf('a,b,c\n1,2\n4,5,6,7\n'))
const slice = await getCsvPreviewSlice(args)
expect(slice.headers).toEqual(['a', 'b', 'c'])
expect(slice.rows[0]).toEqual(['1', '2'])
})
it('truncates an oversized cell', async () => {
const big = 'x'.repeat(3000)
mockDownloadFileStream.mockResolvedValue(streamOf(`a\n${big}\n`))
const slice = await getCsvPreviewSlice(args)
expect(slice.rows[0][0].length).toBeLessThan(3000)
})
it('destroys the source stream after reading the slice', async () => {
const source = streamOf(csvWithRows(CSV_PREVIEW_MAX_ROWS + 50))
const destroySpy = vi.spyOn(source, 'destroy')
mockDownloadFileStream.mockResolvedValue(source)
const slice = await getCsvPreviewSlice(args)
expect(slice.truncated).toBe(true)
expect(destroySpy).toHaveBeenCalled()
})
})