infrastore-cli 0.2.0

Command-line tool for loading and inspecting an infrastore store directly on disk
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
//! Failure paths and untested subcommands for the `infrastore` binary.
//!
//! `cli_round_trip.rs` drives the happy path. This file covers what a real user
//! is far more likely to hit: a ragged CSV, a typo'd descriptor field, a value
//! count that does not match the declared shape. Each case asserts a *nonzero
//! exit* and that the message names the actual problem — an error that exits 0,
//! or exits 1 with an unhelpful message, is a bug even though the data is
//! untouched.
//!
//! Also here: the subcommands no test ever invoked (`transform`, `copy`,
//! `persist`, `compact`, `params`, `template`, real `clear` / `replace-owner`),
//! the `export` -> `add` round trip, `get --time-range`, and the
//! `INFRASTORE_STORE` environment fallback.

use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

// ---------------------------------------------------------------------------
// Harness
// ---------------------------------------------------------------------------

const BIN: &str = env!("CARGO_BIN_EXE_infrastore");

/// Run `infrastore --store <store> <args...>`, asserting success; returns stdout.
fn run(store: &Path, args: &[&str]) -> String {
    let output = Command::new(BIN)
        .arg("--store")
        .arg(store)
        .args(args)
        .output()
        .expect("failed to spawn infrastore");
    assert!(
        output.status.success(),
        "infrastore {args:?} failed:\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
    String::from_utf8(output.stdout).expect("utf8 stdout")
}

/// Run `infrastore`, asserting a nonzero exit **and** the `Error:` stderr prefix that
/// `main` writes; returns stderr. The prefix is part of the CLI's contract with
/// a shell caller: it is how a user tells a diagnostic from log noise.
fn run_err(store: &Path, args: &[&str]) -> String {
    let output = Command::new(BIN)
        .arg("--store")
        .arg(store)
        .args(args)
        .output()
        .expect("failed to spawn infrastore");
    assert!(
        !output.status.success(),
        "infrastore {args:?} unexpectedly succeeded:\nstdout: {}",
        String::from_utf8_lossy(&output.stdout),
    );
    let stderr = String::from_utf8(output.stderr).expect("utf8 stderr");
    assert!(
        stderr.contains("Error: "),
        "infrastore {args:?} exited nonzero without an `Error: ` diagnostic; stderr was:\n{stderr}"
    );
    stderr
}

/// Run `infrastore`, asserting a nonzero exit without requiring the `Error: ` prefix.
/// `verify` reports through its normal output and then exits 1, so it fails
/// without writing a diagnostic; returns `(stdout, stderr)`.
fn run_fail(store: &Path, args: &[&str]) -> (String, String) {
    let output = Command::new(BIN)
        .arg("--store")
        .arg(store)
        .args(args)
        .output()
        .expect("failed to spawn infrastore");
    assert!(
        !output.status.success(),
        "infrastore {args:?} unexpectedly succeeded:\nstdout: {}",
        String::from_utf8_lossy(&output.stdout),
    );
    (
        String::from_utf8_lossy(&output.stdout).into_owned(),
        String::from_utf8_lossy(&output.stderr).into_owned(),
    )
}

/// Exit code of a `infrastore` invocation.
fn exit_code(store: &Path, args: &[&str]) -> i32 {
    Command::new(BIN)
        .arg("--store")
        .arg(store)
        .args(args)
        .output()
        .expect("failed to spawn infrastore")
        .status
        .code()
        .expect("the process was killed by a signal")
}

fn write(dir: &Path, name: &str, body: &str) -> PathBuf {
    let path = dir.join(name);
    fs::write(&path, body).unwrap();
    path
}

fn data_lines(csv: &str) -> Vec<String> {
    csv.lines()
        .map(str::trim)
        .filter(|l| !l.is_empty())
        .skip(1)
        .map(str::to_string)
        .collect()
}

/// A minimal single-series descriptor with `overrides` merged in as raw JSON
/// members, so a test can add or replace one field without restating the rest.
fn descriptor_json(overrides: &[(&str, &str)]) -> String {
    let mut fields: Vec<(String, String)> = vec![
        ("owner_id".into(), "42".into()),
        ("owner_type".into(), "\"Generator\"".into()),
        ("name".into(), "\"load\"".into()),
        ("type".into(), "\"single\"".into()),
        ("dtype".into(), "\"f64\"".into()),
        ("csv".into(), "\"data.csv\"".into()),
        ("has_header".into(), "false".into()),
        (
            "initial_timestamp".into(),
            "\"2024-01-01T00:00:00Z\"".into(),
        ),
        ("resolution".into(), "\"1h\"".into()),
    ];
    for (k, v) in overrides {
        match fields.iter_mut().find(|(name, _)| name == k) {
            Some(entry) => entry.1 = (*v).to_string(),
            None => fields.push(((*k).to_string(), (*v).to_string())),
        }
    }
    let body: Vec<String> = fields
        .iter()
        .map(|(k, v)| format!("  \"{k}\": {v}"))
        .collect();
    format!("{{\n{}\n}}", body.join(",\n"))
}

/// Write a CSV + descriptor pair into a fresh temp dir and return
/// `(dir, store_path, descriptor_path)`.
fn fixture(csv_body: &str, overrides: &[(&str, &str)]) -> (tempfile::TempDir, PathBuf, PathBuf) {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    write(dir.path(), "data.csv", csv_body);
    let descriptor = write(dir.path(), "d.json", &descriptor_json(overrides));
    (dir, store, descriptor)
}

/// `infrastore add` for a fixture descriptor, expecting failure; returns stderr.
fn add_err(store: &Path, descriptor: &Path) -> String {
    run_err(
        store,
        &["add", "--descriptor", descriptor.to_str().unwrap()],
    )
}

/// `infrastore add` for a fixture descriptor, expecting success.
fn add_ok(store: &Path, descriptor: &Path) -> String {
    run(
        store,
        &["add", "--descriptor", descriptor.to_str().unwrap()],
    )
}

/// Seed a store with one 4-step f64 series named `load` owned by 42.
fn seed(dir: &Path, store: &Path) {
    write(dir, "seed.csv", "10\n11\n12\n13\n");
    let descriptor = write(
        dir,
        "seed.json",
        &descriptor_json(&[("csv", "\"seed.csv\"")]),
    );
    add_ok(store, &descriptor);
}

// ---------------------------------------------------------------------------
// Bad CSV matrix
// ---------------------------------------------------------------------------

#[test]
fn a_ragged_csv_row_is_rejected() {
    // The reader is built with `flexible(false)`, so a row with a different
    // field count than the first is a parse error naming the row.
    let (_dir, store, descriptor) = fixture("1.0,2.0\n3.0\n", &[]);
    let stderr = add_err(&store, &descriptor);
    assert!(
        stderr.contains("row") || stderr.contains("field"),
        "expected a row/field diagnostic, got: {stderr}"
    );
}

#[test]
fn a_non_numeric_cell_in_an_f64_column_is_rejected() {
    let (_dir, store, descriptor) = fixture("1.0\nnot_a_number\n3.0\n", &[]);
    let stderr = add_err(&store, &descriptor);
    assert!(
        stderr.contains("not_a_number"),
        "the diagnostic must quote the offending cell, got: {stderr}"
    );
    assert!(
        stderr.contains("f64"),
        "the diagnostic must name the expected type, got: {stderr}"
    );
}

#[test]
fn a_non_integer_cell_in_an_i64_column_is_rejected() {
    // An empty line is skipped by the CSV reader before it reaches the parser,
    // so it is not in this list — it shortens the series instead.
    for bad in ["abc", "1.5", "0x10", "1e3", ""] {
        let (_dir, store, descriptor) = fixture(&format!("1\n{bad}\n3\n"), &[("dtype", "\"i64\"")]);
        let stderr = add_err(&store, &descriptor);
        assert!(
            stderr.contains("i64"),
            "{bad:?}: expected an i64 diagnostic, got: {stderr}"
        );
    }
}

#[test]
fn a_negative_value_in_a_u64_column_is_rejected() {
    let (_dir, store, descriptor) = fixture("1\n-2\n3\n", &[("dtype", "\"u64\"")]);
    let stderr = add_err(&store, &descriptor);
    assert!(
        stderr.contains("u64") && stderr.contains("-2"),
        "expected a u64 diagnostic quoting -2, got: {stderr}"
    );
}

#[test]
fn an_out_of_range_integer_is_rejected() {
    // i32 cannot hold 3_000_000_000.
    let (_dir, store, descriptor) = fixture("1\n3000000000\n3\n", &[("dtype", "\"i32\"")]);
    let stderr = add_err(&store, &descriptor);
    assert!(stderr.contains("i32"), "got: {stderr}");
}

#[test]
fn a_non_boolean_cell_in_a_bool_column_is_rejected() {
    let (_dir, store, descriptor) = fixture("true\nmaybe\nfalse\n", &[("dtype", "\"bool\"")]);
    let stderr = add_err(&store, &descriptor);
    assert!(
        stderr.contains("true/false") || stderr.contains("maybe"),
        "expected a bool diagnostic, got: {stderr}"
    );
}

#[test]
fn accepted_boolean_spellings_round_trip() {
    // The complement of the case above: `1`/`0` and mixed case are accepted.
    let (dir, store, _) = fixture("TRUE\n0\nFalse\n1\n", &[("dtype", "\"bool\"")]);
    let descriptor = write(
        dir.path(),
        "d.json",
        &descriptor_json(&[("dtype", "\"bool\"")]),
    );
    add_ok(&store, &descriptor);
    let out = run(
        &store,
        &["-f", "csv", "get", "--owner-id", "42", "--name", "load"],
    );
    assert_eq!(data_lines(&out), vec!["true", "false", "false", "true"]);
}

#[test]
fn a_value_count_that_does_not_match_the_declared_shape_is_rejected() {
    // The likeliest real user error: element_shape says 3 per step, but the CSV
    // holds a number of values that is not a multiple of 3.
    let (_dir, store, descriptor) = fixture("1,2,3\n4,5,6\n7,8\n", &[("element_shape", "[3]")]);
    let stderr = add_err(&store, &descriptor);
    assert!(!stderr.is_empty());

    // And a clean multiple that still disagrees with an explicit forecast shape.
    let (_dir, store, descriptor) = fixture(
        "1\n2\n3\n4\n5\n",
        &[
            ("type", "\"deterministic\""),
            ("horizon", "\"2h\""),
            ("interval", "\"1h\""),
            ("count", "3"),
        ],
    );
    let stderr = add_err(&store, &descriptor);
    assert!(
        stderr.contains("expected 6 values") || stderr.contains("shape"),
        "expected a shape/count diagnostic, got: {stderr}"
    );
}

#[test]
fn a_missing_timestamp_column_for_non_sequential_is_rejected() {
    // With `type: non_sequential` the first column is timestamps. A single-column
    // CSV therefore has values that are not parseable as timestamps.
    let (_dir, store, descriptor) = fixture(
        "10\n20\n30\n",
        &[
            ("type", "\"non_sequential\""),
            ("initial_timestamp", "null"),
            ("resolution", "null"),
        ],
    );
    let stderr = add_err(&store, &descriptor);
    assert!(!stderr.is_empty(), "got: {stderr}");
}

#[test]
fn unsorted_and_duplicate_timestamps_are_rejected() {
    for (label, body) in [
        (
            "decreasing",
            "2024-01-02T00:00:00Z,10\n2024-01-01T00:00:00Z,20\n",
        ),
        (
            "duplicate",
            "2024-01-01T00:00:00Z,10\n2024-01-01T00:00:00Z,20\n",
        ),
    ] {
        let (_dir, store, descriptor) = fixture(
            body,
            &[
                ("type", "\"non_sequential\""),
                ("initial_timestamp", "null"),
                ("resolution", "null"),
            ],
        );
        let stderr = add_err(&store, &descriptor);
        assert!(
            stderr.contains("increasing"),
            "{label}: expected a strictly-increasing diagnostic, got: {stderr}"
        );
    }
}

#[test]
fn a_malformed_timestamp_in_a_non_sequential_csv_is_rejected() {
    let (_dir, store, descriptor) = fixture(
        "2024-01-01T00:00:00Z,10\nnot-a-time,20\n",
        &[
            ("type", "\"non_sequential\""),
            ("initial_timestamp", "null"),
            ("resolution", "null"),
        ],
    );
    let stderr = add_err(&store, &descriptor);
    assert!(!stderr.is_empty(), "got: {stderr}");
}

#[test]
fn a_nonexistent_csv_path_is_rejected() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    // No data.csv is written.
    let descriptor = write(dir.path(), "d.json", &descriptor_json(&[]));
    let stderr = add_err(&store, &descriptor);
    assert!(
        stderr.contains("data.csv"),
        "the diagnostic must name the missing file, got: {stderr}"
    );
}

