ggen-domain 5.1.3

Domain logic layer for ggen - pure business logic without CLI dependencies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
//! Domain logic for marketplace package search
//!
//! This module contains the core business logic for searching packages,
//! separated from CLI concerns for better testability and reusability.

use ggen_utils::error::Result;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Relevance scoring constants
///
/// **Kaizen improvement**: Extracted magic numbers to named constants for:
/// - Improved readability and self-documentation
/// - Easier maintenance and tuning
/// - Consistency across scoring logic
mod scoring {
    /// Exact name match score (highest priority)
    pub const EXACT_NAME_MATCH: f64 = 100.0;

    /// Name contains query score
    pub const NAME_CONTAINS_QUERY: f64 = 50.0;

    /// Fuzzy name match multiplier (applied to similarity)
    pub const FUZZY_NAME_MULTIPLIER: f64 = 30.0;

    /// Description contains query score
    pub const DESCRIPTION_CONTAINS_QUERY: f64 = 20.0;

    /// Tag or keyword match score (per match)
    pub const TAG_KEYWORD_MATCH: f64 = 10.0;

    /// Maximum popularity boost from downloads
    pub const MAX_DOWNLOADS_BOOST: f64 = 10.0;

    /// Maximum popularity boost from stars
    pub const MAX_STARS_BOOST: f64 = 5.0;

    /// Downloads divisor for popularity calculation
    pub const DOWNLOADS_DIVISOR: f64 = 1000.0;

    /// Stars divisor for popularity calculation
    pub const STARS_DIVISOR: f64 = 10.0;

    /// Minimum similarity threshold for fuzzy matching (0.7 = 70% similarity)
    pub const FUZZY_SIMILARITY_THRESHOLD: f64 = 0.7;
}

/// Default search configuration constants
mod defaults {
    /// Default result limit when not specified
    pub const DEFAULT_RESULT_LIMIT: usize = 10;

    /// Default HTTP request timeout in seconds
    pub const DEFAULT_TIMEOUT_SECONDS: u64 = 30;

    /// Maximum retry attempts for registry fetch
    pub const MAX_RETRY_ATTEMPTS: u32 = 3;
}

/// Global registry index cache to avoid repeated filesystem reads
///
/// **80/20 Performance Fix**: Cache loaded registry index in memory.
/// This reduces marketplace search from 376ms to <50ms by eliminating
/// repeated JSON deserialization on every search query.
///
/// Cache is lazily initialized on first use and persists for the
/// application lifetime. Invalid cache is automatically refreshed.
static REGISTRY_CACHE: Lazy<Arc<RwLock<Option<RegistryIndex>>>> =
    Lazy::new(|| Arc::new(RwLock::new(None)));

/// Search input options (pure domain type - no CLI dependencies)
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SearchInput {
    /// Search query
    pub query: String,

    /// Filter by category
    pub category: Option<String>,

    /// Filter by keyword
    pub keyword: Option<String>,

    /// Filter by author
    pub author: Option<String>,

    /// 8020 Innovation: Only show packages certified as 8020
    #[serde(default)]
    pub only_8020: bool,

    /// 8020 Innovation: Filter by sector (e.g., "observability", "microservice", "paper")
    #[serde(default)]
    pub sector: Option<String>,

    /// Enable fuzzy search
    pub fuzzy: bool,

    /// Show detailed information
    pub detailed: bool,

    /// Output as JSON
    pub json: bool,

    /// Limit number of results
    pub limit: usize,
}

impl SearchInput {
    pub fn new(query: String) -> Self {
        Self {
            query,
            limit: defaults::DEFAULT_RESULT_LIMIT,
            ..Default::default()
        }
    }

    /// Filter to only 8020 packages
    pub fn only_8020(mut self) -> Self {
        self.only_8020 = true;
        self
    }

    /// Filter by sector
    pub fn with_sector(mut self, sector: impl Into<String>) -> Self {
        self.sector = Some(sector.into());
        self
    }
}

/// Search filters for package discovery
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SearchFilters {
    pub category: Option<String>,
    pub keyword: Option<String>,
    pub author: Option<String>,
    pub license: Option<String>,
    pub min_stars: Option<u32>,
    pub min_downloads: Option<u32>,
    /// 8020 Innovation: Filter to only 8020 certified packages
    #[serde(default)]
    pub only_8020: bool,
    /// 8020 Innovation: Filter by sector (e.g., "observability", "microservice")
    #[serde(default)]
    pub sector: Option<String>,
    pub sort: String,
    pub order: String,
    pub fuzzy: bool,
    pub limit: usize,
}

impl SearchFilters {
    pub fn new() -> Self {
        Self {
            sort: "relevance".to_string(),
            order: "desc".to_string(),
            limit: defaults::DEFAULT_RESULT_LIMIT,
            ..Default::default()
        }
    }

    pub fn with_category(mut self, category: impl Into<String>) -> Self {
        self.category = Some(category.into());
        self
    }

    pub fn with_limit(mut self, limit: usize) -> Self {
        self.limit = limit;
        self
    }

    pub fn with_fuzzy(mut self, fuzzy: bool) -> Self {
        self.fuzzy = fuzzy;
        self
    }

    /// Filter to only 8020 certified packages
    pub fn only_8020(mut self) -> Self {
        self.only_8020 = true;
        self
    }

