wokhei 0.3.0

Agent-first CLI for creating and publishing Decentralized List events on Nostr
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
use std::collections::HashSet;
use std::time::Duration;

use nostr_sdk::prelude::*;
use serde_json::json;

use agcli::{CommandError, CommandOutput, NextAction};

use crate::error::AppError;

const QUERY_TIMEOUT: Duration = Duration::from_secs(10);
const FETCH_PAGE_SIZE: usize = 500;

fn event_to_json(event: &Event) -> serde_json::Value {
    let tags: Vec<Vec<String>> = event
        .tags
        .iter()
        .map(|t| t.as_slice().iter().map(ToString::to_string).collect())
        .collect();

    let mut obj = json!({
        "event_id": event.id.to_hex(),
        "kind": event.kind.as_u16(),
        "pubkey": event.pubkey.to_hex(),
        "created_at": event.created_at.as_secs(),
        "tags": tags,
        "content": event.content,
        "sig": event.sig.to_string(),
    });

    // Extract common DCoSL fields from tags for convenience
    for tag in event.tags.iter() {
        let parts = tag.as_slice();
        if parts.len() >= 2 {
            let key = parts[0].as_str();
            match key {
                "names" => {
                    obj["name"] = json!(parts[1].as_str());
                    if parts.len() >= 3 {
                        obj["plural_name"] = json!(parts[2].as_str());
                        obj["names"] = json!([parts[1].as_str(), parts[2].as_str()]);
                    }
                }
                "titles" => {
                    obj["title"] = json!(parts[1].as_str());
                    if parts.len() >= 3 {
                        obj["plural_title"] = json!(parts[2].as_str());
                        obj["titles"] = json!([parts[1].as_str(), parts[2].as_str()]);
                    }
                }
                "description" => {
                    obj["description"] = json!(parts[1].as_str());
                }
                "d" => {
                    let pubkey_hex = event.pubkey.to_hex();
                    let d_val = parts[1].as_str();
                    obj["coordinate"] =
                        json!(format!("{}:{}:{}", event.kind.as_u16(), pubkey_hex, d_val));
                }
                _ => {}
            }
        }
    }

    obj
}

fn sort_event_json_desc(events: &mut [serde_json::Value]) {
    events.sort_by(|a, b| {
        let a_created = a["created_at"].as_u64().unwrap_or(0);
        let b_created = b["created_at"].as_u64().unwrap_or(0);
        let a_id = a["event_id"].as_str().unwrap_or("");
        let b_id = b["event_id"].as_str().unwrap_or("");

        b_created.cmp(&a_created).then_with(|| a_id.cmp(b_id))
    });
}

fn sort_events_desc(events: &mut [Event]) {
    events.sort_by(|a, b| {
        b.created_at
            .as_secs()
            .cmp(&a.created_at.as_secs())
            .then_with(|| a.id.to_hex().cmp(&b.id.to_hex()))
    });
}

fn paginate<T: Clone>(values: &[T], offset: usize, limit: usize) -> Vec<T> {
    if offset >= values.len() || limit == 0 {
        return Vec::new();
    }

    let end = offset.saturating_add(limit).min(values.len());
    values[offset..end].to_vec()
}

fn header_query_command(
    relay: &str,
    author: Option<&String>,
    tag: Option<&String>,
    name: Option<&String>,
    offset: usize,
    limit: usize,
) -> String {
    let mut parts = vec![
        "wokhei list-headers".to_string(),
        format!("--relay={relay}"),
    ];

    if let Some(author) = author {
        parts.push(format!("--author={author}"));
    }
    if let Some(tag) = tag {
        parts.push(format!("--tag={tag}"));
    }
    if let Some(name) = name {
        parts.push(format!("--name={name}"));
    }

    parts.push(format!("--offset={offset}"));
    parts.push(format!("--limit={limit}"));

    parts.join(" ")
}

fn item_add_command(relay: &str, header_ref: &str, coordinate_mode: bool) -> String {
    if coordinate_mode {
        format!("wokhei add-item --relay={relay} --header-coordinate={header_ref} --resource=<url>")
    } else {
        format!("wokhei add-item --relay={relay} --header={header_ref} --resource=<url>")
    }
}

fn header_d_tag(header_event: &Event) -> Option<String> {
    header_event.tags.iter().find_map(|t| {
        let parts = t.as_slice();
        if parts.first().map(String::as_str) == Some("d") {
            parts.get(1).cloned()
        } else {
            None
        }
    })
}

