|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockDownloadFile, mockGetFileMetadataById, mockRenderSimPageDocument } = vi.hoisted(() => ({ |
| 7 | + mockDownloadFile: vi.fn(), |
| 8 | + mockGetFileMetadataById: vi.fn(), |
| 9 | + mockRenderSimPageDocument: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@/lib/uploads/core/storage-service', () => ({ |
| 13 | + downloadFile: mockDownloadFile, |
| 14 | +})) |
| 15 | + |
| 16 | +vi.mock('@/lib/uploads/server/metadata', () => ({ |
| 17 | + getFileMetadataById: mockGetFileMetadataById, |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock('@/lib/workspace-files/page-document', () => ({ |
| 21 | + renderSimPageDocument: mockRenderSimPageDocument, |
| 22 | +})) |
| 23 | + |
| 24 | +import { renderSimPageDocumentWithAssets } from '@/lib/workspace-files/page-document.server' |
| 25 | + |
| 26 | +const WORKSPACE_ID = 'ws-1' |
| 27 | +const MB = 1024 * 1024 |
| 28 | + |
| 29 | +function imageRecord(id: string, size: number) { |
| 30 | + return { |
| 31 | + id, |
| 32 | + key: `workspace/${WORKSPACE_ID}/${id}.png`, |
| 33 | + context: 'workspace', |
| 34 | + workspaceId: WORKSPACE_ID, |
| 35 | + contentType: 'image/png', |
| 36 | + size, |
| 37 | + sizeBytes: size, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function documentReferencing(ids: string[]) { |
| 42 | + return ids.map((id) => `<img src="/api/files/view/${id}">`).join('') |
| 43 | +} |
| 44 | + |
| 45 | +describe('renderSimPageDocumentWithAssets memory bounds', () => { |
| 46 | + beforeEach(() => { |
| 47 | + vi.clearAllMocks() |
| 48 | + mockDownloadFile.mockImplementation(async ({ maxBytes }) => Buffer.alloc(maxBytes ?? 4 * MB)) |
| 49 | + }) |
| 50 | + |
| 51 | + it('stops inlining once the per-document budget is spent, without fetching the rest', async () => { |
| 52 | + // Five 8MB images against a 32MB document budget: four fit, the fifth must not |
| 53 | + // even be downloaded — discovering its size after the fact is the bug. |
| 54 | + const ids = ['a', 'b', 'c', 'd', 'e'] |
| 55 | + mockRenderSimPageDocument.mockReturnValue(documentReferencing(ids)) |
| 56 | + mockGetFileMetadataById.mockImplementation(async (id: string) => imageRecord(id, 8 * MB)) |
| 57 | + mockDownloadFile.mockImplementation(async () => Buffer.alloc(8 * MB)) |
| 58 | + |
| 59 | + const html = await renderSimPageDocumentWithAssets('source', { workspaceId: WORKSPACE_ID }) |
| 60 | + |
| 61 | + expect(mockDownloadFile).toHaveBeenCalledTimes(4) |
| 62 | + // The image that did not fit keeps its URL reference rather than failing the render. |
| 63 | + expect(html).toContain('src="/api/files/view/e"') |
| 64 | + }) |
| 65 | + |
| 66 | + it('never fetches an image whose recorded size already exceeds the per-image limit', async () => { |
| 67 | + mockRenderSimPageDocument.mockReturnValue(documentReferencing(['big'])) |
| 68 | + mockGetFileMetadataById.mockResolvedValue(imageRecord('big', 9 * MB)) |
| 69 | + |
| 70 | + const html = await renderSimPageDocumentWithAssets('source', { workspaceId: WORKSPACE_ID }) |
| 71 | + |
| 72 | + expect(mockDownloadFile).not.toHaveBeenCalled() |
| 73 | + expect(html).toContain('src="/api/files/view/big"') |
| 74 | + }) |
| 75 | + |
| 76 | + it('caps each download so a row understating its object cannot be inlined', async () => { |
| 77 | + mockRenderSimPageDocument.mockReturnValue(documentReferencing(['liar'])) |
| 78 | + mockGetFileMetadataById.mockResolvedValue(imageRecord('liar', 1024)) |
| 79 | + |
| 80 | + await renderSimPageDocumentWithAssets('source', { workspaceId: WORKSPACE_ID }) |
| 81 | + |
| 82 | + expect(mockDownloadFile).toHaveBeenCalledWith( |
| 83 | + expect.objectContaining({ maxBytes: 8 * MB, context: 'workspace' }) |
| 84 | + ) |
| 85 | + }) |
| 86 | + |
| 87 | + it('keeps the URL reference when a capped download rejects', async () => { |
| 88 | + mockRenderSimPageDocument.mockReturnValue(documentReferencing(['liar'])) |
| 89 | + mockGetFileMetadataById.mockResolvedValue(imageRecord('liar', 1024)) |
| 90 | + mockDownloadFile.mockRejectedValue(new Error('storage download exceeds maximum size')) |
| 91 | + |
| 92 | + const html = await renderSimPageDocumentWithAssets('source', { workspaceId: WORKSPACE_ID }) |
| 93 | + |
| 94 | + expect(html).toContain('src="/api/files/view/liar"') |
| 95 | + }) |
| 96 | + |
| 97 | + it('inlines images that fit and leaves cross-workspace references alone', async () => { |
| 98 | + mockRenderSimPageDocument.mockReturnValue(documentReferencing(['mine', 'theirs'])) |
| 99 | + mockGetFileMetadataById.mockImplementation(async (id: string) => |
| 100 | + id === 'mine' |
| 101 | + ? imageRecord('mine', 1024) |
| 102 | + : { ...imageRecord('theirs', 1024), workspaceId: 'ws-2' } |
| 103 | + ) |
| 104 | + mockDownloadFile.mockResolvedValue(Buffer.from('png-bytes')) |
| 105 | + |
| 106 | + const html = await renderSimPageDocumentWithAssets('source', { workspaceId: WORKSPACE_ID }) |
| 107 | + |
| 108 | + expect(mockDownloadFile).toHaveBeenCalledTimes(1) |
| 109 | + expect(html).toContain(`data:image/png;base64,${Buffer.from('png-bytes').toString('base64')}`) |
| 110 | + expect(html).toContain('src="/api/files/view/theirs"') |
| 111 | + }) |
| 112 | +}) |
0 commit comments