vivac 0.14.2

Provenance tree for work: every node knows which node it was born from
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
//! The two session hooks, against the binary.
//!
//! `f568`: Claude Code does have a `SessionEnd` event, but the automatic stop
//! hangs off `Stop` instead. `Stop` runs **on every turn**, so the automatic
//! stop has to know when there is nothing to stop for.

mod common;
use common::Sandbox;

fn how_many(vivacs: &str, kind: &str) -> usize {
    vivacs.lines().filter(|l| l.contains(kind)).count()
}

/// `Sandbox::run_stdin`, with `stdout` and `stderr` kept apart: proving the
/// copy warning does not repeat what the brief already showed needs the
/// two streams told apart, the same reason `tests/registry.rs`'s own
/// `run_split` exists.
fn run_stdin_split(c: &Sandbox, args: &[&str], stdin: &str) -> (String, String, i32) {
    use std::io::Write;
    let mut child = std::process::Command::new(env!("CARGO_BIN_EXE_vivac"))
        .current_dir(&c.0)
        .env("VIVAC_HOME", c.global_home())
        .args(args)
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .unwrap();
    child
        .stdin
        .take()
        .unwrap()
        .write_all(stdin.as_bytes())
        .unwrap();
    let o = child.wait_with_output().unwrap();
    (
        String::from_utf8_lossy(&o.stdout).into_owned(),
        String::from_utf8_lossy(&o.stderr).into_owned(),
        o.status.code().unwrap_or(-1),
    )
}

/// `t594`: `session start --hook` printed the brief --
/// which already opens with the copy block (`t594` §4.7) -- and then, once
/// its own write ran, the generic dispatch preamble said the very same
/// thing again on `stderr`. The two streams are checked apart, not
/// together: the review's own reproduction counted both at once, and that
/// is exactly what let the leak into `stderr` hide behind a passing count.
#[test]
fn session_start_hook_from_a_copy_shows_the_notice_once() {
    let original = Sandbox::new_seeded("session-copy-orig");
    original.ok(&["push", "a goal", "--why", "so the log has a first event"]);
    let copy = Sandbox::new_empty_in("session-copy-copy", original.global_home());
    std::fs::create_dir_all(copy.0.join(".vivac")).unwrap();
    std::fs::copy(
        original.0.join(".vivac").join("events"),
        copy.0.join(".vivac").join("events"),
    )
    .unwrap();

    let (stdout, stderr, code) = run_stdin_split(&copy, &["session", "start", "--hook"], "{}");
    assert_eq!(code, 0, "{stdout}{stderr}");
    assert_eq!(
        stdout.matches("COPY OF ANOTHER TREE").count(),
        1,
        "the brief itself must still carry it once:\n{stdout}"
    );
    assert_eq!(
        stderr.matches("COPY OF ANOTHER TREE").count(),
        0,
        "the stderr echo repeated what the brief already showed:\n{stderr}"
    );
}

/// Forty turns are not forty stops. A stop that repeats identically is not a
/// stop: it is a log.
#[test]
fn one_stop_per_turn_does_not_leave_one_stop_per_turn() {
    let c = Sandbox::new_seeded("turns");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    for _ in 0..5 {
        c.ok(&["session", "end", "--hook"]);
    }
    let v = c.ok(&["vivacs"]);
    assert_eq!(how_many(&v, "auto"), 1, "one stop per turn:\n{v}");
}

/// But as soon as the tree changes, the next stop does count.
#[test]
fn a_new_stop_once_something_changed() {
    let c = Sandbox::new_seeded("change");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    c.ok(&["session", "end", "--hook"]);
    c.ok(&["note", "1", "something happened"]);
    c.ok(&["session", "end", "--hook"]);
    let v = c.ok(&["vivacs"]);
    assert_eq!(how_many(&v, "auto"), 2, "it swallowed the good stop:\n{v}");
}

