auv-scan 0.0.31

Scroll scanning interaction implementation for AUV
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use std::time::Duration;

use auv_driver::{
  Capture, DisturbanceLevel, InputActionResult, InputAttempt, InputDeliveryPath, RecognizedText, Rect, Scroll, TextRecognition,
};
use image::{Rgba, RgbaImage};

use super::*;

/// A vertical list of distinct rows. `content` grows by `lazy_batch` rows once
/// the viewport has waited at the bottom `lazy_after_waits` times.
struct FakeList {
  position: i64,
  content: i64,
  viewport: i64,
  lazy_batch: i64,
  lazy_after_waits: u32,
  bottom_waits: u32,
  text_row: Option<i64>,
  scrolls: u32,
  recognitions: u32,
  /// Backing pixels per point for native captures.
  scale: u32,
  resolutions: Vec<CaptureResolution>,
}

impl FakeList {
  fn new(content: i64) -> Self {
    Self {
      position: 0,
      content,
      viewport: 60,
      lazy_batch: 0,
      lazy_after_waits: 0,
      bottom_waits: 0,
      text_row: None,
      scrolls: 0,
      recognitions: 0,
      scale: 1,
      resolutions: Vec::new(),
    }
  }

  fn max_position(&self) -> i64 {
    (self.content - self.viewport).max(0)
  }
}

fn action() -> InputActionResult {
  InputActionResult {
    selected_path: InputDeliveryPath::WindowTargetedWheel,
    attempts: vec![InputAttempt::success(
      InputDeliveryPath::WindowTargetedWheel,
    )],
    verified: false,
    mouse_disturbance: DisturbanceLevel::None,
    focus_disturbance: DisturbanceLevel::None,
    clipboard_disturbance: DisturbanceLevel::None,
  }
}

impl ScrollUntilSurface for FakeList {
  fn scroll(&mut self, step: &ScrollUntilStep) -> DriverResult<(InputActionResult, Scroll)> {
    self.scrolls += 1;
    let delta = step.delta().delta_y as i64;
    self.position = (self.position + delta).clamp(0, self.max_position());
    Ok((action(), step.delta()))
  }

  fn capture(&mut self, resolution: CaptureResolution) -> DriverResult<Capture> {
    self.resolutions.push(resolution);
    let position = self.position;
    let scale = if resolution == CaptureResolution::Native {
      self.scale
    } else {
      1
    };
    // Each logical row spans `scale` backing rows.
    let image = RgbaImage::from_fn(40 * scale, self.viewport as u32 * scale, |_, y| {
      let value = (((position + i64::from(y / scale)) * 37).rem_euclid(251)) as u8;
      Rgba([value, value.wrapping_mul(3), value.wrapping_add(90), 255])
    });
    Ok(Capture {
      origin: None,
      image,
      // A window at (100, 200) on screen, so text matches must be offset.
      bounds: Rect::new(100.0, 200.0, 40.0, self.viewport as f64),
      scale_factor: f64::from(scale),
      backend: "fake".to_string(),
      fallback_reason: None,
    })
  }

  fn recognize_text(&mut self, _: &Capture) -> DriverResult<TextRecognition> {
    self.recognitions += 1;
    let visible = self.text_row.filter(|row| (self.position..self.position + self.viewport).contains(row));
    // Like the drivers, bounds are in the capture's screen space: the window
    // sits at (100, 200) on screen.
    let mut regions = vec![RecognizedText {
      text: format!("row at {}", self.position),
      bounds: Rect::new(100.0, 200.0, 40.0, 1.0),
      confidence: None,
    }];
    regions.extend(visible.map(|row| RecognizedText {
      text: "TARGET ROW".to_string(),
      bounds: Rect::new(100.0, 200.0 + (row - self.position) as f64, 40.0, 1.0),
      confidence: None,
    }));
    Ok(TextRecognition {
      origin: None,
      text: String::new(),
      regions,
    })
  }