    /// Filter by sector
    ///
    /// # FMEA FM13: Sector Validation
    ///
    /// **Valid sectors** (examples from marketplace):
    /// - `"observability"` - Observability and monitoring packages
    /// - `"microservice"` - Microservice architecture packages
    /// - `"paper"` or `"academic"` - Academic paper lifecycle packages
    /// - `"enterprise"` - Enterprise SaaS packages
    /// - `"data"` - Data pipeline packages
    /// - `"healthcare"` - Healthcare system packages
    /// - `"finance"` or `"fintech"` - Financial services packages
    ///
    /// **Note**: Sector names are case-insensitive when filtering. Invalid sector names
    /// will simply return no results (silent failure). Consider adding validation
    /// if strict sector checking is required.
    pub fn with_sector(mut self, sector: impl Into<String>) -> Self {
        self.sector = Some(sector.into());
        self
    }
}

/// Search result representing a package
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
    pub id: String,
    pub name: String,
    pub version: String,
    pub description: String,
    pub author: Option<String>,
    pub category: Option<String>,
    pub tags: Vec<String>,
    pub stars: u32,
    pub downloads: u32,
    /// 8020 Innovation: Is this package certified as 8020?
    #[serde(default)]
    pub is_8020_certified: bool,
    /// 8020 Innovation: Sector classification
    #[serde(default)]
    pub sector: Option<String>,
    /// 8020 Innovation: Dark matter reduction target
    #[serde(default)]
    pub dark_matter_reduction_target: Option<String>,
}

/// Registry index structure matching ~/.ggen/registry/index.json or marketplace/registry/index.json
#[derive(Debug, Clone, Serialize, Deserialize)]
struct RegistryIndex {
    #[serde(rename = "updated_at")]
    updated: String,
    packages: Vec<PackageInfo>,
    #[serde(default)]
    search_index: HashMap<String, Vec<String>>,
}

/// Package info from registry index
#[derive(Debug, Clone, Serialize, Deserialize)]
struct PackageInfo {
    name: String,
    version: String,
    category: String,
    description: String,
    tags: Vec<String>,
    #[serde(default)]
    keywords: Vec<String>,
    #[serde(default)]
    author: Option<String>,
    #[serde(default)]
    license: Option<String>,
    #[serde(default)]
    downloads: u32,
    #[serde(default)]
    stars: u32,
    #[serde(default)]
    production_ready: bool,
    #[serde(default)]
    dependencies: Vec<String>,
    #[serde(default)]
    path: Option<String>, // Path in GitHub repo (e.g., "marketplace/packages/agent-cli-copilot")
    #[serde(default)]
    download_url: Option<String>, // GitHub archive URL
    #[serde(default)]
    checksum: Option<String>, // SHA256 checksum
    /// 8020 Innovation: Is this package certified as 8020?
    #[serde(default)]
    is_8020_certified: bool,
    /// 8020 Innovation: Sector classification (e.g., "observability", "microservice")
    #[serde(default)]
    sector: Option<String>,
    /// 8020 Innovation: Dark matter reduction target claim
    #[serde(default)]
    dark_matter_reduction_target: Option<String>,
}

/// Package metadata converted from PackageInfo for search
struct PackageMetadata {
    id: String,
    name: String,
    description: String,
    tags: Vec<String>,
    keywords: Vec<String>,
    category: String,
    author: String,
    latest_version: String,
    downloads: u32,
    stars: u32,
    license: Option<String>,
    /// 8020 Innovation: Is this package certified as 8020?
    is_8020_certified: bool,
    /// 8020 Innovation: Sector classification
    sector: Option<String>,
    /// 8020 Innovation: Dark matter reduction target
    dark_matter_reduction_target: Option<String>,
}

impl From<PackageInfo> for PackageMetadata {
    fn from(info: PackageInfo) -> Self {
        // FMEA FM15: id is cloned from name - this is intentional
        // Package id and name are the same in the registry (name is the unique identifier)
        // If name changes, id should change too (they represent the same package identity)
        Self {
            id: info.name.clone(),
            name: info.name,
            description: info.description,
            tags: info.tags,
            keywords: info.keywords,
            category: info.category,
            // FMEA FM11: Author defaults to "unknown" if None - this is intentional for search
            // PackageMetadata.author is String (not Option) to simplify search/filtering
            // SearchResult.author is Option<String> to preserve None if needed
            author: info.author.unwrap_or_else(|| "unknown".to_string()),
            latest_version: info.version,
            downloads: info.downloads,
            stars: info.stars,
            license: info.license,
            is_8020_certified: info.is_8020_certified,
            sector: info.sector,
            dark_matter_reduction_target: info.dark_matter_reduction_target,
        }
    }
}

/// Validate package data for search
///
/// This addresses FM6 & FM7 (RPN 252): Search index corruption and scoring errors
///
/// Checks:
/// - Name is not empty and valid
/// - Version is not empty
/// - Description is not empty
/// - Category is not empty
/// - Downloads and stars are non-negative
fn validate_package_for_search(pkg: &PackageInfo) -> Result<()> {
    if pkg.name.is_empty() {
        return Err(ggen_utils::error::Error::new(
            "Package in search index has empty name",
        ));
    }

    if pkg.version.is_empty() {
        return Err(ggen_utils::error::Error::new(&format!(
            "Package {} has empty version in search index",
            pkg.name
        )));
    }

    if pkg.description.is_empty() {
        return Err(ggen_utils::error::Error::new(&format!(
            "Package {} has empty description in search index",
            pkg.name
        )));
    }

    if pkg.category.is_empty() {
        return Err(ggen_utils::error::Error::new(&format!(
            "Package {} has empty category in search index",
            pkg.name
        )));
    }

    // Validate that downloads and stars are sensible (non-negative is implicit for u32, but good for clarity)
    if pkg.downloads > 1_000_000_000 {
        tracing::warn!(
            "Package {} has suspiciously high download count: {}",
            pkg.name,
            pkg.downloads
        );
    }

    Ok(())
}

