graphforge-cli 0.6.0-rc.7

GraphForge command-line interface
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
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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
//! The research porcelain end to end (ADR 0061): real `gf` argv through the CLI's
//! own parser and dispatcher, against the reference Hub over the product HTTP
//! transport. No request is written by hand: every operation is a verb.
//!
//! The product transport only speaks HTTPS to public addresses, so the Hub is
//! reached through the loopback transport every publish and clone test uses, and
//! the verbs run in-process; `tests/research_porcelain_walkthrough.rs` drives the
//! built `gf` binary through the verbs that need no Hub.
use super::*;
use crate::porcelain::remote::run_remote_with;
use crate::{Cli, Command};
use clap::Parser as _;
use serde_json::Value;

const REPOSITORY: &str = "https://hub.test/curate/claims";
const ALICE: &str = "Alice <alice@example.org>";
const BOB: &str = "Bob <bob@example.org>";

/// Run one `gf` invocation for the Project at `project` and parse its JSON.
fn gf(hub: &LoopbackHub, project: &Path, arguments: &[&str]) -> Result<Value, GfError> {
    let project_text = project.to_str().unwrap();
    let mut argv = vec!["gf", "--project", project_text, "--json"];
    argv.extend_from_slice(arguments);
    let command = Cli::try_parse_from(argv)
        .unwrap_or_else(|error| panic!("gf {arguments:?} does not parse: {error}"))
        .command
        .expect("a subcommand");
    let mut output = Vec::new();
    match command {
        Command::Clone(args) => {
            run_clone_with(&hub.transport(), args, true, &mut output)?;
        }
        Command::Publish(args) => {
            let mut graph = GraphForge::new(Some(project_text))?;
            let transport = hub.transport();
            crate::hub_publish::publish_with_credentials(
                &transport,
                &args,
                Some(token(hub, "curate/claims")),
                None,
                |environment| {
                    crate::hub_publish::publish_and_report(
                        &transport,
                        &mut graph,
                        &args,
                        environment,
                        true,
                        &mut output,
                    )
                },
            )?;
        }
        command @ (Command::Push(_) | Command::Pull(_)) => {
            let mut graph = GraphForge::new(Some(project_text))?;
            run_remote_with(
                &hub.transport(),
                &|_| Ok(Some(token(hub, "curate/claims"))),
                &mut graph,
                command,
                true,
                &mut output,
            )?;
        }
        command @ Command::Log(_) => {
            let mut graph = GraphForge::new(Some(project_text))?;
            if matches!(&command, Command::Log(args) if args.remote.is_some()) {
                run_remote_with(
                    &hub.transport(),
                    &|_| Ok(None),
                    &mut graph,
                    command,
                    true,
                    &mut output,
                )?;
            } else {
                crate::porcelain::run_local(&mut graph, command, true, &mut output)?;
            }
        }
        command => {
            let mut graph = GraphForge::new(Some(project_text))?;
            crate::porcelain::run_local(&mut graph, command, true, &mut output)?;
        }
    }
    Ok(serde_json::from_slice(&output).unwrap_or(Value::Null))
}

fn ok(hub: &LoopbackHub, project: &Path, arguments: &[&str]) -> Value {
    gf(hub, project, arguments).unwrap_or_else(|error| panic!("gf {arguments:?}: {error}"))
}

fn score(project: &Path) -> i64 {
    let graph = GraphForge::new(project.to_str()).unwrap();
    let registry = graph.research_version_retention().unwrap();
    let branch = *registry.branches.keys().next().unwrap();
    sum(graph
        .open_research_branch(branch)
        .unwrap()
        .graph()
        .execute("MATCH (n:Item) RETURN sum(n.score)")
        .unwrap())
}

fn sum(result: graphforge_api::ExecutionResult) -> i64 {
    result.batches[0]
        .column(0)
        .as_any()
        .downcast_ref::<arrow::array::Int64Array>()
        .unwrap()
        .value(0)
}

/// A Project with one Item, as a researcher would have before the first Branch.
fn seeded(root: &Path, name: &str) -> PathBuf {
    let path = root.join(name);
    let graph = GraphForge::new(path.to_str()).unwrap();
    graph.execute("CREATE (:Item {score:0})").unwrap();
    path
}

fn commit(hub: &LoopbackHub, project: &Path, who: &str, message: &str, value: i64) -> Value {
    ok(
        hub,
        project,
        &[
            "commit",
            "-m",
            message,
            "--query",
            &format!("MATCH (n:Item) SET n.score = {value}"),
            "--author",
            who,
        ],
    )
}

fn names(entries: &Value) -> Vec<String> {
    entries
        .as_array()
        .unwrap()
        .iter()
        .map(|entry| entry["description"].as_str().unwrap_or("").to_owned())
        .collect()
}

