use std::{os::unix::fs::PermissionsExt, path::Path};
use pretty_assertions::assert_eq;
use rho_protocol::{
methods::{
SessionPullRequestLink, SessionPullRequestList, SessionPullRequestShow,
SessionPullRequestUnlink,
},
BranchLookup, BranchPullRequest, ListedSessionPullRequests, PullRequestId, PullRequestTarget,
SessionEvent, SessionPullRequestLinkParams, SessionPullRequestListParams,
SessionPullRequestListResponse, SessionPullRequestShowParams, SessionPullRequestUnlinkParams,
SessionPullRequests,
};
use super::sessions::{Follower, Sandbox};
const PULL_REQUEST: &str = "cli/cli#14578";
const CAPTURED: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/app/pull_requests/fixtures/gh_conversation.json"
);
fn fake_gh(bin: &Path) {
std::fs::create_dir_all(bin).unwrap();
let gh = bin.join("gh");
let script = format!(
"#!/bin/sh\n\
case \"$*\" in\n\
*'p0: pullRequest'*) cat '{CAPTURED}' ;;\n\
*) sed 's/\"p0\":/\"l0\":/' '{CAPTURED}' ;;\n\
esac\n"
);
std::fs::write(&gh, script).unwrap();
std::fs::set_permissions(&gh, std::fs::Permissions::from_mode(0o755)).unwrap();
}
fn carrying(done: impl Fn(&SessionPullRequests) -> bool) -> impl Fn(&SessionEvent) -> bool {
move |event| matches!(event, SessionEvent::PullRequests { pull_requests } if done(pull_requests))
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_client_links_shows_lists_and_unlinks_a_pull_request() {
let sandbox = Sandbox::new();
let bin = sandbox.isolated.rho_home().join("fake-gh-bin");
fake_gh(&bin);
let mut command = sandbox.rho("fixed");
let path = format!("{}:{}", bin.display(), std::env::var("PATH").unwrap());
command.env("PATH", path);
let (_daemon, client) = sandbox.spawn_daemon(command, &[]).await;
let session = sandbox.new_session(&client).await;
let (mut follower, _) = Follower::attach(&sandbox, &session, Vec::new(), None).await;
let record = sandbox
.isolated
.rho_home()
.join("daemon")
.join("pull-requests")
.join(format!("{}.json", session.as_str()));
let list = async || {
let params = SessionPullRequestListParams::new(vec![session.clone()]);
client
.request::<SessionPullRequestList>(¶ms)
.await
.unwrap()
};
let linked = client
.request::<SessionPullRequestLink>(&SessionPullRequestLinkParams::new(
session.clone(),
PULL_REQUEST,
))
.await
.unwrap();
follower
.read_until(carrying(|set| set.linked.len() == 1))
.await;
let saved = record.exists();
let target = PullRequestTarget::Linked {
pull_request: PULL_REQUEST.into(),
};
let shown = client
.request::<SessionPullRequestShow>(&SessionPullRequestShowParams::new(
session.clone(),
target,
))
.await
.unwrap();
follower
.read_until(carrying(|set| {
set.linked.iter().any(|link| link.summary.is_some())
}))
.await;
let listed = list().await;
let unlinked = client
.request::<SessionPullRequestUnlink>(&SessionPullRequestUnlinkParams::new(
session.clone(),
PULL_REQUEST,
))
.await
.unwrap();
follower
.read_until(carrying(|set| set.linked.is_empty()))
.await;
let listed_after = list().await;
let id = PullRequestId::new("github.com", "cli", "cli", 14578);
let detail = shown.detail.expect("the show read the pull request");
let not_git = Some(BranchPullRequest::new(None, BranchLookup::NotGit, None));
let [mut link] = <[_; 1]>::try_from(linked.pull_requests.linked.clone()).unwrap();
let read_before = link.summary.clone();
link.summary = Some(detail.summary.clone());
let listed_as = |links| {
SessionPullRequestListResponse::new(vec![ListedSessionPullRequests::new(
session.clone(),
SessionPullRequests::new(not_git.clone(), links),
)])
};
assert_eq!(
(
(linked.pull_request, read_before, saved),
detail.summary.id.clone(),
listed,
(unlinked.pull_requests.linked, record.exists()),
listed_after,
),
(
(id.clone(), None, true),
id,
listed_as(vec![link]),
(Vec::new(), false),
listed_as(Vec::new()),
),
"log:\n{}",
sandbox.isolated.daemon_log()
);
}