spark-rsi
Resilient systems interface and fault-tolerant agent communication
Brought by drakestapleton
spark-rsi / tests / integration_tests.rs
| 1 | use spark_rsi::balance::BalanceKernel; |
| 2 | use spark_rsi::models::{ProposalKind, RsiConfig}; |
| 3 | use spark_rsi::observe::{calculate_soul_tension, observe_codebase}; |
| 4 | use spark_rsi::propose::ProposalGenerator; |
| 5 | use spark_rsi::verifier::InvariantVerifier; |
| 6 | use std::fs; |
| 7 | use std::path::Path; |
| 8 | |
| 9 | #[test] |
| 10 | fn test_mojo_balance_kernel_scalar() { |
| 11 | let kernel_path = "mojo/balance_bin"; |
| 12 | let verdict = BalanceKernel::evaluate(12.0, 10.0, Some(kernel_path)).expect("Evaluation should succeed"); |
| 13 | assert_eq!(verdict.verdict, "balanced"); |
| 14 | assert!(verdict.score > 0.7); |
| 15 | } |
| 16 | |
| 17 | #[test] |
| 18 | fn test_mojo_balance_kernel_simd() { |
| 19 | let kernel_path = "mojo/balance_bin"; |
| 20 | let drive_vec = [10.0, 8.0, 7.0, 9.0]; |
| 21 | let human_vec = [9.0, 8.0, 8.0, 8.0]; |
| 22 | let verdict = BalanceKernel::evaluate_simd(drive_vec, human_vec, Some(kernel_path)) |
| 23 | .expect("SIMD evaluation should succeed"); |
| 24 | assert_eq!(verdict.verdict, "balanced"); |
| 25 | assert_eq!(verdict.drive, 34.0); |
| 26 | assert_eq!(verdict.humanity, 33.0); |
| 27 | } |
| 28 | |
| 29 | #[test] |
| 30 | fn test_soul_tension_analysis() { |
| 31 | let sample = "We build, fix, finish, and solve problems with love, community, and discipline."; |
| 32 | let tension = calculate_soul_tension(sample); |
| 33 | assert!(tension.drive_score >= 4.0); |
| 34 | assert!(tension.humanity_score >= 3.0); |
| 35 | assert_eq!(tension.state, "Balanced"); |
| 36 | } |
| 37 | |
| 38 | #[test] |
| 39 | fn test_invariant_unslop_enforcement() { |
| 40 | let clean_text = "Standard engineering documentation without banned elements."; |
| 41 | let (clean, em, en, bw, tr) = InvariantVerifier::verify_unslop_text(clean_text); |
| 42 | assert!(clean); |
| 43 | assert_eq!(em, 0); |
| 44 | assert_eq!(en, 0); |
| 45 | assert!(bw.is_empty()); |
| 46 | assert!(tr.is_empty()); |
| 47 | |
| 48 | let em_dash_text = "Violation \u{2014} here."; |
| 49 | let (clean, em, _, _, _) = InvariantVerifier::verify_unslop_text(em_dash_text); |
| 50 | assert!(!clean); |
| 51 | assert_eq!(em, 1); |
| 52 | |
| 53 | let buzzword_text = "We will delve into the tapestry of software."; |
| 54 | let (clean, _, _, bw, _) = InvariantVerifier::verify_unslop_text(buzzword_text); |
| 55 | assert!(!clean); |
| 56 | assert_eq!(bw.len(), 2); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn test_invariant_secret_scanner() { |
| 61 | let temp_dir = tempfile::tempdir().expect("tempdir"); |
| 62 | let safe_file = temp_dir.path().join("config.json"); |
| 63 | fs::write(&safe_file, "{\"setting\": true}").expect("write safe file"); |
| 64 | |
| 65 | let (clean, leaks) = InvariantVerifier::scan_dir_for_secrets(temp_dir.path()); |
| 66 | assert!(clean); |
| 67 | assert!(leaks.is_empty()); |
| 68 | |
| 69 | let env_file = temp_dir.path().join(".env"); |
| 70 | fs::write(&env_file, "SECRET=compromised").expect("write env file"); |
| 71 | |
| 72 | let (clean, leaks) = InvariantVerifier::scan_dir_for_secrets(temp_dir.path()); |
| 73 | assert!(!clean); |
| 74 | assert_eq!(leaks.len(), 1); |
| 75 | } |
| 76 | |
| 77 | #[test] |
| 78 | fn test_proposal_staging_in_sandbox() { |
| 79 | let temp_repo = tempfile::tempdir().expect("temp repo"); |
| 80 | let temp_sandbox = tempfile::tempdir().expect("temp sandbox"); |
| 81 | |
| 82 | let file_path = temp_repo.path().join("test_file.txt"); |
| 83 | fs::write(&file_path, "original content").expect("write original"); |
| 84 | |
| 85 | let mut proposal = ProposalGenerator::create_proposal( |
| 86 | "update file", |
| 87 | "update content in sandbox", |
| 88 | "test_file.txt", |
| 89 | "improved content", |
| 90 | ProposalKind::Optimization, |
| 91 | ); |
| 92 | |
| 93 | let staged_path = ProposalGenerator::stage_in_sandbox(&mut proposal, temp_repo.path(), temp_sandbox.path()) |
| 94 | .expect("stage should succeed"); |
| 95 | |
| 96 | assert!(staged_path.exists()); |
| 97 | let staged_file = staged_path.join("test_file.txt"); |
| 98 | let content = fs::read_to_string(&staged_file).expect("read staged"); |
| 99 | assert_eq!(content, "improved content"); |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn test_codebase_observation_current_repo() { |
| 104 | let repo_path = Path::new("."); |
| 105 | let telemetry = observe_codebase(repo_path).expect("telemetry should succeed"); |
| 106 | assert!(!telemetry.git_branch.is_empty()); |
| 107 | assert!(telemetry.tests_passing); |
| 108 | } |
| 109 | |
| 110 | #[tokio::test] |
| 111 | async fn test_rsi_cycle_execution() { |
| 112 | let config = RsiConfig { |
| 113 | target_repo: ".".to_string(), |
| 114 | cortex_url: "http://127.0.0.1:18080".to_string(), |
| 115 | cortex_space: "atlas-memory".to_string(), |
| 116 | mojo_kernel_path: "mojo/balance_bin".to_string(), |
| 117 | loop_interval_secs: 60, |
| 118 | sandbox_root: "/tmp/spark-rsi-test-sandbox".to_string(), |
| 119 | }; |
| 120 | |
| 121 | let result = spark_rsi::daemon::RsiEngine::run_cycle(&config) |
| 122 | .await |
| 123 | .expect("cycle should execute"); |
| 124 | |
| 125 | assert!(result.elapsed_ms >= 0.0); |
| 126 | assert!(result.balance.is_some()); |
| 127 | let balance = result.balance.unwrap(); |
| 128 | assert_eq!(balance.verdict, "balanced"); |
| 129 | } |