thread-flow 0.1.0

Thread dataflow integration for data processing pipelines, using CocoIndex.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
// SPDX-FileCopyrightText: 2025 Knitli Inc. <knitli@knit.li>
// SPDX-FileCopyrightText: 2026 Knitli Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Integration tests for the D1 incremental backend.
//!
//! Since D1 is accessed via HTTP REST API, these tests use `rusqlite` (in-memory
//! SQLite) to validate the SQL schema, query correctness, and data integrity.
//! The SQL statements match those used by the D1 backend exactly.
//!
//! This approach ensures:
//! - Schema migration SQL is valid SQLite
//! - All queries execute correctly against SQLite
//! - Upsert/conflict handling works as expected
//! - BLOB/INTEGER type conversions are correct
//! - Performance characteristics are validated locally

use recoco::utils::fingerprint::{Fingerprint, Fingerprinter};
use rusqlite::{Connection, params};
use std::time::Instant;

/// Creates an in-memory SQLite database with the D1 schema applied.
fn setup_db() -> Connection {
    let conn = Connection::open_in_memory().expect("Failed to open in-memory SQLite");
    // Enable foreign keys (required for CASCADE behavior).
    conn.execute_batch("PRAGMA foreign_keys = ON;")
        .expect("Failed to set PRAGMA");

    // Strip SQL comments and execute the full migration.
    // rusqlite's execute_batch handles multi-statement SQL with semicolons.
    let migration_sql = include_str!("../migrations/d1_incremental_v1.sql");
    let cleaned = strip_sql_comments(migration_sql);
    conn.execute_batch(&cleaned)
        .unwrap_or_else(|e| panic!("Migration failed: {e}\nSQL:\n{cleaned}"));

    conn
}

/// Strips SQL line comments (-- ...) from a SQL string.
/// Preserves the rest of the SQL including semicolons.
fn strip_sql_comments(sql: &str) -> String {
    sql.lines()
        .map(|line| {
            // Remove everything after `--` (line comment)
            if let Some(pos) = line.find("--") {
                &line[..pos]
            } else {
                line
            }
        })
        .collect::<Vec<_>>()
        .join("\n")
}

/// Creates a test fingerprint from content bytes.
fn make_fingerprint(content: &[u8]) -> Vec<u8> {
    let mut fp = Fingerprinter::default();
    fp.write_raw_bytes(content);
    let fingerprint = fp.into_fingerprint();
    fingerprint.as_slice().to_vec()
}

