chrome-devtools 0.2.3

Profile-aware CLI for running Chrome DevTools MCP with isolated Chrome user data directories
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
use std::collections::HashMap;
use std::env;
use std::fs::{self, OpenOptions};
use std::io::{BufRead, BufReader, Read, Write};
use std::net::Shutdown;
use std::net::{SocketAddr, TcpListener, TcpStream};
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

#[derive(Clone, Debug)]
struct Config {
    profiles: Vec<Profile>,
}

#[derive(Clone, Debug)]
struct Profile {
    name: String,
    user_data_dir: String,
}

#[derive(Default)]
struct ProfileBuilder {
    name: Option<String>,
    user_data_dir: Option<String>,
}

const CONFIG_RELATIVE_PATH: &str = ".config/chrome-devtools/config.toml";
const CACHE_RELATIVE_PATH: &str = ".cache/chrome-devtools";
const DEFAULT_PROFILE_NAME: &str = "default";
const DEFAULT_PROFILE_USER_DATA_DIR: &str = "~/.config/chrome-devtools/profiles/default";
const PROFILE_USER_DATA_DIR_PREFIX: &str = "~/.config/chrome-devtools/profiles";
const DAEMON_READY_TIMEOUT: Duration = Duration::from_secs(30);
const SESSION_IDLE_TTL: Duration = Duration::from_secs(30 * 60);
const SESSION_REAPER_INTERVAL: Duration = Duration::from_secs(60);

#[derive(Clone, Debug)]
struct SessionState {
    id: String,
    created_at: SystemTime,
    last_used_at: SystemTime,
    owned: bool,
}

#[derive(Default)]
struct SessionRegistry {
    sessions: HashMap<String, SessionState>,
}

impl SessionRegistry {
    fn create(&mut self) -> SessionState {
        let now = SystemTime::now();
        let id = generate_session_id();
        let state = SessionState {
            id: id.clone(),
            created_at: now,
            last_used_at: now,
            owned: false,
        };
        self.sessions.insert(id.clone(), state.clone());
        state
    }

    fn list(&self) -> Vec<SessionState> {
        let mut sessions = self.sessions.values().cloned().collect::<Vec<_>>();
        sessions.sort_by_key(|session| session.created_at);
        sessions
    }

    fn close(&mut self, id: &str) -> Result<(), String> {
        if self.sessions.remove(id).is_some() {
            Ok(())
        } else {
            Err(format!("unknown session: {id}"))
        }
    }

    fn bind(&mut self, id: &str) -> Result<(), String> {
        let session = self
            .sessions
            .get_mut(id)
            .ok_or_else(|| format!("unknown session: {id}"))?;
        if session.owned {
            return Err(format!("session in use: {id}"));
        }
        session.owned = true;
        session.last_used_at = SystemTime::now();
        Ok(())
    }

    fn unbind(&mut self, id: &str) {
        if let Some(session) = self.sessions.get_mut(id) {
            session.owned = false;
            session.last_used_at = SystemTime::now();
        }
    }

    fn touch(&mut self, id: &str) {
        if let Some(session) = self.sessions.get_mut(id) {
            session.last_used_at = SystemTime::now();
        }
    }

    fn reap_expired(&mut self) {
        let now = SystemTime::now();
        self.sessions.retain(|_, session| {
            if session.owned {
                return true;
            }
            match now.duration_since(session.last_used_at) {
                Ok(elapsed) => elapsed < SESSION_IDLE_TTL,
                Err(_) => true,
            }
        });
    }
}

fn generate_session_id() -> String {
    static COUNTER: AtomicU64 = AtomicU64::new(0);
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_nanos() as u64)
        .unwrap_or(0);
    let pid = std::process::id() as u64;
    let counter = COUNTER.fetch_add(1, Ordering::Relaxed);
    let mix = nanos
        .wrapping_mul(0x9E3779B97F4A7C15)
        .wrapping_add(pid.wrapping_mul(0xBF58476D1CE4E5B9))
        .wrapping_add(counter.wrapping_mul(0x94D049BB133111EB));
    format!("sess-{mix:016x}")
}

fn unix_secs(time: SystemTime) -> u64 {
    time.duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_secs())
        .unwrap_or(0)
}

fn main() {
    if let Err(error) = run() {
        eprintln!("error: {error}");
        std::process::exit(1);
    }
}

fn run() -> Result<(), String> {
    let mut args = env::args().skip(1);
    let Some(object) = args.next() else {
        print_usage();
        return Err("missing object".to_string());
    };

    if matches!(object.as_str(), "help" | "--help" | "-h") {
        print_usage();
        return Ok(());
    }
    if matches!(object.as_str(), "version" | "--version" | "-V") {
        print_version();
        return Ok(());
    }

    let config = load_or_create_config()?;
    let Some(action) = args.next() else {
        print_usage();
        return Err(format!("missing action for object: {object}"));
    };

    let rest = args.collect::<Vec<_>>();

    match (object.as_str(), action.as_str()) {
        ("mcp", "help" | "--help" | "-h") => {
            print_mcp_help();
            Ok(())
        }
        ("profile", "help" | "--help" | "-h") => {
            print_profile_help();
            Ok(())
        }
        ("daemon", "help" | "--help" | "-h") => {
            print_daemon_help();
            Ok(())
        }
        ("session", "help" | "--help" | "-h") => {
            print_session_help();
            Ok(())
        }
        ("session", "create") => {
            if wants_help(&rest) {
                print_session_create_help();
                return Ok(());
            }
            let profile = require_profile(&config, &rest)?;
            create_session(&profile)
        }
        ("session", "list") => {
            if wants_help(&rest) {
                print_session_list_help();
                return Ok(());
            }
            let profile = require_profile(&config, &rest)?;
            list_sessions(&profile)
        }
        ("session", "close") => {
            if wants_help(&rest) {
                print_session_close_help();
                return Ok(());
            }
            let (profile, session_id) = require_profile_and_session(&config, &rest)?;
            close_session(&profile, &session_id)
        }
        ("mcp", "call") => {
            if wants_help(&rest) {
                print_mcp_call_help();
                return Ok(());
            }
            let (profile, session_id) = require_profile_and_session(&config, &rest)?;
            call_daemon(&profile, &session_id)
        }
        ("mcp", "batch") => {
            if wants_help(&rest) {
                print_mcp_batch_help();
                return Ok(());
            }
            run_batch(&config, &rest)
        }
        ("mcp", "list") => {
            if wants_help(&rest) {
                print_mcp_list_help();
                return Ok(());
            }
            let profile = require_profile(&config, &rest)?;
            list_mcp_tools_via_daemon(&profile)
        }
        ("mcp", "direct-call") => {
            if wants_help(&rest) {
                print_mcp_direct_call_help();
                return Ok(());
            }
            let profile = require_profile(&config, &rest)?;
            let _lock = acquire_profile_lock(&profile)?;
            ensure_chrome(&profile)?;
            exec_mcp(&profile)
        }
        ("mcp", "direct-list") => {
            if wants_help(&rest) {
                print_mcp_direct_list_help();
                return Ok(());
            }
            let profile = require_profile(&config, &rest)?;
            let _lock = acquire_profile_lock(&profile)?;
            ensure_chrome(&profile)?;
            list_mcp_tools(&profile)
        }
        ("profile", "status") => {
            if wants_help(&rest) {
                print_profile_status_help();
                return Ok(());
            }
            let profile = require_profile(&config, &rest)?;
            print_status(&profile);
            Ok(())
        }
        ("profile", "stop") => {
            if wants_help(&rest) {
                print_profile_stop_help();
                return Ok(());
            }
            let profile = require_profile(&config, &rest)?;
            stop_profile(&profile)
        }
        ("profile", "list") => {
            if wants_help(&rest) {
                print_profile_list_help();
                return Ok(());
            }
            reject_extra_args(&rest)?;
            list_profiles(&config);
            Ok(())
        }
        ("daemon", "start") => {
            if wants_help(&rest) {
                print_daemon_start_help();
                return Ok(());
            }
            let profile = require_profile(&config, &rest)?;
            start_daemon(&profile, false)
        }
        ("daemon", "run") => {
            if wants_help(&rest) {
                print_daemon_run_help();
                return Ok(());
            }
            let profile = require_profile(&config, &rest)?;
            run_daemon(&profile)
        }
        ("daemon", "status") => {
            if wants_help(&rest) {
                print_daemon_status_help();
                return Ok(());
            }
            let profile = require_profile(&config, &rest)?;
            print_daemon_status(&profile)
        }
        ("daemon", "stop") => {
            if wants_help(&rest) {
                print_daemon_stop_help();
                return Ok(());
            }
            let profile = require_profile(&config, &rest)?;
            stop_daemon(&profile)
        }
        _ => Err(format!("unknown command: {object} {action}")),
    }
}

fn wants_help(args: &[String]) -> bool {
    args.iter()
        .any(|arg| matches!(arg.as_str(), "--help" | "-h" | "help"))
}

fn load_or_create_config() -> Result<Config, String> {
    let path = config_path()?;
    if !path.exists() {
        create_default_config(&path)?;
    }

    let content = fs::read_to_string(&path)
        .map_err(|error| format!("failed to read {}: {error}", path.display()))?;
    parse_config(&content).map_err(|error| format!("failed to parse {}: {error}", path.display()))
}

fn create_default_config(path: &Path) -> Result<(), String> {
    let Some(parent) = path.parent() else {
        return Err(format!("config path has no parent: {}", path.display()));
    };

    fs::create_dir_all(parent)
        .map_err(|error| format!("failed to create {}: {error}", parent.display()))?;
    fs::create_dir_all(expand_home(DEFAULT_PROFILE_USER_DATA_DIR)?)
        .map_err(|error| format!("failed to create default profile user data dir: {error}"))?;
    fs::write(path, default_config_content())
        .map_err(|error| format!("failed to write {}: {error}", path.display()))
}

fn default_config_content() -> String {
    format!("[[profiles]]\nname = \"{DEFAULT_PROFILE_NAME}\"\n")
}

fn parse_config(content: &str) -> Result<Config, String> {
    let mut profiles = Vec::new();
    let mut current: Option<ProfileBuilder> = None;

    for (line_number, raw_line) in content.lines().enumerate() {
        let line_number = line_number + 1;
        let line = raw_line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }

        if line == "[[profiles]]" {
            push_profile(&mut profiles, current.take(), line_number)?;
            current = Some(ProfileBuilder::default());
            continue;
        }

        let Some((key, value)) = line.split_once('=') else {
            return Err(format!("line {line_number}: expected key = value"));
        };
        let Some(profile) = current.as_mut() else {
            return Err(format!(
                "line {line_number}: profile fields must be inside [[profiles]]"
            ));
        };

        let key = key.trim();
        let value = value.trim();
        match key {
            "name" => profile.name = Some(parse_toml_string(value, line_number)?),
            "port" => {
                eprintln!(
                    "warning: line {line_number}: 'port' is deprecated and ignored; the daemon now picks a free port automatically"
                );
            }
            "user_data_dir" => profile.user_data_dir = Some(parse_toml_string(value, line_number)?),
            unknown => {
                return Err(format!(
                    "line {line_number}: unknown profile key: {unknown}"
                ))
            }
        }
    }

    push_profile(&mut profiles, current.take(), content.lines().count() + 1)?;

    if profiles.is_empty() {
        return Err("config must define at least one [[profiles]] entry".to_string());
    }

    Ok(Config { profiles })
}

