File size: 3,937 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package engine

import (
	"context"
	"testing"
)

// fakeEngine implements Engine for testing.
type fakeEngine struct{ name string }

func (f *fakeEngine) Name() string                                                  { return f.name }
func (f *fakeEngine) Navigate(_ context.Context, _ string) (*NavigateResult, error) { return nil, nil }
func (f *fakeEngine) Snapshot(_ context.Context, _, _ string) ([]SnapshotNode, error) {
	return nil, nil
}
func (f *fakeEngine) Text(_ context.Context, _ string) (string, error) { return "", nil }
func (f *fakeEngine) Click(_ context.Context, _, _ string) error       { return nil }
func (f *fakeEngine) Type(_ context.Context, _, _, _ string) error     { return nil }
func (f *fakeEngine) Capabilities() []Capability                       { return nil }
func (f *fakeEngine) Close() error                                     { return nil }

func TestRouterChromeMode(t *testing.T) {
	r := NewRouter(ModeChrome, nil)
	for _, op := range []Capability{CapNavigate, CapSnapshot, CapText, CapScreenshot} {
		if r.UseLite(op, "https://example.com") {
			t.Errorf("chrome mode should never use lite for %s", op)
		}
	}
}

func TestRouterLiteMode(t *testing.T) {
	r := NewRouter(ModeLite, &fakeEngine{name: "lite"})

	// DOM operations β†’ lite
	for _, op := range []Capability{CapNavigate, CapSnapshot, CapText, CapClick, CapType} {
		if !r.UseLite(op, "https://example.com") {
			t.Errorf("lite mode should use lite for %s", op)
		}
	}
	// Chrome-only operations β†’ chrome
	for _, op := range []Capability{CapScreenshot, CapPDF, CapEvaluate, CapCookies} {
		if r.UseLite(op, "https://example.com") {
			t.Errorf("lite mode should not use lite for %s", op)
		}
	}
}

func TestRouterAutoModeStaticContent(t *testing.T) {
	r := NewRouter(ModeAuto, &fakeEngine{name: "lite"})

	// Static HTML β†’ lite (ContentHintRule)
	if !r.UseLite(CapNavigate, "https://example.com/page.html") {
		t.Error("auto mode should use lite for .html URL")
	}
	// Dynamic page β†’ chrome (no matching rule)
	if r.UseLite(CapNavigate, "https://example.com/app") {
		t.Error("auto mode should use chrome for dynamic URL")
	}
	// Screenshot β†’ always chrome
	if r.UseLite(CapScreenshot, "https://example.com/page.html") {
		t.Error("auto mode should use chrome for screenshot")
	}
}

func TestRouterAutoModeLiteNil(t *testing.T) {
	r := NewRouter(ModeAuto, nil)
	// Even if rule says lite, nil engine β†’ falls through to chrome
	if r.UseLite(CapNavigate, "https://example.com/page.html") {
		t.Error("should not route to lite when lite engine is nil")
	}
}

func TestRouterAddRemoveRule(t *testing.T) {
	r := NewRouter(ModeAuto, &fakeEngine{name: "lite"})

	// Before: dynamic URL β†’ chrome
	if r.UseLite(CapNavigate, "https://example.com/app") {
		t.Error("should route to chrome before custom rule")
	}

	// Add a custom rule that always routes navigate to lite
	r.AddRule(DefaultLiteRule{})

	// After: still chrome because CapabilityRule + ContentHintRule are first
	// and DefaultChromeRule is last. Our added rule goes before the fallback.
	if !r.UseLite(CapNavigate, "https://example.com/app") {
		t.Error("should route to lite after adding DefaultLiteRule")
	}

	// Remove the custom rule
	if !r.RemoveRule("default-lite") {
		t.Error("RemoveRule should return true")
	}

	if r.UseLite(CapNavigate, "https://example.com/app") {
		t.Error("should revert to chrome after removing rule")
	}
}

func TestRouterRulesSnapshot(t *testing.T) {
	r := NewRouter(ModeAuto, &fakeEngine{name: "lite"})
	rules := r.Rules()
	if len(rules) != 3 {
		t.Fatalf("expected 3 rules in auto mode, got %d: %v", len(rules), rules)
	}
	if rules[0] != "capability" {
		t.Errorf("first rule should be capability, got %s", rules[0])
	}
	if rules[1] != "content-hint" {
		t.Errorf("second rule should be content-hint, got %s", rules[1])
	}
	if rules[2] != "default-chrome" {
		t.Errorf("third rule should be default-chrome, got %s", rules[2])
	}
}