printobserver-server 0.20.2

The printobserver server.
Documentation
//! A check of the supervising agent's harness, against a server over the
//! stand-in agent port.
//!
//! What the check finds of a real harness — its status command, its turn, the
//! sign-in directory each is run with — is driven through the command-line
//! program against a stand-in harness program, in that crate's
//! `checking_harness.rs`. This holds what is the server's own: the answer is
//! made of what the port answered, the check commands nothing, and it is the
//! operator's alone.

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};

/// The operations this file drives.
pub const DRIVEN_THERE: [&str; 1] = [CHECK_HARNESS];

/// The check's own route, beneath the versioned prefix.
fn check_path() -> &'static str {
    operation(CHECK_HARNESS).expect("the check is served").path
}

/// A server over the stand-in agent port whose configured harness is outside
/// the adapter's table: no status command is run, so nothing on this host's
/// own path is ever started by a check.
async fn outside_the_table() -> World {
    World::configured(
        RecordingPrinter::printing(),
        StandInAgent::new(),
        |document| {
            set(
                document,
                "supervisor.harness",
                toml::Value::String("copilot".to_owned()),
            );
        },
    )
    .await
}

/// A harness with no status command this program knows, behind a port that
/// runs no readiness turn of its own, is answered as not ready, saying why in
/// the port's own words — and the check commands the machine nothing.
#[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;
}

/// A supervision turn's credential is refused the check, naming its session,
/// before anything is checked: a turn asking whether a turn can run would be a
/// second turn started from inside the first. The operator's is answered.
#[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;
}