Skip to content

Commit 42465e5

Browse files
committed
test scaffolding
1 parent d85b763 commit 42465e5

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

src/browser/browser.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub const Browser = struct {
5858
http_client: *http.Client,
5959
session_pool: SessionPool,
6060
page_arena: std.heap.ArenaAllocator,
61+
pub const EnvType = Env;
6162

6263
const SessionPool = std.heap.MemoryPool(Session);
6364

src/cdp/cdp.zig

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
309309
node_registry: Node.Registry,
310310
node_search_list: Node.Search.List,
311311

312-
isolated_world: ?IsolatedWorld,
312+
isolated_world: ?IsolatedWorld(CDP_T.Browser.EnvType),
313313

314314
const Self = @This();
315315

@@ -481,11 +481,13 @@ pub fn BrowserContext(comptime CDP_T: type) type {
481481
/// An isolated world has it's own instance of globals like Window.
482482
/// Generally the client needs to resolve a node into the isolated world to be able to work with it.
483483
/// An object id is unique across all contexts, different object ids can refer to the same Node in different contexts.
484-
pub const IsolatedWorld = struct {
485-
name: []const u8,
486-
grant_universal_access: bool,
487-
executor: *@import("../browser/env.zig").Env.Executor,
488-
};
484+
pub fn IsolatedWorld(comptime E: type) type {
485+
return struct {
486+
name: []const u8,
487+
grant_universal_access: bool,
488+
executor: *E.Executor,
489+
};
490+
}
489491

490492
// This is a generic because when we send a result we have two different
491493
// behaviors. Normally, we're sending the result to the client. But in some cases

src/cdp/testing.zig

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ pub const Document = @import("../testing.zig").Document;
3939
const Browser = struct {
4040
session: ?*Session = null,
4141
arena: std.heap.ArenaAllocator,
42+
env: Env,
43+
pub const EnvType = Env;
4244

4345
pub fn init(app: *App) !Browser {
4446
return .{
4547
.arena = std.heap.ArenaAllocator.init(app.allocator),
48+
.env = Env{},
4649
};
4750
}
4851

@@ -56,13 +59,14 @@ const Browser = struct {
5659
return error.MockBrowserSessionAlreadyExists;
5760
}
5861
const arena = self.arena.allocator();
59-
const executor = arena.create(Executor) catch unreachable;
62+
const executor = arena.create(Env.Executor) catch unreachable;
6063
self.session = try arena.create(Session);
6164
self.session.?.* = .{
6265
.page = null,
63-
.arena = arena,
66+
.arena = self.arena,
6467
.executor = executor,
6568
.inspector = .{},
69+
.state = 0,
6670
};
6771
return self.session.?;
6872
}
@@ -77,9 +81,10 @@ const Browser = struct {
7781

7882
const Session = struct {
7983
page: ?Page = null,
80-
arena: Allocator,
81-
executor: *Executor,
84+
arena: std.heap.ArenaAllocator,
85+
executor: *Env.Executor,
8286
inspector: Inspector,
87+
state: i32,
8388

8489
pub fn currentPage(self: *Session) ?*Page {
8590
return &(self.page orelse return null);
@@ -92,7 +97,7 @@ const Session = struct {
9297
self.page = .{
9398
.session = self,
9499
.url = URL.parse("https://lightpanda.io/", null) catch unreachable,
95-
.aux_data = try self.arena.dupe(u8, aux_data orelse ""),
100+
.aux_data = try self.arena.allocator().dupe(u8, aux_data orelse ""),
96101
};
97102
return &self.page.?;
98103
}
@@ -107,12 +112,43 @@ const Session = struct {
107112
}
108113
};
109114

110-
const Executor = struct {};
115+
const Env = struct {
116+
pub const Executor = MockExecutor;
117+
pub fn startExecutor(self: *Env, comptime Global: type, state: anytype, module_loader: anytype, kind: anytype) !*Executor {
118+
_ = self;
119+
_ = Global;
120+
_ = state;
121+
_ = module_loader;
122+
_ = kind;
123+
return error.MockExecutor;
124+
}
125+
pub fn stopExecutor(self: *Env, executor: *Executor) void {
126+
_ = self;
127+
_ = executor;
128+
}
129+
};
130+
const MockExecutor = struct {
131+
context: Context,
132+
133+
pub fn startScope(self: *MockExecutor, global: anytype) !void {
134+
_ = self;
135+
_ = global;
136+
}
137+
pub fn endScope(self: *MockExecutor) void {
138+
_ = self;
139+
}
140+
};
141+
const Context = struct {
142+
pub fn debugContextId(self: Context) i32 {
143+
_ = self;
144+
return 0;
145+
}
146+
};
111147

112148
const Inspector = struct {
113149
pub fn getRemoteObject(
114150
self: *const Inspector,
115-
executor: *Executor,
151+
executor: *Env.Executor,
116152
group: []const u8,
117153
value: anytype,
118154
) !RemoteObject {
@@ -127,6 +163,19 @@ const Inspector = struct {
127163
_ = object_id;
128164
return try alloc.create(i32);
129165
}
166+
pub fn contextCreated(self: *const Inspector,
167+
executor: *const Env.Executor,
168+
name: []const u8,
169+
origin: []const u8,
170+
aux_data: ?[]const u8,
171+
is_default_context: bool,) void {
172+
_ = self;
173+
_ = executor;
174+
_ = name;
175+
_ = origin;
176+
_ = aux_data;
177+
_ = is_default_context;
178+
}
130179
};
131180

132181
const RemoteObject = struct {

src/runtime/testing.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn Runner(comptime State: type, comptime Global: type, comptime types: anyty
4343

4444
const G = if (Global == void) DefaultGlobal else Global;
4545

46-
runner.executor = try runner.env.startExecutor(G, state, runner);
46+
runner.executor = try runner.env.startExecutor(G, state, runner, .main);
4747
errdefer runner.env.stopExecutor(runner.executor);
4848

4949
try runner.executor.startScope(if (Global == void) &default_global else global);

0 commit comments

Comments
 (0)