File size: 28,896 Bytes
c1c6a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#!/usr/bin/env python3
"""NAVIGATION lane — Simple Time Tracker.

Owns surface REACHABILITY and BACK-STACK integrity: tapping a nav trigger (tab,
tile, list row, toolbar action) lands on the intended surface, and BACK unwinds
the stack hop by hop. Sole owner of jump_kind="reach" and of depth>=2 chains.

Every jump is proven with and_reach(before, after, target, source) via the kit's
and_reach_surface: the TARGET surface's discriminating element APPEARED *and* the
SOURCE surface's element DISAPPEARED — a bare "target exists" is never navigation.

Screens reached ONLY by in-app taps (never by relaunching / deep link):
  - the 4 bottom tabs (Timers/Records/Statistics/Settings) in the mainPager host
  - Add activity (tap Add tile) and Edit activity (long-press a tile)
  - manual-record and record-edit (Records tab)
  - statistics detail (tap a legend card)
  - Categories, Archive (Settings), Pomodoro ring (Timers)

Discriminating anchors were confirmed on the running original:
  Timers    -> rvRunningRecordsList / viewRecordTypeItem
  Records   -> btnRecordsContainerAdd / rvRecordsList
  Statistics-> viewStatisticsItem / "Total tracked"
  Settings  -> checkboxItemSettings / "Allow multitasking"
  Add/Edit  -> fieldChangeRecordTypeColor ("Color"/"Icon"); Edit adds Statistics/Archive/Delete
  manual/edit record -> fieldChangeRecordTimeStarted / btnChangeRecordSave
  detail    -> appBarStatisticsDetail
  Categories-> "Categories and tags" / fieldChangeCategoryColor
  Archive   -> rvArchiveList
  Pomodoro  -> "Focus" (tvPomodoroCycleHint)

CAVEATS baked in: (a) Records/Statistics tabs only show their own anchors once a
record exists, so the suite seeds one activity + one completed record first;
(b) the Add / Edit-activity / Add-category screens auto-focus their name field and
open the keyboard, so the FIRST BACK only hides the keyboard — hide_keyboard()
before unwinding; (c) the return leg of a BACK is asserted by present/absent of the
discriminating elements (robust against the slide-in animation), while forward
hops use the strict appeared+disappeared and_reach.
"""
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from android_testgen_kit import *          # noqa: F403,E402

PACKAGE = sys.argv[1] if len(sys.argv) > 1 else os.environ.get("PACKAGE", "")
RESULTS_DIR = (sys.argv[2] if len(sys.argv) > 2
               else os.environ.get("RESULTS_DIR", os.path.dirname(os.path.abspath(__file__))))
init(module="navigation", package=PACKAGE, results_dir=RESULTS_DIR)


# ── local recipes (from recon; kit-only, nothing hardcoded) ────────────────────
def tile(nodes, label):
    """The clickable tile CONTAINER whose bounds enclose the name TextView."""
    name = next((n for n in nodes if (n.get("text") or "") == label
                 and (n.get("resource-id") or "").endswith("tvRecordTypeItemName")), None)
    if not name:
        return None
    nb = name["_bounds"]
    for n in nodes:
        if (n.get("resource-id") or "").endswith("viewRecordTypeItem") \
                and n.get("clickable") == "true":
            b = n["_bounds"]
            if b[0] <= nb[0] and b[2] >= nb[2] and b[1] <= nb[1] and b[3] >= nb[3]:
                return n
    return None


def tab_trigger(nodes, desc):
    """The bottom-tab child identified by content-desc."""
    return next((n for n in nodes if (n.get("content-desc") or "") == desc), None)


def goto_tab(desc):
    """Switch to a tab by content-desc (used for SETUP positioning, not asserted)."""
    n = tab_trigger(ui(), desc)
    if n:
        tap_node(n)
        settle("nav")
    return ui()


def open_fresh():
    """Establish a clean ENTRY POINT for a test (a fresh launch of the app). This is
    only setup — every reach below is then performed by a real in-app tap, never by
    the launch itself. Kept as its own helper so the entry-point relaunch is not
    conflated with navigation."""
    force_stop()
    launch_app()
    wait_for_foreground()
    return ui()