  fn wait(&mut self, _: Duration) -> DriverResult<()> {
    if self.position == self.max_position() && self.lazy_batch > 0 {
      self.bottom_waits += 1;
      if self.bottom_waits >= self.lazy_after_waits {
        self.content += self.lazy_batch;
        self.lazy_batch = 0;
      }
    }
    Ok(())
  }
}

fn request(condition: ScrollUntilCondition) -> ScrollUntilRequest {
  ScrollUntilRequest {
    step: ScrollUntilStep::Instant {
      delta: Scroll::new(0.0, 50.0),
    },
    condition,
    max_steps: 50,
    settle: Duration::ZERO,
    no_motion_confirmations: 2,
    motion_region: None,
    observe: ScrollUntilObserve::default(),
  }
}

fn run(list: &mut FakeList, request: &ScrollUntilRequest) -> DriverResult<ScrollUntilResult> {
  scroll_until(list, request, &mut |_| Ok(ScrollUntilDecision::Continue))
}

#[test]
fn end_stops_after_consecutive_no_motion_at_the_bottom() {
  let mut list = FakeList::new(260);
  let mut streaks = Vec::new();
  let result = scroll_until(&mut list, &request(ScrollUntilCondition::End), &mut |observation| {
    streaks.push(observation.no_motion_streak);
    Ok(ScrollUntilDecision::Continue)
  })
  .unwrap();
  assert_eq!(result.reason, ScrollUntilStopReason::EndByNoVisualProgress);
  assert_eq!(list.position, 200);
  // Four steps reach 200; two more confirm no motion.
  assert_eq!(result.steps, 6);
  assert_eq!(streaks, [0, 0, 0, 0, 0, 1, 2]);
  assert_eq!(result.action.unwrap().selected_path, InputDeliveryPath::WindowTargetedWheel);
}

#[test]
fn end_keeps_scrolling_when_lazy_content_arrives_during_settle() {
  let mut list = FakeList::new(260);
  list.lazy_batch = 300;
  list.lazy_after_waits = 1;
  let result = run(&mut list, &request(ScrollUntilCondition::End)).unwrap();
  assert_eq!(result.reason, ScrollUntilStopReason::EndByNoVisualProgress);
  assert_eq!(list.content, 560);
  assert_eq!(list.position, 500);
}

#[test]
fn text_condition_stops_as_soon_as_the_query_is_visible() {
  let mut list = FakeList::new(2_000);
  list.text_row = Some(420);
  let result = scroll_until(
    &mut list,
    &request(ScrollUntilCondition::TextVisible {
      query: "Target".to_string(),
    }),
    &mut |_| Ok(ScrollUntilDecision::Continue),
  )
  .unwrap();
  assert_eq!(result.reason, ScrollUntilStopReason::TextVisible);
  assert!(list.position <= 420 && 420 < list.position + 60, "{}", list.position);
  let matched = result.text_match.unwrap();
  assert_eq!(matched.text, "TARGET ROW");
  // ROOT CAUSE:
  //
  // If the window was not at the screen origin, the match was offset by the
  // window origin twice: the drivers already return OCR bounds in screen
  // space, and `text_match` added `capture.bounds.origin` again. The fake
  // returned offsets instead of screen bounds, which hid it.
  //
  // The fix uses the driver's screen bounds as they are.
  assert_eq!(matched.bounds, Rect::new(100.0, 200.0 + (420 - list.position) as f64, 40.0, 1.0));
}

#[test]
fn text_already_visible_needs_no_step() {
  let mut list = FakeList::new(2_000);
  list.text_row = Some(10);
  let result = scroll_until(
    &mut list,
    &request(ScrollUntilCondition::TextVisible {
      query: "Target".to_string(),
    }),
    &mut |_| Ok(ScrollUntilDecision::Continue),
  )
  .unwrap();
  assert_eq!((result.reason, result.steps, list.scrolls), (ScrollUntilStopReason::TextVisible, 0, 0));
  assert!(result.action.is_none());
}

