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_lines_without_content(home: &Path, project: &Path, lines: &[String]) -> 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 line in lines {
writeln!(stdin, "{line}").expect("write request");
}
}
child.wait_with_output().expect("tcode exits on stdin EOF")
}
fn serve_without_content(home: &Path, project: &Path, ids: &[u32]) -> Output {
let lines: Vec<String> = ids
.iter()
.map(|id| format!(r#"{{"jsonrpc":"2.0","id":{id},"method":"agents.list","params":{{}}}}"#))
.collect();
serve_lines_without_content(home, project, &lines)
}
#[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 be answered\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!(
!agents.exists(),
"run-task left {} behind",
agents.display()
);
assert!(!lock.exists(), "run-task left {} behind", lock.display());
}
#[test]
fn no_content_run_task_fails_closed_naming_the_remedy() {
let home = tempfile::tempdir().expect("scratch home");
let project = tempfile::tempdir().expect("scratch project");
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");
let output = format!(
"{}{}",
String::from_utf8_lossy(&ran.stdout),
String::from_utf8_lossy(&ran.stderr)
);
assert!(
!ran.status.success(),
"run-task must refuse with no content installed\n{output}"
);
for needle in ["engineer", "tm content update", "tm content install --from"] {
assert!(
output.contains(needle),
"output must name `{needle}`\n{output}"
);
}
}
#[test]
fn no_content_bundled_skill_name_guard_still_answers_403() {
let home = tempfile::tempdir().expect("scratch home");
let project = tempfile::tempdir().expect("scratch project");
let name = trusty_code::assets::DEFAULT_SKILL_NAMES[0];
let lines = vec![
format!(
r#"{{"jsonrpc":"2.0","id":1,"method":"skills.create","params":{{"name":"{name}","content":"x"}}}}"#
),
format!(
r#"{{"jsonrpc":"2.0","id":2,"method":"skills.delete","params":{{"name":"{name}"}}}}"#
),
];
let out = serve_lines_without_content(home.path(), project.path(), &lines);
let stdout = String::from_utf8_lossy(&out.stdout);
for id in [1, 2] {
let reply = stdout
.lines()
.find(|l| l.contains(&format!(r#""id":{id},"#)))
.unwrap_or_else(|| panic!("no reply for {id}\nstdout: {stdout}"));
assert!(
reply.contains(r#""code":-32001"#),
"request {id} must be refused with 403\n{reply}"
);
}
assert!(
!project
.path()
.join(".trusty-code")
.join("skills")
.join(name)
.exists(),
"skills.create wrote a bundled-name skill with no content installed"
);
}