File size: 53,743 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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
#!/usr/bin/env python3
"""INTERACTION lane — EFFECT-BY-CONSEQUENCE on user DATA for EasyNotes.

Every test drives the app to a data-mutating action through the real user path,
then reads the CONSEQUENCE back out of a fresh ui(): a card appeared / changed /
disappeared, a selection count changed, a markdown token landed in the body.

Ground truth (verified live against the original before writing):
  * Create: New Note FAB -> Edit page (Name/Description EditTexts) -> Done. Empty
    notes (no title AND no body) are discarded. A note created via Done stays on
    Home (app foreground).
  * Home card = a title TextView (bold) + a body-preview TextView. The app
    CAPITALISES the first character of the displayed TITLE (a title typed as
    'kitI_x' renders as 'KitI_x'), so all title read-backs are case-insensitive.
    The body preview is shown verbatim.
  * Edit existing: tap card -> Preview (Back/Info) -> tap the pencil toggle (left
    half of the top-center switch, cx~466) -> Edit page (Done appears) -> change
    Description -> BACK. Leaving Edit that was reached FROM Preview pops back past
    Home to the launcher, so the suite RELAUNCHES to read the persisted result.
  * Delete via More: Preview -> Info -> Delete menu item. NO confirmation; the app
    backgrounds afterwards, so relaunch to observe. DB reflects the deletion.
  * Delete via selection bar: long-press card -> Delete -> confirm "Yes".
  * Selection mode: long-press a card -> top bar Close / count TextView / Pin /
    Delete, and (when selected != total) `Select All` (content-desc, capital A).
    Cards render in a 2-col grid; the selected card gets a coloured border.
  * Markdown toolbar (Description focused): the two arrow buttons ALSO carry
    content-desc `Bullet List`; the REAL Bullet List is the interior node (sort by
    x, take the middle). Bullet inserts `- `, Checkbox inserts `[ ] `.
  * Notes + edits persist across process death (Room DB).
"""
import os
import sys

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


# ── distinctive markers (so a read-back cannot match pre-existing content) ──────
M_CREATE = "kitI_CreateA"
M_BODY_T = "kitI_BodyT"
M_BODY_B = "kitI_bodybtext"
M_TWO_1 = "kitI_TwoA"
M_TWO_2 = "kitI_TwoB"
M_EDIT_T = "kitI_EditMe"
M_EDIT_MARK = "kitiedited"          # goes in the BODY (verbatim), lower-case safe
M_DEL_T = "kitI_DelTarget"
M_KEEP_T = "kitI_KeepSafe"
M_SOLO = "kitI_Solo"
M_PERSIST = "kitI_Persist"
M_EDITP_T = "kitI_EditPersist"
M_LP = "kitI_LongPress"
M_SEL1 = "kitI_SelOne"
M_SEL2 = "kitI_SelTwo"
M_SA1 = "kitI_SaOne"
M_SA2 = "kitI_SaTwo"
M_SDEL_DEL = "kitI_SbDel"
M_SDEL_KEEP = "kitI_SbKeep"
M_MK = "kitI_MarkNote"
M_MK2 = "kitI_MarkCheck"

# body texts (shown verbatim on the card — chosen distinctive, lower-case ok)
B_ORIG = "origbody"
B_TRI = ("btrione", "btritwo", "btrithree")


# ══════════════════════════════════════════════════════════════════════════════
# Helpers (plain; the and_enter_* / and_reach_* NAMES back the declared depth)
# ══════════════════════════════════════════════════════════════════════════════

def _ci(nodes, marker):
    """Case-insensitive text/desc lookup. EasyNotes CAPITALISES the first char of
    a displayed note TITLE ('kitI_x' -> 'KitI_x'), so every title/body read-back
    matches case-insensitively — never exact."""
    return find_node_ci(nodes, marker)


def find_card(nodes, marker):
    """The node carrying `marker` as a note title/body (case-insensitive)."""
    return _ci(nodes, marker)


def has_text(nodes, marker):
    return _ci(nodes, marker) is not None


def sorted_edittexts(nodes):
    """EditTexts top-to-bottom: [0]=Name, [1]=Description on the Edit page."""
    return sorted(find_by_class(nodes, "EditText"),
                  key=lambda e: e.get("_bounds", (0, 0, 0, 0))[1])


def _on_home(nodes=None):
    nodes = nodes or ui()
    return find_node(nodes, content_desc="New Note") is not None


def ensure_home():
    """Return to the app's Home surface, launcher-proof.

    Deleting or editing a note opened from Preview pops the app PAST Home to the
    launcher; is_app_foreground() is unreliable there (the app still shows in
    `dumpsys window`), so this keys ONLY off the New Note FAB — Home's
    discriminator. It first tries BACK (in case we are one surface above Home),
    then RELAUNCHES unconditionally until the FAB reappears."""
    for attempt in range(4):
        if _on_home():
            return
        # one BACK may drop Preview/Edit -> Home without a relaunch
        back()
        settle("tap")
        if _on_home():
            return
        launch_app()
        wait_for_foreground()
        settle("tap")
    # last resort: relaunch once more and settle harder
    if not _on_home():
        launch_app()
        wait_for_foreground()
        settle("dump")


def and_enter_editor():
    """Open the note editor via the New Note FAB. Backs depth>=1."""
    n = ui()
    fab = find_node(n, content_desc="New Note")
    if fab is None or not tap_node(fab):
        return False
    and_enter("editor")
    return True


