helix-graph-algorithms 0.1.0

Storage-independent native graph representation and algorithms for Helix SDKs
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
//! Validation boundary for graph-query response bytes.
//!
//! SDKs own query construction. This module owns the shared response aliases
//! and turns the resulting bytes into a graph only after every row and
//! topology invariant has been validated.

use std::collections::{BTreeMap, BTreeSet};

use serde::Deserialize;
use serde_json::Value;
use thiserror::Error;

use crate::{Edge, ExternalId, Graph, GraphError, GraphKind, IdentitySelection, Node};

/// Prefix reserved for graph loader projection aliases.
pub const PRIVATE_PREFIX: &str = "__helix_graph_";
/// Projected internal node ID alias.
pub const NODE_ID: &str = "__helix_graph_node_id";
/// Projected public external node ID alias.
pub const EXTERNAL_ID: &str = "__helix_graph_external_id";
/// Projected node label alias.
pub const NODE_LABEL: &str = "__helix_graph_node_label";
/// Projected stable edge ID alias.
pub const EDGE_ID: &str = "__helix_graph_edge_id";
/// Projected Graphify multigraph key alias.
pub const EDGE_KEY: &str = "__helix_graph_edge_key";
/// Projected internal source-node ID alias.
pub const EDGE_SOURCE: &str = "__helix_graph_edge_source";
/// Projected internal target-node ID alias.
pub const EDGE_TARGET: &str = "__helix_graph_edge_target";
/// Projected edge label alias.
pub const EDGE_LABEL: &str = "__helix_graph_edge_label";
/// Projected algorithm weight alias.
pub const EDGE_WEIGHT: &str = "__helix_graph_edge_weight";

/// Metadata required to interpret one graph query response.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GraphLoadSpec {
    /// Declared topology contract of the constructed graph.
    pub kind: GraphKind,
    /// How projected node identities are decoded.
    pub node_identity: IdentitySelection,
    /// How an optional projected Graphify edge key is decoded.
    pub edge_key_identity: Option<IdentitySelection>,
    /// Maximum complete node result size.
    pub node_limit: Option<usize>,
    /// Maximum complete edge result size.
    pub edge_limit: Option<usize>,
}