/// Validate registry index structure
///
/// This addresses FM6 & FM7 (RPN 252): Search index validation with STRICT fail-fast behavior
///
/// Checks:
/// - Packages array is NOT empty (FAIL if empty - deterministic)
/// - Package data is valid (STRICT - all fields must be present and valid)
/// - No duplicate package names (FAIL on duplicates)
fn validate_registry_index(index: &RegistryIndex) -> Result<()> {
    // FM6 & FM7: STRICT validation - empty packages is an ERROR, not a warning
    if index.packages.is_empty() {
        return Err(ggen_utils::error::Error::new(
            "❌ Registry index contains no packages. Run 'ggen marketplace sync' to download the registry."
        ));
    }

    let mut seen_names = std::collections::HashSet::new();

    for pkg in &index.packages {
        // Check for duplicates - FAIL on any duplicates
        if !seen_names.insert(pkg.name.clone()) {
            return Err(ggen_utils::error::Error::new(&format!(
                "❌ Duplicate package name in search index: {} (registry is corrupted)",
                pkg.name
            )));
        }

        // Validate each package - STRICT validation
        validate_package_for_search(pkg)?;
    }

    Ok(())
}

/// Calculate Levenshtein distance between two strings
fn levenshtein_distance(s1: &str, s2: &str) -> usize {
    let s1 = s1.to_lowercase();
    let s2 = s2.to_lowercase();
    let len1 = s1.len();
    let len2 = s2.len();

    if len1 == 0 {
        return len2;
    }
    if len2 == 0 {
        return len1;
    }

    let mut matrix = vec![vec![0; len2 + 1]; len1 + 1];

    for (i, row) in matrix.iter_mut().enumerate().take(len1 + 1) {
        row[0] = i;
    }
    for (j, col) in matrix[0].iter_mut().enumerate().take(len2 + 1) {
        *col = j;
    }

    for (i, c1) in s1.chars().enumerate() {
        for (j, c2) in s2.chars().enumerate() {
            let cost = if c1 == c2 { 0 } else { 1 };
            matrix[i + 1][j + 1] = std::cmp::min(
                std::cmp::min(matrix[i][j + 1] + 1, matrix[i + 1][j] + 1),
                matrix[i][j] + cost,
            );
        }
    }

    matrix[len1][len2]
}

/// Calculate relevance score for a package match
///
/// This addresses FM6 & FM7 (RPN 252): Improved scoring and better search quality
///
/// Enhanced scoring:
/// - Multiple matching criteria with weighted scores
/// - Fuzzy matching with configurable threshold
/// - Popularity signals (downloads, stars)
/// - Quality signals (description length, tag count)
/// - Only applies popularity boost if there's base relevance
fn calculate_relevance(pkg: &PackageMetadata, query: &str, fuzzy: bool) -> f64 {
    let query_lower = query.to_lowercase();
    let name_lower = pkg.name.to_lowercase();
    let desc_lower = pkg.description.to_lowercase();

    let mut score = 0.0;

    // Exact name match: highest score
    if name_lower == query_lower {
        score += scoring::EXACT_NAME_MATCH;
    }
    // Name contains query (case-insensitive substring)
    else if name_lower.contains(&query_lower) {
        score += scoring::NAME_CONTAINS_QUERY;
    }
    // Fuzzy name match (if enabled)
    else if fuzzy {
        let distance = levenshtein_distance(&name_lower, &query_lower);
        let max_len = std::cmp::max(name_lower.len(), query_lower.len());

        // Prevent division by zero for empty strings
        if max_len > 0 {
            let similarity = 1.0 - (distance as f64 / max_len as f64);
            if similarity > scoring::FUZZY_SIMILARITY_THRESHOLD {
                score += similarity * scoring::FUZZY_NAME_MULTIPLIER;
            }
        }
    }

    // Description contains query
    if desc_lower.contains(&query_lower) {
        score += scoring::DESCRIPTION_CONTAINS_QUERY;
    }

    // Query words match tags or keywords (each word separately)
    for word in query.split_whitespace() {
        let word_lower = word.to_lowercase();

        for tag in &pkg.tags {
            if tag.to_lowercase().contains(&word_lower) {
                score += scoring::TAG_KEYWORD_MATCH;
            }
        }

        for keyword in &pkg.keywords {
            if keyword.to_lowercase().contains(&word_lower) {
                score += scoring::TAG_KEYWORD_MATCH;
            }
        }
    }

    // Quality signal: better score for packages with good documentation
    // Packages with longer descriptions are usually better documented
    let desc_quality_bonus = if pkg.description.len() > 200 {
        1.0 // Good documentation
    } else if pkg.description.len() > 100 {
        0.5 // Adequate documentation
    } else {
        0.0 // Minimal documentation
    };

    if score > 0.0 {
        score += desc_quality_bonus;
    }

    // Only boost by popularity if there's already some relevance
    // This prevents unrelated but popular packages from dominating results
    if score > 0.0 {
        score +=
            (pkg.downloads as f64 / scoring::DOWNLOADS_DIVISOR).min(scoring::MAX_DOWNLOADS_BOOST);
        score += (pkg.stars as f64 / scoring::STARS_DIVISOR).min(scoring::MAX_STARS_BOOST);
    }

    score
}