def create_note(title, body=""):
    """Full create path: FAB -> type Name (+Description) -> Done. Leaves the app
    on Home (verified: Done from a fresh note keeps the app foreground)."""
    if not and_enter_editor():
        return False
    ets = sorted_edittexts(ui())
    if not ets:
        return False
    if title:
        type_text(ets[0], title)
    if body:
        ets = sorted_edittexts(ui())
        if len(ets) > 1:
            type_text(ets[1], body)
    hide_keyboard()
    done = find_node(ui(), content_desc="Done") or find_node(ui(), content_desc="Back")
    if done:
        tap_node(done)
    wait_for_foreground()
    settle("tap")
    return True


def and_reach_preview(title):
    """Tap a note card and PROVE Preview opened: Info appeared, the FAB (a Home
    discriminator) vanished. Backs depth>=1 with a real reach."""
    n = ui()
    card = find_card(n, title)
    if card is None:
        return False
    before = and_showing(n)
    if not tap_node(card):
        return False
    after = and_showing()
    ok = and_reach(before, after, "Info", "New Note")
    if ok:
        and_enter("preview")
    return ok


def find_edit_pencil(nodes):
    """The Edit (pencil) half of the top-center mode toggle: a small anonymous
    View at cx~466 in the top strip (the eye/Preview half sits at cx~613)."""
    cands = []
    for x in nodes:
        b = x.get("_bounds")
        if not b:
            continue
        cx, cy = x.get("_center", (0, 0))
        w, h = b[2] - b[0], b[3] - b[1]
        if (100 < cy < 200 and 40 < w < 200 and 40 < h < 200
                and 380 < cx < 520 and x.get("class", "").endswith("View")):
            cands.append(x)
    cands.sort(key=lambda x: x.get("_center", (0, 0))[0])
    return cands[0] if cands else None


def and_enter_edit_from_preview():
    """From Preview, tap the pencil toggle to enter Edit. Confirms via Done. Backs
    one more surface (depth>=2 when chained after and_reach_preview)."""
    pencil = find_edit_pencil(ui())
    if pencil is None or not tap_node(pencil):
        return False
    settle("tap")
    if find_node(ui(), content_desc="Done") is None:
        return False
    and_enter("edit")
    return True


def save_edit():
    """Persist the current Edit and return to Home DETERMINISTICALLY. Tapping the
    Done checkmark saves and lands directly on Home (verified) — unlike
    hide_keyboard()+BACK, whose BACK can overshoot Home to the launcher."""
    done = find_node(ui(), content_desc="Done")
    if done:
        tap_node(done)
    settle("tap")
    ensure_home()


def and_enter_selection(title):
    """Long-press a card to enter selection mode. Confirms via the Close control
    (selection-bar discriminator). Backs depth>=1."""
    n = ui()
    card = find_card(n, title)
    if card is None:
        return False
    if not long_press(card, ms=900):
        return False
    settle("tap")
    if find_node(ui(), content_desc="Close") is None:
        return False
    and_enter("selection")
    return True


def and_enter_more_menu():
    """From Preview, tap Info to open the More dropdown. Confirms via the Delete
    item. Backs one more surface."""
    info = find_node(ui(), content_desc="Info")
    if info is None or not tap_node(info):
        return False
    settle("tap")
    if find_node(ui(), text="Delete") is None and find_node(ui(), content_desc="Delete") is None:
        return False
    and_enter("more_menu")
    return True


def selection_count(nodes=None):
    """The selection-bar count as an int (the lone digit TextView), or None."""
    nodes = nodes or ui()
    for n in nodes:
        t = n.get("text", "").strip()
        if t.isdigit() and n.get("class", "").endswith("TextView"):
            return int(t)
    return None


def focus_description_toolbar():
    """In Edit, focus the Description field so the markdown toolbar appears.
    Returns the toolbar nodes, or None."""
    ets = sorted_edittexts(ui())
    if len(ets) < 2:
        return None
    type_text(ets[1], "x")          # focuses + gives the field content
    n = ui()
    if find_node(n, content_desc="Checkbox") is None:
        return None
    return n


def markdown_bullet_node(nodes):
    """The interior Bullet List button (the two arrows share that content-desc)."""
    bl = sorted(find_nodes(nodes, content_desc="Bullet List"),
                key=lambda x: x.get("_center", (0, 0))[0])
    if len(bl) >= 3:
        return bl[1]
    if bl:
        return bl[len(bl) // 2]
    return None


# ══════════════════════════════════════════════════════════════════════════════
# 1. Create note -> new card with that title appears on Home
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_create_note",
                   "interaction_create_note_vlm"))
def test_create_note():
    clear_data()
    restart_app()
    if not wait_for_foreground():
        record("interaction_create_note", False,
               "setup_failed: app did not reach foreground",
               depth=1, jump_kind="value", expect=f"card {M_CREATE!r} on Home")
        record("interaction_create_note_vlm", False,
               "setup_failed: app did not reach foreground",
               depth=1, jump_kind="value", expect="bold title card", tag="content")
        return
    absent_before = not has_text(ui(), M_CREATE)
    if not create_note(M_CREATE, "somebody"):
        record("interaction_create_note", False,
               "feature_missing: could not open editor / type / save",
               depth=1, jump_kind="value", expect=f"card {M_CREATE!r} on Home")
        record("interaction_create_note_vlm", False,
               "feature_missing: could not create note",
               depth=1, jump_kind="value", expect="bold title card", tag="content")
        return
    ensure_home()
    present_after = has_text(ui(), M_CREATE)
    ok = absent_before and present_after
    record("interaction_create_note", ok,
           "" if ok else "behavior_wrong: created card did not appear on Home",
           depth=1, jump_kind="value", expect=f"card {M_CREATE!r} on Home")
    # VLM: the created note renders as a card with a bold title.
    vlm_assert("interaction_create_note",
               f"Is there a note card near the top of the list whose title "
               f"'{M_CREATE}' is shown in BOLD (heavier weight than the body text "
               f"below it)?",
               depth=1, jump_kind="value", tag="content")


