|
| 1 | +import { PassThrough } from 'node:stream'; |
| 2 | +import { vi } from 'vitest'; |
| 3 | +import type { ChildProcessByStdio } from 'node:child_process'; |
| 4 | + |
| 5 | +const createMockChildProcess = (): ChildProcessByStdio< |
| 6 | + null, |
| 7 | + PassThrough, |
| 8 | + PassThrough |
| 9 | +> => { |
| 10 | + const stdout = new PassThrough(); |
| 11 | + const stderr = new PassThrough(); |
| 12 | + |
| 13 | + return { |
| 14 | + stdout, |
| 15 | + stderr, |
| 16 | + unref: vi.fn(), |
| 17 | + once: vi.fn(), |
| 18 | + removeAllListeners: vi.fn(), |
| 19 | + } as unknown as ChildProcessByStdio<null, PassThrough, PassThrough>; |
| 20 | +}; |
| 21 | + |
| 22 | +const createMockSubprocess = () => ({ |
| 23 | + nodeChildProcess: Promise.resolve({ |
| 24 | + kill: vi.fn(), |
| 25 | + }), |
| 26 | + [Symbol.asyncIterator]: async function* () {}, |
| 27 | +}); |
| 28 | + |
| 29 | +vi.mock('../src/adb.js', async (importOriginal) => { |
| 30 | + const actual = await importOriginal<typeof import('../src/adb.js')>(); |
| 31 | + |
| 32 | + return { |
| 33 | + ...actual, |
| 34 | + emulatorProcess: { |
| 35 | + startDetachedProcess: vi.fn(createMockChildProcess), |
| 36 | + }, |
| 37 | + verifyAndroidEmulatorSdk: vi.fn().mockResolvedValue(undefined), |
| 38 | + isAppInstalled: vi.fn().mockResolvedValue(true), |
| 39 | + reversePort: vi.fn().mockResolvedValue(undefined), |
| 40 | + stopApp: vi.fn().mockResolvedValue(undefined), |
| 41 | + startApp: vi.fn().mockResolvedValue(undefined), |
| 42 | + getDeviceIds: vi.fn().mockResolvedValue([]), |
| 43 | + getEmulatorName: vi.fn().mockResolvedValue(''), |
| 44 | + getShellProperty: vi.fn().mockResolvedValue(''), |
| 45 | + getDeviceInfo: vi.fn().mockResolvedValue(null), |
| 46 | + isBootCompleted: vi.fn().mockResolvedValue(true), |
| 47 | + stopEmulator: vi.fn().mockResolvedValue(undefined), |
| 48 | + installApp: vi.fn().mockResolvedValue(undefined), |
| 49 | + uninstallApp: vi.fn().mockResolvedValue(undefined), |
| 50 | + hasAvd: vi.fn().mockResolvedValue(false), |
| 51 | + createAvd: vi.fn().mockResolvedValue(undefined), |
| 52 | + deleteAvd: vi.fn().mockResolvedValue(undefined), |
| 53 | + startEmulator: vi.fn().mockResolvedValue(undefined), |
| 54 | + waitForEmulator: vi.fn().mockResolvedValue('emulator-5554'), |
| 55 | + waitForEmulatorDisconnect: vi.fn().mockResolvedValue(undefined), |
| 56 | + waitForBoot: vi.fn().mockResolvedValue('emulator-5554'), |
| 57 | + isAppRunning: vi.fn().mockResolvedValue(false), |
| 58 | + getAppUid: vi.fn().mockResolvedValue(0), |
| 59 | + setHideErrorDialogs: vi.fn().mockResolvedValue(undefined), |
| 60 | + getLogcatTimestamp: vi.fn().mockResolvedValue('01-01 00:00:00.000'), |
| 61 | + startLogcat: vi.fn(createMockSubprocess), |
| 62 | + getAvds: vi.fn().mockResolvedValue([]), |
| 63 | + getConnectedDevices: vi.fn().mockResolvedValue([]), |
| 64 | + grantPermissions: vi.fn().mockResolvedValue(undefined), |
| 65 | + }; |
| 66 | +}); |
0 commit comments