use crate::support;
use std::io::Write;
use std::path::Path;
use std::process::{Output, Stdio};
use trusty_agents_common::agents::manifest::MANIFEST_FILE;
fn tcode_without_content(home: &Path, project: &Path, args: &[&str]) -> support::TcodeCommand {
let mut cmd = support::tcode_command();
cmd.args(args)
.current_dir(project)
.env("HOME", home)
.env_remove("RUST_LOG");
cmd
}
fn serve_without_content(home: &Path, project: &Path, ids: &[u32]) -> Output {
let project_arg = project.display().to_string();
let mut cmd = tcode_without_content(
home,
project,
&["serve", "--project", &project_arg, "--stdio"],
);
let mut child = cmd
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("spawn tcode serve --stdio");
{
let mut stdin = child.stdin.take().expect("stdin");
for id in ids {
writeln!(
stdin,
r#"{{"jsonrpc":"2.0","id":{id},"method":"agents.list","params":{{}}}}"#
)
.expect("write request");
}
}
child.wait_with_output().expect("tcode exits on stdin EOF")
}
#[test]
fn no_content_logs_one_error_naming_tm_content_install() {
let home = tempfile::tempdir().expect("scratch home");
let project = tempfile::tempdir().expect("scratch project");
let out = serve_without_content(home.path(), project.path(), &[1, 2]);
let stdout = String::from_utf8_lossy(&out.stdout);
let stderr = String::from_utf8_lossy(&out.stderr);
for id in [1, 2] {
assert!(
stdout.contains(&format!(r#""id":{id},"result""#)),
"agents.list {id} must answer from the built-in agents\nstdout: {stdout}\nstderr: {stderr}"
);
}
let remedy: Vec<&str> = stderr
.lines()
.filter(|l| l.contains("tm content install"))
.collect();
assert_eq!(
remedy.len(),
1,
"exactly one line must name the remedy\nstderr: {stderr}"
);
assert!(remedy[0].contains("ERROR"), "{}", remedy[0]);
assert!(remedy[0].contains("tm content update"), "{}", remedy[0]);
assert!(
!stdout.contains("tm content install"),
"the remedy is a log line, not protocol output\nstdout: {stdout}"
);
}
#[test]
fn no_content_writes_no_agents_dir_and_no_lock() {
let home = tempfile::tempdir().expect("scratch home");
let project = tempfile::tempdir().expect("scratch project");
let agents = project.path().join(".trusty-code").join("agents");
let lock = agents.join(format!("{MANIFEST_FILE}.lock"));
let served = serve_without_content(home.path(), project.path(), &[1]);
assert!(
String::from_utf8_lossy(&served.stdout).contains(r#""id":1,"result""#),
"serve must answer agents.list\nstderr: {}",
String::from_utf8_lossy(&served.stderr)
);
assert!(!agents.exists(), "serve left {} behind", agents.display());
assert!(!lock.exists(), "serve left {} behind", lock.display());
let project_arg = project.path().display().to_string();
let ran = tcode_without_content(
home.path(),
project.path(),
&[
"run-task",
"engineer",
"say hi",
"--project",
&project_arg,
"--json",
],
)
.env("TCODE_MOCK_LLM", "echo")
.output()
.expect("spawn tcode run-task");
assert!(
ran.status.success(),
"run-task with the built-in engineer must still run\nstderr: {}",
String::from_utf8_lossy(&ran.stderr)
);
assert!(
!agents.exists(),
"run-task left {} behind",
agents.display()
);
assert!(!lock.exists(), "run-task left {} behind", lock.display());
}