diff --git a/crates/radicle-cli/src/terminal/patch/timeline.rs b/crates/radicle-cli/src/terminal/patch/timeline.rs index c4250bcd3..9a8d8d6a4 100644 --- a/crates/radicle-cli/src/terminal/patch/timeline.rs +++ b/crates/radicle-cli/src/terminal/patch/timeline.rs @@ -74,7 +74,7 @@ impl<'a> RevisionEntry<'a> { ( review.timestamp(), Update::Reviewed { - review: review.clone(), + review: Box::new(review.clone()), }, ) })); @@ -157,7 +157,7 @@ impl<'a> RevisionEntry<'a> { /// An update in the [`Patch`]'s timeline. enum Update<'a> { /// A revision of the patch was reviewed. - Reviewed { review: Review }, + Reviewed { review: Box }, /// A revision of the patch was merged. Merged { author: Author<'a>, diff --git a/crates/radicle/src/cob/thread.rs b/crates/radicle/src/cob/thread.rs index 0368389ac..451376e70 100644 --- a/crates/radicle/src/cob/thread.rs +++ b/crates/radicle/src/cob/thread.rs @@ -3,6 +3,7 @@ use std::collections::{BTreeMap, BTreeSet}; use std::str::FromStr; use std::sync::LazyLock; +use indexmap::IndexSet; use serde::{Deserialize, Serialize, ser::SerializeStruct}; use thiserror::Error; @@ -291,14 +292,14 @@ pub struct Thread { /// The comments under the thread. comments: BTreeMap>, /// Comment timeline. - timeline: Vec, + timeline: IndexSet, } impl Default for Thread { fn default() -> Self { Self { comments: BTreeMap::default(), - timeline: Vec::default(), + timeline: IndexSet::default(), } } } @@ -313,7 +314,7 @@ impl Thread { pub fn new(id: CommentId, comment: T) -> Self { Self { comments: BTreeMap::from_iter([(id, Some(comment))]), - timeline: vec![id], + timeline: [id].into(), } } @@ -512,8 +513,7 @@ pub fn comment( { return Err(Error::Missing(id)); } - debug_assert!(!thread.timeline.contains(&id)); - thread.timeline.push(id); + thread.timeline.insert(id); // Nb. If a comment is already present, it must be redacted, because the // underlying store guarantees exactly-once delivery of ops. @@ -536,8 +536,7 @@ pub fn edit( body: String, embeds: Vec>, ) -> Result<(), Error> { - debug_assert!(!thread.timeline.contains(&id)); - thread.timeline.push(id); + thread.timeline.insert(id); // It's possible for a comment to be redacted before we're able to edit it, in // case of a concurrent update. @@ -556,8 +555,7 @@ pub fn edit( pub fn redact(thread: &mut Thread, id: EntryId, comment: EntryId) -> Result<(), Error> { if let Some(comment) = thread.comments.get_mut(&comment) { - debug_assert!(!thread.timeline.contains(&id)); - thread.timeline.push(id); + thread.timeline.insert(id); *comment = None; } else { @@ -579,8 +577,7 @@ pub fn react( return Err(Error::Missing(comment)); }; if let Some(comment) = comment { - debug_assert!(!thread.timeline.contains(&id)); - thread.timeline.push(id); + thread.timeline.insert(id); if active { comment.reactions.insert(key); @@ -601,8 +598,7 @@ pub fn resolve( }; if let Some(comment) = comment { - debug_assert!(!thread.timeline.contains(&id)); - thread.timeline.push(id); + thread.timeline.insert(id); comment.resolve(); } Ok(()) @@ -618,8 +614,7 @@ pub fn unresolve( }; if let Some(comment) = comment { - debug_assert!(!thread.timeline.contains(&id)); - thread.timeline.push(id); + thread.timeline.insert(id); comment.unresolve(); } Ok(()) @@ -713,6 +708,58 @@ mod tests { assert_eq!(t1.comment(&c0.id()).unwrap().body(), "Goodbye world!"); } + /// Regression test for commit `1bb04020a4afb4b7a071dad9d0a06791c087fb3a` + /// in `rad:z2reN9XFdJgQmSbh23KUp9va689YJ`, which occurs in patch + /// `0978f920a494b52744a2b349b3453a42a8106513`. + /// + /// Generally, operations may carry multiple actions, thus, multiple actions + /// might be referred to by the same ID. + #[test] + fn resolve_and_unresolve_multiple_comments_in_one_operation() { + let mut alice = SigningKey::mock(49); + let repo = r#gen::(1); + + let comment_1 = alice.comment("First", None); + let comment_2 = alice.comment("Second", None); + let mut thread = Thread::from_ops([comment_1.clone(), comment_2.clone()], &repo).unwrap(); + + // Note that the same `operation` is used to resolve *both* + // `comment_1` and `comment_2`. + let operation = arbitrary::entry_id(); + + resolve(&mut thread, operation, comment_1.id()).unwrap(); + resolve(&mut thread, operation, comment_2.id()).unwrap(); + + assert!(thread.comment(&comment_1.id()).unwrap().is_resolved()); + assert!(thread.comment(&comment_2.id()).unwrap().is_resolved()); + assert_eq!( + thread + .timeline + .iter() + .filter(|id| **id == operation) + .count(), + 1 + ); + + // Note that the same `operation` is used to unresolve *both* + // `comment_1` and `comment_2`. + let operation = arbitrary::entry_id(); + + unresolve(&mut thread, operation, comment_1.id()).unwrap(); + unresolve(&mut thread, operation, comment_2.id()).unwrap(); + + assert!(!thread.comment(&comment_1.id()).unwrap().is_resolved()); + assert!(!thread.comment(&comment_2.id()).unwrap().is_resolved()); + assert_eq!( + thread + .timeline + .iter() + .filter(|id| **id == operation) + .count(), + 1 + ); + } + #[test] fn test_timeline() { let alice = SigningKey::mock(94);