11import { describe , expect , it } from "vitest" ;
22import type { FeishuMessageEvent } from "./bot.js" ;
33import { getFeishuSequentialKey } from "./sequential-key.js" ;
4+ import type { FeishuConfig } from "./types.js" ;
45
56function createTextEvent ( params : {
67 text : string ;
78 messageId ?: string ;
89 chatId ?: string ;
10+ chatType ?: "p2p" | "group" | "topic_group" ;
11+ rootId ?: string ;
12+ threadId ?: string ;
13+ senderOpenId ?: string ;
14+ senderUserId ?: string ;
915} ) : FeishuMessageEvent {
1016 return {
1117 sender : {
1218 sender_id : {
13- open_id : "ou_sender_1" ,
14- user_id : "ou_user_1" ,
19+ open_id : params . senderOpenId ?? "ou_sender_1" ,
20+ user_id : params . senderUserId ?? "ou_user_1" ,
1521 } ,
1622 sender_type : "user" ,
1723 } ,
1824 message : {
1925 message_id : params . messageId ?? "om_message_1" ,
2026 chat_id : params . chatId ?? "oc_dm_chat" ,
21- chat_type : "p2p" ,
27+ chat_type : params . chatType ?? "p2p" ,
2228 message_type : "text" ,
2329 content : JSON . stringify ( { text : params . text } ) ,
30+ ...( params . rootId ? { root_id : params . rootId } : { } ) ,
31+ ...( params . threadId ? { thread_id : params . threadId } : { } ) ,
2432 } ,
2533 } as FeishuMessageEvent ;
2634}
2735
36+ function createFeishuConfig ( params : Partial < FeishuConfig > ) : FeishuConfig {
37+ return params as unknown as FeishuConfig ;
38+ }
2839describe ( "getFeishuSequentialKey" , ( ) => {
2940 it . each ( [
3041 [ createTextEvent ( { text : "hello" } ) , "feishu:default:oc_dm_chat" ] ,
@@ -40,6 +51,139 @@ describe("getFeishuSequentialKey", () => {
4051 ) . toBe ( expected ) ;
4152 } ) ;
4253
54+ it ( "keeps group messages on the per-chat lane by default" , ( ) => {
55+ const event = createTextEvent ( {
56+ text : "hello" ,
57+ chatId : "oc_group" ,
58+ chatType : "group" ,
59+ rootId : "om_root_1" ,
60+ } ) ;
61+
62+ expect (
63+ getFeishuSequentialKey ( {
64+ accountId : "default" ,
65+ event,
66+ feishuCfg : createFeishuConfig ( { } ) ,
67+ } ) ,
68+ ) . toBe ( "feishu:default:oc_group" ) ;
69+ } ) ;
70+
71+ it ( "scopes group_sender queues by sender" , ( ) => {
72+ const feishuCfg = createFeishuConfig ( { groupSessionScope : "group_sender" } ) ;
73+ const first = createTextEvent ( {
74+ text : "one" ,
75+ chatId : "oc_group" ,
76+ chatType : "group" ,
77+ senderOpenId : "ou_sender_1" ,
78+ } ) ;
79+ const otherSender = createTextEvent ( {
80+ text : "two" ,
81+ chatId : "oc_group" ,
82+ chatType : "group" ,
83+ senderOpenId : "ou_sender_2" ,
84+ } ) ;
85+
86+ expect ( getFeishuSequentialKey ( { accountId : "default" , event : first , feishuCfg } ) ) . toBe (
87+ "feishu:default:oc_group:sender:ou_sender_1" ,
88+ ) ;
89+ expect ( getFeishuSequentialKey ( { accountId : "default" , event : otherSender , feishuCfg } ) ) . toBe (
90+ "feishu:default:oc_group:sender:ou_sender_2" ,
91+ ) ;
92+ } ) ;
93+
94+ it ( "honors legacy topicSessionMode when resolving group queue lanes" , ( ) => {
95+ const feishuCfg = createFeishuConfig ( { topicSessionMode : "enabled" } ) ;
96+ const event = createTextEvent ( {
97+ text : "hello" ,
98+ chatId : "oc_group" ,
99+ chatType : "group" ,
100+ rootId : "om_root_1" ,
101+ } ) ;
102+
103+ expect ( getFeishuSequentialKey ( { accountId : "default" , event, feishuCfg } ) ) . toBe (
104+ "feishu:default:oc_group:topic:om_root_1" ,
105+ ) ;
106+ } ) ;
107+
108+ it ( "scopes group_topic queues by thread topic" , ( ) => {
109+ const feishuCfg = createFeishuConfig ( { groupSessionScope : "group_topic" } ) ;
110+ const first = createTextEvent ( {
111+ text : "one" ,
112+ chatId : "oc_group" ,
113+ chatType : "group" ,
114+ rootId : "om_root_1" ,
115+ messageId : "om_message_1" ,
116+ } ) ;
117+ const sameTopic = createTextEvent ( {
118+ text : "two" ,
119+ chatId : "oc_group" ,
120+ chatType : "group" ,
121+ rootId : "om_root_1" ,
122+ messageId : "om_message_2" ,
123+ } ) ;
124+ const otherTopic = createTextEvent ( {
125+ text : "three" ,
126+ chatId : "oc_group" ,
127+ chatType : "group" ,
128+ rootId : "om_root_2" ,
129+ messageId : "om_message_3" ,
130+ } ) ;
131+
132+ expect ( getFeishuSequentialKey ( { accountId : "default" , event : first , feishuCfg } ) ) . toBe (
133+ "feishu:default:oc_group:topic:om_root_1" ,
134+ ) ;
135+ expect ( getFeishuSequentialKey ( { accountId : "default" , event : sameTopic , feishuCfg } ) ) . toBe (
136+ "feishu:default:oc_group:topic:om_root_1" ,
137+ ) ;
138+ expect ( getFeishuSequentialKey ( { accountId : "default" , event : otherTopic , feishuCfg } ) ) . toBe (
139+ "feishu:default:oc_group:topic:om_root_2" ,
140+ ) ;
141+ } ) ;
142+
143+ it ( "scopes group_topic_sender queues by thread topic and sender" , ( ) => {
144+ const feishuCfg = createFeishuConfig ( { groupSessionScope : "group_topic_sender" } ) ;
145+ const first = createTextEvent ( {
146+ text : "one" ,
147+ chatId : "oc_group" ,
148+ chatType : "group" ,
149+ rootId : "om_root_1" ,
150+ senderOpenId : "ou_sender_1" ,
151+ } ) ;
152+ const otherSender = createTextEvent ( {
153+ text : "two" ,
154+ chatId : "oc_group" ,
155+ chatType : "group" ,
156+ rootId : "om_root_1" ,
157+ senderOpenId : "ou_sender_2" ,
158+ } ) ;
159+
160+ expect ( getFeishuSequentialKey ( { accountId : "default" , event : first , feishuCfg } ) ) . toBe (
161+ "feishu:default:oc_group:topic:om_root_1:sender:ou_sender_1" ,
162+ ) ;
163+ expect ( getFeishuSequentialKey ( { accountId : "default" , event : otherSender , feishuCfg } ) ) . toBe (
164+ "feishu:default:oc_group:topic:om_root_1:sender:ou_sender_2" ,
165+ ) ;
166+ } ) ;
167+
168+ it ( "honors per-group session scope overrides" , ( ) => {
169+ const feishuCfg = createFeishuConfig ( {
170+ groupSessionScope : "group" ,
171+ groups : {
172+ oc_group : { groupSessionScope : "group_topic" } ,
173+ } ,
174+ } ) ;
175+ const event = createTextEvent ( {
176+ text : "hello" ,
177+ chatId : "oc_group" ,
178+ chatType : "group" ,
179+ rootId : "om_root_1" ,
180+ } ) ;
181+
182+ expect ( getFeishuSequentialKey ( { accountId : "default" , event, feishuCfg } ) ) . toBe (
183+ "feishu:default:oc_group:topic:om_root_1" ,
184+ ) ;
185+ } ) ;
186+
43187 it ( "keeps /btw on a stable per-chat lane across different message ids" , ( ) => {
44188 const first = createTextEvent ( { text : "/btw one" , messageId : "om_message_1" } ) ;
45189 const second = createTextEvent ( { text : "/btw two" , messageId : "om_message_2" } ) ;
0 commit comments