forked from Fission-AI/OpenSpec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathartifact-workflow.test.ts
More file actions
970 lines (832 loc) · 37 KB
/
Copy pathartifact-workflow.test.ts
File metadata and controls
970 lines (832 loc) · 37 KB
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { promises as fs } from 'fs';
import path from 'path';
import os from 'os';
import { runCLI } from '../helpers/run-cli.js';
import { FileSystemUtils } from '../../src/utils/file-system.js';
describe('artifact-workflow CLI commands', () => {
let tempDir: string;
let changesDir: string;
const canonical = (targetPath: string): string => FileSystemUtils.canonicalizeExistingPath(targetPath);
beforeEach(async () => {
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'openspec-artifact-workflow-'));
changesDir = path.join(tempDir, 'openspec', 'changes');
await fs.mkdir(changesDir, { recursive: true });
});
afterEach(async () => {
if (tempDir) {
await fs.rm(tempDir, { recursive: true, force: true });
}
});
/**
* Gets combined output from CLI result (ora outputs to stdout).
*/
function getOutput(result: { stdout: string; stderr: string }): string {
return result.stdout + result.stderr;
}
/**
* Normalizes path separators to forward slashes for cross-platform assertions.
*/
function normalizePaths(str: string): string {
return str.replace(/\\/g, '/');
}
/**
* Creates a test change with the specified artifacts completed.
* Note: An "active" change requires at least a proposal.md file to be detected.
* If no artifacts are specified, we create an empty proposal to make it detectable.
*/
async function createTestChange(
changeName: string,
artifacts: ('proposal' | 'design' | 'specs' | 'tasks')[] = []
): Promise<string> {
const changeDir = path.join(changesDir, changeName);
await fs.mkdir(changeDir, { recursive: true });
// Always create proposal.md for the change to be detected as active
// Content varies based on whether 'proposal' is in artifacts list
const proposalContent = artifacts.includes('proposal')
? '## Why\nTest proposal content that is long enough.\n\n## What Changes\n- **test:** Something'
: '## Why\nMinimal proposal.\n\n## What Changes\n- **test:** Placeholder';
await fs.writeFile(path.join(changeDir, 'proposal.md'), proposalContent);
if (artifacts.includes('design')) {
await fs.writeFile(path.join(changeDir, 'design.md'), '# Design\n\nTechnical design.');
}
if (artifacts.includes('specs')) {
// specs artifact uses glob pattern "specs/*.md" - files directly in specs/ directory
const specsDir = path.join(changeDir, 'specs');
await fs.mkdir(specsDir, { recursive: true });
await fs.writeFile(path.join(specsDir, 'test-spec.md'), '## Purpose\nTest spec.');
}
if (artifacts.includes('tasks')) {
await fs.writeFile(path.join(changeDir, 'tasks.md'), '## Tasks\n- [ ] Task 1');
}
return changeDir;
}
describe('status command', () => {
it('shows status for scaffolded change without proposal.md', async () => {
// Create empty change directory (no proposal.md)
const changeDir = path.join(changesDir, 'scaffolded-change');
await fs.mkdir(changeDir, { recursive: true });
const result = await runCLI(['status', '--change', 'scaffolded-change'], { cwd: tempDir });
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('scaffolded-change');
expect(result.stdout).toContain('0/4 artifacts complete');
});
it('shows status for a change with proposal only', async () => {
// createTestChange always creates proposal.md, so this has 1 artifact complete
await createTestChange('minimal-change');
const result = await runCLI(['status', '--change', 'minimal-change'], { cwd: tempDir });
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('minimal-change');
expect(result.stdout).toContain('spec-driven');
expect(result.stdout).toContain('1/4 artifacts complete');
});
it('shows status for a change with proposal and design', async () => {
await createTestChange('partial-change', ['proposal', 'design']);
const result = await runCLI(['status', '--change', 'partial-change'], { cwd: tempDir });
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('2/4 artifacts complete');
expect(result.stdout).toContain('[x]');
});
it('outputs JSON when --json flag is used', async () => {
await createTestChange('json-change', ['proposal', 'design']);
const result = await runCLI(['status', '--change', 'json-change', '--json'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(0);
expect(result.stderr).toBe('');
const json = JSON.parse(result.stdout);
expect(json.changeName).toBe('json-change');
expect(json.schemaName).toBe('spec-driven');
expect(json.isComplete).toBe(false);
expect(Array.isArray(json.artifacts)).toBe(true);
expect(json.artifacts).toHaveLength(4);
const proposalArtifact = json.artifacts.find((a: any) => a.id === 'proposal');
expect(proposalArtifact.status).toBe('done');
});
it('shows complete status when all artifacts are done', async () => {
await createTestChange('complete-change', ['proposal', 'design', 'specs', 'tasks']);
const result = await runCLI(['status', '--change', 'complete-change'], { cwd: tempDir });
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('4/4 artifacts complete');
expect(result.stdout).toContain('All artifacts complete!');
});
it('exits gracefully when no changes exist', async () => {
const result = await runCLI(['status'], { cwd: tempDir });
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('No active changes');
expect(result.stdout).toContain('openspec new change');
});
it('exits gracefully with JSON when no changes exist', async () => {
const result = await runCLI(['status', '--json'], { cwd: tempDir });
expect(result.exitCode).toBe(0);
const json = JSON.parse(result.stdout);
expect(json.changes).toEqual([]);
expect(json.message).toBe('No active changes.');
});
it('errors when --change is missing and lists available changes', async () => {
await createTestChange('some-change');
const result = await runCLI(['status'], { cwd: tempDir });
expect(result.exitCode).toBe(1);
const output = getOutput(result);
expect(output).toContain('Missing required option --change');
expect(output).toContain('some-change');
});
it('errors for unknown change name and lists available changes', async () => {
await createTestChange('existing-change');
const result = await runCLI(['status', '--change', 'nonexistent'], { cwd: tempDir });
expect(result.exitCode).toBe(1);
const output = getOutput(result);
expect(output).toContain("Change 'nonexistent' not found");
expect(output).toContain('existing-change');
});
it('supports --schema option', async () => {
await createTestChange('schema-change');
const result = await runCLI(['status', '--change', 'schema-change', '--schema', 'spec-driven'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('spec-driven');
});
it('errors for unknown schema', async () => {
await createTestChange('test-change');
const result = await runCLI(['status', '--change', 'test-change', '--schema', 'unknown'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(1);
const output = getOutput(result);
expect(output).toContain("Schema 'unknown' not found");
});
it('rejects path traversal in change name', async () => {
const result = await runCLI(['status', '--change', '../foo'], { cwd: tempDir });
expect(result.exitCode).toBe(1);
const output = getOutput(result);
expect(output).toContain('Invalid change name');
});
it('rejects absolute path in change name', async () => {
const result = await runCLI(['status', '--change', '/etc/passwd'], { cwd: tempDir });
expect(result.exitCode).toBe(1);
const output = getOutput(result);
expect(output).toContain('Invalid change name');
});
it('rejects slashes in change name', async () => {
const result = await runCLI(['status', '--change', 'foo/bar'], { cwd: tempDir });
expect(result.exitCode).toBe(1);
const output = getOutput(result);
expect(output).toContain('Invalid change name');
});
});
describe('instructions command', () => {
it('shows instructions for proposal on scaffolded change', async () => {
// Create empty change directory (no proposal.md)
const changeDir = path.join(changesDir, 'scaffolded-change');
await fs.mkdir(changeDir, { recursive: true });
const result = await runCLI(['instructions', 'proposal', '--change', 'scaffolded-change'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('<artifact id="proposal"');
expect(result.stdout).toContain('proposal.md');
expect(result.stdout).toContain('<template>');
});
it('shows instructions for design artifact', async () => {
await createTestChange('instr-change');
const result = await runCLI(['instructions', 'design', '--change', 'instr-change'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('<artifact id="design"');
expect(result.stdout).toContain('design.md');
expect(result.stdout).toContain('<template>');
});
it('shows blocked warning for artifact with unmet dependencies', async () => {
// tasks depends on design and specs, which are not done yet
await createTestChange('blocked-change');
const result = await runCLI(['instructions', 'tasks', '--change', 'blocked-change'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('<warning>');
expect(result.stdout).toContain('status="missing"');
});
it('outputs JSON for instructions', async () => {
await createTestChange('json-instr', ['proposal']);
const result = await runCLI(['instructions', 'design', '--change', 'json-instr', '--json'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(0);
expect(result.stderr).toBe('');
const json = JSON.parse(result.stdout);
expect(json.artifactId).toBe('design');
expect(json.outputPath).toContain('design.md');
expect(typeof json.template).toBe('string');
expect(Array.isArray(json.dependencies)).toBe(true);
});
it('errors when artifact argument is missing', async () => {
await createTestChange('test-change');
const result = await runCLI(['instructions', '--change', 'test-change'], { cwd: tempDir });
expect(result.exitCode).toBe(1);
const output = getOutput(result);
expect(output).toContain('Missing required argument <artifact>');
expect(output).toContain('Valid artifacts');
});
it('errors for unknown artifact', async () => {
await createTestChange('test-change');
const result = await runCLI(['instructions', 'unknown-artifact', '--change', 'test-change'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(1);
const output = getOutput(result);
expect(output).toContain("Artifact 'unknown-artifact' not found");
expect(output).toContain('Valid artifacts');
});
});
describe('templates command', () => {
it('shows template paths for default schema', async () => {
const result = await runCLI(['templates'], { cwd: tempDir });
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Schema: spec-driven');
expect(result.stdout).toContain('proposal:');
expect(result.stdout).toContain('design:');
expect(result.stdout).toContain('specs:');
expect(result.stdout).toContain('tasks:');
});
it('shows template paths for specified schema', async () => {
const result = await runCLI(['templates', '--schema', 'spec-driven'], { cwd: tempDir });
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Schema: spec-driven');
expect(result.stdout).toContain('proposal:');
expect(result.stdout).toContain('design:');
});
it('outputs JSON mapping of templates', async () => {
const result = await runCLI(['templates', '--json'], { cwd: tempDir });
expect(result.exitCode).toBe(0);
expect(result.stderr).toBe('');
const json = JSON.parse(result.stdout);
expect(json.proposal).toBeDefined();
expect(json.proposal.path).toContain('proposal.md');
expect(json.proposal.source).toBe('package');
});
it('errors for unknown schema', async () => {
const result = await runCLI(['templates', '--schema', 'nonexistent'], { cwd: tempDir });
expect(result.exitCode).toBe(1);
const output = getOutput(result);
expect(output).toContain("Schema 'nonexistent' not found");
});
});
describe('new change command', () => {
it('creates a new change directory', async () => {
const result = await runCLI(['new', 'change', 'my-new-feature'], { cwd: tempDir });
expect(result.exitCode).toBe(0);
const output = getOutput(result);
expect(output).toContain("Created change 'my-new-feature'");
const changeDir = path.join(changesDir, 'my-new-feature');
const stat = await fs.stat(changeDir);
expect(stat.isDirectory()).toBe(true);
});
it('rejects --initiative and writes no change', async () => {
const result = await runCLI(
['new', 'change', 'linked-change', '--initiative', 'billing-launch'],
{ cwd: tempDir }
);
expect(result.exitCode).toBe(1);
const output = getOutput(result);
expect(output).toContain('--initiative is no longer supported');
await expect(fs.stat(path.join(changesDir, 'linked-change'))).rejects.toMatchObject({
code: 'ENOENT',
});
});
it('rejects --areas and writes no affected-area metadata', async () => {
const result = await runCLI(['new', 'change', 'area-change', '--areas', 'api'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(1);
const output = getOutput(result);
expect(output).toContain('--areas is no longer supported');
await expect(fs.stat(path.join(changesDir, 'area-change'))).rejects.toMatchObject({
code: 'ENOENT',
});
});
it('keeps --goal as ordinary metadata without switching schema', async () => {
const result = await runCLI(
['new', 'change', 'goal-change', '--goal', 'Improve billing'],
{ cwd: tempDir }
);
expect(result.exitCode).toBe(0);
const metadata = await fs.readFile(
path.join(changesDir, 'goal-change', '.openspec.yaml'),
'utf-8'
);
expect(metadata).toContain('schema: spec-driven');
expect(metadata).toContain('goal: Improve billing');
expect(metadata).not.toContain('affected_areas');
expect(metadata).not.toContain('initiative');
});
it('creates README.md when --description is provided', async () => {
const result = await runCLI(
['new', 'change', 'described-feature', '--description', 'This is a test feature'],
{ cwd: tempDir }
);
expect(result.exitCode).toBe(0);
const readmePath = path.join(changesDir, 'described-feature', 'README.md');
const content = await fs.readFile(readmePath, 'utf-8');
expect(content).toContain('described-feature');
expect(content).toContain('This is a test feature');
});
it('errors for invalid change name with spaces', async () => {
const result = await runCLI(['new', 'change', 'invalid name'], { cwd: tempDir });
expect(result.exitCode).toBe(1);
const output = getOutput(result);
expect(output).toContain('Error');
});
it('errors for duplicate change name', async () => {
await createTestChange('existing-change');
const result = await runCLI(['new', 'change', 'existing-change'], { cwd: tempDir });
expect(result.exitCode).toBe(1);
const output = getOutput(result);
expect(output).toContain('exists');
});
it('errors when name argument is missing', async () => {
const result = await runCLI(['new', 'change'], { cwd: tempDir });
expect(result.exitCode).toBe(1);
});
});
describe('instructions apply command', () => {
it('shows apply instructions for spec-driven schema with tasks', async () => {
await createTestChange('apply-change', ['proposal', 'design', 'specs', 'tasks']);
const result = await runCLI(['instructions', 'apply', '--change', 'apply-change'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('## Apply: apply-change');
expect(result.stdout).toContain('Schema: spec-driven');
expect(result.stdout).toContain('### Context Files');
expect(result.stdout).toContain('### Instruction');
});
it('shows blocked state when required artifacts are missing', async () => {
// Only create proposal - missing tasks (required by spec-driven apply block)
await createTestChange('blocked-apply', ['proposal']);
const result = await runCLI(['instructions', 'apply', '--change', 'blocked-apply'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Blocked');
expect(result.stdout).toContain('Missing artifacts: tasks');
});
it('outputs JSON for apply instructions', async () => {
await createTestChange('json-apply', ['proposal', 'design', 'specs', 'tasks']);
const result = await runCLI(
['instructions', 'apply', '--change', 'json-apply', '--json'],
{ cwd: tempDir }
);
expect(result.exitCode).toBe(0);
expect(result.stderr).toBe('');
const json = JSON.parse(result.stdout);
const expectedProposalPath = canonical(path.join(changesDir, 'json-apply', 'proposal.md'));
const expectedSpecPath = canonical(path.join(changesDir, 'json-apply', 'specs', 'test-spec.md'));
expect(json.changeName).toBe('json-apply');
expect(json.schemaName).toBe('spec-driven');
expect(json.state).toBe('ready');
expect(json.contextFiles).toBeDefined();
expect(typeof json.contextFiles).toBe('object');
expect(json.contextFiles.proposal).toEqual([expectedProposalPath]);
expect(json.contextFiles.specs).toEqual([expectedSpecPath]);
});
it('resolves single-star glob artifacts consistently between status and apply', async () => {
const schemaDir = path.join(tempDir, 'openspec', 'schemas', 'glob-test');
const templatesDir = path.join(schemaDir, 'templates');
await fs.mkdir(templatesDir, { recursive: true });
await fs.writeFile(
path.join(schemaDir, 'schema.yaml'),
`name: glob-test
version: 1
description: Test schema for single-star globs
artifacts:
- id: specs
generates: specs/*/spec.md
description: Nested specs
template: spec.md
requires: []
apply:
requires: [specs]
instruction: Ready when specs exist.
`
);
await fs.writeFile(path.join(templatesDir, 'spec.md'), '# Spec\n');
const changeDir = path.join(changesDir, 'single-star-glob');
const specPath = path.join(changeDir, 'specs', 'single-star-glob', 'spec.md');
await fs.mkdir(path.dirname(specPath), { recursive: true });
await fs.writeFile(path.join(changeDir, '.openspec.yaml'), 'schema: glob-test\n');
await fs.writeFile(specPath, '# Nested spec\n');
const statusResult = await runCLI(['status', '--change', 'single-star-glob', '--json'], {
cwd: tempDir,
});
expect(statusResult.exitCode).toBe(0);
const statusJson = JSON.parse(statusResult.stdout);
expect(statusJson.artifacts).toEqual([
{
id: 'specs',
outputPath: 'specs/*/spec.md',
status: 'done',
},
]);
const applyResult = await runCLI(
['instructions', 'apply', '--change', 'single-star-glob', '--json'],
{ cwd: tempDir }
);
expect(applyResult.exitCode).toBe(0);
const applyJson = JSON.parse(applyResult.stdout);
const resolvedSpecPath = canonical(specPath);
expect(applyJson.state).toBe('ready');
expect(applyJson.missingArtifacts).toBeUndefined();
expect(applyJson.contextFiles).toEqual({
specs: [resolvedSpecPath],
});
});
it('shows schema instruction from apply block', async () => {
await createTestChange('instr-apply', ['proposal', 'design', 'specs', 'tasks']);
const result = await runCLI(['instructions', 'apply', '--change', 'instr-apply'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(0);
// Should show the instruction from spec-driven schema apply block
expect(result.stdout).toContain('work through pending tasks');
});
it('shows all_done state when all tasks are complete', async () => {
const changeDir = await createTestChange('done-apply', [
'proposal',
'design',
'specs',
'tasks',
]);
// Overwrite tasks with all completed
await fs.writeFile(
path.join(changeDir, 'tasks.md'),
'## Tasks\n- [x] Task 1\n- [x] Task 2'
);
const result = await runCLI(['instructions', 'apply', '--change', 'done-apply'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('complete ✓');
expect(result.stdout).toContain('ready to be archived');
});
it('uses spec-driven schema apply configuration', async () => {
// Create a spec-driven style change with all artifacts
await createTestChange('apply-schema-test', ['proposal', 'design', 'specs', 'tasks']);
const result = await runCLI(
['instructions', 'apply', '--change', 'apply-schema-test', '--schema', 'spec-driven'],
{ cwd: tempDir }
);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Schema: spec-driven');
});
it('spec-driven schema uses apply block configuration', async () => {
// Verify that spec-driven schema uses its apply block (requires: [tasks])
await createTestChange('apply-config-test', ['proposal', 'design', 'specs', 'tasks']);
const result = await runCLI(
['instructions', 'apply', '--change', 'apply-config-test', '--json'],
{ cwd: tempDir }
);
expect(result.exitCode).toBe(0);
const json = JSON.parse(result.stdout);
// spec-driven schema has apply block with requires: [tasks], so should be ready
expect(json.schemaName).toBe('spec-driven');
expect(json.state).toBe('ready');
});
it('fallback: requires all artifacts when schema has no apply block', async () => {
// Create a minimal schema without an apply block in user schemas dir
const userDataDir = path.join(tempDir, 'user-data');
const noApplySchemaDir = path.join(userDataDir, 'openspec', 'schemas', 'no-apply');
const templatesDir = path.join(noApplySchemaDir, 'templates');
await fs.mkdir(templatesDir, { recursive: true });
// Minimal schema with 2 artifacts, no apply block
const schemaContent = `
name: no-apply
version: 1
description: Test schema without apply block
artifacts:
- id: first
generates: first.md
description: First artifact
template: first.md
requires: []
- id: second
generates: second.md
description: Second artifact
template: second.md
requires: [first]
`;
await fs.writeFile(path.join(noApplySchemaDir, 'schema.yaml'), schemaContent);
await fs.writeFile(path.join(templatesDir, 'first.md'), '# First\n');
await fs.writeFile(path.join(templatesDir, 'second.md'), '# Second\n');
// Create a change with only the first artifact (missing second)
const changeDir = path.join(changesDir, 'no-apply-test');
await fs.mkdir(changeDir, { recursive: true });
await fs.writeFile(path.join(changeDir, 'first.md'), '# First artifact content');
// Run with XDG_DATA_HOME pointing to our temp user data dir
const result = await runCLI(
['instructions', 'apply', '--change', 'no-apply-test', '--schema', 'no-apply', '--json'],
{
cwd: tempDir,
env: { XDG_DATA_HOME: userDataDir },
}
);
expect(result.exitCode).toBe(0);
const json = JSON.parse(result.stdout);
// Without apply block, fallback requires ALL artifacts - second is missing
expect(json.schemaName).toBe('no-apply');
expect(json.state).toBe('blocked');
expect(json.missingArtifacts).toContain('second');
});
it('fallback: ready when all artifacts exist for schema without apply block', async () => {
// Create a minimal schema without an apply block
const userDataDir = path.join(tempDir, 'user-data-2');
const noApplySchemaDir = path.join(userDataDir, 'openspec', 'schemas', 'no-apply-full');
const templatesDir = path.join(noApplySchemaDir, 'templates');
await fs.mkdir(templatesDir, { recursive: true });
const schemaContent = `
name: no-apply-full
version: 1
description: Test schema without apply block
artifacts:
- id: only
generates: only.md
description: Only artifact
template: only.md
requires: []
`;
await fs.writeFile(path.join(noApplySchemaDir, 'schema.yaml'), schemaContent);
await fs.writeFile(path.join(templatesDir, 'only.md'), '# Only\n');
// Create a change with the artifact present
const changeDir = path.join(changesDir, 'no-apply-full-test');
await fs.mkdir(changeDir, { recursive: true });
await fs.writeFile(path.join(changeDir, 'only.md'), '# Content');
const result = await runCLI(
['instructions', 'apply', '--change', 'no-apply-full-test', '--schema', 'no-apply-full', '--json'],
{
cwd: tempDir,
env: { XDG_DATA_HOME: userDataDir },
}
);
expect(result.exitCode).toBe(0);
const json = JSON.parse(result.stdout);
// All artifacts exist, should be ready with default instruction
expect(json.schemaName).toBe('no-apply-full');
expect(json.state).toBe('ready');
expect(json.instruction).toContain('All required artifacts complete');
});
});
describe('help text', () => {
it('status command help shows description', async () => {
const result = await runCLI(['status', '--help']);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Display artifact completion status');
});
it('instructions command help shows description', async () => {
const result = await runCLI(['instructions', '--help']);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Output enriched instructions');
});
it('templates command help shows description', async () => {
const result = await runCLI(['templates', '--help']);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Show resolved template paths');
});
it('new command help shows description', async () => {
const result = await runCLI(['new', '--help']);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Create new items');
});
});
describe('experimental command (deprecated alias for init)', () => {
it('shows deprecation notice', async () => {
const result = await runCLI(['experimental', '--tool', 'claude'], { cwd: tempDir });
// May succeed or fail depending on setup, but should show deprecation notice
const output = getOutput(result);
expect(output).toContain('deprecated');
});
it('errors for unknown tool', async () => {
const result = await runCLI(['experimental', '--tool', 'unknown-tool'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(1);
const output = getOutput(result);
expect(output).toContain('Invalid tool(s): unknown-tool');
});
it('errors for tool without skillsDir', async () => {
// Using 'agents' which doesn't have skillsDir configured
const result = await runCLI(['experimental', '--tool', 'agents'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(1);
const output = getOutput(result);
expect(output).toContain('Invalid tool(s): agents');
});
it('creates skills for Claude tool', async () => {
const result = await runCLI(['experimental', '--tool', 'claude'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(0);
const output = normalizePaths(getOutput(result));
expect(output).toContain('Claude Code');
expect(output).toContain('.claude/');
// Verify skill files were created
const skillFile = path.join(tempDir, '.claude', 'skills', 'openspec-explore', 'SKILL.md');
const stat = await fs.stat(skillFile);
expect(stat.isFile()).toBe(true);
});
it('creates skills for Cursor tool', async () => {
const result = await runCLI(['experimental', '--tool', 'cursor'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(0);
const output = normalizePaths(getOutput(result));
expect(output).toContain('Cursor');
expect(output).toContain('.cursor/');
// Verify skill files were created
const skillFile = path.join(tempDir, '.cursor', 'skills', 'openspec-explore', 'SKILL.md');
const stat = await fs.stat(skillFile);
expect(stat.isFile()).toBe(true);
// Verify commands were created with Cursor format
const commandFile = path.join(tempDir, '.cursor', 'commands', 'opsx-explore.md');
const content = await fs.readFile(commandFile, 'utf-8');
expect(content).toContain('name: /opsx:explore');
});
it('creates skills for Windsurf tool', async () => {
const result = await runCLI(['experimental', '--tool', 'windsurf'], {
cwd: tempDir,
});
expect(result.exitCode).toBe(0);
const output = normalizePaths(getOutput(result));
expect(output).toContain('Windsurf');
expect(output).toContain('.windsurf/');
// Verify skill files were created
const skillFile = path.join(tempDir, '.windsurf', 'skills', 'openspec-explore', 'SKILL.md');
const stat = await fs.stat(skillFile);
expect(stat.isFile()).toBe(true);
});
});
describe('project config integration', () => {
describe('new change uses config schema', () => {
it('creates change with schema from project config', async () => {
// Create project config with spec-driven schema
// Note: changesDir is already at tempDir/openspec/changes (created in beforeEach)
await fs.writeFile(
path.join(tempDir, 'openspec', 'config.yaml'),
'schema: spec-driven\n'
);
// Create a new change without specifying schema
const result = await runCLI(['new', 'change', 'test-change'], { cwd: tempDir, timeoutMs: 30000 });
expect(result.exitCode).toBe(0);
// Verify the change was created with spec-driven schema
const metadataPath = path.join(changesDir, 'test-change', '.openspec.yaml');
const metadata = await fs.readFile(metadataPath, 'utf-8');
expect(metadata).toContain('schema: spec-driven');
}, 60000);
it('CLI schema overrides config schema', async () => {
// Create project config with spec-driven schema
// Note: openspec directory already exists (from changesDir creation in beforeEach)
await fs.writeFile(
path.join(tempDir, 'openspec', 'config.yaml'),
'schema: spec-driven\n'
);
// Create change with explicit schema
const result = await runCLI(
['new', 'change', 'override-test', '--schema', 'spec-driven'],
{ cwd: tempDir, timeoutMs: 30000 }
);
expect(result.exitCode).toBe(0);
// Verify the change uses the CLI-specified schema
const metadataPath = path.join(changesDir, 'override-test', '.openspec.yaml');
const metadata = await fs.readFile(metadataPath, 'utf-8');
expect(metadata).toContain('schema: spec-driven');
}, 60000);
});
describe('instructions command with config', () => {
it('injects context and rules from config into instructions', async () => {
// Create project config with context and rules
// Note: openspec directory already exists (from changesDir creation in beforeEach)
await fs.writeFile(
path.join(tempDir, 'openspec', 'config.yaml'),
`schema: spec-driven
context: |
Tech stack: TypeScript, React
API style: RESTful
rules:
proposal:
- Include rollback plan
- Identify affected teams
`
);
// Create a test change
await createTestChange('config-test');
// Get instructions for proposal
const result = await runCLI(
['instructions', 'proposal', '--change', 'config-test'],
{ cwd: tempDir, timeoutMs: 30000 }
);
expect(result.exitCode).toBe(0);
// Verify context is injected
expect(result.stdout).toContain('Tech stack: TypeScript, React');
expect(result.stdout).toContain('API style: RESTful');
// Verify rules are injected for proposal
expect(result.stdout).toContain('Include rollback plan');
expect(result.stdout).toContain('Identify affected teams');
}, 60000);
it('does not inject rules for non-matching artifact', async () => {
// Create project config with rules only for proposal
// Note: openspec directory already exists (from changesDir creation in beforeEach)
await fs.writeFile(
path.join(tempDir, 'openspec', 'config.yaml'),
`schema: spec-driven
rules:
proposal:
- Include rollback plan
`
);
// Create a test change
await createTestChange('non-matching-test');
// Get instructions for design (not proposal)
const result = await runCLI(
['instructions', 'design', '--change', 'non-matching-test'],
{ cwd: tempDir, timeoutMs: 30000 }
);
expect(result.exitCode).toBe(0);
// Verify rules are NOT injected for design
expect(result.stdout).not.toContain('Include rollback plan');
}, 60000);
});
describe('backwards compatibility', () => {
it('existing changes work without config file', async () => {
// Create change without any config file
await createTestChange('no-config-change', ['proposal']);
// Status command should work
const statusResult = await runCLI(
['status', '--change', 'no-config-change'],
{ cwd: tempDir, timeoutMs: 30000 }
);
expect(statusResult.exitCode).toBe(0);
expect(statusResult.stdout).toContain('no-config-change');
expect(statusResult.stdout).toContain('spec-driven'); // Default schema
// Instructions command should work
const instrResult = await runCLI(
['instructions', 'design', '--change', 'no-config-change'],
{ cwd: tempDir, timeoutMs: 30000 }
);
expect(instrResult.exitCode).toBe(0);
expect(instrResult.stdout).toContain('<artifact');
}, 60000);
it('changes with metadata work without config file', async () => {
// Create change with explicit schema in metadata
const changeDir = await createTestChange('metadata-only-change');
await fs.writeFile(
path.join(changeDir, '.openspec.yaml'),
'schema: spec-driven\ncreated: "2025-01-05"\n'
);
// Status should use schema from metadata
const result = await runCLI(
['status', '--change', 'metadata-only-change'],
{ cwd: tempDir, timeoutMs: 30000 }
);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('spec-driven');
}, 60000);
});
describe('config changes reflected immediately', () => {
it('config changes are reflected without restart', async () => {
// Create initial config
// Note: openspec directory already exists (from changesDir creation in beforeEach)
await fs.writeFile(
path.join(tempDir, 'openspec', 'config.yaml'),
`schema: spec-driven
context: Initial context
`
);
// Create a test change
await createTestChange('immediate-test');
// Get instructions - should have initial context
const result1 = await runCLI(
['instructions', 'proposal', '--change', 'immediate-test'],
{ cwd: tempDir, timeoutMs: 30000 }
);
expect(result1.exitCode).toBe(0);
expect(result1.stdout).toContain('Initial context');
// Update config
await fs.writeFile(
path.join(tempDir, 'openspec', 'config.yaml'),
`schema: spec-driven
context: Updated context
`
);
// Get instructions again - should have updated context
const result2 = await runCLI(
['instructions', 'proposal', '--change', 'immediate-test'],
{ cwd: tempDir, timeoutMs: 30000 }
);
expect(result2.exitCode).toBe(0);
expect(result2.stdout).toContain('Updated context');
expect(result2.stdout).not.toContain('Initial context');
}, 60000);
});
});
});