|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ |
| 8 | + resolveDocument: vi.fn(), |
| 9 | + resolvePermission: vi.fn(), |
| 10 | + queryChunks: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('@sim/platform-authz/workspace', () => ({ |
| 14 | + permissionSatisfies: (actual: string | null, required: string) => { |
| 15 | + const rank = { read: 1, write: 2, admin: 3 } as const |
| 16 | + return ( |
| 17 | + actual !== null && rank[actual as keyof typeof rank] >= rank[required as keyof typeof rank] |
| 18 | + ) |
| 19 | + }, |
| 20 | + resolveEffectiveWorkspacePermission: mocks.resolvePermission, |
| 21 | +})) |
| 22 | + |
| 23 | +vi.mock('@/lib/knowledge/application/contexts', () => ({ |
| 24 | + resolveCanonicalActiveKnowledgeDocumentContext: mocks.resolveDocument, |
| 25 | + resolveActiveKnowledgeChunkContext: vi.fn(), |
| 26 | +})) |
| 27 | + |
| 28 | +vi.mock('@/lib/knowledge/chunks/service', () => ({ |
| 29 | + batchChunkOperation: vi.fn(), |
| 30 | + createChunk: vi.fn(), |
| 31 | + deleteChunk: vi.fn(), |
| 32 | + queryChunks: mocks.queryChunks, |
| 33 | + updateChunk: vi.fn(), |
| 34 | +})) |
| 35 | + |
| 36 | +vi.mock('@/lib/execution/durable-secret-provenance', () => ({ |
| 37 | + createDurableSecretProvenanceRegistry: vi.fn(), |
| 38 | +})) |
| 39 | + |
| 40 | +vi.mock('@/lib/knowledge/model-input-provenance', () => ({ |
| 41 | + runWithKnowledgeModelInputProvenance: vi.fn(), |
| 42 | +})) |
| 43 | + |
| 44 | +vi.mock('@/providers/utils', () => ({ calculateCost: vi.fn() })) |
| 45 | + |
| 46 | +import { KnowledgeDocumentNotReadyError } from '@/lib/knowledge/application/chunk-errors' |
| 47 | +import { listKnowledgeChunks } from '@/lib/knowledge/application/chunks' |
| 48 | + |
| 49 | +describe('knowledge chunk application use cases', () => { |
| 50 | + beforeEach(() => { |
| 51 | + vi.clearAllMocks() |
| 52 | + mocks.resolvePermission.mockResolvedValue('read') |
| 53 | + mocks.resolveDocument.mockResolvedValue({ |
| 54 | + workspaceId: 'workspace-1', |
| 55 | + workspaceOrganizationId: null, |
| 56 | + allowPersonalApiKeys: true, |
| 57 | + billedAccountUserId: 'billing-owner-1', |
| 58 | + knowledgeBaseId: 'knowledge-1', |
| 59 | + knowledgeBase: { id: 'knowledge-1' }, |
| 60 | + documentId: 'document-1', |
| 61 | + document: { id: 'document-1', processingStatus: 'processing' }, |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + it('returns a typed transient failure before querying chunks for a processing document', async () => { |
| 66 | + const promise = listKnowledgeChunks.execute({ |
| 67 | + principal: { kind: 'session', userId: 'user-1', sessionId: 'session-1' }, |
| 68 | + input: { knowledgeBaseId: 'knowledge-1', documentId: 'document-1' }, |
| 69 | + }) |
| 70 | + |
| 71 | + await expect(promise).rejects.toBeInstanceOf(KnowledgeDocumentNotReadyError) |
| 72 | + await expect(promise).rejects.toMatchObject({ |
| 73 | + code: 'validation', |
| 74 | + processingStatus: 'processing', |
| 75 | + message: 'Document is not ready for access (status: processing)', |
| 76 | + }) |
| 77 | + expect(mocks.queryChunks).not.toHaveBeenCalled() |
| 78 | + }) |
| 79 | +}) |
0 commit comments