Files
coder/coderd/x/chatd/subagent_test.go
T
Michael Suchacz bcaf21b812 test(coderd/x/chatd): merge the computer-use child CreateChat tests (#30943)
The four `TestSpawnComputerUseAgent_*` tests in `subagent_test.go`
(`CreatesChildWithChatMode`, `SystemPromptFormat`,
`ChildIsListedUnderParent` and `RootChatIDPropagation`) each built a
database and a server, created a parent and a computer-use child through
`Server.CreateChat` with the same options, and checked a different part
of the result. This PR merges them into one test,
`TestCreateChat_ComputerUseChildPersistsLineage`, that builds the
fixture once and keeps every check. The old names suggested
`spawn_agent` coverage, but `spawn_agent` creates its child through
`chatstate.CreateChatWithID`, and its tests live in
`subagent_internal_test.go`, which this PR does not touch.

**Why it is safe**

- Every removed assertion is kept. ParentChatID, RootChatID and Mode are
checked on both the returned chat and a fresh `GetChatByID` read, and
the test still checks that a system-role message contains the prompt.
- I ran 14 production mutants with `go test -overlay` against the old
tests on `main` and against the new test. They drop ParentChatID,
RootChatID or Mode from the `CreateChat` input, from the insert or from
the returned chat, drop the system prompt or store it with the user
role, replace the returned chat with a zero chat, force every message
role to user, or blank the message content. The old tests and the new
test each catch all 14.

**Review focus**

The loop over `{"returned": child, "fetched": got}` runs the same six
checks on both sources, and each failure message names its source.

**Validation**

- `go test ./coderd/x/chatd/ -run
'TestSpawnComputerUseAgent|TestCreateChat_ComputerUse' -count=1 -p 4
-parallel 4`: passes on `main` (old names) and on this PR (new name)
- `go vet ./coderd/x/chatd/`: clean
- `golangci-lint run --fix=false ./coderd/x/chatd/`: 0 issues
- pre-commit hook: passed

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

74 lines
2.4 KiB
Go

package chatd_test
import (
"strings"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/x/chatd"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
)
func TestCreateChat_ComputerUseChildPersistsLineage(t *testing.T) {
t.Parallel()
db, ps := dbtestutil.NewDB(t)
server := newTestServer(t, db, ps, uuid.New())
ctx := testutil.Context(t, testutil.WaitLong)
user, org, model := seedChatDependencies(t, db)
parent, err := server.CreateChat(ctx, chatd.CreateOptions{
OrganizationID: org.ID,
OwnerID: user.ID,
Title: "parent",
ModelConfigID: model.ID,
InitialUserContent: []codersdk.ChatMessagePart{codersdk.ChatMessageText("hello")},
})
require.NoError(t, err)
prompt := "Use the desktop to open Firefox"
parentID := uuid.NullUUID{UUID: parent.ID, Valid: true}
child, err := server.CreateChat(ctx, chatd.CreateOptions{
OrganizationID: org.ID,
OwnerID: parent.OwnerID,
ParentChatID: parentID,
RootChatID: parentID,
ModelConfigID: model.ID,
Title: "computer-use",
ChatMode: database.NullChatMode{ChatMode: database.ChatModeComputerUse, Valid: true},
SystemPrompt: "Computer use instructions\n\n" + prompt,
InitialUserContent: []codersdk.ChatMessagePart{codersdk.ChatMessageText(prompt)},
})
require.NoError(t, err)
got, err := db.GetChatByID(ctx, child.ID)
require.NoError(t, err)
for name, c := range map[string]database.Chat{"returned": child, "fetched": got} {
require.True(t, c.ParentChatID.Valid, name)
assert.Equal(t, parent.ID, c.ParentChatID.UUID, name)
require.True(t, c.RootChatID.Valid, name)
assert.Equal(t, parent.ID, c.RootChatID.UUID, name)
require.True(t, c.Mode.Valid, name)
assert.Equal(t, database.ChatModeComputerUse, c.Mode.ChatMode, name)
}
messages, err := db.GetChatMessagesForPromptByChatID(ctx, child.ID)
require.NoError(t, err)
// The system message raw content is a JSON-encoded string.
var foundPrompt bool
for _, msg := range messages {
if msg.Role == database.ChatMessageRoleSystem && msg.Content.Valid &&
strings.Contains(string(msg.Content.RawMessage), prompt) {
foundPrompt = true
break
}
}
assert.True(t, foundPrompt, "a system message should contain the user prompt")
}