TestRecoverStaleChatsRecoversWaitingWithQueue asserts a Waiting chat with a non-empty queue and stale updated_at gets recovered to Pending, closing the post-promote-stranding hole.
(t *testing.T)
| 11044 | // chat with a non-empty queue and stale updated_at gets recovered |
| 11045 | // to Pending, closing the post-promote-stranding hole. |
| 11046 | func TestRecoverStaleChatsRecoversWaitingWithQueue(t *testing.T) { |
| 11047 | t.Parallel() |
| 11048 | |
| 11049 | db, ps, rawDB := dbtestutil.NewDBWithSQLDB(t) |
| 11050 | ctx := testutil.Context(t, testutil.WaitLong) |
| 11051 | |
| 11052 | staleAfter := 100 * time.Millisecond |
| 11053 | logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}) |
| 11054 | server := chatd.New(chatd.Config{ |
| 11055 | Logger: logger, |
| 11056 | Database: db, |
| 11057 | ReplicaID: uuid.New(), |
| 11058 | Pubsub: ps, |
| 11059 | PendingChatAcquireInterval: testutil.WaitLong, |
| 11060 | InFlightChatStaleAfter: staleAfter, |
| 11061 | }) |
| 11062 | t.Cleanup(func() { require.NoError(t, server.Close()) }) |
| 11063 | user, org, model := seedChatDependencies(t, db) |
| 11064 | |
| 11065 | chat, err := db.InsertChat(ctx, database.InsertChatParams{ |
| 11066 | OrganizationID: org.ID, |
| 11067 | Status: database.ChatStatusWaiting, |
| 11068 | ClientType: database.ChatClientTypeUi, |
| 11069 | OwnerID: user.ID, |
| 11070 | Title: "stale-waiting-with-queue", |
| 11071 | LastModelConfigID: model.ID, |
| 11072 | }) |
| 11073 | require.NoError(t, err) |
| 11074 | |
| 11075 | queuedContent, err := chatprompt.MarshalParts([]codersdk.ChatMessagePart{ |
| 11076 | codersdk.ChatMessageText("queued-stranded"), |
| 11077 | }) |
| 11078 | require.NoError(t, err) |
| 11079 | _, err = db.InsertChatQueuedMessage(ctx, database.InsertChatQueuedMessageParams{ |
| 11080 | ChatID: chat.ID, |
| 11081 | Content: queuedContent.RawMessage, |
| 11082 | ModelConfigID: uuid.NullUUID{UUID: model.ID, Valid: true}, |
| 11083 | }) |
| 11084 | require.NoError(t, err) |
| 11085 | // Backdate updated_at directly so the chat is past the stale |
| 11086 | // threshold without sleeping. |
| 11087 | _, err = rawDB.ExecContext(ctx, |
| 11088 | "UPDATE chats SET updated_at = $1 WHERE id = $2", |
| 11089 | time.Now().Add(-time.Hour), chat.ID) |
| 11090 | require.NoError(t, err) |
| 11091 | |
| 11092 | chatd.RecoverStaleChatsForTest(ctx, server) |
| 11093 | |
| 11094 | got, err := db.GetChatByID(ctx, chat.ID) |
| 11095 | require.NoError(t, err) |
| 11096 | require.Equal(t, database.ChatStatusPending, got.Status, |
| 11097 | "stale-recovery must promote the front-of-queue and set Pending") |
| 11098 | |
| 11099 | messages, err := db.GetChatMessagesByChatID(ctx, database.GetChatMessagesByChatIDParams{ |
| 11100 | ChatID: chat.ID, |
| 11101 | AfterID: 0, |
| 11102 | }) |
| 11103 | require.NoError(t, err) |
nothing calls this directly
no test coverage detected