package config import ( "encoding/json" "testing" ) // TestDefaultConfig 验证默认配置的内置规则与字段正确。 func TestDefaultConfig(t *testing.T) { cfg := Default() if cfg.Version != CurrentVersion { t.Fatalf("version = %d, want %d", cfg.Version, CurrentVersion) } if !cfg.Global.Enabled { t.Fatal("global.enabled should be true") } if !cfg.Global.Notify { t.Fatal("global.notify should default to true") } if len(cfg.Rules) != 6 { t.Fatalf("rules = %d, want 6", len(cfg.Rules)) } r0 := cfg.Rules[0] if r0.ID != "desktop-left-bottom" || r0.Trigger.Type != "mouse_corner" { t.Fatalf("rule 0 unexpected: %+v", r0) } if len(r0.Actions) != 1 || r0.Actions[0].Type != "switch_virtual_desktop" { t.Fatalf("rule 0 actions unexpected: %+v", r0.Actions) } if r0.CooldownMs != 800 { t.Fatalf("rule 0 cooldown = %d, want 800", r0.CooldownMs) } // 音量规则按方向分流(up/down 各一条,动作不互相抵消) r1, r2 := cfg.Rules[1], cfg.Rules[2] if r1.Trigger.Params["direction"] != "up" || r1.Actions[0].Type != "volume_up" { t.Fatalf("rule 1 unexpected: %+v", r1) } if r2.Trigger.Params["direction"] != "down" || r2.Actions[0].Type != "volume_down" { t.Fatalf("rule 2 unexpected: %+v", r2) } if len(r1.Actions) != 1 || len(r2.Actions) != 1 { t.Fatalf("volume rules should have exactly 1 action each") } // 示例 hotkey 规则默认关闭 if r3 := cfg.Rules[3]; r3.ID != "hotkey-switch-desktop" || r3.Enabled || r3.Trigger.Type != "hotkey" { t.Fatalf("rule 3 unexpected: %+v", r3) } // 滚轮音量按格生效:steps=4、无冷却 for _, r := range []Rule{r1, r2} { if r.CooldownMs != 0 { t.Fatalf("%s cooldown = %d, want 0", r.ID, r.CooldownMs) } if got := r.Actions[0].Params["steps"]; got != 4 { t.Fatalf("%s steps = %v, want 4", r.ID, got) } } // 屏幕底部滚轮调亮度(上滚调亮、下滚调暗) up, down := cfg.Rules[4], cfg.Rules[5] if up.ID != "brightness-bottom-wheel-up" || up.Actions[0].Type != "brightness_up" { t.Fatalf("brightness up rule unexpected: %+v", up) } if down.ID != "brightness-bottom-wheel-down" || down.Actions[0].Type != "brightness_down" { t.Fatalf("brightness down rule unexpected: %+v", down) } for _, r := range []Rule{up, down} { if r.Trigger.Type != "mouse_edge_wheel" || r.Trigger.Params["edge"] != "bottom" { t.Fatalf("%s trigger unexpected: %+v", r.ID, r.Trigger) } } } // TestTriggerConfigFlatJSON 验证扁平 JSON 被拆分为 Type + Params。 func TestTriggerConfigFlatJSON(t *testing.T) { var tc TriggerConfig if err := json.Unmarshal([]byte(`{"type":"mouse_corner","corner":"bottom_left","screen":"all","threshold_px":8}`), &tc); err != nil { t.Fatalf("unmarshal: %v", err) } if tc.Type != "mouse_corner" { t.Fatalf("type = %q, want mouse_corner", tc.Type) } if got := tc.Params["corner"]; got != "bottom_left" { t.Fatalf("corner = %v, want bottom_left", got) } if got := tc.Params["threshold_px"]; got != float64(8) { t.Fatalf("threshold_px = %v, want 8", got) } if _, ok := tc.Params["type"]; ok { t.Fatal("type should not remain in Params") } } // TestTriggerConfigMissingType 验证缺 type 时 Unmarshal 不报错但 Type 为空。 func TestTriggerConfigMissingType(t *testing.T) { var tc TriggerConfig if err := json.Unmarshal([]byte(`{"corner":"top_left"}`), &tc); err != nil { t.Fatalf("unmarshal: %v", err) } if tc.Type != "" { t.Fatalf("type = %q, want empty", tc.Type) } if len(tc.Params) != 1 { t.Fatalf("params = %v, want 1 entry", tc.Params) } } // TestTriggerConfigRoundTrip 验证扁平 JSON 往返对称(Marshal 不丢参数)。 func TestTriggerConfigRoundTrip(t *testing.T) { src := `{"type":"mouse_corner","corner":"bottom_left","screen":"all","threshold_px":8}` var tc TriggerConfig if err := json.Unmarshal([]byte(src), &tc); err != nil { t.Fatalf("unmarshal: %v", err) } out, err := json.Marshal(&tc) if err != nil { t.Fatalf("marshal: %v", err) } var back TriggerConfig if err := json.Unmarshal(out, &back); err != nil { t.Fatalf("unmarshal round trip: %v", err) } if back.Type != "mouse_corner" || back.Params["corner"] != "bottom_left" || back.Params["screen"] != "all" { t.Fatalf("round trip lost data: %s -> %s", src, out) } }