@@ -2,44 +2,29 @@ import { act, renderHook, waitFor } from "@testing-library/react";
2
2
import { API } from "api/api" ;
3
3
import * as APIModule from "api/api" ;
4
4
import { agentLogsKey } from "api/queries/workspaces" ;
5
- import type { WorkspaceAgentLog } from "api/typesGenerated" ;
5
+ import type { WorkspaceAgent , WorkspaceAgentLog } from "api/typesGenerated" ;
6
6
import WS from "jest-websocket-mock" ;
7
7
import { type QueryClient , QueryClientProvider } from "react-query" ;
8
- import { MockWorkspace , MockWorkspaceAgent } from "testHelpers/entities" ;
8
+ import { MockWorkspaceAgent } from "testHelpers/entities" ;
9
9
import { createTestQueryClient } from "testHelpers/renderHelpers" ;
10
- import { type UseAgentLogsOptions , useAgentLogs } from "./useAgentLogs" ;
10
+ import { useAgentLogs } from "./useAgentLogs" ;
11
11
12
12
afterEach ( ( ) => {
13
13
WS . clean ( ) ;
14
14
} ) ;
15
15
16
16
describe ( "useAgentLogs" , ( ) => {
17
- it ( "should not fetch logs if disabled" , async ( ) => {
18
- const queryClient = createTestQueryClient ( ) ;
19
- const fetchSpy = jest . spyOn ( API , "getWorkspaceAgentLogs" ) ;
20
- const wsSpy = jest . spyOn ( APIModule , "watchWorkspaceAgentLogs" ) ;
21
- renderUseAgentLogs ( queryClient , {
22
- workspaceId : MockWorkspace . id ,
23
- agentId : MockWorkspaceAgent . id ,
24
- agentLifeCycleState : "ready" ,
25
- enabled : false ,
26
- } ) ;
27
- expect ( fetchSpy ) . not . toHaveBeenCalled ( ) ;
28
- expect ( wsSpy ) . not . toHaveBeenCalled ( ) ;
29
- } ) ;
30
-
31
17
it ( "should return existing logs without network calls if state is off" , async ( ) => {
32
18
const queryClient = createTestQueryClient ( ) ;
33
19
queryClient . setQueryData (
34
- agentLogsKey ( MockWorkspace . id , MockWorkspaceAgent . id ) ,
20
+ agentLogsKey ( MockWorkspaceAgent . id ) ,
35
21
generateLogs ( 5 ) ,
36
22
) ;
37
23
const fetchSpy = jest . spyOn ( API , "getWorkspaceAgentLogs" ) ;
38
24
const wsSpy = jest . spyOn ( APIModule , "watchWorkspaceAgentLogs" ) ;
39
25
const { result } = renderUseAgentLogs ( queryClient , {
40
- workspaceId : MockWorkspace . id ,
41
- agentId : MockWorkspaceAgent . id ,
42
- agentLifeCycleState : "off" ,
26
+ ...MockWorkspaceAgent ,
27
+ lifecycle_state : "off" ,
43
28
} ) ;
44
29
await waitFor ( ( ) => {
45
30
expect ( result . current ) . toHaveLength ( 5 ) ;
@@ -55,9 +40,8 @@ describe("useAgentLogs", () => {
55
40
. mockResolvedValueOnce ( generateLogs ( 5 ) ) ;
56
41
jest . spyOn ( APIModule , "watchWorkspaceAgentLogs" ) ;
57
42
const { result } = renderUseAgentLogs ( queryClient , {
58
- workspaceId : MockWorkspace . id ,
59
- agentId : MockWorkspaceAgent . id ,
60
- agentLifeCycleState : "ready" ,
43
+ ...MockWorkspaceAgent ,
44
+ lifecycle_state : "ready" ,
61
45
} ) ;
62
46
await waitFor ( ( ) => {
63
47
expect ( result . current ) . toHaveLength ( 5 ) ;
@@ -77,11 +61,7 @@ describe("useAgentLogs", () => {
77
61
MockWorkspaceAgent . id
78
62
} /logs?follow&after=${ logs [ logs . length - 1 ] . id } `,
79
63
) ;
80
- const { result } = renderUseAgentLogs ( queryClient , {
81
- workspaceId : MockWorkspace . id ,
82
- agentId : MockWorkspaceAgent . id ,
83
- agentLifeCycleState : "starting" ,
84
- } ) ;
64
+ const { result } = renderUseAgentLogs ( queryClient , MockWorkspaceAgent ) ;
85
65
await waitFor ( ( ) => {
86
66
expect ( result . current ) . toHaveLength ( 5 ) ;
87
67
} ) ;
@@ -102,11 +82,7 @@ describe("useAgentLogs", () => {
102
82
MockWorkspaceAgent . id
103
83
} /logs?follow&after=${ logs [ logs . length - 1 ] . id } `,
104
84
) ;
105
- const { result } = renderUseAgentLogs ( queryClient , {
106
- workspaceId : MockWorkspace . id ,
107
- agentId : MockWorkspaceAgent . id ,
108
- agentLifeCycleState : "starting" ,
109
- } ) ;
85
+ const { result } = renderUseAgentLogs ( queryClient , MockWorkspaceAgent ) ;
110
86
await waitFor ( ( ) => {
111
87
expect ( result . current ) . toHaveLength ( 5 ) ;
112
88
} ) ;
@@ -120,11 +96,8 @@ describe("useAgentLogs", () => {
120
96
} ) ;
121
97
} ) ;
122
98
123
- function renderUseAgentLogs (
124
- queryClient : QueryClient ,
125
- options : UseAgentLogsOptions ,
126
- ) {
127
- return renderHook ( ( ) => useAgentLogs ( options ) , {
99
+ function renderUseAgentLogs ( queryClient : QueryClient , agent : WorkspaceAgent ) {
100
+ return renderHook ( ( ) => useAgentLogs ( agent ) , {
128
101
wrapper : ( { children } ) => (
129
102
< QueryClientProvider client = { queryClient } > { children } </ QueryClientProvider >
130
103
) ,
0 commit comments