-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcontext_test.go
More file actions
88 lines (63 loc) · 2.03 KB
/
context_test.go
File metadata and controls
88 lines (63 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package context
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/coder/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 := AsActor(context.Background(), "actor-123", metadata)
// Then: the actor should be retrievable with correct ID and metadata
actor := 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 := AsActor(context.Background(), "test-id", recorder.Metadata{})
// When: extracting the actor from context
actor := 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 := 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 := AsActor(context.Background(), "test-actor-id", recorder.Metadata{})
// When: extracting the actor ID from context
got := 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 := ActorIDFromContext(ctx)
// Then: an empty string should be returned
assert.Empty(t, got)
})
}