plugin_test.go
1 package test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/TransformerOS/kamaji-go/internal/plugins" 8 ) 9 10 // Mock tool for testing 11 type mockTool struct { 12 name string 13 description string 14 } 15 16 func (m *mockTool) Name() string { 17 return m.name 18 } 19 20 func (m *mockTool) Description() string { 21 return m.description 22 } 23 24 func (m *mockTool) Call(ctx context.Context, input string) (string, error) { 25 return "mock result", nil 26 } 27 28 // Mock plugin for testing 29 type mockPlugin struct { 30 *plugins.BasePlugin 31 loaded bool 32 } 33 34 func newMockPlugin() *mockPlugin { 35 base := plugins.NewBasePlugin("test-plugin", "1.0.0") 36 plugin := &mockPlugin{ 37 BasePlugin: base, 38 loaded: false, 39 } 40 41 // Add a mock tool 42 tool := &mockTool{ 43 name: "mock_tool", 44 description: "A mock tool for testing", 45 } 46 plugin.AddTool(tool) 47 48 return plugin 49 } 50 51 func (m *mockPlugin) Load() error { 52 m.loaded = true 53 return nil 54 } 55 56 func (m *mockPlugin) Unload() error { 57 m.loaded = false 58 return nil 59 } 60 61 func TestPluginManagerBasicOperations(t *testing.T) { 62 manager := plugins.NewPluginManager() 63 64 // Test empty manager 65 plugins := manager.ListPlugins() 66 if len(plugins) != 0 { 67 t.Errorf("Expected 0 plugins, got %d", len(plugins)) 68 } 69 70 // Test plugin not found 71 _, exists := manager.GetPlugin("nonexistent") 72 if exists { 73 t.Error("Expected plugin not to exist") 74 } 75 } 76 77 func TestBasePluginFunctionality(t *testing.T) { 78 plugin := newMockPlugin() 79 80 // Test basic properties 81 if plugin.Name() != "test-plugin" { 82 t.Errorf("Expected name 'test-plugin', got '%s'", plugin.Name()) 83 } 84 85 if plugin.Version() != "1.0.0" { 86 t.Errorf("Expected version '1.0.0', got '%s'", plugin.Version()) 87 } 88 89 // Test load/unload 90 if plugin.loaded { 91 t.Error("Plugin should not be loaded initially") 92 } 93 94 err := plugin.Load() 95 if err != nil { 96 t.Errorf("Failed to load plugin: %v", err) 97 } 98 99 if !plugin.loaded { 100 t.Error("Plugin should be loaded after Load()") 101 } 102 103 err = plugin.Unload() 104 if err != nil { 105 t.Errorf("Failed to unload plugin: %v", err) 106 } 107 108 if plugin.loaded { 109 t.Error("Plugin should not be loaded after Unload()") 110 } 111 112 // Test tools 113 tools := plugin.GetTools() 114 if len(tools) != 1 { 115 t.Errorf("Expected 1 tool, got %d", len(tools)) 116 } 117 118 if tools[0].Name() != "mock_tool" { 119 t.Errorf("Expected tool name 'mock_tool', got '%s'", tools[0].Name()) 120 } 121 } 122 123 func TestPluginToolIntegration(t *testing.T) { 124 plugin := newMockPlugin() 125 126 // Get tools from plugin 127 pluginTools := plugin.GetTools() 128 if len(pluginTools) == 0 { 129 t.Fatal("Plugin should have tools") 130 } 131 132 tool := pluginTools[0] 133 134 // Test tool functionality 135 ctx := context.Background() 136 result, err := tool.Call(ctx, "test input") 137 if err != nil { 138 t.Errorf("Tool call failed: %v", err) 139 } 140 141 if result != "mock result" { 142 t.Errorf("Expected 'mock result', got '%s'", result) 143 } 144 145 // Test tool properties 146 if tool.Name() != "mock_tool" { 147 t.Errorf("Expected tool name 'mock_tool', got '%s'", tool.Name()) 148 } 149 150 if tool.Description() == "" { 151 t.Error("Tool description should not be empty") 152 } 153 }