ito-core 0.1.30

Core functionality and business logic for Ito
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
use ito_core::distribution::{
    AssetType, claude_manifests, codex_manifests, github_manifests, install_manifests,
    opencode_manifests,
};
use ito_core::installers::{InitOptions, InstallMode};
use ito_templates::project_templates::WorktreeTemplateContext;
use std::collections::BTreeSet;
use std::path::Path;

/// Default init mode + opts used by every legacy `install_manifests` test
/// caller in this file. These tests originally pre-dated the mode/opts
/// signature change in 023-09 and assume "fresh init, no force, no update".
fn legacy_init_args() -> (InstallMode, InitOptions) {
    (
        InstallMode::Init,
        InitOptions::new(BTreeSet::new(), false, false),
    )
}

#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

#[test]
fn opencode_manifests_includes_plugin_and_skills() {
    let config_dir = Path::new("/tmp/test/.opencode");
    let manifests = opencode_manifests(config_dir);

    // Should include the plugin adapter
    let mut plugin = None;
    for manifest in &manifests {
        if manifest.source == "opencode/ito-skills.js" {
            plugin = Some(manifest);
            break;
        }
    }
    let plugin = plugin.expect("should include opencode plugin adapter");
    assert_eq!(plugin.asset_type, AssetType::Adapter);
    assert!(plugin.dest.ends_with("plugins/ito-skills.js"));

    // Should include skills with ito- prefix
    let skills: Vec<_> = manifests
        .iter()
        .filter(|m| m.asset_type == AssetType::Skill)
        .collect();
    assert!(!skills.is_empty(), "should include skills");

    // All skills should have ito prefix in dest (either "ito-" or just "ito/")
    for skill in &skills {
        let dest_str = skill.dest.to_string_lossy();
        assert!(
            dest_str.contains("/ito"),
            "skill dest should have ito prefix: {}",
            dest_str
        );
    }

    let has_ito_loop = manifests.iter().any(|m| {
        m.asset_type == AssetType::Skill
            && m.source == "ito-loop/SKILL.md"
            && m.dest
                .to_string_lossy()
                .ends_with("/skills/ito-loop/SKILL.md")
    });
    assert!(has_ito_loop, "should include ito-loop skill");

    let has_ito_orchestrate = manifests.iter().any(|m| {
        m.asset_type == AssetType::Skill
            && m.source == "ito-orchestrate/SKILL.md"
            && m.dest
                .to_string_lossy()
                .ends_with("/skills/ito-orchestrate/SKILL.md")
    });
    assert!(has_ito_orchestrate, "should include ito-orchestrate skill");

    // Should include the renamed ito-loop command.
    let has_loop = manifests.iter().any(|m| {
        m.asset_type == AssetType::Command
            && m.source == "ito-loop.md"
            && m.dest.to_string_lossy().ends_with("/commands/ito-loop.md")
    });
    assert!(has_loop, "should include ito-loop command");

    let has_orchestrate = manifests.iter().any(|m| {
        m.asset_type == AssetType::Command
            && m.source == "ito-orchestrate.md"
            && m.dest
                .to_string_lossy()
                .ends_with("/commands/ito-orchestrate.md")
    });
    assert!(has_orchestrate, "should include ito-orchestrate command");
}

#[test]
fn claude_manifests_includes_hooks_and_skills() {
    let project_root = Path::new("/tmp/test");
    let manifests = claude_manifests(project_root);

    // Should include session-start.sh adapter
    let mut adapter = None;
    for manifest in &manifests {
        if manifest.source == "claude/session-start.sh" {
            adapter = Some(manifest);
            break;
        }
    }
    let adapter = adapter.expect("should include claude session-start.sh");
    assert_eq!(adapter.asset_type, AssetType::Adapter);
    assert!(adapter.dest.ends_with(".claude/session-start.sh"));

    // Should include pre-tool audit hook adapter
    let mut hook = None;
    for manifest in &manifests {
        if manifest.source == "claude/hooks/ito-audit.sh" {
            hook = Some(manifest);
            break;
        }
    }
    let hook = hook.expect("should include claude pre-tool audit hook");
    assert_eq!(hook.asset_type, AssetType::Adapter);
    assert!(hook.dest.ends_with(".claude/hooks/ito-audit.sh"));

    // Should include skills
    let skills: Vec<_> = manifests
        .iter()
        .filter(|m| m.asset_type == AssetType::Skill)
        .collect();
    assert!(!skills.is_empty(), "should include skills");

    // Skills should go under .claude/skills/
    for skill in &skills {
        let dest_str = skill.dest.to_string_lossy();
        assert!(
            dest_str.contains(".claude/skills/"),
            "skill should be under .claude/skills/: {}",
            dest_str
        );
    }
}

