zenith-tool 0.0.5

The Zenith command-line interface (the `zenith` binary) for the design-document toolchain.
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
//! Integration tests for `zenith workspace scratch`, `zenith workspace candidate`,
//! `zenith workspace promote`, `zenith workspace bundle`, and
//! `zenith workspace unbundle`.
//!
//! Uses the `_in` variants of the command functions so that a tempdir-rooted
//! `StorePaths` is passed explicitly — no real data directory is touched and
//! no `ZENITH_DATA_DIR` env-var is required. The harness mirrors
//! `history_pipeline.rs`.

use tempfile::TempDir;
use zenith_cli::cli::ScratchNewArgs;
use zenith_cli::commands::workspace::{
    bundle_doc_in, candidate_set_status_in, finalize_in, promote_in, scratch_list_in,
    scratch_new_in, scratch_show_in, unbundle_doc_in,
};
use zenith_session::StorePaths;

// ── Fixture ───────────────────────────────────────────────────────────────────

/// A minimal valid `.zen` document WITH a `doc-id` attribute (required by
/// `read_doc_id`, which errors when no id is present).
const FIXTURE: &str = r##"zenith version=1 doc-id="01HWSCRATCHTEST000000000001" {
  project id="proj.ws" name="Workspace Test"
  tokens format="zenith-token-v1" {
    token id="color.bg" type="color" value="#ffffff"
  }
  styles {
  }
  document id="doc.ws" title="Workspace Test" {
    page id="page.main" w=(px)400 h=(px)300 {
      rect id="rect.bg" x=(px)0 y=(px)0 w=(px)400 h=(px)300 fill=(token)"color.bg"
    }
  }
}
"##;

const DOC_ID: &str = "01HWSCRATCHTEST000000000001";

fn setup() -> (TempDir, StorePaths, std::path::PathBuf) {
    let tmp = TempDir::new().unwrap();
    let paths = StorePaths::new(tmp.path());
    let doc_path = tmp.path().join("doc.zen");
    std::fs::write(&doc_path, FIXTURE).unwrap();
    (tmp, paths, doc_path)
}

