Luigit
repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by admin

smith-harness/tests/e2e_oauth_login.rs

Raw
//! Real binary OAuth acceptance (`SMH-SPEC-SPEC0001`, `SMH-SPEC-PLUG0001`).
//!
//! The e2e tier builds smith-cli before running this test.

#[path = "../src/oauth_crypto.rs"]
mod crypto;

use serde_json::{Value, json};
use std::io::{BufRead, BufReader, Read, Write};
use std::net::{TcpListener, TcpStream};
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use std::sync::mpsc;
use std::time::Duration;

const DEADLINE: Duration = Duration::from_secs(15);
const ACCESS: &str = r#"fixture-access-\"sentinel"#;
const REFRESH: &str = r#"fixture-refresh-\"sentinel"#;
const ROTATED_ACCESS: &str = r#"fixture-rotated-\"access"#;
const ROTATED_REFRESH: &str = r#"fixture-rotated-\"refresh"#;

fn binary() -> PathBuf {
    std::env::current_exe()
        .unwrap()
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .join(format!("smith{}", std::env::consts::EXE_SUFFIX))
}

fn command(home: &std::path::Path) -> Command {
    let mut command = Command::new(binary());
    command
        .env_clear()
        .env("HOME", home)
        .env("XDG_DATA_HOME", home.join("data"))
        .env("XDG_CONFIG_HOME", home.join("config"))
        .env("XDG_STATE_HOME", home.join("state"))
        .env("XDG_RUNTIME_DIR", home);
    command
}

fn file_limited_command(home: &std::path::Path, blocks: u64) -> Command {
    let mut command = Command::new("bash");
    command
        .arg("-c")
        .arg("trap '' XFSZ; ulimit -f \"$1\"; shift; exec \"$@\"")
        .arg("bash")
        .arg(blocks.to_string())
        .arg(binary())
        .env_clear()
        .env("HOME", home)
        .env("XDG_DATA_HOME", home.join("data"))
        .env("XDG_CONFIG_HOME", home.join("config"))
        .env("XDG_STATE_HOME", home.join("state"))
        .env("XDG_RUNTIME_DIR", home);
    command
}

fn external_credential_module(home: &std::path::Path) -> String {
    let package = home.join("external-oauth-module");
    std::fs::create_dir(&package).unwrap();
    std::fs::write(
        package.join("plugin.kdl"),
        "id \"fixture:external-oauth\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"oauth.wasm\"\ncapability \"secret-proxy\"\n",
    )
    .unwrap();
    let artifact = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("../target/wasm32-wasip2/release/smith_plugin_oauth.wasm");
    std::fs::copy(artifact, package.join("oauth.wasm")).unwrap();
    let linked = offline_output(command(home).args([
        "plugin",
        "link",
        package.to_str().unwrap(),
        "--grant",
        "secret-proxy",
    ]));
    assert!(
        linked.status.success(),
        "{}",
        String::from_utf8_lossy(&linked.stderr)
    );
    "fixture:external-oauth".into()
}

#[track_caller]
fn offline_output(command: &mut Command) -> std::process::Output {
    let invocation = format!("{command:?}");
    offline_child_output(
        ChildGuard(
            command
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .spawn()
                .unwrap(),
        ),
        &invocation,
    )
}

fn decode_form(text: &str) -> Vec<(String, String)> {
    let decode = |text: &str| {
        let mut bytes = Vec::new();
        let mut input = text.bytes();
        while let Some(byte) = input.next() {
            bytes.push(match byte {
                b'+' => b' ',
                b'%' => {
                    let high = char::from(input.next().unwrap()).to_digit(16).unwrap();
                    let low = char::from(input.next().unwrap()).to_digit(16).unwrap();
                    u8::try_from(high * 16 + low).unwrap()
                }
                _ => byte,
            });
        }
        String::from_utf8(bytes).unwrap()
    };
    text.split('&')
        .map(|item| {
            let (key, value) = item.split_once('=').unwrap();
            (decode(key), decode(value))
        })
        .collect()
}

fn field<'a>(values: &'a [(String, String)], name: &str) -> &'a str {
    &values.iter().find(|(key, _)| key == name).unwrap().1
}

fn request(stream: &mut TcpStream) -> Result<(String, String, Option<String>), String> {
    let request = request_with_headers(stream)?;
    Ok((request.route, request.body, request.authorization))
}

struct FixtureRequest {
    method: String,
    route: String,
    body: String,
    authorization: Option<String>,
    headers: Vec<(String, String)>,
}

fn request_with_headers(stream: &mut TcpStream) -> Result<FixtureRequest, String> {
    stream.set_read_timeout(Some(DEADLINE)).unwrap();
    stream.set_write_timeout(Some(DEADLINE)).unwrap();
    let mut reader = BufReader::new(stream);
    let mut line = String::new();
    reader
        .by_ref()
        .take(8193)
        .read_line(&mut line)
        .map_err(|_| "fixture request read")?;
    if line.is_empty() || line.len() > 8192 {
        return Err("fixture request closed or oversized".into());
    }
    let method = line
        .split_whitespace()
        .next()
        .ok_or("fixture request method")?
        .to_string();
    let route = line
        .split_whitespace()
        .nth(1)
        .ok_or("fixture request route")?
        .to_string();
    let mut length = 0;
    let mut authorization = None;
    let mut headers = Vec::new();
    let mut total = line.len();
    loop {
        line.clear();
        reader
            .by_ref()
            .take(8193)
            .read_line(&mut line)
            .map_err(|_| "fixture header read")?;
        total += line.len();
        if total > 8192 || line.is_empty() {
            return Err("fixture header bound".into());
        }
        if line == "\r\n" {
            break;
        }
        if let Some((name, value)) = line.split_once(':') {
            headers.push((name.to_ascii_lowercase(), value.trim().to_string()));
        }
        if let Some((name, value)) = line.split_once(':')
            && name.eq_ignore_ascii_case("authorization")
        {
            authorization = Some(value.trim().to_string());
        }
        if let Some((name, value)) = line.split_once(':')
            && name.eq_ignore_ascii_case("content-length")
        {
            length = value
                .trim()
                .parse::<usize>()
                .map_err(|_| "fixture content length")?;
        }
    }
    if length > 8192 {
        return Err("fixture body bound".into());
    }
    let mut body = vec![0; length];
    reader
        .read_exact(&mut body)
        .map_err(|_| "fixture body read")?;
    Ok(FixtureRequest {
        method,
        route,
        body: String::from_utf8(body).map_err(|_| "fixture body UTF-8")?,
        authorization,
        headers,
    })
}