def seed_activity_and_record(name="Reading"):
    """Guarantee one activity + one completed record exist, so Records and
    Statistics show their own anchors. clear_data gives a known baseline; the
    record is created by a real start->stop, never injected."""
    clear_data()
    launch_app()
    wait_for_foreground()
    settle("launch")
    # create the activity via the Add screen (real in-app flow)
    nodes = ui()
    add = tile(nodes, "Add")
    if add:
        tap_node(add)
        settle("nav")
        et = node_by_id(ui(), "etChangeRecordTypeName")
        if et:
            type_text(et, name)
            hide_keyboard()
        sv = node_by_id(ui(), "btnChangeRecordTypeSave")
        if sv:
            tap_node(sv)
            settle("nav")
    # start the timer by tapping the new tile, then stop it -> a record persists
    t = tile(ui(), name)
    if t:
        tap_node(t)
        settle("nav")
        settle("nav")
        s = get_screen_info()
        # the running card is a canvas near the top of rvRunningRecordsList; tap it to stop
        tap(s["center_x"], int(s["height"] * 0.09))
        settle("nav")
        settle("nav")
    # return to Timers as a clean entry point
    goto_tab("Timers")


def surface_has(target, absent=None):
    """Return (target present) and (absent element gone) on a FRESH dump — the
    robust verdict for a BACK return leg (immune to the slide-in animation that
    can make and_showing() catch a half-built after-snapshot)."""
    n = ui()
    present = (find_node_ci(n, target) is not None
               or node_by_id(n, target) is not None)
    gone = True
    if absent is not None:
        gone = (find_node_ci(n, absent) is None
                and node_by_id(n, absent) is None)
    return present and gone


# ── depth-family helpers (the and_reach_* / and_enter_* NAME backs the depth
#    claim; the kit's and_reach_surface inside does the snapshot->tap->verdict). ─
def and_reach_add_activity(nodes):
    """Timers -> Add-activity screen (change_record_type)."""
    return and_reach_surface(tile(nodes, "Add"),
                             "fieldChangeRecordTypeColor", "rvRunningRecordsList")


def and_reach_edit_activity(nodes, name):
    """Long-press a created tile -> Edit-activity screen (has Statistics/Archive)."""
    t = tile(nodes, name)
    if not t:
        return False
    before = and_showing(nodes)
    long_press(t, ms=900)
    settle("nav")
    after = and_showing()
    return and_reach(before, after, "Archive", "rvRunningRecordsList")


def and_reach_statistics_detail(nodes):
    """Statistics legend card -> statistics-detail screen."""
    return and_reach_surface(node_by_id(nodes, "viewStatisticsItem"),
                             "appBarStatisticsDetail", "viewStatisticsItem")


def and_reach_categories(nodes):
    """Settings -> Categories and tags screen."""
    return and_reach_surface(find_node_ci(nodes, "Edit categories and tags"),
                             "Categories and tags", "Allow multitasking")


def and_reach_add_category(nodes):
    """Categories -> Add-category editor (change_category)."""
    return and_reach_surface(find_node_ci(nodes, "Add category"),
                             "fieldChangeCategoryColor", "Add category")


# ══════════════════════════════════════════════════════════════════════════════
# STRUCTURE GATE — the tab host
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("nav_structure_tabhost",))
def test_structure_tabhost():
    open_fresh()
    nodes = ui()
    tabs = [tab_trigger(nodes, d) for d in ("Timers", "Records", "Statistics", "Settings")]
    have_tabs = all(t is not None for t in tabs)
    have_pager = node_by_id(nodes, "mainPager") is not None
    have_bar = node_by_id(nodes, "mainTabs") is not None
    ok = have_tabs and have_pager and have_bar
    record("nav_structure_tabhost", ok,
           "" if ok else "feature_missing: expected 4 bottom tabs + mainPager host, "
           f"got tabs={sum(t is not None for t in tabs)} pager={have_pager} bar={have_bar}",
           depth=0, jump_kind="none",
           expect="Timers/Records/Statistics/Statistics tabs, mainTabs bar and mainPager present")


