@@ -2,18 +2,18 @@ import { renderHook, act } from '@testing-library/react'
22import { describe , it , expect , beforeEach , vi } from 'vitest'
33import { useAssistantSessionLauncher } from './useAssistantSessionLauncher'
44import { OpenCodeClient } from '@/api/opencode'
5- import { initializeAssistantMode } from '@/api/repos'
5+ import { getAssistantModeStatus } from '@/api/repos'
66
77const mocks = vi . hoisted ( ( ) => ( {
88 listSessions : vi . fn ( ) ,
99 listSessionsPage : vi . fn ( ) ,
1010 createSession : vi . fn ( ) ,
1111 sendPromptAsync : vi . fn ( ) ,
12- initializeAssistantMode : vi . fn ( ) ,
12+ getAssistantModeStatus : vi . fn ( ) ,
1313} ) )
1414
1515vi . mock ( '@/api/repos' , ( ) => ( {
16- initializeAssistantMode : mocks . initializeAssistantMode ,
16+ getAssistantModeStatus : mocks . getAssistantModeStatus ,
1717} ) )
1818
1919vi . mock ( '@/api/opencode' , ( ) => ( {
@@ -33,7 +33,14 @@ describe('useAssistantSessionLauncher', () => {
3333 beforeEach ( ( ) => {
3434 vi . clearAllMocks ( )
3535 localStorage . clear ( )
36- mocks . initializeAssistantMode . mockResolvedValue ( { directory : '/assistant' } )
36+ mocks . getAssistantModeStatus . mockResolvedValue ( {
37+ directory : '/assistant' ,
38+ files : {
39+ agentsMd : { path : '/assistant/AGENTS.md' , exists : true , created : true } ,
40+ opencodeJson : { path : '/assistant/.opencode.json' , exists : true , created : true } ,
41+ } ,
42+ defaultAgent : { name : 'assistant' , path : '/assistant/.opencode/agents/assistant.md' , exists : true , created : true } ,
43+ } )
3744 } )
3845
3946 it ( 'opens the latest root session in the assistant directory' , async ( ) => {
@@ -56,7 +63,7 @@ describe('useAssistantSessionLauncher', () => {
5663 await result . current . openAssistant ( )
5764 } )
5865
59- expect ( initializeAssistantMode ) . toHaveBeenCalledWith ( 123 )
66+ expect ( getAssistantModeStatus ) . toHaveBeenCalledWith ( 123 )
6067 expect ( OpenCodeClient ) . toHaveBeenCalledWith ( 'http://localhost:5551' , '/assistant' )
6168 expect ( mocks . listSessionsPage ) . toHaveBeenCalledWith ( { limit : 25 , order : 'desc' } )
6269 expect ( mocks . listSessions ) . not . toHaveBeenCalled ( )
@@ -97,8 +104,13 @@ describe('useAssistantSessionLauncher', () => {
97104 } )
98105
99106 it ( 'notifies an existing assistant session when some generated updates were preserved' , async ( ) => {
100- mocks . initializeAssistantMode . mockResolvedValue ( {
107+ mocks . getAssistantModeStatus . mockResolvedValue ( {
101108 directory : '/assistant' ,
109+ files : {
110+ agentsMd : { path : '/assistant/AGENTS.md' , exists : true , created : true } ,
111+ opencodeJson : { path : '/assistant/.opencode.json' , exists : true , created : true } ,
112+ } ,
113+ defaultAgent : { name : 'assistant' , path : '/assistant/.opencode/agents/assistant.md' , exists : true , created : true } ,
102114 warnings : [
103115 {
104116 code : 'assistant-agents-md-preserved' ,
@@ -228,4 +240,82 @@ describe('useAssistantSessionLauncher', () => {
228240 expect ( onNavigate ) . toHaveBeenCalledWith ( 'created' )
229241 expect ( mocks . sendPromptAsync ) . toHaveBeenCalled ( )
230242 } )
243+
244+ it ( 'rejects with readiness error when agentMd is missing' , async ( ) => {
245+ mocks . getAssistantModeStatus . mockResolvedValue ( {
246+ directory : '/assistant' ,
247+ files : {
248+ agentsMd : { path : '/assistant/AGENTS.md' , exists : false , created : false } ,
249+ opencodeJson : { path : '/assistant/.opencode.json' , exists : true , created : true } ,
250+ } ,
251+ defaultAgent : { name : 'assistant' , path : '/assistant/.opencode/agents/assistant.md' , exists : true , created : true } ,
252+ } )
253+ const onNavigate = vi . fn ( )
254+ const { result } = renderHook ( ( ) => useAssistantSessionLauncher ( {
255+ repoId : 123 ,
256+ opcodeUrl : 'http://localhost:5551' ,
257+ onNavigate,
258+ } ) )
259+
260+ await act ( async ( ) => {
261+ await expect ( result . current . openAssistant ( ) ) . rejects . toThrow ( 'Assistant workspace is not ready' )
262+ } )
263+
264+ expect ( OpenCodeClient ) . not . toHaveBeenCalled ( )
265+ expect ( mocks . listSessionsPage ) . not . toHaveBeenCalled ( )
266+ expect ( mocks . createSession ) . not . toHaveBeenCalled ( )
267+ expect ( onNavigate ) . not . toHaveBeenCalled ( )
268+ } )
269+
270+ it ( 'rejects with readiness error when opencodeJson is missing' , async ( ) => {
271+ mocks . getAssistantModeStatus . mockResolvedValue ( {
272+ directory : '/assistant' ,
273+ files : {
274+ agentsMd : { path : '/assistant/AGENTS.md' , exists : true , created : true } ,
275+ opencodeJson : { path : '/assistant/.opencode.json' , exists : false , created : false } ,
276+ } ,
277+ defaultAgent : { name : 'assistant' , path : '/assistant/.opencode/agents/assistant.md' , exists : true , created : true } ,
278+ } )
279+ const onNavigate = vi . fn ( )
280+ const { result } = renderHook ( ( ) => useAssistantSessionLauncher ( {
281+ repoId : 123 ,
282+ opcodeUrl : 'http://localhost:5551' ,
283+ onNavigate,
284+ } ) )
285+
286+ await act ( async ( ) => {
287+ await expect ( result . current . openAssistant ( ) ) . rejects . toThrow ( 'Assistant workspace is not ready' )
288+ } )
289+
290+ expect ( OpenCodeClient ) . not . toHaveBeenCalled ( )
291+ expect ( mocks . listSessionsPage ) . not . toHaveBeenCalled ( )
292+ expect ( mocks . createSession ) . not . toHaveBeenCalled ( )
293+ expect ( onNavigate ) . not . toHaveBeenCalled ( )
294+ } )
295+
296+ it ( 'rejects with readiness error when defaultAgent is missing' , async ( ) => {
297+ mocks . getAssistantModeStatus . mockResolvedValue ( {
298+ directory : '/assistant' ,
299+ files : {
300+ agentsMd : { path : '/assistant/AGENTS.md' , exists : true , created : true } ,
301+ opencodeJson : { path : '/assistant/.opencode.json' , exists : true , created : true } ,
302+ } ,
303+ defaultAgent : undefined ,
304+ } )
305+ const onNavigate = vi . fn ( )
306+ const { result } = renderHook ( ( ) => useAssistantSessionLauncher ( {
307+ repoId : 123 ,
308+ opcodeUrl : 'http://localhost:5551' ,
309+ onNavigate,
310+ } ) )
311+
312+ await act ( async ( ) => {
313+ await expect ( result . current . openAssistant ( ) ) . rejects . toThrow ( 'Assistant workspace is not ready' )
314+ } )
315+
316+ expect ( OpenCodeClient ) . not . toHaveBeenCalled ( )
317+ expect ( mocks . listSessionsPage ) . not . toHaveBeenCalled ( )
318+ expect ( mocks . createSession ) . not . toHaveBeenCalled ( )
319+ expect ( onNavigate ) . not . toHaveBeenCalled ( )
320+ } )
231321} )
0 commit comments