use std::time::Duration;
use tempfile::tempdir;
use super::super::Runs;
use super::{lines, script, settled, wired, workspace};
use crate::cli_outbound::Cli;
use crate::login::LoginView;
const BZ: &str = "#!/bin/sh\necho \"$*\" 1>&2\ncase \"$*\" in\n\
*--code=good*) echo 'logged in' 1>&2 ;;\n\
*--code=*) echo 'login failed: wrong code' 1>&2; exit 77 ;;\n\
*) echo \"https://x/authorize?state=$$\" ;;\nesac\n";
fn waiting(runs: &Runs, provider: &str) -> LoginView {
for _ in 0..600 {
let view = runs.standing(&workspace(), provider);
if view.authorize.is_some() {
return view;
}
std::thread::sleep(Duration::from_millis(5));
}
panic!("{provider}'s run never came to wait for a code");
}
#[test]
fn a_start_fires_the_bare_line_and_a_code_finishes_the_waiting_run() {
let dir = tempdir().expect("tmp");
let world = crate::test_support::world::world_under(dir.path());
let state = tempdir().expect("tmp");
let runs = Runs::of(Cli::new(script(dir.path(), "bz", BZ)));
let act = |provider: &str, code: Option<&str>, ts: &str| {
runs.start(&world, &workspace(), provider, code, state.path(), ts)
};
let receipt = act("sub@laptop", None, "100").expect("started");
assert_eq!((receipt.outcome, receipt.authorize), (None, None));
let asked = waiting(&runs, "sub@laptop");
assert_eq!(asked.outcome, None, "a URL is not a sign-in");
assert!(
lines(&asked).contains(&"--login --provider sub@laptop".to_owned()),
"{asked:?}"
);
let receipt = act("sub@laptop", Some("bad"), "101").expect("fired");
assert_eq!(
receipt.authorize, None,
"the receipt is the standing re-read"
);
let refused = settled(&runs, "sub@laptop");
assert_eq!(refused.outcome, Some(77));
let said = lines(&refused);
assert!(
said.contains(&"--login --provider sub@laptop --code=bad".to_owned()),
"{said:?}"
);
assert_eq!(
said.last().map(String::as_str),
Some("login failed: wrong code")
);
assert_eq!(said.len(), 4, "one buffer across both command lines");
let spent = act("sub@laptop", Some("good"), "102").expect_err("nothing waits");
assert!(spent.contains("/login sub@laptop"), "{spent}");
act("sub@laptop", None, "103").expect("restarted");
waiting(&runs, "sub@laptop");
act("sub@laptop", Some("good"), "104").expect("fired");
let done = settled(&runs, "sub@laptop");
assert_eq!((done.outcome, done.fallback), (Some(0), None));
assert_eq!(crate::opslog::tail(state.path(), 8).len(), 4);
}
#[test]
fn a_code_with_no_run_waiting_for_it_refuses_and_touches_nothing() {
let dir = tempdir().expect("tmp");
let world = crate::test_support::world::world_under(dir.path());
let state = tempdir().expect("tmp");
let runs = Runs::of(Cli::new("/definitely/not/a/real/bz-xyz"));
let code = |provider: &str| {
runs.start(
&world,
&workspace(),
provider,
Some("c"),
state.path(),
"100",
)
};
let nobody = code("openai").expect_err("no run");
assert!(
nobody.contains("no sign-in for \"openai\" is waiting for a code"),
"{nobody}"
);
assert_eq!(runs.standing(&workspace(), "openai"), LoginView::default());
let (run, _tx) = wired(dir.path());
let serial = runs.seat(&workspace(), "live", run, 100);
assert!(code("live").is_err(), "a live run is not a waiting one");
assert!(
runs.read_once(&workspace(), "live", serial),
"its reader still owns it: the refusal re-stamped nothing"
);
assert!(
crate::opslog::tail(state.path(), 8).is_empty(),
"nothing ran"
);
}
#[test]
fn a_second_start_replaces_a_waiting_run_with_a_new_url() {
let dir = tempdir().expect("tmp");
let world = crate::test_support::world::world_under(dir.path());
let state = tempdir().expect("tmp");
let runs = Runs::of(Cli::new(script(dir.path(), "bz", BZ)));
let start = |ts: &str| {
runs.start(&world, &workspace(), "web", None, state.path(), ts)
.expect("started")
};
start("100");
let first = waiting(&runs, "web").authorize;
let receipt = start("101");
assert_eq!(receipt.authorize, None, "the older URL went with its run");
let second = waiting(&runs, "web");
assert_ne!(second.authorize, first, "a new start is a new URL");
assert_eq!(lines(&second).len(), 2, "and a buffer that starts over");
}