#[test]
fn a_descriptor_with_no_csv_and_no_flag_is_rejected() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    let descriptor = write(dir.path(), "d.json", &descriptor_json(&[("csv", "null")]));
    let stderr = add_err(&store, &descriptor);
    assert!(
        stderr.contains("csv"),
        "expected a diagnostic about the missing csv path, got: {stderr}"
    );
}

#[test]
fn has_header_true_skips_the_first_row() {
    // `has_header` defaults to true but no test ever exercised that path: every
    // fixture sets it to false. A header row must be consumed, not parsed as data.
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    write(dir.path(), "data.csv", "value\n1.5\n2.5\n3.5\n");
    let descriptor = write(
        dir.path(),
        "d.json",
        &descriptor_json(&[("has_header", "true")]),
    );
    add_ok(&store, &descriptor);

    let out = run(
        &store,
        &["-f", "csv", "get", "--owner-id", "42", "--name", "load"],
    );
    assert_eq!(data_lines(&out), vec!["1.5", "2.5", "3.5"]);

    // With `has_header: false` the same file fails, because "value" is not an f64
    // — which is what makes the assertion above meaningful.
    let store2 = dir.path().join("store2.nc");
    let no_header = write(
        dir.path(),
        "d2.json",
        &descriptor_json(&[("has_header", "false")]),
    );
    let stderr = add_err(&store2, &no_header);
    assert!(stderr.contains("value"), "got: {stderr}");
}