#[test]
fn porcelain_walkthrough_clones_commits_tags_pushes_pulls_and_logs() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");

    // Alice starts a Branch and publishes it; the ref is the Branch label. Until
    // it is published the Branch has no upstream and a push must name one.
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    let unpublished = gf(&hub, &alice, &["push"]).unwrap_err().to_string();
    assert!(
        unpublished.contains("GF_RESEARCH_UPSTREAM_UNSET"),
        "{unpublished}"
    );
    ok(&hub, &alice, &["push", REPOSITORY]);
    // The confirmed publication is her upstream, and she is not ahead of it.
    let tracked = ok(&hub, &alice, &["status"]);
    assert_eq!(tracked["upstream"]["repository_url"], REPOSITORY);
    assert_eq!(tracked["upstream"]["ref"], "main");
    assert_eq!(
        tracked["upstream"]["head_version_uuid"],
        tracked["head_version_uuid"]
    );
    assert_eq!(tracked["ahead"], 0);

    // Bob clones that ref and sees it as his current Branch with an upstream.
    let bob = root.path().join("bob");
    ok(
        &hub,
        &bob,
        &["clone", REPOSITORY, bob.to_str().unwrap(), "--ref", "main"],
    );
    let status = ok(&hub, &bob, &["status"]);
    assert_eq!(status["label"], "main");
    assert_eq!(status["ahead"], 0);
    assert_eq!(status["upstream"]["repository_url"], REPOSITORY);
    assert_eq!(status["upstream"]["ref"], "main");
    let base = status["head_version_uuid"].as_str().unwrap().to_owned();

    // Bob commits twice, tags the second, and pushes with no arguments.
    let first = commit(&hub, &bob, BOB, "first", 1);
    let second = commit(&hub, &bob, BOB, "second", 2);
    assert_eq!(first["parents"][0], base);
    assert_eq!(second["parents"][0], first["version_uuid"]);
    assert_eq!(ok(&hub, &bob, &["status"])["ahead"], 2);
    ok(&hub, &bob, &["tag", "v1"]);
    assert_eq!(ok(&hub, &bob, &["tag"])[0]["name"], "v1");
    ok(&hub, &bob, &["push"]);
    // The push moved Bob's recorded upstream head, so nothing is left unpublished.
    let pushed = ok(&hub, &bob, &["status"]);
    assert_eq!(pushed["ahead"], 0);
    assert_eq!(
        pushed["upstream"]["head_version_uuid"],
        second["version_uuid"]
    );

    // Alice pulls Bob's two commits: her head had not moved, and the pull still
    // records a two-parent Version (there is no pointer fast-forward).
    let alice_head = ok(&hub, &alice, &["status"])["head_version_uuid"].clone();
    let pulled = ok(&hub, &alice, &["pull", REPOSITORY, "--author", ALICE]);
    assert_eq!(pulled["parents"][0], alice_head);
    assert_eq!(pulled["parents"][1], second["version_uuid"]);
    assert_eq!(score(&alice), 2);
    assert_eq!(ok(&hub, &alice, &["tag"])[0]["name"], "v1");
    let again = ok(&hub, &alice, &["pull", "--author", ALICE]);
    assert_eq!(again["up_to_date"], true);

    // Alice commits on top and pushes: a fast-forward of the Hub head.
    let alices = commit(&hub, &alice, ALICE, "alice's change", 3);
    ok(&hub, &alice, &["push"]);

    // Bob pulls her change: his head (the second commit) is not her ancestor.
    let merged = ok(&hub, &bob, &["pull", "--author", BOB]);
    assert_eq!(merged["parents"][0], second["version_uuid"]);
    assert_eq!(merged["parents"][1], alices["version_uuid"]);
    assert_eq!(score(&bob), 3);

    // His log lists both of his commits, Alice's, and the merge Versions.
    let log = ok(&hub, &bob, &["log", "--all"]);
    let entries = log.as_array().unwrap();
    let by_message = |message: &str| {
        entries
            .iter()
            .find(|entry| entry["description"] == message)
            .unwrap_or_else(|| panic!("no '{message}' in {log}"))
    };
    assert_eq!(entries[0]["version_uuid"], merged["version_uuid"]);
    let limited = ok(&hub, &bob, &["log", "--all", "-n", "2"]);
    assert_eq!(limited[0]["version_uuid"], merged["version_uuid"]);
    assert_eq!(limited[1]["version_uuid"], alices["version_uuid"]);
    assert_eq!(by_message("first")["author"]["name"], "Bob");
    assert_eq!(by_message("first")["committer"]["name"], "Bob");
    assert_eq!(by_message("alice's change")["author"]["name"], "Alice");
    assert_eq!(
        by_message("second")["parents"],
        serde_json::json!([first["version_uuid"]])
    );
    assert!(
        entries
            .iter()
            .any(|entry| entry["parents"].as_array().is_some_and(|p| p.len() == 2)),
        "a merge Version has two parents: {log}"
    );
    assert_eq!(
        names(&ok(&hub, &bob, &["log"]))[..3],
        [
            "Merge main of https://hub.test/curate/claims".to_owned(),
            "second".to_owned(),
            "first".to_owned()
        ],
        "the default log follows first parents"
    );

    // The Hub's published history agrees with the served lineage.
    let remote = ok(&hub, &bob, &["log", "--remote", "--all"]);
    let (_, lineage) = served_lineage(&hub, "curate/claims");
    let walked: Vec<&str> = remote
        .as_array()
        .unwrap()
        .iter()
        .map(|entry| entry["version_uuid"].as_str().unwrap())
        .collect();
    let head = lineage.branches[0].head_version_uuid.as_str();
    assert_eq!(walked[0], head);
    assert_eq!(head, alices["version_uuid"].as_str().unwrap());
    for version in &walked {
        assert!(lineage.version(version).is_some());
    }
    let mut pending = vec![head];
    let mut reachable = BTreeSet::new();
    while let Some(version) = pending.pop() {
        if let Some(record) = lineage.version(version)
            && reachable.insert(version)
        {
            pending.extend(record.parents.iter().map(String::as_str));
        }
    }
    assert_eq!(
        walked.iter().copied().collect::<BTreeSet<_>>(),
        reachable,
        "a boundary anchor must not hide the other merge parent's history"
    );
    for (child, version) in walked.iter().enumerate() {
        for parent in &lineage.version(version).unwrap().parents {
            if let Some(position) = walked.iter().position(|id| *id == parent) {
                assert!(child < position, "child {version} follows parent {parent}");
            }
        }
    }
    let limited = ok(&hub, &bob, &["log", "--remote", "--all", "-n", "3"]);
    assert_eq!(limited[2]["version_uuid"], second["version_uuid"]);

    // A tag names its Version for log, and a third clone materializes it.
    let tagged = ok(&hub, &bob, &["log", "v1", "-n", "1"]);
    assert_eq!(tagged[0]["version_uuid"], second["version_uuid"]);
    assert_eq!(tagged[0]["tags"][0], "v1");
    let third = root.path().join("third");
    ok(
        &hub,
        &third,
        &["clone", REPOSITORY, third.to_str().unwrap(), "--tag", "v1"],
    );
    let cloned = GraphForge::new(third.to_str()).unwrap();
    assert_eq!(ok(&hub, &third, &["tag"])[0]["name"], "v1");
    assert_eq!(
        ok(&hub, &third, &["log", "v1", "-n", "1"])[0]["version_uuid"],
        second["version_uuid"]
    );
    assert_eq!(
        sum(cloned
            .execute("MATCH (n:Item) RETURN sum(n.score)")
            .unwrap()),
        2
    );
}