/// Failure to validate raw graph response bytes.
#[derive(Debug, Error)]
pub enum GraphLoadError {
    /// The response is not the expected two-array result object.
    #[error("invalid graph query response: {0}")]
    InvalidResponse(String),
    /// A projected row is missing or contains an invalid field.
    #[error("invalid {kind} row {index}: {details}")]
    InvalidRow {
        /// `node` or `edge`.
        kind: &'static str,
        /// Zero-based row index.
        index: usize,
        /// Validation details.
        details: String,
    },
    /// More rows existed than the explicit safety limit permits.
    #[error("graph selection exceeded the {kind} safety limit of {limit}")]
    IncompleteSelection {
        /// `node` or `edge`.
        kind: &'static str,
        /// Configured maximum complete result size.
        limit: usize,
    },
    /// Two internal nodes map to the same external identity.
    #[error("duplicate external node identity: {0}")]
    DuplicateExternalIdentity(ExternalId),
    /// Graph metadata selection returned more than one row.
    #[error("graph metadata selection returned {count} rows; expected at most one")]
    MultipleGraphMetadataRows {
        /// Number of returned rows.
        count: usize,
    },
    /// Graph construction found a topology invariant violation.
    #[error(transparent)]
    Graph(#[from] GraphError),
}

#[derive(Debug, Deserialize)]
struct GraphResponse {
    nodes: Vec<BTreeMap<String, Value>>,
    edges: Vec<BTreeMap<String, Value>>,
    #[serde(default)]
    metadata: Vec<BTreeMap<String, Value>>,
}

/// Validate raw `/v2/query` response bytes and construct an immutable graph.
pub fn graph_from_response(spec: GraphLoadSpec, response: &[u8]) -> Result<Graph, GraphLoadError> {
    let response: GraphResponse = serde_json::from_slice(response)
        .map_err(|error| GraphLoadError::InvalidResponse(error.to_string()))?;
    if let Some(limit) = spec.node_limit
        && response.nodes.len() > limit
    {
        return Err(GraphLoadError::IncompleteSelection {
            kind: "node",
            limit,
        });
    }
    if let Some(limit) = spec.edge_limit
        && response.edges.len() > limit
    {
        return Err(GraphLoadError::IncompleteSelection {
            kind: "edge",
            limit,
        });
    }
    let graph_attributes = match response.metadata.len() {
        0 => BTreeMap::new(),
        1 => {
            let attributes = response
                .metadata
                .into_iter()
                .next()
                .expect("metadata length is one");
            if let Some(alias) = attributes
                .keys()
                .find(|alias| alias.starts_with(PRIVATE_PREFIX))
            {
                return Err(GraphLoadError::InvalidRow {
                    kind: "metadata",
                    index: 0,
                    details: format!("reserved projection alias {alias}"),
                });
            }
            attributes
        }
        count => return Err(GraphLoadError::MultipleGraphMetadataRows { count }),
    };

    let mut internal_to_external = BTreeMap::new();
    let mut external_ids = BTreeSet::new();
    let nodes = response
        .nodes
        .into_iter()
        .enumerate()
        .map(|(index, mut row)| {
            let internal = take_identity(&mut row, NODE_ID, "node", index)?;
            let external =
                take_external_id(&mut row, EXTERNAL_ID, "node", index, &spec.node_identity)?;
            if internal_to_external
                .insert(internal.clone(), external.clone())
                .is_some()
            {
                return Err(invalid_row(
                    "node",
                    index,
                    format!("duplicate internal ID {internal}"),
                ));
            }
            if !external_ids.insert(external.clone()) {
                return Err(GraphLoadError::DuplicateExternalIdentity(external));
            }
            let label = take_optional_string(&mut row, NODE_LABEL, "node", index)?;
            let mut node = Node::new(external).with_attributes(row);
            if let Some(label) = label {
                node = node.with_label(label);
            }
            Ok(node)
        })
        .collect::<Result<Vec<_>, GraphLoadError>>()?;

    let edges = response
        .edges
        .into_iter()
        .enumerate()
        .map(|(index, mut row)| {
            let edge_id = take_identity(&mut row, EDGE_ID, "edge", index)?;
            let source_internal = take_identity(&mut row, EDGE_SOURCE, "edge", index)?;
            let target_internal = take_identity(&mut row, EDGE_TARGET, "edge", index)?;
            let source = internal_to_external
                .get(&source_internal)
                .cloned()
                .ok_or_else(|| {
                    invalid_row(
                        "edge",
                        index,
                        format!("source {source_internal} is outside the node selection"),
                    )
                })?;
            let target = internal_to_external
                .get(&target_internal)
                .cloned()
                .ok_or_else(|| {
                    invalid_row(
                        "edge",
                        index,
                        format!("target {target_internal} is outside the node selection"),
                    )
                })?;
            let key = match &spec.edge_key_identity {
                Some(selection) => {
                    take_optional_external_id(&mut row, EDGE_KEY, "edge", index, selection)?
                }
                None => {
                    row.remove(EDGE_KEY);
                    None
                }
            };
            let label = take_optional_string(&mut row, EDGE_LABEL, "edge", index)?;
            let weight = take_optional_weight(&mut row, EDGE_WEIGHT, index)?;
            let mut edge = Edge::new(edge_id, source, target).with_attributes(row);
            if let Some(key) = key {
                edge = edge.with_graphify_key(key);
            }
            if let Some(label) = label {
                edge = edge.with_label(label);
            }
            if let Some(weight) = weight {
                edge = edge.with_weight(weight);
            }
            Ok(edge)
        })
        .collect::<Result<Vec<_>, GraphLoadError>>()?;
    Graph::with_attributes(spec.kind, graph_attributes, nodes, edges).map_err(Into::into)
}

fn take_identity(
    row: &mut BTreeMap<String, Value>,
    field: &str,
    kind: &'static str,
    index: usize,
) -> Result<String, GraphLoadError> {
    let value = row
        .remove(field)
        .ok_or_else(|| invalid_row(kind, index, format!("missing {field}")))?;
    identity(value).ok_or_else(|| invalid_row(kind, index, format!("invalid {field}")))
}

fn take_external_id(
    row: &mut BTreeMap<String, Value>,
    field: &str,
    kind: &'static str,
    index: usize,
    selection: &IdentitySelection,
) -> Result<ExternalId, GraphLoadError> {
    let value = row
        .remove(field)
        .ok_or_else(|| invalid_row(kind, index, format!("missing {field}")))?;
    decode_external_id(value, selection)
        .map_err(|error| invalid_row(kind, index, format!("invalid {field}: {error}")))
}

fn take_optional_external_id(
    row: &mut BTreeMap<String, Value>,
    field: &str,
    kind: &'static str,
    index: usize,
    selection: &IdentitySelection,
) -> Result<Option<ExternalId>, GraphLoadError> {
    match row.remove(field) {
        None => Ok(None),
        Some(value) => decode_external_id(value, selection)
            .map(Some)
            .map_err(|error| invalid_row(kind, index, format!("invalid {field}: {error}"))),
    }
}

fn decode_external_id(
    value: Value,
    selection: &IdentitySelection,
) -> Result<ExternalId, GraphError> {
    match selection {
        IdentitySelection::InternalId | IdentitySelection::ScalarProperty(_) => {
            ExternalId::from_scalar(value)
        }
        IdentitySelection::TaggedProperty(_) => ExternalId::from_tagged_value(value),
    }
}

fn identity(value: Value) -> Option<String> {
    match value {
        Value::String(value) if !value.is_empty() => Some(value),
        Value::Number(value) => Some(value.to_string()),
        Value::Null | Value::Bool(_) | Value::String(_) | Value::Array(_) | Value::Object(_) => {
            None
        }
    }
}

fn take_optional_string(
    row: &mut BTreeMap<String, Value>,
    field: &str,
    kind: &'static str,
    index: usize,
) -> Result<Option<String>, GraphLoadError> {
    match row.remove(field) {
        None | Some(Value::Null) => Ok(None),
        Some(Value::String(value)) => Ok(Some(value)),
        Some(_) => Err(invalid_row(kind, index, format!("invalid {field}"))),
    }
}

fn take_optional_weight(
    row: &mut BTreeMap<String, Value>,
    field: &str,
    index: usize,
) -> Result<Option<f64>, GraphLoadError> {
    match row.remove(field) {
        None | Some(Value::Null) => Ok(None),
        Some(Value::Number(value)) => value
            .as_f64()
            .filter(|weight| weight.is_finite() && *weight >= 0.0)
            .map(Some)
            .ok_or_else(|| invalid_row("edge", index, format!("invalid {field}"))),
        Some(_) => Err(invalid_row("edge", index, format!("invalid {field}"))),
    }
}

fn invalid_row(kind: &'static str, index: usize, details: String) -> GraphLoadError {
    GraphLoadError::InvalidRow {
        kind,
        index,
        details,
    }
}

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

