File size: 26,389 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
#!/usr/bin/env python3
"""NAVIGATION lane — surface REACHABILITY + back-stack integrity for EasyNotes.

Every test proves a JUMP the honest way (and_reach / and_reach_surface): the
TARGET surface's discriminating element APPEARED *and* the SOURCE surface's
element DISAPPEARED. Deep chains (Settings subscreens = depth 2, dialogs / the
note-info modal = depth 3) are owned here and are each backed by real
and_enter*/and_reach* calls — never by relaunching the app.

Surfaces (validated on the running original):
  Home        content-desc New Note / Search / Settings
  Settings    text Colors & Styles / Behavior / Language / Backup / Privacy /
              Tools / About  (+ subtitle "Themes • Size • Font")
  Colors      Minimalistic Mode / Radius / Font Size / Accent Color / System Theme
  Behavior    Markdown / Prioritize Editing Mode / Gallery Sync / Disable swipe
  Language    Change the app language / Monospace font
  Backup      Encrypt Database / Restore / Import
  Privacy     Screen Protection / App lock / Vault
  Tools       Notes (+ count)
  About       Version / Build Type / Source Code / Supporters List
  Edit        content-desc Back / Done ; text Name / Description
  Preview     content-desc Back / Info
  More menu   Delete / Pinned / Copy / Information (Preview elements vanish)
  Info modal  Created Time / Words / Characters
  dialogs     Select Radius ; Select Accent Color+Use System Colors ; Font Size:+Preview

CAUTION (learned from the running dump): uiautomator keeps "Colors & Styles"
as a literal XML entity, so it can NEVER be a target/source token — every reach
here uses ampersand-free discriminators (Behavior, Language, Minimalistic Mode,
Build Type, ...). Likewise, a note NAME must not contain a source token like
'Done' or the disappearance check false-positives on the card text.
"""
import os
import sys

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

TESTS = []


def test(fn):
    TESTS.append(fn)
    return fn


# ── shared entry-point helpers (plain helpers; the and_reach_*/and_enter_* NAME
#    is what the depth audit greps for — implementation is ours) ─────────────────

def _home_nodes():
    """Restart the app and return a fresh Home dump, or None on failure."""
    restart_app()
    if not wait_for_foreground():
        return None
    return ui()


def and_reach_settings():
    """Home -> Settings. Returns True on a proven reach. Counts one surface."""
    nodes = ui()
    gear = find_node(nodes, content_desc="Settings")
    if gear is None:
        return False
    # target: a Settings-only section ('Behavior' — NO ampersand, because the
    # dump keeps "Colors & Styles" as a literal XML entity that would never
    # substring-match "Colors & Styles"). source: the Home FAB that leaves.
    return and_reach_surface(gear, "Behavior", "New Note")


def and_enter_settings():
    """Navigate from a fresh Home into Settings; True once on Settings."""
    nodes = _home_nodes()
    return bool(nodes) and and_reach_settings()


def and_reach_section(nodes, subtitle, target, source):
    """Settings -> a section subscreen, tapping the section's SUBTITLE node
    (whole card is clickable) to dodge the 'Colors & Styles' XML-entity
    matching issue. target = a body element unique to the subscreen; source = a
    *sibling* section name (ampersand-free) that leaves the tree."""
    row = find_node_ci(nodes, subtitle)
    if row is None:
        return False
    return and_reach_surface(row, target, source)


def and_enter_colors():
    """Home -> Settings -> Colors & Styles. True once on Colors (depth 2)."""
    if not and_enter_settings():
        return False
    return and_reach_section(ui(), "Themes • Size • Font",
                             "Minimalistic Mode", "Behavior")