#[test]
fn porcelain_walkthrough_preserves_replay_and_fast_forward() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    ok(&hub, &alice, &["push", REPOSITORY]);
    let bob = root.path().join("bob");
    ok(
        &hub,
        &bob,
        &["clone", REPOSITORY, bob.to_str().unwrap(), "--ref", "main"],
    );

    // A commit rerun with its operation UUID returns the original receipt.
    let operation = Uuid::now_v7().to_string();
    let arguments = [
        "commit",
        "-m",
        "once",
        "--query",
        "MATCH (n:Item) SET n.score = 1",
        "--author",
        BOB,
        "--operation-uuid",
        operation.as_str(),
    ];
    let original = ok(&hub, &bob, &arguments);
    let versions = |project: &Path| {
        GraphForge::new(project.to_str())
            .unwrap()
            .research_version_retention()
            .unwrap()
            .versions
            .len()
    };
    let count = versions(&bob);
    let replay = ok(&hub, &bob, &arguments);
    assert_eq!(replay["version_uuid"], original["version_uuid"]);
    assert_eq!(replay["replayed"], true);
    assert_eq!(versions(&bob), count);
    // The same identity with another message is a conflict, not a replay.
    let mut other = arguments;
    other[2] = "different";
    gf(&hub, &bob, &other).unwrap_err();

    // Alice advances the Hub; Bob's push from a stale clone is refused exactly as
    // `gf publish` refuses it, and `--rewrite` replaces the head under the lease.
    commit(&hub, &alice, ALICE, "alice's change", 5);
    ok(&hub, &alice, &["push"]);
    let recorded = |project: &Path| {
        GraphForge::new(project.to_str())
            .unwrap()
            .research_version_retention()
            .unwrap()
            .remote_branches
    };
    let before = recorded(&bob);
    let refused = gf(&hub, &bob, &["push"]).unwrap_err();
    assert_eq!(error_code(&refused), "hub.publish.non_fast_forward");
    assert_eq!(
        recorded(&bob),
        before,
        "a refused push leaves the upstream alone"
    );
    assert_eq!(ok(&hub, &bob, &["status"])["ahead"], 1);
    ok(&hub, &bob, &["push", "--rewrite"]);
    assert_eq!(ok(&hub, &bob, &["status"])["ahead"], 0);
    let (_, lineage) = served_lineage(&hub, "curate/claims");
    assert_eq!(
        lineage.branches[0].head_version_uuid,
        original["version_uuid"].as_str().unwrap()
    );
}

#[test]
fn pull_refuses_before_update_when_the_preview_has_incompatible_changes() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    ok(&hub, &alice, &["push", REPOSITORY]);
    let bob = root.path().join("bob");
    ok(
        &hub,
        &bob,
        &["clone", REPOSITORY, bob.to_str().unwrap(), "--ref", "main"],
    );
    // Both sides change the same field from the same base.
    commit(&hub, &alice, ALICE, "alice", 1);
    ok(&hub, &alice, &["push"]);
    commit(&hub, &bob, BOB, "bob", 2);
    let before = GraphForge::new(bob.to_str())
        .unwrap()
        .research_version_retention()
        .unwrap();
    let error = gf(&hub, &bob, &["pull", "--author", BOB])
        .unwrap_err()
        .to_string();
    assert!(error.contains("property:score"), "{error}");
    assert!(error.contains("gf research upstream update"), "{error}");
    let after = GraphForge::new(bob.to_str())
        .unwrap()
        .research_version_retention()
        .unwrap();
    // The fetch is durable (it retains the remote head) but nothing was incorporated.
    assert_eq!(after.versions.len(), before.versions.len() + 1);
    assert_eq!(after.heads, before.heads);
    assert_eq!(after.upstream.reviews.len(), before.upstream.reviews.len());
}

