Files
bjarneo--cliamp/luaplugin/regression_bundled_test.go
wehub-resource-sync ead81af521
CI / go (push) Waiting to run
CI / build (darwin, macos-14) (push) Waiting to run
CI / build (windows, windows-2025) (push) Waiting to run
Deploy to GitHub Pages / deploy (push) Failing after 0s
chore: import upstream snapshot with attribution
2026-07-13 12:31:13 +08:00

72 lines
2.2 KiB
Go

package luaplugin
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/bjarneo/cliamp/internal/plugintrust"
)
// TestBundledPluginsLoad is the backward-compatibility guard for the plugin
// API. It loads every first-party plugin shipped in the repo's plugins/
// directory through a real Manager and asserts they all register cleanly.
//
// Any change that renames or removes an existing cliamp.* function, event, or
// permission will break one of these plugins and fail here. Keep it green by
// only ever ADDING to the plugin surface, never altering the existing shape.
func TestBundledPluginsLoad(t *testing.T) {
srcDir := filepath.Join("..", "plugins")
entries, err := os.ReadDir(srcDir)
if err != nil {
t.Fatalf("read bundled plugins dir: %v", err)
}
var luaFiles []string
for _, e := range entries {
if !e.IsDir() && filepath.Ext(e.Name()) == ".lua" {
luaFiles = append(luaFiles, e.Name())
}
}
if len(luaFiles) == 0 {
t.Fatal("no bundled .lua plugins found — expected at least one")
}
// Seed an isolated HOME so appdir.PluginDir() resolves into a temp tree.
home := t.TempDir()
t.Setenv("HOME", home)
pluginDir := filepath.Join(home, ".config", "cliamp", "plugins")
if err := os.MkdirAll(pluginDir, 0o755); err != nil {
t.Fatalf("mkdir plugin dir: %v", err)
}
for _, name := range luaFiles {
data, err := os.ReadFile(filepath.Join(srcDir, name))
if err != nil {
t.Fatalf("read %s: %v", name, err)
}
if err := os.WriteFile(filepath.Join(pluginDir, name), data, 0o644); err != nil {
t.Fatalf("copy %s: %v", name, err)
}
pluginName := strings.TrimSuffix(name, ".lua")
if _, err := plugintrust.Approve(pluginDir, pluginName, filepath.Join(pluginDir, name)); err != nil {
t.Fatalf("approve %s: %v", name, err)
}
}
mgr, err := New(nil)
if err != nil {
t.Fatalf("bundled plugins failed to load (API compatibility broken): %v", err)
}
defer mgr.Close()
if got, want := mgr.PluginCount(), len(luaFiles); got != want {
t.Fatalf("loaded %d plugins, want %d (a bundled plugin failed to register)", got, want)
}
for _, p := range mgr.plugins {
if p.Type == "" {
t.Errorf("plugin %q registered with empty type", p.Name)
}
}
}