MCPcopy Index your code
hub / github.com/coder/coder / TestInterruptChatPersistsPartialResponse

Function TestInterruptChatPersistsPartialResponse

coderd/x/chatd/chatd_test.go:7210–7346  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

7208}
7209
7210func TestInterruptChatPersistsPartialResponse(t *testing.T) {
7211 t.Parallel()
7212
7213 db, ps := dbtestutil.NewDB(t)
7214 ctx := testutil.Context(t, testutil.WaitLong)
7215
7216 // Set up a mock OpenAI that streams a partial response and then
7217 // blocks until the request context is canceled (simulating an
7218 // interrupt mid-stream).
7219 chunksDelivered := make(chan struct{})
7220 openAIURL := chattest.NewOpenAI(t, func(req *chattest.OpenAIRequest) chattest.OpenAIResponse {
7221 if !req.Stream {
7222 return chattest.OpenAINonStreamingResponse("title")
7223 }
7224 chunks := make(chan chattest.OpenAIChunk, 1)
7225 go func() {
7226 defer close(chunks)
7227 // Send two partial text chunks so there is meaningful
7228 // content to persist.
7229 for _, c := range chattest.OpenAITextChunks("hello world") {
7230 chunks <- c
7231 }
7232 // Signal that chunks have been written to the HTTP response.
7233 select {
7234 case <-chunksDelivered:
7235 default:
7236 close(chunksDelivered)
7237 }
7238 // Block until interrupt cancels the context.
7239 <-req.Context().Done()
7240 }()
7241 return chattest.OpenAIResponse{StreamingChunks: chunks}
7242 })
7243
7244 logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
7245 server := chatd.New(chatd.Config{
7246 Logger: logger,
7247 Database: db,
7248 ReplicaID: uuid.New(),
7249 Pubsub: ps,
7250 PendingChatAcquireInterval: 10 * time.Millisecond,
7251 InFlightChatStaleAfter: testutil.WaitSuperLong,
7252 })
7253 server.Start()
7254 t.Cleanup(func() {
7255 require.NoError(t, server.Close())
7256 })
7257
7258 user, org, model := seedChatDependencies(t, db)
7259 setOpenAIProviderBaseURL(ctx, t, db, openAIURL)
7260
7261 chat, err := server.CreateChat(ctx, chatd.CreateOptions{
7262 OrganizationID: org.ID,
7263 OwnerID: user.ID,
7264 Title: "interrupt-persist-test",
7265 ModelConfigID: model.ID,
7266 InitialUserContent: []codersdk.ChatMessagePart{codersdk.ChatMessageText("hello")},
7267 })

Callers

nothing calls this directly

Calls 15

NewDBFunction · 0.92
ContextFunction · 0.92
NewOpenAIFunction · 0.92
OpenAITextChunksFunction · 0.92
NewFunction · 0.92
ChatMessageTextFunction · 0.92
EventuallyFunction · 0.92
ParseContentFunction · 0.92
seedChatDependenciesFunction · 0.70
setOpenAIProviderBaseURLFunction · 0.70
ContextMethod · 0.65

Tested by

no test coverage detected