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
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
// SPDX-FileCopyrightText: 2025 Knitli Inc. <knitli@knit.li>
// SPDX-FileCopyrightText: 2026 Knitli Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Real-World Codebase Validation Tests (Phase 5.5)
//!
//! Validates the incremental analysis system on large-scale codebases (10K+ files)
//! across multiple programming languages. Uses a hybrid approach:
//! - Synthetic scale tests (generated 10K+ file fixtures)
//! - Real-world pattern tests (code samples from major open-source projects)
//!
//! ## Test Coverage (16 tests)
//!
//! 1. **Scale Tests** (4 tests): 10K+ files per language, performance validation
//! 2. **Pattern Tests** (8 tests): Real-world code patterns, edge cases
//! 3. **Performance Tests** (4 tests): Cold start, incremental updates, cache efficiency
//!
//! ## Constitutional Requirements (Principle VI)
//!
//! - Content-addressed caching: >90% hit rate
//! - Storage latency: <10ms (Postgres), <50ms (D1)
//! - Incremental updates: Only affected components reanalyzed
//!
//! ## Success Criteria
//!
//! - All tests pass with `cargo nextest run --all-features`
//! - Performance targets met at 10K+ file scale
//! - Edge cases discovered and documented
//! - Validation report generated in claudedocs/REAL_WORLD_VALIDATION.md

use std::path::{Path, PathBuf};
use std::time::Instant;
use thread_flow::incremental::analyzer::IncrementalAnalyzer;
use thread_flow::incremental::dependency_builder::DependencyGraphBuilder;
use thread_flow::incremental::storage::InMemoryStorage;
use tokio::fs;
use tokio::io::AsyncWriteExt;

// ═══════════════════════════════════════════════════════════════════════════
// Test Fixtures
// ═══════════════════════════════════════════════════════════════════════════

/// Test fixture for real-world validation tests.
///
/// Provides infrastructure for large-scale testing including:
/// - Temporary directory management
/// - Large-scale file generation (10K+ files)
/// - Performance measurement utilities
/// - Real-world pattern templates
struct ValidationFixture {
    /// Temporary directory for test files
    temp_dir: tempfile::TempDir,
    /// Analyzer with InMemory storage
    analyzer: IncrementalAnalyzer,
    /// Dependency graph builder
    builder: DependencyGraphBuilder,
}

impl ValidationFixture {
    /// Creates a new validation fixture.
    async fn new() -> Self {
        let temp_dir = tempfile::tempdir().expect("create temp dir");

        let analyzer_storage = InMemoryStorage::new();
        let analyzer = IncrementalAnalyzer::new(Box::new(analyzer_storage));

        let builder_storage = InMemoryStorage::new();
        let builder = DependencyGraphBuilder::new(Box::new(builder_storage));

        Self {
            temp_dir,
            analyzer,
            builder,
        }
    }

    /// Returns the path to the temporary directory.
    fn temp_path(&self) -> &Path {
        self.temp_dir.path()
    }

    /// Creates a test file with the given content.
    async fn create_file(&self, relative_path: &str, content: &str) -> PathBuf {
        let file_path = self.temp_path().join(relative_path);

        // Create parent directories if needed
        if let Some(parent) = file_path.parent() {
            fs::create_dir_all(parent).await.expect("create parent dir");
        }

        // Write file content
        let mut file = fs::File::create(&file_path).await.expect("create file");
        file.write_all(content.as_bytes())
            .await
            .expect("write file");
        file_path
    }

    /// Updates an existing test file with new content.
    async fn update_file(&self, file_path: &Path, content: &str) {
        let mut file = fs::File::create(file_path).await.expect("open file");
        file.write_all(content.as_bytes())
            .await
            .expect("write file");
    }

    /// Analyzes changes and extracts dependencies (E2E workflow).
    async fn analyze_and_extract(
        &mut self,
        paths: &[PathBuf],
    ) -> thread_flow::incremental::analyzer::AnalysisResult {
        // Step 1: Analyze changes (fingerprinting)
        let result = self
            .analyzer
            .analyze_changes(paths)
            .await
            .expect("analyze changes");

        // Step 2: Extract dependencies (graph building)
        self.builder
            .extract_files(paths)
            .await
            .expect("extract dependencies");

        // Step 3: Sync builder's graph to analyzer's graph
        let builder_graph = self.builder.graph();
        let analyzer_graph = self.analyzer.graph_mut();

        for edge in &builder_graph.edges {
            analyzer_graph.add_edge(edge.clone());
        }

        result
    }

