terraphim-session-analyzer 1.16.34

Analyze AI coding assistant session logs to identify agent usage patterns
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
//! Pattern matcher implementation using Aho-Corasick algorithm
//!
//! This module provides efficient multi-pattern string matching to identify
//! tool usage in Bash commands.

use aho_corasick::{AhoCorasick, AhoCorasickBuilder, MatchKind};
use anyhow::Result;
use std::collections::HashMap;

// Terraphim imports for knowledge graph automata
#[cfg(feature = "terraphim")]
use terraphim_automata::find_matches as terraphim_find_matches;
#[cfg(feature = "terraphim")]
use terraphim_types::{NormalizedTerm, NormalizedTermValue, Thesaurus};

use super::loader::ToolPattern;

/// Trait for pattern matching implementations
pub trait PatternMatcher: Send + Sync {
    /// Initialize the matcher with tool patterns
    ///
    /// # Errors
    ///
    /// Returns an error if the automaton cannot be built from the patterns
    fn initialize(&mut self, patterns: &[ToolPattern]) -> Result<()>;

    /// Find all tool matches in the given text
    ///
    /// Returns matches ordered by position (leftmost-longest)
    fn find_matches<'a>(&self, text: &'a str) -> Vec<ToolMatch<'a>>;

    /// Get the matcher type identifier
    #[allow(dead_code)] // May be used for debugging
    fn matcher_type(&self) -> &'static str;
}

/// Represents a tool match found in text
#[derive(Debug, Clone, PartialEq)]
pub struct ToolMatch<'a> {
    /// The name of the matched tool
    pub tool_name: String,

    /// Start position in the text
    pub start: usize,

    /// End position in the text
    pub end: usize,

    /// The matched text
    pub text: &'a str,

    /// Category of the tool
    pub category: String,

    /// Confidence score (0.0 - 1.0)
    pub confidence: f32,
}

/// Aho-Corasick based pattern matcher
///
/// Uses efficient automaton-based matching for high performance
/// even with many patterns.
pub struct AhoCorasickMatcher {
    /// The Aho-Corasick automaton
    automaton: Option<AhoCorasick>,

    /// Mapping from pattern index to tool metadata
    pattern_to_tool: HashMap<usize, ToolInfo>,
}

#[derive(Debug, Clone)]
struct ToolInfo {
    name: String,
    category: String,
    confidence: f32,
}

impl Default for AhoCorasickMatcher {
    fn default() -> Self {
        Self::new()
    }
}

impl AhoCorasickMatcher {
    /// Create a new uninitialized matcher
    #[must_use]
    pub fn new() -> Self {
        Self {
            automaton: None,
            pattern_to_tool: HashMap::new(),
        }
    }

    /// Build the Aho-Corasick automaton from patterns
    fn build_automaton(&mut self, patterns: &[ToolPattern]) -> Result<()> {
        let mut all_patterns = Vec::new();
        self.pattern_to_tool.clear();

        for tool in patterns.iter() {
            for pattern in &tool.patterns {
                let pattern_idx = all_patterns.len();
                all_patterns.push(pattern.clone());

                self.pattern_to_tool.insert(
                    pattern_idx,
                    ToolInfo {
                        name: tool.name.clone(),
                        category: tool.metadata.category.clone(),
                        confidence: tool.metadata.confidence,
                    },
                );
            }
        }

        let automaton = AhoCorasickBuilder::new()
            .ascii_case_insensitive(true)
            .match_kind(MatchKind::LeftmostLongest)
            .build(&all_patterns)
            .map_err(|e| anyhow::anyhow!("Failed to build Aho-Corasick automaton: {e}"))?;

        self.automaton = Some(automaton);
        Ok(())
    }
}

impl PatternMatcher for AhoCorasickMatcher {
    fn initialize(&mut self, patterns: &[ToolPattern]) -> Result<()> {
        self.build_automaton(patterns)
    }