// ═══════════════════════════════════════════════════════════════════════════
// Schema Migration Tests
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_d1_migration_creates_all_tables() {
    let conn = setup_db();

    // Verify all three tables exist.
    let tables: Vec<String> = conn
        .prepare("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
        .unwrap()
        .query_map([], |row| row.get(0))
        .unwrap()
        .filter_map(|r| r.ok())
        .collect();

    assert!(
        tables.contains(&"analysis_fingerprints".to_string()),
        "Missing analysis_fingerprints table"
    );
    assert!(
        tables.contains(&"dependency_edges".to_string()),
        "Missing dependency_edges table"
    );
    assert!(
        tables.contains(&"source_files".to_string()),
        "Missing source_files table"
    );
}

#[test]
fn test_d1_migration_creates_indexes() {
    let conn = setup_db();

    let indexes: Vec<String> = conn
        .prepare("SELECT name FROM sqlite_master WHERE type='index' AND name LIKE 'idx_%'")
        .unwrap()
        .query_map([], |row| row.get(0))
        .unwrap()
        .filter_map(|r| r.ok())
        .collect();

    assert!(indexes.contains(&"idx_edges_from".to_string()));
    assert!(indexes.contains(&"idx_edges_to".to_string()));
    assert!(indexes.contains(&"idx_source_files_fp".to_string()));
    assert!(indexes.contains(&"idx_source_files_src".to_string()));
}

#[test]
fn test_d1_migration_is_idempotent() {
    let conn = setup_db();

    // Run migrations again - should not fail (IF NOT EXISTS).
    let migration_sql = include_str!("../migrations/d1_incremental_v1.sql");
    let cleaned = strip_sql_comments(migration_sql);
    conn.execute_batch(&cleaned)
        .unwrap_or_else(|e| panic!("Re-migration failed: {e}"));
}

// ═══════════════════════════════════════════════════════════════════════════
// Fingerprint CRUD Tests
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_d1_save_and_load_fingerprint() {
    let conn = setup_db();
    let fp_bytes = make_fingerprint(b"fn main() {}");

    // Insert fingerprint.
    conn.execute(
        "INSERT INTO analysis_fingerprints (file_path, content_fingerprint, last_analyzed) \
         VALUES (?1, ?2, ?3)",
        params!["src/main.rs", fp_bytes, 1_706_400_000_000_000_i64],
    )
    .unwrap();

    // Load it back.
    let (loaded_fp, loaded_ts): (Vec<u8>, Option<i64>) = conn
        .query_row(
            "SELECT content_fingerprint, last_analyzed FROM analysis_fingerprints WHERE file_path = ?1",
            params!["src/main.rs"],
            |row| Ok((row.get(0)?, row.get(1)?)),
        )
        .unwrap();

    assert_eq!(loaded_fp, fp_bytes);
    assert_eq!(loaded_ts, Some(1_706_400_000_000_000_i64));
}

#[test]
fn test_d1_fingerprint_upsert() {
    let conn = setup_db();
    let fp_v1 = make_fingerprint(b"version 1");
    let fp_v2 = make_fingerprint(b"version 2");

    // Insert v1.
    conn.execute(
        "INSERT INTO analysis_fingerprints (file_path, content_fingerprint, last_analyzed, updated_at) \
         VALUES (?1, ?2, ?3, strftime('%s', 'now')) \
         ON CONFLICT (file_path) DO UPDATE SET \
             content_fingerprint = excluded.content_fingerprint, \
             last_analyzed = excluded.last_analyzed, \
             updated_at = strftime('%s', 'now')",
        params!["file.rs", fp_v1, 100i64],
    )
    .unwrap();

    // Upsert v2 on the same path.
    conn.execute(
        "INSERT INTO analysis_fingerprints (file_path, content_fingerprint, last_analyzed, updated_at) \
         VALUES (?1, ?2, ?3, strftime('%s', 'now')) \
         ON CONFLICT (file_path) DO UPDATE SET \
             content_fingerprint = excluded.content_fingerprint, \
             last_analyzed = excluded.last_analyzed, \
             updated_at = strftime('%s', 'now')",
        params!["file.rs", fp_v2, 200i64],
    )
    .unwrap();

    // Verify v2 is stored (only 1 row).
    let count: i64 = conn
        .query_row("SELECT COUNT(*) FROM analysis_fingerprints", [], |row| {
            row.get(0)
        })
        .unwrap();
    assert_eq!(count, 1);

    let loaded_fp: Vec<u8> = conn
        .query_row(
            "SELECT content_fingerprint FROM analysis_fingerprints WHERE file_path = ?1",
            params!["file.rs"],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(loaded_fp, fp_v2);
}

#[test]
fn test_d1_fingerprint_delete() {
    let conn = setup_db();
    let fp = make_fingerprint(b"content");

    conn.execute(
        "INSERT INTO analysis_fingerprints (file_path, content_fingerprint) VALUES (?1, ?2)",
        params!["to_delete.rs", fp],
    )
    .unwrap();

    let changes = conn
        .execute(
            "DELETE FROM analysis_fingerprints WHERE file_path = ?1",
            params!["to_delete.rs"],
        )
        .unwrap();

    assert_eq!(changes, 1);

    let count: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM analysis_fingerprints WHERE file_path = ?1",
            params!["to_delete.rs"],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(count, 0);
}

#[test]
fn test_d1_fingerprint_load_nonexistent() {
    let conn = setup_db();

    let result = conn.query_row(
        "SELECT content_fingerprint FROM analysis_fingerprints WHERE file_path = ?1",
        params!["nonexistent.rs"],
        |row| row.get::<_, Vec<u8>>(0),
    );

    assert!(result.is_err()); // QueryReturnedNoRows
}

// ═══════════════════════════════════════════════════════════════════════════
// Source File Tracking Tests
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_d1_source_files_tracking() {
    let conn = setup_db();
    let fp = make_fingerprint(b"analysis result");

    // Insert fingerprint.
    conn.execute(
        "INSERT INTO analysis_fingerprints (file_path, content_fingerprint) VALUES (?1, ?2)",
        params!["main.rs", fp],
    )
    .unwrap();

    // Add source files.
    conn.execute(
        "INSERT INTO source_files (fingerprint_path, source_path) VALUES (?1, ?2)",
        params!["main.rs", "utils.rs"],
    )
    .unwrap();
    conn.execute(
        "INSERT INTO source_files (fingerprint_path, source_path) VALUES (?1, ?2)",
        params!["main.rs", "config.rs"],
    )
    .unwrap();

    // Load source files.
    let sources: Vec<String> = conn
        .prepare("SELECT source_path FROM source_files WHERE fingerprint_path = ?1")
        .unwrap()
        .query_map(params!["main.rs"], |row| row.get(0))
        .unwrap()
        .filter_map(|r| r.ok())
        .collect();

    assert_eq!(sources.len(), 2);
    assert!(sources.contains(&"utils.rs".to_string()));
    assert!(sources.contains(&"config.rs".to_string()));
}

#[test]
fn test_d1_source_files_cascade_delete() {
    let conn = setup_db();
    let fp = make_fingerprint(b"content");

    conn.execute(
        "INSERT INTO analysis_fingerprints (file_path, content_fingerprint) VALUES (?1, ?2)",
        params!["main.rs", fp],
    )
    .unwrap();
    conn.execute(
        "INSERT INTO source_files (fingerprint_path, source_path) VALUES (?1, ?2)",
        params!["main.rs", "dep.rs"],
    )
    .unwrap();

    // Delete the fingerprint - should cascade to source_files.
    conn.execute(
        "DELETE FROM analysis_fingerprints WHERE file_path = ?1",
        params!["main.rs"],
    )
    .unwrap();

    let count: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM source_files WHERE fingerprint_path = ?1",
            params!["main.rs"],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(
        count, 0,
        "CASCADE delete should remove source_files entries"
    );
}

// ═══════════════════════════════════════════════════════════════════════════
// Dependency Edge CRUD Tests
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_d1_save_and_load_edge() {
    let conn = setup_db();

    conn.execute(
        "INSERT INTO dependency_edges \
         (from_path, to_path, dep_type, symbol_from, symbol_to, symbol_kind, dependency_strength) \
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
        params![
            "main.rs",
            "utils.rs",
            "import",
            None::<String>,
            None::<String>,
            None::<String>,
            None::<String>
        ],
    )
    .unwrap();

    let rows: Vec<(String, String, String)> = conn
        .prepare("SELECT from_path, to_path, dep_type FROM dependency_edges WHERE from_path = ?1")
        .unwrap()
        .query_map(params!["main.rs"], |row| {
            Ok((row.get(0)?, row.get(1)?, row.get(2)?))
        })
        .unwrap()
        .filter_map(|r| r.ok())
        .collect();

    assert_eq!(rows.len(), 1);
    assert_eq!(
        rows[0],
        (
            "main.rs".to_string(),
            "utils.rs".to_string(),
            "import".to_string()
        )
    );
}

#[test]
fn test_d1_edge_upsert_on_conflict() {
    let conn = setup_db();

    // Insert edge without symbol.
    conn.execute(
        "INSERT INTO dependency_edges \
         (from_path, to_path, dep_type, symbol_from, symbol_to, symbol_kind, dependency_strength) \
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) \
         ON CONFLICT (from_path, to_path, dep_type) DO UPDATE SET \
             symbol_from = excluded.symbol_from, \
             symbol_to = excluded.symbol_to, \
             symbol_kind = excluded.symbol_kind, \
             dependency_strength = excluded.dependency_strength",
        params![
            "a.rs",
            "b.rs",
            "import",
            None::<String>,
            None::<String>,
            None::<String>,
            None::<String>
        ],
    )
    .unwrap();

    // Upsert same edge with symbol info.
    conn.execute(
        "INSERT INTO dependency_edges \
         (from_path, to_path, dep_type, symbol_from, symbol_to, symbol_kind, dependency_strength) \
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) \
         ON CONFLICT (from_path, to_path, dep_type) DO UPDATE SET \
             symbol_from = excluded.symbol_from, \
             symbol_to = excluded.symbol_to, \
             symbol_kind = excluded.symbol_kind, \
             dependency_strength = excluded.dependency_strength",
        params![
            "a.rs", "b.rs", "import", "main", "helper", "function", "strong"
        ],
    )
    .unwrap();

    // Should be 1 row with updated symbol info.
    let count: i64 = conn
        .query_row("SELECT COUNT(*) FROM dependency_edges", [], |row| {
            row.get(0)
        })
        .unwrap();
    assert_eq!(count, 1);

    let sym_from: Option<String> = conn
        .query_row(
            "SELECT symbol_from FROM dependency_edges WHERE from_path = ?1",
            params!["a.rs"],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(sym_from, Some("main".to_string()));
}

#[test]
fn test_d1_edge_with_symbol_data() {
    let conn = setup_db();

    conn.execute(
        "INSERT INTO dependency_edges \
         (from_path, to_path, dep_type, symbol_from, symbol_to, symbol_kind, dependency_strength) \
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
        params![
            "api.rs",
            "router.rs",
            "import",
            "handler",
            "Router",
            "class",
            "strong"
        ],
    )
    .unwrap();

    let (sym_from, sym_to, sym_kind, strength): (String, String, String, String) = conn
        .query_row(
            "SELECT symbol_from, symbol_to, symbol_kind, dependency_strength \
             FROM dependency_edges WHERE from_path = ?1",
            params!["api.rs"],
            |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)),
        )
        .unwrap();

    assert_eq!(sym_from, "handler");
    assert_eq!(sym_to, "Router");
    assert_eq!(sym_kind, "class");
    assert_eq!(strength, "strong");
}

#[test]
fn test_d1_load_edges_to() {
    let conn = setup_db();

    // Two files depend on utils.rs.
    conn.execute(
        "INSERT INTO dependency_edges (from_path, to_path, dep_type) VALUES (?1, ?2, ?3)",
        params!["main.rs", "utils.rs", "import"],
    )
    .unwrap();
    conn.execute(
        "INSERT INTO dependency_edges (from_path, to_path, dep_type) VALUES (?1, ?2, ?3)",
        params!["lib.rs", "utils.rs", "import"],
    )
    .unwrap();

    let dependents: Vec<String> = conn
        .prepare("SELECT from_path FROM dependency_edges WHERE to_path = ?1")
        .unwrap()
        .query_map(params!["utils.rs"], |row| row.get(0))
        .unwrap()
        .filter_map(|r| r.ok())
        .collect();

    assert_eq!(dependents.len(), 2);
    assert!(dependents.contains(&"main.rs".to_string()));
    assert!(dependents.contains(&"lib.rs".to_string()));
}

#[test]
fn test_d1_delete_edges_for_file() {
    let conn = setup_db();

    // a.rs -> b.rs, c.rs -> a.rs, d.rs -> e.rs
    conn.execute(
        "INSERT INTO dependency_edges (from_path, to_path, dep_type) VALUES (?1, ?2, ?3)",
        params!["a.rs", "b.rs", "import"],
    )
    .unwrap();
    conn.execute(
        "INSERT INTO dependency_edges (from_path, to_path, dep_type) VALUES (?1, ?2, ?3)",
        params!["c.rs", "a.rs", "import"],
    )
    .unwrap();
    conn.execute(
        "INSERT INTO dependency_edges (from_path, to_path, dep_type) VALUES (?1, ?2, ?3)",
        params!["d.rs", "e.rs", "import"],
    )
    .unwrap();

    let changes = conn
        .execute(
            "DELETE FROM dependency_edges WHERE from_path = ?1 OR to_path = ?1",
            params!["a.rs"],
        )
        .unwrap();

    assert_eq!(changes, 2); // Both edges involving a.rs

    let remaining: i64 = conn
        .query_row("SELECT COUNT(*) FROM dependency_edges", [], |row| {
            row.get(0)
        })
        .unwrap();
    assert_eq!(remaining, 1); // Only d.rs -> e.rs remains
}

// ═══════════════════════════════════════════════════════════════════════════
// Full Graph Save/Load Tests
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_d1_full_graph_roundtrip() {
    let conn = setup_db();
    let fp_a = make_fingerprint(b"file a");
    let fp_b = make_fingerprint(b"file b");
    let fp_c = make_fingerprint(b"file c");

    // Save fingerprints.
    for (path, fp) in [("a.rs", &fp_a), ("b.rs", &fp_b), ("c.rs", &fp_c)] {
        conn.execute(
            "INSERT INTO analysis_fingerprints (file_path, content_fingerprint, last_analyzed) \
             VALUES (?1, ?2, ?3)",
            params![path, fp, 1000i64],
        )
        .unwrap();
    }

    // Save source files.
    conn.execute(
        "INSERT INTO source_files (fingerprint_path, source_path) VALUES (?1, ?2)",
        params!["a.rs", "dep1.rs"],
    )
    .unwrap();

    // Save edges.
    conn.execute(
        "INSERT INTO dependency_edges (from_path, to_path, dep_type) VALUES (?1, ?2, ?3)",
        params!["a.rs", "b.rs", "import"],
    )
    .unwrap();
    conn.execute(
        "INSERT INTO dependency_edges (from_path, to_path, dep_type) VALUES (?1, ?2, ?3)",
        params!["b.rs", "c.rs", "import"],
    )
    .unwrap();

    // Load and verify.
    let fp_count: i64 = conn
        .query_row("SELECT COUNT(*) FROM analysis_fingerprints", [], |row| {
            row.get(0)
        })
        .unwrap();
    assert_eq!(fp_count, 3);

    let edge_count: i64 = conn
        .query_row("SELECT COUNT(*) FROM dependency_edges", [], |row| {
            row.get(0)
        })
        .unwrap();
    assert_eq!(edge_count, 2);

    let src_count: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM source_files WHERE fingerprint_path = ?1",
            params!["a.rs"],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(src_count, 1);
}

#[test]
fn test_d1_full_graph_clear_and_replace() {
    let conn = setup_db();
    let fp = make_fingerprint(b"old data");

    // Insert initial data.
    conn.execute(
        "INSERT INTO analysis_fingerprints (file_path, content_fingerprint) VALUES (?1, ?2)",
        params!["old.rs", fp],
    )
    .unwrap();
    conn.execute(
        "INSERT INTO dependency_edges (from_path, to_path, dep_type) VALUES (?1, ?2, ?3)",
        params!["old.rs", "dep.rs", "import"],
    )
    .unwrap();

    // Clear all data (D1 uses DELETE, not TRUNCATE).
    conn.execute_batch(
        "DELETE FROM source_files; \
         DELETE FROM dependency_edges; \
         DELETE FROM analysis_fingerprints;",
    )
    .unwrap();

    let fp_count: i64 = conn
        .query_row("SELECT COUNT(*) FROM analysis_fingerprints", [], |row| {
            row.get(0)
        })
        .unwrap();
    assert_eq!(fp_count, 0);

    let edge_count: i64 = conn
        .query_row("SELECT COUNT(*) FROM dependency_edges", [], |row| {
            row.get(0)
        })
        .unwrap();
    assert_eq!(edge_count, 0);
}

// ═══════════════════════════════════════════════════════════════════════════
// BLOB/INTEGER Conversion Tests
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_d1_blob_fingerprint_roundtrip() {
    let conn = setup_db();
    let fp_bytes = make_fingerprint(b"test content for blob roundtrip");

    // Insert as BLOB.
    conn.execute(
        "INSERT INTO analysis_fingerprints (file_path, content_fingerprint) VALUES (?1, ?2)",
        params!["blob_test.rs", fp_bytes],
    )
    .unwrap();

    // Read back as BLOB.
    let loaded: Vec<u8> = conn
        .query_row(
            "SELECT content_fingerprint FROM analysis_fingerprints WHERE file_path = ?1",
            params!["blob_test.rs"],
            |row| row.get(0),
        )
        .unwrap();

    assert_eq!(loaded.len(), 16, "Fingerprint must be 16 bytes");
    assert_eq!(loaded, fp_bytes);

    // Verify it can be converted back to a Fingerprint.
    let arr: [u8; 16] = loaded.try_into().unwrap();
    let restored = Fingerprint(arr);
    assert_eq!(restored.as_slice(), &fp_bytes[..]);
}

#[test]
fn test_d1_integer_timestamp_handling() {
    let conn = setup_db();
    let fp = make_fingerprint(b"timestamp test");

    // Test with large Unix microsecond timestamp.
    let timestamp: i64 = 1_706_400_000_000_000; // 2024-01-28 in microseconds

    conn.execute(
        "INSERT INTO analysis_fingerprints (file_path, content_fingerprint, last_analyzed) \
         VALUES (?1, ?2, ?3)",
        params!["ts_test.rs", fp, timestamp],
    )
    .unwrap();

    let loaded: i64 = conn
        .query_row(
            "SELECT last_analyzed FROM analysis_fingerprints WHERE file_path = ?1",
            params!["ts_test.rs"],
            |row| row.get(0),
        )
        .unwrap();

    assert_eq!(loaded, timestamp);
}

#[test]
fn test_d1_null_timestamp() {
    let conn = setup_db();
    let fp = make_fingerprint(b"null ts");

    conn.execute(
        "INSERT INTO analysis_fingerprints (file_path, content_fingerprint, last_analyzed) \
         VALUES (?1, ?2, NULL)",
        params!["null_ts.rs", fp],
    )
    .unwrap();

    let loaded: Option<i64> = conn
        .query_row(
            "SELECT last_analyzed FROM analysis_fingerprints WHERE file_path = ?1",
            params!["null_ts.rs"],
            |row| row.get(0),
        )
        .unwrap();

    assert!(loaded.is_none());
}

// ═══════════════════════════════════════════════════════════════════════════
// Performance Validation Tests
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_d1_performance_single_fingerprint_op() {
    let conn = setup_db();

    // Insert 100 fingerprints.
    let start = Instant::now();
    for i in 0..100 {
        let fp = make_fingerprint(format!("content {i}").as_bytes());
        conn.execute(
            "INSERT INTO analysis_fingerprints (file_path, content_fingerprint, last_analyzed) \
             VALUES (?1, ?2, ?3)",
            params![format!("file_{i}.rs"), fp, i as i64],
        )
        .unwrap();
    }
    let insert_duration = start.elapsed();

    // Lookup performance.
    let start = Instant::now();
    for i in 0..100 {
        let _: Vec<u8> = conn
            .query_row(
                "SELECT content_fingerprint FROM analysis_fingerprints WHERE file_path = ?1",
                params![format!("file_{i}.rs")],
                |row| row.get(0),
            )
            .unwrap();
    }
    let lookup_duration = start.elapsed();

    // SQLite in-memory should be much faster than the 50ms D1 target.
    // This validates the query structure is efficient.
    assert!(
        insert_duration.as_millis() < 500,
        "100 inserts took {}ms (should be <500ms even on SQLite)",
        insert_duration.as_millis()
    );
    assert!(
        lookup_duration.as_millis() < 100,
        "100 lookups took {}ms (should be <100ms on SQLite)",
        lookup_duration.as_millis()
    );
}

#[test]
fn test_d1_performance_edge_traversal() {
    let conn = setup_db();

    // Create a graph with 100 edges.
    for i in 0..100 {
        conn.execute(
            "INSERT INTO dependency_edges (from_path, to_path, dep_type) VALUES (?1, ?2, ?3)",
            params![
                format!("file_{i}.rs"),
                format!("dep_{}.rs", i % 10),
                "import"
            ],
        )
        .unwrap();
    }

    // Measure forward traversal.
    let start = Instant::now();
    for i in 0..100 {
        let _: Vec<String> = conn
            .prepare("SELECT to_path FROM dependency_edges WHERE from_path = ?1")
            .unwrap()
            .query_map(params![format!("file_{i}.rs")], |row| row.get(0))
            .unwrap()
            .filter_map(|r| r.ok())
            .collect();
    }
    let forward_duration = start.elapsed();

    // Measure reverse traversal (dependents lookup).
    let start = Instant::now();
    for i in 0..10 {
        let _: Vec<String> = conn
            .prepare("SELECT from_path FROM dependency_edges WHERE to_path = ?1")
            .unwrap()
            .query_map(params![format!("dep_{i}.rs")], |row| row.get(0))
            .unwrap()
            .filter_map(|r| r.ok())
            .collect();
    }
    let reverse_duration = start.elapsed();

    assert!(
        forward_duration.as_millis() < 200,
        "100 forward lookups took {}ms",
        forward_duration.as_millis()
    );
    assert!(
        reverse_duration.as_millis() < 50,
        "10 reverse lookups took {}ms",
        reverse_duration.as_millis()
    );
}

// ═══════════════════════════════════════════════════════════════════════════
// Batch Operation Tests
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_d1_batch_edge_insertion() {
    let conn = setup_db();

    // Simulate batch insertion (D1 sends multiple individual statements).
    let edges = vec![
        ("a.rs", "b.rs", "import"),
        ("a.rs", "c.rs", "import"),
        ("b.rs", "c.rs", "trait"),
        ("c.rs", "d.rs", "type"),
    ];

    for (from, to, dep_type) in &edges {
        conn.execute(
            "INSERT INTO dependency_edges \
             (from_path, to_path, dep_type) \
             VALUES (?1, ?2, ?3) \
             ON CONFLICT (from_path, to_path, dep_type) DO NOTHING",
            params![from, to, dep_type],
        )
        .unwrap();
    }

    let count: i64 = conn
        .query_row("SELECT COUNT(*) FROM dependency_edges", [], |row| {
            row.get(0)
        })
        .unwrap();
    assert_eq!(count, 4);
}

#[test]
fn test_d1_unique_constraint_prevents_duplicate_edges() {
    let conn = setup_db();

    conn.execute(
        "INSERT INTO dependency_edges (from_path, to_path, dep_type) VALUES (?1, ?2, ?3)",
        params!["a.rs", "b.rs", "import"],
    )
    .unwrap();

    // Same edge should fail (UNIQUE constraint).
    let result = conn.execute(
        "INSERT INTO dependency_edges (from_path, to_path, dep_type) VALUES (?1, ?2, ?3)",
        params!["a.rs", "b.rs", "import"],
    );
    assert!(
        result.is_err(),
        "Duplicate edge should violate UNIQUE constraint"
    );

    // But same files with different dep_type should succeed.
    conn.execute(
        "INSERT INTO dependency_edges (from_path, to_path, dep_type) VALUES (?1, ?2, ?3)",
        params!["a.rs", "b.rs", "type"],
    )
    .unwrap();

    let count: i64 = conn
        .query_row("SELECT COUNT(*) FROM dependency_edges", [], |row| {
            row.get(0)
        })
        .unwrap();
    assert_eq!(count, 2);
}

// ═══════════════════════════════════════════════════════════════════════════
// Edge Case Tests
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_d1_empty_fingerprint_content() {
    let conn = setup_db();
    let fp = make_fingerprint(b"");

    conn.execute(
        "INSERT INTO analysis_fingerprints (file_path, content_fingerprint) VALUES (?1, ?2)",
        params!["empty.rs", fp],
    )
    .unwrap();

    let loaded: Vec<u8> = conn
        .query_row(
            "SELECT content_fingerprint FROM analysis_fingerprints WHERE file_path = ?1",
            params!["empty.rs"],
            |row| row.get(0),
        )
        .unwrap();

    assert_eq!(loaded.len(), 16);
    assert_eq!(loaded, fp);
}

#[test]
fn test_d1_path_with_special_characters() {
    let conn = setup_db();
    let fp = make_fingerprint(b"special path content");

    // Paths with spaces, dots, and non-ASCII.
    let paths = [
        "src/my module/file.rs",
        "src/../lib.rs",
        "src/unicode\u{00e9}.rs",
    ];

    for path in &paths {
        conn.execute(
            "INSERT INTO analysis_fingerprints (file_path, content_fingerprint) VALUES (?1, ?2)",
            params![path, fp],
        )
        .unwrap();
    }

    let count: i64 = conn
        .query_row("SELECT COUNT(*) FROM analysis_fingerprints", [], |row| {
            row.get(0)
        })
        .unwrap();
    assert_eq!(count, 3);
}