Removes the experimental Chat Automations feature (`chat-automations` experiment) from main. This reverts the 16-PR stack that added it and adds two forward migrations that undo its schema and credential changes.
> [!WARNING]
> **Upgrade consequences**
>
> - **Automation data is deleted.** Migration 000615 drops the `chat_automations` table and deletes every queued message an automation delivered. Chats, their messages and ordinary queued messages stay. Messages lose their automation provenance (`automation_id`, `input_id`).
> - **Automation-only credentials are revoked.** Migration 000616 deletes API keys, OAuth2 codes and OAuth2 tokens whose only scopes (or allow-list entries) were `chat_automation:*`. Mixed credentials keep their other scopes. Custom roles lose their `chat_automation` permissions. OAuth2 apps that allowed only `chat_automation` scopes get the whitespace allowlist that grants nothing, as in 000595.
> - **A coordinated restart is required.** Stop all old replicas before the new version runs the migrations. Old replicas read the dropped table and columns, so a rolling deploy is not supported.
> - **Remove `--chat-max-automations-per-owner` before upgrading.** The flag, the YAML key `chat.maxAutomationsPerOwner` and the env var `CODER_CHAT_MAX_AUTOMATIONS_PER_OWNER` are removed. A deployment that still sets the flag or the YAML key will not start (`unknown flag` / `unknown option`). The env var is ignored. This includes dogfood. No release or tag contains the flag.
> - **Down migrations restore the schema only.** 000616 down and 000615 down recreate the 000611 schema with empty tables. They do not restore deleted automations, webhook secrets, provenance, queued deliveries, revoked credentials, stripped permissions or the deleted experiment rule.
## What is removed
- The `chat-automations` experiment and its stored runtime rule (`experiment_rule:chat-automations`).
- The `--chat-max-automations-per-owner` deployment option.
- The `chat_automation` RBAC resource, its member and chatd grants, and the `chat_automation:*` API key scopes.
- The automation sqlc queries, dbauthz wrappers and audit table entries.
- The management routes under `/api/experimental/organizations/{organization}/chat-automations`, and the webhook route `POST /api/experimental/chat-automations/{automation}/events` with its rate limiter.
- The chatd management service, webhook publish, schedule loop, the `manage_automations` tool, and the chatstate admission seam and queue promotion guard that only automations used.
- The Agents Automations page, editor, nav entry, chat labels, the Manage automations toggle and the API client.
- The `archived:any` chat search value that the automations "View chats" dialog added.
- `docs/ai-coder/agents/automations.md`, its manifest entry, the glossary entry and the related rows.
Reverted PRs: #30085, #30096, #30112, #30117, #30127, #30134, #30160, #30166, #30167, #30171, #30187, #30207, #30214, #30217, #30219, #30220.
## What stays, and why
- Migrations 000609, 000610 and 000611, their fixture, and the #30297 lock fix with `migration000609_test.go` stay unchanged. Deployments such as dogfood already ran them.
- New migration **000615** reverses the schema changes of 000609 and 000611 against the current schema. It uses the same short-wait lock-and-retry block as 000609, and it rebuilds `chats_expanded` from main's current definition minus the two automation columns. New migration **000616** follows the 000595 Tasks removal for credentials and recreates `api_key_scope` without the five `chat_automation:*` values. It keeps the `chat_project_memory:*` values from 000612.
- The `chat_automation` value stays in the `resource_type` enum, and `codersdk.ResourceTypeChatAutomation` stays. Old audit log rows stay readable, as with `task` after the Tasks removal.
- The 000615 delete fires the existing queue-version trigger, so `queue_version` moves to `snapshot_version` for each affected chat. `snapshot_version` is not bumped: the coordinated restart restarts every stream loop from an empty state, and the site does not read the version fields.
- `coderd/x/chatd/ARCHITECTURE.md` drops the sections the stack added and gets back its pre-stack wording, merged with the later main edits to the same file.
## Notes for reviewers
- #30379 (chat project page) merged while this PR was in progress. It passed `manageAutomationsEnabled` in `AgentCreatePage.test.tsx` and listed an `automations` route in `AgentsPageLayout.test.tsx`. Later main commits also added automation props in `ConversationTimeline.test.tsx` and `ConversationTimelineWorkingBlocks.stories.tsx`. This PR removes all of them.
- Remaining `automation` matches under `coderd`, `codersdk`, `enterprise`, `site/src` and `docs` are intended:
- migrations 000609 to 000611, their fixture and `migration000609_test.go`;
- the new 000615 and 000616 migrations and `migration000615_test.go`;
- the `chat_automation` resource type in `dump.sql`, `models.go`, `codersdk/audit.go`, the generated API docs, `swagger.json` and `typesGenerated.ts`;
- unrelated uses of the word "automation" (docs, `chatlabels_test.go`, `ratelimit.go`, `autostop.go`, `subagent_catalog.go`, `WorkspaceSharingSection.tsx`, `AgentChatPage.stories.tsx`).
Remote UAT on an isolated dev deployment passed. It upgraded a populated pre-removal database, then checked chats, queued messages, tokens, the 404 routes, the removed page and nav entry, and the experiments list.
---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_

