|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { parse } from 'yaml'; |
| 4 | +import { expect, test } from 'vitest'; |
| 5 | +import { runCmdSync } from '@agent-device/host-kit/command'; |
| 6 | +import { mkdtempForTest } from '../../src/__tests__/test-utils/tmp-dir.ts'; |
| 7 | + |
| 8 | +test('emulator action invocation keeps shell state and summarizes a failed replay before returning its status', async () => { |
| 9 | + const cwd = await mkdtempForTest('android-manual-invocation-'); |
| 10 | + const bin = path.join(cwd, 'bin'); |
| 11 | + fs.mkdirSync(bin); |
| 12 | + const writeTool = (name: string, script: string) => |
| 13 | + fs.writeFileSync(path.join(bin, name), `#!/bin/sh\n${script}\n`, { mode: 0o755 }); |
| 14 | + writeTool('adb', String.raw`printf "List of devices attached\nemulator-7000\tdevice\n"`); |
| 15 | + writeTool( |
| 16 | + 'pnpm', |
| 17 | + 'printf "%s\\n" "$*" >> "$READINESS_TEST_LOG"\nif [ "$1 $2" = "gate replay-android" ]; then exit 7; fi', |
| 18 | + ); |
| 19 | + writeTool( |
| 20 | + 'node', |
| 21 | + 'if [ "$3" = begin ]; then touch started; fi\nif [ "$3" = summarize ]; then touch summarized; fi', |
| 22 | + ); |
| 23 | + fs.mkdirSync(path.join(cwd, 'scripts/replays'), { recursive: true }); |
| 24 | + fs.copyFileSync( |
| 25 | + 'scripts/replays/android-manual.sh', |
| 26 | + path.join(cwd, 'scripts/replays/android-manual.sh'), |
| 27 | + ); |
| 28 | + const workflow = parse(fs.readFileSync('.github/workflows/replays-manual.yml', 'utf8')); |
| 29 | + const runner = workflow.jobs['manual-android'].steps.find((step: { uses?: string }) => |
| 30 | + step.uses?.startsWith('reactivecircus/android-emulator-runner@'), |
| 31 | + ); |
| 32 | + const script = runner.with.script |
| 33 | + .replaceAll(/\$\{\{ inputs.readiness_wait \}\}/g, 'enabled') |
| 34 | + .replaceAll(/\$\{\{ steps.fixture-app.outputs.apk-path \}\}/g, '') |
| 35 | + .replaceAll(/\$\{\{ steps.fixture-app.outputs.app-id \}\}/g, '') |
| 36 | + .replaceAll(/\$\{\{ steps.android-setup.outputs.seconds \}\}/g, '0'); |
| 37 | + const commands = script |
| 38 | + .split('\n') |
| 39 | + .map((line: string) => line.trim()) |
| 40 | + .filter((line: string) => line.length > 0 && !line.startsWith('#')); |
| 41 | + const log = path.join(cwd, 'calls.log'); |
| 42 | + let exitCode = 0; |
| 43 | + for (const command of commands) { |
| 44 | + exitCode = runCmdSync('sh', ['-c', command], { |
| 45 | + cwd, |
| 46 | + allowFailure: true, |
| 47 | + env: { |
| 48 | + PATH: `${bin}:${process.env.PATH}`, |
| 49 | + ANDROID_SERIAL: 'emulator-legacy', |
| 50 | + AGENT_DEVICE_STATE_DIR: path.join(cwd, 'state'), |
| 51 | + READINESS_TEST_LOG: log, |
| 52 | + }, |
| 53 | + }).exitCode; |
| 54 | + if (exitCode !== 0) break; |
| 55 | + } |
| 56 | + expect(exitCode).toBe(7); |
| 57 | + expect(fs.existsSync(path.join(cwd, 'started'))).toBe(true); |
| 58 | + expect(fs.existsSync(path.join(cwd, 'summarized'))).toBe(true); |
| 59 | + expect(fs.readFileSync(log, 'utf8')).toContain( |
| 60 | + 'replay-android --serial emulator-7000 --debug --retries 0', |
| 61 | + ); |
| 62 | +}); |
0 commit comments