Files
kandev/apps/backend/internal/plugins/registry_test.go
T
2026-09-15 19:05:40 +01:00

183 lines
6.0 KiB
Go

package plugins
import (
"testing"
"github.com/kandev/kandev/internal/plugins/manifest"
"github.com/kandev/kandev/internal/plugins/store"
)
// testManifest returns a minimal valid runtime-managed manifest for tests
// that only need a store.Record, not a full installed package (see
// service_test.go's testPackage for that).
func testManifest(id string) *manifest.Manifest {
return &manifest.Manifest{
ID: id,
APIVersion: 1,
Version: "1.0.0",
DisplayName: "Test Plugin",
Runtime: manifest.Runtime{
Type: "binary",
Executables: map[string]string{"linux-amd64": "server/plugin-linux-amd64"},
},
}
}
func TestRegistryLoadPopulatesFromStore(t *testing.T) {
dir := t.TempDir()
fsStore := store.NewFSStore(dir)
if err := fsStore.Save(&store.Record{Manifest: *testManifest("kandev-plugin-slack"), Status: store.StatusRegistered}); err != nil {
t.Fatalf("Save: %v", err)
}
reg := NewRegistry()
if err := reg.Load(fsStore); err != nil {
t.Fatalf("Load() unexpected error: %v", err)
}
rec, ok := reg.Get("kandev-plugin-slack")
if !ok {
t.Fatalf("Get() expected record to be present after Load()")
}
if rec.ID != "kandev-plugin-slack" {
t.Fatalf("Get() ID = %q, want %q", rec.ID, "kandev-plugin-slack")
}
}
func TestRegistryGetMissingReturnsNotOK(t *testing.T) {
reg := NewRegistry()
if _, ok := reg.Get("missing"); ok {
t.Fatalf("Get() expected ok = false for missing id")
}
}
func TestRegistryAddThenListReturnsRecord(t *testing.T) {
reg := NewRegistry()
reg.Add(&store.Record{Manifest: *testManifest("kandev-plugin-slack"), Status: store.StatusRegistered})
list := reg.List()
if len(list) != 1 {
t.Fatalf("List() len = %d, want 1", len(list))
}
if list[0].ID != "kandev-plugin-slack" {
t.Fatalf("List()[0].ID = %q, want %q", list[0].ID, "kandev-plugin-slack")
}
}
func TestRegistryRemoveDeletesRecord(t *testing.T) {
reg := NewRegistry()
reg.Add(&store.Record{Manifest: *testManifest("kandev-plugin-slack"), Status: store.StatusRegistered})
reg.Remove("kandev-plugin-slack")
if _, ok := reg.Get("kandev-plugin-slack"); ok {
t.Fatalf("Get() expected record to be removed")
}
}
func TestRegistrySetStatusUpdatesRecord(t *testing.T) {
reg := NewRegistry()
reg.Add(&store.Record{Manifest: *testManifest("kandev-plugin-slack"), Status: store.StatusRegistered})
updated, ok := reg.SetStatus("kandev-plugin-slack", store.StatusActive)
if !ok {
t.Fatalf("SetStatus() expected ok = true")
}
if updated.Status != store.StatusActive {
t.Fatalf("SetStatus() returned Status = %q, want %q", updated.Status, store.StatusActive)
}
rec, ok := reg.Get("kandev-plugin-slack")
if !ok {
t.Fatalf("Get() expected record present")
}
if rec.Status != store.StatusActive {
t.Fatalf("Get() Status = %q, want %q", rec.Status, store.StatusActive)
}
}
func TestRegistrySetStatusMissingReturnsNotOK(t *testing.T) {
reg := NewRegistry()
if _, ok := reg.SetStatus("missing", store.StatusActive); ok {
t.Fatalf("SetStatus() expected ok = false for missing id")
}
}
func TestRegistryGetReturnsIndependentCopy(t *testing.T) {
reg := NewRegistry()
autoUpdate := true
mf := testManifest("kandev-plugin-slack")
mf.Categories = []string{"chat"}
mf.RepositoryProviders = []string{"github"}
mf.ConfigSchema = map[string]any{"nested": map[string]any{"deep": "value"}}
mf.AgentTools = []manifest.AgentTool{{Name: "tool", Surfaces: []string{"kanban-task"}}}
reg.Add(&store.Record{
Manifest: *mf,
Status: store.StatusRegistered,
AutoUpdate: &autoUpdate,
})
rec, ok := reg.Get("kandev-plugin-slack")
if !ok {
t.Fatalf("Get() expected ok = true")
}
rec.Status = store.StatusActive // mutate the returned copy
*rec.AutoUpdate = false // mutate the returned pointer copy
rec.Categories[0] = "mutated" // mutate a returned nested slice element
rec.RepositoryProviders = append(rec.RepositoryProviders, "gitlab")
rec.ConfigSchema["nested"].(map[string]any)["deep"] = "mutated"
rec.AgentTools[0].Surfaces[0] = "office-task"
rec.Runtime.Executables["linux-amd64"] = "server/mutated"
fresh, ok := reg.Get("kandev-plugin-slack")
if !ok {
t.Fatalf("Get() expected ok = true")
}
if fresh.Status != store.StatusRegistered {
t.Fatalf("mutating a Get() result leaked into the registry: Status = %q, want %q", fresh.Status, store.StatusRegistered)
}
if fresh.AutoUpdate == nil || !*fresh.AutoUpdate {
t.Fatalf("mutating a Get() result leaked into the registry: AutoUpdate = %v, want true", fresh.AutoUpdate)
}
if fresh.Categories[0] != "chat" || len(fresh.RepositoryProviders) != 1 || fresh.RepositoryProviders[0] != "github" {
t.Fatalf("mutating a Get() result leaked into the registry: Categories=%v RepositoryProviders=%v", fresh.Categories, fresh.RepositoryProviders)
}
if fresh.ConfigSchema["nested"].(map[string]any)["deep"] != "value" {
t.Fatalf("mutating a Get() result leaked into the registry: ConfigSchema=%v", fresh.ConfigSchema)
}
if fresh.AgentTools[0].Surfaces[0] != "kanban-task" {
t.Fatalf("mutating a Get() result leaked into the registry: AgentTools=%v", fresh.AgentTools)
}
if fresh.Runtime.Executables["linux-amd64"] != "server/plugin-linux-amd64" {
t.Fatalf("mutating a Get() result leaked into the registry: Executables=%v", fresh.Runtime.Executables)
}
}
func TestRegistrySetRestartCountUpdatesRecord(t *testing.T) {
reg := NewRegistry()
reg.Add(&store.Record{Manifest: *testManifest("kandev-plugin-slack"), Status: store.StatusActive})
updated, ok := reg.SetRestartCount("kandev-plugin-slack", 2)
if !ok {
t.Fatalf("SetRestartCount() expected ok = true")
}
if updated.RestartCount != 2 {
t.Fatalf("SetRestartCount() RestartCount = %d, want 2", updated.RestartCount)
}
rec, ok := reg.Get("kandev-plugin-slack")
if !ok {
t.Fatalf("Get() expected record present")
}
if rec.RestartCount != 2 {
t.Fatalf("Get() RestartCount = %d, want 2", rec.RestartCount)
}
}
func TestRegistrySetRestartCountMissingReturnsNotOK(t *testing.T) {
reg := NewRegistry()
if _, ok := reg.SetRestartCount("missing", 1); ok {
t.Fatalf("SetRestartCount() expected ok = false for missing id")
}
}