seshat-cli 0.2.1

CLI commands and TUI for Seshat
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
//! Integration tests for `seshat init` agent instruction writing (Story 9.2).
//!
//! These tests exercise `run_init()` end-to-end against a temp directory,
//! verifying that instruction files, skill files, and hook registrations are
//! created correctly for each supported client.

use std::fs;
use std::path::Path;

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

/// Verify that `upsert_instructions` creates AGENTS.md with seshat markers
/// when called directly — simulating what run_init does for OpenCode.
#[test]
fn init_writes_instructions_to_agents_md() {
    use seshat_cli::instructions::{AGENTS_MD_CONTENT, UpsertResult, upsert_instructions};

    let tmp = tempfile::tempdir().unwrap();
    let agents_md = tmp.path().join("AGENTS.md");

    let result = upsert_instructions(&agents_md, AGENTS_MD_CONTENT, false).unwrap();

    assert_eq!(result, UpsertResult::Created);
    let content = fs::read_to_string(&agents_md).unwrap();
    assert!(
        content.contains("<!-- seshat:start -->"),
        "start marker present"
    );
    assert!(
        content.contains("<!-- seshat:end -->"),
        "end marker present"
    );
    assert!(
        content.contains("query_project_context"),
        "seshat tool reference present"
    );
    assert!(
        content.contains("query_code_pattern"),
        "seshat tool reference present"
    );
}

/// Verify that running upsert twice produces exactly one seshat section.
#[test]
fn init_instructions_are_idempotent() {
    use seshat_cli::instructions::{AGENTS_MD_CONTENT, upsert_instructions};

    let tmp = tempfile::tempdir().unwrap();
    let agents_md = tmp.path().join("AGENTS.md");

    upsert_instructions(&agents_md, AGENTS_MD_CONTENT, false).unwrap();
    upsert_instructions(&agents_md, AGENTS_MD_CONTENT, false).unwrap();

    let content = fs::read_to_string(&agents_md).unwrap();
    let count = content.matches("<!-- seshat:start -->").count();
    assert_eq!(count, 1, "exactly one seshat section after two upserts");
}

/// Verify that seshat section is appended to an existing AGENTS.md.
#[test]
fn init_appends_to_existing_agents_md() {
    use seshat_cli::instructions::{AGENTS_MD_CONTENT, UpsertResult, upsert_instructions};

    let tmp = tempfile::tempdir().unwrap();
    let agents_md = tmp.path().join("AGENTS.md");
    fs::write(&agents_md, "# My Project\n\nSome existing instructions.\n").unwrap();

    let result = upsert_instructions(&agents_md, AGENTS_MD_CONTENT, false).unwrap();
    assert_eq!(result, UpsertResult::Appended);

    let content = fs::read_to_string(&agents_md).unwrap();
    assert!(
        content.contains("# My Project"),
        "existing content preserved"
    );
    assert!(
        content.contains("<!-- seshat:start -->"),
        "seshat section appended"
    );
}

/// Verify skill installation creates the correct directory and SKILL.md.
#[test]
fn init_installs_skill_file() {
    use seshat_cli::instructions::{SKILL_MD_CONTENT, SkillResult, install_skill};

    let tmp = tempfile::tempdir().unwrap();
    let skill_dir = tmp.path().join("skills").join("seshat");

    let result = install_skill(&skill_dir, SKILL_MD_CONTENT, false).unwrap();
    assert_eq!(result, SkillResult::Installed);

    let skill_path = skill_dir.join("SKILL.md");
    assert!(skill_path.exists(), "SKILL.md created");

    let content = fs::read_to_string(&skill_path).unwrap();
    assert!(content.contains("name: seshat"), "skill name present");
    assert!(
        content.contains("query_code_pattern"),
        "workflow content present"
    );
}

/// Verify that --skip-instructions leaves no instruction files behind.
#[test]
fn upsert_dry_run_leaves_no_files() {
    use seshat_cli::instructions::{AGENTS_MD_CONTENT, UpsertResult, upsert_instructions};

    let tmp = tempfile::tempdir().unwrap();
    let agents_md = tmp.path().join("AGENTS.md");

    let result = upsert_instructions(&agents_md, AGENTS_MD_CONTENT, true).unwrap();
    assert!(matches!(result, UpsertResult::DryRun(Some(_))));
    assert!(!agents_md.exists(), "no file created in dry-run mode");
}

