#![cfg(feature = "server")]
#[path = "support/stel_surface_env.rs"]
mod stel_surface_env;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::process::Command;
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use parking_lot::Mutex;
use symforge::live_index::LiveIndex;
use symforge::live_index::git_temporal::{
CoChangeEntry, CommitSummary, GitFileHistory, GitTemporalIndex, GitTemporalState,
GitTemporalStats,
};
use symforge::protocol::SymForgeServer;
use symforge::stel::{self, IntentBucket, StelRequest};
use symforge::watcher::WatcherInfo;
use tempfile::TempDir;
fn library_with_one_dependent() -> Vec<(&'static str, &'static str)> {
vec![
("src/lib.rs", "pub mod widget;\npub mod consumer;\n"),
("src/widget.rs", "pub fn render() -> u32 {\n 1\n}\n"),
(
"src/consumer.rs",
"use crate::widget;\n\npub fn run() -> u32 {\n widget::render()\n}\n",
),
]
}
fn git(args: &[&str], root: &Path) {
let output = Command::new("git")
.args(args)
.current_dir(root)
.output()
.unwrap_or_else(|e| panic!("git {args:?} failed to spawn: {e}"));
assert!(
output.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}
fn server_over_repo_with_uncommitted_widget_change(
seed_cochange: bool,
) -> (TempDir, SymForgeServer) {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path().to_path_buf();
for (rel, content) in library_with_one_dependent() {
let path = root.join(rel);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).expect("create parent dir");
}
fs::write(&path, content).expect("write fixture file");
}
git(&["init"], &root);
git(&["config", "user.email", "test@test.com"], &root);
git(&["config", "user.name", "Test"], &root);
git(&["add", "."], &root);
git(&["commit", "-m", "initial"], &root);
git(&["branch", "-M", "main"], &root);
fs::write(
root.join("src/widget.rs"),
"pub fn render() -> u32 {\n 2\n}\n",
)
.expect("modify widget.rs");
let shared = LiveIndex::load(&root).expect("LiveIndex::load");
if seed_cochange {
let history = GitFileHistory {
commit_count: 7,
churn_score: 0.9,
last_commit: CommitSummary {
hash: "abc1234".to_string(),
timestamp: "2026-06-01T12:00:00Z".to_string(),
author: "Tester".to_string(),
message_head: "touch widget".to_string(),
days_ago: 2.0,
},
contributors: vec![],
co_changes: vec![CoChangeEntry {
path: "src/consumer.rs".to_string(),
coupling_score: 0.71,
shared_commits: 5,
}],
weak_co_changes: vec![],
};
shared.update_git_temporal(GitTemporalIndex {
files: HashMap::from([("src/widget.rs".to_string(), history)]),
stats: GitTemporalStats {
total_commits_analyzed: 14,
analysis_window_days: 90,
hotspots: vec![],
most_coupled: vec![],
computed_at: SystemTime::now(),
compute_duration: Duration::ZERO,
},
state: GitTemporalState::Ready,
});
}
let watcher_info = Arc::new(Mutex::new(WatcherInfo::default()));
let server = SymForgeServer::new(
shared,
"impact_intent_test".to_string(),
watcher_info,
Some(root),
None,
);
(dir, server)
}
fn tool_result_text(result: &serde_json::Value) -> String {
result["content"][0]["text"]
.as_str()
.expect("symforge result must contain text content")
.to_string()
}
async fn run_impact_intent(server: &SymForgeServer, path: &str) -> String {
let request = StelRequest {
query: format!("what depends on {path}"),
intent: Some(IntentBucket::Impact),
path: Some(path.to_string()),
symbol: None,
max_tokens: None,
preview: None,
project: None,
projects: None,
};
let params = serde_json::to_value(stel::SymforgeCallInput {
request,
probe_legacy_tool: None,
probe_legacy_args: None,
})
.expect("symforge params serialize");
let result = server
.dispatch_tool_result_for_tests("symforge", params)
.await
.expect("symforge dispatch");
let serialized = serde_json::to_value(&result).expect("serialize CallToolResult");
tool_result_text(&serialized)
}
#[tokio::test]
async fn impact_intent_routes_to_detect_impact_blast_radius() {
let _guard = stel_surface_env::COMPACT_ENV_LOCK.lock().await;
let _surface = stel_surface_env::set_symforge_surface("compact");
let (_dir, server) = server_over_repo_with_uncommitted_widget_change(false);
let body = run_impact_intent(&server, "src/widget.rs").await;
assert!(
body.contains("Chosen tool: detect_impact"),
"impact intent (path only) must route to detect_impact:\n{body}"
);
assert!(
body.contains("\"src/widget.rs\""),
"detect_impact must report the actual uncommitted change:\n{body}"
);
assert!(
body.contains("\"symbol\": \"src/consumer.rs\""),
"blast radius must include widget.rs's caller file (src/consumer.rs):\n{body}"
);
}
#[tokio::test]
async fn impact_intent_discloses_unconsumed_path() {
let _guard = stel_surface_env::COMPACT_ENV_LOCK.lock().await;
let _surface = stel_surface_env::set_symforge_surface("compact");
let (_dir, server) = server_over_repo_with_uncommitted_widget_change(false);
let body = run_impact_intent(&server, "src/widget.rs").await;
assert!(
body.contains("Chosen tool: detect_impact"),
"impact intent (path only) must route to detect_impact:\n{body}"
);
assert!(
body.contains("`path` \"src/widget.rs\" not consumed"),
"impact intent must disclose the caller's path was not consumed:\n{body}"
);
assert!(
body.contains("derives its changed-file set from git"),
"disclosure must name WHY the path was dropped:\n{body}"
);
}
#[tokio::test]
async fn impact_intent_no_longer_chains_cochanges() {
let _guard = stel_surface_env::COMPACT_ENV_LOCK.lock().await;
let _surface = stel_surface_env::set_symforge_surface("compact");
let (_dir, server) = server_over_repo_with_uncommitted_widget_change(true);
let body = run_impact_intent(&server, "src/widget.rs").await;
assert!(
body.contains("Chosen tool: detect_impact"),
"impact intent (path only) must route to detect_impact:\n{body}"
);
assert!(
!body.contains("Co-changing files") && !body.contains("Git temporal data for"),
"detect_impact does not chain co-change data, even when a Ready snapshot exists:\n{body}"
);
}