forked from k1LoW/runn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdp_timeout_test.go
More file actions
225 lines (209 loc) · 4.54 KB
/
cdp_timeout_test.go
File metadata and controls
225 lines (209 loc) · 4.54 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package runn
import (
"testing"
"time"
)
func TestParseCDPRunnerWithTimeout(t *testing.T) {
tests := []struct {
name string
yamlConfig string
wantTimeout time.Duration
wantDetected bool
wantErr bool
}{
{
name: "valid timeout configuration",
yamlConfig: `
addr: chrome://new
timeout: 120sec
flags:
headless: true
`,
wantTimeout: 120 * time.Second,
wantDetected: true,
wantErr: false,
},
{
name: "valid timeout configuration cdp",
yamlConfig: `
addr: cdp://new
timeout: 120sec
flags:
headless: true
`,
wantTimeout: 120 * time.Second,
wantDetected: true,
wantErr: false,
},
{
name: "timeout with minutes",
yamlConfig: `
addr: chrome://new
timeout: 2m
`,
wantTimeout: 2 * time.Minute,
wantDetected: true,
wantErr: false,
},
{
name: "timeout with complex duration",
yamlConfig: `
addr: chrome://new
timeout: 1m30s
`,
wantTimeout: 90 * time.Second,
wantDetected: true,
wantErr: false,
},
{
name: "no timeout specified - uses default",
yamlConfig: `
addr: chrome://new
flags:
headless: true
`,
wantTimeout: cdpTimeoutByStep, // default value
wantDetected: true,
wantErr: false,
},
{
name: "invalid timeout format",
yamlConfig: `
addr: chrome://new
timeout: invalid
`,
wantDetected: false,
wantErr: true,
},
{
name: "empty config - not detected as CDP runner",
yamlConfig: ``,
wantDetected: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bk := &book{
cdpRunners: map[string]*cdpRunner{},
}
detected, err := bk.parseCDPRunnerWithDetailed("test", []byte(tt.yamlConfig))
// Check error condition
if (err != nil) != tt.wantErr {
t.Errorf("%v: parseCDPRunnerWithDetailed() error = %v, wantErr %v", tt.name, err, tt.wantErr)
return
}
// Check detection
if detected != tt.wantDetected {
t.Errorf("%v: parseCDPRunnerWithDetailed() detected = %v, want %v", tt.name, detected, tt.wantDetected)
return
}
// If runner was created and no error, check timeout value
if tt.wantDetected && !tt.wantErr {
runner, ok := bk.cdpRunners["test"]
if !ok {
t.Error("CDP runner not found in book")
return
}
if runner.timeoutByStep != tt.wantTimeout {
t.Errorf("%v: timeout = %v, want %v", tt.name, runner.timeoutByStep, tt.wantTimeout)
}
}
})
}
}
func TestCDPTimeoutOption(t *testing.T) {
tests := []struct {
name string
timeout string
}{
{
name: "valid timeout string",
timeout: "30s",
},
{
name: "timeout in minutes",
timeout: "5m",
},
{
name: "empty timeout - use default",
timeout: "",
},
{
name: "invalid timeout format",
timeout: "invalid",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opt := CDPTimeout(tt.timeout)
config := &cdpRunnerConfig{
Flags: map[string]any{},
Remote: cdpNewKey,
}
err := opt(config)
if err != nil {
t.Errorf("CDPTimeout option returned error: %v", err)
return
}
if config.Timeout != tt.timeout {
t.Errorf("config.Timeout = %s, want %s", config.Timeout, tt.timeout)
}
})
}
}
func TestApplyCDPTimeout(t *testing.T) {
tests := []struct {
name string
timeout string
wantTimeout time.Duration
wantErr bool
}{
{
name: "valid timeout",
timeout: "90sec",
wantTimeout: 90 * time.Second,
wantErr: false,
},
{
name: "valid timeout min",
timeout: "3min",
wantTimeout: 3 * time.Minute,
wantErr: false,
},
{
name: "valid timeout mix",
timeout: "3min30sec",
wantTimeout: 3*time.Minute + 30*time.Second,
wantErr: false,
},
{
name: "empty timeout - no change",
timeout: "",
wantTimeout: cdpTimeoutByStep, // default remains unchanged
wantErr: false,
},
{
name: "invalid timeout format",
timeout: "not-a-duration",
wantTimeout: cdpTimeoutByStep,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a mock CDP runner
runner := &cdpRunner{
timeoutByStep: cdpTimeoutByStep, // start with default
}
err := applyCDPTimeout(runner, tt.timeout)
if (err != nil) != tt.wantErr {
t.Errorf("applyCDPTimeout() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && runner.timeoutByStep != tt.wantTimeout {
t.Errorf("timeoutByStep = %v, want %v", runner.timeoutByStep, tt.wantTimeout)
}
})
}
}