|
| 1 | +package context |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/coder/aibridge/recorder" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAsActor(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + // Given: a metadata map |
| 17 | + metadata := recorder.Metadata{"key": "value"} |
| 18 | + |
| 19 | + // When: storing an actor in the context |
| 20 | + ctx := AsActor(context.Background(), "actor-123", metadata) |
| 21 | + |
| 22 | + // Then: the actor should be retrievable with correct ID and metadata |
| 23 | + actor := ActorFromContext(ctx) |
| 24 | + require.NotNil(t, actor) |
| 25 | + assert.Equal(t, "actor-123", actor.ID) |
| 26 | + assert.Equal(t, "value", actor.Metadata["key"]) |
| 27 | +} |
| 28 | + |
| 29 | +func TestActorFromContext(t *testing.T) { |
| 30 | + t.Parallel() |
| 31 | + |
| 32 | + t.Run("returns actor when present", func(t *testing.T) { |
| 33 | + t.Parallel() |
| 34 | + |
| 35 | + // Given: a context with an actor |
| 36 | + ctx := AsActor(context.Background(), "test-id", recorder.Metadata{}) |
| 37 | + |
| 38 | + // When: extracting the actor from context |
| 39 | + actor := ActorFromContext(ctx) |
| 40 | + |
| 41 | + // Then: the actor should be returned with correct ID |
| 42 | + require.NotNil(t, actor) |
| 43 | + assert.Equal(t, "test-id", actor.ID) |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("returns nil when no actor", func(t *testing.T) { |
| 47 | + t.Parallel() |
| 48 | + |
| 49 | + // Given: a context without an actor |
| 50 | + ctx := context.Background() |
| 51 | + |
| 52 | + // When: extracting the actor from context |
| 53 | + actor := ActorFromContext(ctx) |
| 54 | + |
| 55 | + // Then: nil should be returned |
| 56 | + assert.Nil(t, actor) |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +func TestActorIDFromContext(t *testing.T) { |
| 61 | + t.Parallel() |
| 62 | + |
| 63 | + t.Run("returns actor ID when present", func(t *testing.T) { |
| 64 | + t.Parallel() |
| 65 | + |
| 66 | + // Given: a context with an actor |
| 67 | + ctx := AsActor(context.Background(), "test-actor-id", recorder.Metadata{}) |
| 68 | + |
| 69 | + // When: extracting the actor ID from context |
| 70 | + got := ActorIDFromContext(ctx) |
| 71 | + |
| 72 | + // Then: the actor ID should be returned |
| 73 | + assert.Equal(t, "test-actor-id", got) |
| 74 | + }) |
| 75 | + |
| 76 | + t.Run("returns empty string when no actor", func(t *testing.T) { |
| 77 | + t.Parallel() |
| 78 | + |
| 79 | + // Given: a context without an actor |
| 80 | + ctx := context.Background() |
| 81 | + |
| 82 | + // When: extracting the actor ID from context |
| 83 | + got := ActorIDFromContext(ctx) |
| 84 | + |
| 85 | + // Then: an empty string should be returned |
| 86 | + assert.Empty(t, got) |
| 87 | + }) |
| 88 | +} |
0 commit comments