File size: 8,423 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
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
342
343
344
345
346
347
348
349
350
351
352
package engine

import (
	"context"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

func newTestServer(body string) *httptest.Server {
	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/html; charset=utf-8")
		_, _ = w.Write([]byte(body))
	}))
}

const testPage = `<!DOCTYPE html>
<html>
<head><title>Test Page</title></head>
<body>
	<h1>Hello World</h1>
	<nav><a href="/about">About</a></nav>
	<main>
		<p>Some content here.</p>
		<button id="btn">Click Me</button>
		<input type="text" placeholder="Enter name">
		<textarea placeholder="Description"></textarea>
		<form><select><option>One</option></select></form>
	</main>
	<footer>Footer text</footer>
</body>
</html>`

func TestLiteEngine_Navigate(t *testing.T) {
	ts := newTestServer(testPage)
	defer ts.Close()

	lite := NewLiteEngine()
	defer func() { _ = lite.Close() }()

	result, err := lite.Navigate(context.Background(), ts.URL)
	if err != nil {
		t.Fatalf("Navigate: %v", err)
	}
	if result.TabID == "" {
		t.Error("expected non-empty tabId")
	}
	if result.URL != ts.URL {
		t.Errorf("URL = %q, want %q", result.URL, ts.URL)
	}
}

func TestLiteEngine_Snapshot_All(t *testing.T) {
	ts := newTestServer(testPage)
	defer ts.Close()

	lite := NewLiteEngine()
	defer func() { _ = lite.Close() }()

	_, err := lite.Navigate(context.Background(), ts.URL)
	if err != nil {
		t.Fatalf("Navigate: %v", err)
	}

	nodes, err := lite.Snapshot(context.Background(), "", "all")
	if err != nil {
		t.Fatalf("Snapshot: %v", err)
	}
	if len(nodes) == 0 {
		t.Fatal("expected nodes in snapshot")
	}

	// Verify roles are assigned
	roleSet := make(map[string]bool)
	for _, n := range nodes {
		roleSet[n.Role] = true
	}
	for _, expected := range []string{"heading", "link", "button", "textbox", "combobox"} {
		if !roleSet[expected] {
			t.Errorf("expected role %q in snapshot, roles found: %v", expected, roleSet)
		}
	}
}

func TestLiteEngine_Snapshot_Interactive(t *testing.T) {
	ts := newTestServer(testPage)
	defer ts.Close()

	lite := NewLiteEngine()
	defer func() { _ = lite.Close() }()

	_, _ = lite.Navigate(context.Background(), ts.URL)

	nodes, err := lite.Snapshot(context.Background(), "", "interactive")
	if err != nil {
		t.Fatalf("Snapshot interactive: %v", err)
	}

	for _, n := range nodes {
		if !n.Interactive {
			t.Errorf("interactive filter returned non-interactive node: %+v", n)
		}
	}

	if len(nodes) < 3 {
		t.Errorf("expected at least 3 interactive nodes (link, button, input), got %d", len(nodes))
	}
}

func TestLiteEngine_Text(t *testing.T) {
	ts := newTestServer(testPage)
	defer ts.Close()

	lite := NewLiteEngine()
	defer func() { _ = lite.Close() }()

	_, _ = lite.Navigate(context.Background(), ts.URL)

	text, err := lite.Text(context.Background(), "")
	if err != nil {
		t.Fatalf("Text: %v", err)
	}

	for _, want := range []string{"Hello World", "About", "Click Me", "Some content here"} {
		if !strings.Contains(text, want) {
			t.Errorf("text should contain %q, got: %s", want, text)
		}
	}
}

func TestLiteEngine_Click(t *testing.T) {
	ts := newTestServer(testPage)
	defer ts.Close()

	lite := NewLiteEngine()
	defer func() { _ = lite.Close() }()

	_, _ = lite.Navigate(context.Background(), ts.URL)

	nodes, _ := lite.Snapshot(context.Background(), "", "interactive")
	var buttonRef string
	for _, n := range nodes {
		if n.Role == "button" {
			buttonRef = n.Ref
			break
		}
	}
	if buttonRef == "" {
		t.Fatal("no button found in snapshot")
	}

	if err := lite.Click(context.Background(), "", buttonRef); err != nil {
		t.Errorf("Click: %v", err)
	}
}

func TestLiteEngine_Type(t *testing.T) {
	ts := newTestServer(testPage)
	defer ts.Close()

	lite := NewLiteEngine()
	defer func() { _ = lite.Close() }()

	_, _ = lite.Navigate(context.Background(), ts.URL)

	nodes, _ := lite.Snapshot(context.Background(), "", "interactive")
	var inputRef string
	for _, n := range nodes {
		if n.Role == "textbox" {
			inputRef = n.Ref
			break
		}
	}
	if inputRef == "" {
		t.Fatal("no textbox found in snapshot")
	}

	if err := lite.Type(context.Background(), "", inputRef, "hello"); err != nil {
		t.Errorf("Type: %v", err)
	}
}

