-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathintegration.zig
More file actions
749 lines (606 loc) · 28.5 KB
/
integration.zig
File metadata and controls
749 lines (606 loc) · 28.5 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
const std = @import("std");
const net = std.net;
// Import all modules under test
const config_mod = @import("../bridge/config.zig");
const Bridge = @import("../bridge/bridge.zig").Bridge;
const TabEntry = @import("../bridge/bridge.zig").TabEntry;
const RefCache = @import("../bridge/bridge.zig").RefCache;
const SnapshotRefCache = @import("../snapshot/ref_cache.zig").SnapshotRefCache;
const diff = @import("../snapshot/diff.zig");
const A11yNode = @import("../snapshot/a11y.zig").A11yNode;
const a11y = @import("../snapshot/a11y.zig");
const markdown = @import("../crawler/markdown.zig");
const validator = @import("../crawler/validator.zig");
const json_util = @import("../util/json.zig");
const harness_mod = @import("harness.zig");
const launcher_mod = @import("../chrome/launcher.zig");
const router_mod = @import("../server/router.zig");
const FakeChromeServer = struct {
port: u16,
thread: std.Thread,
fn start(body: []const u8) !FakeChromeServer {
var port: u16 = 19440;
while (port < 19540) : (port += 1) {
const address = try net.Address.parseIp4("127.0.0.1", port);
const server = address.listen(.{ .reuse_address = true }) catch |err| switch (err) {
error.AddressInUse => continue,
else => return err,
};
const thread = try std.Thread.spawn(.{}, serveOnce, .{ server, body });
return .{ .port = port, .thread = thread };
}
return error.NoFreePort;
}
fn stop(self: *FakeChromeServer) void {
self.thread.join();
}
fn serveOnce(server: net.Server, body: []const u8) !void {
var tcp_server = server;
defer tcp_server.deinit();
const conn = try tcp_server.accept();
defer conn.stream.close();
var req_buf: [2048]u8 = undefined;
_ = conn.stream.read(&req_buf) catch 0;
const response = try std.fmt.allocPrint(
std.heap.page_allocator,
"HTTP/1.1 200 OK\r\nContent-Length:{d}\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n{s}",
.{ body.len, body },
);
defer std.heap.page_allocator.free(response);
try conn.stream.writeAll(response);
}
};
// ─── Config Tests ───────────────────────────────────────────────────────
test "config defaults are sensible" {
const cfg = config_mod.load();
try std.testing.expectEqualStrings("127.0.0.1", cfg.host);
try std.testing.expectEqual(@as(u16, 8080), cfg.port);
try std.testing.expectEqual(@as(?[]const u8, null), cfg.cdp_url);
try std.testing.expectEqual(@as(?[]const u8, null), cfg.auth_secret);
try std.testing.expectEqualStrings(".kuri", cfg.state_dir);
try std.testing.expectEqual(@as(u32, 30), cfg.stale_tab_interval_s);
try std.testing.expectEqual(@as(u32, 30_000), cfg.request_timeout_ms);
try std.testing.expectEqual(@as(u32, 30_000), cfg.navigate_timeout_ms);
}
test "discoverTabs hydrates bridge from Chrome target list" {
const body =
\\[
\\ {
\\ "id":"page-1",
\\ "type":"page",
\\ "url":"https://example.com",
\\ "title":"Example",
\\ "webSocketDebuggerUrl":"ws://127.0.0.1:9222/devtools/page/page-1"
\\ },
\\ {
\\ "id":"worker-1",
\\ "type":"service_worker",
\\ "url":"https://example.com/sw.js",
\\ "title":"Worker",
\\ "webSocketDebuggerUrl":"ws://127.0.0.1:9222/devtools/page/worker-1"
\\ }
\\]
;
var fake = try FakeChromeServer.start(body);
defer fake.stop();
const cdp_url = try std.fmt.allocPrint(std.testing.allocator, "http://127.0.0.1:{d}/json/version", .{fake.port});
defer std.testing.allocator.free(cdp_url);
var bridge = Bridge.init(std.testing.allocator);
defer bridge.deinit();
const cfg = config_mod.Config{
.host = "127.0.0.1",
.port = 8080,
.cdp_url = cdp_url,
.auth_secret = null,
.state_dir = ".kuri",
.stale_tab_interval_s = 30,
.request_timeout_ms = 30_000,
.navigate_timeout_ms = 30_000,
.extensions = null,
.headless = true,
};
var arena_impl = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_impl.deinit();
const discovered = try router_mod.discoverTabs(arena_impl.allocator(), &bridge, cfg, fake.port);
try std.testing.expectEqual(@as(usize, 1), discovered);
try std.testing.expectEqual(@as(usize, 1), bridge.tabCount());
const tab = bridge.getTab("page-1").?;
try std.testing.expectEqualStrings("https://example.com", tab.url);
try std.testing.expectEqualStrings("Example", tab.title);
try std.testing.expectEqualStrings("ws://127.0.0.1:9222/devtools/page/page-1", tab.ws_url);
}
// ─── Bridge Stress Tests ────────────────────────────────────────────────
test "bridge handles many tabs" {
var bridge = Bridge.init(std.testing.allocator);
defer bridge.deinit();
// Add 100 tabs
for (0..100) |i| {
const id = try std.fmt.allocPrint(std.testing.allocator, "tab-{d}", .{i});
defer std.testing.allocator.free(id);
const url = try std.fmt.allocPrint(std.testing.allocator, "https://example.com/{d}", .{i});
defer std.testing.allocator.free(url);
try bridge.putTab(.{
.id = id,
.url = url,
.title = "Test",
.ws_url = "",
.created_at = @as(i64, @intCast(i)),
.last_accessed = @as(i64, @intCast(i)),
});
}
try std.testing.expectEqual(@as(usize, 100), bridge.tabCount());
// Remove all tabs
for (0..100) |i| {
const id = try std.fmt.allocPrint(std.testing.allocator, "tab-{d}", .{i});
defer std.testing.allocator.free(id);
bridge.removeTab(id);
}
try std.testing.expectEqual(@as(usize, 0), bridge.tabCount());
}
test "bridge tab overwrite replaces old entry" {
var bridge = Bridge.init(std.testing.allocator);
defer bridge.deinit();
try bridge.putTab(.{
.id = "tab-x",
.url = "https://old.com",
.title = "Old",
.ws_url = "",
.created_at = 1,
.last_accessed = 1,
});
try bridge.putTab(.{
.id = "tab-x",
.url = "https://new.com",
.title = "New",
.ws_url = "",
.created_at = 2,
.last_accessed = 2,
});
try std.testing.expectEqual(@as(usize, 1), bridge.tabCount());
const tab = bridge.getTab("tab-x").?;
try std.testing.expectEqualStrings("https://new.com", tab.url);
}
test "bridge removeTab non-existent is safe" {
var bridge = Bridge.init(std.testing.allocator);
defer bridge.deinit();
bridge.removeTab("does-not-exist");
try std.testing.expectEqual(@as(usize, 0), bridge.tabCount());
}
test "bridge listTabs returns all entries" {
var bridge = Bridge.init(std.testing.allocator);
defer bridge.deinit();
try bridge.putTab(.{ .id = "a", .url = "u1", .title = "t1", .ws_url = "", .created_at = 0, .last_accessed = 0 });
try bridge.putTab(.{ .id = "b", .url = "u2", .title = "t2", .ws_url = "", .created_at = 0, .last_accessed = 0 });
const tabs = try bridge.listTabs(std.testing.allocator);
defer std.testing.allocator.free(tabs);
try std.testing.expectEqual(@as(usize, 2), tabs.len);
}
// ─── Snapshot Diff Tests ────────────────────────────────────────────────
test "diff empty snapshots" {
const empty: []const A11yNode = &.{};
const result = try diff.diffSnapshots(empty, empty, std.testing.allocator);
defer std.testing.allocator.free(result);
try std.testing.expectEqual(@as(usize, 0), result.len);
}
test "diff all new nodes" {
const empty: []const A11yNode = &.{};
const current = [_]A11yNode{
.{ .ref = "e0", .role = "button", .name = "A", .value = "", .backend_node_id = 1, .depth = 0 },
.{ .ref = "e1", .role = "link", .name = "B", .value = "", .backend_node_id = 2, .depth = 0 },
};
const result = try diff.diffSnapshots(empty, ¤t, std.testing.allocator);
defer std.testing.allocator.free(result);
try std.testing.expectEqual(@as(usize, 2), result.len);
try std.testing.expectEqual(diff.DiffKind.added, result[0].kind);
try std.testing.expectEqual(diff.DiffKind.added, result[1].kind);
}
test "diff all removed nodes" {
const prev = [_]A11yNode{
.{ .ref = "e0", .role = "button", .name = "A", .value = "", .backend_node_id = 1, .depth = 0 },
};
const empty: []const A11yNode = &.{};
const result = try diff.diffSnapshots(&prev, empty, std.testing.allocator);
defer std.testing.allocator.free(result);
try std.testing.expectEqual(@as(usize, 1), result.len);
try std.testing.expectEqual(diff.DiffKind.removed, result[0].kind);
}
test "diff value change detected" {
const prev = [_]A11yNode{
.{ .ref = "e0", .role = "textbox", .name = "Email", .value = "", .backend_node_id = 10, .depth = 0 },
};
const current = [_]A11yNode{
.{ .ref = "e0", .role = "textbox", .name = "Email", .value = "user@test.com", .backend_node_id = 10, .depth = 0 },
};
const result = try diff.diffSnapshots(&prev, ¤t, std.testing.allocator);
defer std.testing.allocator.free(result);
try std.testing.expectEqual(@as(usize, 1), result.len);
try std.testing.expectEqual(diff.DiffKind.changed, result[0].kind);
}
test "diff role change detected" {
const prev = [_]A11yNode{
.{ .ref = "e0", .role = "button", .name = "X", .value = "", .backend_node_id = 5, .depth = 0 },
};
const current = [_]A11yNode{
.{ .ref = "e0", .role = "link", .name = "X", .value = "", .backend_node_id = 5, .depth = 0 },
};
const result = try diff.diffSnapshots(&prev, ¤t, std.testing.allocator);
defer std.testing.allocator.free(result);
try std.testing.expectEqual(@as(usize, 1), result.len);
try std.testing.expectEqual(diff.DiffKind.changed, result[0].kind);
}
test "diff unchanged nodes not reported" {
const nodes = [_]A11yNode{
.{ .ref = "e0", .role = "button", .name = "OK", .value = "", .backend_node_id = 1, .depth = 0 },
};
const result = try diff.diffSnapshots(&nodes, &nodes, std.testing.allocator);
defer std.testing.allocator.free(result);
try std.testing.expectEqual(@as(usize, 0), result.len);
}
test "ref cache sequential put/get" {
var cache = SnapshotRefCache.init(std.testing.allocator);
defer cache.deinit();
// Use static ref names to avoid dangling pointer issue (StringHashMap borrows keys)
const refs = [_][]const u8{ "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9" };
for (refs, 0..) |ref, i| {
try cache.put(ref, @as(u32, @intCast(i + 100)));
}
try std.testing.expectEqual(@as(usize, 10), cache.count());
try std.testing.expectEqual(@as(?u32, 100), cache.get("e0"));
try std.testing.expectEqual(@as(?u32, 109), cache.get("e9"));
try std.testing.expectEqual(@as(?u32, null), cache.get("e10"));
}
test "ref cache clear invalidates all refs" {
var cache = SnapshotRefCache.init(std.testing.allocator);
defer cache.deinit();
try cache.put("e0", 10);
try cache.put("e1", 20);
try std.testing.expectEqual(@as(usize, 2), cache.count());
cache.clear();
try std.testing.expectEqual(@as(usize, 0), cache.count());
try std.testing.expectEqual(@as(?u32, null), cache.get("e0"));
}
test "ref cache overwrite updates node id" {
var cache = SnapshotRefCache.init(std.testing.allocator);
defer cache.deinit();
try cache.put("e0", 10);
try cache.put("e0", 99);
try std.testing.expectEqual(@as(?u32, 99), cache.get("e0"));
try std.testing.expectEqual(@as(usize, 1), cache.count());
}
// ─── Markdown Converter Tests ───────────────────────────────────────────
test "markdown nested tags" {
const html = "<p><strong><em>bold italic</em></strong></p>";
const md = try markdown.htmlToMarkdown(html, std.testing.allocator);
defer std.testing.allocator.free(md);
try std.testing.expect(std.mem.indexOf(u8, md, "***bold italic***") != null or
std.mem.indexOf(u8, md, "**") != null);
}
test "markdown list items" {
const html = "<ul><li>Alpha</li><li>Beta</li><li>Gamma</li></ul>";
const md = try markdown.htmlToMarkdown(html, std.testing.allocator);
defer std.testing.allocator.free(md);
try std.testing.expect(std.mem.indexOf(u8, md, "- Alpha") != null);
try std.testing.expect(std.mem.indexOf(u8, md, "- Beta") != null);
try std.testing.expect(std.mem.indexOf(u8, md, "- Gamma") != null);
}
test "markdown blockquote" {
const html = "<blockquote>Wise words</blockquote>";
const md = try markdown.htmlToMarkdown(html, std.testing.allocator);
defer std.testing.allocator.free(md);
try std.testing.expect(std.mem.indexOf(u8, md, "> Wise words") != null);
}
test "markdown pre code block" {
const html = "<pre>fn main() {}</pre>";
const md = try markdown.htmlToMarkdown(html, std.testing.allocator);
defer std.testing.allocator.free(md);
try std.testing.expect(std.mem.indexOf(u8, md, "```") != null);
try std.testing.expect(std.mem.indexOf(u8, md, "fn main() {}") != null);
}
test "markdown hr tag" {
const html = "above<hr>below";
const md = try markdown.htmlToMarkdown(html, std.testing.allocator);
defer std.testing.allocator.free(md);
try std.testing.expect(std.mem.indexOf(u8, md, "---") != null);
}
test "markdown style tag stripped" {
const html = "before<style>.x{color:red}</style>after";
const md = try markdown.htmlToMarkdown(html, std.testing.allocator);
defer std.testing.allocator.free(md);
try std.testing.expectEqualStrings("beforeafter", md);
}
test "markdown nbsp entity" {
const html = "hello world";
const md = try markdown.htmlToMarkdown(html, std.testing.allocator);
defer std.testing.allocator.free(md);
try std.testing.expectEqualStrings("hello world", md);
}
test "markdown empty input" {
const md = try markdown.htmlToMarkdown("", std.testing.allocator);
defer std.testing.allocator.free(md);
try std.testing.expectEqualStrings("", md);
}
// ─── URL Validator Tests ────────────────────────────────────────────────
test "validator accepts HTTPS with port" {
try validator.validateUrl("https://example.com:443/path");
}
test "validator accepts HTTP with query" {
try validator.validateUrl("http://example.com?key=val&foo=bar");
}
test "validator rejects data URI" {
try std.testing.expectError(
validator.ValidationError.InvalidScheme,
validator.validateUrl("data:text/html,<h1>hi</h1>"),
);
}
test "validator rejects all private ranges" {
// 10.x.x.x
try std.testing.expectError(validator.ValidationError.PrivateIp, validator.validateUrl("http://10.255.255.255"));
// 172.16-31.x.x
try std.testing.expectError(validator.ValidationError.PrivateIp, validator.validateUrl("http://172.31.255.255"));
// 192.168.x.x
try std.testing.expectError(validator.ValidationError.PrivateIp, validator.validateUrl("http://192.168.0.1"));
// 172.15 is NOT private
try validator.validateUrl("http://172.15.0.1");
// 172.32 is NOT private
try validator.validateUrl("http://172.32.0.1");
}
test "validator blocks IPv6 loopback" {
try std.testing.expectError(
validator.ValidationError.LocalhostBlocked,
validator.validateUrl("http://[::1]/path"),
);
}
test "validator handles URL with auth" {
try validator.validateUrl("https://user:pass@example.com/path");
}
// ─── JSON Utility Tests ────────────────────────────────────────────────
test "jsonEscape control characters" {
const result = try json_util.jsonEscape("tab\there\nnewline\rcarriage", std.testing.allocator);
defer std.testing.allocator.free(result);
try std.testing.expect(std.mem.indexOf(u8, result, "\\t") != null);
try std.testing.expect(std.mem.indexOf(u8, result, "\\n") != null);
try std.testing.expect(std.mem.indexOf(u8, result, "\\r") != null);
}
test "jsonEscape low control char" {
// ASCII 0x01 should become \u0001
const input = &[_]u8{0x01};
const result = try json_util.jsonEscape(input, std.testing.allocator);
defer std.testing.allocator.free(result);
try std.testing.expect(std.mem.indexOf(u8, result, "\\u") != null);
}
test "jsonEscape empty string" {
const result = try json_util.jsonEscape("", std.testing.allocator);
defer std.testing.allocator.free(result);
try std.testing.expectEqualStrings("", result);
}
test "jsonEscape no escaping needed" {
const result = try json_util.jsonEscape("hello world 123", std.testing.allocator);
defer std.testing.allocator.free(result);
try std.testing.expectEqualStrings("hello world 123", result);
}
test "writeJsonObject single field" {
var buf: [256]u8 = undefined;
var stream = std.io.fixedBufferStream(&buf);
const writer = stream.writer();
const fields = [_][2][]const u8{
.{ "key", "value" },
};
try json_util.writeJsonObject(writer, &fields);
try std.testing.expectEqualStrings("{\"key\":\"value\"}", stream.getWritten());
}
test "writeJsonObject multiple fields" {
var buf: [256]u8 = undefined;
var stream = std.io.fixedBufferStream(&buf);
const writer = stream.writer();
const fields = [_][2][]const u8{
.{ "a", "1" },
.{ "b", "2" },
};
try json_util.writeJsonObject(writer, &fields);
try std.testing.expectEqualStrings("{\"a\":\"1\",\"b\":\"2\"}", stream.getWritten());
}
test "writeJsonObject empty" {
var buf: [256]u8 = undefined;
var stream = std.io.fixedBufferStream(&buf);
const writer = stream.writer();
const fields: [0][2][]const u8 = .{};
try json_util.writeJsonObject(writer, &fields);
try std.testing.expectEqualStrings("{}", stream.getWritten());
}
// ─── Harness Self-Tests ─────────────────────────────────────────────────
test "harness init default port" {
var h = harness_mod.TestHarness.init(std.testing.allocator);
_ = &h;
try std.testing.expectEqual(@as(u16, 8080), h.browdie_port);
}
test "harness countOccurrences overlapping" {
// "aaa" in "aaaa" = 2 (non-overlapping)
try std.testing.expectEqual(@as(usize, 2), harness_mod.TestHarness.countOccurrences("aaaa", "aa"));
}
test "harness snapshotElementCount with no refs" {
try std.testing.expectEqual(@as(usize, 0), harness_mod.TestHarness.snapshotElementCount("{}"));
}
// ─── Chrome Launcher Tests ──────────────────────────────────────────────
test "launcher healthCheck on unbound port returns not alive" {
var chrome = launcher_mod.Launcher.init(std.testing.allocator, config_mod.load());
defer chrome.deinit();
// Force a high port that won't be in use
chrome.cdp_port = 19876;
const status = chrome.healthCheck();
try std.testing.expect(!status.alive);
try std.testing.expectEqual(@as(?[]const u8, null), status.ws_url);
}
test "launcher findFreePort returns valid port" {
const port = try launcher_mod.findFreePort(9222);
try std.testing.expect(port >= 9222);
try std.testing.expect(port <= 9322);
}
test "isPortInUse returns false for unbound port" {
try std.testing.expect(!launcher_mod.isPortInUse(19999));
}
// ─── A11y Snapshot Tests ────────────────────────────────────────────────
test "isInteractive roles" {
try std.testing.expect(a11y.isInteractive("button"));
try std.testing.expect(a11y.isInteractive("link"));
try std.testing.expect(a11y.isInteractive("textbox"));
try std.testing.expect(!a11y.isInteractive("generic"));
try std.testing.expect(!a11y.isInteractive(""));
}
// ─── Lightpanda Parity Protocol Tests ──────────────────────────────────
const protocol = @import("../cdp/protocol.zig");
test "lightpanda parity: network domain methods defined" {
try std.testing.expectEqualStrings("Network.getCookies", protocol.Methods.network_get_cookies);
try std.testing.expectEqualStrings("Network.setCookies", protocol.Methods.network_set_cookies);
try std.testing.expectEqualStrings("Network.deleteCookies", protocol.Methods.network_delete_cookies);
try std.testing.expectEqualStrings("Network.setExtraHTTPHeaders", protocol.Methods.network_set_extra_http_headers);
}
test "lightpanda parity: page domain methods defined" {
try std.testing.expectEqualStrings("Page.printToPDF", protocol.Methods.page_print_to_pdf);
try std.testing.expectEqualStrings("Page.stopLoading", protocol.Methods.page_stop_loading);
try std.testing.expectEqualStrings("Page.addScriptToEvaluateOnNewDocument", protocol.Methods.page_add_script);
}
test "lightpanda parity: DOM domain methods defined" {
try std.testing.expectEqualStrings("DOM.querySelector", protocol.Methods.dom_query_selector);
try std.testing.expectEqualStrings("DOM.querySelectorAll", protocol.Methods.dom_query_selector_all);
try std.testing.expectEqualStrings("DOM.getOuterHTML", protocol.Methods.dom_get_outer_html);
try std.testing.expectEqualStrings("DOM.getDocument", protocol.Methods.dom_get_document);
try std.testing.expectEqualStrings("DOM.resolveNode", protocol.Methods.dom_resolve_node);
}
test "lightpanda parity: all new methods are unique strings" {
const methods = [_][]const u8{
protocol.Methods.network_get_cookies,
protocol.Methods.network_set_cookies,
protocol.Methods.network_delete_cookies,
protocol.Methods.network_set_extra_http_headers,
protocol.Methods.page_print_to_pdf,
protocol.Methods.page_stop_loading,
protocol.Methods.dom_query_selector,
protocol.Methods.dom_query_selector_all,
protocol.Methods.dom_get_outer_html,
};
// Verify no duplicates
for (methods, 0..) |m1, i| {
for (methods[i + 1 ..]) |m2| {
try std.testing.expect(!std.mem.eql(u8, m1, m2));
}
}
}
// ─── Tier 1 Handler Logic Tests ──────────────────────────────────────────
test "action kinds support new tier 1 types" {
const actions = @import("../cdp/actions.zig");
// Verify all new action kinds resolve correctly
try std.testing.expect(actions.ActionKind.fromString("dblclick") != null);
try std.testing.expect(actions.ActionKind.fromString("check") != null);
try std.testing.expect(actions.ActionKind.fromString("uncheck") != null);
try std.testing.expect(actions.ActionKind.fromString("blur") != null);
// Verify original ones still work
try std.testing.expect(actions.ActionKind.fromString("click") != null);
try std.testing.expect(actions.ActionKind.fromString("fill") != null);
try std.testing.expect(actions.ActionKind.fromString("hover") != null);
// Unknown still returns null
try std.testing.expect(actions.ActionKind.fromString("swipe") == null);
}
test "ref cache supports tier 1 handler lookups" {
// Simulates what scrollintoview, highlight, drag handlers do:
// 1. Put refs in cache from snapshot
// 2. Look them up by ref name to get backend node IDs
var cache = SnapshotRefCache.init(std.testing.allocator);
defer cache.deinit();
// Simulate snapshot populating refs
try cache.put("e0", 100);
try cache.put("e1", 101);
try cache.put("e2", 102);
try cache.put("e3", 103);
// scrollintoview: lookup single ref
try std.testing.expectEqual(@as(?u32, 100), cache.get("e0"));
// drag: lookup src and tgt refs
try std.testing.expectEqual(@as(?u32, 101), cache.get("e1"));
try std.testing.expectEqual(@as(?u32, 103), cache.get("e3"));
// highlight: lookup ref
try std.testing.expectEqual(@as(?u32, 102), cache.get("e2"));
// Unknown ref returns null (handler should return 400)
try std.testing.expect(cache.get("e99") == null);
}
test "bridge tab operations for tab/new and tab/close" {
var bridge = Bridge.init(std.testing.allocator);
defer bridge.deinit();
// Initially no tabs
try std.testing.expectEqual(@as(usize, 0), bridge.tabCount());
// Simulate adding tabs (what tab/new does after CDP call)
try bridge.putTab(.{
.id = "tab-1",
.ws_url = "ws://127.0.0.1:9222/devtools/page/tab-1",
.url = "about:blank",
.title = "New Tab",
.created_at = 1000,
.last_accessed = 1000,
});
try std.testing.expectEqual(@as(usize, 1), bridge.tabCount());
try bridge.putTab(.{
.id = "tab-2",
.ws_url = "ws://127.0.0.1:9222/devtools/page/tab-2",
.url = "https://example.com",
.title = "Example",
.created_at = 1001,
.last_accessed = 1001,
});
try std.testing.expectEqual(@as(usize, 2), bridge.tabCount());
// tab/close: remove specific tab
bridge.removeTab("tab-1");
try std.testing.expectEqual(@as(usize, 1), bridge.tabCount());
// Verify correct tab was removed
try std.testing.expect(bridge.getCdpClient("tab-1") == null);
}
test "CDP protocol methods for tier 1 endpoints exist" {
// Input domain
try std.testing.expectEqualStrings("Input.dispatchKeyEvent", protocol.Methods.input_dispatch_key_event);
try std.testing.expectEqualStrings("Input.insertText", protocol.Methods.input_insert_text);
try std.testing.expectEqualStrings("Input.dispatchMouseEvent", protocol.Methods.input_dispatch_mouse_event);
// DOM scroll
try std.testing.expectEqualStrings("DOM.scrollIntoViewIfNeeded", protocol.Methods.dom_scroll_into_view);
// Emulation
try std.testing.expectEqualStrings("Emulation.setEmulatedMedia", protocol.Methods.emulation_set_emulated_media);
// Network
try std.testing.expectEqualStrings("Network.emulateNetworkConditions", protocol.Methods.network_emulate_conditions);
}
test "snapshot ref cache clear and repopulate cycle" {
// Tests the pattern: navigate → snapshot → action → re-snapshot
// The ref cache must be clearable and repopulatable
var cache = SnapshotRefCache.init(std.testing.allocator);
defer cache.deinit();
// First snapshot
try cache.put("e0", 10);
try cache.put("e1", 11);
try std.testing.expectEqual(@as(?u32, 10), cache.get("e0"));
// After re-snapshot, refs may change (page re-rendered)
cache.clear();
try std.testing.expect(cache.get("e0") == null);
// New snapshot populates different refs
try cache.put("e0", 20); // same name, different node ID
try cache.put("e1", 21);
try cache.put("e2", 22); // new element appeared
try std.testing.expectEqual(@as(?u32, 20), cache.get("e0"));
try std.testing.expectEqual(@as(?u32, 22), cache.get("e2"));
}
test "action microtask flush params are well-formed JSON (#125)" {
// Regression test for #125: DOM mutations not applied after action.
// After every DOM-mutating action (click, fill, press, etc.) handleAction
// sends a Runtime.evaluate with awaitPromise:true to drain the browser's
// microtask queue so React/Vue state updates and MutationObserver callbacks
// are committed before we return. Verify the flush expression string is
// valid JSON containing the required fields.
const flush_json =
"{\"expression\":\"new Promise(r => setTimeout(r, 0))\",\"awaitPromise\":true,\"returnByValue\":true}";
// Must contain awaitPromise:true so CDP waits for the promise to resolve
try std.testing.expect(std.mem.indexOf(u8, flush_json, "\"awaitPromise\":true") != null);
// Must contain the setTimeout-based promise so the macrotask queue drains
try std.testing.expect(std.mem.indexOf(u8, flush_json, "setTimeout") != null);
try std.testing.expect(std.mem.indexOf(u8, flush_json, "new Promise") != null);
// Must be parseable as a JSON object (basic brace balance check)
var depth: i32 = 0;
for (flush_json) |ch| {
if (ch == '{') depth += 1;
if (ch == '}') depth -= 1;
}
try std.testing.expectEqual(@as(i32, 0), depth);
}