Skip to content

Commit 3c05927

Browse files
test(acp): prove a control notice lands in the command's thread
Review asked to confirm that the notice inherits the thread tags of the command, so a `!rotate` typed in a thread is not answered with an unrelated root-level message. It does — `spawn_control_notice` and the awaited `!shutdown` path both derive their `ThreadTags` from the triggering event — but nothing guarded it. Extract the `ThreadTags` to `ThreadRef` mapping out of `post_notice` into `thread_ref_from_tags` so the test exercises the real path rather than a copy of it, and assert both directions: a threaded command keeps its root and parent, a channel-level command stays unthreaded. Verified the test fails when `thread_ref_from_tags` is made to return `None`. Refs #3711 Co-Authored-By: Claude Opus 5 <[email protected]> Signed-off-by: Bérenger Ouadi <[email protected]>
1 parent cc911a9 commit 3c05927

2 files changed

Lines changed: 50 additions & 12 deletions

File tree

‎crates/buzz-acp/src/lib.rs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4375,6 +4375,35 @@ mod owner_control_command_tests {
43754375
));
43764376
}
43774377

4378+
#[test]
4379+
fn control_notice_lands_in_the_thread_the_command_was_typed_in() {
4380+
// A command typed in a thread must be answered in that thread. Posting
4381+
// the notice at channel level instead would surface an unrelated root
4382+
// message, which is how the owner loses track of what it answers.
4383+
let keys = Keys::generate();
4384+
let root = "11".repeat(32);
4385+
let reply = "22".repeat(32);
4386+
let threaded = EventBuilder::new(Kind::Custom(KIND_STREAM_MESSAGE as u16), "!rotate")
4387+
.tags([
4388+
Tag::parse(["e", &root, "", "root"]).expect("root tag"),
4389+
Tag::parse(["e", &reply, "", "reply"]).expect("reply tag"),
4390+
])
4391+
.sign_with_keys(&keys)
4392+
.unwrap();
4393+
4394+
let thread_ref = pool::thread_ref_from_tags(&queue::parse_thread_tags(&threaded))
4395+
.expect("a threaded command yields a thread ref");
4396+
assert_eq!(thread_ref.root_event_id.to_hex(), root);
4397+
assert_eq!(thread_ref.parent_event_id.to_hex(), reply);
4398+
4399+
// Conversely, a command typed at channel level must NOT be threaded, or
4400+
// the notice would attach itself to whatever root it inherited.
4401+
let bare = EventBuilder::new(Kind::Custom(KIND_STREAM_MESSAGE as u16), "!rotate")
4402+
.sign_with_keys(&keys)
4403+
.unwrap();
4404+
assert!(pool::thread_ref_from_tags(&queue::parse_thread_tags(&bare)).is_none());
4405+
}
4406+
43784407
#[test]
43794408
fn rotate_idle_notice_distinguishes_a_rotation_from_a_no_op() {
43804409
// The no-op is the case worth wording carefully: nothing was cached, so

‎crates/buzz-acp/src/pool.rs‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3615,6 +3615,26 @@ pub(crate) async fn reaction_add(rest: &crate::relay::RestClient, event_id: &str
36153615
}
36163616
}
36173617

3618+
/// Resolve the NIP-10 thread a notice must land in, from the tags of the event
3619+
/// that triggered it.
3620+
///
3621+
/// `None` when the trigger carried no thread tags — the notice then posts at
3622+
/// channel level. This is what keeps a notice in the thread the owner was
3623+
/// typing in instead of surfacing as an unrelated root message.
3624+
pub(crate) fn thread_ref_from_tags(thread_tags: &ThreadTags) -> Option<buzz_sdk::ThreadRef> {
3625+
let root = thread_tags.root_event_id.as_deref()?;
3626+
let root_id = nostr::EventId::from_hex(root).ok()?;
3627+
let parent_id = thread_tags
3628+
.parent_event_id
3629+
.as_deref()
3630+
.and_then(|p| nostr::EventId::from_hex(p).ok())
3631+
.unwrap_or(root_id);
3632+
Some(buzz_sdk::ThreadRef {
3633+
root_event_id: root_id,
3634+
parent_event_id: parent_id,
3635+
})
3636+
}
3637+
36183638
/// Best-effort: post a visible notice (kind:9) to a channel — a dead-letter
36193639
/// warning, or the outcome of a consumed owner control command. Replies into
36203640
/// the thread of `thread_tags` when the triggering event was threaded. Errors
@@ -3625,18 +3645,7 @@ pub(crate) async fn post_notice(
36253645
thread_tags: &ThreadTags,
36263646
content: &str,
36273647
) {
3628-
let thread_ref = thread_tags.root_event_id.as_deref().and_then(|root| {
3629-
let root_id = nostr::EventId::from_hex(root).ok()?;
3630-
let parent_id = thread_tags
3631-
.parent_event_id
3632-
.as_deref()
3633-
.and_then(|p| nostr::EventId::from_hex(p).ok())
3634-
.unwrap_or(root_id);
3635-
Some(buzz_sdk::ThreadRef {
3636-
root_event_id: root_id,
3637-
parent_event_id: parent_id,
3638-
})
3639-
});
3648+
let thread_ref = thread_ref_from_tags(thread_tags);
36403649
let builder =
36413650
match buzz_sdk::build_message(channel_id, content, thread_ref.as_ref(), &[], false, &[]) {
36423651
Ok(b) => b,

0 commit comments

Comments
 (0)