77 lines
2.6 KiB
Go
77 lines
2.6 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestRenderNotifyTemplate(t *testing.T) {
|
|
out := renderNotifyTemplate("❌ {{task_name}}\n{{error}}\n{{unknown}}", map[string]string{
|
|
"task_name": "抢机", "error": "boom",
|
|
})
|
|
if !strings.Contains(out, "抢机") || !strings.Contains(out, "boom") {
|
|
t.Errorf("渲染缺变量: %q", out)
|
|
}
|
|
if !strings.Contains(out, "{{unknown}}") {
|
|
t.Errorf("未知变量应原样保留: %q", out)
|
|
}
|
|
// 公共变量 time 自动注入
|
|
out = renderNotifyTemplate("at {{time}}", nil)
|
|
if strings.Contains(out, "{{time}}") {
|
|
t.Errorf("time 未注入: %q", out)
|
|
}
|
|
}
|
|
|
|
func TestMdToTelegramHTML(t *testing.T) {
|
|
got := mdToTelegramHTML("*任务失败*:`err<1>` _注意_\n```\nraw & code\n```")
|
|
for _, want := range []string{"<b>任务失败</b>", "<code>err<1></code>", "<i>注意</i>", "<pre>raw & code</pre>"} {
|
|
if !strings.Contains(got, want) {
|
|
t.Errorf("got %q, want contains %q", got, want)
|
|
}
|
|
}
|
|
// 未闭合标记原样保留,不产生残缺标签
|
|
if got := mdToTelegramHTML("孤立*星号 和 `反引号"); strings.Contains(got, "<b>") || strings.Contains(got, "<code>") {
|
|
t.Errorf("未闭合标记不应转换: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestNotifyTemplateReadWrite(t *testing.T) {
|
|
svc, _ := newSettingEnv(t)
|
|
ctx := context.Background()
|
|
|
|
items, err := svc.NotifyTemplates(ctx)
|
|
if err != nil || len(items) != len(notifyTplOrder) {
|
|
t.Fatalf("NotifyTemplates = %d 项, %v", len(items), err)
|
|
}
|
|
if items[0].Kind != "task_fail" || items[0].DefaultTemplate == "" || items[0].Template != "" {
|
|
t.Errorf("默认视图 = %+v", items[0])
|
|
}
|
|
// 保存自定义 → 读回
|
|
if err := svc.UpdateNotifyTemplate(ctx, "task_fail", "自定义 {{task_name}}"); err != nil {
|
|
t.Fatalf("UpdateNotifyTemplate: %v", err)
|
|
}
|
|
custom, _ := svc.NotifyTemplate(ctx, "task_fail")
|
|
if custom != "自定义 {{task_name}}" {
|
|
t.Errorf("读回 = %q", custom)
|
|
}
|
|
if notifyTplText(custom, "task_fail") != "自定义 {{task_name}}" {
|
|
t.Error("生效模板应为自定义")
|
|
}
|
|
// 清空恢复默认
|
|
if err := svc.UpdateNotifyTemplate(ctx, "task_fail", ""); err != nil {
|
|
t.Fatalf("清空: %v", err)
|
|
}
|
|
custom, _ = svc.NotifyTemplate(ctx, "task_fail")
|
|
if notifyTplText(custom, "task_fail") != notifyTplDefs["task_fail"].Default {
|
|
t.Error("清空后应回落默认模板")
|
|
}
|
|
// 未知 kind / 超长拒绝
|
|
if err := svc.UpdateNotifyTemplate(ctx, "nope", "x"); err == nil {
|
|
t.Error("未知 kind 应被拒绝")
|
|
}
|
|
if err := svc.UpdateNotifyTemplate(ctx, "task_fail", strings.Repeat("字", 2001)); err == nil {
|
|
t.Error("超长模板应被拒绝")
|
|
}
|
|
}
|