#[test]
fn codex_manifests_includes_bootstrap_and_skills() {
    let project_root = Path::new("/tmp/test");
    let manifests = codex_manifests(project_root);

    // Should include bootstrap adapter
    let mut adapter = None;
    for manifest in &manifests {
        if manifest.source == "codex/ito-skills-bootstrap.md" {
            adapter = Some(manifest);
            break;
        }
    }
    let adapter = adapter.expect("should include codex bootstrap");
    assert_eq!(adapter.asset_type, AssetType::Adapter);
    assert!(
        adapter
            .dest
            .ends_with(".codex/instructions/ito-skills-bootstrap.md")
    );

    // Should include skills
    let skills: Vec<_> = manifests
        .iter()
        .filter(|m| m.asset_type == AssetType::Skill)
        .collect();
    assert!(!skills.is_empty(), "should include skills");

    // Skills should go under .codex/skills/
    for skill in &skills {
        let dest_str = skill.dest.to_string_lossy();
        assert!(
            dest_str.contains(".codex/skills/"),
            "skill should be under .codex/skills/: {}",
            dest_str
        );
    }
}

#[test]
fn github_manifests_includes_skills_and_commands() {
    let project_root = Path::new("/tmp/test");
    let manifests = github_manifests(project_root);

    // Should include skills and commands (no special adapter files)
    let skills: Vec<_> = manifests
        .iter()
        .filter(|m| m.asset_type == AssetType::Skill)
        .collect();
    let commands: Vec<_> = manifests
        .iter()
        .filter(|m| m.asset_type == AssetType::Command)
        .collect();

    assert!(!skills.is_empty(), "should include skills");
    assert!(!commands.is_empty(), "should include commands");

    // Skills should go under .github/skills/
    for skill in &skills {
        let dest_str = skill.dest.to_string_lossy();
        assert!(
            dest_str.contains(".github/skills/"),
            "skill should be under .github/skills/: {}",
            dest_str
        );
    }

    // Commands should go under .github/prompts/ with .prompt.md suffix
    for cmd in &commands {
        let dest_str = cmd.dest.to_string_lossy();
        assert!(
            dest_str.contains(".github/prompts/"),
            "command should be under .github/prompts/: {}",
            dest_str
        );
        assert!(
            dest_str.ends_with(".prompt.md"),
            "github prompts should have .prompt.md suffix: {}",
            dest_str
        );
    }
}

#[test]
fn install_manifests_writes_files_to_disk() {
    let td = tempfile::tempdir().unwrap();
    let config_dir = td.path().join(".opencode");

    let manifests = opencode_manifests(&config_dir);
    let (mode, opts) = legacy_init_args();
    install_manifests(&manifests, None, mode, &opts).unwrap();

    // Check plugin was installed
    assert!(
        config_dir.join("plugins").join("ito-skills.js").exists(),
        "plugin should be installed"
    );

    // Check at least one skill was installed
    let skills_dir = config_dir.join("skills");
    assert!(skills_dir.exists(), "skills directory should exist");

    // Should have ito-brainstorming skill
    assert!(
        skills_dir
            .join("ito-brainstorming")
            .join("SKILL.md")
            .exists(),
        "brainstorming skill should be installed"
    );
}

#[cfg(unix)]
#[test]
fn install_manifests_make_tmux_skill_scripts_executable() {
    let td = tempfile::tempdir().unwrap();
    let config_dir = td.path().join(".opencode");

    let manifests = opencode_manifests(&config_dir);
    let (mode, opts) = legacy_init_args();
    install_manifests(&manifests, None, mode, &opts).unwrap();

    let wait_for_text = config_dir.join("skills/ito-tmux/scripts/wait-for-text.sh");
    let find_sessions = config_dir.join("skills/ito-tmux/scripts/find-sessions.sh");

    assert!(wait_for_text.exists());
    assert!(find_sessions.exists());

    let wait_mode = std::fs::metadata(&wait_for_text)
        .unwrap()
        .permissions()
        .mode();
    let find_mode = std::fs::metadata(&find_sessions)
        .unwrap()
        .permissions()
        .mode();

    assert_ne!(
        wait_mode & 0o111,
        0,
        "wait-for-text.sh should be executable"
    );
    assert_ne!(
        find_mode & 0o111,
        0,
        "find-sessions.sh should be executable"
    );
}