fn new_args(
    doc_path: &std::path::Path,
    page: Option<&str>,
    status: &str,
    notes: Option<&str>,
    promotion_target: Option<&str>,
    cleanup_policy: Option<&str>,
    workspace_role: Option<&str>,
) -> ScratchNewArgs {
    ScratchNewArgs {
        doc: doc_path.to_path_buf(),
        page: page.map(str::to_owned),
        status: status.to_owned(),
        notes: notes.map(str::to_owned),
        promotion_target: promotion_target.map(str::to_owned),
        cleanup_policy: cleanup_policy.map(str::to_owned),
        workspace_role: workspace_role.map(str::to_owned),
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[test]
fn scratch_new_returns_candidate_id() {
    let (_tmp, paths, doc_path) = setup();

    let id = scratch_new_in(
        &paths,
        FIXTURE.as_bytes(),
        &doc_path,
        &new_args(&doc_path, None, "draft", None, None, None, None),
    )
    .unwrap()
    .id;

    assert_eq!(id, "cand0", "first candidate must be cand0");
}

#[test]
fn scratch_list_shows_new_candidate() {
    let (_tmp, paths, doc_path) = setup();

    scratch_new_in(
        &paths,
        FIXTURE.as_bytes(),
        &doc_path,
        &new_args(
            &doc_path,
            Some("page.main"),
            "draft",
            Some("first"),
            None,
            None,
            None,
        ),
    )
    .unwrap();

    let out = scratch_list_in(&paths, &doc_path, false).unwrap();
    assert!(out.contains("cand0"), "listing must mention cand0");
    assert!(out.contains("draft"), "listing must show draft status");
    assert!(out.contains("page.main"), "listing must show page id");
    assert!(out.contains("first"), "listing must show notes");
}

#[test]
fn scratch_list_empty_when_no_candidates() {
    let (_tmp, paths, doc_path) = setup();

    let out = scratch_list_in(&paths, &doc_path, false).unwrap();
    assert!(
        out.contains("no scratch candidates"),
        "empty listing must say so"
    );
}

#[test]
fn scratch_list_json_is_array() {
    let (_tmp, paths, doc_path) = setup();

    scratch_new_in(
        &paths,
        FIXTURE.as_bytes(),
        &doc_path,
        &new_args(&doc_path, None, "draft", None, None, None, None),
    )
    .unwrap();

    let out = scratch_list_in(&paths, &doc_path, true).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert!(parsed.is_array(), "JSON output must be an array");
    assert_eq!(parsed.as_array().unwrap().len(), 1);
}

#[test]
fn candidate_set_status_selected_reflected_in_list() {
    let (_tmp, paths, doc_path) = setup();

    scratch_new_in(
        &paths,
        FIXTURE.as_bytes(),
        &doc_path,
        &new_args(&doc_path, None, "draft", None, None, None, None),
    )
    .unwrap();

    let confirm = candidate_set_status_in(&paths, &doc_path, "cand0", "selected").unwrap();
    assert!(
        confirm.contains("cand0"),
        "confirmation must mention the candidate id"
    );
    assert!(
        confirm.contains("selected"),
        "confirmation must mention the new status"
    );

    // The store must reflect the new status.
    let out = scratch_list_in(&paths, &doc_path, false).unwrap();
    assert!(
        out.contains("selected"),
        "list must show 'selected' after status transition; got: {out}"
    );

    // Verify via session API directly.
    use zenith_session::adapter::OsFs;
    use zenith_session::{CandidateStatus, list_scratch};
    let entries = list_scratch(&OsFs, &paths, DOC_ID).unwrap();
    assert_eq!(entries.len(), 1);
    assert_eq!(entries[0].status, CandidateStatus::Selected);
}

#[test]
fn scratch_show_unknown_id_errors() {
    let (_tmp, paths, doc_path) = setup();

    // Record one candidate so the index exists.
    scratch_new_in(
        &paths,
        FIXTURE.as_bytes(),
        &doc_path,
        &new_args(&doc_path, None, "draft", None, None, None, None),
    )
    .unwrap();

    let result = scratch_show_in(&paths, &doc_path, "cand99", false);
    assert!(result.is_err(), "unknown candidate must error");
    let msg = result.unwrap_err();
    assert!(
        msg.contains("cand99"),
        "error message must mention the missing id; got: {msg}"
    );
}

#[test]
fn bad_status_string_errors() {
    let (_tmp, paths, doc_path) = setup();

    let result = scratch_new_in(
        &paths,
        FIXTURE.as_bytes(),
        &doc_path,
        &new_args(&doc_path, None, "nonsense", None, None, None, None),
    );
    assert!(result.is_err(), "bad status string must error");
    let msg = result.unwrap_err();
    assert!(
        msg.contains("nonsense"),
        "error must include the bad value; got: {msg}"
    );
}

#[test]
fn scratch_show_returns_detail() {
    let (_tmp, paths, doc_path) = setup();

    scratch_new_in(
        &paths,
        FIXTURE.as_bytes(),
        &doc_path,
        &new_args(
            &doc_path,
            Some("page.main"),
            "draft",
            Some("show-me"),
            Some("slot-a"),
            Some("on_select"),
            Some("hero"),
        ),
    )
    .unwrap();

    let out = scratch_show_in(&paths, &doc_path, "cand0", false).unwrap();
    assert!(out.contains("cand0"), "show must include id");
    assert!(out.contains("page.main"), "show must include page");
    assert!(out.contains("draft"), "show must include status");
    assert!(out.contains("show-me"), "show must include notes");
    assert!(out.contains("slot-a"), "show must include promotion_target");
    assert!(
        out.contains("on_select"),
        "show must include cleanup_policy"
    );
    assert!(out.contains("hero"), "show must include workspace_role");
}

#[test]
fn scratch_show_json_roundtrip() {
    let (_tmp, paths, doc_path) = setup();

    scratch_new_in(
        &paths,
        FIXTURE.as_bytes(),
        &doc_path,
        &new_args(
            &doc_path,
            Some("page.main"),
            "selected",
            Some("json-test"),
            None,
            None,
            None,
        ),
    )
    .unwrap();

    let out = scratch_show_in(&paths, &doc_path, "cand0", true).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(parsed["id"], "cand0");
    assert_eq!(parsed["status"], "selected");
    assert_eq!(parsed["page_id"], "page.main");
    assert_eq!(parsed["notes"], "json-test");
}

// ── Promote tests ─────────────────────────────────────────────────────────────

/// Deliverable document: has `doc-id`, two pages — `page.export` is the
/// target page with a placeholder rect.
const DELIVERABLE: &str = r##"zenith version=1 doc-id="01HWSCRATCHTEST000000000001" {
  project id="proj.del" name="Deliverable"
  tokens format="zenith-token-v1" {
    token id="color.bg" type="color" value="#ffffff"
  }
  styles {
  }
  document id="doc.del" title="Deliverable" {
    page id="page.export" w=(px)400 h=(px)300 {
      rect id="placeholder" x=(px)0 y=(px)0 w=(px)400 h=(px)300
    }
  }
}
"##;

/// Candidate snapshot: a selected-status page with content nodes.
const CANDIDATE_SNAP: &str = r##"zenith version=1 doc-id="01HWSCRATCHTEST000000000001" {
  project id="proj.cand" name="Candidate"
  tokens format="zenith-token-v1" {
    token id="color.bg" type="color" value="#ffffff"
  }
  styles {
  }
  document id="doc.cand" title="Candidate" {
    page id="page.source" w=(px)400 h=(px)300 {
      rect id="hero" x=(px)0 y=(px)0 w=(px)400 h=(px)300
      rect id="sub" x=(px)10 y=(px)10 w=(px)100 h=(px)50
    }
  }
}
"##;