/// With no stack there is no pitch to close.
#[test]
fn no_stack_no_stop() {
    let c = Sandbox::new_seeded("nostack");
    c.ok(&["session", "end", "--hook"]);
    let v = c.ok(&["vivacs"]);
    assert_eq!(how_many(&v, "auto"), 0, "it invented an empty stop:\n{v}");
}

/// `f403`, `f404`: the brief goes straight to stdout in plain text, the shape
/// Claude Code's own hook reference says becomes context on `SessionStart`.
/// No JSON envelope, and the opening still lands in the log.
#[test]
fn the_start_hook_prints_the_brief_as_plain_text() {
    let c = Sandbox::new_seeded("plaintext");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    let (s, code) = c.run_stdin(
        &["session", "start", "--hook"],
        r#"{"session_id":"abc-123","source":"startup"}"#,
    );
    assert_eq!(code, 0, "{s}");
    assert!(!s.contains("hookSpecificOutput"), "{s}");
    assert!(s.starts_with("vivac · project:"), "{s}");
    assert!(s.contains("A goal"), "the brief went out empty:\n{s}");
    let log = c.log();
    assert!(log.contains("session.started"), "no opening:\n{log}");
    assert!(
        log.contains(r#""source":"startup""#),
        "the source did not survive:\n{log}"
    );
}

/// **A hook that fails in every directory without a tree gets switched off
/// within two days.** Both stay quiet and exit 0 where there is no `.vivac/`,
/// which is what makes it safe to leave them in the global configuration.
#[test]
fn they_stay_quiet_where_there_is_no_tree() {
    let c = Sandbox::new_empty("notree");
    for args in [["session", "start", "--hook"], ["session", "end", "--hook"]] {
        let (s, code) = c.run(&args);
        assert_eq!(code, 0, "{args:?} failed outside a tree:\n{s}");
        assert_eq!(s.trim(), "", "{args:?} said too much:\n{s}");
    }
}

/// `f708`: Codex's own hook manual is taxative about `Stop` -- plain text on
/// its stdout is **invalid**, and exiting 0 with nothing printed is what
/// counts as success there. The end hook already behaves this way, in every
/// shape a turn can leave it: no tree at all, a tree with nothing new to
/// close, and a tree with a change that does leave an automatic stop behind.
/// Nothing here made that true on purpose -- it only holds because nothing
/// in `session::end`'s hook path ever calls `outln!` -- so this is the test
/// that would have caught the day a courtesy line got added to it, the
/// mirror of [`the_start_hook_prints_the_brief_as_plain_text`] for the
/// opposite promise: not what the hook prints, but that it prints nothing
/// at all.
#[test]
fn the_end_hook_prints_nothing_to_stdout() {
    let no_tree = Sandbox::new_empty("end-hook-silent-no-tree");
    let (stdout, _stderr, code) = run_stdin_split(&no_tree, &["session", "end", "--hook"], "");
    assert_eq!(code, 0, "{stdout}");
    assert_eq!(
        stdout, "",
        "the end hook printed something with no tree:\n{stdout}"
    );

    let unchanged = Sandbox::new_seeded("end-hook-silent-unchanged");
    let (stdout, _stderr, code) = run_stdin_split(&unchanged, &["session", "end", "--hook"], "");
    assert_eq!(code, 0, "{stdout}");
    assert_eq!(
        stdout, "",
        "the end hook printed something with nothing changed:\n{stdout}"
    );

    let changed = Sandbox::new_seeded("end-hook-silent-changed");
    changed.ok(&["push", "A goal", "--why", "it is needed"]);
    let (stdout, _stderr, code) = run_stdin_split(&changed, &["session", "end", "--hook"], "");
    assert_eq!(code, 0, "{stdout}");
    assert_eq!(
        stdout, "",
        "the end hook printed something even though it left a stop:\n{stdout}"
    );
}

/// An automatic stop nobody declared still has to say something. Two autos in
/// a row that read identically do not segment a session, they log it (`f59`).
/// The label is derived from the seams --what the segment contained-- and never
/// from a judgement of relevance, which `DX` already measured at zero uses.
#[test]
fn an_automatic_stop_says_what_its_segment_contained() {
    let c = Sandbox::new_seeded("segment");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    c.ok(&["session", "end", "--hook"]);
    c.ok(&["add", "First finding", "--why", "it turned up"]);
    c.ok(&["add", "Second finding", "--why", "it turned up too"]);
    c.ok(&["session", "end", "--hook"]);
    let v = c.ok(&["vivacs"]);
    assert!(
        v.contains("2 new"),
        "the automatic stop did not say what it closed:\n{v}"
    );
}

/// Closing is as much of a seam as opening. A segment that only settled things
/// would otherwise read as if nothing had happened in it.
#[test]
fn an_automatic_stop_counts_what_its_segment_closed() {
    let c = Sandbox::new_seeded("closed");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    c.ok(&["add", "First finding", "--why", "it turned up"]);
    c.ok(&["session", "end", "--hook"]);
    c.ok(&["done", "2", "it was settled"]);
    c.ok(&["session", "end", "--hook"]);
    let v = c.ok(&["vivacs"]);
    assert!(
        v.contains("1 closed"),
        "the automatic stop counted no closes:\n{v}"
    );
}

/// A segment made only of notes is still a segment. The real tree has turns
/// that wrote nothing but notes, and they have to be tellable apart.
#[test]
fn an_automatic_stop_counts_the_notes_of_its_segment() {
    let c = Sandbox::new_seeded("notes");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    c.ok(&["session", "end", "--hook"]);
    c.ok(&["note", "1", "something turned up"]);
    c.ok(&["note", "1", "and something else"]);
    c.ok(&["session", "end", "--hook"]);
    let v = c.ok(&["vivacs"]);
    assert!(
        v.contains("2 notes"),
        "the automatic stop counted no notes:\n{v}"
    );
}

/// One note is a note, not notes. The label is prose the maintainer reads in
/// `vivac vivacs`, and prose that counts wrong reads like a machine talking.
#[test]
fn a_single_note_is_not_pluralised() {
    let c = Sandbox::new_seeded("plural");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    c.ok(&["session", "end", "--hook"]);
    c.ok(&["note", "1", "something turned up"]);
    c.ok(&["session", "end", "--hook"]);
    let v = c.ok(&["vivacs"]);
    assert!(v.contains("1 note"), "it counted no notes:\n{v}");
    assert!(!v.contains("1 notes"), "it said `1 notes`:\n{v}");
}

/// Not every seam is a birth, a close or a note. A segment that only raised a
/// flag still moved the tree, and if the label came out empty the stop would be
/// back to being the blank line `f59` was about.
#[test]
fn a_segment_of_none_of_the_three_still_says_something() {
    let c = Sandbox::new_seeded("other");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    c.ok(&["session", "end", "--hook"]);
    c.ok(&["flag", "1", "review", "--why", "it needs a second look"]);
    c.ok(&["session", "end", "--hook"]);
    let v = c.ok(&["vivacs"]);
    assert!(
        v.contains("1 change"),
        "the stop came out blank after a flag:\n{v}"
    );
}

/// `d45` retired the Spanish layer, and retiring it means the old spelling is
/// an unknown subcommand rather than a silent synonym. A word that is accepted
/// instead of rejected teaches the caller a spelling that does not exist, and
/// `session` was the last place in the product still doing it (`f57`).
#[test]
fn the_spanish_spellings_of_the_session_hooks_are_rejected() {
    let c = Sandbox::new_seeded("spanish");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    for args in [["session", "inicio"], ["session", "fin"]] {
        let (s, code) = c.run(&args);
        assert_ne!(code, 0, "`vivac {}` was accepted:\n{s}", args.join(" "));
        assert!(
            s.contains("usage"),
            "`vivac {}` failed without saying how:\n{s}",
            args.join(" ")
        );
    }
}

/// `SESSION-EVENT.md` §0: opening a session left no trace, so question 1 of
/// the falsification criterion --was the brief read?-- could not be answered
/// from the log at all. Only the gap between two writes, which also happens
/// when somebody goes to lunch.
#[test]
fn opening_a_session_leaves_a_trace() {
    let c = Sandbox::new_seeded("opening");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    let (out, code) = c.run_stdin(
        &["session", "start", "--hook"],
        r#"{"session_id":"abc-123","source":"startup"}"#,
    );
    assert_eq!(code, 0, "the start hook failed:\n{out}");
    assert!(
        c.log().contains("session.started"),
        "the opening left no trace:\n{}",
        c.log()
    );
}

/// The four openings are not the same experiment --the brief competes with
/// nothing on a cold start and with a whole restored transcript on a resume--
/// so which one it was has to survive into the log.
#[test]
fn the_source_of_the_opening_travels_into_the_log() {
    let c = Sandbox::new_seeded("source");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    c.run_stdin(
        &["session", "start", "--hook"],
        r#"{"session_id":"abc-123","source":"compact"}"#,
    );
    assert!(
        c.log().contains(r#""source":"compact""#),
        "the source did not survive:\n{}",
        c.log()
    );
}

/// The hook writes; the command a person runs does not. `vivac session start`
/// typed by hand stays a pure read, which is what confines the write to the
/// seam of the machine.
#[test]
fn the_command_a_person_runs_writes_nothing() {
    let c = Sandbox::new_seeded("pureread");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    let before = c.log();
    c.ok(&["session", "start"]);
    assert_eq!(before, c.log(), "the read path wrote to the log");
}

/// A hook that fails in every directory gets switched off within two days,
/// and the measurement goes with it. Garbage on stdin is not a reason to fail.
#[test]
fn a_broken_payload_still_opens_the_session() {
    let c = Sandbox::new_seeded("garbage");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    let (out, code) = c.run_stdin(&["session", "start", "--hook"], "not json at all");
    assert_eq!(code, 0, "garbage on stdin took the hook down:\n{out}");
    assert!(
        c.log().contains(r#""source":"unknown""#),
        "an unsaid source has to read as `unknown`, not as empty:\n{}",
        c.log()
    );
}

/// An opening is a fact about the session, not about the tree. If it counted
/// as a change, the next `Stop` would leave an automatic stop for a session
/// that did nothing -- which is the repeated stop the guard exists to avoid.
#[test]
fn an_opening_does_not_arm_the_automatic_stop() {
    let c = Sandbox::new_seeded("noarm");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    c.ok(&["session", "end", "--hook"]);
    let before = how_many(&c.ok(&["vivacs"]), "auto");
    c.run_stdin(
        &["session", "start", "--hook"],
        r#"{"session_id":"abc-123","source":"startup"}"#,
    );
    c.ok(&["session", "end", "--hook"]);
    assert_eq!(
        how_many(&c.ok(&["vivacs"]), "auto"),
        before,
        "the opening armed a stop for a session that did nothing"
    );
}

/// What the brief claimed, so that "was it followed?" stops being a judgement
/// and becomes a query. The identifiers are the ones the rest of the log
/// already uses, not the alias, which is recomputed on every fold.
///
/// These are inputs, never a verdict: what counts as *following* the brief
/// lives in whoever reads.
#[test]
fn the_opening_records_what_the_brief_claimed() {
    let c = Sandbox::new_seeded("claimed");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    c.run_stdin(
        &["session", "start", "--hook"],
        r#"{"session_id":"abc-123","source":"startup"}"#,
    );
    let log = c.log();
    let opening = log
        .lines()
        .find(|l| l.contains("session.started"))
        .unwrap_or_else(|| panic!("no opening in the log:\n{log}"));
    assert!(
        opening.contains(r#""session":"abc-123""#),
        "the session identifier did not survive:\n{opening}"
    );
    assert!(
        !opening.contains(r#""focus":null"#),
        "the opening recorded no focus, and there was one:\n{opening}"
    );
    // The push left a stop behind, so there is a last one to point at.
    assert!(
        !opening.contains(r#""vivac":null"#),
        "the opening recorded no last stop, and there was one:\n{opening}"
    );
}

/// The line inside the payload: an opaque identifier yes, a filesystem path
/// no. The transcript path arrives beside the session id and carries the
/// user's home directory, which the security pillar vetoes.
#[test]
fn the_transcript_path_never_reaches_the_log() {
    let c = Sandbox::new_seeded("noleak");
    c.ok(&["push", "A goal", "--why", "it is needed"]);
    c.run_stdin(
        &["session", "start", "--hook"],
        r#"{"session_id":"abc-123","source":"startup","transcript_path":"/home/someone/.claude/projects/x/y.jsonl","cwd":"/home/someone/work"}"#,
    );
    let log = c.log();
    assert!(
        !log.contains("someone"),
        "a path out of the payload reached the log:\n{log}"
    );
    assert!(
        !log.contains("transcript"),
        "the transcript path reached the log:\n{log}"
    );
}

/// With nothing on the stack the brief names no focus, and the opening has to
/// say so rather than invent one.
#[test]
fn an_opening_with_no_focus_records_none() {
    let c = Sandbox::new_seeded("nofocus");
    c.run_stdin(
        &["session", "start", "--hook"],
        r#"{"session_id":"abc-123","source":"startup"}"#,
    );
    let log = c.log();
    assert!(log.contains("session.started"), "no opening:\n{log}");
    assert!(
        log.contains(r#""focus":null"#),
        "it invented a focus out of an empty stack:\n{log}"
    );
}

/// A payload the guard refuses still opens the session: refusing the write
/// outright would drop the seam in silence, and a hook has nobody standing
/// by to reword the sentence that tripped it.
#[test]
fn a_refused_payload_still_opens_the_session() {
    let c = Sandbox::new_seeded("refused-open");
    let (out, code) = c.run_stdin(
        &["session", "start", "--hook"],
        r#"{"session_id":"abc-123","source":"/home/someone/.claude/projects/x"}"#,
    );
    assert_eq!(code, 0, "a refused field took the hook down:\n{out}");
    let log = c.log();
    assert!(log.contains("session.started"), "no opening:\n{log}");
    assert!(
        !log.contains("someone"),
        "the refused text reached the log:\n{log}"
    );
    assert!(
        log.contains(r#""source":"refused: "#),
        "the refused field was not replaced:\n{log}"
    );
}

/// What replaces a refused field names the rule and nothing past it, so the
/// log shows a refusal happened without repeating what caused it.
#[test]
fn the_refusal_names_the_rule_not_the_text() {
    let c = Sandbox::new_seeded("refused-rule");
    c.run_stdin(
        &["session", "start", "--hook"],
        r#"{"session_id":"abc-123","source":"/home/someone/.claude/projects/x"}"#,
    );
    let log = c.log();
    assert!(
        log.contains(r#""source":"refused: path to a user home directory (personal data)""#),
        "the rule did not land in the field as written:\n{log}"
    );
}

/// A refused session identifier is no different from a refused source: it
/// does not take the rest of the opening, or its sibling field, down with it.
#[test]
fn a_refused_session_identifier_does_not_take_the_opening_down() {
    let c = Sandbox::new_seeded("refused-session");
    c.run_stdin(
        &["session", "start", "--hook"],
        r#"{"session_id":"someone@example.com","source":"startup"}"#,
    );
    let log = c.log();
    assert!(log.contains("session.started"), "no opening:\n{log}");
    assert!(
        log.contains(r#""source":"startup""#),
        "the clean field was touched by its refused sibling:\n{log}"
    );
    assert!(
        !log.contains("someone@example.com"),
        "the refused session identifier reached the log:\n{log}"
    );
    assert!(
        log.contains(r#""session":"refused: email address (personal data)""#),
        "the session field was not replaced:\n{log}"
    );
}

/// The regression that matters most: a real, UUID-shaped session identifier
/// from Claude Code passes through untouched. This rests on `known_shape`
/// exempting UUIDs from the entropy check.
#[test]
fn a_real_session_identifier_passes_through_untouched() {
    let c = Sandbox::new_seeded("real-session");
    c.run_stdin(
        &["session", "start", "--hook"],
        r#"{"session_id":"019316f8-4c2a-7b31-9f0e-8d1a2b3c4d5e","source":"startup"}"#,
    );
    let log = c.log();
    assert!(
        log.contains("019316f8-4c2a-7b31-9f0e-8d1a2b3c4d5e"),
        "a real session identifier did not survive:\n{log}"
    );
    assert!(
        !log.contains("refused:"),
        "the guard fired on a real payload:\n{log}"
    );
}

// ---------------------------------------------------------------------------
// `d738`: the hook brief names the capture seams. `f737` measured why -- an
// agent with no project doctrine of its own only writes to the tree when a
// person asks, because nothing it receives unasked says when to.
// ---------------------------------------------------------------------------

/// The approved text, byte for byte (`d757`). Widths measured by machine,
/// every line included, none over the 76-column ceiling that
/// `capture_seams_lines_never_widen_past_76_columns`, below, checks against
/// the real rendering rather than against this constant.
const CAPTURE_SEAMS_BLOCK: &str = "\n WRITE AT THESE SEAMS\n  Look first: vivac find \"<words>\". Work the tree already holds goes under\n  its node, never into a second one. The focus above is where work was\n  left, maybe not by you: hang new work from what it continues.\n  new line of work     vivac push \"<title>\" --why \"<why>\" --parent <id>\n                       or --root, when it continues nothing in the tree\n  a choice is settled  vivac decide \"<t>\" --reason \"<r>\" --alternative \"<x>\"\n  you report findings  vivac add \"<t>\" --type finding --why \"<where>\"\n                       one for each thing found that you tell the person\n  told \"not now\"       vivac park <id> \"<their words>\"\n                       nothing to park yet? vivac add it, then park it\n  the work is done     vivac pop \"<outcome>\"\n                       and again if that settles the node it returns to\n  Or the same moves through the vivac_* tools.\n";

/// Test (a): the hook's own brief carries the block, exactly.
#[test]
fn the_hook_brief_names_the_capture_seams() {
    let c = Sandbox::new_seeded("capture-seams-hook");
    let (s, code) = c.run_stdin(&["session", "start", "--hook"], "{}");
    assert_eq!(code, 0, "{s}");
    assert!(
        s.contains(CAPTURE_SEAMS_BLOCK),
        "the hook brief did not carry the capture-seams block byte for byte:\n{s}"
    );
    // It sits right ahead of the closing rule and the tokens/depth footer,
    // not buried earlier in the body.
    let block_at = s.find(CAPTURE_SEAMS_BLOCK).unwrap();
    let footer_at = s.find("tokens · depth").unwrap();
    assert!(
        block_at < footer_at,
        "the block did not land ahead of the footer:\n{s}"
    );
}

/// Test (b)'s other half lives in `tests/brief.rs`, as
/// `the_capture_seams_block_is_hook_only`; this is the same claim read off
/// the person-facing render this file already exercises: a plain `vivac
/// session start` (no `--hook`) is the CLI path a person runs, and it stays
/// out.
#[test]
fn a_person_running_session_start_without_hook_gets_no_capture_seams() {
    let c = Sandbox::new_seeded("capture-seams-no-hook");
    let out = c.ok(&["session", "start"]);
    assert!(
        !out.contains("WRITE AT THESE SEAMS"),
        "a person reading `vivac session start` was told when to write:\n{out}"
    );
}

/// Test (f): no line of the block, as actually rendered, is wider than 76
/// columns. Read off `s` itself rather than off [`CAPTURE_SEAMS_BLOCK`], so
/// a row that grows without anyone updating that constant still gets
/// caught here.
#[test]
fn capture_seams_lines_never_widen_past_76_columns() {
    let c = Sandbox::new_seeded("capture-seams-width");
    let (s, code) = c.run_stdin(&["session", "start", "--hook"], "{}");
    assert_eq!(code, 0, "{s}");
    let start = s
        .find(" WRITE AT THESE SEAMS")
        .expect("no capture-seams heading in the hook brief");
    let block = &s[start..];
    let end = block.find("\n\n").unwrap_or(block.len());
    for line in block[..end].lines() {
        assert!(
            line.chars().count() <= 76,
            "a capture-seams line is wider than 76 columns ({} chars): {line:?}",
            line.chars().count()
        );
    }
}

/// A command line, split the way a shell would: whitespace-separated,
/// except inside a pair of double quotes. Enough to run the exact commands
/// the capture-seams block shows, placeholders swapped for real values.
fn shell_split(line: &str) -> Vec<String> {
    let mut args = Vec::new();
    let mut current = String::new();
    let mut in_quotes = false;
    for c in line.chars() {
        match c {
            '"' => in_quotes = !in_quotes,
            c if c.is_whitespace() && !in_quotes => {
                if !current.is_empty() {
                    args.push(std::mem::take(&mut current));
                }
            }
            c => current.push(c),
        }
    }
    if !current.is_empty() {
        args.push(current);
    }
    args
}

/// Test (d): every command the block shows is a real one -- its verb
/// dispatches, and every flag on its row is a flag that verb accepts.
/// Checked two ways per row: a bogus flag on the verb comes back with
/// "does not take" (the verb was recognised) rather than "unknown command"
/// (it was not), and the row itself, with its `<placeholders>` swapped for
/// real values, exits 0.
///
/// `d757` adds the look-first line's own `vivac find` and `push`'s new
/// `--parent <id>`: `push` needs a real, open node to point `--parent` at,
/// the same as `park` and `pop` already need one to act on, so it joins
/// them below.
#[test]
fn every_capture_seam_command_dispatches_and_takes_its_flags() {
    let rows = [
        "vivac find \"<words>\"",
        "vivac push \"<title>\" --why \"<why>\" --parent <id>",
        "vivac decide \"<t>\" --reason \"<r>\" --alternative \"<x>\"",
        "vivac add \"<t>\" --type finding --why \"<where>\"",
        "vivac park <id> \"<their words>\"",
        "vivac pop \"<outcome>\"",
    ];
    for row in rows {
        let mut argv = shell_split(row);
        assert_eq!(argv.remove(0), "vivac");
        let verb = argv[0].clone();

        // The verb dispatches: a bogus flag is refused by name, not by
        // "unknown command" -- proof the command itself was recognised.
        let (out, code) = Sandbox::new_seeded(&format!("capture-seams-bogus-{verb}"))
            .run(&[verb.as_str(), "--bogus-flag-for-this-test"]);
        assert_ne!(code, 0, "a bogus flag was silently accepted:\n{out}");
        assert!(
            out.contains("does not take"),
            "{verb} did not dispatch as a known command:\n{out}"
        );
        assert!(
            !out.contains(&format!("unknown command: {verb}")),
            "{verb} is not a real command:\n{out}"
        );

        // Every flag on the row is accepted: the row itself, run for real
        // with placeholders swapped for plain values, exits 0.
        let c = Sandbox::new_seeded(&format!("capture-seams-real-{verb}"));
        if verb == "park" || verb == "pop" || verb == "push" {
            c.ok(&["push", "a title to act on", "--why", "seed"]);
        }
        let real_args: Vec<String> = argv
            .into_iter()
            .map(|a| {
                if a == "<id>" {
                    "1".to_string()
                } else if a.starts_with('<') && a.ends_with('>') {
                    "a value".to_string()
                } else {
                    a
                }
            })
            .collect();
        let arg_refs: Vec<&str> = real_args.iter().map(String::as_str).collect();
        c.ok(&arg_refs);
    }

    // `--root` is `push`'s own other way of saying where a node is born
    // (`d757`'s hint line names it): on an empty stack it needs nothing to
    // point at and still exits 0.
    let c = Sandbox::new_seeded("capture-seams-push-root");
    c.ok(&["push", "A goal", "--why", "a value", "--root"]);
}