Skip to content

Commit 116a9df

Browse files
committed
fix(context): surface driver errors instead of empty context list
1 parent 0ceacdf commit 116a9df

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

src/tests/tools/context/context.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

3434
const mockGetCurrentContext = getCurrentContext as jest.MockedFunction<typeof getCurrentContext>;
35+
const mockGetContexts = getContexts as jest.MockedFunction<typeof getContexts>;
3536

3637
describe('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
});

src/tools/context/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export default function context(server: FastMCP): void {
3737

3838
try {
3939
const [currentContext, availableContexts] = await Promise.all([
40-
getCurrentContext(driver).catch(() => null),
41-
getContexts(driver).catch(() => [] as string[]),
40+
getCurrentContext(driver),
41+
getContexts(driver),
4242
]);
4343

4444
if (currentContext) {

0 commit comments

Comments
 (0)