Repository navigation
Expand file tree
/
Copy pathclient_lifecycle_e2e_test.go
More file actions
148 lines (125 loc) · 4.21 KB
/
Copy pathclient_lifecycle_e2e_test.go
File metadata and controls
148 lines (125 loc) · 4.21 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package e2e
import (
"sync/atomic"
"testing"
"time"
copilot "github.com/github/copilot-sdk/go"
"github.com/github/copilot-sdk/go/internal/e2e/testharness"
)
// Mirrors dotnet/test/ClientLifecycleTests.cs.
func TestClientLifecycleE2E(t *testing.T) {
ctx := testharness.NewTestContext(t)
t.Run("should receive session created lifecycle event", func(t *testing.T) {
client := ctx.NewClient()
t.Cleanup(func() { client.ForceStop() })
created := make(chan copilot.SessionLifecycleEvent, 4)
unsubscribe := client.On(func(event copilot.SessionLifecycleEvent) {
if event.Type == copilot.SessionLifecycleCreated {
select {
case created <- event:
default:
}
}
})
defer unsubscribe()
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
select {
case evt := <-created:
if evt.Type != copilot.SessionLifecycleCreated {
t.Errorf("Expected event type %q, got %q", copilot.SessionLifecycleCreated, evt.Type)
}
if evt.SessionID != session.SessionID {
t.Errorf("Expected session id %q, got %q", session.SessionID, evt.SessionID)
}
case <-time.After(10 * time.Second):
t.Fatal("Timed out waiting for session.created lifecycle event")
}
})
t.Run("should filter session lifecycle events by type", func(t *testing.T) {
client := ctx.NewClient()
t.Cleanup(func() { client.ForceStop() })
created := make(chan copilot.SessionLifecycleEvent, 4)
unsubscribe := client.OnEventType(copilot.SessionLifecycleCreated, func(event copilot.SessionLifecycleEvent) {
select {
case created <- event:
default:
}
})
defer unsubscribe()
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
select {
case evt := <-created:
if evt.Type != copilot.SessionLifecycleCreated {
t.Errorf("Expected event type %q, got %q", copilot.SessionLifecycleCreated, evt.Type)
}
if evt.SessionID != session.SessionID {
t.Errorf("Expected session id %q, got %q", session.SessionID, evt.SessionID)
}
case <-time.After(10 * time.Second):
t.Fatal("Timed out waiting for filtered session.created lifecycle event")
}
})
t.Run("disposing lifecycle subscription stops receiving events", func(t *testing.T) {
client := ctx.NewClient()
t.Cleanup(func() { client.ForceStop() })
var disposedCount int64
unsubscribeFirst := client.On(func(event copilot.SessionLifecycleEvent) {
atomic.AddInt64(&disposedCount, 1)
})
// Dispose before any session is created — should never be invoked.
unsubscribeFirst()
created := make(chan copilot.SessionLifecycleEvent, 4)
unsubscribeActive := client.OnEventType(copilot.SessionLifecycleCreated, func(event copilot.SessionLifecycleEvent) {
select {
case created <- event:
default:
}
})
defer unsubscribeActive()
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
select {
case evt := <-created:
if evt.SessionID != session.SessionID {
t.Errorf("Expected session id %q, got %q", session.SessionID, evt.SessionID)
}
case <-time.After(10 * time.Second):
t.Fatal("Timed out waiting for active subscription to receive event")
}
if got := atomic.LoadInt64(&disposedCount); got != 0 {
t.Errorf("Expected disposed subscription to receive 0 events, got %d", got)
}
})
t.Run("stop disconnects client", func(t *testing.T) {
client := ctx.NewClient()
t.Cleanup(func() { client.ForceStop() })
if err := client.Start(t.Context()); err != nil {
t.Fatalf("Failed to start client: %v", err)
}
if err := client.Stop(); err != nil {
t.Fatalf("Failed to stop client: %v", err)
}
})
t.Run("force stop disconnects client", func(t *testing.T) {
client := ctx.NewClient()
t.Cleanup(func() { client.ForceStop() })
if err := client.Start(t.Context()); err != nil {
t.Fatalf("Failed to start client: %v", err)
}
client.ForceStop()
})
}