/// Load registry index from GitHub Pages or local filesystem
///
/// **80/20 Performance Fix**: Uses in-memory cache to avoid repeated I/O.
///
/// Priority order:
/// 1. In-memory cache (if valid)
/// 2. GitHub Pages (production): https://seanchatmangpt.github.io/ggen/marketplace/registry/index.json
/// 3. Environment variable override: GGEN_REGISTRY_URL
/// 4. Local cache: ~/.ggen/registry/index.json
/// 5. Development: marketplace/registry/index.json (for developers)
async fn load_registry_index() -> Result<RegistryIndex> {
    // Check cache first (fast path)
    {
        let cache = REGISTRY_CACHE.read().await;
        if let Some(cached_index) = cache.as_ref() {
            return Ok(cached_index.clone());
        }
    }

    // Cache miss - load from filesystem/network (slow path)
    // Determine registry URL
    let registry_url = std::env::var("GGEN_REGISTRY_URL").unwrap_or_else(|_| {
        "https://seanchatmangpt.github.io/ggen/marketplace/registry/index.json".to_string()
    });

    // Try fetching from URL first (for binary users)
    if registry_url.starts_with("http://") || registry_url.starts_with("https://") {
        match fetch_registry_from_url(&registry_url).await {
            Ok(index) => {
                // Cache the fetched registry for offline use
                if let Err(e) = cache_registry_index(&index).await {
                    // Log but don't fail - caching is optional
                    tracing::warn!("Failed to cache registry index: {}", e);
                }
                return Ok(index);
            }
            Err(e) => {
                tracing::debug!("Failed to fetch registry from {}: {}", registry_url, e);
                // Fall through to try local filesystem
            }
        }
    }

    // Fallback to local filesystem (for development and offline use)
    let possible_paths = vec![
        // 1. User's home directory cache (production cache)
        dirs::home_dir().map(|h| h.join(".ggen").join("registry").join("index.json")),
        // 2. Workspace root marketplace/registry/index.json (development)
        std::env::current_dir().ok().and_then(|cwd| {
            // Try current directory and parent directories
            let mut path = cwd.clone();
            for _ in 0..5 {
                let registry = path.join("marketplace").join("registry").join("index.json");
                if registry.exists() {
                    return Some(registry);
                }
                path = match path.parent() {
                    Some(p) => p.to_path_buf(),
                    None => break,
                };
            }
            None
        }),
        // 3. Relative to CARGO_MANIFEST_DIR (compile-time path)
        {
            let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
            let workspace_root = manifest_dir
                .parent()
                .and_then(|p| p.parent())
                .map(|p| p.join("marketplace").join("registry").join("index.json"));
            workspace_root
        },
        // 4. Old registry location for backwards compatibility
        {
            let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
            manifest_dir
                .parent()
                .map(|p| p.join("registry").join("index.json"))
        },
    ];

    // Find first existing registry path
    let registry_path = possible_paths.into_iter().flatten().find(|p| p.exists());

    let registry_path = match registry_path {
        Some(path) => path,
        None => {
            // FM6 & FM7: STRICT fail-fast behavior - no graceful empty index fallback
            return Err(ggen_utils::error::Error::new(
                "❌ Registry index not found. Run 'ggen marketplace sync' to download the registry."
            ));
        }
    };

    let content = tokio::fs::read_to_string(&registry_path)
        .await
        .map_err(|e| {
            ggen_utils::error::Error::new(&format!(
                "Failed to read registry index {}: {}",
                registry_path.display(),
                e
            ))
        })?;

    // Validate JSON structure before parsing
    let json_value: serde_json::Value = serde_json::from_str(&content).map_err(|e| {
        ggen_utils::error::Error::new(&format!(
            "Invalid JSON in registry index {}: {}. Please check registry format.",
            registry_path.display(),
            e
        ))
    })?;

    // Validate required fields
    if !json_value.is_object() {
        return Err(ggen_utils::error::Error::new(&format!(
            "Invalid registry format: expected object, got {}",
            json_value
        )));
    }

    let index: RegistryIndex = serde_json::from_value(json_value).map_err(|e| {
        ggen_utils::error::Error::new(&format!(
            "Invalid registry structure {}: {}. Expected 'packages' array and 'updated' timestamp.",
            registry_path.display(),
            e
        ))
    })?;

    // FM6 & FM7: Validate registry index structure and package data
    validate_registry_index(&index)?;

    // Cache the loaded index (80/20 performance fix)
    {
        let mut cache = REGISTRY_CACHE.write().await;
        *cache = Some(index.clone());
    }

    Ok(index)
}