def and_reach_preview():
    """Create a note in setup, return Home, then open it -> Preview.

    We navigate to a record created in setup and assert the JUMP, not the
    creation. Returns (ok, body) where body is the note text for source-matching.
    Neither the name ('Groceries') nor the body ('Milk Eggs Bread') contains a
    surface token (Info / New Note / Done / Back), so they are safe sources.
    """
    body = "Milk Eggs Bread"
    nodes = _home_nodes()
    if not nodes:
        return False, body
    fab = find_node(nodes, content_desc="New Note")
    if fab is None or not tap_node(fab):
        return False, body
    ed = ui()
    fields = find_by_class(ed, "EditText")
    if len(fields) < 2:
        return False, body
    # document order: [0]=Name, [1]=Description (validated on the original)
    type_text(fields[0], "Groceries")
    ed2 = ui()
    fields2 = find_by_class(ed2, "EditText")
    if len(fields2) >= 2:
        type_text(fields2[1], body)
    # Save & return Home via Done.
    done = find_node(ui(), content_desc="Done")
    if done is None or not tap_node(done):
        return False, body
    settle("tap")
    # Open the card -> Preview. Re-find the card fresh (grid recycles children).
    card = find_node_ci(ui(), "Groceries")
    if card is None:
        return False, body
    reached = and_reach_surface(card, "Info", "New Note")
    return reached, body


def and_reach_more(preview_body):
    """Preview -> More dropdown. target Information appears; the note body (a
    Preview-only element) disappears when the dropdown overlays it."""
    info = find_node(ui(), content_desc="Info")
    if info is None:
        return False
    return and_reach_surface(info, "Information", preview_body)


# ══════════════════════════════════════════════════════════════════════════════
# 1. Home -> Settings, and each Settings subscreen (depth 1 / depth 2)
# ══════════════════════════════════════════════════════════════════════════════

@test
@and_test(expects=("nav_reach_settings", "nav_reach_settings_vlm"))
def test_reach_settings():
    nodes = _home_nodes()
    ok = bool(nodes) and and_reach_settings()
    record("nav_reach_settings", ok,
           "" if ok else "feature_missing: Settings not reachable from Home "
                          "('Behavior' section did not appear / New Note did not leave)",
           depth=1, jump_kind="reach",
           expect="Settings surface: 'Behavior' section appeared; Home 'New Note' gone")
    vlm_assert("nav_reach_settings",
               "Is there a left-pointing back/up arrow at the very top-LEFT of the "
               "screen, to the left of a 'Settings' title?",
               RESULTS_DIR, depth=1, jump_kind="reach", tag="position")


@test
@and_test(expects=("nav_reach_colors", "nav_reach_colors_vlm"))
def test_reach_colors():
    ok = and_enter_settings()
    if ok:
        ok = and_reach_section(ui(), "Themes • Size • Font",
                               "Minimalistic Mode", "Behavior")
    record("nav_reach_colors", ok,
           "" if ok else "feature_missing: Colors & Styles subscreen not reached "
                          "(Minimalistic Mode absent / Behavior still showing)",
           depth=2, jump_kind="reach",
           expect="Colors & Styles: 'Minimalistic Mode' appeared; Settings 'Behavior' gone")
    vlm_assert("nav_reach_colors",
               "Does this screen show a vertical list of settings rows, each with a "
               "round icon on the LEFT and a toggle switch or chevron on the RIGHT?",
               RESULTS_DIR, depth=2, jump_kind="reach", tag="layout")


@test
@and_test(expects=("nav_reach_behavior", "nav_reach_behavior_vlm"))
def test_reach_behavior():
    ok = and_enter_settings()
    if ok:
        # source 'Language' (ampersand-free sibling that leaves the Settings list)
        ok = and_reach_section(ui(), "Support • Images", "Markdown", "Language")
    record("nav_reach_behavior", ok,
           "" if ok else "feature_missing: Behavior subscreen not reached "
                          "(Markdown absent / Language section still showing)",
           depth=2, jump_kind="reach",
           expect="Behavior: 'Markdown' appeared; Settings 'Language' section gone")
    vlm_assert("nav_reach_behavior",
               "Is there a screen title in a top app bar with a back arrow at its "
               "LEFT edge?",
               RESULTS_DIR, depth=2, jump_kind="reach", tag="position")


