22 * @vitest -environment node
33 */
44
5- import { describe , expect , it , vi } from 'vitest'
5+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
66
77const { mockCreateOpenCodeClient } = vi . hoisted ( ( ) => ( {
88 mockCreateOpenCodeClient : vi . fn ( ) ,
@@ -39,10 +39,19 @@ vi.mock('@/lib/opencode/client', () => ({
3939} ) )
4040
4141import {
42+ listOpenCodeRepositories ,
4243 promptOpenCodeSession ,
4344 shouldRetryWithFreshOpenCodeSession ,
4445} from '@/lib/opencode/service'
4546
47+ beforeEach ( ( ) => {
48+ vi . clearAllMocks ( )
49+ } )
50+
51+ afterEach ( ( ) => {
52+ vi . unstubAllEnvs ( )
53+ } )
54+
4655describe ( 'shouldRetryWithFreshOpenCodeSession' , ( ) => {
4756 it ( 'returns true for stale-session errors' , ( ) => {
4857 expect ( shouldRetryWithFreshOpenCodeSession ( new Error ( '404 session not found' ) ) ) . toBe ( true )
@@ -56,6 +65,36 @@ describe('shouldRetryWithFreshOpenCodeSession', () => {
5665 expect ( shouldRetryWithFreshOpenCodeSession ( 'model not found' ) ) . toBe ( false )
5766 expect ( shouldRetryWithFreshOpenCodeSession ( 'provider does not exist' ) ) . toBe ( false )
5867 } )
68+
69+ it ( 'does not crash for undefined, symbol, or function errors' , ( ) => {
70+ expect ( ( ) => shouldRetryWithFreshOpenCodeSession ( undefined ) ) . not . toThrow ( )
71+ expect ( ( ) => shouldRetryWithFreshOpenCodeSession ( Symbol ( 'session' ) ) ) . not . toThrow ( )
72+ expect ( ( ) => shouldRetryWithFreshOpenCodeSession ( ( ) => 'session' ) ) . not . toThrow ( )
73+ expect ( shouldRetryWithFreshOpenCodeSession ( undefined ) ) . toBe ( false )
74+ } )
75+ } )
76+
77+ describe ( 'listOpenCodeRepositories' , ( ) => {
78+ it ( 'handles OPENCODE_REPOSITORY_ROOT set to / without double slashes' , async ( ) => {
79+ vi . stubEnv ( 'OPENCODE_REPOSITORY_ROOT' , '/' )
80+
81+ mockCreateOpenCodeClient . mockReturnValue ( {
82+ project : {
83+ list : vi . fn ( ) . mockResolvedValue ( {
84+ data : [ { id : 'project-1' , worktree : '/repo-a' } ] ,
85+ } ) ,
86+ } ,
87+ } )
88+
89+ await expect ( listOpenCodeRepositories ( ) ) . resolves . toEqual ( [
90+ {
91+ id : 'repo-a' ,
92+ label : 'repo-a' ,
93+ directory : '/repo-a' ,
94+ projectId : 'project-1' ,
95+ } ,
96+ ] )
97+ } )
5998} )
6099
61100describe ( 'promptOpenCodeSession' , ( ) => {
0 commit comments