use tempfile::tempdir;
use super::super::Runs;
use super::{lines, script, settled, workspace};
use crate::cli_outbound::Cli;
use crate::config_edit::brazen::{BzRunner as _, RealBzRunner};
#[test]
fn the_spawn_drops_browser_exactly_where_the_row_declares_a_device_endpoint() {
let dir = tempdir().expect("tmp");
let world = crate::test_support::world::world_under(dir.path());
let state = tempdir().expect("tmp");
let paths = crate::test_support::world::wall_paths(dir.path());
std::fs::create_dir_all(paths.config.parent().expect("the wall's brazen dir")).expect("mkdir");
std::fs::write(
&paths.config,
"[[provider]]\nname = \"acme-oauth\"\nprotocol = \"openai_chat\"\n\
base_url = \"https://acme.test\"\nauth = \"oauth2\"\n\
api_header = { name = \"Authorization\", scheme = \"bearer\" }\n\
[provider.oauth]\nclient_id = \"c\"\n\
authorize_url = \"https://acme.test/authorize\"\n\
token_url = \"https://acme.test/token\"\n\n\
[[provider]]\nname = \"openai-chatgpt\"\n",
)
.expect("write");
let bz = script(
dir.path(),
"bz",
"#!/bin/sh\nprintf '%s\\n' \"$*\" 1>&2\n\
printf 'open https://example.test/device and enter CODE-1234\\n' 1>&2\nexit 0\n",
);
let runs = Runs::of(Cli::new(bz));
let table = RealBzRunner::new(crate::world::wall::env(&world, &workspace())).catalog();
let flow = |row: &str| super::super::flow_of(&table, row);
runs.start(
&world,
&workspace(),
"openai-chatgpt",
flow("openai-chatgpt"),
state.path(),
"100",
)
.expect("started");
let headless = lines(&settled(&runs, "openai-chatgpt"));
assert_eq!(
headless[0], "--login --provider openai-chatgpt",
"no --browser: bz binds no loopback and serves the row's device flow"
);
assert!(
headless[1].contains("https://example.test/device") && headless[1].contains("CODE-1234"),
"the URL and the user code stream down the lane: {headless:?}"
);
runs.start(
&world,
&workspace(),
"acme-oauth",
flow("acme-oauth"),
state.path(),
"100",
)
.expect("started");
assert_eq!(
lines(&settled(&runs, "acme-oauth"))[0],
"--login --provider acme-oauth --browser",
"a row declaring no device endpoint keeps the loopback flow"
);
}