spectra-cli 0.5.0

OpenSpectra command-line interface.
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
//! `spectra update` 整合測試 — 訊息、exit code、寫檔集合與檔案位元組
//! 全部對照 oracle 2.3.1 的 probe / golden
//! (docs/reverse-engineering/update.md、golden/update-trees-2.3.1.tsv)。

mod common;

use std::path::Path;
use std::process::Output;

use common::{spectra, TempDir};
use sha2::{Digest, Sha256};

/// 建一個最小已初始化專案(`.spectra.yaml` 是唯一的判定檔)。
fn init_root(root: &Path, spec_dir: &str) {
    std::fs::write(
        root.join(".spectra.yaml"),
        format!("spec_dir: {spec_dir}\n"),
    )
    .unwrap();
}

fn run_update(root: &Path, extra: &[&str]) -> Output {
    spectra()
        .arg("update")
        .arg(root)
        .args(extra)
        .arg("--no-color")
        .output()
        .unwrap()
}

fn stdout(out: &Output) -> String {
    String::from_utf8(out.stdout.clone()).unwrap()
}

// ---- 訊息與 exit code(逐字 pin 自 oracle)----

#[test]
fn fresh_project_without_tools_reports_and_exits_zero() {
    let root = TempDir::new("update-notools");
    init_root(&root, "openspec");

    let out = run_update(&root, &[]);
    assert!(out.status.success());
    assert_eq!(
        stdout(&out),
        "! No AI tool configurations found. Use 'spectra init --tools' to set up.\n"
    );
    assert!(out.stderr.is_empty());
    // 沒偵測到工具就什麼都不寫。
    assert_eq!(std::fs::read_dir(&*root).unwrap().count(), 1);
}

#[test]
fn uninitialized_path_errors_like_every_other_command() {
    let root = TempDir::new("update-uninit");

    let out = run_update(&root, &[]);
    assert_eq!(out.status.code(), Some(1));
    assert!(stdout(&out).is_empty());
    assert_eq!(
        String::from_utf8(out.stderr).unwrap(),
        "Error: Not initialized. Run 'spectra init' first.\n"
    );
}

#[test]
fn explicit_path_does_not_walk_up_but_cwd_does() {
    // oracle probe:無 [PATH] 從 cwd 往上找專案根;明確 [PATH] 必須正好是
    // 根,子目錄直接 Not initialized。
    let root = TempDir::new("update-walkup");
    init_root(&root, "openspec");
    std::fs::create_dir_all(root.join("sub/inner")).unwrap();
    std::fs::create_dir_all(root.join(".claude")).unwrap();

    let from_sub = spectra()
        .args(["update", "--no-color"])
        .current_dir(root.join("sub/inner"))
        .output()
        .unwrap();
    assert!(
        from_sub.status.success(),
        "cwd walk-up failed: {from_sub:?}"
    );
    assert_eq!(
        String::from_utf8(from_sub.stdout).unwrap(),
        "✓ Updated instruction files for: claude\n"
    );

    let explicit_sub = run_update(&root.join("sub"), &[]);
    assert_eq!(explicit_sub.status.code(), Some(1));
    assert_eq!(
        String::from_utf8(explicit_sub.stderr).unwrap(),
        "Error: Not initialized. Run 'spectra init' first.\n"
    );
}

#[test]
fn multi_tool_message_lists_ids_in_registry_order() {
    let root = TempDir::new("update-multi");
    init_root(&root, "openspec");
    // 建立順序故意反著來:訊息仍要照 registry 順序。
    for d in [".agents", ".cursor", ".claude", ".gemini"] {
        std::fs::create_dir_all(root.join(d)).unwrap();
    }

    let out = run_update(&root, &[]);
    assert!(out.status.success());
    assert_eq!(
        stdout(&out),
        "✓ Updated instruction files for: claude, cursor, gemini, codex\n"
    );
    assert!(out.stderr.is_empty(), "unexpected stderr: {out:?}");
}