# ══════════════════════════════════════════════════════════════════════════════
# 2. Create note -> the body preview also shows on the card
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_create_body_preview",))
def test_create_body_preview():
    clear_data()
    restart_app()
    wait_for_foreground()
    absent_before = not has_text(ui(), M_BODY_B)
    if not create_note(M_BODY_T, M_BODY_B):
        record("interaction_create_body_preview", False,
               "feature_missing: could not create note with a body",
               depth=1, jump_kind="value", expect=f"body preview {M_BODY_B!r} on card")
        return
    ensure_home()
    n = ui()
    ok = absent_before and has_text(n, M_BODY_T) and has_text(n, M_BODY_B)
    record("interaction_create_body_preview", ok,
           "" if ok else "behavior_wrong: card body preview text did not appear",
           depth=1, jump_kind="value", expect=f"body preview {M_BODY_B!r} on card")


# ══════════════════════════════════════════════════════════════════════════════
# 3. Create TWO notes -> both cards present (multi-record integrity)
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_two_notes",
                   "interaction_two_notes_vlm"))
def test_two_notes():
    clear_data()
    restart_app()
    wait_for_foreground()
    ok1 = create_note(M_TWO_1, "aaa")
    ok2 = create_note(M_TWO_2, "bbb")
    ensure_home()
    n = ui()
    both = ok1 and ok2 and has_text(n, M_TWO_1) and has_text(n, M_TWO_2)
    record("interaction_two_notes", both,
           "" if both else "behavior_wrong: both created cards are not both present",
           depth=1, jump_kind="value",
           expect=f"cards {M_TWO_1!r} and {M_TWO_2!r} both on Home")
    # VLM: two distinct cards, laid out as a grid/stack (not one merged row).
    vlm_assert("interaction_two_notes",
               "Are there exactly two separate note cards laid out in the list "
               "area (arranged as a grid or stacked column), each a distinct "
               "rounded rectangle?",
               depth=1, jump_kind="value", tag="layout")


# ══════════════════════════════════════════════════════════════════════════════
# 4. Edit existing note body -> card body preview updates
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_edit_body",))
def test_edit_body():
    clear_data()
    restart_app()
    wait_for_foreground()
    if not create_note(M_EDIT_T, B_ORIG):
        record("interaction_edit_body", False,
               "setup_failed: could not create the note to edit",
               depth=2, jump_kind="value", expect=f"card body contains {M_EDIT_MARK!r}")
        return
    ensure_home()
    if not and_reach_preview(M_EDIT_T):
        record("interaction_edit_body", False,
               "setup_failed: could not open the note's Preview",
               depth=2, jump_kind="value", expect=f"card body contains {M_EDIT_MARK!r}")
        return
    if not and_enter_edit_from_preview():
        record("interaction_edit_body", False,
               "setup_failed: could not switch Preview -> Edit",
               depth=2, jump_kind="value", expect=f"card body contains {M_EDIT_MARK!r}")
        return
    ets = sorted_edittexts(ui())
    if len(ets) < 2:
        record("interaction_edit_body", False,
               "setup_failed: Description field not found in Edit",
               depth=2, jump_kind="value", expect=f"card body contains {M_EDIT_MARK!r}")
        return
    type_text(ets[1], " " + M_EDIT_MARK)     # append the distinctive marker
    save_edit()                                # Done -> Home (deterministic)
    n = ui()
    body = has_text(n, B_ORIG + " " + M_EDIT_MARK)
    ok = has_text(n, M_EDIT_T) and body
    record("interaction_edit_body", ok,
           "" if ok else "behavior_wrong: edited body preview did not update on the card",
           depth=2, jump_kind="value",
           expect=f"card {M_EDIT_T!r} body now '{B_ORIG} {M_EDIT_MARK}'")


# ══════════════════════════════════════════════════════════════════════════════
# 5. Delete via More menu -> card GONE (and placeholder returns if it was the last)
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_delete_via_menu",
                   "interaction_delete_via_menu_vlm"))
