-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathdispatch-push.test.ts
More file actions
91 lines (84 loc) · 3.23 KB
/
dispatch-push.test.ts
File metadata and controls
91 lines (84 loc) · 3.23 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
import test from 'node:test';
import assert from 'node:assert/strict';
import { promises as fs } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { dispatchCommand } from '../dispatch.ts';
import { AppError } from '../../utils/errors.ts';
import type { DeviceInfo } from '../../utils/device.ts';
const ANDROID_DEVICE: DeviceInfo = {
platform: 'android',
id: 'emulator-5554',
name: 'Pixel',
kind: 'emulator',
booted: true,
};
test('dispatch push reports missing payload file as INVALID_ARGS', async () => {
await assert.rejects(
() => dispatchCommand(ANDROID_DEVICE, 'push', ['com.example.app', './missing-payload.json']),
(error: unknown) => {
assert.equal(error instanceof AppError, true);
assert.equal((error as AppError).code, 'INVALID_ARGS');
assert.match((error as AppError).message, /payload file not found/i);
return true;
},
);
});
test('dispatch push reports directory payload path as INVALID_ARGS', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-dispatch-push-dir-'));
try {
await assert.rejects(
() => dispatchCommand(ANDROID_DEVICE, 'push', ['com.example.app', tempDir]),
(error: unknown) => {
assert.equal(error instanceof AppError, true);
assert.equal((error as AppError).code, 'INVALID_ARGS');
assert.match((error as AppError).message, /not a file/i);
return true;
},
);
} finally {
await fs.rm(tempDir, { recursive: true, force: true });
}
});
test('dispatch push prefers existing brace-prefixed payload file over inline parsing', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-dispatch-push-brace-'));
const adbPath = path.join(tempDir, 'adb');
const argsLogPath = path.join(tempDir, 'args.log');
const payloadPath = path.join(tempDir, '{payload}.json');
await fs.writeFile(
adbPath,
'#!/bin/sh\nprintf "%s\\n" "$@" > "$AGENT_DEVICE_TEST_ARGS_FILE"\nexit 0\n',
'utf8',
);
await fs.chmod(adbPath, 0o755);
await fs.writeFile(payloadPath, '{"action":"com.example.app.PUSH","extras":{"title":"Hello"}}\n', 'utf8');
const previousPath = process.env.PATH;
const previousArgsFile = process.env.AGENT_DEVICE_TEST_ARGS_FILE;
process.env.PATH = `${tempDir}${path.delimiter}${previousPath ?? ''}`;
process.env.AGENT_DEVICE_TEST_ARGS_FILE = argsLogPath;
try {
const result = await dispatchCommand(ANDROID_DEVICE, 'push', ['com.example.app', payloadPath]);
assert.deepEqual(result, {
platform: 'android',
package: 'com.example.app',
action: 'com.example.app.PUSH',
extrasCount: 1,
});
const args = (await fs.readFile(argsLogPath, 'utf8'))
.trim()
.split('\n')
.filter(Boolean);
assert.equal(args.includes('-a'), true);
assert.equal(args.includes('com.example.app.PUSH'), true);
assert.equal(args.includes('--es'), true);
assert.equal(args.includes('title'), true);
} finally {
process.env.PATH = previousPath;
if (previousArgsFile === undefined) {
delete process.env.AGENT_DEVICE_TEST_ARGS_FILE;
} else {
process.env.AGENT_DEVICE_TEST_ARGS_FILE = previousArgsFile;
}
await fs.rm(tempDir, { recursive: true, force: true });
}
});