@test
@and_test(expects=("nav_reach_language",))
def test_reach_language():
    ok = and_enter_settings()
    if ok:
        ok = and_reach_section(ui(), "Font • Language",
                               "Change the app language", "Behavior")
    record("nav_reach_language", ok,
           "" if ok else "feature_missing: Language subscreen not reached "
                          "(language body absent / Behavior still showing)",
           depth=2, jump_kind="reach",
           expect="Language: 'Change the app language' appeared; Settings 'Behavior' gone")


@test
@and_test(expects=("nav_reach_backup",))
def test_reach_backup():
    ok = and_enter_settings()
    if ok:
        ok = and_reach_section(ui(), "NextCloud • Local • Encryption",
                               "Encrypt Database", "Behavior")
    record("nav_reach_backup", ok,
           "" if ok else "feature_missing: Backup subscreen not reached "
                          "(Encrypt Database absent / Behavior still showing)",
           depth=2, jump_kind="reach",
           expect="Backup: 'Encrypt Database' appeared; Settings 'Behavior' gone")


@test
@and_test(expects=("nav_reach_privacy",))
def test_reach_privacy():
    ok = and_enter_settings()
    if ok:
        ok = and_reach_section(ui(), "Screen Protection", "App lock", "Behavior")
    record("nav_reach_privacy", ok,
           "" if ok else "feature_missing: Privacy subscreen not reached "
                          "(App lock absent / Behavior still showing)",
           depth=2, jump_kind="reach",
           expect="Privacy: 'App lock' appeared; Settings 'Behavior' gone")


@test
@and_test(expects=("nav_reach_tools",))
def test_reach_tools():
    ok = and_enter_settings()
    if ok:
        ok = and_reach_section(ui(), "Created • Delete All", "Notes", "Behavior")
    record("nav_reach_tools", ok,
           "" if ok else "feature_missing: Tools subscreen not reached "
                          "('Notes' counter absent / Behavior still showing)",
           depth=2, jump_kind="reach",
           expect="Tools: 'Notes' appeared; Settings 'Behavior' gone")


@test
@and_test(expects=("nav_reach_about", "nav_reach_about_vlm"))
def test_reach_about():
    ok = and_enter_settings()
    if ok:
        ok = and_reach_section(ui(), "Version • Developers • License",
                               "Build Type", "Behavior")
    record("nav_reach_about", ok,
           "" if ok else "feature_missing: About subscreen not reached "
                          "(Build Type absent / Behavior still showing)",
           depth=2, jump_kind="reach",
           expect="About: 'Build Type' appeared; Settings 'Behavior' gone")
    vlm_assert("nav_reach_about",
               "Is a screen title showing at the TOP of the screen with a back arrow "
               "to its LEFT?",
               RESULTS_DIR, depth=2, jump_kind="reach", tag="position")


# ══════════════════════════════════════════════════════════════════════════════
# 2-3. Home -> New Note editor ; Home -> Preview
# ══════════════════════════════════════════════════════════════════════════════

@test
@and_test(expects=("nav_reach_editor", "nav_reach_editor_vlm"))
def test_reach_editor():
    nodes = _home_nodes()
    ok = False
    if nodes:
        fab = find_node(nodes, content_desc="New Note")
        ok = fab is not None and and_reach_surface(fab, "Done", "New Note")
    record("nav_reach_editor", ok,
           "" if ok else "feature_missing: note editor not reached from FAB "
                          "(Done affordance absent / New Note still showing)",
           depth=1, jump_kind="reach",
           expect="Edit surface: 'Done' appeared; Home 'New Note' gone")
    vlm_assert("nav_reach_editor",
               "Is there a checkmark / done (tick) icon at the top-RIGHT corner of "
               "the screen?",
               RESULTS_DIR, depth=1, jump_kind="reach", tag="position")