#[test]
fn text_condition_stops_at_the_end_when_the_query_never_appears() {
  let mut list = FakeList::new(260);
  let result = scroll_until(
    &mut list,
    &request(ScrollUntilCondition::TextVisible {
      query: "Missing".to_string(),
    }),
    &mut |_| Ok(ScrollUntilDecision::Continue),
  )
  .unwrap();
  assert_eq!(result.reason, ScrollUntilStopReason::EndByNoVisualProgress);
  assert!(result.text_match.is_none());
}

#[test]
fn budget_exhaustion_is_reported_separately_from_the_end() {
  let mut list = FakeList::new(10_000);
  let mut short = request(ScrollUntilCondition::End);
  short.max_steps = 3;
  let result = run(&mut list, &short).unwrap();
  assert_eq!((result.reason, result.steps), (ScrollUntilStopReason::BudgetExhausted, 3));
  assert_eq!(result.delivered, Scroll::new(0.0, 150.0));
}

#[test]
fn invalid_requests_are_rejected_before_any_input() {
  let base = request(ScrollUntilCondition::End);
  let mut cases = Vec::new();
  let mut diagonal = base.clone();
  diagonal.step = ScrollUntilStep::Instant {
    delta: Scroll::new(10.0, 10.0),
  };
  cases.push(diagonal);
  let mut zero = base.clone();
  zero.step = ScrollUntilStep::Instant {
    delta: Scroll::new(0.0, 0.0),
  };
  cases.push(zero);
  let mut budget = base.clone();
  budget.max_steps = 0;
  cases.push(budget);
  let mut confirmations = base.clone();
  confirmations.no_motion_confirmations = 0;
  cases.push(confirmations);
  let mut region = base.clone();
  region.motion_region = Some(auv_driver::RatioRect::new(0.5, 0.0, 0.6, 1.0));
  cases.push(region);
  cases.push(request(ScrollUntilCondition::TextVisible {
    query: "  ".to_string(),
  }));
  for case in cases {
    let mut list = FakeList::new(260);
    assert!(matches!(run(&mut list, &case), Err(DriverError::InvalidInput { .. })), "{case:?}");
    assert_eq!(list.scrolls, 0);
  }
}

#[test]
fn observer_sees_every_observation_with_text_by_default() {
  let mut list = FakeList::new(260);
  let mut seen = Vec::new();
  scroll_until(&mut list, &request(ScrollUntilCondition::End), &mut |observation| {
    assert!(observation.text.is_some(), "{observation:?}");
    seen.push((observation.steps, observation.motion.is_some(), observation.stop));
    Ok(ScrollUntilDecision::Continue)
  })
  .unwrap();
  assert_eq!(seen.first(), Some(&(0, false, None)), "initial observation has no motion yet");
  assert_eq!(seen.len(), 7);
  assert_eq!(seen.last(), Some(&(6, true, Some(ScrollUntilStopReason::EndByNoVisualProgress))));
}

#[test]
fn observer_stop_ends_the_loop_as_predicate_satisfied() {
  let mut list = FakeList::new(2_000);
  let result = scroll_until(&mut list, &request(ScrollUntilCondition::End), &mut |observation| {
    let text = observation.text.expect("text is observed by default");
    Ok(if text.regions[0].text == "row at 150" {
      ScrollUntilDecision::Stop
    } else {
      ScrollUntilDecision::Continue
    })
  })
  .unwrap();
  assert_eq!((result.reason, result.steps, list.position), (ScrollUntilStopReason::PredicateSatisfied, 3, 150));
}

#[test]
fn observer_can_stop_before_the_first_step() {
  let mut list = FakeList::new(2_000);
  let result = scroll_until(&mut list, &request(ScrollUntilCondition::End), &mut |_| Ok(ScrollUntilDecision::Stop)).unwrap();
  assert_eq!((result.reason, result.steps, list.scrolls), (ScrollUntilStopReason::PredicateSatisfied, 0, 0));
  assert!(result.action.is_none());
}

