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
// EXTREME TDD: Red Team CLI Handler Tests (RED Phase)
//
// Test-Driven Development for `pmat red-team analyze` command handler
// Specification: Section 4.2 - CLI Interface
use pmat::red_team::{
ClaimExtractor, CommitInfo, EvidenceGatherer, IntentClassifier, RepositoryContext, TestChanges,
};
// RED Test 1: Handler analyzes commit message and detects hallucination
#[test]
fn test_handler_analyze_commit_message() {
// Simulate input: commit message
let commit_message = "feat: All tests passing";
// Expected flow:
// 1. Extract claims from message
// 2. Gather evidence (mock context)
// 3. Report hallucination if evidence contradicts
let extractor = ClaimExtractor::new();
let claims = extractor.extract(commit_message);
assert_eq!(claims.len(), 1);
assert_eq!(claims[0].text.to_lowercase(), "all tests passing");
// Mock: 5 tests are ignored
let context = RepositoryContext::new_mock().with_test_results(true, 5);
let gatherer = EvidenceGatherer::new();
let evidence = gatherer.gather_evidence(&claims[0], &context);
// Should find contradicting evidence
assert!(evidence.iter().any(|e| !e.supports_claim));
}
// RED Test 2: Handler analyzes two commits and classifies intent
#[test]
fn test_handler_analyze_commit_pair() {
let original = CommitInfo {
message: "feat: Complete feature X".to_string(),
timestamp_seconds: 1000,
modified_files: vec!["src/feature_x.rs".to_string()],
issue_number: None,
issue_created_timestamp: None,
branch: "feature/x".to_string(),
test_changes: TestChanges {
added_tests: 0,
fixed_tests: 0,
modified_test_files: vec![],
},
};
let followup = CommitInfo {
message: "fix: Bug in feature X".to_string(),
timestamp_seconds: 1000 + (100 * 3600),
modified_files: vec!["src/feature_x.rs".to_string()],
issue_number: Some(42),
issue_created_timestamp: Some(5000),
branch: "hotfix/feature-x".to_string(),
test_changes: TestChanges {
added_tests: 0,
fixed_tests: 3,
modified_test_files: vec!["tests/feature_x_tests.rs".to_string()],
},
};
let classifier = IntentClassifier::new();
let classification = classifier.classify(&original, &followup);
assert_eq!(
classification.intent,
pmat::red_team::CommitIntent::HallucinationFix
);
assert!(classification.confidence > 0.7);
}
// RED Test 3: Handler output formatting (human-readable report)
#[test]
fn test_handler_generates_human_readable_report() {
let commit_message = "feat: All tests passing";
let extractor = ClaimExtractor::new();
let claims = extractor.extract(commit_message);
// Mock: contradicting evidence
let context = RepositoryContext::new_mock()
.with_test_results(false, 5)
.with_subsequent_commits(vec!["fix: tests".to_string()]);
let gatherer = EvidenceGatherer::new();
let evidence = gatherer.gather_evidence(&claims[0], &context);
// Expected report structure (verified in handler implementation):
// 🔴 HALLUCINATION DETECTED
//
// Claim: "all tests passing"
// Evidence:
// 1. Test Execution: 5 tests ignored (confidence: X.XX)
// 2. Git History: 1 subsequent fix found (confidence: X.XX)
// Verdict: POTENTIAL HALLUCINATION
// Verify evidence exists and can be formatted
assert!(evidence.len() >= 2);
assert!(evidence.iter().all(|e| !e.details.is_empty()));
assert!(evidence
.iter()
.all(|e| e.confidence >= 0.0 && e.confidence <= 1.0));
}
// RED Test 4: Handler supports multiple output formats
#[test]
fn test_handler_supports_output_formats() {
// Test that handler can output in different formats
// Formats: text (default), json, junit
let commit_message = "feat: Complete migration to libsql";
let extractor = ClaimExtractor::new();
let claims = extractor.extract(commit_message);
assert_eq!(claims.len(), 1);
// Mock: old system still referenced
let context = RepositoryContext::new_mock().with_code_grep_results("sled", 15);
let gatherer = EvidenceGatherer::new();
let evidence = gatherer.gather_evidence(&claims[0], &context);
// Should detect migration incompleteness
assert!(evidence.iter().any(|e| !e.supports_claim));
}
// RED Test 5: Handler respects confidence threshold
#[test]
fn test_handler_respects_confidence_threshold() {
let commit_message = "fix: Minor improvements";
let extractor = ClaimExtractor::new();
let claims = extractor.extract(commit_message);
// "Minor improvements" should not extract any testable claims
assert_eq!(claims.len(), 0);
// Handler should not flag commits with no testable claims
// (Even if there's no confidence threshold applied)
}
// RED Test 6: Handler can analyze git repository commits
#[test]
#[ignore] // Requires git repository access
fn test_handler_analyzes_git_commits() {
// This test would analyze real git commits
// Using git2 or similar library
//
// Example flow:
// 1. Open git repository
// 2. Get commits since date
// 3. Analyze each commit message
// 4. Generate report
// Placeholder for future implementation
}
// RED Test 7: Handler exit codes
#[test]
fn test_handler_exit_codes() {
// Handler should return appropriate exit codes:
// 0 = No hallucinations found
// 1 = Hallucinations found (if --fail-on-hallucination)
// 2 = Error in execution
let commit_message = "test: Add new tests";
let extractor = ClaimExtractor::new();
let claims = extractor.extract(commit_message);
// "Add new tests" is a factual statement, not a claim
assert_eq!(claims.len(), 0);
// Handler should exit 0 (no hallucinations)
}
// RED Test 8: Handler configuration loading
#[test]
fn test_handler_loads_configuration() {
// Handler should support loading config from .pmat/red-team.toml
// Config options:
// - semantic_entropy_threshold
// - categories
// - validation settings
// - reporting format
// This test verifies config structure exists
// (Implementation will load from file system)
}