nostro2 0.7.0

Nostro2 is a simple toolset for interacting with the Nostr protocol.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
use bourne::{
    Error as BourneError, ErrorKind as BourneErrorKind, FromJson, JsonWrite, Lexer, ToJson,
};

/// NIP-01 relay message tags. Wire form is uppercase (`"EVENT"`, `"OK"`, …).
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum RelayEventTag {
    Event,
    Ok,
    Eose,
    Notice,
    Close,
    Auth,
    Req,
    Closed,
}

/// Shared parser for NIP-01 relay-event wire frames.
///
/// Both the zero-copy [`NostrRelayEventView`](crate::NostrRelayEventView) and
/// the owning [`NostrRelayEvent`] parse the same `["TAG", …]` array shapes —
/// they differ only in allocation strategy and whether the tag is stored.
/// This trait factors out the common dispatch logic so there is a single
/// implementation to test and maintain.
pub trait RelayFrameParser<'input>: Sized {
    type Str: FromJson<'input>;
    type Note: FromJson<'input>;

    fn new_note(tag: RelayEventTag, sub_id: Self::Str, note: Self::Note) -> Self;
    fn sent_ok(tag: RelayEventTag, event_id: Self::Str, success: bool, message: Self::Str) -> Self;
    fn eose(tag: RelayEventTag, sub_id: Self::Str) -> Self;
    fn closed(tag: RelayEventTag, sub_id: Self::Str) -> Self;
    fn notice(tag: RelayEventTag, message: Self::Str) -> Self;
    fn auth(tag: RelayEventTag, challenge: Self::Str) -> Self;

    fn from_lex(lex: &mut Lexer<'input>) -> Result<Self, BourneError> {
        let tag = lex.parse_frame_tag()?;
        match tag {
            RelayEventTag::Event => {
                let sub_id = Self::Str::from_lex(lex)?;
                lex.expect_more()?;
                let note = Self::Note::from_lex(lex)?;
                lex.expect_end()?;
                Ok(Self::new_note(tag, sub_id, note))
            }
            RelayEventTag::Ok => {
                let event_id = Self::Str::from_lex(lex)?;
                lex.expect_more()?;
                let success = bool::from_lex(lex)?;
                lex.expect_more()?;
                let message = Self::Str::from_lex(lex)?;
                lex.expect_end()?;
                Ok(Self::sent_ok(tag, event_id, success, message))
            }
            RelayEventTag::Eose
            | RelayEventTag::Closed
            | RelayEventTag::Notice
            | RelayEventTag::Auth => {
                let val = Self::Str::from_lex(lex)?;
                lex.expect_end()?;
                Ok(match tag {
                    RelayEventTag::Eose => Self::eose(tag, val),
                    RelayEventTag::Closed => Self::closed(tag, val),
                    RelayEventTag::Notice => Self::notice(tag, val),
                    RelayEventTag::Auth => Self::auth(tag, val),
                    _ => unreachable!(),
                })
            }
            _ => Err(BourneError::new(
                BourneErrorKind::UnknownField,
                lex.position(),
            )),
        }
    }
}

impl RelayEventTag {
    const fn as_quoted(self) -> &'static str {
        match self {
            Self::Event => "\"EVENT\"",
            Self::Ok => "\"OK\"",
            Self::Eose => "\"EOSE\"",
            Self::Notice => "\"NOTICE\"",
            Self::Close => "\"CLOSE\"",
            Self::Auth => "\"AUTH\"",
            Self::Req => "\"REQ\"",
            Self::Closed => "\"CLOSED\"",
        }
    }

    pub(crate) fn from_str_wire(s: &str) -> Option<Self> {
        Some(match s {
            "EVENT" => Self::Event,
            "OK" => Self::Ok,
            "EOSE" => Self::Eose,
            "NOTICE" => Self::Notice,
            "CLOSE" => Self::Close,
            "AUTH" => Self::Auth,
            "REQ" => Self::Req,
            "CLOSED" => Self::Closed,
            _ => return None,
        })
    }
}

impl<'input> FromJson<'input> for RelayEventTag {
    fn from_lex(lex: &mut Lexer<'input>) -> Result<Self, BourneError> {
        let s = lex.parse_str_value()?;
        Self::from_str_wire(s)
            .ok_or_else(|| BourneError::new(BourneErrorKind::UnknownField, lex.position()))
    }
}