#[test]
fn adding_the_same_series_twice_is_a_duplicate_with_a_nonzero_exit() {
    let (_dir, store, descriptor) = fixture("1.0\n2.0\n3.0\n", &[]);
    add_ok(&store, &descriptor);
    let stderr = add_err(&store, &descriptor);
    assert!(
        stderr.to_lowercase().contains("already exists")
            || stderr.to_lowercase().contains("duplicate"),
        "expected a duplicate diagnostic, got: {stderr}"
    );
    // The first series is intact.
    let out = run(
        &store,
        &["-f", "csv", "get", "--owner-id", "42", "--name", "load"],
    );
    assert_eq!(data_lines(&out), vec!["1", "2", "3"]);
}

// ---------------------------------------------------------------------------
// Descriptor matrix
// ---------------------------------------------------------------------------

#[test]
fn an_unknown_descriptor_field_is_rejected() {
    // `Descriptor` is `deny_unknown_fields`, so a typo is caught rather than
    // silently ignored — the difference between "my units were dropped" and a
    // clear error.
    // `unit`, not `units`.
    let (_dir, store, descriptor) = fixture("1.0\n2.0\n3.0\n", &[("unit", "\"MW\"")]);
    let stderr = add_err(&store, &descriptor);
    assert!(
        stderr.contains("unit"),
        "the diagnostic must name the unknown field, got: {stderr}"
    );
}

#[test]
fn each_type_reports_its_own_missing_required_field() {
    /// `(type, extra descriptor fields, the word the diagnostic must contain)`.
    type Case<'a> = (&'a str, &'a [(&'a str, &'a str)], &'a str);
    let cases: &[Case] = &[
        // single without initial_timestamp
        (
            "single",
            &[("initial_timestamp", "null")],
            "initial_timestamp",
        ),
        // single without resolution
        ("single", &[("resolution", "null")], "resolution"),
        // deterministic without horizon
        (
            "deterministic",
            &[("interval", "\"1h\""), ("count", "3")],
            "horizon",
        ),
        // deterministic without interval
        (
            "deterministic",
            &[("horizon", "\"2h\""), ("count", "3")],
            "interval",
        ),
        // deterministic without count
        (
            "deterministic",
            &[("horizon", "\"2h\""), ("interval", "\"1h\"")],
            "count",
        ),
        // probabilistic without percentiles
        (
            "probabilistic",
            &[
                ("horizon", "\"2h\""),
                ("interval", "\"1h\""),
                ("count", "3"),
            ],
            "percentiles",
        ),
    ];

    for (ts_type, extra, expect) in cases {
        let mut overrides: Vec<(&str, &str)> = vec![(
            "type",
            match *ts_type {
                "single" => "\"single\"",
                "deterministic" => "\"deterministic\"",
                "probabilistic" => "\"probabilistic\"",
                other => panic!("unhandled type {other}"),
            },
        )];
        overrides.extend_from_slice(extra);
        let (_dir, store, descriptor) = fixture("1\n2\n3\n4\n5\n6\n", &overrides);
        let stderr = add_err(&store, &descriptor);
        assert!(
            stderr.contains(expect),
            "{ts_type} missing {expect}: got {stderr}"
        );
    }
}

#[test]
fn an_empty_root_array_is_rejected() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    let descriptor = write(dir.path(), "d.json", "[]");
    let stderr = add_err(&store, &descriptor);
    assert!(
        stderr.contains("empty array"),
        "expected an empty-array diagnostic, got: {stderr}"
    );
}

#[test]
fn a_root_scalar_descriptor_is_rejected() {
    for body in ["42", "\"a string\"", "true", "null"] {
        let dir = tempfile::tempdir().unwrap();
        let store = dir.path().join("store.nc");
        let descriptor = write(dir.path(), "d.json", body);
        let stderr = add_err(&store, &descriptor);
        assert!(
            stderr.contains("JSON object or array"),
            "{body}: expected a shape diagnostic, got: {stderr}"
        );
    }
}

#[test]
fn malformed_json_is_rejected() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    for body in ["{", "{\"owner_id\": }", "{,}", "not json at all"] {
        let descriptor = write(dir.path(), "d.json", body);
        let stderr = add_err(&store, &descriptor);
        assert!(
            stderr.contains("parsing descriptor"),
            "{body:?}: expected a parse diagnostic, got: {stderr}"
        );
    }
}

#[test]
fn a_nonexistent_descriptor_path_is_rejected() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    let stderr = run_err(
        &store,
        &[
            "add",
            "--descriptor",
            dir.path().join("no_such.json").to_str().unwrap(),
        ],
    );
    assert!(
        stderr.contains("reading descriptor"),
        "expected a read diagnostic, got: {stderr}"
    );
}

#[test]
fn a_zero_element_shape_dimension_is_rejected() {
    // PIN the message a user actually gets. `element_shape: [0]` does *not*
    // reach `steps_from_values`'s "must not contain a zero dimension" guard:
    // `per_step` is `product().max(1)`, so a zero dimension becomes 1 and the
    // failure surfaces later as a shape/value-count mismatch instead. The
    // diagnostic still names the offending shape, so it is actionable, but the
    // dedicated guard is unreachable this way.
    let (_dir, store, descriptor) = fixture("1.0\n2.0\n3.0\n", &[("element_shape", "[0]")]);
    let stderr = add_err(&store, &descriptor);
    assert!(
        stderr.contains("[3, 0]"),
        "expected the degenerate shape in the diagnostic, got: {stderr}"
    );

    // A zero in a multi-dimensional element shape behaves the same way.
    let (_dir, store, descriptor) = fixture("1.0\n2.0\n3.0\n", &[("element_shape", "[2, 0]")]);
    let stderr = add_err(&store, &descriptor);
    assert!(stderr.contains("0"), "got: {stderr}");
}

#[test]
fn a_non_divisible_value_count_is_rejected() {
    // 5 values, 2 per step: not a whole number of steps.
    let (_dir, store, descriptor) = fixture("1,2\n3,4\n5\n", &[("element_shape", "[2]")]);
    let stderr = add_err(&store, &descriptor);
    assert!(!stderr.is_empty());

    // A single-column CSV with an odd count is the cleaner form of the same error.
    let (_dir, store, descriptor) = fixture("1\n2\n3\n4\n5\n", &[("element_shape", "[2]")]);
    let stderr = add_err(&store, &descriptor);
    assert!(
        stderr.contains("divisible"),
        "expected a divisibility diagnostic, got: {stderr}"
    );
}