func TestLiteEngine_RefNotFound(t *testing.T) {
	lite := NewLiteEngine()
	defer func() { _ = lite.Close() }()

	// No page loaded
	_, err := lite.Snapshot(context.Background(), "", "all")
	if err == nil {
		t.Error("expected error for snapshot without navigate")
	}

	// After navigate, bad ref
	ts := newTestServer(testPage)
	defer ts.Close()

	_, _ = lite.Navigate(context.Background(), ts.URL)
	_, _ = lite.Snapshot(context.Background(), "", "all")

	if err := lite.Click(context.Background(), "", "nonexistent"); err == nil {
		t.Error("expected error for bad ref")
	}
}

func TestLiteEngine_ScriptStyleSkipped(t *testing.T) {
	page := `<!DOCTYPE html>
<html><head><title>T</title></head>
<body>
<script>var x = 1;</script>
<style>.red { color: red; }</style>
<p>Visible</p>
</body></html>`

	ts := newTestServer(page)
	defer ts.Close()

	lite := NewLiteEngine()
	defer func() { _ = lite.Close() }()

	_, _ = lite.Navigate(context.Background(), ts.URL)
	nodes, _ := lite.Snapshot(context.Background(), "", "all")

	for _, n := range nodes {
		if n.Tag == "script" || n.Tag == "style" {
			t.Errorf("snapshot should skip %s elements", n.Tag)
		}
	}
}

func TestLiteEngine_AriaAttributes(t *testing.T) {
	page := `<!DOCTYPE html>
<html><head><title>Aria</title></head>
<body>
<div role="navigation" aria-label="Main Nav">content</div>
<span role="button" tabindex="0">Custom Btn</span>
</body></html>`

	ts := newTestServer(page)
	defer ts.Close()

	lite := NewLiteEngine()
	defer func() { _ = lite.Close() }()

	_, _ = lite.Navigate(context.Background(), ts.URL)
	nodes, _ := lite.Snapshot(context.Background(), "", "all")

	foundNav := false
	foundBtn := false
	for _, n := range nodes {
		if n.Role == "navigation" && n.Name == "Main Nav" {
			foundNav = true
		}
		if n.Role == "button" && n.Name == "Custom Btn" {
			foundBtn = true
		}
	}
	if !foundNav {
		t.Error("expected to find navigation role with aria-label")
	}
	if !foundBtn {
		t.Error("expected to find button role from role attribute")
	}
}

func TestLiteEngine_MultiTab(t *testing.T) {
	page1 := `<!DOCTYPE html><html><head><title>Page 1</title></head><body><p>First</p></body></html>`
	page2 := `<!DOCTYPE html><html><head><title>Page 2</title></head><body><p>Second</p></body></html>`

	ts1 := newTestServer(page1)
	defer ts1.Close()
	ts2 := newTestServer(page2)
	defer ts2.Close()

	lite := NewLiteEngine()
	defer func() { _ = lite.Close() }()

	res1, _ := lite.Navigate(context.Background(), ts1.URL)
	res2, _ := lite.Navigate(context.Background(), ts2.URL)

	if res1.TabID == res2.TabID {
		t.Error("tab IDs should be different")
	}

	// Current tab is the most recent (page2)
	text, _ := lite.Text(context.Background(), "")
	if !strings.Contains(text, "Second") {
		t.Errorf("expected page 2 text, got: %s", text)
	}

	text, _ = lite.Text(context.Background(), res1.TabID)
	if !strings.Contains(text, "First") {
		t.Errorf("expected page 1 text via tab ID, got: %s", text)
	}
}

func TestLiteEngine_Close(t *testing.T) {
	ts := newTestServer(testPage)
	defer ts.Close()

	lite := NewLiteEngine()
	_, _ = lite.Navigate(context.Background(), ts.URL)

	if err := lite.Close(); err != nil {
		t.Errorf("Close: %v", err)
	}

	// After close, operations should fail
	_, err := lite.Snapshot(context.Background(), "", "all")
	if err == nil {
		t.Error("expected error after close")
	}
}

func TestLiteEngine_Capabilities(t *testing.T) {
	lite := NewLiteEngine()
	defer func() { _ = lite.Close() }()

	caps := lite.Capabilities()
	if len(caps) != 5 {
		t.Errorf("expected 5 capabilities, got %d", len(caps))
	}
}

func TestLiteEngine_Name(t *testing.T) {
	lite := NewLiteEngine()
	defer func() { _ = lite.Close() }()

	if lite.Name() != "lite" {
		t.Errorf("Name() = %q, want %q", lite.Name(), "lite")
	}
}

func TestNormalizeWhitespace(t *testing.T) {
	tests := []struct {
		in   string
		want string
	}{
		{"hello world", "hello world"},
		{"  hello   world  ", "hello world"},
		{"line1\n\n\nline2", "line1 line2"},
		{"\t  tabs \t and  \t spaces", "tabs and spaces"},
		{"", ""},
	}
	for _, tt := range tests {
		got := normalizeWhitespace(tt.in)
		if got != tt.want {
			t.Errorf("normalizeWhitespace(%q) = %q, want %q", tt.in, got, tt.want)
		}
	}
}