#[test]
fn pull_is_a_no_op_when_the_remote_head_is_an_ancestor() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    ok(&hub, &alice, &["push", REPOSITORY]);
    commit(&hub, &alice, ALICE, "ahead", 1);
    let before = GraphForge::new(alice.to_str())
        .unwrap()
        .research_version_retention()
        .unwrap();
    let pulled = ok(&hub, &alice, &["pull", REPOSITORY, "--author", ALICE]);
    assert_eq!(pulled["up_to_date"], true);
    let after = GraphForge::new(alice.to_str())
        .unwrap()
        .research_version_retention()
        .unwrap();
    assert_eq!(after.versions.len(), before.versions.len());
    assert_eq!(after.receipts.len(), before.receipts.len());
}

#[test]
fn served_tags_are_installed_with_the_fetch_even_when_the_update_is_refused() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    ok(&hub, &alice, &["push", REPOSITORY]);
    let bob = root.path().join("bob");
    ok(
        &hub,
        &bob,
        &["clone", REPOSITORY, bob.to_str().unwrap(), "--ref", "main"],
    );
    // Alice tags a new commit and pushes; Bob changes the same field.
    let theirs = commit(&hub, &alice, ALICE, "alice", 1)["version_uuid"].clone();
    ok(&hub, &alice, &["tag", "a1"]);
    ok(&hub, &alice, &["push"]);
    commit(&hub, &bob, BOB, "bob", 2);
    assert!(ok(&hub, &bob, &["tag"]).as_array().unwrap().is_empty());

    // The update is refused between the durable fetch and the incorporation; the
    // tag came with the fetch, in the same publication as the admitted Version.
    gf(&hub, &bob, &["pull", "--author", BOB]).unwrap_err();
    let tags = ok(&hub, &bob, &["tag"]);
    assert_eq!(tags[0]["name"], "a1");
    assert_eq!(tags[0]["version_uuid"], theirs);
}

#[test]
fn pull_refuses_a_served_tag_that_conflicts_with_a_local_tag() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    ok(&hub, &alice, &["tag", "v1"]);
    ok(&hub, &alice, &["push", REPOSITORY]);
    let bob = root.path().join("bob");
    ok(
        &hub,
        &bob,
        &["clone", REPOSITORY, bob.to_str().unwrap(), "--ref", "main"],
    );
    commit(&hub, &bob, BOB, "bob", 1);
    ok(&hub, &bob, &["tag", "-d", "v1"]);
    ok(&hub, &bob, &["tag", "v1"]);
    let error = gf(&hub, &bob, &["pull", "--author", BOB])
        .unwrap_err()
        .to_string();
    assert!(error.contains("GF_RESEARCH_TAG_CONFLICT"), "{error}");
}

#[test]
fn publish_records_the_upstream_a_later_push_uses() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    ok(&hub, &alice, &["publish", REPOSITORY, "--ref", "main"]);
    assert_eq!(ok(&hub, &alice, &["status"])["upstream"]["ref"], "main");
    commit(&hub, &alice, ALICE, "more", 1);
    assert_eq!(ok(&hub, &alice, &["status"])["ahead"], 1);
    ok(&hub, &alice, &["push"]);
    assert_eq!(ok(&hub, &alice, &["status"])["ahead"], 0);
}

fn assert_conflict(result: Result<Value, GfError>) {
    let error = result.expect_err("a different request under a used identity must be refused");
    assert_eq!(error_code(&error), "GF_IDEMPOTENCY_CONFLICT", "{error}");
}

