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
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
require_relative "../../test_helper"
require_relative "../../photo_card"
require "date"
require "rack/test"
require "pro_tacts/vcard"
require "pro_tacts/web"
# The dashboard root (docs/DESIGN.md), exercised the same way WebTest
# exercises the CardDAV routes: real requests through the Roda app,
# against a throwaway store. The birthdays column reads the real
# clock, so its cards are built around Date.today rather than a fixed
# date.
class AdminContactsPagesTest < Minitest::Test
include Rack::Test::Methods
include SentryTracing
include ThrowawayContacts
def app
traced(ProTacts::Web)
end
def setup
header "Remote-User", "test@example.com"
end
ADA = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Ada Lovelace\r\nN:Lovelace;Ada;;;\r\n" \
"TEL;TYPE=mobile:+1-555-0100\r\nEMAIL;TYPE=home:ada@example.com\r\n" \
"ADR;TYPE=home:;;12 Analytical Way;London;England;NW1 1AA;United Kingdom\r\n" \
"BDAY:1985-12-10\r\nNOTE:Countess of Lovelace.\r\nUID:ada\r\nEND:VCARD\r\n"
HOUSEHOLD = [
"ADR;TYPE=home:;;7 Calculus Close;London;England;NW1 1AB;United Kingdom",
"NOTE:Gate code 1854.",
].freeze #: Array[String]
# The birthday row's calendar affordance
# (docs/plans/2026-09-15-a-picker-beside-the-birthday.md): a date
# input the row never shows and never submits, and the button that
# opens its native picker. Byte-identical in the standing row and
# the one the add dialog reveals — the picker prefills from the
# three boxes rather than from anything rendered, so there is
# nothing for the two callers to differ about — which is why the
# two tests over it share this rather than each spelling it out.
BIRTHDAY_PICKER =
%(<input type="date" tabindex="-1" aria-hidden="true" @change="if (!$el.value) return; ) +
%(const r = $el.closest('.date-row'), n = r.querySelectorAll('input[type=number]'), ) +
%([y, m, d] = $el.value.split('-'); ) +
%(r.querySelector('select').value = +m; n[0].value = +d; n[1].value = +y">) +
%(<button type="button" class="icon-button" data-size="sm" ) +
%(aria-label="Pick a birthday from a calendar" ) +
%(@click="const r = $el.closest('.date-row'), n = r.querySelectorAll('input[type=number]'), ) +
%(p = r.querySelector('input[type=date]'); ) +
%(p.value = `${n[1].value.padStart(4, '0')}-${r.querySelector('select').value.padStart(2, '0')}) +
%(-${n[0].value.padStart(2, '0')}`; p.showPicker()">) +
%(<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" ) +
%(stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden>) +
%(<path d="M8 2v4"></path><path d="M16 2v4"></path>) +
%(<rect width="18" height="18" x="3" y="4" rx="2"></rect><path d="M3 10h18"></path></svg></button>) #: String
# The month select's fit to its room
# (Admin::ContactsEdit#birthday_controls): short names in each
# option's label while the select is narrower than its full names,
# asked again on every resize. Shared by the standing row and the
# added one for the picker's reason above.
BIRTHDAY_MONTH_FIT = <<~JS #: String
const months = $el.querySelectorAll('option[data-short]'), s = $el.style, room = $el.offsetWidth
months.forEach(o => o.removeAttribute('label'))
s.position = 'absolute'; s.width = 'max-content'
const need = $el.offsetWidth; s.position = s.width = ''
if (room < need) months.forEach(o => o.label = o.dataset.short)
JS
def test_index_lists_recently_updated_contacts
with_contacts({"ada" => ADA}) do
get "/"
assert_equal 200, last_response.status
assert_equal "text/html; charset=utf-8", last_response["Content-Type"]
assert_includes last_response.body, "Ada Lovelace"
assert_includes last_response.body, "recently updated"
end
end
# A card with a NICKNAME lists as "Name (Nickname)" — the formal
# name with the one someone is known by beside it. The plain-name
# row is what every other index test already shows.
def test_index_lists_a_nicknamed_contact_as_name_and_nickname
red = ADA.sub("FN:Ada Lovelace", "FN:Sarah\r\nNICKNAME:Red").sub("UID:ada", "UID:red")
with_contacts({"red" => red}) do
get "/"
assert_includes last_response.body, "Sarah (Red)"
end
end
def test_index_with_no_contacts_says_so
with_contacts({}) { get "/" }
assert_includes last_response.body, "No contacts yet."
end
## The whole-set listing (GET /contacts)
# Sorted by N's family name whatever its case, and a card with no N
# under its FN — the fallback chain the avatar's initials walk too
# (Format.initials). Seeded out of order to prove the sort.
def test_contacts_lists_every_contact_sorted_by_last_name
boole = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:George Boole\r\nN:Boole;George;;;\r\nUID:boole\r\nEND:VCARD\r\n"
shelley = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Mary Shelley\r\nN:shelley;Mary;;;\r\nUID:shelley\r\nEND:VCARD\r\n"
zed = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Zed Aileron\r\nUID:zed\r\nEND:VCARD\r\n"
with_contacts({"shelley" => shelley, "zed" => zed, "boole" => boole}) do
get "/contacts"
assert_equal 200, last_response.status
assert_equal "text/html; charset=utf-8", last_response["Content-Type"]
assert_includes last_response.body, "<title>pro-tacts — Contacts</title>"
assert_includes last_response.body, "contacts (3)"
assert_includes last_response.body, %(<a href="/contacts/boole" class="row-link">)
body = last_response.body
assert body.index("George Boole") < body.index("Mary Shelley")
assert body.index("Mary Shelley") < body.index("Zed Aileron")
end
end
def test_contacts_files_each_contact_under_the_first_letter_of_its_last_name
boole = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:George Boole\r\nN:Boole;George;;;\r\nUID:boole\r\nEND:VCARD\r\n"
babbage = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Charles Babbage\r\nN:Babbage;Charles;;;\r\nUID:babbage\r\nEND:VCARD\r\n"
shelley = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Mary Shelley\r\nN:shelley;Mary;;;\r\nUID:shelley\r\nEND:VCARD\r\n"
with_contacts({"shelley" => shelley, "boole" => boole, "babbage" => babbage}) do
get "/contacts"
body = last_response.body
assert_equal 2, body.scan(%(<ul class="card">)).length
assert body.index(%(<h3 class="type-label">B</h3>)) < body.index("Charles Babbage")
assert body.index("George Boole") < body.index(%(<h3 class="type-label">S</h3>))
assert body.index(%(<h3 class="type-label">S</h3>)) < body.index("Mary Shelley")
end
end
def test_contacts_files_a_card_with_no_last_name_under_its_name
zed = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Zed Aileron\r\nUID:zed\r\nEND:VCARD\r\n"
with_contacts({"zed" => zed}) do
get "/contacts"
assert_includes last_response.body, %(<h3 class="type-label">Z</h3>)
end
end
def test_contacts_files_an_accented_last_name_under_its_base_letter
eve = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Eve Evans\r\nN:Evans;Eve;;;\r\nUID:eve\r\nEND:VCARD\r\n"
emile = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Émile Écrivain\r\nN:Écrivain;Émile;;;\r\nUID:emile\r\nEND:VCARD\r\n"
with_contacts({"emile" => emile, "eve" => eve}) do
get "/contacts"
body = last_response.body
assert_equal 1, body.scan(%(<ul class="card">)).length
assert_includes body, %(<h3 class="type-label">E</h3>)
assert body.index("Eve Evans") < body.index("Émile Écrivain")
end
end
def test_contacts_files_a_last_name_that_is_no_letter_under_a_hash_after_z
agency = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:3M\r\nUID:agency\r\nEND:VCARD\r\n"
zed = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Zed Aileron\r\nN:Zuse;Zed;;;\r\nUID:zed\r\nEND:VCARD\r\n"
with_contacts({"agency" => agency, "zed" => zed}) do
get "/contacts"
body = last_response.body
assert body.index(%(<h3 class="type-label">Z</h3>)) < body.index(%(<h3 class="type-label">#</h3>))
assert body.index(%(<h3 class="type-label">#</h3>)) < body.index("3M")
end
end
# The row shows the same composition the dashboard's rows do
# (Format.name_label).
def test_contacts_lists_a_nicknamed_contact_as_name_and_nickname
red = ADA.sub("FN:Ada Lovelace", "FN:Sarah\r\nNICKNAME:Red").sub("UID:ada", "UID:red")
with_contacts({"red" => red}) do
get "/contacts"
assert_includes last_response.body, "Sarah (Red)"
end
end
# The row says what the contact belongs to, as chips under the name,
# each opening its group the way the record page's tags do.
def test_contacts_chips_each_row_with_links_to_its_groups
with_contacts({"ada" => ADA}) do |store|
group_id = FixtureData.seed_group(store, name: "Booles", members: ["ada"])
get "/contacts"
assert_includes last_response.body,
%(<a href="/groups/#{group_id}" class="tag">Booles</a><ol role="tooltip"><li>Ada Lovelace</li></ol>)
end
end
def test_contacts_rows_without_groups_carry_no_chips
with_contacts({"ada" => ADA}) { get "/contacts" }
refute_includes last_response.body, %(class="tag-set")
end
def test_contacts_lists_a_nameless_card_under_its_id
nameless = "BEGIN:VCARD\r\nVERSION:3.0\r\nUID:nameless\r\nEND:VCARD\r\n"
with_contacts({"nameless" => nameless}) do
get "/contacts"
assert_includes last_response.body, "nameless"
end
end
# Finding a contact is the header's search, which matches more than
# a name; the list has no filter of its own to disagree with it.
def test_contacts_has_no_filter_of_its_own
with_contacts({"ada" => ADA}) { get "/contacts" }
refute_includes last_response.body, "Filter contacts"
refute_includes last_response.body, "data-label="
end
def test_contacts_with_no_contacts_says_so
with_contacts({}) { get "/contacts" }
assert_includes last_response.body, "contacts (0)"
assert_includes last_response.body, "No contacts yet."
end
# The link's answer: every stored card's bytes in id order — the
# same read rake db:dump makes (Store#snapshot) — joined whole, each
# card ending in its own final newline so the cards never run
# together. The stored bytes are the assertion's truth, not the
# input: a write composes the modeled BDAY back in.
def test_the_export_serves_every_stored_card_as_one_vcard_file
grace = ADA.sub("Ada Lovelace", "Grace Hopper").sub("UID:ada", "UID:grace")
with_contacts({"grace" => grace, "ada" => ADA}) do |store|
get "/contacts/export.vcf"
assert_equal 200, last_response.status
assert_equal "text/vcard; charset=utf-8", last_response["Content-Type"]
assert_equal 'attachment; filename="contacts.vcf"', last_response["Content-Disposition"]
assert_equal store.snapshot.cards.values.join, last_response.body
assert_equal 2, last_response.body.scan("BEGIN:VCARD").length
end
end
# A contact born on `date`'s month and day in 2000, so the
# birthdays column's ordering and labels are deterministic against
# the real clock.
def born(date, name:, id:)
"BEGIN:VCARD\r\nVERSION:3.0\r\nFN:#{name}\r\n" \
"BDAY:2000-%02d-%02d\r\nUID:#{id}\r\nEND:VCARD\r\n" % [date.month, date.day]
end
# The ambient column answers "who is next": names in arrival order,
# each row opening the contact's card, the birthday shown with the
# age it turns and the day counted down beside it.
def test_index_lists_birthdays_in_arrival_order_linking_to_the_contact
tomorrow = born(Date.today + 1, name: "Sooner Person", id: "sooner")
fortnight = born(Date.today + 13, name: "Later Person", id: "later")
with_contacts({"later" => fortnight, "sooner" => tomorrow}) do
get "/"
body = last_response.body
assert_includes body, "upcoming birthdays"
# Both contacts also sit in "recently updated", where a
# same-millisecond tie can order them either way — arrival
# order is asserted inside the birthdays column alone.
birthdays = body.split("upcoming birthdays").last
assert birthdays.index("Sooner Person") < birthdays.index("Later Person")
# The rows are CardRows — li under ul.card, where the row look
# lives — not bare anchors.
assert_includes body, '<li><a href="/contacts/sooner">'
assert_includes body, "(turns #{(Date.today + 1).year - 2000})"
assert_includes body, "tomorrow"
assert_includes body, "in 13d"
end
end
# A birthday without a year is on the day — shown with no age, the
# one thing a missing year cannot answer.
def test_index_shows_a_birthday_without_a_year_with_no_age
week = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:No Year\r\n" \
"BDAY;X-APPLE-OMIT-YEAR=1604:1604-%02d-%02d\r\nUID:noyear\r\nEND:VCARD\r\n" %
[(Date.today + 7).month, (Date.today + 7).day]
with_contacts({"noyear" => week}) do
get "/"
body = last_response.body
assert_includes body, "No Year"
assert_includes body, "in 7d"
refute_includes body, "turns"
end
end
def test_index_with_no_birthdays_says_so
with_contacts({}) { get "/" }
assert_includes last_response.body, "No birthdays to show."
end
# The caption row's height comes from .record-nav's own inherited
# line box, not from the type-label inside it (see admin.css), so
# the two record pages have to open the block the same way. A bare
# link on one of them is the shorter row, and the card under it —
# along with the line itself — moves when you switch modes.
def test_both_record_pages_open_the_block_with_the_same_row
with_contacts({"ada" => ADA}) do
get "/contacts/ada"
show = last_response.body
get "/contacts/ada/edit"
edit = last_response.body
{"details" => show, "edit" => edit}.each do |page, body|
record = body.split('<div class="record">').last
assert_match(/\A<div class="record-nav"><a href="[^"]+" class="type-label">/,
record, "the #{page} page opens .record with a different row")
end
end
end
# This app is reachable only over Tailscale, so Alpine is vendored
# (tasks/alpine.rake) and there is no webfont: --gl-font-mono falls
# through to the platform's monospace. The IBM Plex refutation
# guards against @font-face rules pointing at files that 404.
def test_the_layout_asks_for_nothing_off_this_host
with_contacts({}) do
get "/"
head = last_response.body.split("</head>").first
refute_includes head, "fonts.googleapis.com"
refute_includes head, "fonts.gstatic.com"
refute_includes head, "ibm-plex-mono"
assert_includes head,
'<script defer src="/vendor/alpine/resize.min.js"></script>' \
'<script defer src="/vendor/alpine/alpine.min.js"></script>'
assert_empty head.scan(%r{(?:href|src)="(https?:)?//[^"]*"}),
"the layout links something off this host"
end
end
# The search rides every screen's header (docs/DESIGN.md), the
# dashboard's and a record's alike — finding a contact never
# requires going home first. The dashboard focuses nothing on
# load: the search is a button and `/` away (Admin::SearchDialog).
def test_the_search_renders_on_every_screen
with_contacts({"ada" => ADA}) do
["/", "/contacts/ada"].each do |path|
get path
header = last_response.body.split("<main").first
assert_includes header, %(popovertarget="search"), path
assert_includes header, %(<dialog id="search" popover="auto"), path
end
end
end
# The dialog fetches the recently updated list as it opens with
# nothing typed, the list GET /search/results answers a blank query
# (Admin::SearchField), so the box is never empty.
def test_the_search_dialog_fetches_as_it_opens
with_contacts({"ada" => ADA}) do
get "/"
dialog = last_response.body[/<dialog id="search".*?<\/dialog>/m]
assert_includes dialog, "addEventListener('toggle'"
assert_includes dialog, "opened() { if (this.q.trim() === '') this.search() }"
end
end
# A stale ?q= link lands on recency like any other visit.
def test_the_dashboard_ignores_a_query
grace = ADA.sub("Ada Lovelace", "Grace Hopper").sub("UID:ada", "UID:grace")
with_contacts({"ada" => ADA, "grace" => grace}) do
get "/", q: "hopper"
assert_includes last_response.body, %(<h2 class="type-label">recently updated</h2>)
assert_includes last_response.body, "Ada Lovelace"
end
end
# GET /search/results is the field's list, not a page: no layout
# around it.
def test_search_answers_a_fragment
with_contacts({"ada" => ADA}) do
get "/search/results", q: "ada"
assert_equal 200, last_response.status
assert_equal "text/html; charset=utf-8", last_response["Content-Type"]
assert last_response.body.start_with?(%(<h2 class="type-label">contacts</h2>))
refute_includes last_response.body, "<html"
assert_includes last_response.body, %(<a href="/contacts/ada">)
end
end
# Each step of a search is a span on the request's transaction, and
# none of them carries the query (Web's search branch).
def test_search_times_its_steps_without_the_query
setup_sentry_tracing
with_contacts({"ada" => ADA}) do
get "/search/results", q: "lovelace"
transactions = sentry_events.grep(Sentry::TransactionEvent)
assert_equal 1, transactions.length
transaction = transactions.first
spans = transaction.spans.select { it[:op].start_with?("search.") }
assert_equal %w[search.search_keys_by_recency search.all_groups search.match search.contacts_by_recency search.render], spans.map { it[:op] }
assert_equal({"otel.kind" => :internal, "words" => 1, "contacts" => 1}, spans.find { it[:op] == "search.match" }[:data])
refute_includes JSON.generate(transaction.to_h), "lovelace"
end
ensure
teardown_sentry_test
end
# A blank query answers the recently updated list, the search page
# showing it before a word is typed and again once the input is
# cleared.
def test_search_with_a_blank_query_answers_the_recent_contacts
with_contacts({"ada" => ADA}) do
get "/search/results", q: " "
assert_equal 200, last_response.status
assert last_response.body.start_with?(%(<h2 class="type-label">recently updated</h2>))
assert_includes last_response.body, %(<a href="/contacts/ada">)
end
end
# The page a phone's header links to (Admin::SearchPage): the field
# in a form back to itself, focused, over the recently updated list
# until a word is typed.
def test_the_search_page_renders_the_field
with_contacts({"ada" => ADA}) do
get "/search"
assert_equal 200, last_response.status
main = last_response.body.split("<main").last
assert_includes main, %(<form action="/search" method="get" class="search-page">)
assert_includes main, %(<input type="search" name="q" value="" placeholder="Search contacts")
assert_includes main, "autofocus"
assert_includes main, %(<h2 class="type-label">recently updated</h2>)
assert_includes main, %(<a href="/contacts/ada">)
end
end
# On the search page a phone's header glyph goes home rather than
# back to the page it is on.
def test_the_search_page_trades_the_phone_search_link_for_home
with_contacts({}) do
get "/search"
header = last_response.body.split("<main").first
assert_includes header, %(<a href="/" class="icon-button phone-glyph" aria-label="Home">)
refute_includes header, %(<a href="/search" class="icon-button phone-glyph")
get "/"
header = last_response.body.split("<main").first
assert_includes header, %(<a href="/search" class="icon-button phone-glyph" aria-label="Search">)
end
end
# A query in the address renders its results with the page, so a
# submitted search answers without script and going back to the
# page lands on the results it showed.
def test_the_search_page_renders_the_results_of_its_query
with_contacts({"ada" => ADA}) do
get "/search", q: "ada"
main = last_response.body.split("<main").last
assert_includes main, %(value="ada")
assert_includes main, %(<a href="/contacts/ada">)
end
end
def test_search_narrows_to_matches_across_contacts
grace = ADA.sub("Ada Lovelace", "Grace Hopper")
.sub("ada@example.com", "grace@example.com")
.sub("UID:ada", "UID:grace")
with_contacts({"ada" => ADA, "grace" => grace}) do
get "/search/results", q: "hopper"
assert_includes last_response.body, "Grace Hopper"
refute_includes last_response.body, "Ada Lovelace"
end
end
# Match generously (docs/DESIGN.md): a contact is findable by name,
# any of its values, or the groups it belongs to.
def test_search_matches_a_phone_or_email_value
with_contacts({"ada" => ADA}) do
get "/search/results", q: "555-0100"
assert_includes last_response.body, "Ada Lovelace"
get "/search/results", q: "ada@example.com"
assert_includes last_response.body, "Ada Lovelace"
end
end
# The nickname is a name the contact lists under, so it finds the
# contact the same way the name does.
def test_search_matches_the_nickname
red = ADA.sub("FN:Ada Lovelace", "FN:Sarah\r\nNICKNAME:Red").sub("UID:ada", "UID:red")
with_contacts({"red" => red}) do
get "/search/results", q: "red"
assert_includes last_response.body, "Sarah (Red)"
end
end
# ORG is a name a person works under: it finds them the way a
# nickname does. An organization card's own name is its FN, which
# the name match already reads.
def test_search_matches_the_organization
employed = ADA.sub("UID:ada", "UID:employed").sub("END:VCARD", "ORG:Acme Corp\r\nEND:VCARD")
with_contacts({"employed" => employed}) do
get "/search/results", q: "acme"
assert_includes last_response.body, "Ada Lovelace"
end
end
# Both sides of the match fold (Format.search_key): a query without
# the accents finds a name with them, and a query with them a name
# without.
def test_search_folds_accents
emile = ADA.sub("FN:Ada Lovelace", "FN:Émile Muñoz")
.sub("N:Lovelace;Ada;;;", "N:Muñoz;Émile;;;").sub("UID:ada", "UID:emile")
jose = ADA.sub("FN:Ada Lovelace", "FN:Jose Alvarez")
.sub("N:Lovelace;Ada;;;", "N:Alvarez;Jose;;;").sub("UID:ada", "UID:jose")
with_contacts({"emile" => emile, "jose" => jose}) do
get "/search/results", q: "munoz"
assert_includes last_response.body, "Émile Muñoz"
refute_includes last_response.body, "Jose Alvarez"
get "/search/results", q: "josé"
assert_includes last_response.body, "Jose Alvarez"
end
end
# Past the cap, the rest are counted rather than listed.
def test_search_lists_up_to_its_limit_and_counts_the_rest
limit = ProTacts::Admin::SearchResults::LIMIT
cards = (limit + 2).times.to_h { ["ada#{it}", ADA.sub("Ada Lovelace", "Ada #{it}").sub("UID:ada", "UID:ada#{it}")] }
with_contacts(cards) do
get "/search/results", q: "ada"
assert_equal limit, last_response.body.scan("<li").length
assert_includes last_response.body, "2 more. Keep typing to narrow them down."
end
end
def test_search_with_no_matches_says_so
with_contacts({"ada" => ADA}) do
get "/search/results", q: "nobody"
assert_includes last_response.body, "Nothing matches."
end
end
def test_show_renders_the_contacts_fields
with_contacts({"ada" => ADA}) do
get "/contacts/ada"
assert_equal 200, last_response.status
assert_includes last_response.body, "Ada Lovelace"
assert_includes last_response.body, "mobile"
assert_includes last_response.body, "+1-555-0100"
assert_includes last_response.body, "12 Analytical Way"
assert_includes last_response.body, "London"
assert_includes last_response.body, "December 10, 1985"
assert_includes last_response.body, "Countess of Lovelace."
end
end
# A member whose own card already carries the address its group
# lends — the address was on the card before the join — shows both
# rows, and only the composed copy is marked as the group's: the
# member's own reads as the member's own (the row's lent_by).
def test_show_marks_only_the_lent_copy_of_an_address_the_member_also_holds
with_contacts({"ada" => ADA}) do |store|
own = ADA.sub("ADR;TYPE=home:;;12 Analytical Way;London;England;NW1 1AA;United Kingdom", HOUSEHOLD.fetch(0))
store.put("ada", ProTacts::VCard.new(own))
id = FixtureData.seed_group(store, name: "Booles", members: ["ada"], lines: [HOUSEHOLD.fetch(0)])
get "/contacts/ada"
rows = last_response.body.scan(%r{<dt class="type-label">home</dt><dd class="type-body-sm">.*?</dd>}m)
.select { it.include?("7 Calculus Close") }
assert_equal 2, rows.size
refute_includes rows.fetch(0), %(class="tag")
assert_includes rows.fetch(1), %(<a href="/groups/#{id}" class="tag">Booles</a>)
end
end
# A row a group lends is marked with a tag opening the group, and the
# contact's own rows are not: Ada carries an address and a note of
# her own beside the household's two, and only the household's are
# marked.
def test_show_marks_the_rows_a_group_lends
with_contacts({"ada" => ADA}) do |store|
id = FixtureData.seed_group(store, name: "Booles", members: ["ada"], lines: HOUSEHOLD)
get "/contacts/ada"
marks = last_response.body.scan(%(<a href="/groups/#{id}" class="tag">Booles</a><ol role="tooltip"><li>Ada Lovelace</li></ol></span></dd>))
assert_equal 2, marks.size
assert_includes last_response.body, "7 Calculus Close"
assert_includes last_response.body, "Gate code 1854."
assert_includes last_response.body, "Countess of Lovelace."
end
end
# The card says what the contact belongs to, as tags: the group
# lending it two rows and the group lending it nothing both appear,
# because the row is membership rather than what came of it. Every
# tag opens the group it names (docs/DESIGN.md, "Relationships are
# navigable").
def test_show_tags_the_groups_a_contact_belongs_to
with_contacts({"ada" => ADA}) do |store|
named = FixtureData.seed_group(store, name: "Booles", members: ["ada"], lines: HOUSEHOLD)
nameless = FixtureData.seed_group(store, members: ["ada"], lines: [])
get "/contacts/ada"
tag_set = last_response.body[%r{<div class="tag-set">.*?</div>}].to_s
tags = tag_set.scan(%r{<a href="/groups/([^"]*)" class="tag">(.*?)</a>})
assert_equal({named => "Booles", nameless => %(<span data-nameless>#{nameless}</span>)}.sort, tags)
# The grid leads with what the contact belongs to, so the values
# under it are read already knowing whose they might be.
assert_equal "groups", last_response.body[%r{<dt class="type-label">([^<]*)<}, 1]
end
end
# A group with no name marks its rows with its id, which is what the
# mark is for: an empty tag would say a row came from somewhere and
# then not say where.
def test_show_marks_a_nameless_groups_rows_with_its_id
with_contacts({"ada" => ADA}) do |store|
id = FixtureData.seed_group(store, members: ["ada"], lines: HOUSEHOLD)
get "/contacts/ada"
marks = last_response.body.scan(%r{<a href="/groups/#{id}" class="tag">((?:(?!</a>).)*)</a><ol role="tooltip"><li>[^<]*</li></ol></span></dd>})
assert_equal [[%(<span data-nameless>#{id}</span>)]] * 2, marks
end
end
# In the group page's order, by family name.
def test_a_tag_names_the_first_five_members_and_counts_the_rest
families = %w[Grey Abel Fox Cole Eddy Baker Drew]
cards = families.to_h {
[it.downcase, "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Sam #{it}\r\nN:#{it};Sam;;;\r\nUID:#{it.downcase}\r\nEND:VCARD\r\n"]
}
with_contacts(cards) do |store|
FixtureData.seed_group(store, name: "Club", members: cards.keys)
get "/contacts/grey"
names = last_response.body[%r{<ol role="tooltip">(.*?)</ol>}, 1].to_s.scan(%r{<li>(.*?)</li>}).flatten
assert_equal ["Sam Abel", "Sam Baker", "Sam Cole", "Sam Drew", "Sam Eddy", "and 2 more"], names
end
end
# A contact in no group is marked nowhere: the tag is provenance,
# not decoration, so a card with nothing lent renders none.
def test_show_marks_nothing_on_a_contact_in_no_group
with_contacts({"ada" => ADA}) do
get "/contacts/ada"
refute_includes last_response.body, 'class="tag"'
end
end
# The row holds the way to join a group or start one, so it renders
# before any group exists.
def test_show_offers_the_groups_dialog_before_any_group_exists
with_contacts({"ada" => ADA}) do
get "/contacts/ada"
assert_includes last_response.body, %(<dt class="type-label">groups</dt>)
assert_includes last_response.body, %(popovertarget="edit-groups")
assert_includes last_response.body, %(<dialog id="edit-groups")
end
end
def test_the_groups_dialogs_filter_offer_commits_its_name_on_the_tick
with_contacts({"ada" => ADA}) do
get "/contacts/ada"
dialog = last_response.body[%r{<dialog id="edit-groups".*?</dialog>}].to_s
assert_includes dialog,
%(<input type="checkbox" @change="if ($event.target.checked) named.push(filter.trim())">)
# And somewhere for the committed name to stand, inside the form
# so the save carries it.
assert_includes dialog, %(<template x-for="name in named">)
assert_includes dialog, %(name="named[]" :value="name" checked)
end
end
def test_the_groups_dialog_checks_the_groups_a_contact_is_in
with_contacts({"ada" => ADA}) do |store|
joined = FixtureData.seed_group(store, name: "Booles", members: ["ada"])
other = FixtureData.seed_group(store, name: "Babbages")
get "/contacts/ada"
dialog = last_response.body[%r{<dialog id="edit-groups".*?</dialog>}].to_s
assert_includes dialog, %(name="groups[]" value="#{joined}" checked>)
assert_includes dialog, %(name="groups[]" value="#{other}">)
end
end
def test_the_groups_dialog_leads_with_the_sync_groups
with_contacts({"ada" => ADA}) do |store|
%w[zoetrope Abacus sync:alpha barometer sync:*].each { FixtureData.seed_group(store, name: it) }
ids = store.all_groups.to_h { [it.name, it.id] }
get "/contacts/ada"
dialog = last_response.body[%r{<dialog id="edit-groups".*?</dialog>}].to_s
assert_equal %w[sync:* sync:alpha Abacus barometer zoetrope].map { ids.fetch(it) },
dialog.scan(/name="groups\[\]" value="([^"]+)"/).flatten
end
end
# The cap (docs/plans/2026-09-22-a-few-groups-at-a-time.md): eight
# rows stand shown and the ninth is marked, rendered for the filter
# to reach rather than left out of the page.
def test_the_groups_dialog_shows_a_few_groups_at_a_time
with_contacts({"ada" => ADA}) do |store|
("a".."i").each { FixtureData.seed_group(store, name: "Group #{it}") }
get "/contacts/ada"
dialog = last_response.body[%r{<dialog id="edit-groups".*?</dialog>}].to_s
assert_includes dialog, %(<label data-label="group h" :hidden="!visible($el)">)
assert_includes dialog, %(<label data-label="group i" data-capped :hidden="!visible($el)">)
assert_equal 1, dialog.scan("data-capped").length
end
end
# A group the contact is in takes a row from the cap rather than
# standing outside it: nine groups, the last of them joined, and the
# eighth is what goes under.
def test_the_groups_dialog_caps_nothing_a_contact_is_in
with_contacts({"ada" => ADA}) do |store|
("a".."h").each { FixtureData.seed_group(store, name: "Group #{it}") }
FixtureData.seed_group(store, name: "Zulus", members: ["ada"])
get "/contacts/ada"
dialog = last_response.body[%r{<dialog id="edit-groups".*?</dialog>}].to_s
assert_includes dialog, %(<label data-label="zulus" :hidden="!visible($el)">)
assert_includes dialog, %(<label data-label="group h" data-capped :hidden="!visible($el)">)
assert_equal 1, dialog.scan("data-capped").length
end
end
# The button counts the whole list, and renders only where a row is
# actually held back. It rolls the list back up as well as down, so
# the long dialog the cap exists to avoid is not a one-way door.
def test_the_groups_dialog_offers_the_rest_of_a_capped_list
with_contacts({"ada" => ADA}) do |store|
("a".."i").each { FixtureData.seed_group(store, name: "Group #{it}") }
get "/contacts/ada"
assert_includes last_response.body,
%(<button type="button" data-size="sm" x-show="needle === \'\'" ) +
%(@click="all = !all" x-text="all ? 'show fewer' : "show all 9 groups"">) +
%(show all 9 groups</button>)
end
end
# A popover is hidden rather than rebuilt, so what the dialog was
# left in is what it reopens in unless the close clears it.
def test_the_groups_dialog_recaps_itself_when_it_closes
with_contacts({"ada" => ADA}) do
get "/contacts/ada"
assert_includes last_response.body,
%(@toggle="if ($event.newState === 'closed') { all = false; filter = ''; named = [] }")
end
end
def test_the_groups_dialog_offers_no_such_thing_uncapped
with_contacts({"ada" => ADA}) do |store|
("a".."h").each { FixtureData.seed_group(store, name: "Group #{it}") }
get "/contacts/ada"
refute_includes last_response.body, "show all"
refute_includes last_response.body, "data-capped"
end
end
# Joining is the membership and what comes of it: the card serves
# what the group lends, and the change log tells every client so.
def test_checking_a_group_adds_the_contact_to_it
with_contacts({"ada" => ADA}) do |store|
id = FixtureData.seed_group(store, name: "Booles", lines: HOUSEHOLD)
post "/contacts/ada/groups", groups: [id]
assert_equal 303, last_response.status
assert_equal "/contacts/ada", last_response["Location"]
assert_equal ["ada"], store.group(id).members
assert_includes store.contact("ada").vcard.to_s, "7 Calculus Close"
assert_equal "group", store.changes_of("ada").first.action
end
end
# Leaving twice is leaving once: the second finds nothing to remove
# and lands on the same page.
def test_unchecking_a_group_removes_the_contact_from_it
with_contacts({"ada" => ADA}) do |store|
id = FixtureData.seed_group(store, name: "Booles", members: ["ada"])
2.times do
post "/contacts/ada/groups", was: [id]
assert_equal 303, last_response.status
assert_equal "/contacts/ada", last_response["Location"]
end
assert_empty store.group(id).members
end
end
# A save moves only the boxes its page toggled: a group joined in
# another tab since stays joined, and an id naming no group is
# dropped rather than reaching a foreign key.
def test_a_save_moves_only_the_groups_it_toggled
with_contacts({"ada" => ADA}) do |store|
booles = FixtureData.seed_group(store, name: "Booles", members: ["ada"])
babbages = FixtureData.seed_group(store, name: "Babbages")
elsewhere = FixtureData.seed_group(store, name: "Clarks")
store.add_member(elsewhere, "ada")
post "/contacts/ada/groups", groups: [babbages, "zzzz"], was: [booles]
assert_equal [babbages, elsewhere].sort, store.groups_of("ada").map(&:id).sort
end
end
def test_a_new_group_is_created_and_joined_with_the_rest_of_the_save
with_contacts({"ada" => ADA}) do |store|
booles = FixtureData.seed_group(store, name: "Booles")
post "/contacts/ada/groups", groups: [booles], named: [" Clarks "]
assert_equal 303, last_response.status
assert_equal ["Booles", "Clarks"], store.groups_of("ada").map(&:label).sort
end
end
# Several, because the commit is per name: a filter names one group
# at a time, and nothing stops a person naming two before the save.
def test_several_new_groups_are_created_with_one_save
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada/groups", named: ["Clarks", " Babbages "]
assert_equal 303, last_response.status
assert_equal ["Babbages", "Clarks"], store.groups_of("ada").map(&:label).sort
end
end
def test_a_blank_new_group_creates_nothing
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada/groups", named: [" "]
assert_equal 303, last_response.status
assert_empty store.all_groups
end
end
# A name is one group's (db/migrations/008_group_names.rb), and the
# refusal is the whole save.
def test_a_new_group_under_a_taken_name_saves_nothing
with_contacts({"ada" => ADA}) do |store|
booles = FixtureData.seed_group(store, name: "Booles")
FixtureData.seed_group(store, name: "Clarks")
post "/contacts/ada/groups", groups: [booles], named: ["Clarks"]
assert_equal 200, last_response.status
assert_includes last_response.body, "Another group is already named Clarks; nothing was saved."
assert_empty store.groups_of("ada")
assert_equal 2, store.all_groups.length
end
end
def test_a_save_for_an_unknown_contact_is_a_404
with_contacts({"ada" => ADA}) do |store|
id = FixtureData.seed_group(store, name: "Booles")
post "/contacts/nobody/groups", groups: [id]
assert_equal 404, last_response.status
end
end
# `land` is a form field, and the redirect is not its to choose: a
# path not shaped like a walk row is refused rather than followed,
# and the save with it (Web's walk_row type).
def test_a_groups_save_refuses_a_land_that_is_not_a_walk_row
with_contacts({"ada" => ADA}) do |store|
id = FixtureData.seed_group(store, name: "Family")
post "/contacts/ada/groups", "land" => "//evil.example", "groups" => [id]
assert_equal 400, last_response.status
assert_nil last_response.headers["location"]
assert_empty store.groups_of("ada")
end
end
# The groups row's two ways in: "edit" opens the dialog over the
# groups that exist, and "new" opens the members screen of one not
# yet made.
def test_show_offers_to_edit_the_groups_or_start_a_new_one
with_contacts({"ada" => ADA}) do
get "/contacts/ada"
assert_includes last_response.body, %(popovertarget="edit-groups">edit</button>)
assert_includes last_response.body, %(<a href="/contacts/ada/groups/new" class="btn" data-size="sm">new</a>)
end
end
# "new" makes nothing: it is the members screen with the contact
# ticked, its Create the create and its Cancel the card again.
def test_new_opens_the_members_screen_of_a_group_not_yet_made
with_contacts({"ada" => ADA}) do |store|
get "/contacts/ada/groups/new"
assert_equal 200, last_response.status
assert_empty store.all_groups
assert_includes last_response.body, %(<form action="/contacts/ada/groups/new" method="post")
assert_includes last_response.body, %(<input type="checkbox" name="members[]" value="ada" checked>)
assert_includes last_response.body, %(<a href="/contacts/ada" class="btn">Cancel</a>)
assert_includes last_response.body, %(data-variant="primary">Create</button>)
refute_includes last_response.body, %(name="was[]")
end
end
# A family needs no name: the save makes a nameless group of the
# ticked contacts and lands on it.
def test_the_new_groups_save_makes_a_nameless_group_of_the_ticked
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada/groups/new", members: %w[ada nobody]
groups = store.all_groups
group = groups.fetch(0)
assert_equal 1, groups.length
assert_equal 303, last_response.status
assert_equal "/groups/#{group.id}", last_response["Location"]
assert_nil group.name
assert_equal ["ada"], group.members
assert_equal %w[create join], store.group_changes_of(group.id).map(&:action).reverse
end
end
# A Create with nobody ticked makes nothing and says so.
def test_the_new_groups_save_with_none_ticked_makes_none
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada/groups/new", members: %w[nobody]
assert_equal 200, last_response.status
assert_includes last_response.body, "A group needs a member; nothing was made."
assert_empty store.all_groups
end
end
def test_a_new_group_for_an_unknown_contact_is_a_404
with_contacts({"ada" => ADA}) do |store|
get "/contacts/nobody/groups/new"
assert_equal 404, last_response.status
post "/contacts/nobody/groups/new", members: %w[ada]
assert_equal 404, last_response.status
assert_empty store.all_groups
end
end
def test_show_labels_untyped_values_with_the_property_name
untyped = ADA.sub("TEL;TYPE=mobile:", "TEL:").sub("EMAIL;TYPE=home:", "EMAIL:")
with_contacts({"ada" => untyped}) do
get "/contacts/ada"
assert_includes last_response.body, '<dt class="type-label">phone</dt>'
assert_includes last_response.body, '<dt class="type-label">email</dt>'
end
end
def test_show_labels_a_value_with_every_type_it_carries
typed = ADA.sub("TEL;TYPE=mobile:", "TEL;type=WORK;type=FAX;type=VOICE;type=pref:")
with_contacts({"ada" => typed}) do
get "/contacts/ada"
assert_includes last_response.body, '<dt class="type-label">work, fax</dt>'
end
end
# A phone's label wins its row's key where the row carries types
# too — which is what Contacts itself shows
# (docs/plans/2026-09-18-phone-labels.md).
def test_show_labels_a_labeled_phone_with_its_label
labeled = ADA.sub("TEL;TYPE=mobile:+1-555-0100\r\n",
"item1.TEL;type=CELL:+1-555-0100\r\nitem1.X-ABLabel:Google Voice\r\n")
with_contacts({"ada" => labeled}) do
get "/contacts/ada"
assert_includes last_response.body, '<dt class="type-label">Google Voice</dt>'
refute_includes last_response.body, '<dt class="type-label">cell</dt>'
end
end
# Empty attributes do not render (docs/DESIGN.md) — a bare card shows
# only the header, not a scaffold of blank rows.
def test_show_hides_attributes_the_contact_has_no_data_for
bare = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Bare Contact\r\nUID:bare\r\nEND:VCARD\r\n"
with_contacts({"bare" => bare}) do
get "/contacts/bare"
assert_equal 200, last_response.status
refute_includes last_response.body, '<dt class="type-label">nickname</dt>'
refute_includes last_response.body, '<dt class="type-label">notes</dt>'
refute_includes last_response.body, '<dt class="type-label">birthday</dt>'
end
end
# Empty values are absent by the time the view reads them (see
# Contact), so a present-but-empty property renders no row either.
def test_show_hides_attributes_whose_values_are_empty
empty = ADA.sub("TEL;TYPE=mobile:+1-555-0100", "TEL;TYPE=mobile:")
.sub("NOTE:Countess of Lovelace.", "NOTE:")
with_contacts({"ada" => empty}) do
get "/contacts/ada"
refute_includes last_response.body, '<dt class="type-label">phone</dt>'
refute_includes last_response.body, "notes"
end
end
# The stored bytes are the truth the grid interprets, so the exact
# bytes stay reachable in their own card under the record —
# collapsed by default, so a PHOTO-heavy card does not push the page
# around. The stored card is the assertion's source of truth, not
# the input: a write composes the modeled BDAY back in, so what is
# served can be ordered differently from what was put.
def test_show_offers_the_raw_card_collapsed
with_contacts({"ada" => ADA}) do |store|
get "/contacts/ada"
body = last_response.body
assert_includes body, '<details><summary class="type-label">raw vCard</summary>'
assert_includes body, store.contact("ada").vcard.to_s
refute_includes body, "<details open"
end
end
# The seeded card has been written once, so its log is one put, at
# the etag that write recorded — which for a card whose birthday is
# composed back in on read is the served card's, not the stored
# bytes'. Collapsed like the card above it, and the stamp is the
# store's own minus the milliseconds.
def test_show_renders_the_change_log_collapsed
with_contacts({"ada" => ADA}) do |store|
get "/contacts/ada"
body = last_response.body
assert_includes body, '<details><summary class="type-label">change log</summary>'
assert_includes body, '<dt class="type-label">put</dt>'
assert_includes body, store.changes.last.created_at.sub(/\.\d+Z\z/, "Z")
refute_includes body, "<details open"
end
end
# Newest first, and an edit is logged as an edit rather than a put —
# the distinction db/migrations/003_change_log_edit_action.rb added
# the action for, rendered here.
def test_the_change_log_leads_with_the_latest_entry
with_contacts({"ada" => ADA}) do |store|
store.save_edit("ada", ProTacts::VCard.new(ADA.sub("Ada Lovelace", "Ada L.")),
birthday: store.contact("ada").birthday)
get "/contacts/ada"
body = last_response.body
actions = body.scan(/<dt class="type-label">(put|edit|delete|group)<\/dt>/).flatten
assert_equal %w[edit put], actions
end
end
# The hash is cut to a readable length and the whole of it rides the
# abbr's title, so the value stays reachable without a wall of hex
# in the value column. The quotes an entity-tag is spelled with are
# the wire's and do not come to the page.
def test_the_change_log_cuts_the_etag_down_and_keeps_it_whole
with_contacts({"ada" => ADA}) do |store|
get "/contacts/ada"
digest = store.contact("ada").etag.delete('"')
assert_includes last_response.body,
%(<abbr class="type-mono" title="#{digest}">#{digest[0, 12]}…</abbr>)
refute_includes last_response.body, ""#{digest}"
end
end
# Each entry shows the lines its write moved, removed above added
# and signed rather than only colored.
def test_the_change_log_shows_what_each_write_moved
with_contacts({"ada" => ADA}) do |store|
store.save_edit("ada", ProTacts::VCard.new(ADA.sub("FN:Ada Lovelace", "FN:Ada L.")),
birthday: store.contact("ada").birthday)
get "/contacts/ada"
body = last_response.body
assert_includes body, '<div class="diff-removed">-FN:Ada Lovelace</div>'
assert_includes body, '<div class="diff-added">+FN:Ada L.</div>'
assert_operator body.index("-FN:Ada Lovelace"), :<, body.index("+FN:Ada L.")
end
end
# A first write's diff is the whole card, so a picture's payload
# would be a base64 wall in the log as surely as in the raw card —
# and elides the same way. The refuted chunk is a continuation line
# of the stored card, contiguous only in an unelided render.
def test_the_change_log_elides_a_photo_payload
with_contacts({"pic" => PhotoCard.photo("pic", bytes: 256)}) do |store|
get "/contacts/pic"
body = last_response.body
assert_includes body, '<div class="diff-added">+PHOTO'
refute_includes body, store.contact("pic").vcard.to_s.lines.grep(/\A /).first.to_s
end
end
# A photo card's raw section elides the base64 wall to its octet
# count; the property's name and parameters, and every other line,
# stay byte for byte. The refuted chunk is a continuation line of
# the stored card itself — the one string guaranteed contiguous in
# an unelided render.
def test_show_elides_photo_payloads_in_the_raw_card
with_contacts({"pic" => PhotoCard.photo("pic", bytes: 256)}) do |store|
get "/contacts/pic"
body = last_response.body
assert_includes body, " octets elided]"
chunk = store.contact("pic").vcard.to_s.lines.grep(/\A /).first
refute_includes body, chunk
assert_includes body, "X-IMAGETYPE:PHOTO"
end
end
# A memoji's parameter section is itself 1,685 octets of base64 on
# one line — a long parameter value elides like a long property
# value, and the short parameters around it render whole.
def test_show_elides_long_parameter_values_in_the_raw_card
with_contacts({"mochi" => PhotoCard.memoji("mochi")}) do |store|
get "/contacts/mochi"
body = last_response.body
assert_includes body, "VND-63-MEMOJI-DETAILS=["
assert_includes body, ";ENCODING=b;TYPE=JPEG:["
details = store.contact("mochi").properties
.find { it.name.casecmp?("PHOTO") }.parameters
.find { |name, _| name.casecmp?("VND-63-MEMOJI-DETAILS") }.last
refute_includes body, details[0, 60]
end
end
# Any overly long value elides, not only a picture's: a long NOTE
# renders as its count in the raw card while the grid above still
# shows the text itself.
def test_show_elides_any_overly_long_value
note = "n" * 200
card = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Wordy\r\nNOTE:#{note}\r\nUID:wordy\r\nEND:VCARD\r\n"
with_contacts({"wordy" => card}) do
get "/contacts/wordy"
assert_includes last_response.body, "NOTE:[#{note.bytesize} octets elided]"
end
end
# A card that will not parse is served from its bytes regardless —
# the raw section is the one thing such a card has to show, so it
# renders with only the groups row under the header, not instead of
# the page. The record card, specifically: the change log rides the
# same grid in a card of its own, and every card has a history.
def test_show_of_a_bare_contact_renders_only_the_groups_row
bare = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Bare Contact\r\nUID:bare\r\nEND:VCARD\r\n"
with_contacts({"bare" => bare}) do
get "/contacts/bare"
grid = last_response.body[%r{<dl class="detail-grid">.*?</dl>}].to_s
assert_equal ["groups"], grid.scan(%r{<dt class="type-label">([^<]*)</dt>}).flatten
assert_includes last_response.body, "<details"
end
end
# The nickname is a name-like fact, so it rides under the name in
# the header, in the heading family but muted — a name, visibly
# secondary to the formal one — and the grid keeps only what
# reaches the person.
def test_show_puts_the_nickname_under_the_name_in_the_header
red = ADA.sub("FN:Ada Lovelace", "FN:Sarah\r\nNICKNAME:Red").sub("UID:ada", "UID:red")
with_contacts({"red" => red}) do
get "/contacts/red"
body = last_response.body
header = body.split("<dl").first
assert_includes header, "Sarah</h1>"
assert_includes header, 'class="type-h3 gl-muted"'
assert_includes header, ">Red<"
refute_includes body, '">nickname</dt>'
end
end
def test_show_of_an_unknown_contact_is_404
with_contacts({}) { get "/contacts/nope" }
assert_equal 404, last_response.status
end
## Photos
def test_show_without_a_picture_renders_no_avatar
with_contacts({"ada" => ADA}) do
get "/contacts/ada"
assert_equal 200, last_response.status
refute_includes last_response.body, 'class="avatar"'
end
end
# A contact with a picture renders it where the initials were —
# here the detail header's large avatar — pointing at the photo
# route rather than carrying the bytes in the page. Alt is empty:
# the name is the heading right beside it.
def test_show_renders_the_cards_picture_as_the_avatar
with_contacts({"pic" => PhotoCard.photo("pic", bytes: 256)}) do
get "/contacts/pic"
assert_equal 200, last_response.status
assert_includes last_response.body, '<img class="avatar" src="/contacts/pic/photo" alt="" loading="lazy"'
assert_includes last_response.body, 'data-size="xl"'
refute_includes last_response.body, '<span class="avatar"'
end
end
# The dashboard rows render the same avatar — one component, every
# slot — so a picture shows in recently updated and in results.
def test_the_index_rows_render_the_picture_too
with_contacts({"pic" => PhotoCard.photo("pic", bytes: 256)}) do
get "/"
assert_includes last_response.body, 'src="/contacts/pic/photo"'
get "/search/results", q: "ada"
assert_includes last_response.body, 'src="/contacts/pic/photo"'
end
end
# The route the avatars point at: the decoded bytes under the type
# their own magic bytes name, with an etag of those bytes.
def test_the_photo_route_serves_the_decoded_bytes
with_contacts({"pic" => PhotoCard.photo("pic", bytes: 256)}) do |store|
get "/contacts/pic/photo"
bytes = store.contact("pic").photo.bytes
assert_equal 200, last_response.status
assert_equal "image/jpeg", last_response["Content-Type"]
assert_equal bytes, last_response.body
assert_equal %("#{Digest::SHA256.hexdigest(bytes)}"), last_response["ETag"]
assert_equal "private, no-cache", last_response["Cache-Control"]
end
end
# A browser holding the picture asks with its etag and is told to
# keep it, so a page of avatars it has seen sends no picture again
# (Web#serve_photo). An edit that leaves the picture alone keeps
# its etag; a new picture is a new one, and is sent.
def test_the_photo_route_answers_a_held_picture_with_a_304
with_contacts({"pic" => PhotoCard.photo("pic", bytes: 256)}) do |store|
get "/contacts/pic/photo"
etag = last_response["ETag"]
get "/contacts/pic/photo", {}, "HTTP_IF_NONE_MATCH" => etag
assert_equal 304, last_response.status
assert_empty last_response.body
store.put("pic", ProTacts::VCard.new(PhotoCard.photo("pic", bytes: 256, extra: ["NICKNAME:Countess"])))
get "/contacts/pic/photo", {}, "HTTP_IF_NONE_MATCH" => etag
assert_equal 304, last_response.status
store.put("pic", ProTacts::VCard.new(PhotoCard.photo("pic", bytes: 512)))
get "/contacts/pic/photo", {}, "HTTP_IF_NONE_MATCH" => etag
assert_equal 200, last_response.status
refute_equal etag, last_response["ETag"]
end
end
# A contact whose card carries no picture a browser can show is no
# match at the route — the views never point at one — same 404 as an
# unknown contact.
def test_the_photo_route_is_404_without_a_picture
with_contacts({"ada" => ADA}) do
get "/contacts/ada/photo"
assert_equal 404, last_response.status
get "/contacts/nobody/photo"
assert_equal 404, last_response.status
end
end
# The seed book carries one fixture card per picture kind macOS
# sends — photo, emoji, memoji, promoted from real client sessions
# (see test/fixtures/cards and docs/apple-contacts.md) — and these
# tests are what keeps them exercising the path: a seed that stops
# parsing, or stops sniffing to an image, fails here rather than
# passing silently as an initials avatar. Runs against the suite's
# fixture store rather than a with_contacts throwaway, because the
# seeds themselves are the thing under test.
%w[photo emoji memoji].each do |id|
define_method("test_the_#{id}_seed_serves_its_picture") do
get "/contacts/#{id}"
assert_equal 200, last_response.status
assert_includes last_response.body, "src=\"/contacts/#{id}/photo\""
get "/contacts/#{id}/photo"
assert_equal 200, last_response.status
assert_equal "image/jpeg", last_response["Content-Type"]
end
end
## Creating contacts
# The add affordance and the dialog it opens, both carried by the
# dashboard: a popovertarget button and a popover element, the
# Popover API's declarative pair (see Admin::ContactDialog).
# rack-test cannot open a popover, so this pins the wiring the same
# way the search-in-the-header test does — the attributes a browser
# acts on, asserted on the page a browser gets.
def test_the_dashboard_carries_the_new_contact_dialog
with_contacts({}) do
get "/"
body = last_response.body
assert_includes body, '<button data-size="sm" popovertarget="new-contact">add contact</button>'
assert_includes body, '<dialog id="new-contact" popover="auto">'
assert_includes body, '<form id="new-contact-form" action="/contacts" method="post" ' \
'x-data="{ firstBlank: true, lastBlank: true }">'
# The captions render — a bare string mid-block is void in
# Phlex, so the labels carry their text through plain (the
# assertion keeps a regression from rendering a silent label).
assert_includes body, '<label class="field">First<input type="text" name="first" required ' \
':required="lastBlank" @input="firstBlank = !$el.value.trim()" autofocus></label>'
assert_includes body, '<label class="field">Middle<input type="text" name="middle"></label>'
assert_includes body, '<label class="field">Last<input type="text" name="last" required ' \
':required="firstBlank" @input="lastBlank = !$el.value.trim()"></label>'
assert_includes body, 'popovertargetaction="hide"'
# No company create rides along: organizations arrive by client
# and by import, and the dialog is exactly the person's.
refute_includes body, "new-company"
end
end
# A create lands through Store#put — change log, index, and all —
# and the answer is a redirect to the record, the details page
# where further editing belongs.
def test_creating_a_contact_from_the_dialog
with_contacts({}) do |store|
post "/contacts", first: "Grace", last: "Hopper"
assert_equal 303, last_response.status
id = last_response["Location"].delete_prefix("/contacts/")
assert_match(/\A[k-z]{12}\z/, id)
follow_redirect!
assert_equal 200, last_response.status
assert_includes last_response.body, "Grace Hopper"
# N and FN both written, per RFC 2426 section 4's mandatory set,
# and the card's UID is the id it is served under.
card = store.contact(id).vcard.to_s
assert_includes card, "N:Hopper;Grace;;;"
assert_includes card, "FN:Grace Hopper"
assert_includes card, "UID:#{id}"
assert(store.changes.any? { it.action == "put" && it.card_id == id })
end
end
# The middle box writes N's additional component (RFC 2426 section
# 3.1.2) and joins the display name the other two make.
def test_creating_a_contact_with_a_middle_name
with_contacts({}) do |store|
post "/contacts", first: "Ada", middle: "Byron", last: "Lovelace"
assert_equal 303, last_response.status
id = last_response["Location"].delete_prefix("/contacts/")
card = store.contact(id).vcard.to_s
assert_includes card, "N:Lovelace;Ada;Byron;;"
assert_includes card, "FN:Ada Byron Lovelace"
end
end
# A middle name is not one of the halves a contact is named by, so a
# create carrying nothing else is the nameless one, refused.
def test_a_create_with_only_a_middle_name_is_refused
with_contacts({}) do |store|
post "/contacts", first: "", middle: "Byron", last: ""
assert_equal 200, last_response.status
assert_includes last_response.body, "A contact needs a name."
assert_empty store.contacts
end
end
# A single name is a fine contact: the model carries either half
# alone, and the card says exactly what was given.
def test_creating_a_contact_with_only_a_first_name
with_contacts({}) do |store|
post "/contacts", first: "Cher"
assert_equal 303, last_response.status
id = last_response["Location"].delete_prefix("/contacts/")
card = store.contact(id).vcard.to_s
assert_includes card, "N:;Cher;;;"
assert_includes card, "FN:Cher"
end
end
# A name is text, and the card is structure: a comma or semicolon
# in a name must not read as a component separator (RFC 2426
# section 2.4.2).
def test_names_with_punctuation_are_escaped_in_the_card
with_contacts({}) do |store|
post "/contacts", first: "Ada, Jr.", last: "Lovelace; Countess"
assert_equal 303, last_response.status
id = last_response["Location"].delete_prefix("/contacts/")
card = store.contact(id).vcard.to_s
assert_includes card, "N:Lovelace\\; Countess;Ada\\, Jr.;;;"
assert_includes card, "FN:Ada\\, Jr. Lovelace\\; Countess"
end
end
# The backstop for the one request no browser can send (one name
# box or the other is required): a plain dashboard re-render with
# the refusal in a toast, and nothing stored.
def test_a_nameless_create_is_refused_with_a_toast
with_contacts({}) do |store|
post "/contacts", first: " ", last: ""
assert_equal 200, last_response.status
assert_includes last_response.body, '<div role="status" data-fixed><span>A contact needs a name.</span></div>'
assert_empty store.contacts
end
end
## Editing contacts
# The edit screen: one form prefilled from the card — N's two
# leading components as the name fields, the accessors' unescaped
# readings as the rest — carrying the etag of the card it renders
# from, the snapshot guard's hidden half.
def test_the_edit_screen_prefills_the_cards_values
red = ADA.sub("FN:Ada Lovelace", "FN:Sarah\r\nNICKNAME:Red").sub("UID:ada", "UID:red")
with_contacts({"red" => red}) do |store|
get "/contacts/red/edit"
assert_equal 200, last_response.status
body = last_response.body
assert_includes body, '<form action="/contacts/red" method="post" class="field-stack" id="contact-form">'
assert_includes body, '<button type="submit" form="contact-form" data-variant="primary">Save</button>'
assert_includes body, '<input type="text" name="first" value="Ada" ' \
':required="lastBlank" @input="firstBlank = !$el.value.trim()" :disabled="company" autofocus>'
assert_includes body, '<span>Middle</span><input type="text" name="middle" :disabled="company">'
assert_includes body, '<input type="text" name="last" value="Lovelace" ' \
':required="firstBlank" @input="lastBlank = !$el.value.trim()" :disabled="company">'
assert_includes body,
'<label class="field" data-blank-removes><span>Nickname</span>' \
'<input type="text" name="nickname" value="Red" placeholder="removed on save"></label>'
assert_includes body,
"<textarea name=\"note\" rows=\"4\" placeholder=\"removed on save\">Countess of Lovelace.</textarea>"
# The etag carries its quotes, HTML-escaped in the attribute.
assert_includes body, %(<input type="hidden" name="etag" value=""#{store.contact("red").etag.delete('"')}"">)
end
end
# The middle box prefills from N's additional component, unescaped
# like every other reading the form starts from.
def test_the_edit_screen_prefills_the_middle_name
byron = ADA.sub("N:Lovelace;Ada;;;", "N:Lovelace;Ada;Byron\\, the poet;;")
with_contacts({"ada" => byron}) do
get "/contacts/ada/edit"
assert_equal 200, last_response.status
assert_includes last_response.body, '<input type="text" name="middle" value="Byron, the poet" :disabled="company">'
end
end
# A family name alone is a name (`N:Prince;;;;`): the blank first box
# is not required over it, or the browser would refuse every save.
def test_a_family_name_alone_is_a_name_the_editor_can_save
prince = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Prince\r\nN:Prince;;;;\r\nUID:prince\r\nEND:VCARD\r\n"
with_contacts({"prince" => prince}) do |store|
get "/contacts/prince/edit"
body = last_response.body
assert_includes body, "firstBlank: true, lastBlank: false"
assert_includes body, '<input type="text" name="first" ' \
':required="lastBlank" @input="firstBlank = !$el.value.trim()" :disabled="company" autofocus>'
assert_includes body, '<input type="text" name="last" value="Prince" required ' \
':required="firstBlank" @input="lastBlank = !$el.value.trim()" :disabled="company">'
post "/contacts/prince", first: "", last: "Prince", company: "0", etag: store.contact("prince").etag
assert_equal 303, last_response.status
assert_includes store.contact("prince").vcard.to_s, "N:Prince;;;;\r\n"
end
end
ACME = "BEGIN:VCARD\r\nVERSION:3.0\r\nN:Acme Corp;;;;\r\nFN:Acme Corp\r\nORG:Acme Corp;Sales\r\n" \
"X-ABShowAs:COMPANY\r\nUID:acme\r\nEND:VCARD\r\n"
# The two cards macOS PUT as a card was switched in Contacts.app
# (log/dev.log, 2026-10-07): a person with an employer (exchange
# b5ce7eaebe0e) and that card made a company again (ec5ce1d8dd03).
# Their X-ADDRESSING-GRAMMAR blob is left out, being Apple's opaque
# bytes that no read here touches.
SWITCHED_PERSON = "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Apple Inc.//macOS 27.0//EN\r\nN:B;A;;;\r\n" \
"FN:A B\r\nORG:Arbitrary Definitions;\r\nREV:2026-10-07T13:34:15Z\r\nUID:company\r\n" \
"END:VCARD\r\n"
SWITCHED_COMPANY = "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Apple Inc.//macOS 27.0//EN\r\nN:;;;;\r\n" \
"FN:Arbitrary Definitions\r\nORG:Arbitrary Definitions;\r\nX-ABShowAs:COMPANY\r\n" \
"REV:2026-10-07T13:34:40Z\r\nUID:company\r\nEND:VCARD\r\n"
# A company card's name is the organization row, required and
# focused; the pair is rendered hidden and disabled, so the Company
# box can switch to it and it neither submits nor blocks a save.
def test_the_edit_screen_shows_one_organization_field_for_a_company_card
with_contacts({"acme" => ACME}) do
get "/contacts/acme/edit"
body = last_response.body
assert_equal 200, last_response.status
assert_includes body, '<div class="field"><label for="company">Company</label>' \
'<input type="hidden" name="company" value="0">' \
'<input type="checkbox" id="company" name="company" value="1" checked x-model="company">'
assert_includes body, %(<label class="field" :data-blank-removes="!company"><span>Organization</span>) +
%(<input type="text" name="organization" value="Acme Corp" required :required="company" ) +
%(autofocus :placeholder="company ? '' : 'removed on save'">)
assert_includes body, '<label class="field" x-show="!company" style="display: none"><span>First</span>'
assert_includes body, '<input type="text" name="last" value="Acme Corp" required ' \
':required="firstBlank" @input="lastBlank = !$el.value.trim()" disabled :disabled="company">'
# The organization, then the box, under the name fields.
assert_operator body.index('name="organization"'), :>, body.index('name="last"')
assert_operator body.index('id="company"'), :>, body.index('name="organization"')
end
end
# A person's organization is their employer: shown, optional, and a
# whole-property row whose blank removes it, like the nickname's.
def test_the_edit_screen_shows_a_persons_organization
with_contacts({"company" => SWITCHED_PERSON}) do
get "/contacts/company/edit"
body = last_response.body
assert_includes body, '<input type="checkbox" id="company" name="company" value="1" x-model="company">'
assert_includes body, %(<label class="field" data-blank-removes :data-blank-removes="!company">) +
%(<span>Organization</span><input type="text" name="organization" ) +
%(value="Arbitrary Definitions" :required="company" placeholder="removed on save" ) +
%(:placeholder="company ? '' : 'removed on save'">)
end
end
# A person without one has an empty row and nothing to lose, and
# their own name is no organization's to prefill.
def test_a_person_without_an_organization_gets_an_empty_row
with_contacts({"ada" => ADA}) do
get "/contacts/ada/edit"
assert_includes last_response.body,
'<label class="field"><span>Organization</span><input type="text" name="organization" :required="company">'
end
end
# The person's save writes the employer into ORG's first component,
# the units after it kept; blank removes the property; a POST
# without the field leaves it be.
def test_saving_a_persons_organization
with_contacts({"company" => SWITCHED_PERSON.sub("ORG:Arbitrary Definitions;", "ORG:Arbitrary Definitions;Ops")}) do |store|
post "/contacts/company", first: "A", last: "B", organization: "Gloss", company: "0", etag: store.contact("company").etag
assert_includes store.contact("company").vcard.to_s, "ORG:Gloss;Ops\r\n"
post "/contacts/company", first: "A", last: "B", company: "0", etag: store.contact("company").etag
assert_includes store.contact("company").vcard.to_s, "ORG:Gloss;Ops\r\n"
post "/contacts/company", first: "A", last: "B", organization: "", company: "0", etag: store.contact("company").etag
refute_includes store.contact("company").vcard.to_s, "ORG"
refute store.contact("company").company?
end
end
# The details page shows a person's organization under their name,
# and a company's not twice.
def test_show_puts_a_persons_organization_under_the_name
with_contacts({"company" => SWITCHED_PERSON, "acme" => ACME}) do
get "/contacts/company"
header = last_response.body.split("<dl").first
assert_includes header, '<div class="type-body gl-muted" style="margin-top: var(--gl-space-2xs);">' \
"Arbitrary Definitions</div>"
get "/contacts/acme"
refute_includes last_response.body.split("<dl").first, "type-body gl-muted"
end
end
# A company's name goes into ORG's first component and FN, where
# macOS keeps it; the units after it keep their bytes, and N is
# emptied, the older shape's name in it included.
def test_saving_a_company_writes_its_name_into_org_and_fn
with_contacts({"acme" => ACME}) do |store|
post "/contacts/acme", company: "1", organization: "Acme, Inc.", etag: store.contact("acme").etag
assert_equal 303, last_response.status
card = store.contact("acme").vcard.to_s
assert_includes card, "N:;;;;\r\n"
assert_includes card, "FN:Acme\\, Inc.\r\n"
assert_includes card, "ORG:Acme\\, Inc.;Sales\r\n"
assert_includes card, "X-ABShowAs:COMPANY\r\n"
end
end
# An untouched save of the card macOS wrote keeps every byte.
def test_an_untouched_company_save_keeps_the_card
with_contacts({"company" => SWITCHED_COMPANY}) do |store|
get "/contacts/company/edit"
assert_includes last_response.body, 'name="organization" value="Arbitrary Definitions"'
post "/contacts/company", company: "1", organization: "Arbitrary Definitions",
etag: store.contact("company").etag
assert_equal 303, last_response.status
assert_equal SWITCHED_COMPANY, store.contact("company").vcard.to_s
end
end
# Ticking Company on a person: the flag goes on and FN takes the
# organization's name, and N is emptied, as in macOS's second PUT:
# the person's names would otherwise stay on a card that no longer
# shows them.
def test_ticking_company_makes_a_person_a_company
with_contacts({"company" => SWITCHED_PERSON}) do |store|
post "/contacts/company", company: "1", organization: "Arbitrary Definitions",
etag: store.contact("company").etag
assert_equal 303, last_response.status
contact = store.contact("company")
assert contact.company?
card = contact.vcard.to_s
assert_includes card, "N:;;;;\r\n"
assert_includes card, "FN:Arbitrary Definitions\r\n"
assert_includes card, "ORG:Arbitrary Definitions;\r\n"
end
end
# Unticking it: the flag comes off and the pair writes N and FN, as
# in macOS's first PUT; ORG stays, the person's employer now.
def test_unticking_company_makes_a_company_a_person
with_contacts({"company" => SWITCHED_COMPANY}) do |store|
post "/contacts/company", company: "0", first: "A", last: "B", etag: store.contact("company").etag
assert_equal 303, last_response.status
contact = store.contact("company")
refute contact.company?
card = contact.vcard.to_s
assert_includes card, "N:B;A;;;\r\n"
assert_includes card, "FN:A B\r\n"
assert_includes card, "ORG:Arbitrary Definitions;\r\n"
refute_includes card, "X-ABShowAs"
end
end
# A company switched to a person whose N already holds names still
# gets its FN from them: the FN it had was the organization's.
def test_a_switch_back_to_a_person_rewrites_the_display_name
names_kept = SWITCHED_COMPANY.sub("N:;;;;", "N:B;A;;;")
with_contacts({"company" => names_kept}) do |store|
post "/contacts/company", company: "0", first: "A", last: "B", etag: store.contact("company").etag
assert_includes store.contact("company").vcard.to_s, "FN:A B\r\n"
end
end
# Neither kind saves without its name: macOS would not save the
# person it unticked until one was typed, and a company's one field
# is the whole of its name.
def test_a_switch_without_the_new_kinds_name_is_refused
with_contacts({"company" => SWITCHED_COMPANY, "person" => SWITCHED_PERSON.sub("UID:company", "UID:person")}) do |store|
post "/contacts/company", company: "0", first: "", last: "", etag: store.contact("company").etag
assert_includes last_response.body, "A contact needs a name."
assert store.contact("company").company?
post "/contacts/person", company: "1", organization: " ", etag: store.contact("person").etag
assert_includes last_response.body, "A contact needs a name."
refute store.contact("person").company?
end
end
# The editor renders the contact's own rows and none a group lends:
# a row here is a row the save may rewrite or remove, and neither is
# this screen's to do to a value the household owns. The details
# page is where an inherited row is read.
def test_the_edit_screen_omits_the_rows_a_group_lends
with_contacts({"ada" => ADA}) do |store|
FixtureData.seed_group(store, name: "Booles", members: ["ada"], lines: HOUSEHOLD)
get "/contacts/ada/edit"
assert_includes last_response.body, "12 Analytical Way"
refute_includes last_response.body, "7 Calculus Close"
end
end
# The note row is the collision's own case: a member with no note of
# its own inherits the household's, and a textarea prefilled from it
# would write the group's words onto the member the first time
# anyone saved.
def test_the_note_row_is_blank_over_a_note_only_the_group_lends
noteless = ADA.sub("NOTE:Countess of Lovelace.\r\n", "")
with_contacts({"ada" => noteless}) do |store|
FixtureData.seed_group(store, name: "Booles", members: ["ada"], lines: HOUSEHOLD)
get "/contacts/ada/edit"
assert_includes last_response.body, '<textarea name="note" rows="4"></textarea>'
refute_includes last_response.body, "Gate code 1854."
end
end
# A save reaches the contact's own lines and no others, so a request
# naming a lent line — the screen renders no such row, but a request
# is not the screen — matches nothing and writes nothing.
def test_a_save_naming_a_lent_line_touches_nothing
with_contacts({"ada" => ADA}) do |store|
FixtureData.seed_group(store, name: "Booles", members: ["ada"], lines: HOUSEHOLD)
contact = store.contact("ada")
lent = contact.addresses.find { it.lent_by }
post "/contacts/ada", first: "Ada", last: "Lovelace", note: "Countess of Lovelace.",
company: "0", etag: contact.etag,
address: {lent.line.digest => {"street" => "9 Elsewhere Lane"}}
assert_equal 303, last_response.status
after = store.contact("ada")
refute_includes after.stored.to_s, "9 Elsewhere Lane"
refute_includes after.stored.to_s, "7 Calculus Close"
assert_includes after.vcard.to_s, "7 Calculus Close"
end
end
def test_the_edit_screen_of_an_unknown_contact_is_404
with_contacts({}) { get "/contacts/nope/edit" }
assert_equal 404, last_response.status
end
# The removal state rides only where blanking deletes: nickname
# and note render whether or not the contact carries one, and a
# box that rendered empty cannot lose anything — no attribute, no
# "removed on save" in it, no remove button. The birthday row
# does not render at all over a birthday unheld: absence is added
# by the dialog like any other property, not scaffolded as an
# empty row — the radio leaves the list the moment its row stands.
def test_rows_without_a_property_to_lose_state_no_removal
bare = ADA.sub("NOTE:Countess of Lovelace.\r\n", "").sub("BDAY:1985-12-10\r\n", "")
with_contacts({"ada" => bare}) do
get "/contacts/ada/edit"
body = last_response.body
assert_includes body, '<label class="field"><span>Nickname</span><input type="text" name="nickname"></label>'
assert_includes body, '<textarea name="note" rows="4"></textarea>'
refute_includes body, '<span>birthday</span>'
assert_includes body,
'<label x-show="!added.includes(\'birthday\')"><input type="radio" name="add-type" value="birthday" x-model="type">birthday</label>'
# The remove control by its own label, not by the class it
# shares: the add dialog's birthday template carries a picker
# wearing the same one, inert inside its <template> but present
# in the bytes, so "icon-button" stopped meaning "a control
# that deletes something."
refute_includes body, 'aria-label="Remove birthday"'
end
end
# A po box has no field, and the save's removal is blank throughout,
# po box included (Contact::Address#edit) — so a line
# surviving by its po box alone cannot be removed from this form, and
# its row, blank as it renders, wears no removal state.
def test_a_po_box_only_address_row_wears_no_removal_state
po = ADA.sub(
"ADR;TYPE=home:;;12 Analytical Way;London;England;NW1 1AA;United Kingdom",
"ADR;TYPE=home:PO Box 1;;;;;",
)
with_contacts({"ada" => po}) do |store|
get "/contacts/ada/edit"
digest = store.contact("ada").addresses.first.line.digest
assert_includes last_response.body,
'<div class="field"><input type="text" class="row-label" name="address[' + digest + '][label]" value="home"'
end
end
# The details page names its other mode: the edit link rides the
# back-link's line at the right edge, so the card below stays a
# clean read of the record.
def test_the_details_page_links_to_the_edit_screen
with_contacts({"ada" => ADA}) do
get "/contacts/ada"
assert_includes last_response.body,
'<div class="record-nav"><a href="/" class="type-label">‹ contacts</a>' \
'<a href="/contacts/ada/edit" class="btn" data-size="sm">edit</a></div>'
end
end
# The save is addressed operations, and this is the whole design:
# the four properties the form names are replaced, every other line
# of the stored card is byte-identical, and the write lands through
# Store#save_edit with its change-log entry.
def test_saving_the_edit_replaces_only_the_addressed_properties
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "King", nickname: "Countess",
note: "First programmer.", company: "0", etag: store.contact("ada").etag
assert_equal 303, last_response.status
assert_equal "/contacts/ada", last_response["Location"]
card = store.contact("ada").vcard.to_s
assert_includes card, "N:King;Ada;;;"
assert_includes card, "FN:Ada King"
assert_includes card, "NICKNAME:Countess"
assert_includes card, "NOTE:First programmer."
assert_includes card, "TEL;TYPE=mobile:+1-555-0100\r\n"
assert_includes card, "EMAIL;TYPE=home:ada@example.com\r\n"
assert_includes card, "ADR;TYPE=home:;;12 Analytical Way;London;England;NW1 1AA;United Kingdom\r\n"
assert_includes card, "UID:ada\r\n"
# The birthday is model state the edit never touched, composed
# back into the served card.
assert_includes card, "BDAY:1985-12-10\r\n"
assert(store.changes.any? { it.action == "edit" && it.card_id == "ada" })
end
end
# FN is the card's own display name, not a rendering of N: a save
# that left every name field alone must not rebuild it from them,
# or a card spelling the name in full loses every part the form has
# no field for — the parts N itself keeps.
def test_a_save_that_moves_no_name_field_keeps_the_cards_own_display_name
formal = ADA.sub("FN:Ada Lovelace", "FN:Dr. Ada B. Lovelace, Jr.")
.sub("N:Lovelace;Ada;;;", "N:Lovelace;Ada;B.;Dr.;Jr.")
with_contacts({"ada" => formal}) do |store|
digest = store.contact("ada").phones.first.line.digest
post "/contacts/ada", first: "Ada", middle: "B.", last: "Lovelace",
note: "Countess of Lovelace.",
company: "0", etag: store.contact("ada").etag, phone: {digest => {"value" => "+1-555-0199"}}
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
assert_includes card, "FN:Dr. Ada B. Lovelace, Jr.\r\n"
assert_includes card, "N:Lovelace;Ada;B.;Dr.;Jr.\r\n"
assert_includes card, "TEL;TYPE=mobile:+1-555-0199\r\n"
end
end
# A moved name field is the one time the old display name is stale,
# and then FN is the form's reading — N's prefixes and suffixes
# staying where they were, this being an edit of the name and not of
# the card's spelling of it.
def test_a_moved_name_field_rewrites_the_display_name
formal = ADA.sub("FN:Ada Lovelace", "FN:Dr. Ada B. Lovelace, Jr.")
.sub("N:Lovelace;Ada;;;", "N:Lovelace;Ada;B.;Dr.;Jr.")
with_contacts({"ada" => formal}) do |store|
post "/contacts/ada", first: "Ada", middle: "B.", last: "King", company: "0", etag: store.contact("ada").etag
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
assert_includes card, "FN:Ada B. King\r\n"
assert_includes card, "N:King;Ada;B.;Dr.;Jr.\r\n"
end
end
# The middle box is a name field like the two beside it: moving it
# alone is what makes the old display name stale.
def test_a_moved_middle_name_rewrites_the_display_name
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", middle: "Byron", last: "Lovelace",
company: "0", etag: store.contact("ada").etag
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
assert_includes card, "N:Lovelace;Ada;Byron;;\r\n"
assert_includes card, "FN:Ada Byron Lovelace\r\n"
end
end
# Blank equals absent in the name row too: emptying the middle box
# clears N's additional component and drops it from the display
# name. The line stays — N is mandatory (RFC 2426 section 4) and a
# component is not a property.
def test_a_blank_middle_name_clears_the_additional_component
byron = ADA.sub("FN:Ada Lovelace", "FN:Ada Byron Lovelace")
.sub("N:Lovelace;Ada;;;", "N:Lovelace;Ada;Byron;;")
with_contacts({"ada" => byron}) do |store|
post "/contacts/ada", first: "Ada", middle: "", last: "Lovelace", company: "0", etag: store.contact("ada").etag
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
assert_includes card, "N:Lovelace;Ada;;;\r\n"
assert_includes card, "FN:Ada Lovelace\r\n"
end
end
# The mandatory property is filled in rather than left absent (RFC
# 2426 section 4): a card with no FN has no display name to keep, so
# the untouched-name rule does not apply to it.
def test_a_save_writes_the_display_name_a_card_lacks
nameless = "BEGIN:VCARD\r\nVERSION:3.0\r\nN:Lovelace;Ada;;;\r\nUID:ada\r\nEND:VCARD\r\n"
with_contacts({"ada" => nameless}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag
assert_equal 303, last_response.status
assert_includes store.contact("ada").vcard.to_s, "FN:Ada Lovelace\r\n"
end
end
# Blank equals absent on the way back: a blank nickname or note
# removes the property, as a blank value is absent to the reader
# (Contact#text_of) — and filling one in adds it where the card
# carried none.
def test_a_blank_nickname_or_note_removes_the_property
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", nickname: "", note: "", company: "0", etag: store.contact("ada").etag
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
refute_includes card, "NICKNAME"
refute_includes card, "NOTE"
end
end
# N's unedited components — the prefixes and suffixes no box shows —
# splice through byte for byte, the still-escaped value never
# rounding through unescape and re-escape. The escaped semicolon
# inside the prefixes is the one an unescaped split would break on;
# the seed's four components leave the fifth to the padding rule.
def test_the_names_unedited_components_are_preserved_byte_for_byte
honorific = ADA.sub("N:Lovelace;Ada;;;", "N:Lovelace;Ada;;Countess\\; Dr.")
with_contacts({"ada" => honorific}) do |store|
post "/contacts/ada", first: "Ada", middle: "", last: "King", company: "0", etag: store.contact("ada").etag
assert_equal 303, last_response.status
assert_includes store.contact("ada").vcard.to_s, "N:King;Ada;;Countess\\; Dr.;\r\n"
end
end
# A name box that submits what it was rendered from keeps its
# component's own bytes, rather than rounding through unescape and
# re-escape — which is not byte-stable: `\x` is no escape the
# reader knows, so it survives the read and the writer would
# double its backslash. The rule Contact.address_value already writes under
# (CardForm.n_line), at the name's grain.
def test_a_name_component_returned_unchanged_keeps_its_own_bytes
odd = ADA.sub("N:Lovelace;Ada;;;", "N:Lovelace;Ada;Byron\\x;;")
with_contacts({"ada" => odd}) do |store|
post "/contacts/ada", first: "Ada", middle: "Byron\\x", last: "King",
company: "0", etag: store.contact("ada").etag
assert_equal 303, last_response.status
assert_includes store.contact("ada").vcard.to_s, "N:King;Ada;Byron\\x;;\r\n"
end
end
# A card whose N stops short of five components is padded with the
# same empties a whole-N writer would leave.
def test_a_short_n_is_padded_to_five_components
short = ADA.sub("N:Lovelace;Ada;;;", "N:Lovelace;Ada")
with_contacts({"ada" => short}) do |store|
post "/contacts/ada", first: "Ada", last: "King", company: "0", etag: store.contact("ada").etag
assert_equal 303, last_response.status
assert_includes store.contact("ada").vcard.to_s, "N:King;Ada;;;\r\n"
end
end
# Values are text and the card is structure: punctuation escapes in
# the written lines, and a note's line breaks travel as the escape
# rather than ending the property's line.
def test_punctuation_and_line_breaks_escape_in_the_written_lines
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "King",
nickname: "Countess, of mathematics", note: "line one\nline two",
company: "0", etag: store.contact("ada").etag
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
assert_includes card, "NICKNAME:Countess\\, of mathematics\r\n"
assert_includes card, "NOTE:line one\\nline two\r\n"
follow_redirect!
assert_includes last_response.body, "Countess, of mathematics"
end
end
# A fold on a line the save never mentioned is the byte-identity
# claim at its sharpest: the continuation survives with its own
# bytes, not normalized into the unfolded reading.
def test_a_folded_line_the_save_never_mentioned_is_left_alone
folded = ADA.sub("TEL;TYPE=mobile:+1-555-0100\r\n", "TEL;TYPE=mobile:+1-555-\r\n 0100\r\n")
with_contacts({"ada" => folded}) do |store|
post "/contacts/ada", first: "Ada", last: "King", company: "0", etag: store.contact("ada").etag
assert_equal 303, last_response.status
assert_includes store.contact("ada").vcard.to_s, "TEL;TYPE=mobile:+1-555-\r\n 0100\r\n"
end
end
## Editing phones
# The phone rows: a label field in the type column and a value field
# beside it, both named by the line's digest — the address the save
# substitutes. The label offers the kind's words from a list by id,
# and wears the kind as its placeholder. The blank row a new phone
# goes in is in the add dialog, not the card (see below).
def test_the_edit_screen_renders_a_digest_named_field_per_phone
with_contacts({"ada" => ADA}) do |store|
get "/contacts/ada/edit"
digest = store.contact("ada").phones.first.line.digest
body = last_response.body
assert_includes body,
'<div class="field" data-blank-removes>' \
'<input type="text" class="row-label" name="phone[' + digest + '][label]" value="mobile" autocomplete="off" ' \
'list="phone-labels" placeholder="phone" aria-label="phone label">' \
'<input type="tel" name="phone[' + digest + '][value]" value="+1-555-0100" placeholder="removed on save" ' \
'aria-label="phone"></div>'
assert_includes body,
'<datalist id="phone-labels"><option value="cell"></option><option value="home"></option>' \
'<option value="work"></option><option value="other"></option></datalist>'
end
end
# The edit screen's phone row labels render the same word the show
# screen's keys do — both read Contact::Phone#label.
def test_the_edit_screen_captions_a_labeled_phone_with_its_label
labeled = ADA.sub("TEL;TYPE=mobile:+1-555-0100\r\n",
"item1.TEL;type=CELL:+1-555-0100\r\nitem1.X-ABLabel:Google Voice\r\n")
with_contacts({"ada" => labeled}) do |store|
get "/contacts/ada/edit"
digest = store.contact("ada").phones.first.line.digest
assert_includes last_response.body,
'<input type="text" class="row-label" name="phone[' + digest + '][label]" value="Google Voice"'
end
end
# A labeled number is two lines, and blanking it removes both: the
# label left behind would name nothing, and the next line Contacts
# files under its group would wear it.
def test_a_blank_labeled_phone_removes_its_label_too
labeled = ADA.sub("TEL;TYPE=mobile:+1-555-0100\r\n",
"item1.TEL;type=CELL:+1-555-0100\r\nitem1.X-ABLabel:Google Voice\r\n")
with_contacts({"ada" => labeled}) do |store|
digest = store.contact("ada").phones.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
phone: {digest => {"value" => ""}}
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
refute_includes card, "TEL"
refute_includes card, "X-ABLabel"
end
end
## Editing labels
# A relabel posts the row's label and value both, the value as it
# rendered: the save writes the new word and leaves the value's bytes.
def relabel(store, field, label)
contact = store.contact("ada")
row = contact.own.public_send({"phone" => :phones, "email" => :emails, "address" => :addresses}.fetch(field)).first
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: contact.etag,
field => {row.line.digest => {"label" => label, **row.fields}}
assert_equal 303, last_response.status
store.contact("ada").vcard.to_s
end
# A word Contacts models goes in as a TYPE, in its spelling, in place
# of the types the label field showed
# (docs/plans/2026-10-22-a-row-edits-its-label.md, "The typed kinds'
# rule").
def test_a_relabel_to_a_type_word_writes_the_type
with_contacts({"ada" => ADA}) do |store|
card = relabel(store, "phone", "Work")
assert_includes card, "TEL;TYPE=WORK:+1-555-0100\r\n"
end
end
# What the label never showed stays: `pref` and the default `voice`
# ride through a relabel, with the line's other parameters.
def test_a_relabel_keeps_the_types_the_label_did_not_show
apple = ADA.sub("TEL;TYPE=mobile:", "TEL;type=CELL;type=VOICE;type=pref:")
with_contacts({"ada" => apple}) do |store|
card = relabel(store, "phone", "home")
assert_includes card, "TEL;type=VOICE;type=pref;TYPE=HOME:+1-555-0100\r\n"
end
end
# Any other word is a custom label: an X-ABLabel under the card's
# lowest free group, straight after its row — the pair Contacts keeps
# bound (docs/apple-contacts.md, "An annotation survives only on its
# own line").
def test_a_relabel_to_any_other_word_writes_an_x_ablabel_in_a_free_group
taken = ADA.sub("EMAIL;", "item1.EMAIL;")
with_contacts({"ada" => taken}) do |store|
card = relabel(store, "phone", "Google Voice")
assert_includes card, "item2.TEL:+1-555-0100\r\nitem2.X-ABLabel:Google Voice\r\n"
assert_equal "Google Voice", store.contact("ada").phones.first.label
end
end
# A labeled row keeps its group: relabelled, the old label goes and
# the new one takes its place; relabelled to a type word, the label
# goes and the type replaces the types it hid behind.
def test_a_relabel_of_a_labeled_row_replaces_its_label
labeled = ADA.sub("TEL;TYPE=mobile:+1-555-0100\r\n",
"item1.TEL;type=CELL:+1-555-0100\r\nURL:https://example.com\r\nitem1.X-ABLabel:Google Voice\r\n")
with_contacts({"ada" => labeled}) do |store|
card = relabel(store, "phone", "Signal number")
assert_includes card, "item1.TEL:+1-555-0100\r\nitem1.X-ABLabel:Signal number\r\nURL:https://example.com\r\n"
refute_includes card, "Google Voice"
card = relabel(store, "phone", "home")
assert_includes card, "item1.TEL;TYPE=HOME:+1-555-0100\r\nURL:https://example.com\r\n"
refute_includes card, "X-ABLabel"
end
end
# Other is Apple's own word and goes in wrapped, as the macOS data
# carries it; a blank label writes no label at all.
def test_other_writes_apples_wrapped_label_and_blank_writes_none
with_contacts({"ada" => ADA}) do |store|
card = relabel(store, "email", "other")
assert_includes card, "item1.EMAIL:ada@example.com\r\nitem1.X-ABLabel:_$!<Other>!$_\r\n"
assert_equal "Other", store.contact("ada").emails.first.label
card = relabel(store, "address", "")
assert_includes card, "ADR:;;12 Analytical Way;"
end
end
# An added row takes its label as a standing one does.
def test_an_added_row_lands_under_its_label
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
new_email: {"0" => {"label" => "work", "value" => "countess@example.com"}},
new_address: {"0" => {"label" => "lab", "street" => "1 Engine Row"}}
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
assert_includes card, "EMAIL;TYPE=WORK:countess@example.com\r\n"
assert_includes card, "item1.ADR:;;1 Engine Row;;;;\r\nitem1.X-ABLabel:lab\r\n"
end
end
## Editing emails
# The email rows: the phone row's own shape over EMAIL.
def test_the_edit_screen_renders_a_digest_named_field_per_email
with_contacts({"ada" => ADA}) do |store|
get "/contacts/ada/edit"
digest = store.contact("ada").emails.first.line.digest
assert_includes last_response.body,
'<div class="field" data-blank-removes>' \
'<input type="text" class="row-label" name="email[' + digest + '][label]" value="home" autocomplete="off" ' \
'list="email-labels" placeholder="email" aria-label="email label">' \
'<input type="email" name="email[' + digest + '][value]" value="ada@example.com" placeholder="removed on save" ' \
'aria-label="email"></div>'
end
end
# The addressed edit at work over EMAIL: the submitted row swaps its
# line's value under the line's own header, and every other byte of
# the stored card — the other EMAIL-able lines included — stands
# untouched.
def test_saving_a_changed_email_replaces_only_its_line
two = ADA.sub("EMAIL;TYPE=home:ada@example.com\r\n",
"EMAIL;TYPE=home:ada@example.com\r\nEMAIL;TYPE=work:countess@example.com\r\n")
with_contacts({"ada" => two}) do |store|
digest = store.contact("ada").emails.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
email: {digest => {"value" => "lovelace@example.com"}}
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
assert_includes card, "EMAIL;TYPE=home:lovelace@example.com\r\n"
assert_includes card, "EMAIL;TYPE=work:countess@example.com\r\n"
refute_includes card, "ada@example.com"
end
end
# The header is the line's own, kept byte for byte — the phones'
# own rule, over the parameters an EMAIL line can carry.
def test_a_saved_email_keeps_its_lines_own_parameters
apple = ADA.sub("EMAIL;TYPE=home:ada@example.com", "EMAIL;type=INTERNET;type=HOME;type=pref:ada@example.com")
with_contacts({"ada" => apple}) do |store|
digest = store.contact("ada").emails.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
email: {digest => {"value" => "lovelace@example.com"}}
assert_equal 303, last_response.status
assert_includes store.contact("ada").vcard.to_s,
"EMAIL;type=INTERNET;type=HOME;type=pref:lovelace@example.com\r\n"
end
end
# Blank equals absent for a row with a digest: the blank removes
# the line, the phones' own rule.
def test_a_blank_email_row_removes_the_line
with_contacts({"ada" => ADA}) do |store|
digest = store.contact("ada").emails.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
email: {digest => {"value" => " "}}
assert_equal 303, last_response.status
refute_includes store.contact("ada").vcard.to_s, "EMAIL"
end
end
# An unchanged row never rewrites its line, and the whole save
# leaves the stored card byte for byte as it lay.
def test_an_unchanged_email_and_address_leave_the_stored_card_byte_identical
with_contacts({"ada" => ADA}) do |store|
before = store.contact("ada").stored.to_s
email_digest = store.contact("ada").emails.first.line.digest
address_digest = store.contact("ada").addresses.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", note: "Countess of Lovelace.",
company: "0", etag: store.contact("ada").etag, new_email: {"0" => {"value" => ""}},
email: {email_digest => {"value" => "ada@example.com"}},
address: {address_digest => {
"street" => "12 Analytical Way", "locality" => "London",
"region" => "England", "postal_code" => "NW1 1AA",
"country" => "United Kingdom",
}}
assert_equal 303, last_response.status
assert_equal before, store.contact("ada").stored.to_s
end
end
# The revealed row: a value lands as a bare EMAIL before END:VCARD,
# and a blank one inserts nothing — the phones' own rules.
def test_an_added_email_lands_as_a_bare_email_before_the_end
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
new_email: {"0" => {"value" => "lovelace@example.com"}, "1" => {"value" => ""},
"2" => {"value" => "countess@example.com"}}
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
assert_equal ["ada@example.com", "lovelace@example.com", "countess@example.com"],
store.contact("ada").emails.map(&:value)
assert_includes card, "EMAIL:countess@example.com\r\nBDAY:1985-12-10\r\nEND:VCARD\r\n"
end
end
## Editing handles
SIGNAL = ADA.sub("BDAY:", "IMPP;X-SERVICE-TYPE=signal:x-apple:ada.42\r\nBDAY:")
# A handle shows under its service, the way a labeled phone shows
# under its label.
def test_show_lists_a_handle_under_its_service
with_contacts({"ada" => SIGNAL}) do
get "/contacts/ada"
assert_includes last_response.body, '<dt class="type-label">signal</dt><dd class="type-body-sm"><div style="white-space: pre-wrap;">ada.42</div></dd>'
end
end
# A handle row is the phone row's shape, its service the label in
# the type column; the scheme is the line's, like its parameters,
# and no field models it.
def test_the_edit_screen_renders_a_handle_under_its_service
with_contacts({"ada" => SIGNAL}) do |store|
get "/contacts/ada/edit"
digest = store.contact("ada").handles.first.line.digest
assert_includes last_response.body,
'<div class="field" data-blank-removes>' \
'<input type="text" class="row-label" name="handle[' + digest + '][label]" value="signal" autocomplete="off" ' \
'list="handle-labels" placeholder="handle" aria-label="handle label">' \
'<input type="text" name="handle[' + digest + '][value]" value="ada.42" placeholder="removed on save" ' \
'aria-label="handle"></div>'
end
end
def post_handle(store, service:, value:)
digest = store.contact("ada").handles.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", note: "Countess of Lovelace.",
etag: store.contact("ada").etag,
handle: {digest => {"label" => service, "value" => value}}
assert_equal 303, last_response.status
store.contact("ada").vcard.to_s
end
# A changed handle lands under the line's own header and scheme.
def test_saving_a_changed_handle_keeps_its_service_and_scheme
with_contacts({"ada" => SIGNAL.sub("IMPP;", "item1.IMPP;type=pref;")}) do |store|
card = post_handle(store, service: "signal", value: "countess.01")
assert_includes card, "item1.IMPP;type=pref;X-SERVICE-TYPE=signal:x-apple:countess.01\r\n"
refute_includes card, "ada.42"
end
end
# A changed service keeps the line's group and other parameters and
# moves the handle under x-apple:, the old scheme being the old
# service's (docs/plans/2026-10-18-signal-handles.md, "The service
# is the row's").
def test_saving_a_changed_service_rewrites_the_header_and_scheme
xmpp = ADA.sub("BDAY:", "item1.IMPP;type=pref;X-SERVICE-TYPE=Jabber:xmpp:ada@example.com\r\nBDAY:")
with_contacts({"ada" => xmpp}) do |store|
card = post_handle(store, service: "Delta Chat", value: "ada@example.com")
assert_includes card, "item1.IMPP;type=pref;X-SERVICE-TYPE=Delta Chat:x-apple:ada@example.com\r\n"
refute_includes card, "Jabber"
end
end
def test_a_blank_service_writes_none
with_contacts({"ada" => SIGNAL}) do |store|
card = post_handle(store, service: " ", value: "ada.42")
assert_includes card, "IMPP:x-apple:ada.42\r\n"
assert_equal [[nil, "ada.42"]], store.contact("ada").handles.map { [it.service, it.value] }
end
end
def test_a_blank_handle_removes_the_line_whatever_its_service
with_contacts({"ada" => SIGNAL}) do |store|
card = post_handle(store, service: "signal", value: "")
refute_includes card, "IMPP"
end
end
def test_an_unchanged_handle_leaves_the_stored_card_byte_identical
with_contacts({"ada" => SIGNAL}) do |store|
before = store.contact("ada").stored.to_s
post_handle(store, service: "signal", value: "ada.42")
assert_equal before, store.contact("ada").stored.to_s
end
end
# Each repeatable row posts as a hash of its label and its fields;
# any other shape is a 400, the address rows' own rule.
def test_a_row_field_in_a_shape_the_form_never_sends_is_a_400
with_contacts({"ada" => SIGNAL}) do |store|
contact = store.contact("ada")
digest = contact.handles.first.line.digest
[{handle: {digest => "ada.42"}},
{handle: "ada.42"},
{new_handle: ["ada.42"]},
{phone: {"0" * 64 => "+1-555-0199"}},
{new_email: ["ada@example.com"]}].each do |field|
post "/contacts/ada", {first: "Ada", last: "Lovelace", company: "0", etag: contact.etag}.merge(field)
assert_equal 400, last_response.status, field
assert_equal contact.etag, store.contact("ada").etag
end
end
end
# The revealed rows write each service as typed, in Contacts.app's
# spelling of a handle (docs/plans/2026-10-18-signal-handles.md),
# and a row with no handle inserts nothing.
def test_added_handles_land_as_impp_lines_before_the_end
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
new_handle: {"0" => {"label" => "signal", "value" => "ada.42"},
"1" => {"label" => "signal", "value" => ""},
"2" => {"label" => "WhatsApp", "value" => "+1-555-0100"}}
assert_equal 303, last_response.status
assert_includes store.contact("ada").vcard.to_s,
"IMPP;X-SERVICE-TYPE=signal:x-apple:ada.42\r\n" \
"IMPP;X-SERVICE-TYPE=WhatsApp:x-apple:+1-555-0100\r\nBDAY:1985-12-10\r\nEND:VCARD\r\n"
assert_equal [["signal", "ada.42"], ["WhatsApp", "+1-555-0100"]],
store.contact("ada").handles.map { [it.service, it.value] }
end
end
# A quote is no character a parameter value can carry, quoted or
# not, so it is dropped rather than ending the value early.
def test_an_added_service_drops_what_a_parameter_cannot_carry
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
new_handle: {"0" => {"label" => %(Sig"nal), "value" => "ada.42"}}
assert_equal 303, last_response.status
assert_includes store.contact("ada").vcard.to_s, "IMPP;X-SERVICE-TYPE=Signal:x-apple:ada.42\r\n"
end
end
## Editing addresses
# An address row: the label field and a stack of component
# fields in the value column, each named by the row's digest and
# its component — and no po box field, which the save preserves
# raw rather than letting a form near it.
def test_the_edit_screen_renders_component_fields_per_address
with_contacts({"ada" => ADA}) do |store|
get "/contacts/ada/edit"
digest = store.contact("ada").addresses.first.line.digest
body = last_response.body
assert_includes body,
'<div class="field" data-blank-removes>' \
'<input type="text" class="row-label" name="address[' + digest + '][label]" value="home" autocomplete="off" ' \
'list="address-labels" placeholder="address" aria-label="address label">' \
'<div class="field-stack">' \
'<input type="text" name="address[' + digest + '][street]" value="12 Analytical Way" placeholder="street" aria-label="street">' \
'<input type="text" name="address[' + digest + '][extended]" placeholder="street 2" aria-label="street 2">' \
'<input type="text" name="address[' + digest + '][locality]" value="London" placeholder="city" aria-label="city">'
assert_includes body,
'name="address[' + digest + '][postal_code]" value="NW1 1AA" placeholder="postal code"'
assert_includes body,
'name="address[' + digest + '][country]" value="United Kingdom" placeholder="country"'
refute_includes body, "po_box"
end
end
# The addressed edit at work over ADR: the submitted components
# swap under the line's own header, and every other byte of the
# stored card stands untouched.
def test_saving_a_changed_address_replaces_only_its_line
with_contacts({"ada" => ADA}) do |store|
digest = store.contact("ada").addresses.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
address: {digest => {
"street" => "5 Analytical Way", "locality" => "London",
"region" => "England", "postal_code" => "NW1 1AA",
"country" => "United Kingdom",
}}
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
assert_includes card, "ADR;TYPE=home:;;5 Analytical Way;London;England;NW1 1AA;United Kingdom\r\n"
assert_includes card, "TEL;TYPE=mobile:+1-555-0100\r\n"
assert_includes card, "EMAIL;TYPE=home:ada@example.com\r\n"
follow_redirect!
assert_includes last_response.body, "5 Analytical Way"
end
end
# The editor posts every address field as a hash of components; any
# other shape is a POST made by hand, refused as a 400 before the
# save reads it (Web#card_form_params), the card untouched.
def test_an_address_field_in_a_shape_the_form_never_sends_is_a_400
with_contacts({"ada" => ADA}) do |store|
contact = store.contact("ada")
digest = contact.addresses.first.line.digest
[{address: {digest => "5 Analytical Way"}},
{address: "5 Analytical Way"},
{new_address: ["5 Analytical Way"]},
{phone: "+1-555-0199"}].each do |field|
post "/contacts/ada", {first: "Ada", last: "Lovelace", company: "0", etag: contact.etag}.merge(field)
assert_equal 400, last_response.status, field
assert_equal contact.etag, store.contact("ada").etag
end
end
end
# The name, note and Company fields, the birthday row and the
# snapshot etag are on every save the editor posts, each in one
# shape; a save missing the etag or the Company box's answer, or
# carrying a field in another shape, is a POST made by hand and a
# 400, the card untouched.
def test_a_save_missing_a_field_or_in_another_shape_is_a_400
with_contacts({"ada" => ADA}) do |store|
contact = store.contact("ada")
good = {"first" => "Ada", "last" => "Lovelace", "company" => "0", "etag" => contact.etag}
[good.except("etag"),
good.except("company"),
good.merge("company" => "maybe"),
good.merge("first" => {"x" => "Ada"}),
good.merge("note" => ["two", "notes"]),
good.merge("new_phone" => "+1-555-0199"),
good.merge("birthday" => "1815-12-10"),
good.merge("birthday" => {"year" => {"x" => "1815"}})].each do |params|
post "/contacts/ada", params
assert_equal 400, last_response.status, params
assert_equal contact.etag, store.contact("ada").etag
end
end
end
# The groups dialog posts its boxes as lists; a `groups` in another
# shape is a 400, so it cannot read as every box unticked against a
# `was` that still lists them.
def test_a_groups_save_with_a_groups_field_that_is_not_a_list_is_a_400
with_contacts({"ada" => ADA}) do |store|
id = FixtureData.seed_group(store, name: "Family", members: %w[ada])
post "/contacts/ada/groups", "groups" => id, "was" => [id]
assert_equal 400, last_response.status
assert_equal [id], store.groups_of("ada").map(&:id)
end
end
# The header is the line's own, kept byte for byte — the phones'
# own rule, over the parameters an ADR line can carry.
def test_a_saved_address_keeps_its_lines_own_parameters
apple = ADA.sub(
"ADR;TYPE=home:;;12 Analytical Way;London;England;NW1 1AA;United Kingdom",
"ADR;type=HOME;type=pref:;;12 Analytical Way;London;England;NW1 1AA;United Kingdom",
)
with_contacts({"ada" => apple}) do |store|
digest = store.contact("ada").addresses.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
address: {digest => {
"street" => "5 Analytical Way", "locality" => "London",
"region" => "England", "postal_code" => "NW1 1AA",
"country" => "United Kingdom",
}}
assert_equal 303, last_response.status
assert_includes store.contact("ada").vcard.to_s,
"ADR;type=HOME;type=pref:;;5 Analytical Way;London;England;NW1 1AA;United Kingdom\r\n"
end
end
# Removal is the reader's own rule: a row blank throughout, po box
# included, removes the line.
def test_an_address_row_blank_throughout_removes_the_line
with_contacts({"ada" => ADA}) do |store|
digest = store.contact("ada").addresses.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
address: {digest => {
"street" => " ", "extended" => "", "locality" => "",
"region" => "", "postal_code" => "", "country" => "",
}}
assert_equal 303, last_response.status
refute_includes store.contact("ada").vcard.to_s, "ADR"
end
end
# A partially blanked row keeps its line — partial blanks are legal
# empty components, the reader's own rule in the other direction.
def test_a_partially_blanked_address_keeps_the_line
with_contacts({"ada" => ADA}) do |store|
digest = store.contact("ada").addresses.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
address: {digest => {
"street" => "12 Analytical Way", "locality" => "London",
"region" => "", "postal_code" => "", "country" => "",
}}
assert_equal 303, last_response.status
assert_includes store.contact("ada").vcard.to_s,
"ADR;TYPE=home:;;12 Analytical Way;London;;;\r\n"
end
end
# The po box and any component the form left alone keep their own
# bytes — n_line's splice rule at the component grain, where a
# rebuild through unescape and re-escape is not byte-stable (the
# unrecognized `\\q` escape would come back doubled).
def test_the_addresss_untouched_components_are_preserved_byte_for_byte
po_box = ADA.sub(
"ADR;TYPE=home:;;12 Analytical Way;London;England;NW1 1AA;United Kingdom",
"ADR;TYPE=home:P.O. Box 5;Apt \\q;12 Way\\, West;London;England;NW1 1AA;United Kingdom",
)
with_contacts({"ada" => po_box}) do |store|
digest = store.contact("ada").addresses.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
address: {digest => {
"street" => "12 Way, West", "extended" => "Apt \\q",
"locality" => "Manchester", "region" => "England",
"postal_code" => "NW1 1AA", "country" => "United Kingdom",
}}
assert_equal 303, last_response.status
assert_includes store.contact("ada").vcard.to_s,
"ADR;TYPE=home:P.O. Box 5;Apt \\q;12 Way\\, West;Manchester;England;NW1 1AA;United Kingdom\r\n"
end
end
# A component is text and the value is structure: punctuation
# escapes in the written line (RFC 2426 section 2.4.2).
def test_address_punctuation_escapes_in_the_written_line
with_contacts({"ada" => ADA}) do |store|
digest = store.contact("ada").addresses.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
address: {digest => {
"street" => "12 Way, East; Annexe", "locality" => "London",
"region" => "England", "postal_code" => "NW1 1AA",
"country" => "United Kingdom",
}}
assert_equal 303, last_response.status
assert_includes store.contact("ada").vcard.to_s,
"ADR;TYPE=home:;;12 Way\\, East\\; Annexe;London;England;NW1 1AA;United Kingdom\r\n"
end
end
# The revealed address row: the components land in the value's own
# order — po box empty, extended before street (RFC 2426 section
# 3.2.1) — whatever order the form stacks them in, and a row blank
# throughout inserts nothing, the new-row rule.
def test_an_added_address_lands_in_component_order_before_the_end
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
new_address: {"1" => {
"street" => "5 Analytical Way", "extended" => "Apt 9",
"locality" => "London", "country" => "United Kingdom",
}}
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
assert_includes card, "ADR:;Apt 9;5 Analytical Way;London;;;United Kingdom\r\nBDAY:1985-12-10\r\nEND:VCARD\r\n"
assert_includes card, "ADR;TYPE=home:;;12 Analytical Way;London;England;NW1 1AA;United Kingdom\r\n"
end
end
def test_a_blank_address_add_row_inserts_nothing
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
new_address: {"0" => {"street" => " ", "country" => ""}}
assert_equal 303, last_response.status
assert_equal 1, store.contact("ada").addresses.length
end
end
## Editing the birthday
# The one row that saves to the model rather than the card: the
# prefill is Contact#birthday, three controls on one line — a month
# select (names, constrained by construction) and ranged number
# inputs — in the value column, month-day-year matching the prose
# the details page renders. The picker follows them and is not a
# fourth field: it carries no name, so the three boxes are still
# the whole of what a save reads
# (docs/plans/2026-09-15-a-picker-beside-the-birthday.md).
def test_the_edit_screen_renders_the_birthday_row
with_contacts({"ada" => ADA}) do
get "/contacts/ada/edit"
body = last_response.body
assert_includes body,
'<div class="field" data-blank-removes><span>birthday</span><div class="date-row">' +
%(<select name="birthday[month]" aria-label="month" x-resize="#{BIRTHDAY_MONTH_FIT}"><option value="">month</option>) +
'<option value="1" data-short="Jan">January</option>'
assert_includes body, '<option value="12" selected data-short="Dec">December</option></select>'
assert_includes body,
'<input type="number" name="birthday[day]" value="10" min="1" max="31" placeholder="day" aria-label="day">'
assert_includes body,
'<input type="number" name="birthday[year]" value="1985" min="1" max="9999" placeholder="year" aria-label="year">' +
BIRTHDAY_PICKER
# The remove control: one click that speaks the three-blank
# rule, over a held birthday only — Gloss's IconButton, so the
# glyph carries the label.
assert_includes body,
%(<button type="button" class="icon-button" data-size="sm" aria-label="Remove birthday" @click="$el.closest('.date-row').querySelectorAll('select, input').forEach(el => el.value = '')"><svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden><path d="M18 6 6 18"></path><path d="m6 6 12 12"></path></svg></button>)
# A held birthday is not addable again: the dialog's list drops
# the type.
refute_includes body, 'value="birthday"'
end
end
# A birthday edit is a model write: the served card composes the new
# BDAY, the stored bytes never move, and the change-log entry carries
# the new composed etag — a client's token sees what a device
# downloads (Store#save_edit).
def test_saving_a_changed_birthday_moves_the_served_card_only
with_contacts({"ada" => ADA}) do |store|
before = store.contact("ada").stored.to_s
post "/contacts/ada", first: "Ada", last: "Lovelace", note: "Countess of Lovelace.",
company: "0", etag: store.contact("ada").etag,
birthday: {"month" => "4", "day" => "12", "year" => "1990"}
assert_equal 303, last_response.status
contact = store.contact("ada")
assert_equal before, contact.stored.to_s
assert_includes contact.vcard.to_s, "BDAY:1990-04-12\r\n"
assert_equal contact.etag, store.changes.last.etag
follow_redirect!
assert_includes last_response.body, "April 12, 1990"
end
end
# A birthday without a year is the one partial shape the wire
# carries — composed as Apple's sentinel, the form a client has been
# verified to accept (docs/plans/2026-08-31-partial-birthdays.md).
def test_a_birthday_without_a_year_is_saved_and_served_apples_way
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
birthday: {"month" => "12", "day" => "10", "year" => ""}
assert_equal 303, last_response.status
assert_includes store.contact("ada").vcard.to_s,
"BDAY;X-APPLE-OMIT-YEAR=1604:1604-12-10\r\n"
end
end
# The shapes no client renders are editable all the same — the model
# holds them, the web UI shows them, and nothing composes on their
# behalf: their documented fate.
def test_a_month_alone_is_saved_and_serves_nothing
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
birthday: {"month" => "4", "day" => "", "year" => ""}
assert_equal 303, last_response.status
contact = store.contact("ada")
assert_equal ProTacts::Birthday.new(month: 4), contact.birthday
refute_includes contact.vcard.to_s, "BDAY:"
follow_redirect!
assert_includes last_response.body, "April"
end
end
# Three blanks remove the birthday, the form's blank-equals-absent —
# the model row goes and the composed card with it.
def test_a_blank_birthday_removes_it
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
birthday: {"month" => "", "day" => "", "year" => ""}
assert_equal 303, last_response.status
contact = store.contact("ada")
assert_nil contact.birthday
refute_includes contact.vcard.to_s, "BDAY"
end
end
# A birthday added from the dialog saves like one edited: the
# added row names the same birthday[] group the standing row
# would, the group parses into the model, and the served card
# composes the line over the card's untouched bytes.
def test_a_birthday_added_from_the_dialog_saves
bare = ADA.sub("BDAY:1985-12-10\r\n", "")
with_contacts({"ada" => bare}) do |store|
assert_nil store.contact("ada").birthday
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
birthday: {"month" => "3", "day" => "14", "year" => "1879"}
assert_equal 303, last_response.status
contact = store.contact("ada")
assert_equal ProTacts::Birthday.new(year: 1879, month: 3, day: 14), contact.birthday
assert_includes contact.vcard.to_s, "BDAY:1879-03-14\r\n"
follow_redirect!
assert_includes last_response.body, "March 14, 1879"
end
end
# A POST that carries no birthday group keeps the model — a group
# the request never carried touches nothing, this form's own no-row
# save included.
def test_a_save_without_birthday_fields_leaves_the_model_alone
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag
assert_equal 303, last_response.status
assert_equal ProTacts::Birthday.new(year: 1985, month: 12, day: 10),
store.contact("ada").birthday
end
end
# A shape the grammar refuses — a year and a day with no month
# between them — is a toast and nothing written: the constructor's
# ArgumentError is the refusal, caught where a re-render is the
# fallback. The browser's number inputs make this a hand-crafted
# POST's own, the name toast's backstop sibling.
def test_a_year_and_day_without_a_month_is_refused
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
birthday: {"month" => "", "day" => "12", "year" => "1985"}
assert_equal 200, last_response.status
assert_includes last_response.body,
'<div role="status" data-fixed><span>That birthday is not a shape a date can take.</span></div>'
assert_equal ProTacts::Birthday.new(year: 1985, month: 12, day: 10),
store.contact("ada").birthday
end
end
def test_nonsense_in_a_birthday_field_is_refused
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
birthday: {"month" => "4", "day" => "12", "year" => "19x5"}
assert_equal 200, last_response.status
assert_includes last_response.body, "That birthday is not a shape a date can take."
assert_equal ProTacts::Birthday.new(year: 1985, month: 12, day: 10),
store.contact("ada").birthday
end
end
# The one state the row cannot write: a stored card carrying its own
# BDAY spelling (the model refused it, so it stayed in the card) — a
# submitted birthday would compose a second BDAY beside it. Refused
# whole, name edit included: one submit, one write.
def test_a_birthday_against_a_cards_own_spelling_is_refused
resident = ADA.sub("BDAY:1985-12-10\r\n", "BDAY:1985-13\r\n")
with_contacts({"ada" => resident}) do |store|
assert_nil store.contact("ada").birthday
post "/contacts/ada", first: "Grace", last: "Hopper", company: "0", etag: store.contact("ada").etag,
birthday: {"month" => "4", "day" => "", "year" => "1985"}
assert_equal 200, last_response.status
assert_includes last_response.body,
'<div role="status" data-fixed><span>This contact's card carries its own birthday spelling; nothing was saved.</span></div>'
contact = store.contact("ada")
assert_nil contact.birthday
assert_equal 1, contact.vcard.to_s.scan("BDAY").length
assert_includes contact.vcard.to_s, "BDAY:1985-13\r\n"
assert_includes contact.vcard.to_s, "FN:Ada Lovelace\r\n"
end
end
## Adding a property
# The add affordance: a trigger on the back link's line — the place
# the details page puts its edit link — and a dialog that names
# types rather than collecting values.
def test_the_edit_screen_opens_an_add_dialog_from_the_record_nav
with_contacts({"ada" => ADA}) do
get "/contacts/ada/edit"
body = last_response.body
assert_includes body,
'<a href="/contacts/ada" class="type-label">‹ Ada Lovelace</a>' \
'<button type="button" data-size="sm" popovertarget="add-property">add property</button></div>'
assert_includes body, '<dialog id="add-property" popover="auto">'
# A radio group of the kinds the save can insert, minus the
# birthday this contact holds (#addable_types). The shared
# `name` is what makes them a native group; it is never
# submitted, the dialog having no form owner.
assert_includes body,
'<div class="field-stack" role="radiogroup" aria-label="Type">' \
'<label><input type="radio" name="add-type" value="phone" x-model="type">phone</label>' \
'<label><input type="radio" name="add-type" value="email" x-model="type">email</label>' \
'<label><input type="radio" name="add-type" value="handle" x-model="type">handle</label>' \
'<label><input type="radio" name="add-type" value="address" x-model="type">address</label></div>'
assert_includes body,
'<button type="button" popovertarget="add-property" popovertargetaction="hide">Cancel</button>'
# Add makes the row and then hides the popover, in that order —
# the row has to exist before x-init's focus can land on it.
# The guard refuses a second birthday: a hidden radio can stay
# x-model's pick, and the row's fields are one group Rack
# would collapse to the last copy.
assert_includes body,
%(@click="(type !== 'birthday' || !added.includes('birthday')) && added.push(type); $el.closest('dialog').hidePopover()")
end
end
# The card carries no blank row at rest: the rows are a template
# Alpine fills from `added`, so none stands waiting to be used and
# none lingers after one is. The dialog and the form share a scope
# because Alpine scopes by ancestry.
def test_added_rows_are_a_template_the_dialog_appends_to
with_contacts({"ada" => ADA}) do
body = (get("/contacts/ada/edit") && last_response.body)
assert_includes body, %(<div x-data="{ added: [], type: 'phone', company: false, firstBlank: false, lastBlank: false }">)
# One template serves every kind, each the standing row's shape: a
# label field bound to the kind, then the single-value kinds an
# input whose type and name bind to it, the address kind the same
# six component fields a standing row gets — named by the add
# index, there being no line yet to digest.
assert_includes body,
'<template x-for="(kind, i) in added" :key="i"><div class="field">' \
'<template x-if="kind === \'birthday\'"><span x-text="kind"></span></template>' \
'<template x-if="kind !== \'birthday\'">' \
'<input type="text" class="row-label" autocomplete="off" :name="`new_${kind}[${i}][label]`" ' \
':list="`${kind}-labels`" :placeholder="kind" :aria-label="`${kind} label`"></template>' \
'<template x-if="{ phone: \'tel\', email: \'email\', handle: \'text\' }[kind]">' \
'<input :type="{ phone: \'tel\', email: \'email\', handle: \'text\' }[kind]" ' \
':name="`new_${kind}[${i}][value]`" :aria-label="kind" x-init="$el.focus()">' \
'</template>' \
'<template x-if="kind === \'address\'">' \
'<div class="field-stack">' \
'<input type="text" :name="`new_address[${i}][street]`" placeholder="street" aria-label="street" x-init="$el.focus()">' \
'<input type="text" :name="`new_address[${i}][extended]`" placeholder="street 2" aria-label="street 2">' \
'<input type="text" :name="`new_address[${i}][locality]`" placeholder="city" aria-label="city">' \
'<input type="text" :name="`new_address[${i}][region]`" placeholder="region" aria-label="region">' \
'<input type="text" :name="`new_address[${i}][postal_code]`" placeholder="postal code" aria-label="postal code">' \
'<input type="text" :name="`new_address[${i}][country]`" placeholder="country" aria-label="country">' \
'</div></template>'
# The birthday kind: the standing row's own controls with
# nothing prefilled, naming the model's birthday[] group — no
# digest, a contact holding at most one, and no removal state,
# the added rows' exemption. The picker comes along, an added
# birthday being as pickable as a standing one; the remove
# control does not, there being nothing yet to remove.
# Asserted in two contiguous pieces: the month names sit
# between the empty option and December.
assert_includes body,
'<template x-if="kind === \'birthday\'"><div class="date-row">' +
%(<select name="birthday[month]" aria-label="month" x-init="$el.focus()" x-resize="#{BIRTHDAY_MONTH_FIT}">) +
'<option value="" selected>month</option>'
assert_includes body,
'<option value="12" data-short="Dec">December</option></select>' \
'<input type="number" name="birthday[day]" min="1" max="31" placeholder="day" aria-label="day">' \
'<input type="number" name="birthday[year]" min="1" max="9999" placeholder="year" aria-label="year">' +
BIRTHDAY_PICKER + "</div></template></div></template>"
# The template is inside the edit form, so one Save writes every
# row it made along with everything else. Indexed from the
# form's start, the header's search form closing before both.
edit_form = body.index('<form action="/contacts/ada" method="post"')
assert_operator body.index("<template"), :<, body.index("</form>", edit_form)
# No standing row, and no field the server would read if Alpine
# never ran.
refute_includes body, 'name="new_phone[]"'
end
end
# One pass adds as many phones as rows were made for it. The blanks
# among them are rows nobody typed in, and they cost nothing.
def test_a_single_save_adds_every_phone_that_was_typed
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
new_phone: {"0" => {"value" => "+1-555-0177"}, "1" => {"value" => "+1-555-0188"},
"2" => {"value" => ""}, "3" => {"value" => ""}}
assert_equal 303, last_response.status
assert_equal ["+1-555-0100", "+1-555-0177", "+1-555-0188"],
store.contact("ada").phones.map(&:value)
end
end
# The addressed edit at work: the submitted row swaps its line's
# value under the line's own header, and every other byte of the
# stored card — the other TEL-able lines included — stands
# untouched.
def test_saving_a_changed_phone_replaces_only_its_line
two = ADA.sub("TEL;TYPE=mobile:+1-555-0100\r\n", "TEL;TYPE=mobile:+1-555-0100\r\nTEL;TYPE=work:+1-555-0199\r\n")
with_contacts({"ada" => two}) do |store|
digest = store.contact("ada").phones.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
phone: {digest => {"value" => "+1-555-0150"}}
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
assert_includes card, "TEL;TYPE=mobile:+1-555-0150\r\n"
assert_includes card, "TEL;TYPE=work:+1-555-0199\r\n"
refute_includes card, "+1-555-0100"
follow_redirect!
assert_includes last_response.body, "+1-555-0150"
end
end
# A labeled phone's value edit swaps the number under the line's own
# header — group prefix included — so the X-ABLabel stays bound to
# the row it names. Removing the row is the editor's own unsolved
# rule, and waits for it
# (docs/plans/2026-09-18-phone-labels.md, "What this leaves").
def test_saving_a_changed_labeled_phone_keeps_its_label_bound
labeled = ADA.sub("TEL;TYPE=mobile:+1-555-0100\r\n",
"item1.TEL;type=CELL:+1-555-0100\r\nitem1.X-ABLabel:Google Voice\r\n")
with_contacts({"ada" => labeled}) do |store|
digest = store.contact("ada").phones.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
phone: {digest => {"value" => "+1-555-0150"}}
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
assert_includes card, "item1.TEL;type=CELL:+1-555-0150\r\n"
assert_includes card, "item1.X-ABLabel:Google Voice\r\n"
end
end
# The header is the line's own, kept byte for byte: macOS writes
# three TYPE parameters on one TEL, and a save that rebuilt the
# header from a type field would drop the two the form never
# showed. The value is the only thing that moves.
def test_a_saved_phone_keeps_its_lines_own_parameters
apple = ADA.sub("TEL;TYPE=mobile:+1-555-0100", "TEL;type=CELL;type=VOICE;type=pref:+1-555-0100")
with_contacts({"ada" => apple}) do |store|
digest = store.contact("ada").phones.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
phone: {digest => {"value" => "+1-555-0150"}}
assert_equal 303, last_response.status
assert_includes store.contact("ada").vcard.to_s,
"TEL;type=CELL;type=VOICE;type=pref:+1-555-0150\r\n"
end
end
# Blank equals absent for a row with a digest: the blank removes
# the line, the reader's own rule in the other direction. The FN
# assertion keeps an empty-card regression from passing vacuously.
def test_a_blank_phone_row_removes_the_line
with_contacts({"ada" => ADA}) do |store|
digest = store.contact("ada").phones.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
phone: {digest => {"value" => " "}}
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
refute_includes card, "TEL"
assert_includes card, "FN:Ada Lovelace\r\n"
end
end
# An unchanged row never rewrites its line — byte identity by
# construction, not by faithful re-rendering — and the whole save
# leaves the stored card byte for byte as it lay.
def test_an_unchanged_phone_leaves_the_stored_card_byte_identical
with_contacts({"ada" => ADA}) do |store|
before = store.contact("ada").stored.to_s
digest = store.contact("ada").phones.first.line.digest
post "/contacts/ada", first: "Ada", last: "Lovelace", note: "Countess of Lovelace.",
company: "0", etag: store.contact("ada").etag, new_phone: {"0" => {"value" => ""}},
phone: {digest => {"value" => " +1-555-0100 "}}
assert_equal 303, last_response.status
assert_equal before, store.contact("ada").stored.to_s
end
end
# The revealed row: a value lands as a bare TEL before
# END:VCARD — the served card composes the BDAY in under it — and a
# blank one inserts nothing, inserting absence being a no-op.
def test_an_added_phone_lands_as_a_bare_tel_before_the_end
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
new_phone: {"0" => {"value" => "+1 (555) 020-0700, x9"}}
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
assert_includes card, "TEL:+1 (555) 020-0700\\, x9\r\nBDAY:1985-12-10\r\nEND:VCARD\r\n"
assert_includes card, "TEL;TYPE=mobile:+1-555-0100\r\n"
end
end
def test_a_blank_add_row_inserts_nothing
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
new_phone: {"0" => {"value" => " "}}
assert_equal 303, last_response.status
assert_equal 1, store.contact("ada").phones.length
end
end
# The addresses and emails come from the contact, never the
# request: a doctored digest names no row and touches nothing, where
# a doctored index into a list would have rewritten some other line.
def test_a_digest_the_card_never_had_is_ignored
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "Lovelace", company: "0", etag: store.contact("ada").etag,
phone: {"0" * 64 => {"value" => "+1-555-9999"}},
email: {"1" * 64 => {"value" => "x@example.com"}},
address: {"2" * 64 => {"street" => "nowhere"}}
assert_equal 303, last_response.status
card = store.contact("ada").vcard.to_s
assert_includes card, "TEL;TYPE=mobile:+1-555-0100\r\n"
assert_includes card, "EMAIL;TYPE=home:ada@example.com\r\n"
assert_includes card, "ADR;TYPE=home:;;12 Analytical Way;London;England;NW1 1AA;United Kingdom\r\n"
end
end
# The snapshot guard: a save whose etag names a card the store no
# longer holds is refused with a toast, and nothing is written —
# another tab or a client sync edited the contact in between, and
# the save would have reverted it.
def test_a_save_over_a_stale_snapshot_is_refused
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: "Ada", last: "King", company: "0", etag: '"stale"'
assert_equal 200, last_response.status
assert_includes last_response.body,
'<div role="status" data-fixed><span>This contact changed since the page loaded; nothing was saved.</span></div>'
# The refusal re-renders the screen from the current card.
assert_includes last_response.body, 'value="Ada"'
assert_includes store.contact("ada").vcard.to_s, "FN:Ada Lovelace"
end
end
# The backstop for the one request no browser can send (the first
# field is required): a refused save, nothing written — N and FN are
# mandatory, so a name blank throughout cannot be saved.
def test_a_nameless_edit_is_refused_with_a_toast
with_contacts({"ada" => ADA}) do |store|
post "/contacts/ada", first: " ", last: "", company: "0", etag: store.contact("ada").etag
assert_equal 200, last_response.status
assert_includes last_response.body, '<div role="status" data-fixed><span>A contact needs a name.</span></div>'
assert_includes store.contact("ada").vcard.to_s, "FN:Ada Lovelace"
end
end
def test_saving_an_unknown_contact_is_404
with_contacts({}) { post "/contacts/nope", first: "No", company: "0", etag: '"x"' }
assert_equal 404, last_response.status
end
end