fn push_profile(
    profiles: &mut Vec<Profile>,
    builder: Option<ProfileBuilder>,
    line_number: usize,
) -> Result<(), String> {
    let Some(builder) = builder else {
        return Ok(());
    };

    let name = builder
        .name
        .ok_or_else(|| format!("line {line_number}: profile is missing name"))?;
    let user_data_dir = builder
        .user_data_dir
        .unwrap_or_else(|| default_user_data_dir_for_profile(&name));

    if profiles.iter().any(|profile| profile.name == name) {
        return Err(format!("duplicate profile name: {name}"));
    }

    profiles.push(Profile {
        name,
        user_data_dir,
    });
    Ok(())
}

fn default_user_data_dir_for_profile(name: &str) -> String {
    format!("{PROFILE_USER_DATA_DIR_PREFIX}/{name}")
}

fn parse_toml_string(value: &str, line_number: usize) -> Result<String, String> {
    let Some(inner) = value
        .strip_prefix('"')
        .and_then(|value| value.strip_suffix('"'))
    else {
        return Err(format!("line {line_number}: expected quoted string"));
    };

    let mut parsed = String::new();
    let mut chars = inner.chars();
    while let Some(character) = chars.next() {
        if character != '\\' {
            parsed.push(character);
            continue;
        }

        let Some(escaped) = chars.next() else {
            return Err(format!("line {line_number}: dangling escape in string"));
        };
        match escaped {
            '\\' => parsed.push('\\'),
            '"' => parsed.push('"'),
            'n' => parsed.push('\n'),
            'r' => parsed.push('\r'),
            't' => parsed.push('\t'),
            other => return Err(format!("line {line_number}: unsupported escape: \\{other}")),
        }
    }

    Ok(parsed)
}

fn config_path() -> Result<PathBuf, String> {
    let Some(home) = env::var_os("HOME") else {
        return Err("HOME is not set".to_string());
    };
    Ok(PathBuf::from(home).join(CONFIG_RELATIVE_PATH))
}

fn cache_dir() -> Result<PathBuf, String> {
    let Some(home) = env::var_os("HOME") else {
        return Err("HOME is not set".to_string());
    };
    Ok(PathBuf::from(home).join(CACHE_RELATIVE_PATH))
}

struct ProfileLock {
    path: PathBuf,
}

impl Drop for ProfileLock {
    fn drop(&mut self) {
        let _ = fs::remove_file(&self.path);
    }
}

fn acquire_profile_lock(profile: &Profile) -> Result<ProfileLock, String> {
    let lock_dir = cache_dir()?.join("locks");
    fs::create_dir_all(&lock_dir)
        .map_err(|error| format!("failed to create {}: {error}", lock_dir.display()))?;

    let path = lock_dir.join(format!("{}.lock", safe_lock_name(&profile.name)));
    let timeout = lock_timeout();
    let started = Instant::now();

    loop {
        match OpenOptions::new().write(true).create_new(true).open(&path) {
            Ok(mut file) => {
                writeln!(file, "pid={}", std::process::id())
                    .map_err(|error| format!("failed to write {}: {error}", path.display()))?;
                writeln!(file, "profile={}", profile.name)
                    .map_err(|error| format!("failed to write {}: {error}", path.display()))?;
                return Ok(ProfileLock { path });
            }
            Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
                if remove_stale_lock(&path)? {
                    continue;
                }
                if started.elapsed() >= timeout {
                    return Err(format!(
                        "profile {} is locked by another chrome-devtools MCP session: {}",
                        profile.name,
                        path.display()
                    ));
                }
                thread::sleep(Duration::from_millis(250));
            }
            Err(error) => {
                return Err(format!("failed to create {}: {error}", path.display()));
            }
        }
    }
}

fn lock_timeout() -> Duration {
    env::var("CHROME_DEVTOOLS_LOCK_TIMEOUT_SECS")
        .ok()
        .and_then(|value| value.parse::<u64>().ok())
        .map(Duration::from_secs)
        .unwrap_or_else(|| Duration::from_secs(300))
}

fn remove_stale_lock(path: &Path) -> Result<bool, String> {
    let content = fs::read_to_string(path)
        .map_err(|error| format!("failed to read lock {}: {error}", path.display()))?;
    let Some(pid) = parse_lock_pid(&content) else {
        return Ok(false);
    };

    if process_exists(pid) {
        return Ok(false);
    }

    fs::remove_file(path)
        .map_err(|error| format!("failed to remove stale lock {}: {error}", path.display()))?;
    Ok(true)
}

fn parse_lock_pid(content: &str) -> Option<u32> {
    content.lines().find_map(|line| {
        line.strip_prefix("pid=")
            .and_then(|value| value.trim().parse::<u32>().ok())
    })
}

fn process_exists(pid: u32) -> bool {
    PathBuf::from(format!("/proc/{pid}")).exists()
}

fn safe_lock_name(name: &str) -> String {
    name.chars()
        .map(|character| {
            if character.is_ascii_alphanumeric() || matches!(character, '-' | '_') {
                character
            } else {
                '_'
            }
        })
        .collect()
}

fn daemon_dir() -> Result<PathBuf, String> {
    Ok(cache_dir()?.join("daemons"))
}

fn daemon_socket_path(profile: &Profile) -> Result<PathBuf, String> {
    Ok(daemon_dir()?.join(format!("{}.sock", safe_lock_name(&profile.name))))
}

fn daemon_pid_path(profile: &Profile) -> Result<PathBuf, String> {
    Ok(daemon_dir()?.join(format!("{}.pid", safe_lock_name(&profile.name))))
}

fn daemon_log_path(profile: &Profile) -> Result<PathBuf, String> {
    Ok(daemon_dir()?.join(format!("{}.log", safe_lock_name(&profile.name))))
}

fn daemon_port_path(profile: &Profile) -> Result<PathBuf, String> {
    Ok(daemon_dir()?.join(format!("{}.port", safe_lock_name(&profile.name))))
}

fn read_runtime_port(profile: &Profile) -> Option<u16> {
    let path = daemon_port_path(profile).ok()?;
    let raw = fs::read_to_string(path).ok()?;
    raw.trim().parse().ok()
}

fn write_runtime_port(profile: &Profile, port: u16) -> Result<(), String> {
    let path = daemon_port_path(profile)?;
    fs::write(&path, port.to_string())
        .map_err(|error| format!("failed to write {}: {error}", path.display()))
}

fn current_port(profile: &Profile) -> Option<u16> {
    read_runtime_port(profile)
}

fn pick_free_port() -> Result<u16, String> {
    let listener = TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))
        .map_err(|error| format!("failed to acquire a free port: {error}"))?;
    let addr = listener
        .local_addr()
        .map_err(|error| format!("failed to read local addr: {error}"))?;
    Ok(addr.port())
}

fn start_daemon(profile: &Profile, quiet: bool) -> Result<(), String> {
    if is_daemon_ready(profile)? {
        if !quiet {
            println!(
                "profile={} daemon=ready socket={}",
                profile.name,
                daemon_socket_path(profile)?.display()
            );
        }
        return Ok(());
    }

    let dir = daemon_dir()?;
    fs::create_dir_all(&dir)
        .map_err(|error| format!("failed to create {}: {error}", dir.display()))?;
    cleanup_stale_daemon_files(profile)?;

    let current_exe = env::current_exe()
        .map_err(|error| format!("failed to locate current executable: {error}"))?;
    let log_path = daemon_log_path(profile)?;
    let log = OpenOptions::new()
        .create(true)
        .append(true)
        .open(&log_path)
        .map_err(|error| format!("failed to open {}: {error}", log_path.display()))?;
    let log_for_stderr = log
        .try_clone()
        .map_err(|error| format!("failed to clone daemon log handle: {error}"))?;

    let child = Command::new(current_exe)
        .arg("daemon")
        .arg("run")
        .arg("--profile")
        .arg(&profile.name)
        .stdin(Stdio::null())
        .stdout(Stdio::from(log))
        .stderr(Stdio::from(log_for_stderr))
        .spawn()
        .map_err(|error| format!("failed to start daemon: {error}"))?;

    wait_for_daemon(profile, DAEMON_READY_TIMEOUT).map_err(|error| {
        format!(
            "failed to start daemon process {} for profile {}: {error}; log={}",
            child.id(),
            profile.name,
            log_path.display()
        )
    })?;

    if !quiet {
        println!(
            "profile={} daemon=started pid={} socket={}",
            profile.name,
            child.id(),
            daemon_socket_path(profile)?.display()
        );
    }
    Ok(())
}

fn run_daemon(profile: &Profile) -> Result<(), String> {
    let dir = daemon_dir()?;
    fs::create_dir_all(&dir)
        .map_err(|error| format!("failed to create {}: {error}", dir.display()))?;
    let socket_path = daemon_socket_path(profile)?;
    let pid_path = daemon_pid_path(profile)?;
    let _lock = acquire_profile_lock(profile)?;

    if socket_path.exists() {
        fs::remove_file(&socket_path).map_err(|error| {
            format!(
                "failed to remove stale socket {}: {error}",
                socket_path.display()
            )
        })?;
    }

    ensure_chrome(profile)?;
    let mut mcp = mcp_command(profile)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::inherit())
        .spawn()
        .map_err(|error| format!("failed to run chrome-devtools-mcp: {error}"))?;
    let mut mcp_stdin = mcp
        .stdin
        .take()
        .ok_or_else(|| "failed to open chrome-devtools-mcp stdin".to_string())?;
    let mcp_stdout = mcp
        .stdout
        .take()
        .ok_or_else(|| "failed to open chrome-devtools-mcp stdout".to_string())?;
    let mut mcp_reader = BufReader::new(mcp_stdout);
    initialize_daemon_mcp(&mut mcp_stdin, &mut mcp_reader)?;

    fs::write(&pid_path, format!("{}\n", std::process::id()))
        .map_err(|error| format!("failed to write {}: {error}", pid_path.display()))?;

    let listener = UnixListener::bind(&socket_path)
        .map_err(|error| format!("failed to bind {}: {error}", socket_path.display()))?;

    let sessions: Arc<Mutex<SessionRegistry>> = Arc::new(Mutex::new(SessionRegistry::default()));
    let sessions_for_reaper = Arc::clone(&sessions);
    thread::spawn(move || loop {
        thread::sleep(SESSION_REAPER_INTERVAL);
        if let Ok(mut registry) = sessions_for_reaper.lock() {
            registry.reap_expired();
        }
    });

    for stream in listener.incoming() {
        let mut stream =
            stream.map_err(|error| format!("failed to accept daemon client: {error}"))?;
        let should_stop =
            handle_daemon_client(&mut stream, &mut mcp_stdin, &mut mcp_reader, &sessions)?;
        if should_stop {
            break;
        }
        if let Some(status) = mcp
            .try_wait()
            .map_err(|error| format!("failed to poll chrome-devtools-mcp: {error}"))?
        {
            return Err(format!("chrome-devtools-mcp exited with {status}"));
        }
    }

    terminate_child(&mut mcp);
    let _ = fs::remove_file(&socket_path);
    let _ = fs::remove_file(&pid_path);
    Ok(())
}

fn initialize_daemon_mcp(
    mcp_stdin: &mut impl Write,
    mcp_reader: &mut impl BufRead,
) -> Result<(), String> {
    write_json_line(
        mcp_stdin,
        r#"{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{"roots":{"listChanged":false}},"clientInfo":{"name":"chrome-devtools-daemon","version":"0.1.0"}}}"#,
    )?;
    read_response(mcp_reader, mcp_stdin, 1)?;
    write_json_line(
        mcp_stdin,
        r#"{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}"#,
    )
}

struct BoundSessionGuard<'a> {
    sessions: &'a Arc<Mutex<SessionRegistry>>,
    id: Option<String>,
}