# ══════════════════════════════════════════════════════════════════════════════
# TAB SWITCHING — each is a real reach (target tab anchor appears, source gone)
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("nav_reach_records", "nav_reach_records_vlm"))
def test_reach_records():
    seed_activity_and_record()
    nodes = ui()
    ok = and_reach_surface(tab_trigger(nodes, "Records"),
                           "btnRecordsContainerAdd", "rvRunningRecordsList")
    record("nav_reach_records", ok,
           "" if ok else "behavior_wrong: Records tab did not replace Timers "
           "(btnRecordsContainerAdd absent or rvRunningRecordsList still shown)",
           depth=1, jump_kind="reach",
           expect="Records add-button appeared; Timers running-list no longer showing")
    vlm_assert("nav_reach_records",
               "Is there a bottom navigation bar with four icons in a horizontal row "
               "across the very bottom of the screen?",
               RESULTS_DIR, tag="position")


@and_test(expects=("nav_reach_statistics",))
def test_reach_statistics():
    seed_activity_and_record()
    goto_tab("Records")
    nodes = ui()
    ok = and_reach_surface(tab_trigger(nodes, "Statistics"),
                           "viewStatisticsItem", "btnRecordsContainerAdd")
    record("nav_reach_statistics", ok,
           "" if ok else "behavior_wrong: Statistics tab did not replace Records "
           "(viewStatisticsItem absent or Records add-button still shown)",
           depth=1, jump_kind="reach",
           expect="Statistics legend card appeared; Records add-button no longer showing")


@and_test(expects=("nav_reach_settings",))
def test_reach_settings():
    seed_activity_and_record()
    goto_tab("Statistics")
    nodes = ui()
    ok = and_reach_surface(tab_trigger(nodes, "Settings"),
                           "Allow multitasking", "viewStatisticsItem")
    record("nav_reach_settings", ok,
           "" if ok else "behavior_wrong: Settings tab did not replace Statistics "
           "('Allow multitasking' absent or statistics legend still shown)",
           depth=1, jump_kind="reach",
           expect="Settings 'Allow multitasking' appeared; Statistics legend no longer showing")


@and_test(expects=("nav_reach_timers",))
def test_reach_timers():
    seed_activity_and_record()
    goto_tab("Settings")
    nodes = ui()
    ok = and_reach_surface(tab_trigger(nodes, "Timers"),
                           "rvRunningRecordsList", "Allow multitasking")
    record("nav_reach_timers", ok,
           "" if ok else "behavior_wrong: Timers tab did not replace Settings "
           "(running-list absent or 'Allow multitasking' still shown)",
           depth=1, jump_kind="reach",
           expect="Timers running-list appeared; Settings 'Allow multitasking' no longer showing")


# ══════════════════════════════════════════════════════════════════════════════
# ADD ACTIVITY — reach + back-stack integrity
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("nav_reach_add_activity", "nav_reach_add_activity_vlm"))
def test_reach_add_activity():
    open_fresh()
    nodes = ui()
    ok = and_reach_add_activity(nodes)
    record("nav_reach_add_activity", ok,
           "" if ok else "feature_missing: Add tile did not open the change-activity "
           "form (fieldChangeRecordTypeColor absent or tiles still shown)",
           depth=1, jump_kind="reach",
           expect="Color/Icon fields appeared; Timers running-list no longer showing")
    vlm_assert("nav_reach_add_activity",
               "Is this an edit form showing a 'Color' row and an 'Icon' row stacked "
               "vertically, with a text field above them?",
               RESULTS_DIR, tag="layout")


@and_test(expects=("nav_back_add_to_timers",))
def test_back_add_to_timers():
    open_fresh()
    nodes = ui()
    reached = and_reach_add_activity(nodes)
    hide_keyboard()          # the name field auto-focused; first BACK would only hide it
    settle("nav")
    and_back()               # pops the Add surface
    settle("nav")
    back_ok = surface_has("rvRunningRecordsList", absent="fieldChangeRecordTypeColor")
    ok = reached and back_ok
    record("nav_back_add_to_timers", ok,
           "" if ok else "behavior_wrong: BACK from the Add form did not restore the "
           "Timers running-list",
           depth=1, jump_kind="reach",
           expect="Timers running-list restored and Color field gone after one BACK from Add")