@test
@and_test(expects=("nav_reach_editor_toggle_vlm",))
def test_reach_editor_toggle_vlm():
    # Enter the editor, then ask the VLM about the centered Edit/Preview pill.
    nodes = _home_nodes()
    reached = False
    if nodes:
        fab = find_node(nodes, content_desc="New Note")
        reached = fab is not None and and_reach_surface(fab, "Done", "New Note")
    if reached:
        vlm_assert("nav_reach_editor_toggle",
                   "Is there a rounded pill-shaped toggle with two icons "
                   "(a pencil and an eye) centered at the TOP of the screen?",
                   RESULTS_DIR, depth=1, jump_kind="reach", tag="shape")
    else:
        record("nav_reach_editor_toggle_vlm", False,
               "setup_failed: could not reach the editor to photograph the toggle",
               depth=1, jump_kind="reach", tag="shape")


@test
@and_test(expects=("nav_reach_preview", "nav_reach_preview_vlm"))
def test_reach_preview():
    ok, _body = and_reach_preview()
    record("nav_reach_preview", ok,
           "" if ok else "feature_missing: Preview not reached from a created card "
                          "(Info affordance absent / New Note still showing)",
           depth=1, jump_kind="reach",
           expect="Preview surface: 'Info' appeared; Home 'New Note' gone")
    vlm_assert("nav_reach_preview",
               "Is there an overflow menu button (three vertical dots) at the "
               "top-RIGHT corner of the screen?",
               RESULTS_DIR, depth=1, jump_kind="reach", tag="position")


# ══════════════════════════════════════════════════════════════════════════════
# 4-5. Preview -> More dropdown ; More -> Note Info modal (deep chains)
# ══════════════════════════════════════════════════════════════════════════════

@test
@and_test(expects=("nav_reach_more", "nav_reach_more_vlm"))
def test_reach_more():
    reached_preview, body = and_reach_preview()
    ok = reached_preview and and_reach_more(body)
    record("nav_reach_more", ok,
           "" if ok else "feature_missing: More dropdown not reached from Preview "
                          "(Information item absent / note body still showing)",
           depth=2, jump_kind="reach",
           expect="More dropdown: 'Information' appeared; Preview note body gone")
    vlm_assert("nav_reach_more",
               "Is a small dropdown menu panel anchored near the top-RIGHT corner, "
               "listing rows such as Delete, Pinned, Copy and Information?",
               RESULTS_DIR, depth=2, jump_kind="reach", tag="position")


@test
@and_test(expects=("nav_reach_note_info", "nav_reach_note_info_vlm"))
def test_reach_note_info():
    reached_preview, body = and_reach_preview()
    ok = False
    if reached_preview and and_reach_more(body):
        info_item = find_node_ci(ui(), "Information")
        ok = info_item is not None and and_reach_surface(
            info_item, "Created Time", "Copy")
    record("nav_reach_note_info", ok,
           "" if ok else "feature_missing: Note Info modal not reached "
                          "(Created Time absent / More 'Copy' still showing)",
           depth=3, jump_kind="reach",
           expect="Note Info modal: 'Created Time' appeared; More 'Copy' gone")
    vlm_assert("nav_reach_note_info",
               "Is there a panel / sheet at the BOTTOM of the screen showing labelled "
               "rows like 'Created Time', 'Words' and 'Characters'?",
               RESULTS_DIR, depth=3, jump_kind="reach", tag="position")


# ══════════════════════════════════════════════════════════════════════════════
# 6. Colors & Styles -> dialogs (depth 3)
# ══════════════════════════════════════════════════════════════════════════════