fn setup_promote() -> (TempDir, StorePaths, std::path::PathBuf) {
    let tmp = TempDir::new().unwrap();
    let paths = StorePaths::new(tmp.path());
    let doc_path = tmp.path().join("deliver.zen");
    std::fs::write(&doc_path, DELIVERABLE).unwrap();
    (tmp, paths, doc_path)
}

fn record_selected_candidate(
    paths: &StorePaths,
    doc_path: &std::path::Path,
    page_id: &str,
    snap: &[u8],
) -> String {
    let cand_id = scratch_new_in(
        paths,
        snap,
        doc_path,
        &new_args(doc_path, Some(page_id), "draft", None, None, None, None),
    )
    .unwrap()
    .id;
    candidate_set_status_in(paths, doc_path, &cand_id, "selected").unwrap();
    cand_id
}

#[test]
fn promote_selected_candidate_merges_content() {
    let (_tmp, paths, doc_path) = setup_promote();
    let cand_id =
        record_selected_candidate(&paths, &doc_path, "page.source", CANDIDATE_SNAP.as_bytes());

    let out = promote_in(&paths, &doc_path, &cand_id, "page.export", ".promoted").unwrap();
    assert!(
        out.contains(&cand_id),
        "confirmation must mention candidate id"
    );
    assert!(
        out.contains("page.export"),
        "confirmation must mention target page"
    );

    // Read the written file and verify the promoted content.
    let written = std::fs::read_to_string(&doc_path).unwrap();
    assert!(
        written.contains("hero.promoted"),
        "written doc must contain suffixed hero id; got:\n{written}"
    );
    assert!(
        written.contains("sub.promoted"),
        "written doc must contain suffixed sub id; got:\n{written}"
    );
    // The old placeholder must be gone (replaced).
    assert!(
        !written.contains("\"placeholder\""),
        "placeholder must be replaced; got:\n{written}"
    );
}

