Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/tests/tools/context/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ jest.unstable_mockModule('../../../ui/mcp-ui-utils', () => ({
addUIResourceToResponse: jest.fn((_result: unknown) => _result),
}));

const {getCurrentContext} = await import('../../../command.js');
const {getCurrentContext, getContexts} = await import('../../../command.js');

const mockGetCurrentContext = getCurrentContext as jest.MockedFunction<typeof getCurrentContext>;
const mockGetContexts = getContexts as jest.MockedFunction<typeof getContexts>;

describe('appium_context tool', () => {
const mockServer = {addTool: jest.fn()} as any;
Expand All @@ -45,6 +46,7 @@ describe('appium_context tool', () => {
beforeEach(() => {
jest.clearAllMocks();
mockGetCurrentContext.mockResolvedValue('NATIVE_APP');
mockGetContexts.mockResolvedValue(['NATIVE_APP', 'WEBVIEW_com.example']);
});

test('setCurrentContext uses sessionId on list', async () => {
Expand All @@ -70,4 +72,15 @@ describe('appium_context tool', () => {

expect(mockSetCurrentContext).toHaveBeenLastCalledWith('WEBVIEW_com.example', 'session-b');
});

test('surfaces driver errors instead of reporting no contexts', async () => {
const tool = await getToolExecute();
mockGetContexts.mockRejectedValue(new Error('session is not started'));

const result = await tool.execute({action: 'list', sessionId: 'session-b'}, undefined);

expect(result.isError).toBe(true);
expect(result.content[0].text).toContain('session is not started');
expect(result.content[0].text).not.toContain('No contexts available');
});
});
5 changes: 1 addition & 4 deletions src/tools/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ export default function context(server: FastMCP): void {
const {driver} = resolved;

try {
const [currentContext, availableContexts] = await Promise.all([
getCurrentContext(driver).catch(() => null),
getContexts(driver).catch(() => [] as string[]),
]);
const [currentContext, availableContexts] = await Promise.all([getCurrentContext(driver), getContexts(driver)]);

if (currentContext) {
setCurrentContext(currentContext, args.sessionId);
Expand Down