zenith-tx 0.0.7

Zenith transaction op set, apply/dry-run engine, diffs, and audit records.
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
mod common;
use common::*;
use zenith_tx::{Op, Permissions, Position, Transaction, TxStatus, run_transaction};

// ── MoveForward: a moves after b ──────────────────────────────────────────

#[test]
fn move_forward_reorders() {
    let doc = parse(TWO_RECT_DOC);
    let tx = Transaction {
        ops: vec![Op::MoveForward {
            node: "a".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");

    assert_eq!(result.status, TxStatus::Accepted);
    assert_eq!(result.affected_node_ids, vec!["a".to_owned()]);

    // In source_after, "b" should appear before "a" (a is now last).
    let pos_a = result
        .source_after
        .find("id=\"a\"")
        .expect("a in source_after");
    let pos_b = result
        .source_after
        .find("id=\"b\"")
        .expect("b in source_after");
    assert!(pos_b < pos_a, "b should appear before a in source_after");

    // source_before has a before b.
    let pb_a = result
        .source_before
        .find("id=\"a\"")
        .expect("a in source_before");
    let pb_b = result
        .source_before
        .find("id=\"b\"")
        .expect("b in source_before");
    assert!(pb_a < pb_b, "a should appear before b in source_before");
}

// ── MoveForward: reorder among group siblings ─────────────────────────────

#[test]
fn tx_move_forward_reorders_nested_child() {
    // Two rects (a then b) nested inside a group. MoveForward on "a"
    // should reorder them so b appears before a in source_after.
    let doc = parse(GROUP_TWO_RECT_DOC);
    let tx = Transaction {
        ops: vec![Op::MoveForward {
            node: "a".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");

    assert_eq!(result.status, TxStatus::Accepted);
    assert_eq!(result.affected_node_ids, vec!["a".to_owned()]);

    // In source_after, "b" should appear before "a".
    let pos_a = result
        .source_after
        .find("id=\"a\"")
        .expect("a in source_after");
    let pos_b = result
        .source_after
        .find("id=\"b\"")
        .expect("b in source_after");
    assert!(pos_b < pos_a, "b should appear before a in source_after");

    // source_before has a before b.
    let pb_a = result
        .source_before
        .find("id=\"a\"")
        .expect("a in source_before");
    let pb_b = result
        .source_before
        .find("id=\"b\"")
        .expect("b in source_before");
    assert!(pb_a < pb_b, "a should appear before b in source_before");
}

// ── MoveBackward tests ────────────────────────────────────────────────────

#[test]
fn move_backward_reorders() {
    // Doc: a (bottom) then b (top). MoveBackward on b → b moves before a.
    let doc = parse(TWO_RECT_DOC);
    let tx = Transaction {
        ops: vec![Op::MoveBackward {
            node: "b".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");

    assert_eq!(result.status, TxStatus::Accepted);
    assert_eq!(result.affected_node_ids, vec!["b".to_owned()]);

    // In source_after, b should appear before a.
    let pos_a = result
        .source_after
        .find("id=\"a\"")
        .expect("a in source_after");
    let pos_b = result
        .source_after
        .find("id=\"b\"")
        .expect("b in source_after");
    assert!(pos_b < pos_a, "b should appear before a in source_after");
}

#[test]
fn move_backward_already_at_back_noop() {
    // Doc: a (bottom) then b. MoveBackward on "a" → already at back → noop advisory.
    let doc = parse(TWO_RECT_DOC);
    let tx = Transaction {
        ops: vec![Op::MoveBackward {
            node: "a".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");

    assert_eq!(result.status, TxStatus::Accepted);
    assert!(
        result.affected_node_ids.is_empty(),
        "affected must be empty for noop; got: {:?}",
        result.affected_node_ids
    );
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "tx.noop" && d.message.contains("back")),
        "expected tx.noop advisory mentioning \"back\"; got: {:?}",
        result.diagnostics
    );
    assert_eq!(result.source_after, result.source_before);
}

#[test]
fn move_backward_nested_child() {
    // Group with x (bottom) then y (top). MoveBackward on y → recursion into
    // group, y swaps with x.
    let doc = parse(GROUP_TWO_RECT_BACKWARD_DOC);
    let tx = Transaction {
        ops: vec![Op::MoveBackward {
            node: "y".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");

    assert_eq!(result.status, TxStatus::Accepted);
    assert_eq!(result.affected_node_ids, vec!["y".to_owned()]);

    // In source_after, y should appear before x.
    let pos_x = result
        .source_after
        .find("id=\"x\"")
        .expect("x in source_after");
    let pos_y = result
        .source_after
        .find("id=\"y\"")
        .expect("y in source_after");
    assert!(pos_y < pos_x, "y should appear before x in source_after");
}

// ── MoveToFront tests ─────────────────────────────────────────────────────

#[test]
fn move_to_front_moves_to_top() {
    // THREE_RECT_DOC: a (0), b (1), c (2). MoveToFront on "a" → order becomes b, c, a.
    let doc = parse(THREE_RECT_DOC);
    let tx = Transaction {
        ops: vec![Op::MoveToFront {
            node: "a".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");

    assert_eq!(result.status, TxStatus::Accepted);
    assert_eq!(result.affected_node_ids, vec!["a".to_owned()]);

    // In source_after: b appears before c, c appears before a.
    let pos_a = result
        .source_after
        .find("id=\"a\"")
        .expect("a in source_after");
    let pos_b = result
        .source_after
        .find("id=\"b\"")
        .expect("b in source_after");
    let pos_c = result
        .source_after
        .find("id=\"c\"")
        .expect("c in source_after");
    assert!(pos_b < pos_c, "b should appear before c in source_after");
    assert!(pos_c < pos_a, "c should appear before a in source_after");
}

#[test]
fn move_to_front_already_front_noop() {
    // THREE_RECT_DOC: c is already the last (topmost). MoveToFront on "c" → noop.
    let doc = parse(THREE_RECT_DOC);
    let tx = Transaction {
        ops: vec![Op::MoveToFront {
            node: "c".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");

    assert_eq!(result.status, TxStatus::Accepted);
    assert!(
        result.affected_node_ids.is_empty(),
        "affected must be empty for noop; got: {:?}",
        result.affected_node_ids
    );
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "tx.noop" && d.message.contains("front")),
        "expected tx.noop advisory mentioning \"front\"; got: {:?}",
        result.diagnostics
    );
    assert_eq!(result.source_after, result.source_before);
}

// ── MoveToBack tests ──────────────────────────────────────────────────────

#[test]
fn move_to_back_moves_to_bottom() {
    // THREE_RECT_DOC: a (0), b (1), c (2). MoveToBack on "c" → order becomes c, a, b.
    let doc = parse(THREE_RECT_DOC);
    let tx = Transaction {
        ops: vec![Op::MoveToBack {
            node: "c".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");

    assert_eq!(result.status, TxStatus::Accepted);
    assert_eq!(result.affected_node_ids, vec!["c".to_owned()]);

    // In source_after: c appears before a, a appears before b.
    let pos_a = result
        .source_after
        .find("id=\"a\"")
        .expect("a in source_after");
    let pos_b = result
        .source_after
        .find("id=\"b\"")
        .expect("b in source_after");
    let pos_c = result
        .source_after
        .find("id=\"c\"")
        .expect("c in source_after");
    assert!(pos_c < pos_a, "c should appear before a in source_after");
    assert!(pos_a < pos_b, "a should appear before b in source_after");
}

// ── Group tests ───────────────────────────────────────────────────────────

/// Group two sibling rects → parent now has one group containing both,
/// inserted at the position of the first (r1's original index = 0).
#[test]
fn group_two_sibling_rects() {
    let doc = parse(TWO_SIBLING_RECTS);
    let tx = Transaction {
        ops: vec![Op::Group {
            node_ids: vec!["r1".to_owned(), "r2".to_owned()],
            group_id: "grp-new".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    assert_eq!(
        result.status,
        TxStatus::Accepted,
        "diagnostics: {:?}",
        result.diagnostics
    );
    assert!(
        result.affected_node_ids.contains(&"grp-new".to_owned()),
        "grp-new must be in affected_node_ids"
    );
    // The page should now contain exactly one top-level node: the group.
    assert!(
        result.source_after.contains("id=\"grp-new\""),
        "source_after must contain the new group id"
    );
    // r1 and r2 are inside the group, not at the page level.
    // Both ids should still appear in the source (as group children).
    assert!(
        result.source_after.contains("id=\"r1\""),
        "r1 must appear inside the group"
    );
    assert!(
        result.source_after.contains("id=\"r2\""),
        "r2 must appear inside the group"
    );
    // r1 must appear before r2 in source_after (relative order preserved).
    let pos_r1 = result
        .source_after
        .find("id=\"r1\"")
        .expect("r1 in source_after");
    let pos_r2 = result
        .source_after
        .find("id=\"r2\"")
        .expect("r2 in source_after");
    assert!(pos_r1 < pos_r2, "r1 must precede r2 inside the group");
    // The group must appear before both rects in source_after (group wraps them).
    let pos_grp = result
        .source_after
        .find("id=\"grp-new\"")
        .expect("grp-new in source_after");
    assert!(pos_grp < pos_r1, "group node must open before its children");
}

/// Attempting to group nodes that do not share a parent → tx.invalid_parent.
#[test]
fn group_non_siblings_rejected() {
    let doc = parse(PAGE_WITH_GROUP);
    // r1 is inside grp1, r3 is a top-level sibling of grp1 → different parents.
    let tx = Transaction {
        ops: vec![Op::Group {
            node_ids: vec!["r1".to_owned(), "r3".to_owned()],
            group_id: "grp-bad".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    assert_eq!(result.status, TxStatus::Rejected);
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "tx.invalid_parent"),
        "expected tx.invalid_parent; got: {:?}",
        result.diagnostics
    );
    assert_eq!(result.source_after, result.source_before);
}

// ── Ungroup tests ─────────────────────────────────────────────────────────

/// Ungroup a group → its children move up to the parent in order, group gone.
#[test]
fn ungroup_splices_children_in_place() {
    let doc = parse(PAGE_WITH_GROUP);
    let tx = Transaction {
        ops: vec![Op::Ungroup {
            group_id: "grp1".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    // AcceptedWithWarnings is fine; exact status depends on post-validate.
    assert_ne!(
        result.status,
        TxStatus::Rejected,
        "ungroup must not be rejected; diagnostics: {:?}",
        result.diagnostics
    );
    // The group id should no longer appear in source_after.
    assert!(
        !result.source_after.contains("id=\"grp1\""),
        "group grp1 must be gone from source_after;\n{}",
        result.source_after
    );
    // r1 and r2 must still be present (now at page level).
    assert!(
        result.source_after.contains("id=\"r1\""),
        "r1 must appear in source_after"
    );
    assert!(
        result.source_after.contains("id=\"r2\""),
        "r2 must appear in source_after"
    );
    // r1 must appear before r2 (order preserved).
    let pos_r1 = result
        .source_after
        .find("id=\"r1\"")
        .expect("r1 in source_after");
    let pos_r2 = result
        .source_after
        .find("id=\"r2\"")
        .expect("r2 in source_after");
    assert!(pos_r1 < pos_r2, "r1 must precede r2 after ungroup");
    // r3 must still be present.
    assert!(
        result.source_after.contains("id=\"r3\""),
        "r3 must remain in source_after"
    );
}

/// Ungrouping a node that is not a group → tx.unsupported_property.
#[test]
fn ungroup_non_group_rejected() {
    let doc = parse(PAGE_WITH_GROUP);
    let tx = Transaction {
        ops: vec![Op::Ungroup {
            group_id: "r1".to_owned(), // r1 is a rect, not a group
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    assert_eq!(result.status, TxStatus::Rejected);
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "tx.unsupported_property"),
        "expected tx.unsupported_property; got: {:?}",
        result.diagnostics
    );
    assert_eq!(result.source_after, result.source_before);
}

/// Ungrouping a group with non-zero x/y emits an advisory but still applies.
#[test]
fn ungroup_with_offset_emits_advisory() {
    let doc = parse(PAGE_WITH_OFFSET_GROUP);
    let tx = Transaction {
        ops: vec![Op::Ungroup {
            group_id: "grp1".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    // Must not be rejected.
    assert_ne!(
        result.status,
        TxStatus::Rejected,
        "ungroup with offset must not be rejected; diagnostics: {:?}",
        result.diagnostics
    );
    // Advisory (tx.noop) must be present.
    assert!(
        result.diagnostics.iter().any(|d| d.code == "tx.noop"),
        "expected tx.noop advisory for offset group; got: {:?}",
        result.diagnostics
    );
    // Group must be gone; r1 must remain.
    assert!(
        !result.source_after.contains("id=\"grp1\""),
        "group must be dissolved"
    );
    assert!(
        result.source_after.contains("id=\"r1\""),
        "r1 must survive ungroup"
    );
}

// ── Reparent tests ────────────────────────────────────────────────────────

/// Move a top-level rect into an existing group.
#[test]
fn reparent_rect_into_group() {
    let doc = parse(PAGE_WITH_GROUP);
    // r3 is a top-level rect; move it into grp1.
    let tx = Transaction {
        ops: vec![Op::Reparent {
            node: "r3".to_owned(),
            new_parent: "grp1".to_owned(),
            position: Position::Last,
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    assert_eq!(
        result.status,
        TxStatus::Accepted,
        "diagnostics: {:?}",
        result.diagnostics
    );
    assert!(
        result.affected_node_ids.contains(&"r3".to_owned()),
        "r3 must be in affected_node_ids"
    );
    // r3 must still be present somewhere in the output.
    assert!(
        result.source_after.contains("id=\"r3\""),
        "r3 must appear in source_after"
    );
    // grp1 must contain r3 (grp1 opens before r3 in the serialised form).
    let pos_grp = result
        .source_after
        .find("id=\"grp1\"")
        .expect("grp1 in source_after");
    let pos_r3 = result
        .source_after
        .find("id=\"r3\"")
        .expect("r3 in source_after");
    assert!(
        pos_grp < pos_r3,
        "r3 must appear after grp1 opens (inside it)"
    );
}

/// Reparent into a non-container (a rect) → tx.invalid_parent.
#[test]
fn reparent_into_non_container_rejected() {
    let doc = parse(PAGE_WITH_GROUP);
    let tx = Transaction {
        ops: vec![Op::Reparent {
            node: "r3".to_owned(),
            new_parent: "r1".to_owned(), // r1 is a rect, not a container
            position: Position::Last,
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    assert_eq!(result.status, TxStatus::Rejected);
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "tx.invalid_parent"),
        "expected tx.invalid_parent; got: {:?}",
        result.diagnostics
    );
    assert_eq!(result.source_after, result.source_before);
}

/// Reparent a group into its own child group → cycle → tx.invalid_parent.
#[test]
fn reparent_into_own_subtree_rejected() {
    let doc = parse(NESTED_GROUPS);
    // Try to move `outer` into `inner` (inner is a descendant of outer).
    let tx = Transaction {
        ops: vec![Op::Reparent {
            node: "outer".to_owned(),
            new_parent: "inner".to_owned(),
            position: Position::Last,
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    assert_eq!(result.status, TxStatus::Rejected);
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "tx.invalid_parent"),
        "expected tx.invalid_parent (cycle); got: {:?}",
        result.diagnostics
    );
    assert_eq!(result.source_after, result.source_before);
}

// ── AddPage / DeletePage / ReorderPages tests ─────────────────────────────

#[test]
fn add_page_append() {
    let doc = parse(TWO_PAGE_STRUCT_DOC);
    let tx = Transaction {
        ops: vec![Op::AddPage {
            id: "pg3".to_owned(),
            w: "(px)800".to_owned(),
            h: "(px)600".to_owned(),
            background: Some("color.bg".to_owned()),
            index: None,
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");
    assert_eq!(
        result.status,
        TxStatus::Accepted,
        "diagnostics: {:?}",
        result.diagnostics
    );
    assert_eq!(
        page_id_order(&result.source_after),
        vec!["pg1", "pg2", "pg3"],
        "new page must be appended last"
    );
    assert_eq!(result.affected_node_ids, vec!["pg3".to_owned()]);
}

#[test]
fn add_page_at_index() {
    let doc = parse(TWO_PAGE_STRUCT_DOC);
    let tx = Transaction {
        ops: vec![Op::AddPage {
            id: "pg.mid".to_owned(),
            w: "(px)800".to_owned(),
            h: "(px)600".to_owned(),
            background: None,
            index: Some(1),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");
    assert_eq!(
        result.status,
        TxStatus::Accepted,
        "diagnostics: {:?}",
        result.diagnostics
    );
    assert_eq!(
        page_id_order(&result.source_after),
        vec!["pg1", "pg.mid", "pg2"],
        "new page must be inserted at index 1"
    );
}

#[test]
fn add_page_duplicate_id_rejected() {
    let doc = parse(TWO_PAGE_STRUCT_DOC);
    let tx = Transaction {
        ops: vec![Op::AddPage {
            id: "pg1".to_owned(),
            w: "(px)800".to_owned(),
            h: "(px)600".to_owned(),
            background: None,
            index: None,
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");
    assert_eq!(result.status, TxStatus::Rejected);
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "tx.duplicate_id"),
        "expected tx.duplicate_id; got: {:?}",
        result.diagnostics
    );
    assert_eq!(result.source_after, result.source_before);
}

#[test]
fn add_page_out_of_range_index_rejected() {
    let doc = parse(TWO_PAGE_STRUCT_DOC);
    let tx = Transaction {
        ops: vec![Op::AddPage {
            id: "pg3".to_owned(),
            w: "(px)800".to_owned(),
            h: "(px)600".to_owned(),
            background: None,
            index: Some(5),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");
    assert_eq!(result.status, TxStatus::Rejected);
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "tx.out_of_range"),
        "expected tx.out_of_range; got: {:?}",
        result.diagnostics
    );
    assert_eq!(result.source_after, result.source_before);
}

#[test]
fn add_page_invalid_dimension_rejected() {
    let doc = parse(TWO_PAGE_STRUCT_DOC);
    let tx = Transaction {
        ops: vec![Op::AddPage {
            id: "pg3".to_owned(),
            w: "not-a-dim".to_owned(),
            h: "(px)600".to_owned(),
            background: None,
            index: None,
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");
    assert_eq!(result.status, TxStatus::Rejected);
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "tx.invalid_value"),
        "expected tx.invalid_value; got: {:?}",
        result.diagnostics
    );
}

#[test]
fn delete_page_removes() {
    let doc = parse(TWO_PAGE_STRUCT_DOC);
    let tx = Transaction {
        ops: vec![Op::DeletePage {
            page: "pg1".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");
    assert_eq!(
        result.status,
        TxStatus::Accepted,
        "diagnostics: {:?}",
        result.diagnostics
    );
    assert_eq!(
        page_id_order(&result.source_after),
        vec!["pg2"],
        "pg1 must be removed"
    );
    assert_eq!(result.affected_node_ids, vec!["pg1".to_owned()]);
}

#[test]
fn delete_page_unknown_rejected() {
    let doc = parse(TWO_PAGE_STRUCT_DOC);
    let tx = Transaction {
        ops: vec![Op::DeletePage {
            page: "nope".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");
    assert_eq!(result.status, TxStatus::Rejected);
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "tx.unknown_node"),
        "expected tx.unknown_node; got: {:?}",
        result.diagnostics
    );
    assert_eq!(result.source_after, result.source_before);
}

#[test]
fn reorder_pages_permutation() {
    let doc = parse(TWO_PAGE_STRUCT_DOC);
    let tx = Transaction {
        ops: vec![Op::ReorderPages {
            order: vec!["pg2".to_owned(), "pg1".to_owned()],
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");
    assert_eq!(
        result.status,
        TxStatus::Accepted,
        "diagnostics: {:?}",
        result.diagnostics
    );
    assert_eq!(
        page_id_order(&result.source_after),
        vec!["pg2", "pg1"],
        "pages must be reordered to match `order`"
    );
}

#[test]
fn reorder_pages_missing_id_rejected() {
    let doc = parse(TWO_PAGE_STRUCT_DOC);
    let tx = Transaction {
        ops: vec![Op::ReorderPages {
            order: vec!["pg1".to_owned()],
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");
    assert_eq!(result.status, TxStatus::Rejected);
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "tx.invalid_value"),
        "expected tx.invalid_value; got: {:?}",
        result.diagnostics
    );
    assert_eq!(result.source_after, result.source_before);
}

#[test]
fn reorder_pages_extra_id_rejected() {
    let doc = parse(TWO_PAGE_STRUCT_DOC);
    let tx = Transaction {
        ops: vec![Op::ReorderPages {
            order: vec!["pg1".to_owned(), "pg2".to_owned(), "pg3".to_owned()],
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");
    assert_eq!(result.status, TxStatus::Rejected);
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "tx.invalid_value"),
        "expected tx.invalid_value; got: {:?}",
        result.diagnostics
    );
}

#[test]
fn reorder_pages_duplicate_id_rejected() {
    let doc = parse(TWO_PAGE_STRUCT_DOC);
    let tx = Transaction {
        ops: vec![Op::ReorderPages {
            order: vec!["pg1".to_owned(), "pg1".to_owned()],
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction should not error");
    assert_eq!(result.status, TxStatus::Rejected);
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "tx.invalid_value"),
        "expected tx.invalid_value; got: {:?}",
        result.diagnostics
    );
}