forked from appium/appium-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.test.ts
More file actions
86 lines (67 loc) · 3.12 KB
/
Copy pathcontext.test.ts
File metadata and controls
86 lines (67 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import {beforeEach, describe, test, expect, jest} from '@jest/globals';
const mockDriver = {};
const mockSetCurrentContext = jest.fn<(context: string, sessionId?: string) => boolean>(() => true);
jest.unstable_mockModule('../../../session-store', () => ({
setCurrentContext: mockSetCurrentContext,
}));
jest.unstable_mockModule('../../../command', () => ({
getContexts: jest.fn(async () => ['NATIVE_APP', 'WEBVIEW_com.example']),
getCurrentContext: jest.fn(async () => 'NATIVE_APP'),
setContext: jest.fn(async () => undefined),
}));
jest.unstable_mockModule('../../../tools/tool-response', () => ({
resolveDriver: jest.fn(async () => ({ok: true, driver: mockDriver})),
textResult: (text: string) => ({content: [{type: 'text', text}]}),
errorResult: (text: string) => ({
content: [{type: 'text', text}],
isError: true,
}),
toolErrorMessage: (err: unknown) => (err instanceof Error ? err.message : String(err)),
}));
jest.unstable_mockModule('../../../ui/mcp-ui-utils', () => ({
createUIResource: jest.fn(() => ({})),
createContextSwitcherUI: jest.fn(() => ''),
addUIResourceToResponse: jest.fn((_result: unknown) => _result),
}));
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;
async function getToolExecute() {
const {default: contextTool} = await import('../../../tools/context/context.js');
contextTool(mockServer);
return (mockServer.addTool as jest.MockedFunction<any>).mock.calls.at(-1)?.[0];
}
beforeEach(() => {
jest.clearAllMocks();
mockGetCurrentContext.mockResolvedValue('NATIVE_APP');
mockGetContexts.mockResolvedValue(['NATIVE_APP', 'WEBVIEW_com.example']);
});
test('setCurrentContext uses sessionId on list', async () => {
const tool = await getToolExecute();
await tool.execute({action: 'list', sessionId: 'session-b'}, undefined);
expect(mockSetCurrentContext).toHaveBeenCalledWith('NATIVE_APP', 'session-b');
});
test('setCurrentContext uses sessionId on switch', async () => {
const tool = await getToolExecute();
mockGetCurrentContext.mockResolvedValueOnce('NATIVE_APP').mockResolvedValueOnce('WEBVIEW_com.example');
await tool.execute(
{
action: 'switch',
context: 'WEBVIEW_com.example',
sessionId: 'session-b',
},
undefined,
);
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');
});
});