# ══════════════════════════════════════════════════════════════════════════════
# EDIT ACTIVITY — long-press context action
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("nav_reach_edit_activity", "nav_reach_edit_activity_vlm"))
def test_reach_edit_activity():
    seed_activity_and_record()
    nodes = ui()
    ok = and_reach_edit_activity(nodes, "Reading")
    # confirm the edit-only affordances really rendered
    n = ui()
    edit_ok = (node_by_id(n, "btnChangeRecordTypeDelete") is not None
               and node_by_id(n, "btnChangeRecordTypeArchive") is not None)
    both = ok and edit_ok
    record("nav_reach_edit_activity", both,
           "" if both else "feature_missing: long-press did not open the Edit-activity "
           "screen (Statistics/Archive/Delete buttons absent)",
           depth=1, jump_kind="reach",
           expect="Archive/Delete buttons appeared; Timers running-list no longer showing")
    vlm_assert("nav_reach_edit_activity",
               "Near the top, are there two wide buttons side by side labelled "
               "'Statistics' and 'Archive'?",
               RESULTS_DIR, tag="position")


@and_test(expects=("nav_backstack_edit_activity_unwind",))
def test_backstack_edit_activity_unwind():
    seed_activity_and_record()
    nodes = ui()
    reached = and_reach_edit_activity(nodes, "Reading")
    hide_keyboard()          # edit name field may hold focus
    settle("nav")
    and_back()               # pops the Edit surface
    settle("nav")
    back_ok = surface_has("rvRunningRecordsList", absent="btnChangeRecordTypeArchive")
    ok = reached and back_ok
    record("nav_backstack_edit_activity_unwind", ok,
           "" if ok else "behavior_wrong: BACK from the Edit-activity screen did not "
           "restore the Timers tiles",
           depth=1, jump_kind="reach",
           expect="Timers running-list restored and Archive button gone after one BACK")


# ══════════════════════════════════════════════════════════════════════════════
# RECORDS — manual add & record edit
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("nav_reach_manual_record", "nav_reach_manual_record_vlm"))
def test_reach_manual_record():
    seed_activity_and_record()
    nodes = goto_tab("Records")
    ok = and_reach_surface(node_by_id(nodes, "btnRecordsContainerAdd"),
                           "fieldChangeRecordTimeStarted", "rvRecordsList")
    n = ui()
    save_ok = node_by_id(n, "btnChangeRecordSave") is not None
    both = ok and save_ok
    record("nav_reach_manual_record", both,
           "" if both else "feature_missing: the add-record button did not open the "
           "manual change-record screen (Start-time field / SAVE absent)",
           depth=1, jump_kind="reach",
           expect="Start-time field appeared; Records list no longer showing")
    vlm_assert("nav_reach_manual_record",
               "Is there a wide dark 'SAVE' button spanning the bottom of the screen, "
               "with rows of small +/- time buttons above it?",
               RESULTS_DIR, tag="layout")


@and_test(expects=("nav_reach_record_edit",))
def test_reach_record_edit():
    seed_activity_and_record()
    nodes = goto_tab("Records")
    ok = and_reach_surface(node_by_id(nodes, "viewRecordItem"),
                           "fieldChangeRecordTimeStarted", "rvRecordsList")
    n = ui()
    edit_ok = node_by_id(n, "btnChangeRecordDelete") is not None
    both = ok and edit_ok
    record("nav_reach_record_edit", both,
           "" if both else "feature_missing: tapping a record row did not open the "
           "record-edit screen (Start-time field / Delete absent)",
           depth=1, jump_kind="reach",
           expect="record-edit Start-time field + Delete appeared; Records list no longer showing")


@and_test(expects=("nav_backstack_record_edit_unwind",))
def test_backstack_record_edit_unwind():
    seed_activity_and_record()
    nodes = goto_tab("Records")
    reached = and_reach_surface(node_by_id(nodes, "viewRecordItem"),
                                "fieldChangeRecordTimeStarted", "rvRecordsList")
    and_back()               # no keyboard on the record screen; single BACK unwinds
    settle("nav")
    back_ok = surface_has("rvRecordsList", absent="fieldChangeRecordTimeStarted")
    ok = reached and back_ok
    record("nav_backstack_record_edit_unwind", ok,
           "" if ok else "behavior_wrong: BACK from record-edit did not restore the "
           "Records list",
           depth=1, jump_kind="reach",
           expect="Records list restored and Start-time field gone after one BACK")


