File size: 2,628 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package human

import (
	"context"
	"math/rand"
	"testing"

	"github.com/chromedp/chromedp"
)

func TestSetHumanRandSeed(t *testing.T) {
	// Just verify it doesn't panic
	SetHumanRandSeed(12345)
}

func TestType(t *testing.T) {
	text := "hello"

	// Test normal typing
	actions := Type(text, false)
	if len(actions) < len(text) {
		t.Errorf("expected at least %d actions, got %d", len(text), len(actions))
	}

	// Test fast typing
	fastActions := Type(text, true)
	if len(fastActions) < len(text) {
		t.Errorf("expected at least %d actions, got %d", len(text), len(fastActions))
	}
}

func TestTypeWithCorrections(t *testing.T) {
	// Use a fixed seed that we know triggers a correction (statistically likely with long string)
	SetHumanRandSeed(1)
	text := "this is a very long string to increase the chance of a simulated typo correction"
	actions := Type(text, false)

	// If a typo happened, there will be more actions than just KeyEvents and Sleeps for each char
	if len(actions) < len(text)*2 {
		t.Errorf("expected many actions for long string, got %d", len(actions))
	}
}

func TestMouseMove(t *testing.T) {
	// Create a context that chromedp.Run won't immediately reject
	ctx, _ := chromedp.NewContext(context.Background())

	// MouseMove will try to call chromedp.Run.
	// Without a real browser it will return an error, but we cover the code path.
	_ = MouseMove(ctx, 0, 0, 100, 100)
}

func TestClick(t *testing.T) {
	ctx, _ := chromedp.NewContext(context.Background())
	_ = Click(ctx, 50, 50)
}

func TestTypeWithConfig(t *testing.T) {
	// Test with fixed seed for reproducibility
	cfg := &Config{
		Rand: rand.New(rand.NewSource(12345)),
	}

	// Generate actions twice with same config - should be identical
	actions1 := TypeWithConfig("hello", false, cfg)

	// Reset the rand source to same seed
	cfg.Rand = rand.New(rand.NewSource(12345))
	actions2 := TypeWithConfig("hello", false, cfg)

	if len(actions1) != len(actions2) {
		t.Errorf("expected same number of actions with same seed, got %d and %d", len(actions1), len(actions2))
	}

	// Verify at least some actions were generated
	if len(actions1) < 10 {
		t.Errorf("expected at least 10 actions, got %d", len(actions1))
	}
}

func TestClickElement_RequiresMinContentLength(t *testing.T) {
	// ClickElement accesses box.Content[0], [1], [2], and [5]
	// CDP BoxModel Content has 8 float64 values (4 x/y pairs)
	// The guard must check len(box.Content) < 8
	// Without a browser, GetBoxModel will fail
	ctx, _ := chromedp.NewContext(context.Background())
	err := ClickElement(ctx, 0)
	if err == nil {
		t.Error("expected error without browser connection")
	}
}