|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import type { |
| 4 | + AgentDeviceBackend, |
| 5 | + BackendAppEvent, |
| 6 | + BackendOpenTarget, |
| 7 | + BackendPushInput, |
| 8 | +} from '../backend.ts'; |
| 9 | +import { createLocalArtifactAdapter } from '../io.ts'; |
| 10 | +import { createAgentDevice, localCommandPolicy, restrictedCommandPolicy } from '../runtime.ts'; |
| 11 | + |
| 12 | +test('runtime app commands call typed backend lifecycle primitives', async () => { |
| 13 | + const calls: unknown[] = []; |
| 14 | + const device = createAgentDevice({ |
| 15 | + backend: createAppsBackend(calls), |
| 16 | + artifacts: createLocalArtifactAdapter(), |
| 17 | + policy: localCommandPolicy(), |
| 18 | + }); |
| 19 | + |
| 20 | + const opened = await device.apps.open({ |
| 21 | + session: 'default', |
| 22 | + app: ' com.example.app ', |
| 23 | + relaunch: true, |
| 24 | + }); |
| 25 | + assert.deepEqual(opened, { |
| 26 | + kind: 'appOpened', |
| 27 | + target: { app: 'com.example.app' }, |
| 28 | + relaunch: true, |
| 29 | + backendResult: { opened: true }, |
| 30 | + message: 'Opened: com.example.app', |
| 31 | + }); |
| 32 | + |
| 33 | + const closed = await device.apps.close({ app: 'com.example.app' }); |
| 34 | + assert.equal(closed.kind, 'appClosed'); |
| 35 | + |
| 36 | + const listed = await device.apps.list({ filter: 'user-installed' }); |
| 37 | + assert.deepEqual(listed.apps, [ |
| 38 | + { |
| 39 | + id: 'com.example.app', |
| 40 | + name: 'Example', |
| 41 | + bundleId: 'com.example.app', |
| 42 | + }, |
| 43 | + ]); |
| 44 | + |
| 45 | + const state = await device.apps.state({ app: 'com.example.app' }); |
| 46 | + assert.deepEqual(state.state, { bundleId: 'com.example.app', state: 'foreground' }); |
| 47 | + |
| 48 | + const pushed = await device.apps.push({ |
| 49 | + app: 'com.example.app', |
| 50 | + input: { kind: 'json', payload: { aps: { alert: 'hello' } } }, |
| 51 | + }); |
| 52 | + assert.equal(pushed.inputKind, 'json'); |
| 53 | + |
| 54 | + const triggered = await device.apps.triggerEvent({ |
| 55 | + name: 'example.ready', |
| 56 | + payload: { source: 'test' }, |
| 57 | + }); |
| 58 | + assert.equal(triggered.name, 'example.ready'); |
| 59 | + |
| 60 | + assert.deepEqual(calls, [ |
| 61 | + { |
| 62 | + command: 'openApp', |
| 63 | + target: { app: 'com.example.app' }, |
| 64 | + options: { relaunch: true }, |
| 65 | + session: 'default', |
| 66 | + }, |
| 67 | + { command: 'closeApp', app: 'com.example.app' }, |
| 68 | + { command: 'listApps', filter: 'user-installed' }, |
| 69 | + { command: 'getAppState', app: 'com.example.app' }, |
| 70 | + { |
| 71 | + command: 'pushFile', |
| 72 | + target: 'com.example.app', |
| 73 | + input: { kind: 'json', payload: { aps: { alert: 'hello' } } }, |
| 74 | + }, |
| 75 | + { |
| 76 | + command: 'triggerAppEvent', |
| 77 | + event: { name: 'example.ready', payload: { source: 'test' } }, |
| 78 | + }, |
| 79 | + ]); |
| 80 | +}); |
| 81 | + |
| 82 | +test('runtime app push rejects local payload paths under restricted policy', async () => { |
| 83 | + let pushCalled = false; |
| 84 | + const device = createAgentDevice({ |
| 85 | + backend: { |
| 86 | + ...createAppsBackend([]), |
| 87 | + pushFile: async () => { |
| 88 | + pushCalled = true; |
| 89 | + }, |
| 90 | + }, |
| 91 | + artifacts: createLocalArtifactAdapter(), |
| 92 | + policy: restrictedCommandPolicy(), |
| 93 | + }); |
| 94 | + |
| 95 | + await assert.rejects( |
| 96 | + () => |
| 97 | + device.apps.push({ |
| 98 | + app: 'com.example.app', |
| 99 | + input: { kind: 'path', path: '/tmp/payload.json' }, |
| 100 | + }), |
| 101 | + /Local input paths are not allowed/, |
| 102 | + ); |
| 103 | + assert.equal(pushCalled, false); |
| 104 | +}); |
| 105 | + |
| 106 | +test('runtime app commands validate JSON payloads', async () => { |
| 107 | + const device = createAgentDevice({ |
| 108 | + backend: createAppsBackend([]), |
| 109 | + artifacts: createLocalArtifactAdapter(), |
| 110 | + policy: localCommandPolicy(), |
| 111 | + }); |
| 112 | + |
| 113 | + await assert.rejects( |
| 114 | + () => device.apps.triggerEvent({ name: 'bad event' }), |
| 115 | + /Invalid apps\.triggerEvent name/, |
| 116 | + ); |
| 117 | + await assert.rejects( |
| 118 | + () => |
| 119 | + device.apps.push({ |
| 120 | + app: 'com.example.app', |
| 121 | + input: { kind: 'json', payload: [] as unknown as Record<string, unknown> }, |
| 122 | + }), |
| 123 | + /JSON payload must be a JSON object/, |
| 124 | + ); |
| 125 | + await assert.rejects( |
| 126 | + () => |
| 127 | + device.apps.push({ |
| 128 | + app: 'com.example.app', |
| 129 | + input: { |
| 130 | + kind: 'json', |
| 131 | + payload: { count: 1n } as unknown as Record<string, unknown>, |
| 132 | + }, |
| 133 | + }), |
| 134 | + /JSON payload must be JSON-serializable/, |
| 135 | + ); |
| 136 | + await assert.rejects( |
| 137 | + () => |
| 138 | + device.apps.push({ |
| 139 | + app: 'com.example.app', |
| 140 | + input: { |
| 141 | + kind: 'json', |
| 142 | + payload: { toJSON: () => undefined } as unknown as Record<string, unknown>, |
| 143 | + }, |
| 144 | + }), |
| 145 | + /JSON payload must be JSON-serializable/, |
| 146 | + ); |
| 147 | + await assert.rejects( |
| 148 | + () => |
| 149 | + device.apps.push({ |
| 150 | + app: 'com.example.app', |
| 151 | + input: { |
| 152 | + kind: 'json', |
| 153 | + payload: { data: 'x'.repeat(8 * 1024) }, |
| 154 | + }, |
| 155 | + }), |
| 156 | + /JSON payload exceeds 8192 bytes/, |
| 157 | + ); |
| 158 | + await assert.rejects( |
| 159 | + () => |
| 160 | + device.apps.triggerEvent({ |
| 161 | + name: 'example.ready', |
| 162 | + payload: { count: 1n } as unknown as Record<string, unknown>, |
| 163 | + }), |
| 164 | + /payload for "example.ready" must be JSON-serializable/, |
| 165 | + ); |
| 166 | + await assert.rejects( |
| 167 | + () => |
| 168 | + device.apps.triggerEvent({ |
| 169 | + name: 'example.ready', |
| 170 | + payload: { toJSON: () => undefined } as unknown as Record<string, unknown>, |
| 171 | + }), |
| 172 | + /payload for "example.ready" must be JSON-serializable/, |
| 173 | + ); |
| 174 | + await assert.rejects( |
| 175 | + () => |
| 176 | + device.apps.triggerEvent({ |
| 177 | + name: 'example.ready', |
| 178 | + payload: { data: 'x'.repeat(8 * 1024) }, |
| 179 | + }), |
| 180 | + /payload for "example.ready" exceeds 8192 bytes/, |
| 181 | + ); |
| 182 | + await assert.rejects( |
| 183 | + () => |
| 184 | + device.apps.push({ |
| 185 | + app: 'com.example.app', |
| 186 | + input: undefined as unknown as Parameters<typeof device.apps.push>[0]['input'], |
| 187 | + }), |
| 188 | + /apps\.push requires an input/, |
| 189 | + ); |
| 190 | +}); |
| 191 | + |
| 192 | +function createAppsBackend(calls: unknown[]): AgentDeviceBackend { |
| 193 | + return { |
| 194 | + platform: 'ios', |
| 195 | + openApp: async (context, target: BackendOpenTarget, options) => { |
| 196 | + calls.push({ |
| 197 | + command: 'openApp', |
| 198 | + target, |
| 199 | + options, |
| 200 | + session: context.session, |
| 201 | + }); |
| 202 | + return { opened: true }; |
| 203 | + }, |
| 204 | + closeApp: async (_context, app) => { |
| 205 | + calls.push({ command: 'closeApp', app }); |
| 206 | + }, |
| 207 | + listApps: async (_context, filter) => { |
| 208 | + calls.push({ command: 'listApps', filter }); |
| 209 | + return [{ id: 'com.example.app', name: 'Example', bundleId: 'com.example.app' }]; |
| 210 | + }, |
| 211 | + getAppState: async (_context, app) => { |
| 212 | + calls.push({ command: 'getAppState', app }); |
| 213 | + return { bundleId: app, state: 'foreground' }; |
| 214 | + }, |
| 215 | + pushFile: async (_context, input: BackendPushInput, target) => { |
| 216 | + calls.push({ command: 'pushFile', target, input }); |
| 217 | + }, |
| 218 | + triggerAppEvent: async (_context, event: BackendAppEvent) => { |
| 219 | + calls.push({ command: 'triggerAppEvent', event }); |
| 220 | + }, |
| 221 | + }; |
| 222 | +} |
0 commit comments