use printobserver_server::{CHECK_HARNESS, operation};
use printobserver_types::serde_json::json;
use crate::agent::{StandInAgent, session_of};
use crate::binding::{held_turn, sent};
use crate::printer::RecordingPrinter;
use crate::world::{World, presenting, set};
pub const DRIVEN_THERE: [&str; 1] = [CHECK_HARNESS];
fn check_path() -> &'static str {
operation(CHECK_HARNESS).expect("the check is served").path
}
async fn outside_the_table() -> World {
World::configured(
RecordingPrinter::printing(),
StandInAgent::new(),
|document| {
set(
document,
"supervisor.harness",
toml::Value::String("copilot".to_owned()),
);
},
)
.await
}
#[tokio::test(flavor = "multi_thread")]
async fn a_port_that_runs_no_readiness_turn_is_answered_as_not_ready() {
let world = outside_the_table().await;
world.printer.forget();
let (status, answer) = world.get(&world.url(check_path())).await;
assert_eq!(status, reqwest::StatusCode::OK, "{answer}");
assert_eq!(
answer,
json!({
"ready": false,
"harness": "copilot",
"signed_in": "unknown",
"turn": "failed",
"reason": "The copilot harness could not run its check turn: this supervisor \
port runs no readiness turn.",
"next_step": "Check that the copilot harness is installed where this server's \
PATH finds it, then run `printobserver check-harness` again.",
})
);
assert!(
world.printer.calls().is_empty(),
"the check asked the machine for something: {:?}",
world.printer.calls()
);
world.server.stop().await;
}
#[tokio::test(flavor = "multi_thread")]
async fn a_turn_credential_is_refused_the_check() {
let world = outside_the_table().await;
let (print_id, pass) = held_turn(&world).await;
let turn = presenting(&pass.credential);
let (status, refused) = sent(turn.get(world.url(check_path()))).await;
let (operator, answered) = world.get(&world.url(check_path())).await;
assert_eq!(status, reqwest::StatusCode::FORBIDDEN, "{refused}");
let said = refused["error"].as_str().unwrap_or_default();
assert!(
said.contains(&session_of(print_id)) && said.contains("a check of the supervising harness"),
"the refusal names neither the turn nor what it asked for: {said}"
);
assert_eq!(operator, reqwest::StatusCode::OK, "{answered}");
assert_eq!(answered["ready"], false, "{answered}");
world.agent.release_turns();
world.server.stop().await;
}