fn respond(stream: &mut TcpStream, value: Value) {
    let bytes = serde_json::to_vec(&value).unwrap();
    write!(stream, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", bytes.len()).unwrap();
    stream.write_all(&bytes).unwrap();
}

struct ChildGuard(Child);
impl Drop for ChildGuard {
    fn drop(&mut self) {
        let _ = self.0.kill();
        let _ = self.0.wait();
    }
}

struct ServerGuard {
    address: std::net::SocketAddr,
    worker: Option<std::thread::JoinHandle<()>>,
}
impl Drop for ServerGuard {
    fn drop(&mut self) {
        // Wake a failed fixture's blocked accept; an empty request terminates the server.
        let _ = TcpStream::connect_timeout(&self.address, DEADLINE);
        if let Some(worker) = self.worker.take() {
            let _joined = worker.join();
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum LoginCase {
    Authorized,
    Expired,
    ReturningIdentityRejected,
    EntitlementRejected,
    NonceRejected,
    ScopeRejected,
    IssuerRejected,
    AudienceRejected,
    SignatureRejected,
    StateRejected,
    CallbackClientRejected,
}

impl LoginCase {
    fn initial_authorized(self) -> bool {
        matches!(
            self,
            Self::Authorized
                | Self::Expired
                | Self::ReturningIdentityRejected
                | Self::EntitlementRejected
        )
    }
}

#[test]
fn binary_login_persists_only_verified_profile_without_credentials_in_output_or_journal() {
    for case in [
        LoginCase::Authorized,
        LoginCase::Expired,
        LoginCase::ReturningIdentityRejected,
        LoginCase::EntitlementRejected,
        LoginCase::NonceRejected,
        LoginCase::ScopeRejected,
        LoginCase::IssuerRejected,
        LoginCase::AudienceRejected,
        LoginCase::SignatureRejected,
        LoginCase::StateRejected,
        LoginCase::CallbackClientRejected,
    ] {
        login_case(case);
    }
}

fn serve_login(
    listener: &TcpListener,
    issuer: &str,
    authorization_receive: &mpsc::Receiver<Vec<(String, String)>>,
    case: LoginCase,
) -> Result<(String, Vec<(String, String)>), String> {
    let (mut stream, _) = listener.accept().map_err(|_| "fixture discovery accept")?;
    let (route, _, auth) = request(&mut stream)?;
    assert!(auth.is_none());
    if route != "/.well-known/openid-configuration" {
        return Err("fixture discovery route".into());
    }
    respond(
        &mut stream,
        json!({"issuer":issuer,"authorization_endpoint":format!("{issuer}/authorize"),"token_endpoint":format!("{issuer}/token"),"jwks_uri":format!("{issuer}/keys")}),
    );
    let authorization = authorization_receive
        .recv_timeout(DEADLINE)
        .map_err(|_| "fixture authorization deadline")?;
    if matches!(
        case,
        LoginCase::StateRejected | LoginCase::CallbackClientRejected
    ) {
        return Ok(("fixture-no-token-issued".into(), authorization));
    }
    let (mut stream, _) = listener.accept().map_err(|_| "fixture token accept")?;
    let (route, body, auth) = request(&mut stream)?;
    assert!(auth.is_none());
    if route != "/token" {
        return Err("fixture token route".into());
    }
    let body = decode_form(&body);
    assert_eq!(field(&body, "grant_type"), "authorization_code");
    assert_eq!(field(&body, "client_id"), "oaiapp_fixture");
    assert_eq!(field(&body, "code"), "fixture-code");
    assert_eq!(
        field(&body, "redirect_uri"),
        field(&authorization, "redirect_uri")
    );
    assert_eq!(field(&body, "resource"), format!("{issuer}/v1"));
    assert_eq!(
        crypto::challenge(field(&body, "code_verifier")),
        field(&authorization, "code_challenge")
    );
    let nonce = if case == LoginCase::NonceRejected {
        "foreign-attempt"
    } else {
        field(&authorization, "nonce")
    };
    let scopes = if case == LoginCase::ScopeRejected {
        "openid profile email"
    } else {
        field(&authorization, "scope")
    };
    let mut claims = json!({"iss":issuer,"sub":"fixture-subject","aud":"oaiapp_fixture","nonce":nonce,"exp":4102444800_u64,"email":"fixture@example.test","workspace_id":"fixture-workspace"});
    if case == LoginCase::ReturningIdentityRejected {
        claims["sub"] = json!("foreign-subject");
    }
    if case == LoginCase::IssuerRejected {
        claims["iss"] = json!("https://foreign.example");
    }
    if case == LoginCase::AudienceRejected {
        claims["aud"] = json!("oaiapp_foreign");
    }
    let (mut identity, keys) = crypto::tests::signed(&claims);
    if case == LoginCase::SignatureRejected {
        let (claims, signature) = identity.rsplit_once('.').unwrap();
        identity = format!(
            "{claims}.{}{}",
            if signature.starts_with('A') { 'B' } else { 'A' },
            &signature[1..]
        );
    }
    respond(
        &mut stream,
        json!({"access_token":ACCESS,"refresh_token":REFRESH,"id_token":identity,"token_type":"Bearer","expires_in":if case == LoginCase::Expired { 1 } else { 3600 },"scope":scopes}),
    );
    let (mut stream, _) = listener.accept().map_err(|_| "fixture JWKS accept")?;
    let (route, _, auth) = request(&mut stream)?;
    assert!(auth.is_none());
    if route != "/keys" {
        return Err("fixture JWKS route".into());
    }
    respond(&mut stream, keys);
    Ok((identity, authorization))
}

fn browser_login(
    home: &std::path::Path,
    origin: &str,
    profile: Option<&str>,
    authorization_send: &mpsc::SyncSender<Vec<(String, String)>>,
    case: LoginCase,
) -> (std::process::Output, String) {
    browser_login_for(
        "openai-chatgpt-plan",
        home,
        origin,
        profile,
        authorization_send,
        case,
    )
}

fn browser_login_for(
    provider: &str,
    home: &std::path::Path,
    origin: &str,
    profile: Option<&str>,
    authorization_send: &mpsc::SyncSender<Vec<(String, String)>>,
    case: LoginCase,
) -> (std::process::Output, String) {
    let mut login_command = command(home);
    login_command.args(["login", "--provider", provider, "--fixture-issuer", origin]);
    if let Some(profile) = profile {
        login_command.args(["--profile", profile]);
    }
    let mut child = ChildGuard(
        login_command
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .unwrap(),
    );
    let stdout = child.0.stdout.take().unwrap();
    let stderr = child.0.stderr.take().unwrap();
    let (line_send, line_receive) = mpsc::sync_channel(1);
    let (tail_send, tail_receive) = mpsc::sync_channel(1);
    let (error_send, error_receive) = mpsc::sync_channel(1);
    let output_reader = std::thread::spawn(move || {
        let mut reader = BufReader::new(stdout);
        let mut line = String::new();
        reader.by_ref().take(8193).read_line(&mut line).unwrap();
        let _ = line_send.send(line);
        let mut tail = Vec::new();
        reader.take(65537).read_to_end(&mut tail).unwrap();
        let _ = tail_send.send(tail);
    });
    let error_reader = std::thread::spawn(move || {
        let mut errors = Vec::new();
        stderr.take(65537).read_to_end(&mut errors).unwrap();
        let _ = error_send.send(errors);
    });
    let line = line_receive.recv_timeout(DEADLINE).unwrap();
    let url = line
        .trim_end()
        .strip_prefix(if provider == "anthropic-claude-plan" {
            "Continue with Claude: "
        } else if provider == "google-gemini-cli-plan" {
            "Continue with Google: "
        } else {
            "Continue with ChatGPT: "
        })
        .unwrap();
    let (endpoint, query) = url.split_once('?').unwrap();
    assert_eq!(endpoint, format!("{origin}/authorize"));
    let authorization = decode_form(query);
    assert_eq!(
        field(&authorization, "client_id"),
        if provider == "anthropic-claude-plan" {
            "9d1c250a-e61b-44d9-88ed-5944d1962f5e"
        } else if provider == "google-gemini-cli-plan" {
            "681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com"
        } else if profile.is_some() {
            "oaiapp_fixture"
        } else {
            "dynamic_agent_client"
        }
    );
    if profile.is_some()
        || provider == "anthropic-claude-plan"
        || provider == "google-gemini-cli-plan"
    {
        assert!(
            !authorization
                .iter()
                .any(|(key, _)| key == "agent_name_hint")
        );
    } else {
        assert_eq!(field(&authorization, "agent_name_hint"), "Smith");
    }
    let host_id =
        if provider != "openai-chatgpt-plan" {
            assert!(!authorization.iter().any(|(name, _)| matches!(
                name.as_str(),
                "nonce" | "resource" | "ext_agent_host_id"
            )));
            String::new()
        } else {
            let host_id = field(&authorization, "ext_agent_host_id").to_string();
            assert!(host_id.starts_with("urn:uuid:"));
            assert_eq!(host_id.len(), 45);
            host_id
        };
    assert_eq!(field(&authorization, "code_challenge_method"), "S256");
    let redirect = field(&authorization, "redirect_uri").to_string();
    let state = if case == LoginCase::StateRejected {
        "foreign-state".into()
    } else {
        field(&authorization, "state").to_string()
    };
    authorization_send.send(authorization.clone()).unwrap();
    let (authority, path) = redirect
        .strip_prefix("http://")
        .unwrap()
        .split_once('/')
        .unwrap();
    if provider == "anthropic-claude-plan" {
        assert!(authority.starts_with("localhost:"));
        assert_eq!(path, "callback");
        assert_eq!(
            field(&authorization, "scope"),
            "org:create_api_key user:profile user:inference user:sessions:claude_code user:mcp_servers user:file_upload"
        );
    }
    let mut callback = TcpStream::connect_timeout(
        &authority.replace("localhost", "127.0.0.1").parse().unwrap(),
        DEADLINE,
    )
    .unwrap();
    callback.set_read_timeout(Some(DEADLINE)).unwrap();
    callback.set_write_timeout(Some(DEADLINE)).unwrap();
    let client = if case == LoginCase::CallbackClientRejected {
        "&client_id=dynamic_agent_client"
    } else if profile.is_some()
        || provider == "anthropic-claude-plan"
        || provider == "google-gemini-cli-plan"
    {
        ""
    } else {
        "&client_id=oaiapp_fixture"
    };
    write!(
        callback,
        "GET /{path}?state={state}&code=fixture-code{client} HTTP/1.1\r\nHost: {authority}\r\n\r\n"
    )
    .unwrap();
    let mut callback_response = Vec::new();
    callback
        .take(8193)
        .read_to_end(&mut callback_response)
        .unwrap();
    let tail = tail_receive.recv_timeout(DEADLINE).unwrap();
    let errors = error_receive.recv_timeout(DEADLINE).unwrap();
    assert!(tail.len() <= 65536 && errors.len() <= 65536);
    // Smith retains its standard streams until exit; EOF is its exit-readiness signal.
    let status = child.0.wait().unwrap();
    output_reader.join().unwrap();
    error_reader.join().unwrap();
    let mut stdout = line.into_bytes();
    stdout.extend_from_slice(&tail);
    (
        std::process::Output {
            status,
            stdout,
            stderr: errors,
        },
        host_id,
    )
}

#[test]
fn binary_refresh_publication_failure_retains_pending_generation_and_refuses_replay() {
    let identity = smith::id::SessionId::new().to_string();
    let digest = smith::sha256::digest_hex(identity.as_bytes());
    let home = std::env::temp_dir().join(format!("opf-{}", &digest[..12]));
    std::fs::create_dir(&home).unwrap();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&home, std::fs::Permissions::from_mode(0o700)).unwrap();
    }
    let listener = TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0)).unwrap();
    let address = listener.local_addr().unwrap();
    let origin = format!("http://{address}");
    let (authorization_send, authorization_receive) = mpsc::sync_channel(1);
    let (posted_send, posted_receive) = mpsc::sync_channel(1);
    let (release_send, release_receive) = mpsc::sync_channel(1);
    let (finished_send, finished_receive) = mpsc::sync_channel(1);
    let padded_access = format!(r#"fixture-rotated-\"access{}"#, "a".repeat(32 * 1024));
    let padded_refresh = format!(r#"fixture-rotated-\"refresh{}"#, "b".repeat(32 * 1024));
    let issuer = origin.clone();
    let server_listener = listener.try_clone().unwrap();
    let server_access = padded_access.clone();
    let server_refresh = padded_refresh.clone();
    let server = std::thread::spawn(move || {
        let result = (|| -> Result<(), String> {
            let (_, authorization) = serve_login(
                &server_listener,
                &issuer,
                &authorization_receive,
                LoginCase::Authorized,
            )?;
            let (mut stream, _) = server_listener
                .accept()
                .map_err(|_| "fixture refresh discovery accept")?;
            let (route, _, authorization_header) = request(&mut stream)?;
            assert_eq!(route, "/.well-known/openid-configuration");
            assert!(authorization_header.is_none());
            respond(
                &mut stream,
                json!({"issuer":issuer,"jwks_uri":format!("{issuer}/keys")}),
            );
            let (mut stream, _) = server_listener
                .accept()
                .map_err(|_| "fixture refresh token accept")?;
            let (route, body, authorization_header) = request(&mut stream)?;
            assert_eq!(route, "/token");
            assert!(authorization_header.is_none());
            assert_eq!(field(&decode_form(&body), "grant_type"), "refresh_token");
            assert_eq!(field(&decode_form(&body), "refresh_token"), REFRESH);
            posted_send
                .send(())
                .map_err(|_| "fixture refresh post receiver")?;
            release_receive
                .recv_timeout(DEADLINE)
                .map_err(|_| "fixture refresh response release deadline")?;
            let (rotated_identity, keys) = crypto::tests::signed(
                &json!({"iss":issuer,"sub":"fixture-subject","aud":"oaiapp_fixture","exp":4102444800_u64,"email":"fixture@example.test","workspace_id":"fixture-workspace"}),
            );
            respond(
                &mut stream,
                json!({"access_token":server_access,"refresh_token":server_refresh,"id_token":rotated_identity,"token_type":"Bearer","expires_in":3600,"scope":field(&authorization,"scope")}),
            );
            let (mut stream, _) = server_listener
                .accept()
                .map_err(|_| "fixture refresh JWKS accept")?;
            assert_eq!(request(&mut stream)?.0, "/keys");
            respond(&mut stream, keys);
            Ok(())
        })();
        let _sent = finished_send.send(result);
    });
    let _server = ServerGuard {
        address,
        worker: Some(server),
    };
    let (login, _) = browser_login(
        &home,
        &origin,
        None,
        &authorization_send,
        LoginCase::Authorized,
    );
    assert!(
        login.status.success(),
        "{}",
        String::from_utf8_lossy(&login.stderr)
    );
    let profiles =
        offline_output(command(&home).args(["auth", "profiles", "--fixture-issuer", &origin]));
    assert!(profiles.status.success());
    let profile: Value = serde_json::from_slice(&profiles.stdout).unwrap();
    let profile = profile[0]["id"].as_str().unwrap().to_string();
    let path = home
        .join("data/smith/auth/oauth-fixtures")
        .join(smith::sha256::digest_hex(origin.as_bytes()))
        .join("profiles.json");
    let mut stored: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
    stored["profiles"][0]["metadata"]["expires_at"] = json!(0);
    std::fs::write(&path, serde_json::to_vec(&stored).unwrap()).unwrap();
    let evaluation = [
        "eval",
        "publication failure proof",
        "--json",
        "--provider",
        "openai-chatgpt-plan",
        "--profile",
        &profile,
        "--model",
        "gpt-4o-mini",
        "--fixture-issuer",
        &origin,
    ];
    let failed = ChildGuard(
        file_limited_command(&home, 128)
            .args(evaluation)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .unwrap(),
    );
    posted_receive.recv_timeout(DEADLINE).unwrap();
    let pending = std::fs::read(&path).unwrap();
    let pending_profile: Value = serde_json::from_slice(&pending).unwrap();
    assert_eq!(pending_profile["profiles"][0]["refresh_pending"], true);
    assert_eq!(pending_profile["profiles"][0]["tokens"]["access"], ACCESS);
    assert_eq!(pending_profile["profiles"][0]["tokens"]["refresh"], REFRESH);
    release_send.send(()).unwrap();
    let failed = offline_child_output(failed, "publication failure proof");
    assert!(!failed.status.success());
    finished_receive.recv_timeout(DEADLINE).unwrap().unwrap();
    let replay = offline_output(command(&home).args(evaluation));
    assert!(!replay.status.success());
    assert!(String::from_utf8_lossy(&replay.stderr).contains("explicitly reauthorize"));
    listener.set_nonblocking(true).unwrap();
    assert_eq!(
        listener.accept().unwrap_err().kind(),
        std::io::ErrorKind::WouldBlock
    );
    let reopened = std::fs::read(&path).unwrap();
    assert_eq!(reopened, pending);
    let output = [
        failed.stdout.as_slice(),
        failed.stderr.as_slice(),
        replay.stdout.as_slice(),
        replay.stderr.as_slice(),
    ]
    .concat();
    for secret in [&padded_access, &padded_refresh] {
        let escaped = serde_json::to_string(secret).unwrap();
        for bytes in [secret.as_bytes(), &escaped.as_bytes()[1..escaped.len() - 1]] {
            assert!(!output.windows(bytes.len()).any(|window| window == bytes));
        }
    }
    std::fs::remove_dir_all(home).unwrap();
}

#[test]
fn binary_refreshing_process_proof() {
    refresh_process_case(true);
    refresh_process_case(false);
}

fn refresh_process_case(response_lost: bool) {
    let identity = smith::id::SessionId::new().to_string();
    let digest = smith::sha256::digest_hex(identity.as_bytes());
    let home = std::env::temp_dir().join(format!("orp-{}", &digest[..12]));
    std::fs::create_dir(&home).unwrap();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&home, std::fs::Permissions::from_mode(0o700)).unwrap();
    }
    let listener = TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0)).unwrap();
    let address = listener.local_addr().unwrap();
    let origin = format!("http://{address}");
    let worker_listener = listener.try_clone().unwrap();
    let (authorization_send, authorization_receive) = mpsc::sync_channel(1);
    let (dispatched_send, dispatched_receive) = mpsc::sync_channel(1);
    let (release_send, release_receive) = mpsc::sync_channel(1);
    let (finished_send, finished_receive) = mpsc::sync_channel(1);
    let issuer = origin.clone();
    let server = std::thread::spawn(move || {
        let result = (|| -> Result<(), String> {
            let (_, authorization) = serve_login(
                &worker_listener,
                &issuer,
                &authorization_receive,
                LoginCase::Authorized,
            )?;
            let (mut stream, _) = worker_listener
                .accept()
                .map_err(|_| "fixture refresh discovery accept")?;
            let (route, _, authorization_header) = request(&mut stream)?;
            assert_eq!(route, "/.well-known/openid-configuration");
            assert!(authorization_header.is_none());
            respond(
                &mut stream,
                json!({"issuer":issuer,"jwks_uri":format!("{issuer}/keys")}),
            );
            let (mut stream, _) = worker_listener
                .accept()
                .map_err(|_| "fixture refresh token accept")?;
            let (route, body, authorization_header) = request(&mut stream)?;
            assert_eq!(route, "/token");
            assert!(authorization_header.is_none());
            let body = decode_form(&body);
            assert_eq!(field(&body, "grant_type"), "refresh_token");
            assert_eq!(field(&body, "refresh_token"), REFRESH);
            dispatched_send
                .send(())
                .map_err(|_| "fixture refresh dispatch receiver")?;
            release_receive
                .recv_timeout(DEADLINE)
                .map_err(|_| "fixture refresh release deadline")?;
            if response_lost {
                return Ok(());
            }
            let (identity, keys) = crypto::tests::signed(
                &json!({"iss":issuer,"sub":"fixture-subject","aud":"oaiapp_fixture","exp":4102444800_u64,"email":"fixture@example.test","workspace_id":"fixture-workspace"}),
            );
            respond(
                &mut stream,
                json!({"access_token":ROTATED_ACCESS,"refresh_token":ROTATED_REFRESH,"id_token":identity,"token_type":"Bearer","expires_in":3600,"scope":field(&authorization,"scope")}),
            );
            let (mut stream, _) = worker_listener
                .accept()
                .map_err(|_| "fixture refresh JWKS accept")?;
            let (route, _, _) = request(&mut stream)?;
            assert_eq!(route, "/keys");
            respond(&mut stream, keys);
            let rotated_authorization = format!("Bearer {ROTATED_ACCESS}");
            let (mut stream, _) = worker_listener
                .accept()
                .map_err(|_| "fixture catalog accept")?;
            let (route, _, authorization_header) = request(&mut stream)?;
            assert_eq!(route, "/v1/models");
            assert_eq!(
                authorization_header.as_deref(),
                Some(rotated_authorization.as_str())
            );
            respond(
                &mut stream,
                json!({"models":[{"slug":"gpt-4o-mini","display_name":"Fixture","visibility":"list"}]}),
            );
            let (mut stream, _) = worker_listener
                .accept()
                .map_err(|_| "fixture response accept")?;
            let (route, _, authorization_header) = request(&mut stream)?;
            assert_eq!(route, "/v1/responses");
            assert_eq!(
                authorization_header.as_deref(),
                Some(rotated_authorization.as_str())
            );
            let events = "data: {\"type\":\"response.output_text.delta\",\"delta\":\"fixture refreshed\"}\n\ndata: {\"type\":\"response.completed\",\"response\":{\"status\":\"completed\",\"usage\":{\"input_tokens\":1,\"output_tokens\":1}}}\n\n";
            write!(stream, "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{events}", events.len()).unwrap();
            let (mut stream, _) = worker_listener
                .accept()
                .map_err(|_| "fixture logout discovery accept")?;
            let (route, _, _) = request(&mut stream)?;
            assert_eq!(route, "/.well-known/openid-configuration");
            respond(
                &mut stream,
                json!({"issuer":issuer,"revocation_endpoint":format!("{issuer}/revoke")}),
            );
            let (mut stream, _) = worker_listener
                .accept()
                .map_err(|_| "fixture revocation accept")?;
            let (route, body, authorization_header) = request(&mut stream)?;
            assert_eq!(route, "/revoke");
            assert!(authorization_header.is_none());
            assert_eq!(field(&decode_form(&body), "token"), ROTATED_REFRESH);
            write!(
                stream,
                "HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
            )
            .unwrap();
            Ok(())
        })();
        let _sent = finished_send.send(result);
    });
    let _server = ServerGuard {
        address,
        worker: Some(server),
    };
    let (login, _) = browser_login(
        &home,
        &origin,
        None,
        &authorization_send,
        LoginCase::Authorized,
    );
    assert!(
        login.status.success(),
        "{}",
        String::from_utf8_lossy(&login.stderr)
    );
    let profiles =
        offline_output(command(&home).args(["auth", "profiles", "--fixture-issuer", &origin]));
    assert!(profiles.status.success());
    let profiles: Value = serde_json::from_slice(&profiles.stdout).unwrap();
    let profile = profiles[0]["id"].as_str().unwrap().to_string();
    let path = home
        .join("data/smith/auth/oauth-fixtures")
        .join(smith::sha256::digest_hex(origin.as_bytes()))
        .join("profiles.json");
    let mut stored: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
    stored["profiles"][0]["metadata"]["expires_at"] = json!(0);
    std::fs::write(&path, serde_json::to_vec(&stored).unwrap()).unwrap();
    let evaluation = [
        "eval",
        "refresh process proof",
        "--json",
        "--provider",
        "openai-chatgpt-plan",
        "--profile",
        &profile,
        "--model",
        "gpt-4o-mini",
        "--fixture-issuer",
        &origin,
    ];
    let mut first_command = command(&home);
    let mut first = ChildGuard(
        first_command
            .args(evaluation)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .unwrap(),
    );
    if dispatched_receive.recv_timeout(DEADLINE).is_err() {
        first.0.kill().unwrap();
        let output = offline_child_output(first, "refresh dispatch");
        panic!(
            "fixture refresh dispatch deadline: stdout={} stderr={}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
    }
    let pending: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
    assert_eq!(pending["profiles"][0]["refresh_pending"], true);
    if response_lost {
        first.0.kill().unwrap();
        let first = offline_child_output(first, "refresh response loss");
        release_send.send(()).unwrap();
        assert!(!first.status.success());
        let replay = offline_output(command(&home).args(evaluation));
        assert!(!replay.status.success());
        assert!(
            String::from_utf8_lossy(&replay.stderr).contains("explicitly reauthorize"),
            "{}",
            String::from_utf8_lossy(&replay.stderr)
        );
        let pending: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
        assert_eq!(pending["profiles"][0]["refresh_pending"], true);
        listener.set_nonblocking(true).unwrap();
        assert_eq!(
            listener.accept().unwrap_err().kind(),
            std::io::ErrorKind::WouldBlock
        );
        assert_no_oauth_secret(&[&first.stdout, &first.stderr, &replay.stdout, &replay.stderr]);
        finished_receive.recv_timeout(DEADLINE).unwrap().unwrap();
    } else {
        let busy = offline_output(command(&home).args(evaluation));
        assert!(!busy.status.success());
        assert!(String::from_utf8_lossy(&busy.stderr).contains("busy"));
        let busy_logout = offline_output(command(&home).args([
            "logout",
            "--profile",
            &profile,
            "--fixture-issuer",
            &origin,
        ]));
        assert!(!busy_logout.status.success());
        let busy_logout_stderr = String::from_utf8_lossy(&busy_logout.stderr);
        assert!(busy_logout_stderr.contains("busy"), "{busy_logout_stderr}");
        assert!(!busy_logout_stderr.contains("signed out"));
        let pending: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
        assert_eq!(pending["profiles"][0]["refresh_pending"], true);
        assert_eq!(pending["profiles"][0]["tokens"]["access"], ACCESS);
        assert_eq!(pending["profiles"][0]["tokens"]["refresh"], REFRESH);
        listener.set_nonblocking(true).unwrap();
        assert_eq!(
            listener.accept().unwrap_err().kind(),
            std::io::ErrorKind::WouldBlock
        );
        listener.set_nonblocking(false).unwrap();
        release_send.send(()).unwrap();
        let first = offline_child_output(first, "refresh persistence");
        assert!(
            first.status.success(),
            "{}",
            String::from_utf8_lossy(&first.stderr)
        );
        let stored: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
        assert_eq!(stored["profiles"][0]["refresh_pending"], false);
        assert!(!stored["profiles"][0]["tokens"].is_null());
        assert_ne!(stored["profiles"][0]["metadata"]["expires_at"], json!(0));
        let logout = offline_output(command(&home).args([
            "logout",
            "--profile",
            &profile,
            "--fixture-issuer",
            &origin,
        ]));
        assert!(
            logout.status.success(),
            "{}",
            String::from_utf8_lossy(&logout.stderr)
        );
        let stored: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
        assert!(stored["profiles"][0]["tokens"].is_null());
        let signed_out = offline_output(command(&home).args(evaluation));
        assert!(!signed_out.status.success());
        listener.set_nonblocking(true).unwrap();
        assert_eq!(
            listener.accept().unwrap_err().kind(),
            std::io::ErrorKind::WouldBlock
        );
        assert_no_oauth_secret(&[
            &first.stdout,
            &first.stderr,
            &busy.stdout,
            &busy.stderr,
            &busy_logout.stdout,
            &busy_logout.stderr,
            &logout.stdout,
            &logout.stderr,
            &signed_out.stdout,
            &signed_out.stderr,
        ]);
        finished_receive.recv_timeout(DEADLINE).unwrap().unwrap();
    }
    std::fs::remove_dir_all(home).unwrap();
}

