Files
CLIProxyAPI/sdk/cliproxy/home_plugins_test.go
hkfires e9a11db7b9 feat(home): enhance plugin management and synchronization
- Added NodeID field to HomeConfig for better identification.
- Updated ConfigFromJWT to populate NodeID from claims.
- Introduced new Redis keys for managing plugin status and tasks.
- Implemented RPushPluginStatus and GetPluginTasks methods in the client for handling plugin-related data.
- Enhanced Sync functionality to include detailed reporting on plugin installation and deletion tasks.
- Added error handling and reporting for plugin synchronization failures.
- Created tests for new plugin synchronization and management features.
- Improved the overall structure and readability of the plugin synchronization logic.
2026-06-23 20:42:33 +08:00

104 lines
3.0 KiB
Go

package cliproxy
import (
"context"
"testing"
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
"github.com/router-for-me/CLIProxyAPI/v7/internal/home"
"gopkg.in/yaml.v3"
)
func TestSyncHomePluginsSkipsUnchangedSignature(t *testing.T) {
cfg := &config.Config{}
cfg.Home.Enabled = true
cfg.Plugins.Enabled = true
cfg.Plugins.Configs = map[string]config.PluginInstanceConfig{}
service := &Service{}
_, key, didSync, errSync := service.syncHomePlugins(context.Background(), cfg)
if errSync != nil {
t.Fatalf("syncHomePlugins() error = %v", errSync)
}
if !didSync || key == "" {
t.Fatalf("syncHomePlugins() didSync=%v key=%q, want first sync with key", didSync, key)
}
service.markHomePluginsSynced(key)
_, gotKey, didSync, errSync := service.syncHomePlugins(context.Background(), cfg)
if errSync != nil {
t.Fatalf("syncHomePlugins(second) error = %v", errSync)
}
if didSync || gotKey != key {
t.Fatalf("syncHomePlugins(second) didSync=%v key=%q, want skipped same key %q", didSync, gotKey, key)
}
}
func TestApplyHomeOverlayWarnsOnRuntimePluginSyncFailure(t *testing.T) {
base := &config.Config{}
base.Home.Enabled = true
base.Plugins.Enabled = true
service := &Service{cfg: base}
enabled := true
remote := &config.Config{}
remote.Plugins.Enabled = true
remote.Plugins.Configs = map[string]config.PluginInstanceConfig{
"broken": {
Enabled: &enabled,
Raw: yaml.Node{
Kind: yaml.MappingNode,
Tag: "!!map",
Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Tag: "!!str", Value: "store"},
{
Kind: yaml.MappingNode,
Tag: "!!map",
Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Tag: "!!str", Value: "id"},
{Kind: yaml.ScalarNode, Tag: "!!str", Value: "broken"},
},
},
},
},
},
}
if errApply := service.applyHomeOverlayContext(context.Background(), remote); errApply != nil {
t.Fatalf("applyHomeOverlayContext() error = %v, want warning-only plugin sync failure", errApply)
}
if service.cfg == nil || !service.cfg.Home.Enabled || !service.cfg.Plugins.Enabled {
t.Fatalf("service cfg = %+v, want applied home config despite plugin sync failure", service.cfg)
}
if service.homePluginSyncKey != "" {
t.Fatalf("homePluginSyncKey = %q, want empty after plugin sync failure", service.homePluginSyncKey)
}
}
func TestStartHomeSubscriberDoesNotPreMarkPluginSync(t *testing.T) {
cfg := &config.Config{}
cfg.Home.Enabled = true
cfg.Home.Host = "127.0.0.1"
cfg.Home.Port = 1
cfg.Plugins.Enabled = true
cfg.Plugins.Configs = map[string]config.PluginInstanceConfig{}
service := &Service{cfg: cfg}
ctx, cancel := context.WithCancel(context.Background())
cancel()
service.startHomeSubscriber(ctx)
defer func() {
home.ClearCurrent()
if service.homeCancel != nil {
service.homeCancel()
}
if service.homeClient != nil {
service.homeClient.Close()
}
}()
if service.homePluginSyncKey != "" {
t.Fatalf("homePluginSyncKey = %q, want empty before a successful plugin sync", service.homePluginSyncKey)
}
}