#[test]
fn a_scenario_count_that_cannot_be_inferred_is_rejected() {
    // H = 2, count = 3 -> 6 values per scenario. 7 values divides by nothing.
    let (_dir, store, descriptor) = fixture(
        "1\n2\n3\n4\n5\n6\n7\n",
        &[
            ("type", "\"scenarios\""),
            ("horizon", "\"2h\""),
            ("interval", "\"1h\""),
            ("count", "3"),
        ],
    );
    let stderr = add_err(&store, &descriptor);
    assert!(
        stderr.contains("infer scenario_count"),
        "expected an inference diagnostic, got: {stderr}"
    );
}

#[test]
fn a_scenario_count_that_can_be_inferred_is_used() {
    // 12 values / (H=2 * count=3) = 2 scenarios.
    let (_dir, store, descriptor) = fixture(
        "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n",
        &[
            ("type", "\"scenarios\""),
            ("horizon", "\"2h\""),
            ("interval", "\"1h\""),
            ("count", "3"),
        ],
    );
    add_ok(&store, &descriptor);
    let out = run(&store, &["-f", "json", "list"]);
    assert!(out.contains("Scenarios"), "got: {out}");
}

#[test]
fn a_deterministic_single_time_series_descriptor_points_at_transform() {
    let (_dir, store, descriptor) = fixture(
        "1\n2\n3\n4\n",
        &[
            ("type", "\"deterministic_single\""),
            ("horizon", "\"2h\""),
            ("interval", "\"1h\""),
            ("count", "2"),
        ],
    );
    let stderr = add_err(&store, &descriptor);
    // Either the type spelling is unknown, or the build path reports the
    // transform hint. Both are acceptable; assert whichever is emitted names a
    // route forward.
    assert!(
        stderr.contains("transform") || stderr.contains("deterministic_single"),
        "expected a diagnostic mentioning transform or the type name, got: {stderr}"
    );
}

#[test]
fn an_unknown_type_or_dtype_is_rejected() {
    let (_dir, store, descriptor) = fixture("1.0\n2.0\n", &[("type", "\"quantum\"")]);
    let stderr = add_err(&store, &descriptor);
    assert!(stderr.contains("quantum"), "got: {stderr}");

    let (_dir, store, descriptor) = fixture("1.0\n2.0\n", &[("dtype", "\"f16\"")]);
    let stderr = add_err(&store, &descriptor);
    assert!(stderr.contains("f16"), "got: {stderr}");

    let (_dir, store, descriptor) = fixture("1.0\n2.0\n", &[("owner_category", "\"neither\"")]);
    let stderr = add_err(&store, &descriptor);
    assert!(stderr.contains("neither"), "got: {stderr}");

    let (_dir, store, descriptor) = fixture("1.0\n2.0\n", &[("resolution", "\"1 fortnight\"")]);
    let stderr = add_err(&store, &descriptor);
    assert!(!stderr.is_empty(), "got: {stderr}");
}

#[test]
fn a_features_map_in_a_descriptor_round_trips() {
    // `features` is never populated in any other test, so the JSON -> FeatureValue
    // conversion (including each value kind) was unexercised through the CLI.
    let (_dir, store, descriptor) = fixture(
        "1.0\n2.0\n3.0\n",
        &[(
            "features",
            "{\"model_year\": 2030, \"scenario\": \"base\", \"flag\": true, \"scale\": 1.5}",
        )],
    );
    add_ok(&store, &descriptor);

    // `list` rows do not carry features; `info` does, one `feature.<key>` field
    // each.
    let out = run(
        &store,
        &["-f", "json", "info", "--owner-id", "42", "--name", "load"],
    );
    for expect in [
        "feature.model_year",
        "2030",
        "feature.scenario",
        "base",
        "feature.flag",
        "feature.scale",
        "1.5",
    ] {
        assert!(out.contains(expect), "{expect} missing from: {out}");
    }

    // The features are part of the identity: selecting by one of them matches.
    let out = run(
        &store,
        &[
            "-f",
            "csv",
            "get",
            "--owner-id",
            "42",
            "--name",
            "load",
            "--feature",
            "model_year=2030",
        ],
    );
    assert_eq!(data_lines(&out), vec!["1", "2", "3"]);

    // A feature value that is neither scalar nor string is rejected.
    let (_dir, store2, descriptor2) = fixture(
        "1.0\n2.0\n3.0\n",
        &[("features", "{\"nested\": {\"a\": 1}}")],
    );
    let stderr = add_err(&store2, &descriptor2);
    assert!(stderr.contains("nested"), "got: {stderr}");
}

#[test]
fn an_attribute_owned_series_can_be_added_and_listed() {
    // `owner_category: supplemental_attribute` is accepted by the descriptor but
    // was never exercised.
    let (_dir, store, descriptor) = fixture(
        "1.0\n2.0\n3.0\n",
        &[
            ("owner_category", "\"supplemental_attribute\""),
            ("owner_type", "\"GeographicInfo\""),
        ],
    );
    add_ok(&store, &descriptor);

    let out = run(&store, &["-f", "json", "list"]);
    assert!(out.contains("SupplementalAttribute"), "got: {out}");

    // And it is addressable by that category, but not by the other one.
    let out = run(
        &store,
        &[
            "-f",
            "csv",
            "get",
            "--owner-id",
            "42",
            "--owner-category",
            "supplemental_attribute",
            "--name",
            "load",
        ],
    );
    assert_eq!(data_lines(&out), vec!["1", "2", "3"]);
    run_err(
        &store,
        &[
            "get",
            "--owner-id",
            "42",
            "--owner-category",
            "component",
            "--name",
            "load",
        ],
    );
}

// ---------------------------------------------------------------------------
// Untested subcommands
// ---------------------------------------------------------------------------

#[test]
fn transform_derives_a_dst_that_list_shows() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    write(dir.path(), "t.csv", "1\n2\n3\n4\n5\n6\n7\n8\n");
    let descriptor = write(
        dir.path(),
        "t.json",
        &descriptor_json(&[("csv", "\"t.csv\"")]),
    );
    add_ok(&store, &descriptor);

    let out = run(
        &store,
        &["transform", "--horizon", "4h", "--interval", "2h"],
    );
    assert!(!out.is_empty() || out.is_empty()); // output shape is not the contract

    let listed = run(&store, &["-f", "json", "list"]);
    assert!(
        listed.contains("DeterministicSingleTimeSeries"),
        "the DST tag must be visible after transform, got: {listed}"
    );

    // The DST reads back as a Deterministic view of the same data.
    let out = run(
        &store,
        &[
            "-f",
            "json",
            "get",
            "--owner-id",
            "42",
            "--name",
            "load",
            "--type",
            "deterministic_single",
        ],
    );
    assert!(!out.trim().is_empty());

    // A transform with a bad period is rejected.
    run_err(
        &store,
        &["transform", "--horizon", "not-a-period", "--interval", "2h"],
    );
}