@test
@and_test(expects=("nav_reach_fontsize_dialog", "nav_reach_fontsize_dialog_vlm"))
def test_reach_fontsize_dialog():
    ok = and_enter_colors()
    if ok:
        row = find_node_ci(ui(), "Font Size")
        ok = row is not None and and_reach_surface(row, "Preview", "Radius")
    record("nav_reach_fontsize_dialog", ok,
           "" if ok else "feature_missing: Font Size dialog not reached "
                          "('Preview' absent / 'Radius' row still showing)",
           depth=3, jump_kind="reach",
           expect="Font Size dialog: 'Preview' appeared; Colors 'Radius' row gone")
    vlm_assert("nav_reach_fontsize_dialog",
               "Is a dialog card centered on the screen containing a horizontal "
               "slider control?",
               RESULTS_DIR, depth=3, jump_kind="reach", tag="shape")


@test
@and_test(expects=("nav_reach_radius_dialog",))
def test_reach_radius_dialog():
    ok = and_enter_colors()
    if ok:
        row = find_node_ci(ui(), "Radius")
        ok = row is not None and and_reach_surface(row, "Select Radius", "Font Size")
    record("nav_reach_radius_dialog", ok,
           "" if ok else "feature_missing: Radius dialog not reached "
                          "('Select Radius' absent / 'Font Size' row still showing)",
           depth=3, jump_kind="reach",
           expect="Radius dialog: 'Select Radius' appeared; Colors 'Font Size' row gone")


@test
@and_test(expects=("nav_reach_accent_dialog",))
def test_reach_accent_dialog():
    ok = and_enter_colors()
    if ok:
        row = find_node_ci(ui(), "Accent Color")
        ok = row is not None and and_reach_surface(
            row, "Use System Colors", "Minimalistic Mode")
    record("nav_reach_accent_dialog", ok,
           "" if ok else "feature_missing: Accent Color dialog not reached "
                          "('Use System Colors' absent / 'Minimalistic Mode' still showing)",
           depth=3, jump_kind="reach",
           expect="Accent dialog: 'Use System Colors' appeared; Colors 'Minimalistic Mode' gone")


# ══════════════════════════════════════════════════════════════════════════════
# 7. Back-stack integrity
# ══════════════════════════════════════════════════════════════════════════════

@test
@and_test(expects=("nav_back_colors_to_settings",))
def test_back_colors_to_settings():
    # Home -> Settings -> Colors (depth 2), then one BACK must restore Settings.
    reached = and_enter_colors()
    ok = False
    if reached:
        before = and_showing()          # Colors surface
        and_back()                      # pop one surface
        after = and_showing()
        # parent (Language section) reappeared AND child (Minimalistic Mode) left.
        ok = and_reach(before, after, "Language", "Minimalistic Mode")
    record("nav_back_colors_to_settings", ok,
           "" if ok else "behavior_wrong: BACK from Colors did not restore Settings "
                          "('Language' section did not reappear / Colors row still showing)",
           depth=2, jump_kind="reach",
           expect="After BACK: Settings 'Language' reappeared; Colors 'Minimalistic Mode' gone")


@test
@and_test(expects=("nav_back_settings_to_home",))
def test_back_settings_to_home():
    reached = and_enter_settings()      # Home -> Settings (depth 1)
    ok = False
    if reached:
        before = and_showing()          # Settings surface
        and_back()
        after = and_showing()
        # Home FAB reappeared AND a Settings-only section (Behavior) left.
        ok = and_reach(before, after, "New Note", "Behavior")
    record("nav_back_settings_to_home", ok,
           "" if ok else "behavior_wrong: BACK from Settings did not restore Home "
                          "('New Note' did not reappear / 'Behavior' still showing)",
           depth=1, jump_kind="reach",
           expect="After BACK: Home 'New Note' reappeared; Settings 'Behavior' gone")


