import { describe, expect, it, vi } from 'vitest'; import { DocumentCaptureService } from '@/src/services/document-capture-service'; import { DocumentExtractionService } from '@/src/services/document-extraction-service'; describe('document extraction scope', () => { it.each([ { tenant_id: 'tenant-b', project_id: 'project-a' }, { tenant_id: 'tenant-a', project_id: 'project-b' }, ])('rejects an out-of-scope document before any byte read or external call', async (scope) => { const capture = new DocumentCaptureService(); vi.spyOn(capture, 'getDocument').mockResolvedValue({ tenant_id: 'tenant-a', project_id: 'project-a' } as never); const bytes = vi.spyOn(capture, 'getDocumentBytes'); const fetchMock = vi.fn(); vi.stubGlobal('fetch', fetchMock); try { const service = new DocumentExtractionService(capture, { documentIntelligence: { endpoint: 'https://di.example.test', apiKey: 'test-key' }, }); await expect(service.extractDocument('doc', { ...scope, actor_id: 'actor-a', correlation_id: 'corr', model_id: 'test' })) .rejects.toThrow('Document doc not found'); expect(bytes).not.toHaveBeenCalled(); expect(fetchMock).not.toHaveBeenCalled(); } finally { vi.unstubAllGlobals(); vi.restoreAllMocks(); } }); });