diff --git a/internal/service/bot_features_test.go b/internal/service/bot_features_test.go index 72d975d..ac17c01 100644 --- a/internal/service/bot_features_test.go +++ b/internal/service/bot_features_test.go @@ -319,6 +319,34 @@ func TestBotAdminCommandsManageDevicePolicy(t *testing.T) { } } +func TestBotCleanupRulesCanBeDeletedUntilEmpty(t *testing.T) { + ctx := context.Background() + repos, bot := newBotTestService(t) + admin := &model.User{Username: "root", PasswordHash: "x", Role: "admin", IsActive: true} + if err := repos.User.Create(ctx, admin); err != nil { + t.Fatal(err) + } + channel := &model.NotifyChannel{Name: "Telegram", Type: "telegram", Enabled: true, Config: `{"admin_user_ids":"9001"}`} + msg := &TelegramMessage{From: TelegramUser{ID: 9001, Username: "root"}, Chat: TelegramChat{ID: 9001, Type: "private"}} + + reply, err := bot.executeCommand(ctx, channel, msg, "/cleanup_rule del watch_3_5d_6h") + if err != nil { + t.Fatal(err) + } + cfg := loadBotConfig(ctx, repos) + if len(cfg.AccountCleanupRules) != 0 { + t.Fatalf("cleanup rules should stay empty after deleting the last rule; reply=%q rules=%+v", reply.Text, cfg.AccountCleanupRules) + } + + reply, err = bot.executeCommand(ctx, channel, msg, "/cleanup_rule list") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(reply.Text, "暂无规则") { + t.Fatalf("expected empty rule list, got %q", reply.Text) + } +} + func TestBotRegistrationCommandUsesOpenRegQuota(t *testing.T) { ctx := context.Background() repos, bot := newBotTestService(t) diff --git a/internal/service/bot_settings.go b/internal/service/bot_settings.go index a75643f..1657f97 100644 --- a/internal/service/bot_settings.go +++ b/internal/service/bot_settings.go @@ -141,7 +141,7 @@ func loadBotConfig(ctx context.Context, repo *repository.Container) botConfig { cfg.AccountCleanupRequiredCount = parseIntSettingDefault(get(SettingAccountCleanupRequiredCount), cfg.AccountCleanupRequiredCount) if raw := strings.TrimSpace(get(SettingAccountCleanupRules)); raw != "" { var rules []accountCleanupRule - if err := json.Unmarshal([]byte(raw), &rules); err == nil && len(rules) > 0 { + if err := json.Unmarshal([]byte(raw), &rules); err == nil { cfg.AccountCleanupRules = normalizeCleanupRules(rules) } }