package config import ( "encoding/json" "os" "strings" "testing" ) // TestMigrateV2ToV3EnablesNotify 验证旧配置升级后默认开启调节通知, // 避免 v2 缺失 notify 字段被零值静音。 func TestMigrateV2ToV3EnablesNotify(t *testing.T) { p := tmpConfig(t, `{"version":2,"global":{"enabled":true,"theme":"system","log_level":"info"},"rules":[]}`) 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 should be true after v2→v3 migration") } 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.Global.Notify { t.Fatalf("migrated config not persisted: %s", data) } if !strings.Contains(string(data), `"notify": true`) { t.Fatalf("notify missing from migrated json: %s", data) } } // TestV3NotifyFalsePreserved 验证已显式关闭通知的 v3 配置不会被重新打开。 func TestV3NotifyFalsePreserved(t *testing.T) { p := tmpConfig(t, `{"version":3,"global":{"enabled":true,"notify":false,"theme":"system","log_level":"info"},"rules":[]}`) cfg, err := loadAt(p) if err != nil { t.Fatalf("load: %v", err) } if cfg.Global.Notify { t.Fatal("explicit notify=false should be preserved") } }