4
4
*/
5
5
6
6
import { act , renderHook } from '@testing-library/react-hooks' ;
7
+ import { AllTheProviders } from '../../../../../../test/test_utils' ;
7
8
import { useModel } from '../use-model' ;
8
9
9
10
jest . useFakeTimers ( ) ;
10
11
11
12
// Mock getUseCases() to control polling behavior
12
- type ModelWithAgent = { id : string ; agentId ?: string ; inUse ?: boolean ; version ?: string ; createdAt ?: string ; status ?: string } ;
13
+ type ModelWithAgent = {
14
+ id : string ;
15
+ agentId ?: string ;
16
+ inUse ?: boolean ;
17
+ version ?: string ;
18
+ createdAt ?: string ;
19
+ status ?: string ;
20
+ } ;
13
21
const mockUseAgent = jest . fn ( ) ;
14
22
const mockGetModelsWithAgentData = jest . fn < Promise < ModelWithAgent [ ] > , [ ] > ( ) ;
15
23
@@ -25,6 +33,7 @@ jest.mock('../../../../services/ml-use-cases.service', () => {
25
33
describe ( 'useModel' , ( ) => {
26
34
beforeEach ( ( ) => {
27
35
jest . clearAllMocks ( ) ;
36
+ jest . useFakeTimers ( ) ;
28
37
} ) ;
29
38
30
39
it ( 'registers the agent, polls until inUse is true, then calls onSuccess' , async ( ) => {
@@ -40,62 +49,23 @@ describe('useModel', () => {
40
49
] as ModelWithAgent [ ] ) ;
41
50
42
51
const onSuccess = jest . fn ( ) ;
43
- const { result } = renderHook ( ( ) => useModel ( { onSuccess } ) ) ;
44
-
45
- await act ( async ( ) => {
46
- const p = result . current . activateModel ( agentId ) ;
47
- // Let first poll run and queue the delay
48
- await Promise . resolve ( ) ;
49
- // Advance the delay between polls
50
- jest . advanceTimersByTime ( 300 ) ;
51
- await p ;
52
+ const { result } = renderHook ( ( ) => useModel ( { onSuccess } ) , {
53
+ wrapper : AllTheProviders ,
52
54
} ) ;
53
55
54
- expect ( mockUseAgent ) . toHaveBeenCalledWith ( agentId ) ;
55
- expect ( mockGetModelsWithAgentData ) . toHaveBeenCalledTimes ( 2 ) ;
56
- expect ( onSuccess ) . toHaveBeenCalled ( ) ;
57
- } ) ;
58
-
59
- it ( 'eventually calls onSuccess even if inUse never becomes true within attempts' , async ( ) => {
60
- const agentId = 'agent-456' ;
61
- mockUseAgent . mockResolvedValueOnce ( undefined ) ;
62
- // Always return not active
63
- mockGetModelsWithAgentData . mockResolvedValue ( [
64
- { id : 'm2' , agentId, inUse : false } ,
65
- ] as ModelWithAgent [ ] ) ;
66
-
67
- const onSuccess = jest . fn ( ) ;
68
- const { result } = renderHook ( ( ) => useModel ( { onSuccess } ) ) ;
69
-
56
+ let p : Promise < void > ;
70
57
await act ( async ( ) => {
71
- const p = result . current . activateModel ( agentId ) ;
72
- // Run through all retry delays (10 attempts * 300ms)
73
- jest . advanceTimersByTime ( 300 * 10 ) ;
74
- await p ;
58
+ p = result . current . activateModel ( agentId ) ;
75
59
} ) ;
76
-
77
- expect ( mockUseAgent ) . toHaveBeenCalledWith ( agentId ) ;
78
- expect ( mockGetModelsWithAgentData ) . toHaveBeenCalled ( ) ;
79
- expect ( onSuccess ) . toHaveBeenCalled ( ) ;
80
- } ) ;
81
-
82
- it ( 'continues polling if a fetch error occurs and succeeds afterwards' , async ( ) => {
83
- const agentId = 'agent-789' ;
84
- mockUseAgent . mockResolvedValueOnce ( undefined ) ;
85
- // First call throws, second returns active
86
- mockGetModelsWithAgentData . mockRejectedValueOnce ( new Error ( 'network' ) ) ;
87
- mockGetModelsWithAgentData . mockResolvedValueOnce ( [
88
- { id : 'm3' , agentId, inUse : true } ,
89
- ] as ModelWithAgent [ ] ) ;
90
-
91
- const onSuccess = jest . fn ( ) ;
92
- const { result } = renderHook ( ( ) => useModel ( { onSuccess } ) ) ;
93
-
60
+ // Let promises resolve so the first delay is scheduled
94
61
await act ( async ( ) => {
95
- const p = result . current . activateModel ( agentId ) ;
96
- // Allow first failure to settle and then the delay
97
62
await Promise . resolve ( ) ;
63
+ } ) ;
64
+ // Advance the delay between polls for the second check
65
+ await act ( async ( ) => {
98
66
jest . advanceTimersByTime ( 300 ) ;
67
+ } ) ;
68
+ await act ( async ( ) => {
99
69
await p ;
100
70
} ) ;
101
71
@@ -104,4 +74,3 @@ describe('useModel', () => {
104
74
expect ( onSuccess ) . toHaveBeenCalled ( ) ;
105
75
} ) ;
106
76
} ) ;
107
-
0 commit comments