feat(auto-updater): refactor skip logic and add unit tests for autoUpdateSkipReason

This commit is contained in:
hkfires
2026-06-10 16:15:33 +08:00
parent 21387f5c71
commit 3c773b6d92
2 changed files with 80 additions and 10 deletions
+18 -10
View File
@@ -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}
+62
View File
@@ -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)
}
})
}
}