    use super::*;
    use crate::NodeId;

    fn response(nodes: Value, edges: Value) -> Vec<u8> {
        serde_json::to_vec(&json!({ "nodes": nodes, "edges": edges })).expect("fixture JSON")
    }

    fn response_with_metadata(nodes: Value, edges: Value, metadata: Value) -> Vec<u8> {
        serde_json::to_vec(&json!({
            "nodes": nodes,
            "edges": edges,
            "metadata": metadata,
        }))
        .expect("fixture JSON")
    }

    fn spec() -> GraphLoadSpec {
        GraphLoadSpec {
            kind: GraphKind::DiGraph,
            node_identity: IdentitySelection::ScalarProperty(
                crate::GraphProperty::new("external_id").unwrap(),
            ),
            edge_key_identity: Some(IdentitySelection::ScalarProperty(
                crate::GraphProperty::new("key").unwrap(),
            )),
            node_limit: None,
            edge_limit: None,
        }
    }

    #[test]
    fn loader_preserves_identity_topology_properties_and_weights() {
        let bytes = response(
            json!([
                { (NODE_ID): "n1", (EXTERNAL_ID): "a", (NODE_LABEL): "File", "name": "A" },
                { (NODE_ID): "n2", (EXTERNAL_ID): "b", (NODE_LABEL): "File", "name": "B" }
            ]),
            json!([{
                (EDGE_ID): "e1", (EDGE_SOURCE): "n1", (EDGE_TARGET): "n2",
                (EDGE_KEY): "imports", (EDGE_LABEL): "DEPENDS_ON", (EDGE_WEIGHT): 2.5,
                "relation": "import"
            }]),
        );
        let graph = graph_from_response(spec(), &bytes).expect("valid graph response");
        assert_eq!(graph.node_count(), 2);
        let edge = graph.edge("e1").expect("edge");
        assert_eq!(
            (&edge.source, &edge.target),
            (&NodeId::from("a"), &NodeId::from("b"))
        );
        assert_eq!(edge.weight, Some(2.5));
    }