#[test]
fn an_unwritable_target_reports_the_oracle_error_verbatim() {
    // AC-2 要求錯誤字串逐字對齊:oracle 對唯讀父目錄印裸的
    // `Error: Permission denied (os error 13)`,不含路徑。第一輪 review 曾
    // 要求比照 crate 慣例加上路徑 context,第二輪實測發現那樣就不再等於
    // oracle 的輸出——這個測試釘住實測的那一版。
    let root = TempDir::new("update-unwritable");
    init_root(&root, "openspec");
    let locked_dir = root.join(".claude/skills/spectra-drift");
    std::fs::create_dir_all(&locked_dir).unwrap();
    std::fs::write(locked_dir.join("SKILL.md"), "x\n").unwrap();
    let mut perms = std::fs::metadata(&locked_dir).unwrap().permissions();
    std::os::unix::fs::PermissionsExt::set_mode(&mut perms, 0o500);
    std::fs::set_permissions(&locked_dir, perms.clone()).unwrap();

    let out = run_update(&root, &[]);

    // 還原權限,否則 TempDir 的 drop 清不掉。
    std::os::unix::fs::PermissionsExt::set_mode(&mut perms, 0o755);
    std::fs::set_permissions(&locked_dir, perms).unwrap();

    assert_eq!(out.status.code(), Some(1));
    assert_eq!(
        String::from_utf8(out.stderr).unwrap(),
        "Error: Permission denied (os error 13)\n"
    );
}

// ---- 寫檔集合與位元組(對 golden TSV)----

/// golden TSV:tool → [(relpath, sha256-hex)],capture 自 oracle 實際輸出。
fn golden_rows() -> Vec<(String, String, String)> {
    let path = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("../../docs/reverse-engineering/golden/update-trees-2.3.1.tsv");
    std::fs::read_to_string(&path)
        .unwrap()
        .lines()
        .filter(|l| !l.starts_with('#') && !l.is_empty())
        .map(|l| {
            let mut it = l.split('\t');
            (
                it.next().unwrap().to_string(),
                it.next().unwrap().to_string(),
                it.next().unwrap().to_string(),
            )
        })
        .collect()
}

fn sha256_hex(bytes: &[u8]) -> String {
    format!("{:x}", Sha256::digest(bytes))
}

/// 每個工具單獨跑一次 update,寫出的「每個檔案」都必須 hash-match oracle
/// 的 golden,且不能多寫或少寫任何檔。這是 CI 端不需要 oracle binary 的
/// 逐位元 parity 驗證。
#[test]
fn every_tool_tree_matches_the_oracle_golden_byte_for_byte() {
    let rows = golden_rows();
    assert_eq!(rows.len(), 445, "golden TSV row count drifted");
    let mut tools: Vec<String> = Vec::new();
    for (tool, _, _) in &rows {
        if !tools.contains(tool) {
            tools.push(tool.clone());
        }
    }
    assert_eq!(tools.len(), 23, "golden TSV tool count drifted");

    for tool in &tools {
        let detect_dir = match tool.as_str() {
            "claude" => ".claude",
            "cursor" => ".cursor",
            "windsurf" => ".windsurf",
            "cline" => ".clinerules",
            "gemini" => ".gemini",
            "github-copilot" => ".github/prompts",
            "kiro" => ".kiro",
            "roocode" => ".roo",
            "continue" => ".continue",
            "opencode" => ".opencode",
            "codebuddy" => ".codebuddy",
            "costrict" => ".cospec",
            "antigravity" => ".agent",
            "auggie" => ".augment",
            "amazon-q" => ".amazonq",
            "kilocode" => ".kilocode",
            "factory" => ".factory",
            "iflow" => ".iflow",
            "qoder" => ".qoder",
            "qwen" => ".qwen",
            "codex" => ".agents",
            "crush" => ".crush",
            "trae" => ".trae",
            other => panic!("unknown tool in golden TSV: {other}"),
        };
        let root = TempDir::new(&format!("update-golden-{tool}"));
        init_root(&root, "openspec");
        std::fs::create_dir_all(root.join(detect_dir)).unwrap();

        let out = run_update(&root, &[]);
        assert!(out.status.success(), "{tool}: update failed: {out:?}");
        assert_eq!(
            stdout(&out),
            format!("✓ Updated instruction files for: {tool}\n")
        );
        // stderr parity:oracle 成功時 stderr 全空。少了這個斷言,一行
        // `eprintln!` 可以在全套測試綠燈下混進去(PR #86 round-2, Codex 以
        // mutation 示範)。
        assert!(out.stderr.is_empty(), "{tool}: unexpected stderr: {out:?}");

        let expected: Vec<(&str, &str)> = rows
            .iter()
            .filter(|(t, _, _)| t == tool)
            .map(|(_, rel, sha)| (rel.as_str(), sha.as_str()))
            .collect();
        for (rel, sha) in &expected {
            let bytes = std::fs::read(root.join(rel))
                .unwrap_or_else(|e| panic!("{tool}: missing {rel}: {e}"));
            assert_eq!(&sha256_hex(&bytes), sha, "{tool}: {rel} bytes drifted");
        }

        // 反向:不能多寫 golden 以外的檔案。
        let mut written = Vec::new();
        collect_files(&root, &root, &mut written);
        written.retain(|p| p != ".spectra.yaml");
        assert_eq!(
            written.len(),
            expected.len(),
            "{tool}: extra files written: {written:?}"
        );
    }
}

