-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtesting.zig
More file actions
375 lines (323 loc) · 11.5 KB
/
Copy pathtesting.zig
File metadata and controls
375 lines (323 loc) · 11.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
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const json = std.json;
const Allocator = std.mem.Allocator;
const Testing = @This();
const main = @import("cdp.zig");
const parser = @import("netsurf");
const App = @import("../app.zig").App;
const base = @import("../testing.zig");
pub const allocator = base.allocator;
pub const expectJson = base.expectJson;
pub const expectEqual = base.expectEqual;
pub const expectError = base.expectError;
pub const expectEqualSlices = base.expectEqualSlices;
pub const Document = @import("../testing.zig").Document;
const Browser = struct {
session: ?*Session = null,
arena: std.heap.ArenaAllocator,
pub fn init(app: *App) Browser {
return .{
.arena = std.heap.ArenaAllocator.init(app.allocator),
};
}
pub fn deinit(self: *Browser) void {
self.arena.deinit();
}
pub fn newSession(self: *Browser, ctx: anytype) !*Session {
_ = ctx;
if (self.session != null) {
return error.MockBrowserSessionAlreadyExists;
}
const arena = self.arena.allocator();
self.session = try arena.create(Session);
self.session.?.* = .{
.page = null,
.arena = arena,
.env = Env{},
.inspector = Inspector{},
};
return self.session.?;
}
pub fn hasSession(self: *const Browser, session_id: []const u8) bool {
const session = self.session orelse return false;
return std.mem.eql(u8, session.id, session_id);
}
pub fn runMicrotasks(_: *const Browser) void {}
};
const Session = struct {
page: ?Page = null,
arena: Allocator,
env: Env,
inspector: Inspector,
pub fn currentPage(self: *Session) ?*Page {
return &(self.page orelse return null);
}
pub fn createPage(self: *Session, aux_data: ?[]const u8) !*Page {
if (self.page != null) {
return error.MockBrowserPageAlreadyExists;
}
self.page = .{
.rawuri = "",
.session = self,
.aux_data = try self.arena.dupe(u8, aux_data orelse ""),
};
return &self.page.?;
}
pub fn removePage(self: *Session) void {
self.page = null;
}
pub fn callInspector(self: *Session, msg: []const u8) void {
_ = self;
_ = msg;
}
};
const Env = struct {
pub fn findOrAddValue(self: *Env, value: anytype) !@TypeOf(value) { // ?
_ = self;
return value;
}
};
const Inspector = struct {
pub fn getRemoteObject(self: Inspector, env: *Env, jsValue: anytype, groupName: []const u8) !RemoteObject {
_ = self;
_ = env;
_ = jsValue;
_ = groupName;
return RemoteObject{};
}
};
const RemoteObject = struct {
pub fn deinit(self: RemoteObject) void {
_ = self;
}
pub fn getType(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
_ = self;
_ = alloc;
return "TheType";
}
pub fn getSubtype(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
_ = self;
_ = alloc;
return "TheSubtype";
}
pub fn getClassName(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
_ = self;
_ = alloc;
return "TheClassName";
}
pub fn getDescription(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
_ = self;
_ = alloc;
return "TheDescription";
}
pub fn getObjectId(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
_ = self;
_ = alloc;
return "TheObjectId";
}
};
const Page = struct {
session: *Session,
rawuri: []const u8,
aux_data: []const u8 = "",
doc: ?*parser.Document = null,
pub fn navigate(_: *Page, url: []const u8, aux_data: []const u8) !void {
_ = url;
_ = aux_data;
}
const MouseEvent = @import("../browser/browser.zig").Page.MouseEvent;
const ClickResult = @import("../browser/browser.zig").Page.ClickResult;
pub fn mouseEvent(_: *Page, _: Allocator, _: MouseEvent) !?ClickResult {
return null;
}
};
const Client = struct {
allocator: Allocator,
sent: std.ArrayListUnmanaged(json.Value) = .{},
serialized: std.ArrayListUnmanaged([]const u8) = .{},
fn init(alloc: Allocator) Client {
return .{
.allocator = alloc,
};
}
pub fn sendJSON(self: *Client, message: anytype, opts: json.StringifyOptions) !void {
var opts_copy = opts;
opts_copy.whitespace = .indent_2;
const serialized = try json.stringifyAlloc(self.allocator, message, opts_copy);
try self.serialized.append(self.allocator, serialized);
const value = try json.parseFromSliceLeaky(json.Value, self.allocator, serialized, .{});
try self.sent.append(self.allocator, value);
}
};
const TestCDP = main.CDPT(struct {
pub const Browser = Testing.Browser;
pub const Session = Testing.Session;
pub const Client = *Testing.Client;
});
const TestContext = struct {
app: *App,
client: ?Client = null,
cdp_: ?TestCDP = null,
arena: std.heap.ArenaAllocator,
pub fn deinit(self: *TestContext) void {
if (self.cdp_) |*c| {
c.deinit();
}
self.app.deinit();
self.arena.deinit();
}
pub fn cdp(self: *TestContext) *TestCDP {
if (self.cdp_ == null) {
self.client = Client.init(self.arena.allocator());
// Don't use the arena here. We want to detect leaks in CDP.
// The arena is only for test-specific stuff
self.cdp_ = TestCDP.init(self.app, &self.client.?);
}
return &self.cdp_.?;
}
const BrowserContextOpts = struct {
id: ?[]const u8 = null,
target_id: ?[]const u8 = null,
session_id: ?[]const u8 = null,
html: ?[]const u8 = null,
};
pub fn loadBrowserContext(self: *TestContext, opts: BrowserContextOpts) !*main.BrowserContext(TestCDP) {
var c = self.cdp();
c.browser.session = null;
if (c.browser_context) |bc| {
bc.deinit();
c.browser_context = null;
}
_ = try c.createBrowserContext();
var bc = c.browser_context.?;
if (opts.id) |id| {
bc.id = id;
}
if (opts.target_id) |tid| {
bc.target_id = tid;
}
if (opts.session_id) |sid| {
bc.session_id = sid;
}
if (opts.html) |html| {
parser.deinit();
try parser.init();
const page = try bc.session.createPage(null);
page.doc = (try Document.init(html)).doc;
}
return bc;
}
pub fn processMessage(self: *TestContext, msg: anytype) !void {
var json_message: []const u8 = undefined;
if (@typeInfo(@TypeOf(msg)) != .pointer) {
json_message = try std.json.stringifyAlloc(self.arena.allocator(), msg, .{});
} else {
// assume this is a string we want to send as-is, if it isn't, we'll
// get a compile error, so no big deal.
json_message = msg;
}
return self.cdp().processMessage(json_message);
}
pub fn expectSentCount(self: *TestContext, expected: usize) !void {
try expectEqual(expected, self.client.?.sent.items.len);
}
const ExpectResultOpts = struct {
id: ?usize = null,
index: ?usize = null,
session_id: ?[]const u8 = null,
};
pub fn expectSentResult(self: *TestContext, expected: anytype, opts: ExpectResultOpts) !void {
const expected_result = .{
.id = opts.id,
.result = if (comptime @typeInfo(@TypeOf(expected)) == .null) struct {}{} else expected,
.sessionId = opts.session_id,
};
try self.expectSent(expected_result, .{ .index = opts.index });
}
const ExpectEventOpts = struct {
index: ?usize = null,
session_id: ?[]const u8 = null,
};
pub fn expectSentEvent(self: *TestContext, method: []const u8, params: anytype, opts: ExpectEventOpts) !void {
const expected_event = .{
.method = method,
.params = if (comptime @typeInfo(@TypeOf(params)) == .null) struct {}{} else params,
.sessionId = opts.session_id,
};
try self.expectSent(expected_event, .{ .index = opts.index });
}
const ExpectErrorOpts = struct {
id: ?usize = null,
index: ?usize = null,
};
pub fn expectSentError(self: *TestContext, code: i32, message: []const u8, opts: ExpectErrorOpts) !void {
const expected_message = .{
.id = opts.id,
.code = code,
.message = message,
};
try self.expectSent(expected_message, .{ .index = opts.index });
}
const SentOpts = struct {
index: ?usize = null,
};
pub fn expectSent(self: *TestContext, expected: anytype, opts: SentOpts) !void {
const serialized = try json.stringifyAlloc(self.arena.allocator(), expected, .{
.whitespace = .indent_2,
.emit_null_optional_fields = false,
});
for (self.client.?.sent.items, 0..) |sent, i| {
if (try compareExpectedToSent(serialized, sent) == false) {
continue;
}
if (opts.index) |expected_index| {
if (expected_index != i) {
return error.ErrorAtWrongIndex;
}
}
_ = self.client.?.sent.orderedRemove(i);
_ = self.client.?.serialized.orderedRemove(i);
return;
}
std.debug.print("Error not found. Expecting:\n{s}\n\nGot:\n", .{serialized});
for (self.client.?.serialized.items, 0..) |sent, i| {
std.debug.print("#{d}\n{s}\n\n", .{ i, sent });
}
return error.ErrorNotFound;
}
};
pub fn context() TestContext {
return .{
.app = App.init(std.testing.allocator, .{ .run_mode = .serve }) catch unreachable,
.arena = std.heap.ArenaAllocator.init(std.testing.allocator),
};
}
// Zig makes this hard. When sendJSON is called, we're sending an anytype.
// We can't record that in an ArrayList(???), so we serialize it to JSON.
// Now, ideally, we could just take our expected structure, serialize it to
// json and check if the two are equal.
// Except serializing to JSON isn't deterministic.
// So we serialize the JSON then we deserialize to json.Value. And then we can
// compare our anytype expection with the json.Value that we captured
fn compareExpectedToSent(expected: []const u8, actual: json.Value) !bool {
const expected_value = try std.json.parseFromSlice(json.Value, std.testing.allocator, expected, .{});
defer expected_value.deinit();
return base.isEqualJson(expected_value.value, actual);
}