#[test]
fn copy_shares_the_array_and_dry_run_changes_nothing() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    seed(dir.path(), &store);

    // --dry-run reports but does not write.
    let out = run(
        &store,
        &[
            "copy",
            "--owner-id",
            "42",
            "--name",
            "load",
            "--dst-owner-id",
            "43",
            "--dst-owner-type",
            "Generator",
            "--dry-run",
        ],
    );
    assert!(
        !out.trim().is_empty(),
        "dry-run should report what it would do"
    );
    assert_eq!(
        run(&store, &["-f", "json", "list"])
            .matches("\"owner_id\"")
            .count(),
        1,
        "--dry-run must not add a series"
    );

    // The real copy.
    run(
        &store,
        &[
            "copy",
            "--owner-id",
            "42",
            "--name",
            "load",
            "--dst-owner-id",
            "43",
            "--dst-owner-type",
            "Generator",
            "--new-name",
            "load_copy",
        ],
    );
    let out = run(
        &store,
        &[
            "-f",
            "csv",
            "get",
            "--owner-id",
            "43",
            "--name",
            "load_copy",
        ],
    );
    assert_eq!(data_lines(&out), vec!["10", "11", "12", "13"]);

    // No array was duplicated.
    let stats = run(&store, &["-f", "json", "stats"]);
    let parsed: serde_json::Value = serde_json::from_str(&stats).unwrap();
    assert_eq!(
        parsed.get("num_distinct_arrays").and_then(|v| v.as_i64()),
        Some(1),
        "the copy must share the source array, got: {stats}"
    );
    assert_eq!(
        parsed.get("static_time_series").and_then(|v| v.as_i64()),
        Some(2),
        "but there are two associations, got: {stats}"
    );

    // Copying onto the same destination twice is a duplicate.
    run_err(
        &store,
        &[
            "copy",
            "--owner-id",
            "42",
            "--name",
            "load",
            "--dst-owner-id",
            "43",
            "--dst-owner-type",
            "Generator",
            "--new-name",
            "load_copy",
        ],
    );
}

#[test]
fn persist_writes_a_readable_copy() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    seed(dir.path(), &store);

    let dest = dir.path().join("copy.nc");
    run(&store, &["persist", "--dest", dest.to_str().unwrap()]);
    assert!(dest.exists(), "the destination .nc must exist");
    let mut sqlite = dest.clone().into_os_string();
    sqlite.push(".sqlite");
    assert!(
        PathBuf::from(sqlite).exists(),
        "the companion catalog must exist too"
    );

    // The copy reads the same values, and verifies clean.
    let out = run(
        &dest,
        &["-f", "csv", "get", "--owner-id", "42", "--name", "load"],
    );
    assert_eq!(data_lines(&out), vec!["10", "11", "12", "13"]);
    assert_eq!(exit_code(&dest, &["verify"]), 0);
}

#[test]
fn compact_runs_and_reports() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    seed(dir.path(), &store);
    // Add a second series and remove it, leaving a reusable slot.
    write(dir.path(), "b.csv", "90\n91\n92\n93\n");
    let second = write(
        dir.path(),
        "b.json",
        &descriptor_json(&[("owner_id", "43"), ("csv", "\"b.csv\"")]),
    );
    add_ok(&store, &second);
    run(
        &store,
        &["remove", "--owner-id", "43", "--name", "load", "--force"],
    );

    let out = run(&store, &["-f", "json", "compact", "--force"]);
    assert!(!out.trim().is_empty(), "compact must print a report");

    // The surviving series is intact and the store still verifies.
    let out = run(
        &store,
        &["-f", "csv", "get", "--owner-id", "42", "--name", "load"],
    );
    assert_eq!(data_lines(&out), vec!["10", "11", "12", "13"]);
    assert_eq!(exit_code(&store, &["verify"]), 0);
}

#[test]
fn params_reports_forecast_parameters() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    write(dir.path(), "f.csv", "1\n2\n3\n4\n5\n6\n");
    let descriptor = write(
        dir.path(),
        "f.json",
        &descriptor_json(&[
            ("csv", "\"f.csv\""),
            ("type", "\"deterministic\""),
            ("horizon", "\"2h\""),
            ("interval", "\"1h\""),
            ("count", "3"),
        ]),
    );
    add_ok(&store, &descriptor);

    let out = run(&store, &["-f", "json", "params"]);
    assert!(out.contains("PT2H"), "the horizon must appear: {out}");
    assert!(out.contains("PT1H"), "the interval must appear: {out}");

    // Scoped by resolution and interval.
    let out = run(
        &store,
        &[
            "-f",
            "json",
            "params",
            "--resolution",
            "1h",
            "--interval",
            "1h",
        ],
    );
    assert!(out.contains("PT2H"), "got: {out}");

    // A bad period is rejected.
    run_err(&store, &["params", "--resolution", "nonsense"]);

    // An empty store reports no parameters rather than failing.
    let empty_dir = tempfile::tempdir().unwrap();
    let empty = empty_dir.path().join("empty.nc");
    seed(empty_dir.path(), &empty); // statics only, no forecasts
    let out = run(&empty, &["-f", "json", "params"]);
    assert!(!out.trim().is_empty());
}

#[test]
fn template_prints_a_usable_descriptor_for_every_type() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    for ts_type in [
        "single",
        "non_sequential",
        "deterministic",
        "probabilistic",
        "scenarios",
    ] {
        let out = run(&store, &["template", ts_type]);
        // Each template must be valid JSON with a matching `type`.
        let value: serde_json::Value =
            serde_json::from_str(&out).unwrap_or_else(|e| panic!("{ts_type}: {e}\n{out}"));
        assert_eq!(
            value.get("type").and_then(|v| v.as_str()),
            Some(ts_type),
            "{ts_type}: template's type field"
        );
        assert!(value.get("dtype").is_some(), "{ts_type}: dtype");
    }

    // DST has no descriptor form.
    let stderr = run_err(&store, &["template", "deterministic_single"]);
    assert!(stderr.contains("transform"), "got: {stderr}");

    // An unknown type is rejected.
    run_err(&store, &["template", "quantum"]);
}

#[test]
fn clear_removes_everything_or_one_owner() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    seed(dir.path(), &store);
    write(dir.path(), "b.csv", "90\n91\n92\n93\n");
    let second = write(
        dir.path(),
        "b.json",
        &descriptor_json(&[("owner_id", "43"), ("csv", "\"b.csv\"")]),
    );
    add_ok(&store, &second);
    assert_eq!(
        run(&store, &["-f", "json", "list"])
            .matches("\"owner_id\"")
            .count(),
        2
    );

    // Scoped to one owner.
    run(
        &store,
        &[
            "clear",
            "--owner-id",
            "43",
            "--owner-category",
            "component",
            "--force",
        ],
    );
    let listed = run(&store, &["-f", "json", "list"]);
    assert_eq!(listed.matches("\"owner_id\"").count(), 1);
    assert!(listed.contains("42"));

    // Then everything.
    run(&store, &["clear", "--force"]);
    let listed = run(&store, &["-f", "json", "list"]);
    assert_eq!(listed.matches("\"owner_id\"").count(), 0, "got: {listed}");

    // Clearing an empty store is not an error.
    run(&store, &["clear", "--force"]);
}