async fn connect_client(relay: &str) -> Result<Client, AppError> {
    let client = Client::default();
    client
        .add_relay(relay)
        .await
        .map_err(|_| AppError::RelayUnreachable {
            url: relay.to_string(),
        })?;
    client.connect().await;
    Ok(client)
}

fn build_header_filter(
    author: Option<&String>,
    tag: Option<&String>,
) -> Result<Filter, CommandError> {
    let mut filter = Filter::new().kinds(vec![Kind::Custom(9998), Kind::Custom(39998)]);

    if let Some(author_hex) = author {
        let pk = PublicKey::parse(author_hex).map_err(|_| {
            CommandError::from(AppError::InvalidEventId {
                id: author_hex.clone(),
            })
        })?;
        filter = filter.author(pk);
    }

    if let Some(t) = tag {
        filter = filter.hashtag(t);
    }

    Ok(filter)
}

async fn fetch_all_events(
    client: &Client,
    relay: &str,
    base_filter: Filter,
) -> Result<Vec<Event>, CommandError> {
    let mut all_events: Vec<Event> = Vec::new();
    let mut seen_ids: HashSet<String> = HashSet::new();
    let mut until_secs: Option<u64> = None;

    loop {
        let mut filter = base_filter.clone().limit(FETCH_PAGE_SIZE);
        if let Some(secs) = until_secs {
            filter = filter.until(Timestamp::from_secs(secs));
        }

        let batch = client
            .fetch_events(filter, QUERY_TIMEOUT)
            .await
            .map_err(|_| {
                CommandError::from(AppError::RelayUnreachable {
                    url: relay.to_string(),
                })
            })?;

        if batch.is_empty() {
            break;
        }

        let mut oldest_created_at = u64::MAX;
        for event in batch.iter() {
            oldest_created_at = oldest_created_at.min(event.created_at.as_secs());
            let event_id = event.id.to_hex();
            if seen_ids.insert(event_id) {
                all_events.push(event.clone());
            }
        }

        if batch.len() < FETCH_PAGE_SIZE || oldest_created_at == 0 {
            break;
        }

        let next_until = oldest_created_at.saturating_sub(1);
        if until_secs == Some(next_until) {
            break;
        }
        until_secs = Some(next_until);
    }

    Ok(all_events)
}

async fn count_filter(client: &Client, relay: &str, filter: Filter) -> Result<usize, CommandError> {
    let relay_handle = client.relay(relay).await.map_err(|_| {
        CommandError::from(AppError::RelayUnreachable {
            url: relay.to_string(),
        })
    })?;

    if let Ok(count) = relay_handle
        .count_events(filter.clone(), QUERY_TIMEOUT)
        .await
    {
        return Ok(count);
    }

    let events = fetch_all_events(client, relay, filter).await?;
    Ok(events.len())
}