    fn find_matches<'a>(&self, text: &'a str) -> Vec<ToolMatch<'a>> {
        let Some(ref automaton) = self.automaton else {
            return Vec::new();
        };

        let mut matches = Vec::new();

        for mat in automaton.find_iter(text) {
            if let Some(tool_info) = self.pattern_to_tool.get(&mat.pattern().as_usize()) {
                matches.push(ToolMatch {
                    tool_name: tool_info.name.clone(),
                    start: mat.start(),
                    end: mat.end(),
                    text: &text[mat.start()..mat.end()],
                    category: tool_info.category.clone(),
                    confidence: tool_info.confidence,
                });
            }
        }

        matches
    }

    fn matcher_type(&self) -> &'static str {
        "aho-corasick"
    }
}

/// Terraphim-based pattern matcher using knowledge graph automata
///
/// This implementation uses the actual terraphim_automata library for pattern matching,
/// which provides knowledge graph-based semantic search capabilities.
#[cfg(feature = "terraphim")]
pub struct TerraphimMatcher {
    /// Thesaurus containing the pattern mappings
    thesaurus: Option<Thesaurus>,

    /// Mapping from tool name to metadata
    tool_metadata: HashMap<String, (String, f32)>, // (category, confidence)

    /// Fallback Aho-Corasick matcher for error cases
    fallback: AhoCorasickMatcher,
}

#[cfg(feature = "terraphim")]
impl Default for TerraphimMatcher {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "terraphim")]
impl TerraphimMatcher {
    /// Create a new uninitialized Terraphim matcher
    #[must_use]
    pub fn new() -> Self {
        Self {
            thesaurus: None,
            tool_metadata: HashMap::new(),
            fallback: AhoCorasickMatcher::new(),
        }
    }

    /// Build a Thesaurus from tool patterns
    fn build_thesaurus(&mut self, patterns: &[ToolPattern]) -> Result<()> {
        let mut thesaurus = Thesaurus::new("Tool Patterns".to_string());
        let mut pattern_id = 0u64;

        // Clear and rebuild metadata map
        self.tool_metadata.clear();

        for tool in patterns {
            // Store tool metadata
            self.tool_metadata.insert(
                tool.name.clone(),
                (tool.metadata.category.clone(), tool.metadata.confidence),
            );

            for pattern in &tool.patterns {
                pattern_id += 1;

                // Create a normalized term for this pattern
                let normalized_term = NormalizedTerm {
                    id: pattern_id,
                    value: NormalizedTermValue::from(tool.name.as_str()),
                    display_value: None,
                    url: tool.metadata.description.as_ref().map(|d| d.to_string()),
                };

                // Insert the pattern -> normalized term mapping
                thesaurus.insert(NormalizedTermValue::from(pattern.as_str()), normalized_term);
            }
        }

        self.thesaurus = Some(thesaurus);
        Ok(())
    }
}

#[cfg(feature = "terraphim")]
impl PatternMatcher for TerraphimMatcher {
    fn initialize(&mut self, patterns: &[ToolPattern]) -> Result<()> {
        // Build the terraphim thesaurus
        self.build_thesaurus(patterns)?;

        // Also initialize fallback in case terraphim fails
        self.fallback.initialize(patterns)?;

        Ok(())
    }

    fn find_matches<'a>(&self, text: &'a str) -> Vec<ToolMatch<'a>> {
        // Use the actual terraphim_automata library
        let Some(ref thesaurus) = self.thesaurus else {
            // If thesaurus not initialized, use fallback
            return self.fallback.find_matches(text);
        };

        // Call the actual terraphim_automata find_matches function
        match terraphim_find_matches(text, thesaurus.clone(), true) {
            Ok(matches) => {
                // Convert terraphim matches to our ToolMatch format
                matches
                    .into_iter()
                    .filter_map(|m| {
                        let tool_name = m.normalized_term.value.to_string();

                        // Look up category and confidence from metadata
                        let (category, confidence) = self
                            .tool_metadata
                            .get(&tool_name)
                            .map(|(cat, conf)| (cat.clone(), *conf))
                            .unwrap_or_else(|| ("unknown".to_string(), 0.5));

                        // Extract position from the pos field
                        m.pos.map(|(start, end)| ToolMatch {
                            tool_name,
                            start,
                            end,
                            text: &text[start..end],
                            category,
                            confidence,
                        })
                    })
                    .collect()
            }
            Err(_) => {
                // If terraphim fails, fall back to aho-corasick
                self.fallback.find_matches(text)
            }
        }
    }

    fn matcher_type(&self) -> &'static str {
        if self.thesaurus.is_some() {
            "terraphim-automata"
        } else {
            "terraphim-automata (uninitialized)"
        }
    }
}