#[test]
fn every_local_verb_replays_only_the_complete_original_request() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    let operation = || Uuid::now_v7().to_string();
    let versions = |project: &Path| {
        GraphForge::new(project.to_str())
            .unwrap()
            .research_version_retention()
            .unwrap()
            .versions
            .len()
    };

    // branch: the first Branch, then the same request again.
    let o = operation();
    let branch = ["branch", "main", "--author", ALICE, "--operation-uuid", &o];
    let created = ok(&hub, &alice, &branch);
    let repeated = ok(&hub, &alice, &branch);
    assert_eq!(repeated["replayed"], true);
    assert_eq!(repeated["version_uuid"], created["version_uuid"]);
    assert_eq!(repeated["generation_uuid"], created["generation_uuid"]);
    assert_conflict(gf(
        &hub,
        &alice,
        &["branch", "other", "--author", ALICE, "--operation-uuid", &o],
    ));
    assert_conflict(gf(
        &hub,
        &alice,
        &["branch", "main", "--author", BOB, "--operation-uuid", &o],
    ));
    assert_conflict(gf(
        &hub,
        &alice,
        &[
            "branch",
            "main",
            "--from",
            "v0",
            "--author",
            ALICE,
            "--operation-uuid",
            &o,
        ],
    ));

    // commit: query, message and credit are all part of the request.
    let o = operation();
    let commit = |message: &str, query: &str, who: &str| {
        gf(
            &hub,
            &alice,
            &[
                "commit",
                "-m",
                message,
                "--query",
                query,
                "--author",
                who,
                "--operation-uuid",
                &o,
            ],
        )
    };
    let first = commit("one", "MATCH (n:Item) SET n.score = 1", ALICE).unwrap();
    let count = versions(&alice);
    let again = commit("one", "MATCH (n:Item) SET n.score = 1", ALICE).unwrap();
    assert_eq!(again["replayed"], true);
    assert_eq!(again["version_uuid"], first["version_uuid"]);
    assert_eq!(again["generation_uuid"], first["generation_uuid"]);
    assert_eq!(versions(&alice), count);
    assert_conflict(commit("one", "MATCH (n:Item) SET n.score = 2", ALICE));
    assert_conflict(commit("two", "MATCH (n:Item) SET n.score = 1", ALICE));
    assert_conflict(commit("one", "MATCH (n:Item) SET n.score = 1", BOB));
    assert_eq!(versions(&alice), count, "a refused repeat records nothing");
    // The same identity for another kind of operation is a conflict too.
    assert_conflict(gf(&hub, &alice, &["tag", "v1", "--operation-uuid", &o]));
    assert_conflict(gf(
        &hub,
        &alice,
        &["branch", "x", "--author", ALICE, "--operation-uuid", &o],
    ));

    // tag: name and target.
    let o = operation();
    let created = ok(&hub, &alice, &["tag", "v1", "--operation-uuid", &o]);
    let head = created["version_uuid"].as_str().unwrap().to_owned();
    // CURRENT has moved since; the rerun is still a replay of the same receipt.
    ok(
        &hub,
        &alice,
        &["tag", "other", "--operation-uuid", &operation()],
    );
    let again = ok(&hub, &alice, &["tag", "v1", &head, "--operation-uuid", &o]);
    assert_eq!(again["replayed"], true);
    assert_eq!(again["generation_uuid"], created["generation_uuid"]);
    assert_conflict(gf(&hub, &alice, &["tag", "v2", "--operation-uuid", &o]));
    assert_conflict(gf(
        &hub,
        &alice,
        &["tag", "-d", "v1", "--operation-uuid", &o],
    ));

    // switch: the target Branch.
    ok(
        &hub,
        &alice,
        &["branch", "feature", "--branch", "main", "--author", ALICE],
    );
    let o = operation();
    let switched = ok(&hub, &alice, &["switch", "feature", "--operation-uuid", &o]);
    assert_eq!(switched["label"], "feature");
    ok(&hub, &alice, &["switch", "feature", "--operation-uuid", &o]);
    assert_conflict(gf(
        &hub,
        &alice,
        &["switch", "main", "--operation-uuid", &o],
    ));
    assert_eq!(ok(&hub, &alice, &["status"])["label"], "feature");
    // Selecting the Branch that is already current still records the identity, so
    // the identity cannot later be reused for another selection.
    let o = operation();
    ok(&hub, &alice, &["switch", "feature", "--operation-uuid", &o]);
    let recorded = GraphForge::new(alice.to_str())
        .unwrap()
        .research_version_retention()
        .unwrap()
        .receipts
        .contains_key(&Uuid::parse_str(&o).unwrap());
    assert!(
        recorded,
        "a no-op selection under an identity leaves its receipt"
    );
    assert_conflict(gf(
        &hub,
        &alice,
        &["switch", "main", "--operation-uuid", &o],
    ));
    assert_eq!(ok(&hub, &alice, &["status"])["label"], "feature");
}

#[test]
fn pull_replays_a_completed_receipt_without_the_network_and_refuses_another_request() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    ok(&hub, &alice, &["push", REPOSITORY]);
    let bob = root.path().join("bob");
    ok(
        &hub,
        &bob,
        &["clone", REPOSITORY, bob.to_str().unwrap(), "--ref", "main"],
    );
    commit(&hub, &alice, ALICE, "alice", 3);
    ok(&hub, &alice, &["push"]);

    let o = Uuid::now_v7().to_string();
    let pull = ["pull", "--author", BOB, "--operation-uuid", o.as_str()];
    let merged = ok(&hub, &bob, &pull);
    assert_eq!(merged["parents"].as_array().unwrap().len(), 2);
    // A Hub that has nothing at all: a replay must not need the network.
    let nowhere = LoopbackHub::start();
    let replay = ok(&nowhere, &bob, &pull);
    assert_eq!(replay["replayed"], true);
    assert_eq!(replay["version_uuid"], merged["version_uuid"]);
    assert_eq!(replay["generation_uuid"], merged["generation_uuid"]);
    assert_eq!(replay["parents"], merged["parents"]);
    assert_eq!(replay["incorporated"], merged["incorporated"]);
    assert_eq!(nowhere.log().len(), 0, "no request reached the Hub");
    // Another request under the identity is refused, offline as well.
    assert_conflict(gf(
        &nowhere,
        &bob,
        &["pull", "--author", ALICE, "--operation-uuid", &o],
    ));
    assert_conflict(gf(
        &nowhere,
        &bob,
        &[
            "pull",
            "https://hub.test/curate/other",
            "--author",
            BOB,
            "--operation-uuid",
            &o,
        ],
    ));
}