    /// Generates a large-scale Rust codebase (10K+ files).
    ///
    /// Creates a synthetic project structure simulating a large Rust application:
    /// - Multiple modules organized hierarchically
    /// - Realistic import patterns (use statements)
    /// - Mix of library and binary crates
    async fn generate_rust_scale(&self, file_count: usize) -> Vec<PathBuf> {
        let mut paths = Vec::new();

        // Calculate module structure (10 top-level modules, 100 submodules each)
        let modules_per_level = (file_count as f64).sqrt() as usize;
        let files_per_module = file_count / modules_per_level;

        for module_idx in 0..modules_per_level {
            let module_name = format!("module_{}", module_idx);

            // Create module directory
            let module_dir = self.temp_path().join(&module_name);
            fs::create_dir_all(&module_dir)
                .await
                .expect("create module");

            // Create mod.rs for the module
            let mod_file = module_dir.join("mod.rs");
            let mut mod_content = String::from("// Module exports\n\n");

            for file_idx in 0..files_per_module {
                let file_name = format!("file_{}.rs", file_idx);
                mod_content.push_str(&format!("pub mod file_{};\n", file_idx));

                // Create source file with imports
                let file_path = module_dir.join(&file_name);
                let content = format!(
                    "// File {} in module {}\n\
                     use std::collections::HashMap;\n\
                     use crate::{}::mod;\n\
                     \n\
                     pub fn function_{}() -> HashMap<String, i32> {{\n\
                         HashMap::new()\n\
                     }}\n",
                    file_idx, module_idx, module_name, file_idx
                );

                let mut file = fs::File::create(&file_path).await.expect("create file");
                file.write_all(content.as_bytes())
                    .await
                    .expect("write file");

                paths.push(file_path);
            }

            // Write mod.rs
            let mut file = fs::File::create(&mod_file).await.expect("create mod.rs");
            file.write_all(mod_content.as_bytes())
                .await
                .expect("write mod.rs");
            paths.push(mod_file);
        }

        paths
    }

    /// Generates a large-scale TypeScript codebase (10K+ files).
    async fn generate_typescript_scale(&self, file_count: usize) -> Vec<PathBuf> {
        let mut paths = Vec::new();
        let modules_per_level = (file_count as f64).sqrt() as usize;
        let files_per_module = file_count / modules_per_level;

        for module_idx in 0..modules_per_level {
            let module_name = format!("module_{}", module_idx);
            let module_dir = self.temp_path().join(&module_name);
            fs::create_dir_all(&module_dir)
                .await
                .expect("create module");

            // Create index.ts for module
            let index_file = module_dir.join("index.ts");
            let mut index_content = String::from("// Module exports\n\n");

            for file_idx in 0..files_per_module {
                let file_name = format!("file_{}.ts", file_idx);
                index_content.push_str(&format!("export * from './file_{}';\n", file_idx));

                let file_path = module_dir.join(&file_name);
                let content = format!(
                    "// File {} in module {}\n\
                     import {{ Map }} from './index';\n\
                     \n\
                     export class Component_{} {{\n\
                         private data: Map<string, number> = new Map();\n\
                         \n\
                         public process(): void {{\n\
                             // Processing logic\n\
                         }}\n\
                     }}\n",
                    file_idx, module_idx, file_idx
                );

                let mut file = fs::File::create(&file_path).await.expect("create file");
                file.write_all(content.as_bytes())
                    .await
                    .expect("write file");
                paths.push(file_path);
            }

            let mut file = fs::File::create(&index_file)
                .await
                .expect("create index.ts");
            file.write_all(index_content.as_bytes())
                .await
                .expect("write index.ts");
            paths.push(index_file);
        }

        paths
    }

    /// Generates a large-scale Python codebase (10K+ files).
    async fn generate_python_scale(&self, file_count: usize) -> Vec<PathBuf> {
        let mut paths = Vec::new();
        let packages = (file_count as f64).sqrt() as usize;
        let files_per_package = file_count / packages;

        for pkg_idx in 0..packages {
            let pkg_name = format!("package_{}", pkg_idx);
            let pkg_dir = self.temp_path().join(&pkg_name);
            fs::create_dir_all(&pkg_dir).await.expect("create package");

            // Create __init__.py
            let init_file = pkg_dir.join("__init__.py");
            let mut init_content = String::from("# Package exports\n\n");

            for file_idx in 0..files_per_package {
                let file_name = format!("module_{}.py", file_idx);
                init_content.push_str(&format!("from .module_{} import *\n", file_idx));

                let file_path = pkg_dir.join(&file_name);
                let content = format!(
                    "# Module {} in package {}\n\
                     from typing import Dict\n\
                     from . import __init__\n\
                     \n\
                     class Service_{}:\n\
                         def __init__(self):\n\
                             self.data: Dict[str, int] = {{}}\n\
                         \n\
                         def process(self) -> None:\n\
                             pass\n",
                    file_idx, pkg_idx, file_idx
                );

                let mut file = fs::File::create(&file_path).await.expect("create file");
                file.write_all(content.as_bytes())
                    .await
                    .expect("write file");
                paths.push(file_path);
            }

            let mut file = fs::File::create(&init_file)
                .await
                .expect("create __init__.py");
            file.write_all(init_content.as_bytes())
                .await
                .expect("write __init__.py");
            paths.push(init_file);
        }

        paths
    }