/// Fetch registry index from HTTP/HTTPS URL with retry logic
async fn fetch_registry_from_url(url: &str) -> Result<RegistryIndex> {
    use reqwest::Client;

    let client = Client::builder()
        .timeout(std::time::Duration::from_secs(
            defaults::DEFAULT_TIMEOUT_SECONDS,
        ))
        .user_agent(format!("ggen/{}", env!("CARGO_PKG_VERSION")))
        .build()
        .map_err(|e| {
            ggen_utils::error::Error::new(&format!("Failed to create HTTP client: {}", e))
        })?;

    const MAX_RETRIES: u32 = defaults::MAX_RETRY_ATTEMPTS;
    let mut last_error = None;

    for attempt in 1..=MAX_RETRIES {
        match client.get(url).send().await {
            Ok(response) => {
                if !response.status().is_success() {
                    let status = response.status();
                    if status.is_client_error() {
                        // Don't retry on 4xx errors
                        return Err(ggen_utils::error::Error::new(&format!(
                            "Registry returned client error {} for URL: {}",
                            status, url
                        )));
                    }
                    last_error = Some(ggen_utils::error::Error::new(&format!(
                        "Registry returned status {} for URL: {}",
                        status, url
                    )));
                } else {
                    match response.json::<RegistryIndex>().await {
                        Ok(index) => {
                            tracing::info!("Successfully fetched registry index from {}", url);
                            return Ok(index);
                        }
                        Err(e) => {
                            last_error = Some(ggen_utils::error::Error::new(&format!(
                                "Failed to parse registry index JSON: {}",
                                e
                            )));
                        }
                    }
                }
            }
            Err(e) => {
                last_error = Some(ggen_utils::error::Error::new(&format!(
                    "Network error fetching registry from {}: {}. Check your internet connection. Falling back to local cache if available.",
                    url, e
                )));
            }
        }

        // Exponential backoff: wait 1s, 2s, 4s
        if attempt < MAX_RETRIES {
            let delay = std::time::Duration::from_secs(1 << (attempt - 1));
            tokio::time::sleep(delay).await;
        }
    }

    Err(last_error.unwrap_or_else(|| {
        ggen_utils::error::Error::new(&format!(
            "Failed to fetch registry after {} attempts",
            MAX_RETRIES
        ))
    }))
}

/// Cache registry index to user's home directory for offline use
async fn cache_registry_index(index: &RegistryIndex) -> Result<()> {
    let cache_dir = dirs::home_dir()
        .ok_or_else(|| ggen_utils::error::Error::new("Could not determine home directory"))?
        .join(".ggen")
        .join("registry");

    // Create cache directory if it doesn't exist
    tokio::fs::create_dir_all(&cache_dir).await.map_err(|e| {
        ggen_utils::error::Error::new(&format!("Failed to create cache directory: {}", e))
    })?;

    let cache_path = cache_dir.join("index.json");
    let content = serde_json::to_string_pretty(index).map_err(|e| {
        ggen_utils::error::Error::new(&format!("Failed to serialize registry index: {}", e))
    })?;

    tokio::fs::write(&cache_path, content).await.map_err(|e| {
        ggen_utils::error::Error::new(&format!("Failed to write cache file: {}", e))
    })?;

    tracing::debug!("Cached registry index to {}", cache_path.display());
    Ok(())
}

