|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import type { SnapshotProvenance, SnapshotState } from '@agent-device/kernel/snapshot'; |
| 4 | +import { ref } from './selector-read-utils.ts'; |
| 5 | +import { |
| 6 | + buttonSnapshot, |
| 7 | + createSettleDevice, |
| 8 | + welcomeSnapshot, |
| 9 | +} from './__tests__/settle-device-fixtures.ts'; |
| 10 | + |
| 11 | +// The `--settle` DEFAULT quiet window comes from the capture the action was performed against |
| 12 | +// (#3470 follow-up): the iOS simulator snapshot bridge goes quiet well inside the general 500 ms |
| 13 | +// default, and only that read carries the measurement. A window the caller asks for with |
| 14 | +// `--settle-quiet` always outranks it, and every other capture — the Apple runner's own tree read, |
| 15 | +// the Android helper, a capture that arrived without provenance — keeps the general default. |
| 16 | +// |
| 17 | +// Budgets are injected (fake clock) — no real waiting. |
| 18 | + |
| 19 | +function withProvenance<P extends SnapshotProvenance>(state: SnapshotState, provenance: P) { |
| 20 | + return { ...state, ...provenance }; |
| 21 | +} |
| 22 | + |
| 23 | +function pressWithBaseline( |
| 24 | + baseline: SnapshotState, |
| 25 | + settle?: { quietMs?: number; timeoutMs?: number }, |
| 26 | +) { |
| 27 | + let captures = 0; |
| 28 | + const device = createSettleDevice({ |
| 29 | + stored: baseline, |
| 30 | + captureSnapshot: () => { |
| 31 | + captures += 1; |
| 32 | + // Capture 1 is the selector resolution (the pre-action tree); captures 2+ are the loop. |
| 33 | + return { snapshot: captures === 1 ? baseline : welcomeSnapshot() }; |
| 34 | + }, |
| 35 | + }); |
| 36 | + return device.interactions.press(ref('@e1'), { session: 'default', settle: settle ?? {} }); |
| 37 | +} |
| 38 | + |
| 39 | +const BRIDGE = { backend: 'xctest', producer: 'simulator-ax-bridge' } as const; |
| 40 | + |
| 41 | +test('a simulator snapshot-bridge baseline settles on the shorter default window', async () => { |
| 42 | + const result = await pressWithBaseline(withProvenance(buttonSnapshot(), BRIDGE)); |
| 43 | + |
| 44 | + const settle = result.settle; |
| 45 | + assert.ok(settle); |
| 46 | + assert.equal(settle.settled, true); |
| 47 | + assert.equal(settle.quietMs, 300); |
| 48 | +}); |
| 49 | + |
| 50 | +test('an explicit --settle-quiet outranks the per-capture default window', async () => { |
| 51 | + const result = await pressWithBaseline(withProvenance(buttonSnapshot(), BRIDGE), { |
| 52 | + quietMs: 700, |
| 53 | + timeoutMs: 5_000, |
| 54 | + }); |
| 55 | + |
| 56 | + const settle = result.settle; |
| 57 | + assert.ok(settle); |
| 58 | + assert.equal(settle.quietMs, 700); |
| 59 | +}); |
| 60 | + |
| 61 | +// Each of these is a capture path the 300 ms default was NOT measured on, so shortening it there |
| 62 | +// would be an unmeasured change of behaviour rather than the measured one. The Apple runner shares |
| 63 | +// the `xctest` CHANNEL with the bridge, which is why the rule keys on the producer. |
| 64 | +const UNMEASURED_CAPTURES: Array<[string, SnapshotProvenance?]> = [ |
| 65 | + [ |
| 66 | + 'the Apple runner tree read (same channel, different producer)', |
| 67 | + { backend: 'xctest', producer: 'apple-runner' }, |
| 68 | + ], |
| 69 | + ['an Appium-sourced tree on the same channel', { backend: 'xctest', producer: 'appium-source' }], |
| 70 | + ['the Android helper', { backend: 'android', producer: 'android-uiautomator' }], |
| 71 | + ['a capture that declares no provenance at all', undefined], |
| 72 | +]; |
| 73 | + |
| 74 | +for (const [label, provenance] of UNMEASURED_CAPTURES) { |
| 75 | + test(`${label} keeps the 500ms default window`, async () => { |
| 76 | + const result = await pressWithBaseline( |
| 77 | + provenance ? withProvenance(buttonSnapshot(), provenance) : buttonSnapshot(), |
| 78 | + ); |
| 79 | + |
| 80 | + const settle = result.settle; |
| 81 | + assert.ok(settle); |
| 82 | + assert.equal(settle.quietMs, 500); |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +test('the shorter default window never shortens a presented-surface dismissal confirmation', async () => { |
| 87 | + // The pre-action tree is a presented surface (a tiny tree over app content), so the dismissal |
| 88 | + // confirmation owns the wait: the 300 ms default must still hold for its 1.5 s floor (#3470). |
| 89 | + let captures = 0; |
| 90 | + const presented = withProvenance(buttonSnapshot(), BRIDGE); |
| 91 | + const device = createSettleDevice({ |
| 92 | + stored: presented, |
| 93 | + captureSnapshot: () => { |
| 94 | + captures += 1; |
| 95 | + return { snapshot: captures === 1 ? presented : welcomeSnapshot() }; |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + const result = await device.interactions.press(ref('@e1'), { |
| 100 | + session: 'default', |
| 101 | + settle: { timeoutMs: 5_000 }, |
| 102 | + }); |
| 103 | + |
| 104 | + const settle = result.settle; |
| 105 | + assert.ok(settle); |
| 106 | + assert.equal(settle.settled, true); |
| 107 | + assert.equal(settle.quietMs, 300); |
| 108 | + assert.ok( |
| 109 | + settle.waitedMs >= 1_500, |
| 110 | + `dismissal confirmation did not outlast the shorter default: waitedMs=${settle.waitedMs}`, |
| 111 | + ); |
| 112 | +}); |
0 commit comments