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
mod common;

use std::io::Write;
use std::path::Path;
use std::process::{Output, Stdio};

use common::{change_dir, init_project_with_change, spectra, TempDir};

fn run_with_stdin(root: &Path, args: &[&str], content: &str) -> Output {
    let mut child = spectra()
        .args(args)
        .current_dir(root)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();
    child
        .stdin
        .take()
        .unwrap()
        .write_all(content.as_bytes())
        .unwrap();
    child.wait_with_output().unwrap()
}

#[test]
fn proposal_stdin_writes_exact_bytes_and_compact_json() {
    let root = TempDir::new("proposal");
    init_project_with_change(&root, "demo-feature");
    let content = "intro ## Why this matters";

    let out = run_with_stdin(
        &root,
        &[
            "new",
            "artifact",
            "proposal",
            "--change",
            "demo-feature",
            "--stdin",
            "--json",
        ],
        content,
    );

    assert!(out.status.success(), "new artifact failed: {out:?}");
    let path = change_dir(&root, "demo-feature").join("proposal.md");
    assert_eq!(std::fs::read(&path).unwrap(), content.as_bytes());
    assert_eq!(
        String::from_utf8(out.stdout).unwrap(),
        format!(
            "{{\"artifact\":\"proposal\",\"change\":\"demo-feature\",\"path\":\"{}\",\"status\":\"created\",\"validated\":true,\"warnings\":[]}}\n",
            path.display()
        )
    );
}

#[test]
fn design_template_uses_schema_constant_and_is_not_validated() {
    let root = TempDir::new("design-template");
    init_project_with_change(&root, "demo-feature");

    let out = spectra()
        .args([
            "new",
            "artifact",
            "design",
            "--change",
            "demo-feature",
            "--json",
        ])
        .current_dir(&*root)
        .output()
        .unwrap();

    assert!(out.status.success(), "new artifact failed: {out:?}");
    let path = change_dir(&root, "demo-feature").join("design.md");
    assert_eq!(
        std::fs::read_to_string(&path).unwrap(),
        spectra_core::schema::DESIGN_TEMPLATE
    );
    let report: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(report["artifact"], "design");
    assert_eq!(report["path"], path.to_string_lossy().as_ref());
    assert_eq!(report["validated"], false);
}

#[test]
fn tasks_stdin_with_checkbox_is_validated() {
    let root = TempDir::new("tasks");
    init_project_with_change(&root, "demo-feature");
    let content = "## 1. Work\n\n- [ ] 1.1 Implement it";

    let out = run_with_stdin(
        &root,
        &[
            "new",
            "artifact",
            "tasks",
            "--change",
            "demo-feature",
            "--stdin",
            "--json",
        ],
        content,
    );

    assert!(out.status.success(), "new artifact failed: {out:?}");
    let report: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(report["validated"], true);
    assert_eq!(
        std::fs::read_to_string(change_dir(&root, "demo-feature").join("tasks.md")).unwrap(),
        content
    );
}

#[test]
fn spec_stdin_lands_under_capability_directory() {
    let root = TempDir::new("spec");
    init_project_with_change(&root, "demo-feature");
    let content = "## ADDED Requirements";

    let out = run_with_stdin(
        &root,
        &[
            "new",
            "artifact",
            "spec",
            "user-auth",
            "--change",
            "demo-feature",
            "--stdin",
            "--json",
        ],
        content,
    );

    assert!(out.status.success(), "new artifact failed: {out:?}");
    let path = change_dir(&root, "demo-feature")
        .join("specs")
        .join("user-auth")
        .join("spec.md");
    assert_eq!(std::fs::read_to_string(&path).unwrap(), content);
    let report: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(report["artifact"], "spec");
    assert_eq!(report["path"], path.to_string_lossy().as_ref());
    assert_eq!(report["validated"], true);
}

#[test]
fn unknown_type_reports_the_oracle_error() {
    let root = TempDir::new("unknown-type");
    init_project_with_change(&root, "demo-feature");

    let out = spectra()
        .args(["new", "artifact", "bogus", "--change", "demo-feature"])
        .current_dir(&*root)
        .output()
        .unwrap();

    assert_eq!(out.status.code(), Some(1));
    assert_eq!(
        String::from_utf8(out.stderr).unwrap(),
        "Error: Unknown artifact type 'bogus'. Valid types: proposal, design, tasks, spec\n"
    );
}

#[test]
fn spec_without_capability_reports_the_oracle_error() {
    let root = TempDir::new("missing-capability");
    init_project_with_change(&root, "demo-feature");

    let out = spectra()
        .args(["new", "artifact", "spec", "--change", "demo-feature"])
        .current_dir(&*root)
        .output()
        .unwrap();

    assert_eq!(out.status.code(), Some(1));
    assert_eq!(
        String::from_utf8(out.stderr).unwrap(),
        "Error: Capability name is required for spec type. Usage: spectra new artifact spec <capability> --change <name>\n"
    );
}

#[test]
fn already_exists_errors_then_force_overwrites() {
    let root = TempDir::new("force");
    init_project_with_change(&root, "demo-feature");
    let path = change_dir(&root, "demo-feature").join("proposal.md");

    let first = run_with_stdin(
        &root,
        &[
            "new",
            "artifact",
            "proposal",
            "--change",
            "demo-feature",
            "--stdin",
        ],
        "## Why first",
    );
    assert!(first.status.success(), "first create failed: {first:?}");

    let second = run_with_stdin(
        &root,
        &[
            "new",
            "artifact",
            "proposal",
            "--change",
            "demo-feature",
            "--stdin",
        ],
        "## Why second",
    );
    assert_eq!(second.status.code(), Some(1));
    assert_eq!(
        String::from_utf8(second.stderr).unwrap(),
        format!(
            "Error: Artifact already exists: {}. Use --force to overwrite\n",
            path.display()
        )
    );

    let forced = run_with_stdin(
        &root,
        &[
            "new",
            "artifact",
            "proposal",
            "--change",
            "demo-feature",
            "--stdin",
            "--force",
        ],
        "## Why replacement",
    );
    assert!(forced.status.success(), "forced create failed: {forced:?}");
    assert_eq!(std::fs::read_to_string(path).unwrap(), "## Why replacement");
}

