Files
zerorigin-toolbox/internal/config/config_test.go
T
zerorigin2019 0a1c1e82ea chore: 初始化仓库(零源工具箱 0.1.0 基线)
- Go 主进程 cmd/zerorigin(托盘常驻 + 触发器/规则/动作引擎)
- Wails v3 设置进程 cmd/settings(独立 module)
- 监督进程 cmd/watchdog(-supervise 异常重启)
- 构建脚本 build.bat / NSIS 安装包 deploy/、便携版 portable/
- 架构与 MVP 计划文档 docs/、阶段 0-4 验收记录 deploy/acceptance-0-4.md
2026-08-05 00:42:45 +08:00

100 lines
3.2 KiB
Go

package config
import (
"encoding/json"
"testing"
)
// TestDefaultConfig 验证默认配置包含两条内置规则且字段正确。
func TestDefaultConfig(t *testing.T) {
cfg := Default()
if cfg.Version != 1 {
t.Fatalf("version = %d, want 1", cfg.Version)
}
if !cfg.Global.Enabled {
t.Fatal("global.enabled should be true")
}
if len(cfg.Rules) != 4 {
t.Fatalf("rules = %d, want 4", 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)
}
}
// 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)
}
}