    #[test]
    fn loader_rejects_duplicate_external_identity_and_missing_endpoint() {
        let duplicate = response(
            json!([
                { (NODE_ID): "n1", (EXTERNAL_ID): "same", (NODE_LABEL): null },
                { (NODE_ID): "n2", (EXTERNAL_ID): "same", (NODE_LABEL): null }
            ]),
            json!([]),
        );
        assert!(matches!(
            graph_from_response(spec(), &duplicate),
            Err(GraphLoadError::DuplicateExternalIdentity(id)) if id == "same"
        ));

        let outside = response(
            json!([{ (NODE_ID): "n1", (EXTERNAL_ID): "a", (NODE_LABEL): null }]),
            json!([{
                (EDGE_ID): "e1", (EDGE_SOURCE): "n1", (EDGE_TARGET): "missing",
                (EDGE_LABEL): null
            }]),
        );
        assert!(matches!(
            graph_from_response(spec(), &outside),
            Err(GraphLoadError::InvalidRow { kind: "edge", .. })
        ));
    }

    #[test]
    fn loader_rejects_malformed_rows_invalid_weights_and_truncation() {
        assert!(matches!(
            graph_from_response(spec(), b"[]"),
            Err(GraphLoadError::InvalidResponse(_))
        ));
        let bad_weight = response(
            json!([{ (NODE_ID): "n1", (EXTERNAL_ID): "a", (NODE_LABEL): null }]),
            json!([{
                (EDGE_ID): "e1", (EDGE_SOURCE): "n1", (EDGE_TARGET): "n1",
                (EDGE_LABEL): null, (EDGE_WEIGHT): "heavy"
            }]),
        );
        assert!(matches!(
            graph_from_response(spec(), &bad_weight),
            Err(GraphLoadError::InvalidRow { kind: "edge", .. })
        ));

        let nodes = response(
            json!([
                { (NODE_ID): "n1", (EXTERNAL_ID): "a", (NODE_LABEL): null },
                { (NODE_ID): "n2", (EXTERNAL_ID): "b", (NODE_LABEL): null }
            ]),
            json!([]),
        );
        assert!(matches!(
            graph_from_response(
                GraphLoadSpec {
                    node_limit: Some(1),
                    ..spec()
                },
                &nodes
            ),
            Err(GraphLoadError::IncompleteSelection {
                kind: "node",
                limit: 1
            })
        ));

        let edges = response(
            json!([{ (NODE_ID): 1, (EXTERNAL_ID): 10, (NODE_LABEL): null }]),
            json!([{
                (EDGE_ID): 2, (EDGE_SOURCE): 1, (EDGE_TARGET): 1,
                (EDGE_LABEL): null
            }]),
        );
        let graph = graph_from_response(spec(), &edges).expect("numeric identities are scalar");
        assert!(graph.contains_node(10_i64));
        assert!(matches!(
            graph_from_response(
                GraphLoadSpec {
                    edge_limit: Some(0),
                    ..spec()
                },
                &edges
            ),
            Err(GraphLoadError::IncompleteSelection {
                kind: "edge",
                limit: 0
            })
        ));
    }

