|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { Readable } from 'node:stream' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { mockCreateReadStream, mockReadFile, mockStat } = vi.hoisted(() => ({ |
| 8 | + mockCreateReadStream: vi.fn(), |
| 9 | + mockReadFile: vi.fn(), |
| 10 | + mockStat: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('fs', () => ({ createReadStream: mockCreateReadStream })) |
| 14 | +vi.mock('fs/promises', () => ({ readFile: mockReadFile, stat: mockStat })) |
| 15 | + |
| 16 | +vi.mock('@/lib/uploads/config', () => ({ |
| 17 | + USE_S3_STORAGE: false, |
| 18 | + USE_BLOB_STORAGE: false, |
| 19 | + USE_GCS_STORAGE: false, |
| 20 | + getStorageConfig: () => ({ bucket: 'b', region: 'r' }), |
| 21 | +})) |
| 22 | + |
| 23 | +vi.mock('@/lib/uploads/core/setup.server', () => ({ UPLOAD_DIR_SERVER: '/uploads' })) |
| 24 | + |
| 25 | +vi.mock('@/lib/uploads/server/metadata', () => ({ insertFileMetadata: vi.fn() })) |
| 26 | + |
| 27 | +import { isPayloadSizeLimitError } from '@/lib/core/utils/stream-limits' |
| 28 | +import { downloadFile } from '@/lib/uploads/core/storage-service' |
| 29 | + |
| 30 | +/** A stream that delivers `bytes`, whatever a prior `stat` would have claimed. */ |
| 31 | +function streamOf(bytes: number) { |
| 32 | + const stream = Readable.from([Buffer.alloc(bytes)]) as Readable & { destroy: () => void } |
| 33 | + vi.spyOn(stream, 'destroy') |
| 34 | + return stream |
| 35 | +} |
| 36 | + |
| 37 | +describe('downloadFile on local storage', () => { |
| 38 | + beforeEach(() => { |
| 39 | + vi.clearAllMocks() |
| 40 | + mockReadFile.mockResolvedValue(Buffer.alloc(10)) |
| 41 | + }) |
| 42 | + |
| 43 | + it('reads without a ceiling when the caller asks for none', async () => { |
| 44 | + const buffer = await downloadFile({ key: 'workspace/ws/file.bin', context: 'workspace' }) |
| 45 | + |
| 46 | + expect(buffer.length).toBe(10) |
| 47 | + expect(mockReadFile).toHaveBeenCalled() |
| 48 | + expect(mockCreateReadStream).not.toHaveBeenCalled() |
| 49 | + }) |
| 50 | + |
| 51 | + it('enforces the ceiling on the bytes as they arrive, not on a prior stat', async () => { |
| 52 | + // The file grew (or was replaced) after any size a caller could have measured: |
| 53 | + // the stream delivers more than the ceiling allows, and a stat-then-read |
| 54 | + // implementation would have admitted it. |
| 55 | + mockCreateReadStream.mockReturnValue(streamOf(500)) |
| 56 | + |
| 57 | + await expect( |
| 58 | + downloadFile({ key: 'workspace/ws/file.bin', context: 'workspace', maxBytes: 100 }) |
| 59 | + ).rejects.toSatisfy(isPayloadSizeLimitError) |
| 60 | + |
| 61 | + expect(mockStat).not.toHaveBeenCalled() |
| 62 | + expect(mockReadFile).not.toHaveBeenCalled() |
| 63 | + }) |
| 64 | + |
| 65 | + it('returns the bytes when they fit the ceiling', async () => { |
| 66 | + mockCreateReadStream.mockReturnValue(streamOf(50)) |
| 67 | + |
| 68 | + const buffer = await downloadFile({ |
| 69 | + key: 'workspace/ws/file.bin', |
| 70 | + context: 'workspace', |
| 71 | + maxBytes: 100, |
| 72 | + }) |
| 73 | + |
| 74 | + expect(buffer.length).toBe(50) |
| 75 | + }) |
| 76 | + |
| 77 | + it('destroys the stream once the read settles', async () => { |
| 78 | + const stream = streamOf(50) |
| 79 | + mockCreateReadStream.mockReturnValue(stream) |
| 80 | + |
| 81 | + await downloadFile({ key: 'workspace/ws/file.bin', context: 'workspace', maxBytes: 100 }) |
| 82 | + |
| 83 | + expect(stream.destroy).toHaveBeenCalled() |
| 84 | + }) |
| 85 | +}) |
0 commit comments