impl ToJson for RelayEventTag {
    fn write_json<W: JsonWrite + ?Sized>(&self, w: &mut W) -> Result<(), W::Error> {
        w.write_str_raw(self.as_quoted())
    }
}

// ── Wire-frame parsing helpers ───────────────────────────────────

/// Extension trait that adds nostr wire-frame parsing sugar to `Lexer`.
pub trait WireFrameExt {
    /// Consumes `[`, the tag string, and the following comma.
    /// Returns `TypeMismatch` for an empty array, `UnknownField` for an
    /// unrecognized tag, or `MissingField` if no further elements follow.
    fn parse_frame_tag(&mut self) -> Result<RelayEventTag, BourneError>;
    /// Asserts the array has at least one more element (comma, not `]`).
    fn expect_more(&mut self) -> Result<(), BourneError>;
    /// Asserts the array has been fully consumed (closing `]`).
    fn expect_end(&mut self) -> Result<(), BourneError>;
}

impl WireFrameExt for Lexer<'_> {
    fn parse_frame_tag(&mut self) -> Result<RelayEventTag, BourneError> {
        if self.array_start()? {
            return Err(BourneError::new(
                BourneErrorKind::TypeMismatch,
                self.position(),
            ));
        }
        let tag = RelayEventTag::from_str_wire(self.parse_str_value()?)
            .ok_or_else(|| BourneError::new(BourneErrorKind::UnknownField, self.position()))?;
        self.expect_more()?;
        Ok(tag)
    }

    fn expect_more(&mut self) -> Result<(), BourneError> {
        if self.array_continue(b']')? {
            Err(BourneError::new(
                BourneErrorKind::MissingField,
                self.position(),
            ))
        } else {
            Ok(())
        }
    }

    fn expect_end(&mut self) -> Result<(), BourneError> {
        if self.array_continue(b']')? {
            Ok(())
        } else {
            Err(BourneError::new(
                BourneErrorKind::TrailingData,
                self.position(),
            ))
        }
    }
}

// ── FROM RELAY TO CLIENT ──────────────────────────────────────────
//
// Nostr wire frames are JSON arrays: `["EVENT", "sub_id", {note}]`,
// `["OK", "event_id", true, "msg"]`, etc. Each variant maps 1:1 to
// a NIP-01 / NIP-42 frame shape. Discrimination is by the first
// array element (the tag string) plus the element count.

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum NostrRelayEvent {
    NewNote(RelayEventTag, String, crate::note::NostrNote),
    SentOk(RelayEventTag, String, bool, String),
    EndOfSubscription(RelayEventTag, String),
    ClosedSubscription(RelayEventTag, String),
    Notice(RelayEventTag, String),
    Auth(RelayEventTag, String),
}

impl RelayFrameParser<'_> for NostrRelayEvent {
    type Str = String;
    type Note = crate::note::NostrNote;

    fn new_note(tag: RelayEventTag, sub_id: String, note: Self::Note) -> Self {
        Self::NewNote(tag, sub_id, note)
    }
    fn sent_ok(tag: RelayEventTag, event_id: String, success: bool, message: String) -> Self {
        Self::SentOk(tag, event_id, success, message)
    }
    fn eose(tag: RelayEventTag, sub_id: String) -> Self {
        Self::EndOfSubscription(tag, sub_id)
    }
    fn closed(tag: RelayEventTag, sub_id: String) -> Self {
        Self::ClosedSubscription(tag, sub_id)
    }
    fn notice(tag: RelayEventTag, message: String) -> Self {
        Self::Notice(tag, message)
    }
    fn auth(tag: RelayEventTag, challenge: String) -> Self {
        Self::Auth(tag, challenge)
    }
}

impl<'input> FromJson<'input> for NostrRelayEvent {
    fn from_lex(lex: &mut Lexer<'input>) -> Result<Self, BourneError> {
        <Self as RelayFrameParser>::from_lex(lex)
    }
}