#[test]
fn binary_subscription_responses_tool_round_resumes_complete_history_without_token_leakage() {
    let identity = smith::id::SessionId::new().to_string();
    let digest = smith::sha256::digest_hex(identity.as_bytes());
    let home = std::env::temp_dir().join(format!("ort-{}", &digest[..12]));
    let workdir = home.join("workspace");
    std::fs::create_dir_all(&workdir).unwrap();
    std::fs::write(workdir.join("fixture.txt"), "fixture tool bytes\n").unwrap();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&home, std::fs::Permissions::from_mode(0o700)).unwrap();
    }
    let listener = TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0)).unwrap();
    let address = listener.local_addr().unwrap();
    let origin = format!("http://{address}");
    let (authorization_send, authorization_receive) = mpsc::sync_channel(1);
    let (finished_send, finished_receive) = mpsc::sync_channel(1);
    let issuer = origin.clone();
    let server = std::thread::spawn(move || {
        let result = (|| -> Result<(), String> {
            let _login = serve_login(
                &listener,
                &issuer,
                &authorization_receive,
                LoginCase::Authorized,
            )?;
            for round in 0..3 {
                let (mut stream, _) = listener.accept().map_err(|_| "fixture catalog accept")?;
                let (route, _, authorization) = request(&mut stream)?;
                assert_eq!(route, "/v1/models");
                assert_eq!(
                    authorization.as_deref(),
                    Some("Bearer fixture-access-\\\"sentinel")
                );
                respond(
                    &mut stream,
                    json!({"models":[{"slug":"gpt-4o-mini","display_name":"Fixture","visibility":"list","tool_use":true}]}),
                );
                let (mut stream, _) = listener.accept().map_err(|_| "fixture responses accept")?;
                let (route, body, authorization) = request(&mut stream)?;
                assert_eq!(route, "/v1/responses");
                assert_eq!(
                    authorization.as_deref(),
                    Some("Bearer fixture-access-\\\"sentinel")
                );
                let body: Value =
                    serde_json::from_str(&body).map_err(|_| "fixture response JSON")?;
                assert_eq!(body["store"], false);
                assert_eq!(body["stream"], true);
                assert!(body.get("previous_response_id").is_none());
                assert!(body.get("api_key").is_none());
                if round == 0 {
                    assert!(
                        body["tools"]
                            .as_array()
                            .is_some_and(|tools| tools.iter().any(|tool| tool["name"] == "smith"
                                && tool["tools"].as_array().is_some_and(|tools| tools
                                    .iter()
                                    .any(|tool| tool["name"] == "read"))))
                    );
                    assert!(body["input"].as_array().is_some_and(|input| {
                        input.iter().any(|message| {
                            message["role"] == "user" && message["content"] == "read fixture"
                        })
                    }));
                    let events = "data: {\"type\":\"response.output_item.done\",\"output_index\":0,\"item\":{\"type\":\"function_call\",\"status\":\"completed\",\"namespace\":\"smith\",\"name\":\"read\",\"call_id\":\"fixture-call\",\"arguments\":\"{\\\"path\\\":\\\"fixture.txt\\\"}\"}}\n\ndata: {\"type\":\"response.completed\",\"response\":{\"status\":\"completed\",\"usage\":{\"input_tokens\":1,\"output_tokens\":1}}}\n\n";
                    write!(stream, "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{events}", events.len()).map_err(|_| "fixture first response")?;
                } else if round == 1 {
                    let input = body["input"]
                        .as_array()
                        .ok_or("fixture continuation input")?;
                    assert!(input.iter().any(|item| item["role"] == "user" && item["content"] == "read fixture"));
                    assert!(
                        input.iter().any(|item| item["type"] == "function_call"
                            && item["call_id"] == "fixture-call")
                    );
                    assert!(input.iter().any(|item| {
                        item["type"] == "function_call_output"
                            && item["call_id"] == "fixture-call"
                            && item["output"]
                                .as_str()
                                .is_some_and(|output| output.contains("fixture tool bytes"))
                    }));
                    let events = "data: {\"type\":\"response.output_text.delta\",\"delta\":\"tool continuation complete\"}\n\ndata: {\"type\":\"response.completed\",\"response\":{\"status\":\"completed\",\"usage\":{\"input_tokens\":1,\"output_tokens\":1}}}\n\n";
                    write!(stream, "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{events}", events.len()).map_err(|_| "fixture continuation response")?;
                } else {
                    let input = body["input"].as_array().ok_or("fixture resumed input")?;
                    assert!(input.iter().any(|item| item["role"] == "user" && item["content"] == "read fixture"));
                    assert!(
                        input.iter().any(|item| item["type"] == "function_call"
                            && item["call_id"] == "fixture-call")
                    );
                    assert!(input.iter().any(|item| {
                        item["type"] == "function_call_output"
                            && item["call_id"] == "fixture-call"
                            && item["output"]
                                .as_str()
                                .is_some_and(|output| output.contains("fixture tool bytes"))
                    }));
                    assert!(input.iter().any(|item| item["role"] == "assistant"
                        && item["content"] == "tool continuation complete"));
                    assert!(input.iter().any(
                        |item| item["role"] == "user" && item["content"] == "continue fixture"
                    ));
                    let events = "data: {\"type\":\"response.output_text.delta\",\"delta\":\"resumed complete\"}\n\ndata: {\"type\":\"response.completed\",\"response\":{\"status\":\"completed\",\"usage\":{\"input_tokens\":1,\"output_tokens\":1}}}\n\n";
                    write!(stream, "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{events}", events.len()).map_err(|_| "fixture resumed response")?;
                }
            }
            Ok(())
        })();
        let _sent = finished_send.send(result);
    });
    let _server = ServerGuard {
        address,
        worker: Some(server),
    };
    let (login, _) = browser_login(
        &home,
        &origin,
        None,
        &authorization_send,
        LoginCase::Authorized,
    );
    assert!(
        login.status.success(),
        "{}",
        String::from_utf8_lossy(&login.stderr)
    );
    let profiles =
        offline_output(command(&home).args(["auth", "profiles", "--fixture-issuer", &origin]));
    assert!(profiles.status.success());
    let profile: Value = serde_json::from_slice(&profiles.stdout).unwrap();
    let profile = profile[0]["id"].as_str().unwrap().to_string();
    let evaluation = [
        "eval",
        "read fixture",
        "--json",
        "--provider",
        "openai-chatgpt-plan",
        "--profile",
        &profile,
        "--model",
        "gpt-4o-mini",
        "--fixture-issuer",
        &origin,
    ];
    let first = offline_output(command(&home).current_dir(&workdir).args(evaluation));
    assert!(
        first.status.success(),
        "{}",
        String::from_utf8_lossy(&first.stderr)
    );
    let session = std::fs::read_dir(home.join("data/smith/sessions/run"))
        .unwrap()
        .find_map(|entry| {
            let path = entry.unwrap().path();
            (path.extension().is_some_and(|extension| extension == "smh")
                && std::fs::read(&path)
                    .unwrap()
                    .windows(b"read fixture".len())
                    .any(|bytes| bytes == b"read fixture"))
            .then(|| path.file_stem().unwrap().to_string_lossy().into_owned())
        })
        .unwrap();
    let resumed = offline_output(command(&home).current_dir(&workdir).args([
        "eval",
        "continue fixture",
        "--json",
        "--session",
        &session,
        "--provider",
        "openai-chatgpt-plan",
        "--profile",
        &profile,
        "--model",
        "gpt-4o-mini",
        "--fixture-issuer",
        &origin,
    ]));
    assert!(
        resumed.status.success(),
        "{}",
        String::from_utf8_lossy(&resumed.stderr)
    );
    let reopened = offline_output(command(&home).args(["sessions", "get", &session, "--json"]));
    assert!(
        reopened.status.success(),
        "{}",
        String::from_utf8_lossy(&reopened.stderr)
    );
    finished_receive.recv_timeout(DEADLINE).unwrap().unwrap();
    let session_bytes = std::fs::read(
        home.join("data/smith/sessions/run")
            .join(format!("{session}.smh")),
    )
    .unwrap();
    let observed = [
        login.stdout.as_slice(),
        login.stderr.as_slice(),
        first.stdout.as_slice(),
        first.stderr.as_slice(),
        resumed.stdout.as_slice(),
        resumed.stderr.as_slice(),
        reopened.stdout.as_slice(),
        reopened.stderr.as_slice(),
        session_bytes.as_slice(),
    ]
    .concat();
    for secret in [ACCESS, REFRESH] {
        let escaped = serde_json::to_string(secret).unwrap();
        for bytes in [secret.as_bytes(), &escaped.as_bytes()[1..escaped.len() - 1]] {
            assert!(!observed.windows(bytes.len()).any(|window| window == bytes));
        }
    }
    std::fs::remove_dir_all(home).unwrap();
}

