feat: allow enabling all plugins into a channel
This commit is contained in:
parent
899ac49336
commit
3b09a9dd47
10 changed files with 915 additions and 17 deletions
|
@ -44,11 +44,17 @@ type Channel struct {
|
|||
PlatformChannelID string
|
||||
ChannelRaw map[string]interface{}
|
||||
Enabled bool
|
||||
EnableAllPlugins bool
|
||||
Plugins map[string]*ChannelPlugin
|
||||
}
|
||||
|
||||
// HasEnabledPlugin checks if a plugin is enabled for this channel
|
||||
func (c *Channel) HasEnabledPlugin(pluginID string) bool {
|
||||
// If EnableAllPlugins is true, all plugins are considered enabled
|
||||
if c.EnableAllPlugins {
|
||||
return true
|
||||
}
|
||||
|
||||
plugin, exists := c.Plugins[pluginID]
|
||||
if !exists {
|
||||
return false
|
||||
|
|
234
internal/model/message_test.go
Normal file
234
internal/model/message_test.go
Normal file
|
@ -0,0 +1,234 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestChannel_HasEnabledPlugin(t *testing.T) {
|
||||
t.Run("EnableAllPlugins false - plugin not in map", func(t *testing.T) {
|
||||
channel := &Channel{
|
||||
ID: 1,
|
||||
Platform: "telegram",
|
||||
PlatformChannelID: "123456",
|
||||
Enabled: true,
|
||||
EnableAllPlugins: false,
|
||||
Plugins: make(map[string]*ChannelPlugin),
|
||||
}
|
||||
|
||||
// Plugin not in map should return false
|
||||
result := channel.HasEnabledPlugin("nonexistent.plugin")
|
||||
if result {
|
||||
t.Errorf("Expected HasEnabledPlugin to return false for nonexistent plugin, got true")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("EnableAllPlugins false - plugin disabled", func(t *testing.T) {
|
||||
channel := &Channel{
|
||||
ID: 1,
|
||||
Platform: "telegram",
|
||||
PlatformChannelID: "123456",
|
||||
Enabled: true,
|
||||
EnableAllPlugins: false,
|
||||
Plugins: map[string]*ChannelPlugin{
|
||||
"test.plugin": {
|
||||
ID: 1,
|
||||
ChannelID: 1,
|
||||
PluginID: "test.plugin",
|
||||
Enabled: false,
|
||||
Config: make(map[string]any),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Disabled plugin should return false
|
||||
result := channel.HasEnabledPlugin("test.plugin")
|
||||
if result {
|
||||
t.Errorf("Expected HasEnabledPlugin to return false for disabled plugin, got true")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("EnableAllPlugins false - plugin enabled", func(t *testing.T) {
|
||||
channel := &Channel{
|
||||
ID: 1,
|
||||
Platform: "telegram",
|
||||
PlatformChannelID: "123456",
|
||||
Enabled: true,
|
||||
EnableAllPlugins: false,
|
||||
Plugins: map[string]*ChannelPlugin{
|
||||
"test.plugin": {
|
||||
ID: 1,
|
||||
ChannelID: 1,
|
||||
PluginID: "test.plugin",
|
||||
Enabled: true,
|
||||
Config: make(map[string]any),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Enabled plugin should return true
|
||||
result := channel.HasEnabledPlugin("test.plugin")
|
||||
if !result {
|
||||
t.Errorf("Expected HasEnabledPlugin to return true for enabled plugin, got false")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("EnableAllPlugins true - plugin not in map", func(t *testing.T) {
|
||||
channel := &Channel{
|
||||
ID: 1,
|
||||
Platform: "telegram",
|
||||
PlatformChannelID: "123456",
|
||||
Enabled: true,
|
||||
EnableAllPlugins: true,
|
||||
Plugins: make(map[string]*ChannelPlugin),
|
||||
}
|
||||
|
||||
// When EnableAllPlugins is true, any plugin should be considered enabled
|
||||
result := channel.HasEnabledPlugin("nonexistent.plugin")
|
||||
if !result {
|
||||
t.Errorf("Expected HasEnabledPlugin to return true when EnableAllPlugins is true, got false")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("EnableAllPlugins true - plugin disabled", func(t *testing.T) {
|
||||
channel := &Channel{
|
||||
ID: 1,
|
||||
Platform: "telegram",
|
||||
PlatformChannelID: "123456",
|
||||
Enabled: true,
|
||||
EnableAllPlugins: true,
|
||||
Plugins: map[string]*ChannelPlugin{
|
||||
"test.plugin": {
|
||||
ID: 1,
|
||||
ChannelID: 1,
|
||||
PluginID: "test.plugin",
|
||||
Enabled: false,
|
||||
Config: make(map[string]any),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// When EnableAllPlugins is true, even disabled plugins should be considered enabled
|
||||
result := channel.HasEnabledPlugin("test.plugin")
|
||||
if !result {
|
||||
t.Errorf("Expected HasEnabledPlugin to return true when EnableAllPlugins is true (even for disabled plugin), got false")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("EnableAllPlugins true - plugin enabled", func(t *testing.T) {
|
||||
channel := &Channel{
|
||||
ID: 1,
|
||||
Platform: "telegram",
|
||||
PlatformChannelID: "123456",
|
||||
Enabled: true,
|
||||
EnableAllPlugins: true,
|
||||
Plugins: map[string]*ChannelPlugin{
|
||||
"test.plugin": {
|
||||
ID: 1,
|
||||
ChannelID: 1,
|
||||
PluginID: "test.plugin",
|
||||
Enabled: true,
|
||||
Config: make(map[string]any),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// When EnableAllPlugins is true, enabled plugins should also return true
|
||||
result := channel.HasEnabledPlugin("test.plugin")
|
||||
if !result {
|
||||
t.Errorf("Expected HasEnabledPlugin to return true when EnableAllPlugins is true, got false")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("EnableAllPlugins true - multiple plugins", func(t *testing.T) {
|
||||
channel := &Channel{
|
||||
ID: 1,
|
||||
Platform: "telegram",
|
||||
PlatformChannelID: "123456",
|
||||
Enabled: true,
|
||||
EnableAllPlugins: true,
|
||||
Plugins: map[string]*ChannelPlugin{
|
||||
"plugin1": {
|
||||
ID: 1,
|
||||
ChannelID: 1,
|
||||
PluginID: "plugin1",
|
||||
Enabled: true,
|
||||
Config: make(map[string]any),
|
||||
},
|
||||
"plugin2": {
|
||||
ID: 2,
|
||||
ChannelID: 1,
|
||||
PluginID: "plugin2",
|
||||
Enabled: false,
|
||||
Config: make(map[string]any),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// All plugins should be enabled when EnableAllPlugins is true
|
||||
testCases := []string{"plugin1", "plugin2", "plugin3", "any.plugin"}
|
||||
for _, pluginID := range testCases {
|
||||
result := channel.HasEnabledPlugin(pluginID)
|
||||
if !result {
|
||||
t.Errorf("Expected HasEnabledPlugin('%s') to return true when EnableAllPlugins is true, got false", pluginID)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestChannelName(t *testing.T) {
|
||||
t.Run("Returns PlatformChannelID when ChannelRaw is nil", func(t *testing.T) {
|
||||
channel := &Channel{
|
||||
PlatformChannelID: "test-id",
|
||||
ChannelRaw: nil,
|
||||
}
|
||||
|
||||
result := channel.ChannelName()
|
||||
if result != "test-id" {
|
||||
t.Errorf("Expected channel name to be 'test-id', got '%s'", result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Returns name from ChannelRaw when available", func(t *testing.T) {
|
||||
channel := &Channel{
|
||||
PlatformChannelID: "test-id",
|
||||
ChannelRaw: map[string]interface{}{
|
||||
"name": "Test Channel",
|
||||
},
|
||||
}
|
||||
|
||||
result := channel.ChannelName()
|
||||
if result != "Test Channel" {
|
||||
t.Errorf("Expected channel name to be 'Test Channel', got '%s'", result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Returns title from nested chat object (Telegram style)", func(t *testing.T) {
|
||||
channel := &Channel{
|
||||
PlatformChannelID: "test-id",
|
||||
ChannelRaw: map[string]interface{}{
|
||||
"chat": map[string]interface{}{
|
||||
"title": "Telegram Group",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result := channel.ChannelName()
|
||||
if result != "Telegram Group" {
|
||||
t.Errorf("Expected channel name to be 'Telegram Group', got '%s'", result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Falls back to PlatformChannelID when no valid name found", func(t *testing.T) {
|
||||
channel := &Channel{
|
||||
PlatformChannelID: "fallback-id",
|
||||
ChannelRaw: map[string]interface{}{
|
||||
"other_field": "value",
|
||||
},
|
||||
}
|
||||
|
||||
result := channel.ChannelName()
|
||||
if result != "fallback-id" {
|
||||
t.Errorf("Expected channel name to fallback to 'fallback-id', got '%s'", result)
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue