Files
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

139 lines
4.1 KiB
Go

package action
import (
"context"
"os"
"path/filepath"
"testing"
"time"
)
func TestRegistry(t *testing.T) {
for _, id := range []string{
"switch_virtual_desktop", "volume_up", "volume_down",
"toggle_mute", "show_notification", "delay",
} {
if !Known(id) {
t.Errorf("builtin action %q not registered", id)
}
}
if Known("no_such_action") {
t.Error("unknown action reported as known")
}
if _, err := New("no_such_action"); err == nil {
t.Error("New(unknown) should fail")
}
}
func TestSwitchDesktopValidate(t *testing.T) {
a, _ := New("switch_virtual_desktop")
// 有状态动作必须是单例:alternate 往返方向跨执行保持。
b, _ := New("switch_virtual_desktop")
if a != b {
t.Error("switch_virtual_desktop should be a singleton (alternate state)")
}
for _, mode := range []string{"alternate", "left", "right"} {
if err := a.Validate(map[string]any{"mode": mode}); err != nil {
t.Errorf("mode %q should pass: %v", mode, err)
}
}
if err := a.Validate(map[string]any{"mode": "up"}); err == nil {
t.Error("invalid mode should fail")
}
if err := a.Validate(map[string]any{}); err == nil {
t.Error("missing mode should fail")
}
// method 参数校验
for _, m := range []string{"com", "taskview", "hotkey", ""} {
if err := a.Validate(map[string]any{"mode": "left", "method": m}); err != nil {
t.Errorf("method %q should pass: %v", m, err)
}
}
if err := a.Validate(map[string]any{"mode": "left", "method": "cli"}); err == nil {
t.Error("invalid method should fail")
}
if err := a.Validate(map[string]any{"mode": "left", "taskview_delay_ms": -1}); err == nil {
t.Error("negative delay should fail")
}
}
func TestVolumeValidate(t *testing.T) {
up, _ := New("volume_up")
if err := up.Validate(map[string]any{}); err != nil {
t.Errorf("empty params should pass: %v", err)
}
if err := up.Validate(map[string]any{"steps": -1}); err == nil {
t.Error("negative steps should fail")
}
if err := up.Validate(map[string]any{"steps": 3}); err != nil {
t.Errorf("positive steps should pass: %v", err)
}
}
func TestNotificationValidate(t *testing.T) {
a, _ := New("show_notification")
if err := a.Validate(map[string]any{}); err == nil {
t.Error("missing title/text should fail")
}
if err := a.Validate(map[string]any{"title": "t", "text": "x"}); err != nil {
t.Errorf("valid params should pass: %v", err)
}
}
func TestDelay(t *testing.T) {
a, _ := New("delay")
if err := a.Validate(map[string]any{"ms": -1}); err == nil {
t.Error("negative ms should fail")
}
start := time.Now()
if err := a.Execute(context.Background(), map[string]any{"ms": 30}); err != nil {
t.Fatalf("execute: %v", err)
}
if elapsed := time.Since(start); elapsed < 25*time.Millisecond {
t.Errorf("delay too short: %v", elapsed)
}
// 取消传播
ctx, cancel := context.WithCancel(context.Background())
cancel()
if err := a.Execute(ctx, map[string]any{"ms": 5000}); err == nil {
t.Error("cancelled ctx should return error")
}
}
func TestLaunchApplicationValidate(t *testing.T) {
a, _ := New("launch_application")
if err := a.Validate(map[string]any{}); err == nil {
t.Error("missing path should fail")
}
if err := a.Validate(map[string]any{"path": "notepad.exe"}); err == nil {
t.Error("relative path should fail")
}
if err := a.Validate(map[string]any{"path": filepath.Join(t.TempDir(), "nope.exe")}); err == nil {
t.Error("nonexistent path should fail")
}
// 绝对路径 + 存在的文件(用测试二进制自身)
exe, err := os.Executable()
if err != nil {
t.Fatal(err)
}
if err := a.Validate(map[string]any{"path": exe}); err != nil {
t.Errorf("existing abs path should pass: %v", err)
}
}
func TestOpenURLValidate(t *testing.T) {
a, _ := New("open_url")
if err := a.Validate(map[string]any{"url": "https://example.com"}); err != nil {
t.Errorf("https should pass: %v", err)
}
if err := a.Validate(map[string]any{"url": "http://example.com"}); err != nil {
t.Errorf("http should pass: %v", err)
}
for _, bad := range []string{"", "file:///etc/passwd", "cmd://x", "javascript:alert(1)", "not a url"} {
if err := a.Validate(map[string]any{"url": bad}); err == nil {
t.Errorf("url %q should fail", bad)
}
}
}