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

Function TestUpdateChatStatusPersistsLastError

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

Source from the content-addressed store, hash-verified

3931}
3932
3933func TestUpdateChatStatusPersistsLastError(t *testing.T) {
3934 t.Parallel()
3935
3936 db, ps := dbtestutil.NewDB(t)
3937 _ = newTestServer(t, db, ps, uuid.New())
3938
3939 ctx := testutil.Context(t, testutil.WaitLong)
3940 user, org, model := seedChatDependencies(t, db)
3941
3942 chat := dbgen.Chat(t, db, database.Chat{
3943 OrganizationID: org.ID,
3944 OwnerID: user.ID,
3945 Title: "error-persisted",
3946 LastModelConfigID: model.ID,
3947 })
3948
3949 // Write a minimal structured last_error payload through the
3950 // query layer, then verify it round-trips through storage.
3951 errorMessage := "stream response: status 500: internal server error"
3952 wantPayload := codersdk.ChatError{
3953 Message: errorMessage,
3954 Kind: codersdk.ChatErrorKindGeneric,
3955 }
3956 chat, err := db.UpdateChatStatus(ctx, database.UpdateChatStatusParams{
3957 ID: chat.ID,
3958 Status: database.ChatStatusError,
3959 WorkerID: uuid.NullUUID{},
3960 StartedAt: sql.NullTime{},
3961 HeartbeatAt: sql.NullTime{},
3962 LastError: mustChatLastErrorRawMessage(t, wantPayload),
3963 })
3964 require.NoError(t, err)
3965 require.Equal(t, database.ChatStatusError, chat.Status)
3966 require.Equal(t, wantPayload, requireChatLastErrorPayload(t, chat.LastError))
3967
3968 // Verify the error is persisted when re-read from the database.
3969 fromDB, err := db.GetChatByID(ctx, chat.ID)
3970 require.NoError(t, err)
3971 require.Equal(t, database.ChatStatusError, fromDB.Status)
3972 require.Equal(t, wantPayload, requireChatLastErrorPayload(t, fromDB.LastError))
3973
3974 // Verify the error is cleared when the chat transitions to a
3975 // non-error status (e.g. pending after a retry).
3976 chat, err = db.UpdateChatStatus(ctx, database.UpdateChatStatusParams{
3977 ID: chat.ID,
3978 Status: database.ChatStatusPending,
3979 WorkerID: uuid.NullUUID{},
3980 StartedAt: sql.NullTime{},
3981 HeartbeatAt: sql.NullTime{},
3982 LastError: pqtype.NullRawMessage{},
3983 })
3984 require.NoError(t, err)
3985 require.Equal(t, database.ChatStatusPending, chat.Status)
3986 require.False(t, chat.LastError.Valid)
3987
3988 fromDB, err = db.GetChatByID(ctx, chat.ID)
3989 require.NoError(t, err)
3990 require.False(t, fromDB.LastError.Valid)

Callers

nothing calls this directly

Calls 11

NewDBFunction · 0.92
ContextFunction · 0.92
ChatFunction · 0.92
newTestServerFunction · 0.70
seedChatDependenciesFunction · 0.70
NewMethod · 0.65
UpdateChatStatusMethod · 0.65
GetChatByIDMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected