@@ -29,9 +29,10 @@ jest.unstable_mockModule('../../../ui/mcp-ui-utils', () => ({
2929 addUIResourceToResponse : jest . fn ( ( _result : unknown ) => _result ) ,
3030} ) ) ;
3131
32- const { getCurrentContext} = await import ( '../../../command.js' ) ;
32+ const { getCurrentContext, getContexts } = await import ( '../../../command.js' ) ;
3333
3434const mockGetCurrentContext = getCurrentContext as jest . MockedFunction < typeof getCurrentContext > ;
35+ const mockGetContexts = getContexts as jest . MockedFunction < typeof getContexts > ;
3536
3637describe ( 'appium_context tool' , ( ) => {
3738 const mockServer = { addTool : jest . fn ( ) } as any ;
@@ -45,6 +46,7 @@ describe('appium_context tool', () => {
4546 beforeEach ( ( ) => {
4647 jest . clearAllMocks ( ) ;
4748 mockGetCurrentContext . mockResolvedValue ( 'NATIVE_APP' ) ;
49+ mockGetContexts . mockResolvedValue ( [ 'NATIVE_APP' , 'WEBVIEW_com.example' ] ) ;
4850 } ) ;
4951
5052 test ( 'setCurrentContext uses sessionId on list' , async ( ) => {
@@ -70,4 +72,15 @@ describe('appium_context tool', () => {
7072
7173 expect ( mockSetCurrentContext ) . toHaveBeenLastCalledWith ( 'WEBVIEW_com.example' , 'session-b' ) ;
7274 } ) ;
75+
76+ test ( 'surfaces driver errors instead of reporting no contexts' , async ( ) => {
77+ const tool = await getToolExecute ( ) ;
78+ mockGetContexts . mockRejectedValue ( new Error ( 'session is not started' ) ) ;
79+
80+ const result = await tool . execute ( { action : 'list' , sessionId : 'session-b' } , undefined ) ;
81+
82+ expect ( result . isError ) . toBe ( true ) ;
83+ expect ( result . content [ 0 ] . text ) . toContain ( 'session is not started' ) ;
84+ expect ( result . content [ 0 ] . text ) . not . toContain ( 'No contexts available' ) ;
85+ } ) ;
7386} ) ;
0 commit comments