craft-esign close context, idempotency and busy-ownership gaps in the dialog

A reply from a replaced backend could overwrite capabilities for the new
one. A reply for an older document revision could install under a reopened
dialog. A send request taken before a reopen could tag its reply with the
wrong context. A duplicate answer from a backend holding a second Replier
could reinstall state the dialog had already moved past. A superseded
status refresh could overwrite a newer one. A failed lookup could clear
busy for a create that was still running.

Placements and the draft idempotency key could also survive a document
change or a field edit that should have invalidated them. A contact with no
email address could become a recipient using its share id as the address.
Two dialog instances could mint the same idempotency key for the same
document. An unlinked account showed a disabled Send button with no
explanation.

The context model now carries the document revision and an opening counter
alongside the document id and account. Pending-send context is captured at
queue time and consumed by the eventual submit or result call. A late reply
or result keeps the context it was built under. Each fix adds a test that
fails without it.
This commit is contained in:
William Dunn committed 2026-11-17 21:03:40 -05:00
1 parent 98bfed94c2
commit b0b5c2bf17
3 files changed
+473 -33

No files matched your search

+180
View File
@@ -0,0 +1,180 @@
//! Regression tests for the CodeRabbit findings reviewed on PR #661,
//! reproduced against ef496161 (storytold/craft-libs#661).
//!
//! Reuses `review_repro`'s `Hold` backend and helpers: a test chooses when,
//! and in what order, a held request is answered.
use craft_esign::{Envelope, EnvelopeStatus};
use super::review_repro::{envelope_for, ready, send, who};
use super::*;
/// A document's revision changing is itself a context change: a reply for
/// the bytes under the old `document_etag` must not install under the new
/// one, even though the document id never changed.
#[test]
fn stale_create_reply_for_old_revision_is_dropped_after_reopen_with_new_etag() {
let (mut dialog, hold) = ready();
dialog.open("doc-A".into(), "A.pdf".into(), Some("etag-1".into()), who("alice"));
send(&mut dialog, b"A bytes etag1");
dialog.open("doc-A".into(), "A.pdf".into(), Some("etag-2".into()), who("alice"));
let (_, create) = hold.take(|r| matches!(r, Request::Create { .. }));
create.answer(Answer::Created(envelope_for("doc-A", "alice")));
dialog.pump(1.0);
assert!(
dialog.envelope.is_none(),
"stale reply for etag-1 installed into the etag-2 opening: {:?}",
dialog.envelope.as_ref().map(|e| e.id.clone())
);
}
/// `take_send_request` already handed the application doc-A's draft; the
/// dialog then reopens for doc-B before the application calls
/// `submit_request`. The reply this produces must still be tagged with
/// doc-A's context, not doc-B's.
#[test]
fn create_reply_for_a_request_taken_before_reopening_does_not_install_in_the_new_document() {
let (mut dialog, hold) = ready();
dialog.open("doc-A".into(), "A.pdf".into(), None, who("alice"));
dialog.add_email("bob@example.com".into());
dialog.queue_send();
let out = dialog.take_send_request().expect("send produced a request");
// The dialog moves on before the application gets around to submitting
// what it already took.
dialog.open("doc-B".into(), "B.pdf".into(), None, who("alice"));
dialog.submit_request(Request::Create { draft: Box::new(out.draft), document: b"A bytes".to_vec(), idempotency_key: out.idempotency_key, send: true });
let (_, create) = hold.take(|r| matches!(r, Request::Create { .. }));
create.answer(Answer::Created(envelope_for("doc-A", "alice")));
dialog.pump(1.0);
assert!(
dialog.envelope.is_none(),
"doc-A's envelope, submitted after reopening doc-B, installed into doc-B's dialog: {:?}",
dialog.envelope.as_ref().map(|e| e.id.clone())
);
}
/// An untaken `SendRequest` is specific to the context that queued it; it
/// must not survive into a reopened dialog for another document.
#[test]
fn an_untaken_send_request_does_not_survive_reopening_for_another_document() {
let (mut dialog, _hold) = ready();
dialog.open("doc-A".into(), "A.pdf".into(), None, who("alice"));
dialog.add_email("bob@example.com".into());
dialog.queue_send();
dialog.open("doc-B".into(), "B.pdf".into(), None, who("alice"));
assert!(dialog.take_send_request().is_none(), "doc-B's dialog still held doc-A's untaken send request");
}
/// `note_create_result` is the synchronous twin of `submit_request`: an app
/// that handles create itself, rather than through the backend channel,
/// reports the outcome here. It must drop a result for a context the dialog
/// has already left, exactly as a channel reply would.
#[test]
fn note_create_result_for_a_previous_document_does_not_install_in_the_reopened_dialog() {
let (mut dialog, _hold) = ready();
dialog.open("doc-A".into(), "A.pdf".into(), None, who("alice"));
dialog.add_email("bob@example.com".into());
dialog.queue_send();
let _out = dialog.take_send_request().expect("send produced a request");
dialog.open("doc-B".into(), "B.pdf".into(), None, who("alice"));
dialog.note_create_result(Ok(envelope_for("doc-A", "alice")));
assert!(
dialog.envelope.is_none(),
"doc-A's result, reported after reopening doc-B, installed into doc-B's dialog: {:?}",
dialog.envelope.as_ref().map(|e| e.id.clone())
);
}
/// `Replier::dup` is the only way a backend can answer one sequence twice.
/// The dialog answers the first reply normally, but a second reply for the
/// same (already resolved) sequence must drop unconditionally, not reach
/// the match arms with no context left to check against.
#[test]
fn a_duplicated_answer_for_an_already_resolved_sequence_is_dropped() {
let (mut dialog, hold) = ready();
dialog.open("doc-A".into(), "A.pdf".into(), None, who("alice"));
send(&mut dialog, b"A bytes");
let (_, create) = hold.take(|r| matches!(r, Request::Create { .. }));
let dup = create.dup();
create.answer(Answer::Created(envelope_for("doc-A", "alice")));
dialog.pump(1.0);
assert_eq!(dialog.envelope.as_ref().map(|e| e.id.clone()), Some("env-doc-A".to_owned()), "the first answer should have installed normally");
// The dialog moves on; a backend still holding `dup` from the first
// request answers it again.
dialog.open("doc-B".into(), "B.pdf".into(), None, who("alice"));
dup.answer(Answer::Created(envelope_for("doc-A", "alice")));
dialog.pump(2.0);
let shown = dialog.envelope.as_ref().and_then(|e| e.document_id.clone());
assert!(
shown.is_none() || shown.as_deref() == Some("doc-B"),
"a duplicate answer for the first request reinstalled doc-A's envelope into doc-B's dialog: {shown:?}"
);
}
/// `busy` belongs to whichever operation is still pending. A lookup that
/// fails while a create is genuinely in flight must not report the create
/// as finished.
#[test]
fn a_failed_lookup_does_not_clear_busy_for_a_pending_create() {
let (mut dialog, hold) = ready();
dialog.open("doc-A".into(), "A.pdf".into(), None, who("alice"));
send(&mut dialog, b"A bytes");
assert!(dialog.busy, "queue_send should have left the dialog busy");
dialog.query = "ca".into();
dialog.query_changed_at = 0.0;
dialog.maybe_lookup(1.0);
let (_, lookup) = hold.take(|r| matches!(r, Request::Lookup { .. }));
lookup.refuse("lookup failed");
dialog.pump(2.0);
assert!(dialog.busy, "a failed lookup cleared busy while the create for doc-A was still pending");
}
/// Two "Refresh status" clicks before either reply arrives race two
/// `Request::Status` calls. If the older one answers after the newer one
/// already installed, it must not overwrite the newer status.
#[test]
fn an_older_status_reply_does_not_overwrite_a_newer_one() {
let (mut dialog, hold) = ready();
dialog.open("doc-A".into(), "A.pdf".into(), None, who("alice"));
send(&mut dialog, b"A bytes");
let (_, create) = hold.take(|r| matches!(r, Request::Create { .. }));
create.answer(Answer::Created(envelope_for("doc-A", "alice")));
dialog.pump(1.0);
let env_id = dialog.envelope.as_ref().expect("created").id.clone();
dialog.busy = true;
let seq1 = dialog.submit_in_context(Request::Status { envelope: env_id.clone() });
dialog.pending_op_seq = Some(seq1);
dialog.status_seq = seq1;
dialog.busy = true;
let seq2 = dialog.submit_in_context(Request::Status { envelope: env_id.clone() });
dialog.pending_op_seq = Some(seq2);
dialog.status_seq = seq2;
let (_, status1) = hold.take(|r| matches!(r, Request::Status { .. }));
let (_, status2) = hold.take(|r| matches!(r, Request::Status { .. }));
// The newer request (seq2) answers first.
status2.answer(Answer::Status(Envelope { status: EnvelopeStatus::Completed, ..envelope_for("doc-A", "alice") }));
dialog.pump(2.0);
assert_eq!(dialog.envelope.as_ref().map(|e| e.status), Some(EnvelopeStatus::Completed));
// The older request (seq1), issued first, answers last.
status1.answer(Answer::Status(Envelope { status: EnvelopeStatus::Sent, ..envelope_for("doc-A", "alice") }));
dialog.pump(3.0);
assert_eq!(dialog.envelope.as_ref().map(|e| e.status), Some(EnvelopeStatus::Completed), "an older status reply overwrote a newer one");
}
+285 -27
View File
@@ -14,6 +14,8 @@ use egui::{Context, RichText, Ui};
mod backend;
#[cfg(test)]
mod coderabbit_repro;
#[cfg(test)]
mod review_repro;
pub use backend::{Answer, EsignBackend, Replier, Request};
@@ -27,15 +29,24 @@ const RETRY: f64 = 20.0;
type Inbox = (u64, Result<Answer, String>);
/// What dialog state a reply belongs to: which document, under which
/// signed-in account. A reply installs only when this still matches the
/// dialog's current context; see [`EsignDialog::submit_in_context`] and
/// [`EsignDialog::pump`]. Deliberately excludes incidental UI state such as
/// search text, which changes without the dialog meaning something new.
/// What dialog state a reply belongs to: which document revision, under
/// which signed-in account, opened which time. A reply installs only when
/// this still matches the dialog's current context; see
/// [`EsignDialog::submit_in_context`] and [`EsignDialog::pump`]. Deliberately
/// excludes incidental UI state such as search text, which changes without
/// the dialog meaning something new.
///
/// `opening` is bumped every time [`EsignDialog::open`] actually changes
/// something (document, revision or account). `document_etag` alone would
/// already separate two revisions of the same document id, but it cannot
/// separate "document A, closed, then A opened again" from the first
/// opening: both carry the same id and the same etag. `opening` does.
#[derive(Clone, Debug, PartialEq, Eq)]
struct EsignContext {
document_id: String,
document_etag: Option<String>,
account: Option<String>,
opening: u64,
}
/// Every string the dialog shows.
@@ -57,6 +68,9 @@ pub struct EsignLabels {
pub no_service: String,
pub sign_in_first: String,
pub need_recipient: String,
/// Shown instead of adding the person, when a search match has no email
/// address the provider can invite.
pub contact_missing_email: String,
pub status: String,
pub void: String,
pub remind: String,
@@ -83,6 +97,7 @@ impl Default for EsignLabels {
no_service: "Signatures cannot be requested here.".into(),
sign_in_first: "Sign in to request signatures.".into(),
need_recipient: "Add at least one signer.".into(),
contact_missing_email: "This contact has no email address to invite.".into(),
status: "Status".into(),
void: "Void".into(),
remind: "Remind".into(),
@@ -120,6 +135,23 @@ pub struct EsignDialog {
/// Context (document plus account) each in-flight context-sensitive
/// request was issued under. Consulted, then removed, in [`Self::pump`].
contexts: HashMap<u64, EsignContext>,
/// Sequences whose context has already been consulted once in
/// [`Self::pump`], bounded to the most recent entries. A backend that
/// answers the same sequence twice (via [`Replier::dup`]) has its second
/// answer dropped unconditionally instead of re-entering the match arms
/// with no context to check.
consumed: std::collections::HashSet<u64>,
consumed_order: std::collections::VecDeque<u64>,
/// The sequence that currently owns `busy`, if any. An answer (success or
/// failure) for any other sequence must not clear `busy` on its behalf.
pending_op_seq: Option<u64>,
/// The most recently issued `Request::Status` sequence. An older status
/// reply that arrives after a newer one has already installed is
/// superseded and dropped.
status_seq: u64,
/// How many times [`Self::open`] has actually changed something. Part of
/// [`EsignContext`]; see its docs.
opening: u64,
document_id: String,
document_etag: Option<String>,
document_title: String,
@@ -137,6 +169,12 @@ pub struct EsignDialog {
error: Option<String>,
envelope: Option<Envelope>,
send_out: Option<SendRequest>,
/// The context [`Self::queue_send`] captured for the still-outstanding
/// [`SendRequest`], consumed by [`Self::submit_request`] or
/// [`Self::note_create_result`] so a reply is tagged with the context the
/// send was queued under, not whatever context is current when the
/// application finally submits it.
pending_send_context: Option<EsignContext>,
completed_bytes: Option<Vec<u8>>,
account_fingerprint: Option<String>,
}
@@ -159,6 +197,11 @@ impl Default for EsignDialog {
rx,
newest: 0,
contexts: HashMap::new(),
consumed: std::collections::HashSet::new(),
consumed_order: std::collections::VecDeque::new(),
pending_op_seq: None,
status_seq: 0,
opening: 0,
document_id: String::new(),
document_etag: None,
document_title: String::new(),
@@ -176,12 +219,28 @@ impl Default for EsignDialog {
error: None,
envelope: None,
send_out: None,
pending_send_context: None,
completed_bytes: None,
account_fingerprint: None,
}
}
}
/// A send operation id no other send in this process has used, from a random
/// start so one saved by an earlier process is unlikely to repeat either.
/// Mirrors `docking::model::fresh_window_id`: a counter kept on one
/// [`EsignDialog`] repeats across two dialog instances (two windows, or one
/// replaced after a document closes), and two sends sharing an idempotency
/// key have a conforming provider return the first envelope for the second.
fn fresh_send_id() -> u64 {
use std::hash::{BuildHasher, Hasher};
use std::sync::OnceLock;
use std::sync::atomic::{AtomicU64, Ordering};
static NEXT: OnceLock<AtomicU64> = OnceLock::new();
let next = NEXT.get_or_init(|| AtomicU64::new(std::collections::hash_map::RandomState::new().build_hasher().finish() >> 1));
next.fetch_add(1, Ordering::Relaxed)
}
impl EsignDialog {
pub fn new() -> EsignDialog {
EsignDialog::default()
@@ -213,17 +272,29 @@ impl EsignDialog {
let fingerprint = format!("{}@{}", sender.id, sender.email.as_deref().unwrap_or(""));
let account_changed = self.account_fingerprint.as_deref() != Some(fingerprint.as_str());
let document_changed = self.document_id != document_id;
if account_changed || document_changed {
let revision_changed = self.document_etag != document_etag;
if account_changed || document_changed || revision_changed {
self.recipients.clear();
self.message.clear();
self.envelope = None;
self.completed_bytes = None;
self.error = None;
self.draft_idempotency_key = None;
// A's fields must never reappear as B's: see the Major finding
// this guards against.
self.placements.clear();
// An untaken send request was built for the context being left;
// drop it so a later `take_send_request` cannot hand the
// application A's draft to submit under B's dialog.
// `pending_send_context`, captured once the request is taken, is
// deliberately left alone here: see its field doc.
self.send_out = None;
self.opening = self.opening.wrapping_add(1);
// Any job still in flight was issued for the context being left.
// Its reply will be dropped in `pump` (context no longer
// matches), so nothing will ever clear `busy` on its behalf.
self.busy = false;
self.pending_op_seq = None;
}
self.account_fingerprint = Some(fingerprint);
self.document_id = document_id;
@@ -242,6 +313,10 @@ impl EsignDialog {
/// Field placements the app prepared (page-normalised). Replaces any prior set.
pub fn set_placements(&mut self, placements: Vec<FieldPlacement>) {
self.placements = placements;
// Same reasoning as `add_contact`/`add_email`/removing a recipient: the
// fields a retried create would send have changed, so a key from
// before this call must not be reused for them.
self.draft_idempotency_key = None;
}
/// What Send asked the application to encode and submit, if anything.
@@ -251,8 +326,20 @@ impl EsignDialog {
/// Submit a request through the host backend (for example after the app
/// encoded document bytes for a [`SendRequest`]).
///
/// Tags the reply with the context [`Self::queue_send`] captured when it
/// built the [`SendRequest`] being submitted, not whatever context is
/// current now. The application can encode bytes, or the dialog can be
/// reopened for another document or account, before this runs; a reply
/// for a [`SendRequest`] built under a now-abandoned context must drop in
/// [`Self::pump`] exactly as one issued under that context would, not
/// install under whichever context happens to be current when this is
/// finally called.
pub fn submit_request(&mut self, request: Request) {
let _ = self.submit_in_context(request);
let context = self.pending_send_context.take().unwrap_or_else(|| self.current_context());
let seq = self.submit(request);
self.contexts.insert(seq, context);
self.pending_op_seq = Some(seq);
}
/// Completed document bytes after a successful download request.
@@ -261,8 +348,19 @@ impl EsignDialog {
}
/// Tell the dialog create/send finished (or failed) after the app submitted.
///
/// Gated the same way as [`Self::submit_request`]: when a context was
/// captured for the outstanding send, a result reported after the
/// dialog moved on to another document or account is dropped instead of
/// installing under the new context.
pub fn note_create_result(&mut self, result: Result<Envelope, String>) {
if let Some(context) = self.pending_send_context.take()
&& context != self.current_context()
{
return;
}
self.busy = false;
self.pending_op_seq = None;
match result {
Ok(env) => {
self.envelope = Some(env);
@@ -284,9 +382,15 @@ impl EsignDialog {
seq
}
/// The context (document plus account) a reply must still match to install.
/// The context (document revision plus account, plus which opening) a
/// reply must still match to install. See [`EsignContext`].
fn current_context(&self) -> EsignContext {
EsignContext { document_id: self.document_id.clone(), account: self.account_fingerprint.clone() }
EsignContext {
document_id: self.document_id.clone(),
document_etag: self.document_etag.clone(),
account: self.account_fingerprint.clone(),
opening: self.opening,
}
}
/// Like [`Self::submit`], but tags the request with the dialog's current
@@ -301,24 +405,51 @@ impl EsignDialog {
seq
}
/// Remembers that `seq`'s context has already been consulted once in
/// [`Self::pump`], bounded to the most recently consulted sequences. A
/// backend that answers the same sequence twice (only possible through
/// [`Replier::dup`]) has its second answer dropped unconditionally,
/// instead of reaching the match arms with no context left to check.
fn remember_consumed(&mut self, seq: u64) {
const TRACKED: usize = 64;
if self.consumed_order.len() >= TRACKED
&& let Some(oldest) = self.consumed_order.pop_front()
{
self.consumed.remove(&oldest);
}
self.consumed_order.push_back(seq);
self.consumed.insert(seq);
}
fn pump(&mut self, now: f64) {
while let Ok((seq, result)) = self.rx.try_recv() {
if self.caps_seq == Some(seq) {
let is_caps_reply = self.caps_seq == Some(seq);
if is_caps_reply {
self.caps_seq = None;
}
if self.consumed.contains(&seq) {
continue;
}
// A context-tagged reply installs only for the context that
// issued it; it drops silently for any other, including a
// document switch or an account switch. Typing in the signer
// search bumps `newest` and is not a context change, so it no
// longer gates a create/status/completed/void/remind reply: the
// rule here is context equality, nothing else.
if let Some(context) = self.contexts.remove(&seq)
&& context != self.current_context()
{
continue;
// document switch, a revision change or an account switch.
// Typing in the signer search bumps `newest` and is not a
// context change, so it no longer gates a create/status/
// completed/void/remind reply: the rule here is context
// equality, nothing else.
if let Some(context) = self.contexts.remove(&seq) {
self.remember_consumed(seq);
if context != self.current_context() {
continue;
}
}
match result {
Ok(Answer::Capabilities { capabilities, responsible_party, linked }) => {
// Guarded by `is_caps_reply`, computed before the swap
// above cleared it: a reply from a backend `set_backend`
// already replaced carries a sequence that is not (or no
// longer) `caps_seq`, so it cannot overwrite capabilities,
// responsible or linked for the new backend.
Ok(Answer::Capabilities { capabilities, responsible_party, linked }) if is_caps_reply => {
self.capabilities = Some(capabilities);
self.responsible = Some(responsible_party);
self.linked = linked;
@@ -327,27 +458,48 @@ impl EsignDialog {
self.matches = contacts;
self.looking = false;
}
Ok(Answer::Created(env)) | Ok(Answer::Status(env)) => {
Ok(Answer::Created(env)) => {
self.busy = false;
self.pending_op_seq = None;
self.envelope = Some(env);
self.error = None;
}
// Guarded by `status_seq`, the most recently issued status
// request: two "Refresh status" clicks in flight at once can
// answer out of order, and the older one must not overwrite
// state a newer one already installed.
Ok(Answer::Status(env)) if seq == self.status_seq => {
self.busy = false;
self.pending_op_seq = None;
self.envelope = Some(env);
self.error = None;
}
Ok(Answer::Completed(doc)) => {
self.busy = false;
self.pending_op_seq = None;
self.completed_bytes = Some(doc.bytes);
self.error = None;
}
Ok(Answer::Voided { .. }) => {
self.busy = false;
self.pending_op_seq = None;
if let Some(env) = self.envelope.as_mut() {
env.status = EnvelopeStatus::Voided;
}
}
Ok(Answer::Reminded { .. }) => {
self.busy = false;
self.pending_op_seq = None;
}
// Clears `busy` only when this failure belongs to the
// operation that currently owns it: a lookup failing (or a
// superseded status request failing) must not wrongly
// report a still-running create as finished.
Err(reason) => {
self.busy = false;
if self.pending_op_seq == Some(seq) {
self.busy = false;
self.pending_op_seq = None;
}
self.looking = false;
self.error = Some(reason);
}
@@ -381,7 +533,15 @@ impl EsignDialog {
}
fn add_contact(&mut self, contact: Contact) {
let email = contact.email.clone().unwrap_or_else(|| contact.id.clone());
// `Contact::id` is the provider's share identifier, decided by
// `target`, and is not promised to be an address: `craft_cloud`
// documents the two as distinct and warns against treating one as
// the other. Falling back to it here would hand the envelope create
// request a recipient email that is not an email.
let Some(email) = contact.email.clone() else {
self.error = Some(self.labels.contact_missing_email.clone());
return;
};
if self.recipients.iter().any(|r| r.email.eq_ignore_ascii_case(&email)) {
return;
}
@@ -431,12 +591,13 @@ impl EsignDialog {
expires: None,
};
if self.draft_idempotency_key.is_none() {
self.draft_idempotency_key = Some(format!("esign-{}-{}", self.document_id, self.seq.saturating_add(1)));
self.draft_idempotency_key = Some(format!("esign-{}-{}", self.document_id, fresh_send_id()));
}
let idempotency_key = self.draft_idempotency_key.clone().unwrap_or_default();
self.busy = true;
self.error = None;
self.send_out = Some(SendRequest { draft, idempotency_key });
self.pending_send_context = Some(self.current_context());
}
/// Draw the dialog when open. Call every frame (even when closed) so
@@ -480,6 +641,13 @@ impl EsignDialog {
return;
}
if !self.linked {
// Send is disabled below without saying why; the compose form is
// the one place an unlinked user sees before reaching it.
ui.label(&self.labels.sign_in_first);
ui.add_space(4.0);
}
ui.label(RichText::new(&self.document_title).strong());
ui.add_space(4.0);
@@ -562,18 +730,21 @@ impl EsignDialog {
&& let Some(r) = env.recipients.iter().find(|r| r.status == RecipientStatus::Pending)
{
self.busy = true;
let _ = self.submit_in_context(Request::Remind { envelope: env.id.clone(), recipient: r.id.clone() });
let seq = self.submit_in_context(Request::Remind { envelope: env.id.clone(), recipient: r.id.clone() });
self.pending_op_seq = Some(seq);
}
if caps.contains(&EsignCapability::Void)
&& env.status == EnvelopeStatus::Sent
&& ui.add_enabled(!self.busy, egui::Button::new(&self.labels.void)).clicked()
{
self.busy = true;
let _ = self.submit_in_context(Request::Void { envelope: env.id.clone(), reason: "voided by sender".into() });
let seq = self.submit_in_context(Request::Void { envelope: env.id.clone(), reason: "voided by sender".into() });
self.pending_op_seq = Some(seq);
}
if env.status == EnvelopeStatus::Completed && ui.add_enabled(!self.busy, egui::Button::new(&self.labels.download)).clicked() {
self.busy = true;
let _ = self.submit_in_context(Request::Completed { envelope: env.id.clone() });
let seq = self.submit_in_context(Request::Completed { envelope: env.id.clone() });
self.pending_op_seq = Some(seq);
}
if ui.button(&self.labels.close).clicked() {
self.open = false;
@@ -581,7 +752,9 @@ impl EsignDialog {
});
if ui.button("Refresh status").clicked() {
self.busy = true;
let _ = self.submit_in_context(Request::Status { envelope: env.id.clone() });
let seq = self.submit_in_context(Request::Status { envelope: env.id.clone() });
self.pending_op_seq = Some(seq);
self.status_seq = seq;
}
}
}
@@ -645,4 +818,89 @@ mod tests {
assert_eq!(req.draft.recipients.len(), 1);
assert_eq!(req.draft.recipients[0].email, "bob@example.com");
}
/// The texts egui drew, walking into `Shape::Vec` groups the way widgets nest them. Same
/// pattern as `docking::native_tests::texts`.
fn texts(shapes: &[egui::epaint::ClippedShape]) -> Vec<String> {
fn walk(shape: &egui::Shape, out: &mut Vec<String>) {
match shape {
egui::Shape::Vec(shapes) => shapes.iter().for_each(|s| walk(s, out)),
egui::Shape::Text(text) => out.push(text.galley.text().to_owned()),
_ => {}
}
}
let mut out = Vec::new();
shapes.iter().for_each(|clipped| walk(&clipped.shape, &mut out));
out
}
#[test]
fn compose_form_shows_sign_in_message_when_not_linked() {
let mut dialog = EsignDialog {
capabilities: Some(vec![EsignCapability::Envelopes]),
document_title: "A.pdf".into(),
linked: false,
..Default::default()
};
let ctx = Context::default();
let full = ctx.run_ui(egui::RawInput::default(), |ui| dialog.ui(ui));
let shown = texts(&full.shapes);
assert!(shown.iter().any(|t| t == &dialog.labels.sign_in_first), "sign-in message not shown for an unlinked account: {shown:?}");
}
fn ada() -> Identity {
Identity { id: "u".into(), display: "Ada".into(), email: Some("ada@example.com".into()), avatar: None }
}
fn placement() -> FieldPlacement {
FieldPlacement { role: "Signer".into(), page: 0, x: 0.1, y: 0.2, w: 0.3, h: 0.05, kind: "signature".into(), required: true }
}
#[test]
fn add_contact_rejects_a_contact_with_no_email() {
let mut dialog = EsignDialog::default();
let contact =
Contact { id: "share-id-123".into(), target: craft_cloud::ShareTarget::User("share-id-123".into()), name: "Mallory".into(), email: None, avatar: None, is_group: false };
dialog.add_contact(contact);
assert!(dialog.recipients.is_empty(), "a contact with no email became a recipient: {:?}", dialog.recipients);
assert_eq!(dialog.error.as_deref(), Some(dialog.labels.contact_missing_email.as_str()));
}
#[test]
fn set_placements_invalidates_the_idempotency_key() {
let mut dialog = EsignDialog { linked: true, sender: Some(ada()), ..Default::default() };
dialog.add_email("bob@example.com".into());
dialog.queue_send();
let first = dialog.take_send_request().expect("first send").idempotency_key;
dialog.set_placements(vec![placement()]);
dialog.queue_send();
let second = dialog.take_send_request().expect("second send").idempotency_key;
assert_ne!(first, second, "changing placements kept the old idempotency key: {first}");
}
#[test]
fn placements_do_not_survive_a_document_change() {
let mut dialog = EsignDialog::default();
dialog.open("doc-A".into(), "A.pdf".into(), None, ada());
dialog.set_placements(vec![placement()]);
dialog.open("doc-B".into(), "B.pdf".into(), None, ada());
assert!(dialog.placements.is_empty(), "doc-B inherited doc-A's placements: {:?}", dialog.placements);
}
#[test]
fn queue_send_keys_are_unique_across_dialog_instances() {
fn send_key(doc: &str) -> String {
let mut dialog = EsignDialog { linked: true, sender: Some(ada()), document_id: doc.into(), ..Default::default() };
dialog.add_email("bob@example.com".into());
dialog.queue_send();
dialog.take_send_request().expect("send").idempotency_key
}
let a = send_key("doc-A");
let b = send_key("doc-A");
assert_ne!(a, b, "two fresh dialog instances for the same document produced the same idempotency key: {a}");
}
}
+8 -6
View File
@@ -12,8 +12,10 @@ use craft_esign::{Envelope, EnvelopeStatus, EsignCapability};
use super::*;
/// Reused by `coderabbit_repro`, the regression tests for the CodeRabbit
/// findings reviewed on the same pull request.
#[derive(Clone, Default)]
struct Hold(Arc<Mutex<Vec<(Request, Replier)>>>);
pub(super) struct Hold(Arc<Mutex<Vec<(Request, Replier)>>>);
impl EsignBackend for Hold {
fn submit(&self, request: Request, reply: Replier) {
@@ -23,18 +25,18 @@ impl EsignBackend for Hold {
impl Hold {
/// Takes the oldest held request matching `pick`.
fn take(&self, pick: impl Fn(&Request) -> bool) -> (Request, Replier) {
pub(super) fn take(&self, pick: impl Fn(&Request) -> bool) -> (Request, Replier) {
let mut held = self.0.lock().unwrap();
let i = held.iter().position(|(r, _)| pick(r)).expect("request was submitted");
held.remove(i)
}
}
fn who(id: &str) -> Identity {
pub(super) fn who(id: &str) -> Identity {
Identity { id: id.into(), display: id.into(), email: Some(format!("{id}@example.com")), avatar: None }
}
fn envelope_for(doc: &str, sender: &str) -> Envelope {
pub(super) fn envelope_for(doc: &str, sender: &str) -> Envelope {
Envelope {
id: format!("env-{doc}"),
title: format!("Contract {doc}"),
@@ -53,7 +55,7 @@ fn envelope_for(doc: &str, sender: &str) -> Envelope {
}
/// A dialog with a held backend whose capabilities probe has been answered.
fn ready() -> (EsignDialog, Hold) {
pub(super) fn ready() -> (EsignDialog, Hold) {
let hold = Hold::default();
let mut dialog = EsignDialog::default();
dialog.set_backend(hold.clone());
@@ -70,7 +72,7 @@ fn ready() -> (EsignDialog, Hold) {
}
/// The app's Send path: queue_send -> take_send_request -> submit Create.
fn send(dialog: &mut EsignDialog, doc_bytes: &[u8]) {
pub(super) fn send(dialog: &mut EsignDialog, doc_bytes: &[u8]) {
dialog.add_email("bob@example.com".into());
dialog.queue_send();
let out = dialog.take_send_request().expect("Send produced a request");