|
| 1 | +import { test } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { setAndroidSetting } from '../settings.ts'; |
| 4 | +import type { AndroidAirplaneMode } from '../settings-airplane.ts'; |
| 5 | +import { assertRejectsAppError } from './test-utils/app-error.ts'; |
| 6 | +import { withFakeAdb, type FakeAdbScript } from './test-utils/fake-adb.ts'; |
| 7 | + |
| 8 | +const READ = 'shell cmd connectivity airplane-mode'; |
| 9 | + |
| 10 | +/** |
| 11 | + * An Android build whose connectivity service owns airplane mode. `honorWrites: false` models the |
| 12 | + * service accepting the command and leaving the state alone, which is what a silently ineffective |
| 13 | + * airplane-mode path looks like from the outside. |
| 14 | + */ |
| 15 | +function connectivityService( |
| 16 | + initial: AndroidAirplaneMode, |
| 17 | + options: { honorWrites?: boolean } = {}, |
| 18 | +): FakeAdbScript { |
| 19 | + let state = initial; |
| 20 | + return (args) => { |
| 21 | + const flat = args.join(' '); |
| 22 | + if (flat === READ) return state; |
| 23 | + if (flat === `${READ} enable` || flat === `${READ} disable`) { |
| 24 | + if (options.honorWrites !== false) state = flat.endsWith('enable') ? 'enabled' : 'disabled'; |
| 25 | + return ''; |
| 26 | + } |
| 27 | + return { stderr: `unexpected args: ${flat}`, exitCode: 1 }; |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +test('setAndroidSetting airplane on enables through connectivity and reports its state', async () => { |
| 32 | + await withFakeAdb(connectivityService('disabled'), async ({ calls, device }) => { |
| 33 | + const result = await setAndroidSetting(device, 'airplane', 'on'); |
| 34 | + assert.deepEqual(result, { airplaneMode: 'enabled' }); |
| 35 | + assert.deepEqual( |
| 36 | + calls.map((args) => args.join(' ')), |
| 37 | + [READ, `${READ} enable`, READ], |
| 38 | + ); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +test('setAndroidSetting airplane off disables through connectivity and reports its state', async () => { |
| 43 | + await withFakeAdb(connectivityService('enabled'), async ({ calls, device }) => { |
| 44 | + const result = await setAndroidSetting(device, 'airplane', 'off'); |
| 45 | + assert.deepEqual(result, { airplaneMode: 'disabled' }); |
| 46 | + assert.deepEqual( |
| 47 | + calls.map((args) => args.join(' ')), |
| 48 | + [READ, `${READ} disable`, READ], |
| 49 | + ); |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +test('setAndroidSetting airplane reports the state connectivity holds, not the one requested', async () => { |
| 54 | + await withFakeAdb( |
| 55 | + connectivityService('disabled', { honorWrites: false }), |
| 56 | + async ({ calls, device }) => { |
| 57 | + await assertRejectsAppError(() => setAndroidSetting(device, 'airplane', 'on'), { |
| 58 | + code: 'COMMAND_FAILED', |
| 59 | + message: /reads disabled after requesting enabled/, |
| 60 | + }); |
| 61 | + assert.deepEqual( |
| 62 | + calls.map((args) => args.join(' ')), |
| 63 | + [READ, `${READ} enable`, READ], |
| 64 | + ); |
| 65 | + }, |
| 66 | + ); |
| 67 | +}); |
| 68 | + |
| 69 | +test('setAndroidSetting airplane refuses builds without the connectivity command before writing', async () => { |
| 70 | + await withFakeAdb( |
| 71 | + () => ({ stdout: 'Unknown command: airplane-mode', exitCode: 255 }), |
| 72 | + async ({ calls, device }) => { |
| 73 | + await assertRejectsAppError(() => setAndroidSetting(device, 'airplane', 'on'), { |
| 74 | + code: 'UNSUPPORTED_OPERATION', |
| 75 | + message: /no airplane-mode command/, |
| 76 | + hint: /Android 11 \(API 30\)/, |
| 77 | + }); |
| 78 | + assert.deepEqual( |
| 79 | + calls.map((args) => args.join(' ')), |
| 80 | + [READ], |
| 81 | + ); |
| 82 | + }, |
| 83 | + ); |
| 84 | +}); |
| 85 | + |
| 86 | +test('setAndroidSetting airplane refuses unreadable state before writing', async () => { |
| 87 | + await withFakeAdb( |
| 88 | + () => 'Airplane mode: who knows', |
| 89 | + async ({ calls, device }) => { |
| 90 | + await assertRejectsAppError(() => setAndroidSetting(device, 'airplane', 'off'), { |
| 91 | + code: 'COMMAND_FAILED', |
| 92 | + message: /Failed to read Android airplane mode/, |
| 93 | + }); |
| 94 | + assert.deepEqual( |
| 95 | + calls.map((args) => args.join(' ')), |
| 96 | + [READ], |
| 97 | + ); |
| 98 | + }, |
| 99 | + ); |
| 100 | +}); |
| 101 | + |
| 102 | +test('setAndroidSetting airplane keeps a refused read a command failure, not an unsupported build', async () => { |
| 103 | + await withFakeAdb( |
| 104 | + () => ({ |
| 105 | + stdout: '', |
| 106 | + stderr: 'java.lang.SecurityException: Permission Denial: not allowed to change airplane mode', |
| 107 | + exitCode: 255, |
| 108 | + }), |
| 109 | + async ({ calls, device }) => { |
| 110 | + await assertRejectsAppError(() => setAndroidSetting(device, 'airplane', 'on'), { |
| 111 | + code: 'COMMAND_FAILED', |
| 112 | + message: /Failed to read Android airplane mode/, |
| 113 | + }); |
| 114 | + assert.deepEqual( |
| 115 | + calls.map((args) => args.join(' ')), |
| 116 | + [READ], |
| 117 | + ); |
| 118 | + }, |
| 119 | + ); |
| 120 | +}); |
| 121 | + |
| 122 | +test('setAndroidSetting airplane fails the change when the state cannot be read back', async () => { |
| 123 | + const service = connectivityService('disabled'); |
| 124 | + let changed = false; |
| 125 | + await withFakeAdb( |
| 126 | + (args) => { |
| 127 | + const flat = args.join(' '); |
| 128 | + if (flat === READ && changed) |
| 129 | + return { stdout: 'Unknown command: airplane-mode', exitCode: 255 }; |
| 130 | + if (flat === `${READ} enable`) changed = true; |
| 131 | + return service(args); |
| 132 | + }, |
| 133 | + async ({ device }) => { |
| 134 | + await assertRejectsAppError(() => setAndroidSetting(device, 'airplane', 'on'), { |
| 135 | + code: 'COMMAND_FAILED', |
| 136 | + message: /Failed to read Android airplane mode after changing it/, |
| 137 | + }); |
| 138 | + }, |
| 139 | + ); |
| 140 | +}); |
| 141 | + |
| 142 | +test('setAndroidSetting airplane keeps adb transport failures typed as command failures', async () => { |
| 143 | + await withFakeAdb( |
| 144 | + () => ({ stderr: 'error: device offline', exitCode: 1 }), |
| 145 | + async ({ calls, device }) => { |
| 146 | + await assertRejectsAppError(() => setAndroidSetting(device, 'airplane', 'on'), { |
| 147 | + code: 'COMMAND_FAILED', |
| 148 | + message: /Failed to read Android airplane mode/, |
| 149 | + hint: /adb reconnect/, |
| 150 | + }); |
| 151 | + assert.deepEqual( |
| 152 | + calls.map((args) => args.join(' ')), |
| 153 | + [READ], |
| 154 | + ); |
| 155 | + }, |
| 156 | + ); |
| 157 | +}); |
0 commit comments