omni-dev 0.41.0

AI-powered git commit rewriter, PR generator, and MCP server for Jira, Confluence, Datadog, Gmail, and Drive.
Documentation
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
//! Drive OAuth2 authentication: authorization-code + PKCE login, credential
//! storage, and in-memory access-token refresh.
//!
//! See [ADR-0069](../../../docs/adrs/adr-0069.md) for the design rationale
//! (applying [ADR-0063](../../../docs/adrs/adr-0063.md), Gmail's OAuth2
//! credential-storage design, to a second Google API). The loopback-listener
//! and browser-launch shape follows `crate::gmail::auth`, itself following
//! the Snowflake client's external-browser SSO flow
//! (`crate::snowflake::client`'s private `auth` module), extended with PKCE
//! (RFC 7636), a `state` nonce, and an `error=` branch — none of which a
//! static-token or SSO-only flow needs.

use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::Path;
use std::process::{Command, Stdio};
use std::time::Duration;

use anyhow::{Context, Result};
use base64::Engine as _;
use chrono::{DateTime, TimeDelta, Utc};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use url::Url;

use crate::drive::account::{self, ResolvedAccount};
use crate::drive::chrome_profile;
use crate::drive::error::{DriveError, GrantContext};
use crate::request_log;
use crate::utils::browser_command::split_browser_command;
use crate::utils::env::SystemEnv;
use crate::utils::secret::Secret;
use crate::utils::settings::{active_profile_from, DriveAccountSettings, DriveSettings, Settings};

/// Environment variable / settings key for the user's Google Cloud OAuth2
/// client id.
pub const DRIVE_CLIENT_ID: &str = "DRIVE_CLIENT_ID";
/// Environment variable / settings key for the user's Google Cloud OAuth2
/// client secret.
pub const DRIVE_CLIENT_SECRET: &str = "DRIVE_CLIENT_SECRET";
/// Environment variable / settings key for the stored OAuth2 refresh token.
pub const DRIVE_REFRESH_TOKEN: &str = "DRIVE_REFRESH_TOKEN";
/// Environment variable / settings key recording the scope granted at login.
pub const DRIVE_SCOPE: &str = "DRIVE_SCOPE";
/// Environment variable overriding the real Drive API host.
///
/// Process-env only — never written to `settings.json` by `auth login`,
/// unlike the four keys above (`crate::drive::client::DriveClient`'s
/// default base URL). Useful for:
/// - Tests that point at a wiremock server (e.g. `http://127.0.0.1:PORT`).
/// - Environments where outbound traffic must go through a forced proxy.
///
/// Mirrors `GMAIL_API_URL` (`crate::gmail::auth`); Drive has no per-tenant
/// site/region the override is *deriving from* — it's a flat replacement of
/// the one real host, not a site substitution.
pub const DRIVE_API_URL: &str = "DRIVE_API_URL";

/// Google's OAuth2 authorization endpoint. Identical to Gmail's — shared
/// Google infrastructure, not a Drive-specific host.
const AUTHORIZATION_ENDPOINT: &str = "https://accounts.google.com/o/oauth2/v2/auth";
/// Google's OAuth2 token endpoint. Identical to Gmail's.
const TOKEN_ENDPOINT: &str = "https://oauth2.googleapis.com/token";
/// The read-only Drive scope — the default.
///
/// The only scope requested before [ADR-0070](../../../docs/adrs/adr-0070.md)
/// reversed [ADR-0069](../../../docs/adrs/adr-0069.md) §2's "no `DriveScope`
/// enum, read-only by design."
pub const SCOPE_READONLY: &str = "https://www.googleapis.com/auth/drive.readonly";
/// Drive's narrowest write scope: `files.update` on `name`/`parents` only
/// (rename/move), no file-content access. Opt-in via `--write`.
pub const SCOPE_METADATA: &str = "https://www.googleapis.com/auth/drive.metadata";

/// How long to wait for the browser sign-in callback before giving up.
const CALLBACK_TIMEOUT: Duration = Duration::from_secs(120);
/// How much slack to leave before an access token's tracked expiry before
/// proactively refreshing it.
const REFRESH_SKEW: TimeDelta = TimeDelta::seconds(60);
/// Upper bound on a trusted `expires_in` from the token endpoint. Comfortably
/// inside what `TimeDelta::seconds` and `DateTime<Utc>` addition can
/// represent without panicking, and far beyond any real OAuth token
/// lifetime — an out-of-range value is clamped rather than trusted, so a
/// misbehaving or malicious token endpoint can't crash the process (#1531).
const MAX_EXPIRES_IN_SECONDS: i64 = 100 * 365 * 24 * 60 * 60;

/// The Drive OAuth2 scope granted at login.
///
/// Unlike Gmail's readonly/modify split, `Metadata` is requested
/// *additively* over `ReadOnly`, never as a replacement: `drive.metadata`
/// alone grants no file-content access, so read commands (`search`, `read`,
/// export/download) still need `drive.readonly` too. The authorization
/// request for `Metadata` therefore asks for both scopes together — see
/// [`build_authorization_url`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DriveScope {
    /// List/read/export/download files and metadata. Default; never
    /// requests any mutation.
    #[default]
    ReadOnly,
    /// Everything [`ReadOnly`](Self::ReadOnly) grants, plus `drive.metadata`
    /// (`files.update` on `name`/`parents` — rename/move).
    Metadata,
}

impl DriveScope {
    /// Returns the wire scope string Google expects for status-reporting /
    /// storage purposes. For [`Metadata`](Self::Metadata) this is just
    /// [`SCOPE_METADATA`] — the *additive* `drive.readonly` request shape
    /// lives in [`build_authorization_url`] alone, since Google's granted-
    /// scope response and [`from_granted`](Self::from_granted) already
    /// round-trip correctly off the single [`SCOPE_METADATA`] token.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::ReadOnly => SCOPE_READONLY,
            Self::Metadata => SCOPE_METADATA,
        }
    }

    /// Parses Google's space-separated granted-scope response, treating the
    /// presence of the metadata scope anywhere in it as
    /// [`Metadata`](Self::Metadata) and the readonly scope (with no
    /// metadata) as [`ReadOnly`](Self::ReadOnly).
    ///
    /// Returns `None` when neither Drive scope is present — e.g. the user
    /// left the Drive permission unticked on Google's consent screen — so
    /// callers can reject the grant instead of silently defaulting to
    /// [`ReadOnly`](Self::ReadOnly).
    #[must_use]
    pub fn from_granted(granted: &str) -> Option<Self> {
        let tokens: Vec<&str> = granted.split_whitespace().collect();
        if tokens.contains(&SCOPE_METADATA) {
            Some(Self::Metadata)
        } else if tokens.contains(&SCOPE_READONLY) {
            Some(Self::ReadOnly)
        } else {
            None
        }
    }

    /// Whether this scope allows mutating calls (`files.update` on
    /// `name`/`parents` — rename/move).
    #[must_use]
    pub fn allows_write(self) -> bool {
        matches!(self, Self::Metadata)
    }
}

/// Drive OAuth2 credentials.
#[derive(Debug, Clone)]
pub struct DriveCredentials {
    /// OAuth2 client id (not secret — visible in the browser's own network
    /// traffic during login regardless).
    pub client_id: String,
    /// OAuth2 client secret (redacted in `Debug` output).
    pub client_secret: Secret,
    /// The stored refresh token (redacted in `Debug` output).
    pub refresh_token: Secret,
    /// The scope granted at the login that produced this refresh token.
    pub scope: DriveScope,
}

/// Secret-free presence/scope report, safe to serialise (e.g. over MCP).
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct DriveAuthStatus {
    /// Whether [`DRIVE_CLIENT_ID`] is present.
    pub has_client_id: bool,
    /// Whether [`DRIVE_CLIENT_SECRET`] is present.
    pub has_client_secret: bool,
    /// Whether [`DRIVE_REFRESH_TOKEN`] is present.
    pub has_refresh_token: bool,
    /// The granted scope, if recorded. `None` when unset.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope: Option<String>,
}

/// Resolves the active Drive account for this call (mirrors Gmail's issue
/// #1500), folding an explicit per-call override together with the ambient
/// `--account`/[`account::DRIVE_ACCOUNT_ENV`] value. The one seam every
/// credential CRUD entry point in this module routes through.
pub(crate) fn resolve(drive: &DriveSettings, explicit: Option<&str>) -> Result<ResolvedAccount> {
    let explicit = fold_explicit(explicit);
    account::resolve_account(&SystemEnv, drive, explicit.as_deref())
}

/// Like [`resolve`], but for account-creating writes (`drive auth login`,
/// `drive auth import`) — see [`account::resolve_account_for_write`] for why
/// an explicit target need not already exist.
pub(crate) fn resolve_for_write(
    drive: &DriveSettings,
    explicit: Option<&str>,
) -> Result<ResolvedAccount> {
    let explicit = fold_explicit(explicit);
    account::resolve_account_for_write(&SystemEnv, drive, explicit.as_deref())
}

/// Folds an explicit per-call account override together with the ambient
/// `--account`/[`account::DRIVE_ACCOUNT_ENV`] value — shared by [`resolve`]
/// and [`resolve_for_write`].
fn fold_explicit(explicit: Option<&str>) -> Option<String> {
    explicit
        .map(str::to_string)
        .or_else(|| account::active_drive_account_from(&SystemEnv))
}

/// Resolves the [`BrowserConfig`] `drive auth login` should open the
/// authorization URL with, honoring a named account's manual
/// `browser_command` override and opt-in automatic Chrome-profile
/// resolution (mirrors Gmail's issue #1505). `explicit` is folded exactly
/// like [`resolve_for_write`]'s. A [`ResolvedAccount::Unconfigured`] account
/// (no named accounts configured, or a literal credential env set) always
/// yields [`BrowserLaunch::Auto`].
pub(crate) fn resolve_browser_config_for(
    drive: &DriveSettings,
    explicit: Option<&str>,
) -> Result<BrowserConfig> {
    match resolve_for_write(drive, explicit)? {
        ResolvedAccount::Unconfigured => Ok(BrowserConfig::default()),
        ResolvedAccount::Named(name) => build_browser_config(
            drive.accounts.get(&name),
            chrome_profile::resolve_launch_command,
        ),
    }
}

/// The pure/injectable core of [`resolve_browser_config_for`] —
/// `resolve_chrome_profile` is [`chrome_profile::resolve_launch_command`] in
/// production, a stub in tests, so this stays testable without touching a
/// real Chrome install.
///
/// Precedence:
/// 1. `account.browser_command` set (non-blank) → used verbatim; a
///    malformed command is a hard error.
/// 2. `account.chrome_profile_from_email` set *and* `account.email_address`
///    set → automatic resolution; any resolution failure (per
///    `resolve_chrome_profile`'s fail-open contract) falls back to `Auto`.
/// 3. Otherwise → `Auto`.
fn build_browser_config(
    account: Option<&DriveAccountSettings>,
    resolve_chrome_profile: impl FnOnce(&str) -> Option<Vec<String>>,
) -> Result<BrowserConfig> {
    let Some(account) = account else {
        return Ok(BrowserConfig::default());
    };

    if let Some(command) = account
        .browser_command
        .as_deref()
        .map(str::trim)
        .filter(|command| !command.is_empty())
    {
        return Ok(BrowserConfig {
            launch: BrowserLaunch::Command(split_browser_command("browser_command", command)?),
            ..BrowserConfig::default()
        });
    }

    if account.chrome_profile_from_email {
        if let Some(email) = account.email_address.as_deref() {
            if let Some(args) = resolve_chrome_profile(email) {
                return Ok(BrowserConfig {
                    launch: BrowserLaunch::Command(args),
                    ..BrowserConfig::default()
                });
            }
        } else {
            tracing::info!(
                "chrome_profile_from_email is set but email_address is not; \
                 falling back to the default browser"
            );
        }
    }

    Ok(BrowserConfig::default())
}

/// Loads Drive credentials from environment variables or settings.json.
///
/// Environment variables take precedence over the settings file.
pub fn load_credentials() -> Result<DriveCredentials> {
    load_credentials_with(&crate::utils::settings::SettingsEnv::load())
}

