shiplog 0.3.0

CLI for generating self-review packets from GitHub/manual activity with receipts and coverage.
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
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::TempDir;

fn shiplog_bin() -> Command {
    Command::new(env!("CARGO_BIN_EXE_shiplog"))
}

fn fixture_dir() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("../..")
        .join("examples/fixture")
}

/// Run `collect json` into `tmp` and return the run directory path.
fn collect_json_into(tmp: &Path) -> PathBuf {
    let fixtures = fixture_dir();
    let out = shiplog_bin()
        .args([
            "collect",
            "--out",
            tmp.to_str().unwrap(),
            "json",
            "--events",
            fixtures.join("ledger.events.jsonl").to_str().unwrap(),
            "--coverage",
            fixtures.join("coverage.manifest.json").to_str().unwrap(),
        ])
        .output()
        .expect("failed to run shiplog collect json");
    assert!(
        out.status.success(),
        "collect json setup failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    tmp.join("run_fixture")
}

// ── 1. help text for each subcommand ───────────────────────────────────────

#[test]
fn render_help_shows_usage() {
    let out = shiplog_bin()
        .args(["render", "--help"])
        .output()
        .expect("failed to run shiplog render --help");

    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("--out"), "render help should mention --out");
    assert!(stdout.contains("--run"), "render help should mention --run");
    assert!(
        stdout.contains("--latest"),
        "render help should mention --latest"
    );
    assert!(
        stdout.contains("--mode"),
        "render help should mention --mode"
    );
    assert!(
        stdout.contains("--receipt-limit"),
        "render help should mention --receipt-limit"
    );
    assert!(
        stdout.contains("--appendix"),
        "render help should mention --appendix"
    );
}

#[test]
fn render_invalid_appendix_mode_fails() {
    let out = shiplog_bin()
        .args(["render", "--appendix", "bogus"])
        .output()
        .expect("failed to run shiplog render --appendix bogus");

    assert!(!out.status.success());
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("invalid value"),
        "invalid appendix mode should fail via clap value parsing, got: {stderr}"
    );
}

#[test]
fn refresh_help_shows_usage() {
    let out = shiplog_bin()
        .args(["refresh", "--help"])
        .output()
        .expect("failed to run shiplog refresh --help");

    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("github"),
        "refresh help should list github source"
    );
    assert!(
        stdout.contains("gitlab"),
        "refresh help should list gitlab source"
    );
    assert!(
        stdout.contains("jira"),
        "refresh help should list jira source"
    );
    assert!(
        stdout.contains("linear"),
        "refresh help should list linear source"
    );
    assert!(
        stdout.contains("--out"),
        "refresh help should mention --out"
    );
}

#[test]
fn import_help_shows_usage() {
    let out = shiplog_bin()
        .args(["import", "--help"])
        .output()
        .expect("failed to run shiplog import --help");

    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("--dir"), "import help should mention --dir");
    assert!(stdout.contains("--out"), "import help should mention --out");
}

#[test]
fn run_help_shows_usage() {
    let out = shiplog_bin()
        .args(["run", "--help"])
        .output()
        .expect("failed to run shiplog run --help");

    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("github"),
        "run help should list github source"
    );
    assert!(
        stdout.contains("gitlab"),
        "run help should list gitlab source"
    );
    assert!(stdout.contains("jira"), "run help should list jira source");
    assert!(
        stdout.contains("linear"),
        "run help should list linear source"
    );
    assert!(stdout.contains("--out"), "run help should mention --out");
}

#[test]
fn collect_json_help_shows_flags() {
    let out = shiplog_bin()
        .args(["collect", "json", "--help"])
        .output()
        .expect("failed to run shiplog collect json --help");

    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("--events"),
        "collect json help should mention --events"
    );
    assert!(
        stdout.contains("--coverage"),
        "collect json help should mention --coverage"
    );
}

// ── 2. version flag ────────────────────────────────────────────────────────

#[test]
fn version_flag_prints_version() {
    let out = shiplog_bin()
        .arg("--version")
        .output()
        .expect("failed to run shiplog --version");

    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("shiplog"),
        "version output should contain binary name, got: {stdout}"
    );
    // Version string should match semver-like pattern
    assert!(
        stdout.contains('.'),
        "version output should contain a dotted version number, got: {stdout}"
    );
}

// ── 3. error messages for invalid arguments ────────────────────────────────

#[test]
fn collect_github_invalid_since_date_fails() {
    let out = shiplog_bin()
        .args([
            "collect",
            "github",
            "--user",
            "octocat",
            "--since",
            "not-a-date",
            "--until",
            "2025-12-31",
        ])
        .output()
        .expect("failed to run shiplog");

    assert!(
        !out.status.success(),
        "invalid --since date should cause failure"
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("invalid value") || stderr.contains("error"),
        "stderr should report the invalid date, got: {stderr}"
    );
}

