@@ -136,17 +136,30 @@ func (f *clarifyChatFixture) lastSession(conv string) (domain.BotClarifySession,
136136 return f .s .ActiveBotClarifySession (conv )
137137}
138138
139- // posts lists the clarification messages queued in a conversation .
140- func (f * clarifyChatFixture ) posts (t * testing.T , convID string ) []domain.BotInteraction {
139+ // waitPosts waits for clarification messages queued after the session changes .
140+ func (f * clarifyChatFixture ) waitPosts (t * testing.T , convID string , count int ) []domain.BotInteraction {
141141 t .Helper ()
142- records , _ := f .s .BotInteractionRecords (f .bot .ID )
143- out := []domain.BotInteraction {}
144- for i := len (records ) - 1 ; i >= 0 ; i -- {
145- if records [i ].Action == botinteraction .ActionClarifyPost && records [i ].ConversationID == convID {
146- out = append (out , records [i ])
142+ deadline := time .Now ().Add (10 * time .Second )
143+ var posts []domain.BotInteraction
144+ for {
145+ records , err := f .s .BotInteractionRecords (f .bot .ID )
146+ if err != nil {
147+ t .Fatal (err )
148+ }
149+ posts = posts [:0 ]
150+ for i := len (records ) - 1 ; i >= 0 ; i -- {
151+ if records [i ].Action == botinteraction .ActionClarifyPost && records [i ].ConversationID == convID {
152+ posts = append (posts , records [i ])
153+ }
147154 }
155+ if len (posts ) >= count {
156+ return posts
157+ }
158+ if ! time .Now ().Before (deadline ) {
159+ t .Fatalf ("queued clarification posts = %+v, want at least %d" , posts , count )
160+ }
161+ time .Sleep (20 * time .Millisecond )
148162 }
149- return out
150163}
151164
152165// Group clarification on Feishu: questions are posted to the group, numbered
@@ -167,7 +180,7 @@ func TestChatClarificationInAGroup(t *testing.T) {
167180 if sess .Round != 1 || len (sess .Questions ) != 2 || sess .Questions [1 ].N != 2 || sess .StarterUserID != 42 {
168181 t .Fatalf ("session = %+v" , sess )
169182 }
170- posts := f .posts (t , conv )
183+ posts := f .waitPosts (t , conv , 1 )
171184 if len (posts ) != 1 || posts [0 ].Quote != botinteraction .ClarifyPostQuestions || posts [0 ].SendRoute != repo .BotSendRoutePushChat || posts [0 ].SenderID != "ou_owner" {
172185 t .Fatalf ("posts = %+v" , posts )
173186 }
@@ -225,7 +238,7 @@ func TestChatClarificationInAGroup(t *testing.T) {
225238 if issue .Clarification .Status == domain .ClarificationReady || strings .Contains (issue .Description , "只做手机号" ) {
226239 t .Fatalf ("conclusion saved without a click: %+v" , issue .Clarification )
227240 }
228- posts = f .posts (t , conv )
241+ posts = f .waitPosts (t , conv , 2 )
229242 last := posts [len (posts )- 1 ]
230243 if last .Quote != botinteraction .ClarifyPostConclusion || ! strings .Contains (last .Content , "保存澄清结果" ) {
231244 t .Fatalf ("conclusion post = %+v" , last )
@@ -343,7 +356,7 @@ func TestChatClarificationInPrivateChat(t *testing.T) {
343356 }
344357 conv := start .ConversationID
345358 sess := f .waitStatus (t , conv , domain .BotClarifyAsking )
346- posts := f .posts (t , conv )
359+ posts := f .waitPosts (t , conv , 1 )
347360 if len (posts ) != 1 || ! strings .Contains (posts [0 ].Content , "直接回复即可" ) || ! strings .Contains (posts [0 ].Content , "发「下一轮」" ) || strings .Contains (posts [0 ].Content , "卡片" ) {
348361 t .Fatalf ("dingtalk questions = %+v" , posts )
349362 }
@@ -355,6 +368,7 @@ func TestChatClarificationInPrivateChat(t *testing.T) {
355368 if sess .Round != 2 {
356369 t .Fatalf ("round = %d" , sess .Round )
357370 }
371+ f .waitPosts (t , conv , 2 )
358372 if reply , _ := f .say (t , "single" , "cid-p2p" , "ou_member" , "另外老用户也要能登录" ); ! strings .Contains (reply , "已记录补充说明" ) {
359373 t .Fatalf ("general = %q" , reply )
360374 }
@@ -393,7 +407,7 @@ func TestChatClarificationTimers(t *testing.T) {
393407 if sess .Round != 2 {
394408 t .Fatalf ("idle answers did not start the next round: %+v" , sess )
395409 }
396- posts := f .posts (t , conv )
410+ posts := f .waitPosts (t , conv , 3 )
397411 notice := false
398412 for _ , p := range posts {
399413 if p .Quote == botinteraction .ClarifyPostNotice && strings .Contains (p .Content , "3 分钟没有新回答" ) {
@@ -502,7 +516,7 @@ func TestChatClarificationEdgePaths(t *testing.T) {
502516 sess := f .waitStatus (t , conv , domain .BotClarifyAsking )
503517 // Rule ①: a quote of the question card is an answer; the model only
504518 // picks the question.
505- post := f .posts (t , conv )[0 ]
519+ post := f .waitPosts (t , conv , 1 )[0 ]
506520 f .s .DB .Model (& domain.BotInteraction {}).Where ("id = ?" , post .ID ).Update ("platform_message_id" , "om_questions" )
507521 item := domain.BotInteraction {ID : "msg-quote" , BotID : f .bot .ID , SenderID : "ou_member" , Content : "要短信" , ReplyTo : "om_questions" }
508522 f .s .EnqueueBotInteraction (item , "group" , "oc_edge" )
@@ -533,7 +547,7 @@ func TestChatClarificationEdgePaths(t *testing.T) {
533547 if sess .Error == "" || sess .Round != 1 || sess .AnswerCount () != 1 {
534548 t .Fatalf ("failed round = %+v" , sess )
535549 }
536- posts := f .posts (t , conv )
550+ posts := f .waitPosts (t , conv , 2 )
537551 if last := posts [len (posts )- 1 ]; last .Quote != botinteraction .ClarifyPostNotice || ! strings .Contains (last .Content , "没有完成" ) {
538552 t .Fatalf ("failure notice = %+v" , last )
539553 }
@@ -619,7 +633,7 @@ func TestChatClarificationOnWeCom(t *testing.T) {
619633 }
620634 conv := start .ConversationID
621635 f .waitStatus (t , conv , domain .BotClarifyAsking )
622- post := f .posts (t , conv )[0 ]
636+ post := f .waitPosts (t , conv , 1 )[0 ]
623637 if ! strings .Contains (post .Content , "点卡片上的「提交回答,进入下一轮」" ) || post .SendRoute != repo .BotSendRoutePushChat {
624638 t .Fatalf ("questions = %+v" , post )
625639 }
0 commit comments