/// [`load_credentials`], but honoring the named-account resolution (mirrors
/// Gmail's issue #1500). `explicit` is the already-resolved `--account`/
/// [`account::DRIVE_ACCOUNT_ENV`] override, if any (`None` still resolves
/// the ambient env var — see [`resolve`]). Falls through to
/// [`load_credentials_with`]'s exact behavior when no named account applies
/// — an empty `drive.accounts` map or the literal-env bypass both resolve
/// to [`ResolvedAccount::Unconfigured`], and [`load_credentials_with`]
/// naturally fails loudly with [`DriveError::CredentialsNotFound`] (whose
/// message names `drive auth login`) when there is nothing to load.
pub(crate) fn load_credentials_for(explicit: Option<&str>) -> Result<DriveCredentials> {
    let settings = Settings::load().unwrap_or_default();
    match resolve(&settings.drive, explicit)? {
        ResolvedAccount::Unconfigured => {
            let profile = active_profile_from(&SystemEnv);
            load_credentials_with(&crate::utils::settings::SettingsEnv::from_settings(
                settings,
                profile.as_deref(),
            ))
        }
        ResolvedAccount::Named(name) => load_named_credentials(&settings.drive, &name),
    }
}

/// Reads `drive.accounts.<name>` into [`DriveCredentials`], wrapping
/// `client_secret`/`refresh_token` into [`Secret`] immediately, mirroring
/// [`load_credentials_with`].
fn load_named_credentials(drive: &DriveSettings, name: &str) -> Result<DriveCredentials> {
    let account = drive
        .accounts
        .get(name)
        .ok_or(DriveError::CredentialsNotFound)?;
    let client_id = account
        .client_id
        .clone()
        .ok_or(DriveError::CredentialsNotFound)?;
    let client_secret = account
        .client_secret
        .clone()
        .ok_or(DriveError::CredentialsNotFound)?;
    let refresh_token = account
        .refresh_token
        .clone()
        .ok_or(DriveError::CredentialsNotFound)?;
    let scope = account
        .scope
        .as_deref()
        .and_then(DriveScope::from_granted)
        .unwrap_or_default();

    Ok(DriveCredentials {
        client_id,
        client_secret: client_secret.into(),
        refresh_token: refresh_token.into(),
        scope,
    })
}

/// [`load_credentials`] over an injected
/// [`EnvSource`](crate::utils::env::EnvSource).
///
/// Tests pass a pure `MapEnv` so credential resolution is exercised without
/// mutating the process environment (issue #1030 / STYLE-0028).
pub(crate) fn load_credentials_with(
    env: &impl crate::utils::env::EnvSource,
) -> Result<DriveCredentials> {
    let client_id = env
        .var(DRIVE_CLIENT_ID)
        .ok_or(DriveError::CredentialsNotFound)?;
    let client_secret = env
        .var(DRIVE_CLIENT_SECRET)
        .ok_or(DriveError::CredentialsNotFound)?;
    let refresh_token = env
        .var(DRIVE_REFRESH_TOKEN)
        .ok_or(DriveError::CredentialsNotFound)?;
    // Unlike login (which rejects an unparseable grant outright), a stored
    // scope that no longer parses degrades to ReadOnly rather than erroring:
    // it was already validated when written, and failing closed here is
    // safe (a stale Metadata silently becoming ReadOnly still fails at the
    // API, not open).
    let scope = env
        .var(DRIVE_SCOPE)
        .and_then(|s| DriveScope::from_granted(&s))
        .unwrap_or_default();

    Ok(DriveCredentials {
        client_id,
        client_secret: client_secret.into(),
        refresh_token: refresh_token.into(),
        scope,
    })
}

/// Builds a [`DriveAuthStatus`] from the current settings / environment.
///
/// Reports credential presence without leaking any secret values. Safe to
/// call with no credentials configured.
pub fn status() -> DriveAuthStatus {
    status_with(&crate::utils::settings::SettingsEnv::load())
}

/// [`status`] over an injected [`EnvSource`](crate::utils::env::EnvSource).
pub(crate) fn status_with(env: &impl crate::utils::env::EnvSource) -> DriveAuthStatus {
    DriveAuthStatus {
        has_client_id: env.var(DRIVE_CLIENT_ID).is_some(),
        has_client_secret: env.var(DRIVE_CLIENT_SECRET).is_some(),
        has_refresh_token: env.var(DRIVE_REFRESH_TOKEN).is_some(),
        scope: env.var(DRIVE_SCOPE),
    }
}

/// [`status`], but honoring the named-account resolution (mirrors Gmail's
/// issue #1500). `explicit` is the already-resolved `--account`/
/// [`account::DRIVE_ACCOUNT_ENV`] override, if any. Unlike [`status`], this
/// can fail — once named accounts exist, resolution itself can (e.g. an
/// unknown or ambiguous account) — so callers that want [`status`]'s
/// never-fails presence report keep calling that instead.
///
/// Only compiled with the `mcp` feature — the MCP `drive_auth_status` tool
/// is its sole consumer; the CLI's `drive auth status` goes through
/// [`load_credentials_for`] instead.
#[cfg(feature = "mcp")]
pub(crate) fn status_for(explicit: Option<&str>) -> Result<DriveAuthStatus> {
    let settings = Settings::load().unwrap_or_default();
    match resolve(&settings.drive, explicit)? {
        ResolvedAccount::Unconfigured => {
            let profile = active_profile_from(&SystemEnv);
            Ok(status_with(
                &crate::utils::settings::SettingsEnv::from_settings(settings, profile.as_deref()),
            ))
        }
        ResolvedAccount::Named(name) => Ok(status_from_named(&settings.drive, &name)),
    }
}

/// Builds a [`DriveAuthStatus`] from `drive.accounts.<name>`'s presence
/// flags — the named-account counterpart of [`status_with`].
///
/// Only compiled with the `mcp` feature — see [`status_for`], its sole
/// caller.
#[cfg(feature = "mcp")]
fn status_from_named(drive: &DriveSettings, name: &str) -> DriveAuthStatus {
    let account = drive.accounts.get(name);
    DriveAuthStatus {
        has_client_id: account.is_some_and(|a| a.client_id.is_some()),
        has_client_secret: account.is_some_and(|a| a.client_secret.is_some()),
        has_refresh_token: account.is_some_and(|a| a.refresh_token.is_some()),
        scope: account.and_then(|a| a.scope.clone()),
    }
}

/// Opportunistic `email_address` backfill for `name`, populated by `drive
/// auth status --all` after a successful live API call. Never used for
/// authentication, never written by `login`/`import`. A no-op when `name`
/// already has an `email_address` — an explicit or previously-backfilled
/// value is never overwritten (mirrors Gmail's issue #1505).
pub(crate) fn record_account_email(name: &str, email: &str) -> Result<()> {
    let settings = Settings::load().unwrap_or_default();
    if settings
        .drive
        .accounts
        .get(name)
        .is_some_and(|account| account.email_address.is_some())
    {
        return Ok(());
    }
    Settings::upsert_drive_account(
        &Settings::get_settings_path()?,
        name,
        &[(
            "email_address",
            serde_json::Value::String(email.to_string()),
        )],
    )
}

/// Saves Drive credentials to `~/.omni-dev/settings.json`.
///
/// Merges the four credential keys into the active profile's `env` map (the
/// base `env` when no profile is active), preserving all other settings.
pub fn save_credentials(credentials: &DriveCredentials) -> Result<()> {
    save_credentials_to(
        &Settings::get_settings_path()?,
        active_profile_from(&SystemEnv).as_deref(),
        credentials,
    )
}

/// [`save_credentials`], writing to an explicit settings-file path and env
/// map (`profiles.<name>.env` when `profile` is `Some`, base `env` otherwise).
pub(crate) fn save_credentials_to(
    settings_path: &Path,
    profile: Option<&str>,
    credentials: &DriveCredentials,
) -> Result<()> {
    Settings::upsert_env_vars_in(
        settings_path,
        profile,
        &[
            (DRIVE_CLIENT_ID, credentials.client_id.as_str()),
            (
                DRIVE_CLIENT_SECRET,
                credentials.client_secret.expose_secret(),
            ),
            (
                DRIVE_REFRESH_TOKEN,
                credentials.refresh_token.expose_secret(),
            ),
            (DRIVE_SCOPE, credentials.scope.as_str()),
        ],
    )
}

/// The `drive.accounts.<name>` field names/values for `credentials` — the
/// named-account counterpart of the flat `DRIVE_*` env keys
/// [`save_credentials_to`] writes.
fn named_account_vars(credentials: &DriveCredentials) -> [(&str, serde_json::Value); 4] {
    [
        (
            "client_id",
            serde_json::Value::String(credentials.client_id.clone()),
        ),
        (
            "client_secret",
            serde_json::Value::String(credentials.client_secret.expose_secret().to_string()),
        ),
        (
            "refresh_token",
            serde_json::Value::String(credentials.refresh_token.expose_secret().to_string()),
        ),
        (
            "scope",
            serde_json::Value::String(credentials.scope.as_str().to_string()),
        ),
    ]
}

/// Removes Drive credential keys from `~/.omni-dev/settings.json` — this
/// *is* `drive auth logout`.
///
/// Returns `true` if any Drive key was present and removed, `false`
/// otherwise.
pub fn remove_credentials() -> Result<bool> {
    remove_credentials_at(
        &Settings::get_settings_path()?,
        active_profile_from(&SystemEnv).as_deref(),
    )
}

/// [`remove_credentials`], operating on an explicit settings-file path and
/// env map.
pub(crate) fn remove_credentials_at(settings_path: &Path, profile: Option<&str>) -> Result<bool> {
    Settings::remove_env_vars_in(
        settings_path,
        profile,
        &[
            DRIVE_CLIENT_ID,
            DRIVE_CLIENT_SECRET,
            DRIVE_REFRESH_TOKEN,
            DRIVE_SCOPE,
        ],
    )
}

/// [`remove_credentials`], but honoring the named-account resolution
/// (mirrors Gmail's issue #1500). `explicit` is the already-resolved
/// `--account`/[`account::DRIVE_ACCOUNT_ENV`] override, if any. Removes the
/// whole `drive.accounts.<name>` entry — an account is coherent as a unit.
pub(crate) fn remove_credentials_for(explicit: Option<&str>) -> Result<bool> {
    let settings = Settings::load().unwrap_or_default();
    match resolve(&settings.drive, explicit)? {
        ResolvedAccount::Unconfigured => remove_credentials_at(
            &Settings::get_settings_path()?,
            active_profile_from(&SystemEnv).as_deref(),
        ),
        ResolvedAccount::Named(name) => {
            Settings::remove_drive_account(&Settings::get_settings_path()?, &name)
        }
    }
}

// ── Browser launch ──────────────────────────────────────────────────────

/// How to open the authorization URL during login.
///
/// Deliberately duplicated from (not shared with) `crate::gmail::auth`'s
/// identical type (itself duplicated from
/// [`crate::snowflake::client::config::BrowserLaunch`]) — a small, stable
/// shape with no existing "generic browser launch" module to promote into;
/// extract only on a third consumer (see
/// [ADR-0069](../../../docs/adrs/adr-0069.md) §4).
#[derive(Clone, Debug, Default)]
pub enum BrowserLaunch {
    /// Open with the OS default handler (`open` / `xdg-open` / `start`).
    #[default]
    Auto,
    /// Run a custom command; `{url}` (or a trailing arg) receives the
    /// authorization URL. Use this to target a specific Chrome profile,
    /// e.g. `Google Chrome --profile-directory=Profile 1 --new-window {url}`.
    Command(Vec<String>),
    /// Do not open a browser; the authorization URL is logged for manual
    /// opening.
    Manual,
}

/// Loopback OAuth2 callback settings.
#[derive(Clone, Debug)]
pub struct BrowserConfig {
    /// How to open the authorization URL.
    pub launch: BrowserLaunch,
    /// Bind address for the loopback callback listener.
    pub callback_addr: IpAddr,
    /// Bind port for the callback listener (`0` = OS-assigned ephemeral port).
    pub callback_port: u16,
}

impl Default for BrowserConfig {
    fn default() -> Self {
        Self {
            launch: BrowserLaunch::Auto,
            callback_addr: IpAddr::V4(Ipv4Addr::LOCALHOST),
            callback_port: 0,
        }
    }
}

/// Opens `url` in the configured browser.
// `{url}` is a literal command placeholder we substitute, not a format string.
#[allow(clippy::literal_string_with_formatting_args)]
fn open_browser(launch: &BrowserLaunch, url: &str) -> Result<()> {
    match launch {
        BrowserLaunch::Manual => {
            tracing::info!("Open this URL in a browser to sign in to Drive:\n{url}");
            Ok(())
        }
        BrowserLaunch::Command(args) => {
            let mut parts = args.iter();
            let program = parts
                .next()
                .ok_or_else(|| DriveError::InvalidBrowserCommand("empty browser command".into()))?;
            let mut command = Command::new(program);
            let mut placed = false;
            for arg in parts {
                if arg.contains("{url}") {
                    command.arg(arg.replace("{url}", url));
                    placed = true;
                } else {
                    command.arg(arg);
                }
            }
            if !placed {
                command.arg(url);
            }
            spawn_detached(command)
        }
        BrowserLaunch::Auto => {
            let program = if cfg!(target_os = "macos") {
                "open"
            } else if cfg!(target_os = "windows") {
                "explorer"
            } else {
                "xdg-open"
            };
            let mut command = Command::new(program);
            command.arg(url);
            spawn_detached(command)
        }
    }
}