#[test]
fn push_replays_a_completed_publication_and_refuses_another_request() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    let o = Uuid::now_v7().to_string();
    let push = ["push", REPOSITORY, "--operation-uuid", o.as_str()];
    let first = ok(&hub, &alice, &push);
    // Recording the confirmed publication moved CURRENT; the rerun is a replay.
    let writes = || {
        hub.log()
            .iter()
            .filter(|entry| matches!(entry.method.as_str(), "POST" | "PUT"))
            .count()
    };
    let before = writes();
    let receipts = |project: &Path| {
        GraphForge::new(project.to_str())
            .unwrap()
            .research_version_retention()
            .unwrap()
            .receipts
            .len()
    };
    let recorded = receipts(&alice);
    let again = ok(&hub, &alice, &["push", "--operation-uuid", &o]);
    assert_eq!(again, first);
    assert_eq!(writes(), before, "a replay uploads and commits nothing");
    // Replays are free: the permanent receipt ledger does not grow with them.
    for _ in 0..5 {
        assert_eq!(ok(&hub, &alice, &["push", "--operation-uuid", &o]), first);
    }
    assert_eq!(receipts(&alice), recorded);
    // The same identity for another ref or another flag is refused.
    ok(
        &hub,
        &alice,
        &["branch", "feature", "--branch", "main", "--author", ALICE],
    );
    assert_conflict(gf(
        &hub,
        &alice,
        &[
            "push",
            REPOSITORY,
            "--ref",
            "feature",
            "--operation-uuid",
            &o,
        ],
    ));
    assert_conflict(gf(
        &hub,
        &alice,
        &["push", "--ref", "main", "--rewrite", "--operation-uuid", &o],
    ));
    assert_eq!(writes(), before);
}

#[test]
fn publication_record_is_bound_to_the_asserted_documents() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    let receipt: graphforge_hub_publish::PublishReceipt =
        serde_json::from_value(ok(&hub, &alice, &["push", REPOSITORY])).unwrap();
    let published = ok(&hub, &alice, &["status"])["head_version_uuid"].clone();
    // An unpublished descendant: the Branch head moves past what the Hub holds.
    let unpublished = commit(&hub, &alice, ALICE, "not pushed", 9)["version_uuid"].clone();
    let (manifest, lineage) = served_lineage(&hub, "curate/claims");
    let mut graph = GraphForge::new(alice.to_str()).unwrap();
    let branch = *graph
        .research_version_retention()
        .unwrap()
        .branches
        .keys()
        .next()
        .unwrap();
    let mut record = |receipt: graphforge_hub_publish::PublishReceipt,
                      manifest: DiscoveryManifest,
                      lineage: ResearchLineage| {
        let request = graphforge_api::RecordResearchPublicationRequest {
            operation_uuid: Uuid::now_v7(),
            expected_generation_uuid: generation(&graph),
            branch_uuid: branch,
            repository_url: REPOSITORY.into(),
            receipt,
            manifest,
            lineage,
        };
        graph.record_research_publication(&request, &CancellationToken::new())
    };

    // What the receipt confirms is the published head, not the Branch head, and it
    // is already recorded: recording again writes nothing.
    assert!(
        record(receipt.clone(), manifest.clone(), lineage.clone())
            .unwrap()
            .is_none()
    );
    drop(record);
    drop(graph);
    let upstream = ok(&hub, &alice, &["status"]);
    assert_eq!(upstream["upstream"]["head_version_uuid"], published);
    assert_ne!(upstream["upstream"]["head_version_uuid"], unpublished);

    let mut graph = GraphForge::new(alice.to_str()).unwrap();
    let mut record = |receipt: graphforge_hub_publish::PublishReceipt,
                      manifest: DiscoveryManifest,
                      lineage: ResearchLineage| {
        let request = graphforge_api::RecordResearchPublicationRequest {
            operation_uuid: Uuid::now_v7(),
            expected_generation_uuid: generation(&graph),
            branch_uuid: branch,
            repository_url: REPOSITORY.into(),
            receipt,
            manifest,
            lineage,
        };
        graph.record_research_publication(&request, &CancellationToken::new())
    };
    // A lineage that claims the unpublished head is not the one the receipt commits to.
    let mut forged = lineage.clone();
    forged.branches[0].head_version_uuid = unpublished.as_str().unwrap().to_owned();
    record(receipt.clone(), manifest.clone(), forged).unwrap_err();
    // Nor is a receipt for other documents, another repository, or other refs.
    let mut other_documents = receipt.clone();
    other_documents.manifest_validator = format!("sha256:{}", "0".repeat(64));
    record(other_documents, manifest.clone(), lineage.clone()).unwrap_err();
    let mut elsewhere = receipt.clone();
    elsewhere.repository =
        graphforge_discovery::RepositoryIdentity::parse("curate/elsewhere").unwrap();
    record(elsewhere, manifest.clone(), lineage.clone()).unwrap_err();
    let mut other_refs = receipt;
    other_refs.refs = vec!["feature".into()];
    record(other_refs, manifest, lineage).unwrap_err();
}

