-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathcdp_options_test.go
More file actions
341 lines (319 loc) · 7.23 KB
/
Copy pathcdp_options_test.go
File metadata and controls
341 lines (319 loc) · 7.23 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package runn
import (
"context"
"os"
"testing"
"github.com/chromedp/chromedp"
"github.com/k1LoW/donegroup"
"github.com/k1LoW/runn/testutil"
)
func TestCDPRunnerWithOptions(t *testing.T) {
if testutil.SkipCDPTest(t) {
t.Skip("chrome not found")
}
tests := []struct {
name string
flags map[string]any
wantErr bool
}{
{
name: "with headless flag",
flags: map[string]any{
"headless": true,
},
wantErr: false,
},
{
name: "with multiple flags",
flags: map[string]any{
"headless": true,
"disable-gpu": true,
"no-sandbox": true,
},
wantErr: false,
},
{
name: "with window size",
flags: map[string]any{
"window-size": "1280,720",
},
wantErr: false,
},
{
name: "with user agent",
flags: map[string]any{
"user-agent": "Custom User Agent for Testing",
},
wantErr: false,
},
{
name: "with disable web security",
flags: map[string]any{
"disable-web-security": true,
},
wantErr: false,
},
{
name: "empty flags",
flags: map[string]any{},
wantErr: false,
},
{
name: "nil flags",
flags: nil,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := donegroup.WithCancel(context.Background())
t.Cleanup(cancel)
r, err := newCDPRunnerWithOptions("test", cdpNewKey, tt.flags)
if (err != nil) != tt.wantErr {
t.Errorf("newCDPRunnerWithOptions() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil {
return
}
t.Cleanup(func() {
if err := r.Close(); err != nil {
t.Error(err)
}
})
// Verify that flags were applied to opts
if tt.flags != nil {
// Base: DefaultExecAllocatorOptions + WSURLReadTimeout
expectedOptCount := len(chromedp.DefaultExecAllocatorOptions) + 1
// WindowSize is added only when flags don't contain a valid "window-size" string
hasValidWindowSize := false
if v, ok := tt.flags["window-size"]; ok {
if s, ok := v.(string); ok && s != "" {
hasValidWindowSize = true
}
}
if !hasValidWindowSize {
expectedOptCount++ // +1 for default WindowSize
}
// Count user flags that will be applied
for key, value := range tt.flags {
if key == "window-size" {
if hasValidWindowSize {
expectedOptCount++ // valid window-size is applied via dedicated path
}
continue
}
switch value.(type) {
case bool, string, int, float64:
expectedOptCount++
}
}
if len(r.opts) != expectedOptCount {
t.Errorf("opts not properly initialized, got %d options, expected %d", len(r.opts), expectedOptCount)
}
}
// Test that the runner can be used
o, err := New()
if err != nil {
t.Fatal(err)
}
s := newStep(0, "stepKey", o, nil)
t.Cleanup(func() {
o.store.ClearSteps()
})
// Simple test action to verify the runner works
actions := CDPActions{
{
Fn: "navigate",
Args: map[string]any{
"url": "about:blank",
},
},
}
if err := r.run(ctx, actions, s); err != nil {
t.Errorf("failed to run actions with flags: %v", err)
}
})
}
}
func TestParseCDPRunnerWithDetailed(t *testing.T) {
tests := []struct {
name string
yamlConfig string
wantRunner bool
wantErr bool
}{
{
name: "with addr and flags",
yamlConfig: `
addr: chrome://new
flags:
headless: true
disable-gpu: true
`,
wantRunner: true,
wantErr: false,
},
{
name: "with only flags",
yamlConfig: `
flags:
headless: false
no-sandbox: true
`,
wantRunner: true,
wantErr: false,
},
{
name: "with only addr",
yamlConfig: `
addr: chrome://new
`,
wantRunner: true,
wantErr: false,
},
{
name: "empty config",
yamlConfig: `
`,
wantRunner: false,
wantErr: false,
},
{
name: "invalid yaml",
yamlConfig: `
addr: chrome://new
flags:
- invalid
`,
wantRunner: 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))
if (err != nil) != tt.wantErr {
t.Errorf("parseCDPRunnerWithDetailed() error = %v, wantErr %v", err, tt.wantErr)
return
}
if detected != tt.wantRunner {
t.Errorf("parseCDPRunnerWithDetailed() detected = %v, want %v", detected, tt.wantRunner)
}
if tt.wantRunner {
if _, ok := bk.cdpRunners["test"]; !ok {
t.Error("runner not added to book")
}
}
})
}
}
func TestCDPOptionParsing(t *testing.T) {
tests := []struct {
name string
flags map[string]any
wantCount int // number of user flags expected to be applied
}{
{
name: "boolean flags",
flags: map[string]any{
"headless": true,
"disable-gpu": false,
},
wantCount: 2,
},
{
name: "string flags",
flags: map[string]any{
"user-agent": "Test Agent",
"window-size": "800,600",
},
wantCount: 2, // both applied (window-size via dedicated path)
},
{
name: "integer flags",
flags: map[string]any{
"remote-debugging-port": 9222,
},
wantCount: 1,
},
{
name: "float flags (converted to int)",
flags: map[string]any{
"remote-debugging-port": 9223.0,
},
wantCount: 1,
},
{
name: "mixed types",
flags: map[string]any{
"headless": true,
"user-agent": "Test",
"remote-debugging-port": 9222,
},
wantCount: 3,
},
{
name: "unsupported types are skipped",
flags: map[string]any{
"valid-flag": true,
"array-flag": []string{"ignored"},
"map-flag": map[string]string{"ignored": "value"},
},
wantCount: 1, // only valid-flag is counted
},
{
name: "invalid window-size type is ignored",
flags: map[string]any{
"window-size": 12345,
},
wantCount: 0, // invalid type: skipped in flags loop and no custom WindowSize applied
},
{
name: "empty window-size is ignored",
flags: map[string]any{
"window-size": "",
},
wantCount: 0, // empty string: skipped in flags loop and no custom WindowSize applied
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r, err := newCDPRunnerWithOptions("test", cdpNewKey, tt.flags)
if err != nil {
t.Fatalf("newCDPRunnerWithOptions() error = %v", err)
}
t.Cleanup(func() {
if err := r.Close(); err != nil {
t.Error(err)
}
})
// Base: DefaultExecAllocatorOptions + WSURLReadTimeout
baseCount := len(chromedp.DefaultExecAllocatorOptions) + 1
// WindowSize is added when no valid custom window-size is provided
hasValidWindowSize := false
if v, ok := tt.flags["window-size"]; ok {
if s, ok := v.(string); ok && s != "" {
hasValidWindowSize = true
}
}
if !hasValidWindowSize {
baseCount++ // +1 for default WindowSize
}
// Account for environment variable options
if os.Getenv("RUNN_DISABLE_HEADLESS") != "" {
baseCount += 3
}
if os.Getenv("RUNN_DISABLE_CHROME_SANDBOX") != "" {
baseCount += 1
}
expectedTotal := baseCount + tt.wantCount
if len(r.opts) != expectedTotal {
t.Errorf("expected exactly %d options (base %d + user flags %d), got %d", expectedTotal, baseCount, tt.wantCount, len(r.opts))
}
})
}
}