    #[test]
    fn loader_rejects_duplicate_internal_ids_missing_sources_and_invalid_optional_fields() {
        let duplicate = response(
            json!([
                { (NODE_ID): "n1", (EXTERNAL_ID): "a", (NODE_LABEL): null },
                { (NODE_ID): "n1", (EXTERNAL_ID): "b", (NODE_LABEL): null }
            ]),
            json!([]),
        );
        assert!(matches!(
            graph_from_response(spec(), &duplicate),
            Err(GraphLoadError::InvalidRow { kind: "node", .. })
        ));
        let missing_source = response(
            json!([{ (NODE_ID): "n1", (EXTERNAL_ID): "a", (NODE_LABEL): null }]),
            json!([{
                (EDGE_ID): "e", (EDGE_SOURCE): "missing", (EDGE_TARGET): "n1",
                (EDGE_LABEL): null
            }]),
        );
        assert!(matches!(
            graph_from_response(spec(), &missing_source),
            Err(GraphLoadError::InvalidRow { kind: "edge", .. })
        ));
        let invalid_key = response(
            json!([{ (NODE_ID): "n1", (EXTERNAL_ID): "a", (NODE_LABEL): null }]),
            json!([{
                (EDGE_ID): "e", (EDGE_SOURCE): "n1", (EDGE_TARGET): "n1",
                (EDGE_KEY): {}, (EDGE_LABEL): 1
            }]),
        );
        assert!(matches!(
            graph_from_response(spec(), &invalid_key),
            Err(GraphLoadError::InvalidRow { kind: "edge", .. })
        ));
    }

    #[test]
    fn scalar_and_tagged_identities_preserve_types_without_collisions() {
        let scalar = response(
            json!([
                { (NODE_ID): "n1", (EXTERNAL_ID): 1, (NODE_LABEL): null },
                { (NODE_ID): "n2", (EXTERNAL_ID): "1", (NODE_LABEL): null }
            ]),
            json!([]),
        );
        let graph = graph_from_response(spec(), &scalar).unwrap();
        assert!(graph.contains_node(1_i64));
        assert!(graph.contains_node("1"));

        let tuple =
            ExternalId::tuple(vec![ExternalId::from(1_i64), ExternalId::from("1")]).unwrap();
        let bytes = ExternalId::Bytes(vec![0, 255]);
        let tagged = response(
            json!([
                {
                    (NODE_ID): "n1",
                    (EXTERNAL_ID): serde_json::to_value(&tuple).unwrap(),
                    (NODE_LABEL): null
                },
                {
                    (NODE_ID): "n2",
                    (EXTERNAL_ID): serde_json::to_value(&bytes).unwrap(),
                    (NODE_LABEL): null
                }
            ]),
            json!([]),
        );
        let graph = graph_from_response(
            GraphLoadSpec {
                node_identity: IdentitySelection::TaggedProperty(
                    crate::GraphProperty::new("external_id").unwrap(),
                ),
                edge_key_identity: None,
                ..spec()
            },
            &tagged,
        )
        .unwrap();
        assert!(graph.contains_node(tuple));
        assert!(graph.contains_node(bytes));
    }

