From d666de07a71a36b22f8dd59d0fe79544056b7612 Mon Sep 17 00:00:00 2001 From: sjorsdonkers <72333389+sjorsdonkers@users.noreply.github.com> Date: Tue, 22 Apr 2025 16:47:04 +0200 Subject: [PATCH] test scaffolding --- src/browser/browser.zig | 1 + src/cdp/cdp.zig | 14 +++++---- src/cdp/testing.zig | 63 ++++++++++++++++++++++++++++++++++++----- src/runtime/testing.zig | 2 +- 4 files changed, 66 insertions(+), 14 deletions(-) diff --git a/src/browser/browser.zig b/src/browser/browser.zig index 2455077b..c5f96b36 100644 --- a/src/browser/browser.zig +++ b/src/browser/browser.zig @@ -58,6 +58,7 @@ pub const Browser = struct { http_client: *http.Client, session_pool: SessionPool, page_arena: std.heap.ArenaAllocator, + pub const EnvType = Env; const SessionPool = std.heap.MemoryPool(Session); diff --git a/src/cdp/cdp.zig b/src/cdp/cdp.zig index 3b891e67..ed297355 100644 --- a/src/cdp/cdp.zig +++ b/src/cdp/cdp.zig @@ -309,7 +309,7 @@ pub fn BrowserContext(comptime CDP_T: type) type { node_registry: Node.Registry, node_search_list: Node.Search.List, - isolated_world: ?IsolatedWorld, + isolated_world: ?IsolatedWorld(CDP_T.Browser.EnvType), const Self = @This(); @@ -481,11 +481,13 @@ pub fn BrowserContext(comptime CDP_T: type) type { /// An isolated world has it's own instance of globals like Window. /// Generally the client needs to resolve a node into the isolated world to be able to work with it. /// An object id is unique across all contexts, different object ids can refer to the same Node in different contexts. -pub const IsolatedWorld = struct { - name: []const u8, - grant_universal_access: bool, - executor: *@import("../browser/env.zig").Env.Executor, -}; +pub fn IsolatedWorld(comptime E: type) type { + return struct { + name: []const u8, + grant_universal_access: bool, + executor: *E.Executor, + }; +} // This is a generic because when we send a result we have two different // behaviors. Normally, we're sending the result to the client. But in some cases diff --git a/src/cdp/testing.zig b/src/cdp/testing.zig index b702b923..eff633e4 100644 --- a/src/cdp/testing.zig +++ b/src/cdp/testing.zig @@ -39,10 +39,13 @@ pub const Document = @import("../testing.zig").Document; const Browser = struct { session: ?*Session = null, arena: std.heap.ArenaAllocator, + env: Env, + pub const EnvType = Env; pub fn init(app: *App) !Browser { return .{ .arena = std.heap.ArenaAllocator.init(app.allocator), + .env = Env{}, }; } @@ -56,13 +59,14 @@ const Browser = struct { return error.MockBrowserSessionAlreadyExists; } const arena = self.arena.allocator(); - const executor = arena.create(Executor) catch unreachable; + const executor = arena.create(Env.Executor) catch unreachable; self.session = try arena.create(Session); self.session.?.* = .{ .page = null, - .arena = arena, + .arena = self.arena, .executor = executor, .inspector = .{}, + .state = 0, }; return self.session.?; } @@ -77,9 +81,10 @@ const Browser = struct { const Session = struct { page: ?Page = null, - arena: Allocator, - executor: *Executor, + arena: std.heap.ArenaAllocator, + executor: *Env.Executor, inspector: Inspector, + state: i32, pub fn currentPage(self: *Session) ?*Page { return &(self.page orelse return null); @@ -92,7 +97,7 @@ const Session = struct { self.page = .{ .session = self, .url = URL.parse("https://lightpanda.io/", null) catch unreachable, - .aux_data = try self.arena.dupe(u8, aux_data orelse ""), + .aux_data = try self.arena.allocator().dupe(u8, aux_data orelse ""), }; return &self.page.?; } @@ -107,12 +112,43 @@ const Session = struct { } }; -const Executor = struct {}; +const Env = struct { + pub const Executor = MockExecutor; + pub fn startExecutor(self: *Env, comptime Global: type, state: anytype, module_loader: anytype, kind: anytype) !*Executor { + _ = self; + _ = Global; + _ = state; + _ = module_loader; + _ = kind; + return error.MockExecutor; + } + pub fn stopExecutor(self: *Env, executor: *Executor) void { + _ = self; + _ = executor; + } +}; +const MockExecutor = struct { + context: Context, + + pub fn startScope(self: *MockExecutor, global: anytype) !void { + _ = self; + _ = global; + } + pub fn endScope(self: *MockExecutor) void { + _ = self; + } +}; +const Context = struct { + pub fn debugContextId(self: Context) i32 { + _ = self; + return 0; + } +}; const Inspector = struct { pub fn getRemoteObject( self: *const Inspector, - executor: *Executor, + executor: *Env.Executor, group: []const u8, value: anytype, ) !RemoteObject { @@ -127,6 +163,19 @@ const Inspector = struct { _ = object_id; return try alloc.create(i32); } + pub fn contextCreated(self: *const Inspector, + executor: *const Env.Executor, + name: []const u8, + origin: []const u8, + aux_data: ?[]const u8, + is_default_context: bool,) void { + _ = self; + _ = executor; + _ = name; + _ = origin; + _ = aux_data; + _ = is_default_context; + } }; const RemoteObject = struct { diff --git a/src/runtime/testing.zig b/src/runtime/testing.zig index 2a09b2e8..fcfab82b 100644 --- a/src/runtime/testing.zig +++ b/src/runtime/testing.zig @@ -43,7 +43,7 @@ pub fn Runner(comptime State: type, comptime Global: type, comptime types: anyty const G = if (Global == void) DefaultGlobal else Global; - runner.executor = try runner.env.startExecutor(G, state, runner); + runner.executor = try runner.env.startExecutor(G, state, runner, .main); errdefer runner.env.stopExecutor(runner.executor); try runner.executor.startScope(if (Global == void) &default_global else global);