/// Verify Claude Code hook installation creates scripts and settings.json.
#[test]
fn init_installs_claude_code_hooks() {
    use seshat_cli::instructions::install_hooks_claude_code;

    let tmp = tempfile::tempdir().unwrap();
    let hooks_dir = tmp.path().join("hooks");
    let settings = tmp.path().join("settings.json");

    install_hooks_claude_code(&hooks_dir, &settings, false).unwrap();

    assert!(
        hooks_dir.join("seshat-session-start").exists(),
        "session-start hook created"
    );
    assert!(
        hooks_dir.join("seshat-pre-tool").exists(),
        "pre-tool hook created"
    );

    let settings_content = fs::read_to_string(&settings).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&settings_content).unwrap();
    assert!(
        parsed["hooks"]["PreToolUse"].is_array(),
        "PreToolUse registered"
    );
    assert!(
        parsed["hooks"]["SessionStart"].is_array(),
        "SessionStart registered"
    );
    assert!(
        settings_content.contains("seshat-pre-tool"),
        "pre-tool command in settings"
    );
    assert!(
        settings_content.contains("seshat-session-start"),
        "session-start command in settings"
    );
}

/// Simulate `write_instructions_for_client(ClaudeCode, ...)` end-to-end.
/// This mirrors the exact logic in `init.rs::write_instructions_for_client`.
#[test]
fn write_instructions_for_client_claude_code_full_path() {
    use seshat_cli::instructions::{
        AGENTS_MD_CONTENT, HooksResult, SKILL_MD_CONTENT, SkillResult, UpsertResult,
        install_hooks_claude_code, install_skill, upsert_instructions,
    };

    let tmp = tempfile::tempdir().unwrap();
    let claude_home = tmp.path().join(".claude");
    fs::create_dir_all(&claude_home).unwrap();

    // ── Step 1: upsert_instructions (CLAUDE.md) ──────────────────────
    let claude_md = claude_home.join("CLAUDE.md");
    let upsert_result = upsert_instructions(&claude_md, AGENTS_MD_CONTENT, false).unwrap();
    assert!(
        matches!(upsert_result, UpsertResult::Created),
        "CLAUDE.md should be created (not appended or updated)"
    );
    assert!(claude_md.exists());
    let claude_content = fs::read_to_string(&claude_md).unwrap();
    assert!(claude_content.contains("<!-- seshat:start -->"));
    assert!(claude_content.contains("<!-- seshat:end -->"));

    // ── Step 2: install_skill ─────────────────────────────────────────
    let skill_dir = claude_home.join("skills").join("seshat");
    let skill_result = install_skill(&skill_dir, SKILL_MD_CONTENT, false).unwrap();
    assert!(
        matches!(skill_result, SkillResult::Installed),
        "skill should be Installed"
    );
    assert!(skill_dir.join("SKILL.md").exists());

    // ── Step 3: install_hooks_claude_code ─────────────────────────────
    let hooks_dir = claude_home.join("hooks");
    let settings_path = claude_home.join("settings.json");
    let hooks_result = install_hooks_claude_code(&hooks_dir, &settings_path, false).unwrap();
    // When settings.json is NEW (doesn't exist), no backup is created.
    assert!(
        matches!(hooks_result, HooksResult::Installed(None)),
        "should have no backup since settings.json is new"
    );
    assert!(hooks_dir.join("seshat-session-start").exists());
    assert!(hooks_dir.join("seshat-pre-tool").exists());
    let parsed: serde_json::Value =
        serde_json::from_str(&fs::read_to_string(&settings_path).unwrap()).unwrap();
    assert!(parsed["hooks"]["PreToolUse"].is_array());
    assert!(parsed["hooks"]["SessionStart"].is_array());
}