#[test]
fn an_interrupted_publication_is_recorded_when_it_is_replayed() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    // The Hub committed the publication, but the process died before recording it.
    let graph = GraphForge::new(alice.to_str()).unwrap();
    publish_ok(&hub, &graph, &on_ref("curate/claims", "main"))
        .result
        .unwrap();
    drop(graph);
    assert!(
        ok(&hub, &alice, &["status"])
            .get("upstream")
            .is_none_or(Value::is_null)
    );
    // Rerunning the command replays the committed operation and records it.
    ok(&hub, &alice, &["push", REPOSITORY]);
    let status = ok(&hub, &alice, &["status"]);
    assert_eq!(status["upstream"]["ref"], "main");
    assert_eq!(status["ahead"], 0);
}

#[test]
fn an_interrupted_republication_is_recorded_when_it_is_replayed() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    ok(&hub, &alice, &["push", REPOSITORY]);
    let first = ok(&hub, &alice, &["status"])["head_version_uuid"].clone();
    let second = commit(&hub, &alice, ALICE, "second", 2)["version_uuid"].clone();

    // The Hub commits the second publication, but the process dies before it is
    // recorded: the Project still tracks the first head.
    let o = Uuid::now_v7().to_string();
    let mut interrupted = on_ref("curate/claims", "main");
    interrupted.operation_uuid = Some(o.clone());
    let graph = GraphForge::new(alice.to_str()).unwrap();
    publish_ok(&hub, &graph, &interrupted).result.unwrap();
    drop(graph);
    let stale = ok(&hub, &alice, &["status"]);
    assert_eq!(stale["upstream"]["head_version_uuid"], first);
    assert_eq!(stale["ahead"], 1);

    // Rerunning the operation replays it and records the head it committed.
    ok(&hub, &alice, &["push", "--operation-uuid", &o]);
    let recovered = ok(&hub, &alice, &["status"]);
    assert_eq!(recovered["upstream"]["head_version_uuid"], second);
    assert_eq!(recovered["ahead"], 0);

    // Replaying an older operation never moves the recorded head backwards.
    let receipts = |project: &Path| {
        GraphForge::new(project.to_str())
            .unwrap()
            .research_version_retention()
            .unwrap()
            .receipts
            .len()
    };
    let count = receipts(&alice);
    ok(&hub, &alice, &["push", "--operation-uuid", &o]);
    assert_eq!(receipts(&alice), count);
}

#[test]
fn an_interrupted_pull_resumes_from_its_recorded_fetch() {
    use crate::hub_clone::remote_read::{RemoteRequest, read_remote};
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    ok(&hub, &alice, &["push", REPOSITORY]);
    let bob = root.path().join("bob");
    ok(
        &hub,
        &bob,
        &["clone", REPOSITORY, bob.to_str().unwrap(), "--ref", "main"],
    );
    commit(&hub, &alice, ALICE, "alice", 3);
    ok(&hub, &alice, &["push"]);

    // The fetch step commits under the pull's identity; the process dies before the
    // incorporation, leaving CURRENT one generation past the fetch's precondition.
    let o = Uuid::now_v7();
    let mut graph = GraphForge::new(bob.to_str()).unwrap();
    let branch = *graph
        .research_version_retention()
        .unwrap()
        .branches
        .keys()
        .next()
        .unwrap();
    let scratch = tempfile::tempdir().unwrap();
    let (remote, head) = read_remote(
        &hub.transport(),
        &RemoteRequest {
            repository: REPOSITORY,
            hub: None,
            token_env: None,
            protection_bypass_env: None,
        },
        Some(("main", scratch.path())),
    )
    .unwrap();
    let before = generation(&graph);
    crate::porcelain::remote::fetch_step(
        &mut graph,
        branch,
        &remote,
        &head.unwrap(),
        o,
        &CancellationToken::new(),
    )
    .unwrap();
    assert_ne!(generation(&graph), before);
    drop(graph);

    // Rerunning the pull under the same identity resumes and completes the merge.
    let o = o.to_string();
    let merged = ok(
        &hub,
        &bob,
        &["pull", "--author", BOB, "--operation-uuid", &o],
    );
    assert_eq!(merged["parents"].as_array().unwrap().len(), 2);
    assert_eq!(score(&bob), 3);
    // A different request under the identity is still refused.
    assert_conflict(gf(
        &hub,
        &bob,
        &["pull", "--author", ALICE, "--operation-uuid", &o],
    ));
}

#[test]
fn a_clone_selects_its_branch_for_the_verbs() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    ok(&hub, &alice, &["push", REPOSITORY]);
    let bob = root.path().join("bob");
    ok(
        &hub,
        &bob,
        &["clone", REPOSITORY, bob.to_str().unwrap(), "--ref", "main"],
    );
    // A second Branch does not unselect the cloned one.
    ok(&hub, &bob, &["branch", "feature", "--author", BOB]);
    assert_eq!(ok(&hub, &bob, &["status"])["label"], "main");
    let committed = commit(&hub, &bob, BOB, "on main", 4);
    assert_eq!(committed["parents"].as_array().unwrap().len(), 1);
    assert_eq!(
        ok(&hub, &bob, &["log", "-n", "1"])[0]["description"],
        "on main"
    );
}