def test_delete_via_menu():
    clear_data()
    restart_app()
    wait_for_foreground()
    if not create_note(M_SOLO, "byebye"):
        record("interaction_delete_via_menu", False,
               "setup_failed: could not create the note to delete",
               depth=2, jump_kind="value", expect=f"{M_SOLO!r} gone; placeholder shown")
        record("interaction_delete_via_menu_vlm", False,
               "setup_failed: could not create note",
               depth=2, jump_kind="value", expect="empty-state placeholder", tag="layout")
        return
    ensure_home()
    present_before = has_text(ui(), M_SOLO)
    if not and_reach_preview(M_SOLO):
        record("interaction_delete_via_menu", False,
               "setup_failed: could not open Preview",
               depth=2, jump_kind="value", expect=f"{M_SOLO!r} gone; placeholder shown")
        record("interaction_delete_via_menu_vlm", False,
               "setup_failed: could not open Preview",
               depth=2, jump_kind="value", expect="empty-state placeholder", tag="layout")
        return
    if not and_enter_more_menu():
        record("interaction_delete_via_menu", False,
               "feature_missing: no More menu / Delete item",
               depth=2, jump_kind="value", expect=f"{M_SOLO!r} gone; placeholder shown")
        record("interaction_delete_via_menu_vlm", False,
               "feature_missing: no Delete item",
               depth=2, jump_kind="value", expect="empty-state placeholder", tag="layout")
        return
    n = ui()
    dele = find_node(n, text="Delete") or find_node(n, content_desc="Delete")
    if dele is None or not tap_node(dele):
        record("interaction_delete_via_menu", False,
               "feature_missing: Delete menu item not tappable",
               depth=2, jump_kind="value", expect=f"{M_SOLO!r} gone; placeholder shown")
        record("interaction_delete_via_menu_vlm", False,
               "feature_missing: Delete not tappable",
               depth=2, jump_kind="value", expect="empty-state placeholder", tag="layout")
        return
    settle("tap")
    ensure_home()                              # deleting from Preview backgrounds
    n = ui()
    gone = not has_text(n, M_SOLO)
    placeholder = find_node(n, text="No created notes.") is not None
    ok = present_before and gone and placeholder
    record("interaction_delete_via_menu", ok,
           "" if ok else "behavior_wrong: note not removed after menu Delete",
           depth=2, jump_kind="value",
           expect=f"{M_SOLO!r} gone from Home; 'No created notes.' shown")
    # VLM: empty-state placeholder (list icon + text) now shown.
    vlm_assert("interaction_delete_via_menu",
               "Does the screen show an empty-state placeholder — a centered icon "
               "with the message 'No created notes.' — and no note cards at all?",
               depth=2, jump_kind="value", tag="layout")


# ══════════════════════════════════════════════════════════════════════════════
# 6. Delete via menu leaves OTHER notes intact
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_delete_leaves_others",))
def test_delete_leaves_others():
    clear_data()
    restart_app()
    wait_for_foreground()
    create_note(M_KEEP_T, "keepbody")
    create_note(M_DEL_T, "delbody")
    ensure_home()
    if not (has_text(ui(), M_KEEP_T) and has_text(ui(), M_DEL_T)):
        record("interaction_delete_leaves_others", False,
               "setup_failed: could not create both notes",
               depth=2, jump_kind="value",
               expect=f"{M_KEEP_T!r} remains after deleting {M_DEL_T!r}")
        return
    if not and_reach_preview(M_DEL_T):
        record("interaction_delete_leaves_others", False,
               "setup_failed: could not open Preview of the delete target",
               depth=2, jump_kind="value",
               expect=f"{M_KEEP_T!r} remains after deleting {M_DEL_T!r}")
        return
    if not and_enter_more_menu():
        record("interaction_delete_leaves_others", False,
               "feature_missing: no More menu / Delete item",
               depth=2, jump_kind="value",
               expect=f"{M_KEEP_T!r} remains after deleting {M_DEL_T!r}")
        return
    n = ui()
    dele = find_node(n, text="Delete") or find_node(n, content_desc="Delete")
    if dele is None or not tap_node(dele):
        record("interaction_delete_leaves_others", False,
               "feature_missing: Delete menu item not tappable",
               depth=2, jump_kind="value",
               expect=f"{M_KEEP_T!r} remains after deleting {M_DEL_T!r}")
        return
    settle("tap")
    ensure_home()
    n = ui()
    ok = (not has_text(n, M_DEL_T)) and has_text(n, M_KEEP_T)
    record("interaction_delete_leaves_others", ok,
           "" if ok else "behavior_wrong: sibling note lost (or target survived) after delete",
           depth=2, jump_kind="value",
           expect=f"{M_DEL_T!r} gone, {M_KEEP_T!r} still on Home")


# ══════════════════════════════════════════════════════════════════════════════
# 7. Note persists across process death  (signature test)
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_note_persists",))
def test_note_persists():
    clear_data()
    restart_app()
    wait_for_foreground()
    absent_before = not has_text(ui(), M_PERSIST)
    if not create_note(M_PERSIST, "survive"):
        record("interaction_note_persists", False,
               "setup_failed: could not create the note",
               depth=0, jump_kind="value", expect=f"card {M_PERSIST!r} after relaunch")
        return
    ensure_home()
    present_before = has_text(ui(), M_PERSIST)
    force_stop_and_relaunch()
    wait_for_foreground()
    settle("tap")
    ensure_home()
    present_after = has_text(ui(), M_PERSIST)
    ok = absent_before and present_before and present_after
    record("interaction_note_persists", ok,
           "" if ok else "behavior_wrong: note did not survive process death",
           depth=0, jump_kind="value", expect=f"card {M_PERSIST!r} still on Home after relaunch")


# ══════════════════════════════════════════════════════════════════════════════
# 8. Edit persists across process death
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_edit_persists",))
def test_edit_persists():
    clear_data()
    restart_app()
    wait_for_foreground()
    if not create_note(M_EDITP_T, "beforeedit"):
        record("interaction_edit_persists", False,
               "setup_failed: could not create the note to edit",
               depth=2, jump_kind="value", expect=f"body 'beforeedit {M_EDIT_MARK}' after relaunch")
        return
    ensure_home()
    if not and_reach_preview(M_EDITP_T):
        record("interaction_edit_persists", False,
               "setup_failed: could not open Preview",
               depth=2, jump_kind="value", expect=f"body 'beforeedit {M_EDIT_MARK}' after relaunch")
        return
    if not and_enter_edit_from_preview():
        record("interaction_edit_persists", False,
               "setup_failed: could not enter Edit",
               depth=2, jump_kind="value", expect=f"body 'beforeedit {M_EDIT_MARK}' after relaunch")
        return
    ets = sorted_edittexts(ui())
    if len(ets) < 2:
        record("interaction_edit_persists", False,
               "setup_failed: Description field missing",
               depth=2, jump_kind="value", expect=f"body 'beforeedit {M_EDIT_MARK}' after relaunch")
        return
    type_text(ets[1], " " + M_EDIT_MARK)
    save_edit()                                # commit the edit to the DB first
    force_stop_and_relaunch()
    wait_for_foreground()
    settle("tap")
    ensure_home()
    n = ui()
    ok = has_text(n, M_EDITP_T) and has_text(n, "beforeedit " + M_EDIT_MARK)
    record("interaction_edit_persists", ok,
           "" if ok else "behavior_wrong: edited body did not survive process death",
           depth=2, jump_kind="value",
           expect=f"card {M_EDITP_T!r} body 'beforeedit {M_EDIT_MARK}' after relaunch")


