package channelbridge import ( "context" "testing" "time" "git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter" "git.yeij.top/AskaEth/Cyrene_v2/internal/user" ) // gapCounter returns a bubbleGapFn that counts how many times the inter-bubble delay // is consulted and returns zero, so the multi-bubble spacing can be asserted without // sleeping a real 0.5-2s per gap. func gapCounter(counter *int) func() time.Duration { return func() time.Duration { *counter++ return 0 } } func TestSendIMBubbles_SplitsStripsAndGaps(t *testing.T) { t.Parallel() ad := &fakeAdapter{name: "kook", connected: true} b, err := New(Options{Loop: &stubChatter{}, Sessions: &stubSessions{}}) if err != nil { t.Fatalf("New: %v", err) } gaps := 0 b.bubbleGapFn = gapCounter(&gaps) target := adapter.UnifiedMessage{ Platform: "kook", ChannelID: "329284282", ChannelType: adapter.ChannelDirect, SenderID: "329284282", } content := "
第二句话。
" if err := b.sendIMBubbles(context.Background(), target, func(ctx context.Context, m adapter.UnifiedMessage) error { return ad.SendMessage(ctx, adapter.UnifiedResponse{Messages: []adapter.UnifiedMessage{m}}) }, content); err != nil { t.Fatalf("sendIMBubbles err = %v", err) } if len(ad.sends) != 2 { t.Fatalf("sends = %d, want 2 bubbles", len(ad.sends))
}
if ad.sends[0].Content != "当然看得到呀。" {
t.Fatalf("bubble[0] = %q, want the role-play-stripped text", ad.sends[0].Content)
}
if ad.sends[1].Content != "第二句话。" {
t.Fatalf("bubble[1] = %q, want the second paragraph", ad.sends[1].Content)
}
if ad.sends[0].Platform != "kook" || ad.sends[0].ChannelID != "329284282" {
t.Fatalf("bubble[0] target = %+v, want kook 329284282", ad.sends[0])
}
// Two bubbles → exactly one inter-bubble delay is consulted (and applies to both
// reply and proactive paths, since this drives the shared dispatcher).
if gaps != 1 {
t.Fatalf("gap calls = %d, want 1 (2 bubbles → one gap)", gaps)
}
}
func TestSendIMBubbles_SingleBubbleNoGap(t *testing.T) {
t.Parallel()
ad := &fakeAdapter{name: "weixin-ilink", connected: true}
b, err := New(Options{Loop: &stubChatter{}, Sessions: &stubSessions{}})
if err != nil {
t.Fatalf("New: %v", err)
}
gaps := 0
b.bubbleGapFn = gapCounter(&gaps)
target := adapter.UnifiedMessage{
Platform: "weixin-ilink", ChannelID: "o9cq@im.wechat", ChannelType: adapter.ChannelDirect,
SenderID: "o9cq@im.wechat",
}
if err := b.sendIMBubbles(context.Background(), target, func(ctx context.Context, m adapter.UnifiedMessage) error {
return ad.SendProactive(ctx, m)
}, "记得喝水哦"); err != nil {
t.Fatalf("sendIMBubbles err = %v", err)
}
if len(ad.sends) != 1 {
t.Fatalf("sends = %d, want 1 bubble", len(ad.sends))
}
if gaps != 0 {
t.Fatalf("gap calls = %d, want 0 for a single bubble", gaps)
}
}
func TestSendIMBubbles_PureActionDropped(t *testing.T) {
t.Parallel()
ad := &fakeAdapter{name: "kook", connected: true}
b, err := New(Options{Loop: &stubChatter{}, Sessions: &stubSessions{}})
if err != nil {
t.Fatalf("New: %v", err)
}
target := adapter.UnifiedMessage{
Platform: "kook", ChannelID: "1", ChannelType: adapter.ChannelDirect, SenderID: "1",
}
if err := b.sendIMBubbles(context.Background(), target, func(ctx context.Context, m adapter.UnifiedMessage) error {
return ad.SendMessage(ctx, adapter.UnifiedResponse{Messages: []adapter.UnifiedMessage{m}})
}, "
reply is delivered as several stripped bubbles through the // shared dispatcher, targeting the source conversation. func TestSendReply_SplitsBubblesAndStrips(t *testing.T) { t.Parallel() b, _, _, ad := newTestBridge(t) b.bubbleGapFn = func() time.Duration { return 0 } in := directMessage() content := "
怎么突然这么问。
" if err := b.sendReply(context.Background(), in, ad, content); err != nil { t.Fatalf("sendReply err = %v", err) } if len(ad.sends) != 2 { t.Fatalf("sends = %d, want 2 (reply splits intobubbles)", len(ad.sends)) } if ad.sends[0].Content != "当然看得到呀。" || ad.sends[1].Content != "怎么突然这么问。" { t.Fatalf("bubbles = %#v, want stripped narration + both paragraphs", ad.sends) } if ad.sends[0].Platform != "telegram" || ad.sends[0].ChannelID != "100" { t.Fatalf("reply target = %+v, want telegram channel 100", ad.sends[0]) } } // TestSendProactive_SplitsWeChatBubbles verifies the proactive push now splits a // multi-paragraph message into bubbles on WeChat (the reported regression: WeChat // proactive pushes arrived as one un-split
blob). func TestSendProactive_SplitsWeChatBubbles(t *testing.T) { t.Parallel() const acct = "o9cq802QPKPscBenJAp-SMZPBlnQ@im.wechat" weixin := &fakeAdapter{name: "weixin-ilink", connected: true} owner := &user.OwnerProfile{Accounts: []user.OwnerAccount{{Platform: "weixin-ilink", AccountID: acct}}} b := newProactiveBridge(t, owner, weixin) b.bubbleGapFn = func() time.Duration { return 0 } msg := proactiveMsg("
第一段正文。
bubbles)", len(weixin.sends)) } if weixin.sends[0].Content != "第一段正文。" || weixin.sends[1].Content != "第二段。" { t.Fatalf("weixin bubbles = %#v, want stripped narration + both paragraphs", weixin.sends) } if weixin.sends[0].Platform != "weixin-ilink" || weixin.sends[0].ChannelID != acct || weixin.sends[0].SenderID != acct { t.Fatalf("weixin peer = %+v, want platform weixin-ilink account %s", weixin.sends[0], acct) } }