#[test]
fn replace_owner_moves_series_and_dry_run_changes_nothing() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    seed(dir.path(), &store);

    // --dry-run reports the count without moving.
    let out = run(
        &store,
        &[
            "replace-owner",
            "--old",
            "42",
            "--new",
            "99",
            "--owner-category",
            "component",
            "--dry-run",
        ],
    );
    assert!(!out.trim().is_empty());
    assert!(
        run(&store, &["-f", "json", "list"]).contains("42"),
        "--dry-run must not move the series"
    );

    // The real move.
    run(
        &store,
        &[
            "replace-owner",
            "--old",
            "42",
            "--new",
            "99",
            "--owner-category",
            "component",
        ],
    );
    let out = run(
        &store,
        &["-f", "csv", "get", "--owner-id", "99", "--name", "load"],
    );
    assert_eq!(data_lines(&out), vec!["10", "11", "12", "13"]);
    run_err(&store, &["get", "--owner-id", "42", "--name", "load"]);

    // A bad owner category is rejected.
    run_err(
        &store,
        &[
            "replace-owner",
            "--old",
            "99",
            "--new",
            "100",
            "--owner-category",
            "neither",
        ],
    );
}

#[test]
fn remove_without_all_removes_exactly_one_series() {
    // The non-`--all` path: a selector that resolves to one series.
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    seed(dir.path(), &store);
    write(dir.path(), "b.csv", "90\n91\n92\n93\n");
    let second = write(
        dir.path(),
        "b.json",
        &descriptor_json(&[("owner_id", "43"), ("csv", "\"b.csv\"")]),
    );
    add_ok(&store, &second);

    run(
        &store,
        &["remove", "--owner-id", "43", "--name", "load", "--force"],
    );
    let listed = run(&store, &["-f", "json", "list"]);
    assert_eq!(listed.matches("\"owner_id\"").count(), 1);
    assert!(listed.contains("42"));

    // Without `--all`, a selector matching several series is an error naming
    // them, not a partial removal.
    write(dir.path(), "c.csv", "80\n81\n82\n83\n");
    let third = write(
        dir.path(),
        "c.json",
        &descriptor_json(&[("owner_id", "44"), ("csv", "\"c.csv\"")]),
    );
    add_ok(&store, &third);
    let stderr = run_err(&store, &["remove", "--name", "load", "--force"]);
    assert!(
        stderr.contains("matched"),
        "expected a multi-match diagnostic, got: {stderr}"
    );
    assert_eq!(
        run(&store, &["-f", "json", "list"])
            .matches("\"owner_id\"")
            .count(),
        2,
        "a failed remove must not delete anything"
    );

    // A selector matching nothing is an error too.
    let stderr = run_err(&store, &["remove", "--name", "absent", "--force"]);
    assert!(stderr.contains("no time series matched"), "got: {stderr}");
}

// ---------------------------------------------------------------------------
// export -> add round trip
// ---------------------------------------------------------------------------

/// Drop the leading timestamp column from an exported CSV, keeping the header
/// row, so the result is addable as a `single` series.
fn strip_timestamp_column(csv: &str) -> String {
    let mut out = String::new();
    for line in csv.lines() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        let rest = line.split_once(',').map(|(_, r)| r).unwrap_or(line);
        out.push_str(rest);
        out.push('\n');
    }
    out
}

#[test]
fn export_then_add_reproduces_the_values() {
    // `export` is the read-direction inverse of `add`, so exporting and re-adding
    // must reproduce the values. FINDING F10: the export is *timestamped*
    // (`timestamp,value`), while a `single` descriptor's CSV holds values only —
    // so the output is not directly re-addable as the same type. A caller must
    // either strip the timestamp column (below) or re-add as `non_sequential`.
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    seed(dir.path(), &store);

    let original = run(
        &store,
        &["-f", "csv", "get", "--owner-id", "42", "--name", "load"],
    );

    let exported = run(
        &store,
        &["-f", "csv", "export", "--owner-id", "42", "--name", "load"],
    );
    // The export carries a timestamp column and a header row.
    assert!(exported.lines().next().unwrap().starts_with("timestamp,"));
    assert_eq!(data_lines(&exported).len(), 4);

    // Re-add as a `single` after stripping the timestamp column.
    write(dir.path(), "values.csv", &strip_timestamp_column(&exported));
    let fresh = dir.path().join("fresh.nc");
    let descriptor = write(
        dir.path(),
        "re.json",
        &descriptor_json(&[("csv", "\"values.csv\""), ("has_header", "true")]),
    );
    add_ok(&fresh, &descriptor);

    assert_eq!(
        data_lines(&run(
            &fresh,
            &["-f", "csv", "get", "--owner-id", "42", "--name", "load"]
        )),
        data_lines(&original),
        "export -> add must reproduce the values"
    );

    // The timestamped export re-adds directly as a `non_sequential` series,
    // which is the shape it is already in; timestamps and values both survive.
    write(dir.path(), "stamped.csv", &exported);
    let ns_store = dir.path().join("ns.nc");
    let ns = write(
        dir.path(),
        "ns.json",
        &descriptor_json(&[
            ("csv", "\"stamped.csv\""),
            ("has_header", "true"),
            ("type", "\"non_sequential\""),
            ("initial_timestamp", "null"),
            ("resolution", "null"),
        ]),
    );
    add_ok(&ns_store, &ns);
    let ns_out = run(
        &ns_store,
        &["-f", "csv", "get", "--owner-id", "42", "--name", "load"],
    );
    // Same timestamps and same values as the export it came from.
    assert_eq!(data_lines(&ns_out), data_lines(&exported));

    // JSON export is valid JSON.
    let json = run(
        &store,
        &["-f", "json", "export", "--owner-id", "42", "--name", "load"],
    );
    serde_json::from_str::<serde_json::Value>(&json)
        .unwrap_or_else(|e| panic!("json export is not valid JSON: {e}\n{json}"));
}

#[test]
fn export_then_add_reproduces_a_non_f64_dtype() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    write(
        dir.path(),
        "i.csv",
        "-9223372036854775808\n0\n9223372036854775807\n",
    );
    let descriptor = write(
        dir.path(),
        "i.json",
        &descriptor_json(&[("csv", "\"i.csv\""), ("dtype", "\"i64\"")]),
    );
    add_ok(&store, &descriptor);

    let original = run(
        &store,
        &["-f", "csv", "get", "--owner-id", "42", "--name", "load"],
    );
    let exported = run(
        &store,
        &["-f", "csv", "export", "--owner-id", "42", "--name", "load"],
    );
    write(dir.path(), "values.csv", &strip_timestamp_column(&exported));

    let fresh = dir.path().join("fresh.nc");
    let re = write(
        dir.path(),
        "re.json",
        &descriptor_json(&[
            ("csv", "\"values.csv\""),
            ("dtype", "\"i64\""),
            ("has_header", "true"),
        ]),
    );
    add_ok(&fresh, &re);

    assert_eq!(
        data_lines(&run(
            &fresh,
            &["-f", "csv", "get", "--owner-id", "42", "--name", "load"]
        )),
        data_lines(&original),
        "i64 extremes must survive export -> add"
    );
    // And the extremes really are the extremes, not a lossy f64 detour.
    assert_eq!(
        data_lines(&original),
        vec!["-9223372036854775808", "0", "9223372036854775807"]
    );
}

