|
| 1 | +package coderd_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/coder/coder/v2/coderd/coderdtest" |
| 12 | + "github.com/coder/coder/v2/coderd/database" |
| 13 | + "github.com/coder/coder/v2/coderd/database/dbgen" |
| 14 | + "github.com/coder/coder/v2/coderd/database/dbtime" |
| 15 | + "github.com/coder/coder/v2/codersdk" |
| 16 | + "github.com/coder/coder/v2/testutil" |
| 17 | +) |
| 18 | + |
| 19 | +func TestChat(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + t.Run("ExperimentAgenticChatDisabled", func(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + |
| 25 | + client, _ := coderdtest.NewWithDatabase(t, nil) |
| 26 | + owner := coderdtest.CreateFirstUser(t, client) |
| 27 | + memberClient, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID) |
| 28 | + |
| 29 | + // Hit the endpoint to get the chat. It should return a 404. |
| 30 | + ctx := testutil.Context(t, testutil.WaitShort) |
| 31 | + _, err := memberClient.ListChats(ctx) |
| 32 | + require.Error(t, err, "list chats should fail") |
| 33 | + var sdkErr *codersdk.Error |
| 34 | + require.ErrorAs(t, err, &sdkErr, "request should fail with an SDK error") |
| 35 | + require.Equal(t, http.StatusForbidden, sdkErr.StatusCode()) |
| 36 | + }) |
| 37 | + |
| 38 | + t.Run("ChatCRUD", func(t *testing.T) { |
| 39 | + t.Parallel() |
| 40 | + |
| 41 | + dv := coderdtest.DeploymentValues(t) |
| 42 | + dv.Experiments = []string{string(codersdk.ExperimentAgenticChat)} |
| 43 | + dv.AI.Value = codersdk.AIConfig{ |
| 44 | + Providers: []codersdk.AIProviderConfig{ |
| 45 | + { |
| 46 | + Type: "fake", |
| 47 | + APIKey: "", |
| 48 | + BaseURL: "http://localhost", |
| 49 | + Models: []string{"fake-model"}, |
| 50 | + }, |
| 51 | + }, |
| 52 | + } |
| 53 | + client, db := coderdtest.NewWithDatabase(t, &coderdtest.Options{ |
| 54 | + DeploymentValues: dv, |
| 55 | + }) |
| 56 | + owner := coderdtest.CreateFirstUser(t, client) |
| 57 | + memberClient, memberUser := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID) |
| 58 | + |
| 59 | + // Seed the database with some data. |
| 60 | + dbChat := dbgen.Chat(t, db, database.Chat{ |
| 61 | + OwnerID: memberUser.ID, |
| 62 | + CreatedAt: dbtime.Now().Add(-time.Hour), |
| 63 | + UpdatedAt: dbtime.Now().Add(-time.Hour), |
| 64 | + Title: "This is a test chat", |
| 65 | + }) |
| 66 | + _ = dbgen.ChatMessage(t, db, database.ChatMessage{ |
| 67 | + ChatID: dbChat.ID, |
| 68 | + CreatedAt: dbtime.Now().Add(-time.Hour), |
| 69 | + Content: []byte(`[{"content": "Hello world"}]`), |
| 70 | + Model: "fake model", |
| 71 | + Provider: "fake", |
| 72 | + }) |
| 73 | + |
| 74 | + ctx := testutil.Context(t, testutil.WaitShort) |
| 75 | + |
| 76 | + // Listing chats should return the chat we just inserted. |
| 77 | + chats, err := memberClient.ListChats(ctx) |
| 78 | + require.NoError(t, err, "list chats should succeed") |
| 79 | + require.Len(t, chats, 1, "response should have one chat") |
| 80 | + require.Equal(t, dbChat.ID, chats[0].ID, "unexpected chat ID") |
| 81 | + require.Equal(t, dbChat.Title, chats[0].Title, "unexpected chat title") |
| 82 | + require.Equal(t, dbChat.CreatedAt.UTC(), chats[0].CreatedAt.UTC(), "unexpected chat created at") |
| 83 | + require.Equal(t, dbChat.UpdatedAt.UTC(), chats[0].UpdatedAt.UTC(), "unexpected chat updated at") |
| 84 | + |
| 85 | + // Fetching a single chat by ID should return the same chat. |
| 86 | + chat, err := memberClient.Chat(ctx, dbChat.ID) |
| 87 | + require.NoError(t, err, "get chat should succeed") |
| 88 | + require.Equal(t, chats[0], chat, "get chat should return the same chat") |
| 89 | + |
| 90 | + // Listing chat messages should return the message we just inserted. |
| 91 | + messages, err := memberClient.ChatMessages(ctx, dbChat.ID) |
| 92 | + require.NoError(t, err, "list chat messages should succeed") |
| 93 | + require.Len(t, messages, 1, "response should have one message") |
| 94 | + require.Equal(t, "Hello world", messages[0].Content, "response should have the correct message content") |
| 95 | + |
| 96 | + // Creating a new chat will fail because the model does not exist. |
| 97 | + // TODO: Test the message streaming functionality with a mock model. |
| 98 | + // Inserting a chat message will fail due to the model not existing. |
| 99 | + _, err = memberClient.CreateChatMessage(ctx, dbChat.ID, codersdk.CreateChatMessageRequest{ |
| 100 | + Model: "echo", |
| 101 | + Message: codersdk.ChatMessage{ |
| 102 | + Role: "user", |
| 103 | + Content: "Hello world", |
| 104 | + }, |
| 105 | + Thinking: false, |
| 106 | + }) |
| 107 | + require.Error(t, err, "create chat message should fail") |
| 108 | + var sdkErr *codersdk.Error |
| 109 | + require.ErrorAs(t, err, &sdkErr, "create chat should fail with an SDK error") |
| 110 | + require.Equal(t, http.StatusBadRequest, sdkErr.StatusCode(), "create chat should fail with a 400 when model does not exist") |
| 111 | + |
| 112 | + // Creating a new chat message with malformed content should fail. |
| 113 | + res, err := memberClient.Request(ctx, http.MethodPost, "/api/v2/chats/"+dbChat.ID.String()+"/messages", strings.NewReader(`{malformed json}`)) |
| 114 | + require.NoError(t, err) |
| 115 | + defer res.Body.Close() |
| 116 | + apiErr := codersdk.ReadBodyAsError(res) |
| 117 | + require.Contains(t, apiErr.Error(), "Failed to decode chat message") |
| 118 | + |
| 119 | + _, err = memberClient.CreateChat(ctx) |
| 120 | + require.NoError(t, err, "create chat should succeed") |
| 121 | + chats, err = memberClient.ListChats(ctx) |
| 122 | + require.NoError(t, err, "list chats should succeed") |
| 123 | + require.Len(t, chats, 2, "response should have two chats") |
| 124 | + }) |
| 125 | +} |
0 commit comments