#[test]
fn collect_github_invalid_until_date_fails() {
    let out = shiplog_bin()
        .args([
            "collect",
            "github",
            "--user",
            "octocat",
            "--since",
            "2025-01-01",
            "--until",
            "01/31/2025",
        ])
        .output()
        .expect("failed to run shiplog");

    assert!(
        !out.status.success(),
        "invalid --until date format should cause failure"
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("invalid value") || stderr.contains("error"),
        "stderr should report the invalid date, got: {stderr}"
    );
}

#[test]
fn collect_github_missing_user_fails() {
    let out = shiplog_bin()
        .args([
            "collect",
            "github",
            "--since",
            "2025-01-01",
            "--until",
            "2025-12-31",
        ])
        .output()
        .expect("failed to run shiplog");

    assert!(!out.status.success(), "missing --user should cause failure");
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("provide --user") || stderr.contains("--me"),
        "stderr should mention the missing identity arg, got: {stderr}"
    );
}

#[test]
fn collect_json_missing_coverage_arg_fails() {
    let out = shiplog_bin()
        .args(["collect", "json", "--events", "some_file.jsonl"])
        .output()
        .expect("failed to run shiplog");

    assert!(
        !out.status.success(),
        "missing --coverage should cause failure"
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("--coverage") || stderr.contains("required"),
        "stderr should mention the missing --coverage arg, got: {stderr}"
    );
}

#[test]
fn render_with_unknown_flag_fails() {
    let out = shiplog_bin()
        .args(["render", "--bogus-flag"])
        .output()
        .expect("failed to run shiplog render");

    assert!(!out.status.success(), "unknown flag should cause failure");
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("unexpected argument") || stderr.contains("error"),
        "stderr should report the unknown flag, got: {stderr}"
    );
}

// ── 4. --no-details flag behaviour ─────────────────────────────────────────

#[test]
fn collect_github_help_mentions_no_details() {
    // Verify the --no-details flag is documented in github subcommand help
    let out = shiplog_bin()
        .args(["collect", "github", "--help"])
        .output()
        .expect("failed to run shiplog collect github --help");

    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("--no-details"),
        "collect github help should list --no-details"
    );
}

#[test]
fn run_github_help_mentions_no_details() {
    // --no-details is also available on the `run` subcommand's github source
    let out = shiplog_bin()
        .args(["run", "github", "--help"])
        .output()
        .expect("failed to run shiplog run github --help");

    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("--no-details"),
        "run github help should list --no-details"
    );
}

// ── 5. --regen flag with collect json ──────────────────────────────────────

