[client] Polls: a poll is a message, a vote replaces the voter's earlier one, only the asker closes and the close carries the final votes; the client keeps each conversation's polls
This commit is contained in:
@@ -0,0 +1,241 @@
|
||||
//! Polls (`.agents/features/polls.md`). No I/O: runs on the Android emulator too.
|
||||
|
||||
use tabellarius_client::*;
|
||||
|
||||
type C = Client<MemoryTransport>;
|
||||
|
||||
fn pair(relay: &MemoryTransport, receipts: bool) -> (C, ContactId, C, ContactId) {
|
||||
let mut eva = Client::new("Eva", relay.clone()).expect("eva");
|
||||
let mut jan = Client::new("Jan", relay.clone()).expect("jan");
|
||||
eva.set_receipts(receipts);
|
||||
jan.set_receipts(receipts);
|
||||
let invite = eva.create_invite().expect("invite");
|
||||
let jan_side = jan.accept_invite(&invite).expect("accept");
|
||||
let Some(Event::Joined {
|
||||
contact: eva_side, ..
|
||||
}) = eva.poll().expect("poll").first().cloned()
|
||||
else {
|
||||
panic!("no join")
|
||||
};
|
||||
jan.poll().expect("poll");
|
||||
(eva, eva_side, jan, jan_side)
|
||||
}
|
||||
|
||||
fn strings(xs: &[&str]) -> Vec<String> {
|
||||
xs.iter().map(|x| x.to_string()).collect()
|
||||
}
|
||||
|
||||
fn send(c: &mut C, out: Result<Outgoing, ClientError>) {
|
||||
let out = out.expect("compose");
|
||||
c.deliver(&out).expect("deliver");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_poll_arrives_votes_change_and_the_close_gives_both_sides_the_same_result() {
|
||||
let relay = MemoryTransport::default();
|
||||
let (mut eva, eva_side, mut jan, jan_side) = pair(&relay, false);
|
||||
eva.send(eva_side, "Ahoj").expect("send");
|
||||
let options = strings(&["Italská", "Thajská", "Doma"]);
|
||||
let out = eva.compose_poll(eva_side, "Kam na večeři?", &options, true);
|
||||
send(&mut eva, out);
|
||||
assert_eq!(eva.sent_seq(eva_side), Some(2), "a poll is a message");
|
||||
let poll = MessageRef {
|
||||
author: eva.author(),
|
||||
seq: 2,
|
||||
};
|
||||
let got = jan.poll().expect("poll");
|
||||
assert_eq!(
|
||||
got[1],
|
||||
Event::Poll {
|
||||
contact: jan_side,
|
||||
seq: 2,
|
||||
question: "Kam na večeři?".into(),
|
||||
options: options.clone(),
|
||||
multiple: true,
|
||||
}
|
||||
);
|
||||
// Jan votes, changes his mind, takes it back, votes again; Eva votes in her own poll.
|
||||
for choices in [&[0u8][..], &[0, 1], &[], &[1, 2]] {
|
||||
let out = jan.vote(jan_side, poll, choices);
|
||||
send(&mut jan, out);
|
||||
}
|
||||
let out = eva.vote(eva_side, poll, &[1]);
|
||||
send(&mut eva, out);
|
||||
let votes: Vec<Vec<u8>> = eva
|
||||
.poll()
|
||||
.expect("poll")
|
||||
.into_iter()
|
||||
.map(|e| match e {
|
||||
Event::Vote {
|
||||
contact,
|
||||
poll: p,
|
||||
choices,
|
||||
} if contact == eva_side && p == poll => choices,
|
||||
other => panic!("{other:?}"),
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(votes, vec![vec![0], vec![0, 1], vec![], vec![1, 2]]);
|
||||
assert_eq!(
|
||||
jan.poll().expect("poll"),
|
||||
vec![Event::Vote {
|
||||
contact: jan_side,
|
||||
poll,
|
||||
choices: vec![1]
|
||||
}]
|
||||
);
|
||||
// A poll can be answered and reacted to like a text.
|
||||
let out = jan.compose_text(jan_side, "Thajská!", Some(poll));
|
||||
send(&mut jan, out);
|
||||
let out = jan.react(jan_side, poll, Some("👍"));
|
||||
send(&mut jan, out);
|
||||
assert_eq!(eva.poll().expect("poll").len(), 2);
|
||||
|
||||
// Jan cannot close Eva's poll; Eva closes it.
|
||||
assert_eq!(
|
||||
jan.close_poll(jan_side, poll).err(),
|
||||
Some(ClientError::BadPoll)
|
||||
);
|
||||
let out = eva.close_poll(eva_side, poll);
|
||||
send(&mut eva, out);
|
||||
let final_ = vec![
|
||||
Ballot {
|
||||
author: jan.author(),
|
||||
choices: vec![1, 2],
|
||||
},
|
||||
Ballot {
|
||||
author: eva.author(),
|
||||
choices: vec![1],
|
||||
},
|
||||
];
|
||||
let got = jan.poll().expect("poll");
|
||||
let [Event::PollClosed {
|
||||
contact,
|
||||
poll: p,
|
||||
ballots,
|
||||
}] = got.as_slice()
|
||||
else {
|
||||
panic!("{got:?}")
|
||||
};
|
||||
assert_eq!((*contact, *p), (jan_side, poll));
|
||||
let mut sorted = ballots.clone();
|
||||
sorted.sort_by_key(|b| b.author.as_bytes().to_vec());
|
||||
let mut expected = final_.clone();
|
||||
expected.sort_by_key(|b| b.author.as_bytes().to_vec());
|
||||
assert_eq!(sorted, expected);
|
||||
assert_eq!(
|
||||
jan.vote(jan_side, poll, &[0]).err(),
|
||||
Some(ClientError::PollClosed)
|
||||
);
|
||||
assert_eq!(
|
||||
eva.vote(eva_side, poll, &[0]).err(),
|
||||
Some(ClientError::PollClosed)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_vote_that_crosses_the_close_is_counted_nowhere() {
|
||||
let relay = MemoryTransport::default();
|
||||
let (mut eva, eva_side, mut jan, jan_side) = pair(&relay, false);
|
||||
let out = eva.compose_poll(eva_side, "Pátek?", &strings(&["ano", "ne"]), false);
|
||||
send(&mut eva, out);
|
||||
jan.poll().expect("poll");
|
||||
let poll = MessageRef {
|
||||
author: eva.author(),
|
||||
seq: 1,
|
||||
};
|
||||
let out = jan.vote(jan_side, poll, &[0]);
|
||||
send(&mut jan, out);
|
||||
eva.poll().expect("poll");
|
||||
// Both at once: Jan changes his vote while Eva closes.
|
||||
let out = jan.vote(jan_side, poll, &[1]);
|
||||
send(&mut jan, out);
|
||||
let out = eva.close_poll(eva_side, poll);
|
||||
send(&mut eva, out);
|
||||
|
||||
assert_eq!(
|
||||
eva.poll().expect("poll"),
|
||||
vec![],
|
||||
"the late vote is dropped"
|
||||
);
|
||||
let got = jan.poll().expect("poll");
|
||||
assert_eq!(
|
||||
got,
|
||||
vec![Event::PollClosed {
|
||||
contact: jan_side,
|
||||
poll,
|
||||
ballots: vec![Ballot {
|
||||
author: jan.author(),
|
||||
choices: vec![0]
|
||||
}],
|
||||
}],
|
||||
"Jan's result is Eva's: his first vote, not the one that crossed"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_receipt_covers_a_poll() {
|
||||
let relay = MemoryTransport::default();
|
||||
let (mut eva, eva_side, mut jan, _) = pair(&relay, true);
|
||||
let out = eva.compose_poll(eva_side, "Pivo?", &strings(&["ano", "jasně"]), false);
|
||||
send(&mut eva, out);
|
||||
let got = jan.poll().expect("poll");
|
||||
assert!(
|
||||
matches!(got.as_slice(), [Event::Poll { seq: 1, .. }]),
|
||||
"{got:?}"
|
||||
);
|
||||
assert!(eva.poll().expect("poll").contains(&Event::Delivered {
|
||||
contact: eva_side,
|
||||
upto: 1
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_poll_survives_a_restart_open_or_closed_with_its_votes() {
|
||||
let relay = MemoryTransport::default();
|
||||
let (mut eva, eva_side, mut jan, jan_side) = pair(&relay, false);
|
||||
let out = eva.compose_poll(eva_side, "Kdy?", &strings(&["pá", "so", "ne"]), true);
|
||||
send(&mut eva, out);
|
||||
let out = eva.compose_poll(eva_side, "Kde?", &strings(&["u nás", "u vás"]), false);
|
||||
send(&mut eva, out);
|
||||
jan.poll().expect("poll");
|
||||
let when = MessageRef {
|
||||
author: eva.author(),
|
||||
seq: 1,
|
||||
};
|
||||
let where_ = MessageRef {
|
||||
author: eva.author(),
|
||||
seq: 2,
|
||||
};
|
||||
let out = jan.vote(jan_side, when, &[0, 2]);
|
||||
send(&mut jan, out);
|
||||
eva.poll().expect("poll");
|
||||
let out = eva.close_poll(eva_side, where_);
|
||||
send(&mut eva, out);
|
||||
jan.poll().expect("poll");
|
||||
|
||||
let mut eva = C::restore(&eva.snapshot().expect("snapshot"), relay.clone()).expect("eva");
|
||||
let mut jan = C::restore(&jan.snapshot().expect("snapshot"), relay.clone()).expect("jan");
|
||||
// Closed stays closed, on both sides.
|
||||
assert_eq!(
|
||||
jan.vote(jan_side, where_, &[0]).err(),
|
||||
Some(ClientError::PollClosed)
|
||||
);
|
||||
assert_eq!(
|
||||
eva.close_poll(eva_side, where_).err(),
|
||||
Some(ClientError::PollClosed)
|
||||
);
|
||||
// The open one keeps Jan's vote: Eva's close after the restart carries it.
|
||||
let out = eva.close_poll(eva_side, when);
|
||||
send(&mut eva, out);
|
||||
assert_eq!(
|
||||
jan.poll().expect("poll"),
|
||||
vec![Event::PollClosed {
|
||||
contact: jan_side,
|
||||
poll: when,
|
||||
ballots: vec![Ballot {
|
||||
author: jan.author(),
|
||||
choices: vec![0, 2]
|
||||
}],
|
||||
}]
|
||||
);
|
||||
}
|
||||
@@ -214,6 +214,13 @@ fn pair_and_talk_through_a_real_relay_which_never_sees_a_name_or_a_word() {
|
||||
};
|
||||
let out = jan.react(jan_side, mine, Some(emoji)).expect("react");
|
||||
jan.deliver(&out).expect("deliver");
|
||||
// And a poll (F28): its question and options neither.
|
||||
let question = "POLL-QUESTION-CANARY";
|
||||
let options = vec!["OPTION-CANARY-A".to_string(), "OPTION-CANARY-B".to_string()];
|
||||
let out = jan
|
||||
.compose_poll(jan_side, question, &options, false)
|
||||
.expect("poll");
|
||||
jan.deliver(&out).expect("deliver");
|
||||
|
||||
// Snapshot the database while both messages wait, before anyone fetches.
|
||||
let waiting = std::fs::read(&db).expect("db");
|
||||
@@ -240,6 +247,13 @@ fn pair_and_talk_through_a_real_relay_which_never_sees_a_name_or_a_word() {
|
||||
to: mine,
|
||||
emoji: Some(emoji.into()),
|
||||
},
|
||||
Event::Poll {
|
||||
contact: eva_side,
|
||||
seq: 2,
|
||||
question: question.into(),
|
||||
options: options.clone(),
|
||||
multiple: false,
|
||||
},
|
||||
]
|
||||
);
|
||||
let after = std::fs::read(&db).expect("db");
|
||||
@@ -248,6 +262,9 @@ fn pair_and_talk_through_a_real_relay_which_never_sees_a_name_or_a_word() {
|
||||
for (what, needle) in [
|
||||
("canary", canary.as_bytes()),
|
||||
("reaction", emoji.as_bytes()),
|
||||
("poll question", question.as_bytes()),
|
||||
("poll option", options[0].as_bytes()),
|
||||
("poll option", options[1].as_bytes()),
|
||||
("inviter's name", "Eva Nováková".as_bytes()),
|
||||
("invitee's name", "Jan Dvořák".as_bytes()),
|
||||
("invite secret", invite.secret.as_bytes().as_slice()),
|
||||
|
||||
Reference in New Issue
Block a user