    #[test]
    fn internal_identity_and_no_edge_key_selection_are_explicit() {
        let bytes = response(
            json!([
                { (NODE_ID): "n1", (EXTERNAL_ID): "n1", (NODE_LABEL): null },
                { (NODE_ID): "n2", (EXTERNAL_ID): "n2", (NODE_LABEL): null }
            ]),
            json!([{
                (EDGE_ID): "e1", (EDGE_SOURCE): "n1", (EDGE_TARGET): "n2",
                (EDGE_KEY): {"ignored": true}, (EDGE_LABEL): null, (EDGE_WEIGHT): null
            }]),
        );
        let graph = graph_from_response(
            GraphLoadSpec {
                node_identity: IdentitySelection::InternalId,
                edge_key_identity: None,
                ..spec()
            },
            &bytes,
        )
        .unwrap();
        assert!(graph.contains_node("n1"));
        assert_eq!(graph.edge("e1").unwrap().graphify_key, None);
        assert!(!graph.edge("e1").unwrap().attributes.contains_key(EDGE_KEY));

        let invalid_label = response(
            json!([{ (NODE_ID): "n1", (EXTERNAL_ID): "n1", (NODE_LABEL): null }]),
            json!([{
                (EDGE_ID): "e1", (EDGE_SOURCE): "n1", (EDGE_TARGET): "n1",
                (EDGE_LABEL): 1
            }]),
        );
        assert!(matches!(
            graph_from_response(spec(), &invalid_label),
            Err(GraphLoadError::InvalidRow { kind: "edge", .. })
        ));
    }

    #[test]
    fn declared_kind_controls_parallel_edges_even_for_small_graphs() {
        let parallel = response(
            json!([
                { (NODE_ID): "n1", (EXTERNAL_ID): "a", (NODE_LABEL): null },
                { (NODE_ID): "n2", (EXTERNAL_ID): "b", (NODE_LABEL): null }
            ]),
            json!([
                {
                    (EDGE_ID): "e1", (EDGE_SOURCE): "n1", (EDGE_TARGET): "n2",
                    (EDGE_LABEL): null
                },
                {
                    (EDGE_ID): "e2", (EDGE_SOURCE): "n1", (EDGE_TARGET): "n2",
                    (EDGE_LABEL): null
                }
            ]),
        );
        assert!(matches!(
            graph_from_response(spec(), &parallel),
            Err(GraphLoadError::Graph(GraphError::ParallelEdge { .. }))
        ));
        let multigraph = graph_from_response(
            GraphLoadSpec {
                kind: GraphKind::MultiDiGraph,
                ..spec()
            },
            &parallel,
        )
        .unwrap();
        assert!(multigraph.is_multigraph());

        let empty = graph_from_response(
            GraphLoadSpec {
                kind: GraphKind::MultiGraph,
                ..spec()
            },
            &response(json!([]), json!([])),
        )
        .unwrap();
        assert!(empty.is_multigraph());
    }

    #[test]
    fn metadata_loads_once_and_rejects_multiple_or_reserved_rows() {
        let graph = graph_from_response(
            spec(),
            &response_with_metadata(json!([]), json!([]), json!([{"name": "demo"}])),
        )
        .unwrap();
        assert_eq!(graph.attributes()["name"], json!("demo"));

        assert!(matches!(
            graph_from_response(
                spec(),
                &response_with_metadata(
                    json!([]),
                    json!([]),
                    json!([{"name": "one"}, {"name": "two"}])
                )
            ),
            Err(GraphLoadError::MultipleGraphMetadataRows { count: 2 })
        ));
        assert!(matches!(
            graph_from_response(
                spec(),
                &response_with_metadata(json!([]), json!([]), json!([{(NODE_ID): "private"}]))
            ),
            Err(GraphLoadError::InvalidRow {
                kind: "metadata",
                ..
            })
        ));
    }
}