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
|
//! docs/typ/reference.typ, Addons <addons> and the PDF passages of Panes
//! <panes> and Look and exec <look-and-exec>: a PDF is a pane of the pdf
//! addon (addons/pdf/pdf.zig), driven here as a 9P client drives it. What
//! test/pdf.zig asked of the core's PDF code before it left the core.
const std = @import("std");
const Session = @import("session").Session;
fn has(hay: []const u8, needle: []const u8) bool {
return std.mem.indexOf(u8, hay, needle) != null;
}
/// The pictures /screen places for pane `serial`: their keys and the part
/// of their pixels shown, `[x, y, w, h]`.
const Shown = struct { key: []const u8, src: [4]i64 };
fn shown(s: *Session, serial: u32) ![]Shown {
const a = s.arena.allocator();
const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try s.read("/screen"), .{});
var out: std.ArrayList(Shown) = .empty;
const images = json.object.get("images") orelse return out.items;
for (images.array.items) |im| {
if (im.object.get("serial").?.integer != serial) continue;
var src: [4]i64 = undefined;
for (im.object.get("src").?.array.items, &src) |v, *o| o.* = v.integer;
const key = im.object.get("key").?;
try out.append(a, .{ .key = if (key == .string) key.string else try std.fmt.allocPrint(a, "{d}", .{key.integer}), .src = src });
}
return out.items;
}
fn ctlOf(s: *Session, n: u32) ![]const u8 {
return s.read(s.at("/pane/{d}/ctl", .{n}));
}
test "addons: a PDF is the pdf addon's pane, its pages pictures, its page in its tag, its fit and tint in its ctl and stepped by its words, all of it back from a Dump" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
s.graphics();
const n = try s.lookFile("docs/design.pdf");
try std.testing.expect(has(try s.read("/index"), " pdf "));
try std.testing.expect(std.mem.startsWith(u8, try s.read("/addons"), "pdf 1 3.4 1 /"));
const pages = (try s.pdfPage(n))[1];
try std.testing.expectEqual(@as(u32, 16), pages);
// Its first page, a picture over its body.
const first = try shown(&s, n);
try std.testing.expect(first.len >= 1);
// Its tag: its page, then the core's art words (it placed a picture),
// its path and its kind's words, then Del (UX K'3).
const tag = try s.read(s.at("/pane/{d}/tag", .{n}));
try std.testing.expect(std.mem.startsWith(u8, tag, "[1/16] petscii:off palette:commodore ascii:on /"));
try std.testing.expect(has(tag, "/docs/design.pdf PdfSections PdfTint PdfFit Del"));
try std.testing.expect(has(try ctlOf(&s, n), " fit:width tint:filtered mode:normal "));
// The words: bare they step, given a value they take it, any other
// value refused; another pane's is refused and leaves the PDF alone.
const ctl = s.at("/pane/{d}/ctl", .{n});
try s.write(ctl, "PdfFit\n");
try std.testing.expect(has(try ctlOf(&s, n), " fit:height "));
try s.write(ctl, "PdfTint full\n");
try std.testing.expect(has(try ctlOf(&s, n), " tint:full "));
// A tint is a page drawn again: another picture.
const tinted = try shown(&s, n);
try std.testing.expect(tinted.len >= 1 and !std.mem.eql(u8, tinted[0].key, first[0].key));
_ = try s.refused(ctl, "PdfTint loud\n");
try std.testing.expect(has(s.said, "PdfTint takes disabled, filtered or full"));
const text = std.mem.sliceTo(try s.read("/index"), ' ');
_ = try s.refused(s.at("/pane/{s}/ctl", .{text}), "PdfFit\n");
try std.testing.expect(has(try ctlOf(&s, n), " fit:height tint:full "));
// Its last page, then a Dump: restored it is there, as it was.
try s.keys("ge");
try std.testing.expectEqual(@as(u32, 16), (try s.pdfPage(n))[0]);
try s.write("/ctl", "Dump\n");
const label = try s.arena.allocator().dupe(u8, std.mem.sliceTo(try s.read("/dumps"), ' '));
try s.write(ctl, "Del\n");
try s.write("/ctl", s.at("Restore {s}\n", .{label}));
// (A test's session steps only on its writes: a key that moves nothing
// is the step after the Restore a host's wake makes.)
try s.keys("0");
var back: u32 = 0;
var lines = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n');
while (lines.next()) |line| if (has(line, " pdf ")) {
back = try std.fmt.parseInt(u32, std.mem.sliceTo(line, ' '), 10);
};
try std.testing.expect(back != 0);
try std.testing.expectEqual(@as(u32, 16), (try s.pdfPage(back))[0]);
try std.testing.expect(has(try ctlOf(&s, back), " fit:height tint:full "));
try std.testing.expect((try shown(&s, back)).len >= 1);
}
test "addons: a PDF's / lists its hits as file.pdf:P:H rows in a +Search, n walks them, a Look of one turns to its page, Esc drops the search first and then leaves, and a hit not there is a miss" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.lookFile("docs/design.pdf");
try s.keys("/pardes\r");
var rows: []const u8 = "";
var it = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n');
while (it.next()) |line| if (has(line, "+Search")) {
rows = try s.read(s.at("/pane/{s}/body", .{std.mem.sliceTo(line, ' ')}));
};
// (Each row its place, then the text, as a +Search lays its rows out.)
try std.testing.expect(std.mem.startsWith(u8, rows, "design.pdf:1:1\n"));
try std.testing.expect(std.mem.count(u8, rows, "design.pdf:") > 3);
// At its first hit's page; from the PDF n walks its rows (the
// keyboard on the next, selected), and a Look of a row turns there.
try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]);
var search: u32 = 0;
it = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n');
while (it.next()) |line| if (has(line, "+Search")) {
search = try std.fmt.parseInt(u32, std.mem.sliceTo(line, ' '), 10);
};
try s.write("/focus", s.at("{d}\n", .{n}));
try s.keys("n");
try std.testing.expectEqual(search, try s.focused());
var last: []const u8 = "";
var places = std.mem.tokenizeScalar(u8, rows, '\n');
while (places.next()) |row| if (std.mem.startsWith(u8, row, "design.pdf:")) {
last = row;
};
const page = try std.fmt.parseInt(u32, last["design.pdf:".len..std.mem.indexOfScalarPos(u8, last, "design.pdf:".len, ':').?], 10);
try s.write(s.at("/pane/{d}/look", .{search}), s.at("{s}\n", .{last}));
try std.testing.expectEqual(page, (try s.pdfPage(n))[0]);
try std.testing.expectEqual(n, try s.focused());
// A hit there is a place; one not there a miss said with its path.
const look = s.at("/pane/{d}/look", .{n});
try s.write(look, ":1:1\n");
_ = try s.refused(look, ":1:99\n");
try std.testing.expect(has(try s.read("/log"), "design.pdf has no search hit 99 on page 1"));
// Esc: the search goes first (no hit is a place now), then the pane.
try s.keys("\x1b");
try std.testing.expectEqual(n, try s.focused());
_ = try s.refused(look, ":1:1\n");
try s.keys("\x1b");
try std.testing.expect(try s.focused() != n);
}
test "addons: a PDF's / of what its page has not, and f on a PDF with no outline, each say so and the session goes on (the bridge's none is a NULL with a count of 0: a safe build's abort before)" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.lookFile("docs/design.pdf");
// No page has it: page 1's hits of it asked, none; a hit of it a miss.
try s.keys("/zzqxv\r");
try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]);
_ = try s.refused(s.at("/pane/{d}/look", .{n}), ":1:1\n");
try std.testing.expect(has(try s.read("/log"), "design.pdf has no search hit 1 on page 1"));
// tall.pdf has no outline.
const t = try s.lookFile("test/pdf-fixtures/tall.pdf");
try s.keys("f");
try std.testing.expect(has(try s.read("/log"), "PdfSections: this PDF has no outline"));
try std.testing.expectEqual(t, try s.focused());
}
test "addons: a PDF's f is its PdfSections, its outline as places a Look in it goes to (none once the PDF is gone), and a jump back over a far one is Ctrl-o's, Forward's on, two places on a page a window apart two" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
s.graphics();
const n = try s.lookFile("test/pdf-fixtures/outline.pdf");
try s.keys("f");
var list: u32 = 0;
var it = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n');
while (it.next()) |line| if (has(line, "/+PdfSections")) {
list = try std.fmt.parseInt(u32, std.mem.sliceTo(line, ' '), 10);
};
try std.testing.expect(list != 0);
const body = try s.read(s.at("/pane/{d}/body", .{list}));
try std.testing.expect(std.mem.startsWith(u8, body, "outline.pdf:"));
// The last row of a section in it: its page, where a Look lands.
var last: []const u8 = "";
var rows = std.mem.tokenizeScalar(u8, body, '\n');
while (rows.next()) |row| if (std.mem.startsWith(u8, row, "outline.pdf:")) {
last = row;
};
const place = last[0..std.mem.indexOfScalar(u8, last, ' ').?];
var fields = std.mem.splitScalar(u8, place, ':');
_ = fields.next();
const page = try std.fmt.parseInt(u32, fields.next().?, 10);
try s.write(s.at("/pane/{d}/look", .{list}), s.at("{s}\n", .{place}));
try std.testing.expectEqual(page, (try s.pdfPage(n))[0]);
// A jump far: Ctrl-o goes back, Ctrl-i on.
try s.write(s.at("/pane/{d}/look", .{n}), ":1\n");
try s.keys("ge");
const end = (try s.pdfPage(n))[0];
try s.keys("\x0f");
try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]);
// (Ctrl-i is Tab without kitty's keyboard: its word, Forward.)
try s.write("/ctl", "Forward\n");
try std.testing.expectEqual(end, (try s.pdfPage(n))[0]);
// Two places on page 1 more than a window apart (the second its 0.8,
// as its `where` says a place) are two visits: Ctrl-o from a far one
// goes to the second, then the first, where they were.
const look = s.at("/pane/{d}/look", .{n});
try s.write(look, ":1\n");
const top = (try shown(&s, n))[0].src[1];
try s.write(look, ":1:0.8\n");
try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]);
const down = (try shown(&s, n))[0].src[1];
try std.testing.expect(down > top);
try s.write(look, ":3\n");
try s.keys("\x0f");
try std.testing.expectEqual(down, (try shown(&s, n))[0].src[1]);
try s.keys("\x0f");
try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]);
try std.testing.expectEqual(top, (try shown(&s, n))[0].src[1]);
// The PDF gone, its list's rows are no place: not another pane's.
try s.write(s.at("/pane/{d}/ctl", .{n}), "Del\n");
_ = try s.refused(s.at("/pane/{d}/look", .{list}), s.at("{s}\n", .{place}));
try std.testing.expect(has(try s.read("/log"), "the pane its list is of is gone"));
}
test "addons: a closed PDF is in Recent and its jumps, and a jump back to it opens it at its page; a tall page is shown a band of it at a time" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
s.graphics();
const n = try s.lookFile("docs/design.pdf");
try s.write(s.at("/pane/{d}/look", .{n}), ":5\n");
const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n");
// From a scratch, Back is the nearest place of any file: the PDF's,
// closed meanwhile, opened again at its page.
const scratch = try s.newPane();
try s.write("/focus", s.at("{d}\n", .{scratch}));
try s.write(s.at("/pane/{d}/ctl", .{n}), "Del\n");
try std.testing.expect(has(try s.read("/recent"), s.at("closed {s}", .{name})));
try s.keys("\x0f");
const again = try s.focused();
try std.testing.expect(again != scratch);
try std.testing.expectEqual(@as(u32, 5), (try s.pdfPage(again))[0]);
// A page 9000 points tall: a band of its rows, never the whole.
const tall = try s.lookFile("test/pdf-fixtures/tall.pdf");
const bands = try shown(&s, tall);
try std.testing.expect(bands.len >= 1);
try std.testing.expect(bands[0].src[3] <= 8192);
}
test "addons: a PDF's body is its page's text layer, read from any offset and never written; a truncated PDF is a refused Look saying why" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.lookFile("test/pdf-fixtures/links.pdf");
const body = s.at("/pane/{d}/body", .{n});
const whole = try s.arena.allocator().dupe(u8, try s.read(body));
try std.testing.expect(has(whole, "target.txt"));
// (Read from an offset, as a mount's chunked reads ask.)
try std.testing.expectEqualStrings(whole[3..], try s.readAt(body, 3, 4096));
try std.testing.expect(try s.refused(body, "more\n") != 0);
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const bytes = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "test/pdf-fixtures/links.pdf", s.arena.allocator(), .unlimited);
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "cut.pdf", .data = bytes[0..40] });
var buf: [4096]u8 = undefined;
const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
_ = try s.refused("/look", s.at("{s}/cut.pdf\n", .{dir}));
try std.testing.expect(has(s.said, "cut.pdf") and has(s.said, "not a PDF pardes can read"));
}
test "addons: a PDF whose file changes on disk shows its new pages, under new keys: a window keeps a texture by its key" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
s.graphics();
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var buf: [4096]u8 = undefined;
const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
const a = s.arena.allocator();
const links = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "test/pdf-fixtures/links.pdf", a, .unlimited);
const outline = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "test/pdf-fixtures/outline.pdf", a, .unlimited);
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "doc.pdf", .data = links });
const n = try s.lookFile(s.at("{s}/doc.pdf", .{dir}));
const first = try shown(&s, n);
try std.testing.expect(first.len >= 1);
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "doc.pdf", .data = outline });
try std.testing.expect(try s.filesChanged());
// Its pages laid out at the step after (the host's next).
try s.keys("0");
const now = try shown(&s, n);
try std.testing.expect(now.len >= 1);
for (now) |pic| for (first) |old| try std.testing.expect(!std.mem.eql(u8, pic.key, old.key));
}
test "addons: a PDF whose file changes on disk while a page of it renders: the pixels go with the document they are of, once" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
s.graphics();
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var buf: [4096]u8 = undefined;
const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
const a = s.arena.allocator();
const links = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "test/pdf-fixtures/links.pdf", a, .unlimited);
const outline = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "test/pdf-fixtures/outline.pdf", a, .unlimited);
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "doc.pdf", .data = links });
const n = try s.lookFile(s.at("{s}/doc.pdf", .{dir}));
try std.testing.expect((try shown(&s, n)).len >= 1);
// A page drawn again (another tint), its render done on a worker but
// not yet answered as the file changes and the pane opens it again.
s.holdJobs();
try s.write(s.at("/pane/{d}/ctl", .{n}), "PdfTint full\n");
_ = try shown(&s, n);
s.runJobs();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "doc.pdf", .data = outline });
try std.testing.expect(try s.filesChanged());
s.releaseJobs();
try s.keys("0");
try std.testing.expect((try shown(&s, n)).len >= 1);
}
test "addons: a PDF under a picker's cursor shows through the pdf addon in the pane it lands in, never its bytes as text, and Esc puts the pane's own text back" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
s.graphics();
try s.loadPdf();
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const pdf = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "docs/design.pdf", s.arena.allocator(), .limited(4 << 20));
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "paper.pdf", .data = pdf });
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" });
var buf: [4096]u8 = undefined;
const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
const pane = try s.lookFile(s.at("{s}/notes.txt", .{dir}));
try s.keys(" ffpaper");
// (Its kind told its view at the frame after the one that opened it.)
s.tick();
s.tick();
const screen = try s.screen();
try std.testing.expect(!has(screen, "%PDF") and !has(screen, "the notes"));
// Its pages pictures in that pane's body, as a PDF's pane shows them.
try std.testing.expect((try shown(&s, pane)).len > 0);
try s.keys("\x1b");
s.tick();
try s.expect(s.at("/pane/{d}/body", .{pane}), "the notes\n");
try std.testing.expectEqual(0, (try shown(&s, pane)).len);
try std.testing.expect(has(try s.screen(), "the notes"));
}
|