use std::path::PathBuf;
use std::time::Duration;
use tempfile::tempdir;
use super::super::Frame;
use super::{ask, deps_with, lane, script, settle, wired, workspace};
use crate::boundary::Action;
use crate::boundary::dispatch::{Deps, dispatch};
use crate::boundary::reply::Reply;
use crate::cli_outbound::{Chunk, ExitInfo};
use crate::login::runs::Runs;
use crate::ui_state::UiState;
fn sign_in(deps: &Deps, provider: &str, code: Option<&str>) -> Result<Reply, String> {
let mut ui = UiState::open(PathBuf::from("/nonexistent/ui.json"));
let act = Action::Login {
workspace: crate::naming::leaf(&workspace()),
provider: provider.to_owned(),
code: code.map(str::to_owned),
};
dispatch(deps, &mut ui, "100", &act)
}
#[test]
fn a_far_row_is_fired_as_a_bare_one_and_its_code_goes_to_the_waiting_run() {
let dir = tempdir().expect("tmp");
let bz = script(
dir.path(),
"bz",
"#!/bin/sh\necho \"$*\" 1>&2\ncase \"$*\" in\n\
*--code=*) echo 'logged in' 1>&2 ;;\n\
*) echo 'https://x/authorize?state=s' ;;\nesac\n",
);
let deps = deps_with(dir.path(), &bz);
let said =
|view: &crate::login::LoginView, line: &str| view.lines.iter().any(|l| l.text == line);
assert!(sign_in(&deps, "web@laptop", None).is_ok());
let mut asked = ask(&deps, "web@laptop");
for _ in 0..600 {
if asked.authorize.is_some() {
break;
}
std::thread::sleep(Duration::from_millis(5));
asked = ask(&deps, "web@laptop");
}
assert_eq!(
asked.authorize.as_deref(),
Some("https://x/authorize?state=s")
);
assert_eq!(asked.outcome, None, "a URL is not a sign-in");
assert!(said(&asked, "--login --provider web@laptop"), "{asked:?}");
let Ok(Reply::Login(receipt)) = sign_in(&deps, "web@laptop", Some("abc#def")) else {
panic!("the finishing act answers a standing");
};
assert_eq!(receipt.authorize, None);
let done = settle(&deps, "web@laptop");
assert_eq!(done.outcome, Some(0));
assert!(
said(&done, "--login --provider web@laptop --code=abc#def"),
"{done:?}"
);
let refusal = sign_in(&deps, "other", Some("abc")).expect_err("nothing waits");
assert!(refusal.contains("waiting for a code"), "{refusal}");
}
#[test]
fn a_lane_says_a_run_came_to_wait_for_a_code_once() {
let dir = tempdir().expect("tmp");
let runs = Runs::default();
let (run, tx) = wired(dir.path());
let serial = runs.seat(&workspace(), "web", run, 0);
let mut held = lane(&runs, "web");
assert!(
matches!(held.poll(), Frame::Ready(_)),
"it opens on the run"
);
tx.send(Chunk::Stdout(b"https://x/authorize\n".to_vec()))
.expect("send");
assert!(runs.read_once(&workspace(), "web", serial));
let Frame::Ready(line) = held.poll() else {
panic!("an append is a frame");
};
assert_eq!((line.lines.len(), line.authorize), (1, None));
tx.send(Chunk::Exited(ExitInfo::Code(0))).expect("send");
assert!(
!runs.read_once(&workspace(), "web", serial),
"a waiting run has nothing left to read"
);
let Frame::Ready(asking) = held.poll() else {
panic!("coming to wait is a frame, with nothing appended");
};
assert!(asking.lines.is_empty());
assert_eq!(asking.authorize.as_deref(), Some("https://x/authorize"));
assert_eq!(asking.outcome, None);
assert!(matches!(held.poll(), Frame::Waiting), "said once");
let Some(Reply::Login(replayed)) = lane(&runs, "web").next() else {
panic!("a later lane opens on the standing");
};
assert_eq!(replayed.lines.len(), 1);
assert_eq!(replayed.authorize.as_deref(), Some("https://x/authorize"));
}