# ══════════════════════════════════════════════════════════════════════════════
# 9. Long-press -> selection mode (SOLE OWNER of long-press)
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_longpress_selection",
                   "interaction_longpress_selection_vlm"))
def test_longpress_selection():
    clear_data()
    restart_app()
    wait_for_foreground()
    if not create_note(M_LP, "lp"):
        record("interaction_longpress_selection", False,
               "setup_failed: could not create a card to long-press",
               depth=1, jump_kind="transition", expect="selection bar: Close/Pin/Delete + count 1")
        record("interaction_longpress_selection_vlm", False,
               "setup_failed: could not create note",
               depth=1, jump_kind="transition", expect="selected card border", tag="color")
        return
    ensure_home()
    n = ui()
    before = and_showing(n)
    normal_mode = find_node(n, content_desc="Close") is None
    if not and_enter_selection(M_LP):
        record("interaction_longpress_selection", False,
               "feature_missing: long-press did not enter selection mode",
               depth=1, jump_kind="transition", expect="selection bar: Close/Pin/Delete + count 1")
        record("interaction_longpress_selection_vlm", False,
               "feature_missing: no selection mode",
               depth=1, jump_kind="transition", expect="selected card border", tag="color")
        return
    after = and_showing()
    n = ui()
    bar = (find_node(n, content_desc="Close") is not None
           and find_node(n, content_desc="Pin") is not None
           and find_node(n, content_desc="Delete") is not None)
    count1 = selection_count(n) == 1
    flipped = normal_mode and and_reach(before, after, "Close", "Settings")
    ok = bar and count1 and flipped
    record("interaction_longpress_selection", ok,
           "" if ok else "behavior_wrong: selection bar (Close/Pin/Delete + count 1) not shown",
           depth=1, jump_kind="transition",
           expect="selection top bar shows Close/Pin/Delete and count '1'")
    # VLM: the long-pressed card gains a coloured highlighted border outline.
    vlm_assert("interaction_longpress_selection",
               f"The note card '{M_LP}' is selected. Does that card have a "
               f"clearly visible coloured (blue/dark) BORDER outline around its "
               f"edges, distinguishing it as selected?",
               depth=1, jump_kind="transition", tag="color")


# ══════════════════════════════════════════════════════════════════════════════
# 10. Selection count increments 1 -> 2
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_selection_count",
                   "interaction_selection_count_vlm"))
def test_selection_count():
    clear_data()
    restart_app()
    wait_for_foreground()
    create_note(M_SEL1, "s1")
    create_note(M_SEL2, "s2")
    ensure_home()
    if not (has_text(ui(), M_SEL1) and has_text(ui(), M_SEL2)):
        record("interaction_selection_count", False,
               "setup_failed: could not create both notes",
               depth=1, jump_kind="value", expect="selection count 1 -> 2")
        record("interaction_selection_count_vlm", False,
               "setup_failed: could not create notes",
               depth=1, jump_kind="value", expect="both cards selected border", tag="color")
        return
    if not and_enter_selection(M_SEL1):
        record("interaction_selection_count", False,
               "feature_missing: long-press did not enter selection mode",
               depth=1, jump_kind="value", expect="selection count 1 -> 2")
        record("interaction_selection_count_vlm", False,
               "feature_missing: no selection mode",
               depth=1, jump_kind="value", expect="both cards selected border", tag="color")
        return
    c1 = selection_count(ui())
    # tap the OTHER card to add it to the selection
    other = find_card(ui(), M_SEL2)
    tapped = tap_node(other) if other else False
    settle("tap")
    c2 = selection_count(ui())
    ok = tapped and c1 == 1 and c2 == 2
    record("interaction_selection_count", ok,
           "" if ok else f"behavior_wrong: count did not go 1->2 (got {c1}->{c2})",
           depth=1, jump_kind="value", expect="selection count text '1' then '2'")
    # VLM: two cards, both now selected (both show a highlighted border).
    vlm_assert("interaction_selection_count",
               "Two note cards are selected. Do BOTH cards show a coloured "
               "(blue/dark) highlighted border outline indicating they are both "
               "selected?",
               depth=1, jump_kind="value", tag="color")


# ══════════════════════════════════════════════════════════════════════════════
# 11. Select-all with 2 notes -> count reaches total (2)
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_select_all",))
def test_select_all():
    clear_data()
    restart_app()
    wait_for_foreground()
    create_note(M_SA1, "sa1")
    create_note(M_SA2, "sa2")
    ensure_home()
    if not (has_text(ui(), M_SA1) and has_text(ui(), M_SA2)):
        record("interaction_select_all", False,
               "setup_failed: could not create both notes",
               depth=1, jump_kind="value", expect="count reaches 2 after Select All")
        return
    if not and_enter_selection(M_SA1):
        record("interaction_select_all", False,
               "feature_missing: long-press did not enter selection mode",
               depth=1, jump_kind="value", expect="count reaches 2 after Select All")
        return
    n = ui()
    sa = find_node(n, content_desc="Select All") or find_node_ci(n, "Select all")
    if sa is None:
        record("interaction_select_all", False,
               "feature_missing: 'Select All' control not present with 1 of 2 selected",
               depth=1, jump_kind="value", expect="count reaches 2 after Select All")
        return
    present_when_partial = True
    tap_node(sa)
    settle("tap")
    c = selection_count(ui())
    ok = present_when_partial and c == 2
    record("interaction_select_all", ok,
           "" if ok else f"behavior_wrong: Select All did not select all (count={c})",
           depth=1, jump_kind="value", expect="selection count '2' after tapping Select All")


