1919const std = @import ("std" );
2020const json = std .json ;
2121const Allocator = std .mem .Allocator ;
22+ const ArenaAllocator = std .heap .ArenaAllocator ;
2223
2324const Testing = @This ();
2425
@@ -36,196 +37,6 @@ pub const expectEqualSlices = base.expectEqualSlices;
3637
3738pub const Document = @import ("../testing.zig" ).Document ;
3839
39- const Browser = struct {
40- session : ? * Session = null ,
41- arena : std.heap.ArenaAllocator ,
42- env : Env ,
43- pub const EnvType = Env ;
44-
45- pub fn init (app : * App ) ! Browser {
46- return .{
47- .arena = std .heap .ArenaAllocator .init (app .allocator ),
48- .env = Env {},
49- };
50- }
51-
52- pub fn deinit (self : * Browser ) void {
53- self .arena .deinit ();
54- }
55-
56- pub fn newSession (self : * Browser , ctx : anytype ) ! * Session {
57- _ = ctx ;
58- if (self .session != null ) {
59- return error .MockBrowserSessionAlreadyExists ;
60- }
61- const arena = self .arena .allocator ();
62- const executor = arena .create (Env .Executor ) catch unreachable ;
63- self .session = try arena .create (Session );
64- self .session .?.* = .{
65- .page = null ,
66- .arena = self .arena ,
67- .executor = executor ,
68- .inspector = .{},
69- .state = 0 ,
70- };
71- return self .session .? ;
72- }
73-
74- pub fn hasSession (self : * const Browser , session_id : []const u8 ) bool {
75- const session = self .session orelse return false ;
76- return std .mem .eql (u8 , session .id , session_id );
77- }
78-
79- pub fn runMicrotasks (_ : * const Browser ) void {}
80- };
81-
82- const Session = struct {
83- page : ? Page = null ,
84- arena : std.heap.ArenaAllocator ,
85- executor : * Env.Executor ,
86- inspector : Inspector ,
87- state : i32 ,
88-
89- pub fn currentPage (self : * Session ) ? * Page {
90- return &(self .page orelse return null );
91- }
92-
93- pub fn createPage (self : * Session , aux_data : ? []const u8 ) ! * Page {
94- if (self .page != null ) {
95- return error .MockBrowserPageAlreadyExists ;
96- }
97- self .page = .{
98- .session = self ,
99- .url = URL .parse ("https://lightpanda.io/" , null ) catch unreachable ,
100- .aux_data = try self .arena .allocator ().dupe (u8 , aux_data orelse "" ),
101- };
102- return & self .page .? ;
103- }
104-
105- pub fn removePage (self : * Session ) void {
106- self .page = null ;
107- }
108-
109- pub fn callInspector (self : * Session , msg : []const u8 ) void {
110- _ = self ;
111- _ = msg ;
112- }
113- };
114-
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- };
147-
148- const Inspector = struct {
149- pub fn getRemoteObject (
150- self : * const Inspector ,
151- executor : * Env.Executor ,
152- group : []const u8 ,
153- value : anytype ,
154- ) ! RemoteObject {
155- _ = self ;
156- _ = executor ;
157- _ = group ;
158- _ = value ;
159- return RemoteObject {};
160- }
161- pub fn getNodePtr (self : Inspector , alloc : std.mem.Allocator , object_id : []const u8 ) ! ? * anyopaque {
162- _ = self ;
163- _ = object_id ;
164- return try alloc .create (i32 );
165- }
166- pub fn contextCreated (
167- self : * const Inspector ,
168- executor : * const Env.Executor ,
169- name : []const u8 ,
170- origin : []const u8 ,
171- aux_data : ? []const u8 ,
172- is_default_context : bool ,
173- ) void {
174- _ = self ;
175- _ = executor ;
176- _ = name ;
177- _ = origin ;
178- _ = aux_data ;
179- _ = is_default_context ;
180- }
181- };
182-
183- const RemoteObject = struct {
184- pub fn deinit (self : RemoteObject ) void {
185- _ = self ;
186- }
187- pub fn getType (self : RemoteObject , alloc : std.mem.Allocator ) ! [:0 ]const u8 {
188- _ = self ;
189- _ = alloc ;
190- return "TheType" ;
191- }
192- pub fn getSubtype (self : RemoteObject , alloc : std.mem.Allocator ) ! [:0 ]const u8 {
193- _ = self ;
194- _ = alloc ;
195- return "TheSubtype" ;
196- }
197- pub fn getClassName (self : RemoteObject , alloc : std.mem.Allocator ) ! [:0 ]const u8 {
198- _ = self ;
199- _ = alloc ;
200- return "TheClassName" ;
201- }
202- pub fn getDescription (self : RemoteObject , alloc : std.mem.Allocator ) ! [:0 ]const u8 {
203- _ = self ;
204- _ = alloc ;
205- return "TheDescription" ;
206- }
207- pub fn getObjectId (self : RemoteObject , alloc : std.mem.Allocator ) ! [:0 ]const u8 {
208- _ = self ;
209- _ = alloc ;
210- return "TheObjectId" ;
211- }
212- };
213-
214- const Page = struct {
215- session : * Session ,
216- url : ? URL = null ,
217- aux_data : []const u8 = "" ,
218- doc : ? * parser.Document = null ,
219-
220- pub fn navigate (_ : * Page , url : URL , opts : anytype ) ! void {
221- _ = url ;
222- _ = opts ;
223- }
224-
225- const MouseEvent = @import ("../browser/browser.zig" ).Page .MouseEvent ;
226- pub fn mouseEvent (_ : * Page , _ : MouseEvent ) ! void {}
227- };
228-
22940const Client = struct {
23041 allocator : Allocator ,
23142 sent : std .ArrayListUnmanaged (json .Value ) = .{},
@@ -246,19 +57,22 @@ const Client = struct {
24657 const value = try json .parseFromSliceLeaky (json .Value , self .allocator , serialized , .{});
24758 try self .sent .append (self .allocator , value );
24859 }
60+
61+ pub fn sendJSONRaw (self : * Client , _ : ArenaAllocator , buf : std .ArrayListUnmanaged (u8 )) ! void {
62+ const value = try json .parseFromSliceLeaky (json .Value , self .allocator , buf .items , .{});
63+ try self .sent .append (self .allocator , value );
64+ }
24965};
25066
25167const TestCDP = main .CDPT (struct {
252- pub const Browser = Testing .Browser ;
253- pub const Session = Testing .Session ;
25468 pub const Client = * Testing .Client ;
25569});
25670
25771const TestContext = struct {
25872 app : * App ,
25973 client : ? Client = null ,
26074 cdp_ : ? TestCDP = null ,
261- arena : std.heap. ArenaAllocator ,
75+ arena : ArenaAllocator ,
26276
26377 pub fn deinit (self : * TestContext ) void {
26478 if (self .cdp_ ) | * c | {
@@ -273,7 +87,7 @@ const TestContext = struct {
27387 self .client = Client .init (self .arena .allocator ());
27488 // Don't use the arena here. We want to detect leaks in CDP.
27589 // The arena is only for test-specific stuff
276- self .cdp_ = try TestCDP .init (self .app , & self .client .? );
90+ self .cdp_ = TestCDP .init (self .app , & self .client .? ) catch unreachable ;
27791 }
27892 return & self .cdp_ .? ;
27993 }
@@ -286,11 +100,8 @@ const TestContext = struct {
286100 };
287101 pub fn loadBrowserContext (self : * TestContext , opts : BrowserContextOpts ) ! * main .BrowserContext (TestCDP ) {
288102 var c = self .cdp ();
289- c .browser .session = null ;
290-
291103 if (c .browser_context ) | bc | {
292- bc .deinit ();
293- c .browser_context = null ;
104+ _ = c .disposeBrowserContext (bc .id );
294105 }
295106
296107 _ = try c .createBrowserContext ();
@@ -410,7 +221,7 @@ const TestContext = struct {
410221pub fn context () TestContext {
411222 return .{
412223 .app = App .init (std .testing .allocator , .{ .run_mode = .serve }) catch unreachable ,
413- .arena = std . heap . ArenaAllocator .init (std .testing .allocator ),
224+ .arena = ArenaAllocator .init (std .testing .allocator ),
414225 };
415226}
416227
@@ -420,7 +231,7 @@ pub fn context() TestContext {
420231// json and check if the two are equal.
421232// Except serializing to JSON isn't deterministic.
422233// So we serialize the JSON then we deserialize to json.Value. And then we can
423- // compare our anytype expection with the json.Value that we captured
234+ // compare our anytype expectation with the json.Value that we captured
424235
425236fn compareExpectedToSent (expected : []const u8 , actual : json.Value ) ! bool {
426237 const expected_value = try std .json .parseFromSlice (json .Value , std .testing .allocator , expected , .{});
0 commit comments