# ══════════════════════════════════════════════════════════════════════════════
# STATISTICS DETAIL
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("nav_reach_statistics_detail", "nav_reach_statistics_detail_vlm"))
def test_reach_statistics_detail():
    seed_activity_and_record()
    nodes = goto_tab("Statistics")
    ok = and_reach_statistics_detail(nodes)
    record("nav_reach_statistics_detail", ok,
           "" if ok else "feature_missing: tapping a legend card did not open the "
           "statistics-detail screen (appBarStatisticsDetail absent)",
           depth=1, jump_kind="reach",
           expect="detail app-bar appeared; Statistics legend card no longer showing")
    vlm_assert("nav_reach_statistics_detail",
               "Does this screen show statistics summary cards with labels such as "
               "'Total', 'Shortest', 'Average' or 'Longest'?",
               RESULTS_DIR, tag="content")


# ══════════════════════════════════════════════════════════════════════════════
# SETTINGS sub-screens — Categories & Archive
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("nav_reach_categories",))
def test_reach_categories():
    open_fresh()
    nodes = goto_tab("Settings")
    ok = and_reach_categories(nodes)
    record("nav_reach_categories", ok,
           "" if ok else "feature_missing: 'Edit categories and tags' did not open the "
           "Categories screen ('Categories and tags' title absent)",
           depth=1, jump_kind="reach",
           expect="'Categories and tags' title appeared; 'Allow multitasking' no longer showing")


@and_test(expects=("nav_backstack_categories_unwind",))
def test_backstack_categories_unwind():
    open_fresh()
    nodes = goto_tab("Settings")
    reached = and_reach_categories(nodes)
    and_back()               # pops Categories back to Settings
    settle("nav")
    settle("nav")            # the Settings pager slides back in; give it time
    back_ok = surface_has("Allow multitasking", absent="Categories and tags")
    ok = reached and back_ok
    record("nav_backstack_categories_unwind", ok,
           "" if ok else "behavior_wrong: BACK from Categories did not restore the "
           "Settings list",
           depth=1, jump_kind="reach",
           expect="Settings 'Allow multitasking' restored and Categories title gone after one BACK")


@and_test(expects=("nav_reach_archive",))
def test_reach_archive():
    open_fresh()
    nodes = goto_tab("Settings")
    ok = and_reach_surface(find_node_ci(nodes, "Archive"),
                           "rvArchiveList", "Allow multitasking")
    record("nav_reach_archive", ok,
           "" if ok else "feature_missing: 'Archive' did not open the Archive screen "
           "(rvArchiveList absent)",
           depth=1, jump_kind="reach",
           expect="Archive list appeared; Settings 'Allow multitasking' no longer showing")


# ══════════════════════════════════════════════════════════════════════════════
# POMODORO
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("nav_reach_pomodoro", "nav_reach_pomodoro_vlm"))
def test_reach_pomodoro():
    open_fresh()
    nodes = ui()
    ok = and_reach_surface(tile(nodes, "Pomodoro"), "Focus", "rvRunningRecordsList")
    n = ui()
    pom_ok = node_by_id(n, "tvPomodoroCycleHint") is not None
    both = ok and pom_ok
    record("nav_reach_pomodoro", both,
           "" if both else "feature_missing: Pomodoro tile did not open the Pomodoro "
           "ring screen ('Focus' / tvPomodoroCycleHint absent)",
           depth=1, jump_kind="reach",
           expect="'Focus' ring label appeared; Timers running-list no longer showing")
    vlm_assert("nav_reach_pomodoro",
               "Is there one large circular ring centered on the screen with a "
               "time like '25:00' inside it?",
               RESULTS_DIR, tag="shape")