# ══════════════════════════════════════════════════════════════════════════════
# 12. Delete via selection mode -> target gone, other remains
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_selection_delete",
                   "interaction_selection_delete_vlm"))
def test_selection_delete():
    clear_data()
    restart_app()
    wait_for_foreground()
    create_note(M_SDEL_KEEP, "keep")
    create_note(M_SDEL_DEL, "del")
    ensure_home()
    if not (has_text(ui(), M_SDEL_KEEP) and has_text(ui(), M_SDEL_DEL)):
        record("interaction_selection_delete", False,
               "setup_failed: could not create both notes",
               depth=1, jump_kind="value",
               expect=f"{M_SDEL_DEL!r} gone, {M_SDEL_KEEP!r} remains")
        record("interaction_selection_delete_vlm", False,
               "setup_failed: could not create notes",
               depth=1, jump_kind="content", expect="pin + trash icons", tag="content")
        return
    if not and_enter_selection(M_SDEL_DEL):
        record("interaction_selection_delete", False,
               "feature_missing: long-press did not enter selection mode",
               depth=1, jump_kind="value",
               expect=f"{M_SDEL_DEL!r} gone, {M_SDEL_KEEP!r} remains")
        record("interaction_selection_delete_vlm", False,
               "feature_missing: no selection mode",
               depth=1, jump_kind="content", expect="pin + trash icons", tag="content")
        return
    # VLM (before we destroy anything): the selection bar shows Pin + trash icons.
    vlm_assert("interaction_selection_delete",
               "In the top action bar of this selection mode, are BOTH a pin icon "
               "and a delete (trash can) icon visible on the right-hand side?",
               depth=1, jump_kind="content", tag="content")
    n = ui()
    dele = find_node(n, content_desc="Delete")
    if dele is None or not tap_node(dele):
        record("interaction_selection_delete", False,
               "feature_missing: selection-bar Delete not tappable",
               depth=1, jump_kind="value",
               expect=f"{M_SDEL_DEL!r} gone, {M_SDEL_KEEP!r} remains")
        return
    settle("tap")
    # confirmation dialog: "Are you sure ..." -> Yes
    yes = find_node(ui(), text="Yes")
    if yes is not None:
        tap_node(yes)
        settle("tap")
    ensure_home()
    n = ui()
    ok = (not has_text(n, M_SDEL_DEL)) and has_text(n, M_SDEL_KEEP)
    record("interaction_selection_delete", ok,
           "" if ok else "behavior_wrong: selection-delete removed wrong note(s)",
           depth=1, jump_kind="value",
           expect=f"{M_SDEL_DEL!r} gone, {M_SDEL_KEEP!r} still on Home")


# ══════════════════════════════════════════════════════════════════════════════
# 13. Markdown bullet insert -> Description value contains "- "
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_markdown_bullet",))
def test_markdown_bullet():
    clear_data()
    restart_app()
    wait_for_foreground()
    if not and_enter_editor():
        record("interaction_markdown_bullet", False,
               "setup_failed: could not open the editor",
               depth=1, jump_kind="value", expect="Description value contains '- '")
        return
    ets = sorted_edittexts(ui())
    if ets:
        type_text(ets[0], M_MK)
    toolbar = focus_description_toolbar()
    if toolbar is None:
        record("interaction_markdown_bullet", False,
               "feature_missing: markdown toolbar did not appear on Description focus",
               depth=1, jump_kind="value", expect="Description value contains '- '")
        return
    bullet = markdown_bullet_node(toolbar)
    if bullet is None or not tap_node(bullet):
        record("interaction_markdown_bullet", False,
               "feature_missing: Bullet List button not found/tappable",
               depth=1, jump_kind="value", expect="Description value contains '- '")
        return
    settle("tap")
    ets = sorted_edittexts(ui())
    desc_val = ets[1].get("text", "") if len(ets) > 1 else ""
    # XML encodes the inserted newline as &#10;; the "- " token is what we assert.
    ok = "- " in desc_val
    record("interaction_markdown_bullet", ok,
           "" if ok else f"behavior_wrong: bullet token not inserted (desc={desc_val!r})",
           depth=1, jump_kind="value", expect="Description value contains '- '")


# ══════════════════════════════════════════════════════════════════════════════
# 14. Markdown checkbox insert -> Description value contains "[ ] "
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_markdown_checkbox",))
def test_markdown_checkbox():
    clear_data()
    restart_app()
    wait_for_foreground()
    if not and_enter_editor():
        record("interaction_markdown_checkbox", False,
               "setup_failed: could not open the editor",
               depth=1, jump_kind="value", expect="Description value contains '[ ] '")
        return
    ets = sorted_edittexts(ui())
    if ets:
        type_text(ets[0], M_MK2)
    toolbar = focus_description_toolbar()
    if toolbar is None:
        record("interaction_markdown_checkbox", False,
               "feature_missing: markdown toolbar did not appear on Description focus",
               depth=1, jump_kind="value", expect="Description value contains '[ ] '")
        return
    cb = find_node(toolbar, content_desc="Checkbox")
    if cb is None or not tap_node(cb):
        record("interaction_markdown_checkbox", False,
               "feature_missing: Checkbox button not found/tappable",
               depth=1, jump_kind="value", expect="Description value contains '[ ] '")
        return
    settle("tap")
    ets = sorted_edittexts(ui())
    desc_val = ets[1].get("text", "") if len(ets) > 1 else ""
    ok = "[ ] " in desc_val
    record("interaction_markdown_checkbox", ok,
           "" if ok else f"behavior_wrong: checkbox token not inserted (desc={desc_val!r})",
           depth=1, jump_kind="value", expect="Description value contains '[ ] '")


