#![allow(dead_code)]
use std::path::{Path, PathBuf};
use std::sync::Arc;
use serde_json::Value;
use symforge::live_index::LiveIndex;
use symforge::protocol::SymForgeServer;
pub struct Reply {
pub text: String,
pub meta: Option<Value>,
}
pub fn fixture_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/reliability")
}
pub fn server_for(root: &Path) -> SymForgeServer {
let shared = LiveIndex::load(root).expect("load index");
SymForgeServer::new(
shared,
"reliability".to_string(),
Arc::new(parking_lot::Mutex::new(
symforge::watcher::WatcherInfo::default(),
)),
Some(root.to_path_buf()),
None,
)
}
pub fn fixture_server() -> SymForgeServer {
server_for(&fixture_root())
}
pub fn repo_server() -> SymForgeServer {
server_for(Path::new(env!("CARGO_MANIFEST_DIR")))
}
pub async fn call(server: &SymForgeServer, tool: &str, params: Value) -> String {
server.dispatch_tool_for_tests(tool, params).await
}
pub async fn call_with_meta(server: &SymForgeServer, tool: &str, params: Value) -> Reply {
let result = server
.dispatch_tool_result_for_tests(tool, params)
.await
.expect("statused dispatch");
let value = serde_json::to_value(&result).expect("serialize CallToolResult");
let text = value["content"][0]["text"]
.as_str()
.unwrap_or_default()
.to_string();
Reply {
text,
meta: value.get("_meta").cloned(),
}
}
pub fn numbered_lines(text: &str) -> Vec<u32> {
text.lines()
.filter_map(|line| {
let line = line.trim_start();
let digits: String = line.chars().take_while(char::is_ascii_digit).collect();
let rest = &line[digits.len()..];
if digits.is_empty() || !rest.starts_with([':', '|', '\t']) {
return None;
}
digits.parse().ok()
})
.collect()
}
fn copy_dir(from: &Path, to: &Path) {
std::fs::create_dir_all(to).expect("create dir");
for entry in std::fs::read_dir(from).expect("read dir") {
let entry = entry.expect("dir entry");
let target = to.join(entry.file_name());
if entry.file_type().expect("file type").is_dir() {
copy_dir(&entry.path(), &target);
} else {
std::fs::copy(entry.path(), &target).expect("copy file");
}
}
}
pub fn git(dir: &Path, args: &[&str]) -> String {
let output = symforge::process_util::hidden_command("git")
.arg("-C")
.arg(dir)
.args(["-c", "user.name=t", "-c", "user.email=t@example.invalid"])
.args(["-c", "commit.gpgsign=false", "-c", "core.autocrlf=false"])
.args(args)
.output()
.expect("spawn git");
assert!(
output.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&output.stderr)
);
String::from_utf8_lossy(&output.stdout).into_owned()
}
pub fn committed_fixture_copy() -> tempfile::TempDir {
let dir = tempfile::tempdir().expect("tempdir");
copy_dir(&fixture_root(), dir.path());
git(dir.path(), &["init", "-q", "-b", "main"]);
git(dir.path(), &["add", "-A"]);
git(dir.path(), &["commit", "-q", "-m", "baseline"]);
dir
}