1package webkit
2
3import (
4 "errors"
5 "testing"
6 "time"
7
8 "github.com/stretchr/testify/assert"
9)
10
11func TestClassifyRunJSEvaluateError(t *testing.T) {
12 t.Run("non-fatal canceled", func(t *testing.T) {
13 nonFatal, signature := classifyRunJSEvaluateError(errors.New("Operation was canceled"))
14 assert.True(t, nonFatal)
15 assert.Equal(t, "evaluate_error:operation was canceled", signature)
16 })
17
18 t.Run("non-fatal context destroyed", func(t *testing.T) {
19 nonFatal, signature := classifyRunJSEvaluateError(errors.New("JavaScript execution context was destroyed"))
20 assert.True(t, nonFatal)
21 assert.Equal(t, "evaluate_error:javascript execution context was destroyed", signature)
22 })
23
24 t.Run("fatal unknown", func(t *testing.T) {
25 nonFatal, signature := classifyRunJSEvaluateError(errors.New("segmentation fault in JS runtime"))
26 assert.False(t, nonFatal)
27 assert.Equal(t, "evaluate_error:segmentation fault in js runtime", signature)
28 })
29}
30
31func TestNormalizeRunJSErrorSignature(t *testing.T) {
32 assert.Equal(t, "empty", normalizeRunJSErrorSignature(" \n\t "))
33 assert.Equal(t, "foo bar baz", normalizeRunJSErrorSignature(" Foo BAR\tbaz "))
34}
35
36func TestWebViewRunJSDomain(t *testing.T) {
37 wv := &WebView{uri: "https://Sub.Example.com/path?q=1"}
38 assert.Equal(t, "sub.example.com", wv.runJSDomain())
39
40 wv = &WebView{uri: "not a uri"}
41 assert.Equal(t, "unknown", wv.runJSDomain())
42}
43
44func TestShouldLogRunJSError_NonFatalAggregation(t *testing.T) {
45 wv := &WebView{}
46 now := time.Now()
47
48 shouldLog, count := wv.shouldLogRunJSError("example.com", "sig", true, now)
49 assert.True(t, shouldLog)
50 assert.Equal(t, uint64(1), count)
51
52 shouldLog, count = wv.shouldLogRunJSError("example.com", "sig", true, now.Add(10*time.Second))
53 assert.False(t, shouldLog)
54 assert.Equal(t, uint64(2), count)
55
56 shouldLog, count = wv.shouldLogRunJSError("example.com", "sig", true, now.Add(31*time.Second))
57 assert.True(t, shouldLog)
58 assert.Equal(t, uint64(3), count)
59}
60
61func TestShouldLogRunJSError_NonFatalLogsEveryN(t *testing.T) {
62 wv := &WebView{}
63 base := time.Now()
64 for i := 1; i < runJSAggregateLogEvery; i++ {
65 shouldLog, _ := wv.shouldLogRunJSError("example.com", "sig", true, base)
66 if i == 1 {
67 assert.True(t, shouldLog)
68 continue
69 }
70 assert.False(t, shouldLog)
71 }
72
73 shouldLog, count := wv.shouldLogRunJSError("example.com", "sig", true, base)
74 assert.True(t, shouldLog)
75 assert.Equal(t, uint64(runJSAggregateLogEvery), count)
76}
77
78func TestShouldLogRunJSError_FatalAlwaysLogs(t *testing.T) {
79 wv := &WebView{}
80 base := time.Now()
81
82 shouldLog, count := wv.shouldLogRunJSError("example.com", "fatal-sig", false, base)
83 assert.True(t, shouldLog)
84 assert.Equal(t, uint64(1), count)
85
86 shouldLog, count = wv.shouldLogRunJSError("example.com", "fatal-sig", false, base)
87 assert.True(t, shouldLog)
88 assert.Equal(t, uint64(2), count)
89}