Skip to content

Commit 776028e

Browse files
committed
feat(bump): working on next release
1 parent 5bb6f47 commit 776028e

7 files changed

Lines changed: 1687 additions & 0 deletions

File tree

‎tests/unit/checkpoint_tests.rs‎

Lines changed: 374 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,374 @@
1+
//! Lane B checkpoint tests - 10 specific tests per target/lane_b_spec.txt
2+
//!
3+
//! These tests cover the exact requirements specified in the audit pinning.
4+
5+
use baco::checkpoint::{Checkpoint, ScanPhase};
6+
use baco::evidence::{Evidence, EvidenceSource, VerificationTier};
7+
use baco::findings::{Severity, VulnerabilityFinding};
8+
use chrono::Utc;
9+
use std::fs;
10+
11+
/// Create a test finding with verification_tier and evidence fields
12+
fn make_test_finding_with_evidence() -> VulnerabilityFinding {
13+
VulnerabilityFinding {
14+
id: "test-finding-123".to_string(),
15+
title: "Test Finding".to_string(),
16+
description: "A test vulnerability with evidence".to_string(),
17+
severity: Severity::High,
18+
confidence_score: 0.9,
19+
cwe_id: Some("CWE-79".to_string()),
20+
file_path: "src/test.rs".to_string(),
21+
line_number: Some(42),
22+
code_snippet: Some("unsafe_code()".to_string()),
23+
diff_hunk: None,
24+
recommendation: Some("Fix this".to_string()),
25+
code_location: Some("src/test.rs:42".to_string()),
26+
already_reported: false,
27+
sources: vec!["test".to_string()],
28+
commit_reference: None,
29+
ticket_reference: None,
30+
priority_score: Some(0.8),
31+
cross_file_references: None,
32+
verification_status: None,
33+
verification_notes: None,
34+
verification_error: None,
35+
agent_evidence_path: None,
36+
security_issue: None,
37+
poc_code: None,
38+
mitigation_code: None,
39+
poc_format: None,
40+
llm_model: None,
41+
agent_mode: false,
42+
statement_range: None,
43+
triage_verdict: None,
44+
evidence: vec![Evidence {
45+
source: EvidenceSource::Semgrep("semgrep-rule-1".to_string()),
46+
weight: 0.8,
47+
detail: "Test evidence detail".to_string(),
48+
timestamp: Utc::now(),
49+
}],
50+
verification_tier: Some(VerificationTier::Supported),
51+
}
52+
}
53+
54+
/// Get a unique temp path for checkpoint files
55+
fn temp_checkpoint_path(test_name: &str) -> String {
56+
format!("/tmp/baco_lane_b_test_{}.json", test_name)
57+
}
58+
59+
// ============================================================================
60+
// TEST 1: Table test for all 20 sequential phases
61+
// ============================================================================
62+
63+
#[test]
64+
fn test_sequential_phase_20_phase_table() {
65+
// Test that resume_from(phase) equals the next phase in sequential_phases order
66+
// This pins the exact mapping verified in audit
67+
let sequential_phases = [
68+
ScanPhase::CweRouting,
69+
ScanPhase::RuleSynthesis,
70+
ScanPhase::LlmDiscovery,
71+
ScanPhase::LlmVerification,
72+
ScanPhase::Validate,
73+
ScanPhase::SecurityAgentVerification,
74+
ScanPhase::TicketCrossRef,
75+
ScanPhase::GitAnalysis,
76+
ScanPhase::CrossFileAnalysis,
77+
ScanPhase::ConfidenceScoring,
78+
ScanPhase::AiAggregation,
79+
ScanPhase::ThreatModeling,
80+
ScanPhase::RootCauseDedup,
81+
ScanPhase::MultiVerifier,
82+
ScanPhase::AutoPatching,
83+
ScanPhase::CveBootstrap,
84+
ScanPhase::PocCompiler,
85+
ScanPhase::ExploitSynth,
86+
ScanPhase::VariantSearch,
87+
ScanPhase::Reporting,
88+
];
89+
90+
for i in 0..sequential_phases.len() - 1 {
91+
let mut checkpoint = Checkpoint::new("test", "/tmp", Utc::now());
92+
checkpoint.current_phase = sequential_phases[i].clone();
93+
let temp_path = temp_checkpoint_path(&format!("seq_phase_{}", i));
94+
checkpoint.save(&temp_path).unwrap();
95+
96+
let next_phase = Checkpoint::resume_from(&temp_path).unwrap();
97+
assert_eq!(
98+
next_phase,
99+
sequential_phases[i + 1],
100+
"Sequential phase {} ({:?}) should resume to {:?}, got {:?}",
101+
i,
102+
sequential_phases[i],
103+
sequential_phases[i + 1],
104+
next_phase
105+
);
106+
107+
let _ = fs::remove_file(&temp_path);
108+
}
109+
}
110+
111+
// ============================================================================
112+
// TEST 2: Parallel chain test
113+
// ============================================================================
114+
115+
#[test]
116+
fn test_parallel_chain_indexing_to_cwe_routing() {
117+
// Parallel chain: Indexing -> Semgrep -> CpgSlice -> LlmStaticAnalysis -> CweRouting
118+
let parallel_chain = [
119+
(ScanPhase::Indexing, ScanPhase::Semgrep),
120+
(ScanPhase::Semgrep, ScanPhase::CpgSlice),
121+
(ScanPhase::CpgSlice, ScanPhase::LlmStaticAnalysis),
122+
(ScanPhase::LlmStaticAnalysis, ScanPhase::CweRouting),
123+
];
124+
125+
for (current, expected_next) in parallel_chain {
126+
let mut checkpoint = Checkpoint::new("test", "/tmp", Utc::now());
127+
checkpoint.current_phase = current.clone();
128+
let temp_path = temp_checkpoint_path(&format!("parallel_{:?}", current));
129+
checkpoint.save(&temp_path).unwrap();
130+
131+
let next_phase = Checkpoint::resume_from(&temp_path).unwrap();
132+
assert_eq!(
133+
next_phase, expected_next,
134+
"Parallel chain: {:?} should resume to {:?}",
135+
current, expected_next
136+
);
137+
138+
let _ = fs::remove_file(&temp_path);
139+
}
140+
}
141+
142+
// ============================================================================
143+
// TEST 3: Terminal state transitions
144+
// ============================================================================
145+
146+
#[test]
147+
fn test_terminal_transitions_reporting_complete_error() {
148+
// Reporting -> Complete; Complete -> Indexing; Error -> Indexing
149+
let terminal_cases = [
150+
(ScanPhase::Reporting, ScanPhase::Complete),
151+
(ScanPhase::Complete, ScanPhase::Indexing),
152+
(ScanPhase::Error, ScanPhase::Indexing),
153+
];
154+
155+
for (current, expected_next) in terminal_cases {
156+
let mut checkpoint = Checkpoint::new("test", "/tmp", Utc::now());
157+
checkpoint.current_phase = current.clone();
158+
let temp_path = temp_checkpoint_path(&format!("terminal_{:?}", current));
159+
checkpoint.save(&temp_path).unwrap();
160+
161+
let next_phase = Checkpoint::resume_from(&temp_path).unwrap();
162+
assert_eq!(
163+
next_phase, expected_next,
164+
"Terminal: {:?} should resume to {:?}",
165+
current, expected_next
166+
);
167+
168+
let _ = fs::remove_file(&temp_path);
169+
}
170+
}
171+
172+
// ============================================================================
173+
// TEST 4: save then load round-trips all fields
174+
// ============================================================================
175+
176+
#[test]
177+
fn test_save_load_roundtrip_all_fields() {
178+
let mut checkpoint = Checkpoint::new("scan-roundtrip", "/tmp/project", Utc::now());
179+
checkpoint.current_phase = ScanPhase::Semgrep;
180+
checkpoint.file_count = 150;
181+
checkpoint
182+
.findings_so_far
183+
.push(make_test_finding_with_evidence());
184+
checkpoint.analyzed_files.push("src/main.rs".to_string());
185+
checkpoint.analyzed_files.push("src/lib.rs".to_string());
186+
187+
let temp_path = temp_checkpoint_path("roundtrip");
188+
checkpoint.save(&temp_path).unwrap();
189+
190+
let loaded = Checkpoint::load(&temp_path).unwrap();
191+
192+
assert_eq!(checkpoint.scan_id, loaded.scan_id);
193+
assert_eq!(checkpoint.project_path, loaded.project_path);
194+
assert_eq!(checkpoint.current_phase, loaded.current_phase);
195+
assert_eq!(checkpoint.file_count, loaded.file_count);
196+
assert_eq!(
197+
checkpoint.findings_so_far.len(),
198+
loaded.findings_so_far.len()
199+
);
200+
assert_eq!(checkpoint.analyzed_files.len(), loaded.analyzed_files.len());
201+
assert_eq!(
202+
checkpoint.analyzed_files, loaded.analyzed_files,
203+
"analyzed_files should match"
204+
);
205+
206+
let _ = fs::remove_file(&temp_path);
207+
}
208+
209+
// ============================================================================
210+
// TEST 5: validate() empty scan_id and project_path
211+
// ============================================================================
212+
213+
#[test]
214+
fn test_validate_empty_scan_id_and_project_path() {
215+
// empty scan_id -> Err
216+
let checkpoint_empty_id = Checkpoint::new("", "/tmp/project", Utc::now());
217+
let result = checkpoint_empty_id.validate();
218+
assert!(result.is_err());
219+
assert!(result.unwrap_err().contains("scan_id"));
220+
221+
// empty project_path -> Err
222+
let checkpoint_empty_path = Checkpoint::new("scan-123", "", Utc::now());
223+
let result = checkpoint_empty_path.validate();
224+
assert!(result.is_err());
225+
assert!(result.unwrap_err().contains("project_path"));
226+
227+
// valid values -> Ok
228+
let checkpoint_valid = Checkpoint::new("scan-123", "/tmp/project", Utc::now());
229+
let result = checkpoint_valid.validate();
230+
assert!(result.is_ok());
231+
}
232+
233+
// ============================================================================
234+
// TEST 6: resume_from on non-existent file returns Err (no panic)
235+
// ============================================================================
236+
237+
#[test]
238+
fn test_resume_from_nonexistent_file_returns_err() {
239+
let result = Checkpoint::resume_from("/nonexistent/path/checkpoint_12345.json");
240+
assert!(result.is_err());
241+
// Should not panic, should return a descriptive error
242+
let err = result.unwrap_err();
243+
assert!(err.contains("read") || err.contains("Failed to read"));
244+
}
245+
246+
// ============================================================================
247+
// TEST 7: resume_from on corrupt JSON file returns Err (no panic)
248+
// ============================================================================
249+
250+
#[test]
251+
fn test_resume_from_corrupt_json_returns_err() {
252+
let temp_path = temp_checkpoint_path("corrupt");
253+
fs::write(&temp_path, "this is not valid json {{{").unwrap();
254+
255+
let result = Checkpoint::resume_from(&temp_path);
256+
assert!(result.is_err());
257+
let err = result.unwrap_err();
258+
assert!(err.contains("parse") || err.contains("Failed to parse"));
259+
260+
let _ = fs::remove_file(&temp_path);
261+
}
262+
263+
// ============================================================================
264+
// TEST 8: Full flow - save checkpoint for phase X, load, resume_from returns X
265+
// ============================================================================
266+
267+
#[test]
268+
fn test_full_flow_save_load_resume() {
269+
let phase_to_test = ScanPhase::LlmDiscovery;
270+
271+
let mut checkpoint = Checkpoint::new("scan-flow", "/tmp/project", Utc::now());
272+
checkpoint.current_phase = phase_to_test.clone();
273+
274+
let temp_path = temp_checkpoint_path("full_flow");
275+
checkpoint.save(&temp_path).unwrap();
276+
277+
// Load the checkpoint
278+
let loaded = Checkpoint::load(&temp_path).unwrap();
279+
assert_eq!(loaded.current_phase, phase_to_test);
280+
281+
// resume_from should return the NEXT phase after phase_to_test
282+
let next_phase = Checkpoint::resume_from(&temp_path).unwrap();
283+
// LlmDiscovery should resume to LlmVerification
284+
assert_eq!(next_phase, ScanPhase::LlmVerification);
285+
286+
let _ = fs::remove_file(&temp_path);
287+
}
288+
289+
// ============================================================================
290+
// TEST 9: save_checkpoint writes a complete file
291+
// ============================================================================
292+
293+
#[test]
294+
fn test_save_checkpoint_writes_complete_file() {
295+
// Note: This is a simplified test since save_checkpoint is async and requires
296+
// full scanner infrastructure. We test the core checkpoint save functionality.
297+
let mut checkpoint = Checkpoint::new("async-test-scan", "/tmp/test-project", Utc::now());
298+
checkpoint.current_phase = ScanPhase::CweRouting;
299+
checkpoint.file_count = 50;
300+
checkpoint.completed_phases.push(ScanPhase::Indexing);
301+
checkpoint.completed_phases.push(ScanPhase::Semgrep);
302+
303+
let temp_path = temp_checkpoint_path("complete_file");
304+
checkpoint.save(&temp_path).unwrap();
305+
306+
// Loading it immediately after succeeds
307+
let loaded = Checkpoint::load(&temp_path).unwrap();
308+
assert_eq!(loaded.scan_id, "async-test-scan");
309+
assert_eq!(loaded.current_phase, ScanPhase::CweRouting);
310+
assert_eq!(loaded.file_count, 50);
311+
assert_eq!(loaded.completed_phases.len(), 2);
312+
313+
let _ = fs::remove_file(&temp_path);
314+
}
315+
316+
// ============================================================================
317+
// TEST 10: Checkpointed findings preserve verification_tier and evidence
318+
// ============================================================================
319+
320+
#[test]
321+
fn test_findings_preserve_verification_tier_and_evidence() {
322+
let mut checkpoint = Checkpoint::new("evidence-test", "/tmp/project", Utc::now());
323+
checkpoint.current_phase = ScanPhase::Reporting;
324+
325+
let mut finding = make_test_finding_with_evidence();
326+
finding.verification_tier = Some(VerificationTier::Verified);
327+
finding.evidence = vec![
328+
Evidence {
329+
source: EvidenceSource::Semgrep("semgrep-rule-1".to_string()),
330+
weight: 0.8,
331+
detail: "Direct evidence of vulnerability".to_string(),
332+
timestamp: Utc::now(),
333+
},
334+
Evidence {
335+
source: EvidenceSource::LlmAnalysis("llm-model-v1".to_string()),
336+
weight: 0.7,
337+
detail: "LLM confirms the vulnerability pattern".to_string(),
338+
timestamp: Utc::now(),
339+
},
340+
];
341+
342+
checkpoint.findings_so_far.push(finding);
343+
344+
let temp_path = temp_checkpoint_path("evidence_preserve");
345+
checkpoint.save(&temp_path).unwrap();
346+
347+
let loaded = Checkpoint::load(&temp_path).unwrap();
348+
assert_eq!(loaded.findings_so_far.len(), 1);
349+
350+
let loaded_finding = &loaded.findings_so_far[0];
351+
assert_eq!(
352+
loaded_finding.verification_tier,
353+
Some(VerificationTier::Verified),
354+
"verification_tier should be preserved"
355+
);
356+
assert_eq!(
357+
loaded_finding.evidence.len(),
358+
2,
359+
"evidence vector should be preserved"
360+
);
361+
assert!(
362+
matches!(
363+
loaded_finding.evidence[0].source,
364+
EvidenceSource::Semgrep(_)
365+
),
366+
"first evidence source should be Semgrep"
367+
);
368+
assert_eq!(
369+
loaded_finding.evidence[1].detail, "LLM confirms the vulnerability pattern",
370+
"second evidence detail should match"
371+
);
372+
373+
let _ = fs::remove_file(&temp_path);
374+
}

0 commit comments

Comments
 (0)