-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathtest_server.zig
More file actions
265 lines (208 loc) · 9.23 KB
/
test_server.zig
File metadata and controls
265 lines (208 loc) · 9.23 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
const std = @import("std");
const testing = std.testing;
const expect = testing.expect;
const builtin = @import("builtin");
fn getOdiffPath(allocator: std.mem.Allocator) ![]const u8 {
const odiff_name = if (builtin.os.tag == .windows) "odiff.exe" else "odiff";
const installed_path = try std.fs.path.join(allocator, &[_][]const u8{ "zig-out", "bin", odiff_name });
const cwd = std.fs.cwd();
cwd.access(installed_path, .{}) catch {
allocator.free(installed_path);
const exe_dir_path = try std.fs.selfExeDirPathAlloc(allocator);
defer allocator.free(exe_dir_path);
return std.fs.path.join(allocator, &[_][]const u8{ exe_dir_path, odiff_name });
};
return installed_path;
}
fn readLineFromPipe(file: std.fs.File, buf: []u8) ![]const u8 {
var file_reader = file.reader(buf);
const reader = &file_reader.interface;
const line = try reader.takeDelimiter('\n') orelse return error.EndOfStream;
return line;
}
test "server: end-to-end identical images match" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const odiff_path = try getOdiffPath(allocator);
defer allocator.free(odiff_path);
const cwd = std.fs.cwd();
cwd.access(odiff_path, .{}) catch |err| {
std.debug.print("odiff binary not found at {s}: {}\n", .{ odiff_path, err });
return error.SkipZigTest;
};
var child = std.process.Child.init(&[_][]const u8{ odiff_path, "--server" }, allocator);
child.stdin_behavior = .Pipe;
child.stdout_behavior = .Pipe;
child.stderr_behavior = .Pipe;
try child.spawn();
defer _ = child.kill() catch {};
const stdin = child.stdin.?;
const stdout = child.stdout.?;
const stderr = child.stderr.?;
var ready_buf: [256]u8 = undefined;
const ready_msg = readLineFromPipe(stdout, &ready_buf) catch |err| {
var stderr_buf: [1024]u8 = undefined;
const stderr_len = stderr.read(&stderr_buf) catch 0;
std.debug.print("Failed to read ready message: {}\nstderr: {s}\n", .{ err, stderr_buf[0..stderr_len] });
return err;
};
try expect(std.mem.indexOf(u8, ready_msg, "\"ready\":true") != null);
const request =
\\{"requestId":1,"base":"test/png/orange.png","compare":"test/png/orange.png","output":"/tmp/test_diff.png"}
\\
;
try stdin.writeAll(request);
var response_buf: [1024]u8 = undefined;
const response = try readLineFromPipe(stdout, &response_buf);
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, response, .{});
defer parsed.deinit();
const obj = parsed.value.object;
try expect(obj.get("requestId").?.integer == 1);
try expect(obj.get("match").?.bool == true);
}
test "server: end-to-end different images show pixel diff" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const odiff_path = try getOdiffPath(allocator);
defer allocator.free(odiff_path);
const cwd = std.fs.cwd();
cwd.access(odiff_path, .{}) catch |err| {
std.debug.print("odiff binary not found at {s}: {}\n", .{ odiff_path, err });
return error.SkipZigTest;
};
var child = std.process.Child.init(&[_][]const u8{ odiff_path, "--server" }, allocator);
child.stdin_behavior = .Pipe;
child.stdout_behavior = .Pipe;
child.stderr_behavior = .Pipe;
try child.spawn();
defer _ = child.kill() catch {};
const stdin = child.stdin.?;
const stdout = child.stdout.?;
var ready_buf: [256]u8 = undefined;
_ = try readLineFromPipe(stdout, &ready_buf);
const request =
\\{"requestId":2,"base":"test/png/orange.png","compare":"test/png/orange_changed.png","output":"/tmp/test_diff2.png","options":{"threshold":0.1}}
\\
;
try stdin.writeAll(request);
var response_buf: [1024]u8 = undefined;
const response = try readLineFromPipe(stdout, &response_buf);
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, response, .{});
defer parsed.deinit();
const obj = parsed.value.object;
try expect(obj.get("requestId").?.integer == 2);
try expect(obj.get("match").?.bool == false);
try expect(obj.get("reason") != null);
try expect(std.mem.eql(u8, "pixel-diff", obj.get("reason").?.string));
try expect(obj.get("diffCount").?.integer > 0);
try expect(obj.get("diffPercentage").?.float > 0.0);
}
test "server: ignore regions support" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const odiff_path = try getOdiffPath(allocator);
defer allocator.free(odiff_path);
const cwd = std.fs.cwd();
cwd.access(odiff_path, .{}) catch |err| {
std.debug.print("odiff binary not found at {s}: {}\n", .{ odiff_path, err });
return error.SkipZigTest;
};
var child = std.process.Child.init(&[_][]const u8{ odiff_path, "--server" }, allocator);
child.stdin_behavior = .Pipe;
child.stdout_behavior = .Pipe;
child.stderr_behavior = .Pipe;
try child.spawn();
defer _ = child.kill() catch {};
const stdin = child.stdin.?;
const stdout = child.stdout.?;
var ready_buf: [256]u8 = undefined;
_ = try readLineFromPipe(stdout, &ready_buf);
// Use the same ignore regions as in test_core.zig that cover all differences
const request =
\\{"requestId":3,"base":"test/png/orange.png","compare":"test/png/orange_changed.png","output":"/tmp/test_diff3.png","options":{"ignoreRegions":[{"x1":150,"y1":30,"x2":310,"y2":105},{"x1":20,"y1":175,"x2":105,"y2":200}]}}
\\
;
try stdin.writeAll(request);
var response_buf: [1024]u8 = undefined;
const response = try readLineFromPipe(stdout, &response_buf);
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, response, .{});
defer parsed.deinit();
const obj = parsed.value.object;
try expect(obj.get("requestId").?.integer == 3);
try expect(obj.get("match").?.bool == true); // All diffs are in ignored regions
}
test "server: empty ignore regions array works correctly" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const odiff_path = try getOdiffPath(allocator);
defer allocator.free(odiff_path);
const cwd = std.fs.cwd();
cwd.access(odiff_path, .{}) catch |err| {
std.debug.print("odiff binary not found at {s}: {}\n", .{ odiff_path, err });
return error.SkipZigTest;
};
var child = std.process.Child.init(&[_][]const u8{ odiff_path, "--server" }, allocator);
child.stdin_behavior = .Pipe;
child.stdout_behavior = .Pipe;
child.stderr_behavior = .Pipe;
try child.spawn();
defer _ = child.kill() catch {};
const stdin = child.stdin.?;
const stdout = child.stdout.?;
var ready_buf: [256]u8 = undefined;
_ = try readLineFromPipe(stdout, &ready_buf);
const request =
\\{"requestId":4,"base":"test/png/orange.png","compare":"test/png/orange_changed.png","output":"/tmp/test_diff4.png","options":{"ignoreRegions":[]}}
\\
;
try stdin.writeAll(request);
var response_buf: [1024]u8 = undefined;
const response = try readLineFromPipe(stdout, &response_buf);
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, response, .{});
defer parsed.deinit();
const obj = parsed.value.object;
try expect(obj.get("requestId").?.integer == 4);
try expect(obj.get("match").?.bool == false); // Should behave like normal diff
try expect(obj.get("diffCount").?.integer > 0);
}
test "server: invalid ignore regions return error" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const odiff_path = try getOdiffPath(allocator);
defer allocator.free(odiff_path);
const cwd = std.fs.cwd();
cwd.access(odiff_path, .{}) catch |err| {
std.debug.print("odiff binary not found at {s}: {}\n", .{ odiff_path, err });
return error.SkipZigTest;
};
var child = std.process.Child.init(&[_][]const u8{ odiff_path, "--server" }, allocator);
child.stdin_behavior = .Pipe;
child.stdout_behavior = .Pipe;
child.stderr_behavior = .Pipe;
try child.spawn();
defer _ = child.kill() catch {};
const stdin = child.stdin.?;
const stdout = child.stdout.?;
var ready_buf: [256]u8 = undefined;
_ = try readLineFromPipe(stdout, &ready_buf);
// Test missing required field (y2)
const request =
\\{"requestId":5,"base":"test/png/orange.png","compare":"test/png/orange_changed.png","output":"/tmp/test_diff5.png","options":{"ignoreRegions":[{"x1":10,"y1":20,"x2":30}]}}
\\
;
try stdin.writeAll(request);
var response_buf: [1024]u8 = undefined;
const response = try readLineFromPipe(stdout, &response_buf);
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, response, .{});
defer parsed.deinit();
const obj = parsed.value.object;
try expect(obj.get("requestId").?.integer == 5);
try expect(obj.get("error") != null);
const error_msg = obj.get("error").?.string;
try expect(std.mem.indexOf(u8, error_msg, "missing required field") != null);
}