#[test]
fn promote_draft_candidate_errors() {
    let (_tmp, paths, doc_path) = setup_promote();
    // Record a draft candidate (do NOT transition to selected).
    let cand_id = scratch_new_in(
        &paths,
        CANDIDATE_SNAP.as_bytes(),
        &doc_path,
        &new_args(
            &doc_path,
            Some("page.source"),
            "draft",
            None,
            None,
            None,
            None,
        ),
    )
    .unwrap()
    .id;

    let result = promote_in(&paths, &doc_path, &cand_id, "page.export", ".promoted");
    assert!(result.is_err(), "promoting a draft candidate must error");
    let msg = result.unwrap_err();
    assert!(
        msg.contains("selected"),
        "error must mention 'selected'; got: {msg}"
    );
    assert!(
        msg.contains(&cand_id),
        "error must mention the candidate id; got: {msg}"
    );
}

#[test]
fn promote_missing_candidate_errors() {
    let (_tmp, paths, doc_path) = setup_promote();

    let result = promote_in(&paths, &doc_path, "cand99", "page.export", ".promoted");
    assert!(result.is_err(), "unknown candidate must error");
    let msg = result.unwrap_err();
    assert!(
        msg.contains("cand99"),
        "error must mention the missing id; got: {msg}"
    );
}

#[test]
fn promote_missing_target_page_errors() {
    let (_tmp, paths, doc_path) = setup_promote();
    let cand_id =
        record_selected_candidate(&paths, &doc_path, "page.source", CANDIDATE_SNAP.as_bytes());

    let result = promote_in(
        &paths,
        &doc_path,
        &cand_id,
        "page.does-not-exist",
        ".promoted",
    );
    assert!(result.is_err(), "missing target page must error");
    let msg = result.unwrap_err();
    assert!(
        msg.contains("page.does-not-exist"),
        "error must mention the missing page id; got: {msg}"
    );
}

// ── Token-palette promote tests ───────────────────────────────────────────────

/// Deliverable with palette B: has `color.shared` (value "#ffffff") and
/// `color.del-only` ("#aabbcc"). The candidate will carry a DIFFERENT value for
/// `color.shared` and an additional `color.cand-only` token.
const DELIVERABLE_PALETTE_B: &str = r##"zenith version=1 doc-id="01HWSCRATCHTEST000000000002" {
  project id="proj.del2" name="Deliverable-B"
  tokens format="zenith-token-v1" {
    token id="color.shared" type="color" value="#ffffff"
    token id="color.del-only" type="color" value="#aabbcc"
  }
  styles {
  }
  document id="doc.del2" title="Deliverable-B" {
    page id="page.export" w=(px)400 h=(px)300 {
      rect id="placeholder" x=(px)0 y=(px)0 w=(px)400 h=(px)300
    }
  }
}
"##;

/// Candidate snapshot with palette A: `color.shared` has a DIFFERENT value
/// ("#000000"), and it introduces `color.cand-only` ("#112233") while being
/// silent about `color.del-only`.
const CANDIDATE_PALETTE_A: &str = r##"zenith version=1 doc-id="01HWSCRATCHTEST000000000002" {
  project id="proj.cand2" name="Candidate-A"
  tokens format="zenith-token-v1" {
    token id="color.shared" type="color" value="#000000"
    token id="color.cand-only" type="color" value="#112233"
  }
  styles {
  }
  document id="doc.cand2" title="Candidate-A" {
    page id="page.source" w=(px)400 h=(px)300 {
      rect id="hero2" x=(px)0 y=(px)0 w=(px)400 h=(px)300
    }
  }
}
"##;

fn setup_promote_palette() -> (TempDir, StorePaths, std::path::PathBuf) {
    let tmp = TempDir::new().unwrap();
    let paths = StorePaths::new(tmp.path());
    let doc_path = tmp.path().join("deliver2.zen");
    std::fs::write(&doc_path, DELIVERABLE_PALETTE_B).unwrap();
    (tmp, paths, doc_path)
}

