File size: 4,354 Bytes
497bb49
fbf73ff
 
 
 
 
 
497bb49
 
 
 
 
 
 
 
fbf73ff
 
 
 
 
 
497bb49
fbf73ff
 
497bb49
fbf73ff
 
 
 
 
 
 
 
497bb49
fbf73ff
497bb49
fbf73ff
497bb49
 
fbf73ff
 
497bb49
fbf73ff
 
497bb49
 
 
 
 
 
 
 
 
 
fbf73ff
497bb49
 
 
 
 
 
fbf73ff
 
 
 
 
 
 
497bb49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbf73ff
 
 
497bb49
 
fbf73ff
 
497bb49
 
 
 
 
fbf73ff
 
 
 
497bb49
fbf73ff
 
 
497bb49
 
 
 
 
 
fbf73ff
 
 
 
 
497bb49
 
 
fbf73ff
 
 
497bb49
fbf73ff
 
497bb49
fbf73ff
 
497bb49
 
fbf73ff
 
497bb49
 
 
 
 
fbf73ff
497bb49
fbf73ff
 
497bb49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbf73ff
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { renderHook, waitFor, act } from '@testing-library/react'
import { server } from '../../mocks/server'
import { errorHandlers } from '../../mocks/handlers'
import { useSegmentation } from '../useSegmentation'

describe('useSegmentation', () => {
  beforeEach(() => {
    vi.useFakeTimers({ shouldAdvanceTime: true })
  })

  afterEach(() => {
    vi.useRealTimers()
  })

  it('starts with null result and not loading', () => {
    const { result } = renderHook(() => useSegmentation())

    expect(result.current.result).toBeNull()
    expect(result.current.isLoading).toBe(false)
    expect(result.current.error).toBeNull()
    expect(result.current.jobStatus).toBeNull()
  })

  it('sets loading state and job status during segmentation', async () => {
    const { result } = renderHook(() => useSegmentation())

    act(() => {
      result.current.runSegmentation('sub-stroke0001')
    })

    expect(result.current.isLoading).toBe(true)

    // Wait for job to be created
    await waitFor(() => {
      expect(result.current.jobId).toBeDefined()
    })

    expect(result.current.jobStatus).toBeDefined()
  })

  it('returns result on job completion', async () => {
    const { result } = renderHook(() => useSegmentation())

    act(() => {
      result.current.runSegmentation('sub-stroke0001')
    })

    // Wait for job creation
    await waitFor(() => {
      expect(result.current.jobId).toBeDefined()
    })

    // Advance time to allow job to complete (mock jobs complete in ~3s)
    await act(async () => {
      await vi.advanceTimersByTimeAsync(5000)
    })

    await waitFor(() => {
      expect(result.current.isLoading).toBe(false)
      expect(result.current.result).not.toBeNull()
    })

    expect(result.current.result?.metrics.caseId).toBe('sub-stroke0001')
    expect(result.current.result?.metrics.diceScore).toBe(0.847)
    expect(result.current.result?.dwiUrl).toContain('dwi.nii.gz')
  })

  it('shows progress updates during job execution', async () => {
    const { result } = renderHook(() => useSegmentation())

    act(() => {
      result.current.runSegmentation('sub-stroke0001')
    })

    // Wait for job to start
    await waitFor(() => {
      expect(result.current.jobId).toBeDefined()
    })

    // Progress should be tracked
    expect(result.current.progress).toBeGreaterThanOrEqual(0)
    expect(result.current.progressMessage).toBeDefined()
  })

  it('sets error on job creation failure', async () => {
    server.use(errorHandlers.segmentCreateError)

    const { result } = renderHook(() => useSegmentation())

    act(() => {
      result.current.runSegmentation('sub-stroke0001')
    })

    await waitFor(() => {
      expect(result.current.isLoading).toBe(false)
    })

    expect(result.current.error).toMatch(/failed to create job/i)
    expect(result.current.result).toBeNull()
  })

  it('clears previous error on new request', async () => {
    server.use(errorHandlers.segmentCreateError)
    const { result } = renderHook(() => useSegmentation())

    // First request fails
    act(() => {
      result.current.runSegmentation('sub-stroke0001')
    })

    await waitFor(() => {
      expect(result.current.error).not.toBeNull()
    })

    // Reset to success handler
    server.resetHandlers()

    // Second request should clear error
    act(() => {
      result.current.runSegmentation('sub-stroke0001')
    })

    expect(result.current.error).toBeNull()
    expect(result.current.isLoading).toBe(true)
  })

  it('can cancel a running job', async () => {
    const { result } = renderHook(() => useSegmentation())

    act(() => {
      result.current.runSegmentation('sub-stroke0001')
    })

    await waitFor(() => {
      expect(result.current.isLoading).toBe(true)
    })

    // Cancel the job
    act(() => {
      result.current.cancelJob()
    })

    expect(result.current.isLoading).toBe(false)
    expect(result.current.jobStatus).toBeNull()
  })

  it('cleans up polling on unmount', async () => {
    const { result, unmount } = renderHook(() => useSegmentation())

    act(() => {
      result.current.runSegmentation('sub-stroke0001')
    })

    await waitFor(() => {
      expect(result.current.isLoading).toBe(true)
    })

    // Unmount should not throw
    unmount()
  })
})