-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathrequest-router-android-modal.test.ts
More file actions
136 lines (122 loc) · 4.03 KB
/
request-router-android-modal.test.ts
File metadata and controls
136 lines (122 loc) · 4.03 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { test, expect, vi } from 'vitest';
import os from 'node:os';
import path from 'node:path';
let snapshotCalls = 0;
const dispatchCalls: string[][] = [];
vi.mock('../../core/dispatch.ts', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../core/dispatch.ts')>();
return {
...actual,
dispatchCommand: vi.fn(async (_device: unknown, command: string, positionals: string[]) => {
dispatchCalls.push([command, ...positionals]);
return {};
}),
};
});
import { createRequestHandler } from '../request-router.ts';
import type { SessionState } from '../types.ts';
import { LeaseRegistry } from '../lease-registry.ts';
import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts';
vi.mock('../../platforms/android/snapshot.ts', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../platforms/android/snapshot.ts')>();
return {
...actual,
snapshotAndroid: vi.fn(async () => {
snapshotCalls += 1;
if (snapshotCalls === 1) {
return {
nodes: [
{
index: 0,
type: 'android.widget.TextView',
label: 'Process system is not responding',
rect: { x: 50, y: 400, width: 500, height: 80 },
},
{
index: 1,
type: 'android.widget.Button',
label: 'Close app',
rect: { x: 100, y: 600, width: 220, height: 80 },
},
],
};
}
return { nodes: [] };
}),
};
});
vi.mock('../../platforms/android/app-lifecycle.ts', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../platforms/android/app-lifecycle.ts')>();
return {
...actual,
openAndroidApp: vi.fn(async () => {}),
getAndroidAppState: vi.fn(async () => ({ package: 'com.android.settings' })),
getAndroidBlockingDialogFocus: vi.fn(async () => null),
};
});
const execCalls: string[][] = [];
vi.mock('../../utils/exec.ts', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../utils/exec.ts')>();
return {
...actual,
runCmd: vi.fn(async (_cmd: string, args: string[]) => {
execCalls.push(args);
return { stdout: '', stderr: '', exitCode: 0 };
}),
};
});
function makeAndroidSession(name: string): SessionState {
return {
name,
createdAt: Date.now(),
appBundleId: 'com.android.settings',
actions: [],
device: {
platform: 'android',
target: 'mobile',
id: 'emulator-5554',
name: 'Pixel 9 Pro XL',
kind: 'emulator',
booted: true,
},
recording: {
platform: 'android',
outPath: '/tmp/demo.mp4',
remotePath: '/sdcard/demo.mp4',
remotePid: '4242',
startedAt: Date.now() - 1_000,
showTouches: true,
gestureEvents: [],
},
};
}
test('generic Android gesture commands dismiss blocking system dialogs during recording', async () => {
snapshotCalls = 0;
execCalls.length = 0;
dispatchCalls.length = 0;
const sessionStore = makeSessionStore('agent-device-router-android-modal-');
sessionStore.set('default', makeAndroidSession('default'));
const { openAndroidApp } = await import('../../platforms/android/app-lifecycle.ts');
const handler = createRequestHandler({
logPath: path.join(os.tmpdir(), 'daemon.log'),
token: 'test-token',
sessionStore,
leaseRegistry: new LeaseRegistry(),
trackDownloadableArtifact: () => 'artifact-id',
});
const response = await handler({
token: 'test-token',
session: 'default',
command: 'scroll',
positionals: ['down', '0.55'],
meta: { requestId: 'req-android-modal' },
});
expect(response.ok).toBe(true);
expect(dispatchCalls).toEqual([['scroll', 'down', '0.55']]);
expect(execCalls).toEqual([['-s', 'emulator-5554', 'shell', 'input', 'tap', '210', '640']]);
expect(openAndroidApp).toHaveBeenCalledWith(
expect.objectContaining({ id: 'emulator-5554' }),
'com.android.settings',
);
expect(snapshotCalls).toBe(2);
});