#[test]
fn built_in_stop_wins_over_the_observer_decision() {
  let mut list = FakeList::new(2_000);
  list.text_row = Some(10);
  let result = scroll_until(
    &mut list,
    &request(ScrollUntilCondition::TextVisible {
      query: "target".to_string(),
    }),
    &mut |observation| {
      assert_eq!(observation.stop, Some(ScrollUntilStopReason::TextVisible));
      Ok(ScrollUntilDecision::Stop)
    },
  )
  .unwrap();
  assert_eq!(result.reason, ScrollUntilStopReason::TextVisible);
}

#[test]
fn observer_errors_abort_the_loop() {
  let mut list = FakeList::new(2_000);
  let error = scroll_until(&mut list, &request(ScrollUntilCondition::End), &mut |observation| {
    if observation.steps == 2 {
      Err(DriverError::InvalidInput {
        message: "client went away".to_string(),
      })
    } else {
      Ok(ScrollUntilDecision::Continue)
    }
  })
  .unwrap_err();
  assert!(error.to_string().contains("client went away"), "{error}");
  assert_eq!(list.scrolls, 2);
}

#[test]
fn opted_out_observations_skip_recognition() {
  let mut list = FakeList::new(260);
  let mut end = request(ScrollUntilCondition::End);
  end.observe = ScrollUntilObserve { text: false };
  scroll_until(&mut list, &end, &mut |observation| {
    assert!(observation.text.is_none());
    Ok(ScrollUntilDecision::Continue)
  })
  .unwrap();
  assert_eq!(list.recognitions, 0);
}

#[test]
fn text_condition_still_recognizes_when_text_is_opted_out() {
  let mut list = FakeList::new(2_000);
  list.text_row = Some(420);
  let mut find = request(ScrollUntilCondition::TextVisible {
    query: "target".to_string(),
  });
  find.observe.text = false;
  let result = scroll_until(&mut list, &find, &mut |observation| {
    assert!(observation.text.is_none());
    Ok(ScrollUntilDecision::Continue)
  })
  .unwrap();
  assert_eq!(result.reason, ScrollUntilStopReason::TextVisible);
  assert!(list.recognitions > 0);
}

#[test]
fn motion_only_loops_capture_at_logical_resolution() {
  let mut list = FakeList::new(260);
  let mut end = request(ScrollUntilCondition::End);
  end.observe = ScrollUntilObserve { text: false };
  scroll_until(&mut list, &end, &mut |_| Ok(ScrollUntilDecision::Continue)).unwrap();
  assert!(list.resolutions.iter().all(|resolution| *resolution == CaptureResolution::Logical), "{:?}", list.resolutions);

  let mut list = FakeList::new(260);
  scroll_until(&mut list, &request(ScrollUntilCondition::End), &mut |_| Ok(ScrollUntilDecision::Continue)).unwrap();
  assert!(list.resolutions.iter().all(|resolution| *resolution == CaptureResolution::Native), "text needs native pixels");
}

#[test]
fn retina_captures_detect_the_same_motion_as_one_x_captures() {
  // ROOT CAUSE:
  //
  // If a window capture came back at 2x, motion was compared in backing
  // pixels, so the ±24 px search policy validated on 1x captures covered only
  // 12 points and a step's shift read as twice as large.
  //
  // The fix compares motion per logical point whatever the capture resolution.
  let run = |scale: u32| {
    let mut list = FakeList::new(260);
    list.scale = scale;
    let mut seen = Vec::new();
    scroll_until(&mut list, &request(ScrollUntilCondition::End), &mut |observation| {
      seen.push((observation.steps, observation.motion.map(|motion| (motion.estimated_shift, motion.no_motion))));
      Ok(ScrollUntilDecision::Continue)
    })
    .unwrap();
    seen
  };
  assert_eq!(run(2), run(1));
}