use crate::agents::AgentConfig;
use crate::mode::HarnessMode;
use crate::prompt::{
BASE_PREAMBLE, BASE_PREAMBLE_VERSION, PromptAssembler, assemble_system_prompt,
assemble_system_prompt_for_mode,
};
fn config_with_prompt(content: &str) -> AgentConfig {
let mut cfg = AgentConfig::default();
cfg.system_prompt.content = content.to_string();
cfg
}
#[test]
fn assembled_order_is_stable() {
let cfg = config_with_prompt("AGENT_PROMPT_MARKER");
let out = assemble_system_prompt(
&cfg,
Some("PROJECT_CONTEXT_MARKER"),
Some("FALLBACK_GUIDANCE_MARKER"),
);
let base = out.find(BASE_PREAMBLE).expect("base present");
let agent = out.find("AGENT_PROMPT_MARKER").expect("agent present");
let project = out.find("PROJECT_CONTEXT_MARKER").expect("project present");
let fallback = out
.find("FALLBACK_GUIDANCE_MARKER")
.expect("fallback present");
assert!(base < agent, "BASE must precede agent prompt");
assert!(agent < project, "agent prompt must precede project context");
assert!(
project < fallback,
"project context must precede fallback guidance"
);
}
#[test]
fn base_identical_across_calls() {
let cfg = config_with_prompt("You are an engineer.");
let first = assemble_system_prompt(&cfg, Some("# Project\nrules"), None);
let second = assemble_system_prompt(&cfg, Some("# Project\nrules"), None);
assert_eq!(first, second);
}
#[test]
fn omitted_sections_produce_no_double_separator() {
let cfg = config_with_prompt("Solo agent prompt.");
let out = assemble_system_prompt(&cfg, None, None);
assert!(
!out.contains("\n\n---\n\n\n\n---\n\n"),
"must not contain a doubled separator: {out:?}"
);
assert!(
!out.ends_with("\n\n---\n\n"),
"must not end with a dangling separator: {out:?}"
);
assert_eq!(out.matches("\n\n---\n\n").count(), 1);
}
#[test]
fn empty_agent_prompt_skipped() {
let cfg = config_with_prompt("");
let out = assemble_system_prompt(&cfg, None, None);
assert!(out.starts_with(BASE_PREAMBLE), "must start with BASE");
assert_eq!(out, BASE_PREAMBLE, "BASE only — no separators added");
assert!(
!out.contains("\n\n---\n\n"),
"no separator with one section"
);
}
#[test]
fn whitespace_only_sections_are_skipped() {
let cfg = config_with_prompt(" \n ");
let out = assemble_system_prompt(&cfg, Some("\n\t \n"), Some(" "));
assert_eq!(out, BASE_PREAMBLE);
}
#[test]
fn fallback_guidance_is_last() {
let cfg = config_with_prompt("Agent prompt.");
let out = assemble_system_prompt(&cfg, Some("Project context."), Some("FALLBACK_LAST"));
assert!(
out.ends_with("FALLBACK_LAST"),
"fallback must be last: {out:?}"
);
let project = out.find("Project context.").expect("project present");
let fallback = out.find("FALLBACK_LAST").expect("fallback present");
assert!(project < fallback);
}
#[test]
fn native_tier_omits_fallback_section() {
let cfg = config_with_prompt("Agent prompt.");
let out = assemble_system_prompt(&cfg, Some("PROJECT_END"), None);
assert!(out.ends_with("PROJECT_END"));
assert_eq!(out.matches("\n\n---\n\n").count(), 2);
}
#[test]
fn base_preamble_contains_required_blocks() {
assert!(
BASE_PREAMBLE.contains("tool call"),
"tool-use protocol block missing"
);
assert!(
BASE_PREAMBLE.contains("project root"),
"filesystem-safety block missing"
);
assert!(
BASE_PREAMBLE.contains("## Summary"),
"output-convention block missing"
);
assert!(
BASE_PREAMBLE.contains("contains no tool call"),
"finish-convention block missing"
);
assert!(
BASE_PREAMBLE.contains("trusty-code harness"),
"identity/role block missing"
);
}
#[test]
fn base_preamble_requires_running_project_test_suite_before_finishing() {
assert!(
BASE_PREAMBLE.contains("run it, then report the")
|| BASE_PREAMBLE.to_lowercase().contains("run it"),
"must instruct running the project's own test suite before finishing"
);
assert!(
BASE_PREAMBLE.contains("not a substitute for the"),
"must reject self-authored tests as a substitute for the project's own suite"
);
assert!(
BASE_PREAMBLE.contains("Never claim tests passed without"),
"must forbid claiming unverified test results"
);
}
#[test]
fn base_preamble_is_model_agnostic() {
for forbidden in ["claude", "openai", "gpt", "anthropic", "qwen", "deepseek"] {
assert!(
!BASE_PREAMBLE.to_lowercase().contains(forbidden),
"BASE_PREAMBLE must not mention provider/model token {forbidden:?}"
);
}
}
#[test]
fn base_preamble_version_is_semver_shaped() {
let parts: Vec<&str> = BASE_PREAMBLE_VERSION.split('.').collect();
assert_eq!(parts.len(), 3, "expected MAJOR.MINOR.PATCH");
for part in parts {
assert!(
part.chars().all(|c| c.is_ascii_digit()),
"non-numeric version component: {part:?}"
);
}
}
const EXPECTED_PREAMBLE_HASH: u64 = 0x9376_d3b2_eb84_3e4e;
#[test]
fn base_preamble_hash_tripwire() {
const FNV_OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;
let mut hash = FNV_OFFSET_BASIS;
for &byte in BASE_PREAMBLE.as_bytes() {
hash ^= u64::from(byte);
hash = hash.wrapping_mul(FNV_PRIME);
}
hash ^= BASE_PREAMBLE.len() as u64;
hash = hash.wrapping_mul(FNV_PRIME);
assert_eq!(
hash, EXPECTED_PREAMBLE_HASH,
"BASE_PREAMBLE changed — update EXPECTED_PREAMBLE_HASH and bump \
BASE_PREAMBLE_VERSION in prompt/version.rs (actual hash: {hash:#018x})"
);
}
#[test]
fn assembler_struct_matches_free_function() {
let cfg = config_with_prompt("Agent prompt.");
let via_struct = PromptAssembler.assemble(&cfg, Some("ctx"), Some("fb"));
let via_fn = assemble_system_prompt(&cfg, Some("ctx"), Some("fb"));
assert_eq!(via_struct, via_fn);
}
#[test]
fn all_sections_present_when_supplied() {
let cfg = config_with_prompt("AGENT");
let out = assemble_system_prompt(&cfg, Some("PROJECT"), Some("FALLBACK"));
assert!(out.contains(BASE_PREAMBLE));
assert!(out.contains("AGENT"));
assert!(out.contains("PROJECT"));
assert!(out.contains("FALLBACK"));
}
#[test]
fn assemble_system_prompt_for_mode_identical_when_no_skills() {
let cfg = config_with_prompt("AGENT");
let baseline = assemble_system_prompt(&cfg, Some("PROJECT"), Some("FALLBACK"));
for mode in [HarnessMode::DailyDriver, HarnessMode::Parity] {
let via_mode =
assemble_system_prompt_for_mode(mode, &cfg, Some("PROJECT"), Some("FALLBACK"), None);
assert_eq!(
via_mode, baseline,
"mode {mode:?} must match baseline when there is no skills catalog"
);
}
}
#[test]
fn daily_driver_appends_skills_catalog() {
let cfg = config_with_prompt("AGENT");
let catalog = "Available skills:\n- demo-skill: Does demo things";
let out = assemble_system_prompt_for_mode(
HarnessMode::DailyDriver,
&cfg,
Some("PROJECT"),
Some("FALLBACK"),
Some(catalog),
);
assert!(out.contains("AGENT"));
assert!(out.contains(catalog));
assert!(
out.find("FALLBACK").unwrap() < out.find(catalog).unwrap(),
"the skills catalog must be the trailing section"
);
}
#[test]
fn parity_ignores_skills_catalog() {
let cfg = config_with_prompt("AGENT");
let baseline = assemble_system_prompt(&cfg, Some("PROJECT"), Some("FALLBACK"));
let catalog = "Available skills:\n- demo-skill: Does demo things";
let out = assemble_system_prompt_for_mode(
HarnessMode::Parity,
&cfg,
Some("PROJECT"),
Some("FALLBACK"),
Some(catalog),
);
assert_eq!(out, baseline);
assert!(!out.contains("demo-skill"));
}