#[test]
fn porcelain_walkthrough_uses_no_handwritten_json() {
    // No argument list in this file passes a native request file.
    let source = include_str!("porcelain.rs");
    let flag = ["\"--", "file\""].concat();
    assert_eq!(
        source.matches(&flag).count(),
        0,
        "the walkthrough must not hand a request file to any verb"
    );
}

#[test]
fn human_push_and_pull_report_their_retry_identity_on_success_and_refusal() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    let human = |project: &Path, arguments: &[&str]| {
        let mut argv = vec!["gf"];
        argv.extend_from_slice(arguments);
        let command = Cli::try_parse_from(argv).unwrap().command.unwrap();
        let mut graph = GraphForge::new(project.to_str()).unwrap();
        let mut output = Vec::new();
        let result = run_remote_with(
            &hub.transport(),
            &|_| Ok(Some(token(&hub, "curate/claims"))),
            &mut graph,
            command,
            false,
            &mut output,
        );
        let text = String::from_utf8(output).unwrap();
        let operation = Uuid::parse_str(
            text.lines()
                .next()
                .unwrap()
                .strip_prefix("Operation UUID: ")
                .expect(&text),
        )
        .unwrap();
        (result, operation, text)
    };
    let (result, pushed, text) = human(&alice, &["push", REPOSITORY]);
    result.unwrap();
    assert!(text.contains("Published"), "{text}");
    assert_eq!(
        ok(&hub, &alice, &["push"])["operation_uuid"],
        pushed.to_string()
    );
    let bob = root.path().join("bob");
    ok(
        &hub,
        &bob,
        &["clone", REPOSITORY, bob.to_str().unwrap(), "--ref", "main"],
    );
    commit(&hub, &alice, ALICE, "alice", 1);
    ok(&hub, &alice, &["push"]);
    let (result, pulled, text) = human(&bob, &["pull", "--author", BOB]);
    result.unwrap();
    assert!(text.contains("Merged"), "{text}");
    assert_eq!(
        ok(
            &hub,
            &bob,
            &[
                "pull",
                "--author",
                BOB,
                "--operation-uuid",
                &pulled.to_string()
            ]
        )["replayed"],
        true
    );

    // Both sides advance the same field. The pull refuses after its durable fetch,
    // and the printed identity lets a human retry that fetch rather than minting one.
    commit(&hub, &alice, ALICE, "alice again", 2);
    ok(&hub, &alice, &["push"]);
    commit(&hub, &bob, BOB, "bob", 3);
    let (result, operation, _) = human(&bob, &["pull", "--author", BOB]);
    assert!(result.unwrap_err().to_string().contains("property:score"));
    let state = GraphForge::new(bob.to_str())
        .unwrap()
        .research_version_retention()
        .unwrap();
    assert!(
        state
            .receipts
            .contains_key(&crate::porcelain::derived_uuid(operation, "fetch"))
    );
    let (result, repeated, _) = human(
        &bob,
        &[
            "pull",
            "--author",
            BOB,
            "--operation-uuid",
            &operation.to_string(),
        ],
    );
    assert!(result.unwrap_err().to_string().contains("property:score"));
    assert_eq!(repeated, operation);
    let (result, _, _) = human(&bob, &["push"]);
    assert_eq!(
        error_code(&result.unwrap_err()),
        "hub.publish.non_fast_forward"
    );
}

#[test]
fn remote_log_visits_the_other_merge_parent_after_a_project_boundary() {
    let hub = LoopbackHub::start();
    let root = tempfile::tempdir().unwrap();
    let alice = seeded(root.path(), "alice");
    ok(&hub, &alice, &["branch", "main", "--author", ALICE]);
    ok(&hub, &alice, &["push", REPOSITORY]);
    let base = ok(&hub, &alice, &["status"])["head_version_uuid"].clone();
    let bob = root.path().join("bob");
    ok(
        &hub,
        &bob,
        &["clone", REPOSITORY, bob.to_str().unwrap(), "--ref", "main"],
    );
    let changed = commit(&hub, &alice, ALICE, "alice", 1)["version_uuid"].clone();
    ok(&hub, &alice, &["push"]);
    let merged = ok(&hub, &bob, &["pull", "--author", BOB])["version_uuid"].clone();
    ok(&hub, &bob, &["push"]);
    let (_, lineage) = served_lineage(&hub, "curate/claims");
    let parent = &lineage.version(base.as_str().unwrap()).unwrap().parents[0];
    assert!(lineage.version(parent).is_none());
    assert!(
        lineage
            .boundary_anchors
            .iter()
            .any(|anchor| &anchor.version_uuid == parent)
    );
    let log = ok(&hub, &bob, &["log", "--remote", "--all"]);
    assert!(
        log.as_array()
            .unwrap()
            .iter()
            .any(|entry| entry["version_uuid"] == changed),
        "the first parent's boundary must not hide the second parent: {log}"
    );
    let limited = ok(&hub, &bob, &["log", "--remote", "--all", "-n", "2"]);
    assert_eq!(limited[0]["version_uuid"], merged);
    assert_eq!(limited[1]["version_uuid"], changed);
}