impl<'a> BoundSessionGuard<'a> {
    fn new(sessions: &'a Arc<Mutex<SessionRegistry>>) -> Self {
        Self { sessions, id: None }
    }
}

impl Drop for BoundSessionGuard<'_> {
    fn drop(&mut self) {
        if let Some(id) = self.id.take() {
            if let Ok(mut registry) = self.sessions.lock() {
                registry.unbind(&id);
            }
        }
    }
}

fn handle_daemon_client(
    stream: &mut UnixStream,
    mcp_stdin: &mut impl Write,
    mcp_reader: &mut impl BufRead,
    sessions: &Arc<Mutex<SessionRegistry>>,
) -> Result<bool, String> {
    let mut client_reader = BufReader::new(
        stream
            .try_clone()
            .map_err(|error| format!("failed to clone daemon client stream: {error}"))?,
    );
    let mut line = String::new();
    let mut bound = BoundSessionGuard::new(sessions);

    loop {
        line.clear();
        let bytes = client_reader
            .read_line(&mut line)
            .map_err(|error| format!("failed to read daemon client request: {error}"))?;
        if bytes == 0 {
            return Ok(false);
        }

        let line = line.trim_end();
        if line.is_empty() {
            continue;
        }

        if let Some(command) = line.strip_prefix("__chrome_devtools_daemon__:") {
            match handle_control_command(stream, sessions, &mut bound, command)? {
                ControlOutcome::Continue => continue,
                ControlOutcome::CloseConnection => return Ok(false),
                ControlOutcome::StopDaemon => return Ok(true),
            }
        }

        if json_has_method(line, "initialize") {
            if let Some(id) = extract_jsonrpc_id(line) {
                let response = daemon_initialize_response(id);
                stream
                    .write_all(response.as_bytes())
                    .and_then(|_| stream.write_all(b"\n"))
                    .and_then(|_| stream.flush())
                    .map_err(|error| {
                        format!("failed to write daemon initialize response: {error}")
                    })?;
            }
            continue;
        }
        if json_has_method(line, "notifications/initialized") {
            continue;
        }

        let forwarded = sanitize_outgoing_request(line);
        let Some(pending_id) = extract_jsonrpc_id(&forwarded) else {
            write_json_line(mcp_stdin, &forwarded)?;
            continue;
        };

        write_json_line(mcp_stdin, &forwarded)?;

        loop {
            let mut response_line = String::new();
            let bytes = mcp_reader
                .read_line(&mut response_line)
                .map_err(|error| format!("failed to read MCP response: {error}"))?;
            if bytes == 0 {
                return Err("chrome-devtools-mcp closed stdout before responding".to_string());
            }
            let response_line = response_line.trim_end().to_string();
            if json_has_method(&response_line, "roots/list") {
                if let Some(id) = extract_jsonrpc_id(&response_line) {
                    let response =
                        format!(r#"{{"jsonrpc":"2.0","id":{id},"result":{{"roots":[]}}}}"#);
                    write_json_line(mcp_stdin, &response)?;
                }
                continue;
            }

            stream
                .write_all(response_line.as_bytes())
                .and_then(|_| stream.write_all(b"\n"))
                .and_then(|_| stream.flush())
                .map_err(|error| format!("failed to write daemon client response: {error}"))?;

            if extract_jsonrpc_id(&response_line) == Some(pending_id) {
                if let Some(id) = bound.id.as_ref() {
                    if let Ok(mut registry) = sessions.lock() {
                        registry.touch(id);
                    }
                }
                break;
            }
        }
    }
}

enum ControlOutcome {
    Continue,
    CloseConnection,
    StopDaemon,
}

fn handle_control_command(
    stream: &mut UnixStream,
    sessions: &Arc<Mutex<SessionRegistry>>,
    bound: &mut BoundSessionGuard,
    command: &str,
) -> Result<ControlOutcome, String> {
    let (head, rest) = match command.split_once(' ') {
        Some((head, rest)) => (head, rest.trim()),
        None => (command, ""),
    };
    match head {
        "status" => {
            write_control_line(stream, "daemon=ready")?;
            Ok(ControlOutcome::CloseConnection)
        }
        "stop" => {
            write_control_line(stream, "daemon=stopping")?;
            Ok(ControlOutcome::StopDaemon)
        }
        "session_create" => {
            let state = sessions
                .lock()
                .map_err(|_| "session registry poisoned".to_string())?
                .create();
            write_control_line(stream, &format_session_line(&state))?;
            Ok(ControlOutcome::CloseConnection)
        }
        "session_list" => {
            let snapshot = sessions
                .lock()
                .map_err(|_| "session registry poisoned".to_string())?
                .list();
            for state in &snapshot {
                write_control_line(stream, &format_session_line(state))?;
            }
            Ok(ControlOutcome::CloseConnection)
        }
        "session_close" => {
            let id = parse_session_arg(rest)?;
            let result = sessions
                .lock()
                .map_err(|_| "session registry poisoned".to_string())?
                .close(&id);
            match result {
                Ok(()) => write_control_line(stream, &format!("closed={id}"))?,
                Err(message) => write_control_line(stream, &format!("error={message}"))?,
            }
            Ok(ControlOutcome::CloseConnection)
        }
        "bind" => {
            let id = parse_session_arg(rest)?;
            let result = sessions
                .lock()
                .map_err(|_| "session registry poisoned".to_string())?
                .bind(&id);
            match result {
                Ok(()) => {
                    bound.id = Some(id.clone());
                    write_control_line(stream, &format!("bound={id}"))?;
                    Ok(ControlOutcome::Continue)
                }
                Err(message) => {
                    write_control_line(stream, &format!("error={message}"))?;
                    Ok(ControlOutcome::CloseConnection)
                }
            }
        }
        other => {
            write_control_line(stream, &format!("error=unknown command: {other}"))?;
            Ok(ControlOutcome::CloseConnection)
        }
    }
}

fn write_control_line(stream: &mut UnixStream, body: &str) -> Result<(), String> {
    stream
        .write_all(body.as_bytes())
        .and_then(|_| stream.write_all(b"\n"))
        .and_then(|_| stream.flush())
        .map_err(|error| format!("failed to write daemon response: {error}"))
}

fn format_session_line(state: &SessionState) -> String {
    format!(
        "session={} created={} last_used={} owned={}",
        state.id,
        unix_secs(state.created_at),
        unix_secs(state.last_used_at),
        state.owned
    )
}

fn parse_session_arg(args: &str) -> Result<String, String> {
    for part in args.split_whitespace() {
        if let Some(value) = part.strip_prefix("session=") {
            if value.is_empty() {
                return Err("session id must not be empty".to_string());
            }
            return Ok(value.to_string());
        }
    }
    Err("missing session=<id> argument".to_string())
}

fn create_session(profile: &Profile) -> Result<(), String> {
    ensure_daemon(profile)?;
    let response = send_daemon_control(profile, "session_create")?;
    let line = response
        .lines()
        .next()
        .ok_or_else(|| "daemon returned empty response".to_string())?;
    if let Some(message) = line.strip_prefix("error=") {
        return Err(message.to_string());
    }
    println!("{line}");
    Ok(())
}

fn list_sessions(profile: &Profile) -> Result<(), String> {
    if !is_daemon_ready(profile)? {
        return Ok(());
    }
    let response = send_daemon_control(profile, "session_list")?;
    print!("{response}");
    Ok(())
}

fn close_session(profile: &Profile, session_id: &str) -> Result<(), String> {
    if !is_daemon_ready(profile)? {
        return Err(format!("unknown session: {session_id}"));
    }
    let response = send_daemon_control(profile, &format!("session_close session={session_id}"))?;
    let line = response
        .lines()
        .next()
        .ok_or_else(|| "daemon returned empty response".to_string())?;
    if let Some(message) = line.strip_prefix("error=") {
        return Err(message.to_string());
    }
    println!("{line}");
    Ok(())
}

fn call_daemon(profile: &Profile, session_id: &str) -> Result<(), String> {
    ensure_daemon(profile)?;
    let socket_path = daemon_socket_path(profile)?;
    let mut stream = UnixStream::connect(&socket_path)
        .map_err(|error| format!("failed to connect {}: {error}", socket_path.display()))?;
    let mut read_stream = stream
        .try_clone()
        .map_err(|error| format!("failed to clone daemon stream: {error}"))?;
    let mut daemon_reader = BufReader::new(&mut read_stream);

    bind_session(&mut stream, &mut daemon_reader, session_id)?;

    let stdin_to_daemon = thread::spawn(move || -> Result<(), String> {
        let mut stdin = std::io::stdin().lock();
        std::io::copy(&mut stdin, &mut stream)
            .map_err(|error| format!("failed to send daemon request: {error}"))?;
        stream
            .shutdown(Shutdown::Write)
            .map_err(|error| format!("failed to close daemon request stream: {error}"))?;
        Ok(())
    });

    let mut stdout = std::io::stdout().lock();
    let mut line = String::new();
    loop {
        line.clear();
        let bytes = daemon_reader
            .read_line(&mut line)
            .map_err(|error| format!("failed to read daemon response: {error}"))?;
        if bytes == 0 {
            break;
        }
        stdout
            .write_all(line.as_bytes())
            .and_then(|_| stdout.flush())
            .map_err(|error| format!("failed to write stdout: {error}"))?;
    }

    stdin_to_daemon
        .join()
        .map_err(|_| "stdin forwarding thread panicked".to_string())??;
    Ok(())
}

fn bind_session(
    stream: &mut UnixStream,
    reader: &mut impl BufRead,
    session_id: &str,
) -> Result<(), String> {
    let request = format!("__chrome_devtools_daemon__:bind session={session_id}\n");
    stream
        .write_all(request.as_bytes())
        .and_then(|_| stream.flush())
        .map_err(|error| format!("failed to send bind request: {error}"))?;
    let mut response = String::new();
    let bytes = reader
        .read_line(&mut response)
        .map_err(|error| format!("failed to read bind response: {error}"))?;
    if bytes == 0 {
        return Err("daemon closed connection before bind response".to_string());
    }
    let response = response.trim_end();
    if let Some(message) = response.strip_prefix("error=") {
        return Err(message.to_string());
    }
    if !response.starts_with("bound=") {
        return Err(format!("unexpected bind response: {response}"));
    }
    Ok(())
}

struct BatchOptions {
    profile_name: String,
    session_id: String,
    script_path: String,
    output_path: Option<String>,
    fail_fast: bool,
}

fn run_batch(config: &Config, args: &[String]) -> Result<(), String> {
    let options = parse_batch_args(args)?;
    let profile = find_profile(config, &options.profile_name)?;
    let script_content = read_script_source(&options.script_path)?;
    let steps_value: serde_json::Value = serde_json::from_str(&script_content)
        .map_err(|error| format!("failed to parse {}: {error}", options.script_path))?;
    let steps = steps_value
        .as_array()
        .ok_or_else(|| "script must be a JSON array of steps".to_string())?;

    ensure_daemon(&profile)?;
    let socket_path = daemon_socket_path(&profile)?;
    let mut stream = UnixStream::connect(&socket_path)
        .map_err(|error| format!("failed to connect {}: {error}", socket_path.display()))?;
    let mut read_stream = stream
        .try_clone()
        .map_err(|error| format!("failed to clone daemon stream: {error}"))?;
    let mut reader = BufReader::new(&mut read_stream);

    bind_session(&mut stream, &mut reader, &options.session_id)?;

    write_json_line(
        &mut stream,
        r#"{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{"roots":{"listChanged":false}},"clientInfo":{"name":"chrome-devtools-batch","version":"0.1.0"}}}"#,
    )?;
    write_json_line(
        &mut stream,
        r#"{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}"#,
    )?;
    read_response(&mut reader, &mut stream, 1)?;

    let mut results: Vec<serde_json::Value> = Vec::new();
    let mut next_id: u64 = 2;
    let mut stopped_on_error: Option<String> = None;

    for step in steps {
        let step_type = step
            .get("type")
            .and_then(|value| value.as_str())
            .ok_or_else(|| "step missing 'type'".to_string())?;
        let label = step.get("label").cloned();
        let on_error = step
            .get("on_error")
            .and_then(|value| value.as_str())
            .map(|value| value.to_string())
            .unwrap_or_else(|| {
                if options.fail_fast {
                    "stop".to_string()
                } else {
                    "continue".to_string()
                }
            });
        match step_type {
            "sleep_ms" => {
                let ms = step
                    .get("ms")
                    .and_then(|value| value.as_u64())
                    .ok_or_else(|| "sleep_ms requires 'ms' (non-negative integer)".to_string())?;
                thread::sleep(Duration::from_millis(ms));
                results.push(serde_json::json!({
                    "type": "sleep_ms",
                    "label": label,
                    "ms": ms,
                }));
            }
            "tool" => {
                let name = step
                    .get("name")
                    .and_then(|value| value.as_str())
                    .ok_or_else(|| "tool requires 'name'".to_string())?;
                let raw_arguments = step
                    .get("args")
                    .cloned()
                    .unwrap_or_else(|| serde_json::json!({}));
                let arguments = resolve_refs(raw_arguments, &results)?;
                let id = next_id;
                next_id += 1;
                let request = serde_json::json!({
                    "jsonrpc": "2.0",
                    "id": id,
                    "method": "tools/call",
                    "params": {
                        "name": name,
                        "arguments": arguments,
                    }
                });
                write_json_line(&mut stream, &request.to_string())?;
                let response_line = read_response(&mut reader, &mut stream, id)?;
                let response_value: serde_json::Value =
                    serde_json::from_str(&response_line).unwrap_or(serde_json::Value::Null);
                let result_value = response_value.get("result").cloned();
                let error_value = response_value.get("error").cloned();
                let is_error_result = matches!(
                    result_value.as_ref().and_then(|value| value.get("isError")),
                    Some(serde_json::Value::Bool(true))
                );
                let has_error =
                    error_value.as_ref().is_some_and(|value| !value.is_null()) || is_error_result;
                results.push(serde_json::json!({
                    "type": "tool",
                    "name": name,
                    "label": label,
                    "result": result_value,
                    "error": error_value,
                }));
                if has_error && on_error == "stop" {
                    stopped_on_error = Some(
                        label
                            .as_ref()
                            .and_then(|value| value.as_str())
                            .map(|value| value.to_string())
                            .unwrap_or_else(|| name.to_string()),
                    );
                    break;
                }
            }
            other => return Err(format!("unknown step type: {other}")),
        }
    }

    let _ = stream.shutdown(Shutdown::Write);

    let output = serde_json::to_string_pretty(&serde_json::Value::Array(results))
        .map_err(|error| format!("failed to serialize results: {error}"))?;
    if let Some(path) = &options.output_path {
        fs::write(path, &output).map_err(|error| format!("failed to write {path}: {error}"))?;
    } else {
        println!("{output}");
    }

    if let Some(label) = stopped_on_error {
        return Err(format!("scenario stopped on error at step: {label}"));
    }
    Ok(())
}

fn read_script_source(path: &str) -> Result<String, String> {
    if path == "-" {
        let mut content = String::new();
        std::io::stdin()
            .read_to_string(&mut content)
            .map_err(|error| format!("failed to read script from stdin: {error}"))?;
        Ok(content)
    } else {
        fs::read_to_string(path).map_err(|error| format!("failed to read {path}: {error}"))
    }
}

fn parse_batch_args(args: &[String]) -> Result<BatchOptions, String> {
    let mut profile_name = None;
    let mut session_id = None;
    let mut script_path = None;
    let mut output_path = None;
    let mut fail_fast = false;
    let mut index = 0;
    while index < args.len() {
        match args[index].as_str() {
            "--profile" => {
                let Some(value) = args.get(index + 1) else {
                    return Err("--profile requires a value".to_string());
                };
                profile_name = Some(value.clone());
                index += 2;
            }
            "--session" => {
                let Some(value) = args.get(index + 1) else {
                    return Err("--session requires a value".to_string());
                };
                session_id = Some(value.clone());
                index += 2;
            }
            "--script" => {
                let Some(value) = args.get(index + 1) else {
                    return Err("--script requires a value".to_string());
                };
                script_path = Some(value.clone());
                index += 2;
            }
            "--output" => {
                let Some(value) = args.get(index + 1) else {
                    return Err("--output requires a value".to_string());
                };
                output_path = Some(value.clone());
                index += 2;
            }
            "--fail-fast" => {
                fail_fast = true;
                index += 1;
            }
            unknown => return Err(format!("unknown argument: {unknown}")),
        }
    }
    let profile_name = profile_name.ok_or_else(|| "--profile is required".to_string())?;
    let session_id = session_id.ok_or_else(|| "--session is required".to_string())?;
    let script_path = script_path.ok_or_else(|| "--script is required".to_string())?;
    Ok(BatchOptions {
        profile_name,
        session_id,
        script_path,
        output_path,
        fail_fast,
    })
}

/// Recursively resolve `{ "$ref": "<label>.<path>..." }` markers using `results`.
/// `path` segments are dot-separated; numeric segments index into arrays.
fn resolve_refs(
    value: serde_json::Value,
    results: &[serde_json::Value],
) -> Result<serde_json::Value, String> {
    match value {
        serde_json::Value::Object(map) if map.len() == 1 && map.contains_key("$ref") => {
            let path = map
                .get("$ref")
                .and_then(|value| value.as_str())
                .ok_or_else(|| "$ref must be a string".to_string())?;
            Ok(resolve_path(path, results))
        }
        serde_json::Value::Object(map) => {
            let mut resolved = serde_json::Map::with_capacity(map.len());
            for (key, sub) in map {
                resolved.insert(key, resolve_refs(sub, results)?);
            }
            Ok(serde_json::Value::Object(resolved))
        }
        serde_json::Value::Array(items) => {
            let mut resolved = Vec::with_capacity(items.len());
            for item in items {
                resolved.push(resolve_refs(item, results)?);
            }
            Ok(serde_json::Value::Array(resolved))
        }
        other => Ok(other),
    }
}

fn resolve_path(path: &str, results: &[serde_json::Value]) -> serde_json::Value {
    let mut parts = path.split('.');
    let Some(label) = parts.next() else {
        return serde_json::Value::Null;
    };
    let Some(entry) = results
        .iter()
        .find(|entry| entry.get("label").and_then(|value| value.as_str()) == Some(label))
    else {
        return serde_json::Value::Null;
    };
    let mut current = entry.clone();
    for part in parts {
        current = if let Ok(index) = part.parse::<usize>() {
            current
                .get(index)
                .cloned()
                .unwrap_or(serde_json::Value::Null)
        } else {
            current
                .get(part)
                .cloned()
                .unwrap_or(serde_json::Value::Null)
        };
    }
    current
}

fn find_profile(config: &Config, name: &str) -> Result<Profile, String> {
    config
        .profiles
        .iter()
        .find(|profile| profile.name == name)
        .cloned()
        .ok_or_else(|| format!("unknown profile: {name}"))
}

fn list_mcp_tools_via_daemon(profile: &Profile) -> Result<(), String> {
    ensure_daemon(profile)?;
    let request = concat!(
        r#"{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{"roots":{"listChanged":false}},"clientInfo":{"name":"chrome-devtools","version":"0.1.0"}}}"#,
        "\n",
        r#"{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}"#,
        "\n",
        r#"{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}"#,
        "\n",
    );
    let response = send_daemon_request(profile, request)?;
    if let Some(line) = response
        .lines()
        .rev()
        .find(|line| extract_jsonrpc_id(line) == Some(2))
    {
        println!("{line}");
    } else {
        print!("{response}");
    }
    Ok(())
}

fn ensure_daemon(profile: &Profile) -> Result<(), String> {
    if is_daemon_ready(profile)? {
        ensure_chrome(profile)?;
        return Ok(());
    }
    start_daemon(profile, true)
}

fn wait_for_daemon(profile: &Profile, timeout: Duration) -> Result<(), String> {
    let started = Instant::now();
    while started.elapsed() < timeout {
        if is_daemon_ready(profile)? {
            return Ok(());
        }
        thread::sleep(Duration::from_millis(250));
    }
    Err(format!(
        "daemon did not become ready within {} seconds",
        timeout.as_secs()
    ))
}

fn is_daemon_ready(profile: &Profile) -> Result<bool, String> {
    let socket_path = daemon_socket_path(profile)?;
    if !socket_path.exists() {
        return Ok(false);
    }
    match send_daemon_control(profile, "status") {
        Ok(response) => Ok(response.contains("daemon=ready")),
        Err(_) => Ok(false),
    }
}

fn print_daemon_status(profile: &Profile) -> Result<(), String> {
    let socket_path = daemon_socket_path(profile)?;
    let pid_path = daemon_pid_path(profile)?;
    let ready = is_daemon_ready(profile)?;
    let pid = read_pid_file(&pid_path)
        .map(|pid| pid.to_string())
        .unwrap_or_else(|| "unknown".to_string());
    println!(
        "profile={} daemon={} pid={} socket={}",
        profile.name,
        if ready { "ready" } else { "stopped" },
        pid,
        socket_path.display()
    );
    Ok(())
}

fn stop_daemon(profile: &Profile) -> Result<(), String> {
    if is_daemon_ready(profile)? {
        let response = send_daemon_control(profile, "stop")?;
        print!("{response}");
        wait_for_daemon_stop(profile, Duration::from_secs(5))?;
        return Ok(());
    }

    let pid_path = daemon_pid_path(profile)?;
    if let Some(pid) = read_pid_file(&pid_path) {
        let status = Command::new("kill")
            .arg(pid.to_string())
            .status()
            .map_err(|error| format!("failed to run kill: {error}"))?;
        if !status.success() {
            return Err(format!("kill exited with {status}"));
        }
    }
    cleanup_stale_daemon_files(profile)
}

fn wait_for_daemon_stop(profile: &Profile, timeout: Duration) -> Result<(), String> {
    let started = Instant::now();
    while started.elapsed() < timeout {
        if !is_daemon_ready(profile)? {
            cleanup_stale_daemon_files(profile)?;
            return Ok(());
        }
        thread::sleep(Duration::from_millis(100));
    }
    Err(format!(
        "daemon did not stop within {} seconds",
        timeout.as_secs()
    ))
}

fn send_daemon_control(profile: &Profile, command: &str) -> Result<String, String> {
    send_daemon_request(profile, &format!("__chrome_devtools_daemon__:{command}\n"))
}

fn send_daemon_request(profile: &Profile, request: &str) -> Result<String, String> {
    let socket_path = daemon_socket_path(profile)?;
    let mut stream = UnixStream::connect(&socket_path)
        .map_err(|error| format!("failed to connect {}: {error}", socket_path.display()))?;
    stream
        .write_all(request.as_bytes())
        .and_then(|_| stream.shutdown(Shutdown::Write))
        .map_err(|error| format!("failed to send daemon request: {error}"))?;
    let mut response = String::new();
    stream
        .read_to_string(&mut response)
        .map_err(|error| format!("failed to read daemon response: {error}"))?;
    Ok(response)
}

fn read_pid_file(path: &Path) -> Option<u32> {
    fs::read_to_string(path).ok()?.trim().parse().ok()
}

fn cleanup_stale_daemon_files(profile: &Profile) -> Result<(), String> {
    let pid_path = daemon_pid_path(profile)?;
    if let Some(pid) = read_pid_file(&pid_path) {
        if process_exists(pid) {
            return Ok(());
        }
    }
    let socket_path = daemon_socket_path(profile)?;
    let port_path = daemon_port_path(profile)?;
    let _ = fs::remove_file(socket_path);
    let _ = fs::remove_file(pid_path);
    let _ = fs::remove_file(port_path);
    Ok(())
}

fn reject_extra_args(args: &[String]) -> Result<(), String> {
    if args.is_empty() {
        Ok(())
    } else {
        Err(format!("unknown argument: {}", args[0]))
    }
}

fn require_profile(config: &Config, args: &[String]) -> Result<Profile, String> {
    let mut profile_name = None;
    let mut index = 0;

    while index < args.len() {
        match args[index].as_str() {
            "--profile" => {
                let Some(value) = args.get(index + 1) else {
                    return Err("--profile requires a value".to_string());
                };
                profile_name = Some(value.as_str());
                index += 2;
            }
            unknown => return Err(format!("unknown argument: {unknown}")),
        }
    }

    let Some(profile_name) = profile_name else {
        return Err("--profile is required".to_string());
    };

    config
        .profiles
        .iter()
        .find(|profile| profile.name == profile_name)
        .cloned()
        .ok_or_else(|| format!("unknown profile: {profile_name}"))
}

fn require_profile_and_session(
    config: &Config,
    args: &[String],
) -> Result<(Profile, String), String> {
    let mut profile_name = None;
    let mut session_id = None;
    let mut index = 0;

    while index < args.len() {
        match args[index].as_str() {
            "--profile" => {
                let Some(value) = args.get(index + 1) else {
                    return Err("--profile requires a value".to_string());
                };
                profile_name = Some(value.clone());
                index += 2;
            }
            "--session" => {
                let Some(value) = args.get(index + 1) else {
                    return Err("--session requires a value".to_string());
                };
                session_id = Some(value.clone());
                index += 2;
            }
            unknown => return Err(format!("unknown argument: {unknown}")),
        }
    }

    let profile_name = profile_name.ok_or_else(|| "--profile is required".to_string())?;
    let session_id = session_id.ok_or_else(|| "--session is required".to_string())?;
    let profile = config
        .profiles
        .iter()
        .find(|profile| profile.name == profile_name)
        .cloned()
        .ok_or_else(|| format!("unknown profile: {profile_name}"))?;
    Ok((profile, session_id))
}

fn list_profiles(config: &Config) {
    for profile in &config.profiles {
        println!("{}\tuser_data_dir={}", profile.name, profile.user_data_dir);
    }
}

fn print_status(profile: &Profile) {
    match current_port(profile) {
        Some(port) if is_devtools_ready(port) => {
            println!(
                "profile={} status=ready port={} user_data_dir={}",
                profile.name, port, profile.user_data_dir
            );
        }
        _ => {
            println!(
                "profile={} status=stopped user_data_dir={}",
                profile.name, profile.user_data_dir
            );
        }
    }
}

fn default_chrome_binary() -> String {
    #[cfg(target_os = "macos")]
    {
        for candidate in [
            "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
            "/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta",
            "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
            "/Applications/Chromium.app/Contents/MacOS/Chromium",
        ] {
            if std::path::Path::new(candidate).exists() {
                return candidate.to_string();
            }
        }
    }
    "google-chrome-stable".to_string()
}

fn ensure_chrome(profile: &Profile) -> Result<(), String> {
    if let Some(port) = read_runtime_port(profile) {
        if is_devtools_ready(port) {
            return Ok(());
        }
    }

    if let Some(port) = find_running_chrome_port(profile) {
        if is_devtools_ready(port) {
            write_runtime_port(profile, port)?;
            return Ok(());
        }
    }

    let port = pick_free_port()?;
    let chrome = env::var("CHROME").unwrap_or_else(|_| default_chrome_binary());
    let user_data_dir = expand_home(&profile.user_data_dir)?;

    Command::new(chrome)
        .arg("--remote-debugging-address=127.0.0.1")
        .arg(format!("--remote-debugging-port={port}"))
        .arg(format!("--user-data-dir={}", user_data_dir.display()))
        .arg("--no-first-run")
        .arg("--no-default-browser-check")
        .arg("--disable-gpu")
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .map_err(|error| format!("failed to start Chrome: {error}"))?;

    wait_for_devtools(port, Duration::from_secs(30))?;
    write_runtime_port(profile, port)
}

fn exec_mcp(profile: &Profile) -> Result<(), String> {
    let status = mcp_command(profile)
        .stdin(Stdio::inherit())
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .status()
        .map_err(|error| format!("failed to run chrome-devtools-mcp: {error}"))?;

    if status.success() {
        Ok(())
    } else {
        Err(format!("chrome-devtools-mcp exited with {status}"))
    }
}

fn list_mcp_tools(profile: &Profile) -> Result<(), String> {
    let mut child = mcp_command(profile)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::inherit())
        .spawn()
        .map_err(|error| format!("failed to run chrome-devtools-mcp: {error}"))?;

    let mut stdin = child
        .stdin
        .take()
        .ok_or_else(|| "failed to open chrome-devtools-mcp stdin".to_string())?;
    let stdout = child
        .stdout
        .take()
        .ok_or_else(|| "failed to open chrome-devtools-mcp stdout".to_string())?;
    let mut reader = BufReader::new(stdout);

    write_json_line(
        &mut stdin,
        r#"{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{"roots":{"listChanged":false}},"clientInfo":{"name":"chrome-devtools","version":"0.1.0"}}}"#,
    )?;
    read_response(&mut reader, &mut stdin, 1)?;

    write_json_line(
        &mut stdin,
        r#"{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}"#,
    )?;
    write_json_line(
        &mut stdin,
        r#"{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}"#,
    )?;

    let tools = read_response(&mut reader, &mut stdin, 2)?;
    println!("{tools}");

    terminate_child(&mut child);
    Ok(())
}