#[test]
fn regen_regenerates_suggested_workstreams() {
    let tmp = TempDir::new().unwrap();
    let fixtures = fixture_dir();

    // First collect: creates workstreams.suggested.yaml
    let run_dir = collect_json_into(tmp.path());
    let suggested = run_dir.join("workstreams.suggested.yaml");
    assert!(
        suggested.exists(),
        "first collect should create suggested yaml"
    );

    // Modify the suggested file so we can detect regeneration
    std::fs::write(&suggested, "# marker: original\n").unwrap();

    // Second collect with --regen: should regenerate workstreams.suggested.yaml
    let out = shiplog_bin()
        .args([
            "collect",
            "--out",
            tmp.path().to_str().unwrap(),
            "--regen",
            "json",
            "--events",
            fixtures.join("ledger.events.jsonl").to_str().unwrap(),
            "--coverage",
            fixtures.join("coverage.manifest.json").to_str().unwrap(),
        ])
        .output()
        .expect("failed to run shiplog collect json --regen");

    assert!(
        out.status.success(),
        "collect json --regen failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // The suggested file should have been regenerated (our marker is gone)
    let content = std::fs::read_to_string(&suggested).unwrap();
    assert!(
        !content.contains("# marker: original"),
        "workstreams.suggested.yaml should have been regenerated by --regen"
    );
}

#[test]
fn regen_does_not_overwrite_curated_workstreams() {
    let tmp = TempDir::new().unwrap();
    let fixtures = fixture_dir();

    // First collect
    let run_dir = collect_json_into(tmp.path());

    // Simulate user curation: copy suggested → curated workstreams.yaml
    let suggested = run_dir.join("workstreams.suggested.yaml");
    let curated = run_dir.join("workstreams.yaml");
    std::fs::copy(&suggested, &curated).unwrap();
    // Add a user marker
    let mut curated_content = std::fs::read_to_string(&curated).unwrap();
    curated_content.push_str("\n# user curation marker\n");
    std::fs::write(&curated, &curated_content).unwrap();

    // Re-collect with --regen
    let out = shiplog_bin()
        .args([
            "collect",
            "--out",
            tmp.path().to_str().unwrap(),
            "--regen",
            "json",
            "--events",
            fixtures.join("ledger.events.jsonl").to_str().unwrap(),
            "--coverage",
            fixtures.join("coverage.manifest.json").to_str().unwrap(),
        ])
        .output()
        .expect("failed to run shiplog collect json --regen");

    assert!(
        out.status.success(),
        "collect json --regen (with curated) failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // workstreams.yaml should be untouched
    let after = std::fs::read_to_string(&curated).unwrap();
    assert!(
        after.contains("# user curation marker"),
        "workstreams.yaml should NOT be overwritten by --regen"
    );
}

// ── 6. import subcommand with a pre-built run directory ────────────────────

#[test]
fn import_from_collected_run_dir() {
    let collect_tmp = TempDir::new().unwrap();
    let import_tmp = TempDir::new().unwrap();

    // Build a run directory via collect json
    let run_dir = collect_json_into(collect_tmp.path());
    assert!(run_dir.join("ledger.events.jsonl").exists());
    assert!(run_dir.join("coverage.manifest.json").exists());

    // Import from that pre-built run directory
    let out = shiplog_bin()
        .args([
            "import",
            "--dir",
            run_dir.to_str().unwrap(),
            "--out",
            import_tmp.path().to_str().unwrap(),
        ])
        .output()
        .expect("failed to run shiplog import");

    assert!(
        out.status.success(),
        "import from collected run dir failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("Imported"),
        "expected 'Imported' in output, got: {stdout}"
    );

    // Verify outputs exist in the import target
    let import_run_dir = import_tmp.path().join("run_fixture");
    assert!(
        import_run_dir.join("packet.md").exists(),
        "imported run should contain packet.md"
    );
    assert!(
        import_run_dir.join("ledger.events.jsonl").exists(),
        "imported run should contain ledger.events.jsonl"
    );
    assert!(
        import_run_dir.join("coverage.manifest.json").exists(),
        "imported run should contain coverage.manifest.json"
    );
}

#[test]
fn import_with_custom_user_and_window() {
    let collect_tmp = TempDir::new().unwrap();
    let import_tmp = TempDir::new().unwrap();

    let run_dir = collect_json_into(collect_tmp.path());

    let out = shiplog_bin()
        .args([
            "import",
            "--dir",
            run_dir.to_str().unwrap(),
            "--out",
            import_tmp.path().to_str().unwrap(),
            "--user",
            "importbot",
            "--window-label",
            "H1-2025",
        ])
        .output()
        .expect("failed to run shiplog import");

    assert!(
        out.status.success(),
        "import with custom user failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let import_run_dir = import_tmp.path().join("run_fixture");
    let packet = std::fs::read_to_string(import_run_dir.join("packet.md")).unwrap();
    assert!(
        packet.contains("H1-2025"),
        "imported packet should contain custom window label"
    );
}

// ── 7. output directory creation ───────────────────────────────────────────

#[test]
fn collect_creates_nonexistent_out_dir() {
    let tmp = TempDir::new().unwrap();
    let fixtures = fixture_dir();

    // Use a deeply nested --out path that doesn't exist yet
    let nested_out = tmp.path().join("deep").join("nested").join("output");
    assert!(!nested_out.exists());

    let out = shiplog_bin()
        .args([
            "collect",
            "--out",
            nested_out.to_str().unwrap(),
            "json",
            "--events",
            fixtures.join("ledger.events.jsonl").to_str().unwrap(),
            "--coverage",
            fixtures.join("coverage.manifest.json").to_str().unwrap(),
        ])
        .output()
        .expect("failed to run shiplog collect json");

    assert!(
        out.status.success(),
        "collect json with nested --out failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // The nested directory should now exist with outputs inside
    let run_dir = nested_out.join("run_fixture");
    assert!(
        run_dir.join("packet.md").exists(),
        "packet.md should exist in newly created nested output dir"
    );
}

#[test]
fn import_creates_nonexistent_out_dir() {
    let collect_tmp = TempDir::new().unwrap();
    let import_tmp = TempDir::new().unwrap();

    let run_dir = collect_json_into(collect_tmp.path());

    let nested_out = import_tmp.path().join("a").join("b").join("c");
    assert!(!nested_out.exists());

    let out = shiplog_bin()
        .args([
            "import",
            "--dir",
            run_dir.to_str().unwrap(),
            "--out",
            nested_out.to_str().unwrap(),
        ])
        .output()
        .expect("failed to run shiplog import");

    assert!(
        out.status.success(),
        "import with nested --out failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let import_run_dir = nested_out.join("run_fixture");
    assert!(
        import_run_dir.join("packet.md").exists(),
        "packet.md should exist in newly created nested output dir"
    );
}