1246 lines
39 KiB
Go
1246 lines
39 KiB
Go
package db2sdk_test
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/url"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"charm.land/fantasy"
|
|
"github.com/google/uuid"
|
|
"github.com/sqlc-dev/pqtype"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/db2sdk"
|
|
"github.com/coder/coder/v2/coderd/database/dbgen"
|
|
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
|
"github.com/coder/coder/v2/coderd/database/dbtime"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/provisionersdk/proto"
|
|
)
|
|
|
|
func TestGroupMemberAISpend(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
userID := uuid.New()
|
|
effectiveGroupID := uuid.New()
|
|
everyoneGroupID := uuid.New()
|
|
tests := []struct {
|
|
name string
|
|
row database.GetGroupMembersAISpendRow
|
|
queriedGroupID uuid.UUID
|
|
want codersdk.GroupMemberAISpend
|
|
}{
|
|
{
|
|
name: "EffectiveGroupIsQueriedGroup",
|
|
row: database.GetGroupMembersAISpendRow{
|
|
UserID: userID,
|
|
EffectiveGroupID: uuid.NullUUID{UUID: effectiveGroupID, Valid: true},
|
|
EffectiveSpendLimitMicros: sql.NullInt64{Int64: 2_000_000, Valid: true},
|
|
EffectiveLimitSource: sql.NullString{String: "user_override", Valid: true},
|
|
GroupSpendMicros: 500_000,
|
|
},
|
|
queriedGroupID: effectiveGroupID,
|
|
want: codersdk.GroupMemberAISpend{
|
|
UserID: userID,
|
|
EffectiveGroupID: &effectiveGroupID,
|
|
EffectiveBudget: &codersdk.AIBudgetLimit{
|
|
SpendLimitMicros: 2_000_000,
|
|
LimitSource: codersdk.AIBudgetLimitSourceUserOverride,
|
|
},
|
|
GroupBudget: &codersdk.AIBudgetLimit{
|
|
SpendLimitMicros: 2_000_000,
|
|
LimitSource: codersdk.AIBudgetLimitSourceUserOverride,
|
|
},
|
|
GroupSpendMicros: 500_000,
|
|
},
|
|
},
|
|
{
|
|
name: "EffectiveGroupDiffersFromQueriedGroup",
|
|
row: database.GetGroupMembersAISpendRow{
|
|
UserID: userID,
|
|
EffectiveGroupID: uuid.NullUUID{UUID: effectiveGroupID, Valid: true},
|
|
EffectiveSpendLimitMicros: sql.NullInt64{Int64: 1_000_000, Valid: true},
|
|
EffectiveLimitSource: sql.NullString{String: "group", Valid: true},
|
|
GroupSpendMicros: 250_000,
|
|
},
|
|
queriedGroupID: uuid.New(),
|
|
want: codersdk.GroupMemberAISpend{
|
|
UserID: userID,
|
|
EffectiveGroupID: &effectiveGroupID,
|
|
EffectiveBudget: &codersdk.AIBudgetLimit{
|
|
SpendLimitMicros: 1_000_000,
|
|
LimitSource: codersdk.AIBudgetLimitSourceGroup,
|
|
},
|
|
GroupBudget: nil,
|
|
GroupSpendMicros: 250_000,
|
|
},
|
|
},
|
|
{
|
|
name: "BudgetedEveryoneGroupDiffersFromQueriedGroup",
|
|
row: database.GetGroupMembersAISpendRow{
|
|
UserID: userID,
|
|
EffectiveGroupID: uuid.NullUUID{UUID: everyoneGroupID, Valid: true},
|
|
EffectiveSpendLimitMicros: sql.NullInt64{Int64: 1_000_000, Valid: true},
|
|
EffectiveLimitSource: sql.NullString{String: "group", Valid: true},
|
|
},
|
|
queriedGroupID: uuid.New(),
|
|
want: codersdk.GroupMemberAISpend{
|
|
UserID: userID,
|
|
EffectiveGroupID: &everyoneGroupID,
|
|
EffectiveBudget: &codersdk.AIBudgetLimit{
|
|
SpendLimitMicros: 1_000_000,
|
|
LimitSource: codersdk.AIBudgetLimitSourceGroup,
|
|
},
|
|
GroupBudget: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "UnlimitedEveryoneFallback",
|
|
row: database.GetGroupMembersAISpendRow{
|
|
UserID: userID,
|
|
EffectiveGroupID: uuid.NullUUID{UUID: everyoneGroupID, Valid: true},
|
|
},
|
|
queriedGroupID: uuid.New(),
|
|
want: codersdk.GroupMemberAISpend{
|
|
UserID: userID,
|
|
EffectiveGroupID: &everyoneGroupID,
|
|
EffectiveBudget: nil,
|
|
GroupBudget: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "NullFields",
|
|
row: database.GetGroupMembersAISpendRow{UserID: userID},
|
|
want: codersdk.GroupMemberAISpend{
|
|
UserID: userID,
|
|
EffectiveGroupID: nil,
|
|
EffectiveBudget: nil,
|
|
GroupBudget: nil,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
require.Equal(t, tt.want, db2sdk.GroupMemberAISpend(tt.row, tt.queriedGroupID))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProvisionerJobStatus(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
name string
|
|
job database.ProvisionerJob
|
|
status codersdk.ProvisionerJobStatus
|
|
}{
|
|
{
|
|
name: "canceling",
|
|
job: database.ProvisionerJob{
|
|
CanceledAt: sql.NullTime{
|
|
Time: dbtime.Now().Add(-time.Minute),
|
|
Valid: true,
|
|
},
|
|
},
|
|
status: codersdk.ProvisionerJobCanceling,
|
|
},
|
|
{
|
|
name: "canceled",
|
|
job: database.ProvisionerJob{
|
|
CanceledAt: sql.NullTime{
|
|
Time: dbtime.Now().Add(-time.Minute),
|
|
Valid: true,
|
|
},
|
|
CompletedAt: sql.NullTime{
|
|
Time: dbtime.Now().Add(-30 * time.Second),
|
|
Valid: true,
|
|
},
|
|
},
|
|
status: codersdk.ProvisionerJobCanceled,
|
|
},
|
|
{
|
|
name: "canceled_failed",
|
|
job: database.ProvisionerJob{
|
|
CanceledAt: sql.NullTime{
|
|
Time: dbtime.Now().Add(-time.Minute),
|
|
Valid: true,
|
|
},
|
|
CompletedAt: sql.NullTime{
|
|
Time: dbtime.Now().Add(-30 * time.Second),
|
|
Valid: true,
|
|
},
|
|
Error: sql.NullString{String: "badness", Valid: true},
|
|
},
|
|
status: codersdk.ProvisionerJobFailed,
|
|
},
|
|
{
|
|
name: "pending",
|
|
job: database.ProvisionerJob{},
|
|
status: codersdk.ProvisionerJobPending,
|
|
},
|
|
{
|
|
name: "succeeded",
|
|
job: database.ProvisionerJob{
|
|
StartedAt: sql.NullTime{
|
|
Time: dbtime.Now().Add(-time.Minute),
|
|
Valid: true,
|
|
},
|
|
CompletedAt: sql.NullTime{
|
|
Time: dbtime.Now().Add(-30 * time.Second),
|
|
Valid: true,
|
|
},
|
|
},
|
|
status: codersdk.ProvisionerJobSucceeded,
|
|
},
|
|
{
|
|
name: "completed_failed",
|
|
job: database.ProvisionerJob{
|
|
StartedAt: sql.NullTime{
|
|
Time: dbtime.Now().Add(-time.Minute),
|
|
Valid: true,
|
|
},
|
|
CompletedAt: sql.NullTime{
|
|
Time: dbtime.Now().Add(-30 * time.Second),
|
|
Valid: true,
|
|
},
|
|
Error: sql.NullString{String: "badness", Valid: true},
|
|
},
|
|
status: codersdk.ProvisionerJobFailed,
|
|
},
|
|
{
|
|
name: "updated",
|
|
job: database.ProvisionerJob{
|
|
StartedAt: sql.NullTime{
|
|
Time: dbtime.Now().Add(-time.Minute),
|
|
Valid: true,
|
|
},
|
|
UpdatedAt: dbtime.Now(),
|
|
},
|
|
status: codersdk.ProvisionerJobRunning,
|
|
},
|
|
}
|
|
|
|
// Share db for all job inserts.
|
|
db, _ := dbtestutil.NewDB(t)
|
|
org := dbgen.Organization(t, db, database.Organization{})
|
|
|
|
for i, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
// Populate standard fields
|
|
now := dbtime.Now().Round(time.Minute)
|
|
tc.job.ID = uuid.New()
|
|
tc.job.CreatedAt = now
|
|
tc.job.UpdatedAt = now
|
|
tc.job.InitiatorID = org.ID
|
|
tc.job.OrganizationID = org.ID
|
|
tc.job.Input = []byte("{}")
|
|
tc.job.Provisioner = database.ProvisionerTypeEcho
|
|
// Unique tags for each job.
|
|
tc.job.Tags = map[string]string{fmt.Sprintf("%d", i): "true"}
|
|
|
|
inserted := dbgen.ProvisionerJob(t, db, nil, tc.job)
|
|
// Make sure the inserted job has the right values.
|
|
require.Equal(t, tc.job.StartedAt.Time.UTC(), inserted.StartedAt.Time.UTC(), "started at")
|
|
require.Equal(t, tc.job.CompletedAt.Time.UTC(), inserted.CompletedAt.Time.UTC(), "completed at")
|
|
require.Equal(t, tc.job.CanceledAt.Time.UTC(), inserted.CanceledAt.Time.UTC(), "canceled at")
|
|
require.Equal(t, tc.job.Error, inserted.Error, "error")
|
|
require.Equal(t, tc.job.ErrorCode, inserted.ErrorCode, "error code")
|
|
|
|
actual := codersdk.ProvisionerJobStatus(inserted.JobStatus)
|
|
require.Equal(t, tc.status, actual)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTemplateVersionParameter_OK(t *testing.T) {
|
|
t.Parallel()
|
|
req := require.New(t)
|
|
|
|
// In this test we're just going to cover the fields that have to get parsed.
|
|
options := []*proto.RichParameterOption{
|
|
{
|
|
Name: "foo",
|
|
Description: "bar",
|
|
Value: "baz",
|
|
Icon: "David Bowie",
|
|
},
|
|
}
|
|
ob, err := json.Marshal(&options)
|
|
req.NoError(err)
|
|
|
|
db := database.TemplateVersionParameter{
|
|
Options: json.RawMessage(ob),
|
|
Description: "_The Rise and Fall of **Ziggy Stardust** and the Spiders from Mars_",
|
|
}
|
|
sdk, err := db2sdk.TemplateVersionParameter(db)
|
|
req.NoError(err)
|
|
req.Len(sdk.Options, 1)
|
|
req.Equal("foo", sdk.Options[0].Name)
|
|
req.Equal("bar", sdk.Options[0].Description)
|
|
req.Equal("baz", sdk.Options[0].Value)
|
|
req.Equal("David Bowie", sdk.Options[0].Icon)
|
|
req.Equal("The Rise and Fall of Ziggy Stardust and the Spiders from Mars", sdk.DescriptionPlaintext)
|
|
}
|
|
|
|
func TestTemplateVersionParameter_BadOptions(t *testing.T) {
|
|
t.Parallel()
|
|
req := require.New(t)
|
|
|
|
db := database.TemplateVersionParameter{
|
|
Options: json.RawMessage("not really JSON!"),
|
|
Description: "_The Rise and Fall of **Ziggy Stardust** and the Spiders from Mars_",
|
|
}
|
|
_, err := db2sdk.TemplateVersionParameter(db)
|
|
req.Error(err)
|
|
}
|
|
|
|
func TestTemplateVersionParameter_BadDescription(t *testing.T) {
|
|
t.Parallel()
|
|
req := require.New(t)
|
|
desc := make([]byte, 300)
|
|
_, err := rand.Read(desc)
|
|
req.NoError(err)
|
|
|
|
db := database.TemplateVersionParameter{
|
|
Options: json.RawMessage("[]"),
|
|
Description: string(desc),
|
|
}
|
|
sdk, err := db2sdk.TemplateVersionParameter(db)
|
|
// Although the markdown parser can return an error, the way we use it should not, even
|
|
// if we feed it garbage data.
|
|
req.NoError(err)
|
|
req.NotEmpty(sdk.DescriptionPlaintext, "broke the markdown parser with %v", desc)
|
|
}
|
|
|
|
func TestOAuth2ProviderApp_ClientType(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
accessURL := &url.URL{Scheme: "https", Host: "coder.example.com"}
|
|
|
|
tests := []struct {
|
|
name string
|
|
dbClientType string
|
|
wantClientType codersdk.OAuth2ClientType
|
|
}{
|
|
{
|
|
name: "Confidential",
|
|
dbClientType: "confidential",
|
|
wantClientType: codersdk.OAuth2ClientTypeConfidential,
|
|
},
|
|
{
|
|
name: "Public",
|
|
dbClientType: "public",
|
|
wantClientType: codersdk.OAuth2ClientTypePublic,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
dbApp := database.OAuth2ProviderApp{
|
|
ID: uuid.New(),
|
|
Name: "test-app-" + tt.name,
|
|
CallbackURL: "https://example.com/callback",
|
|
ClientType: tt.dbClientType,
|
|
}
|
|
|
|
sdkApp := db2sdk.OAuth2ProviderApp(accessURL, dbApp)
|
|
|
|
require.Equal(t, tt.wantClientType, sdkApp.ClientType)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestChatDebugRunSummary(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
startedAt := time.Now().UTC().Round(time.Second)
|
|
finishedAt := startedAt.Add(5 * time.Second)
|
|
|
|
run := database.ChatDebugRun{
|
|
ID: uuid.New(),
|
|
ChatID: uuid.New(),
|
|
Kind: "chat_turn",
|
|
Status: "completed",
|
|
Provider: sql.NullString{String: "openai", Valid: true},
|
|
Model: sql.NullString{String: "gpt-4o", Valid: true},
|
|
Summary: json.RawMessage(`{"step_count":3,"has_error":false}`),
|
|
StartedAt: startedAt,
|
|
UpdatedAt: finishedAt,
|
|
FinishedAt: sql.NullTime{Time: finishedAt, Valid: true},
|
|
}
|
|
|
|
sdk := db2sdk.ChatDebugRunSummary(run)
|
|
|
|
require.Equal(t, run.ID, sdk.ID)
|
|
require.Equal(t, run.ChatID, sdk.ChatID)
|
|
require.Equal(t, codersdk.ChatDebugRunKindChatTurn, sdk.Kind)
|
|
require.Equal(t, codersdk.ChatDebugStatusCompleted, sdk.Status)
|
|
require.NotNil(t, sdk.Provider)
|
|
require.Equal(t, "openai", *sdk.Provider)
|
|
require.NotNil(t, sdk.Model)
|
|
require.Equal(t, "gpt-4o", *sdk.Model)
|
|
require.Equal(t, map[string]any{"step_count": float64(3), "has_error": false}, sdk.Summary)
|
|
require.Equal(t, startedAt, sdk.StartedAt)
|
|
require.Equal(t, finishedAt, sdk.UpdatedAt)
|
|
require.NotNil(t, sdk.FinishedAt)
|
|
require.Equal(t, finishedAt, *sdk.FinishedAt)
|
|
}
|
|
|
|
func TestChatDebugRunSummary_NullableFieldsNil(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
run := database.ChatDebugRun{
|
|
ID: uuid.New(),
|
|
ChatID: uuid.New(),
|
|
Kind: "title_generation",
|
|
Status: "in_progress",
|
|
Summary: json.RawMessage(`{}`),
|
|
StartedAt: time.Now().UTC(),
|
|
UpdatedAt: time.Now().UTC(),
|
|
}
|
|
|
|
sdk := db2sdk.ChatDebugRunSummary(run)
|
|
|
|
require.Nil(t, sdk.Provider, "NULL Provider should map to nil")
|
|
require.Nil(t, sdk.Model, "NULL Model should map to nil")
|
|
require.Nil(t, sdk.FinishedAt, "NULL FinishedAt should map to nil")
|
|
}
|
|
|
|
func TestChatDebugStep(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
startedAt := time.Now().UTC().Round(time.Second)
|
|
finishedAt := startedAt.Add(2 * time.Second)
|
|
attempts := json.RawMessage(`[
|
|
{
|
|
"attempt_number": 1,
|
|
"status": "completed",
|
|
"raw_request": {"url": "https://example.com"},
|
|
"raw_response": {"status": "200"},
|
|
"duration_ms": 123,
|
|
"started_at": "2026-03-01T10:00:01Z",
|
|
"finished_at": "2026-03-01T10:00:02Z"
|
|
}
|
|
]`)
|
|
step := database.ChatDebugStep{
|
|
ID: uuid.New(),
|
|
RunID: uuid.New(),
|
|
ChatID: uuid.New(),
|
|
StepNumber: 1,
|
|
Operation: "stream",
|
|
Status: "completed",
|
|
NormalizedRequest: json.RawMessage(`{"messages":[]}`),
|
|
Attempts: attempts,
|
|
Metadata: json.RawMessage(`{"provider":"openai"}`),
|
|
StartedAt: startedAt,
|
|
UpdatedAt: finishedAt,
|
|
FinishedAt: sql.NullTime{Time: finishedAt, Valid: true},
|
|
}
|
|
|
|
sdk := db2sdk.ChatDebugStep(step)
|
|
|
|
// Verify all scalar fields are mapped correctly.
|
|
require.Equal(t, step.ID, sdk.ID)
|
|
require.Equal(t, step.RunID, sdk.RunID)
|
|
require.Equal(t, step.ChatID, sdk.ChatID)
|
|
require.Equal(t, step.StepNumber, sdk.StepNumber)
|
|
require.Equal(t, codersdk.ChatDebugStepOperationStream, sdk.Operation)
|
|
require.Equal(t, codersdk.ChatDebugStatusCompleted, sdk.Status)
|
|
require.Equal(t, startedAt, sdk.StartedAt)
|
|
require.Equal(t, finishedAt, sdk.UpdatedAt)
|
|
require.Equal(t, &finishedAt, sdk.FinishedAt)
|
|
|
|
// Verify JSON object fields are deserialized.
|
|
require.NotNil(t, sdk.NormalizedRequest)
|
|
require.Equal(t, map[string]any{"messages": []any{}}, sdk.NormalizedRequest)
|
|
require.NotNil(t, sdk.Metadata)
|
|
require.Equal(t, map[string]any{"provider": "openai"}, sdk.Metadata)
|
|
|
|
// Verify nullable fields are nil when the DB row has NULL values.
|
|
require.Nil(t, sdk.HistoryTipMessageID, "NULL HistoryTipMessageID should map to nil")
|
|
require.Nil(t, sdk.AssistantMessageID, "NULL AssistantMessageID should map to nil")
|
|
require.Nil(t, sdk.NormalizedResponse, "NULL NormalizedResponse should map to nil")
|
|
require.Nil(t, sdk.Usage, "NULL Usage should map to nil")
|
|
require.Nil(t, sdk.Error, "NULL Error should map to nil")
|
|
|
|
// Verify attempts are preserved with all fields.
|
|
require.Len(t, sdk.Attempts, 1)
|
|
require.Equal(t, float64(1), sdk.Attempts[0]["attempt_number"])
|
|
require.Equal(t, "completed", sdk.Attempts[0]["status"])
|
|
require.Equal(t, float64(123), sdk.Attempts[0]["duration_ms"])
|
|
require.Equal(t, map[string]any{"url": "https://example.com"}, sdk.Attempts[0]["raw_request"])
|
|
require.Equal(t, map[string]any{"status": "200"}, sdk.Attempts[0]["raw_response"])
|
|
}
|
|
|
|
func TestChatDebugStep_NullableFieldsPopulated(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tipID := int64(42)
|
|
asstID := int64(99)
|
|
step := database.ChatDebugStep{
|
|
ID: uuid.New(),
|
|
RunID: uuid.New(),
|
|
ChatID: uuid.New(),
|
|
StepNumber: 2,
|
|
Operation: "generate",
|
|
Status: "completed",
|
|
HistoryTipMessageID: sql.NullInt64{Int64: tipID, Valid: true},
|
|
AssistantMessageID: sql.NullInt64{Int64: asstID, Valid: true},
|
|
NormalizedRequest: json.RawMessage(`{}`),
|
|
NormalizedResponse: pqtype.NullRawMessage{RawMessage: json.RawMessage(`{"text":"hi"}`), Valid: true},
|
|
Usage: pqtype.NullRawMessage{RawMessage: json.RawMessage(`{"tokens":10}`), Valid: true},
|
|
Error: pqtype.NullRawMessage{RawMessage: json.RawMessage(`{"code":"rate_limit"}`), Valid: true},
|
|
Attempts: json.RawMessage(`[]`),
|
|
Metadata: json.RawMessage(`{}`),
|
|
StartedAt: time.Now().UTC(),
|
|
UpdatedAt: time.Now().UTC(),
|
|
}
|
|
|
|
sdk := db2sdk.ChatDebugStep(step)
|
|
|
|
require.NotNil(t, sdk.HistoryTipMessageID)
|
|
require.Equal(t, tipID, *sdk.HistoryTipMessageID)
|
|
require.NotNil(t, sdk.AssistantMessageID)
|
|
require.Equal(t, asstID, *sdk.AssistantMessageID)
|
|
require.NotNil(t, sdk.NormalizedResponse)
|
|
require.Equal(t, map[string]any{"text": "hi"}, sdk.NormalizedResponse)
|
|
require.NotNil(t, sdk.Usage)
|
|
require.Equal(t, map[string]any{"tokens": float64(10)}, sdk.Usage)
|
|
require.NotNil(t, sdk.Error)
|
|
require.Equal(t, map[string]any{"code": "rate_limit"}, sdk.Error)
|
|
}
|
|
|
|
func TestChatDebugStep_PreservesMalformedAttempts(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
step := database.ChatDebugStep{
|
|
ID: uuid.New(),
|
|
RunID: uuid.New(),
|
|
ChatID: uuid.New(),
|
|
StepNumber: 1,
|
|
Operation: "stream",
|
|
Status: "completed",
|
|
NormalizedRequest: json.RawMessage(`{"messages":[]}`),
|
|
Attempts: json.RawMessage(`{"bad":true}`),
|
|
Metadata: json.RawMessage(`{"provider":"openai"}`),
|
|
StartedAt: time.Now().UTC(),
|
|
UpdatedAt: time.Now().UTC(),
|
|
}
|
|
|
|
sdk := db2sdk.ChatDebugStep(step)
|
|
require.Len(t, sdk.Attempts, 1)
|
|
require.Equal(t, "malformed attempts payload", sdk.Attempts[0]["error"])
|
|
require.NotEmpty(t, sdk.Attempts[0]["parse_error"], "parse_error should contain the unmarshal error")
|
|
require.Equal(t, `{"bad":true}`, sdk.Attempts[0]["raw"])
|
|
}
|
|
|
|
func TestChatDebugRunSummary_PreservesMalformedSummary(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
run := database.ChatDebugRun{
|
|
ID: uuid.New(),
|
|
ChatID: uuid.New(),
|
|
Kind: "chat_turn",
|
|
Status: "completed",
|
|
Summary: json.RawMessage(`not-an-object`),
|
|
StartedAt: time.Now().UTC(),
|
|
UpdatedAt: time.Now().UTC(),
|
|
}
|
|
|
|
sdk := db2sdk.ChatDebugRunSummary(run)
|
|
require.Equal(t, "malformed debug payload", sdk.Summary["error"])
|
|
require.NotEmpty(t, sdk.Summary["parse_error"], "parse_error should contain the unmarshal error")
|
|
require.Equal(t, "not-an-object", sdk.Summary["raw"])
|
|
}
|
|
|
|
func TestChatDebugStep_PreservesMalformedRequest(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
step := database.ChatDebugStep{
|
|
ID: uuid.New(),
|
|
RunID: uuid.New(),
|
|
ChatID: uuid.New(),
|
|
StepNumber: 1,
|
|
Operation: "stream",
|
|
Status: "completed",
|
|
NormalizedRequest: json.RawMessage(`[1,2,3]`),
|
|
Attempts: json.RawMessage(`[]`),
|
|
Metadata: json.RawMessage(`"just-a-string"`),
|
|
StartedAt: time.Now().UTC(),
|
|
UpdatedAt: time.Now().UTC(),
|
|
}
|
|
|
|
sdk := db2sdk.ChatDebugStep(step)
|
|
require.Equal(t, "malformed debug payload", sdk.NormalizedRequest["error"])
|
|
require.NotEmpty(t, sdk.NormalizedRequest["parse_error"], "parse_error should contain the unmarshal error")
|
|
require.Equal(t, "[1,2,3]", sdk.NormalizedRequest["raw"])
|
|
require.Equal(t, "malformed debug payload", sdk.Metadata["error"])
|
|
require.NotEmpty(t, sdk.Metadata["parse_error"], "parse_error should contain the unmarshal error")
|
|
require.Equal(t, `"just-a-string"`, sdk.Metadata["raw"])
|
|
}
|
|
|
|
func TestChatDebugRunSummary_JSONNullYieldsEmptyMap(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
run := database.ChatDebugRun{
|
|
ID: uuid.New(),
|
|
ChatID: uuid.New(),
|
|
Kind: "chat_turn",
|
|
Status: "completed",
|
|
Summary: json.RawMessage(`null`),
|
|
StartedAt: time.Now().UTC(),
|
|
UpdatedAt: time.Now().UTC(),
|
|
}
|
|
|
|
sdk := db2sdk.ChatDebugRunSummary(run)
|
|
require.NotNil(t, sdk.Summary, "JSON literal null must produce non-nil map")
|
|
require.Empty(t, sdk.Summary, "JSON literal null must produce empty map")
|
|
}
|
|
|
|
func TestChatDebugStep_JSONNullYieldsEmptyStructures(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
step := database.ChatDebugStep{
|
|
ID: uuid.New(),
|
|
RunID: uuid.New(),
|
|
ChatID: uuid.New(),
|
|
StepNumber: 1,
|
|
Operation: "stream",
|
|
Status: "completed",
|
|
NormalizedRequest: json.RawMessage(`null`),
|
|
Attempts: json.RawMessage(`null`),
|
|
Metadata: json.RawMessage(`null`),
|
|
StartedAt: time.Now().UTC(),
|
|
UpdatedAt: time.Now().UTC(),
|
|
}
|
|
|
|
sdk := db2sdk.ChatDebugStep(step)
|
|
require.NotNil(t, sdk.NormalizedRequest, "JSON literal null must produce non-nil map")
|
|
require.Empty(t, sdk.NormalizedRequest, "JSON literal null must produce empty map")
|
|
require.NotNil(t, sdk.Attempts, "JSON literal null must produce non-nil slice")
|
|
require.Empty(t, sdk.Attempts, "JSON literal null must produce empty slice")
|
|
require.NotNil(t, sdk.Metadata, "JSON literal null must produce non-nil map")
|
|
require.Empty(t, sdk.Metadata, "JSON literal null must produce empty map")
|
|
}
|
|
|
|
func TestChatDebugRunDetail(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
startedAt := time.Now().UTC().Round(time.Second)
|
|
finishedAt := startedAt.Add(5 * time.Second)
|
|
rootChatID := uuid.New()
|
|
parentChatID := uuid.New()
|
|
modelConfigID := uuid.New()
|
|
triggerMessageID := int64(7)
|
|
historyTipMessageID := int64(11)
|
|
|
|
run := database.ChatDebugRun{
|
|
ID: uuid.New(),
|
|
ChatID: uuid.New(),
|
|
RootChatID: uuid.NullUUID{UUID: rootChatID, Valid: true},
|
|
ParentChatID: uuid.NullUUID{UUID: parentChatID, Valid: true},
|
|
ModelConfigID: uuid.NullUUID{UUID: modelConfigID, Valid: true},
|
|
TriggerMessageID: sql.NullInt64{Int64: triggerMessageID, Valid: true},
|
|
HistoryTipMessageID: sql.NullInt64{Int64: historyTipMessageID, Valid: true},
|
|
Kind: "chat_turn",
|
|
Status: "completed",
|
|
Provider: sql.NullString{String: "openai", Valid: true},
|
|
Model: sql.NullString{String: "gpt-4o", Valid: true},
|
|
Summary: json.RawMessage(`{"step_count":2}`),
|
|
StartedAt: startedAt,
|
|
UpdatedAt: finishedAt,
|
|
FinishedAt: sql.NullTime{Time: finishedAt, Valid: true},
|
|
}
|
|
steps := []database.ChatDebugStep{
|
|
{
|
|
ID: uuid.New(),
|
|
RunID: run.ID,
|
|
ChatID: run.ChatID,
|
|
StepNumber: 1,
|
|
Operation: "stream",
|
|
Status: "completed",
|
|
NormalizedRequest: json.RawMessage(`{"messages":[]}`),
|
|
Attempts: json.RawMessage(`[]`),
|
|
Metadata: json.RawMessage(`{}`),
|
|
StartedAt: startedAt,
|
|
UpdatedAt: finishedAt,
|
|
},
|
|
{
|
|
ID: uuid.New(),
|
|
RunID: run.ID,
|
|
ChatID: run.ChatID,
|
|
StepNumber: 2,
|
|
Operation: "generate",
|
|
Status: "completed",
|
|
NormalizedRequest: json.RawMessage(`{"messages":[]}`),
|
|
Attempts: json.RawMessage(`[]`),
|
|
Metadata: json.RawMessage(`{}`),
|
|
StartedAt: startedAt,
|
|
UpdatedAt: finishedAt,
|
|
},
|
|
}
|
|
|
|
sdk := db2sdk.ChatDebugRunDetail(run, steps)
|
|
|
|
require.Equal(t, run.ID, sdk.ID)
|
|
require.Equal(t, run.ChatID, sdk.ChatID)
|
|
require.NotNil(t, sdk.RootChatID)
|
|
require.Equal(t, rootChatID, *sdk.RootChatID)
|
|
require.NotNil(t, sdk.ParentChatID)
|
|
require.Equal(t, parentChatID, *sdk.ParentChatID)
|
|
require.NotNil(t, sdk.ModelConfigID)
|
|
require.Equal(t, modelConfigID, *sdk.ModelConfigID)
|
|
require.NotNil(t, sdk.TriggerMessageID)
|
|
require.Equal(t, triggerMessageID, *sdk.TriggerMessageID)
|
|
require.NotNil(t, sdk.HistoryTipMessageID)
|
|
require.Equal(t, historyTipMessageID, *sdk.HistoryTipMessageID)
|
|
require.Equal(t, codersdk.ChatDebugRunKindChatTurn, sdk.Kind)
|
|
require.Equal(t, codersdk.ChatDebugStatusCompleted, sdk.Status)
|
|
require.NotNil(t, sdk.Provider)
|
|
require.Equal(t, "openai", *sdk.Provider)
|
|
require.NotNil(t, sdk.Model)
|
|
require.Equal(t, "gpt-4o", *sdk.Model)
|
|
require.Equal(t, map[string]any{"step_count": float64(2)}, sdk.Summary)
|
|
require.Equal(t, startedAt, sdk.StartedAt)
|
|
require.Equal(t, finishedAt, sdk.UpdatedAt)
|
|
require.NotNil(t, sdk.FinishedAt)
|
|
require.Equal(t, finishedAt, *sdk.FinishedAt)
|
|
require.Len(t, sdk.Steps, 2)
|
|
require.Equal(t, steps[0].ID, sdk.Steps[0].ID)
|
|
require.Equal(t, codersdk.ChatDebugStepOperationStream, sdk.Steps[0].Operation)
|
|
require.Equal(t, steps[1].ID, sdk.Steps[1].ID)
|
|
require.Equal(t, codersdk.ChatDebugStepOperationGenerate, sdk.Steps[1].Operation)
|
|
}
|
|
|
|
func TestChatDebugRunDetail_NullableFieldsNil(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
run := database.ChatDebugRun{
|
|
ID: uuid.New(),
|
|
ChatID: uuid.New(),
|
|
Kind: "chat_turn",
|
|
Status: "in_progress",
|
|
Summary: json.RawMessage(`{}`),
|
|
StartedAt: time.Now().UTC(),
|
|
UpdatedAt: time.Now().UTC(),
|
|
}
|
|
|
|
sdk := db2sdk.ChatDebugRunDetail(run, nil)
|
|
|
|
require.Nil(t, sdk.RootChatID, "NULL RootChatID should map to nil")
|
|
require.Nil(t, sdk.ParentChatID, "NULL ParentChatID should map to nil")
|
|
require.Nil(t, sdk.ModelConfigID, "NULL ModelConfigID should map to nil")
|
|
require.Nil(t, sdk.TriggerMessageID, "NULL TriggerMessageID should map to nil")
|
|
require.Nil(t, sdk.HistoryTipMessageID, "NULL HistoryTipMessageID should map to nil")
|
|
require.Nil(t, sdk.Provider, "NULL Provider should map to nil")
|
|
require.Nil(t, sdk.Model, "NULL Model should map to nil")
|
|
require.Nil(t, sdk.FinishedAt, "NULL FinishedAt should map to nil")
|
|
require.NotNil(t, sdk.Steps, "nil steps slice should serialize as empty array")
|
|
require.Empty(t, sdk.Steps)
|
|
}
|
|
|
|
func TestChatMessage_QueuedMessageID(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("Promoted", func(t *testing.T) {
|
|
t.Parallel()
|
|
result := db2sdk.ChatMessage(database.ChatMessage{
|
|
ID: 2,
|
|
ChatID: uuid.New(),
|
|
Role: database.ChatMessageRoleUser,
|
|
QueuedMessageID: sql.NullInt64{Int64: 7, Valid: true},
|
|
})
|
|
require.NotNil(t, result.QueuedMessageID)
|
|
require.Equal(t, int64(7), *result.QueuedMessageID)
|
|
|
|
raw, err := json.Marshal(result)
|
|
require.NoError(t, err)
|
|
var fields map[string]json.RawMessage
|
|
require.NoError(t, json.Unmarshal(raw, &fields))
|
|
require.JSONEq(t, "7", string(fields["queued_message_id"]))
|
|
})
|
|
|
|
t.Run("NotPromoted", func(t *testing.T) {
|
|
t.Parallel()
|
|
result := db2sdk.ChatMessage(database.ChatMessage{
|
|
ID: 1,
|
|
ChatID: uuid.New(),
|
|
Role: database.ChatMessageRoleUser,
|
|
})
|
|
require.Nil(t, result.QueuedMessageID)
|
|
|
|
raw, err := json.Marshal(result)
|
|
require.NoError(t, err)
|
|
var fields map[string]json.RawMessage
|
|
require.NoError(t, json.Unmarshal(raw, &fields))
|
|
require.NotContains(t, fields, "queued_message_id")
|
|
})
|
|
}
|
|
|
|
func TestChatMessage_PreservesProviderExecutedOnToolResults(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
toolCallID := uuid.New().String()
|
|
toolName := "web_search"
|
|
|
|
// Build assistant content blocks with ProviderExecuted set.
|
|
toolCall := fantasy.ToolCallContent{
|
|
ToolCallID: toolCallID,
|
|
ToolName: toolName,
|
|
Input: `{"query":"test"}`,
|
|
ProviderExecuted: true,
|
|
}
|
|
toolResult := fantasy.ToolResultContent{
|
|
ToolCallID: toolCallID,
|
|
ToolName: toolName,
|
|
Result: fantasy.ToolResultOutputContentText{Text: `{"results":[]}`},
|
|
ProviderExecuted: true,
|
|
}
|
|
|
|
tcJSON, err := json.Marshal(toolCall)
|
|
require.NoError(t, err)
|
|
trJSON, err := json.Marshal(toolResult)
|
|
require.NoError(t, err)
|
|
|
|
rawContent := json.RawMessage("[" + string(tcJSON) + "," + string(trJSON) + "]")
|
|
|
|
dbMsg := database.ChatMessage{
|
|
ID: 1,
|
|
ChatID: uuid.New(),
|
|
Role: database.ChatMessageRoleAssistant,
|
|
Content: pqtype.NullRawMessage{
|
|
RawMessage: rawContent,
|
|
Valid: true,
|
|
},
|
|
CreatedAt: time.Now(),
|
|
}
|
|
|
|
result := db2sdk.ChatMessage(dbMsg)
|
|
|
|
require.Len(t, result.Content, 2)
|
|
|
|
// First part: tool call.
|
|
require.Equal(t, codersdk.ChatMessagePartTypeToolCall, result.Content[0].Type)
|
|
require.Equal(t, toolCallID, result.Content[0].ToolCallID)
|
|
require.Equal(t, toolName, result.Content[0].ToolName)
|
|
require.True(t, result.Content[0].ProviderExecuted, "tool call should preserve ProviderExecuted")
|
|
|
|
// Second part: tool result.
|
|
require.Equal(t, codersdk.ChatMessagePartTypeToolResult, result.Content[1].Type)
|
|
require.Equal(t, toolCallID, result.Content[1].ToolCallID)
|
|
require.Equal(t, toolName, result.Content[1].ToolName)
|
|
require.True(t, result.Content[1].ProviderExecuted, "tool result should preserve ProviderExecuted")
|
|
}
|
|
|
|
func TestChatQueuedMessage_ParsesUserContentParts(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Queued messages are always written via MarshalParts (SDK format).
|
|
rawContent, err := json.Marshal([]codersdk.ChatMessagePart{
|
|
codersdk.ChatMessageText("queued text"),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
queued := db2sdk.ChatQueuedMessage(database.ChatQueuedMessage{
|
|
ID: 1,
|
|
ChatID: uuid.New(),
|
|
Content: rawContent,
|
|
CreatedAt: time.Now(),
|
|
})
|
|
|
|
require.Len(t, queued.Content, 1)
|
|
require.Equal(t, codersdk.ChatMessagePartTypeText, queued.Content[0].Type)
|
|
require.Equal(t, "queued text", queued.Content[0].Text)
|
|
}
|
|
|
|
func TestChat_AllFieldsPopulated(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Every field of database.Chat is set to a non-zero value so
|
|
// that the reflection check below catches any field that
|
|
// db2sdk.Chat forgets to populate. When someone adds a new
|
|
// field to codersdk.Chat, this test will fail until the
|
|
// converter is updated.
|
|
now := dbtime.Now()
|
|
lastErrorPayload := codersdk.ChatError{
|
|
Message: "boom",
|
|
Detail: "provider detail",
|
|
Kind: codersdk.ChatErrorKindGeneric,
|
|
Provider: "openai",
|
|
Retryable: true,
|
|
StatusCode: 503,
|
|
}
|
|
lastErrorRaw, err := json.Marshal(lastErrorPayload)
|
|
require.NoError(t, err)
|
|
|
|
input := database.Chat{
|
|
ID: uuid.New(),
|
|
OwnerID: uuid.New(),
|
|
OwnerUsername: "owner-username",
|
|
OwnerName: "Owner Name",
|
|
OrganizationID: uuid.New(),
|
|
WorkspaceID: uuid.NullUUID{UUID: uuid.New(), Valid: true},
|
|
BuildID: uuid.NullUUID{UUID: uuid.New(), Valid: true},
|
|
AgentID: uuid.NullUUID{UUID: uuid.New(), Valid: true},
|
|
ParentChatID: uuid.NullUUID{UUID: uuid.New(), Valid: true},
|
|
RootChatID: uuid.NullUUID{UUID: uuid.New(), Valid: true},
|
|
LastModelConfigID: uuid.New(),
|
|
LastReasoningEffort: database.NullChatReasoningEffort{ChatReasoningEffort: database.ChatReasoningEffortHigh, Valid: true},
|
|
Title: "all-fields-test",
|
|
TitleSource: database.ChatTitleSourceUser,
|
|
TitleUpdatedAt: now,
|
|
Status: database.ChatStatusRunning,
|
|
ClientType: database.ChatClientTypeUi,
|
|
LastError: pqtype.NullRawMessage{RawMessage: lastErrorRaw, Valid: true},
|
|
LastTurnSummary: sql.NullString{String: "turn completed", Valid: true},
|
|
Summary: sql.NullString{String: "summarized the whole chat", Valid: true},
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
Archived: true,
|
|
UserACL: database.ChatACL{uuid.NewString(): database.ChatACLEntry{}},
|
|
PinOrder: 1,
|
|
PlanMode: database.NullChatPlanMode{ChatPlanMode: database.ChatPlanModePlan, Valid: true},
|
|
MCPServerIDs: []uuid.UUID{uuid.New()},
|
|
Labels: database.StringMap{"env": "prod"},
|
|
DynamicTools: pqtype.NullRawMessage{
|
|
RawMessage: json.RawMessage(`[{"name":"tool1","description":"test tool","inputSchema":{"type":"object"}}]`),
|
|
Valid: true,
|
|
},
|
|
// Pinned-context columns drive codersdk.Chat.Context. Set all of
|
|
// them so the converted sub-struct's fields are non-zero too.
|
|
ProjectID: uuid.NullUUID{UUID: uuid.New(), Valid: true},
|
|
ContextAggregateHash: []byte{0x01, 0x02, 0x03},
|
|
ContextDirtySince: sql.NullTime{Time: now, Valid: true},
|
|
ContextError: "context boom",
|
|
}
|
|
// Only ChatID is needed here. This test checks that
|
|
// Chat.DiffStatus is non-nil, not that every DiffStatus
|
|
// field is populated — that would be a separate test for
|
|
// the ChatDiffStatus converter.
|
|
diffStatus := &database.ChatDiffStatus{
|
|
ChatID: input.ID,
|
|
}
|
|
diffStatuses := []database.ChatDiffStatus{*diffStatus}
|
|
|
|
fileRows := []database.GetChatFileMetadataByChatIDRow{
|
|
{
|
|
ID: uuid.New(),
|
|
OwnerID: input.OwnerID,
|
|
OrganizationID: uuid.New(),
|
|
Name: "test.png",
|
|
Mimetype: "image/png",
|
|
CreatedAt: now,
|
|
},
|
|
}
|
|
|
|
got := db2sdk.ChatWithDiffStatuses(input, diffStatus, diffStatuses, fileRows)
|
|
|
|
require.Equal(t, &lastErrorPayload, got.LastError)
|
|
|
|
v := reflect.ValueOf(got)
|
|
typ := v.Type()
|
|
// These fields are set outside db2sdk.Chat and intentionally remain zero.
|
|
skip := map[string]bool{"HasUnread": true, "Warnings": true, "QueuedForCapacity": true, "InlineMCPServers": true}
|
|
for i := range typ.NumField() {
|
|
field := typ.Field(i)
|
|
if skip[field.Name] {
|
|
continue
|
|
}
|
|
require.False(t, v.Field(i).IsZero(),
|
|
"codersdk.Chat field %q is zero-valued — db2sdk.Chat may not be populating it",
|
|
field.Name,
|
|
)
|
|
}
|
|
}
|
|
|
|
func TestChat_Shared(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := []struct {
|
|
name string
|
|
userACL database.ChatACL
|
|
groupACL database.ChatACL
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "not shared",
|
|
},
|
|
{
|
|
name: "user ACL",
|
|
userACL: database.ChatACL{uuid.NewString(): database.ChatACLEntry{}},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "group ACL",
|
|
groupACL: database.ChatACL{uuid.NewString(): database.ChatACLEntry{}},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "user and group ACLs",
|
|
userACL: database.ChatACL{uuid.NewString(): database.ChatACLEntry{}},
|
|
groupACL: database.ChatACL{uuid.NewString(): database.ChatACLEntry{}},
|
|
expected: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
chat := database.Chat{
|
|
ID: uuid.New(),
|
|
OwnerID: uuid.New(),
|
|
LastModelConfigID: uuid.New(),
|
|
Title: tc.name,
|
|
Status: database.ChatStatusWaiting,
|
|
CreatedAt: dbtime.Now(),
|
|
UpdatedAt: dbtime.Now(),
|
|
UserACL: tc.userACL,
|
|
GroupACL: tc.groupACL,
|
|
}
|
|
|
|
got := db2sdk.Chat(chat, nil, nil)
|
|
require.Equal(t, tc.expected, got.Shared)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestChat_FileMetadataConversion(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ownerID := uuid.New()
|
|
orgID := uuid.New()
|
|
fileID := uuid.New()
|
|
now := dbtime.Now()
|
|
|
|
chat := database.Chat{
|
|
ID: uuid.New(),
|
|
OwnerID: ownerID,
|
|
LastModelConfigID: uuid.New(),
|
|
Title: "file metadata test",
|
|
Status: database.ChatStatusWaiting,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
|
|
rows := []database.GetChatFileMetadataByChatIDRow{
|
|
{
|
|
ID: fileID,
|
|
OwnerID: ownerID,
|
|
OrganizationID: orgID,
|
|
Name: "screenshot.png",
|
|
Mimetype: "image/png",
|
|
SizeBytes: 1234,
|
|
CreatedAt: now,
|
|
},
|
|
}
|
|
|
|
result := db2sdk.Chat(chat, nil, rows)
|
|
|
|
require.Len(t, result.Files, 1)
|
|
f := result.Files[0]
|
|
require.Equal(t, fileID, f.ID)
|
|
require.Equal(t, ownerID, f.OwnerID, "OwnerID must be mapped from DB row")
|
|
require.Equal(t, orgID, f.OrganizationID, "OrganizationID must be mapped from DB row")
|
|
require.Equal(t, "screenshot.png", f.Name)
|
|
require.Equal(t, "image/png", f.MimeType)
|
|
require.Equal(t, int64(1234), f.SizeBytes)
|
|
require.Equal(t, now, f.CreatedAt)
|
|
|
|
// Verify JSON serialization uses snake_case for mime_type.
|
|
data, err := json.Marshal(f)
|
|
require.NoError(t, err)
|
|
require.Contains(t, string(data), `"mime_type"`)
|
|
require.NotContains(t, string(data), `"mimetype"`)
|
|
}
|
|
|
|
func TestChat_NilFilesOmitted(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
chat := database.Chat{
|
|
ID: uuid.New(),
|
|
OwnerID: uuid.New(),
|
|
LastModelConfigID: uuid.New(),
|
|
Title: "no files",
|
|
Status: database.ChatStatusWaiting,
|
|
CreatedAt: dbtime.Now(),
|
|
UpdatedAt: dbtime.Now(),
|
|
}
|
|
|
|
result := db2sdk.Chat(chat, nil, nil)
|
|
require.Empty(t, result.Files)
|
|
}
|
|
|
|
func TestPrimaryChatDiffStatus(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
require.Nil(t, db2sdk.PrimaryChatDiffStatus(nil))
|
|
|
|
older := dbtime.Now()
|
|
newer := older.Add(time.Minute)
|
|
// The primary is not first in the list. Equal report times go to
|
|
// the lowest origin, then the lowest branch.
|
|
statuses := []database.ChatDiffStatus{
|
|
{GitRemoteOrigin: "https://github.com/a/r", GitBranch: "first", UpdatedAt: older},
|
|
{GitRemoteOrigin: "https://github.com/b/r", GitBranch: "main", UpdatedAt: newer},
|
|
{GitRemoteOrigin: "https://github.com/a/r", GitBranch: "z", UpdatedAt: newer},
|
|
{GitRemoteOrigin: "https://github.com/a/r", GitBranch: "y", UpdatedAt: newer},
|
|
}
|
|
|
|
primary := db2sdk.PrimaryChatDiffStatus(statuses)
|
|
require.NotNil(t, primary)
|
|
require.Equal(t, "https://github.com/a/r", primary.GitRemoteOrigin)
|
|
require.Equal(t, "y", primary.GitBranch)
|
|
}
|
|
|
|
func TestChat_LastErrorFallback(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const fallbackMessage = "The chat request failed unexpectedly."
|
|
|
|
tests := []struct {
|
|
name string
|
|
raw json.RawMessage
|
|
expectPayload *codersdk.ChatError
|
|
}{
|
|
{
|
|
name: "MalformedJSON",
|
|
raw: json.RawMessage(`{`),
|
|
expectPayload: &codersdk.ChatError{
|
|
Message: fallbackMessage,
|
|
Kind: codersdk.ChatErrorKindGeneric,
|
|
Retryable: false,
|
|
},
|
|
},
|
|
{
|
|
name: "MessageMissingPreservesMetadata",
|
|
raw: json.RawMessage(`{"kind":"timeout","provider":"openai","status_code":504}`),
|
|
expectPayload: &codersdk.ChatError{
|
|
Message: fallbackMessage,
|
|
Kind: codersdk.ChatErrorKindTimeout,
|
|
Provider: "openai",
|
|
Retryable: false,
|
|
StatusCode: 504,
|
|
},
|
|
},
|
|
{
|
|
name: "WhitespaceMessageDefaultsKind",
|
|
raw: json.RawMessage(`{"message":" ","provider":"openai"}`),
|
|
expectPayload: &codersdk.ChatError{
|
|
Message: fallbackMessage,
|
|
Kind: codersdk.ChatErrorKindGeneric,
|
|
Provider: "openai",
|
|
Retryable: false,
|
|
},
|
|
},
|
|
{
|
|
name: "KindMissingDefaultsGeneric",
|
|
raw: json.RawMessage(`{"message":"OpenAI returned an unexpected error.","provider":"openai","status_code":502}`),
|
|
expectPayload: &codersdk.ChatError{
|
|
Message: "OpenAI returned an unexpected error.",
|
|
Kind: codersdk.ChatErrorKindGeneric,
|
|
Provider: "openai",
|
|
Retryable: false,
|
|
StatusCode: 502,
|
|
},
|
|
},
|
|
{
|
|
name: "UsageLimitKindRoundTrips",
|
|
raw: json.RawMessage(`{"message":"Usage limit reached.","kind":"usage_limit"}`),
|
|
expectPayload: &codersdk.ChatError{
|
|
Message: "Usage limit reached.",
|
|
Kind: codersdk.ChatErrorKindUsageLimit,
|
|
Retryable: false,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
chat := database.Chat{
|
|
ID: uuid.New(),
|
|
OwnerID: uuid.New(),
|
|
LastModelConfigID: uuid.New(),
|
|
Title: "fallback payload",
|
|
Status: database.ChatStatusError,
|
|
CreatedAt: dbtime.Now(),
|
|
UpdatedAt: dbtime.Now(),
|
|
LastError: pqtype.NullRawMessage{
|
|
RawMessage: tc.raw,
|
|
Valid: true,
|
|
},
|
|
}
|
|
|
|
result := db2sdk.Chat(chat, nil, nil)
|
|
require.Equal(t, tc.expectPayload, result.LastError)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestChat_MultipleFiles(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
now := dbtime.Now()
|
|
file1 := uuid.New()
|
|
file2 := uuid.New()
|
|
|
|
chat := database.Chat{
|
|
ID: uuid.New(),
|
|
OwnerID: uuid.New(),
|
|
LastModelConfigID: uuid.New(),
|
|
Title: "multi file test",
|
|
Status: database.ChatStatusWaiting,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
|
|
rows := []database.GetChatFileMetadataByChatIDRow{
|
|
{
|
|
ID: file1,
|
|
OwnerID: chat.OwnerID,
|
|
OrganizationID: uuid.New(),
|
|
Name: "a.png",
|
|
Mimetype: "image/png",
|
|
CreatedAt: now,
|
|
},
|
|
{
|
|
ID: file2,
|
|
OwnerID: chat.OwnerID,
|
|
OrganizationID: uuid.New(),
|
|
Name: "b.txt",
|
|
Mimetype: "text/plain",
|
|
CreatedAt: now,
|
|
},
|
|
}
|
|
|
|
result := db2sdk.Chat(chat, nil, rows)
|
|
require.Len(t, result.Files, 2)
|
|
require.Equal(t, "a.png", result.Files[0].Name)
|
|
require.Equal(t, "b.txt", result.Files[1].Name)
|
|
}
|
|
|
|
func TestChatQueuedMessage_MalformedContent(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
queued := db2sdk.ChatQueuedMessage(database.ChatQueuedMessage{
|
|
ID: 1,
|
|
ChatID: uuid.New(),
|
|
Content: json.RawMessage(`{"unexpected":"shape"}`),
|
|
CreatedAt: time.Now(),
|
|
})
|
|
|
|
require.Empty(t, queued.Content)
|
|
}
|