#[test]
fn proposal_validation_failure_does_not_create_file() {
    let root = TempDir::new("validation-failure");
    init_project_with_change(&root, "demo-feature");
    let path = change_dir(&root, "demo-feature").join("proposal.md");

    let out = run_with_stdin(
        &root,
        &[
            "new",
            "artifact",
            "proposal",
            "--change",
            "demo-feature",
            "--stdin",
        ],
        "## Motivation",
    );

    assert_eq!(out.status.code(), Some(1));
    assert_eq!(
        String::from_utf8(out.stderr).unwrap(),
        "Error: Proposal must contain a ## Why, ## Problem, or ## Summary section\n"
    );
    assert!(!path.exists());
}

#[test]
fn nonexistent_explicit_change_has_no_trailing_period_in_error() {
    let root = TempDir::new("missing-change");
    init_project_with_change(&root, "demo-feature");

    let out = spectra()
        .args(["new", "artifact", "proposal", "--change", "no-such"])
        .current_dir(&*root)
        .output()
        .unwrap();

    assert_eq!(out.status.code(), Some(1));
    assert_eq!(
        String::from_utf8(out.stderr).unwrap(),
        "Error: Change 'no-such' not found\n"
    );
}

#[test]
fn human_output_includes_validation_line_only_for_stdin() {
    let root = TempDir::new("human");
    init_project_with_change(&root, "demo-feature");

    let out = run_with_stdin(
        &root,
        &[
            "new",
            "artifact",
            "proposal",
            "--change",
            "demo-feature",
            "--stdin",
        ],
        "## Why human output",
    );

    assert!(out.status.success(), "new artifact failed: {out:?}");
    let path = change_dir(&root, "demo-feature").join("proposal.md");
    assert_eq!(
        String::from_utf8(out.stdout).unwrap(),
        format!(
            "✓ Created proposal: {}\n  Content validated ✓\n",
            path.display()
        )
    );
}

#[test]
fn force_with_invalid_content_exits_1_and_preserves_the_original_file() {
    // Probed contract (design.md): --force does NOT skip content validation;
    // invalid stdin + --force exits 1 and the existing artifact is untouched.
    // Previously true only by the accident of validation-before-write
    // ordering -- this pins it against refactors that would silently clobber
    // a valid artifact.
    let root = TempDir::new("force-no-clobber");
    init_project_with_change(&root, "demo-feature");
    let path = change_dir(&root, "demo-feature").join("proposal.md");

    let first = run_with_stdin(
        &root,
        &[
            "new",
            "artifact",
            "proposal",
            "--change",
            "demo-feature",
            "--stdin",
        ],
        "## Why original",
    );
    assert!(first.status.success(), "first create failed: {first:?}");

    let clobber_attempt = run_with_stdin(
        &root,
        &[
            "new",
            "artifact",
            "proposal",
            "--change",
            "demo-feature",
            "--stdin",
            "--force",
        ],
        "no required heading here",
    );
    assert_eq!(clobber_attempt.status.code(), Some(1));
    assert_eq!(
        String::from_utf8(clobber_attempt.stderr).unwrap(),
        "Error: Proposal must contain a ## Why, ## Problem, or ## Summary section\n"
    );
    assert_eq!(std::fs::read_to_string(&path).unwrap(), "## Why original");
}

#[test]
fn extra_capability_positional_is_ignored_for_non_spec_types() {
    // Probed against Spectra.app 2.3.1 (2026-07-18): the oracle also accepts
    // and silently ignores a capability positional for non-spec types
    // (`new artifact proposal extra-arg --change X --stdin` exits 0 and
    // creates the file). Pinned so a future "reject it" change is a
    // deliberate divergence, not an accident.
    let root = TempDir::new("extra-positional");
    init_project_with_change(&root, "demo-feature");

    let out = run_with_stdin(
        &root,
        &[
            "new",
            "artifact",
            "proposal",
            "extra-arg",
            "--change",
            "demo-feature",
            "--stdin",
        ],
        "## Why ignored positional",
    );

    assert!(out.status.success(), "new artifact failed: {out:?}");
    let path = change_dir(&root, "demo-feature").join("proposal.md");
    assert_eq!(
        std::fs::read_to_string(&path).unwrap(),
        "## Why ignored positional"
    );
}

#[test]
fn json_mode_errors_stay_on_stderr_with_no_partial_json() {
    // Errors must not change shape under --json: same plain-text stderr,
    // exit 1, and nothing (not even partial JSON) on stdout.
    let root = TempDir::new("json-error");
    init_project_with_change(&root, "demo-feature");

    let out = run_with_stdin(
        &root,
        &[
            "new",
            "artifact",
            "nonsense",
            "--change",
            "demo-feature",
            "--stdin",
            "--json",
        ],
        "## Why",
    );

    assert_eq!(out.status.code(), Some(1));
    assert_eq!(
        String::from_utf8(out.stderr).unwrap(),
        "Error: Unknown artifact type 'nonsense'. Valid types: proposal, design, tasks, spec\n"
    );
    assert_eq!(out.stdout, b"");
}