go-scm/plugin/loader_test.go
Snider f3dd8ca0f0 fix(review): address CodeRabbit PR #2 findings
Critical/Major:
- Remove dead functions syncRepoNameFromArg and repoNameFromArg (used url pkg without import, would cause compile error)
- Migrate forge.lthn.ai/core/config → dappco.re/go/core/config in forge/config.go and gitea/config.go
- Propagate ListIssueCommentsIter errors in forge/meta.go and gitea/meta.go (was silently returning truncated count)
- Add RedactedToken() to gitea/client.go to avoid exposing raw API tokens
- Add 30s timeout to http.DefaultClient usage in gitea/prs.go via package-level httpClient
- Fix stringsx.Fields (bufio 64KiB limit), Repeat (wrong for negative/zero), Replace (ignored n param) to match stdlib
- Fix fmtx.Println to use fmt.Sprintln so spaces appear between operands
- Fix filepathx.Abs to use path/filepath for OS-aware path handling; wrap Getwd error
- Fix stdio.Write to return io.ErrShortWrite on partial writes
- Add mutex lock to jobrunner.Journal.Query to prevent data race with Append
- Add sync.RWMutex to ScmProvider; protect p.index reads/writes in pkg/api/provider.go
- Fix cmd/scm/cmd_index.go: append dir to repoPaths only after ReadDir confirms existence
- Fix manifest/compile.go: copy manifest before applying version override to avoid mutating caller
- Fix forge/labels.go: use ListOrgLabelsIter/ListRepoLabelsIter names in iterator error logs
- Wrap single-segment validation error in syncutil.ParseRepoName with function context

Minor:
- Fix import ordering (stdlib → forge.lthn.ai → third-party) in cmd/forge, cmd/collect, repos, cmd/gitea files
- Add t.Setenv("HOME", t.TempDir()) to gitea testhelpers and forge/labels_test.go
- Add iterator yield guard in forge/orgs_test.go
- Convert syncutil/repo_name_test.go to table-driven tests
- Use json.Marshal in pkg/api/provider_test.go instead of string concatenation
- Fix test naming (redundant/conflicting _Good/_Bad suffixes) across 10 test files

Co-Authored-By: Virgil <virgil@lethean.io>
2026-04-07 09:25:42 +01:00

148 lines
3.6 KiB
Go

// SPDX-License-Identifier: EUPL-1.2
package plugin
import (
"testing"
"dappco.re/go/core/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_Good(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_Good(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_Good(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_Bad(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_Bad(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")
}