Lightpanda: the headless browser designed for AI and automation
test: let several test processes run at once
Adrià Arrufatd6dfd2321754
diff --git a/src/TestHTTPServer.zig b/src/TestHTTPServer.zigindex bbd23289a..6f2d6e47f 100644--- a/src/TestHTTPServer.zig+++ b/src/TestHTTPServer.zig@@ -28,6 +28,8 @@ const TestHTTPServer = @This();shutdown: std.atomic.Value(bool),listener: ?std.Io.net.Server,handler: Handler,+// ephemeral, known once run() has signaled the wait group+port: u16,const Handler = *const fn (req: *std.http.Server.Request) anyerror!void;@@ -36,6 +38,7 @@ pub fn init(handler: Handler) TestHTTPServer {.shutdown = .init(true),.listener = null,.handler = handler,+ .port = 0,};}@@ -54,10 +57,11 @@ pub fn stop(self: *TestHTTPServer) void {}pub fn run(self: *TestHTTPServer, wg: *lp.WaitGroup) !void {- const address = try std.Io.net.IpAddress.parse("127.0.0.1", 9582);+ const address = try std.Io.net.IpAddress.parse("127.0.0.1", 0);self.listener = try address.listen(lp.io, .{ .reuse_address = true });var listener = &self.listener.?;+ self.port = listener.socket.address.getPort();self.shutdown.store(false, .release);wg.finish();diff --git a/src/TestWSServer.zig b/src/TestWSServer.zigindex bd14cc18b..ccf239cec 100644--- a/src/TestWSServer.zig+++ b/src/TestWSServer.zig@@ -26,11 +26,14 @@ const TestWSServer = @This();shutdown: std.atomic.Value(bool),listener: ?posix.socket_t,+// ephemeral, known once run() has signaled the wait group+port: u16,pub fn init() TestWSServer {return .{.shutdown = .init(true),.listener = null,+ .port = 0,};}@@ -54,13 +57,18 @@ fn runImpl(self: *TestWSServer, wg: *lp.WaitGroup) !void {const socket = try sys_net.socket(posix.AF.INET, posix.SOCK.STREAM, 0);errdefer _ = std.c.close(socket);- const addr: sys_net.IpAddress = .{ .ip4 = .loopback(9584) };+ const addr: sys_net.IpAddress = .{ .ip4 = .loopback(0) };try posix.setsockopt(socket, posix.SOL.SOCKET, posix.SO.REUSEADDR, &std.mem.toBytes(@as(c_int, 1)));const sa = sys_net.sockaddrFromAddress(&addr);try sys_net.bind(socket, sa.ptr(), sa.len);try sys_net.listen(socket, 8);+ var bound: posix.sockaddr.storage = undefined;+ var bound_len: posix.socklen_t = @sizeOf(posix.sockaddr.storage);+ try sys_net.getsockname(socket, @ptrCast(&bound), &bound_len);+ self.port = sys_net.addressFromSockaddr(@ptrCast(&bound)).getPort();+self.listener = socket;self.shutdown.store(false, .release);wg.finish();diff --git a/src/mcp/tools.zig b/src/mcp/tools.zigindex caa69f376..899f4df8f 100644--- a/src/mcp/tools.zig+++ b/src/mcp/tools.zig@@ -1170,13 +1170,13 @@ test "MCP - save writes the script to disk" {const server = try testLoadPage("about:blank", &out.writer);defer server.deinit();- const path = "mcp-save-test-script.js";- std.Io.Dir.cwd().deleteFile(lp.io, path) catch {};- defer std.Io.Dir.cwd().deleteFile(lp.io, path) catch {};+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const path = try testing.arena_allocator.print(".zig-cache/tmp/{s}/script.js", .{&tmp.sub_path});- const msg =- \\{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"save","arguments":{"path":"mcp-save-test-script.js","script":"const page = new Page();\nawait page.goto(\"https://example.com\");"}}}- ;+ const msg = try testing.arena_allocator.print(+ \\{{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{{"name":"save","arguments":{{"path":"{s}","script":"const page = new Page();\nawait page.goto(\"https://example.com\");"}}}}}}+ , .{path});try router.handleMessage(server, testing.arena_allocator, msg);try testing.expect(std.mem.find(u8, out.written(), "saved 2 line") != null);@@ -1839,12 +1839,12 @@ test "MCP - screenshot: inline image, file, unsafe path" {// Inline images are narrowed to the model-facing limit.try testing.expect(std.mem.find(u8, out.written(), "PNG, 1280x") != null);- const path = "mcp-screenshot-test.png";- std.Io.Dir.cwd().deleteFile(lp.io, path) catch {};- defer std.Io.Dir.cwd().deleteFile(lp.io, path) catch {};+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const path = try testing.arena_allocator.print(".zig-cache/tmp/{s}/screenshot.png", .{&tmp.sub_path});out.clearRetainingCapacity();- const to_file = "{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/call\",\"params\":{\"name\":\"screenshot\",\"arguments\":{\"path\":\"" ++ path ++ "\",\"selector\":\"#hoverTarget\"}}}";+ const to_file = try testing.arena_allocator.print("{{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/call\",\"params\":{{\"name\":\"screenshot\",\"arguments\":{{\"path\":\"{s}\",\"selector\":\"#hoverTarget\"}}}}}}", .{path});try router.handleMessage(server, testing.arena_allocator, to_file);try testing.expect(std.mem.find(u8, out.written(), "Saved 1920x") != null);try testing.expect(std.mem.find(u8, out.written(), "\"type\":\"image\"") == null);diff --git a/src/network/http.zig b/src/network/http.zigindex f76cf8396..29823b9d7 100644--- a/src/network/http.zig+++ b/src/network/http.zig@@ -18,6 +18,7 @@const std = @import("std");const lp = @import("lightpanda");+const builtin = @import("builtin");const Config = @import("../Config.zig");const sys_net = @import("../sys/net.zig");@@ -31,6 +32,10 @@ const repeat = @import("../string.zig").repeat;const log = lp.log;const posix = std.posix;+// Tests run several processes, each with its own ephemeral test servers, while+// fixtures keep addressing the fixed ports. Routes those to the real ones.+pub var test_connect_to: ?*libcurl.CurlSList = null;+pub const readfunc_pause = libcurl.curl_readfunc_pause;pub const writefunc_error = libcurl.curl_writefunc_error;pub const WsFrameType = libcurl.WsFrameType;@@ -284,6 +289,9 @@ pub const Connection = struct {pub fn setURL(self: *const Connection, url: [:0]const u8) !void {try libcurl.curl_easy_setopt(self._easy, .url, url.ptr);+ if (comptime builtin.is_test) {+ try libcurl.curl_easy_setopt(self._easy, .connect_to, test_connect_to);+ }}pub fn setTimeout(self: *const Connection, timeout_ms: u32) !void {diff --git a/src/server/Server.zig b/src/server/Server.zigindex fb6846683..269402a06 100644--- a/src/server/Server.zig+++ b/src/server/Server.zig@@ -2816,7 +2816,8 @@ fn createHTTPSession(body: []const u8, expect_ws_url: bool) ![36]u8 {try testing.expectEqual(false, capabilities.get("acceptInsecureCerts").?.bool);if (expect_ws_url) {const ws_url = capabilities.get("webSocketUrl").?.string;- try testing.expectEqual("ws://127.0.0.1:9583/session/", ws_url[0 .. ws_url.len - 36]);+ const expected = try testing.arena_allocator.print("ws://127.0.0.1:{d}/session/", .{testing.test_cdp_port});+ try testing.expectEqual(expected, ws_url[0 .. ws_url.len - 36]);try testing.expectEqual(id, ws_url[ws_url.len - 36 ..]);} else {try testing.expectEqual(null, capabilities.get("webSocketUrl"));@@ -3062,7 +3063,8 @@ test "server: get /json/version" {try testing.expect(std.mem.startsWith(u8, res1, "HTTP/1.1 200 OK\r\n"));try testing.expect(std.mem.find(u8, res1, "\"Browser\": \"Lightpanda/") != null);try testing.expect(std.mem.find(u8, res1, "\"Protocol-Version\": \"1.3\"") != null);- try testing.expect(std.mem.find(u8, res1, "\"webSocketDebuggerUrl\": \"ws://127.0.0.1:9583/\"") != null);+ const ws_url = try testing.arena_allocator.print("\"webSocketDebuggerUrl\": \"ws://127.0.0.1:{d}/\"", .{testing.test_cdp_port});+ try testing.expect(std.mem.find(u8, res1, ws_url) != null);}{@@ -3228,7 +3230,7 @@ const MockCDP = struct {};fn createTestClient() !TestClient {- const address: sys_net.IpAddress = .{ .ip4 = .loopback(9583) };+ const address: sys_net.IpAddress = .{ .ip4 = .loopback(testing.test_cdp_port) };const socket = try sys_net.connect(&address);const timeout = std.mem.toBytes(posix.timeval{@@ -3382,7 +3384,7 @@ const TestClient = struct {// A server of our own, bound to an ephemeral port and never run(): these// tests drive its handlers by hand to reproduce what one event batch does.-// The real test server (port 9583) is shared and can't be torn down.+// The real test server is shared and can't be torn down.const LoopTest = struct {server: *Server,address: sys_net.IpAddress,diff --git a/src/server/cdp/domains/dom.zig b/src/server/cdp/domains/dom.zigindex 4c674bc6c..cc2487783 100644--- a/src/server/cdp/domains/dom.zig+++ b/src/server/cdp/domains/dom.zig@@ -958,6 +958,10 @@ test "cdp.dom: focus errors on an element that can't take focus" {try testing.expect(result.isTrue());}+fn tmpPath(tmp: *const std.testing.TmpDir, name: []const u8) ![]const u8 {+ return testing.arena_allocator.print(".zig-cache/tmp/{s}/{s}", .{ &tmp.sub_path, name });+}+test "cdp.dom: setFileInputFiles on file input" {var ctx = try testing.context();defer ctx.deinit();@@ -980,9 +984,9 @@ test "cdp.dom: setFileInputFiles on file input" {try ctx.expectSentResult(.{ .nodeIds = &.{1} }, .{ .id = 2 });// Drop a temp file we can upload.- try std.Io.Dir.cwd().createDirPath(lp.io, ".zig-cache/tmp");- var tmp_dir = try std.Io.Dir.cwd().openDir(lp.io, ".zig-cache/tmp", .{});- defer tmp_dir.close(lp.io);+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const tmp_dir = tmp.dir;{const f = try tmp_dir.createFile(lp.io, "upload.txt", .{ .truncate = true });defer f.close(lp.io);@@ -994,7 +998,7 @@ test "cdp.dom: setFileInputFiles on file input" {.method = "DOM.setFileInputFiles",.params = .{.nodeId = 1,- .files = &[_][]const u8{".zig-cache/tmp/upload.txt"},+ .files = &[_][]const u8{try tmpPath(&tmp, "upload.txt")},},});try ctx.expectSentResult(null, .{ .id = 3 });@@ -1016,9 +1020,9 @@ test "cdp.dom: setFileInputFiles exposes files to JS" {try ctx.expectSentResult(.{ .nodeIds = &.{1} }, .{ .id = 2 });// Two files, so we can assert ordering as well as identity and iteration.- try std.Io.Dir.cwd().createDirPath(lp.io, ".zig-cache/tmp");- var tmp_dir = try std.Io.Dir.cwd().openDir(lp.io, ".zig-cache/tmp", .{});- defer tmp_dir.close(lp.io);+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const tmp_dir = tmp.dir;{const a = try tmp_dir.createFile(lp.io, "a.txt", .{ .truncate = true });defer a.close(lp.io);@@ -1048,7 +1052,7 @@ test "cdp.dom: setFileInputFiles exposes files to JS" {.method = "DOM.setFileInputFiles",.params = .{.nodeId = 1,- .files = &[_][]const u8{ ".zig-cache/tmp/a.txt", ".zig-cache/tmp/b.txt" },+ .files = &[_][]const u8{ try tmpPath(&tmp, "a.txt"), try tmpPath(&tmp, "b.txt") },},});try ctx.expectSentResult(null, .{ .id = 3 });@@ -1138,9 +1142,9 @@ test "cdp.dom: setFileInputFiles errors when a path is missing" {// First path exists, second does not: the first File is created then must be// freed when the second read fails (the test runner panics on a leak).- try std.Io.Dir.cwd().createDirPath(lp.io, ".zig-cache/tmp");- var tmp_dir = try std.Io.Dir.cwd().openDir(lp.io, ".zig-cache/tmp", .{});- defer tmp_dir.close(lp.io);+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const tmp_dir = tmp.dir;{const f = try tmp_dir.createFile(lp.io, "upload.txt", .{ .truncate = true });defer f.close(lp.io);@@ -1152,7 +1156,7 @@ test "cdp.dom: setFileInputFiles errors when a path is missing" {.method = "DOM.setFileInputFiles",.params = .{.nodeId = 1,- .files = &[_][]const u8{ ".zig-cache/tmp/upload.txt", ".zig-cache/tmp/does-not-exist.txt" },+ .files = &[_][]const u8{ try tmpPath(&tmp, "upload.txt"), try tmpPath(&tmp, "does-not-exist.txt") },},});try ctx.expectSentError(-31998, "FileNotFound", .{ .id = 3 });@@ -1170,9 +1174,9 @@ test "cdp.dom: focus and setFileInputFiles fire in the node's own frame" {const text_node = try bc.node_registry.register(text.asNode());const upload_node = try bc.node_registry.register(upload.asNode());- try std.Io.Dir.cwd().createDirPath(lp.io, ".zig-cache/tmp");- var tmp_dir = try std.Io.Dir.cwd().openDir(lp.io, ".zig-cache/tmp", .{});- defer tmp_dir.close(lp.io);+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const tmp_dir = tmp.dir;{const f = try tmp_dir.createFile(lp.io, "upload.txt", .{ .truncate = true });defer f.close(lp.io);@@ -1186,7 +1190,7 @@ test "cdp.dom: focus and setFileInputFiles fire in the node's own frame" {.method = "DOM.setFileInputFiles",.params = .{.nodeId = upload_node.id,- .files = &[_][]const u8{".zig-cache/tmp/upload.txt"},+ .files = &[_][]const u8{try tmpPath(&tmp, "upload.txt")},},});try ctx.expectSentResult(null, .{ .id = 2 });diff --git a/src/storage/sqlite/Pool.zig b/src/storage/sqlite/Pool.zigindex 50b478f2e..4733d1688 100644--- a/src/storage/sqlite/Pool.zig+++ b/src/storage/sqlite/Pool.zig@@ -101,14 +101,13 @@ const testing = @import("../../testing.zig");test "Sqlite: Pool" {// :memory: _has_ to run with a single connection in the pool, which isn't// that useful for testing. So we create a temp file.+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const dir = try tmp.dir.realPathFileAlloc(testing.io, ".", testing.arena_allocator);+ const path = try testing.arena_allocator.printSentinel("{s}/pool.sqlite", .{dir}, 0);- std.Io.Dir.cwd().deleteFile(testing.io, "/tmp/lightpanda_test.sqlite") catch {};- var pool = try Pool.init(testing.allocator, "/tmp/lightpanda_test.sqlite");-- defer {- pool.deinit(testing.allocator);- std.Io.Dir.cwd().deleteFile(testing.io, "/tmp/lightpanda_test.sqlite") catch {};- }+ var pool = try Pool.init(testing.allocator, path);+ defer pool.deinit(testing.allocator);{const conn = try pool.acquire();diff --git a/src/sys/libcurl.zig b/src/sys/libcurl.zigindex 27afabe0c..29e74e40f 100644--- a/src/sys/libcurl.zig+++ b/src/sys/libcurl.zig@@ -233,6 +233,7 @@ const CurlOption = enum(c.CURLoption) {ssl_ctx_function = c.CURLOPT_SSL_CTX_FUNCTION,ssl_ctx_data = c.CURLOPT_SSL_CTX_DATA,ssl_ec_curves = c.CURLOPT_SSL_EC_CURVES,+ connect_to = c.CURLOPT_CONNECT_TO,};pub const CurlHttpVersion = enum(c_long) {@@ -642,7 +643,9 @@ pub fn curl_easy_setopt(easy: *Curl, comptime option: CurlOption, value: anytype.ssl_ec_curves,=> @as(?[*]const u8, value),- .http_header => @as(?*CurlSList, value),+ .http_header,+ .connect_to,+ => @as(?*CurlSList, value),.private,.header_data,diff --git a/src/telemetry/telemetry.zig b/src/telemetry/telemetry.zigindex 4dcb3344c..a0745ed42 100644--- a/src/telemetry/telemetry.zig+++ b/src/telemetry/telemetry.zig@@ -270,16 +270,16 @@ test "telemetry: always disabled in debug builds" {}test "telemetry: getOrCreateId" {- defer std.Io.Dir.cwd().deleteFile(testing.io, "/tmp/" ++ IID_FILE) catch {};+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const dir = try tmp.dir.realPathFileAlloc(testing.io, ".", testing.arena_allocator);- std.Io.Dir.cwd().deleteFile(testing.io, "/tmp/" ++ IID_FILE) catch {};-- const id1 = getOrCreateId("/tmp/").?;- const id2 = getOrCreateId("/tmp/").?;+ const id1 = getOrCreateId(dir).?;+ const id2 = getOrCreateId(dir).?;try testing.expectEqual(&id1, &id2);- std.Io.Dir.cwd().deleteFile(testing.io, "/tmp/" ++ IID_FILE) catch {};- const id3 = getOrCreateId("/tmp/").?;+ try tmp.dir.deleteFile(testing.io, IID_FILE);+ const id3 = getOrCreateId(dir).?;try testing.expectEqual(false, std.mem.eql(u8, &id1, &id3));const id4 = getOrCreateId(null).?;diff --git a/src/testing.zig b/src/testing.zigindex 2fec8b458..d32985c45 100644--- a/src/testing.zig+++ b/src/testing.zig@@ -19,6 +19,9 @@const std = @import("std");const lp = @import("lightpanda");const repeat = @import("string.zig").repeat;+const http = @import("network/http.zig");+const sys_net = @import("sys/net.zig");+const libcurl = @import("sys/libcurl.zig");const log = lp.log;const Allocator = std.mem.Allocator;@@ -515,6 +518,7 @@ const TestWSServer = @import("TestWSServer.zig");const TestHTTPServer = @import("TestHTTPServer.zig");pub var test_cdp_server: ?*Server = null;+pub var test_cdp_port: u16 = 0;var test_cdp_server_thread: ?std.Thread = null;var test_http_server: ?TestHTTPServer = null;var test_http_server_thread: ?std.Thread = null;@@ -571,6 +575,30 @@ test "tests:beforeAll" {// need to wait for the servers to be listening, else tests will fail because// they aren't able to connect.wg.wait();++ try routeTestPorts();+}++// Fixtures address the test servers by fixed ports, but each test process binds+// ephemeral ones so that several can run at once.+fn routeTestPorts() !void {+ const http_port = test_http_server.?.port;+ const ws_port = test_ws_server.?.port;+ const routes = [_]struct { []const u8, u16, u16 }{+ .{ "127.0.0.1", 9582, http_port },+ .{ "localhost", 9582, http_port },+ .{ "127.0.0.1", 9584, ws_port },+ .{ "localhost", 9584, ws_port },+ };++ var list: ?*libcurl.CurlSList = null;+ errdefer libcurl.curl_slist_free_all(list);+ for (routes) |route| {+ const host, const port, const real_port = route;+ const entry = try arena_allocator.printSentinel("{s}:{d}:127.0.0.1:{d}", .{ host, port, real_port }, 0);+ list = libcurl.curl_slist_append(list, entry) orelse return error.OutOfMemory;+ }+ http.test_connect_to = list;}test "tests:afterAll" {@@ -601,6 +629,9 @@ test "tests:afterAll" {thread.join();}+ libcurl.curl_slist_free_all(http.test_connect_to);+ http.test_connect_to = null;+@import("root").v8_peak_memory = test_browser.env.isolate.getHeapStatistics().total_physical_size;// Browser must be deinit'd before the notification — Session/Frame@@ -613,12 +644,17 @@ test "tests:afterAll" {}fn serveCDP(wg: *lp.WaitGroup) !void {- const address = try std.Io.net.IpAddress.parse("127.0.0.1", 9583);+ const address = try std.Io.net.IpAddress.parse("127.0.0.1", 0);test_cdp_server = Server.init(test_app, address) catch |err| {std.debug.print("CDP server error: {}", .{err});return err;};++ var bound: std.posix.sockaddr.storage = undefined;+ var bound_len: std.posix.socklen_t = @sizeOf(std.posix.sockaddr.storage);+ try sys_net.getsockname(test_cdp_server.?.listener, @ptrCast(&bound), &bound_len);+ test_cdp_port = sys_net.addressFromSockaddr(@ptrCast(&bound)).getPort();test_cdp_server.?.protocols = .{ .cdp = true, .webdriver = true };wg.finish();