mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
e00e85765b
This PR merges code from `coder/aibridge` repository into `coder/coder`. It was split into 4 PRs for easier review but stacked PRs will need to be merged into this PR so all checks pass. * https://github.com/coder/coder/pull/24190 -> raw code copy (this PR, before merging PRs on top of it, it was just 1 commit: https://github.com/coder/coder/commit/70d33f33200c7e77df910957595715f81f9bec24) * https://github.com/coder/coder/pull/24570 -> update imports in `coder/coder` to use copied code * https://github.com/coder/coder/pull/24586 -> linter fixes and CI integration (also added README.md) * https://github.com/coder/coder/pull/24571 -> added exclude to scripts/check_emdash.sh check Original PR message (before PR squash): Moves coder/aibridge code into coder/coder repository. Omitted files: - `go.mod`, `go.sum`, `.gitignore`, `.github/workflows/ci.yml,` `Makefile`, `LICENSE`, `README.md` (modified README.md is added later) - `.github`, `example`, `buildinfo,` `scripts` directories Simple verification script (will list omitted files) ``` tmp=$(mktemp -d) echo "$tmp" git clone --depth=1 https://github.com/coder/aibridge "$tmp/aibridge" git clone --depth=1 --branch pb/aibridge-code-move https://github.com/coder/coder "$tmp/coder" diff -rq --exclude=.git "$tmp/aibridge" "$tmp/coder/aibridge" # rm -rf "$tmp" ```
90 lines
2.2 KiB
Go
90 lines
2.2 KiB
Go
package context_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
aibcontext "github.com/coder/coder/v2/aibridge/context"
|
|
"github.com/coder/coder/v2/aibridge/recorder"
|
|
)
|
|
|
|
func TestAsActor(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Given: a metadata map
|
|
metadata := recorder.Metadata{"key": "value"}
|
|
|
|
// When: storing an actor in the context
|
|
ctx := aibcontext.AsActor(context.Background(), "actor-123", metadata)
|
|
|
|
// Then: the actor should be retrievable with correct ID and metadata
|
|
actor := aibcontext.ActorFromContext(ctx)
|
|
require.NotNil(t, actor)
|
|
assert.Equal(t, "actor-123", actor.ID)
|
|
assert.Equal(t, "value", actor.Metadata["key"])
|
|
}
|
|
|
|
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
|
|
ctx := aibcontext.AsActor(context.Background(), "test-id", recorder.Metadata{})
|
|
|
|
// 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, "test-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 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
|
|
ctx := aibcontext.AsActor(context.Background(), "test-actor-id", recorder.Metadata{})
|
|
|
|
// When: extracting the actor ID from context
|
|
got := aibcontext.ActorIDFromContext(ctx)
|
|
|
|
// Then: the actor ID should be returned
|
|
assert.Equal(t, "test-actor-id", got)
|
|
})
|
|
|
|
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)
|
|
})
|
|
}
|