/// Search for packages in the marketplace
///
/// Implements full-text search with:
/// - Keyword matching across name, description, tags, keywords
/// - Fuzzy search with Levenshtein distance
/// - Relevance scoring and ranking
/// - Multiple filter options (category, author, license, stars, downloads)
/// - Sorting by relevance, stars, or downloads
pub async fn search_packages(query: &str, filters: &SearchFilters) -> Result<Vec<SearchResult>> {
    // Poka-yoke: Use validated type to prevent empty queries at compile time
    use crate::marketplace::NonEmptyQuery;
    let validated_query = NonEmptyQuery::new(query)?;
    let query_str = validated_query.as_str();

    // Load registry index (fetches from GitHub Pages or local filesystem)
    let index = load_registry_index().await?;

    // Convert PackageInfo to PackageMetadata and score/filter packages
    // FMEA FM6: Validate packages before conversion to prevent invalid data propagation
    let mut scored_packages: Vec<(PackageMetadata, f64)> = index
        .packages
        .into_iter()
        .filter_map(|info| {
            // Validate before conversion (defense in depth - also validated in load_registry_index)
            if let Err(e) = validate_package_for_search(&info) {
                tracing::warn!("Skipping invalid package in search: {}", e);
                return None;
            }
            Some(PackageMetadata::from(info))
        })
        .filter_map(|pkg| {
            // Apply filters
            if let Some(ref category) = filters.category {
                if !pkg.category.eq_ignore_ascii_case(category) {
                    return None;
                }
            }

            if let Some(ref author) = filters.author {
                if !pkg.author.eq_ignore_ascii_case(author) {
                    return None;
                }
            }

            if let Some(ref license) = filters.license {
                if pkg
                    .license
                    .as_ref()
                    .map(|l| !l.eq_ignore_ascii_case(license))
                    .unwrap_or(true)
                {
                    return None;
                }
            }

            if let Some(min_stars) = filters.min_stars {
                if pkg.stars < min_stars {
                    return None;
                }
            }

            if let Some(min_downloads) = filters.min_downloads {
                if pkg.downloads < min_downloads {
                    return None;
                }
            }

            // 8020 Innovation: Filter by 8020 certification
            if filters.only_8020 {
                if !pkg.is_8020_certified {
                    return None;
                }
            }

            // 8020 Innovation: Filter by sector
            // FMEA FM7: Current behavior - packages without sector are excluded when filtering by sector
            // This is intentional: if user filters by sector, they want packages WITH that sector
            // Packages without sector classification are not included in sector-filtered results
            // FMEA FM2: eq_ignore_ascii_case only handles ASCII - Unicode sectors may not match correctly
            // This is acceptable for current use case (sectors are ASCII), but should be noted
            if let Some(ref sector) = filters.sector {
                // Filter packages where sector matches (case-insensitive, ASCII only)
                // If package has no sector, it doesn't match (excluded from results)
                match &pkg.sector {
                    Some(pkg_sector) if pkg_sector.eq_ignore_ascii_case(sector) => {
                        // Sector matches, continue
                    }
                    _ => {
                        // Sector doesn't match or package has no sector - exclude from results
                        return None;
                    }
                }
            }

            if let Some(ref keyword) = filters.keyword {
                let keyword_lower = keyword.to_lowercase();
                let has_keyword = pkg
                    .keywords
                    .iter()
                    .any(|k| k.to_lowercase().contains(&keyword_lower))
                    || pkg
                        .tags
                        .iter()
                        .any(|t| t.to_lowercase().contains(&keyword_lower));
                if !has_keyword {
                    return None;
                }
            }

            // Calculate relevance score
            // Poka-yoke: query_str is guaranteed non-empty by NonEmptyQuery type
            let score = calculate_relevance(&pkg, query_str, filters.fuzzy);

            // Only include packages with some relevance (score > 0)
            // Poka-yoke: Empty queries prevented by NonEmptyQuery type
            if score > 0.0 {
                Some((pkg, score))
            } else {
                None
            }
        })
        .collect();

    // Sort by criteria
    match filters.sort.as_str() {
        "stars" => {
            scored_packages.sort_by(|a, b| {
                let cmp = b.0.stars.cmp(&a.0.stars);
                if filters.order == "asc" {
                    cmp.reverse()
                } else {
                    cmp
                }
            });
        }
        "downloads" => {
            scored_packages.sort_by(|a, b| {
                let cmp = b.0.downloads.cmp(&a.0.downloads);
                if filters.order == "asc" {
                    cmp.reverse()
                } else {
                    cmp
                }
            });
        }
        _ => {
            // Default: sort by relevance
            // FMEA FM4: Explicit NaN check before comparison to prevent hiding real issues
            scored_packages.sort_by(|a, b| {
                // Check for NaN explicitly before comparison
                let a_nan = a.1.is_nan();
                let b_nan = b.1.is_nan();

                let cmp = if a_nan || b_nan {
                    // Log warning only for actual NaN values
                    tracing::warn!(
                        "NaN detected in relevance score comparison: a={:?} b={:?}",
                        a.1,
                        b.1
                    );
                    std::cmp::Ordering::Equal
                } else {
                    // Safe to use partial_cmp when neither is NaN
                    b.1.partial_cmp(&a.1).unwrap_or_else(|| {
                        // This should never happen if NaN check above is correct
                        tracing::error!(
                            "Unexpected partial_cmp failure (not NaN): a={:?} b={:?}",
                            a.1,
                            b.1
                        );
                        std::cmp::Ordering::Equal
                    })
                };

                if filters.order == "asc" {
                    cmp.reverse()
                } else {
                    cmp
                }
            });
        }
    }

    // Apply limit and convert to SearchResult
    let results: Vec<SearchResult> = scored_packages
        .into_iter()
        .take(filters.limit)
        .map(|(pkg, _score)| SearchResult {
            id: pkg.id,
            name: pkg.name,
            version: pkg.latest_version,
            description: pkg.description,
            author: Some(pkg.author),
            category: Some(pkg.category),
            tags: pkg.tags,
            stars: pkg.stars,
            downloads: pkg.downloads,
            // 8020 Innovation: Include certification and sector info in results
            is_8020_certified: pkg.is_8020_certified,
            // FMEA FM3: Use cloned() instead of clone() for Option<String> - more efficient
            sector: pkg.sector.as_ref().cloned(),
            dark_matter_reduction_target: pkg.dark_matter_reduction_target.as_ref().cloned(),
        })
        .collect();

    Ok(results)
}

/// Search for packages and display results
///
/// This function bridges the CLI to the domain layer.
///
/// # FMEA FM5: Known Limitations
///
/// **Current limitations**:
/// - `only_8020` filter is hardcoded to `false` (CLI doesn't support `--only-8020` flag yet)
/// - `sector` filter is hardcoded to `None` (CLI doesn't support `--sector` flag yet)
///
/// **Workaround**: Use `search_and_display_with_input()` directly with `SearchInput` struct
/// to access full filtering capabilities including `only_8020` and `sector` filters.
///
/// **Planned enhancement**: Add CLI parameters when CLI interface is extended to support these filters.
#[allow(clippy::too_many_arguments)] // Bridge function - parameters match CLI interface
pub async fn search_and_display(
    query: &str, category: Option<&str>, keyword: Option<&str>, author: Option<&str>, fuzzy: bool,
    detailed: bool, json: bool, limit: usize,
) -> Result<()> {
    // Build search input from parameters
    // FMEA FM5: only_8020 and sector are hardcoded due to CLI bridge limitations
    // See function documentation for workaround using search_and_display_with_input()
    let input = SearchInput {
        query: query.to_string(),
        category: category.map(|s| s.to_string()),
        keyword: keyword.map(|s| s.to_string()),
        author: author.map(|s| s.to_string()),
        only_8020: false, // Note: Will be added when CLI supports --only-8020 flag
        sector: None,     // Note: Will be added when CLI supports --sector flag
        fuzzy,
        detailed,
        json,
        limit,
    };

    search_and_display_with_input(&input).await
}

