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
mod common;
use common::*;
use zenith_tx::{Op, Permissions, Transaction, TxStatus, run_transaction};

// ── Fixture ───────────────────────────────────────────────────────────────────
//
// An 800×600 page with:
//   - "box": a 60×40 rect at (100, 80)  — used for align_to_edge tests
//   - "far": a 50×50 rect at (750, 580) — lies near the edge; used for the
//     shrink-page / off_canvas test
//
// All coords and dims are (px). No tokens needed for geometry-only ops.
const LAYOUT_DOC: &str = r##"zenith version=1 {
  project id="proj" name="Test"
  tokens format="zenith-token-v1" { }
  styles { }
  document id="doc1" title="T" {
    page id="pg1" w=(px)800 h=(px)600 {
      rect id="box" x=(px)100 y=(px)80 w=(px)60 h=(px)40
      rect id="far" x=(px)750 y=(px)580 w=(px)50 h=(px)50
    }
  }
}"##;

// ── set_page_size: happy path ─────────────────────────────────────────────────

#[test]
fn set_page_size_accepted_updates_dimensions() {
    let doc = parse(LAYOUT_DOC);
    let tx = Transaction {
        ops: vec![Op::SetPageSize {
            page: "pg1".to_owned(),
            w: "(px)794".to_owned(),
            h: "(px)1123".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    // The transaction must be accepted (or accepted with warnings from off_canvas).
    assert!(
        matches!(
            result.status,
            TxStatus::Accepted | TxStatus::AcceptedWithWarnings
        ),
        "expected Accepted or AcceptedWithWarnings; got {:?}, diagnostics: {:?}",
        result.status,
        result.diagnostics
    );

    // The page id must appear in affected_node_ids.
    assert!(
        result.affected_node_ids.contains(&"pg1".to_owned()),
        "pg1 must be in affected_node_ids; got {:?}",
        result.affected_node_ids
    );

    // The serialised source must reflect the new dimensions.
    assert!(
        result.source_after.contains("w=(px)794"),
        "source_after must contain w=(px)794; got:\n{}",
        result.source_after
    );
    assert!(
        result.source_after.contains("h=(px)1123"),
        "source_after must contain h=(px)1123; got:\n{}",
        result.source_after
    );
}

// ── set_page_size: unknown page → tx.unknown_node ────────────────────────────

#[test]
fn set_page_size_unknown_page_rejected() {
    let doc = parse(LAYOUT_DOC);
    let tx = Transaction {
        ops: vec![Op::SetPageSize {
            page: "ghost.page".to_owned(),
            w: "(px)400".to_owned(),
            h: "(px)300".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    assert_eq!(
        result.status,
        TxStatus::Rejected,
        "diagnostics: {:?}",
        result.diagnostics
    );
    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);
}

// ── set_page_size: unparseable w → tx.invalid_value ──────────────────────────

#[test]
fn set_page_size_bad_w_rejected() {
    let doc = parse(LAYOUT_DOC);
    let tx = Transaction {
        ops: vec![Op::SetPageSize {
            page: "pg1".to_owned(),
            w: "notadimension".to_owned(),
            h: "(px)300".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    assert_eq!(
        result.status,
        TxStatus::Rejected,
        "diagnostics: {:?}",
        result.diagnostics
    );
    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);
}

// ── set_page_size: zero h → tx.invalid_value ─────────────────────────────────

#[test]
fn set_page_size_zero_h_rejected() {
    let doc = parse(LAYOUT_DOC);
    let tx = Transaction {
        ops: vec![Op::SetPageSize {
            page: "pg1".to_owned(),
            w: "(px)800".to_owned(),
            h: "(px)0".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    assert_eq!(
        result.status,
        TxStatus::Rejected,
        "diagnostics: {:?}",
        result.diagnostics
    );
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "tx.invalid_value"),
        "expected tx.invalid_value for zero h; got: {:?}",
        result.diagnostics
    );
}

// ── set_page_size: negative w → tx.invalid_value ─────────────────────────────

#[test]
fn set_page_size_negative_w_rejected() {
    let doc = parse(LAYOUT_DOC);
    let tx = Transaction {
        ops: vec![Op::SetPageSize {
            page: "pg1".to_owned(),
            w: "(px)-100".to_owned(),
            h: "(px)300".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    assert_eq!(
        result.status,
        TxStatus::Rejected,
        "diagnostics: {:?}",
        result.diagnostics
    );
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "tx.invalid_value"),
        "expected tx.invalid_value for negative w; got: {:?}",
        result.diagnostics
    );
}

// ── set_page_size: shrink so "far" falls off-canvas ──────────────────────────
//
// "far" at (750,580) with size 50×50 on the original 800×600 page.
// After shrinking to 400×300 it lies entirely outside the new bounds.
// Verdict: Accepted — off_canvas is an Advisory (not a Warning), so it does NOT
// flip the status to AcceptedWithWarnings; the advisory diagnostic is still
// emitted. The resize is applied AND child coordinates are unchanged (no reflow).
#[test]
fn set_page_size_shrink_child_falls_off_canvas() {
    let doc = parse(LAYOUT_DOC);
    let tx = Transaction {
        ops: vec![Op::SetPageSize {
            page: "pg1".to_owned(),
            w: "(px)400".to_owned(),
            h: "(px)300".to_owned(),
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    // The resize itself is valid → not Rejected. off_canvas is an Advisory
    // (below Warning), so the status remains Accepted.
    assert_eq!(
        result.status,
        TxStatus::Accepted,
        "expected Accepted (off_canvas is an advisory, not a warning); got {:?}, diagnostics: {:?}",
        result.status,
        result.diagnostics
    );

    // At least one off_canvas advisory must be present.
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "layout.off_canvas"),
        "expected an off_canvas advisory; got: {:?}",
        result.diagnostics
    );

    // The page resize must have been applied.
    assert!(
        result.source_after.contains("w=(px)400"),
        "source_after must contain w=(px)400"
    );
    assert!(
        result.source_after.contains("h=(px)300"),
        "source_after must contain h=(px)300"
    );

    // Child node "far" must retain its original coordinates (no reflow).
    let far_x = extract_px_attr(&result.source_after, "far", "x").expect("far.x");
    let far_y = extract_px_attr(&result.source_after, "far", "y").expect("far.y");
    assert!(
        (far_x - 750.0).abs() < 1e-9,
        "far.x must be unchanged (750); got {far_x}"
    );
    assert!(
        (far_y - 580.0).abs() < 1e-9,
        "far.y must be unchanged (580); got {far_y}"
    );

    // "pg1" is in affected.
    assert!(
        result.affected_node_ids.contains(&"pg1".to_owned()),
        "pg1 must be in affected_node_ids"
    );
}

// ── align_to_edge: right with margin 20 ──────────────────────────────────────
//
// "box": w=60, page_w=800 → x = 800 - 60 - 20 = 720

#[test]
fn align_to_edge_right_margin() {
    let doc = parse(LAYOUT_DOC);
    let tx = Transaction {
        ops: vec![Op::AlignToEdge {
            node: "box".to_owned(),
            edge: "right".to_owned(),
            margin: 20.0,
        }],
        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(&"box".to_owned()));

    let x = extract_px_attr(&result.source_after, "box", "x").expect("box.x");
    // 800 - 60 - 20 = 720
    assert!(
        (x - 720.0).abs() < 1e-9,
        "expected x=720 for right+margin=20; got {x}"
    );

    // y must be unchanged.
    let y = extract_px_attr(&result.source_after, "box", "y").expect("box.y");
    assert!((y - 80.0).abs() < 1e-9, "y must be unchanged (80); got {y}");
}

// ── align_to_edge: bottom with margin 20 ─────────────────────────────────────
//
// "box": h=40, page_h=600 → y = 600 - 40 - 20 = 540

#[test]
fn align_to_edge_bottom_margin() {
    let doc = parse(LAYOUT_DOC);
    let tx = Transaction {
        ops: vec![Op::AlignToEdge {
            node: "box".to_owned(),
            edge: "bottom".to_owned(),
            margin: 20.0,
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    assert_eq!(
        result.status,
        TxStatus::Accepted,
        "diagnostics: {:?}",
        result.diagnostics
    );

    let y = extract_px_attr(&result.source_after, "box", "y").expect("box.y");
    // 600 - 40 - 20 = 540
    assert!(
        (y - 540.0).abs() < 1e-9,
        "expected y=540 for bottom+margin=20; got {y}"
    );

    // x must be unchanged.
    let x = extract_px_attr(&result.source_after, "box", "x").expect("box.x");
    assert!(
        (x - 100.0).abs() < 1e-9,
        "x must be unchanged (100); got {x}"
    );
}

// ── align_to_edge: two ops in one transaction → bottom-right corner ───────────
//
// "box": w=60, h=40, page 800×600, margin=20 on both axes.
// right: x = 800 - 60 - 20 = 720
// bottom: y = 600 - 40 - 20 = 540

#[test]
fn align_to_edge_right_and_bottom_corner() {
    let doc = parse(LAYOUT_DOC);
    let tx = Transaction {
        ops: vec![
            Op::AlignToEdge {
                node: "box".to_owned(),
                edge: "right".to_owned(),
                margin: 20.0,
            },
            Op::AlignToEdge {
                node: "box".to_owned(),
                edge: "bottom".to_owned(),
                margin: 20.0,
            },
        ],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    assert_eq!(
        result.status,
        TxStatus::Accepted,
        "diagnostics: {:?}",
        result.diagnostics
    );

    let x = extract_px_attr(&result.source_after, "box", "x").expect("box.x");
    let y = extract_px_attr(&result.source_after, "box", "y").expect("box.y");
    assert!(
        (x - 720.0).abs() < 1e-9,
        "expected x=720 (right corner); got {x}"
    );
    assert!(
        (y - 540.0).abs() < 1e-9,
        "expected y=540 (bottom corner); got {y}"
    );
}

// ── align_to_edge: hcenter ────────────────────────────────────────────────────
//
// "box": w=60, page_w=800 → x = (800 - 60) / 2 = 370

#[test]
fn align_to_edge_hcenter() {
    let doc = parse(LAYOUT_DOC);
    let tx = Transaction {
        ops: vec![Op::AlignToEdge {
            node: "box".to_owned(),
            edge: "hcenter".to_owned(),
            margin: 0.0,
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    assert_eq!(
        result.status,
        TxStatus::Accepted,
        "diagnostics: {:?}",
        result.diagnostics
    );

    let x = extract_px_attr(&result.source_after, "box", "x").expect("box.x");
    // (800 - 60) / 2 = 370
    assert!(
        (x - 370.0).abs() < 1e-9,
        "expected x=370 for hcenter; got {x}"
    );
}

// ── align_to_edge: invalid edge → tx.unsupported_property ────────────────────

#[test]
fn align_to_edge_invalid_edge_rejected() {
    let doc = parse(LAYOUT_DOC);
    let tx = Transaction {
        ops: vec![Op::AlignToEdge {
            node: "box".to_owned(),
            edge: "diagonal".to_owned(),
            margin: 0.0,
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

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

// ── align_to_edge: unknown node → tx.unknown_node ────────────────────────────

#[test]
fn align_to_edge_unknown_node_rejected() {
    let doc = parse(LAYOUT_DOC);
    let tx = Transaction {
        ops: vec![Op::AlignToEdge {
            node: "ghost".to_owned(),
            edge: "right".to_owned(),
            margin: 0.0,
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

    assert_eq!(
        result.status,
        TxStatus::Rejected,
        "diagnostics: {:?}",
        result.diagnostics
    );
    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);
}

// ── align_to_edge: node without x/y/w/h geometry → tx.unsupported_property ───
//
// A `line` node carries x1/y1/x2/y2, not x/y/w/h, so read_geometry_px
// returns None for it. We use LINE_DOC from the common fixtures.

#[test]
fn align_to_edge_no_geometry_rejected() {
    let doc = parse(LINE_DOC);
    let tx = Transaction {
        ops: vec![Op::AlignToEdge {
            node: "ln1".to_owned(),
            edge: "right".to_owned(),
            margin: 0.0,
        }],
        permissions: Permissions::default(),
    };
    let result = run_transaction(&doc, &tx).expect("run_transaction must not error");

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