/// Factory function to create a new pattern matcher
///
/// Returns Terraphim matcher if the feature is enabled,
/// otherwise returns the default Aho-Corasick implementation
#[must_use]
#[allow(dead_code)] // Used in doc examples
pub fn create_matcher() -> Box<dyn PatternMatcher> {
    #[cfg(feature = "terraphim")]
    {
        Box::new(TerraphimMatcher::new())
    }

    #[cfg(not(feature = "terraphim"))]
    {
        Box::new(AhoCorasickMatcher::new())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::patterns::loader::ToolMetadata;

    fn create_test_patterns() -> Vec<ToolPattern> {
        vec![
            ToolPattern {
                name: "wrangler".to_string(),
                patterns: vec!["npx wrangler".to_string(), "bunx wrangler".to_string()],
                metadata: ToolMetadata {
                    category: "cloudflare".to_string(),
                    description: Some("Cloudflare Workers CLI".to_string()),
                    confidence: 0.95,
                },
            },
            ToolPattern {
                name: "npm".to_string(),
                patterns: vec!["npm ".to_string()],
                metadata: ToolMetadata {
                    category: "package-manager".to_string(),
                    description: Some("Node package manager".to_string()),
                    confidence: 0.9,
                },
            },
        ]
    }

    #[test]
    fn test_matcher_initialization() {
        let patterns = create_test_patterns();
        let mut matcher = AhoCorasickMatcher::new();

        let result = matcher.initialize(&patterns);
        assert!(result.is_ok());
        assert!(matcher.automaton.is_some());
    }

    #[test]
    fn test_find_matches_basic() {
        let patterns = create_test_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "npx wrangler deploy --env production";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
        assert_eq!(matches[0].text, "npx wrangler");
        assert_eq!(matches[0].category, "cloudflare");
    }

    #[test]
    fn test_find_matches_case_insensitive() {
        let patterns = create_test_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "NPX WRANGLER deploy";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
    }

    #[test]
    fn test_find_matches_multiple_tools() {
        let patterns = create_test_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "npm install && npx wrangler deploy";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 2);
        assert_eq!(matches[0].tool_name, "npm");
        assert_eq!(matches[1].tool_name, "wrangler");
    }

    #[test]
    fn test_find_matches_alternative_pattern() {
        let patterns = create_test_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "bunx wrangler dev";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
        assert_eq!(matches[0].text, "bunx wrangler");
    }

    #[test]
    fn test_find_matches_no_matches() {
        let patterns = create_test_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "echo hello world";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 0);
    }

    #[test]
    fn test_matcher_type() {
        let matcher = AhoCorasickMatcher::new();
        assert_eq!(matcher.matcher_type(), "aho-corasick");
    }

    #[test]
    fn test_create_matcher_factory() {
        let matcher = create_matcher();

        // Factory returns different matchers based on features
        #[cfg(feature = "terraphim")]
        assert_eq!(matcher.matcher_type(), "terraphim-automata (uninitialized)");

        #[cfg(not(feature = "terraphim"))]
        assert_eq!(matcher.matcher_type(), "aho-corasick");
    }

    #[test]
    fn test_uninitialized_matcher() {
        let matcher = AhoCorasickMatcher::new();
        let matches = matcher.find_matches("npx wrangler deploy");
        assert_eq!(matches.len(), 0);
    }
}

#[cfg(test)]
mod wrangler_tests {
    use super::*;
    use crate::patterns::loader::ToolMetadata;

    /// Create comprehensive wrangler patterns with all package manager variants
    fn create_wrangler_patterns() -> Vec<ToolPattern> {
        vec![ToolPattern {
            name: "wrangler".to_string(),
            patterns: vec![
                "npx wrangler".to_string(),
                "bunx wrangler".to_string(),
                "pnpm wrangler".to_string(),
                "yarn wrangler".to_string(),
            ],
            metadata: ToolMetadata {
                category: "cloudflare".to_string(),
                description: Some("Cloudflare Workers CLI".to_string()),
                confidence: 0.95,
            },
        }]
    }

