|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { renderDocToGrid } = vi.hoisted(() => ({ |
| 8 | + renderDocToGrid: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +const { findWorkspaceFileRecord, listAllWorkspaceFilesExecute, readWorkspaceFileContentExecute } = |
| 12 | + vi.hoisted(() => ({ |
| 13 | + findWorkspaceFileRecord: vi.fn(), |
| 14 | + listAllWorkspaceFilesExecute: vi.fn(), |
| 15 | + readWorkspaceFileContentExecute: vi.fn(), |
| 16 | + })) |
| 17 | + |
| 18 | +vi.mock('@/lib/copilot/tools/server/files/doc-render', () => ({ |
| 19 | + // `odt` exposes the defensive missing-task branch independently from the extension guard. |
| 20 | + isRenderableDocExt: (ext: string) => ['docx', 'odt', 'pdf', 'pptx'].includes(ext.toLowerCase()), |
| 21 | + renderDocToGrid, |
| 22 | +})) |
| 23 | + |
| 24 | +vi.mock('@/lib/uploads/contexts/workspace/workspace-file-manager', () => ({ |
| 25 | + findWorkspaceFileRecord, |
| 26 | +})) |
| 27 | + |
| 28 | +vi.mock('@/lib/workspace-files/application/list-workspace-files', () => ({ |
| 29 | + listAllWorkspaceFiles: { execute: listAllWorkspaceFilesExecute }, |
| 30 | +})) |
| 31 | + |
| 32 | +vi.mock('@/lib/workspace-files/application/read-workspace-file-content', () => ({ |
| 33 | + readWorkspaceFileContent: { execute: readWorkspaceFileContentExecute }, |
| 34 | +})) |
| 35 | + |
| 36 | +import { WorkspaceVFS } from '@/lib/copilot/vfs/workspace-vfs' |
| 37 | + |
| 38 | +const MAX_DOC_READ_INPUT_BYTES = 50 * 1024 * 1024 |
| 39 | +const MAX_DOCUMENT_PREVIEW_CODE_BYTES = 1024 * 1024 |
| 40 | + |
| 41 | +function arrangeRenderRead({ |
| 42 | + name = 'brief.pdf', |
| 43 | + size = 8, |
| 44 | + content = Buffer.from('%PDF-1.7'), |
| 45 | +}: { |
| 46 | + name?: string |
| 47 | + size?: number |
| 48 | + content?: Buffer | { length: number } |
| 49 | +} = {}) { |
| 50 | + const record = { |
| 51 | + id: 'file-1', |
| 52 | + workspaceId: 'ws-1', |
| 53 | + name, |
| 54 | + key: name, |
| 55 | + path: `/api/files/serve/${name}`, |
| 56 | + size, |
| 57 | + type: 'application/octet-stream', |
| 58 | + uploadedBy: 'user-1', |
| 59 | + deletedAt: null, |
| 60 | + uploadedAt: new Date('2026-01-01T00:00:00.000Z'), |
| 61 | + updatedAt: new Date('2026-01-01T00:00:00.000Z'), |
| 62 | + storageContext: 'mothership' as const, |
| 63 | + } |
| 64 | + listAllWorkspaceFilesExecute.mockResolvedValue({ files: [record] }) |
| 65 | + findWorkspaceFileRecord.mockReturnValue(record) |
| 66 | + readWorkspaceFileContentExecute.mockResolvedValue({ content }) |
| 67 | + |
| 68 | + const vfs = new WorkspaceVFS({ kind: 'session', userId: 'user-1', sessionId: 'session-1' }) |
| 69 | + Object.assign(vfs, { _workspaceId: 'ws-1' }) |
| 70 | + return vfs |
| 71 | +} |
| 72 | + |
| 73 | +describe('WorkspaceVFS dynamic render reads', () => { |
| 74 | + beforeEach(() => { |
| 75 | + vi.clearAllMocks() |
| 76 | + }) |
| 77 | + |
| 78 | + it('marks render exceptions as file read errors', async () => { |
| 79 | + const vfs = arrangeRenderRead() |
| 80 | + renderDocToGrid.mockRejectedValue( |
| 81 | + new Error('Document compiler not configured (MOTHERSHIP_E2B_DOC_TEMPLATE_ID is unset)') |
| 82 | + ) |
| 83 | + |
| 84 | + const result = await vfs.readFileContent('files/brief.pdf/render') |
| 85 | + |
| 86 | + expect(result).toEqual({ |
| 87 | + content: |
| 88 | + '{"ok":false,"error":"Document compiler not configured (MOTHERSHIP_E2B_DOC_TEMPLATE_ID is unset)"}', |
| 89 | + totalLines: 1, |
| 90 | + error: 'Document compiler not configured (MOTHERSHIP_E2B_DOC_TEMPLATE_ID is unset)', |
| 91 | + }) |
| 92 | + }) |
| 93 | + |
| 94 | + it.each([ |
| 95 | + { |
| 96 | + label: 'unsupported extensions', |
| 97 | + name: 'brief.txt', |
| 98 | + error: 'Render supports .pptx, .docx, and .pdf only', |
| 99 | + }, |
| 100 | + { |
| 101 | + label: 'oversized file metadata', |
| 102 | + size: MAX_DOC_READ_INPUT_BYTES + 1, |
| 103 | + error: 'File is too large to render', |
| 104 | + }, |
| 105 | + { |
| 106 | + label: 'oversized fetched buffers', |
| 107 | + content: { length: MAX_DOC_READ_INPUT_BYTES + 1 }, |
| 108 | + error: 'File is too large to render', |
| 109 | + }, |
| 110 | + { |
| 111 | + label: 'oversized source', |
| 112 | + content: Buffer.alloc(MAX_DOCUMENT_PREVIEW_CODE_BYTES + 1, 'a'), |
| 113 | + error: 'File source exceeds maximum size', |
| 114 | + }, |
| 115 | + { |
| 116 | + label: 'missing render tasks', |
| 117 | + name: 'brief.odt', |
| 118 | + content: Buffer.from('document source'), |
| 119 | + error: 'Cannot render this file', |
| 120 | + }, |
| 121 | + ])('marks $label as file read errors', async ({ name, size, content, error }) => { |
| 122 | + const vfs = arrangeRenderRead({ name, size, content }) |
| 123 | + |
| 124 | + const result = await vfs.readFileContent(`files/${name ?? 'brief.pdf'}/render`) |
| 125 | + |
| 126 | + expect(result).toEqual({ |
| 127 | + content: JSON.stringify({ ok: false, error }), |
| 128 | + totalLines: 1, |
| 129 | + error, |
| 130 | + }) |
| 131 | + }) |
| 132 | +}) |
0 commit comments