/// Promoting a candidate whose palette diverges from the deliverable must:
/// - overwrite the shared token id with the candidate's value,
/// - retain the deliverable-only token,
/// - append the candidate-only token,
/// - produce a deterministic final ordering.
#[test]
fn promote_reconciles_candidate_tokens_into_deliverable() {
    use zenith_core::{KdlAdapter, KdlSource as _};

    let (_tmp, paths, doc_path) = setup_promote_palette();
    let cand_id = record_selected_candidate(
        &paths,
        &doc_path,
        "page.source",
        CANDIDATE_PALETTE_A.as_bytes(),
    );

    let out = promote_in(&paths, &doc_path, &cand_id, "page.export", ".p").unwrap();
    assert!(
        out.contains(&cand_id),
        "confirmation must mention candidate id"
    );

    // Parse the written document and inspect its token block.
    let written_bytes = std::fs::read(&doc_path).unwrap();
    let doc = KdlAdapter.parse(written_bytes.as_slice()).unwrap();
    let tokens = &doc.tokens.tokens;

    // Collect ids in order for determinism checks.
    let ids: Vec<&str> = tokens.iter().map(|t| t.id.as_str()).collect();

    // shared id must be present and carry the candidate's value.
    let shared = tokens
        .iter()
        .find(|t| t.id == "color.shared")
        .expect("color.shared must be present after promote");
    let shared_val = match &shared.value {
        zenith_core::ast::token::TokenValue::Literal(
            zenith_core::ast::token::TokenLiteral::String(s),
        ) => s.as_str(),
        other => panic!("unexpected token value kind: {other:?}"),
    };
    assert_eq!(
        shared_val, "#000000",
        "color.shared must carry the candidate's value '#000000'; got: {shared_val}"
    );

    // deliverable-only token must be retained.
    assert!(
        tokens.iter().any(|t| t.id == "color.del-only"),
        "color.del-only must be retained; ids present: {ids:?}"
    );

    // candidate-only token must be appended.
    assert!(
        tokens.iter().any(|t| t.id == "color.cand-only"),
        "color.cand-only must be appended; ids present: {ids:?}"
    );

    // Ordering: deliverable order first (shared, del-only), then appended cand-only.
    assert_eq!(
        ids,
        ["color.shared", "color.del-only", "color.cand-only"],
        "token order must be deterministic; got: {ids:?}"
    );
}

// ── Bundle / unbundle tests ───────────────────────────────────────────────────

#[test]
fn bundle_unbundle_roundtrip_through_cli_fns() {
    let (_tmp, paths, doc_path) = setup();

    // Record a scratch candidate so the store has content worth bundling.
    scratch_new_in(
        &paths,
        FIXTURE.as_bytes(),
        &doc_path,
        &new_args(
            &doc_path,
            None,
            "draft",
            Some("bundle-test"),
            None,
            None,
            None,
        ),
    )
    .unwrap();

    // Bundle the doc into a temp file.
    let bundle_file = _tmp.path().join("test.zenithbundle");
    let confirm = bundle_doc_in(&paths, &doc_path, &bundle_file).unwrap();
    assert!(
        confirm.contains(DOC_ID),
        "confirmation must contain doc_id; got: {confirm}"
    );
    assert!(bundle_file.exists(), "bundle file must exist on disk");

    // Unbundle into a completely fresh store root.
    let tmp2 = TempDir::new().unwrap();
    let paths2 = StorePaths::new(tmp2.path());
    let restored_id = unbundle_doc_in(&paths2, &bundle_file).unwrap();
    assert_eq!(restored_id, DOC_ID, "restored doc_id must match original");

    // The scratch candidate list must be accessible in the fresh store.
    let out = scratch_list_in(&paths2, &doc_path, false).unwrap();
    assert!(
        out.contains("cand0"),
        "fresh store must contain the bundled candidate; got: {out}"
    );
}