/// Spawns a browser command detached from this process's stdio.
fn spawn_detached(mut command: Command) -> Result<()> {
    command
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .map(|_| ())
        .context("Failed to launch the browser")
}

// ── PKCE + state ────────────────────────────────────────────────────────

/// A pending login's PKCE verifier and CSRF `state` nonce, generated fresh
/// per login attempt and never persisted.
struct PendingLogin {
    state: String,
    code_verifier: String,
}

fn generate_pending_login() -> PendingLogin {
    PendingLogin {
        state: crate::browser::auth::generate_token(),
        code_verifier: crate::browser::auth::generate_token(),
    }
}

/// Derives the PKCE `code_challenge` (RFC 7636, `S256` method) from a
/// `code_verifier`.
fn code_challenge(code_verifier: &str) -> String {
    let digest = Sha256::digest(code_verifier.as_bytes());
    base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(digest)
}

fn build_authorization_url(
    client_id: &str,
    redirect_uri: &str,
    scope: DriveScope,
    state: &str,
    code_challenge: &str,
) -> Result<Url> {
    let mut url =
        Url::parse(AUTHORIZATION_ENDPOINT).context("Invalid Drive authorization endpoint")?;
    // Metadata is requested *additively* over ReadOnly — see DriveScope's
    // doc comment for why a single `scope.as_str()` isn't enough here.
    let requested_scope = match scope {
        DriveScope::ReadOnly => SCOPE_READONLY.to_string(),
        DriveScope::Metadata => format!("{SCOPE_READONLY} {SCOPE_METADATA}"),
    };
    url.query_pairs_mut()
        .append_pair("client_id", client_id)
        .append_pair("redirect_uri", redirect_uri)
        .append_pair("response_type", "code")
        .append_pair("scope", &requested_scope)
        .append_pair("state", state)
        .append_pair("code_challenge", code_challenge)
        .append_pair("code_challenge_method", "S256")
        .append_pair("access_type", "offline")
        // Without forcing re-consent, Google may not re-issue a refresh
        // token on a second login — which would silently break re-auth
        // after the 7-day testing-mode refresh-token expiry.
        .append_pair("prompt", "consent");
    Ok(url)
}

// ── Loopback callback capture ───────────────────────────────────────────

/// The parsed loopback callback: either `code`+`state`, or an `error`
/// (optionally with `error_description`).
#[derive(Debug)]
pub(crate) struct CallbackResult {
    code: Option<String>,
    state: Option<String>,
    error: Option<String>,
    error_description: Option<String>,
}

/// Binds the loopback callback listener, returning it along with the
/// OS-assigned port so the authorization URL's `redirect_uri` can be built
/// before the browser is opened.
pub(crate) async fn bind_callback_listener(browser: &BrowserConfig) -> Result<(TcpListener, u16)> {
    let listener = TcpListener::bind(SocketAddr::new(
        browser.callback_addr,
        browser.callback_port,
    ))
    .await
    .context("Failed to start the local OAuth callback listener")?;
    let port = listener
        .local_addr()
        .context("Failed to read the callback listener's port")?
        .port();
    Ok((listener, port))
}

/// Waits for the browser's callback connection using the default
/// [`CALLBACK_TIMEOUT`].
pub(crate) async fn wait_for_callback(listener: TcpListener) -> Result<CallbackResult> {
    wait_for_callback_with_timeout(listener, CALLBACK_TIMEOUT).await
}

/// Accepts one loopback connection and extracts the OAuth callback's query
/// parameters from the redirected `GET` request line.
///
/// Never logs the raw request or query string — only that a callback was
/// received — so the authorization `code` can never reach the request log
/// via this path (see ADR-0063's redaction discussion).
pub(crate) async fn wait_for_callback_with_timeout(
    listener: TcpListener,
    timeout: Duration,
) -> Result<CallbackResult> {
    let (mut stream, _addr) = tokio::time::timeout(timeout, listener.accept())
        .await
        .map_err(|_| DriveError::CallbackTimeout(timeout.as_secs()))?
        .context("Failed to accept the browser's callback connection")?;

    let mut buf = vec![0u8; 8192];
    let n = stream
        .read(&mut buf)
        .await
        .context("Failed to read the callback request")?;
    let request = String::from_utf8_lossy(&buf[..n]);

    let result = parse_callback(&request).ok_or(DriveError::MalformedCallback)?;
    tracing::info!("Drive OAuth callback received");

    let body = if result.error.is_some() {
        "<html><body>Sign-in failed. You can close this tab and check the terminal.</body></html>"
    } else {
        "<html><body>Drive sign-in complete. You can close this tab.</body></html>"
    };
    let response =
        format!("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n{body}");
    let _ = stream.write_all(response.as_bytes()).await;
    let _ = stream.flush().await;

    Ok(result)
}

/// Extracts `code`/`state`/`error`/`error_description` from an HTTP
/// request's first line only — headers and body are never inspected.
fn parse_callback(request: &str) -> Option<CallbackResult> {
    let first_line = request.lines().next()?;
    let path = first_line.split_whitespace().nth(1)?; // "/?code=…&state=…"
    let query = path.split_once('?')?.1;

    let mut result = CallbackResult {
        code: None,
        state: None,
        error: None,
        error_description: None,
    };
    for (key, value) in url::form_urlencoded::parse(query.as_bytes()) {
        match key.as_ref() {
            "code" => result.code = Some(value.into_owned()),
            "state" => result.state = Some(value.into_owned()),
            "error" => result.error = Some(value.into_owned()),
            "error_description" => result.error_description = Some(value.into_owned()),
            _ => {}
        }
    }
    Some(result)
}

// ── Token exchange / refresh ────────────────────────────────────────────

#[derive(Debug, Deserialize)]
struct TokenResponse {
    access_token: String,
    #[serde(default)]
    refresh_token: Option<String>,
    expires_in: i64,
    #[serde(default)]
    scope: Option<String>,
}

#[derive(Debug, Deserialize)]
struct TokenErrorResponse {
    error: String,
    #[serde(default)]
    error_description: Option<String>,
}

async fn exchange_code_for_tokens(
    http: &reqwest::Client,
    token_endpoint: &str,
    client_id: &str,
    client_secret: &str,
    code: &str,
    code_verifier: &str,
    redirect_uri: &str,
) -> Result<TokenResponse> {
    let params = [
        ("grant_type", "authorization_code"),
        ("code", code),
        ("client_id", client_id),
        ("client_secret", client_secret),
        ("redirect_uri", redirect_uri),
        ("code_verifier", code_verifier),
    ];
    post_token_request(http, token_endpoint, &params, GrantContext::CodeExchange).await
}

async fn refresh_access_token(
    http: &reqwest::Client,
    token_endpoint: &str,
    client_id: &str,
    client_secret: &str,
    refresh_token: &str,
) -> Result<TokenResponse> {
    let params = [
        ("grant_type", "refresh_token"),
        ("refresh_token", refresh_token),
        ("client_id", client_id),
        ("client_secret", client_secret),
    ];
    post_token_request(http, token_endpoint, &params, GrantContext::Refresh).await
}

/// POSTs a token request. All secrets travel in the `.form(...)` body, never
/// the URL — `token_endpoint` carries no query string, so the request-log's
/// URL redaction has nothing to redact and nothing to miss either.
async fn post_token_request(
    http: &reqwest::Client,
    token_endpoint: &str,
    params: &[(&str, &str)],
    context: GrantContext,
) -> Result<TokenResponse> {
    let started = std::time::Instant::now();
    let result = http.post(token_endpoint).form(params).send().await;
    request_log::record_http_result("drive", "POST", token_endpoint, started, &result);
    let response = result.context("Failed to send token request to Google")?;

    if !response.status().is_success() {
        let body = response.text().await.unwrap_or_default();
        if let Ok(err) = serde_json::from_str::<TokenErrorResponse>(&body) {
            if err.error == "invalid_grant" {
                return Err(DriveError::InvalidGrant(context).into());
            }
            return Err(anyhow::anyhow!(
                "Google token endpoint rejected the request: {} ({})",
                err.error,
                err.error_description.unwrap_or_default()
            ));
        }
        return Err(anyhow::anyhow!(
            "Google token endpoint returned an unparsable error body: {body}"
        ));
    }

    response
        .json::<TokenResponse>()
        .await
        .context("Failed to parse Google's token response")
}

// ── Session (in-memory access-token lifecycle) ──────────────────────────

/// The mutable access-token state, refreshed by [`DriveSession::refresh_locked`].
struct TokenState {
    access_token: Secret,
    expires_at: DateTime<Utc>,
}

/// A live Drive OAuth2 session: holds the refresh token and the current
/// in-memory access token, refreshing on demand.
///
/// Uses [`tokio::sync::Mutex`] (not `std::sync::Mutex`) held *across* the
/// refresh network call — mirrors `crate::gmail::auth::GmailSession`'s
/// explicit single-flight-refresh design (issue #1465's "concurrent callers
/// don't stampede" requirement): a second concurrent caller blocks on this
/// mutex and, once unblocked, observes the already-refreshed token instead
/// of issuing a second POST.
pub struct DriveSession {
    http: reqwest::Client,
    client_id: String,
    client_secret: Secret,
    refresh_token: Secret,
    token_endpoint: String,
    state: tokio::sync::Mutex<TokenState>,
}

impl DriveSession {
    /// Creates a session against Google's real token endpoint.
    pub(crate) fn new(http: reqwest::Client, credentials: &DriveCredentials) -> Self {
        Self::new_with_token_endpoint(http, credentials, TOKEN_ENDPOINT)
    }

    /// [`new`](Self::new) against an explicit token endpoint — the test seam
    /// for pointing at a wiremock server.
    pub(crate) fn new_with_token_endpoint(
        http: reqwest::Client,
        credentials: &DriveCredentials,
        token_endpoint: &str,
    ) -> Self {
        Self {
            http,
            client_id: credentials.client_id.clone(),
            client_secret: credentials.client_secret.clone(),
            refresh_token: credentials.refresh_token.clone(),
            token_endpoint: token_endpoint.to_string(),
            state: tokio::sync::Mutex::new(TokenState {
                access_token: Secret::new(""),
                // No access token is ever persisted (ADR-0063 Decision 2), so
                // every fresh session starts expired and refreshes on its
                // very first call.
                expires_at: DateTime::<Utc>::MIN_UTC,
            }),
        }
    }

    /// Returns a valid access token, refreshing proactively when the
    /// tracked expiry is within [`REFRESH_SKEW`].
    pub(crate) async fn access_token(&self) -> Result<Secret> {
        let mut state = self.state.lock().await;
        if Utc::now() + REFRESH_SKEW >= state.expires_at {
            self.refresh_locked(&mut state).await?;
        }
        Ok(state.access_token.clone())
    }

    /// Forces a refresh, but only if `observed` is still the current token —
    /// i.e. no other caller already refreshed while this caller was waiting
    /// on the lock. Used as the reactive safety net after an HTTP 401 (clock
    /// skew, or server-side revocation the proactive check can't see).
    pub(crate) async fn force_refresh(&self, observed: &Secret) -> Result<Secret> {
        let mut state = self.state.lock().await;
        if state.access_token != *observed {
            return Ok(state.access_token.clone());
        }
        self.refresh_locked(&mut state).await?;
        Ok(state.access_token.clone())
    }

    async fn refresh_locked(&self, state: &mut TokenState) -> Result<()> {
        let response = refresh_access_token(
            &self.http,
            &self.token_endpoint,
            &self.client_id,
            self.client_secret.expose_secret(),
            self.refresh_token.expose_secret(),
        )
        .await?;
        state.access_token = response.access_token.into();
        let expires_in = response.expires_in.clamp(0, MAX_EXPIRES_IN_SECONDS);
        state.expires_at = Utc::now() + TimeDelta::seconds(expires_in);
        Ok(())
    }
}

// ── Login orchestration ─────────────────────────────────────────────────

/// Runs the OAuth2 authorization-code + PKCE login flow, persisting the
/// resulting refresh token to `~/.omni-dev/settings.json`.
pub async fn login(
    client_id: &str,
    client_secret: &Secret,
    scope: DriveScope,
    browser: &BrowserConfig,
) -> Result<DriveAuthStatus> {
    login_to(
        &Settings::get_settings_path()?,
        active_profile_from(&SystemEnv).as_deref(),
        client_id,
        client_secret,
        scope,
        browser,
        TOKEN_ENDPOINT,
    )
    .await
}

