MCPcopy Index your code
hub / github.com/coder/coder / TestChatSystemPrompt

Function TestChatSystemPrompt

coderd/exp_chats_test.go:10751–11178  ·  view source on GitHub ↗

nolint:tparallel,paralleltest // Subtests share a single coderdtest instance.

(t *testing.T)

Source from the content-addressed store, hash-verified

10749
10750//nolint:tparallel,paralleltest // Subtests share a single coderdtest instance.
10751func TestChatSystemPrompt(t *testing.T) {
10752 t.Parallel()
10753
10754 adminClient, db := newChatClientWithDatabase(t)
10755 firstUser := coderdtest.CreateFirstUser(t, adminClient.Client)
10756 _ = createChatModelConfig(t, adminClient)
10757 memberClientRaw, _ := coderdtest.CreateAnotherUser(t, adminClient.Client, firstUser.OrganizationID)
10758 memberClient := codersdk.NewExperimentalClient(memberClientRaw)
10759
10760 const workspaceAwareness = `No workspace is attached to this chat yet.
10761Do not create or start a workspace by default. Many requests can be completed using the conversation, provider tools such as web_search when available, or configured external MCP tools.
10762Workspace tools such as execute, read_file, write_file, and edit_files require an attached workspace. Only call create_workspace or start_workspace when the user explicitly asks for a workspace-backed task, or when the task cannot be completed without inspecting, editing, or running files in a workspace.
10763If a workspace is needed, use list_templates and read_template as needed before create_workspace.`
10764
10765 updateChatSystemPrompt := func(t *testing.T, ctx context.Context, req codersdk.UpdateChatSystemPromptRequest) {
10766 t.Helper()
10767
10768 err := adminClient.UpdateChatSystemPrompt(ctx, req)
10769 require.NoError(t, err)
10770 }
10771
10772 getChatSystemPrompt := func(t *testing.T, ctx context.Context) codersdk.ChatSystemPromptResponse {
10773 t.Helper()
10774
10775 resp, err := adminClient.GetChatSystemPrompt(ctx)
10776 require.NoError(t, err)
10777 return resp
10778 }
10779
10780 assertInjectedSystemMessages := func(t *testing.T, ctx context.Context, wantResolvedPrompt string) {
10781 t.Helper()
10782
10783 chat, err := adminClient.CreateChat(ctx, codersdk.CreateChatRequest{
10784 OrganizationID: firstUser.OrganizationID,
10785 Content: []codersdk.ChatInputPart{
10786 {
10787 Type: codersdk.ChatInputPartTypeText,
10788 Text: fmt.Sprintf("system prompt composition %s", t.Name()),
10789 },
10790 },
10791 })
10792 require.NoError(t, err)
10793
10794 messages, err := db.GetChatMessagesForPromptByChatID(dbauthz.AsSystemRestricted(ctx), chat.ID)
10795 require.NoError(t, err)
10796
10797 var systemTexts []string
10798 for _, message := range messages {
10799 if message.Role != database.ChatMessageRoleSystem {
10800 continue
10801 }
10802 parts, err := chatprompt.ParseContent(message)
10803 require.NoError(t, err)
10804 require.Len(t, parts, 1)
10805 require.Equal(t, codersdk.ChatMessagePartTypeText, parts[0].Type)
10806 systemTexts = append(systemTexts, parts[0].Text)
10807 }
10808

Callers

nothing calls this directly

Calls 15

GetChatSystemPromptMethod · 0.95
CreateChatMethod · 0.95
StatusCodeMethod · 0.95
CreateFirstUserFunction · 0.92
CreateAnotherUserFunction · 0.92
NewExperimentalClientFunction · 0.92
AsSystemRestrictedFunction · 0.92
ParseContentFunction · 0.92
ContextFunction · 0.92
RefFunction · 0.92
NewDBFunction · 0.92

Tested by

no test coverage detected