#[test]
fn bundle_missing_doc_errors() {
    let tmp = TempDir::new().unwrap();
    let paths = StorePaths::new(tmp.path());
    // Create a doc_path that has a doc-id but whose store directory was never created.
    let doc_path = tmp.path().join("ghost.zen");
    std::fs::write(&doc_path, FIXTURE).unwrap();

    let bundle_file = tmp.path().join("ghost.zenithbundle");
    let result = bundle_doc_in(&paths, &doc_path, &bundle_file);
    assert!(result.is_err(), "bundling a non-existent store must error");
    let msg = result.unwrap_err();
    assert!(
        msg.contains(DOC_ID),
        "error must mention the doc_id; got: {msg}"
    );
}

// ── Finalize tests ────────────────────────────────────────────────────────────

#[test]
fn finalize_removes_rejected_delete_candidate() {
    let (_tmp, paths, doc_path) = setup();

    // cand0: rejected + delete → should be removed
    scratch_new_in(
        &paths,
        FIXTURE.as_bytes(),
        &doc_path,
        &new_args(
            &doc_path,
            None,
            "rejected",
            None,
            None,
            Some("delete"),
            None,
        ),
    )
    .unwrap();

    // cand1: draft → kept
    scratch_new_in(
        &paths,
        FIXTURE.as_bytes(),
        &doc_path,
        &new_args(&doc_path, None, "draft", None, None, None, None),
    )
    .unwrap();

    let out = finalize_in(&paths, &doc_path, false).unwrap();
    assert!(
        out.contains("cand0"),
        "report must mention deleted id; got: {out}"
    );
    assert!(
        out.contains("deleted"),
        "report must say 'deleted'; got: {out}"
    );

    // cand0 must be gone from the listing
    let list = scratch_list_in(&paths, &doc_path, false).unwrap();
    assert!(
        !list.contains("cand0"),
        "cand0 must be absent after finalize; got: {list}"
    );
    assert!(
        list.contains("cand1"),
        "cand1 must still be present; got: {list}"
    );
}

#[test]
fn finalize_json_output_is_valid() {
    let (_tmp, paths, doc_path) = setup();

    scratch_new_in(
        &paths,
        FIXTURE.as_bytes(),
        &doc_path,
        &new_args(
            &doc_path,
            None,
            "rejected",
            None,
            None,
            Some("delete"),
            None,
        ),
    )
    .unwrap();

    let out = finalize_in(&paths, &doc_path, true).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&out).unwrap();
    let deleted = parsed["deleted"].as_array().unwrap();
    assert_eq!(deleted.len(), 1);
    assert_eq!(deleted[0], "cand0");
    assert_eq!(parsed["kept"], 0);
}

#[test]
fn finalize_noop_when_no_delete_policy() {
    let (_tmp, paths, doc_path) = setup();

    scratch_new_in(
        &paths,
        FIXTURE.as_bytes(),
        &doc_path,
        &new_args(
            &doc_path,
            None,
            "rejected",
            None,
            None,
            Some("archive"),
            None,
        ),
    )
    .unwrap();

    let out = finalize_in(&paths, &doc_path, false).unwrap();
    assert!(
        out.contains("nothing to delete"),
        "report must say nothing to delete; got: {out}"
    );

    let list = scratch_list_in(&paths, &doc_path, false).unwrap();
    assert!(
        list.contains("cand0"),
        "archive-policy candidate must survive; got: {list}"
    );
}

#[test]
fn unbundle_bad_file_errors() {
    let tmp = TempDir::new().unwrap();
    let paths = StorePaths::new(tmp.path());
    let bad_file = tmp.path().join("bad.zenithbundle");
    std::fs::write(&bad_file, b"not-a-bundle").unwrap();

    let result = unbundle_doc_in(&paths, &bad_file);
    assert!(result.is_err(), "bad bundle file must error");
    let msg = result.unwrap_err();
    assert!(
        msg.contains("magic"),
        "error must mention 'magic'; got: {msg}"
    );
}

// ── auto-attach on `scratch new` ────────────────────────────────────────────────