/// [`login`], writing to an explicit settings-file path/profile and against
/// an explicit token endpoint — the test seam for a wiremock server.
pub(crate) async fn login_to(
    settings_path: &Path,
    profile: Option<&str>,
    client_id: &str,
    client_secret: &Secret,
    scope: DriveScope,
    browser: &BrowserConfig,
    token_endpoint: &str,
) -> Result<DriveAuthStatus> {
    let credentials =
        run_login_flow(client_id, client_secret, scope, browser, token_endpoint).await?;
    save_credentials_to(settings_path, profile, &credentials)?;
    Ok(status_from_credentials(&credentials))
}

/// [`login`], but honoring the named-account resolution (mirrors Gmail's
/// issue #1500): runs the same OAuth2 flow, then persists to
/// `drive.accounts.<name>` when a named account is active instead of the
/// legacy `env`/profile map. `explicit` is the already-resolved
/// `--account`/[`account::DRIVE_ACCOUNT_ENV`] override, if any — resolved
/// via [`resolve_for_write`], so an explicit name need not already be
/// configured (this is how a new account is created).
pub(crate) async fn login_for(
    explicit: Option<&str>,
    client_id: &str,
    client_secret: &Secret,
    scope: DriveScope,
    browser: &BrowserConfig,
) -> Result<DriveAuthStatus> {
    let settings = Settings::load().unwrap_or_default();
    match resolve_for_write(&settings.drive, explicit)? {
        ResolvedAccount::Unconfigured => {
            login_to(
                &Settings::get_settings_path()?,
                active_profile_from(&SystemEnv).as_deref(),
                client_id,
                client_secret,
                scope,
                browser,
                TOKEN_ENDPOINT,
            )
            .await
        }
        ResolvedAccount::Named(name) => {
            let credentials =
                run_login_flow(client_id, client_secret, scope, browser, TOKEN_ENDPOINT).await?;
            Settings::upsert_drive_account(
                &Settings::get_settings_path()?,
                &name,
                &named_account_vars(&credentials),
            )?;
            Ok(status_from_credentials(&credentials))
        }
    }
}

/// Runs the OAuth2 authorization-code + PKCE flow against `token_endpoint`
/// and returns the resulting credentials, without persisting them — the
/// shared core both [`login_to`] (legacy path) and [`login_for`]'s Named
/// branch build on.
async fn run_login_flow(
    client_id: &str,
    client_secret: &Secret,
    scope: DriveScope,
    browser: &BrowserConfig,
    token_endpoint: &str,
) -> Result<DriveCredentials> {
    let (listener, port) = bind_callback_listener(browser).await?;
    let redirect_uri = format!("http://127.0.0.1:{port}");

    let pending = generate_pending_login();
    let challenge = code_challenge(&pending.code_verifier);
    let auth_url =
        build_authorization_url(client_id, &redirect_uri, scope, &pending.state, &challenge)?;
    open_browser(&browser.launch, auth_url.as_str())?;

    let callback = wait_for_callback(listener).await?;
    if let Some(error) = callback.error {
        return Err(DriveError::authorization_denied(
            &error,
            callback.error_description.as_deref(),
        )
        .into());
    }
    let (Some(code), Some(returned_state)) = (callback.code, callback.state) else {
        return Err(DriveError::MalformedCallback.into());
    };
    // Plain equality, not constant-time: `state` is a CSRF nonce carried in
    // a browser-visible URL, not a secret — there's nothing for a timing
    // side-channel to extract here (unlike `constant_time_eq`'s real use
    // guarding a bridge auth token in `src/browser/auth.rs`).
    if returned_state != pending.state {
        return Err(DriveError::StateMismatch.into());
    }

    let http = reqwest::Client::builder()
        .connect_timeout(crate::utils::http::connect_timeout())
        .read_timeout(crate::utils::http::read_timeout())
        .build()
        .context("Failed to build HTTP client")?;
    let tokens = exchange_code_for_tokens(
        &http,
        token_endpoint,
        client_id,
        client_secret.expose_secret(),
        &code,
        &pending.code_verifier,
        &redirect_uri,
    )
    .await?;
    let refresh_token = tokens
        .refresh_token
        .ok_or(DriveError::MalformedTokenResponse("refresh_token"))?;
    let granted_raw = tokens.scope.unwrap_or_default();
    let granted_scope = DriveScope::from_granted(&granted_raw).ok_or_else(|| {
        let received = if granted_raw.trim().is_empty() {
            "none".to_string()
        } else {
            granted_raw
                .split_whitespace()
                .collect::<Vec<_>>()
                .join(", ")
        };
        DriveError::NoScopeGranted(received)
    })?;

    Ok(DriveCredentials {
        client_id: client_id.to_string(),
        client_secret: client_secret.clone(),
        refresh_token: refresh_token.into(),
        scope: granted_scope,
    })
}

