magellan-cli 0.6.0

Deterministic presentation engine for AI-generated technical walkthroughs
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
use std::fs;
use std::os::unix::fs::PermissionsExt;

use assert_cmd::Command;
use predicates::prelude::*;

// ---------------------------------------------------------------------------
// Help text: mandatory render enforcement
// ---------------------------------------------------------------------------

#[test]
fn help_text_opens_with_mandatory_go_instruction() {
    Command::cargo_bin("magellan")
        .expect("binary should build")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "MANDATORY: Your job is to produce rendered artifacts, not prose explanations.",
        ))
        .stdout(predicate::str::contains(
            "After writing JSON, you MUST run:",
        ))
        .stdout(predicate::str::contains(
            "magellan go --input /tmp/magellan.json",
        ));
}

#[test]
fn help_text_forbids_skipping_and_prose_substitution() {
    Command::cargo_bin("magellan")
        .expect("binary should build")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Do not skip it."))
        .stdout(predicate::str::contains(
            "Do not summarize in chat instead.",
        ))
        .stdout(predicate::str::contains(
            "The rendered HTML and markdown files ARE the deliverable.",
        ));
}

#[test]
fn help_text_workflow_uses_single_go_step() {
    Command::cargo_bin("magellan")
        .expect("binary should build")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Validate and render. This step is mandatory, do not skip it.",
        ))
        .stdout(predicate::str::contains(
            "magellan go --input /tmp/magellan.json",
        ));
}

#[test]
fn help_text_commands_list_includes_go() {
    Command::cargo_bin("magellan")
        .expect("binary should build")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "go        Validate, render HTML, open it, and write markdown",
        ));
}

#[test]
fn help_text_render_targets_references_go() {
    Command::cargo_bin("magellan")
        .expect("binary should build")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Use `magellan go` to validate and produce both HTML and markdown in one step:",
        ));
}

#[test]
fn help_text_fast_paths_include_go() {
    Command::cargo_bin("magellan")
        .expect("binary should build")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Validate and render (the mandatory final step):",
        ))
        .stdout(predicate::str::contains(
            "magellan go --input /tmp/magellan.json",
        ));
}

#[test]
fn help_text_ends_with_go_reminder() {
    Command::cargo_bin("magellan")
        .expect("binary should build")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "REMINDER: You MUST run `magellan go` after creating the JSON.",
        ));
}

#[test]
fn guide_command_matches_help_text() {
    let help_output = Command::cargo_bin("magellan")
        .expect("binary should build")
        .arg("--help")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let guide_output = Command::cargo_bin("magellan")
        .expect("binary should build")
        .arg("guide")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    assert_eq!(
        String::from_utf8(help_output).expect("utf-8"),
        String::from_utf8(guide_output).expect("utf-8"),
        "--help and guide should print identical content"
    );
}

// ---------------------------------------------------------------------------
// `magellan go` command: validates + renders HTML + writes markdown
// ---------------------------------------------------------------------------

#[test]
fn go_command_produces_html_and_markdown() {
    let temp_dir = tempfile::tempdir().expect("temp dir should be created");
    let input_path = temp_dir.path().join("payload.json");
    let html_path = temp_dir.path().join("output.html");
    let md_path = temp_dir.path().join("output.md");
    let opened_path = temp_dir.path().join("opened.txt");
    let opener_path = temp_dir.path().join("fake-open.sh");

    fs::write(&input_path, sample_payload()).expect("payload should be written");
    fs::write(
        &opener_path,
        format!(
            "#!/bin/sh\nprintf '%s' \"$1\" > {}\n",
            opened_path.display()
        ),
    )
    .expect("opener should be written");
    let mut perms = fs::metadata(&opener_path).unwrap().permissions();
    perms.set_mode(0o755);
    fs::set_permissions(&opener_path, perms).unwrap();

    Command::cargo_bin("magellan")
        .expect("binary should build")
        .env("MAGELLAN_OPEN_BIN", &opener_path)
        .args(["go", "--input"])
        .arg(&input_path)
        .arg("--out")
        .arg(&html_path)
        .arg("--markdown-out")
        .arg(&md_path)
        .assert()
        .success()
        .stdout(predicate::str::contains(format!(
            "Opened {}",
            html_path.display()
        )))
        .stdout(predicate::str::contains(format!(
            "Wrote {}",
            md_path.display()
        )));

    let html = fs::read_to_string(&html_path).expect("html should be readable");
    let md = fs::read_to_string(&md_path).expect("markdown should be readable");

    assert!(
        html.contains("<!DOCTYPE html>"),
        "html file should contain HTML"
    );
    assert!(
        html.contains("Order validation moved earlier"),
        "html should contain the title"
    );
    assert!(
        md.contains("# Order validation moved earlier"),
        "markdown should contain the title"
    );
    assert!(
        md.contains("sequenceDiagram"),
        "markdown should contain mermaid diagrams"
    );
}