fn mcp_command(profile: &Profile) -> Command {
    let mut command = if let Ok(program) = env::var("CHROME_DEVTOOLS_MCP_COMMAND") {
        Command::new(program)
    } else {
        let mut command = Command::new("npx");
        command.arg("-y").arg("chrome-devtools-mcp@latest");
        command
    };
    let port = current_port(profile)
        .expect("ensure_chrome must run before mcp_command so the runtime port is recorded");
    command
        .arg("--browser-url")
        .arg(format!("http://127.0.0.1:{port}"))
        .arg("--no-usage-statistics")
        .arg("--no-performance-crux");
    command
}

fn write_json_line(stdin: &mut impl Write, json: &str) -> Result<(), String> {
    stdin
        .write_all(json.as_bytes())
        .and_then(|_| stdin.write_all(b"\n"))
        .and_then(|_| stdin.flush())
        .map_err(|error| format!("failed to write MCP request: {error}"))
}

fn read_response(
    reader: &mut impl BufRead,
    stdin: &mut impl Write,
    target_id: u64,
) -> Result<String, String> {
    loop {
        let mut line = String::new();
        let bytes = reader
            .read_line(&mut line)
            .map_err(|error| format!("failed to read MCP response: {error}"))?;
        if bytes == 0 {
            return Err("chrome-devtools-mcp closed stdout before responding".to_string());
        }

        let line = line.trim_end().to_string();
        if json_has_method(&line, "roots/list") {
            if let Some(id) = extract_jsonrpc_id(&line) {
                let response = format!(r#"{{"jsonrpc":"2.0","id":{id},"result":{{"roots":[]}}}}"#);
                write_json_line(stdin, &response)?;
            }
            continue;
        }

        if extract_jsonrpc_id(&line) == Some(target_id) {
            return Ok(line);
        }
    }
}

fn daemon_initialize_response(id: u64) -> String {
    format!(
        r#"{{"jsonrpc":"2.0","id":{id},"result":{{"protocolVersion":"2025-06-18","capabilities":{{}},"serverInfo":{{"name":"chrome-devtools-daemon","version":"0.1.0"}}}}}}"#
    )
}

fn sanitize_outgoing_request(line: &str) -> String {
    let Ok(mut value) = serde_json::from_str::<serde_json::Value>(line) else {
        return line.to_string();
    };
    let Some(obj) = value.as_object_mut() else {
        return line.to_string();
    };
    let method_is_tools_call = obj
        .get("method")
        .and_then(|m| m.as_str())
        .map(|m| m == "tools/call")
        .unwrap_or(false);
    if !method_is_tools_call {
        return line.to_string();
    }
    let Some(params) = obj.get_mut("params").and_then(|p| p.as_object_mut()) else {
        return line.to_string();
    };
    let name_is_new_page = params
        .get("name")
        .and_then(|n| n.as_str())
        .map(|n| n == "new_page")
        .unwrap_or(false);
    if !name_is_new_page {
        return line.to_string();
    }
    let Some(args) = params.get_mut("arguments").and_then(|a| a.as_object_mut()) else {
        return line.to_string();
    };
    if args.remove("isolatedContext").is_none() {
        return line.to_string();
    }
    eprintln!(
        "warning: stripped 'isolatedContext' from new_page; isolated browser contexts disable extensions"
    );
    serde_json::to_string(&value).unwrap_or_else(|_| line.to_string())
}

fn json_has_method(line: &str, method: &str) -> bool {
    compact_json_line(line).contains(&format!(r#""method":"{method}""#))
}

fn compact_json_line(line: &str) -> String {
    line.chars()
        .filter(|character| !character.is_whitespace())
        .collect()
}

fn extract_jsonrpc_id(line: &str) -> Option<u64> {
    let compact = compact_json_line(line);
    let marker = r#""id":"#;
    let start = compact.find(marker)? + marker.len();
    let rest = &compact[start..];
    let digits = rest
        .chars()
        .take_while(|character| character.is_ascii_digit())
        .collect::<String>();
    if digits.is_empty() {
        None
    } else {
        digits.parse().ok()
    }
}

fn terminate_child(child: &mut Child) {
    let _ = child.kill();
    let _ = child.wait();
}

fn find_running_chrome_port(profile: &Profile) -> Option<u16> {
    let user_data_dir = expand_home(&profile.user_data_dir).ok()?;
    let needle = format!("--user-data-dir={}", user_data_dir.display());

    for cmdline in list_browser_cmdlines() {
        if cmdline.contains("--type=") {
            continue;
        }
        if !cmdline.contains(&needle) {
            continue;
        }
        if let Some(port) = extract_remote_debugging_port(&cmdline) {
            return Some(port);
        }
    }
    None
}

#[cfg(target_os = "linux")]
fn list_browser_cmdlines() -> Vec<String> {
    let mut out = Vec::new();
    let Ok(entries) = std::fs::read_dir("/proc") else {
        return out;
    };
    for entry in entries.flatten() {
        let Ok(name) = entry.file_name().into_string() else {
            continue;
        };
        if !name.chars().all(|c| c.is_ascii_digit()) {
            continue;
        }
        let Ok(bytes) = std::fs::read(entry.path().join("cmdline")) else {
            continue;
        };
        let cmdline: String = bytes
            .iter()
            .map(|&b| if b == 0 { ' ' } else { b as char })
            .collect();
        out.push(cmdline);
    }
    out
}

#[cfg(target_os = "macos")]
fn list_browser_cmdlines() -> Vec<String> {
    let Ok(output) = Command::new("ps").args(["-ax", "-o", "command="]).output() else {
        return Vec::new();
    };
    String::from_utf8_lossy(&output.stdout)
        .lines()
        .map(|s| s.to_string())
        .collect()
}

#[cfg(not(any(target_os = "linux", target_os = "macos")))]
fn list_browser_cmdlines() -> Vec<String> {
    Vec::new()
}

fn extract_remote_debugging_port(cmdline: &str) -> Option<u16> {
    let prefix = "--remote-debugging-port=";
    let start = cmdline.find(prefix)? + prefix.len();
    let rest = &cmdline[start..];
    let end = rest.find(|c: char| c.is_whitespace()).unwrap_or(rest.len());
    rest[..end].parse().ok()
}

fn stop_profile(profile: &Profile) -> Result<(), String> {
    let user_data_dir = expand_home(&profile.user_data_dir)?;
    let pattern = format!("--user-data-dir={}", user_data_dir.display());

    let status = Command::new("pkill")
        .arg("-f")
        .arg("--")
        .arg(&pattern)
        .status()
        .map_err(|error| format!("failed to run pkill: {error}"))?;

    if status.success() || status.code() == Some(1) {
        Ok(())
    } else {
        Err(format!("pkill exited with {status}"))
    }
}

fn wait_for_devtools(port: u16, timeout: Duration) -> Result<(), String> {
    let started = Instant::now();

    while started.elapsed() < timeout {
        if is_devtools_ready(port) {
            return Ok(());
        }
        thread::sleep(Duration::from_millis(250));
    }

    Err(format!(
        "Chrome DevTools did not become ready on port {port} within {} seconds",
        timeout.as_secs()
    ))
}

fn is_devtools_ready(port: u16) -> bool {
    let address = SocketAddr::from(([127, 0, 0, 1], port));
    let Ok(mut stream) = TcpStream::connect_timeout(&address, Duration::from_millis(250)) else {
        return false;
    };

    let _ = stream.set_read_timeout(Some(Duration::from_millis(500)));
    let _ = stream.set_write_timeout(Some(Duration::from_millis(500)));

    let request = b"GET /json/version HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n";
    if stream.write_all(request).is_err() {
        return false;
    }

    let mut response = [0; 4096];
    let Ok(bytes) = stream.read(&mut response) else {
        return false;
    };

    String::from_utf8_lossy(&response[..bytes]).contains("200 OK")
}

fn expand_home(path: &str) -> Result<PathBuf, String> {
    if path == "~" {
        return env::var_os("HOME")
            .map(PathBuf::from)
            .ok_or_else(|| "HOME is not set".to_string());
    }

    if let Some(rest) = path.strip_prefix("~/") {
        let Some(home) = env::var_os("HOME") else {
            return Err("HOME is not set".to_string());
        };
        return Ok(PathBuf::from(home).join(rest));
    }

    Ok(PathBuf::from(path))
}

fn print_usage() {
    eprintln!(
        "Usage:\n  chrome-devtools --version\n  chrome-devtools mcp list --profile <profile>\n  chrome-devtools mcp call --profile <profile> --session <id>\n  chrome-devtools mcp batch --profile <profile> --session <id> --script <path>\n  chrome-devtools mcp direct-list --profile <profile>\n  chrome-devtools mcp direct-call --profile <profile>\n  chrome-devtools mcp help\n  chrome-devtools session create --profile <profile>\n  chrome-devtools session list --profile <profile>\n  chrome-devtools session close --profile <profile> --session <id>\n  chrome-devtools daemon start --profile <profile>\n  chrome-devtools daemon status --profile <profile>\n  chrome-devtools daemon stop --profile <profile>\n  chrome-devtools profile status --profile <profile>\n  chrome-devtools profile stop --profile <profile>\n  chrome-devtools profile list\n\nConfig:\n  ~/.config/chrome-devtools/config.toml is created on startup if missing.\n\nConcurrency:\n  MCP commands take a per-profile lock under ~/.cache/chrome-devtools/locks.\n  Set CHROME_DEVTOOLS_LOCK_TIMEOUT_SECS to override the default 300 second wait."
    );
}

fn print_version() {
    println!("chrome-devtools {}", env!("CARGO_PKG_VERSION"));
}

fn print_mcp_help() {
    println!(
        "chrome-devtools mcp\n\nUsage:\n  chrome-devtools mcp list --profile <profile>\n  chrome-devtools mcp call --profile <profile> --session <id>\n  chrome-devtools mcp batch --profile <profile> --session <id> --script <path>\n  chrome-devtools mcp direct-list --profile <profile>\n  chrome-devtools mcp direct-call --profile <profile>\n  chrome-devtools mcp help\n\nCommands:\n  list         Start the selected profile daemon if needed, query tools/list through it, and print the raw MCP JSON response.\n\n  call         Start the selected profile daemon if needed, bind the named session, and forward stdin MCP JSON-RPC lines through its long-lived MCP process.\n\n  batch        Bind the named session and run a JSON batch file of tool/sleep steps through the profile daemon. Prints a JSON array of results.\n\n  direct-list  Bypass the daemon, run chrome-devtools-mcp directly, query tools/list, and print the raw MCP JSON response.\n\n  direct-call  Bypass the daemon, run chrome-devtools-mcp directly over stdio. Use only for fallback/manual debugging.\n\n  help         Show this help.\n\nOptions:\n  -h, --help   Show this help and exit.\n\nExamples:\n  chrome-devtools daemon start --profile default\n  chrome-devtools mcp list --profile default\n  ID=$(chrome-devtools session create --profile default | awk -F= '{{print $2}}' | awk '{{print $1}}')\n  chrome-devtools mcp call --profile default --session \"$ID\"\n  chrome-devtools mcp batch --profile default --session \"$ID\" --script /tmp/batch.json\n\nConfig:\n  Profiles are read from ~/.config/chrome-devtools/config.toml.\n  If the config file is missing, chrome-devtools creates a default profile using ~/.config/chrome-devtools/profiles/default.\n  user_data_dir is optional; when omitted, it defaults to ~/.config/chrome-devtools/profiles/<profile-name>.\n  Prefer user_data_dir values under ~/.config/chrome-devtools/profiles/<profile-name>.\n\nSessions:\n  mcp call and mcp batch require --session <id>. Mint one with `session create`.\n  Sessions live in-memory on the daemon and expire after 30 minutes of inactivity.\n\nDaemon:\n  mcp call, mcp list and mcp batch route through one long-lived per-profile daemon by default.\n  Daemon sockets and pid files live under ~/.cache/chrome-devtools/daemons.\n  direct-call and direct-list bypass the daemon and take a per-profile lock under ~/.cache/chrome-devtools/locks.\n  Set CHROME_DEVTOOLS_LOCK_TIMEOUT_SECS to override the direct-mode/default daemon lock wait.\n\nNotes:\n  Profiles define the Chrome user data directory. The daemon picks a free DevTools port automatically.\n  The call/batch commands do not reimplement MCP tools; they delegate to a daemon-owned chrome-devtools-mcp process.\n  Snapshot uid values are only valid inside the MCP process that produced them.\n  Daemon-routed calls preserve that MCP process across invocations until the daemon stops."
    );
}

fn print_profile_help() {
    println!(
        "chrome-devtools profile\n\nUsage:\n  chrome-devtools profile list\n  chrome-devtools profile status --profile <profile>\n  chrome-devtools profile stop --profile <profile>\n\nCommands:\n  list    List all profiles defined in the config file.\n  status  Show whether the Chrome instance bound to the given profile is running.\n  stop    Stop the Chrome instance bound to the given profile.\n\nOptions:\n  -h, --help  Show this help and exit."
    );
}

fn print_daemon_help() {
    println!(
        "chrome-devtools daemon\n\nUsage:\n  chrome-devtools daemon start --profile <profile>\n  chrome-devtools daemon status --profile <profile>\n  chrome-devtools daemon stop --profile <profile>\n\nCommands:\n  start   Start a background daemon for the profile, or report that one is already ready.\n  status  Show whether the per-profile daemon is ready, along with its pid and socket path.\n  stop    Ask the per-profile daemon to stop and clean up its socket/pid files.\n\nOptions:\n  -h, --help  Show this help and exit.\n\nNotes:\n  Daemon metadata lives under ~/.cache/chrome-devtools/daemons."
    );
}

fn print_mcp_call_help() {
    println!(
        "chrome-devtools mcp call\n\nUsage:\n  chrome-devtools mcp call --profile <profile> --session <id>\n\nDescription:\n  Start the selected profile daemon if needed, bind the named session, then\n  forward stdin MCP JSON-RPC lines through its long-lived chrome-devtools-mcp\n  process and print responses. The session is held for the lifetime of this\n  invocation; activity refreshes its 30 minute idle timer.\n\nOptions:\n  --profile <name>  Required. Profile name from ~/.config/chrome-devtools/config.toml.\n  --session <id>    Required. Session id minted by `session create`.\n  -h, --help        Show this help and exit.\n\nExamples:\n  ID=$(chrome-devtools session create --profile default | awk -F= '{{print $2}}' | awk '{{print $1}}')\n  printf '%s\\n' '{{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/list\",\"params\":{{}}}}' \\\n    | chrome-devtools mcp call --profile default --session \"$ID\""
    );
}

fn print_mcp_batch_help() {
    println!(
        "chrome-devtools mcp batch\n\nUsage:\n  chrome-devtools mcp batch --profile <profile> --session <id> --script <path> [--output <path>] [--fail-fast]\n\nDescription:\n  Read a JSON array of steps from --script, bind the named session, and\n  execute each step in order through the profile daemon (one initialize\n  handshake, then a tools/call per tool step). Prints a JSON array of\n  results to stdout. Activity refreshes the session's 30 minute idle timer.\n\nStep shapes:\n  {{\"type\":\"tool\",\"name\":\"<mcp-tool>\",\"args\":{{...}},\"label\":\"<optional>\",\"on_error\":\"continue|stop\"}}\n  {{\"type\":\"sleep_ms\",\"ms\":<u64>,\"label\":\"<optional>\"}}\n\nValue references inside args:\n  Replace any value in args with {{\"$ref\":\"<label>.<path>\"}} to substitute it\n  with a previous result. <path> is dot-separated; numeric segments index\n  arrays. Example: {{\"$ref\":\"snap.result.content.0.text\"}} resolves to the\n  text of the first content entry returned by the step labelled 'snap'.\n\nResult shape (per step):\n  {{\"type\":\"tool\",\"name\":\"...\",\"label\":\"...\",\"result\":<mcp tools/call result>,\"error\":<mcp error or null>}}\n  {{\"type\":\"sleep_ms\",\"ms\":<u64>,\"label\":\"...\"}}\n\nError handling:\n  A tool step is considered to have errored if the MCP response carries a\n  non-null 'error' field or the result has isError=true. By default the\n  batch continues; pass --fail-fast or set on_error=stop on a step to\n  stop execution after that error. When stopped, batch writes the partial\n  results to stdout/--output and exits non-zero.\n\nOptions:\n  --profile <name>  Required. Profile name from ~/.config/chrome-devtools/config.toml.\n  --session <id>    Required. Session id minted by `session create`.\n  --script <path>   Required. Path to a JSON file with the step array, or `-` for stdin.\n  --output <path>   Optional. Write the JSON results to <path> instead of stdout.\n  --fail-fast       Optional. Stop on the first errored tool step.\n  -h, --help        Show this help and exit.\n\nExamples:\n  cat > /tmp/batch.json <<'EOF'\n  [\n    {{\"type\":\"tool\",\"name\":\"navigate_page\",\"args\":{{\"type\":\"reload\",\"timeout\":15000}}}},\n    {{\"type\":\"sleep_ms\",\"ms\":5000}},\n    {{\"type\":\"tool\",\"name\":\"evaluate_script\",\"label\":\"title\",\"args\":{{\"function\":\"() => document.title\"}}}}\n  ]\n  EOF\n  ID=$(chrome-devtools session create --profile default | awk -F= '{{print $2}}' | awk '{{print $1}}')\n  chrome-devtools mcp batch --profile default --session \"$ID\" --script /tmp/batch.json"
    );
}

fn print_mcp_list_help() {
    println!(
        "chrome-devtools mcp list\n\nUsage:\n  chrome-devtools mcp list --profile <profile>\n\nDescription:\n  Start the selected profile daemon if needed, query tools/list through it,\n  and print the raw MCP JSON response.\n\nOptions:\n  --profile <name>  Required. Profile name from ~/.config/chrome-devtools/config.toml.\n  -h, --help        Show this help and exit."
    );
}

fn print_mcp_direct_call_help() {
    println!(
        "chrome-devtools mcp direct-call\n\nUsage:\n  chrome-devtools mcp direct-call --profile <profile>\n\nDescription:\n  Bypass the daemon and run chrome-devtools-mcp directly over stdio.\n  Use only for fallback/manual debugging; this mode cannot preserve snapshot\n  state across independent process invocations.\n\nOptions:\n  --profile <name>  Required. Profile name from ~/.config/chrome-devtools/config.toml.\n  -h, --help        Show this help and exit.\n\nNotes:\n  Acquires a per-profile lock under ~/.cache/chrome-devtools/locks.\n  Set CHROME_DEVTOOLS_LOCK_TIMEOUT_SECS to override the 300 second wait."
    );
}

fn print_mcp_direct_list_help() {
    println!(
        "chrome-devtools mcp direct-list\n\nUsage:\n  chrome-devtools mcp direct-list --profile <profile>\n\nDescription:\n  Bypass the daemon, run chrome-devtools-mcp directly, query tools/list, and\n  print the raw MCP JSON response.\n\nOptions:\n  --profile <name>  Required. Profile name from ~/.config/chrome-devtools/config.toml.\n  -h, --help        Show this help and exit.\n\nNotes:\n  Acquires a per-profile lock under ~/.cache/chrome-devtools/locks."
    );
}

fn print_profile_status_help() {
    println!(
        "chrome-devtools profile status\n\nUsage:\n  chrome-devtools profile status --profile <profile>\n\nDescription:\n  Show whether the Chrome DevTools endpoint for the given profile is reachable,\n  along with its runtime port and user_data_dir.\n\nOptions:\n  --profile <name>  Required. Profile name from ~/.config/chrome-devtools/config.toml.\n  -h, --help        Show this help and exit."
    );
}

fn print_profile_stop_help() {
    println!(
        "chrome-devtools profile stop\n\nUsage:\n  chrome-devtools profile stop --profile <profile>\n\nDescription:\n  Stop the Chrome instance bound to the given profile by matching processes\n  whose command line contains --user-data-dir=<profile user_data_dir>.\n\nOptions:\n  --profile <name>  Required. Profile name from ~/.config/chrome-devtools/config.toml.\n  -h, --help        Show this help and exit."
    );
}

fn print_profile_list_help() {
    println!(
        "chrome-devtools profile list\n\nUsage:\n  chrome-devtools profile list\n\nDescription:\n  Print all profiles defined in ~/.config/chrome-devtools/config.toml, one per\n  line, as: <name>\\tuser_data_dir=<path>.\n\nOptions:\n  -h, --help  Show this help and exit."
    );
}

fn print_daemon_start_help() {
    println!(
        "chrome-devtools daemon start\n\nUsage:\n  chrome-devtools daemon start --profile <profile>\n\nDescription:\n  Start a background daemon for the profile if one is not already running.\n  The daemon owns one chrome-devtools-mcp process and serializes MCP calls\n  over a Unix socket under ~/.cache/chrome-devtools/daemons.\n\nOptions:\n  --profile <name>  Required. Profile name from ~/.config/chrome-devtools/config.toml.\n  -h, --help        Show this help and exit."
    );
}

fn print_daemon_run_help() {
    println!(
        "chrome-devtools daemon run\n\nUsage:\n  chrome-devtools daemon run --profile <profile>\n\nDescription:\n  Run the per-profile broker in the foreground. This subcommand is normally\n  spawned by `daemon start` and is not intended to be invoked directly.\n\nOptions:\n  --profile <name>  Required. Profile name from ~/.config/chrome-devtools/config.toml.\n  -h, --help        Show this help and exit."
    );
}

fn print_daemon_status_help() {
    println!(
        "chrome-devtools daemon status\n\nUsage:\n  chrome-devtools daemon status --profile <profile>\n\nDescription:\n  Print whether the per-profile daemon is ready or stopped, with its pid and\n  Unix socket path.\n\nOptions:\n  --profile <name>  Required. Profile name from ~/.config/chrome-devtools/config.toml.\n  -h, --help        Show this help and exit."
    );
}

fn print_daemon_stop_help() {
    println!(
        "chrome-devtools daemon stop\n\nUsage:\n  chrome-devtools daemon stop --profile <profile>\n\nDescription:\n  Ask the per-profile daemon to stop and clean up its socket and pid files.\n  If the daemon is unreachable but a pid file exists, fall back to sending it\n  a TERM signal via kill.\n\nOptions:\n  --profile <name>  Required. Profile name from ~/.config/chrome-devtools/config.toml.\n  -h, --help        Show this help and exit."
    );
}

fn print_session_help() {
    println!(
        "chrome-devtools session\n\nUsage:\n  chrome-devtools session create --profile <profile>\n  chrome-devtools session list --profile <profile>\n  chrome-devtools session close --profile <profile> --session <id>\n\nCommands:\n  create  Mint a new session id on the profile daemon.\n  list    List active sessions held by the profile daemon.\n  close   Close (drop) the named session.\n\nOptions:\n  -h, --help  Show this help and exit.\n\nNotes:\n  Sessions live in-memory on the profile daemon. They are dropped after\n  30 minutes of inactivity or when the daemon stops.\n  mcp call and mcp batch require --session <id>; use session create to mint it."
    );
}

fn print_session_create_help() {
    println!(
        "chrome-devtools session create\n\nUsage:\n  chrome-devtools session create --profile <profile>\n\nDescription:\n  Start the profile daemon if needed, then ask it to mint a new in-memory\n  session id. The session is dropped after 30 minutes of inactivity or when\n  the daemon stops. Prints one line to stdout:\n\n    session=<id> created=<unix-ts> last_used=<unix-ts> owned=false\n\nOptions:\n  --profile <name>  Required. Profile name from ~/.config/chrome-devtools/config.toml.\n  -h, --help        Show this help and exit."
    );
}

fn print_session_list_help() {
    println!(
        "chrome-devtools session list\n\nUsage:\n  chrome-devtools session list --profile <profile>\n\nDescription:\n  List active sessions held by the profile daemon. Each session is printed\n  on one line as:\n\n    session=<id> created=<unix-ts> last_used=<unix-ts> owned=<true|false>\n\n  Prints nothing when the daemon is not running.\n\nOptions:\n  --profile <name>  Required. Profile name from ~/.config/chrome-devtools/config.toml.\n  -h, --help        Show this help and exit."
    );
}

fn print_session_close_help() {
    println!(
        "chrome-devtools session close\n\nUsage:\n  chrome-devtools session close --profile <profile> --session <id>\n\nDescription:\n  Ask the profile daemon to drop the named session. Fails if the session is\n  unknown or the daemon is not running.\n\nOptions:\n  --profile <name>  Required. Profile name from ~/.config/chrome-devtools/config.toml.\n  --session <id>    Required. Session id minted by `session create`.\n  -h, --help        Show this help and exit."
    );
}

#[cfg(test)]
mod tests {
    use super::*;

    fn args(values: &[&str]) -> Vec<String> {
        values.iter().map(|value| value.to_string()).collect()
    }

    #[test]
    fn parse_config_accepts_single_minimal_profile() {
        let config = parse_config("[[profiles]]\nname = \"default\"\n").unwrap();
        assert_eq!(config.profiles.len(), 1);
        let profile = &config.profiles[0];
        assert_eq!(profile.name, "default");
        assert_eq!(
            profile.user_data_dir,
            "~/.config/chrome-devtools/profiles/default"
        );
    }

    #[test]
    fn parse_config_accepts_explicit_user_data_dir() {
        let config =
            parse_config("[[profiles]]\nname = \"work\"\nuser_data_dir = \"/tmp/work\"\n").unwrap();
        assert_eq!(config.profiles[0].user_data_dir, "/tmp/work");
    }

    #[test]
    fn parse_config_ignores_deprecated_port_field() {
        let config = parse_config("[[profiles]]\nname = \"legacy\"\nport = 9222\n").unwrap();
        assert_eq!(config.profiles[0].name, "legacy");
    }

    #[test]
    fn parse_config_accepts_multiple_profiles_with_comments() {
        let toml = "
            # leading comment
            [[profiles]]
            name = \"a\"

            [[profiles]]
            name = \"b\"
        ";
        let config = parse_config(toml).unwrap();
        let names: Vec<_> = config
            .profiles
            .iter()
            .map(|profile| &profile.name)
            .collect();
        assert_eq!(names, vec!["a", "b"]);
    }

    #[test]
    fn parse_config_rejects_empty_config() {
        assert!(parse_config("").is_err());
    }

    #[test]
    fn parse_config_rejects_duplicate_profile_names() {
        let toml = "[[profiles]]\nname = \"a\"\n[[profiles]]\nname = \"a\"\n";
        let error = parse_config(toml).unwrap_err();
        assert!(error.contains("duplicate profile name"));
    }

    #[test]
    fn parse_config_rejects_field_outside_profile_block() {
        assert!(parse_config("name = \"x\"\n").is_err());
    }

    #[test]
    fn parse_config_rejects_unknown_key() {
        let toml = "[[profiles]]\nname = \"a\"\nweird = \"x\"\n";
        assert!(parse_config(toml).is_err());
    }

    #[test]
    fn parse_toml_string_handles_escapes() {
        assert_eq!(
            parse_toml_string("\"line\\nbreak\"", 1).unwrap(),
            "line\nbreak"
        );
        assert_eq!(parse_toml_string("\"a\\\\b\"", 1).unwrap(), "a\\b");
        assert_eq!(parse_toml_string("\"a\\\"b\"", 1).unwrap(), "a\"b");
    }

    #[test]
    fn parse_toml_string_rejects_unquoted() {
        assert!(parse_toml_string("foo", 1).is_err());
    }

    #[test]
    fn parse_toml_string_rejects_unknown_escape() {
        assert!(parse_toml_string("\"\\q\"", 1).is_err());
    }

    #[test]
    fn safe_lock_name_keeps_allowed_characters() {
        assert_eq!(safe_lock_name("abc_def-123"), "abc_def-123");
    }

    #[test]
    fn safe_lock_name_replaces_disallowed_characters() {
        assert_eq!(safe_lock_name("a/b c.d"), "a_b_c_d");
    }

    #[test]
    fn parse_lock_pid_extracts_pid_line() {
        assert_eq!(parse_lock_pid("pid=4242\nprofile=default\n"), Some(4242));
        assert_eq!(parse_lock_pid("profile=default\npid=7\n"), Some(7));
    }

    #[test]
    fn parse_lock_pid_returns_none_without_pid() {
        assert_eq!(parse_lock_pid("profile=default\n"), None);
    }

    #[test]
    fn compact_json_line_strips_whitespace() {
        assert_eq!(compact_json_line(" { \"id\" : 1 } "), "{\"id\":1}");
    }

    #[test]
    fn extract_jsonrpc_id_reads_numeric_id() {
        assert_eq!(
            extract_jsonrpc_id(r#"{"jsonrpc":"2.0","id":42,"result":{}}"#),
            Some(42)
        );
        assert_eq!(extract_jsonrpc_id(r#"{ "id" : 7 }"#), Some(7));
    }

    #[test]
    fn extract_jsonrpc_id_returns_none_when_missing() {
        assert_eq!(extract_jsonrpc_id(r#"{"result":{}}"#), None);
    }

    #[test]
    fn json_has_method_detects_method() {
        assert!(json_has_method(
            r#"{"jsonrpc":"2.0","method":"initialize","params":{}}"#,
            "initialize"
        ));
        assert!(!json_has_method(
            r#"{"jsonrpc":"2.0","method":"tools/list"}"#,
            "initialize"
        ));
    }

    #[test]
    fn parse_session_arg_accepts_session_assignment() {
        assert_eq!(parse_session_arg("session=abc").unwrap(), "abc");
        assert_eq!(parse_session_arg("session=foo other=bar").unwrap(), "foo");
    }

    #[test]
    fn parse_session_arg_rejects_missing_session() {
        assert!(parse_session_arg("").is_err());
        assert!(parse_session_arg("other=bar").is_err());
        assert!(parse_session_arg("session=").is_err());
    }

    #[test]
    fn format_session_line_renders_all_fields() {
        let now = SystemTime::now();
        let state = SessionState {
            id: "sess-test".to_string(),
            created_at: now,
            last_used_at: now,
            owned: true,
        };
        let line = format_session_line(&state);
        assert!(line.starts_with("session=sess-test "));
        assert!(line.contains(" owned=true"));
        assert!(line.contains(" created="));
        assert!(line.contains(" last_used="));
    }

    #[test]
    fn generate_session_id_produces_unique_ids() {
        let mut ids = std::collections::HashSet::new();
        for _ in 0..100 {
            assert!(ids.insert(generate_session_id()));
        }
    }

    #[test]
    fn session_registry_create_and_list() {
        let mut registry = SessionRegistry::default();
        let first = registry.create();
        let second = registry.create();
        assert_ne!(first.id, second.id);
        let listed = registry.list();
        assert_eq!(listed.len(), 2);
        assert!(listed[0].created_at <= listed[1].created_at);
    }

    #[test]
    fn session_registry_close_removes_known_and_errors_on_unknown() {
        let mut registry = SessionRegistry::default();
        let state = registry.create();
        assert!(registry.close(&state.id).is_ok());
        assert!(registry.list().is_empty());
        let error = registry.close("sess-missing").unwrap_err();
        assert!(error.contains("unknown session"));
    }

    #[test]
    fn session_registry_bind_marks_owned_and_rejects_second_bind() {
        let mut registry = SessionRegistry::default();
        let state = registry.create();
        assert!(registry.bind(&state.id).is_ok());
        assert!(registry
            .sessions
            .get(&state.id)
            .map(|session| session.owned)
            .unwrap_or(false));
        let error = registry.bind(&state.id).unwrap_err();
        assert!(error.contains("session in use"));
    }

    #[test]
    fn session_registry_unbind_clears_owned() {
        let mut registry = SessionRegistry::default();
        let state = registry.create();
        registry.bind(&state.id).unwrap();
        registry.unbind(&state.id);
        let session = registry.sessions.get(&state.id).unwrap();
        assert!(!session.owned);
    }

    #[test]
    fn session_registry_touch_updates_last_used_at() {
        let mut registry = SessionRegistry::default();
        let state = registry.create();
        let before = registry.sessions.get(&state.id).unwrap().last_used_at;
        std::thread::sleep(Duration::from_millis(5));
        registry.touch(&state.id);
        let after = registry.sessions.get(&state.id).unwrap().last_used_at;
        assert!(after > before);
    }

    #[test]
    fn session_registry_reap_expired_drops_only_expired_unowned_sessions() {
        let mut registry = SessionRegistry::default();
        let fresh = registry.create();
        let expired_idle = registry.create();
        let expired_owned = registry.create();
        let stale = SystemTime::now() - SESSION_IDLE_TTL - Duration::from_secs(60);
        registry
            .sessions
            .get_mut(&expired_idle.id)
            .unwrap()
            .last_used_at = stale;
        let owned = registry.sessions.get_mut(&expired_owned.id).unwrap();
        owned.last_used_at = stale;
        owned.owned = true;
        registry.reap_expired();
        let remaining: Vec<_> = registry
            .list()
            .into_iter()
            .map(|session| session.id)
            .collect();
        assert!(remaining.contains(&fresh.id));
        assert!(remaining.contains(&expired_owned.id));
        assert!(!remaining.contains(&expired_idle.id));
    }

    #[test]
    fn resolve_refs_substitutes_ref_with_previous_result() {
        let results = vec![serde_json::json!({
            "label": "snap",
            "result": {"content": [{"text": "hello"}]}
        })];
        let template = serde_json::json!({
            "value": {"$ref": "snap.result.content.0.text"}
        });
        let resolved = resolve_refs(template, &results).unwrap();
        assert_eq!(resolved, serde_json::json!({"value": "hello"}));
    }

    #[test]
    fn resolve_refs_returns_null_for_unknown_label() {
        let results: Vec<serde_json::Value> = vec![];
        let template = serde_json::json!({"value": {"$ref": "missing.x"}});
        let resolved = resolve_refs(template, &results).unwrap();
        assert_eq!(resolved, serde_json::json!({"value": null}));
    }

    #[test]
    fn resolve_refs_passes_through_non_refs() {
        let template = serde_json::json!({"a": 1, "b": [2, 3]});
        let resolved = resolve_refs(template.clone(), &[]).unwrap();
        assert_eq!(resolved, template);
    }

    #[test]
    fn wants_help_detects_help_flags() {
        assert!(wants_help(&args(&["--help"])));
        assert!(wants_help(&args(&["-h"])));
        assert!(wants_help(&args(&["help"])));
        assert!(!wants_help(&args(&["--profile", "default"])));
    }

    fn dummy_config() -> Config {
        Config {
            profiles: vec![Profile {
                name: "default".to_string(),
                user_data_dir: "/tmp/x".to_string(),
            }],
        }
    }

    #[test]
    fn require_profile_returns_named_profile() {
        let profile = require_profile(&dummy_config(), &args(&["--profile", "default"])).unwrap();
        assert_eq!(profile.name, "default");
    }

    #[test]
    fn require_profile_rejects_missing_flag_and_unknown_name() {
        assert!(require_profile(&dummy_config(), &[]).is_err());
        assert!(require_profile(&dummy_config(), &args(&["--profile", "other"])).is_err());
    }

    #[test]
    fn require_profile_and_session_returns_both() {
        let (profile, session) = require_profile_and_session(
            &dummy_config(),
            &args(&["--profile", "default", "--session", "sess-x"]),
        )
        .unwrap();
        assert_eq!(profile.name, "default");
        assert_eq!(session, "sess-x");
    }

    #[test]
    fn require_profile_and_session_rejects_missing_session() {
        assert!(
            require_profile_and_session(&dummy_config(), &args(&["--profile", "default"]),)
                .is_err()
        );
    }

    #[test]
    fn parse_batch_args_collects_all_flags() {
        let options = parse_batch_args(&args(&[
            "--profile",
            "default",
            "--session",
            "sess-1",
            "--script",
            "/tmp/x.json",
            "--output",
            "/tmp/out.json",
            "--fail-fast",
        ]))
        .unwrap();
        assert_eq!(options.profile_name, "default");
        assert_eq!(options.session_id, "sess-1");
        assert_eq!(options.script_path, "/tmp/x.json");
        assert_eq!(options.output_path.as_deref(), Some("/tmp/out.json"));
        assert!(options.fail_fast);
    }

    #[test]
    fn parse_batch_args_rejects_missing_required_flags() {
        assert!(
            parse_batch_args(&args(&["--session", "sess-1", "--script", "/tmp/x.json"])).is_err()
        );
        assert!(
            parse_batch_args(&args(&["--profile", "default", "--script", "/tmp/x.json"])).is_err()
        );
        assert!(parse_batch_args(&args(&["--profile", "default", "--session", "sess-1"])).is_err());
    }

    #[test]
    fn expand_home_resolves_tilde_paths() {
        let prev = env::var("HOME").ok();
        env::set_var("HOME", "/tmp/fakehome");
        assert_eq!(expand_home("~").unwrap(), PathBuf::from("/tmp/fakehome"));
        assert_eq!(
            expand_home("~/foo").unwrap(),
            PathBuf::from("/tmp/fakehome/foo")
        );
        assert_eq!(
            expand_home("/abs/path").unwrap(),
            PathBuf::from("/abs/path")
        );
        if let Some(value) = prev {
            env::set_var("HOME", value);
        } else {
            env::remove_var("HOME");
        }
    }

    #[test]
    fn daemon_initialize_response_includes_id() {
        let response = daemon_initialize_response(7);
        assert!(response.contains("\"id\":7"));
        assert!(response.contains("\"protocolVersion\""));
    }

    #[test]
    fn default_user_data_dir_for_profile_follows_prefix() {
        assert_eq!(
            default_user_data_dir_for_profile("conao3"),
            "~/.config/chrome-devtools/profiles/conao3"
        );
    }

    #[test]
    fn find_profile_returns_named_profile_or_error() {
        let config = dummy_config();
        assert_eq!(find_profile(&config, "default").unwrap().name, "default");
        assert!(find_profile(&config, "missing").is_err());
    }

    #[test]
    fn unix_secs_returns_zero_for_unix_epoch() {
        assert_eq!(unix_secs(UNIX_EPOCH), 0);
    }

    #[test]
    fn extract_remote_debugging_port_reads_port_number() {
        let cmd = "/path/chrome --foo --remote-debugging-port=39277 --user-data-dir=/x";
        assert_eq!(extract_remote_debugging_port(cmd), Some(39277));
    }

    #[test]
    fn extract_remote_debugging_port_returns_none_when_missing() {
        assert_eq!(extract_remote_debugging_port("/path/chrome --foo"), None);
    }

    #[test]
    fn extract_remote_debugging_port_stops_at_next_flag() {
        let cmd = "chrome --remote-debugging-port=8080 --type=renderer";
        assert_eq!(extract_remote_debugging_port(cmd), Some(8080));
    }

    #[test]
    fn extract_remote_debugging_port_returns_none_for_invalid_value() {
        let cmd = "chrome --remote-debugging-port=abc --user-data-dir=/x";
        assert_eq!(extract_remote_debugging_port(cmd), None);
    }

    #[test]
    fn sanitize_outgoing_request_strips_isolated_context_from_new_page() {
        let input = r#"{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"new_page","arguments":{"url":"https://example.com","isolatedContext":"foo"}}}"#;
        let output = sanitize_outgoing_request(input);
        let value: serde_json::Value = serde_json::from_str(&output).unwrap();
        assert!(value["params"]["arguments"]
            .get("isolatedContext")
            .is_none());
        assert_eq!(value["params"]["arguments"]["url"], "https://example.com");
    }

    #[test]
    fn sanitize_outgoing_request_keeps_other_tools_untouched() {
        let input = r#"{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"navigate_page","arguments":{"isolatedContext":"foo"}}}"#;
        let output = sanitize_outgoing_request(input);
        assert_eq!(output, input);
    }

    #[test]
    fn sanitize_outgoing_request_passes_through_non_tools_call() {
        let input = r#"{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}"#;
        let output = sanitize_outgoing_request(input);
        assert_eq!(output, input);
    }

    #[test]
    fn sanitize_outgoing_request_passes_through_invalid_json() {
        let input = "not json";
        assert_eq!(sanitize_outgoing_request(input), input);
    }
}