#[test]
fn export_writes_one_file_per_series_into_a_directory() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    seed(dir.path(), &store);
    write(dir.path(), "b.csv", "90\n91\n92\n93\n");
    let second = write(
        dir.path(),
        "b.json",
        &descriptor_json(&[
            ("owner_id", "43"),
            ("name", "\"voltage\""),
            ("csv", "\"b.csv\""),
        ]),
    );
    add_ok(&store, &second);

    let out_dir = dir.path().join("out");
    fs::create_dir(&out_dir).unwrap();
    run(
        &store,
        &["-f", "csv", "export", "--dir", out_dir.to_str().unwrap()],
    );

    let written: Vec<String> = fs::read_dir(&out_dir)
        .unwrap()
        .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
        .collect();
    assert_eq!(written.len(), 2, "one file per series, got {written:?}");
    assert!(
        written.iter().any(|n| n.contains("load")),
        "got {written:?}"
    );
    assert!(
        written.iter().any(|n| n.contains("voltage")),
        "got {written:?}"
    );
}

#[test]
fn export_of_a_forecast_round_trips() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    write(dir.path(), "f.csv", "1\n2\n3\n4\n5\n6\n");
    let descriptor = write(
        dir.path(),
        "f.json",
        &descriptor_json(&[
            ("csv", "\"f.csv\""),
            ("type", "\"deterministic\""),
            ("horizon", "\"2h\""),
            ("interval", "\"1h\""),
            ("count", "3"),
        ]),
    );
    add_ok(&store, &descriptor);

    let exported = run(
        &store,
        &["-f", "csv", "export", "--owner-id", "42", "--name", "load"],
    );
    assert!(
        !data_lines(&exported).is_empty(),
        "a forecast export must have rows: {exported}"
    );
    // Timestamped forecast CSV: every data row starts with a timestamp.
    for line in data_lines(&exported) {
        let first = line.split(',').next().unwrap();
        assert!(
            first.contains("2024"),
            "forecast export rows must be timestamped, got {line:?}"
        );
    }
}

// ---------------------------------------------------------------------------
// get: time range, truncation, selectors, globs
// ---------------------------------------------------------------------------

#[test]
fn get_time_range_slices_the_series() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    write(dir.path(), "t.csv", "10\n11\n12\n13\n14\n15\n16\n17\n");
    let descriptor = write(
        dir.path(),
        "t.json",
        &descriptor_json(&[("csv", "\"t.csv\"")]),
    );
    add_ok(&store, &descriptor);

    // RFC3339 bounds.
    let out = run(
        &store,
        &[
            "-f",
            "csv",
            "get",
            "--owner-id",
            "42",
            "--name",
            "load",
            "--time-range",
            "2024-01-01T02:00:00Z..2024-01-01T05:00:00Z",
        ],
    );
    assert_eq!(data_lines(&out), vec!["12", "13", "14"]);

    // Epoch-ms bounds select the same window.
    let start_ms = 1_704_067_200_000i64 + 2 * 3_600_000;
    let end_ms = 1_704_067_200_000i64 + 5 * 3_600_000;
    let range = format!("{start_ms}..{end_ms}");
    let out = run(
        &store,
        &[
            "-f",
            "csv",
            "get",
            "--owner-id",
            "42",
            "--name",
            "load",
            "--time-range",
            &range,
        ],
    );
    assert_eq!(data_lines(&out), vec!["12", "13", "14"]);

    // A malformed range is rejected.
    for bad in [
        "not-a-range",
        "2024-01-01T00:00:00Z",
        "2024-01-01T00:00:00Z..",
        "..2024-01-01T05:00:00Z",
        "nope..alsonope",
    ] {
        run_err(
            &store,
            &[
                "get",
                "--owner-id",
                "42",
                "--name",
                "load",
                "--time-range",
                bad,
            ],
        );
    }
}

#[test]
fn get_limit_and_full_control_table_truncation() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    let body: String = (0..80).map(|i| format!("{i}\n")).collect();
    write(dir.path(), "big.csv", &body);
    let descriptor = write(
        dir.path(),
        "big.json",
        &descriptor_json(&[("csv", "\"big.csv\"")]),
    );
    add_ok(&store, &descriptor);

    // The default table output truncates and says so.
    let out = run(&store, &["get", "--owner-id", "42", "--name", "load"]);
    assert!(
        out.contains("80") || out.to_lowercase().contains("more") || out.contains("--full"),
        "truncated table output should mention the omitted rows: {out}"
    );

    // --limit shows fewer rows than --full.
    let limited = run(
        &store,
        &["get", "--owner-id", "42", "--name", "load", "--limit", "5"],
    );
    let full = run(
        &store,
        &["get", "--owner-id", "42", "--name", "load", "--full"],
    );
    assert!(
        limited.lines().count() < full.lines().count(),
        "--limit must show fewer lines than --full"
    );

    // CSV output is never truncated, whatever the limit.
    let csv = run(
        &store,
        &[
            "-f",
            "csv",
            "get",
            "--owner-id",
            "42",
            "--name",
            "load",
            "--limit",
            "5",
        ],
    );
    assert_eq!(data_lines(&csv).len(), 80, "csv output must be complete");
}

#[test]
fn a_selector_matching_zero_or_several_series_reports_which() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    seed(dir.path(), &store);
    write(dir.path(), "b.csv", "90\n91\n92\n93\n");
    let second = write(
        dir.path(),
        "b.json",
        &descriptor_json(&[("owner_id", "43"), ("csv", "\"b.csv\"")]),
    );
    add_ok(&store, &second);

    // Zero matches.
    let stderr = run_err(&store, &["get", "--name", "absent"]);
    assert!(stderr.contains("no time series matched"), "got: {stderr}");

    // Several matches: the message lists the candidates and names the flags that
    // would narrow it.
    let stderr = run_err(&store, &["get", "--name", "load"]);
    assert!(stderr.contains("2 time series matched"), "got: {stderr}");
    assert!(stderr.contains("owner=42"), "got: {stderr}");
    assert!(stderr.contains("owner=43"), "got: {stderr}");
    assert!(stderr.contains("--name-glob"), "got: {stderr}");

    // `info` uses the same resolver.
    run_err(&store, &["info", "--name", "load"]);
}

