package handler import ( "strings" "testing" "time" "github.com/agentmail/gateway/internal/models" "github.com/google/uuid" ) // 转发主题不能无限叠加 Fwd: 前缀,否则转发几轮后主题栏全是前缀。 func TestForwardSubject(t *testing.T) { cases := []struct{ custom, original, want string }{ {"", "修复登录态", "Fwd: 修复登录态"}, {"", "Fwd: 修复登录态", "Fwd: 修复登录态"}, // 已有前缀不再叠加 {"自定义主题", "修复登录态", "自定义主题"}, {" ", "修复登录态", "Fwd: 修复登录态"}, // 全空白视为未指定 } for _, c := range cases { if got := forwardSubject(c.custom, c.original); got != c.want { t.Errorf("forwardSubject(%q, %q) = %q, want %q", c.custom, c.original, got, c.want) } } } // 引用块必须逐行加 "> ":原文含代码块或列表时, // 只有逐行前缀才能在任何 Markdown 渲染器里保持引用语义。 func TestQuoteBodyPrefixesEveryLine(t *testing.T) { m := &models.Mail{ ID: uuid.New(), FromName: "opencode", FromWorkspace: "/root", Subject: "巡检结果", Body: "第一行\n\n```go\nfmt.Println(1)\n```\n- 列表项", CreatedAt: time.Date(2026, 9, 2, 10, 30, 0, 0, time.UTC), CCList: []models.Address{ {Name: "pi", Path: "root", Raw: "pi@root.new"}, }, } out := quoteBody(m) for _, line := range strings.Split(strings.TrimRight(out, "\n"), "\n") { if line == "---" || line == "" { continue } if !strings.HasPrefix(line, ">") { t.Errorf("引用块出现未加前缀的行: %q", line) } } // 元信息必须齐全,否则收件人不知道这封转发的来路 for _, want := range []string{"opencode@/root", "巡检结果", "2026-09-02 10:30:00", "pi@root.new"} { if !strings.Contains(out, want) { t.Errorf("引用块缺少 %q\n%s", want, out) } } // 原文正文本身要在引用里 if !strings.Contains(out, "> fmt.Println(1)") { t.Errorf("原文代码行未被引用:\n%s", out) } } // 无抄送时不该渲染出空的「抄送」行。 func TestQuoteBodyOmitsEmptyCC(t *testing.T) { m := &models.Mail{ FromName: "admin", Subject: "x", Body: "y", CreatedAt: time.Now(), } if strings.Contains(quoteBody(m), "抄送") { t.Error("无抄送时不应出现「抄送」行") } }