#[test]
fn go_command_derives_markdown_path_from_input() {
    let temp_dir = tempfile::tempdir().expect("temp dir should be created");
    let input_path = temp_dir.path().join("my-walkthrough.json");
    let expected_md = temp_dir.path().join("my-walkthrough.md");
    let html_path = temp_dir.path().join("output.html");
    let opener_path = temp_dir.path().join("fake-open.sh");

    fs::write(&input_path, sample_payload()).expect("payload should be written");
    fs::write(&opener_path, "#!/bin/sh\n").expect("opener should be written");
    let mut perms = fs::metadata(&opener_path).unwrap().permissions();
    perms.set_mode(0o755);
    fs::set_permissions(&opener_path, perms).unwrap();

    Command::cargo_bin("magellan")
        .expect("binary should build")
        .env("MAGELLAN_OPEN_BIN", &opener_path)
        .args(["go", "--input"])
        .arg(&input_path)
        .arg("--out")
        .arg(&html_path)
        .assert()
        .success()
        .stdout(predicate::str::contains(format!(
            "Wrote {}",
            expected_md.display()
        )));

    assert!(
        expected_md.exists(),
        "markdown should be derived from input path"
    );
}

#[test]
fn go_command_rejects_invalid_payload() {
    let temp_dir = tempfile::tempdir().expect("temp dir should be created");
    let input_path = temp_dir.path().join("bad.json");
    fs::write(&input_path, r#"{"title":"X","summary":[],"sections":[]}"#)
        .expect("payload should be written");

    Command::cargo_bin("magellan")
        .expect("binary should build")
        .args(["go", "--input"])
        .arg(&input_path)
        .assert()
        .failure();
}

#[test]
fn go_help_explains_the_workflow() {
    Command::cargo_bin("magellan")
        .expect("binary should build")
        .args(["go", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Validate, render HTML, open it, and write markdown",
        ))
        .stdout(predicate::str::contains(
            "magellan go --input /tmp/magellan.json",
        ))
        .stdout(predicate::str::contains(
            "Agents should always use `go` instead of separate validate + render calls.",
        ));
}

// ---------------------------------------------------------------------------
// Validate breadcrumb: points to go
// ---------------------------------------------------------------------------

#[test]
fn validate_output_points_to_go_command() {
    let temp_dir = tempfile::tempdir().expect("temp dir should be created");
    let input_path = temp_dir.path().join("payload.json");
    fs::write(&input_path, sample_payload()).expect("payload should be written");

    Command::cargo_bin("magellan")
        .expect("binary should build")
        .args(["validate", "--input"])
        .arg(&input_path)
        .assert()
        .success()
        .stdout(predicate::str::contains("Payload is valid. Now render it:"))
        .stdout(predicate::str::contains("magellan go --input"));
}

// ---------------------------------------------------------------------------
// Prompt template: uses go command
// ---------------------------------------------------------------------------

#[test]
fn prompt_template_uses_go_command() {
    Command::cargo_bin("magellan")
        .expect("binary should build")
        .args(["prompt", "--agent-type", "codex"])
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "magellan go --input /tmp/magellan.json",
        ))
        .stdout(predicate::str::contains(
            "This validates, renders HTML (opens it in the browser), and writes markdown.",
        ));
}

#[test]
fn prompt_template_forbids_prose_summary() {
    Command::cargo_bin("magellan")
        .expect("binary should build")
        .args(["prompt", "--agent-type", "claude"])
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Do not describe the walkthrough in prose and then ask if the user wants a report.",
        ))
        .stdout(predicate::str::contains(
            "The rendered artifacts are always the expected output.",
        ));
}