/// A minimal valid `.zen` document WITHOUT a `doc-id` attribute. `scratch new`
/// must transparently mint + stamp one rather than erroring.
const FIXTURE_NO_ID: &str = r##"zenith version=1 {
  project id="proj.ws" name="Workspace Test"
  tokens format="zenith-token-v1" {
    token id="color.bg" type="color" value="#ffffff"
  }
  styles {
  }
  document id="doc.ws" title="Workspace Test" {
    page id="page.main" w=(px)400 h=(px)300 {
      rect id="rect.bg" x=(px)0 y=(px)0 w=(px)400 h=(px)300 fill=(token)"color.bg"
    }
  }
}
"##;

fn setup_no_id() -> (TempDir, StorePaths, std::path::PathBuf) {
    let tmp = TempDir::new().unwrap();
    let paths = StorePaths::new(tmp.path());
    let doc_path = tmp.path().join("doc.zen");
    std::fs::write(&doc_path, FIXTURE_NO_ID).unwrap();
    (tmp, paths, doc_path)
}

/// `scratch new` on a doc with NO doc-id must succeed, stamp a doc-id into the
/// on-disk file, record an initial Tier-2 version (whose content is the stamped
/// bytes), and create exactly one candidate.
#[test]
fn scratch_new_auto_attaches_doc_id_when_absent() {
    use zenith_core::{KdlAdapter, KdlSource as _};
    use zenith_session::adapter::OsFs;
    use zenith_session::{list_scratch, list_versions, version_content};

    let (_tmp, paths, doc_path) = setup_no_id();

    let id = scratch_new_in(
        &paths,
        FIXTURE_NO_ID.as_bytes(),
        &doc_path,
        &new_args(&doc_path, None, "draft", None, None, None, None),
    )
    .expect("scratch new on a doc with no doc-id must succeed")
    .id;
    assert_eq!(id, "cand0", "first candidate must be cand0");

    // The on-disk file must now carry a stamped doc-id.
    let on_disk = std::fs::read(&doc_path).unwrap();
    let stamped_id = KdlAdapter
        .parse(&on_disk)
        .unwrap()
        .doc_id
        .expect("file must carry a doc-id after auto-attach");
    assert_eq!(
        stamped_id.len(),
        26,
        "stamped doc-id must be a 26-char ULID"
    );

    let fs = OsFs;

    // An initial Tier-2 version must have been recorded by the attach.
    let versions = list_versions(&fs, &paths, &stamped_id).unwrap();
    assert!(
        !versions.is_empty(),
        "auto-attach must record an initial version"
    );

    // The candidate snapshot must equal the post-stamp on-disk bytes.
    let entries = list_scratch(&fs, &paths, &stamped_id).unwrap();
    assert_eq!(entries.len(), 1, "exactly one candidate must exist");
    let stored_version = version_content(&fs, &paths, &stamped_id, &versions[0].id).unwrap();
    assert_eq!(
        stored_version, on_disk,
        "recorded version content must equal the stamped on-disk bytes"
    );
}

/// `scratch new` on a doc that ALREADY has a doc-id must NOT add a spurious
/// history version (behavior unchanged from before auto-attach existed).
#[test]
fn scratch_new_does_not_record_history_when_doc_id_present() {
    use zenith_session::adapter::OsFs;
    use zenith_session::list_versions;

    let (_tmp, paths, doc_path) = setup();

    let fs = OsFs;
    let before = list_versions(&fs, &paths, DOC_ID).unwrap();

    scratch_new_in(
        &paths,
        FIXTURE.as_bytes(),
        &doc_path,
        &new_args(&doc_path, None, "draft", None, None, None, None),
    )
    .unwrap();

    let after = list_versions(&fs, &paths, DOC_ID).unwrap();
    assert_eq!(
        before.len(),
        after.len(),
        "scratch new on a doc with an existing doc-id must not record a version"
    );

    // The on-disk file must be byte-identical (no re-stamp / re-format).
    let on_disk = std::fs::read(&doc_path).unwrap();
    assert_eq!(
        on_disk,
        FIXTURE.as_bytes(),
        "file with an existing doc-id must be left untouched"
    );
}