    #[test]
    fn test_wrangler_login_npx() {
        let patterns = create_wrangler_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "npx wrangler login";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
        assert_eq!(matches[0].text, "npx wrangler");
        assert_eq!(matches[0].category, "cloudflare");
        assert_eq!(matches[0].confidence, 0.95);
    }

    #[test]
    fn test_wrangler_login_bunx() {
        let patterns = create_wrangler_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "bunx wrangler login";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
        assert_eq!(matches[0].text, "bunx wrangler");
        assert_eq!(matches[0].category, "cloudflare");
    }

    #[test]
    fn test_wrangler_deploy_basic() {
        let patterns = create_wrangler_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "npx wrangler deploy";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
        assert_eq!(matches[0].text, "npx wrangler");
    }

    #[test]
    fn test_wrangler_deploy_with_env() {
        let patterns = create_wrangler_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        // Test npx variant
        let text = "npx wrangler deploy --env production";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
        assert_eq!(matches[0].text, "npx wrangler");

        // Test bunx variant
        let text = "bunx wrangler deploy --env staging";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
        assert_eq!(matches[0].text, "bunx wrangler");
    }

    #[test]
    fn test_wrangler_deploy_with_minify() {
        let patterns = create_wrangler_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "npx wrangler deploy --minify";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
        assert_eq!(matches[0].text, "npx wrangler");
    }

    #[test]
    fn test_wrangler_deploy_complex_flags() {
        let patterns = create_wrangler_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "npx wrangler deploy --env prod --minify --compatibility-date 2024-01-01";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
        assert_eq!(matches[0].text, "npx wrangler");
        assert_eq!(matches[0].start, 0);
        assert_eq!(matches[0].end, 12); // "npx wrangler" is 12 characters
    }

    #[test]
    fn test_wrangler_all_package_managers() {
        let patterns = create_wrangler_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        // Test all package manager variants
        let test_cases = vec![
            ("npx wrangler deploy", "npx wrangler"),
            ("bunx wrangler deploy", "bunx wrangler"),
            ("pnpm wrangler deploy", "pnpm wrangler"),
            ("yarn wrangler deploy", "yarn wrangler"),
        ];

        for (command, expected_text) in test_cases {
            let matches = matcher.find_matches(command);
            assert_eq!(matches.len(), 1, "Failed for command: {command}");
            assert_eq!(matches[0].tool_name, "wrangler");
            assert_eq!(matches[0].text, expected_text);
            assert_eq!(matches[0].category, "cloudflare");
        }
    }

    #[test]
    fn test_wrangler_publish() {
        let patterns = create_wrangler_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "npx wrangler publish";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
        assert_eq!(matches[0].text, "npx wrangler");
    }

    #[test]
    fn test_wrangler_dev() {
        let patterns = create_wrangler_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "bunx wrangler dev";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
        assert_eq!(matches[0].text, "bunx wrangler");
    }

    #[test]
    fn test_wrangler_tail() {
        let patterns = create_wrangler_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "npx wrangler tail";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
        assert_eq!(matches[0].text, "npx wrangler");
    }

    #[test]
    fn test_wrangler_case_insensitive() {
        let patterns = create_wrangler_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "NPX WRANGLER DEPLOY";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
    }

    #[test]
    fn test_wrangler_in_pipeline() {
        let patterns = create_wrangler_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "npm install && npx wrangler deploy && npm test";
        let matches = matcher.find_matches(text);

        // Should find wrangler
        let wrangler_matches: Vec<_> = matches
            .iter()
            .filter(|m| m.tool_name == "wrangler")
            .collect();
        assert_eq!(wrangler_matches.len(), 1);
        assert_eq!(wrangler_matches[0].text, "npx wrangler");
    }

    #[test]
    fn test_wrangler_multiple_commands() {
        let patterns = create_wrangler_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "npx wrangler login && bunx wrangler deploy";
        let matches = matcher.find_matches(text);

        // Should find both wrangler invocations
        assert_eq!(matches.len(), 2);
        assert_eq!(matches[0].tool_name, "wrangler");
        assert_eq!(matches[0].text, "npx wrangler");
        assert_eq!(matches[1].tool_name, "wrangler");
        assert_eq!(matches[1].text, "bunx wrangler");
    }