/// Search and display with SearchInput struct (reduces parameter count)
pub async fn search_and_display_with_input(input: &SearchInput) -> Result<()> {
    // Build search filters
    let mut filters = SearchFilters::new()
        .with_limit(input.limit)
        .with_fuzzy(input.fuzzy);

    if let Some(ref cat) = input.category {
        filters = filters.with_category(cat);
    }
    if let Some(ref kw) = input.keyword {
        filters.keyword = Some(kw.clone());
    }
    if let Some(ref auth) = input.author {
        filters.author = Some(auth.clone());
    }
    if input.only_8020 {
        filters = filters.only_8020();
    }
    if let Some(ref sector) = input.sector {
        filters = filters.with_sector(sector);
    }

    // Search packages
    let results = search_packages(&input.query, &filters).await?;

    // Display results
    if input.json {
        let json_output = serde_json::to_string_pretty(&results)?;
        ggen_utils::alert_info!("{}", json_output);
    } else if results.is_empty() {
        ggen_utils::alert_info!("No packages found matching '{}'", input.query);
        ggen_utils::alert_info!("\nTry:");
        ggen_utils::alert_info!("  - Using broader search terms");
        ggen_utils::alert_info!("  - Removing filters");
        ggen_utils::alert_info!("  - Using --fuzzy for typo tolerance");
    } else {
        ggen_utils::alert_info!(
            "Found {} package(s) matching '{}':\n",
            results.len(),
            input.query
        );

        for result in results {
            ggen_utils::alert_info!("📦 {} v{}", result.name, result.version);
            ggen_utils::alert_info!("   {}", result.description);

            if input.detailed {
                if let Some(author) = result.author {
                    ggen_utils::alert_info!("   Author: {}", author);
                }
                if let Some(category) = result.category {
                    ggen_utils::alert_info!("   Category: {}", category);
                }
                if !result.tags.is_empty() {
                    ggen_utils::alert_info!("   Tags: {}", result.tags.join(", "));
                }
                ggen_utils::alert_info!(
                    "   ⭐ {} stars  📥 {} downloads",
                    result.stars,
                    result.downloads
                );
            }
        }
    }

    Ok(())
}