pub async fn list_headers(
    relay: String,
    author: Option<String>,
    tag: Option<String>,
    name: Option<String>,
    offset: usize,
    limit: usize,
) -> Result<CommandOutput, CommandError> {
    let client = connect_client(&relay).await.map_err(CommandError::from)?;

    let headers_result = async {
        let filter = build_header_filter(author.as_ref(), tag.as_ref())?;
        let events = fetch_all_events(&client, &relay, filter).await?;

        let mut headers: Vec<serde_json::Value> = events.iter().map(event_to_json).collect();

        // Client-side name substring filter (Nostr can't do substring search)
        if let Some(ref name_filter) = name {
            let lower = name_filter.to_lowercase();
            headers.retain(|h| {
                h["name"]
                    .as_str()
                    .is_some_and(|n| n.to_lowercase().contains(&lower))
            });
        }

        sort_event_json_desc(&mut headers);

        let total = headers.len();

        if total == 0 && offset == 0 {
            return Err(CommandError::from(AppError::NoResults).next_actions(vec![
                NextAction::new(
                    format!(
                        "wokhei create-header --relay={relay} --name=<singular> --plural=<plural>"
                    ),
                    "Create a new list header",
                ),
            ]));
        }

        let page_headers = paginate(&headers, offset, limit);
        let has_more = limit > 0 && offset.saturating_add(limit) < total;
        let page_count = page_headers.len();

        let mut actions = Vec::new();

        if let Some(first) = page_headers.first() {
            let first_id = first["event_id"].as_str().unwrap_or("");
            if !first_id.is_empty() {
                actions.push(NextAction::new(
                    format!("wokhei list-items --relay={relay} {first_id}"),
                    "List items for the first header in this page",
                ));
            }
        }

        if offset > 0 {
            let step = limit.max(1);
            let prev_offset = offset.saturating_sub(step);
            actions.push(NextAction::new(
                header_query_command(
                    &relay,
                    author.as_ref(),
                    tag.as_ref(),
                    name.as_ref(),
                    prev_offset,
                    limit,
                ),
                "Go to the previous page",
            ));
        }

        if has_more {
            actions.push(NextAction::new(
                header_query_command(
                    &relay,
                    author.as_ref(),
                    tag.as_ref(),
                    name.as_ref(),
                    offset.saturating_add(limit),
                    limit,
                ),
                "Go to the next page",
            ));
        }

        if total > 0 && page_count == 0 {
            let step = limit.max(1);
            let last_offset = ((total - 1) / step) * step;
            actions.push(NextAction::new(
                header_query_command(
                    &relay,
                    author.as_ref(),
                    tag.as_ref(),
                    name.as_ref(),
                    last_offset,
                    limit,
                ),
                "Jump to the last non-empty page",
            ));
        }

        actions.push(NextAction::new(
            format!("wokhei create-header --relay={relay} --name=<singular> --plural=<plural>"),
            "Create a new list header",
        ));

        Ok(CommandOutput::new(json!({
            "total": total,
            "count": page_count,
            "offset": offset,
            "limit": limit,
            "has_more": has_more,
            "headers": page_headers,
        }))
        .next_actions(actions))
    }
    .await;

    client.disconnect().await;
    headers_result
}

pub async fn list_items(
    relay: String,
    header_id: Option<String>,
    header_coordinate: Option<String>,
    limit: usize,
) -> Result<CommandOutput, CommandError> {
    let client = connect_client(&relay).await.map_err(CommandError::from)?;

    let (all_items, header_ref, coordinate_mode) = if let Some(ref coord_str) = header_coordinate {
        let normalized_ref = normalize_coordinate_ref(coord_str)?;
        let items = fetch_items_by_z(&client, &normalized_ref, limit).await;
        (items, normalized_ref, true)
    } else {
        let id_str = header_id.as_deref().unwrap_or("");
        let event_id = EventId::parse(id_str).map_err(|_| {
            CommandError::from(AppError::InvalidEventId {
                id: id_str.to_string(),
            })
        })?;
        let header_event = fetch_header_event_by_id(&client, &relay, event_id).await?;
        let (resolved_ref, resolved_coordinate_mode) = z_ref_for_header_event(&header_event)?;
        let items = fetch_items_by_z(&client, &resolved_ref, limit).await;
        (items, resolved_ref, resolved_coordinate_mode)
    };

    client.disconnect().await;

    let add_item_cmd = item_add_command(&relay, &header_ref, coordinate_mode);

    if all_items.is_empty() {
        return Err(
            CommandError::from(AppError::NoResults).next_actions(vec![NextAction::new(
                add_item_cmd,
                "Add an item to this list",
            )]),
        );
    }

    let actions = vec![
        NextAction::new(add_item_cmd, "Add another item to this list"),
        NextAction::new(
            format!(
                "wokhei inspect --relay={relay} {}",
                all_items[0]["event_id"].as_str().unwrap_or("")
            ),
            "Inspect the first item",
        ),
    ];

    Ok(CommandOutput::new(json!({
        "count": all_items.len(),
        "header_ref": header_ref,
        "items": all_items,
    }))
    .next_actions(actions))
}

fn normalize_coordinate_ref(coord_str: &str) -> Result<String, CommandError> {
    let parts: Vec<&str> = coord_str.splitn(3, ':').collect();
    if parts.len() != 3 {
        return Err(CommandError::from(AppError::InvalidCoordinate {
            input: coord_str.to_string(),
        }));
    }
    let kind_num: u16 = parts[0].parse().map_err(|_| {
        CommandError::from(AppError::InvalidCoordinate {
            input: coord_str.to_string(),
        })
    })?;
    if kind_num != 39998 {
        return Err(CommandError::from(AppError::InvalidCoordinate {
            input: coord_str.to_string(),
        }));
    }
    let pubkey = PublicKey::parse(parts[1]).map_err(|_| {
        CommandError::from(AppError::InvalidCoordinate {
            input: coord_str.to_string(),
        })
    })?;
    let d_tag = parts[2];

    Ok(format!("39998:{}:{}", pubkey.to_hex(), d_tag))
}