    /// Generates a large-scale Go codebase (10K+ files).
    async fn generate_go_scale(&self, file_count: usize) -> Vec<PathBuf> {
        let mut paths = Vec::new();
        let packages = (file_count as f64).sqrt() as usize;
        let files_per_package = file_count / packages;

        for pkg_idx in 0..packages {
            let pkg_name = format!("pkg{}", pkg_idx);
            let pkg_dir = self.temp_path().join(&pkg_name);
            fs::create_dir_all(&pkg_dir).await.expect("create package");

            for file_idx in 0..files_per_package {
                let file_name = format!("file_{}.go", file_idx);
                let file_path = pkg_dir.join(&file_name);

                let content = format!(
                    "// File {} in package {}\n\
                     package {}\n\
                     \n\
                     import \"fmt\"\n\
                     \n\
                     type Service_{} struct {{\n\
                         Data map[string]int\n\
                     }}\n\
                     \n\
                     func (s *Service_{}) Process() {{\n\
                         fmt.Println(\"processing\")\n\
                     }}\n",
                    file_idx, pkg_name, pkg_name, file_idx, file_idx
                );

                let mut file = fs::File::create(&file_path).await.expect("create file");
                file.write_all(content.as_bytes())
                    .await
                    .expect("write file");
                paths.push(file_path);
            }
        }

        paths
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// Scale Tests (4 tests)
// ═══════════════════════════════════════════════════════════════════════════

/// Validates incremental analysis on large-scale Rust codebase (10K+ files).
///
/// Simulates a project like tokio with:
/// - Multiple modules organized hierarchically
/// - Realistic import patterns (std, crate, external)
/// - 10,000+ source files
///
/// **Performance Targets**:
/// - Initial analysis: <10s for 10K files
/// - Incremental update (1% change): <1s
/// - Cache hit rate: >90%
#[tokio::test]
async fn test_real_world_rust_scale() {
    let mut fixture = ValidationFixture::new().await;

    // Generate 10K Rust files
    let start = Instant::now();
    let paths = fixture.generate_rust_scale(10_000).await;
    let generation_time = start.elapsed();
    println!(
        "Generated {} Rust files in {:?}",
        paths.len(),
        generation_time
    );

    assert!(
        paths.len() >= 10_000,
        "Expected at least 10K files, got {}",
        paths.len()
    );

    // Initial analysis (cold start)
    let start = Instant::now();
    let result = fixture.analyze_and_extract(&paths).await;
    let analysis_time = start.elapsed();
    println!(
        "Initial analysis of {} files in {:?}",
        paths.len(),
        analysis_time
    );

    // Validate results
    assert!(
        result.changed_files.len() >= 10_000,
        "Expected >=10K changed files, got {}",
        result.changed_files.len()
    );

    // Performance validation: <10s for 10K files
    assert!(
        analysis_time.as_secs() < 10,
        "Initial analysis took {:?}, exceeds 10s target",
        analysis_time
    );

    // Validate dependency graph populated
    let graph = fixture.builder.graph();
    assert!(
        graph.node_count() >= 10_000,
        "Expected >=10K nodes, got {}",
        graph.node_count()
    );

    // Test incremental update (1% change)
    let changed_count = (paths.len() as f64 * 0.01) as usize;
    let changed_paths: Vec<_> = paths.iter().take(changed_count).cloned().collect();

    for path in &changed_paths {
        fixture
            .update_file(path, "// Updated content\npub fn updated() {}")
            .await;
    }

    let start = Instant::now();
    let result = fixture.analyze_and_extract(&changed_paths).await;
    let incremental_time = start.elapsed();
    println!(
        "Incremental update of {} files in {:?}",
        changed_count, incremental_time
    );

    // Validate incremental efficiency
    assert!(
        result.changed_files.len() == changed_count,
        "Expected {} changed files, got {}",
        changed_count,
        result.changed_files.len()
    );

    // Performance validation: <1s for 1% update
    assert!(
        incremental_time.as_secs() < 1,
        "Incremental update took {:?}, exceeds 1s target",
        incremental_time
    );

    // Cache hit rate is already computed in AnalysisResult
    println!("Cache hit rate: {:.1}%", result.cache_hit_rate * 100.0);
}

/// Validates incremental analysis on large-scale TypeScript codebase (10K+ files).
///
/// Simulates a project like VSCode with:
/// - ES6 module system (import/export)
/// - Class-based architecture
/// - 10,000+ TypeScript files
#[tokio::test]
async fn test_real_world_typescript_scale() {
    let mut fixture = ValidationFixture::new().await;

    // Generate 10K TypeScript files
    let start = Instant::now();
    let paths = fixture.generate_typescript_scale(10_000).await;
    let generation_time = start.elapsed();
    println!(
        "Generated {} TypeScript files in {:?}",
        paths.len(),
        generation_time
    );

    assert!(
        paths.len() >= 10_000,
        "Expected at least 10K files, got {}",
        paths.len()
    );

    // Initial analysis
    let start = Instant::now();
    let result = fixture.analyze_and_extract(&paths).await;
    let analysis_time = start.elapsed();
    println!(
        "Initial analysis of {} files in {:?}",
        paths.len(),
        analysis_time
    );

    assert!(result.changed_files.len() >= 10_000);
    // TypeScript parsing is slowest at scale - allow 20s for 10K files
    assert!(
        analysis_time.as_secs() < 20,
        "TypeScript analysis time {:?} exceeded 20s",
        analysis_time
    );

    let graph = fixture.builder.graph();
    assert!(graph.node_count() >= 10_000);
}

/// Validates incremental analysis on large-scale Python codebase (10K+ files).
///
/// Simulates a project like Django with:
/// - Package-based structure (__init__.py)
/// - Import system (from ... import)
/// - 10,000+ Python modules
#[tokio::test]
async fn test_real_world_python_scale() {
    let mut fixture = ValidationFixture::new().await;

    // Generate 10K Python files
    let start = Instant::now();
    let paths = fixture.generate_python_scale(10_000).await;
    let generation_time = start.elapsed();
    println!(
        "Generated {} Python files in {:?}",
        paths.len(),
        generation_time
    );

    assert!(paths.len() >= 10_000);

    // Initial analysis
    let start = Instant::now();
    let result = fixture.analyze_and_extract(&paths).await;
    let analysis_time = start.elapsed();
    println!(
        "Initial analysis of {} files in {:?}",
        paths.len(),
        analysis_time
    );

    assert!(result.changed_files.len() >= 10_000);
    // Python parsing is slower at scale - allow 15s for 10K files
    assert!(
        analysis_time.as_secs() < 15,
        "Python analysis time {:?} exceeded 15s",
        analysis_time
    );

    let graph = fixture.builder.graph();
    assert!(graph.node_count() >= 10_000);
}

/// Validates incremental analysis on large-scale Go codebase (10K+ files).
///
/// Simulates a project like Kubernetes with:
/// - Package-based organization
/// - Interface-driven design
/// - 10,000+ Go source files
#[tokio::test]
async fn test_real_world_go_scale() {
    let mut fixture = ValidationFixture::new().await;

    // Generate 10K Go files
    let start = Instant::now();
    let paths = fixture.generate_go_scale(10_000).await;
    let generation_time = start.elapsed();
    println!(
        "Generated {} Go files in {:?}",
        paths.len(),
        generation_time
    );

    assert!(paths.len() >= 10_000);

    // Initial analysis
    let start = Instant::now();
    let result = fixture.analyze_and_extract(&paths).await;
    let analysis_time = start.elapsed();
    println!(
        "Initial analysis of {} files in {:?}",
        paths.len(),
        analysis_time
    );

    assert!(result.changed_files.len() >= 10_000);
    assert!(analysis_time.as_secs() < 10);

    let graph = fixture.builder.graph();
    assert!(graph.node_count() >= 10_000);
}

// ═══════════════════════════════════════════════════════════════════════════
// Pattern Tests (8 tests)
// ═══════════════════════════════════════════════════════════════════════════

/// Validates handling of tokio-like async Rust patterns.
///
/// Tests real-world patterns found in tokio:
/// - Async traits and impl blocks
/// - Macro-heavy code (tokio::main, tokio::test)
/// - Complex module re-exports
#[tokio::test]
async fn test_real_world_rust_patterns() {
    let mut fixture = ValidationFixture::new().await;

    // Create tokio-like async code
    let runtime_rs = fixture
        .create_file(
            "runtime.rs",
            "use std::sync::Arc;\n\
             use tokio::sync::Mutex;\n\
             \n\
             #[tokio::main]\n\
             async fn main() {\n\
                 let runtime = Arc::new(Mutex::new(Runtime::new()));\n\
                 runtime.lock().await.run();\n\
             }\n\
             \n\
             pub struct Runtime {\n\
                 workers: Vec<Worker>,\n\
             }\n\
             \n\
             impl Runtime {\n\
                 pub fn new() -> Self {\n\
                     Self { workers: vec![] }\n\
                 }\n\
                 pub async fn run(&self) {}\n\
             }\n\
             \n\
             struct Worker;\n",
        )
        .await;

    let result = fixture.analyze_and_extract(&[runtime_rs]).await;
    assert_eq!(result.changed_files.len(), 1);

    // Validate async/macro patterns detected
    let graph = fixture.builder.graph();
    assert!(graph.node_count() >= 1);
}

/// Validates handling of VSCode-like TypeScript patterns.
///
/// Tests patterns found in VSCode:
/// - Decorators and metadata
/// - Dependency injection patterns
/// - Complex class hierarchies
#[tokio::test]
async fn test_real_world_typescript_patterns() {
    let mut fixture = ValidationFixture::new().await;

    // Create VSCode-like dependency injection pattern
    let service_ts = fixture
        .create_file(
            "service.ts",
            "import { injectable, inject } from './di';\n\
             import { ILogger } from './interfaces';\n\
             \n\
             @injectable()\n\
             export class EditorService {\n\
                 constructor(\n\
                     @inject('ILogger') private logger: ILogger\n\
                 ) {}\n\
                 \n\
                 public edit(file: string): void {\n\
                     this.logger.log(`Editing ${file}`);\n\
                 }\n\
             }\n",
        )
        .await;

    let result = fixture.analyze_and_extract(&[service_ts]).await;
    assert_eq!(result.changed_files.len(), 1);

    let graph = fixture.builder.graph();
    assert!(graph.node_count() >= 1);
}

/// Validates handling of Django-like Python patterns.
///
/// Tests patterns found in Django:
/// - Decorators (@property, @classmethod)
/// - ORM model patterns
/// - Settings and configuration imports
#[tokio::test]
async fn test_real_world_python_patterns() {
    let mut fixture = ValidationFixture::new().await;

    // Create Django-like model
    let models_py = fixture
        .create_file(
            "models.py",
            "from django.db import models\n\
             from django.conf import settings\n\
             \n\
             class User(models.Model):\n\
                 username = models.CharField(max_length=100)\n\
                 email = models.EmailField()\n\
                 \n\
                 @property\n\
                 def full_name(self) -> str:\n\
                     return f\"{self.first_name} {self.last_name}\"\n\
                 \n\
                 @classmethod\n\
                 def create_user(cls, username: str) -> 'User':\n\
                     return cls(username=username)\n",
        )
        .await;

    let result = fixture.analyze_and_extract(&[models_py]).await;
    assert_eq!(result.changed_files.len(), 1);

    let graph = fixture.builder.graph();
    assert!(graph.node_count() >= 1);
}

/// Validates handling of Kubernetes-like Go patterns.
///
/// Tests patterns found in Kubernetes:
/// - Interface-driven architecture
/// - Package-level organization
/// - Error handling patterns
#[tokio::test]
async fn test_real_world_go_patterns() {
    let mut fixture = ValidationFixture::new().await;

    // Create Kubernetes-like controller pattern
    let controller_go = fixture
        .create_file(
            "controller.go",
            "package controller\n\
             \n\
             import (\n\
                 \"context\"\n\
                 \"fmt\"\n\
             )\n\
             \n\
             type Controller interface {\n\
                 Run(ctx context.Context) error\n\
                 Stop()\n\
             }\n\
             \n\
             type podController struct {\n\
                 stopCh chan struct{}\n\
             }\n\
             \n\
             func NewPodController() Controller {\n\
                 return &podController{\n\
                     stopCh: make(chan struct{}),\n\
                 }\n\
             }\n\
             \n\
             func (c *podController) Run(ctx context.Context) error {\n\
                 select {\n\
                 case <-ctx.Done():\n\
                     return ctx.Err()\n\
                 case <-c.stopCh:\n\
                     return nil\n\
                 }\n\
             }\n\
             \n\
             func (c *podController) Stop() {\n\
                 close(c.stopCh)\n\
             }\n",
        )
        .await;

    let result = fixture.analyze_and_extract(&[controller_go]).await;
    assert_eq!(result.changed_files.len(), 1);

    let graph = fixture.builder.graph();
    assert!(graph.node_count() >= 1);
}

/// Validates handling of monorepo patterns with multiple languages.
///
/// Tests multi-language monorepo structure:
/// - Rust services + TypeScript frontend + Python scripts
/// - Cross-language boundaries
/// - Shared configuration files
#[tokio::test]
async fn test_real_world_monorepo() {
    let mut fixture = ValidationFixture::new().await;

    // Create monorepo structure
    let rust_service = fixture
        .create_file(
            "services/api/src/main.rs",
            "fn main() { println!(\"API\"); }",
        )
        .await;

    let ts_frontend = fixture
        .create_file(
            "apps/web/src/index.ts",
            "import { App } from './App';\nconst app = new App();",
        )
        .await;

    let python_script = fixture
        .create_file(
            "scripts/deploy.py",
            "#!/usr/bin/env python3\nimport sys\nimport os\n\ndef deploy():\n    pass\n",
        )
        .await;

    let paths = vec![rust_service, ts_frontend, python_script];
    let result = fixture.analyze_and_extract(&paths).await;

    assert_eq!(result.changed_files.len(), 3);

    let graph = fixture.builder.graph();
    assert!(graph.node_count() >= 3);
}

/// Validates handling of deep module nesting.
///
/// Tests deeply nested module hierarchies (10+ levels):
/// - Deeply nested imports
/// - Long dependency chains
/// - Path resolution at depth
#[tokio::test]
async fn test_real_world_deep_nesting() {
    let mut fixture = ValidationFixture::new().await;

    // Create deeply nested module structure (10 levels)
    let mut paths = Vec::new();
    let mut current_path = String::new();

    for level in 0..10 {
        current_path.push_str(&format!("level_{}/", level));
        let module_path = format!("{}mod.rs", current_path);

        let content = if level == 0 {
            "pub mod level_1;\npub fn level_0() {}".to_string()
        } else if level < 9 {
            format!(
                "pub mod level_{};\npub fn level_{}() {{}}\n",
                level + 1,
                level
            )
        } else {
            format!("pub fn level_{}() {{}}\n", level)
        };

        let path = fixture.create_file(&module_path, &content).await;
        paths.push(path);
    }

    let result = fixture.analyze_and_extract(&paths).await;
    assert_eq!(result.changed_files.len(), 10);

    let graph = fixture.builder.graph();
    assert!(graph.node_count() >= 10);
}

/// Validates handling of circular dependency patterns.
///
/// Tests complex circular dependencies:
/// - A → B → C → A cycles
/// - Multiple overlapping cycles
/// - Cycle detection and reporting
#[tokio::test]
async fn test_real_world_circular_deps() {
    let mut fixture = ValidationFixture::new().await;

    // Create circular dependency: a → b → c → a
    let file_a = fixture
        .create_file("a.rs", "use crate::c;\npub fn a() {}")
        .await;
    let file_b = fixture
        .create_file("b.rs", "use crate::a;\npub fn b() {}")
        .await;
    let file_c = fixture
        .create_file("c.rs", "use crate::b;\npub fn c() {}")
        .await;

    let paths = vec![file_a, file_b, file_c];
    let result = fixture.analyze_and_extract(&paths).await;

    assert_eq!(result.changed_files.len(), 3);

    // Validate cycle detection
    let graph = fixture.builder.graph();
    assert!(graph.node_count() >= 3);
    assert!(graph.edge_count() >= 3, "Expected circular edges");
}

/// Validates handling of very large files (>100KB).
///
/// Tests edge case of large source files:
/// - Files with thousands of lines
/// - Large import lists
/// - Memory efficiency validation
#[tokio::test]
async fn test_real_world_large_files() {
    let mut fixture = ValidationFixture::new().await;

    // Generate a large Rust file (10000+ lines, ~600KB)
    let mut large_content = String::from("// Large file with extensive documentation\n");
    large_content.push_str("use std::collections::HashMap;\n");
    large_content.push_str("use std::sync::Arc;\n");
    large_content.push_str("use std::sync::Mutex;\n\n");

    for i in 0..20000 {
        large_content.push_str(&format!(
            "pub fn function_{}() -> HashMap<String, i32> {{\n\
             let mut map = HashMap::new();\n\
             map.insert(String::from(\"key\"), {});\n\
             map\n\
             }}\n",
            i, i
        ));
    }

    let large_file = fixture.create_file("large.rs", &large_content).await;

    // Validate file size
    let metadata = fs::metadata(&large_file).await.expect("get metadata");
    assert!(
        metadata.len() > 50_000,
        "Expected >50KB file, got {} bytes",
        metadata.len()
    );

    // Analyze large file
    let start = Instant::now();
    let result = fixture.analyze_and_extract(&[large_file]).await;
    let analysis_time = start.elapsed();

    assert_eq!(result.changed_files.len(), 1);

    // Performance should still be reasonable (<3s for single very large file with 20K lines)
    assert!(
        analysis_time.as_millis() < 3000,
        "Large file analysis took {:?}, exceeds 3s",
        analysis_time
    );
}

// ═══════════════════════════════════════════════════════════════════════════
// Performance Tests (4 tests)
// ═══════════════════════════════════════════════════════════════════════════

/// Validates cold start performance on large codebase.
///
/// Measures initial analysis performance when cache is empty:
/// - 10K+ file initial analysis
/// - Fingerprinting throughput
/// - Graph construction speed
#[tokio::test]
async fn test_real_world_cold_start() {
    let mut fixture = ValidationFixture::new().await;

    // Generate 10K files
    let paths = fixture.generate_rust_scale(10_000).await;

    // Cold start analysis
    let start = Instant::now();
    let result = fixture.analyze_and_extract(&paths).await;
    let elapsed = start.elapsed();

    println!(
        "Cold start: {} files in {:?} ({:.0} files/sec)",
        result.changed_files.len(),
        elapsed,
        result.changed_files.len() as f64 / elapsed.as_secs_f64()
    );

    // Throughput validation: >1000 files/sec
    let throughput = result.changed_files.len() as f64 / elapsed.as_secs_f64();
    assert!(
        throughput > 1000.0,
        "Cold start throughput {:.0} files/sec < 1000 target",
        throughput
    );
}

/// Validates incremental update efficiency at scale.
///
/// Measures performance when 1% of files change:
/// - Fast invalidation of affected files
/// - Minimal reanalysis overhead
/// - Cache efficiency
#[tokio::test]
async fn test_real_world_incremental_update() {
    let mut fixture = ValidationFixture::new().await;

    // Initial analysis
    let paths = fixture.generate_rust_scale(10_000).await;
    fixture.analyze_and_extract(&paths).await;

    // Change 1% of files
    let changed_count = 100;
    let changed_paths: Vec<_> = paths.iter().take(changed_count).cloned().collect();

    for path in &changed_paths {
        fixture
            .update_file(path, "// Updated\npub fn updated() {}")
            .await;
    }

    // Incremental update
    let start = Instant::now();
    let result = fixture.analyze_and_extract(&changed_paths).await;
    let elapsed = start.elapsed();

    println!(
        "Incremental: {} changed files in {:?} ({:.0} files/sec)",
        result.changed_files.len(),
        elapsed,
        result.changed_files.len() as f64 / elapsed.as_secs_f64()
    );

    assert_eq!(result.changed_files.len(), changed_count);

    // Performance: <1s for 1% update
    assert!(
        elapsed.as_secs() < 1,
        "Incremental update {:?} exceeds 1s",
        elapsed
    );
}

/// Validates cache hit rate meets constitutional requirements (>90%).
///
/// Tests cache efficiency over multiple analysis cycles:
/// - Initial cold start
/// - Warm cache reanalysis
/// - Cache hit rate calculation
#[tokio::test]
async fn test_real_world_cache_hit_rate() {
    let mut fixture = ValidationFixture::new().await;

    // Generate and analyze 1000 files
    let paths = fixture.generate_rust_scale(1_000).await;
    fixture.analyze_and_extract(&paths).await;

    // Reanalyze without changes (should hit cache)
    let result = fixture.analyze_and_extract(&paths).await;

    // All files should be unchanged (cache hits)
    println!("Cache hit rate: {:.1}%", result.cache_hit_rate * 100.0);

    // Constitutional requirement: >90% cache hit rate
    // On reanalysis with no changes, should be 100% cache hits
    assert!(
        result.cache_hit_rate > 0.90,
        "Expected >90% cache hit rate, got {:.1}%",
        result.cache_hit_rate * 100.0
    );

    // Changed files should be 0 on reanalysis
    assert!(
        result.changed_files.is_empty(),
        "Expected 0 changed files on reanalysis, got {}",
        result.changed_files.len()
    );
}

/// Validates parallel processing efficiency at scale.
///
/// Tests Rayon/tokio performance with large batches:
/// - Parallel fingerprinting
/// - Parallel dependency extraction
/// - Scalability validation
#[tokio::test]
#[cfg(feature = "parallel")]
async fn test_real_world_parallel_scaling() {
    let mut fixture = ValidationFixture::new().await;

    // Generate 5K files for parallel processing test
    let paths = fixture.generate_rust_scale(5_000).await;

    // Analyze with parallelism enabled
    let start = Instant::now();
    let result = fixture.analyze_and_extract(&paths).await;
    let parallel_time = start.elapsed();

    println!(
        "Parallel analysis: {} files in {:?} ({:.0} files/sec)",
        result.changed_files.len(),
        parallel_time,
        result.changed_files.len() as f64 / parallel_time.as_secs_f64()
    );

    // Throughput should be higher with parallelism (>1000 files/sec like cold start)
    let throughput = result.changed_files.len() as f64 / parallel_time.as_secs_f64();
    assert!(
        throughput > 1000.0,
        "Parallel throughput {:.0} files/sec < 1000 target",
        throughput
    );
}

// ═══════════════════════════════════════════════════════════════════════════
// Edge Case Tests (4 additional tests)
// ═══════════════════════════════════════════════════════════════════════════

/// Validates handling of empty files and minimal content.
#[tokio::test]
async fn test_real_world_empty_files() {
    let mut fixture = ValidationFixture::new().await;

    // Create mix of empty and minimal files
    let empty = fixture.create_file("empty.rs", "").await;
    let comment_only = fixture
        .create_file("comment.rs", "// Just a comment\n")
        .await;
    let minimal = fixture.create_file("minimal.rs", "fn main() {}").await;

    let paths = vec![empty, comment_only, minimal];
    let result = fixture.analyze_and_extract(&paths).await;

    assert_eq!(result.changed_files.len(), 3);

    let graph = fixture.builder.graph();
    assert!(graph.node_count() >= 3);
}

/// Validates handling of binary files and non-source files.
#[tokio::test]
async fn test_real_world_binary_files() {
    let mut fixture = ValidationFixture::new().await;

    // Create binary file (invalid UTF-8)
    let binary_path = fixture.temp_path().join("binary.bin");
    let mut file = fs::File::create(&binary_path).await.expect("create binary");
    file.write_all(&[0xFF, 0xFE, 0xFD, 0xFC])
        .await
        .expect("write binary");

    // Create valid Rust file
    let rust_file = fixture.create_file("valid.rs", "fn main() {}").await;

    // Analyze both (binary should be skipped gracefully)
    let paths = vec![binary_path.clone(), rust_file];
    let result = fixture.analyze_and_extract(&paths).await;

    // Only Rust file should be analyzed
    assert!(
        !result.changed_files.is_empty(),
        "Expected at least 1 file analyzed (binary skipped)"
    );
}

/// Validates handling of symlinks and hard links.
#[tokio::test]
#[cfg(target_family = "unix")]
async fn test_real_world_symlinks() {
    let mut fixture = ValidationFixture::new().await;

    // Create original file
    let original = fixture
        .create_file("original.rs", "pub fn original() {}")
        .await;

    // Create symlink
    let symlink_path = fixture.temp_path().join("symlink.rs");
    #[cfg(target_family = "unix")]
    std::os::unix::fs::symlink(&original, &symlink_path).expect("create symlink");

    // Analyze both (should handle symlinks correctly)
    let paths = vec![original, symlink_path];
    let result = fixture.analyze_and_extract(&paths).await;

    // Both should be analyzed (symlink follows to original)
    assert!(!result.changed_files.is_empty());
}

/// Validates handling of Unicode and non-ASCII characters.
#[tokio::test]
async fn test_real_world_unicode() {
    let mut fixture = ValidationFixture::new().await;

    // Create files with Unicode content
    let unicode_rs = fixture
        .create_file(
            "unicode.rs",
            "// 日本語コメント\n\
             pub fn process_emoji() -> &'static str {\n\
                 \"🚀 Rocket launched! 中文 العربية\"\n\
             }\n",
        )
        .await;

    let result = fixture.analyze_and_extract(&[unicode_rs]).await;
    assert_eq!(result.changed_files.len(), 1);

    let graph = fixture.builder.graph();
    assert!(graph.node_count() >= 1);
}