Files
gnoma/internal/plugin/manager_test.go
T
vikingowl ec9433d783 chore(lint): clear remaining errcheck and staticcheck findings
Brings the project to a clean `make lint` baseline (0 issues).

Mechanical:
- Wrap deferred resp.Body.Close() in closures (router/discovery.go,
  router/probe.go) so the unchecked return surfaces as `_ = ...`.
- Apply `_ = ...` (single or multi-return blank) to test-file calls
  that intentionally ignore errors: os.MkdirAll / os.WriteFile / os.Chdir
  in setup paths, Close / Shutdown in teardown, Submit / Spawn / Send /
  LoadDir in tests that assert on side effects.

Structural:
- engine.handleRequestTooLarge drops the unused req parameter and
  rebuilds the request from compacted history (SA4009 — argument was
  overwritten before first use).
- provider.ClassifyHTTPStatus and google.applyCapabilityOverrides switch
  to tagged switches over the discriminator (QF1002).
- tui.app.go MouseWheel + inputMode and cmd/gnoma main slm-status use
  tagged switches in place of equality chains (QF1003).
- cmd/gnoma main.go merges a var decl with its immediate assignment
  (S1021).
- Three empty-branch sites (dispatcher_test, loader_test,
  coordinator_test) become real assertions or get the dead `if` removed
  (SA9003).
2026-05-19 17:53:42 +02:00

161 lines
4.4 KiB
Go

package plugin
import (
"os"
"path/filepath"
"testing"
)
func TestManager_Install(t *testing.T) {
dir := t.TempDir()
globalDir := filepath.Join(dir, "global")
projectDir := filepath.Join(dir, "project")
_ = os.MkdirAll(globalDir, 0o755)
_ = os.MkdirAll(projectDir, 0o755)
// Create a source plugin directory.
srcDir := filepath.Join(dir, "src", "my-plugin")
_ = os.MkdirAll(srcDir, 0o755)
m := Manifest{Name: "my-plugin", Version: "1.0.0", Description: "Test plugin"}
data, _ := marshalJSON(m)
_ = os.WriteFile(filepath.Join(srcDir, "plugin.json"), data, 0o644)
mgr := NewManager(globalDir, projectDir, testLogger())
// Install to user scope.
if err := mgr.Install(srcDir, "user"); err != nil {
t.Fatalf("Install: %v", err)
}
// Verify the plugin was copied.
installed := filepath.Join(globalDir, "my-plugin", "plugin.json")
if _, err := os.Stat(installed); err != nil {
t.Errorf("installed manifest not found: %v", err)
}
}
func TestManager_Install_ProjectScope(t *testing.T) {
dir := t.TempDir()
globalDir := filepath.Join(dir, "global")
projectDir := filepath.Join(dir, "project")
_ = os.MkdirAll(globalDir, 0o755)
_ = os.MkdirAll(projectDir, 0o755)
srcDir := filepath.Join(dir, "src", "proj-plugin")
_ = os.MkdirAll(srcDir, 0o755)
m := Manifest{Name: "proj-plugin", Version: "1.0.0"}
data, _ := marshalJSON(m)
_ = os.WriteFile(filepath.Join(srcDir, "plugin.json"), data, 0o644)
mgr := NewManager(globalDir, projectDir, testLogger())
if err := mgr.Install(srcDir, "project"); err != nil {
t.Fatalf("Install: %v", err)
}
installed := filepath.Join(projectDir, "proj-plugin", "plugin.json")
if _, err := os.Stat(installed); err != nil {
t.Errorf("installed manifest not found: %v", err)
}
}
func TestManager_Install_AlreadyInstalled(t *testing.T) {
dir := t.TempDir()
globalDir := filepath.Join(dir, "global")
_ = os.MkdirAll(globalDir, 0o755)
srcDir := filepath.Join(dir, "src", "dup")
_ = os.MkdirAll(srcDir, 0o755)
m := Manifest{Name: "dup", Version: "1.0.0"}
data, _ := marshalJSON(m)
_ = os.WriteFile(filepath.Join(srcDir, "plugin.json"), data, 0o644)
mgr := NewManager(globalDir, filepath.Join(dir, "project"), testLogger())
// First install.
_ = mgr.Install(srcDir, "user")
// Second install should fail.
err := mgr.Install(srcDir, "user")
if err == nil {
t.Error("expected ErrAlreadyInstalled")
}
}
func TestManager_Install_NoManifest(t *testing.T) {
dir := t.TempDir()
globalDir := filepath.Join(dir, "global")
_ = os.MkdirAll(globalDir, 0o755)
srcDir := filepath.Join(dir, "src", "empty")
_ = os.MkdirAll(srcDir, 0o755)
mgr := NewManager(globalDir, filepath.Join(dir, "project"), testLogger())
err := mgr.Install(srcDir, "user")
if err == nil {
t.Error("expected error for missing manifest")
}
}
func TestManager_Uninstall(t *testing.T) {
dir := t.TempDir()
globalDir := filepath.Join(dir, "global")
_ = os.MkdirAll(globalDir, 0o755)
// Pre-install a plugin.
pluginDir := filepath.Join(globalDir, "to-remove")
_ = os.MkdirAll(pluginDir, 0o755)
m := Manifest{Name: "to-remove", Version: "1.0.0"}
data, _ := marshalJSON(m)
_ = os.WriteFile(filepath.Join(pluginDir, "plugin.json"), data, 0o644)
mgr := NewManager(globalDir, filepath.Join(dir, "project"), testLogger())
if err := mgr.Uninstall("to-remove", "user"); err != nil {
t.Fatalf("Uninstall: %v", err)
}
if _, err := os.Stat(pluginDir); !os.IsNotExist(err) {
t.Error("plugin directory should be removed")
}
}
func TestManager_Uninstall_NotFound(t *testing.T) {
dir := t.TempDir()
mgr := NewManager(filepath.Join(dir, "global"), filepath.Join(dir, "project"), testLogger())
err := mgr.Uninstall("nonexistent", "user")
if err == nil {
t.Error("expected ErrNotFound")
}
}
func TestManager_List(t *testing.T) {
dir := t.TempDir()
globalDir := filepath.Join(dir, "global")
projectDir := filepath.Join(dir, "project")
writePlugin(t, globalDir, "global-plugin", "1.0.0", nil)
writePlugin(t, projectDir, "project-plugin", "2.0.0", nil)
mgr := NewManager(globalDir, projectDir, testLogger())
infos, err := mgr.List()
if err != nil {
t.Fatalf("List: %v", err)
}
if len(infos) != 2 {
t.Fatalf("expected 2 plugins, got %d", len(infos))
}
// Check that both scopes are represented.
scopes := map[string]bool{}
for _, info := range infos {
scopes[info.Scope] = true
}
if !scopes["user"] || !scopes["project"] {
t.Errorf("expected both scopes, got %v", scopes)
}
}