async fn fetch_header_event_by_id(
    client: &Client,
    relay: &str,
    event_id: EventId,
) -> Result<Event, CommandError> {
    let filter = Filter::new().id(event_id).limit(1);
    let header_events = client
        .fetch_events(filter, QUERY_TIMEOUT)
        .await
        .map_err(|_| {
            CommandError::from(AppError::RelayUnreachable {
                url: relay.to_string(),
            })
        })?;

    header_events.into_iter().next().ok_or_else(|| {
        CommandError::from(AppError::HeaderNotFound {
            event_id: event_id.to_hex(),
        })
    })
}

fn z_ref_for_header_event(header_event: &Event) -> Result<(String, bool), CommandError> {
    match header_event.kind {
        Kind::Custom(9998) => Ok((header_event.id.to_hex(), false)),
        Kind::Custom(39998) => {
            let d_val = header_d_tag(header_event)
                .ok_or_else(|| CommandError::from(AppError::HeaderMissingDTag))?;
            Ok((
                format!("39998:{}:{}", header_event.pubkey.to_hex(), d_val),
                true,
            ))
        }
        _ => Err(CommandError::new(
            "header reference must point to a list header (kind 9998 or 39998)",
            "INVALID_ARGS",
            "Provide a list header ID, or use --header-coordinate=<39998:pubkey:d-tag>",
        )),
    }
}

async fn fetch_items_by_z(client: &Client, z_ref: &str, limit: usize) -> Vec<serde_json::Value> {
    let filter = Filter::new()
        .kinds(vec![Kind::Custom(9999), Kind::Custom(39999)])
        .custom_tag(SingleLetterTag::lowercase(Alphabet::Z), z_ref.to_string())
        .limit(limit);

    let events = client
        .fetch_events(filter, QUERY_TIMEOUT)
        .await
        .unwrap_or_default();

    events.iter().map(event_to_json).collect()
}

async fn fetch_items_for_header_event(
    client: &Client,
    relay: &str,
    header_event: &Event,
) -> Result<Vec<Event>, CommandError> {
    let (z_ref, _) = z_ref_for_header_event(header_event)?;
    let z_filter = Filter::new()
        .kinds(vec![Kind::Custom(9999), Kind::Custom(39999)])
        .custom_tag(SingleLetterTag::lowercase(Alphabet::Z), z_ref);
    let mut items = fetch_all_events(client, relay, z_filter).await?;
    sort_events_desc(&mut items);
    Ok(items)
}

pub async fn count(relay: String) -> Result<CommandOutput, CommandError> {
    let client = connect_client(&relay).await.map_err(CommandError::from)?;

    let result = async {
        let headers_total = count_filter(
            &client,
            &relay,
            Filter::new().kinds(vec![Kind::Custom(9998), Kind::Custom(39998)]),
        )
        .await?;
        let headers_regular = count_filter(
            &client,
            &relay,
            Filter::new().kinds(vec![Kind::Custom(9998)]),
        )
        .await?;
        let headers_addressable = count_filter(
            &client,
            &relay,
            Filter::new().kinds(vec![Kind::Custom(39998)]),
        )
        .await?;

        let items_total = count_filter(
            &client,
            &relay,
            Filter::new().kinds(vec![Kind::Custom(9999), Kind::Custom(39999)]),
        )
        .await?;
        let items_regular = count_filter(
            &client,
            &relay,
            Filter::new().kinds(vec![Kind::Custom(9999)]),
        )
        .await?;
        let items_addressable = count_filter(
            &client,
            &relay,
            Filter::new().kinds(vec![Kind::Custom(39999)]),
        )
        .await?;

        let actions = vec![
            NextAction::new(
                format!("wokhei list-headers --relay={relay}"),
                "List headers on this relay",
            ),
            NextAction::new(
                format!("wokhei export --relay={relay}"),
                "Export full header and item backup",
            ),
        ];

        Ok(CommandOutput::new(json!({
            "relay": relay,
            "headers": {
                "total": headers_total,
                "regular": headers_regular,
                "addressable": headers_addressable,
            },
            "items": {
                "total": items_total,
                "regular": items_regular,
                "addressable": items_addressable,
            }
        }))
        .next_actions(actions))
    }
    .await;

    client.disconnect().await;
    result
}