impl ToJson for NostrRelayEvent {
    fn write_json<W: JsonWrite + ?Sized>(&self, w: &mut W) -> Result<(), W::Error> {
        w.write_byte(b'[')?;
        match self {
            Self::NewNote(tag, sub_id, note) => {
                tag.write_json(w)?;
                w.write_byte(b',')?;
                w.write_escaped_str(sub_id)?;
                w.write_byte(b',')?;
                note.write_json(w)?;
            }
            Self::SentOk(tag, event_id, success, message) => {
                tag.write_json(w)?;
                w.write_byte(b',')?;
                w.write_escaped_str(event_id)?;
                w.write_byte(b',')?;
                success.write_json(w)?;
                w.write_byte(b',')?;
                w.write_escaped_str(message)?;
            }
            Self::EndOfSubscription(tag, sub_id)
            | Self::ClosedSubscription(tag, sub_id)
            | Self::Notice(tag, sub_id)
            | Self::Auth(tag, sub_id) => {
                tag.write_json(w)?;
                w.write_byte(b',')?;
                w.write_escaped_str(sub_id)?;
            }
        }
        w.write_byte(b']')
    }
}

impl std::str::FromStr for NostrRelayEvent {
    type Err = bourne::Error;
    fn from_str(value: &str) -> Result<Self, Self::Err> {
        bourne::parse_str(value)
    }
}

impl TryFrom<&[u8]> for NostrRelayEvent {
    type Error = bourne::Error;
    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        bourne::parse(value)
    }
}

// ── FROM CLIENT TO RELAY ──────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NostrClientEvent {
    SendNoteEvent(RelayEventTag, super::note::NostrNote),
    Subscribe(
        RelayEventTag,
        String,
        super::subscriptions::NostrSubscription,
    ),
    CloseSubscriptionEvent(RelayEventTag, String),
    AuthEvent(RelayEventTag, crate::note::NostrNote),
}

impl NostrClientEvent {
    fn fresh_sub_id() -> String {
        use std::sync::OnceLock;
        use std::sync::atomic::{AtomicU64, Ordering};

        static START_NS: OnceLock<u64> = OnceLock::new();
        static COUNTER: AtomicU64 = AtomicU64::new(0);

        let start_ns = *START_NS.get_or_init(|| {
            #[cfg(not(target_arch = "wasm32"))]
            {
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .ok()
                    .and_then(|d| u64::try_from(d.as_nanos()).ok())
                    .unwrap_or(0)
            }
            #[cfg(target_arch = "wasm32")]
            #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
            {
                (js_sys::Date::now() * 1_000_000.0) as u64
            }
        });

        let n = COUNTER.fetch_add(1, Ordering::Relaxed);
        format!("{start_ns}-{n}")
    }

    #[must_use]
    pub fn close_subscription(sub_id: &str) -> Self {
        Self::CloseSubscriptionEvent(RelayEventTag::Close, sub_id.to_string())
    }
    #[must_use]
    pub const fn auth_event(note: super::note::NostrNote) -> Self {
        Self::AuthEvent(RelayEventTag::Auth, note)
    }
}

impl From<super::note::NostrNote> for NostrClientEvent {
    fn from(note: super::note::NostrNote) -> Self {
        Self::SendNoteEvent(RelayEventTag::Event, note)
    }
}

impl From<&super::note::NostrNote> for NostrClientEvent {
    fn from(note: &super::note::NostrNote) -> Self {
        Self::SendNoteEvent(RelayEventTag::Event, note.clone())
    }
}

impl From<super::subscriptions::NostrSubscription> for NostrClientEvent {
    fn from(subscription: super::subscriptions::NostrSubscription) -> Self {
        Self::Subscribe(RelayEventTag::Req, Self::fresh_sub_id(), subscription)
    }
}

impl From<&super::subscriptions::NostrSubscription> for NostrClientEvent {
    fn from(subscription: &super::subscriptions::NostrSubscription) -> Self {
        Self::Subscribe(
            RelayEventTag::Req,
            Self::fresh_sub_id(),
            subscription.clone(),
        )
    }
}

impl<'input> FromJson<'input> for NostrClientEvent {
    fn from_lex(lex: &mut Lexer<'input>) -> Result<Self, BourneError> {
        let tag = lex.parse_frame_tag()?;
        match tag {
            RelayEventTag::Event | RelayEventTag::Auth => {
                let note = crate::note::NostrNote::from_lex(lex)?;
                lex.expect_end()?;
                Ok(match tag {
                    RelayEventTag::Event => Self::SendNoteEvent(tag, note),
                    _ => Self::AuthEvent(tag, note),
                })
            }
            RelayEventTag::Req => {
                let sub_id = String::from_lex(lex)?;
                lex.expect_more()?;
                let filter = super::subscriptions::NostrSubscription::from_lex(lex)?;
                lex.expect_end()?;
                Ok(Self::Subscribe(tag, sub_id, filter))
            }
            RelayEventTag::Close => {
                let sub_id = String::from_lex(lex)?;
                lex.expect_end()?;
                Ok(Self::CloseSubscriptionEvent(tag, sub_id))
            }
            _ => Err(BourneError::new(
                BourneErrorKind::UnknownField,
                lex.position(),
            )),
        }
    }
}

