-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Expand file tree
/
Copy pathupdate.test.ts
More file actions
3893 lines (3239 loc) · 151 KB
/
Copy pathupdate.test.ts
File metadata and controls
3893 lines (3239 loc) · 151 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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { UpdateCommand, scanInstalledWorkflows } from '../../src/core/update.js';
import { InitCommand } from '../../src/core/init.js';
import { getConfiguredToolsForProfileSync } from '../../src/core/profile-sync-drift.js';
import { FileSystemUtils } from '../../src/utils/file-system.js';
import { OPENSPEC_MARKERS } from '../../src/core/config.js';
import type { GlobalConfig } from '../../src/core/global-config.js';
import { generateCopilotSetupSteps, persistCopilotCloudOptIn } from '../../src/core/github-copilot/cloud-agent.js';
import path from 'path';
import fs from 'fs/promises';
import os from 'os';
// Shared mutable mock config state
const mockState = {
config: {
featureFlags: {},
profile: 'core' as const,
delivery: 'both' as const,
} as GlobalConfig,
};
// Mock global config module to isolate tests from the machine's actual config
vi.mock('../../src/core/global-config.js', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../src/core/global-config.js')>();
return {
...actual,
getGlobalConfig: () => ({ ...mockState.config }),
saveGlobalConfig: vi.fn(),
};
});
// Helper to set mock config for tests
function setMockConfig(config: GlobalConfig) {
mockState.config = config;
}
function resetMockConfig() {
mockState.config = { featureFlags: {}, profile: 'core', delivery: 'both' };
}
async function markCodexTarget(skillsDir: string): Promise<void> {
await fs.mkdir(skillsDir, { recursive: true });
await fs.writeFile(path.join(skillsDir, '.openspec-target'), 'codex\n');
}
describe('UpdateCommand', () => {
let testDir: string;
let updateCommand: UpdateCommand;
let originalEnv: NodeJS.ProcessEnv;
beforeEach(async () => {
originalEnv = { ...process.env };
// Create a temporary test directory
testDir = await fs.mkdtemp(path.join(os.tmpdir(), 'openspec-test-'));
process.env.CODEX_HOME = path.join(testDir, 'codex-home');
process.env.HOME = path.join(testDir, 'home');
process.env.USERPROFILE = path.join(testDir, 'home');
// Create openspec directory
const openspecDir = path.join(testDir, 'openspec');
await fs.mkdir(openspecDir, { recursive: true });
updateCommand = new UpdateCommand();
// Reset mock config to defaults
resetMockConfig();
// Clear all mocks before each test
vi.restoreAllMocks();
});
afterEach(async () => {
process.env = originalEnv;
// Restore all mocks after each test
vi.restoreAllMocks();
// Clean up test directory
await fs.rm(testDir, { recursive: true, force: true });
});
describe('basic validation', () => {
it('should throw error if openspec directory does not exist', async () => {
// Remove openspec directory
await fs.rm(path.join(testDir, 'openspec'), {
recursive: true,
force: true,
});
await expect(updateCommand.execute(testDir)).rejects.toThrow(
"No OpenSpec directory found. Run 'openspec init' first."
);
});
it('should report no configured tools when none exist', async () => {
const consoleSpy = vi.spyOn(console, 'log');
await updateCommand.execute(testDir);
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('No configured tools found')
);
consoleSpy.mockRestore();
});
it('should remove generated Copilot cloud files when no tools are configured', async () => {
const initCommand = new InitCommand({
tools: 'github-copilot',
force: true,
copilotCloud: true,
});
await initCommand.execute(testDir);
await fs.rm(path.join(testDir, '.github', 'skills'), { recursive: true, force: true });
await fs.rm(path.join(testDir, '.github', 'prompts'), { recursive: true, force: true });
await updateCommand.execute(testDir);
await expect(fs.stat(path.join(testDir, '.github', 'workflows', 'copilot-setup-steps.yml')))
.rejects.toMatchObject({ code: 'ENOENT' });
await expect(fs.stat(path.join(testDir, '.github', 'agents', 'openspec.agent.md')))
.rejects.toMatchObject({ code: 'ENOENT' });
});
});
describe('skill updates', () => {
it('should update skill files for configured Claude tool', async () => {
// Set up a configured Claude tool by creating skill directories
const skillsDir = path.join(testDir, '.claude', 'skills');
const exploreSkillDir = path.join(skillsDir, 'openspec-explore');
await fs.mkdir(exploreSkillDir, { recursive: true });
// Create an existing skill file
const oldSkillContent = `---
name: openspec-explore (old)
description: Old description
license: MIT
compatibility: Requires openspec CLI.
metadata:
author: openspec
version: "0.9"
---
Old instructions content
`;
await fs.writeFile(
path.join(exploreSkillDir, 'SKILL.md'),
oldSkillContent
);
const consoleSpy = vi.spyOn(console, 'log');
await updateCommand.execute(testDir);
// Check skill file was updated
const updatedSkill = await fs.readFile(
path.join(exploreSkillDir, 'SKILL.md'),
'utf-8'
);
expect(updatedSkill).toContain('name: openspec-explore');
expect(updatedSkill).not.toContain('Old instructions content');
expect(updatedSkill).toContain('license: MIT');
// Check console output
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('Updating 1 tool(s): claude')
);
consoleSpy.mockRestore();
});
it('should update MiniMax Code skills without touching unrelated global skills', async () => {
const skillsDir = path.join(testDir, 'home', '.minimax', 'skills');
const exploreSkill = path.join(skillsDir, 'openspec-explore', 'SKILL.md');
const customSkill = path.join(skillsDir, 'my-custom-skill', 'SKILL.md');
await fs.mkdir(path.dirname(exploreSkill), { recursive: true });
await fs.writeFile(exploreSkill, 'old content');
await fs.mkdir(path.dirname(customSkill), { recursive: true });
await fs.writeFile(customSkill, 'custom content');
await updateCommand.execute(testDir);
expect(await fs.readFile(exploreSkill, 'utf-8')).toContain('name: openspec-explore');
expect(await fs.readFile(customSkill, 'utf-8')).toBe('custom content');
expect(await FileSystemUtils.directoryExists(path.join(testDir, '.minimax'))).toBe(false);
expect(await FileSystemUtils.directoryExists(path.join(testDir, '.mavis'))).toBe(false);
});
it('should not update MiniMax skills through a linked directory outside the global skills root', async () => {
const outsideDir = await fs.mkdtemp(path.join(os.tmpdir(), 'openspec-minimax-outside-'));
const skillsRoot = path.join(testDir, 'home', '.minimax', 'skills');
const linkedSkillDir = path.join(skillsRoot, 'openspec-explore');
const skillFile = path.join(outsideDir, 'SKILL.md');
const oldSkillContent = `---
name: openspec-explore
metadata:
author: openspec
version: "0.9"
---
Outside content
`;
await fs.mkdir(skillsRoot, { recursive: true });
await fs.writeFile(skillFile, oldSkillContent);
try {
await fs.symlink(
outsideDir,
linkedSkillDir,
process.platform === 'win32' ? 'junction' : 'dir'
);
await expect(updateCommand.execute(testDir)).rejects.toThrow(
'OpenSpec update failed for: MiniMax Code'
);
expect(await fs.readFile(skillFile, 'utf-8')).toBe(oldSkillContent);
} finally {
await fs.rm(outsideDir, { recursive: true, force: true });
}
});
it('should not delete MiniMax skills through a linked directory outside the global skills root', async () => {
setMockConfig({
featureFlags: {},
profile: 'custom',
workflows: ['propose'],
delivery: 'skills',
});
const outsideDir = await fs.mkdtemp(path.join(os.tmpdir(), 'openspec-minimax-outside-'));
const skillsRoot = path.join(testDir, 'home', '.minimax', 'skills');
const linkedSkillDir = path.join(skillsRoot, 'openspec-explore');
const skillFile = path.join(outsideDir, 'SKILL.md');
const oldSkillContent = `---
name: openspec-explore
metadata:
author: openspec
version: "0.9"
---
Outside content
`;
await fs.mkdir(skillsRoot, { recursive: true });
await fs.writeFile(skillFile, oldSkillContent);
try {
await fs.symlink(
outsideDir,
linkedSkillDir,
process.platform === 'win32' ? 'junction' : 'dir'
);
await expect(updateCommand.execute(testDir)).rejects.toThrow(
'OpenSpec update failed for: MiniMax Code'
);
expect(await fs.readFile(skillFile, 'utf-8')).toBe(oldSkillContent);
} finally {
await fs.rm(outsideDir, { recursive: true, force: true });
}
});
it('should not update generated artifacts through a linked tool directory outside the project', async () => {
const outsideDir = await fs.mkdtemp(path.join(os.tmpdir(), 'openspec-update-outside-'));
const skillFile = path.join(
outsideDir,
'skills',
'openspec-explore',
'SKILL.md'
);
const oldSkillContent = `---
name: openspec-explore
metadata:
author: openspec
version: "0.9"
---
Outside content
`;
await fs.mkdir(path.dirname(skillFile), { recursive: true });
await fs.writeFile(skillFile, oldSkillContent);
try {
await fs.symlink(
outsideDir,
path.join(testDir, '.claude'),
process.platform === 'win32' ? 'junction' : 'dir'
);
await expect(updateCommand.execute(testDir)).rejects.toThrow(
'OpenSpec update failed for: Claude Code'
);
expect(await fs.readFile(skillFile, 'utf-8')).toBe(oldSkillContent);
expect(await fs.readdir(path.join(outsideDir, 'skills'))).toEqual([
'openspec-explore',
]);
} finally {
await fs.rm(outsideDir, { recursive: true, force: true });
}
});
it('should not delete generated artifacts through a linked tool directory outside the project', async () => {
setMockConfig({ featureFlags: {}, profile: 'core', delivery: 'commands' });
const outsideDir = await fs.mkdtemp(path.join(os.tmpdir(), 'openspec-update-outside-'));
const skillFile = path.join(
outsideDir,
'skills',
'openspec-explore',
'SKILL.md'
);
await fs.mkdir(path.dirname(skillFile), { recursive: true });
await fs.writeFile(
skillFile,
`---
name: openspec-explore
metadata:
author: openspec
version: "0.9"
---
`
);
try {
await fs.symlink(
outsideDir,
path.join(testDir, '.claude'),
process.platform === 'win32' ? 'junction' : 'dir'
);
await expect(updateCommand.execute(testDir)).rejects.toThrow(
'OpenSpec update failed for: Claude Code'
);
await expect(fs.stat(skillFile)).resolves.toBeDefined();
} finally {
await fs.rm(outsideDir, { recursive: true, force: true });
}
});
it('should show the Hermes setup note when updating a configured Hermes tool', async () => {
const exploreSkillDir = path.join(testDir, '.hermes', 'skills', 'openspec-explore');
await fs.mkdir(exploreSkillDir, { recursive: true });
await fs.writeFile(
path.join(exploreSkillDir, 'SKILL.md'),
`---\nname: openspec-explore\nmetadata:\n author: openspec\n version: "0.9"\n---\n\nOld instructions content\n`
);
const consoleSpy = vi.spyOn(console, 'log');
await updateCommand.execute(testDir);
const logCalls = consoleSpy.mock.calls.flat().map(String);
expect(
logCalls.some(
(entry) => entry.includes('Setup required for Hermes Agent') && entry.includes('skills.external_dirs'),
),
).toBe(true);
consoleSpy.mockRestore();
});
it('should show the Hermes setup note even when Hermes is already up to date', async () => {
const initCommand = new InitCommand({ tools: 'hermes', force: true });
await initCommand.execute(testDir);
const consoleSpy = vi.spyOn(console, 'log');
await updateCommand.execute(testDir);
const logCalls = consoleSpy.mock.calls.flat().map(String);
expect(logCalls.some((entry) => entry.includes('up to date'))).toBe(true);
expect(
logCalls.some(
(entry) => entry.includes('Setup required for Hermes Agent') && entry.includes('skills.external_dirs'),
),
).toBe(true);
consoleSpy.mockRestore();
});
it('should migrate OpenSpec skills from legacy .kimi to .kimi-code, preserving user files', async () => {
// Managed skill in the legacy Kimi CLI location
const legacySkillDir = path.join(testDir, '.kimi', 'skills', 'openspec-explore');
await fs.mkdir(legacySkillDir, { recursive: true });
await fs.writeFile(
path.join(legacySkillDir, 'SKILL.md'),
`---\nname: openspec-explore\nmetadata:\n author: openspec\n version: "0.9"\n---\n\nOld instructions content\n`
);
// User-owned files in the legacy location that must be preserved
const userSkillDir = path.join(testDir, '.kimi', 'skills', 'my-custom-skill');
await fs.mkdir(userSkillDir, { recursive: true });
await fs.writeFile(path.join(userSkillDir, 'SKILL.md'), 'user skill');
await fs.writeFile(path.join(testDir, '.kimi', 'config.toml'), 'user config');
const consoleSpy = vi.spyOn(console, 'log');
await updateCommand.execute(testDir);
// Managed skill migrated to .kimi-code and refreshed by the update
const migratedSkill = await fs.readFile(
path.join(testDir, '.kimi-code', 'skills', 'openspec-explore', 'SKILL.md'),
'utf-8'
);
expect(migratedSkill).toContain('name: openspec-explore');
expect(migratedSkill).not.toContain('Old instructions content');
// Kimi Code has no command adapter, so the refreshed skill must use
// its documented /skill:<name> invocations, never /opsx:* commands
// that were not generated
expect(migratedSkill).not.toContain('/opsx:');
expect(migratedSkill).not.toContain('/opsx-');
expect(migratedSkill).toContain('/skill:openspec-');
// Legacy managed skill is gone; user files stay where they were
await expect(fs.access(legacySkillDir)).rejects.toThrow();
expect(await fs.readFile(path.join(userSkillDir, 'SKILL.md'), 'utf-8')).toBe('user skill');
expect(await fs.readFile(path.join(testDir, '.kimi', 'config.toml'), 'utf-8')).toBe('user config');
const logCalls = consoleSpy.mock.calls.flat().map(String);
expect(logCalls.some((entry) => entry.includes('.kimi → .kimi-code'))).toBe(true);
consoleSpy.mockRestore();
});
it('should remove the legacy .kimi directory entirely when it only held OpenSpec skills', async () => {
const legacySkillDir = path.join(testDir, '.kimi', 'skills', 'openspec-explore');
await fs.mkdir(legacySkillDir, { recursive: true });
await fs.writeFile(
path.join(legacySkillDir, 'SKILL.md'),
`---\nname: openspec-explore\nmetadata:\n author: openspec\n version: "0.9"\n---\n\nOld instructions content\n`
);
await updateCommand.execute(testDir);
await expect(fs.access(path.join(testDir, '.kimi'))).rejects.toThrow();
const migratedSkill = path.join(testDir, '.kimi-code', 'skills', 'openspec-explore', 'SKILL.md');
await expect(fs.access(migratedSkill)).resolves.toBeUndefined();
});
it('should migrate legacy Codex skills after writing replacements and preserve user files', async () => {
await new InitCommand({ tools: 'codex', force: true }).execute(testDir);
await fs.rename(path.join(testDir, '.agents'), path.join(testDir, '.codex'));
await fs.rm(path.join(testDir, '.codex', 'skills', '.openspec-target'));
const userSkill = path.join(testDir, '.codex', 'skills', 'my-custom-skill', 'SKILL.md');
await fs.mkdir(path.dirname(userSkill), { recursive: true });
await fs.writeFile(userSkill, 'user skill');
await fs.writeFile(path.join(testDir, '.codex', 'config.toml'), 'user config');
const consoleSpy = vi.spyOn(console, 'log');
await updateCommand.execute(testDir);
const currentSkill = path.join(
testDir,
'.agents',
'skills',
'openspec-propose',
'SKILL.md'
);
expect(await fs.readFile(currentSkill, 'utf-8')).toContain('$openspec-apply-change');
await expect(
fs.access(path.join(testDir, '.codex', 'skills', 'openspec-propose', 'SKILL.md'))
).rejects.toThrow();
expect(await fs.readFile(userSkill, 'utf-8')).toBe('user skill');
expect(await fs.readFile(path.join(testDir, '.codex', 'config.toml'), 'utf-8')).toBe(
'user config'
);
expect(
consoleSpy.mock.calls.flat().map(String).some((entry) =>
entry.includes('.codex → .agents')
)
).toBe(true);
});
it('should retry interrupted equivalent Codex cleanup without force', async () => {
await new InitCommand({ tools: 'codex', force: true }).execute(testDir);
const canonicalSkills = path.join(testDir, '.agents', 'skills');
const legacySkills = path.join(testDir, '.codex', 'skills');
await fs.cp(canonicalSkills, legacySkills, { recursive: true });
await fs.rm(path.join(legacySkills, '.openspec-target'));
for (const entry of await fs.readdir(legacySkills, { withFileTypes: true })) {
if (!entry.isDirectory() || !entry.name.startsWith('openspec-')) continue;
const skillFile = path.join(legacySkills, entry.name, 'SKILL.md');
const legacyContent = (await fs.readFile(skillFile, 'utf-8'))
.replace(
/\$openspec-([a-z0-9-]+) \(Codex\) or \/openspec-\1 \(other agents\)/g,
'$openspec-$1'
)
.replace(/generatedBy:\s*"[^"]+"/, 'generatedBy: "0.1.0"')
.replace(/\n/g, '\r\n');
await fs.writeFile(skillFile, `\uFEFF${legacyContent}`);
}
await updateCommand.execute(testDir);
await expect(
fs.access(path.join(legacySkills, 'openspec-propose', 'SKILL.md'))
).rejects.toThrow();
expect(await fs.readFile(
path.join(canonicalSkills, 'openspec-propose', 'SKILL.md'),
'utf-8'
)).toContain('$openspec-apply-change');
});
it('should preserve and report a divergent legacy Codex skill', async () => {
await new InitCommand({ tools: 'codex', force: true }).execute(testDir);
await fs.rename(path.join(testDir, '.agents'), path.join(testDir, '.codex'));
await fs.rm(path.join(testDir, '.codex', 'skills', '.openspec-target'));
const legacySkill = path.join(
testDir,
'.codex',
'skills',
'openspec-propose',
'SKILL.md'
);
await fs.appendFile(legacySkill, '\nUser edit\n');
const consoleSpy = vi.spyOn(console, 'log');
await updateCommand.execute(testDir);
expect(await fs.readFile(legacySkill, 'utf-8')).toContain('User edit');
expect(
consoleSpy.mock.calls.flat().map(String).some((entry) =>
entry.includes('Left 1 file in .codex/')
)
).toBe(true);
consoleSpy.mockClear();
await updateCommand.execute(testDir);
const secondRunLogs = consoleSpy.mock.calls.flat().map(String);
expect(secondRunLogs.some((entry) => entry.includes('up to date'))).toBe(true);
expect(secondRunLogs.some((entry) => entry.includes('Left 1 file in .codex/'))).toBe(false);
});
it('should not restore legacy Codex workflows excluded by the active profile', async () => {
await new InitCommand({ tools: 'codex', force: true }).execute(testDir);
await fs.rename(path.join(testDir, '.agents'), path.join(testDir, '.codex'));
await fs.rm(path.join(testDir, '.codex', 'skills', '.openspec-target'));
setMockConfig({
featureFlags: {},
profile: 'custom',
delivery: 'skills',
workflows: ['explore'],
});
await updateCommand.execute(testDir);
expect(
await FileSystemUtils.fileExists(
path.join(testDir, '.agents', 'skills', 'openspec-explore', 'SKILL.md')
)
).toBe(true);
expect(
await FileSystemUtils.fileExists(
path.join(testDir, '.agents', 'skills', 'openspec-apply-change', 'SKILL.md')
)
).toBe(false);
expect(
await FileSystemUtils.fileExists(
path.join(testDir, '.codex', 'skills', 'openspec-apply-change', 'SKILL.md')
)
).toBe(true);
const consoleSpy = vi.spyOn(console, 'log');
await updateCommand.execute(testDir);
expect(
consoleSpy.mock.calls.flat().map(String).some((entry) => entry.includes('up to date'))
).toBe(true);
});
it('should keep Codex as the sole writer of its marked shared skill tree', async () => {
await new InitCommand({ tools: 'codex,agents', force: true }).execute(testDir);
const consoleSpy = vi.spyOn(console, 'log');
await new UpdateCommand({ force: true }).execute(testDir);
const proposeSkill = await fs.readFile(
path.join(testDir, '.agents', 'skills', 'openspec-propose', 'SKILL.md'),
'utf-8'
);
expect(proposeSkill).toContain('$openspec-apply-change');
expect(proposeSkill).toContain('/openspec-apply-change');
expect(
consoleSpy.mock.calls.flat().map(String).some((entry) =>
entry.includes('Force updating 1 tool(s): codex')
)
).toBe(true);
});
it('should refresh Antigravity workflows without rewriting Codex-owned shared skills', async () => {
await new InitCommand({ tools: 'antigravity,codex', force: true }).execute(testDir);
await new UpdateCommand({ force: true }).execute(testDir);
const skillsDir = path.join(testDir, '.agents', 'skills');
expect(await fs.readFile(path.join(skillsDir, '.openspec-target'), 'utf-8')).toBe('codex\n');
const proposeSkill = await fs.readFile(
path.join(skillsDir, 'openspec-propose', 'SKILL.md'),
'utf-8'
);
expect(proposeSkill).toContain('$openspec-apply-change');
expect(proposeSkill).toContain('/openspec-apply-change');
await expect(
fs.access(path.join(testDir, '.agents', 'workflows', 'opsx-propose.md'))
).resolves.toBeUndefined();
expect(getConfiguredToolsForProfileSync(testDir)).toEqual([
'antigravity',
'codex',
]);
});
it('should upgrade legacy Antigravity workflows beside Codex-owned shared skills', async () => {
await new InitCommand({ tools: 'antigravity', force: true }).execute(testDir);
const legacyWorkflow = path.join(testDir, '.agent', 'workflows', 'opsx-propose.md');
await fs.mkdir(path.dirname(legacyWorkflow), { recursive: true });
await fs.copyFile(
path.join(testDir, '.agents', 'workflows', 'opsx-propose.md'),
legacyWorkflow
);
await fs.cp(
path.join(testDir, '.agents', 'skills'),
path.join(testDir, '.agent', 'skills'),
{ recursive: true }
);
await fs.rm(path.join(testDir, '.agents', 'workflows'), { recursive: true });
await new InitCommand({ tools: 'codex', force: true }).execute(testDir);
await new UpdateCommand().execute(testDir);
const skillsDir = path.join(testDir, '.agents', 'skills');
expect(await fs.readFile(path.join(skillsDir, '.openspec-target'), 'utf-8')).toBe('codex\n');
expect(
await fs.readFile(path.join(skillsDir, 'openspec-propose', 'SKILL.md'), 'utf-8')
).toContain('$openspec-apply-change');
await expect(
fs.access(path.join(testDir, '.agents', 'workflows', 'opsx-propose.md'))
).resolves.toBeUndefined();
await expect(fs.access(legacyWorkflow)).rejects.toThrow();
});
it('should keep an explicit agents target despite preserved legacy Codex skills', async () => {
await new InitCommand({ tools: 'codex', force: true }).execute(testDir);
await fs.rename(path.join(testDir, '.agents'), path.join(testDir, '.codex'));
await fs.rm(path.join(testDir, '.codex', 'skills', '.openspec-target'));
await fs.appendFile(
path.join(testDir, '.codex', 'skills', 'openspec-propose', 'SKILL.md'),
'\nUser edit\n'
);
await new InitCommand({ tools: 'agents', force: true }).execute(testDir);
await updateCommand.execute(testDir);
const skillsDir = path.join(testDir, '.agents', 'skills');
expect(await fs.readFile(path.join(skillsDir, '.openspec-target'), 'utf-8')).toBe('agents\n');
expect(
await fs.readFile(path.join(skillsDir, 'openspec-propose', 'SKILL.md'), 'utf-8')
).toContain('/openspec-apply-change');
expect(
await fs.readFile(
path.join(testDir, '.codex', 'skills', 'openspec-propose', 'SKILL.md'),
'utf-8'
)
).toContain('User edit');
});
it('does not let a legacy Codex global prompt hijack an established agents target', async () => {
// Regression for the hijack this PR fixes: the guard must actually be
// invoked by the update flow, not merely be correct in isolation.
setMockConfig({ featureFlags: {}, profile: 'core', delivery: 'skills' });
// The vendor-neutral `agents` target owns `.agents` (marker + generic skills).
await new InitCommand({ tools: 'agents', force: true }).execute(testDir);
// A leftover global Codex install, detected only from `~/.codex/prompts`.
const promptDir = path.join(process.env.CODEX_HOME!, 'prompts');
const globalPrompt = path.join(promptDir, 'opsx-explore.md');
await fs.mkdir(promptDir, { recursive: true });
await fs.writeFile(globalPrompt, 'legacy explore prompt');
// The skip message is emitted via an ora spinner, which writes to the
// process streams rather than through console.log. Restore the spies in a
// finally so a throw can never swallow stdout for the rest of the suite.
let streamOutput = '';
const capture = (chunk: unknown) => {
streamOutput += String(chunk);
return true;
};
const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(capture as never);
const stderrSpy = vi.spyOn(process.stderr, 'write').mockImplementation(capture as never);
try {
await new UpdateCommand({ force: true }).execute(testDir);
} finally {
stdoutSpy.mockRestore();
stderrSpy.mockRestore();
}
const skillsDir = path.join(testDir, '.agents', 'skills');
// Ownership marker is not flipped to codex...
expect(await fs.readFile(path.join(skillsDir, '.openspec-target'), 'utf-8')).toBe('agents\n');
// ...and the tree keeps generic `/openspec-` syntax, never Codex `$openspec-`.
const propose = await fs.readFile(
path.join(skillsDir, 'openspec-propose', 'SKILL.md'),
'utf-8'
);
expect(propose).not.toContain('$openspec-');
expect(propose).toContain('/openspec-');
// Generation AND configuration are skipped: Codex is never recorded as a
// configured tool, so a stray global prompt cannot flip ownership later.
const configured = getConfiguredToolsForProfileSync(testDir);
expect(configured).toContain('agents');
expect(configured).not.toContain('codex');
// The skip names the established owner so the user understands why.
expect(streamOutput).toMatch(/Skipped Codex/);
expect(streamOutput).toMatch(/managed by another tool \(Shared \.agents skills\)/);
// The legacy signal must survive: because Codex was skipped, no
// replacement skill exists, so the deferred global-prompt cleanup must
// preserve `~/.codex/prompts` untouched (byte-for-byte) rather than
// delete it — otherwise the skip could never re-offer Codex later.
expect(await FileSystemUtils.fileExists(globalPrompt)).toBe(true);
expect(await fs.readFile(globalPrompt, 'utf-8')).toBe('legacy explore prompt');
});
it('lets a first-time legacy Codex upgrade claim an unowned agents root', async () => {
// Inverse of the hijack guard: with no `.agents` tree yet, nothing is
// owned, so the real update path must still generate Codex skills and
// stamp the `codex` marker — proving the guard is not over-broad.
setMockConfig({ featureFlags: {}, profile: 'core', delivery: 'skills' });
const promptDir = path.join(process.env.CODEX_HOME!, 'prompts');
await fs.mkdir(promptDir, { recursive: true });
await fs.writeFile(path.join(promptDir, 'opsx-explore.md'), 'legacy explore prompt');
await new UpdateCommand({ force: true }).execute(testDir);
const skillsDir = path.join(testDir, '.agents', 'skills');
// The codex marker is written (writeSharedSkillTarget on the non-owned path).
expect(await fs.readFile(path.join(skillsDir, '.openspec-target'), 'utf-8')).toBe('codex\n');
// A single opsx-explore prompt infers only the `explore` workflow, and the
// generated skill carries Codex `$openspec-` syntax.
const explore = await fs.readFile(
path.join(skillsDir, 'openspec-explore', 'SKILL.md'),
'utf-8'
);
expect(explore).toContain('$openspec-');
// Codex is now recorded as configured (mirrors the negative check above).
expect(getConfiguredToolsForProfileSync(testDir)).toContain('codex');
});
it('preserves a skipped tool\'s repo-local legacy prompts instead of deleting them', async () => {
// When the guard skips Codex (agents owns `.agents`), no replacement skill
// is written — so Codex's repo-local `.codex/prompts` must NOT be cleaned
// up. Deleting them would strip the legacy signal with nothing in its place.
setMockConfig({ featureFlags: {}, profile: 'core', delivery: 'skills' });
await new InitCommand({ tools: 'agents', force: true }).execute(testDir);
const legacyPrompts = path.join(testDir, '.codex', 'prompts');
await fs.mkdir(legacyPrompts, { recursive: true });
await fs.writeFile(path.join(legacyPrompts, 'openspec-explore.md'), 'legacy repo-local prompt');
await new UpdateCommand({ force: true }).execute(testDir);
// agents tree preserved, and the repo-local legacy prompt survives
// byte-for-byte — asserting content, not mere existence, distinguishes
// "left untouched" from "deleted then rewritten".
expect(
await fs.readFile(path.join(testDir, '.agents', 'skills', '.openspec-target'), 'utf-8')
).toBe('agents\n');
const preservedPrompt = path.join(legacyPrompts, 'openspec-explore.md');
expect(await FileSystemUtils.fileExists(preservedPrompt)).toBe(true);
expect(await fs.readFile(preservedPrompt, 'utf-8')).toBe('legacy repo-local prompt');
});
it('should let an explicit Codex init take ownership of an agents tree', async () => {
await new InitCommand({ tools: 'agents', force: true }).execute(testDir);
await new InitCommand({ tools: 'codex', force: true }).execute(testDir);
const skillsDir = path.join(testDir, '.agents', 'skills');
expect(await fs.readFile(path.join(skillsDir, '.openspec-target'), 'utf-8')).toBe('codex\n');
const proposeSkill = await fs.readFile(
path.join(skillsDir, 'openspec-propose', 'SKILL.md'),
'utf-8'
);
expect(proposeSkill).toContain('$openspec-apply-change');
expect(proposeSkill).toContain('/openspec-apply-change');
});
it('should consolidate an existing unmarked agents tree with legacy Codex skills', async () => {
await new InitCommand({ tools: 'codex', force: true }).execute(testDir);
await fs.rename(path.join(testDir, '.agents'), path.join(testDir, '.codex'));
await fs.rm(path.join(testDir, '.codex', 'skills', '.openspec-target'));
await new InitCommand({ tools: 'agents', force: true }).execute(testDir);
await fs.rm(path.join(testDir, '.agents', 'skills', '.openspec-target'));
const legacyPropose = path.join(
testDir,
'.codex',
'skills',
'openspec-propose',
'SKILL.md'
);
await fs.writeFile(
legacyPropose,
(await fs.readFile(legacyPropose, 'utf-8')).replace(
/generatedBy:\s*"[^"]+"/,
'generatedBy: "0.1.0"'
)
);
await new UpdateCommand({ force: true }).execute(testDir);
const skillsDir = path.join(testDir, '.agents', 'skills');
expect(await fs.readFile(path.join(skillsDir, '.openspec-target'), 'utf-8')).toBe('codex\n');
const proposeSkill = await fs.readFile(
path.join(skillsDir, 'openspec-propose', 'SKILL.md'),
'utf-8'
);
expect(proposeSkill).toContain('$openspec-apply-change');
expect(proposeSkill).toContain('/openspec-apply-change');
await expect(
fs.access(path.join(testDir, '.codex', 'skills', 'openspec-propose', 'SKILL.md'))
).rejects.toThrow();
});
it('should infer an unmarked canonical Codex tree that was moved manually', async () => {
await new InitCommand({ tools: 'codex', force: true }).execute(testDir);
const skillsDir = path.join(testDir, '.agents', 'skills');
await fs.rm(path.join(skillsDir, '.openspec-target'));
await new UpdateCommand({ force: true }).execute(testDir);
expect(await fs.readFile(path.join(skillsDir, '.openspec-target'), 'utf-8')).toBe('codex\n');
const proposeSkill = await fs.readFile(
path.join(skillsDir, 'openspec-propose', 'SKILL.md'),
'utf-8'
);
expect(proposeSkill).toContain('$openspec-apply-change');
expect(proposeSkill).toContain('/openspec-apply-change');
});
it('should preserve agents ownership when it switches to commands-only', async () => {
await new InitCommand({ tools: 'codex', force: true }).execute(testDir);
setMockConfig({ featureFlags: {}, profile: 'core', delivery: 'commands' });
await new InitCommand({ tools: 'agents', force: true }).execute(testDir);
const skillsDir = path.join(testDir, '.agents', 'skills');
expect(await fs.readFile(path.join(skillsDir, '.openspec-target'), 'utf-8')).toBe('agents\n');
await expect(
fs.access(path.join(skillsDir, 'openspec-propose', 'SKILL.md'))
).rejects.toThrow();
});
it('should not resurrect divergent legacy Codex skills after agents switches to commands-only', async () => {
await new InitCommand({ tools: 'agents', force: true }).execute(testDir);
const canonicalSkills = path.join(testDir, '.agents', 'skills');
const legacySkills = path.join(testDir, '.codex', 'skills');
await fs.cp(canonicalSkills, legacySkills, { recursive: true });
await fs.writeFile(
path.join(legacySkills, 'openspec-propose', 'SKILL.md'),
'divergent legacy Codex skill\n'
);
setMockConfig({ featureFlags: {}, profile: 'core', delivery: 'commands' });
await updateCommand.execute(testDir);
await updateCommand.execute(testDir);
expect(await fs.readFile(path.join(canonicalSkills, '.openspec-target'), 'utf-8')).toBe(
'agents\n'
);
await expect(
fs.access(path.join(canonicalSkills, 'openspec-propose', 'SKILL.md'))
).rejects.toThrow();
expect(
await fs.readFile(path.join(legacySkills, 'openspec-propose', 'SKILL.md'), 'utf-8')
).toBe('divergent legacy Codex skill\n');
});
it('should migrate legacy Codex skills under commands-only delivery', async () => {
await new InitCommand({ tools: 'codex', force: true }).execute(testDir);
await fs.rename(path.join(testDir, '.agents'), path.join(testDir, '.codex'));
await fs.rm(path.join(testDir, '.codex', 'skills', '.openspec-target'));
setMockConfig({ featureFlags: {}, profile: 'core', delivery: 'commands' });
await updateCommand.execute(testDir);
const skillsDir = path.join(testDir, '.agents', 'skills');
expect(await fs.readFile(path.join(skillsDir, '.openspec-target'), 'utf-8')).toBe('codex\n');
const proposeSkill = await fs.readFile(
path.join(skillsDir, 'openspec-propose', 'SKILL.md'),
'utf-8'
);
expect(proposeSkill).toContain('$openspec-apply-change');
expect(proposeSkill).toContain('/openspec-apply-change');
await expect(
fs.access(path.join(testDir, '.codex', 'skills', 'openspec-propose', 'SKILL.md'))
).rejects.toThrow();
});
it('should not migrate legacy Codex skills through a symlink outside the project', async () => {
const outsideDir = await fs.mkdtemp(path.join(os.tmpdir(), 'openspec-codex-outside-'));
try {
await new InitCommand({ tools: 'codex', force: true }).execute(testDir);
const outsideSkill = path.join(
outsideDir,
'skills',
'openspec-propose',
'SKILL.md'
);
await fs.mkdir(path.dirname(outsideSkill), { recursive: true });
await fs.copyFile(
path.join(testDir, '.agents', 'skills', 'openspec-propose', 'SKILL.md'),
outsideSkill
);
await fs.symlink(
outsideDir,
path.join(testDir, '.codex'),
process.platform === 'win32' ? 'junction' : 'dir'
);
const warningSpy = vi.spyOn(console, 'warn');
await new UpdateCommand({ force: true }).execute(testDir);
await expect(fs.readFile(outsideSkill, 'utf-8')).resolves.toContain(
'name: openspec-propose'
);
expect(
warningSpy.mock.calls.flat().map(String).some((entry) =>
entry.includes('resolves outside this project')
)
).toBe(true);
} finally {
await fs.rm(outsideDir, { recursive: true, force: true });
}
});
it('should not migrate a nested legacy Codex skill symlink outside the project', async () => {
const outsideDir = await fs.mkdtemp(
path.join(os.tmpdir(), 'openspec-codex-skill-outside-')
);
try {
await new InitCommand({ tools: 'codex', force: true }).execute(testDir);
const outsideSkill = path.join(outsideDir, 'SKILL.md');
await fs.copyFile(
path.join(testDir, '.agents', 'skills', 'openspec-propose', 'SKILL.md'),
outsideSkill
);
const legacySkillsDir = path.join(testDir, '.codex', 'skills');
await fs.mkdir(legacySkillsDir, { recursive: true });
await fs.symlink(
outsideDir,
path.join(legacySkillsDir, 'openspec-propose'),
process.platform === 'win32' ? 'junction' : 'dir'
);
const warningSpy = vi.spyOn(console, 'warn');
await new UpdateCommand({ force: true }).execute(testDir);
await expect(fs.readFile(outsideSkill, 'utf-8')).resolves.toContain(
'name: openspec-propose'
);
expect(
warningSpy.mock.calls.flat().map(String).some((entry) =>
entry.includes('resolves outside this project')
)
).toBe(true);
} finally {
await fs.rm(outsideDir, { recursive: true, force: true });
}
});
it('should update core profile skill files when tool is configured', async () => {
// Set up a configured tool with one skill directory
const skillsDir = path.join(testDir, '.claude', 'skills');
// Create at least one skill to mark tool as configured
await fs.mkdir(path.join(skillsDir, 'openspec-explore'), {
recursive: true,
});
await fs.writeFile(
path.join(skillsDir, 'openspec-explore', 'SKILL.md'),
'old content'
);
await updateCommand.execute(testDir);
// Verify core profile skill files were created/updated (propose, explore, apply, update, sync, archive)
const coreSkillNames = [
'openspec-explore',
'openspec-apply-change',
'openspec-update-change',
'openspec-sync-specs',
'openspec-archive-change',
'openspec-propose',
];
for (const skillName of coreSkillNames) {
const skillFile = path.join(skillsDir, skillName, 'SKILL.md');
const exists = await FileSystemUtils.fileExists(skillFile);
expect(exists).toBe(true);
const content = await fs.readFile(skillFile, 'utf-8');
expect(content).toContain('---');