# ══════════════════════════════════════════════════════════════════════════════
# 15. Empty note discarded -> no new card, placeholder stays
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_empty_discarded",))
def test_empty_note_discarded():
    clear_data()
    restart_app()
    wait_for_foreground()
    placeholder_before = find_node(ui(), text="No created notes.") is not None
    if not and_enter_editor():
        record("interaction_empty_discarded", False,
               "setup_failed: could not open the editor",
               depth=1, jump_kind="value", expect="'No created notes.' still shown")
        return
    # leave without typing anything -> should discard
    done = find_node(ui(), content_desc="Done") or find_node(ui(), content_desc="Back")
    if done:
        tap_node(done)
    settle("tap")
    ensure_home()
    n = ui()
    placeholder_after = find_node(n, text="No created notes.") is not None
    ok = placeholder_before and placeholder_after
    record("interaction_empty_discarded", ok,
           "" if ok else "behavior_wrong: empty note was NOT discarded",
           depth=1, jump_kind="value", expect="'No created notes.' still shown after empty note")


# ══════════════════════════════════════════════════════════════════════════════
# 16. Three notes -> edit the MIDDLE -> first & third intact and still present
# ══════════════════════════════════════════════════════════════════════════════
M_TRI_1 = "kitI_TriOne"
M_TRI_2 = "kitI_TriTwo"
M_TRI_3 = "kitI_TriThree"


@and_test(expects=("interaction_three_edit_middle",))
def test_three_edit_middle():
    clear_data()
    restart_app()
    wait_for_foreground()
    create_note(M_TRI_1, B_TRI[0])
    create_note(M_TRI_2, B_TRI[1])
    create_note(M_TRI_3, B_TRI[2])
    ensure_home()
    n = ui()
    if not (has_text(n, M_TRI_1) and has_text(n, M_TRI_2) and has_text(n, M_TRI_3)):
        record("interaction_three_edit_middle", False,
               "setup_failed: could not create all three notes",
               depth=2, jump_kind="value",
               expect=f"{M_TRI_1!r} & {M_TRI_3!r} intact after editing {M_TRI_2!r}")
        return
    # edit the middle note's body
    if not and_reach_preview(M_TRI_2):
        record("interaction_three_edit_middle", False,
               "setup_failed: could not open Preview of the middle note",
               depth=2, jump_kind="value",
               expect=f"{M_TRI_1!r} & {M_TRI_3!r} intact after editing {M_TRI_2!r}")
        return
    if not and_enter_edit_from_preview():
        record("interaction_three_edit_middle", False,
               "setup_failed: could not enter Edit for the middle note",
               depth=2, jump_kind="value",
               expect=f"{M_TRI_1!r} & {M_TRI_3!r} intact after editing {M_TRI_2!r}")
        return
    ets = sorted_edittexts(ui())
    if len(ets) >= 2:
        type_text(ets[1], " " + M_EDIT_MARK)
    save_edit()
    n = ui()
    middle_edited = has_text(n, B_TRI[1] + " " + M_EDIT_MARK)
    others_intact = (has_text(n, M_TRI_1) and has_text(n, M_TRI_3)
                     and has_text(n, B_TRI[0]) and has_text(n, B_TRI[2]))
    ok = middle_edited and others_intact
    record("interaction_three_edit_middle", ok,
           "" if ok else "behavior_wrong: neighbours lost/changed after editing the middle note",
           depth=2, jump_kind="value",
           expect=f"{M_TRI_2!r} body '{B_TRI[1]} {M_EDIT_MARK}'; {M_TRI_1!r}({B_TRI[0]}) & {M_TRI_3!r}({B_TRI[2]}) intact")


# ══════════════════════════════════════════════════════════════════════════════
# 17. Multi-record persistence: create 2, force-stop+relaunch, BOTH survive
# ══════════════════════════════════════════════════════════════════════════════
M_MP_1 = "kitI_MpOne"
M_MP_2 = "kitI_MpTwo"


@and_test(expects=("interaction_multi_persist",
                   "interaction_multi_persist_vlm"))
def test_multi_persist():
    clear_data()
    restart_app()
    wait_for_foreground()
    create_note(M_MP_1, "mp1")
    create_note(M_MP_2, "mp2")
    ensure_home()
    both_before = has_text(ui(), M_MP_1) and has_text(ui(), M_MP_2)
    if not both_before:
        record("interaction_multi_persist", False,
               "setup_failed: could not create both notes",
               depth=0, jump_kind="value", expect="both cards after process death")
        record("interaction_multi_persist_vlm", False,
               "setup_failed: could not create notes",
               depth=0, jump_kind="value", expect="two cards after relaunch", tag="layout")
        return
    force_stop_and_relaunch()
    wait_for_foreground()
    settle("tap")
    ensure_home()
    n = ui()
    ok = has_text(n, M_MP_1) and has_text(n, M_MP_2)
    record("interaction_multi_persist", ok,
           "" if ok else "behavior_wrong: not both notes survived process death",
           depth=0, jump_kind="value",
           expect=f"cards {M_MP_1!r} and {M_MP_2!r} both present after relaunch")
    # VLM: after relaunch, two cards are laid out in the list.
    vlm_assert("interaction_multi_persist",
               "After the app restarted, are there two note cards displayed in the "
               "list area (a grid or stacked column of two rounded cards)?",
               depth=0, jump_kind="value", tag="layout")