/// Builds the "just authenticated" [`DriveAuthStatus`] from freshly-obtained
/// `credentials` (all fields present by construction).
fn status_from_credentials(credentials: &DriveCredentials) -> DriveAuthStatus {
    DriveAuthStatus {
        has_client_id: true,
        has_client_secret: true,
        has_refresh_token: true,
        scope: Some(credentials.scope.as_str().to_string()),
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use std::fs;
    use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
    use std::sync::Arc;

    use super::*;

    // ── Pure helpers ─────────────────────────────────────────────────

    #[test]
    fn code_challenge_matches_rfc_7636_test_vector() {
        // RFC 7636 Appendix B.1.
        let verifier = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk";
        let expected = "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM";
        assert_eq!(code_challenge(verifier), expected);
    }

    #[test]
    fn code_challenge_output_is_url_safe_no_padding() {
        let challenge = code_challenge("some-verifier-value");
        assert!(!challenge.contains('+'));
        assert!(!challenge.contains('/'));
        assert!(!challenge.contains('='));
    }

    #[test]
    fn generate_pending_login_state_and_verifier_are_distinct_and_rfc_compliant_length() {
        let pending = generate_pending_login();
        assert_ne!(pending.state, pending.code_verifier);
        assert!(pending.code_verifier.len() >= 43 && pending.code_verifier.len() <= 128);
        assert!(pending
            .code_verifier
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_'));
    }

    #[test]
    fn build_authorization_url_includes_pkce_state_and_offline_consent_params() {
        let url = build_authorization_url(
            "client-123",
            "http://127.0.0.1:5555",
            DriveScope::ReadOnly,
            "state-abc",
            "challenge-xyz",
        )
        .unwrap();
        let query: std::collections::HashMap<_, _> = url.query_pairs().collect();
        assert_eq!(query.get("client_id").unwrap(), "client-123");
        assert_eq!(query.get("redirect_uri").unwrap(), "http://127.0.0.1:5555");
        assert_eq!(query.get("response_type").unwrap(), "code");
        assert_eq!(query.get("scope").unwrap(), SCOPE_READONLY);
        assert_eq!(query.get("state").unwrap(), "state-abc");
        assert_eq!(query.get("code_challenge").unwrap(), "challenge-xyz");
        assert_eq!(query.get("code_challenge_method").unwrap(), "S256");
        assert_eq!(query.get("access_type").unwrap(), "offline");
        assert_eq!(query.get("prompt").unwrap(), "consent");
    }

    #[test]
    fn build_authorization_url_uses_additive_scope_when_metadata_requested() {
        let url = build_authorization_url(
            "client-123",
            "http://127.0.0.1:5555",
            DriveScope::Metadata,
            "state-abc",
            "challenge-xyz",
        )
        .unwrap();
        let query: std::collections::HashMap<_, _> = url.query_pairs().collect();
        assert_eq!(
            query.get("scope").unwrap(),
            &format!("{SCOPE_READONLY} {SCOPE_METADATA}")
        );
    }

    #[test]
    fn parse_callback_extracts_code_and_state() {
        let request = "GET /?code=abc123&state=xyz789 HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n";
        let result = parse_callback(request).unwrap();
        assert_eq!(result.code.as_deref(), Some("abc123"));
        assert_eq!(result.state.as_deref(), Some("xyz789"));
        assert!(result.error.is_none());
    }

    #[test]
    fn parse_callback_extracts_error_and_error_description() {
        let request =
            "GET /?error=access_denied&error_description=user+declined&state=xyz HTTP/1.1\r\n\r\n";
        let result = parse_callback(request).unwrap();
        assert_eq!(result.error.as_deref(), Some("access_denied"));
        assert_eq!(result.error_description.as_deref(), Some("user declined"));
    }

    #[test]
    fn parse_callback_missing_query_string_is_none() {
        assert!(parse_callback("GET / HTTP/1.1\r\n\r\n").is_none());
        assert!(parse_callback("garbage").is_none());
    }

    #[test]
    fn parse_callback_ignores_unrecognized_query_keys() {
        let request = "GET /?code=abc&state=xyz&foo=bar HTTP/1.1\r\n\r\n";
        let result = parse_callback(request).unwrap();
        assert_eq!(result.code.as_deref(), Some("abc"));
        assert_eq!(result.state.as_deref(), Some("xyz"));
    }

    #[test]
    fn open_browser_manual_logs_and_succeeds() {
        assert!(open_browser(&BrowserLaunch::Manual, "https://example/auth").is_ok());
    }

    #[test]
    fn open_browser_command_substitutes_url_placeholder() {
        let launch = BrowserLaunch::Command(vec!["true".to_string(), "--url={url}".to_string()]);
        assert!(open_browser(&launch, "https://example/auth").is_ok());
    }

    #[test]
    fn open_browser_command_appends_url_when_no_placeholder() {
        let launch = BrowserLaunch::Command(vec!["true".to_string()]);
        assert!(open_browser(&launch, "https://example/auth").is_ok());
    }

    #[test]
    fn open_browser_command_passes_through_args_without_the_placeholder() {
        // A trailing flag with no `{url}` substring (e.g. `--verbose`) is
        // passed to the command unmodified, and the URL is still appended
        // since no arg claimed the placeholder.
        let launch = BrowserLaunch::Command(vec!["true".to_string(), "--verbose".to_string()]);
        assert!(open_browser(&launch, "https://example/auth").is_ok());
    }

    #[test]
    fn open_browser_command_rejects_empty_args() {
        let launch = BrowserLaunch::Command(vec![]);
        let err = open_browser(&launch, "u").unwrap_err();
        assert!(err.to_string().contains("empty browser command"));
    }

    // ── named_account_vars ───────────────────────────────────────────────

    #[test]
    fn named_account_vars_maps_credentials_to_json_string_values() {
        let credentials = DriveCredentials {
            client_id: "client-1".to_string(),
            client_secret: Secret::new("secret-1"),
            refresh_token: Secret::new("refresh-1"),
            scope: DriveScope::ReadOnly,
        };
        assert_eq!(
            named_account_vars(&credentials),
            [
                (
                    "client_id",
                    serde_json::Value::String("client-1".to_string())
                ),
                (
                    "client_secret",
                    serde_json::Value::String("secret-1".to_string())
                ),
                (
                    "refresh_token",
                    serde_json::Value::String("refresh-1".to_string())
                ),
                (
                    "scope",
                    serde_json::Value::String(SCOPE_READONLY.to_string())
                ),
            ]
        );
    }

    // ── build_browser_config (mirrors Gmail's issue #1505) ──────────────

    fn assert_is_auto(config: BrowserConfig) {
        assert!(matches!(config.launch, BrowserLaunch::Auto));
    }

    #[test]
    fn build_browser_config_defaults_to_auto_with_no_account() {
        assert_is_auto(build_browser_config(None, |_| panic!("must not be called")).unwrap());
    }

    #[test]
    fn build_browser_config_defaults_to_auto_with_no_opt_in() {
        let account = DriveAccountSettings {
            email_address: Some("alice@example.com".to_string()),
            ..DriveAccountSettings::default()
        };
        // chrome_profile_from_email is false, so the resolver must never run
        // even though email_address is set.
        assert_is_auto(
            build_browser_config(Some(&account), |_| panic!("must not be called")).unwrap(),
        );
    }

    #[test]
    fn build_browser_config_uses_browser_command_verbatim() {
        let account = DriveAccountSettings {
            browser_command: Some("chrome --new-window {url}".to_string()),
            ..DriveAccountSettings::default()
        };
        let config =
            build_browser_config(Some(&account), |_| panic!("must not be called")).unwrap();
        assert!(matches!(
            config.launch,
            BrowserLaunch::Command(args) if args == vec!["chrome", "--new-window", "{url}"]
        ));
    }

    #[test]
    fn build_browser_config_browser_command_wins_over_chrome_profile_from_email() {
        let account = DriveAccountSettings {
            browser_command: Some("chrome {url}".to_string()),
            chrome_profile_from_email: true,
            email_address: Some("alice@example.com".to_string()),
            ..DriveAccountSettings::default()
        };
        let config =
            build_browser_config(Some(&account), |_| panic!("must not be called")).unwrap();
        assert!(matches!(config.launch, BrowserLaunch::Command(_)));
    }

    #[test]
    fn build_browser_config_rejects_a_malformed_browser_command() {
        let account = DriveAccountSettings {
            browser_command: Some("chrome \"--flag".to_string()),
            ..DriveAccountSettings::default()
        };
        let err =
            build_browser_config(Some(&account), |_| panic!("must not be called")).unwrap_err();
        assert!(err.to_string().contains("browser_command"));
    }

    #[test]
    fn build_browser_config_resolves_the_chrome_profile_when_opted_in() {
        let account = DriveAccountSettings {
            chrome_profile_from_email: true,
            email_address: Some("alice@example.com".to_string()),
            ..DriveAccountSettings::default()
        };
        let config = build_browser_config(Some(&account), |email| {
            assert_eq!(email, "alice@example.com");
            Some(vec!["chrome-stub".to_string(), "{url}".to_string()])
        })
        .unwrap();
        assert!(matches!(
            config.launch,
            BrowserLaunch::Command(args) if args == vec!["chrome-stub", "{url}"]
        ));
    }

    #[test]
    fn build_browser_config_falls_back_to_auto_when_chrome_resolution_fails() {
        let account = DriveAccountSettings {
            chrome_profile_from_email: true,
            email_address: Some("alice@example.com".to_string()),
            ..DriveAccountSettings::default()
        };
        assert_is_auto(build_browser_config(Some(&account), |_| None).unwrap());
    }

    #[test]
    fn build_browser_config_is_auto_when_opted_in_but_no_email_address() {
        let account = DriveAccountSettings {
            chrome_profile_from_email: true,
            ..DriveAccountSettings::default()
        };
        assert_is_auto(
            build_browser_config(Some(&account), |_| panic!("must not be called")).unwrap(),
        );
    }

    // ── resolve_browser_config_for (mirrors Gmail's issue #1505) ────────

    #[test]
    fn resolve_browser_config_for_unconfigured_account_defaults_to_auto() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();
        std::env::set_var(DRIVE_CLIENT_ID, "literal-id");
        std::env::set_var(DRIVE_CLIENT_SECRET, "literal-secret");
        std::env::set_var(DRIVE_REFRESH_TOKEN, "literal-refresh");

        let drive = DriveSettings::default();
        assert_is_auto(resolve_browser_config_for(&drive, None).unwrap());
    }

    #[test]
    fn resolve_browser_config_for_named_account_without_chrome_opt_in_defaults_to_auto() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();

        let mut drive = DriveSettings::default();
        drive.accounts.insert(
            "work".to_string(),
            DriveAccountSettings {
                email_address: Some("alice@example.com".to_string()),
                ..DriveAccountSettings::default()
            },
        );

        // chrome_profile_from_email is false, so this never touches the
        // real chrome_profile::resolve_launch_command resolver.
        assert_is_auto(resolve_browser_config_for(&drive, Some("work")).unwrap());
    }

    // ── Loopback listener (real sockets, no wiremock) ───────────────────

    #[tokio::test]
    async fn wait_for_callback_times_out_when_nothing_connects() {
        let browser = BrowserConfig::default();
        let (listener, _port) = bind_callback_listener(&browser).await.unwrap();
        let err = wait_for_callback_with_timeout(listener, Duration::from_millis(50))
            .await
            .unwrap_err();
        assert!(matches!(
            err.downcast_ref::<DriveError>(),
            Some(DriveError::CallbackTimeout(_))
        ));
    }

    #[tokio::test]
    async fn wait_for_callback_reads_a_real_connection() {
        let browser = BrowserConfig::default();
        let (listener, port) = bind_callback_listener(&browser).await.unwrap();

        let client = tokio::spawn(async move {
            let mut stream = tokio::net::TcpStream::connect(("127.0.0.1", port))
                .await
                .unwrap();
            stream
                .write_all(b"GET /?code=abc&state=xyz HTTP/1.1\r\n\r\n")
                .await
                .unwrap();
        });

        let result = wait_for_callback(listener).await.unwrap();
        client.await.unwrap();
        assert_eq!(result.code.as_deref(), Some("abc"));
        assert_eq!(result.state.as_deref(), Some("xyz"));
    }

    #[tokio::test]
    async fn wait_for_callback_malformed_request_line_is_malformed_callback() {
        let browser = BrowserConfig::default();
        let (listener, port) = bind_callback_listener(&browser).await.unwrap();

        let client = tokio::spawn(async move {
            let mut stream = tokio::net::TcpStream::connect(("127.0.0.1", port))
                .await
                .unwrap();
            stream.write_all(b"not an http request").await.unwrap();
        });

        let err = wait_for_callback(listener).await.unwrap_err();
        client.await.unwrap();
        assert!(matches!(
            err.downcast_ref::<DriveError>(),
            Some(DriveError::MalformedCallback)
        ));
    }

    // ── Token exchange / refresh (wiremock) ─────────────────────────────

    #[tokio::test]
    async fn exchange_code_for_tokens_posts_expected_form_body() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .and(wiremock::matchers::path("/token"))
            .and(wiremock::matchers::body_string_contains(
                "grant_type=authorization_code",
            ))
            .and(wiremock::matchers::body_string_contains(
                "code_verifier=verifier-1",
            ))
            .and(wiremock::matchers::body_string_contains(
                "redirect_uri=http%3A%2F%2F127.0.0.1%3A9999",
            ))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "at-1",
                    "refresh_token": "rt-1",
                    "expires_in": 3600,
                    "scope": SCOPE_READONLY,
                })),
            )
            .expect(1)
            .mount(&server)
            .await;

        let http = reqwest::Client::new();
        let token_endpoint = format!("{}/token", server.uri());
        let response = exchange_code_for_tokens(
            &http,
            &token_endpoint,
            "client-1",
            "secret-1",
            "code-1",
            "verifier-1",
            "http://127.0.0.1:9999",
        )
        .await
        .unwrap();
        assert_eq!(response.access_token, "at-1");
        assert_eq!(response.refresh_token.as_deref(), Some("rt-1"));
    }

    #[tokio::test]
    async fn exchange_code_for_tokens_maps_invalid_grant_to_pkce_flavored_message() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .respond_with(
                wiremock::ResponseTemplate::new(400).set_body_json(serde_json::json!({
                    "error": "invalid_grant",
                    "error_description": "Bad Request",
                })),
            )
            .mount(&server)
            .await;

        let http = reqwest::Client::new();
        let err = exchange_code_for_tokens(
            &http,
            &server.uri(),
            "c",
            "s",
            "code",
            "verifier",
            "http://127.0.0.1:1",
        )
        .await
        .unwrap_err();
        assert!(err.to_string().contains("PKCE"));
    }

    // ── login_to (end-to-end: state mismatch / access_denied) ───────────
    //
    // These exercise `login_to` itself rather than its sub-components in
    // isolation, so a regression in how it wires the loopback callback into
    // the state-mismatch/access_denied branches would actually be caught.
    // The callback port is picked by binding-then-dropping a std listener
    // (a well-known "reserve a free port" trick) so the test's connector
    // task can dial it directly — `login_to` binds the real listener before
    // opening the browser, so the connector retries briefly to cover the
    // small window before that bind completes.
    //
    // That reserve-then-drop is itself a TOCTOU race against the rest of the
    // parallel test suite: another test can grab the same ephemeral port
    // before `login_to`'s own bind runs, which fails outright rather than
    // retrying (mirrors Gmail's issue #1489). `run_with_port_retry` bounds a
    // retry of the whole reserve/connect/bind attempt on exactly that
    // failure, so a lost race just tries again with a fresh port instead of
    // flaking.

    async fn connect_and_send(port: u16, request_line: &[u8]) {
        let mut stream = loop {
            match tokio::net::TcpStream::connect(("127.0.0.1", port)).await {
                Ok(stream) => break stream,
                Err(_) => tokio::time::sleep(Duration::from_millis(2)).await,
            }
        };
        stream.write_all(request_line).await.unwrap();
    }

    fn reserve_free_port() -> u16 {
        std::net::TcpListener::bind("127.0.0.1:0")
            .unwrap()
            .local_addr()
            .unwrap()
            .port()
    }

    /// True if `err` is `bind_callback_listener`'s wrapped `AddrInUse` —
    /// i.e. some other process/test won the race for the port reserved by
    /// [`reserve_free_port`] before `login_to` could rebind it.
    fn is_callback_bind_conflict(err: &anyhow::Error) -> bool {
        err.to_string()
            .contains("Failed to start the local OAuth callback listener")
    }

    const PORT_RETRY_ATTEMPTS: u32 = 5;

    /// Runs `attempt`, which reserves its own port via [`reserve_free_port`]
    /// and returns `login_to`'s result, retrying up to
    /// [`PORT_RETRY_ATTEMPTS`] times when the attempt loses the ephemeral
    /// port race (see the module comment above `connect_and_send`).
    async fn run_with_port_retry<F, Fut>(mut attempt: F) -> Result<DriveAuthStatus>
    where
        F: FnMut(u16) -> Fut,
        Fut: std::future::Future<Output = Result<DriveAuthStatus>>,
    {
        for remaining in (0..PORT_RETRY_ATTEMPTS).rev() {
            let result = attempt(reserve_free_port()).await;
            let is_retryable_conflict =
                matches!(&result, Err(err) if remaining > 0 && is_callback_bind_conflict(err));
            if !is_retryable_conflict {
                return result;
            }
        }
        unreachable!("loop always returns on its last iteration")
    }

    /// Awaits `connector` normally, unless `result` shows `login_to` lost
    /// the callback-port race — in which case the connector, which will
    /// never see a connection on the now-taken port, is aborted instead of
    /// hung.
    async fn finish_connector(
        connector: tokio::task::JoinHandle<()>,
        result: &Result<DriveAuthStatus>,
    ) {
        match result {
            Err(err) if is_callback_bind_conflict(err) => connector.abort(),
            _ => connector.await.unwrap(),
        }
    }

    /// Polls `path` until it holds non-empty content, then returns it —
    /// used to read back the authorization URL that `open_browser`'s
    /// captured shell command writes asynchronously.
    async fn wait_for_captured_url(path: &Path) -> String {
        loop {
            if let Ok(contents) = std::fs::read_to_string(path) {
                if !contents.is_empty() {
                    return contents;
                }
            }
            tokio::time::sleep(Duration::from_millis(2)).await;
        }
    }

    /// Shared body for the three `login_to_*` tests that drive a single
    /// fixed callback request line through `login_to` and expect it to
    /// error: reserves a port, spawns the connector, calls `login_to`, and
    /// retries the whole attempt (via [`run_with_port_retry`]) if it loses
    /// the ephemeral-port race. Asserts no settings file was written and
    /// returns the resulting error for the caller to inspect.
    async fn run_login_to_expect_err(request_line: &'static [u8]) -> anyhow::Error {
        std::fs::create_dir_all("tmp").ok();
        let temp_dir = tempfile::TempDir::new_in("tmp").unwrap();
        let settings_path = temp_dir.path().join("settings.json");

        let result = run_with_port_retry(|port| {
            let settings_path = settings_path.clone();
            async move {
                let browser = BrowserConfig {
                    launch: BrowserLaunch::Manual,
                    callback_addr: IpAddr::V4(Ipv4Addr::LOCALHOST),
                    callback_port: port,
                };
                let connector = tokio::spawn(connect_and_send(port, request_line));

                let result = login_to(
                    &settings_path,
                    None,
                    "client-id",
                    &Secret::new("client-secret"),
                    DriveScope::ReadOnly,
                    &browser,
                    "http://127.0.0.1:1/token", // never reached — fails before exchange
                )
                .await;

                finish_connector(connector, &result).await;
                result
            }
        })
        .await;

        let err = result.unwrap_err();
        assert!(!settings_path.exists());
        err
    }

    #[tokio::test]
    async fn run_with_port_retry_retries_after_a_callback_bind_conflict_then_succeeds() {
        let attempts = AtomicU32::new(0);

        let status = run_with_port_retry(|_port| {
            let attempt_no = attempts.fetch_add(1, Ordering::SeqCst);
            async move {
                if attempt_no == 0 {
                    Err(anyhow::anyhow!(
                        "Failed to start the local OAuth callback listener: address in use"
                    ))
                } else {
                    Ok(DriveAuthStatus {
                        has_client_id: true,
                        has_client_secret: true,
                        has_refresh_token: true,
                        scope: None,
                    })
                }
            }
        })
        .await
        .unwrap();

        assert_eq!(attempts.load(Ordering::SeqCst), 2);
        assert!(status.has_client_id);
    }

    #[tokio::test]
    async fn run_with_port_retry_does_not_retry_a_non_conflict_error() {
        let attempts = AtomicU32::new(0);

        let result = run_with_port_retry(|_port| {
            attempts.fetch_add(1, Ordering::SeqCst);
            async move { Err(anyhow::anyhow!("some other failure")) }
        })
        .await;

        assert_eq!(attempts.load(Ordering::SeqCst), 1);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn finish_connector_aborts_when_login_to_lost_the_port_race() {
        let (tx, mut rx) = tokio::sync::oneshot::channel::<()>();
        let connector = tokio::spawn(async move {
            tokio::time::sleep(Duration::from_secs(3600)).await;
            let _ = tx.send(());
        });
        let result: Result<DriveAuthStatus> = Err(anyhow::anyhow!(
            "Failed to start the local OAuth callback listener: address in use"
        ));

        tokio::time::timeout(Duration::from_secs(5), finish_connector(connector, &result))
            .await
            .expect("finish_connector must not wait for an aborted connector");

        assert!(
            rx.try_recv().is_err(),
            "connector must have been aborted, not run to completion"
        );
    }

    #[tokio::test]
    async fn finish_connector_awaits_connector_when_login_to_succeeds() {
        let ran = Arc::new(AtomicBool::new(false));
        let ran_clone = ran.clone();
        let connector = tokio::spawn(async move {
            ran_clone.store(true, Ordering::SeqCst);
        });
        let result = Ok(DriveAuthStatus {
            has_client_id: true,
            has_client_secret: true,
            has_refresh_token: true,
            scope: None,
        });

        finish_connector(connector, &result).await;

        assert!(ran.load(Ordering::SeqCst));
    }

    #[tokio::test]
    async fn wait_for_captured_url_polls_until_content_is_written() {
        std::fs::create_dir_all("tmp").ok();
        let temp_dir = tempfile::TempDir::new_in("tmp").unwrap();
        let path = temp_dir.path().join("captured-url.txt");

        let write_path = path.clone();
        tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(10)).await;
            std::fs::write(&write_path, "").unwrap();
            tokio::time::sleep(Duration::from_millis(10)).await;
            std::fs::write(&write_path, "https://example.com/authorize").unwrap();
        });

        let contents = wait_for_captured_url(&path).await;
        assert_eq!(contents, "https://example.com/authorize");
    }

    #[tokio::test]
    async fn login_to_rejects_a_callback_with_mismatched_state() {
        let err =
            run_login_to_expect_err(b"GET /?code=abc&state=the-wrong-state HTTP/1.1\r\n\r\n").await;

        assert!(matches!(
            err.downcast_ref::<DriveError>(),
            Some(DriveError::StateMismatch)
        ));
    }

    #[tokio::test]
    async fn login_to_surfaces_access_denied_from_the_callback() {
        let err = run_login_to_expect_err(
            b"GET /?error=access_denied&error_description=user+declined HTTP/1.1\r\n\r\n",
        )
        .await;

        match err.downcast_ref::<DriveError>() {
            Some(DriveError::AuthorizationDenied(message)) => {
                assert!(message.contains("access_denied"));
                assert!(message.contains("user declined"));
            }
            other => panic!("expected AuthorizationDenied, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn login_to_rejects_a_callback_missing_code_and_state() {
        let err = run_login_to_expect_err(b"GET /?foo=bar HTTP/1.1\r\n\r\n").await;

        assert!(matches!(
            err.downcast_ref::<DriveError>(),
            Some(DriveError::MalformedCallback)
        ));
    }

    #[tokio::test]
    async fn login_to_completes_full_success_flow_and_persists_credentials() {
        // Captures the real authorization URL `login_to` generates (with its
        // randomly-generated CSRF `state`) by pointing the browser launch at
        // a shell command instead of an actual browser: `open_browser`
        // substitutes `{url}` into the command's args and spawns it, so a
        // tiny `/bin/sh` one-liner writes the URL to a file we can read back
        // — letting this test drive the full success path (state echoed
        // correctly, token exchange, credential persistence) without ever
        // opening a real browser or needing to predict the CSRF nonce.
        std::fs::create_dir_all("tmp").ok();
        let temp_dir = tempfile::TempDir::new_in("tmp").unwrap();
        let capture_path = temp_dir.path().join("captured-url.txt");
        let settings_path = temp_dir.path().join("settings.json");

        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .and(wiremock::matchers::path("/token"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "at-1",
                    "refresh_token": "rt-1",
                    "expires_in": 3600,
                    "scope": SCOPE_READONLY,
                })),
            )
            .expect(1)
            .mount(&server)
            .await;

        let status = run_with_port_retry(|port| {
            let capture_path = capture_path.clone();
            let settings_path = settings_path.clone();
            let token_endpoint = format!("{}/token", server.uri());
            async move {
                let browser = BrowserConfig {
                    launch: BrowserLaunch::Command(vec![
                        "/bin/sh".to_string(),
                        "-c".to_string(),
                        format!("printf '%s' \"$0\" > '{}'", capture_path.display()),
                        "{url}".to_string(),
                    ]),
                    callback_addr: IpAddr::V4(Ipv4Addr::LOCALHOST),
                    callback_port: port,
                };

                let connector = tokio::spawn(async move {
                    let auth_url = wait_for_captured_url(&capture_path).await;
                    let parsed = Url::parse(&auth_url).unwrap();
                    let state = parsed
                        .query_pairs()
                        .find(|(k, _)| k == "state")
                        .map(|(_, v)| v.into_owned())
                        .expect("authorization URL must carry a state param");
                    connect_and_send(
                        port,
                        format!("GET /?code=auth-code&state={state} HTTP/1.1\r\n\r\n").as_bytes(),
                    )
                    .await;
                });

                let result = login_to(
                    &settings_path,
                    None,
                    "client-id",
                    &Secret::new("client-secret"),
                    DriveScope::ReadOnly,
                    &browser,
                    &token_endpoint,
                )
                .await;

                finish_connector(connector, &result).await;
                result
            }
        })
        .await
        .unwrap();

        assert!(status.has_client_id);
        assert!(status.has_client_secret);
        assert!(status.has_refresh_token);
        assert_eq!(status.scope.as_deref(), Some(SCOPE_READONLY));

        let saved = std::fs::read_to_string(&settings_path).unwrap();
        assert!(saved.contains("rt-1"));
        assert!(saved.contains("client-id"));
    }

    /// Full mocked login round trip (real state nonce echoed back via the
    /// captured-authorization-URL trick, like
    /// `login_to_completes_full_success_flow_and_persists_credentials`
    /// above), with an injectable token-response body — the seam the
    /// scope-validation tests below use to simulate Google granting no
    /// Drive scope.
    async fn run_login_to_with_token_response(
        token_response_body: serde_json::Value,
    ) -> (Result<DriveAuthStatus>, std::path::PathBuf) {
        std::fs::create_dir_all("tmp").ok();
        let temp_dir = tempfile::TempDir::new_in("tmp").unwrap();
        let capture_path = temp_dir.path().join("captured-url.txt");
        let settings_path = temp_dir.path().join("settings.json");

        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .and(wiremock::matchers::path("/token"))
            .respond_with(wiremock::ResponseTemplate::new(200).set_body_json(&token_response_body))
            .expect(1)
            .mount(&server)
            .await;

        let result = run_with_port_retry(|port| {
            let capture_path = capture_path.clone();
            let settings_path = settings_path.clone();
            let token_endpoint = format!("{}/token", server.uri());
            async move {
                let browser = BrowserConfig {
                    launch: BrowserLaunch::Command(vec![
                        "/bin/sh".to_string(),
                        "-c".to_string(),
                        format!("printf '%s' \"$0\" > '{}'", capture_path.display()),
                        "{url}".to_string(),
                    ]),
                    callback_addr: IpAddr::V4(Ipv4Addr::LOCALHOST),
                    callback_port: port,
                };

                let connector = tokio::spawn(async move {
                    let auth_url = wait_for_captured_url(&capture_path).await;
                    let parsed = Url::parse(&auth_url).unwrap();
                    let state = parsed
                        .query_pairs()
                        .find(|(k, _)| k == "state")
                        .map(|(_, v)| v.into_owned())
                        .expect("authorization URL must carry a state param");
                    connect_and_send(
                        port,
                        format!("GET /?code=auth-code&state={state} HTTP/1.1\r\n\r\n").as_bytes(),
                    )
                    .await;
                });

                let result = login_to(
                    &settings_path,
                    None,
                    "client-id",
                    &Secret::new("client-secret"),
                    DriveScope::ReadOnly,
                    &browser,
                    &token_endpoint,
                )
                .await;

                finish_connector(connector, &result).await;
                result
            }
        })
        .await;

        (result, settings_path)
    }

    #[tokio::test]
    async fn login_to_rejects_a_grant_with_no_drive_scope() {
        let (result, settings_path) = run_login_to_with_token_response(serde_json::json!({
            "access_token": "at-1",
            "refresh_token": "rt-1",
            "expires_in": 3600,
            "scope": "openid email profile",
        }))
        .await;

        let err = result.unwrap_err();
        assert!(matches!(
            err.downcast_ref::<DriveError>(),
            Some(DriveError::NoScopeGranted(received)) if received == "openid, email, profile"
        ));
        assert!(!settings_path.exists());
    }

    #[tokio::test]
    async fn login_to_rejects_a_grant_with_missing_scope_field() {
        let (result, settings_path) = run_login_to_with_token_response(serde_json::json!({
            "access_token": "at-1",
            "refresh_token": "rt-1",
            "expires_in": 3600,
        }))
        .await;

        let err = result.unwrap_err();
        assert!(matches!(
            err.downcast_ref::<DriveError>(),
            Some(DriveError::NoScopeGranted(received)) if received == "none"
        ));
        assert!(!settings_path.exists());
    }

    // ── login_for (named-account login orchestration, mirrors Gmail's
    // issue #1500) ───────────────────────────────────────────────────────
    //
    // `login_for` hardcodes the real `TOKEN_ENDPOINT` in both branches
    // (unlike `login_to`, which takes one as an explicit test seam), so a
    // full success round trip can't be driven against a wiremock server
    // here. These instead drive a callback with a mismatched `state` —
    // which `run_login_flow` rejects *before* ever reaching the token
    // endpoint — to exercise account resolution (`Settings::load` +
    // `resolve_for_write`) and, for the named branch, the `run_login_flow`
    // call site itself, without any real network call.

    #[tokio::test]
    async fn login_for_unconfigured_account_rejects_a_callback_with_mismatched_state() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let dir = guard.clear_credentials();
        let settings_path = dir.path().join(".omni-dev").join("settings.json");

        let result = run_with_port_retry(|port| async move {
            let browser = BrowserConfig {
                launch: BrowserLaunch::Manual,
                callback_addr: IpAddr::V4(Ipv4Addr::LOCALHOST),
                callback_port: port,
            };
            let connector = tokio::spawn(connect_and_send(
                port,
                b"GET /?code=abc&state=the-wrong-state HTTP/1.1\r\n\r\n",
            ));

            let result = login_for(
                None,
                "client-id",
                &Secret::new("client-secret"),
                DriveScope::ReadOnly,
                &browser,
            )
            .await;

            finish_connector(connector, &result).await;
            result
        })
        .await;

        let err = result.unwrap_err();
        assert!(matches!(
            err.downcast_ref::<DriveError>(),
            Some(DriveError::StateMismatch)
        ));
        assert!(!settings_path.exists());
    }

    #[tokio::test]
    async fn login_for_named_account_rejects_a_callback_with_mismatched_state() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let dir = guard.clear_credentials();
        let settings_path = dir.path().join(".omni-dev").join("settings.json");

        let result = run_with_port_retry(|port| async move {
            let browser = BrowserConfig {
                launch: BrowserLaunch::Manual,
                callback_addr: IpAddr::V4(Ipv4Addr::LOCALHOST),
                callback_port: port,
            };
            let connector = tokio::spawn(connect_and_send(
                port,
                b"GET /?code=abc&state=the-wrong-state HTTP/1.1\r\n\r\n",
            ));

            let result = login_for(
                Some("work"),
                "client-id",
                &Secret::new("client-secret"),
                DriveScope::ReadOnly,
                &browser,
            )
            .await;

            finish_connector(connector, &result).await;
            result
        })
        .await;

        let err = result.unwrap_err();
        assert!(matches!(
            err.downcast_ref::<DriveError>(),
            Some(DriveError::StateMismatch)
        ));
        assert!(!settings_path.exists());
    }

    #[tokio::test]
    async fn refresh_access_token_posts_grant_type_refresh_token_and_parses_expires_in() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .and(wiremock::matchers::body_string_contains(
                "grant_type=refresh_token",
            ))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "at-2",
                    "expires_in": 1800,
                })),
            )
            .expect(1)
            .mount(&server)
            .await;

        let http = reqwest::Client::new();
        let response = refresh_access_token(&http, &server.uri(), "c", "s", "rt-1")
            .await
            .unwrap();
        assert_eq!(response.access_token, "at-2");
        assert_eq!(response.expires_in, 1800);
    }

    #[tokio::test]
    async fn refresh_access_token_maps_invalid_grant_to_testing_mode_message() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .respond_with(
                wiremock::ResponseTemplate::new(400).set_body_json(serde_json::json!({
                    "error": "invalid_grant",
                })),
            )
            .mount(&server)
            .await;

        let http = reqwest::Client::new();
        let err = refresh_access_token(&http, &server.uri(), "c", "s", "rt-1")
            .await
            .unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("7 days"));
        assert!(msg.contains("Testing"));
    }

    #[tokio::test]
    async fn refresh_access_token_falls_back_to_raw_body_when_error_is_unparsable() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .respond_with(wiremock::ResponseTemplate::new(400).set_body_string("not json"))
            .mount(&server)
            .await;

        let http = reqwest::Client::new();
        let err = refresh_access_token(&http, &server.uri(), "c", "s", "rt-1")
            .await
            .unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("unparsable error body"));
        assert!(msg.contains("not json"));
    }

    #[tokio::test]
    async fn token_request_propagates_network_errors() {
        let http = reqwest::Client::new();
        let err = refresh_access_token(&http, "http://127.0.0.1:1", "c", "s", "rt")
            .await
            .unwrap_err();
        assert!(err.to_string().contains("Failed to send token request"));
    }

    #[tokio::test]
    async fn token_request_errors_on_unparsable_response_body() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .respond_with(wiremock::ResponseTemplate::new(200).set_body_string("not json"))
            .mount(&server)
            .await;

        let http = reqwest::Client::new();
        let err = refresh_access_token(&http, &server.uri(), "c", "s", "rt")
            .await
            .unwrap_err();
        assert!(err.to_string().contains("Failed to parse"));
    }

    // ── DriveSession ─────────────────────────────────────────────────

    fn test_credentials() -> DriveCredentials {
        DriveCredentials {
            client_id: "client-1".to_string(),
            client_secret: "secret-1".into(),
            refresh_token: "refresh-1".into(),
            scope: DriveScope::ReadOnly,
        }
    }

    #[tokio::test]
    async fn access_token_refreshes_on_first_call() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "at-first",
                    "expires_in": 3600,
                })),
            )
            .expect(1)
            .mount(&server)
            .await;

        let session = DriveSession::new_with_token_endpoint(
            reqwest::Client::new(),
            &test_credentials(),
            &server.uri(),
        );
        let token = session.access_token().await.unwrap();
        assert_eq!(token.expose_secret(), "at-first");
    }

    #[tokio::test]
    async fn access_token_reuses_cached_token_within_skew_window() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "at-cached",
                    "expires_in": 3600,
                })),
            )
            .expect(1)
            .mount(&server)
            .await;

        let session = DriveSession::new_with_token_endpoint(
            reqwest::Client::new(),
            &test_credentials(),
            &server.uri(),
        );
        let first = session.access_token().await.unwrap();
        let second = session.access_token().await.unwrap();
        assert_eq!(first, second);
    }

    #[tokio::test]
    async fn access_token_proactively_refreshes_when_within_skew_window() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "at-short",
                    // Less than REFRESH_SKEW (60s), so the second call also refreshes.
                    "expires_in": 30,
                })),
            )
            .expect(2)
            .mount(&server)
            .await;

        let session = DriveSession::new_with_token_endpoint(
            reqwest::Client::new(),
            &test_credentials(),
            &server.uri(),
        );
        session.access_token().await.unwrap();
        session.access_token().await.unwrap();
    }

    #[tokio::test]
    async fn access_token_refresh_clamps_overflowing_expires_in_without_panicking() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "at-overflow",
                    // Unvalidated, this overflows both TimeDelta::seconds and
                    // the subsequent DateTime<Utc> addition (#1531).
                    "expires_in": i64::MAX,
                })),
            )
            .expect(1)
            .mount(&server)
            .await;

        let session = DriveSession::new_with_token_endpoint(
            reqwest::Client::new(),
            &test_credentials(),
            &server.uri(),
        );
        let token = session.access_token().await.unwrap();
        assert_eq!(token.expose_secret(), "at-overflow");
    }

    #[tokio::test]
    async fn access_token_refresh_clamps_negative_expires_in_to_immediately_expired() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "at-negative",
                    "expires_in": -3600,
                })),
            )
            // Negative expires_in clamps to 0, so the token is already
            // stale and the second call refreshes again.
            .expect(2)
            .mount(&server)
            .await;

        let session = DriveSession::new_with_token_endpoint(
            reqwest::Client::new(),
            &test_credentials(),
            &server.uri(),
        );
        session.access_token().await.unwrap();
        session.access_token().await.unwrap();
    }

    #[tokio::test]
    async fn force_refresh_concurrent_callers_do_not_stampede() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "at-bootstrap",
                    "expires_in": 3600,
                })),
            )
            .up_to_n_times(1)
            .with_priority(1)
            .mount(&server)
            .await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "at-refreshed",
                    "expires_in": 3600,
                })),
            )
            .expect(1)
            .with_priority(2)
            .mount(&server)
            .await;

        let session = DriveSession::new_with_token_endpoint(
            reqwest::Client::new(),
            &test_credentials(),
            &server.uri(),
        );
        let bootstrapped = session.access_token().await.unwrap();
        assert_eq!(bootstrapped.expose_secret(), "at-bootstrap");

        let (a, b) = tokio::join!(
            session.force_refresh(&bootstrapped),
            session.force_refresh(&bootstrapped)
        );
        let a = a.unwrap();
        let b = b.unwrap();
        assert_eq!(a, b);
        assert_eq!(a.expose_secret(), "at-refreshed");
    }

    #[tokio::test]
    async fn force_refresh_skips_network_call_when_token_already_rotated() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "at-a",
                    "expires_in": 3600,
                })),
            )
            .up_to_n_times(1)
            .with_priority(1)
            .mount(&server)
            .await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "at-b",
                    "expires_in": 3600,
                })),
            )
            .expect(1)
            .with_priority(2)
            .mount(&server)
            .await;

        let session = DriveSession::new_with_token_endpoint(
            reqwest::Client::new(),
            &test_credentials(),
            &server.uri(),
        );
        let stale = Secret::new("at-never-issued");
        let bootstrapped = session.access_token().await.unwrap();
        assert_eq!(bootstrapped.expose_secret(), "at-a");

        // `stale` was never the live token, so this should reuse the current
        // one without an extra network call.
        let result = session.force_refresh(&stale).await.unwrap();
        assert_eq!(result, bootstrapped);

        // A real force_refresh against the actual current token does POST.
        let refreshed = session.force_refresh(&bootstrapped).await.unwrap();
        assert_eq!(refreshed.expose_secret(), "at-b");
    }

    // ── Secret non-leakage ───────────────────────────────────────────

    #[test]
    fn drive_credentials_debug_redacts_client_secret_and_refresh_token() {
        let creds = DriveCredentials {
            client_id: "client-visible".to_string(),
            client_secret: "sekret-client-secret".into(),
            refresh_token: "sekret-refresh-token".into(),
            scope: DriveScope::ReadOnly,
        };
        let debug = format!("{creds:?}");
        assert!(debug.contains("DriveCredentials"));
        assert!(debug.contains("client-visible"));
        assert!(!debug.contains("sekret-client-secret"));
        assert!(!debug.contains("sekret-refresh-token"));
        assert!(debug.contains("client_secret: <redacted>"));
        assert!(debug.contains("refresh_token: <redacted>"));
    }

    #[test]
    fn drive_auth_status_yaml_serialization_contains_no_secret_values() {
        let env = crate::test_support::env::MapEnv::new()
            .with(DRIVE_CLIENT_ID, "client-id-value")
            .with(DRIVE_CLIENT_SECRET, "sekret-do-not-leak")
            .with(DRIVE_REFRESH_TOKEN, "sekret-refresh-do-not-leak")
            .with(DRIVE_SCOPE, SCOPE_READONLY);
        let status = status_with(&env);
        let yaml = serde_yaml::to_string(&status).unwrap();
        assert!(!yaml.contains("sekret-do-not-leak"));
        assert!(!yaml.contains("sekret-refresh-do-not-leak"));
    }

    // ── Env-DI boundary tests ────────────────────────────────────────

    use crate::test_support::env::MapEnv;

    #[test]
    fn status_reports_all_false_when_nothing_configured() {
        let status = status_with(&MapEnv::new());
        assert!(!status.has_client_id);
        assert!(!status.has_client_secret);
        assert!(!status.has_refresh_token);
        assert_eq!(status.scope, None);
    }

    #[test]
    fn status_reports_scope_when_present() {
        let env = MapEnv::new().with(DRIVE_SCOPE, SCOPE_READONLY);
        let status = status_with(&env);
        assert_eq!(status.scope.as_deref(), Some(SCOPE_READONLY));
    }

    #[test]
    fn load_credentials_errors_when_client_id_missing() {
        let env = MapEnv::new()
            .with(DRIVE_CLIENT_SECRET, "s")
            .with(DRIVE_REFRESH_TOKEN, "r");
        let err = load_credentials_with(&env).unwrap_err();
        assert!(err.to_string().contains("not configured"));
    }

    #[test]
    fn load_credentials_errors_when_client_secret_missing() {
        let env = MapEnv::new()
            .with(DRIVE_CLIENT_ID, "c")
            .with(DRIVE_REFRESH_TOKEN, "r");
        assert!(load_credentials_with(&env).is_err());
    }

    #[test]
    fn load_credentials_errors_when_refresh_token_missing() {
        let env = MapEnv::new()
            .with(DRIVE_CLIENT_ID, "c")
            .with(DRIVE_CLIENT_SECRET, "s");
        assert!(load_credentials_with(&env).is_err());
    }

    #[test]
    fn load_credentials_succeeds_with_all_three_present() {
        let env = MapEnv::new()
            .with(DRIVE_CLIENT_ID, "c")
            .with(DRIVE_CLIENT_SECRET, "s")
            .with(DRIVE_REFRESH_TOKEN, "r");
        let creds = load_credentials_with(&env).unwrap();
        assert_eq!(creds.client_id, "c");
        assert_eq!(creds.scope, DriveScope::ReadOnly);
    }

    /// Save + remove round-trip against injected settings-file paths — no
    /// `HOME` mutation, so the test needs no lock.
    #[test]
    fn save_then_remove_round_trip() {
        // ── Part 1: creates file from scratch ──────────────────────
        {
            let temp_dir = {
                std::fs::create_dir_all("tmp").ok();
                tempfile::TempDir::new_in("tmp").unwrap()
            };
            let settings_path = temp_dir.path().join(".omni-dev").join("settings.json");

            let creds = DriveCredentials {
                client_id: "client-1".to_string(),
                client_secret: "secret-1".into(),
                refresh_token: "refresh-1".into(),
                scope: DriveScope::ReadOnly,
            };
            save_credentials_to(&settings_path, None, &creds).unwrap();

            assert!(settings_path.exists());
            let val: serde_json::Value =
                serde_json::from_str(&fs::read_to_string(&settings_path).unwrap()).unwrap();
            assert_eq!(val["env"]["DRIVE_CLIENT_ID"], "client-1");
            assert_eq!(val["env"]["DRIVE_CLIENT_SECRET"], "secret-1");
            assert_eq!(val["env"]["DRIVE_REFRESH_TOKEN"], "refresh-1");
            assert_eq!(val["env"]["DRIVE_SCOPE"], SCOPE_READONLY);

            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;
                let mode = fs::metadata(&settings_path).unwrap().permissions().mode();
                assert_eq!(mode & 0o777, 0o600);
            }
        }

        // ── Part 2: merges into existing settings ──────────────────
        {
            let temp_dir = {
                std::fs::create_dir_all("tmp").ok();
                tempfile::TempDir::new_in("tmp").unwrap()
            };
            let omni_dir = temp_dir.path().join(".omni-dev");
            fs::create_dir_all(&omni_dir).unwrap();
            let settings_path = omni_dir.join("settings.json");
            fs::write(
                &settings_path,
                r#"{"env": {"OTHER_KEY": "keep_me"}, "extra": true}"#,
            )
            .unwrap();

            let creds = DriveCredentials {
                client_id: "client-2".to_string(),
                client_secret: "secret-2".into(),
                refresh_token: "refresh-2".into(),
                scope: DriveScope::ReadOnly,
            };
            save_credentials_to(&settings_path, None, &creds).unwrap();

            let val: serde_json::Value =
                serde_json::from_str(&fs::read_to_string(&settings_path).unwrap()).unwrap();
            assert_eq!(val["env"]["OTHER_KEY"], "keep_me");
            assert_eq!(val["extra"], true);
            assert_eq!(val["env"]["DRIVE_SCOPE"], SCOPE_READONLY);
        }

        // ── Part 3: remove clears the four keys, preserves others ──
        {
            let temp_dir = {
                std::fs::create_dir_all("tmp").ok();
                tempfile::TempDir::new_in("tmp").unwrap()
            };
            let omni_dir = temp_dir.path().join(".omni-dev");
            fs::create_dir_all(&omni_dir).unwrap();
            let settings_path = omni_dir.join("settings.json");
            fs::write(
                &settings_path,
                r#"{"env": {
                    "DRIVE_CLIENT_ID": "a",
                    "DRIVE_CLIENT_SECRET": "b",
                    "DRIVE_REFRESH_TOKEN": "c",
                    "DRIVE_SCOPE": "d",
                    "OTHER_KEY": "keep"
                }}"#,
            )
            .unwrap();

            let removed = remove_credentials_at(&settings_path, None).unwrap();
            assert!(removed);

            let val: serde_json::Value =
                serde_json::from_str(&fs::read_to_string(&settings_path).unwrap()).unwrap();
            assert!(val["env"].get("DRIVE_CLIENT_ID").is_none());
            assert!(val["env"].get("DRIVE_CLIENT_SECRET").is_none());
            assert!(val["env"].get("DRIVE_REFRESH_TOKEN").is_none());
            assert!(val["env"].get("DRIVE_SCOPE").is_none());
            assert_eq!(val["env"]["OTHER_KEY"], "keep");
        }

        // ── Part 4: remove returns false when nothing to remove ────
        {
            let temp_dir = {
                std::fs::create_dir_all("tmp").ok();
                tempfile::TempDir::new_in("tmp").unwrap()
            };
            let settings_path = temp_dir.path().join(".omni-dev").join("settings.json");
            let removed = remove_credentials_at(&settings_path, None).unwrap();
            assert!(!removed);
        }
    }

    /// Save + remove round-trip against a profile-targeted env map.
    #[test]
    fn save_then_remove_round_trip_in_profile() {
        let temp_dir = {
            std::fs::create_dir_all("tmp").ok();
            tempfile::TempDir::new_in("tmp").unwrap()
        };
        let omni_dir = temp_dir.path().join(".omni-dev");
        fs::create_dir_all(&omni_dir).unwrap();
        let settings_path = omni_dir.join("settings.json");
        fs::write(&settings_path, r#"{"env": {"OTHER_KEY": "keep_me"}}"#).unwrap();

        let creds = DriveCredentials {
            client_id: "client-p".to_string(),
            client_secret: "secret-p".into(),
            refresh_token: "refresh-p".into(),
            scope: DriveScope::ReadOnly,
        };
        save_credentials_to(&settings_path, Some("work"), &creds).unwrap();

        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&settings_path).unwrap()).unwrap();
        assert_eq!(
            val["profiles"]["work"]["env"]["DRIVE_CLIENT_ID"],
            "client-p"
        );
        assert!(val["env"].get("DRIVE_CLIENT_ID").is_none());
        assert_eq!(val["env"]["OTHER_KEY"], "keep_me");

        let removed = remove_credentials_at(&settings_path, Some("work")).unwrap();
        assert!(removed);
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&settings_path).unwrap()).unwrap();
        assert!(val["profiles"]["work"]["env"]
            .get("DRIVE_CLIENT_ID")
            .is_none());

        let removed = remove_credentials_at(&settings_path, Some("work")).unwrap();
        assert!(!removed);
    }

    /// The production wrappers resolve `~/.omni-dev/settings.json` from
    /// `HOME` and the active profile from `OMNI_DEV_PROFILE`, so this one
    /// test must redirect both via [`crate::drive::test_support::EnvGuard`].
    #[test]
    fn save_and_remove_credentials_resolve_default_settings_path() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let dir = guard.clear_credentials();

        let creds = DriveCredentials {
            client_id: "wrapper-client".to_string(),
            client_secret: "wrapper-secret".into(),
            refresh_token: "wrapper-refresh".into(),
            scope: DriveScope::ReadOnly,
        };
        save_credentials(&creds).unwrap();

        let settings_path = dir.path().join(".omni-dev").join("settings.json");
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&settings_path).unwrap()).unwrap();
        assert_eq!(val["env"]["DRIVE_CLIENT_ID"], "wrapper-client");

        assert!(remove_credentials().unwrap());
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&settings_path).unwrap()).unwrap();
        assert!(val["env"].get("DRIVE_CLIENT_ID").is_none());
    }

    // ── named-account dispatch (mirrors Gmail's issue #1500) ────────────
    //
    // These exercise the production `*_for` wrappers, so — like
    // `save_and_remove_credentials_resolve_default_settings_path` above —
    // they must redirect `HOME` via `EnvGuard`.

    #[test]
    fn load_credentials_for_named_reads_from_drive_accounts() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let dir = guard.clear_credentials();
        let settings_path = dir.path().join(".omni-dev").join("settings.json");
        Settings::upsert_drive_account(
            &settings_path,
            "work",
            &[
                (
                    "client_id",
                    serde_json::Value::String("work-id".to_string()),
                ),
                (
                    "client_secret",
                    serde_json::Value::String("work-secret".to_string()),
                ),
                (
                    "refresh_token",
                    serde_json::Value::String("work-refresh".to_string()),
                ),
                (
                    "scope",
                    serde_json::Value::String(SCOPE_READONLY.to_string()),
                ),
            ],
        )
        .unwrap();

        let creds = load_credentials_for(Some("work")).unwrap();
        assert_eq!(creds.client_id, "work-id");
        assert_eq!(creds.client_secret.expose_secret(), "work-secret");
        assert_eq!(creds.refresh_token.expose_secret(), "work-refresh");
        assert_eq!(creds.scope, DriveScope::ReadOnly);
    }

    #[test]
    fn load_credentials_for_unknown_named_account_errors() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let dir = guard.clear_credentials();
        let settings_path = dir.path().join(".omni-dev").join("settings.json");
        Settings::upsert_drive_account(
            &settings_path,
            "work",
            &[(
                "client_id",
                serde_json::Value::String("work-id".to_string()),
            )],
        )
        .unwrap();

        let err = load_credentials_for(Some("bogus")).unwrap_err();
        assert!(err.to_string().contains("unknown Drive account 'bogus'"));
    }

    #[test]
    fn load_credentials_for_falls_back_to_env_when_accounts_empty() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();
        std::env::set_var(DRIVE_CLIENT_ID, "literal-id");
        std::env::set_var(DRIVE_CLIENT_SECRET, "literal-secret");
        std::env::set_var(DRIVE_REFRESH_TOKEN, "literal-refresh");

        let creds = load_credentials_for(None).unwrap();
        assert_eq!(creds.client_id, "literal-id");
    }

    #[test]
    fn load_credentials_for_none_honors_ambient_account_env_var() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let dir = guard.clear_credentials();
        let settings_path = dir.path().join(".omni-dev").join("settings.json");
        Settings::upsert_drive_account(
            &settings_path,
            "work",
            &[
                (
                    "client_id",
                    serde_json::Value::String("work-id".to_string()),
                ),
                (
                    "client_secret",
                    serde_json::Value::String("work-secret".to_string()),
                ),
                (
                    "refresh_token",
                    serde_json::Value::String("work-refresh".to_string()),
                ),
            ],
        )
        .unwrap();
        std::env::set_var(account::DRIVE_ACCOUNT_ENV, "work");

        let creds = load_credentials_for(None).unwrap();
        assert_eq!(creds.client_id, "work-id");
    }

    #[test]
    fn remove_credentials_for_named_removes_whole_account() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let dir = guard.clear_credentials();
        let settings_path = dir.path().join(".omni-dev").join("settings.json");
        Settings::upsert_drive_account(
            &settings_path,
            "work",
            &[("client_id", serde_json::Value::String("id".to_string()))],
        )
        .unwrap();

        assert!(remove_credentials_for(Some("work")).unwrap());
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&settings_path).unwrap()).unwrap();
        assert!(val["drive"]["accounts"].get("work").is_none());
    }

    #[cfg(feature = "mcp")]
    #[test]
    fn status_for_named_reports_presence_from_account() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let dir = guard.clear_credentials();
        let settings_path = dir.path().join(".omni-dev").join("settings.json");
        Settings::upsert_drive_account(
            &settings_path,
            "work",
            &[
                ("client_id", serde_json::Value::String("id".to_string())),
                (
                    "scope",
                    serde_json::Value::String(SCOPE_READONLY.to_string()),
                ),
            ],
        )
        .unwrap();

        let status = status_for(Some("work")).unwrap();
        assert!(status.has_client_id);
        assert!(!status.has_client_secret);
        assert!(!status.has_refresh_token);
        assert_eq!(status.scope.as_deref(), Some(SCOPE_READONLY));
    }

    #[cfg(feature = "mcp")]
    #[test]
    fn status_for_unconfigured_matches_status_with_when_accounts_empty() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();
        std::env::set_var(DRIVE_CLIENT_ID, "literal-id");

        let status = status_for(None).unwrap();
        assert!(status.has_client_id);
        assert!(!status.has_refresh_token);
    }

    #[test]
    fn record_account_email_writes_email_address_only() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let dir = guard.clear_credentials();
        let settings_path = dir.path().join(".omni-dev").join("settings.json");
        Settings::upsert_drive_account(
            &settings_path,
            "work",
            &[("client_id", serde_json::Value::String("id".to_string()))],
        )
        .unwrap();

        record_account_email("work", "alice@work.com").unwrap();

        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&settings_path).unwrap()).unwrap();
        assert_eq!(
            val["drive"]["accounts"]["work"]["email_address"],
            "alice@work.com"
        );
        assert_eq!(val["drive"]["accounts"]["work"]["client_id"], "id");
    }

    #[test]
    fn record_account_email_does_not_overwrite_an_existing_value() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let dir = guard.clear_credentials();
        let settings_path = dir.path().join(".omni-dev").join("settings.json");
        Settings::upsert_drive_account(
            &settings_path,
            "work",
            &[(
                "email_address",
                serde_json::Value::String("manually-set@work.com".to_string()),
            )],
        )
        .unwrap();

        record_account_email("work", "alice@work.com").unwrap();

        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&settings_path).unwrap()).unwrap();
        assert_eq!(
            val["drive"]["accounts"]["work"]["email_address"],
            "manually-set@work.com"
        );
    }

    /// The empty-`drive.accounts` fallback path (mirrors Gmail's issue
    /// #1500 zero-migration guarantee): with `drive.accounts` empty, the
    /// account-aware `_for(None, ...)` wrappers must behave byte-identically
    /// to the direct env/settings wrappers they sit beside. Both sandboxes
    /// are seeded via the same unchanged [`save_credentials`] (no
    /// account-aware save wrapper exists — `login_for` persists directly,
    /// since a resolve-then-save round trip would reject the very name
    /// being created), so this isolates `load`/`remove`.
    #[test]
    fn accounts_empty_load_remove_byte_identical_via_direct_and_for_wrappers() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let creds = DriveCredentials {
            client_id: "id".to_string(),
            client_secret: "secret".into(),
            refresh_token: "refresh".into(),
            scope: DriveScope::ReadOnly,
        };

        let dir_direct = guard.clear_credentials();
        save_credentials(&creds).unwrap();
        let direct_written =
            fs::read_to_string(dir_direct.path().join(".omni-dev").join("settings.json")).unwrap();
        let direct_loaded = load_credentials().unwrap();
        let direct_removed = remove_credentials().unwrap();

        let dir_for = guard.clear_credentials();
        save_credentials(&creds).unwrap();
        let for_written =
            fs::read_to_string(dir_for.path().join(".omni-dev").join("settings.json")).unwrap();
        let for_loaded = load_credentials_for(None).unwrap();
        let for_removed = remove_credentials_for(None).unwrap();

        assert_eq!(direct_written, for_written);
        assert_eq!(direct_loaded.client_id, for_loaded.client_id);
        assert_eq!(
            direct_loaded.client_secret.expose_secret(),
            for_loaded.client_secret.expose_secret()
        );
        assert_eq!(direct_loaded.scope, for_loaded.scope);
        assert_eq!(direct_removed, for_removed);
    }
}