use crate::assistant::EMBEDDED_ASSISTANT_DOCUMENT;
use super::{HarnessQueueError, harness_queues};
type TestResult = Result<(), Box<dyn std::error::Error>>;
const HARNESS_BLOCK: &str = " harness\n";
fn assistant_harness() -> Result<(usize, usize), Box<dyn std::error::Error>> {
let start = EMBEDDED_ASSISTANT_DOCUMENT
.find(HARNESS_BLOCK)
.ok_or("the embedded assistant must carry a harness section")?;
let end = EMBEDDED_ASSISTANT_DOCUMENT[start..]
.find("\n action ")
.map(|offset| start.saturating_add(offset).saturating_add(1))
.ok_or("the harness section must be followed by an action")?;
Ok((start, end))
}
#[test]
fn a_document_with_no_harness_section_declares_no_queue() -> TestResult {
let (start, end) = assistant_harness()?;
let mut stripped = String::from(&EMBEDDED_ASSISTANT_DOCUMENT[..start]);
stripped.push_str(&EMBEDDED_ASSISTANT_DOCUMENT[end..]);
assert!(
harness_queues(&stripped)?.is_empty(),
"a document with no harness section must mint nothing"
);
Ok(())
}
#[test]
fn the_shipped_assistant_declares_its_queue_and_its_launch_compiles() -> TestResult {
let queues = harness_queues(EMBEDDED_ASSISTANT_DOCUMENT)?;
let queue = queues.first().ok_or("the assistant queue must be read")?;
assert_eq!(queues.len(), 1);
assert_eq!(queue.task_queue, "assistant");
assert!(queue.harness.concurrency > 0);
Ok(())
}
#[test]
fn an_incomplete_section_refuses_and_names_its_queue() -> TestResult {
let (start, end) = assistant_harness()?;
let section = &EMBEDDED_ASSISTANT_DOCUMENT[start..end];
let concurrency = section
.lines()
.find(|line| line.trim_start().starts_with("concurrency "))
.ok_or("the shipped section must declare a concurrency")?;
let maimed = EMBEDDED_ASSISTANT_DOCUMENT.replace(&format!("{concurrency}\n"), "");
match harness_queues(&maimed) {
Err(HarnessQueueError::Harness {
task_queue,
message,
}) => {
assert_eq!(task_queue, "assistant");
assert!(message.contains("concurrency"), "{message}");
Ok(())
}
other => Err(format!("an incomplete section must refuse: {other:?}").into()),
}
}
#[test]
fn a_queue_of_only_bodied_actions_has_nothing_to_serve() -> TestResult {
let agent_action = " action assistant(prompt: String) -> Reply\n agent\n";
assert!(
EMBEDDED_ASSISTANT_DOCUMENT.contains(agent_action),
"the shipped assistant must declare its agent action in the expected shape"
);
let bodyless = EMBEDDED_ASSISTANT_DOCUMENT.replace(agent_action, "");
match harness_queues(&bodyless) {
Err(HarnessQueueError::NoServiceableAction {
task_queue,
actions,
}) => {
assert_eq!(task_queue, "assistant");
assert!(actions.contains("assistant_provision"), "{actions}");
Ok(())
}
other => Err(format!("an all-bodied queue must refuse: {other:?}").into()),
}
}
#[test]
fn unparseable_source_is_a_parse_refusal_not_an_empty_answer() -> TestResult {
match harness_queues("this is not an AWL document") {
Err(HarnessQueueError::Parse { .. }) => Ok(()),
other => Err(format!("unparseable source must refuse: {other:?}").into()),
}
}