diff --git a/coderd/exp_chats_test.go b/coderd/exp_chats_test.go index 4468a7c8c49..e4b91ee8a1b 100644 --- a/coderd/exp_chats_test.go +++ b/coderd/exp_chats_test.go @@ -12422,11 +12422,12 @@ func TestPromoteChatQueuedMessage(t *testing.T) { func TestPostChatFile(t *testing.T) { t.Parallel() + client := newChatClient(t) + firstUser := coderdtest.CreateFirstUser(t, client.Client) + t.Run("Success/PNG", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) // Valid PNG header + padding. data := append([]byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}, make([]byte, 64)...) @@ -12438,8 +12439,6 @@ func TestPostChatFile(t *testing.T) { t.Run("MissingFilename", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) data := append([]byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}, make([]byte, 64)...) _, err := client.UploadChatFile(ctx, firstUser.OrganizationID, "image/png", "", bytes.NewReader(data)) @@ -12451,8 +12450,6 @@ func TestPostChatFile(t *testing.T) { t.Run("Success/TextPlain", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) data := []byte(`This is a test paste. With multiple lines. @@ -12465,8 +12462,6 @@ With multiple lines. t.Run("Success/TextPlainRefinesToJSON", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) resp, err := client.UploadChatFile(ctx, firstUser.OrganizationID, "text/plain", "pasted-text.txt", bytes.NewReader([]byte(`{"ok":true}`))) require.NoError(t, err) @@ -12476,8 +12471,6 @@ With multiple lines. t.Run("Success/TextPlainRefinesToCSV", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) resp, err := client.UploadChatFile(ctx, firstUser.OrganizationID, "text/plain", "pasted-text.txt", bytes.NewReader([]byte(`name,count widgets,3 @@ -12489,8 +12482,6 @@ widgets,3 t.Run("Success/OctetStreamPNG", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) data := append([]byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}, make([]byte, 64)...) uploaded, err := client.UploadChatFile(ctx, firstUser.OrganizationID, "application/octet-stream", "test.png", bytes.NewReader(data)) @@ -12506,8 +12497,6 @@ widgets,3 t.Run("Success/OctetStreamMarkdown", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) data := []byte(`# Markdown upload @@ -12526,8 +12515,6 @@ This arrived as octet-stream. t.Run("OctetStreamRejectsUnsupportedBytes", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) _, err := client.UploadChatFile(ctx, firstUser.OrganizationID, "application/octet-stream", "payload.zip", bytes.NewReader([]byte("PK\x03\x04"))) sdkErr := requireSDKError(t, err, http.StatusBadRequest) @@ -12537,8 +12524,6 @@ This arrived as octet-stream. t.Run("UnsupportedContentType", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) _, err := client.UploadChatFile(ctx, firstUser.OrganizationID, "application/zip", "test.zip", bytes.NewReader([]byte("PK\x03\x04"))) requireSDKError(t, err, http.StatusBadRequest) @@ -12547,8 +12532,6 @@ This arrived as octet-stream. t.Run("Success/SVG", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) _, err := client.UploadChatFile(ctx, firstUser.OrganizationID, "image/svg+xml", "test.svg", bytes.NewReader([]byte(``))) require.NoError(t, err) @@ -12557,8 +12540,6 @@ This arrived as octet-stream. t.Run("Success/SVGPastedAsText", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) // Large pastes arrive as text/plain; SVG bytes are stored as image/svg+xml. uploaded, err := client.UploadChatFile(ctx, firstUser.OrganizationID, "text/plain", "pasted-text-2026-01-01-00-00-00.txt", bytes.NewReader([]byte(``))) @@ -12572,8 +12553,6 @@ This arrived as octet-stream. t.Run("ContentSniffingRejectsPNGAsText", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) // Valid 1x1 PNG declared as text/plain should still be rejected. data := []byte{ @@ -12595,8 +12574,6 @@ This arrived as octet-stream. t.Run("ContentSniffingRejectsPlainTextAsJSON", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) _, err := client.UploadChatFile(ctx, firstUser.OrganizationID, "application/json", "payload.json", bytes.NewReader([]byte("not actually json"))) sdkErr := requireSDKError(t, err, http.StatusBadRequest) @@ -12606,8 +12583,6 @@ This arrived as octet-stream. t.Run("TooLarge", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) // 10 MB + 1 byte, with valid PNG header to pass media type check. data := make([]byte, 10<<20+1) @@ -12619,8 +12594,6 @@ This arrived as octet-stream. t.Run("Success/TextPlainHTMLLikeContent", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) data := []byte(`

Paste me as plain text.

@@ -12633,8 +12606,6 @@ This arrived as octet-stream. t.Run("MissingOrganization", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - coderdtest.CreateFirstUser(t, client.Client) data := append([]byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}, make([]byte, 64)...) res, err := client.Request(ctx, http.MethodPost, "/api/v2/chats/files", bytes.NewReader(data), func(r *http.Request) { @@ -12651,8 +12622,6 @@ This arrived as octet-stream. t.Run("InvalidOrganization", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - coderdtest.CreateFirstUser(t, client.Client) data := append([]byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}, make([]byte, 64)...) res, err := client.Request(ctx, http.MethodPost, "/api/v2/chats/files?organization=not-a-uuid", bytes.NewReader(data), func(r *http.Request) { @@ -12668,8 +12637,6 @@ This arrived as octet-stream. t.Run("WrongOrganization", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - coderdtest.CreateFirstUser(t, client.Client) data := append([]byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}, make([]byte, 64)...) _, err := client.UploadChatFile(ctx, uuid.New(), "image/png", "test.png", bytes.NewReader(data)) @@ -12685,9 +12652,6 @@ This arrived as octet-stream. t.Run("Unauthenticated", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) - unauthed := codersdk.NewExperimentalClient(codersdk.New(client.URL)) data := append([]byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}, make([]byte, 64)...) _, err := unauthed.UploadChatFile(ctx, firstUser.OrganizationID, "image/png", "test.png", bytes.NewReader(data)) @@ -12698,8 +12662,6 @@ This arrived as octet-stream. t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - client := newChatClient(t) - firstUser := coderdtest.CreateFirstUser(t, client.Client) memberClientRaw, _ := coderdtest.CreateAnotherUser(t, client.Client, firstUser.OrganizationID) memberClient := codersdk.NewExperimentalClient(memberClientRaw) diff --git a/coderd/x/chatd/chatd_internal_test.go b/coderd/x/chatd/chatd_internal_test.go index e116032d623..da906978072 100644 --- a/coderd/x/chatd/chatd_internal_test.go +++ b/coderd/x/chatd/chatd_internal_test.go @@ -3597,6 +3597,8 @@ func TestResolveModelConfigOrganizationScope(t *testing.T) { func TestResolveFallbackModelConfigID(t *testing.T) { t.Parallel() + db, ps := dbtestutil.NewDB(t) + newProvider := func(t *testing.T, db database.Store, enabled bool) database.AIProvider { return dbgen.AIProvider(t, db, database.AIProvider{}, func(p *database.InsertAIProviderParams) { p.Enabled = enabled @@ -3618,7 +3620,6 @@ func TestResolveFallbackModelConfigID(t *testing.T) { t.Run("EnabledLastModel", func(t *testing.T) { t.Parallel() - db, _ := dbtestutil.NewDB(t) ctx := testutil.Context(t, testutil.WaitShort) orgID := newModelConfigOrg(t, db) @@ -3632,7 +3633,6 @@ func TestResolveFallbackModelConfigID(t *testing.T) { t.Run("ProviderDisabledLastModelFallsBackToDefault", func(t *testing.T) { t.Parallel() - db, _ := dbtestutil.NewDB(t) ctx := testutil.Context(t, testutil.WaitShort) orgID := newModelConfigOrg(t, db) @@ -3648,7 +3648,6 @@ func TestResolveFallbackModelConfigID(t *testing.T) { t.Run("NilLastModelUsesDefault", func(t *testing.T) { t.Parallel() - db, _ := dbtestutil.NewDB(t) ctx := testutil.Context(t, testutil.WaitShort) orgID := newModelConfigOrg(t, db) @@ -3662,7 +3661,6 @@ func TestResolveFallbackModelConfigID(t *testing.T) { t.Run("NonDefaultOrgWithoutLocalDefaultRejected", func(t *testing.T) { t.Parallel() - db, _ := dbtestutil.NewDB(t) ctx := testutil.Context(t, testutil.WaitShort) otherOrgID := newModelConfigOrg(t, db) @@ -3677,7 +3675,6 @@ func TestResolveFallbackModelConfigID(t *testing.T) { t.Run("DisabledLocalDefaultRejected", func(t *testing.T) { t.Parallel() - db, _ := dbtestutil.NewDB(t) ctx := testutil.Context(t, testutil.WaitShort) orgID := newModelConfigOrg(t, db) @@ -3702,7 +3699,6 @@ func TestResolveFallbackModelConfigID(t *testing.T) { t.Run("DisabledLocalDefaultProviderRejected", func(t *testing.T) { t.Parallel() - db, _ := dbtestutil.NewDB(t) ctx := testutil.Context(t, testutil.WaitShort) orgID := newModelConfigOrg(t, db) @@ -3715,7 +3711,6 @@ func TestResolveFallbackModelConfigID(t *testing.T) { t.Run("ProviderDisabledDefaultRejected", func(t *testing.T) { t.Parallel() - db, _ := dbtestutil.NewDB(t) ctx := testutil.Context(t, testutil.WaitShort) orgID := newModelConfigOrg(t, db) @@ -3729,7 +3724,6 @@ func TestResolveFallbackModelConfigID(t *testing.T) { t.Run("ExplicitEnabledModel", func(t *testing.T) { t.Parallel() - db, _ := dbtestutil.NewDB(t) ctx := testutil.Context(t, testutil.WaitShort) orgID := newModelConfigOrg(t, db) @@ -3743,7 +3737,6 @@ func TestResolveFallbackModelConfigID(t *testing.T) { t.Run("ExplicitDefaultOrgModelRejected", func(t *testing.T) { t.Parallel() - db, _ := dbtestutil.NewDB(t) ctx := testutil.Context(t, testutil.WaitShort) chatOrgID := newModelConfigOrg(t, db) @@ -3763,7 +3756,6 @@ func TestResolveFallbackModelConfigID(t *testing.T) { t.Run("ExplicitUnrelatedOrgModelRejected", func(t *testing.T) { t.Parallel() - db, _ := dbtestutil.NewDB(t) ctx := testutil.Context(t, testutil.WaitShort) chatOrgID := newModelConfigOrg(t, db) @@ -3784,7 +3776,6 @@ func TestResolveFallbackModelConfigID(t *testing.T) { // preflight must still be rejected inside the daemon. t.Run("ExplicitProviderDisabledRejected", func(t *testing.T) { t.Parallel() - db, _ := dbtestutil.NewDB(t) ctx := testutil.Context(t, testutil.WaitShort) orgID := newModelConfigOrg(t, db) @@ -3799,7 +3790,6 @@ func TestResolveFallbackModelConfigID(t *testing.T) { // inserting the chat and its initial messages. t.Run("CreateChatProviderDisabledRejected", func(t *testing.T) { t.Parallel() - db, ps := dbtestutil.NewDB(t) ctx := testutil.Context(t, testutil.WaitShort) owner := dbgen.User(t, db, database.User{}) diff --git a/coderd/x/chatd/chatd_test.go b/coderd/x/chatd/chatd_test.go index 3989fbab500..0be3fe20483 100644 --- a/coderd/x/chatd/chatd_test.go +++ b/coderd/x/chatd/chatd_test.go @@ -260,8 +260,6 @@ func TestSubagentChatExcludesWorkspaceProvisioningTools(t *testing.T) { coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID) coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) - _ = agenttest.New(t, client.URL, agentToken) - // Track tools sent in LLM requests. The first call is for the // root chat which spawns a subagent; the second call is for the // subagent itself. @@ -417,8 +415,6 @@ func TestPlanModeSubagentChatExcludesAskUserQuestion(t *testing.T) { coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID) coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) - _ = agenttest.New(t, client.URL, agentToken) - // Start an external MCP server whose tools should remain available to the // root plan-mode chat but stay hidden from plan-mode subagents. mcpSrv := newTestMCPServer("plan-root-mcp") @@ -3241,7 +3237,8 @@ func TestRequiresActionChatPersistsWaitingStatusLabel(t *testing.T) { got.LastTurnSummary.Valid && got.LastTurnSummary.String == "Waiting for user input" }, testutil.IntervalFast) - chatd.WaitUntilIdleForTest(server) + // A requires_action runner stays active until tool results arrive. + require.NoError(t, server.Close()) require.Equal(t, database.ChatStatusRequiresAction, fromDB.Status, "expected requires_action, got %s (last_error=%q)", @@ -5299,18 +5296,29 @@ func TestStoppedWorkspaceWithPersistedAgentBindingDoesNotBlockChat(t *testing.T) BuildNumber: 2, }).Do() + clock := quartz.NewMock(t).WithLogger(quartz.NoOpLogger) + validationTrap := clock.Trap().NewTimer("chatd", "dial-validation-delay") + defer validationTrap.Close() + dialStarted := make(chan struct{}) var dialCalls atomic.Int32 factory := chattest.NewMockAIBridgeTransport(t, openAIURL) _ = newActiveTestServer(t, db, ps, func(cfg *chatd.Config) { + cfg.Clock = clock cfg.AIBridgeTransportFactory = chatAIGatewayTransportFactoryPointer(factory) cfg.AgentConn = func(ctx context.Context, agentID uuid.UUID) (workspacesdk.AgentConn, func(), error) { dialCalls.Add(1) require.Equal(t, dbAgent.ID, agentID) + close(dialStarted) <-ctx.Done() return nil, nil, ctx.Err() } }) + validationTimer := validationTrap.MustWait(ctx) + validationTimer.MustRelease(ctx) + testutil.TryReceive(ctx, t, dialStarted) + advanceMockClockBy(ctx, t, clock, validationTimer.Duration) + var chatResult database.Chat require.Eventually(t, func() bool { got, getErr := db.GetChatByID(ctx, chat.ID) diff --git a/coderd/x/chatd/chatstate/transitions_matrix_test.go b/coderd/x/chatd/chatstate/transitions_matrix_test.go index dfa939f94d4..d6e0766b62d 100644 --- a/coderd/x/chatd/chatstate/transitions_matrix_test.go +++ b/coderd/x/chatd/chatstate/transitions_matrix_test.go @@ -533,10 +533,9 @@ func remainingBodiesExcluding(bodies []string, exclude int) []string { // runPositiveCase seeds the chat, runs the transition, and asserts the // post-state plus case-specific effects. -func runPositiveCase(t *testing.T, spec transitionCaseSpec) { +func runPositiveCase(t *testing.T, f *testFixture, spec transitionCaseSpec) { t.Helper() require.NotNil(t, spec.apply, "case %s missing apply", spec.subtestName()) - f := newTestFixture(t) ctx := testutil.Context(t, testutil.WaitShort) seeder := spec.seed @@ -571,9 +570,8 @@ func runPositiveCase(t *testing.T, spec transitionCaseSpec) { // runDisallowedCase seeds the chat, runs the transition with default // inputs, and asserts that the chatstate package surfaces the right // sentinel error and rolled the snapshot bump back. -func runDisallowedCase(t *testing.T, tr chatstate.Transition, from chatstate.ExecutionState) { +func runDisallowedCase(t *testing.T, f *testFixture, tr chatstate.Transition, from chatstate.ExecutionState) { t.Helper() - f := newTestFixture(t) ctx := testutil.Context(t, testutil.WaitShort) seeded := seedState(t, f, from) if seeded.exists { @@ -628,6 +626,8 @@ func runDisallowedCase(t *testing.T, tr chatstate.Transition, from chatstate.Exe func TestTransitionMatrix_AllCombinations(t *testing.T) { t.Parallel() + // Each case owns its chat and publisher; dependencies are read-only. + fixture := newTestFixture(t) cases := matrixCases() // Detect duplicate full keys and duplicate subtest names. The @@ -737,7 +737,9 @@ func TestTransitionMatrix_AllCombinations(t *testing.T) { actualPositive[tc.key()] = struct{}{} mu.Unlock() } - runPositiveCase(t, tc) + f := *fixture + f.Pub = newRecordingPubsub() + runPositiveCase(t, &f, tc) }) } }) @@ -769,7 +771,9 @@ func TestTransitionMatrix_AllCombinations(t *testing.T) { mu.Lock() actualDisallowed[disallowedCaseKey{transition: tr, from: from}] = struct{}{} mu.Unlock() - runDisallowedCase(t, tr, from) + f := *fixture + f.Pub = newRecordingPubsub() + runDisallowedCase(t, &f, tr, from) }) } })