TestEditMessageDebugCleanupDeletesPreEditRuns verifies that EditMessage schedules the chat debug cleanup goroutine when debug logging is enabled and that it deletes debug runs tied to the pre-edit conversation branch. This exercises the chatd wiring end to end: lazy debugService init, editCutoff sam
(t *testing.T)
| 3422 | // and the scheduleDebugCleanup retry loop against a real Postgres |
| 3423 | // store. |
| 3424 | func TestEditMessageDebugCleanupDeletesPreEditRuns(t *testing.T) { |
| 3425 | t.Parallel() |
| 3426 | |
| 3427 | db, ps := dbtestutil.NewDB(t) |
| 3428 | replica := newDebugEnabledTestServer(t, db, ps, uuid.New()) |
| 3429 | |
| 3430 | ctx := testutil.Context(t, testutil.WaitLong) |
| 3431 | user, org, model := seedChatDependencies(t, db) |
| 3432 | |
| 3433 | chat, err := replica.CreateChat(ctx, chatd.CreateOptions{ |
| 3434 | OrganizationID: org.ID, |
| 3435 | OwnerID: user.ID, |
| 3436 | Title: "debug-edit-cleanup", |
| 3437 | ModelConfigID: model.ID, |
| 3438 | InitialUserContent: []codersdk.ChatMessagePart{codersdk.ChatMessageText("first")}, |
| 3439 | }) |
| 3440 | require.NoError(t, err) |
| 3441 | |
| 3442 | msgs, err := db.GetChatMessagesByChatID(ctx, database.GetChatMessagesByChatIDParams{ |
| 3443 | ChatID: chat.ID, AfterID: 0, |
| 3444 | }) |
| 3445 | require.NoError(t, err) |
| 3446 | require.Len(t, msgs, 1) |
| 3447 | editedMsgID := msgs[0].ID |
| 3448 | |
| 3449 | // Stale debug run tied to the pre-edit message branch. Stamped |
| 3450 | // well outside the clock-skew buffer so the fast retry path |
| 3451 | // deletes it instead of deferring to the stale sweeper. |
| 3452 | staleStart := time.Now().Add(-time.Hour).UTC().Truncate(time.Microsecond) |
| 3453 | staleRun, err := db.InsertChatDebugRun(ctx, database.InsertChatDebugRunParams{ |
| 3454 | ChatID: chat.ID, |
| 3455 | ModelConfigID: uuid.NullUUID{UUID: model.ID, Valid: true}, |
| 3456 | TriggerMessageID: sql.NullInt64{Int64: editedMsgID, Valid: true}, |
| 3457 | HistoryTipMessageID: sql.NullInt64{Int64: editedMsgID, Valid: true}, |
| 3458 | Kind: "chat_turn", |
| 3459 | Status: "in_progress", |
| 3460 | Provider: sql.NullString{String: "openai", Valid: true}, |
| 3461 | Model: sql.NullString{String: model.Model, Valid: true}, |
| 3462 | StartedAt: sql.NullTime{Time: staleStart, Valid: true}, |
| 3463 | UpdatedAt: sql.NullTime{Time: staleStart, Valid: true}, |
| 3464 | }) |
| 3465 | require.NoError(t, err) |
| 3466 | |
| 3467 | // Run tied to an earlier message branch that the message-id |
| 3468 | // filter should leave alone even though it predates the edit. |
| 3469 | unrelatedRun, err := db.InsertChatDebugRun(ctx, database.InsertChatDebugRunParams{ |
| 3470 | ChatID: chat.ID, |
| 3471 | ModelConfigID: uuid.NullUUID{UUID: model.ID, Valid: true}, |
| 3472 | TriggerMessageID: sql.NullInt64{Int64: editedMsgID - 1, Valid: true}, |
| 3473 | HistoryTipMessageID: sql.NullInt64{Int64: editedMsgID - 1, Valid: true}, |
| 3474 | Kind: "chat_turn", |
| 3475 | Status: "completed", |
| 3476 | Provider: sql.NullString{String: "openai", Valid: true}, |
| 3477 | Model: sql.NullString{String: model.Model, Valid: true}, |
| 3478 | StartedAt: sql.NullTime{Time: staleStart, Valid: true}, |
| 3479 | UpdatedAt: sql.NullTime{Time: staleStart, Valid: true}, |
| 3480 | }) |
| 3481 | require.NoError(t, err) |
nothing calls this directly
no test coverage detected