impl ToJson for NostrClientEvent {
    fn write_json<W: JsonWrite + ?Sized>(&self, w: &mut W) -> Result<(), W::Error> {
        w.write_byte(b'[')?;
        match self {
            Self::SendNoteEvent(tag, note) | Self::AuthEvent(tag, note) => {
                tag.write_json(w)?;
                w.write_byte(b',')?;
                note.write_json(w)?;
            }
            Self::Subscribe(tag, sub_id, filter) => {
                tag.write_json(w)?;
                w.write_byte(b',')?;
                w.write_escaped_str(sub_id)?;
                w.write_byte(b',')?;
                filter.write_json(w)?;
            }
            Self::CloseSubscriptionEvent(tag, sub_id) => {
                tag.write_json(w)?;
                w.write_byte(b',')?;
                w.write_escaped_str(sub_id)?;
            }
        }
        w.write_byte(b']')
    }
}

impl std::str::FromStr for NostrClientEvent {
    type Err = bourne::Error;
    fn from_str(value: &str) -> Result<Self, Self::Err> {
        bourne::parse_str(value)
    }
}

impl TryFrom<&[u8]> for NostrClientEvent {
    type Error = bourne::Error;
    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        bourne::parse(value)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::note::NostrNote;
    use crate::subscriptions::NostrSubscription;

    fn sample_note() -> NostrNote {
        NostrNote {
            pubkey: "a".repeat(64),
            created_at: 1_700_000_000,
            kind: 1,
            content: "hello relay".into(),
            id: Some("b".repeat(64)),
            sig: Some("c".repeat(128)),
            ..Default::default()
        }
    }

    fn round_trip<
        T: bourne::ToJson + for<'a> bourne::FromJson<'a> + std::fmt::Debug + PartialEq,
    >(
        val: &T,
    ) {
        let json = bourne::to_string(val).expect("serialize");
        let back: T = bourne::parse_str(&json).expect("parse back");
        assert_eq!(val, &back);
    }

    #[test]
    fn relay_event_round_trip() {
        let note = sample_note();
        round_trip(&NostrRelayEvent::NewNote(
            RelayEventTag::Event,
            "sub1".into(),
            note,
        ));
    }

    #[test]
    fn relay_ok_round_trip() {
        round_trip(&NostrRelayEvent::SentOk(
            RelayEventTag::Ok,
            "abc".repeat(21),
            true,
            "duplicate:".into(),
        ));
        round_trip(&NostrRelayEvent::SentOk(
            RelayEventTag::Ok,
            "def".repeat(21),
            false,
            "blocked: not in whitelist".into(),
        ));
    }

    #[test]
    fn relay_eose_round_trip() {
        round_trip(&NostrRelayEvent::EndOfSubscription(
            RelayEventTag::Eose,
            "sub42".into(),
        ));
    }

    #[test]
    fn relay_notice_round_trip() {
        round_trip(&NostrRelayEvent::Notice(
            RelayEventTag::Notice,
            "rate limited".into(),
        ));
    }

    #[test]
    fn relay_auth_round_trip() {
        round_trip(&NostrRelayEvent::Auth(
            RelayEventTag::Auth,
            "challenge-xyz".into(),
        ));
    }

    #[test]
    fn relay_closed_round_trip() {
        round_trip(&NostrRelayEvent::ClosedSubscription(
            RelayEventTag::Closed,
            "sub7".into(),
        ));
    }

    #[test]
    fn client_event_round_trip() {
        let note = sample_note();
        round_trip(&NostrClientEvent::SendNoteEvent(RelayEventTag::Event, note));
    }

    #[test]
    fn client_auth_round_trip() {
        let note = sample_note();
        round_trip(&NostrClientEvent::AuthEvent(RelayEventTag::Auth, note));
    }

    #[test]
    fn client_close_round_trip() {
        round_trip(&NostrClientEvent::CloseSubscriptionEvent(
            RelayEventTag::Close,
            "sub99".into(),
        ));
    }

    #[test]
    fn client_subscribe_round_trip() {
        let sub = NostrSubscription::new().kind(1).limit(10);
        round_trip(&NostrClientEvent::Subscribe(
            RelayEventTag::Req,
            "mysub".into(),
            sub,
        ));
    }

