|
1 | | -import { test } from 'vitest'; |
| 1 | +import { test, vi } from 'vitest'; |
2 | 2 | import assert from 'node:assert/strict'; |
3 | 3 | import { SETTINGS_APP_NOT_PACKAGE_REASON } from '@agent-device/contracts/settings'; |
4 | 4 | import { TEXT_SIZE_CATEGORIES } from '@agent-device/contracts/settings-catalog'; |
5 | | -import { PRE_DISPATCH_REFUSAL_REASONS } from '@agent-device/kernel/errors'; |
| 5 | +import { AppError, PRE_DISPATCH_REFUSAL_REASONS } from '@agent-device/kernel/errors'; |
6 | 6 | import { readAndroidSetting, setAndroidSetting } from '../settings.ts'; |
7 | 7 | import { ANDROID_EMULATOR } from './test-utils/device-fixtures.ts'; |
8 | 8 | import { assertRejectsAppError } from './test-utils/app-error.ts'; |
@@ -490,3 +490,165 @@ test('readAndroidSetting text-size fails on a multiplier it cannot order', async |
490 | 490 | }); |
491 | 491 | } |
492 | 492 | }); |
| 493 | + |
| 494 | +// --- settings mobile-data: `svc data` with a read-back of the global `mobile_data` row --- |
| 495 | + |
| 496 | +const GET_MOBILE_DATA = 'shell settings get global mobile_data'; |
| 497 | + |
| 498 | +/** |
| 499 | + * A single-SIM telephony service holding `held` in `mobile_data` (`null` when absent). It stores a |
| 500 | + * `svc data` write after `visibleAfterReads` further reads, or never with `honorWrites: false`. |
| 501 | + */ |
| 502 | +function telephonyService( |
| 503 | + held: string, |
| 504 | + options: { honorWrites?: boolean; visibleAfterReads?: number } = {}, |
| 505 | +): FakeAdbScript { |
| 506 | + let stored = held; |
| 507 | + let pending: string | undefined; |
| 508 | + let readsUntilVisible = 0; |
| 509 | + return (args) => { |
| 510 | + const flat = args.join(' '); |
| 511 | + if (flat === GET_MOBILE_DATA) { |
| 512 | + if (pending !== undefined && readsUntilVisible-- <= 0) { |
| 513 | + stored = pending; |
| 514 | + pending = undefined; |
| 515 | + } |
| 516 | + return stored; |
| 517 | + } |
| 518 | + if (flat === 'shell svc data enable' || flat === 'shell svc data disable') { |
| 519 | + if (options.honorWrites !== false) { |
| 520 | + pending = flat.endsWith('enable') ? '1' : '0'; |
| 521 | + readsUntilVisible = options.visibleAfterReads ?? 0; |
| 522 | + } |
| 523 | + return ''; |
| 524 | + } |
| 525 | + return { stderr: `unexpected args: ${flat}`, exitCode: 1 }; |
| 526 | + }; |
| 527 | +} |
| 528 | + |
| 529 | +/** Advances the faked wall clock past the settle budget on every read of the row. */ |
| 530 | +function expiringOnRead(script: FakeAdbScript): FakeAdbScript { |
| 531 | + return (args, options) => { |
| 532 | + if (args.join(' ') === GET_MOBILE_DATA) vi.setSystemTime(Date.now() + 2_500); |
| 533 | + return script(args, options); |
| 534 | + }; |
| 535 | +} |
| 536 | + |
| 537 | +async function withFakeWallClock(run: () => Promise<void>): Promise<void> { |
| 538 | + vi.useFakeTimers({ now: 0, toFake: ['Date'] }); |
| 539 | + try { |
| 540 | + await run(); |
| 541 | + } finally { |
| 542 | + vi.useRealTimers(); |
| 543 | + } |
| 544 | +} |
| 545 | + |
| 546 | +const flatCalls = (calls: (readonly string[])[]) => calls.map((args) => args.join(' ')); |
| 547 | + |
| 548 | +test('setAndroidSetting mobile-data switches through svc data and reports the stored state', async () => { |
| 549 | + for (const { state, held, verb, mobileData, visibleAfterReads } of [ |
| 550 | + { state: 'off', held: '1', verb: 'disable', mobileData: 'disabled', visibleAfterReads: 0 }, |
| 551 | + { state: 'on', held: '0', verb: 'enable', mobileData: 'enabled', visibleAfterReads: 0 }, |
| 552 | + // The telephony service can store the switch after `svc data` returns, so the row is read again. |
| 553 | + { state: 'on', held: 'null', verb: 'enable', mobileData: 'enabled', visibleAfterReads: 1 }, |
| 554 | + ]) { |
| 555 | + const signal = new AbortController().signal; |
| 556 | + const readOptions: { signal?: AbortSignal; timeoutMs?: number }[] = []; |
| 557 | + const script = telephonyService(held, { visibleAfterReads }); |
| 558 | + await withFakeAdb( |
| 559 | + (args, options) => { |
| 560 | + if (args.join(' ') === GET_MOBILE_DATA) readOptions.push(options ?? {}); |
| 561 | + return script(args, options); |
| 562 | + }, |
| 563 | + async ({ calls, device }) => { |
| 564 | + const result = await setAndroidSetting( |
| 565 | + device, |
| 566 | + 'mobile-data', |
| 567 | + state, |
| 568 | + undefined, |
| 569 | + undefined, |
| 570 | + signal, |
| 571 | + ); |
| 572 | + assert.deepEqual(result, { mobileData }); |
| 573 | + assert.deepEqual(flatCalls(calls), [ |
| 574 | + `shell svc data ${verb}`, |
| 575 | + ...Array(visibleAfterReads + 1).fill(GET_MOBILE_DATA), |
| 576 | + ]); |
| 577 | + }, |
| 578 | + ); |
| 579 | + for (const options of readOptions) { |
| 580 | + assert.equal(options.signal, signal); |
| 581 | + assert.ok(options.timeoutMs !== undefined && options.timeoutMs <= 2000); |
| 582 | + } |
| 583 | + } |
| 584 | +}); |
| 585 | + |
| 586 | +test('setAndroidSetting mobile-data fails on the last read when the switch never settles', async () => { |
| 587 | + for (const { held, reason, mobileData } of [ |
| 588 | + { held: '0', reason: 'mobile-data-not-applied', mobileData: 'disabled' }, |
| 589 | + { held: 'null', reason: 'mobile-data-unreadable', mobileData: undefined }, |
| 590 | + ]) { |
| 591 | + await withFakeWallClock(async () => { |
| 592 | + const script = expiringOnRead(telephonyService(held, { honorWrites: false })); |
| 593 | + await withFakeAdb(script, async ({ device }) => { |
| 594 | + await assert.rejects(setAndroidSetting(device, 'mobile-data', 'on'), (error: unknown) => { |
| 595 | + assert.ok(error instanceof AppError); |
| 596 | + assert.equal(error.code, 'COMMAND_FAILED'); |
| 597 | + assert.equal(error.details?.reason, reason); |
| 598 | + assert.equal(error.details?.storedValue, held); |
| 599 | + assert.equal(error.details?.mobileData, mobileData); |
| 600 | + assert.equal(error.details?.dispatched, 'unknown'); |
| 601 | + return true; |
| 602 | + }); |
| 603 | + }); |
| 604 | + }); |
| 605 | + } |
| 606 | +}); |
| 607 | + |
| 608 | +test('setAndroidSetting mobile-data discloses a possible switch when svc data fails', async () => { |
| 609 | + await withFakeAdb( |
| 610 | + (args) => |
| 611 | + args.join(' ') === 'shell svc data enable' |
| 612 | + ? { stderr: "cmd: Can't find service: phone", exitCode: 20 } |
| 613 | + : undefined, |
| 614 | + async ({ calls, device }) => { |
| 615 | + await assertRejectsAppError(() => setAndroidSetting(device, 'mobile-data', 'on'), { |
| 616 | + code: 'COMMAND_FAILED', |
| 617 | + dispatched: 'unknown', |
| 618 | + }); |
| 619 | + assert.deepEqual(flatCalls(calls), ['shell svc data enable']); |
| 620 | + }, |
| 621 | + ); |
| 622 | +}); |
| 623 | + |
| 624 | +test('setAndroidSetting mobile-data runs nothing for a bad state or a request cancelled up front', async () => { |
| 625 | + const controller = new AbortController(); |
| 626 | + controller.abort(); |
| 627 | + await withFakeAdb(telephonyService('1'), async ({ calls, device }) => { |
| 628 | + await assertRejectsAppError(() => setAndroidSetting(device, 'mobile-data', 'cellular'), { |
| 629 | + code: 'INVALID_ARGS', |
| 630 | + message: /Invalid setting state: cellular/, |
| 631 | + }); |
| 632 | + await assert.rejects( |
| 633 | + setAndroidSetting(device, 'mobile-data', 'off', undefined, undefined, controller.signal), |
| 634 | + ); |
| 635 | + assert.deepEqual(calls, []); |
| 636 | + }); |
| 637 | +}); |
| 638 | + |
| 639 | +test('setAndroidSetting mobile-data stops sampling when the request is cancelled', async () => { |
| 640 | + const controller = new AbortController(); |
| 641 | + const script = telephonyService('0', { honorWrites: false }); |
| 642 | + await withFakeAdb( |
| 643 | + (args, options) => { |
| 644 | + if (args.join(' ') === GET_MOBILE_DATA) controller.abort(); |
| 645 | + return script(args, options); |
| 646 | + }, |
| 647 | + async ({ calls, device }) => { |
| 648 | + await assert.rejects( |
| 649 | + setAndroidSetting(device, 'mobile-data', 'on', undefined, undefined, controller.signal), |
| 650 | + ); |
| 651 | + assert.equal(flatCalls(calls).filter((call) => call === GET_MOBILE_DATA).length, 1); |
| 652 | + }, |
| 653 | + ); |
| 654 | +}); |
0 commit comments