-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
97 lines (86 loc) · 2.31 KB
/
utils_test.go
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
package getstream_test
import (
"context"
"fmt"
"math/rand"
"testing"
"time"
. "github.com/GetStream/getstream-go"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
func WaitForTask(ctx context.Context, client *Stream, taskID string) (*StreamResponse[GetTaskResponse], error) {
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
taskResult, err := client.GetTask(ctx, taskID, &GetTaskRequest{})
if err != nil {
return nil, fmt.Errorf("failed to get task result: %w", err)
}
if taskResult.Data.Status == "completed" || taskResult.Data.Status == "failed" {
return taskResult, nil
}
case <-ctx.Done():
return nil, ctx.Err()
}
}
}
// ResourceManager manages resource cleanup for tests.
type ResourceManager struct {
t *testing.T
}
// NewResourceManager initializes a new ResourceManager.
func NewResourceManager(t *testing.T) *ResourceManager {
return &ResourceManager{t: t}
}
// RegisterCleanup registers a cleanup function to be called when the test finishes.
func (rm *ResourceManager) RegisterCleanup(cleanup func()) {
rm.t.Cleanup(cleanup)
}
func randomString(n int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
bytes := make([]byte, n)
for i := 0; i < n; i++ {
bytes[i] = byte(65 + r.Intn(26)) // should be 26 to include 'Z'
}
return string(bytes)
}
func newCall(t *testing.T, client *Stream) *Call {
t.Helper()
ctx := context.Background()
callID := uuid.New().String()
call := client.Video().Call("default", callID)
callRequest := GetOrCreateCallRequest{
Data: &CallRequest{
CreatedByID: PtrTo("tommaso-id"),
},
}
_, err := call.GetOrCreate(ctx, &callRequest)
require.NoError(t, err, "Error creating call")
return call
}
func getUser(t *testing.T, client *Stream, name *string, image *string, custom map[string]any) (*FullUserResponse, error) {
t.Helper()
ctx := context.Background()
userID := uuid.New().String()
users := []UserRequest{
{
ID: userID,
Name: name,
Image: image,
Custom: custom,
},
}
usersMap := make(map[string]UserRequest)
for _, user := range users {
usersMap[user.ID] = user
}
res, err := client.UpdateUsers(ctx, &UpdateUsersRequest{Users: usersMap})
if err != nil {
return nil, err
}
user := res.Data.Users[userID]
return &user, nil
}