pub async fn export(relay: String) -> Result<CommandOutput, CommandError> {
    let client = connect_client(&relay).await.map_err(CommandError::from)?;

    let result = async {
        let header_filter = Filter::new().kinds(vec![Kind::Custom(9998), Kind::Custom(39998)]);
        let mut header_events = fetch_all_events(&client, &relay, header_filter).await?;
        sort_events_desc(&mut header_events);

        let mut exported_headers = Vec::with_capacity(header_events.len());
        let mut total_items = 0usize;

        for header_event in &header_events {
            let items = fetch_items_for_header_event(&client, &relay, header_event).await?;
            total_items = total_items.saturating_add(items.len());

            let item_json: Vec<serde_json::Value> = items.iter().map(event_to_json).collect();

            exported_headers.push(json!({
                "header": event_to_json(header_event),
                "items_count": item_json.len(),
                "items": item_json,
            }));
        }

        let actions = vec![
            NextAction::new(
                format!("wokhei count --relay={relay}"),
                "Get quick relay counts",
            ),
            NextAction::new(
                format!("wokhei list-headers --relay={relay}"),
                "Inspect exported headers via paged query",
            ),
        ];

        Ok(CommandOutput::new(json!({
            "relay": relay,
            "exported_at": Timestamp::now().as_secs(),
            "counts": {
                "headers": exported_headers.len(),
                "items": total_items,
            },
            "headers": exported_headers,
        }))
        .next_actions(actions))
    }
    .await;

    client.disconnect().await;
    result
}

