把回复转发给 IM 适配器(sendReply)时剥掉 <action>/<narration> 动作/内心独白(Web 端 才渲染灰斜体,IM 只能收纯文本),并按 <p> 段落拆成多条气泡分别发送;多气泡发送间隔 随机 0.5~2s(仅 IM,Web 前端不延迟)。新增 sanitize.go stripNarration/splitBubbles/ bubbleGap + 测试。
92 lines
2.9 KiB
Go
92 lines
2.9 KiB
Go
package channelbridge
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestStripNarration_RemovesActionBlock(t *testing.T) {
|
|
t.Parallel()
|
|
in := "<action>弯起眼睛笑了</action>\n当然看得到呀,亲爱的。\n怎么突然这么问,是想我了吗?"
|
|
got := stripNarration(in)
|
|
want := "当然看得到呀,亲爱的。\n怎么突然这么问,是想我了吗?"
|
|
if got != want {
|
|
t.Errorf("stripNarration = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestStripNarration_RemovesNarrationAndParagraphs(t *testing.T) {
|
|
t.Parallel()
|
|
in := "<p>你好呀</p>\n<narration>心里有点甜</narration>\n<p>今天天气不错</p>"
|
|
got := stripNarration(in)
|
|
// Paragraph markers flattened to newlines; narration block dropped entirely.
|
|
if got != "你好呀\n今天天气不错" {
|
|
t.Errorf("stripNarration = %q, want %q", got, "你好呀\n今天天气不错")
|
|
}
|
|
}
|
|
|
|
func TestStripNarration_PureChatKept(t *testing.T) {
|
|
t.Parallel()
|
|
in := "就是普通一句话,没有动作。\n第二行。"
|
|
if got := stripNarration(in); got != in {
|
|
t.Errorf("stripNarration changed plain text: %q -> %q", in, got)
|
|
}
|
|
}
|
|
|
|
func TestStripNarration_OnlyActionBecomesEmpty(t *testing.T) {
|
|
t.Parallel()
|
|
in := "<action>拍拍你</action>"
|
|
if got := stripNarration(in); got != "" {
|
|
t.Errorf("stripNarration = %q, want empty", got)
|
|
}
|
|
}
|
|
|
|
func TestStripNarration_CaseAndStrayTags(t *testing.T) {
|
|
t.Parallel()
|
|
in := "<Action>扑上来</Action> 你好 <narration/> 世界"
|
|
got := stripNarration(in)
|
|
if strings.Contains(got, "action") || strings.Contains(got, "narration") || strings.Contains(got, "扑上来") {
|
|
t.Errorf("stripNarration left role-play markup: %q", got)
|
|
}
|
|
if !strings.Contains(got, "你好") || !strings.Contains(got, "世界") {
|
|
t.Errorf("stripNarration dropped chat text: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestSplitBubbles_Paragraphs(t *testing.T) {
|
|
t.Parallel()
|
|
in := "<p>晚上好呀,亲爱的♪</p><p>怎么突然这么问,是想我了吗?</p>"
|
|
got := splitBubbles(in)
|
|
if len(got) != 2 || got[0] != "晚上好呀,亲爱的♪" || got[1] != "怎么突然这么问,是想我了吗?" {
|
|
t.Errorf("splitBubbles = %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestSplitBubbles_StripsRoleplayAndDropsEmpty(t *testing.T) {
|
|
t.Parallel()
|
|
in := "<p><action>弯起眼睛笑了</action>当然看得到呀。</p><p>❤</p>"
|
|
got := splitBubbles(in)
|
|
if len(got) != 2 || got[0] != "当然看得到呀。" {
|
|
t.Errorf("splitBubbles = %#v", got)
|
|
}
|
|
if got[1] != "❤" {
|
|
t.Errorf("splitBubbles[1] = %q, want ❤", got[1])
|
|
}
|
|
}
|
|
|
|
func TestSplitBubbles_NoParagraphIsSingleBubble(t *testing.T) {
|
|
t.Parallel()
|
|
in := "就是普通一句话\n带个换行。"
|
|
got := splitBubbles(in)
|
|
if len(got) != 1 || got[0] != in {
|
|
t.Errorf("splitBubbles = %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestSplitBubbles_OnlyActionIsEmpty(t *testing.T) {
|
|
t.Parallel()
|
|
if got := splitBubbles("<action>拍拍你</action>"); len(got) != 0 {
|
|
t.Errorf("splitBubbles = %#v, want empty", got)
|
|
}
|
|
}
|