blyte 0.90.2

The blyte coding-agent harness: headless runs with a sealed verification gate, and a REPL.
Documentation
use std::path::PathBuf;
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Mutex};

use blyte_cli::http::Request;
use blyte_cli::serve::{generate_token, route, OpenCodes, Surface, TOKEN_HEADER};
use blyte_cli::session::{EntryBody, PromptRunner, Session};

struct NoopRunner;

impl PromptRunner for NoopRunner {
    fn run(
        &mut self,
        _prompt: &str,
        _model: &str,
        _emit: &(dyn Fn(EntryBody) + Sync),
    ) -> Result<(), String> {
        Ok(())
    }
}

fn make_surface(root: PathBuf) -> Surface {
    let session = Session::start(Box::new(NoopRunner), "m1".to_string());
    let mcp = Arc::new(blyte_cli::mcp::McpManager::empty_in(&root));
    Surface {
        surface_id: "s00000000000000aa".to_string(),
        surface_name: "alpha".to_string(),
        root,
        repo: "alpha".to_string(),
        session,
        session_id: "sess-usage-route-test".to_string(),
        started_at: 1_795_990_000,
        runtime: blyte_cli::surface_version::SurfaceRuntimeInfo::current().unwrap(),
        days: 14,
        token: generate_token().unwrap(),
        message_credential: generate_token().unwrap(),
        open_codes: OpenCodes::new(),
        requests: Arc::new(blyte_cli::requests::RequestsState::new()),
        publisher: Arc::new(blyte_cli::requests::RealPublisher),
        mcp,
        reconcile_lock: Mutex::new(()),
        reconcile_requested: AtomicBool::new(false),
    }
}

fn make_request(method: &str, path: &str, token: Option<&str>) -> Request {
    let headers = match token {
        Some(t) => vec![(TOKEN_HEADER.to_string(), t.to_string())],
        None => Vec::new(),
    };
    Request {
        method: method.to_string(),
        path: path.to_string(),
        query: Vec::new(),
        headers,
        body: Vec::new(),
    }
}

#[test]
fn get_usage_serves_html_without_token_and_post_is_405() {
    let dir = tempfile::tempdir().unwrap();
    let surface = make_surface(dir.path().to_path_buf());
    let now = 1_796_000_000;

    let get_resp = route(&surface, &make_request("GET", "/usage", None), now);
    assert_eq!(get_resp.status, 200);
    assert!(
        get_resp.content_type.starts_with("text/html"),
        "expected text/html content type, got {}",
        get_resp.content_type
    );
    let body = String::from_utf8(get_resp.body).unwrap();
    assert!(
        body.contains(r#"id="usage-totals""#),
        "usage page should contain id=\"usage-totals\""
    );

    let post_resp = route(
        &surface,
        &make_request("POST", "/usage", Some(&surface.token)),
        now,
    );
    assert_eq!(post_resp.status, 405);

    let put_resp = route(&surface, &make_request("PUT", "/usage", None), now);
    assert_eq!(put_resp.status, 405);
}

#[test]
fn surface_page_links_to_usage_in_same_tab() {
    let dir = tempfile::tempdir().unwrap();
    let surface = make_surface(dir.path().to_path_buf());
    let now = 1_796_000_000;

    let resp = route(&surface, &make_request("GET", "/", None), now);
    assert_eq!(resp.status, 200);
    let html = String::from_utf8(resp.body).unwrap();

    let id_pos = html
        .find(r#"id="usage-link""#)
        .expect("surface page must contain id=\"usage-link\"");
    let tag_start = html[..id_pos]
        .rfind('<')
        .expect("id=\"usage-link\" must be inside an HTML tag");
    let tag_end = id_pos
        + html[id_pos..]
            .find('>')
            .expect("usage-link tag must be closed");
    let open_tag = &html[tag_start..=tag_end];

    assert!(
        open_tag.starts_with("<a "),
        "usage-link should be an anchor element: {open_tag}"
    );
    assert!(
        open_tag.contains(r#"href="/usage""#),
        "usage-link must have href=\"/usage\": {open_tag}"
    );
    assert!(
        !open_tag.contains("target="),
        "usage-link must not set target=: {open_tag}"
    );
}