# ══════════════════════════════════════════════════════════════════════════════
# 18. Pin via More menu -> the note is grouped under a PINNED header (data effect)
# EasyNotes renders NO pin badge on the card; pinning instead splits Home into
# `PINNED` and `OTHERS` sections and moves the note under PINNED. That grouping is
# a dump-readable DATA consequence, so this is a rule (value) test, not a VLM one.
# ══════════════════════════════════════════════════════════════════════════════
M_PIN = "kitI_PinNote"
M_PIN_OTHER = "kitI_PinOther"


def _y_of(nodes, marker):
    n = find_card(nodes, marker)
    return n.get("_bounds", (0, 0, 10 ** 9, 10 ** 9))[1] if n else None


@and_test(expects=("interaction_pin_note",))
def test_pin_note():
    clear_data()
    restart_app()
    wait_for_foreground()
    create_note(M_PIN, "pinbody")
    create_note(M_PIN_OTHER, "otherbody")
    ensure_home()
    n = ui()
    if not (has_text(n, M_PIN) and has_text(n, M_PIN_OTHER)):
        record("interaction_pin_note", False,
               "setup_failed: could not create both notes to pin",
               depth=2, jump_kind="value",
               expect=f"{M_PIN!r} under a PINNED section header")
        return
    grouped_before = find_node_ci(n, "PINNED") is not None
    if not and_reach_preview(M_PIN):
        record("interaction_pin_note", False,
               "setup_failed: could not open Preview to pin",
               depth=2, jump_kind="value",
               expect=f"{M_PIN!r} under a PINNED section header")
        return
    if not and_enter_more_menu():
        record("interaction_pin_note", False,
               "feature_missing: no More menu / Pinned item",
               depth=2, jump_kind="value",
               expect=f"{M_PIN!r} under a PINNED section header")
        return
    n = ui()
    pin = find_node(n, text="Pinned") or find_node(n, content_desc="Pin")
    if pin is None or not tap_node(pin):
        record("interaction_pin_note", False,
               "feature_missing: Pinned menu item not tappable",
               depth=2, jump_kind="value",
               expect=f"{M_PIN!r} under a PINNED section header")
        return
    settle("tap")
    ensure_home()
    n = ui()
    pinned_hdr = find_node_ci(n, "PINNED")
    others_hdr = find_node_ci(n, "OTHERS")
    # the pinned note must sit BELOW the PINNED header and ABOVE the OTHERS header;
    # the other note must sit BELOW OTHERS -> a real regrouping, not just presence.
    grouped = pinned_hdr is not None and others_hdr is not None
    y_pin = _y_of(n, M_PIN)
    y_other = _y_of(n, M_PIN_OTHER)
    ordered = (grouped and y_pin is not None and y_other is not None
               and pinned_hdr.get("_bounds", (0, 0, 0, 0))[1] < y_pin < others_hdr.get("_bounds", (0, 0, 0, 0))[1] < y_other)
    ok = (not grouped_before) and grouped and ordered
    record("interaction_pin_note", ok,
           "" if ok else "behavior_wrong: pinned note not grouped under a PINNED header",
           depth=2, jump_kind="value",
           expect=f"'PINNED' header with {M_PIN!r} above the 'OTHERS' header holding {M_PIN_OTHER!r}")


# ══════════════════════════════════════════════════════════════════════════════
# 19. Editor surface VLM: the markdown toolbar row renders its icons
# ══════════════════════════════════════════════════════════════════════════════
@and_test(expects=("interaction_markdown_toolbar_vlm",))
def test_markdown_toolbar_vlm():
    clear_data()
    restart_app()
    wait_for_foreground()
    if not and_enter_editor():
        record("interaction_markdown_toolbar_vlm", False,
               "setup_failed: could not open the editor",
               depth=1, jump_kind="content", expect="formatting icon row", tag="position")
        return
    ets = sorted_edittexts(ui())
    if ets:
        type_text(ets[0], "TB")
    toolbar = focus_description_toolbar()
    if toolbar is None:
        record("interaction_markdown_toolbar_vlm", False,
               "feature_missing: markdown toolbar did not appear",
               depth=1, jump_kind="content", expect="formatting icon row", tag="position")
        return
    # VLM: a horizontal row of formatting icons sits just above the keyboard.
    vlm_assert("interaction_markdown_toolbar",
               "Is there a horizontal row of formatting icons (such as a bullet-"
               "list icon, a checkbox icon and an image icon) positioned above the "
               "on-screen keyboard, below the note text area?",
               depth=1, jump_kind="content", tag="position")


TESTS = [
    test_create_note,
    test_create_body_preview,
    test_two_notes,
    test_edit_body,
    test_delete_via_menu,
    test_delete_leaves_others,
    test_note_persists,
    test_edit_persists,
    test_longpress_selection,
    test_selection_count,
    test_select_all,
    test_selection_delete,
    test_markdown_bullet,
    test_markdown_checkbox,
    test_empty_note_discarded,
    test_three_edit_middle,
    test_multi_persist,
    test_pin_note,
    test_markdown_toolbar_vlm,
]


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