package config import ( "encoding/json" "os" "strings" "testing" ) // TestMigrateV3ToV4EnablesRuleNotify 验证升级到 v4 后每条规则默认开启通知, // 避免旧配置缺字段导致规则通知被零值静音。 func TestMigrateV3ToV4EnablesRuleNotify(t *testing.T) { p := tmpConfig(t, `{"version":3,"global":{"enabled":true,"notify":false,"theme":"system","log_level":"info"},"rules":[ {"id":"a","name":"A","enabled":true,"notify":false,"priority":10,"trigger":{"type":"mouse_corner","params":{"corner":"top_left","threshold_px":4}},"actions":[{"type":"volume_up","params":{"steps":1}}]}, {"id":"b","name":"B","enabled":false,"priority":20,"trigger":{"type":"mouse_corner","params":{"corner":"bottom_right","threshold_px":4}},"actions":[{"type":"volume_down","params":{"steps":1}}]} ]}`) 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 cfg.Global.Notify { t.Fatal("global.notify=false should be preserved") } if len(cfg.Rules) != 2 { t.Fatalf("rules = %d, want 2", len(cfg.Rules)) } for i := range cfg.Rules { if !cfg.Rules[i].Notify { t.Fatalf("rule %q notify should default to true", cfg.Rules[i].ID) } } 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 || !saved.Rules[0].Notify || !saved.Rules[1].Notify { t.Fatalf("migrated config not persisted: %s", data) } if !strings.Contains(string(data), `"notify": true`) { t.Fatalf("rule notify missing from migrated json: %s", data) } } // TestV4RuleNotifyFalsePreserved 验证已经显式关闭的规则通知不会被迁移重新打开。 func TestV4RuleNotifyFalsePreserved(t *testing.T) { p := tmpConfig(t, `{"version":4,"rules":[{"id":"quiet","name":"静默","enabled":true,"notify":false,"priority":10,"trigger":{"type":"mouse_corner","params":{"corner":"top_left","threshold_px":4}},"actions":[{"type":"volume_up","params":{"steps":1}}]}]}`) cfg, err := loadAt(p) if err != nil { t.Fatalf("load: %v", err) } if cfg.Rules[0].Notify { t.Fatal("explicit rule notify=false should be preserved") } }