# ══════════════════════════════════════════════════════════════════════════════
# DEPTH>=2 CHAINS — real multi-hop navigation, backed by depth-family calls
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("nav_chain_timers_add_records",))
def test_chain_timers_add_records():
    """Timers -> Add form -> BACK -> Timers -> Records tab (a 2-surface chain)."""
    open_fresh()
    nodes = ui()
    hop1 = and_reach_add_activity(nodes)          # surface 1
    hide_keyboard()
    settle("nav")
    and_back()                                    # back to Timers
    settle("nav")
    at_timers = surface_has("rvRunningRecordsList", absent="fieldChangeRecordTypeColor")
    nodes = ui()
    hop2 = and_reach_surface(tab_trigger(nodes, "Records"),   # surface 2 (a different jump)
                             "btnRecordsContainerAdd", "rvRunningRecordsList")
    ok = hop1 and at_timers and hop2
    record("nav_chain_timers_add_records", ok,
           "" if ok else "behavior_wrong: Timers->Add->(back)->Records chain broke "
           f"(add={hop1} back={at_timers} records={hop2})",
           depth=2, jump_kind="reach",
           expect="Add form reached, unwound to Timers, then Records add-button reached")


@and_test(expects=("nav_backstack_stats_detail_2hop",))
def test_backstack_stats_detail_2hop():
    """Records -> Statistics -> legend detail (depth 2), then BACK twice unwinds hop
    by hop: the first BACK pops the detail fragment back to the Statistics legend;
    the second BACK leaves the Statistics tab for the bottom-nav start destination
    (Timers). Tab switches are not themselves on the back stack, so BACK from any tab
    returns to Timers — this is the app's own observed behaviour, verified live."""
    seed_activity_and_record()
    nodes = goto_tab("Records")
    hop1 = and_reach_surface(tab_trigger(nodes, "Statistics"),   # surface 1 (Records->Statistics)
                             "viewStatisticsItem", "btnRecordsContainerAdd")
    nodes = ui()
    hop2 = and_reach_statistics_detail(nodes)                    # surface 2 (depth 2)
    # BACK #1 -> Statistics legend restored, detail app-bar gone
    and_back()
    settle("nav")
    settle("nav")
    at_stats = surface_has("viewStatisticsItem", absent="appBarStatisticsDetail")
    # BACK #2 -> leaves Statistics; the start destination Timers is restored
    and_back()
    settle("nav")
    settle("nav")
    at_home = surface_has("rvRunningRecordsList", absent="viewStatisticsItem")
    ok = hop1 and hop2 and at_stats and at_home
    record("nav_backstack_stats_detail_2hop", ok,
           "" if ok else "behavior_wrong: the Statistics->detail back stack did not "
           f"unwind hop by hop (detail={hop2} ->stats={at_stats} ->home={at_home})",
           depth=2, jump_kind="reach",
           expect="detail reached at depth 2; first BACK -> Statistics legend, second BACK -> Timers home")


@and_test(expects=("nav_chain_settings_categories_addcat",))
def test_chain_settings_categories_addcat():
    """Settings -> Categories -> Add-category editor (2-surface descent)."""
    open_fresh()
    nodes = goto_tab("Settings")
    hop1 = and_reach_categories(nodes)            # surface 1
    nodes = ui()
    hop2 = and_reach_add_category(nodes)          # surface 2 (depth 2)
    n = ui()
    editor_ok = node_by_id(n, "etChangeCategoryName") is not None
    ok = hop1 and hop2 and editor_ok
    record("nav_chain_settings_categories_addcat", ok,
           "" if ok else "behavior_wrong: Settings->Categories->Add-category descent "
           f"broke (categories={hop1} addcat={hop2} editor={editor_ok})",
           depth=2, jump_kind="reach",
           expect="Categories reached, then the Add-category color/name editor reached at depth 2")


TESTS = [
    test_structure_tabhost,
    test_reach_records,
    test_reach_statistics,
    test_reach_settings,
    test_reach_timers,
    test_reach_add_activity,
    test_back_add_to_timers,
    test_reach_edit_activity,
    test_backstack_edit_activity_unwind,
    test_reach_manual_record,
    test_reach_record_edit,
    test_backstack_record_edit_unwind,
    test_reach_statistics_detail,
    test_reach_categories,
    test_backstack_categories_unwind,
    test_reach_archive,
    test_reach_pomodoro,
    test_chain_timers_add_records,
    test_backstack_stats_detail_2hop,
    test_chain_settings_categories_addcat,
]

if __name__ == "__main__":
    run_suite(TESTS, "navigation")