const OUTPUT_LIMIT: u64 = 65_536;
const DIAGNOSTIC_DEADLINE: Duration = Duration::from_secs(1);

#[track_caller]
fn offline_child_output(mut child: ChildGuard, invocation: &str) -> std::process::Output {
    let stdout = child.0.stdout.take().unwrap();
    let stderr = child.0.stderr.take().unwrap();
    let (stdout_send, stdout_receive) = mpsc::sync_channel(1);
    let (stderr_send, stderr_receive) = mpsc::sync_channel(1);
    let stdout_reader = std::thread::spawn(move || {
        let mut bytes = Vec::new();
        stdout
            .take(OUTPUT_LIMIT + 1)
            .read_to_end(&mut bytes)
            .unwrap();
        let _sent = stdout_send.send(bytes);
    });
    let stderr_reader = std::thread::spawn(move || {
        let mut bytes = Vec::new();
        stderr
            .take(OUTPUT_LIMIT + 1)
            .read_to_end(&mut bytes)
            .unwrap();
        let _sent = stderr_send.send(bytes);
    });
    let stdout = receive_output(
        &mut child,
        "stdout",
        invocation,
        &stdout_receive,
        &stderr_receive,
    );
    let stderr = receive_output(
        &mut child,
        "stderr",
        invocation,
        &stderr_receive,
        &stdout_receive,
    );
    assert!(stdout.len() <= OUTPUT_LIMIT as usize && stderr.len() <= OUTPUT_LIMIT as usize);
    let status = child.0.wait().unwrap();
    stdout_reader.join().unwrap();
    stderr_reader.join().unwrap();
    std::process::Output {
        status,
        stdout,
        stderr,
    }
}

