package config import ( "encoding/json" "os" "testing" ) // TestMigrateV1ToV2VolumeRules 验证旧版滚轮音量规则被升级为按格生效。 func TestMigrateV1ToV2VolumeRules(t *testing.T) { p := tmpConfig(t, `{"version":1,"global":{"enabled":true,"theme":"system","log_level":"info"}, "rules":[{"id":"volume-top-wheel-up","name":"顶部滚轮增大音量","enabled":true,"priority":100, "trigger":{"type":"mouse_edge_wheel","edge":"top","direction":"up","threshold_px":32}, "actions":[{"type":"volume_up","params":{"steps":1}}],"cooldown_ms":50}]}`) cfg, err := loadAt(p) if err != nil { t.Fatalf("load: %v", err) } if cfg.Version != CurrentVersion { t.Fatalf("version = %d, want %d", cfg.Version, CurrentVersion) } if len(cfg.Rules) != 3 { t.Fatalf("rules = %d, want 3 (1 volume + 2 brightness)", len(cfg.Rules)) } r := cfg.Rules[0] if r.CooldownMs != 0 { t.Fatalf("cooldown = %d, want 0", r.CooldownMs) } if got := r.Actions[0].Params["steps"]; got != 4 { t.Fatalf("steps = %v, want 4", got) } last := cfg.Rules[2] if last.ID != "brightness-bottom-wheel-down" || last.Actions[0].Type != "brightness_down" { t.Fatalf("brightness rule not appended: %+v", last) } // 迁移结果必须落盘,重启后不再重复迁移 data, _ := os.ReadFile(p) var saved Config if err := json.Unmarshal(data, &saved); err != nil { t.Fatalf("saved json: %v", err) } if saved.Version != CurrentVersion { t.Fatalf("saved version = %d, want %d", saved.Version, CurrentVersion) } } // TestMigrateV1ToV2KeepsExplicitBrightness 验证已有亮度规则的配置不会重复追加。 func TestMigrateV1ToV2KeepsExplicitBrightness(t *testing.T) { p := tmpConfig(t, `{"version":1,"rules":[{"id":"my-bright","name":"我的亮度","enabled":true,"priority":100, "trigger":{"type":"mouse_edge_wheel","edge":"bottom","direction":"up","threshold_px":32}, "actions":[{"type":"brightness_up","params":{}}],"cooldown_ms":0}]}`) cfg, err := loadAt(p) if err != nil { t.Fatalf("load: %v", err) } if len(cfg.Rules) != 1 || cfg.Rules[0].ID != "my-bright" { t.Fatalf("rules = %+v, want only user rule", cfg.Rules) } } // TestMigrateV1ToV2EmptyRules 验证空规则配置不会被灌入默认亮度规则。 func TestMigrateV1ToV2EmptyRules(t *testing.T) { p := tmpConfig(t, `{"version":1,"rules":[]}`) cfg, err := loadAt(p) if err != nil { t.Fatalf("load: %v", err) } if len(cfg.Rules) != 0 { t.Fatalf("rules = %d, want 0", len(cfg.Rules)) } }