Files
HomeAgent/internal/plugin/registry_disable_test.go
JianFeeeee 4e70b8d5d8 refactor(module): 核心仓 Go module 路径改为 github.com/JianFeeeee/HologramAgent
批 1,不含 SDK 仓与目录改名。

- go.mod 第 1 行 module 路径
- 366 个 .go 文件的完整前缀 github.com/JianFeeeee/HomeAgent 机械替换
  (857 处,仅完整前缀;展示名/注释里的 HomeAgent 字样留给批 5)
- 未改:third_party 目录名、SDK 自身 go.mod module、go.mod replace 目标、
  homed/waiter 可执行名、.hmap 后缀
2026-10-21 12:17:19 +08:00

75 lines
2.4 KiB
Go

package plugin
import (
"os"
"path/filepath"
"testing"
sdk "github.com/JianFeeeee/HologramAgent/internal/sdk"
)
// TestDisablePluginNotInstalled 验证 DisablePlugin 对未安装插件的拒绝:
// 修复前会把任意名字写进 disabled_plugins 表(脏数据),修复后应直接报错。
func TestDisablePluginNotInstalled(t *testing.T) {
r := &Registry{
plugins: make(map[string]sdk.Plugin),
factories: make(map[string]NativeFactory),
knownDisabled: make(map[string]bool),
plgDir: t.TempDir(),
}
err := r.DisablePlugin("nonexistent_test_xyz", "test")
if err == nil {
t.Fatal("DisablePlugin should reject a plugin that is not installed")
}
if err.Error() != "plugin nonexistent_test_xyz not installed" {
t.Fatalf("unexpected error: %v", err)
}
if r.knownDisabled["nonexistent_test_xyz"] {
t.Fatal("knownDisabled must not contain a non-installed plugin")
}
}
// TestPluginInstalledFactory 内置插件(工厂注册,无物理目录)应视为已安装。
func TestPluginInstalledFactory(t *testing.T) {
r := &Registry{
plugins: make(map[string]sdk.Plugin),
factories: map[string]NativeFactory{"builtin_demo": nil},
knownDisabled: make(map[string]bool),
plgDir: t.TempDir(),
}
if !r.pluginInstalled("builtin_demo") {
t.Fatal("factory-registered builtin plugin should be considered installed")
}
if r.pluginInstalled("no_such_plugin") {
t.Fatal("unknown plugin should not be considered installed")
}
}
// TestPluginInstalledDir 外部插件按 plgDir/<name> 目录存在判定已安装。
func TestPluginInstalledDir(t *testing.T) {
dir := t.TempDir()
if err := os.MkdirAll(filepath.Join(dir, "external_demo"), 0o755); err != nil {
t.Fatalf("mkdir: %v", err)
}
r := &Registry{
plugins: make(map[string]sdk.Plugin),
factories: make(map[string]NativeFactory),
knownDisabled: make(map[string]bool),
plgDir: dir,
}
if !r.pluginInstalled("external_demo") {
t.Fatal("plugin with an existing directory should be considered installed")
}
// 同名普通文件(非目录)不算已安装
if err := os.WriteFile(filepath.Join(dir, "just_a_file"), []byte("x"), 0o644); err != nil {
t.Fatalf("write: %v", err)
}
if r.pluginInstalled("just_a_file") {
t.Fatal("a regular file must not count as an installed plugin")
}
// 空名兜底
if r.pluginInstalled("") {
t.Fatal("empty plugin name must not be considered installed")
}
}