Files
coder/aibridge/context/context_test.go
T
Paweł Banaszewski ffc7dadfe2 refactor: use typed AI Gateway actor identity (#30258)
Adds typed AI Gateway actor identity, including the API key ID used by
recording.

Recorders now read the authenticated API key ID from each call context
instead of capturing request identity at construction time. This allows
one recorder to be reused safely across sequential and concurrent
requests. Includes context helper coverage and recorder regression
tests.

Generated by Coder Agents on behalf of @pawbana.
2026-10-05 14:00:15 +00:00

147 lines
3.4 KiB
Go

package context_test
import (
"context"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
aibcontext "github.com/coder/coder/v2/aibridge/context"
)
func TestAsActor(t *testing.T) {
t.Parallel()
want := aibcontext.Actor{
ID: uuid.New(),
APIKeyID: "test-key-id",
Username: "actor",
Email: "actor@example.com",
}
ctx := aibcontext.AsActor(t.Context(), want)
actor := aibcontext.ActorFromContext(ctx)
require.NotNil(t, actor)
assert.Equal(t, want, *actor)
want.Username = "changed"
assert.Equal(t, "actor", actor.Username, "the context owns a copy of the actor")
assert.Same(t, actor, aibcontext.ActorFromContext(context.WithoutCancel(ctx)))
}
func TestActorFromContext(t *testing.T) {
t.Parallel()
t.Run("returns actor when present", func(t *testing.T) {
t.Parallel()
// Given: a context with an actor
id := uuid.New()
ctx := aibcontext.AsActor(t.Context(), aibcontext.Actor{ID: id})
// When: extracting the actor from context
actor := aibcontext.ActorFromContext(ctx)
// Then: the actor should be returned with correct ID
require.NotNil(t, actor)
assert.Equal(t, id, actor.ID)
})
t.Run("returns nil when no actor", func(t *testing.T) {
t.Parallel()
// Given: a context without an actor
ctx := context.Background()
// When: extracting the actor from context
actor := aibcontext.ActorFromContext(ctx)
// Then: nil should be returned
assert.Nil(t, actor)
})
}
func TestAPIKeyIDFromContext(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
actor *aibcontext.Actor
detached bool
want string
wantErr error
}{
{
name: "Present",
actor: &aibcontext.Actor{APIKeyID: "test-key-id"},
want: "test-key-id",
},
{
name: "MissingActor",
wantErr: aibcontext.ErrMissingAPIKeyID,
},
{
name: "EmptyAPIKeyID",
actor: &aibcontext.Actor{},
wantErr: aibcontext.ErrMissingAPIKeyID,
},
{
name: "DetachedContext",
actor: &aibcontext.Actor{APIKeyID: "test-key-id"},
detached: true,
want: "test-key-id",
},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := t.Context()
if tc.actor != nil {
ctx = aibcontext.AsActor(ctx, *tc.actor)
}
if tc.detached {
ctx = context.WithoutCancel(ctx)
}
got, err := aibcontext.APIKeyIDFromContext(ctx)
require.ErrorIs(t, err, tc.wantErr)
assert.Equal(t, tc.want, got)
})
}
}
func TestActorIDFromContext(t *testing.T) {
t.Parallel()
t.Run("returns actor ID when present", func(t *testing.T) {
t.Parallel()
// Given: a context with an actor
id := uuid.New()
ctx := aibcontext.AsActor(t.Context(), aibcontext.Actor{ID: id})
// When: extracting the actor ID from context
got := aibcontext.ActorIDFromContext(ctx)
// Then: the actor ID should be returned
assert.Equal(t, id.String(), got)
})
t.Run("returns empty string for nil ID", func(t *testing.T) {
t.Parallel()
ctx := aibcontext.AsActor(t.Context(), aibcontext.Actor{})
assert.Empty(t, aibcontext.ActorIDFromContext(ctx))
})
t.Run("returns empty string when no actor", func(t *testing.T) {
t.Parallel()
// Given: a context without an actor
ctx := context.Background()
// When: extracting the actor ID from context
got := aibcontext.ActorIDFromContext(ctx)
// Then: an empty string should be returned
assert.Empty(t, got)
})
}