#[test]
fn prompt_template_ends_with_required_go_step() {
    Command::cargo_bin("magellan")
        .expect("binary should build")
        .args(["prompt", "--agent-type", "codex"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Required final step:"))
        .stdout(predicate::str::contains(
            "magellan go --input /tmp/magellan.json",
        ));
}

#[test]
fn prompt_with_custom_artifact_uses_go_with_that_path() {
    Command::cargo_bin("magellan")
        .expect("binary should build")
        .args([
            "prompt",
            "--agent-type",
            "claude",
            "--artifact",
            "/tmp/my-session.json",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "magellan go --input /tmp/my-session.json",
        ));
}

#[test]
fn go_command_appears_twice_in_prompt() {
    let output = Command::cargo_bin("magellan")
        .expect("binary should build")
        .args(["prompt", "--agent-type", "codex"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let prompt = String::from_utf8(output).expect("prompt should be utf-8");
    let count = prompt
        .matches("magellan go --input /tmp/magellan.json")
        .count();

    assert_eq!(
        count, 2,
        "go command should appear in both step 5 and the required-final-step footer"
    );
}

// ---------------------------------------------------------------------------
// End-to-end: every goal uses go
// ---------------------------------------------------------------------------

#[test]
fn followup_prompt_uses_go() {
    Command::cargo_bin("magellan")
        .expect("binary should build")
        .args([
            "prompt",
            "--agent-type",
            "codex",
            "--source",
            "diff",
            "--goal",
            "followup",
            "--question",
            "what did the last commit implement?",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("magellan go --input"));
}

#[test]
fn handoff_prompt_uses_go() {
    Command::cargo_bin("magellan")
        .expect("binary should build")
        .args([
            "prompt",
            "--agent-type",
            "claude",
            "--source",
            "branch",
            "--goal",
            "handoff",
            "--scope",
            "backend",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("magellan go --input"))
        .stdout(predicate::str::contains("Required final step"));
}

#[test]
fn walkthrough_prompt_uses_go() {
    Command::cargo_bin("magellan")
        .expect("binary should build")
        .args([
            "prompt",
            "--agent-type",
            "codex",
            "--source",
            "session",
            "--goal",
            "walkthrough",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("magellan go --input"));
}

// ---------------------------------------------------------------------------
// Render --markdown-out still works independently
// ---------------------------------------------------------------------------

#[test]
fn render_markdown_out_writes_alongside_html() {
    let temp_dir = tempfile::tempdir().expect("temp dir should be created");
    let input_path = temp_dir.path().join("payload.json");
    let html_path = temp_dir.path().join("output.html");
    let md_path = temp_dir.path().join("output.md");

    fs::write(&input_path, sample_payload()).expect("payload should be written");

    Command::cargo_bin("magellan")
        .expect("binary should build")
        .args(["render", "--input"])
        .arg(&input_path)
        .args(["--format", "html", "--out"])
        .arg(&html_path)
        .arg("--markdown-out")
        .arg(&md_path)
        .assert()
        .success()
        .stdout(predicate::str::contains(format!(
            "Wrote {}",
            md_path.display()
        )));

    assert!(html_path.exists(), "html should be written");
    assert!(md_path.exists(), "markdown should be written");
}

// ---------------------------------------------------------------------------
// Docs stay in sync with the real renderer
// ---------------------------------------------------------------------------

#[test]
fn readme_does_not_advertise_book_view() {
    let readme = fs::read_to_string("README.md").expect("README.md should be readable");
    let lower = readme.to_lowercase();

    assert!(
        !lower.contains("book view"),
        "README.md must not advertise the removed book view"
    );
    assert!(
        readme.contains("sidebar"),
        "README.md should describe the current sidebar HTML layout"
    );
}

#[test]
fn readme_documents_the_mandatory_go_command() {
    let readme = fs::read_to_string("README.md").expect("README.md should be readable");

    assert!(
        readme.contains("magellan go"),
        "README.md must document the `magellan go` command"
    );
    assert!(
        readme.contains("go        Validate, render HTML, open it, and write markdown"),
        "README.md command list should describe what `go` does"
    );
    assert!(
        readme.contains("magellan go --input /tmp/magellan.json"),
        "README.md should include a concrete `magellan go` invocation example"
    );
}

#[test]
fn agent_docs_mention_go_in_development_commands() {
    for path in ["CLAUDE.md", "AGENTS.md"] {
        let contents =
            fs::read_to_string(path).unwrap_or_else(|_| panic!("{path} should be readable"));

        assert!(
            contents.contains("cargo run -- go --input"),
            "{path} Development Commands should teach the `go` feedback loop"
        );
    }
}

#[test]
fn agent_docs_do_not_contradict_renderer_with_book_view() {
    for path in ["CLAUDE.md", "AGENTS.md"] {
        let contents =
            fs::read_to_string(path).unwrap_or_else(|_| panic!("{path} should be readable"));
        let lower = contents.to_lowercase();

        assert!(
            !lower.contains("book view") && !lower.contains("book-mode"),
            "{path} must not reference the removed book view / book mode"
        );
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn sample_payload() -> &'static str {
    r#"{
  "title": "Order validation moved earlier",
  "summary": [
    "The UI now validates required fields before the network request."
  ],
  "sections": [
    {
      "title": "Request flow",
      "text": [
        "The form blocks invalid submissions locally.",
        "Valid submissions still reach the API."
      ],
      "diagram": {
        "type": "sequence",
        "nodes": ["User", "Form", "API"],
        "edges": [
          { "from": "User", "to": "Form", "label": "submit" },
          { "from": "Form", "to": "API", "label": "valid request" }
        ]
      }
    },
    {
      "title": "Why it matters",
      "text": [
        "Feedback arrives before a round-trip, so broken submissions never reach the backend."
      ]
    }
  ],
  "verification": {
    "text": [
      "Automated tests covered the regression."
    ]
  }
}"#
}