package channelbridge import ( "context" "strings" "testing" "time" "git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter" "git.yeij.top/AskaEth/Cyrene_v2/internal/channel/session" "git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/history" "git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/loop" "git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model" "git.yeij.top/AskaEth/Cyrene_v2/internal/user" ) // stubLightJudge is a configurable LightJudge recording how often it was consulted. type stubLightJudge struct { resp LightDecision err error calls int } func (s *stubLightJudge) Judge(context.Context, LightInput) (LightDecision, error) { s.calls++ return s.resp, s.err } // nonOwnerGroupMsg returns a third-party group message from a member sender, with // the given mentions. Content is left untouched so tests control the signal. func nonOwnerGroupMsg(content string, mentions ...adapter.Mention) adapter.UnifiedMessage { return adapter.UnifiedMessage{ Platform: "telegram", ChannelID: "-20", ChannelType: adapter.ChannelGroup, GroupID: "-20", GroupName: "Gang", SenderID: "9", SenderName: "张三", Sender: adapter.Actor{Role: adapter.RoleMember}, Mentions: mentions, Content: content, ContentType: adapter.ContentText, } } // nonOwnerSession is the restricted (group / stranger) session binding used by the // non-owner tests: read-only memory, no owner. func nonOwnerSession() session.Session { return session.Session{SessionID: "sess-g", IsGroup: true, Owner: false, MemWritable: false} } func TestNonOwnerSilencedRegisteredOnly(t *testing.T) { t.Parallel() chat := &stubChatter{res: &loop.Result{Text: "不该回"}} sess := &stubSessions{sess: nonOwnerSession()} hist := &captureHistory{} b, err := New(Options{ Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{}, History: hist, Now: func() time.Time { return time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) }, }) if err != nil { t.Fatalf("New: %v", err) } // A non-owner message with no @/keyword/name signal is silenced (设计/12 §6 策略二): // it is registered to the isolated session log but never drives the loop. b.process(context.Background(), nonOwnerGroupMsg("hello all")) if chat.sessionID != "" || len(chat.messages) != 0 { t.Fatalf("loop ran for a no-signal non-owner message: session=%q messages=%+v", chat.sessionID, chat.messages) } recs := hist.records() if len(recs) != 1 { t.Fatalf("records = %d, want 1 (inbound registered, no assistant reply)", len(recs)) } if recs[0].SessionID != "sess-g" || recs[0].Role != history.RoleUser || recs[0].Content != "hello all" { t.Fatalf("registered record = %+v, want isolated sess-g user 'hello all'", recs[0]) } } func TestNonOwnerMentionResponds(t *testing.T) { t.Parallel() chat := &stubChatter{res: &loop.Result{Text: "收到"}} sess := &stubSessions{sess: nonOwnerSession()} hist := &captureHistory{} b, err := New(Options{ Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{}, History: hist, Now: func() time.Time { return time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) }, }) if err != nil { t.Fatalf("New: %v", err) } // An addressed (mentioned) non-owner message is worth a response (策略二), so it // drives the full loop against the isolated group session. b.process(context.Background(), nonOwnerGroupMsg("hello", adapter.Mention{UserID: "bot"})) if chat.sessionID != "sess-g" { t.Fatalf("loop session = %q, want isolated sess-g", chat.sessionID) } // Restricted group context → the third-party boundary system message plus the // user message (design/16 §4.6 §1), mirroring TestGroupMessageThirdPartySenderNameAndBoundary. if len(chat.messages) != 2 || chat.messages[1].Role != model.RoleUser { t.Fatalf("loop messages = %+v, want boundary + user", chat.messages) } if chat.messages[1].Content[0].Text != "hello" { t.Fatalf("loop user text = %q, want undecorated 'hello'", chat.messages[1].Content[0].Text) } if !strings.Contains(chat.messages[0].Content[0].Text, "群「Gang」") { t.Fatalf("origin/boundary system = %q, want group Gang", chat.messages[0].Content[0].Text) } } func TestNonOwnerHighValueKeywordResponds(t *testing.T) { t.Parallel() chat := &stubChatter{res: &loop.Result{Text: "我看看"}} sess := &stubSessions{sess: nonOwnerSession()} b, err := New(Options{ Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{}, Now: func() time.Time { return time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) }, }) if err != nil { t.Fatalf("New: %v", err) } // A high-value keyword (求助) is worth a response even without an @ (策略二). b.process(context.Background(), nonOwnerGroupMsg("有人在吗,紧急求助")) if chat.sessionID != "sess-g" { t.Fatalf("loop session = %q, want isolated sess-g for a high-value keyword", chat.sessionID) } } func TestNonOwnerQuietHoursSilenced(t *testing.T) { t.Parallel() chat := &stubChatter{res: &loop.Result{Text: "不该回"}} sess := &stubSessions{sess: nonOwnerSession()} hist := &captureHistory{} b, err := New(Options{ Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{}, History: hist, QuietStartHour: 23, QuietEndHour: 7, Now: func() time.Time { return time.Date(2026, 1, 1, 2, 0, 0, 0, time.UTC) }, }) if err != nil { t.Fatalf("New: %v", err) } // 策略五: during quiet hours a no-signal non-owner message is silenced — it is // registered but not answered. b.process(context.Background(), nonOwnerGroupMsg("hello all")) if chat.sessionID != "" || len(chat.messages) != 0 { t.Fatalf("loop ran during quiet hours for a no-signal message: session=%q messages=%+v", chat.sessionID, chat.messages) } if len(hist.records()) != 1 { t.Fatalf("records = %d, want 1 (registered, no reply)", len(hist.records())) } } func TestNonOwnerQuietHoursMentionResponds(t *testing.T) { t.Parallel() chat := &stubChatter{res: &loop.Result{Text: "在的"}} sess := &stubSessions{sess: nonOwnerSession()} b, err := New(Options{ Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{}, QuietStartHour: 23, QuietEndHour: 7, Now: func() time.Time { return time.Date(2026, 1, 1, 2, 0, 0, 0, time.UTC) }, }) if err != nil { t.Fatalf("New: %v", err) } // 策略五 is not a hard silence: a directly-addressed message during quiet hours // still gets a response. b.process(context.Background(), nonOwnerGroupMsg("在吗", adapter.Mention{UserID: "bot"})) if chat.sessionID != "sess-g" { t.Fatalf("loop session = %q, want sess-g (mention should break quiet-hours silence)", chat.sessionID) } } func TestOwnerUnaffectedInQuietHours(t *testing.T) { t.Parallel() chat := &stubChatter{res: &loop.Result{Text: "主人好"}} sess := &stubSessions{sess: session.Session{SessionID: "sess-1", Owner: true, MemWritable: true}} ad := &fakeAdapter{name: "telegram"} b, err := New(Options{ Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{}, History: &captureHistory{}, QuietStartHour: 23, QuietEndHour: 7, Now: func() time.Time { return time.Date(2026, 1, 1, 2, 0, 0, 0, time.UTC) }, }) if err != nil { t.Fatalf("New: %v", err) } b.Add(ad) // An owner message is never gated, even during quiet hours (策略五 only ever // governs non-owner traffic): owner-private chat still routes to the main web // session and replies. b.process(context.Background(), directMessage()) if chat.sessionID != history.MainSessionID { t.Fatalf("owner loop session = %q, want %q", chat.sessionID, history.MainSessionID) } if len(ad.sends) != 1 || ad.sends[0].Content != "主人好" { t.Fatalf("owner reply = %+v, want '主人好'", ad.sends) } } func TestLightJudgeConsultedWhenInconclusive(t *testing.T) { t.Parallel() // A no-signal daytime message is inconclusive to the rules, so the injected // LightJudge gets a chance to decide (设计/12 §6 策略二: 规则不确定才轻 LLM). judge := &stubLightJudge{resp: LightDecision{Respond: true, Reason: "值得回"}} chat := &stubChatter{res: &loop.Result{Text: "嗯"}} sess := &stubSessions{sess: nonOwnerSession()} b, err := New(Options{ Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{}, Light: judge, Now: func() time.Time { return time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) }, }) if err != nil { t.Fatalf("New: %v", err) } b.process(context.Background(), nonOwnerGroupMsg("今天天气不错")) if judge.calls != 1 { t.Fatalf("light judge calls = %d, want 1 (inconclusive rules escalated)", judge.calls) } if chat.sessionID != "sess-g" { t.Fatalf("loop session = %q, want sess-g (judge said respond)", chat.sessionID) } } func TestLightJudgeSilenceIsRespected(t *testing.T) { t.Parallel() judge := &stubLightJudge{resp: LightDecision{Respond: false, Reason: "不值得"}} chat := &stubChatter{res: &loop.Result{Text: "不该回"}} sess := &stubSessions{sess: nonOwnerSession()} hist := &captureHistory{} b, err := New(Options{ Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{}, History: hist, Light: judge, Now: func() time.Time { return time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) }, }) if err != nil { t.Fatalf("New: %v", err) } b.process(context.Background(), nonOwnerGroupMsg("今天天气不错")) if judge.calls != 1 { t.Fatalf("light judge calls = %d, want 1", judge.calls) } if chat.sessionID != "" { t.Fatalf("loop ran after the judge said silence: session=%q", chat.sessionID) } if len(hist.records()) != 1 { t.Fatalf("records = %d, want 1 (registered, no reply)", len(hist.records())) } } func TestLightJudgeErrorFailsClosedToSilence(t *testing.T) { t.Parallel() judge := &stubLightJudge{err: context.DeadlineExceeded} chat := &stubChatter{res: &loop.Result{Text: "不该回"}} sess := &stubSessions{sess: nonOwnerSession()} hist := &captureHistory{} b, err := New(Options{ Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{}, History: hist, Light: judge, Now: func() time.Time { return time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) }, }) if err != nil { t.Fatalf("New: %v", err) } // A failing light judge is fail-closed toward silence (不打扰为第一原则). b.process(context.Background(), nonOwnerGroupMsg("今天天气不错")) if judge.calls != 1 { t.Fatalf("light judge calls = %d, want 1", judge.calls) } if chat.sessionID != "" || len(chat.messages) != 0 { t.Fatalf("loop ran after judge error: session=%q messages=%+v", chat.sessionID, chat.messages) } if len(hist.records()) != 1 { t.Fatalf("records = %d, want 1 (registered, no reply)", len(hist.records())) } }