Files
skm/internal/fetch/fetch_test.go
T
johannesandClaude Opus 5.5 65a96ada8e feat: skm, pin agent skills from git repos via Nix
CLI (add, remove, list, outdated, update, import) over a skills.json manifest and a skills.lock.json pinned with nix flake prefetch, plus a home-manager module that links each locked skill via builtins.fetchTree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 14:04:50 +02:00

85 lines
2.4 KiB
Go

package fetch
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestWithParam(t *testing.T) {
cases := []struct{ in, key, val, want string }{
{"github:o/r", "ref", "main", "github:o/r?ref=main"},
{"git+ssh://git@h/o/r?ref=main", "rev", "abc", "git+ssh://git@h/o/r?ref=main&rev=abc"},
{"github:o/r?ref=old", "ref", "v2", "github:o/r?ref=v2"},
}
for _, c := range cases {
if got := WithParam(c.in, c.key, c.val); got != c.want {
t.Errorf("WithParam(%q, %q, %q) = %q, want %q", c.in, c.key, c.val, got, c.want)
}
}
}
func TestAtRev(t *testing.T) {
cases := map[string]string{
"github:o/r?ref=main": "github:o/r?rev=abc",
"git+ssh://h/o/r?ref=main": "git+ssh://h/o/r?ref=main&rev=abc",
"git+file:///tmp/r": "git+file:///tmp/r?rev=abc",
}
for in, want := range cases {
if got := AtRev(in, "abc"); got != want {
t.Errorf("AtRev(%q) = %q, want %q", in, got, want)
}
}
}
// TestNixPrefetch runs the real nix against a local git repo. No network.
func TestNixPrefetch(t *testing.T) {
// Not on PATH inside the build sandbox, where nix can't run anyway.
if _, err := exec.LookPath("nix"); err != nil {
t.Skip("nix not on PATH")
}
repo := t.TempDir()
write(t, filepath.Join(repo, "tdd", "SKILL.md"), "# tdd\n")
git(t, repo, "init", "-q", "-b", "main")
git(t, repo, "add", ".")
git(t, repo, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "-m", "init")
head := strings.TrimSpace(git(t, repo, "rev-parse", "HEAD"))
res, err := Nix{}.Prefetch(context.Background(), WithParam("git+file://"+repo, "ref", "main"))
if err != nil {
t.Fatal(err)
}
if res.Rev() != head {
t.Errorf("rev = %q, want %q", res.Rev(), head)
}
if h, _ := res.Locked["narHash"].(string); !strings.HasPrefix(h, "sha256-") {
t.Errorf("narHash = %q, want sha256-…", h)
}
if _, err := os.Stat(filepath.Join(res.StorePath, "tdd", "SKILL.md")); err != nil {
t.Errorf("skill missing from store path: %v", err)
}
}
func write(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 git(t *testing.T, dir string, args ...string) string {
t.Helper()
out, err := exec.Command("git", append([]string{"-C", dir}, args...)...).CombinedOutput()
if err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
return string(out)
}