fn collect_files(base: &Path, dir: &Path, out: &mut Vec<String>) {
    for entry in std::fs::read_dir(dir).unwrap() {
        let path = entry.unwrap().path();
        if path.is_dir() {
            collect_files(base, &path, out);
        } else {
            out.push(
                path.strip_prefix(base)
                    .unwrap()
                    .to_string_lossy()
                    .replace('\\', "/"),
            );
        }
    }
}

// ---- Issue #55 驗收情境:無 --force / --force / 連跑兩次 ----

#[test]
fn existing_files_are_overwritten_even_without_force() {
    // oracle probe:update 對管理檔一律重寫,--force 與否無差別。
    let root = TempDir::new("update-noforce");
    init_root(&root, "openspec");
    std::fs::create_dir_all(root.join(".claude")).unwrap();
    assert!(run_update(&root, &[]).status.success());

    let skill = root.join(".claude/skills/spectra-drift/SKILL.md");
    let original = std::fs::read_to_string(&skill).unwrap();
    std::fs::write(&skill, "tampered").unwrap();

    assert!(run_update(&root, &[]).status.success());
    assert_eq!(std::fs::read_to_string(&skill).unwrap(), original);
}

#[test]
fn force_flag_is_accepted_and_behaves_identically() {
    let root = TempDir::new("update-force");
    init_root(&root, "openspec");
    std::fs::create_dir_all(root.join(".claude")).unwrap();

    let out = run_update(&root, &["--force"]);
    assert!(out.status.success());
    assert_eq!(stdout(&out), "✓ Updated instruction files for: claude\n");

    // 與無 --force 的輸出樹完全一致(逐檔比對)。
    let plain = TempDir::new("update-force-ctl");
    init_root(&plain, "openspec");
    std::fs::create_dir_all(plain.join(".claude")).unwrap();
    assert!(run_update(&plain, &[]).status.success());
    let mut files = Vec::new();
    collect_files(&root, &root, &mut files);
    for rel in files.iter().filter(|p| *p != ".spectra.yaml") {
        assert_eq!(
            std::fs::read(root.join(rel)).unwrap(),
            std::fs::read(plain.join(rel)).unwrap(),
            "{rel} differs between --force and default"
        );
    }
}

#[test]
fn running_twice_is_byte_for_byte_idempotent() {
    let root = TempDir::new("update-twice");
    init_root(&root, "openspec");
    std::fs::create_dir_all(root.join(".claude")).unwrap();

    let first = run_update(&root, &[]);
    assert!(first.status.success());
    let mut files = Vec::new();
    collect_files(&root, &root, &mut files);
    let snapshot: Vec<(String, Vec<u8>)> = files
        .iter()
        .map(|rel| (rel.clone(), std::fs::read(root.join(rel)).unwrap()))
        .collect();

    let second = run_update(&root, &[]);
    assert!(second.status.success());
    assert_eq!(stdout(&first), stdout(&second));
    for (rel, bytes) in &snapshot {
        assert_eq!(
            &std::fs::read(root.join(rel)).unwrap(),
            bytes,
            "{rel} changed on the second run"
        );
    }
}

