(t *testing.T)
| 3251 | } |
| 3252 | |
| 3253 | func TestAwaitSubagentCompletion(t *testing.T) { |
| 3254 | t.Parallel() |
| 3255 | |
| 3256 | // Shared fixtures for subtests that use a real clock. Each |
| 3257 | // subtest creates its own parent+child chats (unique IDs) |
| 3258 | // so they don't collide. Mock-clock subtests need their own |
| 3259 | // DB and server because the Server's background tickers |
| 3260 | // also use the mock clock. |
| 3261 | db, ps := dbtestutil.NewDB(t) |
| 3262 | server := newInternalTestServer(t, db, ps, chatprovider.ProviderAPIKeys{}) |
| 3263 | user, org, model := seedInternalChatDeps(t, db) |
| 3264 | |
| 3265 | t.Run("NotDescendant", func(t *testing.T) { |
| 3266 | t.Parallel() |
| 3267 | ctx := chatdTestContext(t) |
| 3268 | |
| 3269 | parent, _ := createParentChildChats(ctx, t, server, user, org, model) |
| 3270 | |
| 3271 | unrelated, err := server.CreateChat(ctx, CreateOptions{ |
| 3272 | OrganizationID: org.ID, |
| 3273 | OwnerID: user.ID, |
| 3274 | Title: "unrelated", |
| 3275 | ModelConfigID: model.ID, |
| 3276 | InitialUserContent: []codersdk.ChatMessagePart{codersdk.ChatMessageText("other")}, |
| 3277 | }) |
| 3278 | require.NoError(t, err) |
| 3279 | |
| 3280 | _, _, err = server.awaitSubagentCompletion( |
| 3281 | ctx, parent.ID, unrelated.ID, time.Second, |
| 3282 | ) |
| 3283 | require.ErrorIs(t, err, ErrSubagentNotDescendant) |
| 3284 | }) |
| 3285 | |
| 3286 | t.Run("AlreadyWaiting", func(t *testing.T) { |
| 3287 | t.Parallel() |
| 3288 | ctx := chatdTestContext(t) |
| 3289 | |
| 3290 | parent, child := createParentChildChats(ctx, t, server, user, org, model) |
| 3291 | |
| 3292 | setChatStatus(ctx, t, db, child.ID, database.ChatStatusWaiting, "") |
| 3293 | insertAssistantMessage(t, db, child.ID, model.ID, "task complete") |
| 3294 | |
| 3295 | gotChat, report, err := server.awaitSubagentCompletion( |
| 3296 | ctx, parent.ID, child.ID, time.Second, |
| 3297 | ) |
| 3298 | require.NoError(t, err) |
| 3299 | assert.Equal(t, child.ID, gotChat.ID) |
| 3300 | assert.Equal(t, database.ChatStatusWaiting, gotChat.Status) |
| 3301 | assert.Equal(t, "task complete", report) |
| 3302 | }) |
| 3303 | |
| 3304 | t.Run("AlreadyError", func(t *testing.T) { |
| 3305 | t.Parallel() |
| 3306 | ctx := chatdTestContext(t) |
| 3307 | |
| 3308 | parent, child := createParentChildChats(ctx, t, server, user, org, model) |
| 3309 | |
| 3310 | setChatStatus(ctx, t, db, child.ID, database.ChatStatusError, "something broke") |
nothing calls this directly
no test coverage detected