#[test]
fn install_manifests_creates_parent_directories() {
    let td = tempfile::tempdir().unwrap();
    let deep_path = td.path().join("a").join("b").join("c").join(".claude");

    let manifests = claude_manifests(deep_path.parent().unwrap());
    let (mode, opts) = legacy_init_args();
    install_manifests(&manifests, None, mode, &opts).unwrap();

    // Parent directories should be created
    assert!(deep_path.join("session-start.sh").exists());
    assert!(deep_path.join("hooks/ito-audit.sh").exists());
}

#[test]
fn install_manifests_renders_worktree_skill_with_context() {
    let td = tempfile::tempdir().unwrap();
    let project_root = td.path().join("project");

    // Use claude manifests (any harness works — they all install skills)
    let manifests = claude_manifests(&project_root);

    // Install with a disabled worktree context (the most common case)
    let ctx = WorktreeTemplateContext::default();
    let (mode, opts) = legacy_init_args();
    install_manifests(&manifests, Some(&ctx), mode, &opts).unwrap();

    // The using-git-worktrees skill should exist and be rendered (no Jinja2 syntax)
    let worktree_skill = project_root.join(".claude/skills/ito-using-git-worktrees/SKILL.md");
    assert!(
        worktree_skill.exists(),
        "worktree skill should be installed"
    );

    let content = std::fs::read_to_string(&worktree_skill).unwrap();
    assert!(
        !content.contains("{%"),
        "rendered skill should not contain Jinja2 block syntax"
    );
    assert!(
        content.contains("Worktrees are not configured for this project."),
        "disabled context should render explicit non-worktree guidance"
    );
}

#[test]
fn install_manifests_renders_worktree_skill_enabled() {
    let td = tempfile::tempdir().unwrap();
    let project_root = td.path().join("project");

    let manifests = claude_manifests(&project_root);

    let ctx = WorktreeTemplateContext {
        enabled: true,
        strategy: "checkout_subdir".to_string(),
        layout_dir_name: "ito-worktrees".to_string(),
        integration_mode: "commit_pr".to_string(),
        default_branch: "main".to_string(),
        project_root: "/home/user/project".to_string(),
    };
    let (mode, opts) = legacy_init_args();
    install_manifests(&manifests, Some(&ctx), mode, &opts).unwrap();

    let worktree_skill = project_root.join(".claude/skills/ito-using-git-worktrees/SKILL.md");
    let content = std::fs::read_to_string(&worktree_skill).unwrap();
    assert!(
        !content.contains("{%"),
        "rendered skill should not contain Jinja2 block syntax"
    );
    assert!(
        content.contains("**Configured strategy:** `checkout_subdir`"),
        "enabled context should render strategy-specific guidance"
    );
}

#[test]
fn install_manifests_keeps_non_worktree_placeholders_verbatim() {
    let td = tempfile::tempdir().unwrap();
    let project_root = td.path().join("project");

    let manifests = claude_manifests(&project_root);
    let ctx = WorktreeTemplateContext::default();
    let (mode, opts) = legacy_init_args();
    install_manifests(&manifests, Some(&ctx), mode, &opts).unwrap();

    let research_skill = project_root.join(".claude/skills/ito-research/research-stack.md");
    assert!(
        research_skill.exists(),
        "research skill file should be installed"
    );

    let content = std::fs::read_to_string(research_skill).unwrap();
    assert!(
        content.contains("{{topic}}"),
        "non-worktree template placeholders should remain verbatim"
    );
}

#[test]
fn all_manifests_use_embedded_assets() {
    // Verify that all manifest generators produce valid manifests
    // that can be installed from embedded assets
    let td = tempfile::tempdir().unwrap();

    // OpenCode
    let oc = td.path().join("opencode");
    let manifests = opencode_manifests(&oc);
    assert!(
        {
            let (mode, opts) = legacy_init_args();
            install_manifests(&manifests, None, mode, &opts).is_ok()
        },
        "opencode manifests should install successfully"
    );

    // Claude
    let claude = td.path().join("claude");
    let manifests = claude_manifests(&claude);
    assert!(
        {
            let (mode, opts) = legacy_init_args();
            install_manifests(&manifests, None, mode, &opts).is_ok()
        },
        "claude manifests should install successfully"
    );

    // Codex
    let codex = td.path().join("codex");
    let manifests = codex_manifests(&codex);
    assert!(
        {
            let (mode, opts) = legacy_init_args();
            install_manifests(&manifests, None, mode, &opts).is_ok()
        },
        "codex manifests should install successfully"
    );

    // GitHub
    let github = td.path().join("github");
    let manifests = github_manifests(&github);
    assert!(
        {
            let (mode, opts) = legacy_init_args();
            install_manifests(&manifests, None, mode, &opts).is_ok()
        },
        "github manifests should install successfully"
    );
}