File size: 2,094 Bytes
5539271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createAnalysis, fetchAnalyses, fetchAnalysis, deleteAnalysis } from './api'

vi.mock('../../shared/api/http', () => ({
  apiFetch: vi.fn(),
}))

import { apiFetch } from '../../shared/api/http'

describe('analysis API', () => {
  beforeEach(() => {
    vi.clearAllMocks()
  })

  it('createAnalysis sends POST with documentId only', async () => {
    const job = { id: '1', documentId: 'doc-1', status: 'PENDING' }
    apiFetch.mockResolvedValue(job)

    const result = await createAnalysis('doc-1')

    expect(apiFetch).toHaveBeenCalledWith('/api/analyses', {
      method: 'POST',
      body: JSON.stringify({ documentId: 'doc-1' }),
    })
    expect(result).toEqual(job)
  })

  it('createAnalysis sends POST with pipeline options', async () => {
    const job = { id: '2', documentId: 'doc-1', status: 'PENDING' }
    apiFetch.mockResolvedValue(job)

    const options = { do_ocr: false, table_mode: 'fast', do_code_enrichment: true }
    const result = await createAnalysis('doc-1', options)

    expect(apiFetch).toHaveBeenCalledWith('/api/analyses', {
      method: 'POST',
      body: JSON.stringify({ documentId: 'doc-1', pipelineOptions: options }),
    })
    expect(result).toEqual(job)
  })

  it('fetchAnalyses calls GET /api/analyses', async () => {
    const jobs = [{ id: '1', status: 'COMPLETED' }]
    apiFetch.mockResolvedValue(jobs)

    const result = await fetchAnalyses()

    expect(apiFetch).toHaveBeenCalledWith('/api/analyses')
    expect(result).toEqual(jobs)
  })

  it('fetchAnalysis calls GET /api/analyses/:id', async () => {
    const job = { id: '42', status: 'RUNNING' }
    apiFetch.mockResolvedValue(job)

    const result = await fetchAnalysis('42')

    expect(apiFetch).toHaveBeenCalledWith('/api/analyses/42')
    expect(result).toEqual(job)
  })

  it('deleteAnalysis calls DELETE /api/analyses/:id', async () => {
    apiFetch.mockResolvedValue(null)

    await deleteAnalysis('42')

    expect(apiFetch).toHaveBeenCalledWith('/api/analyses/42', { method: 'DELETE' })
  })
})