#[track_caller]
fn receive_output(
    child: &mut ChildGuard,
    pipe: &str,
    invocation: &str,
    receive: &mpsc::Receiver<Vec<u8>>,
    other_receive: &mpsc::Receiver<Vec<u8>>,
) -> Vec<u8> {
    match receive.recv_timeout(DEADLINE) {
        Ok(output) => output,
        Err(error) => {
            let status = child.0.try_wait().unwrap();
            let _killed = child.0.kill();
            let reaped = child.0.wait().unwrap();
            let pipe_output = receive.recv_timeout(DIAGNOSTIC_DEADLINE).ok();
            let other_output = other_receive.recv_timeout(DIAGNOSTIC_DEADLINE).ok();
            panic!(
                "output deadline: pipe={pipe}; error={error}; invocation={invocation:?}; caller={}; child_before_kill={status:?}; child_after_kill={reaped}; {pipe}_bytes={}; other_bytes={}; {pipe}_diagnostic={:?}; other_diagnostic={:?}",
                std::panic::Location::caller(),
                pipe_output.as_ref().map_or(0, Vec::len),
                other_output.as_ref().map_or(0, Vec::len),
                pipe_output.as_deref().map(redact_diagnostic),
                other_output.as_deref().map(redact_diagnostic),
            );
        }
    }
}

fn redact_diagnostic(bytes: &[u8]) -> String {
    let mut text = String::from_utf8_lossy(bytes).into_owned();
    for secret in [ACCESS, REFRESH, ROTATED_ACCESS, ROTATED_REFRESH] {
        let escaped = serde_json::to_string(secret).unwrap();
        for secret in [secret, escaped.trim_matches('"')] {
            text = text.replace(secret, "[REDACTED]");
        }
    }
    const DIAGNOSTIC_BYTES_MAX: usize = 512;
    text.truncate(DIAGNOSTIC_BYTES_MAX);
    text
}

fn assert_no_oauth_secret(outputs: &[&[u8]]) {
    for secret in [ACCESS, REFRESH, ROTATED_ACCESS, ROTATED_REFRESH] {
        let encoded = serde_json::to_string(secret).unwrap();
        for secret in [secret.as_bytes(), &encoded.as_bytes()[1..encoded.len() - 1]] {
            assert!(
                outputs
                    .iter()
                    .all(|output| !output.windows(secret.len()).any(|window| window == secret))
            );
        }
    }
}

fn login_case(case: LoginCase) {
    let identity = smith::id::SessionId::new().to_string();
    let digest = smith::sha256::digest_hex(identity.as_bytes());
    let home = std::env::temp_dir().join(format!("oa-{}", &digest[..12]));
    std::fs::create_dir(&home).unwrap();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&home, std::fs::Permissions::from_mode(0o700)).unwrap();
    }
    let listener = TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0)).unwrap();
    let address = listener.local_addr().unwrap();
    let origin = format!("http://{address}");
    let (authorization_send, authorization_receive) =
        mpsc::sync_channel::<Vec<(String, String)>>(1);
    let (finished_send, finished_receive) = mpsc::sync_channel(1);
    let issuer = origin.clone();
    let server = std::thread::spawn(move || {
        let result = (|| -> Result<Vec<String>, String> {
            let (identity, authorization) = serve_login(
                &listener,
                &issuer,
                &authorization_receive,
                if case == LoginCase::ReturningIdentityRejected {
                    LoginCase::Authorized
                } else {
                    case
                },
            )?;
            let mut identities = vec![identity];
            let access = if case == LoginCase::Expired {
                let (mut stream, _) = listener
                    .accept()
                    .map_err(|_| "fixture refresh discovery accept")?;
                let (route, _, auth) = request(&mut stream)?;
                assert_eq!(route, "/.well-known/openid-configuration");
                assert!(auth.is_none());
                respond(
                    &mut stream,
                    json!({"issuer":issuer,"jwks_uri":format!("{issuer}/keys")}),
                );
                let (mut stream, _) = listener
                    .accept()
                    .map_err(|_| "fixture refresh token accept")?;
                let (route, body, auth) = request(&mut stream)?;
                assert_eq!(route, "/token");
                assert!(auth.is_none());
                let body = decode_form(&body);
                assert_eq!(field(&body, "grant_type"), "refresh_token");
                assert_eq!(field(&body, "client_id"), "oaiapp_fixture");
                assert_eq!(field(&body, "refresh_token"), REFRESH);
                assert_eq!(field(&body, "resource"), format!("{issuer}/v1"));
                assert!(!body.iter().any(|(key, _)| key == "scope"));
                let (identity, keys) = crypto::tests::signed(
                    &json!({"iss":issuer,"sub":"fixture-subject","aud":"oaiapp_fixture","exp":4102444800_u64,"email":"fixture@example.test","workspace_id":"fixture-workspace"}),
                );
                respond(
                    &mut stream,
                    json!({"access_token":ROTATED_ACCESS,"refresh_token":ROTATED_REFRESH,"id_token":identity,"token_type":"Bearer","expires_in":3600,"scope":field(&authorization,"scope")}),
                );
                identities.push(identity);
                let (mut stream, _) = listener
                    .accept()
                    .map_err(|_| "fixture refresh JWKS accept")?;
                let (route, _, _) = request(&mut stream)?;
                assert_eq!(route, "/keys");
                respond(&mut stream, keys);
                format!("Bearer {ROTATED_ACCESS}")
            } else {
                format!("Bearer {ACCESS}")
            };
            if case.initial_authorized() {
                let (mut stream, _) = listener.accept().map_err(|_| "fixture catalog accept")?;
                let (route, _, auth) = request(&mut stream)?;
                assert_eq!(route, "/v1/models");
                assert_eq!(auth.as_deref(), Some(access.as_str()));
                respond(
                    &mut stream,
                    if case == LoginCase::EntitlementRejected {
                        json!({"models":[]})
                    } else {
                        json!({"models":[{"slug":"gpt-4o-mini","display_name":"Fixture","visibility":"list"}]})
                    },
                );
                if case != LoginCase::EntitlementRejected {
                    let (mut stream, _) =
                        listener.accept().map_err(|_| "fixture inference accept")?;
                    let (route, body, auth) = request(&mut stream)?;
                    assert_eq!(route, "/v1/responses");
                    assert_eq!(auth.as_deref(), Some(access.as_str()));
                    let body: Value = serde_json::from_str(&body).unwrap();
                    assert_eq!(body["model"], "gpt-4o-mini");
                    assert_eq!(body["store"], false);
                    assert_eq!(body["stream"], true);
                    assert!(body.get("previous_response_id").is_none());
                    assert!(
                        body["input"]
                            .as_array()
                            .unwrap()
                            .iter()
                            .any(|message| message["content"] == "Say fixture success")
                    );
                    let refresh = if case == LoginCase::Expired {
                        ROTATED_REFRESH
                    } else {
                        REFRESH
                    };
                    let delta = format!(
                        "fixture success:{}:{refresh}:{}",
                        access.strip_prefix("Bearer ").unwrap(),
                        identities.last().unwrap()
                    );
                    let events = format!(
                        "data: {}\n\ndata: {{\"type\":\"response.completed\",\"response\":{{\"status\":\"completed\",\"usage\":{{\"input_tokens\":7,\"output_tokens\":3}}}}}}\n\n",
                        json!({"type":"response.output_text.delta","delta":delta})
                    );
                    write!(stream, "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{events}", events.len()).unwrap();
                }
            }
            if case.initial_authorized() {
                let (identity, _) = serve_login(
                    &listener,
                    &issuer,
                    &authorization_receive,
                    if case == LoginCase::ReturningIdentityRejected {
                        case
                    } else {
                        LoginCase::Authorized
                    },
                )?;
                identities.push(identity);
                let (mut stream, _) = listener
                    .accept()
                    .map_err(|_| "fixture logout discovery accept")?;
                let (route, _, _) = request(&mut stream)?;
                assert_eq!(route, "/.well-known/openid-configuration");
                respond(
                    &mut stream,
                    json!({"issuer":issuer,"revocation_endpoint":format!("{issuer}/revoke")}),
                );
                let (mut stream, _) = listener.accept().map_err(|_| "fixture revocation accept")?;
                let (route, body, auth) = request(&mut stream)?;
                assert_eq!(route, "/revoke");
                assert!(auth.is_none());
                let body = decode_form(&body);
                assert_eq!(field(&body, "client_id"), "oaiapp_fixture");
                assert_eq!(field(&body, "token"), REFRESH);
                assert_eq!(field(&body, "token_type_hint"), "refresh_token");
                let status = if case == LoginCase::Authorized {
                    "200 OK"
                } else {
                    "503 Unavailable"
                };
                write!(
                    stream,
                    "HTTP/1.1 {status}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
                )
                .unwrap();
            }
            Ok(identities)
        })();
        let _ = finished_send.send(result);
    });
    let _server = ServerGuard {
        address,
        worker: Some(server),
    };
    let (login, host_id) = browser_login(&home, &origin, None, &authorization_send, case);
    assert_eq!(
        login.status.success(),
        case.initial_authorized(),
        "{}",
        String::from_utf8_lossy(&login.stderr)
    );
    let listing =
        offline_output(command(&home).args(["auth", "profiles", "--fixture-issuer", &origin]));
    assert!(listing.status.success());
    let profiles: Value = serde_json::from_slice(&listing.stdout).unwrap();
    if case.initial_authorized() {
        assert_eq!(profiles[0]["subject"], "fixture-subject");
        assert_eq!(profiles[0]["client_id"], "oaiapp_fixture");
        assert_eq!(profiles[0]["workspace"], "fixture-workspace");
    } else {
        assert_eq!(profiles, json!([]));
    }
    let mut evaluation = Vec::new();
    if case.initial_authorized() {
        let profile = profiles[0]["id"].as_str().unwrap();
        let credential_module =
            (case == LoginCase::Authorized).then(|| external_credential_module(&home));
        if case == LoginCase::Expired {
            // Inject expiry in this owned fixture instead of sleeping for wall time.
            let path = home
                .join("data/smith/auth/oauth-fixtures")
                .join(smith::sha256::digest_hex(origin.as_bytes()))
                .join("profiles.json");
            let mut stored: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
            stored["profiles"][0]["metadata"]["expires_at"] = json!(0);
            std::fs::write(path, serde_json::to_vec(&stored).unwrap()).unwrap();
        }
        let mut evaluation_args = vec![
            "eval".to_string(),
            "Say fixture success".to_string(),
            "--json".to_string(),
            "--provider".to_string(),
            "openai-chatgpt-plan".to_string(),
            "--profile".to_string(),
            profile.to_string(),
            "--model".to_string(),
            "gpt-4o-mini".to_string(),
            "--fixture-issuer".to_string(),
            origin.clone(),
        ];
        if let Some(module) = &credential_module {
            evaluation_args.extend(["--credential-module".into(), module.clone()]);
        }
        let result = offline_output(command(&home).args(evaluation_args));
        if case == LoginCase::EntitlementRejected {
            assert!(!result.status.success());
            assert!(
                String::from_utf8_lossy(&result.stderr)
                    .contains("not available to selected ChatGPT account")
            );
        } else {
            assert!(
                result.status.success(),
                "{}",
                String::from_utf8_lossy(&result.stderr)
            );
            let output: Value = serde_json::from_slice(&result.stdout).unwrap();
            assert_eq!(
                output["text"],
                "fixture success:{{secret:connection-api-key}}:{{secret:connection-api-key}}:{{secret:connection-api-key}}",
                "all active OAuth credentials must be sanitized before guest decoding"
            );
            assert_eq!(output["reason"], "EndTurn");
        }
        evaluation.extend_from_slice(&result.stdout);
        evaluation.extend_from_slice(&result.stderr);
        let (returning, returning_host) = browser_login(
            &home,
            &origin,
            Some(profile),
            &authorization_send,
            LoginCase::Authorized,
        );
        assert!(
            returning.status.success() == (case != LoginCase::ReturningIdentityRejected),
            "{}",
            String::from_utf8_lossy(&returning.stderr)
        );
        assert_eq!(returning_host, host_id);
        evaluation.extend_from_slice(&returning.stdout);
        evaluation.extend_from_slice(&returning.stderr);
        let logout = offline_output(command(&home).args([
            "logout",
            "--profile",
            profile,
            "--fixture-issuer",
            &origin,
        ]));
        assert!(
            logout.status.success(),
            "{}",
            String::from_utf8_lossy(&logout.stderr)
        );
        assert!(String::from_utf8_lossy(&logout.stdout).contains(
            if case == LoginCase::Authorized {
                "remote revocation confirmed"
            } else {
                "remote revocation unconfirmed"
            }
        ));
        evaluation.extend_from_slice(&logout.stdout);
        evaluation.extend_from_slice(&logout.stderr);
        let signed_out = offline_output(command(&home).args([
            "eval",
            "must not dispatch",
            "--provider",
            "openai-chatgpt-plan",
            "--profile",
            profile,
            "--model",
            "gpt-4o-mini",
            "--fixture-issuer",
            &origin,
        ]));
        assert!(!signed_out.status.success());
        evaluation.extend_from_slice(&signed_out.stdout);
        evaluation.extend_from_slice(&signed_out.stderr);
    }
    let identities = finished_receive.recv_timeout(DEADLINE).unwrap().unwrap();
    let production = offline_output(command(&home).args(["auth", "profiles"]));
    assert!(production.status.success());
    assert_eq!(
        serde_json::from_slice::<Value>(&production.stdout).unwrap(),
        json!([])
    );
    let sessions = home.join("data/smith/sessions/run");
    let mut durable = Vec::new();
    for entry in std::fs::read_dir(sessions).unwrap() {
        let path = entry.unwrap().path();
        if path.extension().is_some_and(|extension| extension == "smh") {
            let bytes = std::fs::read(&path).unwrap();
            if bytes
                .windows(b"Say fixture success".len())
                .any(|window| window == b"Say fixture success")
            {
                let profile = profiles[0]["id"].as_str().unwrap().as_bytes();
                assert!(
                    bytes.windows(profile.len()).any(|window| window == profile),
                    "eval must durably name the selected profile"
                );
                assert!(
                    bytes
                        .windows(b"openai-chatgpt-plan@1".len())
                        .any(|window| window == b"openai-chatgpt-plan@1"),
                    "eval must durably name its renewable module"
                );
                if case == LoginCase::Authorized {
                    assert!(
                        bytes
                            .windows(b"fixture:external-oauth".len())
                            .any(|window| window == b"fixture:external-oauth"),
                        "eval must durably name the selected external credential implementation"
                    );
                }
            }
            durable.extend_from_slice(&bytes);
            let reopened = offline_output(command(&home).args([
                "sessions",
                "get",
                path.file_stem().unwrap().to_str().unwrap(),
                "--json",
            ]));
            assert!(
                reopened.status.success(),
                "{}",
                String::from_utf8_lossy(&reopened.stderr)
            );
            durable.extend_from_slice(&reopened.stdout);
            durable.extend_from_slice(&reopened.stderr);
            smith_core::store::SessionReader::open(&path)
                .unwrap()
                .verify_full(smith::config::Config::default_valid().frame_bytes_max)
                .unwrap();
        }
    }
    assert!(!durable.is_empty());
    let observed = [
        login.stdout.as_slice(),
        &login.stderr,
        &listing.stdout,
        &listing.stderr,
        &durable,
        &evaluation,
    ]
    .concat();
    for secret in [ACCESS, REFRESH, ROTATED_ACCESS, ROTATED_REFRESH]
        .into_iter()
        .chain(identities.iter().map(String::as_str))
    {
        let escaped = serde_json::to_string(secret).unwrap();
        for bytes in [secret.as_bytes(), &escaped.as_bytes()[1..escaped.len() - 1]] {
            assert!(!observed.windows(bytes.len()).any(|window| window == bytes));
        }
    }
    std::fs::remove_dir_all(home).unwrap();
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ClaudeCase {
    Authorized,
    WrongState,
    RefreshUncertain,
    InvalidGrant,
}

