From 3c773b6d92d17e377fc5b9ee8b94767aba389be5 Mon Sep 17 00:00:00 2001 From: hkfires <10558748+hkfires@users.noreply.github.com> Date: Wed, 10 Jun 2026 16:15:33 +0800 Subject: [PATCH] feat(auto-updater): refactor skip logic and add unit tests for autoUpdateSkipReason --- internal/managementasset/updater.go | 28 +++++++---- internal/managementasset/updater_test.go | 62 ++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 internal/managementasset/updater_test.go diff --git a/internal/managementasset/updater.go b/internal/managementasset/updater.go index ea7ca3f5..58499fa5 100644 --- a/internal/managementasset/updater.go +++ b/internal/managementasset/updater.go @@ -81,16 +81,8 @@ func runAutoUpdater(ctx context.Context) { runOnce := func() { cfg := currentConfigPtr.Load() - if cfg == nil { - log.Debug("management asset auto-updater skipped: config not yet available") - return - } - if cfg.RemoteManagement.DisableControlPanel { - log.Debug("management asset auto-updater skipped: control panel disabled") - return - } - if cfg.RemoteManagement.DisableAutoUpdatePanel { - log.Debug("management asset auto-updater skipped: disable-auto-update-panel is enabled") + if reason, skip := autoUpdateSkipReason(cfg); skip { + log.Debugf("management asset auto-updater skipped: %s", reason) return } @@ -111,6 +103,22 @@ func runAutoUpdater(ctx context.Context) { } } +func autoUpdateSkipReason(cfg *config.Config) (string, bool) { + if cfg == nil { + return "config not yet available", true + } + if cfg.Home.Enabled { + return "cluster mode enabled", true + } + if cfg.RemoteManagement.DisableControlPanel { + return "control panel disabled", true + } + if cfg.RemoteManagement.DisableAutoUpdatePanel { + return "disable-auto-update-panel is enabled", true + } + return "", false +} + func newHTTPClient(proxyURL string) *http.Client { client := &http.Client{Timeout: 15 * time.Second} diff --git a/internal/managementasset/updater_test.go b/internal/managementasset/updater_test.go new file mode 100644 index 00000000..82fdb291 --- /dev/null +++ b/internal/managementasset/updater_test.go @@ -0,0 +1,62 @@ +package managementasset + +import ( + "testing" + + "github.com/router-for-me/CLIProxyAPI/v7/internal/config" +) + +func TestAutoUpdateSkipReason(t *testing.T) { + tests := []struct { + name string + cfg *config.Config + wantReason string + wantSkip bool + }{ + { + name: "nil config", + cfg: nil, + wantReason: "config not yet available", + wantSkip: true, + }, + { + name: "cluster mode", + cfg: &config.Config{ + Home: config.HomeConfig{Enabled: true}, + }, + wantReason: "cluster mode enabled", + wantSkip: true, + }, + { + name: "control panel disabled", + cfg: &config.Config{ + RemoteManagement: config.RemoteManagement{DisableControlPanel: true}, + }, + wantReason: "control panel disabled", + wantSkip: true, + }, + { + name: "auto update disabled", + cfg: &config.Config{ + RemoteManagement: config.RemoteManagement{DisableAutoUpdatePanel: true}, + }, + wantReason: "disable-auto-update-panel is enabled", + wantSkip: true, + }, + { + name: "enabled", + cfg: &config.Config{}, + wantReason: "", + wantSkip: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotReason, gotSkip := autoUpdateSkipReason(tt.cfg) + if gotReason != tt.wantReason || gotSkip != tt.wantSkip { + t.Fatalf("autoUpdateSkipReason() = (%q, %t), want (%q, %t)", gotReason, gotSkip, tt.wantReason, tt.wantSkip) + } + }) + } +}