/// Verify that the second run produces `Updated` (not `Created` or `Appended`
/// for instructions) and `Installed` for skills.
#[test]
fn write_instructions_are_idempotent_on_second_run() {
    use seshat_cli::instructions::{
        AGENTS_MD_CONTENT, HooksResult, SKILL_MD_CONTENT, UpsertResult, install_hooks_claude_code,
        install_skill, upsert_instructions,
    };

    let tmp = tempfile::tempdir().unwrap();
    let claude_home = tmp.path().join(".claude");
    fs::create_dir_all(&claude_home).unwrap();

    // First run — all Created.
    let run_once = |claude_home: &Path| {
        let claude_md = claude_home.join("CLAUDE.md");
        let skill_dir = claude_home.join("skills").join("seshat");

        let ins = upsert_instructions(&claude_md, AGENTS_MD_CONTENT, false).unwrap();
        let skill = install_skill(&skill_dir, SKILL_MD_CONTENT, false).unwrap();
        let hooks = install_hooks_claude_code(
            &claude_home.join("hooks"),
            &claude_home.join("settings.json"),
            false,
        )
        .unwrap();
        (ins, skill, hooks)
    };

    let (ins1, _skill1, _hooks1) = run_once(&claude_home);
    assert!(matches!(ins1, UpsertResult::Created));

    // Second run — should be Updated, Installed, Installed.
    let (ins2, _skill2, hooks2) = run_once(&claude_home);
    assert!(
        matches!(ins2, UpsertResult::Updated),
        "second upsert must produce Updated, got {:?}",
        ins2
    );

    // Verify exactly one section.
    let content = fs::read_to_string(claude_home.join("CLAUDE.md")).unwrap();
    assert_eq!(
        content.matches("<!-- seshat:start -->").count(),
        1,
        "only one section after two runs"
    );

    // Verify hooks are still idempotent (no duplicates).
    if let HooksResult::Installed(Some(backup)) = hooks2 {
        assert!(backup.to_string_lossy().contains("seshat-backup"));
    }
}

/// Verify that when settings.json already exists, a backup is created.
#[test]
fn hooks_backup_created_when_settings_exists() {
    use seshat_cli::instructions::{HooksResult, install_hooks_claude_code};

    let tmp = tempfile::tempdir().unwrap();
    let claude_home = tmp.path().join(".claude");
    fs::create_dir_all(&claude_home).unwrap();

    // Pre-populate settings.json with existing content.
    let settings_path = claude_home.join("settings.json");
    fs::write(
        &settings_path,
        r#"{"hooks":{"PreToolUse":[{"matcher":"test","hooks":[{"type":"command","command":"/other/hook"}]}]}}"#,
    )
    .unwrap();

    let hooks_dir = claude_home.join("hooks");
    let result = install_hooks_claude_code(&hooks_dir, &settings_path, false).unwrap();

    // Should have a backup since settings.json existed.
    if let HooksResult::Installed(Some(backup)) = result {
        assert!(backup.to_string_lossy().contains("seshat-backup"));
        assert!(backup.to_string_lossy().contains("settings.json"));
        // Backup should contain the original content.
        let backup_content = fs::read_to_string(&backup).unwrap();
        assert!(backup_content.contains(r#"{"hooks":{"PreToolUse""#));
    } else {
        panic!("expected Installed(Some(backup)), got {:?}", result);
    }

    // Original settings.json should now have seshat hooks merged in.
    let content = fs::read_to_string(&settings_path).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&content).unwrap();
    assert!(parsed["hooks"]["PreToolUse"].is_array());
    let pre_tool = parsed["hooks"]["PreToolUse"].as_array().unwrap();
    assert!(
        pre_tool.len() >= 2,
        "should have original + seshat entries, got {}",
        pre_tool.len()
    );
}

/// Verify that dry-run produces `DryRun(Some(path))` variants and shows specific paths.
#[test]
fn dry_run_shows_specific_paths() {
    use seshat_cli::instructions::{
        AGENTS_MD_CONTENT, HooksResult, SKILL_MD_CONTENT, SkillResult, UpsertResult,
        install_hooks_claude_code, install_skill, upsert_instructions,
    };

    let tmp = tempfile::tempdir().unwrap();
    let claude_home = tmp.path().join(".claude");
    fs::create_dir_all(&claude_home).unwrap();

    // Instruction dry-run.
    let claude_md = claude_home.join("CLAUDE.md");
    let ins_result = upsert_instructions(&claude_md, AGENTS_MD_CONTENT, true).unwrap();
    if let UpsertResult::DryRun(Some(path)) = &ins_result {
        assert_eq!(path, &claude_md, "dry-run must report the instruction path");
    } else {
        panic!("expected DryRun(Some(path)), got {:?}", ins_result);
    }

    // Skill dry-run.
    let skill_dir = claude_home.join("skills").join("seshat");
    let skill_result = install_skill(&skill_dir, SKILL_MD_CONTENT, true).unwrap();
    if let SkillResult::DryRun(Some(path)) = &skill_result {
        assert!(
            path.ends_with("SKILL.md"),
            "dry-run must report the skill path, got {:?}",
            path
        );
    } else {
        panic!(
            "expected SkillResult::DryRun(Some(path)), got {:?}",
            skill_result
        );
    }

    // Hooks dry-run.
    let hooks_dir = claude_home.join("hooks");
    let settings_path = claude_home.join("settings.json");
    let hooks_result = install_hooks_claude_code(&hooks_dir, &settings_path, true).unwrap();
    if let HooksResult::DryRun {
        hooks_dir: hd,
        session_start,
        pre_tool,
        settings,
    } = &hooks_result
    {
        assert!(
            hd.to_string_lossy().contains("hooks"),
            "dry-run must report hooks_dir"
        );
        assert!(
            session_start
                .to_string_lossy()
                .contains("seshat-session-start"),
            "dry-run must report session_start"
        );
        assert!(
            pre_tool.to_string_lossy().contains("seshat-pre-tool"),
            "dry-run must report pre_tool"
        );
        assert!(
            settings.to_string_lossy().ends_with("settings.json"),
            "dry-run must report settings"
        );

        // All paths should exist as non-existent (nothing written).
        assert!(!hd.exists(), "hooks_dir not created in dry-run");
        assert!(
            !session_start.exists(),
            "session_start not created in dry-run"
        );
        assert!(!pre_tool.exists(), "pre_tool not created in dry-run");
        assert!(!settings.exists(), "settings not created in dry-run");
    } else {
        panic!("expected HooksResult::DryRun{{..}}, got {:?}", hooks_result);
    }

    // Verify description messages mention paths.
    let ins_desc = ins_result.description();
    assert!(
        ins_desc.contains("CLAUDE.md"),
        "description must include file path, got: {ins_desc}",
    );
}

/// Verify that an existing AGENTS.md without markers gets seshat section appended.
#[test]
fn open_code_appends_to_existing_if_no_markers() {
    use seshat_cli::instructions::{AGENTS_MD_CONTENT, UpsertResult, upsert_instructions};

    let tmp = tempfile::tempdir().unwrap();
    // Simulate OpenCode global config dir.
    let opencode_dir = tmp.path().join(".config").join("opencode");
    fs::create_dir_all(&opencode_dir).unwrap();

    let agents_md = opencode_dir.join("AGENTS.md");
    fs::write(
        &agents_md,
        "# My Project\n\n## Instructions\n\nWrite your instructions here.\n",
    )
    .unwrap();

    let result = upsert_instructions(&agents_md, AGENTS_MD_CONTENT, false).unwrap();
    assert!(
        matches!(result, UpsertResult::Appended),
        "should append to existing file without markers"
    );

    let content = fs::read_to_string(&agents_md).unwrap();
    assert!(content.contains("# My Project"));
    assert!(content.contains("## Instructions"));
    assert!(content.contains("<!-- seshat:start -->"));
    assert!(
        content.contains("<!-- seshat:end -->"),
        "end marker present"
    );
    // Section should be at the end.
    assert!(
        content.rfind("<!-- seshat:start -->").unwrap() > content.find("## Instructions").unwrap(),
        "seshat section appended after existing content"
    );
}

/// Verify that Claude Code instruction writing preserves existing CLAUDE.md content.
#[test]
fn claude_code_preserves_existing_content() {
    use seshat_cli::instructions::{AGENTS_MD_CONTENT, UpsertResult, upsert_instructions};

    let tmp = tempfile::tempdir().unwrap();
    let claude_home = tmp.path().join(".claude");
    fs::create_dir_all(&claude_home).unwrap();

    let claude_md = claude_home.join("CLAUDE.md");

    // First run: creates.
    let result1 = upsert_instructions(&claude_md, AGENTS_MD_CONTENT, false).unwrap();
    assert!(matches!(result1, UpsertResult::Created));

    // Add some extra content after the seshat section.
    let existing = fs::read_to_string(&claude_md).unwrap();
    let extra = "\n\n## Other Tools\n\nSome other AI tools are configured here.\n";
    fs::write(&claude_md, format!("{existing}{extra}")).unwrap();

    // Second run: should replace between markers, preserve header and footer.
    let result2 = upsert_instructions(&claude_md, AGENTS_MD_CONTENT, false).unwrap();
    assert!(matches!(result2, UpsertResult::Updated));

    // Verify header preserved ("# Claude Code" is a common section in CLAUDE.md).
    let content = fs::read_to_string(&claude_md).unwrap();
    assert!(
        content.contains("## Other Tools"),
        "existing content after markers preserved"
    );
    assert!(
        content.contains("Some other AI tools are configured here"),
        "exact existing content preserved"
    );
}