迁移到 mygo 单进程架构并接入签名自更新
- 单进程:托盘常驻 + 原生设置窗口(internal/settings),移除 Wails 前端与 IPC - 触发器(鼠标角落 / 边缘滚轮 / 热键)+ 规则引擎 + 动作注册表 - 调节提示条:截屏 + glass.Blur 三层毛玻璃、背景跟随屏幕变化、悬停光感与拖动 - 音量 Core Audio COM、亮度 DDC/CI(gamma 兜底)、多显示器求平均 - 配置热重载 + 版本迁移 v1→v5、便携模式、崩溃取证三件套 - updater/native 插件 + 托盘「检查更新…」,watchdog 识别更新重启 - 自建 Gitea 发布:scripts/publish-gitea.ps1(updates.url 指向 latest Release)
This commit is contained in:
1 parent
814012ec52
commit
0b8b9b0775
202 files changed
+7254
-13572
No files matched your search
+131
-13
@@ -14,7 +14,13 @@ import (
|
||||
const MaxConfigSize = 1 << 20 // 1MB
|
||||
|
||||
// CurrentVersion 为当前配置格式版本。
|
||||
const CurrentVersion = 1
|
||||
// v2:滚轮音量按"格"生效(steps 提升到 4、冷却清零),并新增屏幕底部
|
||||
// 滚轮调亮度规则。
|
||||
// v3:全局新增 notify(调节时是否弹出桌面通知,默认开启;音量/静音的
|
||||
// 自动通知默认关闭,见动作参数 toast)。
|
||||
// v4:每条规则新增 notify(该规则是否弹通知,默认开启),global.notify
|
||||
// 仍作为总开关。
|
||||
const CurrentVersion = 5
|
||||
|
||||
// ErrCorrupted 表示配置损坏、已备份并恢复为默认配置(可继续运行)。
|
||||
var ErrCorrupted = errors.New("config corrupted, recovered from default")
|
||||
@@ -31,16 +37,61 @@ type Config struct {
|
||||
|
||||
// GlobalConfig 为全局设置。
|
||||
type GlobalConfig struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Theme string `json:"theme"`
|
||||
LogLevel string `json:"log_level"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Notify bool `json:"notify"` // 调节音量/亮度时是否弹桌面通知
|
||||
Theme string `json:"theme"`
|
||||
LogLevel string `json:"log_level"`
|
||||
OSD OSDConfig `json:"osd"` // 调节提示条的位置
|
||||
}
|
||||
|
||||
// OSDConfig 为调节提示条(音量/亮度胶囊)的位置设置。
|
||||
//
|
||||
// Position 为 "top" / "bottom" / "left" / "right":前两者在水平方向居中,
|
||||
// 后两者在垂直方向居中;Margin 为该边与显示器工作区边缘的距离(DIP)。
|
||||
type OSDConfig struct {
|
||||
Position string `json:"position"`
|
||||
Margin int `json:"margin"`
|
||||
}
|
||||
|
||||
// 提示条位置的合法取值。
|
||||
const (
|
||||
OSDPositionTop = "top"
|
||||
OSDPositionBottom = "bottom"
|
||||
OSDPositionLeft = "left"
|
||||
OSDPositionRight = "right"
|
||||
)
|
||||
|
||||
// DefaultOSD 返回提示条位置的默认值:底部居中、距底边 64 DIP。
|
||||
func DefaultOSD() OSDConfig {
|
||||
return OSDConfig{Position: OSDPositionBottom, Margin: 64}
|
||||
}
|
||||
|
||||
// OSDMaxMargin 为提示条与屏幕边缘距离的上限(DIP);实际显示时会再按
|
||||
// 显示器工作区尺寸钳制,保证胶囊不会跑到屏幕外。
|
||||
const OSDMaxMargin = 1000
|
||||
|
||||
// OSDMarginValid 报告提示条边距是否在允许范围内。
|
||||
func (o OSDConfig) OSDMarginValid() bool {
|
||||
return o.Margin >= 0 && o.Margin <= OSDMaxMargin
|
||||
}
|
||||
|
||||
// OSDPositionValid 报告提示条位置字符串是否合法(空值视为默认底部)。
|
||||
func (o OSDConfig) OSDPositionValid() bool {
|
||||
switch o.Position {
|
||||
case "", OSDPositionTop, OSDPositionBottom, OSDPositionLeft, OSDPositionRight:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Rule 为一条规则:触发器 + 条件(MVP 仅空)+ 动作链。
|
||||
type Rule struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Enabled bool `json:"enabled"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Enabled bool `json:"enabled"`
|
||||
// Notify 控制本规则触发时是否弹右下角系统通知;调节提示条不受影响。
|
||||
// 与 global.notify 是"与"关系:两者都开启才弹通知。
|
||||
Notify bool `json:"notify"`
|
||||
Priority int `json:"priority"`
|
||||
Trigger TriggerConfig `json:"trigger"`
|
||||
Conditions []ConditionConfig `json:"conditions,omitempty"`
|
||||
@@ -106,20 +157,23 @@ type ActionConfig struct {
|
||||
Params map[string]any `json:"params"`
|
||||
}
|
||||
|
||||
// Default 返回内置默认配置(三条首批规则)。
|
||||
// Default 返回内置默认配置(虚拟桌面/音量/亮度/快捷键示例规则)。
|
||||
func Default() *Config {
|
||||
return &Config{
|
||||
Version: 1,
|
||||
Version: CurrentVersion,
|
||||
Global: GlobalConfig{
|
||||
Enabled: true,
|
||||
Notify: true,
|
||||
Theme: "system",
|
||||
LogLevel: "info",
|
||||
OSD: DefaultOSD(),
|
||||
},
|
||||
Rules: []Rule{
|
||||
{
|
||||
ID: "desktop-left-bottom",
|
||||
Name: "左下角切换虚拟桌面",
|
||||
Enabled: true,
|
||||
Notify: true,
|
||||
Priority: 100,
|
||||
Trigger: TriggerConfig{Type: "mouse_corner", Params: map[string]any{"corner": "bottom_left", "screen": "all", "threshold_px": 32}},
|
||||
Actions: []ActionConfig{{Type: "switch_virtual_desktop", Params: map[string]any{"mode": "alternate"}}},
|
||||
@@ -129,33 +183,59 @@ func Default() *Config {
|
||||
ID: "volume-top-wheel-up",
|
||||
Name: "顶部边缘滚轮增大音量",
|
||||
Enabled: true,
|
||||
Notify: true,
|
||||
Priority: 100,
|
||||
Trigger: TriggerConfig{Type: "mouse_edge_wheel", Params: map[string]any{"edge": "top", "direction": "up", "threshold_px": 32}},
|
||||
Actions: []ActionConfig{{Type: "volume_up", Params: map[string]any{}}},
|
||||
CooldownMs: 50,
|
||||
Actions: []ActionConfig{{Type: "volume_up", Params: map[string]any{"steps": 4}}},
|
||||
CooldownMs: 0,
|
||||
},
|
||||
{
|
||||
ID: "volume-top-wheel-down",
|
||||
Name: "顶部边缘滚轮减小音量",
|
||||
Enabled: true,
|
||||
Notify: true,
|
||||
Priority: 100,
|
||||
Trigger: TriggerConfig{Type: "mouse_edge_wheel", Params: map[string]any{"edge": "top", "direction": "down", "threshold_px": 32}},
|
||||
Actions: []ActionConfig{{Type: "volume_down", Params: map[string]any{}}},
|
||||
CooldownMs: 50,
|
||||
Actions: []ActionConfig{{Type: "volume_down", Params: map[string]any{"steps": 4}}},
|
||||
CooldownMs: 0,
|
||||
},
|
||||
{
|
||||
ID: "hotkey-switch-desktop",
|
||||
Name: "示例:Ctrl+Alt+D 切换桌面(默认关闭)",
|
||||
Enabled: false,
|
||||
Notify: true,
|
||||
Priority: 50,
|
||||
Trigger: TriggerConfig{Type: "hotkey", Params: map[string]any{"hotkey": "ctrl+alt+d"}},
|
||||
Actions: []ActionConfig{{Type: "switch_virtual_desktop", Params: map[string]any{"mode": "alternate"}}},
|
||||
CooldownMs: 400,
|
||||
},
|
||||
defaultBrightnessRule("up"),
|
||||
defaultBrightnessRule("down"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// defaultBrightnessRule 构造屏幕底部边缘滚轮调亮/调暗规则。
|
||||
func defaultBrightnessRule(dir string) Rule {
|
||||
actionType, label := "brightness_up", "调亮"
|
||||
if dir == "down" {
|
||||
actionType, label = "brightness_down", "调暗"
|
||||
}
|
||||
return Rule{
|
||||
ID: "brightness-bottom-wheel-" + dir,
|
||||
Name: "屏幕底部滚轮" + label,
|
||||
Enabled: true,
|
||||
Notify: true,
|
||||
Priority: 100,
|
||||
Trigger: TriggerConfig{Type: "mouse_edge_wheel", Params: map[string]any{"edge": "bottom", "direction": dir, "threshold_px": 32}},
|
||||
Actions: []ActionConfig{{Type: actionType, Params: map[string]any{"steps": 5}}},
|
||||
// 滚轮事件按"格"上报,冷却清零才能让快速连滚每一格都生效;
|
||||
// 动作层有合并队列兜底,不会因为 DDC 慢而堆积。
|
||||
CooldownMs: 0,
|
||||
Fullscreen: "ignore",
|
||||
}
|
||||
}
|
||||
|
||||
// AppDataDir 返回应用数据目录。
|
||||
// 便携模式:exe 同目录存在 config.json 时,配置/日志跟随 exe 目录
|
||||
// (绿色便携版);否则使用 %LOCALAPPDATA%\ZerOriginTool(安装版默认)。
|
||||
@@ -206,6 +286,42 @@ func Load() (*Config, error) {
|
||||
return loadAt(path)
|
||||
}
|
||||
|
||||
// Validate 校验全局设置,返回可直接展示给用户的问题描述。
|
||||
// 加载、热重载与保存都会调用;有问题时调用方应回退到默认值而不是拒绝加载。
|
||||
func (c *Config) Validate() []string {
|
||||
var out []string
|
||||
if !c.Global.OSD.OSDPositionValid() {
|
||||
out = append(out, fmt.Sprintf("提示条位置 %q 无效(应为 top/bottom/left/right)", c.Global.OSD.Position))
|
||||
}
|
||||
if !c.Global.OSD.OSDMarginValid() {
|
||||
out = append(out, fmt.Sprintf("提示条边距 %d 超出范围(0–%d)", c.Global.OSD.Margin, OSDMaxMargin))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Normalize 修正可以自动修复的全局设置(目前是提示条位置与边距),
|
||||
// 返回被修正的字段数。加载与热重载都会调用,保证运行期一定拿到合法值;
|
||||
// 与 osd.SetPosition 的归一化重复是刻意的双保险(配置层负责持久化数据)。
|
||||
func (c *Config) Normalize() int {
|
||||
fixed := 0
|
||||
if !c.Global.OSD.OSDPositionValid() {
|
||||
c.Global.OSD.Position = OSDPositionBottom
|
||||
fixed++
|
||||
}
|
||||
if c.Global.OSD.Position == "" {
|
||||
c.Global.OSD.Position = OSDPositionBottom
|
||||
}
|
||||
if !c.Global.OSD.OSDMarginValid() {
|
||||
if c.Global.OSD.Margin < 0 {
|
||||
c.Global.OSD.Margin = 0
|
||||
} else {
|
||||
c.Global.OSD.Margin = OSDMaxMargin
|
||||
}
|
||||
fixed++
|
||||
}
|
||||
return fixed
|
||||
}
|
||||
|
||||
// loadAt 在指定路径上执行完整加载流程(测试可注入临时路径)。
|
||||
func loadAt(path string) (*Config, error) {
|
||||
fi, err := os.Stat(path)
|
||||
@@ -240,6 +356,8 @@ func loadAt(path string) (*Config, error) {
|
||||
return nil, fmt.Errorf("save migrated config: %w", err)
|
||||
}
|
||||
}
|
||||
// 手工改坏的全局设置就地修正(不写盘),避免非法值传到运行期。
|
||||
cfg.Normalize()
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -5,17 +5,20 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestDefaultConfig 验证默认配置包含两条内置规则且字段正确。
|
||||
// TestDefaultConfig 验证默认配置的内置规则与字段正确。
|
||||
func TestDefaultConfig(t *testing.T) {
|
||||
cfg := Default()
|
||||
if cfg.Version != 1 {
|
||||
t.Fatalf("version = %d, want 1", cfg.Version)
|
||||
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 len(cfg.Rules) != 4 {
|
||||
t.Fatalf("rules = %d, want 4", len(cfg.Rules))
|
||||
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" {
|
||||
@@ -42,6 +45,28 @@ func TestDefaultConfig(t *testing.T) {
|
||||
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。
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
package config
|
||||
|
||||
// v2 迁移:滚轮音量从"按次"改成"按格"。
|
||||
//
|
||||
// v1 的鼠标滚轮事件被 120ms 窗口合并,媒体键路径又把 steps 解释成
|
||||
// "按键次数"(默认 1 次 ≈ 2%),结果快速滚三四格只有一次生效、音量
|
||||
// 变化很小。v2 起触发器按"格"(120 的整数倍)上报,动作的 steps 表示
|
||||
// 每格改变的百分比(默认 4),因此:
|
||||
// - 既有滚轮音量规则的 steps <= 1 提升到 4;
|
||||
// - 这些规则的冷却清零,连续滚动的每一格都能立即命中;
|
||||
// - 配置里已有滚轮规则但还没有亮度规则时,补两条屏幕底部调光规则。
|
||||
func init() {
|
||||
RegisterMigration(1, migrateV1ToV2)
|
||||
}
|
||||
|
||||
// migrateV1ToV2 在不修改入参的情况下返回 v2 配置。
|
||||
func migrateV1ToV2(cfg *Config) (*Config, error) {
|
||||
out := *cfg
|
||||
out.Version = 2
|
||||
out.Rules = cloneRules(cfg.Rules)
|
||||
|
||||
hasWheel, hasBrightness := false, false
|
||||
for i := range out.Rules {
|
||||
r := &out.Rules[i]
|
||||
if r.Trigger.Type == "mouse_edge_wheel" {
|
||||
hasWheel = true
|
||||
}
|
||||
for j := range r.Actions {
|
||||
a := &r.Actions[j]
|
||||
switch a.Type {
|
||||
case "volume_up", "volume_down":
|
||||
if intParam(a.Params, "steps") <= 1 {
|
||||
if a.Params == nil {
|
||||
a.Params = map[string]any{}
|
||||
}
|
||||
a.Params["steps"] = 4
|
||||
}
|
||||
if r.CooldownMs > 0 && r.CooldownMs < 1000 {
|
||||
r.CooldownMs = 0
|
||||
}
|
||||
case "brightness_up", "brightness_down":
|
||||
hasBrightness = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if hasWheel && !hasBrightness {
|
||||
out.Rules = append(out.Rules, defaultBrightnessRule("up"), defaultBrightnessRule("down"))
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
// cloneRules 复制规则切片及动作参数,避免迁移改到调用方仍在用的配置。
|
||||
func cloneRules(rules []Rule) []Rule {
|
||||
out := make([]Rule, len(rules))
|
||||
for i, r := range rules {
|
||||
out[i] = r
|
||||
out[i].Actions = make([]ActionConfig, len(r.Actions))
|
||||
for j, a := range r.Actions {
|
||||
na := a
|
||||
if a.Params != nil {
|
||||
p := make(map[string]any, len(a.Params))
|
||||
for k, v := range a.Params {
|
||||
p[k] = v
|
||||
}
|
||||
na.Params = p
|
||||
}
|
||||
out[i].Actions[j] = na
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// intParam 读取参数里的整数(JSON 解析得到 float64,运行时可能是 int)。
|
||||
func intParam(params map[string]any, name string) int {
|
||||
switch v := params[name].(type) {
|
||||
case float64:
|
||||
return int(v)
|
||||
case int:
|
||||
return v
|
||||
case int32:
|
||||
return int(v)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
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))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package config
|
||||
|
||||
// v3 迁移:新增全局"调节时显示通知"开关(notify),默认开启。
|
||||
//
|
||||
// v2 及更早的配置没有该字段,反序列化后为零值 false。若不迁移,旧用户
|
||||
// 升级后会被意外地关掉全部调节通知;因此这里显式补成 true,并把版本
|
||||
// 推到 v3 落盘。音量/静音动作另有 toast 参数(默认 false),不依赖本开关。
|
||||
func init() {
|
||||
RegisterMigration(2, migrateV2ToV3)
|
||||
}
|
||||
|
||||
// migrateV2ToV3 在不修改入参的情况下返回 v3 配置。
|
||||
func migrateV2ToV3(cfg *Config) (*Config, error) {
|
||||
out := *cfg
|
||||
out.Version = 3
|
||||
out.Global.Notify = true
|
||||
return &out, nil
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package config
|
||||
|
||||
// v4 迁移:新增规则级"显示通知"开关(rule.notify),默认开启。
|
||||
//
|
||||
// v3 及更早的配置没有该字段,反序列化后为零值 false。若不迁移,
|
||||
// 升级后每条规则都会静默关掉通知;因此这里把现有规则全部补成 true,
|
||||
// 并把版本推到 v4。新规则在设置界面和默认配置里也都是 true。
|
||||
func init() {
|
||||
RegisterMigration(3, migrateV3ToV4)
|
||||
}
|
||||
|
||||
// migrateV3ToV4 在不修改入参的情况下返回 v4 配置。
|
||||
func migrateV3ToV4(cfg *Config) (*Config, error) {
|
||||
out := *cfg
|
||||
out.Version = 4
|
||||
rules := make([]Rule, len(cfg.Rules))
|
||||
copy(rules, cfg.Rules)
|
||||
for i := range rules {
|
||||
rules[i].Notify = true
|
||||
}
|
||||
out.Rules = rules
|
||||
return &out, nil
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package config
|
||||
|
||||
// v5 迁移:新增提示条位置设置(global.osd.position / global.osd.margin)。
|
||||
//
|
||||
// v4 及更早的配置没有该字段,反序列化后为零值(position==""、margin==0)。
|
||||
// 零值边距会让胶囊贴着屏幕底边,因此迁移时统一补成默认的"底部居中、64 DIP",
|
||||
// 与旧版本的实际表现完全一致,并把版本推到 v5。
|
||||
func init() {
|
||||
RegisterMigration(4, migrateV4ToV5)
|
||||
}
|
||||
|
||||
// migrateV4ToV5 在不修改入参的情况下返回 v5 配置。
|
||||
func migrateV4ToV5(cfg *Config) (*Config, error) {
|
||||
out := *cfg
|
||||
out.Version = 5
|
||||
out.Global.OSD = DefaultOSD()
|
||||
return &out, nil
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestMigrateV4ToV5AddsOSDDefault 验证 v4 配置迁移后被补上默认的提示条位置。
|
||||
func TestMigrateV4ToV5AddsOSDDefault(t *testing.T) {
|
||||
raw := `{
|
||||
"version": 4,
|
||||
"global": {"enabled": true, "notify": false, "theme": "dark", "log_level": "debug"},
|
||||
"rules": []
|
||||
}`
|
||||
var cfg Config
|
||||
if err := json.Unmarshal([]byte(raw), &cfg); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
out, err := migrate(&cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
if out.Version != 5 {
|
||||
t.Fatalf("version = %d, want 5", out.Version)
|
||||
}
|
||||
if want := DefaultOSD(); out.Global.OSD != want {
|
||||
t.Fatalf("osd = %+v, want %+v", out.Global.OSD, want)
|
||||
}
|
||||
// 其他全局设置不能被迁移改掉。
|
||||
if !out.Global.Enabled || out.Global.Notify || out.Global.Theme != "dark" || out.Global.LogLevel != "debug" {
|
||||
t.Fatalf("global changed by migration: %+v", out.Global)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDefaultConfigHasOSD 验证默认配置带合法的提示条位置。
|
||||
func TestDefaultConfigHasOSD(t *testing.T) {
|
||||
cfg := Default()
|
||||
if cfg.Global.OSD != DefaultOSD() {
|
||||
t.Fatalf("default osd = %+v", cfg.Global.OSD)
|
||||
}
|
||||
if problems := cfg.Validate(); len(problems) > 0 {
|
||||
t.Fatalf("default config invalid: %v", problems)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNormalizeOSD 验证加载时的自动修正:非法位置回到底部、空位置补全、
|
||||
// 越界边距被钳制,合法配置保持不变。
|
||||
func TestNormalizeOSD(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
in OSDConfig
|
||||
want OSDConfig
|
||||
}{
|
||||
{"未知位置", OSDConfig{Position: "center", Margin: 30}, OSDConfig{Position: "bottom", Margin: 30}},
|
||||
{"空位置补底部", OSDConfig{Position: "", Margin: 30}, OSDConfig{Position: "bottom", Margin: 30}},
|
||||
{"负边距归零", OSDConfig{Position: "top", Margin: -8}, OSDConfig{Position: "top", Margin: 0}},
|
||||
{"过大边距封顶", OSDConfig{Position: "left", Margin: 5000}, OSDConfig{Position: "left", Margin: OSDMaxMargin}},
|
||||
{"合法不动", OSDConfig{Position: "right", Margin: 40}, OSDConfig{Position: "right", Margin: 40}},
|
||||
}
|
||||
for _, c := range cases {
|
||||
cfg := Default()
|
||||
cfg.Global.OSD = c.in
|
||||
cfg.Normalize()
|
||||
if cfg.Global.OSD != c.want {
|
||||
t.Errorf("%s: osd = %+v, want %+v", c.name, cfg.Global.OSD, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateOSD 验证位置与边距的校验边界。
|
||||
func TestValidateOSD(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
osd OSDConfig
|
||||
bad bool
|
||||
}{
|
||||
{"bottom+64", OSDConfig{Position: "bottom", Margin: 64}, false},
|
||||
{"top+0", OSDConfig{Position: "top", Margin: 0}, false},
|
||||
{"left+120", OSDConfig{Position: "left", Margin: 120}, false},
|
||||
{"right+1000", OSDConfig{Position: "right", Margin: OSDMaxMargin}, false},
|
||||
{"empty position", OSDConfig{Position: "", Margin: 8}, false},
|
||||
{"unknown position", OSDConfig{Position: "center", Margin: 8}, true},
|
||||
{"negative margin", OSDConfig{Position: "top", Margin: -1}, true},
|
||||
{"margin too big", OSDConfig{Position: "top", Margin: OSDMaxMargin + 1}, true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
cfg := Default()
|
||||
cfg.Global.OSD = c.osd
|
||||
got := len(cfg.Validate()) > 0
|
||||
if got != c.bad {
|
||||
t.Fatalf("%s: Validate bad = %v, want %v", c.name, got, c.bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package config
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -29,8 +30,8 @@ func TestLoadCreatesDefaultWhenMissing(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("load: %v", err)
|
||||
}
|
||||
if len(cfg.Rules) != 4 {
|
||||
t.Fatalf("rules = %d, want 4", len(cfg.Rules))
|
||||
if len(cfg.Rules) != 6 {
|
||||
t.Fatalf("rules = %d, want 6", len(cfg.Rules))
|
||||
}
|
||||
if _, err := os.Stat(p); err != nil {
|
||||
t.Fatalf("default config not written: %v", err)
|
||||
@@ -43,7 +44,7 @@ func TestLoadCorruptedRecoversWithBackup(t *testing.T) {
|
||||
if !errors.Is(err, ErrCorrupted) {
|
||||
t.Fatalf("err = %v, want ErrCorrupted", err)
|
||||
}
|
||||
if cfg == nil || len(cfg.Rules) != 4 {
|
||||
if cfg == nil || len(cfg.Rules) != 6 {
|
||||
t.Fatalf("should recover to default config, got %+v", cfg)
|
||||
}
|
||||
if _, err := os.Stat(p + ".bak"); err != nil {
|
||||
@@ -78,7 +79,7 @@ func TestSaveAtomic(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("load saved: %v", err)
|
||||
}
|
||||
if loaded.Version != 1 || len(loaded.Rules) != 4 {
|
||||
if loaded.Version != CurrentVersion || len(loaded.Rules) != 6 {
|
||||
t.Fatalf("saved content wrong: %+v", loaded)
|
||||
}
|
||||
// 无临时文件残留
|
||||
@@ -123,12 +124,12 @@ func TestMigrationFramework(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("load with migration: %v", err)
|
||||
}
|
||||
if cfg.Version != 1 {
|
||||
t.Fatalf("version = %d, want 1 after migration", cfg.Version)
|
||||
if cfg.Version != CurrentVersion {
|
||||
t.Fatalf("version = %d, want %d after migration", cfg.Version, CurrentVersion)
|
||||
}
|
||||
// 迁移结果应已保存
|
||||
data, _ := os.ReadFile(p)
|
||||
if !strings.Contains(string(data), `"version": 1`) {
|
||||
if !strings.Contains(string(data), fmt.Sprintf(`"version": %d`, CurrentVersion)) {
|
||||
t.Fatalf("migrated config not persisted: %s", data)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user