This repository was archived by the owner on Aug 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskills_test.go
More file actions
3935 lines (3469 loc) · 108 KB
/
Copy pathskills_test.go
File metadata and controls
3935 lines (3469 loc) · 108 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
)
// ── test setup — injectable GitHub fakes ─────────────────────────────
func fakeGitHub() {
fetchLatestCommitFn = func(_, _ string) (string, error) {
return "fakecommit1234567890123456789012345678901234", nil
}
fetchTreeFn = func(_, _ string) (tree []treeEntry, err error) {
// Return all possible test paths so tests don't depend on specific tree matches
return []treeEntry{
{Path: "skills/test/SKILL.md", Mode: "100644", Type: "blob"},
{Path: "skills/test/README.md", Mode: "100644", Type: "blob"},
{Path: "skills/new-path/SKILL.md", Mode: "100644", Type: "blob"},
{Path: "skills/old-path/SKILL.md", Mode: "100644", Type: "blob"},
}, nil
}
downloadFileFn = func(_, _, filePath string) ([]byte, error) {
name := filepath.Base(filePath)
return []byte("# " + name), nil
}
}
func restoreGitHub() {
fetchLatestCommitFn = fetchLatestCommit
fetchTreeFn = fetchTree
downloadFileFn = downloadFile
}
// ── helpers ──────────────────────────────────────────────────────────
func writeFile(t *testing.T, path, content string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
func writeJSON(t *testing.T, path string, v interface{}) {
t.Helper()
data, err := json.MarshalIndent(v, "", " ")
if err != nil {
t.Fatal(err)
}
writeFile(t, path, string(data))
}
func installFakeOMP(t *testing.T, agentDir string) {
t.Helper()
binDir := t.TempDir()
ompPath := filepath.Join(binDir, "omp")
writeFile(t, ompPath, "#!/bin/sh\nprintf '%s\\n' \""+agentDir+"\"\n")
if err := os.Chmod(ompPath, 0o755); err != nil {
t.Fatal(err)
}
pathEnv := binDir
if oldPath := os.Getenv("PATH"); oldPath != "" {
pathEnv += string(os.PathListSeparator) + oldPath
}
t.Setenv("PATH", pathEnv)
}
func captureStdout(t *testing.T, fn func()) string {
t.Helper()
tmpf := filepath.Join(t.TempDir(), "stdout")
old := os.Stdout
f, err := os.Create(tmpf)
if err != nil {
t.Fatal(err)
}
os.Stdout = f
defer func() {
os.Stdout = old
}()
fn()
if err := f.Close(); err != nil {
t.Fatal(err)
}
out, err := os.ReadFile(tmpf)
if err != nil {
t.Fatal(err)
}
return string(out)
}
// ── manifest / lock I/O ──────────────────────────────────────────────
func TestReadManifest(t *testing.T) {
dir := t.TempDir()
mf := filepath.Join(dir, ".manifest.json")
writeJSON(t, mf, Manifest{
Version: 1,
Directories: []DirEntry{
{Name: "shared", Path: "~/.agents/skills"},
},
Skills: []SkillEntry{
{
Name: "test-skill",
Target: "shared",
Source: SourceEntry{Repo: "user/repo", Ref: "main", Path: "skills/test"},
},
},
})
m, err := readManifest(mf)
if err != nil {
t.Fatal(err)
}
if len(m.Skills) != 1 || m.Skills[0].Name != "test-skill" {
t.Fatalf("unexpected manifest: %+v", m)
}
}
func TestReadManifestMissing(t *testing.T) {
_, err := readManifest("/nonexistent/manifest.json")
if err == nil {
t.Fatal("expected error for missing manifest")
}
}
func TestReadLockMissing(t *testing.T) {
l, err := readLock("/nonexistent/lock.json")
if err != nil {
t.Fatal(err)
}
if l.Version != 1 {
t.Fatalf("expected version 1, got %d", l.Version)
}
if l.Skills == nil {
t.Fatal("expected non-nil Skills map")
}
}
func TestWriteLockRoundTrip(t *testing.T) {
dir := t.TempDir()
lf := filepath.Join(dir, ".lock.json")
l := &LockFile{
Version: 1,
Skills: map[string]LockSkill{
"drawio": {Commit: "abc123", Path: "skills/drawio"},
},
}
if err := writeLock(lf, l); err != nil {
t.Fatal(err)
}
raw, err := os.ReadFile(lf)
if err != nil {
t.Fatal(err)
}
if !bytes.HasSuffix(raw, []byte("\n")) {
t.Fatal("lock must end with a trailing newline")
}
l2, err := readLock(lf)
if err != nil {
t.Fatal(err)
}
if l2.Skills["drawio"].Commit != "abc123" {
t.Fatalf("unexpected commit: %s", l2.Skills["drawio"].Commit)
}
}
// ── path helpers ─────────────────────────────────────────────────────
func TestExpandPath(t *testing.T) {
home, _ := os.UserHomeDir()
tests := []struct {
input, expected string
}{
{"~/test", filepath.Join(home, "test")},
{"/abs/path", "/abs/path"},
{"relative/path", "relative/path"},
}
for _, tc := range tests {
got := expandPath(tc.input)
if got != tc.expected {
t.Errorf("expandPath(%q) = %q, want %q", tc.input, got, tc.expected)
}
}
}
func TestResolveTargetPath(t *testing.T) {
dirs := []DirEntry{
{Name: "shared", Path: "~/.agents/skills"},
{Name: "codex", Path: "~/.codex/skills"},
}
home, _ := os.UserHomeDir()
if got := resolveTargetPath("shared", dirs); got != filepath.Join(home, ".agents", "skills") {
t.Errorf("shared = %q", got)
}
if got := resolveTargetPath("codex", dirs); got != filepath.Join(home, ".codex", "skills") {
t.Errorf("codex = %q", got)
}
if got := resolveTargetPath("nonexistent", dirs); got != "" {
t.Errorf("nonexistent = %q, want empty", got)
}
t.Run("omp via omp config path", func(t *testing.T) {
agentDir := filepath.Join(t.TempDir(), "profile-agent")
installFakeOMP(t, agentDir)
if got := resolveTargetPath("omp", dirs); got != filepath.Join(agentDir, "skills") {
t.Fatalf("omp = %q, want %q", got, filepath.Join(agentDir, "skills"))
}
})
t.Run("omp fallback honors PI_CONFIG_DIR", func(t *testing.T) {
t.Setenv("PATH", t.TempDir())
t.Setenv("PI_CONFIG_DIR", ".config/omp")
if got := resolveTargetPath("omp", dirs); got != filepath.Join(home, ".config", "omp", "agent", "skills") {
t.Fatalf("omp fallback = %q", got)
}
})
}
// ── applySymlinks safety ─────────────────────────────────────────────
func TestApplySymlinks_RealDirNotDeleted(t *testing.T) {
dir := t.TempDir()
from := filepath.Join(dir, "target")
to := filepath.Join(dir, "source")
// Create a real directory at "from"
if err := os.MkdirAll(from, 0o755); err != nil {
t.Fatal(err)
}
writeFile(t, filepath.Join(from, "KEEP"), "important data")
m := &Manifest{
Symlinks: []SymlinkEntry{
{From: from, To: to},
},
}
applySymlinks(m)
// Real directory should still exist
if _, err := os.Stat(filepath.Join(from, "KEEP")); err != nil {
t.Fatal("real directory was deleted!")
}
}
func TestApplySymlinks_WrongSymlinkReplaced(t *testing.T) {
dir := t.TempDir()
from := filepath.Join(dir, "target")
to1 := filepath.Join(dir, "source1")
to2 := filepath.Join(dir, "source2")
// Create source dirs
os.MkdirAll(to1, 0o755)
os.MkdirAll(to2, 0o755)
// Create wrong symlink
if err := os.Symlink(to1, from); err != nil {
t.Fatal(err)
}
m := &Manifest{
Symlinks: []SymlinkEntry{
{From: from, To: to2},
},
}
applySymlinks(m)
// Should now point to to2
existing, err := os.Readlink(from)
if err != nil {
t.Fatal(err)
}
if existing != to2 {
t.Fatalf("expected symlink to %q, got %q", to2, existing)
}
}
func TestApplySymlinks_CorrectSymlinkSkipped(t *testing.T) {
dir := t.TempDir()
from := filepath.Join(dir, "target")
to := filepath.Join(dir, "source")
os.MkdirAll(to, 0o755)
if err := os.Symlink(to, from); err != nil {
t.Fatal(err)
}
m := &Manifest{
Symlinks: []SymlinkEntry{
{From: from, To: to},
},
}
applySymlinks(m)
existing, err := os.Readlink(from)
if err != nil {
t.Fatal(err)
}
if existing != to {
t.Fatalf("symlink changed unexpectedly: %q → %q", to, existing)
}
}
// ── applyMirrors ───────────────────────────────────────────────
func TestApplyMirrors(t *testing.T) {
dir := t.TempDir()
sharedDir := filepath.Join(dir, "shared")
claudeDir := filepath.Join(dir, "claude")
// Create shared skills
for _, name := range []string{"drawio", "docx", "pdf"} {
skillDir := filepath.Join(sharedDir, name)
os.MkdirAll(skillDir, 0o755)
writeFile(t, filepath.Join(skillDir, "SKILL.md"), "# "+name)
}
m := &Manifest{
Directories: []DirEntry{
{Name: "shared", Path: sharedDir},
{Name: "claude", Path: claudeDir},
},
Mirrors: []MirrorEntry{
{From: "shared", To: "claude"},
},
Skills: []SkillEntry{
{Name: "drawio", Target: "shared", Source: SourceEntry{Repo: "a/b", Path: "skills/drawio"}},
{Name: "docx", Target: "shared", Source: SourceEntry{Repo: "a/b", Path: "skills/docx"}},
{Name: "pdf", Target: "shared", Source: SourceEntry{Repo: "a/b", Path: "skills/pdf"}},
},
}
applyMirrors(m)
// Verify symlinks
for _, name := range []string{"drawio", "docx", "pdf"} {
src := filepath.Join(sharedDir, name)
dst := filepath.Join(claudeDir, name)
existing, err := os.Readlink(dst)
if err != nil {
t.Fatalf("symlink %s: %v", name, err)
}
if existing != src {
t.Fatalf("%s: expected %q, got %q", name, src, existing)
}
}
}
func TestApplyMirrors_Exclude(t *testing.T) {
dir := t.TempDir()
sharedDir := filepath.Join(dir, "shared")
claudeDir := filepath.Join(dir, "claude")
// Create shared skills
for _, name := range []string{"drawio", "anysearch"} {
skillDir := filepath.Join(sharedDir, name)
os.MkdirAll(skillDir, 0o755)
writeFile(t, filepath.Join(skillDir, "SKILL.md"), "# "+name)
}
m := &Manifest{
Directories: []DirEntry{
{Name: "shared", Path: sharedDir},
{Name: "claude", Path: claudeDir},
},
Mirrors: []MirrorEntry{
{From: "shared", To: "claude", Exclude: []string{"anysearch"}},
},
Skills: []SkillEntry{
{Name: "drawio", Target: "shared", Source: SourceEntry{Repo: "a/b", Path: "skills/drawio"}},
{Name: "anysearch", Target: "shared", Source: SourceEntry{Repo: "a/b", Path: "skills/anysearch"}},
},
}
// Create a pre-existing anysearch symlink to test that it gets cleaned up
preExistingLink := filepath.Join(claudeDir, "anysearch")
os.MkdirAll(claudeDir, 0o755)
if err := os.Symlink(filepath.Join(sharedDir, "anysearch"), preExistingLink); err != nil {
t.Fatal(err)
}
applyMirrors(m)
// Verify drawio is mirrored
drawioDst := filepath.Join(claudeDir, "drawio")
if _, err := os.Readlink(drawioDst); err != nil {
t.Fatalf("expected drawio symlink: %v", err)
}
// Verify anysearch is NOT mirrored and pre-existing is cleaned up
if _, err := os.Readlink(preExistingLink); err == nil {
t.Fatal("expected anysearch symlink to be deleted (excluded)")
}
}
func TestApplyMirrors_OrphanCleanup(t *testing.T) {
dir := t.TempDir()
sharedDir := filepath.Join(dir, "shared")
claudeDir := filepath.Join(dir, "claude")
os.MkdirAll(sharedDir, 0o755)
os.MkdirAll(claudeDir, 0o755)
// Create orphan symlink in claude dir
orphanDir := filepath.Join(sharedDir, "orphan")
os.MkdirAll(orphanDir, 0o755)
orphanLink := filepath.Join(claudeDir, "orphan")
if err := os.Symlink(orphanDir, orphanLink); err != nil {
t.Fatal(err)
}
m := &Manifest{
Directories: []DirEntry{
{Name: "shared", Path: sharedDir},
{Name: "claude", Path: claudeDir},
},
Mirrors: []MirrorEntry{
{From: "shared", To: "claude"},
},
Skills: []SkillEntry{},
}
applyMirrors(m)
// Orphan symlink should be removed
if _, err := os.Stat(orphanLink); err == nil {
t.Fatal("orphan symlink was not cleaned up")
}
}
func TestApplyMirrors_RealFileNotReplaced(t *testing.T) {
dir := t.TempDir()
sharedDir := filepath.Join(dir, "shared")
claudeDir := filepath.Join(dir, "claude")
os.MkdirAll(sharedDir, 0o755)
os.MkdirAll(claudeDir, 0o755)
// Create a real file at claude dir (not a symlink)
realFile := filepath.Join(claudeDir, "drawio")
writeFile(t, realFile, "real file content")
// Create shared skill
skillDir := filepath.Join(sharedDir, "drawio")
os.MkdirAll(skillDir, 0o755)
writeFile(t, filepath.Join(skillDir, "SKILL.md"), "# drawio")
m := &Manifest{
Directories: []DirEntry{
{Name: "shared", Path: sharedDir},
{Name: "claude", Path: claudeDir},
},
Mirrors: []MirrorEntry{
{From: "shared", To: "claude"},
},
Skills: []SkillEntry{
{Name: "drawio", Target: "shared", Source: SourceEntry{Repo: "a/b", Path: "skills/drawio"}},
},
}
applyMirrors(m)
// Real file should still exist
if _, err := os.Stat(realFile); err != nil {
t.Fatal("real file was replaced by symlink!")
}
}
func TestApplyMirrors_NoSymlinkForMissingSource(t *testing.T) {
dir := t.TempDir()
sharedDir := filepath.Join(dir, "shared")
claudeDir := filepath.Join(dir, "claude")
os.MkdirAll(sharedDir, 0o755)
os.MkdirAll(claudeDir, 0o755)
// Source skill directory exists but has no SKILL.md
srcSkill := filepath.Join(sharedDir, "half-installed")
os.MkdirAll(srcSkill, 0o755)
m := &Manifest{
Directories: []DirEntry{
{Name: "shared", Path: sharedDir},
{Name: "claude", Path: claudeDir},
},
Mirrors: []MirrorEntry{
{From: "shared", To: "claude"},
},
Skills: []SkillEntry{
{Name: "half-installed", Target: "shared", Source: SourceEntry{Repo: "a/b", Path: "skills/half"}},
},
}
applyMirrors(m)
// Should NOT create a symlink for a source without SKILL.md
dst := filepath.Join(claudeDir, "half-installed")
if _, err := os.Lstat(dst); err == nil {
t.Fatal("mirror created symlink for source without SKILL.md")
}
}
func TestApplyMirrors_ExternalSymlinkNotRemoved(t *testing.T) {
dir := t.TempDir()
sharedDir := filepath.Join(dir, "shared")
claudeDir := filepath.Join(dir, "claude")
externalDir := filepath.Join(dir, "external")
os.MkdirAll(sharedDir, 0o755)
os.MkdirAll(claudeDir, 0o755)
os.MkdirAll(externalDir, 0o755)
// Create a claude-only symlink pointing outside the shared pool
externalSymlink := filepath.Join(claudeDir, "claude-only")
if err := os.Symlink(externalDir, externalSymlink); err != nil {
t.Fatal(err)
}
m := &Manifest{
Directories: []DirEntry{
{Name: "shared", Path: sharedDir},
{Name: "claude", Path: claudeDir},
},
Mirrors: []MirrorEntry{
{From: "shared", To: "claude"},
},
Skills: []SkillEntry{},
}
applyMirrors(m)
// External symlink should survive orphan cleanup
if _, err := os.Stat(externalSymlink); err != nil {
t.Fatal("external symlink was incorrectly removed by orphan cleanup")
}
}
// ── installOneSkill skip logic ───────────────────────────────────────
func TestInstallOneSkill_SkipsWhenLockedAndOnDisk(t *testing.T) {
dir := t.TempDir()
sharedDir := filepath.Join(dir, "shared")
os.MkdirAll(sharedDir, 0o755)
skillDir := filepath.Join(sharedDir, "drawio")
os.MkdirAll(skillDir, 0o755)
writeFile(t, filepath.Join(skillDir, "SKILL.md"), "# drawio")
writeFile(t, filepath.Join(skillDir, ".skills-commit"), "abc123\n")
contentHash, err := computeInstalledContentHash(skillDir)
if err != nil {
t.Fatal(err)
}
lock := &LockFile{
Skills: map[string]LockSkill{
"drawio": {Commit: "abc123", Path: "skills/drawio", ContentHash: contentHash},
},
}
m := &Manifest{
Directories: []DirEntry{
{Name: "shared", Path: sharedDir},
},
}
result, ls := installOneSkill(
SkillEntry{Name: "drawio", Target: "shared", Source: SourceEntry{Repo: "a/b", Path: "skills/drawio"}},
lock, m.Directories,
)
if result.Action != "ok" || result.Error != "already installed" {
t.Fatalf("expected skip, got %+v", result)
}
if ls != nil {
t.Fatal("expected no lock update for skip")
}
}
func TestInstallOneSkill_ReinstallsWhenLockedButDiskMissing(t *testing.T) {
fakeGitHub()
defer restoreGitHub()
dir := t.TempDir()
sharedDir := filepath.Join(dir, "shared")
lock := &LockFile{
Skills: map[string]LockSkill{
"test": {Commit: "fakecommit1234567890123456789012345678901234", Path: "skills/test"},
},
}
m := &Manifest{
Directories: []DirEntry{
{Name: "shared", Path: sharedDir},
},
}
result, ls := installOneSkill(
SkillEntry{Name: "test", Target: "shared", Source: SourceEntry{Repo: "fake/repo", Ref: "main", Path: "skills/test"}},
lock, m.Directories,
)
if result.Action != "ok" {
t.Fatalf("expected install to succeed with fakes, got %+v", result)
}
if ls == nil || ls.Commit != "fakecommit1234567890123456789012345678901234" {
t.Fatalf("expected lock update with fakecommit, got %+v", ls)
}
if _, err := os.Stat(filepath.Join(sharedDir, "test", "SKILL.md")); err != nil {
t.Fatalf("SKILL.md not installed: %v", err)
}
}
func TestInstallOneSkill_EmptyLockWithDisk(t *testing.T) {
fakeGitHub()
defer restoreGitHub()
dir := t.TempDir()
sharedDir := filepath.Join(dir, "shared")
writeFile(t, filepath.Join(sharedDir, "test", "SKILL.md"), "# stale")
lock := &LockFile{Skills: map[string]LockSkill{
"test": {Commit: "", Path: "skills/test"},
}}
result, updatedLock := installOneSkill(
SkillEntry{Name: "test", Target: "shared", Source: SourceEntry{Repo: "fake/repo", Ref: "main", Path: "skills/test"}},
lock, []DirEntry{{Name: "shared", Path: sharedDir}},
)
if result.Action != "ok" || result.Error == "already installed" {
t.Fatalf("expected commitless lock to be reinstalled, got %+v", result)
}
if updatedLock == nil || updatedLock.Commit == "" || updatedLock.ContentHash == "" {
t.Fatalf("expected migrated lock, got %+v", updatedLock)
}
}
// ── updateOneSkill path change detection ─────────────────────────────
func TestUpdateOneSkill_PathChangeTriggersUpdate(t *testing.T) {
fakeGitHub()
defer restoreGitHub()
dir := t.TempDir()
sharedDir := filepath.Join(dir, "shared")
// Lock says old path with a specific commit
lock := &LockFile{
Skills: map[string]LockSkill{
"test": {Commit: "abc123", Path: "skills/old-path"},
},
}
m := &Manifest{
Directories: []DirEntry{
{Name: "shared", Path: sharedDir},
},
}
// Path changed, but fake commit matches locked commit → should still skip?
// No — the path differs, so updateOneSkill must NOT skip
result, _ := updateOneSkill(
SkillEntry{Name: "test", Target: "shared", Source: SourceEntry{Repo: "fake/repo", Ref: "main", Path: "skills/new-path"}},
lock, m.Directories,
)
if result.Action != "ok" {
t.Fatalf("expected update to succeed (path differs, should reinstall), got %+v", result)
}
}
func TestUpdateOneSkill_SkipsWhenPathAndCommitMatch_Integration(t *testing.T) {
fakeGitHub()
defer restoreGitHub()
dir := t.TempDir()
sharedDir := filepath.Join(dir, "shared")
skillDir := filepath.Join(sharedDir, "test")
os.MkdirAll(skillDir, 0o755)
writeFile(t, filepath.Join(skillDir, "SKILL.md"), "# test")
contentHash, err := computeInstalledContentHash(skillDir)
if err != nil {
t.Fatal(err)
}
lock := &LockFile{
Skills: map[string]LockSkill{
"test": {Commit: "fakecommit1234567890123456789012345678901234", Path: "skills/test", ContentHash: contentHash},
},
}
m := &Manifest{
Directories: []DirEntry{
{Name: "shared", Path: sharedDir},
},
}
// Commit and path both match → should skip
result, _ := updateOneSkill(
SkillEntry{Name: "test", Target: "shared", Source: SourceEntry{Repo: "fake/repo", Ref: "main", Path: "skills/test"}},
lock, m.Directories,
)
if result.Action != "ok" || result.Error != "already installed" {
t.Fatalf("expected skip (commit+path match), got %+v", result)
}
}
// ── util / edge cases ────────────────────────────────────────────────
func TestGetLockPath(t *testing.T) {
got := getLockPath("/home/user/.config/skills/.manifest.json")
expected := "/home/user/.config/skills/.lock.json"
if got != expected {
t.Fatalf("getLockPath(%q) = %q, want %q", "/home/user/...", got, expected)
}
}
func TestInstallOneSkill_NoFilesFoundFails(t *testing.T) {
dir := t.TempDir()
sharedDir := filepath.Join(dir, "shared")
result := InstallSkill(
SkillEntry{Name: "test", Source: SourceEntry{Repo: "anthropics/skills", Ref: "main", Path: "skills/definitely-does-not-exist"}},
filepath.Join(sharedDir, "test"), "",
)
if result.Action != "failed" {
t.Fatalf("expected failure for nonexistent source path, got %+v", result)
}
}
// fakeGitHubRoot returns a fake GitHub that has root-level files
// (paths without a directory prefix), used to test root source paths.
func fakeGitHubRoot() {
fetchLatestCommitFn = func(_, _ string) (string, error) {
return "fakecommit1234567890123456789012345678901234", nil
}
fetchTreeFn = func(_, _ string) (tree []treeEntry, err error) {
return []treeEntry{
{Path: "SKILL.md", Mode: "100644", Type: "blob"},
{Path: "README.md", Mode: "100644", Type: "blob"},
{Path: "scripts/anysearch_cli.sh", Mode: "100755", Type: "blob"},
}, nil
}
downloadFileFn = func(_, _, filePath string) ([]byte, error) {
name := filepath.Base(filePath)
return []byte("# " + name), nil
}
}
func TestInstallSkill_RootPathWithDot_Succeeds(t *testing.T) {
fakeGitHubRoot()
defer restoreGitHub()
result := InstallSkill(
SkillEntry{Name: "anysearch", Source: SourceEntry{Repo: "anysearch-ai/anysearch-skill", Ref: "main", Path: "."}},
t.TempDir(), "",
)
if result.Action != "ok" {
t.Fatalf("expected success for root path '.', got %+v", result)
}
}
func TestInstallSkill_RootPathWithEmptyString_Succeeds(t *testing.T) {
fakeGitHubRoot()
defer restoreGitHub()
result := InstallSkill(
SkillEntry{Name: "anysearch", Source: SourceEntry{Repo: "anysearch-ai/anysearch-skill", Ref: "main", Path: ""}},
t.TempDir(), "",
)
if result.Action != "ok" {
t.Fatalf("expected success for empty root path, got %+v", result)
}
}
// ── cmdUpdate integration ─────────────────────────────────────────
func TestCmdUpdate_OutdatedDetectedAndInstalled(t *testing.T) {
fakeGitHub()
defer restoreGitHub()
dir := t.TempDir()
sharedDir := filepath.Join(dir, "shared")
manifestPath := filepath.Join(dir, ".manifest.json")
// Create disk state: SKILL.md exists
skillDir := filepath.Join(sharedDir, "test")
os.MkdirAll(skillDir, 0o755)
writeFile(t, filepath.Join(skillDir, "SKILL.md"), "# test (old)")
// Write manifest
writeJSON(t, manifestPath, Manifest{
Version: 1,
Directories: []DirEntry{
{Name: "shared", Path: sharedDir},
},
Skills: []SkillEntry{
{
Name: "test",
Target: "shared",
Source: SourceEntry{Repo: "fake/repo", Ref: "main", Path: "skills/test"},
},
},
})
// Write lock with OLD commit — fakeGitHub returns "fakecommit123..."
lockPath := getLockPath(manifestPath)
writeJSON(t, lockPath, LockFile{
Version: 1,
Skills: map[string]LockSkill{
"test": {Commit: "oldcommit1234567890123456789012345678901234", Path: "skills/test"},
},
})
m, err := readManifest(manifestPath)
if err != nil {
t.Fatal(err)
}
lock, err := readLock(lockPath)
if err != nil {
t.Fatal(err)
}
// Run cmdUpdate: yes=true (skip confirm), dryRun=false
oldQuiet := quiet
quiet = true
cmdUpdate(m, lock, manifestPath, "", false, true)
quiet = oldQuiet
// Lock should be updated with fake commit
lock2, err := readLock(lockPath)
if err != nil {
t.Fatal(err)
}
ls, ok := lock2.Skills["test"]
if !ok {
t.Fatal("test skill missing from lock after update")
}
if ls.Commit != "fakecommit1234567890123456789012345678901234" {
t.Fatalf("expected fake commit, got %q", ls.Commit)
}
if _, err := os.Stat(filepath.Join(skillDir, "SKILL.md")); err != nil {
t.Fatal("SKILL.md missing after update")
}
}
func TestCmdUpdate_DryRunDoesNotModify(t *testing.T) {
fakeGitHub()
defer restoreGitHub()
dir := t.TempDir()
sharedDir := filepath.Join(dir, "shared")
manifestPath := filepath.Join(dir, ".manifest.json")
skillDir := filepath.Join(sharedDir, "test")
os.MkdirAll(skillDir, 0o755)
writeFile(t, filepath.Join(skillDir, "SKILL.md"), "# test (old)")
writeJSON(t, manifestPath, Manifest{
Version: 1,
Directories: []DirEntry{
{Name: "shared", Path: sharedDir},
},
Skills: []SkillEntry{
{
Name: "test",
Target: "shared",
Source: SourceEntry{Repo: "fake/repo", Ref: "main", Path: "skills/test"},
},
},
})
lockPath := getLockPath(manifestPath)
writeJSON(t, lockPath, LockFile{
Version: 1,
Skills: map[string]LockSkill{
"test": {Commit: "oldcommit1234567890123456789012345678901234", Path: "skills/test"},
},
})
m, err := readManifest(manifestPath)
if err != nil {
t.Fatal(err)
}
lock, err := readLock(lockPath)
if err != nil {
t.Fatal(err)
}
// Dry run
oldQuiet := quiet
quiet = true
cmdUpdate(m, lock, manifestPath, "", true, true)
quiet = oldQuiet
// Lock should still have OLD commit
lock2, err := readLock(lockPath)
if err != nil {
t.Fatal(err)
}
ls, ok := lock2.Skills["test"]
if !ok {
t.Fatal("test skill missing from lock after dry-run")
}
if ls.Commit != "oldcommit1234567890123456789012345678901234" {
t.Fatalf("dry-run should not modify lock, got commit %q", ls.Commit)
}
}
func TestIsRateLimit(t *testing.T) {
if !isRateLimit(fmt.Errorf("HTTP 403")) {
t.Fatal("should detect 403")
}
if !isRateLimit(fmt.Errorf("rate limit exceeded")) {
t.Fatal("should detect rate limit string")
}
if isRateLimit(fmt.Errorf("HTTP 404")) {
t.Fatal("should not detect 404 as rate limit")
}
if isRateLimit(nil) {
t.Fatal("nil should not be rate limit")
}
}
// ── validateSkillName ────────────────────────────────────────────────
func TestValidateSkillName(t *testing.T) {
tests := []struct {
name string
valid bool
}{
{"", false},
{".", false},
{"..", false},
{"foo/bar", false},
{"foo\\bar", false},
{"a\x00b", false},
{"normal-name", true},
{"very_long.name_with-dots", true},
{"a", true},
}
for _, tc := range tests {
err := validateSkillName(tc.name)
if tc.valid && err != nil {
t.Errorf("validateSkillName(%q) = %v, want nil", tc.name, err)
}
if !tc.valid && err == nil {
t.Errorf("validateSkillName(%q) = nil, want error", tc.name)
}
}
}
// ── writeManifest ────────────────────────────────────────────────────
func TestWriteManifestRoundTrip(t *testing.T) {
dir := t.TempDir()
mf := filepath.Join(dir, ".manifest.json")
m := &Manifest{
Version: 1,
Directories: []DirEntry{
{Name: "shared", Path: "~/.agents/skills", Comment: "main pool"},
{Name: "codex", Path: "~/.codex/skills"},
},
Symlinks: []SymlinkEntry{
{From: "~/.codex/skills", To: "~/.agents/skills"},
},
Mirrors: []MirrorEntry{
{From: "shared", To: "claude"},
},
Skills: []SkillEntry{
{
Name: "drawio", Target: "shared",
Source: SourceEntry{Repo: "a/b", Ref: "main", Path: "skills/drawio"},
Note: "test skill",
},
},
}
if err := writeManifest(mf, m); err != nil {
t.Fatal(err)