use super::*;
use crate::cli_outbound::Cli;
use serde_json::json;
use tempfile::tempdir;
#[test]
fn a_follow_resolves_its_address_under_the_callers_scope() {
let root = tempdir().unwrap();
let data = tempdir().unwrap();
let ctx = over(
root.path(),
world_of(data.path(), &["home", "corp"]),
data.path().to_path_buf(),
Cli::new("/no/such/litany"),
);
let phone = seat("phone");
crate::registry::register(root.path(), &phone.client, "home").unwrap();
let follow = |workspace: &str, agent: &str| json!({"op": "follow", "workspace": workspace, "agent": agent});
assert!(ctx.follow(&phone, &follow("corp", "c-1")).is_none());
assert!(ctx.follow(&phone, &follow("home", "nobody")).is_none());
let said = |workspace: &str| {
ctx.answer_as(&phone, &follow(workspace, "c-1"))["error"]
.as_str()
.unwrap_or_default()
.replace(workspace, "<name>")
};
assert_eq!(
said("corp"),
said("nowhere"),
"absence, not a scope error: a workspace this seat is not seated in \
refuses in the identical bytes one nobody founded earns"
);
}
#[test]
fn nothing_but_a_held_lane_is_a_stream() {
let root = tempdir().unwrap();
let ctx = ctx(root.path());
for request in [
json!({"op": "workspaces"}),
json!({"op": "teleport"}),
json!("not even an object"),
] {
assert!(ctx.follow(&seat("phone"), &request).is_none());
}
}
#[test]
fn an_attention_ask_opens_a_lane_for_any_seat() {
let root = tempdir().unwrap();
let ctx = ctx(root.path());
let request = json!({"op": "attention"});
let unseated = seat("phone");
let mut lane = ctx
.follow(&unseated, &request)
.expect("attention is follow-class");
assert_eq!(
lane.next(),
Some(json!({"ok": true, "kind": "attention", "rows": []})),
"the answer as of connect, in the same bytes a one-frame read carries"
);
let intake = crate::wire::intake::Intake::new(std::sync::Arc::new(ctx));
assert_eq!(
crate::wire::server::Answerer::answer(&intake, &unseated, request)
.take(1)
.count(),
1,
"the lane's first frame is written as it is produced"
);
}
#[test]
fn a_resolvable_follow_opens_a_stream_rather_than_answering_once() {
let root = tempdir().unwrap();
let data = tempdir().unwrap();
let mut snap = world_of(data.path(), &["home"]);
let ws = crate::binding::workspace_path(data.path(), "home");
snap.trees.insert(
ws,
crate::git_tree::GitTree {
commits: vec![],
agents: vec![crate::boundary::tests::agent(
"c-1",
crate::git_tree::AgentState::Quiescent,
100,
)],
},
);
let ctx = over(
root.path(),
snap,
data.path().to_path_buf(),
Cli::new("/no/such/litany"),
);
let phone = seat("phone");
crate::registry::register(root.path(), &phone.client, "home").unwrap();
let request = json!({"op": "follow", "workspace": "home", "agent": "c-1"});
assert!(
ctx.follow(&phone, &request).is_some(),
"the address resolved, so there is a stream"
);
let intake = crate::wire::intake::Intake::new(std::sync::Arc::new(ctx));
assert_eq!(
crate::wire::server::Answerer::answer(&intake, &phone, request)
.take(0)
.count(),
0,
"the follow arm hands the stream through untouched"
);
assert_eq!(
crate::wire::server::Answerer::answer(&intake, &phone, json!({"op": "workspaces"})).count(),
1,
"an ordinary read is one frame"
);
}
#[test]
fn a_sign_in_lane_opens_a_stream_and_its_first_frame_is_the_standing() {
let root = tempdir().expect("tmp");
let data = tempdir().expect("tmp");
let ctx = over(
root.path(),
world_of(data.path(), &["home"]),
data.path().to_path_buf(),
crate::cli_outbound::Cli::new("/no/such/litany"),
);
let phone = seat("phone");
crate::registry::register(root.path(), &phone.client, "home").expect("registered");
let request = json!({"op": "login-tail", "workspace": "home", "provider": "acme"});
let mut frames = ctx.follow(&phone, &request).expect("the address resolved");
assert_eq!(
frames.next(),
Some(json!({"ok": true, "kind": "login", "lines": []})),
"the lane opens on the standing"
);
let elsewhere = json!({"op": "login-tail", "workspace": "corp", "provider": "acme"});
assert!(ctx.follow(&phone, &elsewhere).is_none());
}