roam-sdk 0.4.0

Roam Research SDK and terminal UI client
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
use chrono::NaiveDate;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize)]
pub struct PullRequest {
    #[serde(rename = "eid")]
    pub eid: serde_json::Value,
    pub selector: String,
}

#[derive(Debug, Deserialize)]
pub struct PullResponse {
    pub result: serde_json::Value,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Block {
    pub uid: String,
    pub string: String,
    pub order: i64,
    #[serde(default)]
    pub children: Vec<Block>,
    #[serde(default)]
    pub open: bool,
    #[serde(default, skip_serializing)]
    pub refs: Vec<RefEntity>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RefEntity {
    pub uid: String,
    #[serde(default)]
    pub title: Option<String>,
    #[serde(default)]
    pub string: Option<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct DailyNote {
    pub date: NaiveDate,
    pub uid: String,
    pub title: String,
    pub blocks: Vec<Block>,
}

impl DailyNote {
    pub fn from_pull_response(date: NaiveDate, uid: String, result: &serde_json::Value) -> Self {
        let title = result
            .get(":node/title")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();

        let blocks = result
            .get(":block/children")
            .and_then(|v| v.as_array())
            .map(|arr| {
                let mut blocks: Vec<Block> = arr.iter().map(parse_block_from_json).collect();
                blocks.sort_by_key(|b| b.order);
                blocks
            })
            .unwrap_or_default();

        Self {
            date,
            uid,
            title,
            blocks,
        }
    }
}

fn parse_block_from_json(val: &serde_json::Value) -> Block {
    let uid = val
        .get(":block/uid")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .to_string();
    let string = val
        .get(":block/string")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .to_string();
    let order = val
        .get(":block/order")
        .and_then(|v| v.as_i64())
        .unwrap_or(0);
    let open = val
        .get(":block/open")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);

    let mut children: Vec<Block> = val
        .get(":block/children")
        .and_then(|v| v.as_array())
        .map(|arr| arr.iter().map(parse_block_from_json).collect())
        .unwrap_or_default();
    children.sort_by_key(|b| b.order);

    let refs: Vec<RefEntity> = val
        .get(":block/refs")
        .and_then(|v| v.as_array())
        .map(|arr| arr.iter().filter_map(parse_ref_entity).collect())
        .unwrap_or_default();

    Block {
        uid,
        string,
        order,
        children,
        open,
        refs,
    }
}

fn parse_ref_entity(val: &serde_json::Value) -> Option<RefEntity> {
    let uid = val.get(":block/uid").and_then(|v| v.as_str())?.to_string();
    let title = val
        .get(":node/title")
        .and_then(|v| v.as_str())
        .map(|s| s.to_string());
    let string = val
        .get(":block/string")
        .and_then(|v| v.as_str())
        .map(|s| s.to_string());
    Some(RefEntity { uid, title, string })
}

#[derive(Debug, Serialize)]
pub struct QueryRequest {
    pub query: String,
    pub args: Vec<serde_json::Value>,
}

#[derive(Debug, Deserialize)]
pub struct QueryResponse {
    pub result: Vec<Vec<serde_json::Value>>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct LinkedRefBlock {
    pub uid: String,
    pub string: String,
    pub page_title: String,
}

#[derive(Debug, Clone, PartialEq)]
pub struct LinkedRefGroup {
    pub page_title: String,
    pub blocks: Vec<LinkedRefBlock>,
}

pub fn parse_linked_refs(
    result: &[Vec<serde_json::Value>],
    current_page: &str,
) -> Vec<LinkedRefGroup> {
    let mut blocks: Vec<LinkedRefBlock> = Vec::new();

    // Each row is a tuple [uid, string, page_title] from the Datalog query
    for row in result {
        let uid = row
            .first()
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let string = row
            .get(1)
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let page_title = row
            .get(2)
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();

        if uid.is_empty() || page_title.is_empty() {
            continue;
        }
        // Filter self-references
        if page_title == current_page {
            continue;
        }

        blocks.push(LinkedRefBlock {
            uid,
            string,
            page_title,
        });
    }

    // Group by page title
    let mut groups: std::collections::BTreeMap<String, Vec<LinkedRefBlock>> =
        std::collections::BTreeMap::new();
    for block in blocks {
        groups
            .entry(block.page_title.clone())
            .or_default()
            .push(block);
    }

    // Sort blocks within each group by string for stable order
    // Groups already sorted alphabetically by BTreeMap
    groups
        .into_iter()
        .map(|(page_title, mut blocks)| {
            blocks.sort_by(|a, b| a.string.cmp(&b.string));
            LinkedRefGroup { page_title, blocks }
        })
        .collect()
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PageCreate {
    pub title: String,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub uid: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "action")]
#[allow(clippy::enum_variant_names)]
pub enum WriteAction {
    #[serde(rename = "create-block")]
    CreateBlock {
        location: BlockLocation,
        block: NewBlock,
    },
    #[serde(rename = "update-block")]
    UpdateBlock { block: BlockUpdate },
    #[serde(rename = "delete-block")]
    DeleteBlock { block: BlockRef },
    #[serde(rename = "move-block")]
    MoveBlock {
        block: BlockRef,
        location: BlockLocation,
    },
    #[serde(rename = "create-page")]
    CreatePage { page: PageCreate },
    #[serde(rename = "batch-actions")]
    BatchActions { actions: Vec<WriteAction> },
}

#[derive(Debug, Serialize, Deserialize)]
pub struct BlockLocation {
    #[serde(rename = "parent-uid")]
    pub parent_uid: String,
    pub order: OrderValue,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum OrderValue {
    Index(i64),
    Position(String),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct NewBlock {
    pub string: String,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub uid: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub open: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct BlockUpdate {
    pub uid: String,
    pub string: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct BlockRef {
    pub uid: String,
}

/// Parse an order string into an `OrderValue`.
/// Accepts "first", "last", numeric strings, or None (defaults to "last").
pub fn parse_order(order: Option<&str>) -> OrderValue {
    match order {
        None | Some("last") => OrderValue::Position("last".into()),
        Some("first") => OrderValue::Position("first".into()),
        Some(n) => n
            .parse::<i64>()
            .map(OrderValue::Index)
            .unwrap_or(OrderValue::Position("last".into())),
    }
}

/// Generate a short unique block UID compatible with Roam format.
pub fn generate_block_uid() -> String {
    use std::sync::atomic::{AtomicU32, Ordering};
    use std::time::{SystemTime, UNIX_EPOCH};

    static COUNTER: AtomicU32 = AtomicU32::new(0);

    const CHARS: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos() as u64;
    let count = COUNTER.fetch_add(1, Ordering::Relaxed) as u64;
    let seed = nanos
        .wrapping_mul(6364136223846793005)
        .wrapping_add(count ^ (std::process::id() as u64));

    let mut uid = String::with_capacity(9);
    let mut val = seed;
    for _ in 0..9 {
        uid.push(CHARS[(val % 62) as usize] as char);
        val /= 62;
        val = val.wrapping_mul(2862933555777941757).wrapping_add(nanos);
    }
    uid
}

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

    #[test]
    fn pull_request_serializes() {
        let req = PullRequest {
            eid: json!(["block/uid", "abc123"]),
            selector: "[:block/string :block/uid {:block/children ...}]".into(),
        };
        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["eid"], json!(["block/uid", "abc123"]));
        assert!(json["selector"].is_string());
    }

    #[test]
    fn pull_response_deserializes() {
        let raw =
            r#"{"result": {":block/uid": "abc123", ":block/string": "hello", ":block/order": 0}}"#;
        let resp: PullResponse = serde_json::from_str(raw).unwrap();
        assert_eq!(resp.result[":block/uid"], "abc123");
    }

    #[test]
    fn block_serde_roundtrip() {
        let block = Block {
            uid: "def456".into(),
            string: "Hello [[world]]".into(),
            order: 0,
            children: vec![Block {
                uid: "ghi789".into(),
                string: "Child block".into(),
                order: 0,
                children: vec![],
                open: true,
                refs: vec![],
            }],
            open: true,
            refs: vec![],
        };
        let json = serde_json::to_string(&block).unwrap();
        let deserialized: Block = serde_json::from_str(&json).unwrap();
        assert_eq!(block, deserialized);
        assert_eq!(deserialized.children.len(), 1);
    }

    #[test]
    fn block_deserializes_without_optional_fields() {
        let raw = r#"{"uid": "abc", "string": "test", "order": 0}"#;
        let block: Block = serde_json::from_str(raw).unwrap();
        assert!(block.children.is_empty());
        assert!(!block.open);
    }

    #[test]
    fn write_action_create_block_serializes() {
        let action = WriteAction::CreateBlock {
            location: BlockLocation {
                parent_uid: "page-uid".into(),
                order: OrderValue::Position("last".into()),
            },
            block: NewBlock {
                string: "New block content".into(),
                uid: None,
                open: None,
            },
        };
        let json = serde_json::to_value(&action).unwrap();
        assert_eq!(json["action"], "create-block");
        assert_eq!(json["location"]["parent-uid"], "page-uid");
        assert_eq!(json["location"]["order"], "last");
    }

    #[test]
    fn write_action_update_block_serializes() {
        let action = WriteAction::UpdateBlock {
            block: BlockUpdate {
                uid: "abc123".into(),
                string: "Updated content".into(),
            },
        };
        let json = serde_json::to_value(&action).unwrap();
        assert_eq!(json["action"], "update-block");
        assert_eq!(json["block"]["uid"], "abc123");
    }

    #[test]
    fn write_action_delete_block_serializes() {
        let action = WriteAction::DeleteBlock {
            block: BlockRef {
                uid: "abc123".into(),
            },
        };
        let json = serde_json::to_value(&action).unwrap();
        assert_eq!(json["action"], "delete-block");
        assert_eq!(json["block"]["uid"], "abc123");
    }

    #[test]
    fn write_action_move_block_serializes() {
        let action = WriteAction::MoveBlock {
            block: BlockRef {
                uid: "block1".into(),
            },
            location: BlockLocation {
                parent_uid: "new-parent".into(),
                order: OrderValue::Position("last".into()),
            },
        };
        let json = serde_json::to_value(&action).unwrap();
        assert_eq!(json["action"], "move-block");
        assert_eq!(json["block"]["uid"], "block1");
        assert_eq!(json["location"]["parent-uid"], "new-parent");
        assert_eq!(json["location"]["order"], "last");
    }

    #[test]
    fn order_value_index_serializes_as_number() {
        let order = OrderValue::Index(5);
        let json = serde_json::to_value(&order).unwrap();
        assert_eq!(json, 5);
    }

    #[test]
    fn order_value_position_serializes_as_string() {
        let order = OrderValue::Position("last".into());
        let json = serde_json::to_value(&order).unwrap();
        assert_eq!(json, "last");
    }

    #[test]
    fn daily_note_from_pull_response_parses_blocks() {
        let pull_result = json!({
            ":node/title": "February 21, 2026",
            ":block/uid": "02-21-2026",
            ":block/children": [
                {
                    ":block/uid": "block2",
                    ":block/string": "Second block",
                    ":block/order": 1,
                    ":block/open": true
                },
                {
                    ":block/uid": "block1",
                    ":block/string": "First block",
                    ":block/order": 0,
                    ":block/open": true
                }
            ]
        });

        let date = chrono::NaiveDate::from_ymd_opt(2026, 2, 21).unwrap();
        let note = DailyNote::from_pull_response(date, "02-21-2026".into(), &pull_result);

        assert_eq!(note.title, "February 21, 2026");
        assert_eq!(note.uid, "02-21-2026");
        assert_eq!(note.date, date);
        assert_eq!(note.blocks.len(), 2);
        assert_eq!(note.blocks[0].string, "First block");
        assert_eq!(note.blocks[1].string, "Second block");
    }

    #[test]
    fn daily_note_from_pull_response_with_nested_children() {
        let pull_result = json!({
            ":node/title": "February 21, 2026",
            ":block/uid": "02-21-2026",
            ":block/children": [
                {
                    ":block/uid": "parent",
                    ":block/string": "Parent block",
                    ":block/order": 0,
                    ":block/open": true,
                    ":block/children": [
                        {
                            ":block/uid": "child2",
                            ":block/string": "Child B",
                            ":block/order": 1
                        },
                        {
                            ":block/uid": "child1",
                            ":block/string": "Child A",
                            ":block/order": 0
                        }
                    ]
                }
            ]
        });

        let date = chrono::NaiveDate::from_ymd_opt(2026, 2, 21).unwrap();
        let note = DailyNote::from_pull_response(date, "02-21-2026".into(), &pull_result);

        assert_eq!(note.blocks.len(), 1);
        assert_eq!(note.blocks[0].children.len(), 2);
        assert_eq!(note.blocks[0].children[0].string, "Child A");
        assert_eq!(note.blocks[0].children[1].string, "Child B");
    }

    #[test]
    fn daily_note_from_empty_pull_response() {
        let pull_result = json!({});
        let date = chrono::NaiveDate::from_ymd_opt(2026, 2, 21).unwrap();
        let note = DailyNote::from_pull_response(date, "02-21-2026".into(), &pull_result);

        assert!(note.blocks.is_empty());
        assert_eq!(note.title, "");
        assert_eq!(note.blocks.len(), 0);
    }

    #[test]
    fn daily_note_from_pull_response_no_children() {
        let pull_result = json!({
            ":node/title": "February 21, 2026",
            ":block/uid": "02-21-2026"
        });
        let date = chrono::NaiveDate::from_ymd_opt(2026, 2, 21).unwrap();
        let note = DailyNote::from_pull_response(date, "02-21-2026".into(), &pull_result);

        assert!(note.blocks.is_empty());
        assert_eq!(note.title, "February 21, 2026");
    }

    #[test]
    fn block_parses_refs_from_pull_response() {
        let pull_result = json!({
            ":node/title": "February 21, 2026",
            ":block/uid": "02-21-2026",
            ":block/children": [
                {
                    ":block/uid": "block1",
                    ":block/string": "Links to [[ProjectX]]",
                    ":block/order": 0,
                    ":block/open": true,
                    ":block/refs": [
                        {
                            ":block/uid": "page-uid-1",
                            ":node/title": "ProjectX"
                        }
                    ]
                }
            ]
        });

        let date = chrono::NaiveDate::from_ymd_opt(2026, 2, 21).unwrap();
        let note = DailyNote::from_pull_response(date, "02-21-2026".into(), &pull_result);

        assert_eq!(note.blocks[0].refs.len(), 1);
        assert_eq!(note.blocks[0].refs[0].uid, "page-uid-1");
        assert_eq!(note.blocks[0].refs[0].title.as_deref(), Some("ProjectX"));
    }

    #[test]
    fn block_parses_without_refs() {
        let pull_result = json!({
            ":node/title": "February 21, 2026",
            ":block/uid": "02-21-2026",
            ":block/children": [
                {
                    ":block/uid": "block1",
                    ":block/string": "No links here",
                    ":block/order": 0
                }
            ]
        });

        let date = chrono::NaiveDate::from_ymd_opt(2026, 2, 21).unwrap();
        let note = DailyNote::from_pull_response(date, "02-21-2026".into(), &pull_result);

        assert!(note.blocks[0].refs.is_empty());
    }

    #[test]
    fn refs_not_serialized_in_block_json() {
        let block = Block {
            uid: "b1".into(),
            string: "test".into(),
            order: 0,
            children: vec![],
            open: true,
            refs: vec![RefEntity {
                uid: "ref1".into(),
                title: Some("Page".into()),
                string: None,
            }],
        };
        let json = serde_json::to_value(&block).unwrap();
        assert!(json.get("refs").is_none());
    }

    #[test]
    fn query_request_serializes() {
        let req = QueryRequest {
            query: "[:find ?b :where [?b :block/string]]".into(),
            args: vec![],
        };
        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["query"], "[:find ?b :where [?b :block/string]]");
        // args should always be present, even when empty
        assert_eq!(json["args"], json!([]));
    }

    #[test]
    fn query_request_serializes_with_args() {
        let req = QueryRequest {
            query: "[:find ?b :in $ ?title :where [?b :node/title ?title]]".into(),
            args: vec![json!("My Page")],
        };
        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["args"], json!(["My Page"]));
    }

    #[test]
    fn query_response_deserializes() {
        let raw = r#"{"result": [["abc", "hello text", "My Page"]]}"#;
        let resp: QueryResponse = serde_json::from_str(raw).unwrap();
        assert_eq!(resp.result.len(), 1);
        assert_eq!(resp.result[0].len(), 3);
        assert_eq!(resp.result[0][0], "abc");
    }

    #[test]
    fn parse_linked_refs_groups_by_page() {
        // Each row is a tuple [uid, string, page_title]
        let result = vec![
            vec![json!("b1"), json!("mentions [[Target]]"), json!("Page A")],
            vec![json!("b2"), json!("also refs [[Target]]"), json!("Page B")],
            vec![
                json!("b3"),
                json!("another ref [[Target]]"),
                json!("Page A"),
            ],
        ];

        let groups = parse_linked_refs(&result, "Target");
        assert_eq!(groups.len(), 2);
        assert_eq!(groups[0].page_title, "Page A");
        assert_eq!(groups[0].blocks.len(), 2);
        assert_eq!(groups[1].page_title, "Page B");
        assert_eq!(groups[1].blocks.len(), 1);
    }

    #[test]
    fn parse_linked_refs_filters_self_refs() {
        let result = vec![
            vec![json!("b1"), json!("self ref [[MyPage]]"), json!("MyPage")],
            vec![
                json!("b2"),
                json!("external ref [[MyPage]]"),
                json!("Other Page"),
            ],
        ];

        let groups = parse_linked_refs(&result, "MyPage");
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].page_title, "Other Page");
    }

    #[test]
    fn parse_linked_refs_handles_empty() {
        let groups = parse_linked_refs(&[], "AnyPage");
        assert!(groups.is_empty());
    }

    #[test]
    fn parse_linked_refs_skips_missing_fields() {
        let result = vec![
            vec![json!("b1"), json!("text")],              // no page_title
            vec![json!(""), json!("text"), json!("Page")], // empty uid
            vec![json!("b3"), json!("text"), json!("")],   // empty page_title
        ];

        let groups = parse_linked_refs(&result, "X");
        assert!(groups.is_empty());
    }

    #[test]
    fn write_action_create_page_serializes() {
        let action = WriteAction::CreatePage {
            page: PageCreate {
                title: "My New Page".into(),
                uid: Some("page-uid-123".into()),
            },
        };
        let json = serde_json::to_value(&action).unwrap();
        assert_eq!(json["action"], "create-page");
        assert_eq!(json["page"]["title"], "My New Page");
        assert_eq!(json["page"]["uid"], "page-uid-123");
    }

    #[test]
    fn write_action_create_page_without_uid() {
        let action = WriteAction::CreatePage {
            page: PageCreate {
                title: "Auto UID Page".into(),
                uid: None,
            },
        };
        let json = serde_json::to_value(&action).unwrap();
        assert_eq!(json["action"], "create-page");
        assert_eq!(json["page"]["title"], "Auto UID Page");
        assert!(json["page"].get("uid").is_none());
    }

    #[test]
    fn parse_linked_refs_sorts_blocks_within_group() {
        let result = vec![
            vec![json!("b1"), json!("Zebra [[T]]"), json!("Page")],
            vec![json!("b2"), json!("Alpha [[T]]"), json!("Page")],
        ];

        let groups = parse_linked_refs(&result, "T");
        assert_eq!(groups[0].blocks[0].string, "Alpha [[T]]");
        assert_eq!(groups[0].blocks[1].string, "Zebra [[T]]");
    }
}