    #[test]
    fn relay_event_from_str_round_trip() {
        let note = sample_note();
        let event = NostrRelayEvent::NewNote(RelayEventTag::Event, "sub1".into(), note);
        let json = bourne::to_string(&event).unwrap();
        let parsed: NostrRelayEvent = json.parse().unwrap();
        assert_eq!(event, parsed);
    }

    #[test]
    fn client_event_from_str_round_trip() {
        let note = sample_note();
        let event = NostrClientEvent::SendNoteEvent(RelayEventTag::Event, note);
        let json = bourne::to_string(&event).unwrap();
        let parsed: NostrClientEvent = json.parse().unwrap();
        assert_eq!(event, parsed);
    }

    #[test]
    fn relay_event_tag_all_variants() {
        let tags = [
            (RelayEventTag::Event, "EVENT"),
            (RelayEventTag::Ok, "OK"),
            (RelayEventTag::Eose, "EOSE"),
            (RelayEventTag::Notice, "NOTICE"),
            (RelayEventTag::Close, "CLOSE"),
            (RelayEventTag::Auth, "AUTH"),
            (RelayEventTag::Req, "REQ"),
            (RelayEventTag::Closed, "CLOSED"),
        ];
        for (tag, expected_wire) in tags {
            let quoted = tag.as_quoted();
            assert_eq!(quoted, format!("\"{expected_wire}\""));
            let parsed = RelayEventTag::from_str_wire(expected_wire).unwrap();
            assert_eq!(tag, parsed);
        }
        assert!(RelayEventTag::from_str_wire("UNKNOWN").is_none());
    }

    #[test]
    fn relay_event_rejects_empty_array() {
        let result: Result<NostrRelayEvent, _> = bourne::parse_str("[]");
        assert!(result.is_err());
    }

