- 活动切换并发安全:App/Scheduler 增加 client/cfg 读写锁与快照访问, 支持运行中热替换客户端,切换活动不再静默覆盖用户自定义 timeApiUrl - 启动失败时写入崩溃日志文件便于事后排查 - 前端类型声明从 Wails 自动生成的绑定派生,避免手写契约漂移 - 提取 parseAccountFile 消除 ExportAccounts/OrganizeAccounts 重复解析逻辑 - 删除 utils 中未使用的 ReadJSON/WriteJSON/WriteError 及空目录 internal/app - gofmt 统一格式化(activity/types/scheduler 字段对齐) - 新增 logger/seckill/utils 单元测试 - 清理 git 中遗留产物(frontend/app.js、yml.txt、其他日志/)并补全 .gitignore
82 lines
2.4 KiB
Go
82 lines
2.4 KiB
Go
package logger
|
||
|
||
import (
|
||
"testing"
|
||
)
|
||
|
||
func TestIsLogLevel(t *testing.T) {
|
||
valid := []string{"success", "error", "warn", "info", "debug", "SUCCESS", "Warn"}
|
||
for _, l := range valid {
|
||
if !IsLogLevel(l) {
|
||
t.Errorf("IsLogLevel(%q) = false, want true", l)
|
||
}
|
||
}
|
||
invalid := []string{"fatal", "trace", "", "warning"}
|
||
for _, l := range invalid {
|
||
if IsLogLevel(l) {
|
||
t.Errorf("IsLogLevel(%q) = true, want false", l)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestDetectLogLevel(t *testing.T) {
|
||
cases := []struct {
|
||
line string
|
||
want string
|
||
}{
|
||
{"[2026/01/01 00:00:00.000][网络][debug] 调试", "debug"},
|
||
{"[2026/01/01 00:00:00.000][抢购][success] 兑换成功", "success"},
|
||
{"[2026/01/01 00:00:00.000][系统][error] 失败", "error"},
|
||
{"[2026/01/01 00:00:00.000][系统][warn] 跳过", "warn"},
|
||
{"[2026/01/01 00:00:00.000][系统][info] 信息", "info"},
|
||
{"兑换成功完成", "success"},
|
||
{"网络请求失败 error 发生", "error"},
|
||
{"配置降级处理 warn", "warn"},
|
||
{"普通信息日志", "info"},
|
||
}
|
||
for _, c := range cases {
|
||
if got := DetectLogLevel(c.line); got != c.want {
|
||
t.Errorf("DetectLogLevel(%q) = %q, want %q", c.line, got, c.want)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestParseLogLine(t *testing.T) {
|
||
line := "2026/01/01 12:00:00.123 [抢购][success] 账号:testuser 兑换成功"
|
||
meta := ParseLogLine(line)
|
||
if meta.Time != "2026-01-01 12:00:00.123" {
|
||
t.Errorf("ParseLogLine Time = %q, want '2026-01-01 12:00:00.123'", meta.Time)
|
||
}
|
||
if meta.Category != "抢购" {
|
||
t.Errorf("ParseLogLine Category = %q, want '抢购'", meta.Category)
|
||
}
|
||
if meta.Level != "success" {
|
||
t.Errorf("ParseLogLine Level = %q, want 'success'", meta.Level)
|
||
}
|
||
if meta.Account != "testuser" {
|
||
t.Errorf("ParseLogLine Account = %q, want 'testuser'", meta.Account)
|
||
}
|
||
}
|
||
|
||
func TestParseLogLineShort(t *testing.T) {
|
||
// 不含时间的行,ParseLogLine 不应 panic,Time 应为空
|
||
line := "[系统][info] 配置已加载"
|
||
meta := ParseLogLine(line)
|
||
if meta.Category != "系统" {
|
||
t.Errorf("ParseLogLine Category = %q, want '系统'", meta.Category)
|
||
}
|
||
if meta.Level != "info" {
|
||
t.Errorf("ParseLogLine Level = %q, want 'info'", meta.Level)
|
||
}
|
||
if meta.Time != "" {
|
||
t.Errorf("ParseLogLine Time = %q, want empty", meta.Time)
|
||
}
|
||
}
|
||
|
||
func TestParseLogMeta(t *testing.T) {
|
||
cat, lvl := ParseLogMeta("[网络][warn] 降级")
|
||
if cat != "网络" || lvl != "warn" {
|
||
t.Errorf("ParseLogMeta = (%q, %q), want ('网络', 'warn')", cat, lvl)
|
||
}
|
||
}
|