pub async fn inspect(relay: String, event_id_str: String) -> Result<CommandOutput, CommandError> {
    let event_id = EventId::parse(&event_id_str).map_err(|_| {
        CommandError::from(AppError::InvalidEventId {
            id: event_id_str.clone(),
        })
    })?;

    let client = connect_client(&relay).await.map_err(CommandError::from)?;

    let filter = Filter::new().id(event_id).limit(1);
    let events = client
        .fetch_events(filter, QUERY_TIMEOUT)
        .await
        .map_err(|_| CommandError::from(AppError::RelayUnreachable { url: relay.clone() }));

    let events = match events {
        Ok(ev) => {
            client.disconnect().await;
            ev
        }
        Err(e) => {
            client.disconnect().await;
            return Err(e);
        }
    };

    let event = events.into_iter().next().ok_or_else(|| {
        CommandError::from(AppError::HeaderNotFound {
            event_id: event_id_str.clone(),
        })
        .next_actions(vec![NextAction::new(
            format!("wokhei list-headers --relay={relay}"),
            "List available headers",
        )])
    })?;

    let ev_json = event_to_json(&event);
    let kind = event.kind.as_u16();

    let mut actions = vec![];

    // Context-sensitive next actions
    if kind == 9998 || kind == 39998 {
        actions.push(NextAction::new(
            format!("wokhei list-items --relay={relay} {event_id_str}"),
            "List items in this list",
        ));
        actions.push(NextAction::new(
            format!("wokhei add-item --relay={relay} --header={event_id_str} --resource=<url>"),
            "Add an item to this list",
        ));
    }

    actions.push(NextAction::new(
        format!("wokhei delete --relay={relay} {event_id_str}"),
        "Delete this event (NIP-09 request)",
    ));

    Ok(CommandOutput::new(ev_json).next_actions(actions))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_event(kind: Kind, content: &str, tags: Vec<Tag>) -> Event {
        let keys = Keys::generate();
        EventBuilder::new(kind, content)
            .tags(tags)
            .sign_with_keys(&keys)
            .unwrap()
    }

    #[test]
    fn event_to_json_basic_fields() {
        let event = make_event(Kind::Custom(9998), "hello", vec![]);
        let j = event_to_json(&event);
        assert!(j["event_id"].is_string());
        assert_eq!(j["kind"], 9998);
        assert!(j["pubkey"].is_string());
        assert!(j["created_at"].is_number());
        assert!(j["sig"].is_string());
        assert_eq!(j["content"], "hello");
        assert!(j["tags"].is_array());
    }

    #[test]
    fn event_to_json_names_tag_extracts_singular_and_plural() {
        let tags = vec![Tag::custom(TagKind::custom("names"), ["mylist", "mylists"])];
        let event = make_event(Kind::Custom(9998), "", tags);
        let j = event_to_json(&event);
        assert_eq!(j["name"], "mylist");
        assert_eq!(j["plural_name"], "mylists");
        assert_eq!(j["names"], json!(["mylist", "mylists"]));
    }

    #[test]
    fn event_to_json_single_name_sets_only_singular_name() {
        let tags = vec![Tag::custom(TagKind::custom("names"), ["mylist"])];
        let event = make_event(Kind::Custom(9998), "", tags);
        let j = event_to_json(&event);
        assert_eq!(j["name"], "mylist");
        assert!(j.get("plural_name").is_none());
        assert!(j.get("names").is_none());
    }

    #[test]
    fn event_to_json_titles_tag_extracted() {
        let tags = vec![Tag::custom(
            TagKind::custom("titles"),
            ["My List", "My Lists"],
        )];
        let event = make_event(Kind::Custom(9998), "", tags);
        let j = event_to_json(&event);
        assert_eq!(j["title"], "My List");
        assert_eq!(j["plural_title"], "My Lists");
        assert_eq!(j["titles"], json!(["My List", "My Lists"]));
    }

    #[test]
    fn event_to_json_description_extracted() {
        let tags = vec![Tag::custom(
            TagKind::custom("description"),
            ["A description"],
        )];
        let event = make_event(Kind::Custom(9998), "", tags);
        let j = event_to_json(&event);
        assert_eq!(j["description"], "A description");
    }

    #[test]
    fn event_to_json_d_tag_creates_coordinate() {
        let keys = Keys::generate();
        let tags = vec![Tag::identifier("my-list")];
        let event = EventBuilder::new(Kind::Custom(39998), "")
            .tags(tags)
            .sign_with_keys(&keys)
            .unwrap();
        let j = event_to_json(&event);
        let coord = j["coordinate"].as_str().unwrap();
        assert!(coord.starts_with("39998:"));
        assert!(coord.ends_with(":my-list"));
        assert!(coord.contains(&keys.public_key().to_hex()));
    }

    #[test]
    fn event_to_json_unknown_tags_dont_pollute_top_level() {
        let tags = vec![Tag::custom(TagKind::custom("weird"), ["val"])];
        let event = make_event(Kind::Custom(9998), "", tags);
        let j = event_to_json(&event);
        assert!(j.get("weird").is_none());
    }

    #[test]
    fn event_to_json_content_preserved() {
        let event = make_event(Kind::Custom(9999), r#"{"key":"val"}"#, vec![]);
        let j = event_to_json(&event);
        assert_eq!(j["content"], r#"{"key":"val"}"#);
    }

    #[test]
    fn event_to_json_tags_array_structure() {
        let tags = vec![
            Tag::custom(TagKind::custom("r"), ["https://example.com"]),
            Tag::custom(TagKind::custom("z"), ["39998:deadbeef:my-list"]),
        ];
        let event = make_event(Kind::Custom(9999), "", tags);
        let j = event_to_json(&event);
        let tags_arr = j["tags"].as_array().unwrap();
        assert_eq!(tags_arr.len(), 2);
        assert_eq!(tags_arr[0][0], "r");
        assert_eq!(tags_arr[0][1], "https://example.com");
        assert_eq!(tags_arr[1][0], "z");
        assert_eq!(tags_arr[1][1], "39998:deadbeef:my-list");
    }

    #[test]
    fn paginate_returns_expected_window() {
        let values = vec![1, 2, 3, 4, 5];
        assert_eq!(paginate(&values, 1, 2), vec![2, 3]);
    }

    #[test]
    fn paginate_returns_empty_when_offset_out_of_range() {
        let values = vec![1, 2, 3];
        assert!(paginate(&values, 3, 10).is_empty());
    }

    #[test]
    fn paginate_returns_empty_when_limit_zero() {
        let values = vec![1, 2, 3];
        assert!(paginate(&values, 0, 0).is_empty());
    }

    #[test]
    fn sort_event_json_orders_by_created_at_desc_then_id() {
        let mut rows = vec![
            json!({"event_id": "b", "created_at": 100}),
            json!({"event_id": "a", "created_at": 100}),
            json!({"event_id": "c", "created_at": 120}),
        ];

        sort_event_json_desc(&mut rows);

        assert_eq!(rows[0]["event_id"], "c");
        assert_eq!(rows[1]["event_id"], "a");
        assert_eq!(rows[2]["event_id"], "b");
    }
}