Files
Michael Suchacz b9e130283f test(coderd): make the chatd idle wait fail on timeout (#30967)
`chatd.WaitUntilIdleForTest` waited up to 10 seconds for the chat worker
to go idle, dropped the timeout error, and then drained tracked work, so
a test whose worker never went idle passed after a silent 10-second
stall. The hook now takes the caller's `ctx` and returns an error when
`ctx` ends before the worker is idle or the tracked work finishes. The
26 callers that expect success wrap it in `require.NoError`. The poll
loop moves from `chatWorker.WaitIdle`, whose only caller was the hook,
into `testhooks.go`. It polls on a real 10 ms ticker instead of the
server clock, which a mock clock never advances.

## Why it is safe

- The idle check is unchanged: the hook reads the runner manager under
the worker's lock and returns at once when the worker or manager is nil.
The drain still calls `drainInflight`, now in a goroutine so that `ctx`
bounds it; that goroutine exits once `Close` cancels the tracked work.
- `TestWaitUntilIdleForTestTimeout` pins both error returns without a
database: it passes an expired `ctx` while one runner is open
(`RunnerOpen`) or one tracked task is in flight (`InflightWork`) and
requires `context.DeadlineExceeded`. Changing both returns to `return
nil` fails both subtests with `Expected error with "context deadline
exceeded" in chain but got nil`; changing one fails only its subtest.
- Red-green for the idle wait: with `runnerManager.idle` forced to
return `false`, `TestCreateChatImmediatelyProcessesNewChat` passes on
`main` after 10.2 s and fails here at its 25 s context with `wait for
the chat worker to go idle: context deadline exceeded`.
- Red-green for the drain: on this PR's first head, whose drain ignored
`ctx`, a probe holding one tracked task open stayed blocked 2 s past a
100 ms context, then returned `nil`.
- Hidden timeouts: on `main`, with the hook instrumented to report
errors and idle waits over 1 s, all 119 hook calls in the packages below
returned in under 1 s without an error.

## Review focus

Callers now pass their own test context instead of getting a fresh 10 s
one. Every direct caller, and every caller of
`coderdtest.WaitForChatSettled` and `waitForChatProcessed`, passes
`testutil.WaitLong` or longer, so the budget grows, but the rest of the
test shares it.

The PR adds 100 lines and removes 54: the drain goroutine is 9 of them
and the new test 39. Without the goroutine, a stalled title or summary
call holds the hook past its deadline.

## Validation

- `go test ./coderd/x/chatd/ -count=3`: pass.
- `go test ./coderd/ -count=3 -run '<every test in exp_chats_test.go,
exp_chats_workspace_file_test.go, and exp_chats_hooks_test.go>'`: pass.
- `go test ./codersdk/toolsdk/ -count=3 -run
'^(TestChatTools|TestCreateChatOrganizationDefaultModel)$'`: pass.
- `go test -race ./coderd/x/chatd/ -run
'^TestWaitUntilIdleForTestTimeout$' -count=50`: pass.
- Mutants and the probe use `go test -overlay`; a copy with a syntax
error fails to build, so the overlay applies.
- `make pre-commit` passed.

> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
2026-10-27 10:23:01 +01:00

114 lines
3.1 KiB
Go

package coderdtest
import (
"context"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/x/chatd"
"github.com/coder/coder/v2/coderd/x/chatd/chattest"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
)
const (
// TestChatProviderOpenAICompat is the default provider for chat runtime tests.
TestChatProviderOpenAICompat = "openai-compat"
// TestChatProviderAPIKey is a non-secret API key for local chat providers.
TestChatProviderAPIKey = "test-api-key"
// TestChatModelOpenAICompat is the default model for chat runtime tests.
TestChatModelOpenAICompat = "gpt-4o-mini"
)
// CreateOpenAICompatChatModel creates the default provider and ChatModel used
// by chat runtime tests. Tests can pass a baseURL to route chat work to a
// specific local provider. If baseURL is empty, this helper starts a fake
// OpenAI-compatible provider.
func CreateOpenAICompatChatModel(
t testing.TB,
client *codersdk.ExperimentalClient,
baseURL string,
) codersdk.ChatModel {
t.Helper()
if baseURL == "" {
baseURL = chattest.OpenAI(t)
}
ctx := testutil.Context(t, testutil.WaitLong)
provider, err := client.CreateAIProvider(ctx, codersdk.CreateAIProviderRequest{
Type: codersdk.AIProviderType(TestChatProviderOpenAICompat),
Name: "test-" + uuid.NewString(),
BaseURL: baseURL,
Enabled: true,
APIKeys: []string{TestChatProviderAPIKey},
})
require.NoError(t, err)
defaultOrg, err := client.OrganizationByName(ctx, codersdk.DefaultOrganization)
require.NoError(t, err)
contextLimit := int64(4096)
isDefault := true
model, err := client.CreateChatModel(ctx, defaultOrg.ID, codersdk.CreateChatModelRequest{
AIProviderID: &provider.ID,
Model: TestChatModelOpenAICompat,
ContextLimit: &contextLimit,
IsDefault: &isDefault,
})
require.NoError(t, err)
return model
}
// WaitForChatSettled waits for a chat to leave active processing and drains
// tracked chat daemon work before returning the final row.
func WaitForChatSettled(
ctx context.Context,
t testing.TB,
api *coderd.API,
chatID uuid.UUID,
) database.Chat {
t.Helper()
require.NotNil(t, api)
waitForChatTerminalState(ctx, t, api.Database, chatID)
server := api.ChatDaemon()
require.NotNil(t, server)
require.NoError(t, chatd.WaitUntilIdleForTest(ctx, server))
chat, err := getChatByIDAsSystem(ctx, api.Database, chatID)
require.NoError(t, err)
return chat
}
func waitForChatTerminalState(
ctx context.Context,
t testing.TB,
db database.Store,
chatID uuid.UUID,
) {
t.Helper()
require.Eventually(t, func() bool {
chat, err := getChatByIDAsSystem(ctx, db, chatID)
if err != nil {
return false
}
return chat.Status != database.ChatStatusRunning
}, testutil.WaitLong, testutil.IntervalFast)
}
func getChatByIDAsSystem(
ctx context.Context,
db database.Store,
chatID uuid.UUID,
) (database.Chat, error) {
// Test helper needs system scope to observe chatd-owned status changes.
//nolint:gocritic
return db.GetChatByID(dbauthz.AsSystemRestricted(ctx), chatID)
}