// ---- 使用者內容保留與 codex-gemini 怪癖(end-to-end)----

#[test]
fn user_content_outside_managed_regions_survives_an_update() {
    let root = TempDir::new("update-user-content");
    init_root(&root, "openspec");
    std::fs::create_dir_all(root.join(".claude")).unwrap();
    std::fs::write(root.join("CLAUDE.md"), "My own notes\n").unwrap();
    std::fs::write(
        root.join(".claude/settings.json"),
        r#"{"userKey": true, "includeGitInstructions": true}"#,
    )
    .unwrap();

    assert!(run_update(&root, &[]).status.success());

    let claude_md = std::fs::read_to_string(root.join("CLAUDE.md")).unwrap();
    assert!(claude_md.starts_with("<!-- SPECTRA:START"));
    assert!(claude_md.ends_with("<!-- SPECTRA:END -->\n\nMy own notes\n"));
    // 管理鍵被強制回 false、使用者鍵保留、字母排序、無結尾換行。
    assert_eq!(
        std::fs::read_to_string(root.join(".claude/settings.json")).unwrap(),
        "{\n  \"includeGitInstructions\": false,\n  \"userKey\": true\n}"
    );
}

#[test]
fn codex_writes_only_agents_md_when_gemini_is_present() {
    let root = TempDir::new("update-codex-quirk");
    init_root(&root, "openspec");
    std::fs::create_dir_all(root.join(".agents")).unwrap();
    std::fs::create_dir_all(root.join(".gemini")).unwrap();

    let out = run_update(&root, &[]);
    assert!(out.status.success());
    assert_eq!(
        stdout(&out),
        "✓ Updated instruction files for: gemini, codex\n"
    );
    assert!(root.join("AGENTS.md").is_file());
    assert!(!root.join(".agents/skills").exists());
    assert!(root.join(".gemini/skills/spectra-apply/SKILL.md").is_file());
}

// ---- 自訂 spec_dir 代換(end-to-end)----

#[test]
fn a_non_utf8_existing_file_does_not_abort_the_run() {
    // 迴歸(PR #86 review):一個 latin-1 的既有檔曾讓整個 run 變成 exit 1 +
    // 部分寫入(17 檔只寫出 4 檔)。oracle 把讀不懂的既有檔當成不存在。
    let root = TempDir::new("update-non-utf8");
    init_root(&root, "openspec");
    std::fs::create_dir_all(root.join(".claude")).unwrap();
    std::fs::write(root.join(".claude/settings.json"), b"{\"k\":\"caf\xe9\"}").unwrap();
    std::fs::write(root.join("CLAUDE.md"), b"caf\xe9 notes\n").unwrap();

    let out = run_update(&root, &[]);
    assert!(out.status.success(), "run aborted: {out:?}");
    assert_eq!(stdout(&out), "✓ Updated instruction files for: claude\n");

    let mut files = Vec::new();
    collect_files(&root, &root, &mut files);
    assert!(
        files.len() >= 15,
        "expected the full tree to be written, got {files:?}"
    );
    assert_eq!(
        std::fs::read_to_string(root.join(".claude/settings.json")).unwrap(),
        "{\n  \"includeGitInstructions\": false\n}"
    );
}

#[test]
fn a_symlinked_managed_file_does_not_leak_writes_outside_the_project() {
    // 本 repo 的安全 baseline(artifact.rs:374)在 `update` 上的釘子:
    // oracle 會寫穿 symlink 覆寫專案外檔案,openspectra 一律不。
    let root = TempDir::new("update-symlink");
    init_root(&root, "openspec");
    std::fs::create_dir_all(root.join(".claude")).unwrap();
    let outside = root.join("outside-secret.md");
    std::fs::write(&outside, "PRECIOUS").unwrap();
    std::os::unix::fs::symlink(&outside, root.join("CLAUDE.md")).unwrap();

    let out = run_update(&root, &[]);
    assert!(out.status.success());
    assert_eq!(std::fs::read_to_string(&outside).unwrap(), "PRECIOUS");
}

#[test]
fn a_directory_at_a_plain_target_reports_the_oracle_errno() {
    // oracle 對「目標是目錄」只回報建立步驟的錯誤(`Is a directory`);若我們
    // 無條件 unlink,同一輸入會變成 `Operation not permitted`。AC-2 要求逐字對齊。
    let root = TempDir::new("update-dir-target");
    init_root(&root, "openspec");
    std::fs::create_dir_all(root.join(".claude/skills/spectra-drift/SKILL.md")).unwrap();

    let out = run_update(&root, &[]);

    assert_eq!(out.status.code(), Some(1));
    assert_eq!(
        String::from_utf8(out.stderr).unwrap(),
        "Error: Is a directory (os error 21)\n"
    );
}

#[test]
fn a_directory_at_a_managed_target_reports_the_oracle_errno() {
    // 迴歸(PR #86 round-2,Claude round2 lens):寫入半邊改成裸錯誤時,
    // 讀取半邊(read_existing)漏改,於是 Managed / ClaudeSettings 的失敗
    // 訊息多出一段絕對路徑。Plain 形狀當時已經對齊,所以只有這半邊會露餡。
    let root = TempDir::new("update-managed-dir");
    init_root(&root, "openspec");
    std::fs::create_dir_all(root.join(".claude")).unwrap();
    std::fs::create_dir_all(root.join("CLAUDE.md")).unwrap();

    let out = run_update(&root, &[]);

    assert_eq!(out.status.code(), Some(1));
    assert_eq!(
        String::from_utf8(out.stderr).unwrap(),
        "Error: Is a directory (os error 21)\n"
    );
}

#[test]
fn a_directory_at_the_settings_target_reports_the_oracle_errno() {
    let root = TempDir::new("update-settings-dir");
    init_root(&root, "openspec");
    std::fs::create_dir_all(root.join(".claude/settings.json")).unwrap();

    let out = run_update(&root, &[]);

    assert_eq!(out.status.code(), Some(1));
    assert_eq!(
        String::from_utf8(out.stderr).unwrap(),
        "Error: Is a directory (os error 21)\n"
    );
}

#[test]
fn an_unwritable_project_root_still_updates_an_existing_managed_file() {
    // oracle 就地寫入 Managed 檔,只需要檔案的寫入權;0500 的根目錄仍能成功。
    // 我們的 temp+rename 需要目錄寫入權,故在該情境退回就地寫入以維持 parity。
    let root = TempDir::new("update-ro-root");
    init_root(&root, "openspec");
    std::fs::create_dir_all(root.join(".claude")).unwrap();
    assert!(run_update(&root, &[]).status.success());

    let mut perms = std::fs::metadata(&*root).unwrap().permissions();
    std::os::unix::fs::PermissionsExt::set_mode(&mut perms, 0o500);
    std::fs::set_permissions(&*root, perms.clone()).unwrap();

    let out = run_update(&root, &[]);

    std::os::unix::fs::PermissionsExt::set_mode(&mut perms, 0o755);
    std::fs::set_permissions(&*root, perms).unwrap();

    assert!(
        out.status.success(),
        "expected oracle parity (exit 0): {out:?}"
    );
    assert_eq!(stdout(&out), "✓ Updated instruction files for: claude\n");
}

#[test]
fn custom_spec_dir_is_substituted_into_written_files() {
    let root = TempDir::new("update-specdir");
    init_root(&root, "docs/specs");
    std::fs::create_dir_all(root.join(".claude")).unwrap();

    assert!(run_update(&root, &[]).status.success());

    let claude_md = std::fs::read_to_string(root.join("CLAUDE.md")).unwrap();
    assert!(claude_md.contains("`docs/specs/specs/`"));
    assert!(!claude_md.contains("openspec/"));
}