go/pkg/plugin/loader_test.go
Snider 03c9188d79
feat: infrastructure packages and lint cleanup (#281)
* ci: consolidate duplicate workflows and merge CodeQL configs

Remove 17 duplicate workflow files that were split copies of the
combined originals. Each family (CI, CodeQL, Coverage, PR Build,
Alpha Release) had the same job duplicated across separate
push/pull_request/schedule/manual trigger files.

Merge codeql.yml and codescan.yml into a single codeql.yml with
a language matrix covering go, javascript-typescript, python,
and actions — matching the previous default setup coverage.

Remaining workflows (one per family):
- ci.yml (push + PR + manual)
- codeql.yml (push + PR + schedule, all languages)
- coverage.yml (push + PR + manual)
- alpha-release.yml (push + manual)
- pr-build.yml (PR + manual)
- release.yml (tag push)
- agent-verify.yml, auto-label.yml, auto-project.yml

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* feat: add collect, config, crypt, plugin packages and fix all lint issues

Add four new infrastructure packages with CLI commands:
- pkg/config: layered configuration (defaults → file → env → flags)
- pkg/crypt: crypto primitives (Argon2id, AES-GCM, ChaCha20, HMAC, checksums)
- pkg/plugin: plugin system with GitHub-based install/update/remove
- pkg/collect: collection subsystem (GitHub, BitcoinTalk, market, papers, excavate)

Fix all golangci-lint issues across the entire codebase (~100 errcheck,
staticcheck SA1012/SA1019/ST1005, unused, ineffassign fixes) so that
`core go qa` passes with 0 issues.

Closes #167, #168, #170, #250, #251, #252, #253, #254, #255, #256

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-04 11:34:43 +00:00

146 lines
3.6 KiB
Go

package plugin
import (
"testing"
"github.com/host-uk/core/pkg/io"
"github.com/stretchr/testify/assert"
)
func TestLoader_Discover_Good(t *testing.T) {
m := io.NewMockMedium()
baseDir := "/home/user/.core/plugins"
// Set up mock filesystem with two plugins
m.Dirs[baseDir] = true
m.Dirs[baseDir+"/plugin-a"] = true
m.Dirs[baseDir+"/plugin-b"] = true
m.Files[baseDir+"/plugin-a/plugin.json"] = `{
"name": "plugin-a",
"version": "1.0.0",
"description": "Plugin A",
"entrypoint": "main.go"
}`
m.Files[baseDir+"/plugin-b/plugin.json"] = `{
"name": "plugin-b",
"version": "2.0.0",
"description": "Plugin B",
"entrypoint": "run.sh"
}`
loader := NewLoader(m, baseDir)
manifests, err := loader.Discover()
assert.NoError(t, err)
assert.Len(t, manifests, 2)
names := make(map[string]bool)
for _, manifest := range manifests {
names[manifest.Name] = true
}
assert.True(t, names["plugin-a"])
assert.True(t, names["plugin-b"])
}
func TestLoader_Discover_Good_SkipsInvalidPlugins(t *testing.T) {
m := io.NewMockMedium()
baseDir := "/home/user/.core/plugins"
m.Dirs[baseDir] = true
m.Dirs[baseDir+"/good-plugin"] = true
m.Dirs[baseDir+"/bad-plugin"] = true
// Valid plugin
m.Files[baseDir+"/good-plugin/plugin.json"] = `{
"name": "good-plugin",
"version": "1.0.0",
"entrypoint": "main.go"
}`
// Invalid plugin (bad JSON)
m.Files[baseDir+"/bad-plugin/plugin.json"] = `{invalid}`
loader := NewLoader(m, baseDir)
manifests, err := loader.Discover()
assert.NoError(t, err)
assert.Len(t, manifests, 1)
assert.Equal(t, "good-plugin", manifests[0].Name)
}
func TestLoader_Discover_Good_SkipsFiles(t *testing.T) {
m := io.NewMockMedium()
baseDir := "/home/user/.core/plugins"
m.Dirs[baseDir] = true
m.Dirs[baseDir+"/real-plugin"] = true
m.Files[baseDir+"/registry.json"] = `{}` // A file, not a directory
m.Files[baseDir+"/real-plugin/plugin.json"] = `{
"name": "real-plugin",
"version": "1.0.0",
"entrypoint": "main.go"
}`
loader := NewLoader(m, baseDir)
manifests, err := loader.Discover()
assert.NoError(t, err)
assert.Len(t, manifests, 1)
assert.Equal(t, "real-plugin", manifests[0].Name)
}
func TestLoader_Discover_Good_EmptyDirectory(t *testing.T) {
m := io.NewMockMedium()
baseDir := "/home/user/.core/plugins"
m.Dirs[baseDir] = true
loader := NewLoader(m, baseDir)
manifests, err := loader.Discover()
assert.NoError(t, err)
assert.Empty(t, manifests)
}
func TestLoader_LoadPlugin_Good(t *testing.T) {
m := io.NewMockMedium()
baseDir := "/home/user/.core/plugins"
m.Dirs[baseDir+"/my-plugin"] = true
m.Files[baseDir+"/my-plugin/plugin.json"] = `{
"name": "my-plugin",
"version": "1.0.0",
"description": "My plugin",
"author": "Test",
"entrypoint": "main.go"
}`
loader := NewLoader(m, baseDir)
manifest, err := loader.LoadPlugin("my-plugin")
assert.NoError(t, err)
assert.Equal(t, "my-plugin", manifest.Name)
assert.Equal(t, "1.0.0", manifest.Version)
}
func TestLoader_LoadPlugin_Bad_NotFound(t *testing.T) {
m := io.NewMockMedium()
loader := NewLoader(m, "/home/user/.core/plugins")
_, err := loader.LoadPlugin("nonexistent")
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to load plugin")
}
func TestLoader_LoadPlugin_Bad_InvalidManifest(t *testing.T) {
m := io.NewMockMedium()
baseDir := "/home/user/.core/plugins"
m.Dirs[baseDir+"/bad-plugin"] = true
m.Files[baseDir+"/bad-plugin/plugin.json"] = `{
"name": "bad-plugin",
"version": "1.0.0"
}` // Missing entrypoint
loader := NewLoader(m, baseDir)
_, err := loader.LoadPlugin("bad-plugin")
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid plugin manifest")
}