    #[test]
    fn relay_event_rejects_unknown_tag() {
        let result: Result<NostrRelayEvent, _> = bourne::parse_str(r#"["BOGUS","sub"]"#);
        assert!(result.is_err());
    }

    #[test]
    fn relay_event_rejects_tag_only() {
        let result: Result<NostrRelayEvent, _> = bourne::parse_str(r#"["EVENT"]"#);
        assert!(result.is_err());
    }

    #[test]
    fn relay_event_rejects_truncated_ok() {
        let result: Result<NostrRelayEvent, _> = bourne::parse_str(r#"["OK","eid"]"#);
        assert!(result.is_err());
        let result: Result<NostrRelayEvent, _> = bourne::parse_str(r#"["OK","eid",true]"#);
        assert!(result.is_err());
    }

    #[test]
    fn relay_event_rejects_trailing_data() {
        let result: Result<NostrRelayEvent, _> = bourne::parse_str(r#"["EOSE","sub","extra"]"#);
        assert!(result.is_err());
    }

    #[test]
    fn relay_event_rejects_not_array() {
        let result: Result<NostrRelayEvent, _> = bourne::parse_str(r#"{"tag":"EVENT"}"#);
        assert!(result.is_err());
    }

    #[test]
    fn relay_event_rejects_event_missing_note() {
        let result: Result<NostrRelayEvent, _> = bourne::parse_str(r#"["EVENT","sub"]"#);
        assert!(result.is_err());
    }

    #[test]
    fn relay_event_rejects_event_trailing_data() {
        let note = sample_note();
        let note_json = bourne::to_string(&note).unwrap();
        let json = format!(r#"["EVENT","sub",{note_json},"extra"]"#);
        let result: Result<NostrRelayEvent, _> = bourne::parse_str(&json);
        assert!(result.is_err());
    }

    #[test]
    fn relay_event_rejects_ok_trailing_data() {
        let result: Result<NostrRelayEvent, _> =
            bourne::parse_str(r#"["OK","eid",true,"msg","extra"]"#);
        assert!(result.is_err());
    }

    #[test]
    fn relay_event_rejects_ok_missing_bool() {
        let result: Result<NostrRelayEvent, _> = bourne::parse_str(r#"["OK","eid"]"#);
        assert!(result.is_err());
    }

    #[test]
    fn relay_event_rejects_ok_missing_message() {
        let result: Result<NostrRelayEvent, _> = bourne::parse_str(r#"["OK","eid",true]"#);
        assert!(result.is_err());
    }

    #[test]
    fn relay_event_rejects_client_only_tags() {
        let result: Result<NostrRelayEvent, _> = bourne::parse_str(r#"["REQ","sub",{}]"#);
        assert!(result.is_err());
        let result: Result<NostrRelayEvent, _> = bourne::parse_str(r#"["CLOSE","sub"]"#);
        assert!(result.is_err());
    }

    #[test]
    fn client_event_rejects_empty_array() {
        let result: Result<NostrClientEvent, _> = bourne::parse_str("[]");
        assert!(result.is_err());
    }

    #[test]
    fn client_event_rejects_unknown_tag() {
        let result: Result<NostrClientEvent, _> = bourne::parse_str(r#"["BOGUS","sub"]"#);
        assert!(result.is_err());
    }

    #[test]
    fn client_event_rejects_server_only_tags() {
        let result: Result<NostrClientEvent, _> = bourne::parse_str(r#"["EOSE","sub"]"#);
        assert!(result.is_err());
        let result: Result<NostrClientEvent, _> = bourne::parse_str(r#"["OK","eid",true,""]"#);
        assert!(result.is_err());
        let result: Result<NostrClientEvent, _> = bourne::parse_str(r#"["NOTICE","msg"]"#);
        assert!(result.is_err());
    }

    #[test]
    fn client_event_rejects_truncated_event() {
        let result: Result<NostrClientEvent, _> = bourne::parse_str(r#"["EVENT"]"#);
        assert!(result.is_err());
    }

    #[test]
    fn client_event_rejects_truncated_req() {
        let result: Result<NostrClientEvent, _> = bourne::parse_str(r#"["REQ"]"#);
        assert!(result.is_err());
        let result: Result<NostrClientEvent, _> = bourne::parse_str(r#"["REQ","sub"]"#);
        assert!(result.is_err());
    }

    #[test]
    fn client_event_rejects_truncated_close() {
        let result: Result<NostrClientEvent, _> = bourne::parse_str(r#"["CLOSE"]"#);
        assert!(result.is_err());
    }

    #[test]
    fn client_event_rejects_truncated_auth() {
        let result: Result<NostrClientEvent, _> = bourne::parse_str(r#"["AUTH"]"#);
        assert!(result.is_err());
    }

    #[test]
    fn client_event_rejects_event_trailing_data() {
        let note = sample_note();
        let note_json = bourne::to_string(&note).unwrap();
        let json = format!(r#"["EVENT",{note_json},"extra"]"#);
        let result: Result<NostrClientEvent, _> = bourne::parse_str(&json);
        assert!(result.is_err());
    }

    #[test]
    fn client_event_rejects_auth_trailing_data() {
        let note = sample_note();
        let note_json = bourne::to_string(&note).unwrap();
        let json = format!(r#"["AUTH",{note_json},"extra"]"#);
        let result: Result<NostrClientEvent, _> = bourne::parse_str(&json);
        assert!(result.is_err());
    }

    #[test]
    fn client_event_rejects_close_trailing_data() {
        let result: Result<NostrClientEvent, _> = bourne::parse_str(r#"["CLOSE","sub","extra"]"#);
        assert!(result.is_err());
    }

    #[test]
    fn client_event_rejects_req_trailing_data() {
        let result: Result<NostrClientEvent, _> = bourne::parse_str(r#"["REQ","sub",{},"extra"]"#);
        assert!(result.is_err());
    }

    #[test]
    fn fresh_sub_id_does_not_collide() {
        let sub = NostrSubscription::new().kind(1);
        let mut ids = std::collections::HashSet::new();
        for _ in 0..1024 {
            let NostrClientEvent::Subscribe(_, id, _) = NostrClientEvent::from(&sub) else {
                panic!("expected Subscribe variant");
            };
            assert!(ids.insert(id), "duplicate sub_id");
        }
    }

    #[test]
    fn fresh_sub_id_has_start_prefix_format() {
        let sub = NostrSubscription::new();
        let NostrClientEvent::Subscribe(_, a, _) = NostrClientEvent::from(&sub) else {
            unreachable!()
        };
        let NostrClientEvent::Subscribe(_, b, _) = NostrClientEvent::from(&sub) else {
            unreachable!()
        };
        let (pa, na) = a.split_once('-').expect("start_ns-counter format");
        let (pb, nb) = b.split_once('-').expect("start_ns-counter format");
        assert_eq!(pa, pb, "process-start prefix should be stable");
        assert_ne!(na, nb, "counter must advance");
    }
}