|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { |
| 4 | + MOVING_TARGET_GATE_RECT_TOLERANCE_PX, |
| 5 | + MOVING_TARGET_GATE_SAMPLE_GAP_MS, |
| 6 | + MOVING_TARGET_GATE_WINDOW_MS, |
| 7 | + movingTargetGateDecision, |
| 8 | + movingTargetRectsStable, |
| 9 | + targetStillMovingHint, |
| 10 | +} from './moving-target-gate.ts'; |
| 11 | + |
| 12 | +// #3471: the two judgements the moving-target gate makes, isolated from both callers so the window |
| 13 | +// and the stability rule have exactly one reading. The runtime half (what a press does with these |
| 14 | +// answers) is `src/commands/interaction/runtime/moving-target-gate.ts`; the Layer-3 scenarios behind |
| 15 | +// the ADR 0011 `targetStability` cells drive the whole press. |
| 16 | + |
| 17 | +const RECT = { x: 100, y: 200, width: 120, height: 44 }; |
| 18 | + |
| 19 | +function shifted(over: Partial<typeof RECT> = {}) { |
| 20 | + return { ...RECT, ...over }; |
| 21 | +} |
| 22 | + |
| 23 | +// --- is a second observation owed? --- |
| 24 | + |
| 25 | +test('a session with no mutation clock is idle and owes nothing', () => { |
| 26 | + // `targetShownAtRest` is deliberately the disturbing kind here: with no clock there is nothing to |
| 27 | + // wait out, and an absent clock must not read as "a mutation just finished". |
| 28 | + const decision = movingTargetGateDecision({ |
| 29 | + baselineFinishedAtMs: undefined, |
| 30 | + targetShownAtRest: false, |
| 31 | + nowMs: 10_000, |
| 32 | + }); |
| 33 | + |
| 34 | + assert.deepEqual(decision, { kind: 'idle' }); |
| 35 | +}); |
| 36 | + |
| 37 | +test('a target the previous action left in place is answered without a re-observation', () => { |
| 38 | + const decision = movingTargetGateDecision({ |
| 39 | + baselineFinishedAtMs: 9_900, |
| 40 | + targetShownAtRest: true, |
| 41 | + nowMs: 10_000, |
| 42 | + }); |
| 43 | + |
| 44 | + assert.deepEqual(decision, { kind: 'stationary' }); |
| 45 | +}); |
| 46 | + |
| 47 | +test('the window is measured from the mutation END, so only what is LEFT of it is waited out', () => { |
| 48 | + // The action finished 400 ms ago; a full window of waiting would be a 900 ms pause on a press |
| 49 | + // whose surface has already been quiet for 400 ms. |
| 50 | + const decision = movingTargetGateDecision({ |
| 51 | + baselineFinishedAtMs: 9_600, |
| 52 | + targetShownAtRest: false, |
| 53 | + nowMs: 10_000, |
| 54 | + }); |
| 55 | + |
| 56 | + assert.deepEqual(decision, { kind: 'recheck', waitMs: 100 }); |
| 57 | +}); |
| 58 | + |
| 59 | +test('the declared window is the 500 ms transition budget, and its edge decides the branch', () => { |
| 60 | + // The edge is exclusive: at exactly the budget the resolved point already stands, because an |
| 61 | + // action that finished a full window ago has no claim on this press. |
| 62 | + assert.equal(MOVING_TARGET_GATE_WINDOW_MS, 500); |
| 63 | + const atEdge = movingTargetGateDecision({ |
| 64 | + baselineFinishedAtMs: 10_000 - MOVING_TARGET_GATE_WINDOW_MS, |
| 65 | + targetShownAtRest: false, |
| 66 | + nowMs: 10_000, |
| 67 | + }); |
| 68 | + const inside = movingTargetGateDecision({ |
| 69 | + baselineFinishedAtMs: 9_600, |
| 70 | + targetShownAtRest: false, |
| 71 | + nowMs: 10_000, |
| 72 | + }); |
| 73 | + const outside = movingTargetGateDecision({ |
| 74 | + baselineFinishedAtMs: 9_400, |
| 75 | + targetShownAtRest: false, |
| 76 | + nowMs: 10_000, |
| 77 | + }); |
| 78 | + |
| 79 | + assert.deepEqual(atEdge, { kind: 'not-recent' }); |
| 80 | + assert.equal(inside.kind, 'recheck'); |
| 81 | + assert.equal(outside.kind, 'not-recent'); |
| 82 | +}); |
| 83 | + |
| 84 | +test('inside the window, only a tree that shows the target at rest excuses the re-observation', () => { |
| 85 | + // One bit carries the whole screening verdict, because that is all this branch can use: absent, |
| 86 | + // ambiguous, and moved all mean the same thing here — the session cannot show where the target |
| 87 | + // came to rest, so it owes the look. |
| 88 | + for (const targetShownAtRest of [false, true] as const) { |
| 89 | + const decision = movingTargetGateDecision({ |
| 90 | + baselineFinishedAtMs: 9_950, |
| 91 | + targetShownAtRest, |
| 92 | + nowMs: 10_000, |
| 93 | + }); |
| 94 | + |
| 95 | + assert.equal( |
| 96 | + decision.kind, |
| 97 | + targetShownAtRest ? 'stationary' : 'recheck', |
| 98 | + `targetShownAtRest ${String(targetShownAtRest)}`, |
| 99 | + ); |
| 100 | + } |
| 101 | +}); |
| 102 | + |
| 103 | +test('the wait never drops below one sample gap, even as the window closes', () => { |
| 104 | + // Re-observing immediately would compare two captures the same animation frame could produce, |
| 105 | + // and agreement would then be a timing accident rather than the target having stopped. |
| 106 | + const decision = movingTargetGateDecision({ |
| 107 | + baselineFinishedAtMs: 9_520, |
| 108 | + targetShownAtRest: false, |
| 109 | + nowMs: 10_000, |
| 110 | + }); |
| 111 | + |
| 112 | + assert.deepEqual(decision, { kind: 'recheck', waitMs: MOVING_TARGET_GATE_SAMPLE_GAP_MS }); |
| 113 | +}); |
| 114 | + |
| 115 | +test('a clock skew ahead of the mutation does not produce a negative wait', () => { |
| 116 | + const decision = movingTargetGateDecision({ |
| 117 | + baselineFinishedAtMs: 10_100, |
| 118 | + targetShownAtRest: false, |
| 119 | + nowMs: 10_000, |
| 120 | + }); |
| 121 | + |
| 122 | + assert.ok(decision.kind === 'recheck'); |
| 123 | + assert.ok(decision.waitMs >= MOVING_TARGET_GATE_SAMPLE_GAP_MS); |
| 124 | +}); |
| 125 | + |
| 126 | +// --- has the target stopped? --- |
| 127 | + |
| 128 | +test('two observations at the same rect are a resting position', () => { |
| 129 | + assert.equal(movingTargetRectsStable(RECT, shifted()), true); |
| 130 | +}); |
| 131 | + |
| 132 | +test('capture rounding within the declared tolerance is not motion', () => { |
| 133 | + assert.equal(MOVING_TARGET_GATE_RECT_TOLERANCE_PX, 1); |
| 134 | + assert.equal(movingTargetRectsStable(RECT, shifted({ x: RECT.x + 1 })), true); |
| 135 | + assert.equal(movingTargetRectsStable(RECT, shifted({ height: RECT.height + 1 })), true); |
| 136 | +}); |
| 137 | + |
| 138 | +test('one axis moving is enough to still be moving', () => { |
| 139 | + // The slide-in case from the #3471 repro (ADB Android): a translate animation keeps width and |
| 140 | + // height constant, so only one axis says the target moved. |
| 141 | + assert.equal(movingTargetRectsStable(RECT, shifted({ x: RECT.x + 2 })), false); |
| 142 | + assert.equal(movingTargetRectsStable(RECT, shifted({ y: RECT.y + 2 })), false); |
| 143 | + assert.equal(movingTargetRectsStable(RECT, shifted({ width: RECT.width + 2 })), false); |
| 144 | + assert.equal(movingTargetRectsStable(RECT, shifted({ height: RECT.height + 2 })), false); |
| 145 | +}); |
| 146 | + |
| 147 | +test('an observation that could not measure the target never agrees with one that could', () => { |
| 148 | + // Absence is the surface having changed under the point, which is the exposure the gate exists |
| 149 | + // to catch — so it can never be the observation that certifies rest. |
| 150 | + assert.equal(movingTargetRectsStable(RECT, undefined), false); |
| 151 | + assert.equal(movingTargetRectsStable(undefined, RECT), false); |
| 152 | + assert.equal(movingTargetRectsStable(undefined, undefined), false); |
| 153 | + assert.equal(movingTargetRectsStable(RECT, shifted({ width: Number.NaN })), false); |
| 154 | +}); |
| 155 | + |
| 156 | +test('the stability comparison is symmetric', () => { |
| 157 | + assert.equal( |
| 158 | + movingTargetRectsStable(shifted({ y: 205 }), RECT), |
| 159 | + movingTargetRectsStable(RECT, shifted({ y: 205 })), |
| 160 | + ); |
| 161 | +}); |
| 162 | + |
| 163 | +// --- the refusal's published recovery --- |
| 164 | + |
| 165 | +test('a refused selector press names the wait that recovers it', () => { |
| 166 | + const hint = targetStillMovingHint({ selector: 'label="Slide target"' }); |
| 167 | + |
| 168 | + assert.match(hint, /wait 'label="Slide target"'/); |
| 169 | + assert.match(hint, /same selector/); |
| 170 | + assert.match(hint, /--settle/); |
| 171 | + assert.match(hint, /nothing was tapped/); |
| 172 | +}); |
| 173 | + |
| 174 | +test('a refused @ref press is sent to a selector, because the wait would retire its frame', () => { |
| 175 | + // ADR 0014: the capture a `wait` takes expires the ref frame, so telling the caller to retry the |
| 176 | + // same ref would trade a motion refusal for a stale-ref refusal. |
| 177 | + const hint = targetStillMovingHint({ selector: undefined }); |
| 178 | + |
| 179 | + assert.match(hint, /snapshot -i/); |
| 180 | + assert.match(hint, /selector/); |
| 181 | + assert.match(hint, /ref frame/); |
| 182 | + assert.doesNotMatch(hint, /wait '/); |
| 183 | +}); |
0 commit comments