fix: give card fallback a fresh deadline
This commit is contained in:
@@ -1596,3 +1596,61 @@ func TestHandleEventStreamingErrorConvergesCard(t *testing.T) {
|
||||
t.Fatalf("error leaked or markdown duplicated: content=%q markdown=%v", last.content, reply.called)
|
||||
}
|
||||
}
|
||||
|
||||
type deadlineFinalCard struct{}
|
||||
|
||||
func (deadlineFinalCard) SendFinalCard(context.Context, CardRequest) (string, error) {
|
||||
return "track-deadline", nil
|
||||
}
|
||||
|
||||
func (deadlineFinalCard) StartCard(context.Context, CardRequest) (string, error) {
|
||||
return "track-deadline", nil
|
||||
}
|
||||
|
||||
func (deadlineFinalCard) UpdateCard(ctx context.Context, _ string, _ string, isFinalize, _ bool) error {
|
||||
if !isFinalize {
|
||||
return nil
|
||||
}
|
||||
<-ctx.Done()
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
type contextCheckingReply struct {
|
||||
called bool
|
||||
contextErr error
|
||||
}
|
||||
|
||||
func (r *contextCheckingReply) SendMarkdown(ctx context.Context, _, _, _ string) error {
|
||||
r.called = true
|
||||
r.contextErr = ctx.Err()
|
||||
return r.contextErr
|
||||
}
|
||||
|
||||
func TestHandleEventStreamingFinalTimeoutUsesFreshMarkdownContext(t *testing.T) {
|
||||
qa := &fakeEventQA{
|
||||
fakeStreamingQA: fakeStreamingQA{result: core.QAResult{Answer: "完整答案"}},
|
||||
events: []weknora.StreamEvent{{ResponseType: "answer", FullAnswer: "完整答案", Done: true}},
|
||||
}
|
||||
reply := &contextCheckingReply{}
|
||||
h, err := NewHandlerWithOptions(
|
||||
TenantConfig{TenantKey: "tenant-a", Channel: "dingtalk"},
|
||||
qa,
|
||||
reply,
|
||||
HandlerOptions{CardSender: deadlineFinalCard{}},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("NewHandlerWithOptions: %v", err)
|
||||
}
|
||||
h.cardUpdateInterval = time.Hour
|
||||
h.finalDeliveryTimeout = 5 * time.Millisecond
|
||||
|
||||
if err := h.Handle(context.Background(), directTextCallback()); err != nil {
|
||||
t.Fatalf("Markdown fallback must receive a fresh context: %v", err)
|
||||
}
|
||||
if !reply.called {
|
||||
t.Fatal("Markdown fallback was not called")
|
||||
}
|
||||
if reply.contextErr != nil {
|
||||
t.Fatalf("Markdown fallback received expired context: %v", reply.contextErr)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user