|
| 1 | +/** |
| 2 | + * The v2 knowledge document download route against real PostgreSQL and local object storage, |
| 3 | + * through its route handler with a real workspace API key. A download parses nothing, so it serves |
| 4 | + * any stored original up to the stored-document limit, including one past the text-extraction cap |
| 5 | + * that a parsing read refuses. |
| 6 | + */ |
| 7 | +import { mkdtempSync } from 'node:fs' |
| 8 | +import { rm } from 'node:fs/promises' |
| 9 | +import { tmpdir } from 'node:os' |
| 10 | +import path from 'node:path' |
| 11 | +import { db } from '@sim/db' |
| 12 | +import { apiKey, document, knowledgeBase, organization, user, workspace } from '@sim/db/schema' |
| 13 | +import { deleteWorkspaceFixture } from '@sim/db/testing/workspace-fixtures' |
| 14 | +import { generateId } from '@sim/utils/id' |
| 15 | +import { eq, inArray } from 'drizzle-orm' |
| 16 | +import { NextRequest } from 'next/server' |
| 17 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' |
| 18 | + |
| 19 | +const fixtureStorage = vi.hoisted(() => ({ root: '' })) |
| 20 | +vi.mock('@/lib/uploads/core/setup.server', () => ({ |
| 21 | + get UPLOAD_DIR_SERVER() { |
| 22 | + return fixtureStorage.root |
| 23 | + }, |
| 24 | +})) |
| 25 | + |
| 26 | +import { hashApiKey } from '@/lib/api-key/crypto' |
| 27 | +import { |
| 28 | + createKnowledgeAclFixtureIds, |
| 29 | + seedKnowledgeAclFixture, |
| 30 | +} from '@/lib/knowledge/__integration__/seed-source-access-fixture' |
| 31 | +import { uploadFile } from '@/lib/uploads/core/storage-service' |
| 32 | +import { MAX_KNOWLEDGE_DOCUMENT_FILE_SIZE } from '@/lib/uploads/shared/types' |
| 33 | +import { MAX_TEXT_EXTRACTION_BYTES } from '@/lib/uploads/utils/file' |
| 34 | +import { GET } from '@/app/api/v2/knowledge/[knowledgeBaseId]/documents/[documentId]/content/route' |
| 35 | + |
| 36 | +const ids = createKnowledgeAclFixtureIds() |
| 37 | +const key = `sk-sim-fixture-${generateId()}` |
| 38 | + |
| 39 | +async function seedStoredDocument(bytes: Buffer, fileSize = bytes.byteLength) { |
| 40 | + const id = generateId() |
| 41 | + const storageKey = `kb/${id}.bin` |
| 42 | + await uploadFile({ |
| 43 | + file: bytes, |
| 44 | + fileName: 'original.bin', |
| 45 | + contentType: 'application/octet-stream', |
| 46 | + context: 'knowledge-base', |
| 47 | + customKey: storageKey, |
| 48 | + preserveKey: true, |
| 49 | + persistMetadata: false, |
| 50 | + }) |
| 51 | + await db.insert(document).values({ |
| 52 | + id, |
| 53 | + knowledgeBaseId: ids.knowledgeBaseId, |
| 54 | + filename: 'original.bin', |
| 55 | + fileUrl: '', |
| 56 | + storageKey, |
| 57 | + fileSize, |
| 58 | + mimeType: 'application/octet-stream', |
| 59 | + processingStatus: 'completed', |
| 60 | + acl: ['ws'], |
| 61 | + aclRequirements: [], |
| 62 | + aclVerifiedAt: new Date(), |
| 63 | + }) |
| 64 | + return id |
| 65 | +} |
| 66 | + |
| 67 | +function download(documentId: string) { |
| 68 | + const request = new NextRequest( |
| 69 | + `http://localhost:3000/api/v2/knowledge/${ids.knowledgeBaseId}/documents/${documentId}/content?workspaceId=${ids.workspaceId}`, |
| 70 | + { headers: { 'x-api-key': key, 'x-forwarded-for': '127.0.0.1' } } |
| 71 | + ) |
| 72 | + return GET(request, { |
| 73 | + params: Promise.resolve({ knowledgeBaseId: ids.knowledgeBaseId, documentId }), |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +beforeAll(async () => { |
| 78 | + fixtureStorage.root = mkdtempSync(path.join(tmpdir(), 'sim-knowledge-download-')) |
| 79 | + await seedKnowledgeAclFixture(ids) |
| 80 | + await db.insert(apiKey).values({ |
| 81 | + id: generateId(), |
| 82 | + userId: ids.aliceId, |
| 83 | + workspaceId: ids.workspaceId, |
| 84 | + createdBy: ids.aliceId, |
| 85 | + name: 'Download fixture', |
| 86 | + key, |
| 87 | + keyHash: hashApiKey(key), |
| 88 | + type: 'workspace', |
| 89 | + access: 'read', |
| 90 | + }) |
| 91 | +}) |
| 92 | + |
| 93 | +afterAll(async () => { |
| 94 | + await db.delete(knowledgeBase).where(eq(knowledgeBase.id, ids.knowledgeBaseId)) |
| 95 | + await deleteWorkspaceFixture(db, eq(workspace.id, ids.workspaceId)) |
| 96 | + await db.delete(organization).where(eq(organization.id, ids.organizationId)) |
| 97 | + await db.delete(user).where(inArray(user.id, [ids.aliceId, ids.bobId])) |
| 98 | + await rm(fixtureStorage.root, { recursive: true, force: true }) |
| 99 | + await db.$client.end() |
| 100 | +}) |
| 101 | + |
| 102 | +describe('GET /api/v2/knowledge/[knowledgeBaseId]/documents/[documentId]/content', () => { |
| 103 | + it('downloads an original larger than the text-extraction cap', async () => { |
| 104 | + const bytes = Buffer.alloc(MAX_TEXT_EXTRACTION_BYTES + 1024 * 1024, 'k') |
| 105 | + const documentId = await seedStoredDocument(bytes) |
| 106 | + |
| 107 | + const response = await download(documentId) |
| 108 | + |
| 109 | + expect(response.status).toBe(200) |
| 110 | + expect(response.headers.get('content-length')).toBe(String(bytes.byteLength)) |
| 111 | + expect(Buffer.from(await response.arrayBuffer()).equals(bytes)).toBe(true) |
| 112 | + }) |
| 113 | + |
| 114 | + it('refuses a document recorded past the stored-document limit', async () => { |
| 115 | + const documentId = await seedStoredDocument( |
| 116 | + Buffer.from('small'), |
| 117 | + MAX_KNOWLEDGE_DOCUMENT_FILE_SIZE + 1 |
| 118 | + ) |
| 119 | + |
| 120 | + const response = await download(documentId) |
| 121 | + |
| 122 | + expect(response.status).toBe(413) |
| 123 | + }) |
| 124 | +}) |
0 commit comments