#[test]
fn binary_claude_subscription_login_refresh_eval_and_replay_keep_tokens_host_only() {
    for case in [
        ClaudeCase::Authorized,
        ClaudeCase::WrongState,
        ClaudeCase::RefreshUncertain,
        ClaudeCase::InvalidGrant,
    ] {
        claude_subscription_case(case);
    }
}

fn claude_subscription_case(case: ClaudeCase) {
    let home = std::env::temp_dir().join(format!("claude-{}", smith::id::SessionId::new()));
    std::fs::create_dir(&home).unwrap();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&home, std::fs::Permissions::from_mode(0o700)).unwrap();
    }
    let listener = TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0)).unwrap();
    let address = listener.local_addr().unwrap();
    let observer = listener.try_clone().unwrap();
    let origin = format!("http://{address}");
    let (authorization_send, authorization_receive) =
        mpsc::sync_channel::<Vec<(String, String)>>(1);
    let (finished_send, finished_receive) = mpsc::sync_channel(1);
    let server = std::thread::spawn(move || {
        let result = (|| -> Result<(), String> {
            let authorization = authorization_receive
                .recv_timeout(DEADLINE)
                .map_err(|_| "authorization deadline")?;
            if case == ClaudeCase::WrongState {
                return Ok(());
            }
            let (mut stream, _) = listener.accept().map_err(|_| "Claude token accept")?;
            let FixtureRequest {
                method: _,
                route,
                body,
                authorization: auth,
                headers,
            } = request_with_headers(&mut stream)?;
            assert_eq!(route, "/token");
            assert!(auth.is_none());
            assert_eq!(field(&headers, "content-type"), "application/json");
            assert_eq!(field(&headers, "accept"), "application/json");
            let body: Value = serde_json::from_str(&body).map_err(|_| "Claude token body")?;
            assert_eq!(body["grant_type"], "authorization_code");
            assert_eq!(body["client_id"], "9d1c250a-e61b-44d9-88ed-5944d1962f5e");
            assert_eq!(body["code"], "fixture-code");
            assert_eq!(body["state"], field(&authorization, "state"));
            assert_eq!(body["redirect_uri"], field(&authorization, "redirect_uri"));
            assert_eq!(
                crypto::challenge(body["code_verifier"].as_str().unwrap()),
                field(&authorization, "code_challenge")
            );
            // The real fixed-client response does not assert an OIDC identity.
            respond(
                &mut stream,
                json!({"access_token":ACCESS,"refresh_token":REFRESH,"expires_in":1}),
            );
            let (mut stream, _) = listener.accept().map_err(|_| "Claude refresh accept")?;
            let FixtureRequest {
                method: _,
                route,
                body,
                authorization: auth,
                headers,
            } = request_with_headers(&mut stream)?;
            assert_eq!(route, "/token");
            assert!(auth.is_none());
            assert_eq!(field(&headers, "content-type"), "application/json");
            assert_eq!(field(&headers, "accept"), "application/json");
            let body: Value = serde_json::from_str(&body).map_err(|_| "Claude refresh body")?;
            assert_eq!(body["grant_type"], "refresh_token");
            assert_eq!(body["client_id"], "9d1c250a-e61b-44d9-88ed-5944d1962f5e");
            assert_eq!(body["refresh_token"], REFRESH);
            if case == ClaudeCase::RefreshUncertain {
                // Exchange reached the server; losing the response leaves pending state unusable.
                drop(stream);
                return Ok(());
            }
            if case == ClaudeCase::InvalidGrant {
                let body = r#"{"error":"invalid_grant"}"#;
                write!(stream, "HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).map_err(|_| "invalid grant response")?;
                return Ok(());
            }
            respond(
                &mut stream,
                json!({"access_token":ROTATED_ACCESS,"refresh_token":ROTATED_REFRESH,"expires_in":3600}),
            );
            let (mut stream, _) = listener.accept().map_err(|_| "Claude inference accept")?;
            let FixtureRequest {
                method: _,
                route,
                body,
                authorization: auth,
                headers,
            } = request_with_headers(&mut stream)?;
            assert_eq!(route, "/v1/messages");
            assert_eq!(field(&headers, "anthropic-version"), "2023-06-01");
            assert_eq!(
                field(&headers, "anthropic-beta"),
                "claude-code-20250219,oauth-2025-04-20"
            );
            assert_eq!(field(&headers, "user-agent"), "claude-cli/2.1.280");
            assert_eq!(field(&headers, "x-app"), "cli");
            assert!(!headers.iter().any(|(name, _)| name == "x-api-key"));
            assert_eq!(
                auth.as_deref(),
                Some(format!("Bearer {ROTATED_ACCESS}").as_str())
            );
            let body: Value = serde_json::from_str(&body).map_err(|_| "Claude inference body")?;
            assert_eq!(body["model"], "claude-sonnet-4-5");
            assert_eq!(body["stream"], true);
            assert_eq!(
                body["system"],
                json!([{
                    "type": "text",
                    "text": "You are Claude Code, Anthropic's official CLI for Claude."
                }])
            );
            assert_eq!(
                body["messages"],
                json!([{
                    "role": "user",
                    "content": [{"type": "text", "text": "fixture Claude"}]
                }])
            );
            let metadata: Vec<Value> =
                serde_json::from_str(include_str!("../../plugins/coding-tools/metadata.json"))
                    .map_err(|_| "canonical Claude tool metadata")?;
            let mut tools = metadata
                .into_iter()
                .map(|tool| {
                    Ok(json!({
                        "name": tool["name"],
                        "description": tool["description"],
                        "input_schema": serde_json::from_str::<Value>(
                            tool["input_schema_json"]
                                .as_str()
                                .ok_or("canonical Claude tool schema")?,
                        )
                        .map_err(|_| "canonical Claude tool schema")?,
                    }))
                })
                .collect::<Result<Vec<_>, &str>>()?;
            tools.sort_by(|left, right| left["name"].as_str().cmp(&right["name"].as_str()));
            assert_eq!(body["tools"], Value::Array(tools));
            let echo = json!({"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":format!("Claude subscription complete {ROTATED_ACCESS} {ROTATED_REFRESH}")}});
            let events = format!(
                "data: {{\"type\":\"message_start\",\"message\":{{\"usage\":{{\"input_tokens\":1,\"output_tokens\":0}}}}}}\n\ndata: {echo}\n\ndata: {{\"type\":\"message_delta\",\"delta\":{{\"stop_reason\":\"end_turn\"}},\"usage\":{{\"output_tokens\":1}}}}\n\ndata: {{\"type\":\"message_stop\"}}\n\n"
            );
            write!(stream, "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{events}", events.len()).map_err(|_| "Claude SSE write")?;
            Ok(())
        })();
        let _sent = finished_send.send(result);
    });
    let _server = ServerGuard {
        address,
        worker: Some(server),
    };
    let (login, _) = browser_login_for(
        "anthropic-claude-plan",
        &home,
        &origin,
        None,
        &authorization_send,
        if case == ClaudeCase::WrongState {
            LoginCase::StateRejected
        } else {
            LoginCase::Authorized
        },
    );
    assert_eq!(
        login.status.success(),
        case != ClaudeCase::WrongState,
        "{}",
        String::from_utf8_lossy(&login.stderr)
    );
    let profiles = offline_output(command(&home).args([
        "auth",
        "profiles",
        "--provider",
        "anthropic-claude-plan",
        "--fixture-issuer",
        &origin,
    ]));
    assert!(
        profiles.status.success(),
        "{}",
        String::from_utf8_lossy(&profiles.stderr)
    );
    let profiles_value: Value = serde_json::from_slice(&profiles.stdout).unwrap();
    if case == ClaudeCase::WrongState {
        assert_eq!(profiles_value, json!([]));
        observer.set_nonblocking(true).unwrap();
        assert_eq!(
            observer.accept().unwrap_err().kind(),
            std::io::ErrorKind::WouldBlock
        );
        finished_receive.recv_timeout(DEADLINE).unwrap().unwrap();
        assert_no_oauth_secret(&[&login.stdout, &login.stderr, &profiles.stdout]);
        std::fs::remove_dir_all(home).unwrap();
        return;
    }
    let profile = profiles_value[0]["id"].as_str().unwrap();
    assert_eq!(profile.len(), 64);
    assert_eq!(profiles_value[0]["subject"], "");
    assert_eq!(profiles_value[0]["flow"], "FixedClientPkceOpaque");
    assert_eq!(profiles_value[0]["connection_id"], "anthropic-claude-plan");
    let openai =
        offline_output(command(&home).args(["auth", "profiles", "--fixture-issuer", &origin]));
    assert!(openai.status.success());
    assert_eq!(
        serde_json::from_slice::<Value>(&openai.stdout).unwrap(),
        json!([])
    );
    let crossed = offline_output(command(&home).args([
        "login",
        "--profile",
        profile,
        "--fixture-issuer",
        &origin,
    ]));
    assert!(!crossed.status.success());
    assert!(!String::from_utf8_lossy(&crossed.stdout).contains("Continue with"));
    assert_no_oauth_secret(&[&crossed.stdout, &crossed.stderr]);
    let evaluation = [
        "eval",
        "fixture Claude",
        "--json",
        "--provider",
        "anthropic-claude-plan",
        "--profile",
        profile,
        "--model",
        "claude-sonnet-4-5",
        "--fixture-issuer",
        &origin,
    ];
    let evaluated = offline_output(command(&home).args(evaluation));
    assert_eq!(
        evaluated.status.success(),
        !matches!(
            case,
            ClaudeCase::RefreshUncertain | ClaudeCase::InvalidGrant
        ),
        "{}",
        String::from_utf8_lossy(&evaluated.stderr)
    );
    finished_receive.recv_timeout(DEADLINE).unwrap().unwrap();
    let profile_path = home
        .join("data/smith/auth/oauth-fixtures")
        .join(smith::sha256::digest_hex(origin.as_bytes()))
        .join("anthropic-claude-plan/profiles.json");
    let persisted: Value = serde_json::from_slice(&std::fs::read(&profile_path).unwrap()).unwrap();
    match case {
        ClaudeCase::RefreshUncertain => {
            assert_eq!(persisted["profiles"][0]["refresh_pending"], true)
        }
        ClaudeCase::InvalidGrant => assert!(persisted["profiles"][0]["tokens"].is_null()),
        _ => {
            assert_eq!(persisted["profiles"][0]["refresh_pending"], false);
            assert_eq!(persisted["profiles"][0]["tokens"]["access"], ROTATED_ACCESS);
            assert_eq!(
                persisted["profiles"][0]["tokens"]["refresh"],
                ROTATED_REFRESH
            );
            assert_eq!(persisted["profiles"][0]["tokens"]["identity"], "");
        }
    }
    if matches!(
        case,
        ClaudeCase::RefreshUncertain | ClaudeCase::InvalidGrant
    ) {
        let rejected = offline_output(command(&home).args(evaluation));
        assert!(!rejected.status.success());
        assert!(String::from_utf8_lossy(&rejected.stderr).contains("explicitly reauthorize"));
        assert_no_oauth_secret(&[&rejected.stdout, &rejected.stderr]);
        observer.set_nonblocking(true).unwrap();
        assert_eq!(
            observer.accept().unwrap_err().kind(),
            std::io::ErrorKind::WouldBlock
        );
    } else {
        assert!(
            String::from_utf8_lossy(&evaluated.stdout).contains("Claude subscription complete")
        );
        let logout = offline_output(command(&home).args([
            "logout",
            "--provider",
            "anthropic-claude-plan",
            "--profile",
            profile,
            "--fixture-issuer",
            &origin,
        ]));
        assert!(
            logout.status.success(),
            "{}",
            String::from_utf8_lossy(&logout.stderr)
        );
        let rejected = offline_output(command(&home).args(evaluation));
        assert!(!rejected.status.success());
        assert_no_oauth_secret(&[
            &logout.stdout,
            &logout.stderr,
            &rejected.stdout,
            &rejected.stderr,
        ]);
    }
    let mut evidence = vec![];
    for entry in std::fs::read_dir(home.join("data/smith/sessions/run")).unwrap() {
        let path = entry.unwrap().path();
        if path.extension().is_some_and(|extension| extension == "smh") {
            evidence.extend(std::fs::read(&path).unwrap());
            let session = path.file_stem().unwrap().to_str().unwrap();
            let replayed =
                offline_output(command(&home).args(["sessions", "get", session, "--json"]));
            assert!(
                replayed.status.success(),
                "{}",
                String::from_utf8_lossy(&replayed.stderr)
            );
            evidence.extend(replayed.stdout);
            evidence.extend(replayed.stderr);
        }
    }
    assert!(
        !evidence
            .windows(b"/v1/models".len())
            .any(|window| window == b"/v1/models"),
        "Claude dispatch must not record a fictional OpenAI entitlement probe"
    );
    for provenance in [
        "anthropic-claude-plan",
        "smith:protocol-anthropic",
        "anthropic-claude-plan@1",
        profile,
    ] {
        assert!(
            evidence
                .windows(provenance.len())
                .any(|window| window == provenance.as_bytes()),
            "missing provenance: {provenance}"
        );
    }
    assert_no_oauth_secret(&[
        &login.stdout,
        &login.stderr,
        &profiles.stdout,
        &evaluated.stdout,
        &evaluated.stderr,
        &evidence,
    ]);
    std::fs::remove_dir_all(home).unwrap();
}