@test
@and_test(expects=("nav_backstack_2hop_unwind",))
def test_backstack_2hop_unwind():
    # Full deepest->home unwind: Home -> Settings -> Colors, then BACK, BACK.
    reached = and_enter_colors()        # depth 2 via two and_reach_surface calls
    step1 = step2 = False
    if reached:
        before1 = and_showing()         # Colors
        and_back()
        mid = and_showing()             # expect Settings
        step1 = and_reach(before1, mid, "Behavior", "Minimalistic Mode")
        and_back()
        home = and_showing()            # expect Home
        step2 = and_reach(mid, home, "New Note", "Behavior")
    ok = reached and step1 and step2
    record("nav_backstack_2hop_unwind", ok,
           "" if ok else "behavior_wrong: 2-hop BACK did not unwind Colors->Settings->Home "
                          "hop by hop",
           depth=2, jump_kind="reach",
           expect="BACK #1 -> Settings ('Behavior' back), BACK #2 -> Home ('New Note' back)")


# ══════════════════════════════════════════════════════════════════════════════
# 8. Editor -> Home via Back and via Done
# ══════════════════════════════════════════════════════════════════════════════

@test
@and_test(expects=("nav_back_editor_to_home",))
def test_back_editor_to_home():
    nodes = _home_nodes()
    reached = False
    if nodes:
        fab = find_node(nodes, content_desc="New Note")
        reached = fab is not None and and_reach_surface(fab, "Done", "New Note")
    ok = False
    if reached:
        before = and_showing()          # Edit surface
        and_back()                      # empty note discarded, returns Home
        after = and_showing()
        ok = and_reach(before, after, "New Note", "Done")
    record("nav_back_editor_to_home", ok,
           "" if ok else "behavior_wrong: BACK from the editor did not restore Home "
                          "('New Note' did not reappear / 'Done' still showing)",
           depth=1, jump_kind="reach",
           expect="After BACK: Home 'New Note' reappeared; editor 'Done' gone")


@test
@and_test(expects=("nav_done_editor_to_home",))
def test_done_editor_to_home():
    nodes = _home_nodes()
    reached = False
    if nodes:
        fab = find_node(nodes, content_desc="New Note")
        reached = fab is not None and and_reach_surface(fab, "Done", "New Note")
    ok = False
    if reached:
        # Name the note something WITHOUT the source token 'Done' (else the card
        # text keeps a 'done' key alive and the disappearance check fails).
        fields = find_by_class(ui(), "EditText")
        if fields:
            type_text(fields[0], "SaveNav")
        done = find_node(ui(), content_desc="Done")
        if done is not None:
            before = and_showing()      # Edit surface
            tap_node(done)
            after = and_showing()
            ok = and_reach(before, after, "New Note", "Done")
    record("nav_done_editor_to_home", ok,
           "" if ok else "behavior_wrong: Done did not return to Home "
                          "('New Note' did not reappear / 'Done' still showing)",
           depth=1, jump_kind="reach",
           expect="After Done: Home 'New Note' reappeared; editor 'Done' gone")


# ══════════════════════════════════════════════════════════════════════════════
# Preview -> back to Home (back-stack of a note preview)
# ══════════════════════════════════════════════════════════════════════════════

@test
@and_test(expects=("nav_back_preview_to_home",))
def test_back_preview_to_home():
    reached, _body = and_reach_preview()
    ok = False
    if reached:
        before = and_showing()          # Preview surface
        and_back()
        after = and_showing()
        ok = and_reach(before, after, "New Note", "Info")
    record("nav_back_preview_to_home", ok,
           "" if ok else "behavior_wrong: BACK from Preview did not restore Home "
                          "('New Note' did not reappear / 'Info' still showing)",
           depth=1, jump_kind="reach",
           expect="After BACK: Home 'New Note' reappeared; Preview 'Info' gone")


if __name__ == "__main__":
    if not device_online():
        print("device offline — aborting navigation suite")
        sys.exit(2)
    run_suite(TESTS, "navigation")