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
//! Unit tests for Universal Demo components
//!
//! These tests verify individual components without network access.
#[cfg(test)]
mod universal_demo_unit_tests {
use pmat::services::context::AstItem;
use pmat::services::deep_context::{
ComplexityMetricsForQA, DeadCodeAnalysis, DeadCodeSummary, FileComplexityMetricsForQA,
};
use pmat::services::quality_gates::{QAVerification, VerificationStatus};
use std::path::PathBuf;
#[test]
fn test_import_ast_item_creation() {
// Test Python-style imports
let import1 = AstItem::Import {
module: "numpy".to_string(),
items: vec![],
alias: Some("np".to_string()),
line: 1,
};
assert_eq!(import1.display_name(), "numpy");
// Test from...import style
let import2 = AstItem::Import {
module: "typing".to_string(),
items: vec!["List".to_string(), "Dict".to_string()],
alias: None,
line: 2,
};
assert_eq!(import2.display_name(), "typing");
// Test JavaScript-style imports
let import3 = AstItem::Import {
module: "react".to_string(),
items: vec!["useState".to_string(), "useEffect".to_string()],
alias: None,
line: 3,
};
assert_eq!(import3.display_name(), "react");
}
#[test]
fn test_quality_gate_with_no_complexity_metrics() {
use chrono::Utc;
use pmat::services::deep_context::{
AnalysisResults, CacheStats, ContextMetadata, DeepContextResult, DefectSummary,
QualityScorecard,
};
use std::time::Duration;
// Create a result with no complexity metrics but with files
let mut result = DeepContextResult {
metadata: ContextMetadata {
generated_at: Utc::now(),
tool_version: "test".to_string(),
project_root: PathBuf::from("/test"),
cache_stats: CacheStats {
hit_rate: 0.0,
memory_efficiency: 0.0,
time_saved_ms: 0,
},
analysis_duration: Duration::from_secs(1),
},
file_tree: vec![
"main.py".to_string(),
"utils.py".to_string(),
"test.js".to_string(),
],
analyses: AnalysisResults {
complexity_report: None,
churn_analysis: None,
dependency_graph: None,
dead_code_results: None,
satd_results: None,
duplicate_code_results: None,
provability_results: None,
cross_language_refs: vec![],
big_o_analysis: None,
ast_contexts: vec![],
},
quality_scorecard: QualityScorecard {
overall_health: Some(75.0),
complexity_score: Some(80.0),
maintainability_index: Some(70.0),
modularity_score: Some(85.0),
test_coverage: Some(0.0),
technical_debt_hours: Some(0.0),
},
template_provenance: None,
defect_summary: DefectSummary {
total_defects: 0,
by_severity: Default::default(),
by_type: Default::default(),
defect_density: 0.0,
},
hotspots: vec![],
recommendations: vec![],
qa_verification: None,
complexity_metrics: None,
dead_code_analysis: None,
ast_summaries: None,
churn_analysis: None,
language_stats: None,
build_info: None,
project_overview: None,
};
// Test with no metrics at all
let qa = QAVerification::new();
let verification = qa.verify(&result);
// Should handle gracefully
assert!(verification.contains_key("dead_code_sanity"));
// Add dead code analysis with line counts
result.dead_code_analysis = Some(DeadCodeAnalysis {
summary: DeadCodeSummary {
total_functions: 10,
dead_functions: 1,
total_lines: 500,
total_dead_lines: 50,
dead_percentage: 10.0,
},
dead_functions: vec![],
warnings: vec![],
});
let verification2 = qa.verify(&result);
assert!(verification2.contains_key("dead_code_sanity"));
}
#[test]
fn test_complexity_metrics_fallback() {
// Test that complexity metrics can be created from basic file info
let metrics = ComplexityMetricsForQA {
files: vec![
FileComplexityMetricsForQA {
path: PathBuf::from("test.py"),
functions: vec![],
total_cyclomatic: 0,
total_cognitive: 0,
total_lines: 100,
},
FileComplexityMetricsForQA {
path: PathBuf::from("main.js"),
functions: vec![],
total_cyclomatic: 0,
total_cognitive: 0,
total_lines: 200,
},
],
summary: Default::default(),
};
// Should be able to get total line count
let total_lines: usize = metrics.files.iter().map(|f| f.total_lines).sum();
assert_eq!(total_lines, 300);
}
#[test]
fn test_verification_status_ordering() {
// Ensure verification statuses have expected properties
use VerificationStatus::*;
// Test equality
assert_eq!(Pass, Pass);
assert_eq!(Fail, Fail);
assert_eq!(Partial, Partial);
// Test inequality
assert_ne!(Pass, Fail);
assert_ne!(Pass, Partial);
assert_ne!(Fail, Partial);
}
#[test]
fn test_python_import_variations() {
let test_cases = vec![
(
AstItem::Import {
module: "os".to_string(),
items: vec![],
alias: None,
line: 1,
},
"os",
),
(
AstItem::Import {
module: "os.path".to_string(),
items: vec![],
alias: None,
line: 2,
},
"os.path",
),
(
AstItem::Import {
module: "numpy".to_string(),
items: vec![],
alias: Some("np".to_string()),
line: 3,
},
"numpy",
),
(
AstItem::Import {
module: "matplotlib.pyplot".to_string(),
items: vec![],
alias: Some("plt".to_string()),
line: 4,
},
"matplotlib.pyplot",
),
(
AstItem::Import {
module: "typing".to_string(),
items: vec![
"List".to_string(),
"Dict".to_string(),
"Optional".to_string(),
],
alias: None,
line: 5,
},
"typing",
),
];
for (import, expected_name) in test_cases {
assert_eq!(
import.display_name(),
expected_name,
"Import {:?} should display as {}",
import,
expected_name
);
}
}
#[test]
fn test_javascript_import_variations() {
let test_cases = vec![
(
AstItem::Import {
module: "react".to_string(),
items: vec![],
alias: None,
line: 1,
},
"react",
),
(
AstItem::Import {
module: "react".to_string(),
items: vec!["useState".to_string()],
alias: None,
line: 2,
},
"react",
),
(
AstItem::Import {
module: "./utils".to_string(),
items: vec![],
alias: None,
line: 3,
},
"./utils",
),
(
AstItem::Import {
module: "@mui/material".to_string(),
items: vec!["Button".to_string(), "TextField".to_string()],
alias: None,
line: 4,
},
"@mui/material",
),
];
for (import, expected_name) in test_cases {
assert_eq!(
import.display_name(),
expected_name,
"Import {:?} should display as {}",
import,
expected_name
);
}
}
}