#[test]
fn google_subscription_login_project_refresh_and_wrapped_eval() {
    let home = std::env::temp_dir().join(format!("google-plan-{}", smith::id::SessionId::new()));
    std::fs::create_dir(&home).unwrap();
    std::fs::write(home.join("google-read.txt"), "Google tool fixture bytes").unwrap();
    let listener = TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0)).unwrap();
    let address = listener.local_addr().unwrap();
    let origin = format!("http://{address}");
    let (authorization_send, authorization_receive) =
        mpsc::sync_channel::<Vec<(String, String)>>(1);
    let (finished_send, finished_receive) = mpsc::sync_channel(1);
    let server = std::thread::spawn(move || {
        let result = (|| -> Result<String, String> {
            let authorization = authorization_receive
                .recv_timeout(DEADLINE)
                .map_err(|_| "authorization deadline")?;
            assert_eq!(field(&authorization, "response_type"), "code");
            assert_eq!(field(&authorization, "access_type"), "offline");
            assert_eq!(
                field(&authorization, "scope"),
                "https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
            );
            assert_eq!(authorization.len(), 8);
            let (mut stream, _) = listener.accept().map_err(|_| "Google exchange accept")?;
            let exchange = request_with_headers(&mut stream)?;
            assert_eq!(exchange.method, "POST");
            assert_eq!(exchange.route, "/token");
            assert_eq!(
                field(&exchange.headers, "content-type"),
                "application/x-www-form-urlencoded"
            );
            assert!(exchange.authorization.is_none());
            let body = decode_form(&exchange.body);
            assert_eq!(body.len(), 6);
            assert_eq!(field(&body, "grant_type"), "authorization_code");
            assert_eq!(
                field(&body, "client_id"),
                "681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com"
            );
            assert_eq!(
                field(&body, "client_secret"),
                "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"
            );
            assert_eq!(field(&body, "code"), "fixture-code");
            assert_eq!(
                field(&body, "redirect_uri"),
                field(&authorization, "redirect_uri")
            );
            assert_eq!(
                crypto::challenge(field(&body, "code_verifier")),
                field(&authorization, "code_challenge")
            );
            respond(
                &mut stream,
                json!({"access_token":ACCESS,"refresh_token":REFRESH,"expires_in":1,"token_type":"Bearer"}),
            );
            let (mut stream, _) = listener.accept().map_err(|_| "Google load accept")?;
            let load = request_with_headers(&mut stream)?;
            assert_eq!(load.method, "POST");
            assert_eq!(load.route, "/v1internal:loadCodeAssist");
            assert_eq!(
                load.authorization.as_deref(),
                Some(format!("Bearer {ACCESS}").as_str())
            );
            assert_eq!(field(&load.headers, "content-type"), "application/json");
            assert_eq!(
                serde_json::from_str::<Value>(&load.body).unwrap(),
                json!({"metadata":{"ideType":"IDE_UNSPECIFIED","platform":"PLATFORM_UNSPECIFIED","pluginType":"GEMINI"}})
            );
            respond(
                &mut stream,
                json!({"allowedTiers":[{"id":"free-tier","isDefault":true}]}),
            );
            let (mut stream, _) = listener.accept().map_err(|_| "Google onboard accept")?;
            let onboard = request_with_headers(&mut stream)?;
            assert_eq!(onboard.method, "POST");
            assert_eq!(onboard.route, "/v1internal:onboardUser");
            assert_eq!(
                onboard.authorization.as_deref(),
                Some(format!("Bearer {ACCESS}").as_str())
            );
            assert_eq!(
                serde_json::from_str::<Value>(&onboard.body).unwrap(),
                json!({"tierId":"free-tier","metadata":{"ideType":"IDE_UNSPECIFIED","platform":"PLATFORM_UNSPECIFIED","pluginType":"GEMINI"}})
            );
            respond(
                &mut stream,
                json!({"done":false,"name":"operations/fixture-onboard"}),
            );
            let (mut stream, _) = listener.accept().map_err(|_| "Google operation accept")?;
            let operation = request_with_headers(&mut stream)?;
            assert_eq!(operation.method, "GET");
            assert_eq!(operation.route, "/v1internal/operations/fixture-onboard");
            assert_eq!(
                operation.authorization.as_deref(),
                Some(format!("Bearer {ACCESS}").as_str())
            );
            respond(
                &mut stream,
                json!({"done":true,"response":{"cloudaicompanionProject":{"id":"managed-fixture-project"}}}),
            );
            let (mut stream, _) = listener.accept().map_err(|_| "Google refresh accept")?;
            let refresh = request_with_headers(&mut stream)?;
            assert_eq!(refresh.method, "POST");
            assert_eq!(refresh.route, "/token");
            assert_eq!(
                field(&refresh.headers, "content-type"),
                "application/x-www-form-urlencoded"
            );
            let body = decode_form(&refresh.body);
            assert_eq!(body.len(), 4);
            assert_eq!(field(&body, "grant_type"), "refresh_token");
            assert_eq!(field(&body, "refresh_token"), REFRESH);
            assert_eq!(
                field(&body, "client_id"),
                "681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com"
            );
            assert_eq!(
                field(&body, "client_secret"),
                "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"
            );
            respond(
                &mut stream,
                json!({"access_token":ROTATED_ACCESS,"expires_in":3600,"token_type":"Bearer"}),
            );
            let (mut stream, _) = listener.accept().map_err(|_| "Google inference accept")?;
            let inference = request_with_headers(&mut stream)?;
            assert_eq!(inference.method, "POST");
            assert_eq!(inference.route, "/v1internal:streamGenerateContent?alt=sse");
            assert_eq!(
                inference.authorization.as_deref(),
                Some(format!("Bearer {ROTATED_ACCESS}").as_str())
            );
            assert!(
                !inference
                    .headers
                    .iter()
                    .any(|(name, _)| name == "x-goog-api-key")
            );
            let body: Value = serde_json::from_str(&inference.body).unwrap();
            assert_eq!(body["model"], "gemini-2.5-pro");
            assert_eq!(body["project"], "managed-fixture-project");
            assert!((1..=128).contains(&body["user_prompt_id"].as_str().unwrap().len()));
            assert_eq!(
                body["request"]["contents"],
                json!([{"role":"user","parts":[{"text":"fixture Google"}]}])
            );
            assert!(
                body["request"]["tools"][0]["functionDeclarations"]
                    .as_array()
                    .unwrap()
                    .len()
                    >= 4
            );
            assert!(body["request"].get("session_id").is_none());
            let first_identity = body["user_prompt_id"].clone();
            let events = format!(
                "data: {}\n\n",
                json!({"response":{"candidates":[{"content":{"parts":[{"functionCall":{"id":"google-plan-read","name":"read","args":{"path":"google-read.txt"}}}]},"finishReason":"STOP"}]}})
            );
            write!(stream, "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{events}", events.len()).map_err(|_| "Google tool SSE write")?;
            drop(stream);
            let (mut stream, _) = listener
                .accept()
                .map_err(|_| "Google tool followup accept")?;
            let followup = request_with_headers(&mut stream)?;
            assert_eq!(followup.method, "POST");
            assert_eq!(followup.route, "/v1internal:streamGenerateContent?alt=sse");
            assert_eq!(
                followup.authorization.as_deref(),
                Some(format!("Bearer {ROTATED_ACCESS}").as_str())
            );
            let followup: Value = serde_json::from_str(&followup.body).unwrap();
            assert_eq!(followup["model"], "gemini-2.5-pro");
            assert_eq!(followup["project"], "managed-fixture-project");
            assert_eq!(followup["user_prompt_id"], first_identity);
            let contents = followup["request"]["contents"].as_array().unwrap();
            let result = contents
                .iter()
                .find_map(|message| message["parts"][0].get("functionResponse"))
                .unwrap();
            assert_eq!(result["name"], "read");
            assert!(
                result["response"]["output"]
                    .as_str()
                    .unwrap()
                    .contains("Google tool fixture bytes")
            );
            let events = format!(
                "data: {}\n\n",
                json!({"response":{"candidates":[{"content":{"parts":[{"text":format!("Google subscription complete {ROTATED_ACCESS} {REFRESH}")}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":1,"candidatesTokenCount":2}},"traceId":"not-a-response-id"})
            );
            write!(stream, "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{events}",events.len()).map_err(|_| "Google SSE write")?;
            Ok(first_identity.as_str().unwrap().to_string())
        })();
        let _sent = finished_send.send(result);
    });
    let _server = ServerGuard {
        address,
        worker: Some(server),
    };
    let (login, _) = browser_login_for(
        "google-gemini-cli-plan",
        &home,
        &origin,
        None,
        &authorization_send,
        LoginCase::Authorized,
    );
    assert!(
        login.status.success(),
        "{}",
        String::from_utf8_lossy(&login.stderr)
    );
    let profiles = offline_output(command(&home).args([
        "auth",
        "profiles",
        "--provider",
        "google-gemini-cli-plan",
        "--fixture-issuer",
        &origin,
    ]));
    assert!(
        profiles.status.success(),
        "{}",
        String::from_utf8_lossy(&profiles.stderr)
    );
    let metadata: Value = serde_json::from_slice(&profiles.stdout).unwrap();
    let profile = metadata[0]["id"].as_str().unwrap();
    assert_eq!(metadata[0]["flow"], "GoogleInstalledPkce");
    assert_eq!(metadata[0]["subject"], "");
    assert_eq!(
        metadata[0]["code_assist_project"],
        "managed-fixture-project"
    );
    assert!(metadata[0]["workspace"].is_null());
    let eval = [
        "eval",
        "fixture Google",
        "--json",
        "--provider",
        "google-gemini-cli-plan",
        "--profile",
        profile,
        "--model",
        "gemini-2.5-pro",
        "--fixture-issuer",
        &origin,
    ];
    let evaluated = offline_output(command(&home).current_dir(&home).args(eval));
    assert!(
        evaluated.status.success(),
        "{}",
        String::from_utf8_lossy(&evaluated.stderr)
    );
    assert!(String::from_utf8_lossy(&evaluated.stdout).contains("Google subscription complete"));
    let recorded_prompt_identity = finished_receive.recv_timeout(DEADLINE).unwrap().unwrap();
    let profile_path = home
        .join("data/smith/auth/oauth-fixtures")
        .join(smith::sha256::digest_hex(origin.as_bytes()))
        .join("google-gemini-cli-plan/profiles.json");
    let persisted: Value = serde_json::from_slice(&std::fs::read(profile_path).unwrap()).unwrap();
    assert_eq!(persisted["profiles"][0]["tokens"]["refresh"], REFRESH);
    assert_eq!(persisted["profiles"][0]["tokens"]["access"], ROTATED_ACCESS);
    assert_eq!(
        persisted["profiles"][0]["metadata"]["scopes"],
        metadata[0]["scopes"]
    );
    let mut evidence = Vec::new();
    for entry in std::fs::read_dir(home.join("data/smith/sessions/run")).unwrap() {
        let path = entry.unwrap().path();
        if path.extension().is_some_and(|extension| extension == "smh") {
            evidence.extend(std::fs::read(&path).unwrap());
            let replayed = offline_output(command(&home).args([
                "sessions",
                "get",
                path.file_stem().unwrap().to_str().unwrap(),
                "--json",
            ]));
            assert!(replayed.status.success());
            evidence.extend(replayed.stdout);
            evidence.extend(replayed.stderr);
        }
    }
    assert!(
        evidence
            .windows(recorded_prompt_identity.len())
            .any(|bytes| bytes == recorded_prompt_identity.as_bytes()),
        "inference prompt identity must be recorded in session inspection"
    );
    assert_no_oauth_secret(&[
        &login.stdout,
        &login.stderr,
        &profiles.stdout,
        &evaluated.stdout,
        &evaluated.stderr,
        &evidence,
    ]);
    let logout = offline_output(command(&home).args([
        "logout",
        "--provider",
        "google-gemini-cli-plan",
        "--profile",
        profile,
        "--fixture-issuer",
        &origin,
    ]));
    assert!(logout.status.success());
    assert!(!offline_output(command(&home).args(eval)).status.success());
    std::fs::remove_dir_all(home).unwrap();
}

#[derive(Clone, Copy)]
enum GoogleSetupRejection {
    State,
    NonfreeWithoutProject,
    UnsafeOperation,
    OnboardingUncertain,
}

#[test]
fn google_rejected_setup_never_publishes_profile_or_falls_back_to_key() {
    for case in [
        GoogleSetupRejection::State,
        GoogleSetupRejection::NonfreeWithoutProject,
        GoogleSetupRejection::UnsafeOperation,
        GoogleSetupRejection::OnboardingUncertain,
    ] {
        let home =
            std::env::temp_dir().join(format!("google-rejected-{}", smith::id::SessionId::new()));
        std::fs::create_dir(&home).unwrap();
        let listener = TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0)).unwrap();
        let address = listener.local_addr().unwrap();
        let observer = listener.try_clone().unwrap();
        let origin = format!("http://{address}");
        let (authorization_send, authorization_receive) =
            mpsc::sync_channel::<Vec<(String, String)>>(1);
        let (finished_send, finished_receive) = mpsc::sync_channel(1);
        let worker = std::thread::spawn(move || {
            let result = (|| -> Result<(), String> {
                authorization_receive
                    .recv_timeout(DEADLINE)
                    .map_err(|_| "Google authorization deadline")?;
                if matches!(case, GoogleSetupRejection::State) {
                    return Ok(());
                }
                let (mut stream, _) = listener.accept().map_err(|_| "Google token accept")?;
                assert_eq!(request_with_headers(&mut stream)?.route, "/token");
                respond(
                    &mut stream,
                    json!({"access_token":ACCESS,"refresh_token":REFRESH,"expires_in":3600}),
                );
                let (mut stream, _) = listener.accept().map_err(|_| "Google load accept")?;
                assert_eq!(
                    request_with_headers(&mut stream)?.route,
                    "/v1internal:loadCodeAssist"
                );
                respond(
                    &mut stream,
                    json!({"allowedTiers":[{"id":if matches!(case,GoogleSetupRejection::NonfreeWithoutProject) {"standard-tier"} else {"free-tier"},"isDefault":true}]}),
                );
                if matches!(case, GoogleSetupRejection::NonfreeWithoutProject) {
                    return Ok(());
                }
                let (mut stream, _) = listener.accept().map_err(|_| "Google onboard accept")?;
                assert_eq!(
                    request_with_headers(&mut stream)?.route,
                    "/v1internal:onboardUser"
                );
                if matches!(case, GoogleSetupRejection::OnboardingUncertain) {
                    drop(stream);
                    return Ok(());
                }
                respond(
                    &mut stream,
                    json!({"done":false,"name":"https://evil.invalid/operations/one"}),
                );
                Ok(())
            })();
            let _sent = finished_send.send(result);
        });
        let _server = ServerGuard {
            address,
            worker: Some(worker),
        };
        let (login, _) = browser_login_for(
            "google-gemini-cli-plan",
            &home,
            &origin,
            None,
            &authorization_send,
            if matches!(case, GoogleSetupRejection::State) {
                LoginCase::StateRejected
            } else {
                LoginCase::Authorized
            },
        );
        assert!(!login.status.success());
        finished_receive.recv_timeout(DEADLINE).unwrap().unwrap();
        let profiles = offline_output(command(&home).args([
            "auth",
            "profiles",
            "--provider",
            "google-gemini-cli-plan",
            "--fixture-issuer",
            &origin,
        ]));
        assert!(profiles.status.success());
        assert_eq!(
            serde_json::from_slice::<Value>(&profiles.stdout).unwrap(),
            json!([])
        );
        let evaluated = offline_output(
            command(&home)
                .env("GOOGLE_API_KEY", "must-not-be-used")
                .args([
                    "eval",
                    "not billed",
                    "--provider",
                    "google-gemini-cli-plan",
                    "--model",
                    "gemini-2.5-pro",
                    "--fixture-issuer",
                    &origin,
                ]),
        );
        assert!(!evaluated.status.success());
        observer.set_nonblocking(true).unwrap();
        assert_eq!(
            observer.accept().unwrap_err().kind(),
            std::io::ErrorKind::WouldBlock
        );
        assert_no_oauth_secret(&[
            &login.stdout,
            &login.stderr,
            &profiles.stdout,
            &evaluated.stdout,
            &evaluated.stderr,
        ]);
        std::fs::remove_dir_all(home).unwrap();
    }
}