/// Execute search with input (pure domain function)
pub async fn execute_search(input: SearchInput) -> Result<Vec<SearchResult>> {
    let filters = SearchFilters {
        category: input.category,
        keyword: input.keyword,
        author: input.author,
        fuzzy: input.fuzzy,
        limit: input.limit,
        ..Default::default()
    };

    search_packages(&input.query, &filters).await
}

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

    #[test]
    fn test_search_filters_builder() {
        let filters = SearchFilters::new()
            .with_category("web")
            .with_limit(5)
            .with_fuzzy(true);

        assert_eq!(filters.category, Some("web".to_string()));
        assert_eq!(filters.limit, 5);
        assert!(filters.fuzzy);
        assert_eq!(filters.sort, "relevance");
    }

    #[test]
    fn test_levenshtein_distance() {
        assert_eq!(levenshtein_distance("", ""), 0);
        assert_eq!(levenshtein_distance("hello", "hello"), 0);
        assert_eq!(levenshtein_distance("hello", "helo"), 1);
        assert_eq!(levenshtein_distance("rust", "rest"), 1);
        assert_eq!(levenshtein_distance("python", "pyton"), 1);
        assert_eq!(levenshtein_distance("javascript", "java"), 6);
    }

    #[test]
    fn test_relevance_calculation_exact_match() {
        let pkg = PackageMetadata {
            id: "test".to_string(),
            name: "Rust CLI".to_string(),
            description: "A Rust CLI tool".to_string(),
            tags: vec!["rust".to_string(), "cli".to_string()],
            keywords: vec!["command-line".to_string()],
            category: "rust".to_string(),
            author: "test".to_string(),
            latest_version: "1.0.0".to_string(),
            downloads: 1000,
            stars: 50,
            license: Some("MIT".to_string()),
            is_8020_certified: false,
            sector: None,
            dark_matter_reduction_target: None,
        };

        // Exact name match should give highest score
        let score = calculate_relevance(&pkg, "rust cli", false);
        assert!(
            score >= scoring::EXACT_NAME_MATCH,
            "Exact match should have high score: {}",
            score
        );
    }

    #[test]
    fn test_package_metadata_conversion_preserves_8020_fields() {
        // FMEA FM9: Test data consistency from PackageInfo → PackageMetadata → SearchResult
        let info = PackageInfo {
            name: "test-package".to_string(),
            version: "1.0.0".to_string(),
            category: "rust".to_string(),
            description: "Test package".to_string(),
            tags: vec!["test".to_string()],
            keywords: vec![],
            author: Some("test-author".to_string()),
            license: Some("MIT".to_string()),
            downloads: 100,
            stars: 10,
            production_ready: true,
            dependencies: vec![],
            path: None,
            download_url: None,
            checksum: None,
            is_8020_certified: true,
            sector: Some("observability".to_string()),
            dark_matter_reduction_target: Some("reduce-logs".to_string()),
        };

        // Convert to PackageMetadata
        let metadata = PackageMetadata::from(info.clone());
        assert_eq!(metadata.is_8020_certified, info.is_8020_certified);
        assert_eq!(metadata.sector, info.sector);
        assert_eq!(
            metadata.dark_matter_reduction_target,
            info.dark_matter_reduction_target
        );

        // Simulate SearchResult construction
        let search_result = SearchResult {
            id: metadata.id.clone(),
            name: metadata.name.clone(),
            version: metadata.latest_version.clone(),
            description: metadata.description.clone(),
            author: Some(metadata.author.clone()),
            category: Some(metadata.category.clone()),
            tags: metadata.tags.clone(),
            stars: metadata.stars,
            downloads: metadata.downloads,
            is_8020_certified: metadata.is_8020_certified,
            sector: metadata.sector.as_ref().cloned(),
            dark_matter_reduction_target: metadata.dark_matter_reduction_target.as_ref().cloned(),
        };

        // Verify end-to-end consistency
        assert_eq!(search_result.is_8020_certified, info.is_8020_certified);
        assert_eq!(search_result.sector, info.sector);
        assert_eq!(
            search_result.dark_matter_reduction_target,
            info.dark_matter_reduction_target
        );

        // FMEA FM15: Verify id consistency - id should match name
        assert_eq!(search_result.id, info.name);
        assert_eq!(search_result.name, info.name);
    }

    #[test]
    fn test_sector_filter_case_insensitive_ascii() {
        // FMEA FM2: Test that sector filtering works with ASCII case-insensitive matching
        let pkg1 = PackageMetadata {
            id: "test1".to_string(),
            name: "test1".to_string(),
            description: "Test".to_string(),
            tags: vec![],
            keywords: vec![],
            category: "test".to_string(),
            author: "test".to_string(),
            latest_version: "1.0.0".to_string(),
            downloads: 0,
            stars: 0,
            license: None,
            is_8020_certified: false,
            sector: Some("Observability".to_string()), // Mixed case
            dark_matter_reduction_target: None,
        };

        let pkg2 = PackageMetadata {
            id: "test2".to_string(),
            name: "test2".to_string(),
            description: "Test".to_string(),
            tags: vec![],
            keywords: vec![],
            category: "test".to_string(),
            author: "test".to_string(),
            latest_version: "1.0.0".to_string(),
            downloads: 0,
            stars: 0,
            license: None,
            is_8020_certified: false,
            sector: Some("observability".to_string()), // Lower case
            dark_matter_reduction_target: None,
        };

        // Both should match "observability" filter (case-insensitive)
        let filter = "observability";
        assert!(pkg1.sector.as_ref().unwrap().eq_ignore_ascii_case(filter));
        assert!(pkg2.sector.as_ref().unwrap().eq_ignore_ascii_case(filter));

        // Note: eq_ignore_ascii_case only handles ASCII - Unicode sectors may not match
        // This is acceptable for current use case (sectors are ASCII)
    }

    #[test]
    fn test_relevance_calculation_fuzzy_match() {
        let pkg = PackageMetadata {
            id: "test".to_string(),
            name: "Rust Web".to_string(),
            description: "A Rust web framework".to_string(),
            tags: vec!["rust".to_string(), "web".to_string()],
            keywords: vec!["http".to_string()],
            category: "rust".to_string(),
            author: "test".to_string(),
            latest_version: "1.0.0".to_string(),
            downloads: 500,
            stars: 25,
            license: Some("MIT".to_string()),
            is_8020_certified: false,
            sector: None,
            dark_matter_reduction_target: None,
        };

        // Fuzzy match with typo should still match
        let score_fuzzy = calculate_relevance(&pkg, "rst web", true);
        assert!(score_fuzzy > 0.0, "Fuzzy match should have some score");

        // Without fuzzy, no match
        let score_no_fuzzy = calculate_relevance(&pkg, "xyz", false);
        assert!(
            score_no_fuzzy < scoring::TAG_KEYWORD_MATCH,
            "Unrelated query should have low score"
        );
    }

    #[tokio::test]
    async fn test_search_packages_real_index() {
        let filters = SearchFilters::new();
        let results = search_packages("rust", &filters).await;

        // FM6 & FM7: STRICT behavior - fails deterministically if registry doesn't exist
        // In test environment, this is expected to fail if registry index not available
        match results {
            Ok(_) => {
                // If registry exists, search succeeds
                assert!(true);
            }
            Err(e) => {
                // If registry missing, error message should be clear and actionable
                let msg = e.to_string();
                assert!(
                    msg.contains("Registry index not found")
                        || msg.contains("registry is corrupted"),
                    "Error message should indicate missing or corrupted registry: {}",
                    msg
                );
            }
        }
    }

    #[tokio::test]
    async fn test_search_packages_with_limit() {
        let filters = SearchFilters::new().with_limit(1);
        let results = search_packages("cli", &filters).await;

        // FM6 & FM7: STRICT behavior - if search succeeds, results must respect limit
        if let Ok(results) = results {
            assert!(results.len() <= 1, "Should respect limit of 1 result");
        }
    }

    #[tokio::test]
    async fn test_search_packages_with_category_filter() {
        let filters = SearchFilters::new().with_category("rust");
        let results = search_packages("web", &filters).await;

        // FM6 & FM7: STRICT behavior - if search succeeds, all results must match category
        if let Ok(results) = results {
            for result in results {
                assert_eq!(
                    result.category,
                    Some("rust".to_string()),
                    "All results should be in rust category"
                );
            }
        }
    }

    #[tokio::test]
    async fn test_search_packages_with_fuzzy() {
        let filters = SearchFilters::new().with_fuzzy(true);
        let results = search_packages("pyton", &filters).await;

        // FM6 & FM7: STRICT behavior - fuzzy search succeeds if registry exists,
        // but fails deterministically if registry is missing or corrupted
        match results {
            Ok(_results) => {
                // If registry exists, fuzzy search works
                assert!(true);
            }
            Err(e) => {
                // If registry missing, error message should be clear
                let msg = e.to_string();
                assert!(
                    msg.contains("Registry index not found")
                        || msg.contains("registry is corrupted"),
                    "Error should indicate missing or corrupted registry: {}",
                    msg
                );
            }
        }
    }
}