|
1 | 1 | import test from 'node:test'; |
2 | 2 | import assert from 'node:assert/strict'; |
3 | 3 | import { tryRunClientBackedCommand } from '../cli-client-commands.ts'; |
4 | | -import type { AgentDeviceClient, AppInstallFromSourceOptions } from '../client.ts'; |
| 4 | +import type { |
| 5 | + AgentDeviceClient, |
| 6 | + AppInstallFromSourceOptions, |
| 7 | + MetroPrepareOptions, |
| 8 | +} from '../client.ts'; |
5 | 9 | import { AppError } from '../utils/errors.ts'; |
6 | 10 |
|
7 | 11 | test('install-from-source forwards URL and repeated headers to client.apps.installFromSource', async () => { |
@@ -73,8 +77,129 @@ test('install-from-source rejects malformed header syntax', async () => { |
73 | 77 | ); |
74 | 78 | }); |
75 | 79 |
|
| 80 | +test('metro prepare forwards normalized options to client.metro.prepare', async () => { |
| 81 | + let observed: MetroPrepareOptions | undefined; |
| 82 | + const client = createStubClient({ |
| 83 | + installFromSource: async () => { |
| 84 | + throw new Error('unexpected install call'); |
| 85 | + }, |
| 86 | + prepareMetro: async (options) => { |
| 87 | + observed = options; |
| 88 | + return { |
| 89 | + projectRoot: '/tmp/project', |
| 90 | + kind: 'react-native', |
| 91 | + dependenciesInstalled: false, |
| 92 | + packageManager: null, |
| 93 | + started: false, |
| 94 | + reused: true, |
| 95 | + pid: 0, |
| 96 | + logPath: '/tmp/project/.agent-device/metro.log', |
| 97 | + statusUrl: 'http://127.0.0.1:8081/status', |
| 98 | + runtimeFilePath: null, |
| 99 | + iosRuntime: { |
| 100 | + platform: 'ios', |
| 101 | + bundleUrl: 'https://sandbox.example.test/index.bundle?platform=ios', |
| 102 | + }, |
| 103 | + androidRuntime: { |
| 104 | + platform: 'android', |
| 105 | + bundleUrl: 'https://sandbox.example.test/index.bundle?platform=android', |
| 106 | + }, |
| 107 | + bridge: null, |
| 108 | + }; |
| 109 | + }, |
| 110 | + }); |
| 111 | + |
| 112 | + const stdout = await captureStdout(async () => { |
| 113 | + const handled = await tryRunClientBackedCommand({ |
| 114 | + command: 'metro', |
| 115 | + positionals: ['prepare'], |
| 116 | + flags: { |
| 117 | + json: false, |
| 118 | + help: false, |
| 119 | + version: false, |
| 120 | + metroProjectRoot: './apps/demo', |
| 121 | + metroPublicBaseUrl: 'https://sandbox.example.test', |
| 122 | + metroProxyBaseUrl: 'https://proxy.example.test', |
| 123 | + metroBearerToken: 'secret', |
| 124 | + metroPreparePort: 9090, |
| 125 | + metroKind: 'expo', |
| 126 | + metroRuntimeFile: './.agent-device/metro-runtime.json', |
| 127 | + metroNoReuseExisting: true, |
| 128 | + metroNoInstallDeps: true, |
| 129 | + }, |
| 130 | + client, |
| 131 | + }); |
| 132 | + assert.equal(handled, true); |
| 133 | + }); |
| 134 | + const payload = JSON.parse(stdout); |
| 135 | + |
| 136 | + assert.deepEqual(observed, { |
| 137 | + projectRoot: './apps/demo', |
| 138 | + publicBaseUrl: 'https://sandbox.example.test', |
| 139 | + proxyBaseUrl: 'https://proxy.example.test', |
| 140 | + bearerToken: 'secret', |
| 141 | + port: 9090, |
| 142 | + kind: 'expo', |
| 143 | + runtimeFilePath: './.agent-device/metro-runtime.json', |
| 144 | + reuseExisting: false, |
| 145 | + installDependenciesIfNeeded: false, |
| 146 | + listenHost: undefined, |
| 147 | + statusHost: undefined, |
| 148 | + startupTimeoutMs: undefined, |
| 149 | + probeTimeoutMs: undefined, |
| 150 | + }); |
| 151 | + assert.equal(payload.kind, 'react-native'); |
| 152 | + assert.equal(payload.runtimeFilePath, null); |
| 153 | +}); |
| 154 | + |
| 155 | +test('metro prepare wraps output in the standard success envelope for --json', async () => { |
| 156 | + const client = createStubClient({ |
| 157 | + installFromSource: async () => { |
| 158 | + throw new Error('unexpected install call'); |
| 159 | + }, |
| 160 | + }); |
| 161 | + |
| 162 | + const stdout = await captureStdout(async () => { |
| 163 | + const handled = await tryRunClientBackedCommand({ |
| 164 | + command: 'metro', |
| 165 | + positionals: ['prepare'], |
| 166 | + flags: { |
| 167 | + json: true, |
| 168 | + help: false, |
| 169 | + version: false, |
| 170 | + metroPublicBaseUrl: 'https://sandbox.example.test', |
| 171 | + }, |
| 172 | + client, |
| 173 | + }); |
| 174 | + assert.equal(handled, true); |
| 175 | + }); |
| 176 | + |
| 177 | + const payload = JSON.parse(stdout); |
| 178 | + assert.equal(payload.success, true); |
| 179 | + assert.equal(payload.data.kind, 'react-native'); |
| 180 | + assert.equal(payload.data.iosRuntime.platform, 'ios'); |
| 181 | +}); |
| 182 | + |
| 183 | +async function captureStdout(run: () => Promise<void>): Promise<string> { |
| 184 | + let stdout = ''; |
| 185 | + const originalWrite = process.stdout.write.bind(process.stdout); |
| 186 | + (process.stdout as any).write = ((chunk: unknown) => { |
| 187 | + stdout += String(chunk); |
| 188 | + return true; |
| 189 | + }) as typeof process.stdout.write; |
| 190 | + |
| 191 | + try { |
| 192 | + await run(); |
| 193 | + } finally { |
| 194 | + process.stdout.write = originalWrite; |
| 195 | + } |
| 196 | + |
| 197 | + return stdout; |
| 198 | +} |
| 199 | + |
76 | 200 | function createStubClient(params: { |
77 | 201 | installFromSource: AgentDeviceClient['apps']['installFromSource']; |
| 202 | + prepareMetro?: AgentDeviceClient['metro']['prepare']; |
78 | 203 | }): AgentDeviceClient { |
79 | 204 | return { |
80 | 205 | devices: { |
@@ -140,6 +265,31 @@ function createStubClient(params: { |
140 | 265 | identifiers: { session: 'default' }, |
141 | 266 | }), |
142 | 267 | }, |
| 268 | + metro: { |
| 269 | + prepare: |
| 270 | + params.prepareMetro ?? |
| 271 | + (async () => ({ |
| 272 | + projectRoot: '/tmp/project', |
| 273 | + kind: 'react-native', |
| 274 | + dependenciesInstalled: false, |
| 275 | + packageManager: null, |
| 276 | + started: false, |
| 277 | + reused: true, |
| 278 | + pid: 0, |
| 279 | + logPath: '/tmp/project/.agent-device/metro.log', |
| 280 | + statusUrl: 'http://127.0.0.1:8081/status', |
| 281 | + runtimeFilePath: null, |
| 282 | + iosRuntime: { |
| 283 | + platform: 'ios', |
| 284 | + bundleUrl: 'https://sandbox.example.test/index.bundle?platform=ios', |
| 285 | + }, |
| 286 | + androidRuntime: { |
| 287 | + platform: 'android', |
| 288 | + bundleUrl: 'https://sandbox.example.test/index.bundle?platform=android', |
| 289 | + }, |
| 290 | + bridge: null, |
| 291 | + })), |
| 292 | + }, |
143 | 293 | capture: { |
144 | 294 | snapshot: async () => ({ |
145 | 295 | nodes: [], |
|
0 commit comments