#[test]
fn glob_selector_edges() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    write(dir.path(), "g.csv", "1\n2\n3\n4\n");
    for (i, name) in ["wind_a", "wind_b", "solar", "Wind_c"].iter().enumerate() {
        let d = write(
            dir.path(),
            &format!("g{i}.json"),
            &descriptor_json(&[
                ("owner_id", &(i + 1).to_string()),
                ("name", &format!("\"{name}\"")),
                ("csv", "\"g.csv\""),
            ]),
        );
        add_ok(&store, &d);
    }

    let count = |args: &[&str]| run(&store, args).matches("\"owner_id\"").count();

    // `*` and `?`.
    assert_eq!(count(&["-f", "json", "list", "--name-glob", "wind_*"]), 2);
    assert_eq!(count(&["-f", "json", "list", "--name-glob", "wind_?"]), 2);
    // A character class.
    assert_eq!(
        count(&["-f", "json", "list", "--name-glob", "wind_[ab]"]),
        2
    );
    assert_eq!(count(&["-f", "json", "list", "--name-glob", "wind_[a]"]), 1);
    // GLOB is case-sensitive.
    assert_eq!(count(&["-f", "json", "list", "--name-glob", "Wind_*"]), 1);
    // Bare `*` matches everything.
    assert_eq!(count(&["-f", "json", "list", "--name-glob", "*"]), 4);
    // No match is an empty list, not an error.
    assert_eq!(count(&["-f", "json", "list", "--name-glob", "xyz*"]), 0);
    // A glob resolving to one series works with `get`.
    let out = run(&store, &["-f", "csv", "get", "--name-glob", "wind_[a]"]);
    assert_eq!(data_lines(&out), vec!["1", "2", "3", "4"]);
    // A glob resolving to several is a multi-match error.
    run_err(&store, &["get", "--name-glob", "wind_*"]);
}

// ---------------------------------------------------------------------------
// Store selection: INFRASTORE_STORE, precedence, and the missing-store error
// ---------------------------------------------------------------------------

#[test]
fn the_infrastore_store_env_var_is_used_when_no_flag_is_given() {
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    seed(dir.path(), &store);

    let output = Command::new(BIN)
        .env("INFRASTORE_STORE", &store)
        .args(["-f", "csv", "get", "--owner-id", "42", "--name", "load"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "INFRASTORE_STORE was not honored:\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert_eq!(
        data_lines(&String::from_utf8_lossy(&output.stdout)),
        vec!["10", "11", "12", "13"]
    );
}

#[test]
fn the_store_flag_beats_the_env_var() {
    let dir = tempfile::tempdir().unwrap();
    let flagged = dir.path().join("flagged.nc");
    seed(dir.path(), &flagged);

    // A second store with different values, pointed at by the env var.
    let env_store = dir.path().join("env.nc");
    write(dir.path(), "e.csv", "70\n71\n72\n73\n");
    let d = write(
        dir.path(),
        "e.json",
        &descriptor_json(&[("csv", "\"e.csv\"")]),
    );
    add_ok(&env_store, &d);

    let output = Command::new(BIN)
        .env("INFRASTORE_STORE", &env_store)
        .arg("--store")
        .arg(&flagged)
        .args(["-f", "csv", "get", "--owner-id", "42", "--name", "load"])
        .output()
        .unwrap();
    assert!(output.status.success());
    assert_eq!(
        data_lines(&String::from_utf8_lossy(&output.stdout)),
        vec!["10", "11", "12", "13"],
        "--store must win over INFRASTORE_STORE"
    );
}

#[test]
fn no_store_at_all_is_an_error() {
    let output = Command::new(BIN)
        .env_remove("INFRASTORE_STORE")
        .args(["list"])
        .output()
        .unwrap();
    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("missing --store"),
        "expected a missing-store diagnostic, got: {stderr}"
    );
}

#[test]
fn a_nonexistent_store_path_is_an_error_on_a_read_command() {
    let dir = tempfile::tempdir().unwrap();
    let missing = dir.path().join("no_such_store.nc");
    for args in [
        vec!["list"],
        vec!["stats"],
        vec!["verify"],
        vec!["resolutions"],
    ] {
        run_err(&missing, &args);
    }
}

// ---------------------------------------------------------------------------
// verify: the failing case and its exit code
// ---------------------------------------------------------------------------

#[test]
fn verify_exits_one_on_a_corrupt_store() {
    // Corrupt the *NetCDF* side — flip one stored element without touching the
    // recorded content hash — which is the corruption `verify_integrity`
    // detects. A healthy store exits 0; this one must exit exactly 1, since a
    // shell caller branches on that.
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    seed(dir.path(), &store);
    assert_eq!(exit_code(&store, &["verify"]), 0, "a healthy store exits 0");

    {
        let mut f = netcdf::append(&store).unwrap();
        let mut ts = f
            .group_mut("time_series")
            .unwrap()
            .expect("time_series group");
        let mut single = ts.group_mut("single").expect("single group");
        let dataset = {
            let read = netcdf::open(&store);
            drop(read);
            // Find the packed data variable (not its `_h` companion).
            single
                .variables()
                .map(|v| v.name())
                .find(|n| !n.ends_with("_h") && !n.starts_with("arr_"))
                .expect("a packed data variable")
        };
        let mut var = single.variable_mut(&dataset).expect("data variable");
        var.put_value(-999.5f64, [0, 0]).unwrap();
    }

    assert_eq!(
        exit_code(&store, &["verify"]),
        1,
        "a corrupt store must exit 1"
    );
    // `verify` reports through its normal output channel and *then* exits 1, so
    // the diagnostic is on stdout with no `Error: ` prefix.
    let (stdout, _stderr) = run_fail(&store, &["verify"]);
    assert!(
        stdout.to_lowercase().contains("hash") || stdout.to_lowercase().contains("integrity"),
        "expected an integrity report on stdout, got: {stdout}"
    );
    // The JSON form carries the same errors, for a scripted caller.
    let (stdout, _) = run_fail(&store, &["-f", "json", "verify"]);
    let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    let errors = parsed.get("errors").and_then(|v| v.as_array()).unwrap();
    assert_eq!(errors.len(), 1, "got: {stdout}");
    assert!(
        errors[0].as_str().unwrap().contains("hash mismatch"),
        "got: {stdout}"
    );
}

#[test]
fn verify_of_a_store_whose_catalog_was_corrupted_still_exits_zero() {
    // FINDING F3 (TEST_COVERAGE_PLAN.md §9): `verify_integrity` inspects only the
    // NetCDF half, so a `data_hash` corrupted in the SQLite catalog is invisible
    // to `infrastore verify` even though every read of that key now fails. Pinned here
    // at the CLI level because that is where a user would look.
    let dir = tempfile::tempdir().unwrap();
    let store = dir.path().join("store.nc");
    seed(dir.path(), &store);

    let mut sqlite = store.clone().into_os_string();
    sqlite.push(".sqlite");
    let conn = rusqlite::Connection::open(PathBuf::from(sqlite)).unwrap();
    let n = conn
        .execute(
            "UPDATE time_series_associations SET data_hash = ?1",
            [&"0".repeat(64)],
        )
        .unwrap();
    assert_eq!(n, 1);
    drop(conn);

    assert_eq!(
        exit_code(&store, &["verify"]),
        0,
        "PIN: a catalog-side corruption is invisible to `infrastore verify`"
    );
    // But the read genuinely fails, which is what verify failed to surface.
    run_err(&store, &["get", "--owner-id", "42", "--name", "load"]);
}