bd4f1437bc
Adds a SKILL.md bundle validator, immutable per-skill versions with content digests and deduplication, protocol requests to import, list and show skills, and the pipelines skills import/list/show commands. Imports are serialised so concurrent imports never share a version number.
99 lines
2.7 KiB
Go
99 lines
2.7 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"path/filepath"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func digestSkill(files []protocol.SkillFile) string {
|
|
h := sha256.New()
|
|
for _, f := range files {
|
|
fmt.Fprintf(h, "%s\x00%d\x00", f.Path, len(f.Bytes))
|
|
h.Write(f.Bytes)
|
|
}
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|
|
|
|
func TestSkillImportDeduplicatesAndVersions(t *testing.T) {
|
|
s, err := Open(filepath.Join(t.TempDir(), "pipelines.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer s.Close()
|
|
files := []protocol.SkillFile{{Path: "SKILL.md", Bytes: []byte("---\nname: demo\ndescription: Demo\n---\n")}}
|
|
first, err := s.ImportSkill(context.Background(), "demo", "Demo", digestSkill(files), "/one", files)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
same, err := s.ImportSkill(context.Background(), "demo", "Demo", digestSkill(files), "/two", files)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if same.Version != first.Version {
|
|
t.Fatalf("dedup version = %d, want %d", same.Version, first.Version)
|
|
}
|
|
files[0].Bytes = append(files[0].Bytes, '\n')
|
|
second, err := s.ImportSkill(context.Background(), "demo", "Demo", digestSkill(files), "/three", files)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if second.Version != 2 {
|
|
t.Fatalf("new version = %d, want 2", second.Version)
|
|
}
|
|
}
|
|
|
|
func TestSkillImportRejectsUnsafeName(t *testing.T) {
|
|
s, err := Open(filepath.Join(t.TempDir(), "pipelines.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer s.Close()
|
|
files := []protocol.SkillFile{{Path: "SKILL.md", Bytes: []byte("skill")}}
|
|
if _, err := s.ImportSkill(context.Background(), "../../outside", "", digestSkill(files), "/source", files); err == nil {
|
|
t.Fatal("expected unsafe skill name to be rejected")
|
|
}
|
|
}
|
|
|
|
func TestSkillImportConcurrentVersionsAreDistinct(t *testing.T) {
|
|
s, err := Open(filepath.Join(t.TempDir(), "pipelines.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer s.Close()
|
|
const n = 8
|
|
var wg sync.WaitGroup
|
|
versions := make([]int, n)
|
|
errs := make([]error, n)
|
|
for i := range n {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
files := []protocol.SkillFile{{Path: "SKILL.md", Bytes: []byte(fmt.Sprintf("---\nname: demo\ndescription: Demo %d\n---\n", i))}}
|
|
v, err := s.ImportSkill(context.Background(), "demo", "Demo", digestSkill(files), "/src", files)
|
|
versions[i], errs[i] = v.Version, err
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
seen := map[int]bool{}
|
|
for i := range n {
|
|
if errs[i] != nil {
|
|
t.Fatalf("import %d: %v", i, errs[i])
|
|
}
|
|
if seen[versions[i]] {
|
|
t.Fatalf("version %d allocated twice: %v", versions[i], versions)
|
|
}
|
|
seen[versions[i]] = true
|
|
}
|
|
for v := 1; v <= n; v++ {
|
|
if !seen[v] {
|
|
t.Fatalf("versions = %v, want 1..%d", versions, n)
|
|
}
|
|
}
|
|
}
|