|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { AppError } from '@agent-device/kernel/errors'; |
| 4 | +import { bindAndroidAdbHostStub } from './adb-host.fixtures.ts'; |
| 5 | +import { runAndroidHostAdb, withAndroidHostAdbTransport } from './adb-executor.ts'; |
| 6 | + |
| 7 | +test('a scoped transport intercepts host adb without reaching the injected host', async () => { |
| 8 | + let hostCalls = 0; |
| 9 | + bindAndroidAdbHostStub({ |
| 10 | + execHostAdb: async () => { |
| 11 | + hostCalls += 1; |
| 12 | + return { stdout: 'host', stderr: '', exitCode: 0 }; |
| 13 | + }, |
| 14 | + }); |
| 15 | + |
| 16 | + const result = await withAndroidHostAdbTransport( |
| 17 | + async (args, options) => { |
| 18 | + assert.deepEqual(args, ['devices']); |
| 19 | + assert.deepEqual(options, { timeoutMs: 1_234 }); |
| 20 | + return { stdout: 'transport', stderr: '', exitCode: 0 }; |
| 21 | + }, |
| 22 | + async () => await runAndroidHostAdb(['devices'], { timeoutMs: 1_234 }), |
| 23 | + ); |
| 24 | + |
| 25 | + assert.equal(result.stdout, 'transport'); |
| 26 | + assert.equal(hostCalls, 0); |
| 27 | +}); |
| 28 | + |
| 29 | +test('the local host arm always obtains a result before applying the shared failure contract', async () => { |
| 30 | + let receivedOptions: Record<string, unknown> | undefined; |
| 31 | + bindAndroidAdbHostStub({ |
| 32 | + execHostAdb: async (_args, options) => { |
| 33 | + receivedOptions = options; |
| 34 | + return { stdout: '', stderr: 'error: device offline', exitCode: 1 }; |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + const error = await runAndroidHostAdb(['devices']).then( |
| 39 | + () => assert.fail('expected the host adb call to reject'), |
| 40 | + (cause: unknown) => cause, |
| 41 | + ); |
| 42 | + |
| 43 | + assert.deepEqual(receivedOptions, { allowFailure: true }); |
| 44 | + assert.ok(error instanceof AppError); |
| 45 | + assert.equal(error.details?.adbFailure, 'device_offline'); |
| 46 | + assert.equal(error.details?.retriable, true); |
| 47 | + assert.match(String(error.details?.hint), /adb reconnect/i); |
| 48 | +}); |
| 49 | + |
| 50 | +test('allowFailure returns a nonzero local result unchanged', async () => { |
| 51 | + const scripted = { stdout: '', stderr: 'offline', exitCode: 7 }; |
| 52 | + bindAndroidAdbHostStub({ execHostAdb: async () => scripted }); |
| 53 | + |
| 54 | + assert.deepEqual( |
| 55 | + await runAndroidHostAdb(['devices'], { allowFailure: true, timeoutMs: 5_000 }), |
| 56 | + scripted, |
| 57 | + ); |
| 58 | +}); |
| 59 | + |
| 60 | +test('unchecked transport results are normalized at the package boundary', async () => { |
| 61 | + bindAndroidAdbHostStub({ |
| 62 | + coerceAdbResult: (result) => ({ |
| 63 | + ...result, |
| 64 | + stdout: typeof result.stdout === 'string' ? result.stdout : '', |
| 65 | + stderr: typeof result.stderr === 'string' ? result.stderr : '', |
| 66 | + exitCode: Number(result.exitCode), |
| 67 | + }), |
| 68 | + }); |
| 69 | + const sloppy = { stdout: undefined, stderr: null, exitCode: '1' } as never; |
| 70 | + |
| 71 | + const result = await withAndroidHostAdbTransport( |
| 72 | + async () => sloppy, |
| 73 | + async () => await runAndroidHostAdb(['devices'], { allowFailure: true }), |
| 74 | + ); |
| 75 | + |
| 76 | + assert.deepEqual(result, { stdout: '', stderr: '', exitCode: 1 }); |
| 77 | +}); |
| 78 | + |
| 79 | +test('nested transport scopes are innermost-first and restore on scope exit', async () => { |
| 80 | + bindAndroidAdbHostStub({ |
| 81 | + execHostAdb: async () => ({ stdout: 'host', stderr: '', exitCode: 0 }), |
| 82 | + }); |
| 83 | + const transportFor = (name: string) => async () => ({ |
| 84 | + stdout: name, |
| 85 | + stderr: '', |
| 86 | + exitCode: 0, |
| 87 | + }); |
| 88 | + |
| 89 | + await withAndroidHostAdbTransport(transportFor('outer'), async () => { |
| 90 | + assert.equal((await runAndroidHostAdb(['devices'])).stdout, 'outer'); |
| 91 | + await withAndroidHostAdbTransport(transportFor('inner'), async () => { |
| 92 | + assert.equal((await runAndroidHostAdb(['devices'])).stdout, 'inner'); |
| 93 | + }); |
| 94 | + assert.equal((await runAndroidHostAdb(['devices'])).stdout, 'outer'); |
| 95 | + }); |
| 96 | + assert.equal((await runAndroidHostAdb(['devices'])).stdout, 'host'); |
| 97 | +}); |
0 commit comments