|
| 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 keys on the capture the settle loop PERFORMS (#3470 |
| 12 | +// follow-up), because that read is the one the window prices: the loop holds its quiet window open |
| 13 | +// across captures of its own. The pre-action baseline cannot answer this — on the ref route it is |
| 14 | +// the stored ref frame, on the generic route the session's stored tree, and either can be several |
| 15 | +// commands stale and served by a producer other than the one settle then reads through. A caller |
| 16 | +// that asks for a window with `--settle-quiet` always outranks the default. |
| 17 | +// |
| 18 | +// Budgets are injected (fake clock) — no real waiting. |
| 19 | + |
| 20 | +function withProvenance<P extends SnapshotProvenance>(state: SnapshotState, provenance: P) { |
| 21 | + return { ...state, ...provenance }; |
| 22 | +} |
| 23 | + |
| 24 | +const BRIDGE = { backend: 'xctest', producer: 'simulator-ax-bridge' } as const; |
| 25 | +const RUNNER = { backend: 'xctest', producer: 'apple-runner' } as const; |
| 26 | + |
| 27 | +/** |
| 28 | + * A press whose baseline and whose loop captures come from declared capture paths, so the two can |
| 29 | + * be made to agree or to disagree. |
| 30 | + */ |
| 31 | +function pressWithCapturePaths(params: { |
| 32 | + baseline?: SnapshotProvenance; |
| 33 | + loop?: SnapshotProvenance; |
| 34 | + settle?: { quietMs?: number; timeoutMs?: number }; |
| 35 | +}) { |
| 36 | + const baseline = params.baseline |
| 37 | + ? withProvenance(buttonSnapshot(), params.baseline) |
| 38 | + : buttonSnapshot(); |
| 39 | + let captures = 0; |
| 40 | + const device = createSettleDevice({ |
| 41 | + stored: baseline, |
| 42 | + captureSnapshot: () => { |
| 43 | + captures += 1; |
| 44 | + // A ref press captures nothing before the action, so every call is the loop's own sample. |
| 45 | + const settled = welcomeSnapshot(); |
| 46 | + return { |
| 47 | + snapshot: |
| 48 | + captures === 1 ? baseline : params.loop ? withProvenance(settled, params.loop) : settled, |
| 49 | + }; |
| 50 | + }, |
| 51 | + }); |
| 52 | + return device.interactions.press(ref('@e1'), { session: 'default', settle: params.settle ?? {} }); |
| 53 | +} |
| 54 | + |
| 55 | +test('a settle the simulator snapshot bridge performs uses the shorter default window', async () => { |
| 56 | + const result = await pressWithCapturePaths({ loop: BRIDGE }); |
| 57 | + |
| 58 | + const settle = result.settle; |
| 59 | + assert.ok(settle); |
| 60 | + assert.equal(settle.settled, true); |
| 61 | + assert.equal(settle.quietMs, 300); |
| 62 | +}); |
| 63 | + |
| 64 | +test('an explicit --settle-quiet outranks the per-capture default window', async () => { |
| 65 | + const result = await pressWithCapturePaths({ loop: BRIDGE, settle: { quietMs: 700 } }); |
| 66 | + |
| 67 | + const settle = result.settle; |
| 68 | + assert.ok(settle); |
| 69 | + assert.equal(settle.quietMs, 700); |
| 70 | +}); |
| 71 | + |
| 72 | +// Each of these is a read the 300 ms default was NOT measured on, so shortening its window would be |
| 73 | +// an unmeasured change of behaviour rather than the measured one. The Apple runner shares the |
| 74 | +// `xctest` CHANNEL with the bridge, which is why the rule keys on the producer. |
| 75 | +const UNMEASURED_CAPTURES: Array<[string, SnapshotProvenance?]> = [ |
| 76 | + ['the Apple runner tree read (same channel, different producer)', RUNNER], |
| 77 | + ['an Appium-sourced tree on the same channel', { backend: 'xctest', producer: 'appium-source' }], |
| 78 | + ['the Android helper', { backend: 'android', producer: 'android-uiautomator' }], |
| 79 | + ['a capture that declares no provenance at all', undefined], |
| 80 | +]; |
| 81 | + |
| 82 | +for (const [label, provenance] of UNMEASURED_CAPTURES) { |
| 83 | + test(`${label} keeps the 500ms default window`, async () => { |
| 84 | + const result = await pressWithCapturePaths({ loop: provenance }); |
| 85 | + |
| 86 | + const settle = result.settle; |
| 87 | + assert.ok(settle); |
| 88 | + assert.equal(settle.quietMs, 500); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +test('a bridge read during settle earns the shorter window even from a runner-issued ref frame', async () => { |
| 93 | + // The ref frame the action was authorized against came from the runner, so keying the window on |
| 94 | + // the baseline would hold the unmeasured 500 ms against captures the bridge answered. |
| 95 | + const result = await pressWithCapturePaths({ baseline: RUNNER, loop: BRIDGE }); |
| 96 | + |
| 97 | + const settle = result.settle; |
| 98 | + assert.ok(settle); |
| 99 | + assert.equal(settle.quietMs, 300); |
| 100 | +}); |
| 101 | + |
| 102 | +test('a runner read during settle keeps the 500ms window despite a bridge-issued baseline', async () => { |
| 103 | + // The unsafe direction of the same mismatch: holding 300 ms for a read priced at 500 ms would |
| 104 | + // settle on a window the measurement never covered. |
| 105 | + const result = await pressWithCapturePaths({ baseline: BRIDGE, loop: RUNNER }); |
| 106 | + |
| 107 | + const settle = result.settle; |
| 108 | + assert.ok(settle); |
| 109 | + assert.equal(settle.quietMs, 500); |
| 110 | +}); |
| 111 | + |
| 112 | +test('the shorter default window never shortens a broad-transition confirmation', async () => { |
| 113 | + // The trap this layer owes layer 1: a 300 ms default sits under the 1.5 s floor, so a rule that |
| 114 | + // armed on `requestedQuietMs >= DEFAULT_STABLE_QUIET_MS` would silently switch the #1831 |
| 115 | + // confirmation off for every default settle the bridge answers. Here the pre-action tree is the |
| 116 | + // tiny-baseline shape, so the confirmation owns the wait and the 300 ms default still holds its |
| 117 | + // 1.5 s floor (#3470). |
| 118 | + const sparseBaseline = withProvenance(buttonSnapshot(), BRIDGE); |
| 119 | + let captures = 0; |
| 120 | + const device = createSettleDevice({ |
| 121 | + stored: sparseBaseline, |
| 122 | + captureSnapshot: () => { |
| 123 | + captures += 1; |
| 124 | + return { |
| 125 | + snapshot: captures === 1 ? sparseBaseline : withProvenance(welcomeSnapshot(), BRIDGE), |
| 126 | + }; |
| 127 | + }, |
| 128 | + }); |
| 129 | + |
| 130 | + const result = await device.interactions.press(ref('@e1'), { |
| 131 | + session: 'default', |
| 132 | + settle: { timeoutMs: 5_000 }, |
| 133 | + }); |
| 134 | + |
| 135 | + const settle = result.settle; |
| 136 | + assert.ok(settle); |
| 137 | + assert.equal(settle.settled, true); |
| 138 | + assert.equal(settle.quietMs, 300); |
| 139 | + assert.ok( |
| 140 | + settle.waitedMs >= 1_500, |
| 141 | + `confirmation did not outlast the shorter default: waitedMs=${settle.waitedMs}`, |
| 142 | + ); |
| 143 | +}); |
0 commit comments