|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockGetFileMetadataByKey, mockSend, mockHeadObjectCommand } = vi.hoisted(() => ({ |
| 7 | + mockGetFileMetadataByKey: vi.fn(), |
| 8 | + mockSend: vi.fn(), |
| 9 | + mockHeadObjectCommand: vi.fn().mockImplementation(class {}), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@aws-sdk/client-s3', () => ({ |
| 13 | + HeadObjectCommand: mockHeadObjectCommand, |
| 14 | +})) |
| 15 | + |
| 16 | +vi.mock('@/lib/uploads/config', () => ({ |
| 17 | + USE_S3_STORAGE: true, |
| 18 | + USE_BLOB_STORAGE: false, |
| 19 | + USE_GCS_STORAGE: false, |
| 20 | + S3_CONFIG: { bucket: 'bucket', region: 'region' }, |
| 21 | +})) |
| 22 | + |
| 23 | +vi.mock('@/lib/uploads/providers/s3/client', () => ({ |
| 24 | + getS3Client: () => ({ send: mockSend }), |
| 25 | +})) |
| 26 | + |
| 27 | +vi.mock('@/lib/uploads/server/metadata', () => ({ |
| 28 | + getFileMetadataByKey: mockGetFileMetadataByKey, |
| 29 | +})) |
| 30 | + |
| 31 | +import { getFileMetadata } from '@/lib/uploads/core/storage-client' |
| 32 | + |
| 33 | +/** The exact error the AWS SDK raises from HeadObject for an absent object. */ |
| 34 | +const notFound = Object.assign(new Error('NotFound'), { |
| 35 | + name: 'NotFound', |
| 36 | + $fault: 'client', |
| 37 | + $metadata: { httpStatusCode: 404 }, |
| 38 | +}) |
| 39 | + |
| 40 | +describe('getFileMetadata', () => { |
| 41 | + beforeEach(() => { |
| 42 | + vi.clearAllMocks() |
| 43 | + mockGetFileMetadataByKey.mockResolvedValue(null) |
| 44 | + }) |
| 45 | + |
| 46 | + it('reports an absent object as no metadata rather than throwing', async () => { |
| 47 | + mockSend.mockRejectedValue(notFound) |
| 48 | + |
| 49 | + await expect(getFileMetadata('workspace/ws/superseded-key.md')).resolves.toEqual({}) |
| 50 | + }) |
| 51 | + |
| 52 | + it('still propagates a genuine storage failure', async () => { |
| 53 | + const denied = Object.assign(new Error('AccessDenied'), { |
| 54 | + name: 'AccessDenied', |
| 55 | + $metadata: { httpStatusCode: 403 }, |
| 56 | + }) |
| 57 | + mockSend.mockRejectedValue(denied) |
| 58 | + |
| 59 | + await expect(getFileMetadata('workspace/ws/key.md')).rejects.toThrow('AccessDenied') |
| 60 | + }) |
| 61 | + |
| 62 | + it('returns provider metadata when the object exists', async () => { |
| 63 | + mockSend.mockResolvedValue({ Metadata: { workspaceid: 'ws-1' } }) |
| 64 | + |
| 65 | + await expect(getFileMetadata('workspace/ws/key.md')).resolves.toEqual({ workspaceid: 'ws-1' }) |
| 66 | + }) |
| 67 | + |
| 68 | + it('prefers the database record when one exists', async () => { |
| 69 | + mockGetFileMetadataByKey.mockResolvedValue({ |
| 70 | + userId: 'user-1', |
| 71 | + workspaceId: 'ws-1', |
| 72 | + originalName: 'doc.md', |
| 73 | + uploadedAt: new Date('2026-01-01T00:00:00Z'), |
| 74 | + context: 'workspace', |
| 75 | + }) |
| 76 | + |
| 77 | + const metadata = await getFileMetadata('workspace/ws/key.md') |
| 78 | + |
| 79 | + expect(metadata.workspaceId).toBe('ws-1') |
| 80 | + expect(mockSend).not.toHaveBeenCalled() |
| 81 | + }) |
| 82 | +}) |
0 commit comments