    #[test]
    fn test_wrangler_with_output_redirection() {
        let patterns = create_wrangler_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "npx wrangler deploy > deploy.log 2>&1";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
        assert_eq!(matches[0].text, "npx wrangler");
    }

    #[test]
    fn test_wrangler_subcommands() {
        let patterns = create_wrangler_patterns();
        let mut matcher = AhoCorasickMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let subcommands = vec![
            "login",
            "deploy",
            "publish",
            "dev",
            "tail",
            "whoami",
            "init",
            "secret",
            "kv:namespace",
            "pages",
        ];

        for subcommand in subcommands {
            let text = format!("npx wrangler {subcommand}");
            let matches = matcher.find_matches(&text);

            assert_eq!(matches.len(), 1, "Failed for subcommand: {subcommand}");
            assert_eq!(matches[0].tool_name, "wrangler");
            assert_eq!(matches[0].text, "npx wrangler");
        }
    }
}

#[cfg(all(test, feature = "terraphim"))]
mod terraphim_tests {
    use super::*;
    use crate::patterns::loader::ToolMetadata;

    fn create_test_patterns() -> Vec<ToolPattern> {
        vec![
            ToolPattern {
                name: "wrangler".to_string(),
                patterns: vec!["npx wrangler".to_string(), "bunx wrangler".to_string()],
                metadata: ToolMetadata {
                    category: "cloudflare".to_string(),
                    description: Some("Cloudflare Workers CLI".to_string()),
                    confidence: 0.95,
                },
            },
            ToolPattern {
                name: "npm".to_string(),
                patterns: vec!["npm ".to_string()],
                metadata: ToolMetadata {
                    category: "package-manager".to_string(),
                    description: Some("Node package manager".to_string()),
                    confidence: 0.9,
                },
            },
        ]
    }

    #[test]
    fn test_terraphim_matcher_initialization() {
        let patterns = create_test_patterns();
        let mut matcher = TerraphimMatcher::new();

        let result = matcher.initialize(&patterns);
        assert!(result.is_ok());
    }

    #[test]
    fn test_terraphim_find_matches_basic() {
        let patterns = create_test_patterns();
        let mut matcher = TerraphimMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "npx wrangler deploy --env production";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
        assert_eq!(matches[0].category, "cloudflare");
    }

    #[test]
    fn test_terraphim_find_matches_case_insensitive() {
        let patterns = create_test_patterns();
        let mut matcher = TerraphimMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "NPX WRANGLER deploy";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
    }

    #[test]
    fn test_terraphim_find_matches_multiple_tools() {
        let patterns = create_test_patterns();
        let mut matcher = TerraphimMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "npm install && npx wrangler deploy";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 2);
        assert_eq!(matches[0].tool_name, "npm");
        assert_eq!(matches[1].tool_name, "wrangler");
    }

    #[test]
    fn test_terraphim_find_matches_alternative_pattern() {
        let patterns = create_test_patterns();
        let mut matcher = TerraphimMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "bunx wrangler dev";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].tool_name, "wrangler");
    }

    #[test]
    fn test_terraphim_find_matches_no_matches() {
        let patterns = create_test_patterns();
        let mut matcher = TerraphimMatcher::new();
        matcher.initialize(&patterns).unwrap();

        let text = "echo hello world";
        let matches = matcher.find_matches(text);

        assert_eq!(matches.len(), 0);
    }

    #[test]
    fn test_terraphim_matcher_type() {
        let matcher = TerraphimMatcher::new();
        assert_eq!(matcher.matcher_type(), "terraphim-automata (uninitialized)");

        // After initialization, should be terraphim-automata
        let patterns = create_test_patterns();
        let mut matcher = TerraphimMatcher::new();
        matcher.initialize(&patterns).unwrap();
        assert_eq!(matcher.matcher_type(), "terraphim-automata");
    }

    #[test]
    fn test_terraphim_create_matcher_factory() {
        let matcher = create_matcher();
        // Uninitialized matcher
        assert_eq!(matcher.matcher_type(), "terraphim-automata (uninitialized)");
    }

    #[test]
    fn test_terraphim_uninitialized_matcher() {
        let matcher = TerraphimMatcher::new();
        let matches = matcher.find_matches("npx wrangler deploy");
        assert_eq!(matches.len(), 0);
    }
}