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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
|
const std = @import("std");
const pardes = @import("pardes");
const panes = pardes.panes;
const config = pardes.config;
const builtins = pardes.builtins;
const modal = pardes.modal;
const Pardes = pardes.Pardes;
const Key = pardes.Key;
const platform = pardes.platform;
const font_picker = pardes.font_picker;
const fonts = if (font_picker) pardes.fonts else struct {};
// Sorting tests compare original locations and source bytes independently of
// alignment. Dedicated Locations tests verify the rendered column widths.
fn expectResultRows(expected: []const u8, file: *const panes.File.State) !void {
if (file.location_rows.len == 0) return std.testing.expectEqualStrings(expected, file.content);
try std.testing.expectEqual(std.mem.endsWith(u8, expected, "\n"), std.mem.endsWith(u8, file.content, "\n"));
var source = std.mem.splitScalar(u8, expected, '\n');
var shown = std.mem.splitScalar(u8, file.content, '\n');
for (file.location_rows) |row| {
const want = source.next() orelse return error.MissingExpectedResult;
const actual = shown.next() orelse return error.MissingRenderedResult;
const target = panes.Output.location(want);
try std.testing.expectEqualStrings(want[0..target.end], actual[0..row.location_end]);
try std.testing.expectEqualStrings(want[@min(want.len, target.end + 1)..], actual[row.code_start..]);
}
try std.testing.expectEqualStrings("", source.next() orelse "");
try std.testing.expect(source.next() == null);
}
test "search tint survives wrapping and theme changes while selections remain authoritative" {
const text = "nested/path/a.txt:1:5-7 let abc = 1;\n";
for ([_]u16{ 30, 80 }) |width| {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = width, .rows = 12 });
defer p.deinit();
const pane = try p.setTestFile(text);
pane.file.?.output = .{ .from = .search };
var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
defer frame.deinit();
for ([_]bool{ false, true }) |bottom| {
p.settings.tag_bottom = bottom;
for ([_]bool{ true, false, true }) |colors| {
p.settings.colors = colors;
const surface = try p.render(frame.allocator());
var marked: usize = 0;
for (surface.cells) |cell| {
if (std.meta.eql(cell.style.bg, pardes.Color{ .rgb = p.theme().search_bg.? })) marked += 1;
}
try std.testing.expectEqual(@as(usize, if (colors) 3 else 0), marked);
_ = frame.reset(.retain_capacity);
}
}
// The body changes immediately even when dark chrome is still fading.
try std.testing.expect(p.executeBuiltinLine(0, "Theme daybreak"));
const light = try p.render(frame.allocator());
var light_matches: usize = 0;
for (light.cells) |cell| {
if (!std.meta.eql(cell.style.bg, pardes.Color{ .rgb = p.theme().search_bg.? })) continue;
try std.testing.expectEqualDeep(pardes.Color{ .rgb = p.theme().search_fg.? }, cell.style.fg);
light_matches += 1;
}
try std.testing.expectEqual(@as(usize, 3), light_matches);
pane.mode = .normal;
pane.cur_row = 0;
pane.cur_col = @intCast(std.mem.indexOf(u8, text, "abc").? + 2);
pane.vsel = .{ .active = true, .row = 0, .col = pane.cur_col - 2, .explicit = true };
const selected = try p.render(frame.allocator());
for (selected.cells) |cell|
try std.testing.expect(!std.meta.eql(cell.style.bg, pardes.Color{ .rgb = p.theme().search_bg.? }));
try std.testing.expectEqualStrings(text, pane.file.?.content);
}
}
test "location results sort filenames and numeric positions while remapping their anchor" {
const before = "z.zig:1:1 z\nb.zig:10:2-3 ten\nb.zig:2:10-12 two-b\nb.zig:2:2-3 two-a\na.zig:99:1 a\nb.zig:2:2-3 duplicate\n";
const after = "a.zig:99:1 a\nb.zig:2:2-3 two-a\nb.zig:2:2-3 duplicate\nb.zig:2:10-12 two-b\nb.zig:10:2-3 ten\nz.zig:1:1 z\n";
for ([_]panes.Output.Origin{ .search, .{ .cmd = .Grep }, .{ .query = .references }, .{ .query = .diagnostics } }) |origin| {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
const source = try p.setTestFile("notes\n");
try panes.Output.fillResults(p, 0, "/tmp", origin, "", try p.gpa.dupe(u8, before), 2);
const results = p.panes[source.search_pane.?].?;
try expectResultRows(after, &results.file.?);
try std.testing.expectEqual(@as(usize, 3), source.search_row.?);
}
}
test "ranked choices and prose retain their supplied order" {
const text = "z.zig:10:1 first\na.zig:2:1 second\n";
for ([_]pardes.lsp.Kind{ .completion, .hover, .code_action }) |kind| {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
_ = try p.setTestFile("notes\n");
try panes.Output.fillResults(p, 0, "/tmp", .{ .query = kind }, "", try p.gpa.dupe(u8, text), null);
const result = for (p.panes) |slot| {
const pane = slot orelse continue;
const file = pane.file orelse continue;
if (file.output != null) break file;
} else return error.MissingResults;
try std.testing.expectEqualStrings(text, result.content);
}
}
test "reference Look selects the next sorted row from the captured request and keeps navigation there" {
if (!pardes.lsp.supports.contains(.references)) return error.SkipZigTest;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "m.zig", .data = "const item = 0;\n_ = item;\n_ = item;\n_ = item;\n_ = item;\n" });
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.zig", .data = "a\n" });
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "z.zig", .data = "z\n" });
var path_buf: [256]u8 = undefined;
const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/m.zig", .{tmp.sub_path});
const p = try Pardes.init(std.testing.allocator, .{ .file = path, .cols = 80, .rows = 24 });
defer p.deinit();
const source = p.panes[0].?;
const response = "m.zig:5:5-8 later\nz.zig:1:1 last-file\nm.zig:1:7-10 earlier\nm.zig:3:5-8 current\na.zig:1:1 first-file\nm.zig:4:5-8 next\n";
const sorted = "a.zig:1:1 first-file\nm.zig:1:7-10 earlier\nm.zig:3:5-8 current\nm.zig:4:5-8 next\nm.zig:5:5-8 later\nz.zig:1:1 last-file\n";
source.cur_row = 2;
source.cur_col = 4;
p.lspRequest(0, .references, "");
const request = p.lsp_wait.?.id;
source.cur_row = 4;
p.lspResponse(request, response);
const rid = source.search_pane orelse return error.MissingResults;
const results = p.panes[rid].?;
try expectResultRows(sorted, &results.file.?);
try std.testing.expectEqual(@as(usize, 0), p.active);
try std.testing.expectEqual(@as(i32, 3), source.cur_row);
try std.testing.expect(source.vsel.active);
try std.testing.expectEqual(@as(i32, 4), @min(source.cur_col, source.vsel.col));
try std.testing.expectEqual(@as(i32, 7), @max(source.cur_col, source.vsel.col));
try std.testing.expectEqual(@as(i32, 3), results.cur_row);
try std.testing.expectEqual(@as(usize, 3), source.search_row.?);
try std.testing.expectEqual(results.serial, p.look_walk_owner.?);
try std.testing.expectEqual(@as(i32, 3), results.look_at.?.row);
const available = p.freeSlot();
p.update(.{ .key = .{ .cp = 'n' } });
try std.testing.expectEqual(rid, p.active);
try std.testing.expectEqual(@as(i32, 4), results.cur_row);
try std.testing.expectEqual(available, p.freeSlot());
p.update(.{ .key = .{ .cp = 'N' } });
try std.testing.expectEqual(@as(i32, 3), results.cur_row);
p.active = 0;
source.cur_row = 4;
source.cur_col = 4;
p.lspRequest(0, .references, "");
p.lspResponse(p.lsp_wait.?.id, response);
try std.testing.expect(std.mem.endsWith(u8, p.panes[p.active].?.file.?.path, "/z.zig"));
try std.testing.expectEqual(@as(i32, 5), results.cur_row);
try std.testing.expectEqual(@as(usize, 5), source.search_row.?);
const last = p.active;
p.lspRequest(last, .references, "");
p.lspResponse(p.lsp_wait.?.id, response);
try std.testing.expect(std.mem.endsWith(u8, p.panes[p.active].?.file.?.path, "/a.zig"));
const wrapped = p.panes[p.panes[last].?.search_pane.?].?;
try std.testing.expectEqual(@as(i32, 0), wrapped.cur_row);
try std.testing.expectEqual(wrapped.serial, p.look_walk_owner.?);
}
test "location sorting leaves mixed prose alone and preserves newline shape" {
const cases = [_]struct { before: []const u8, after: []const u8, anchor: usize }{
.{
.before = "heading\nz.zig:10:1 z\nnot a location\nx file.zig:2:10 x\nx file.zig:2:2 y\n\n",
.after = "heading\nz.zig:10:1 z\nnot a location\nx file.zig:2:10 x\nx file.zig:2:2 y\n\n",
.anchor = 1,
},
.{
.before = "z.zig:10:1 z\nx file.zig:2:10 x\nx file.zig:2:2 y",
.after = "x file.zig:2:2 y\nx file.zig:2:10 x\nz.zig:10:1 z",
.anchor = 1,
},
};
for (cases) |case| {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
const source = try p.setTestFile("notes\n");
try panes.Output.fillResults(p, 0, "/tmp", .search, "", try p.gpa.dupe(u8, case.before), 1);
try expectResultRows(case.after, &p.panes[source.search_pane.?].?.file.?);
try std.testing.expectEqual(case.anchor, source.search_row.?);
}
}
test "a single language location opens the full spaced filename" {
if (!pardes.lsp.supports.contains(.references)) return error.SkipZigTest;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "from.zig", .data = "const item = 0;\n" });
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "with space.zig", .data = "first\nsecond\n" });
var path_buf: [256]u8 = undefined;
const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/from.zig", .{tmp.sub_path});
const p = try Pardes.init(std.testing.allocator, .{ .file = path });
defer p.deinit();
p.lspRequest(0, .references, "");
p.lspResponse(p.lsp_wait.?.id, "with space.zig:2:1 second\n");
try std.testing.expect(std.mem.endsWith(u8, p.panes[p.active].?.file.?.path, "/with space.zig"));
try std.testing.expectEqual(@as(i32, 1), p.panes[p.active].?.cur_row);
for (p.panes) |slot| {
const pane = slot orelse continue;
if (pane.file) |file| try std.testing.expect(file.output == null);
}
}
test "result locations stop before preview text and retain full addresses" {
const cases = [_]struct { row: []const u8, path: []const u8, line: usize, col: usize, address: []const u8 }{
.{ .row = "a.zig:2 see other.zig:10:1", .path = "a.zig", .line = 2, .col = 0, .address = "a.zig:2" },
.{ .row = "a.zig:2:10-12 see other.zig:1:1", .path = "a.zig", .line = 2, .col = 10, .address = "a.zig:2:10-12" },
.{ .row = "with space.zig:100:2 café", .path = "with space.zig", .line = 100, .col = 2, .address = "with space.zig:100:2" },
.{ .row = "@p3:20:1 pane", .path = "@p3", .line = 20, .col = 1, .address = "@p3:20:1" },
.{ .row = "/n/peer/file.zig:5-8 span", .path = "/n/peer/file.zig", .line = 5, .col = 0, .address = "/n/peer/file.zig:5-8" },
};
for (cases) |case| {
const got = panes.Output.location(case.row);
try std.testing.expectEqualStrings(case.path, got.path);
try std.testing.expectEqual(case.line, got.at.line);
try std.testing.expectEqual(case.col, got.at.col);
try std.testing.expectEqualStrings(case.address, case.row[0..got.end]);
}
}
test "reordered identical results preserve the selected row and warm line index" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
const source = try p.setTestFile("notes\n");
const sorted = "a.zig:2:1 first\na.zig:10:1 second\n";
try panes.Output.fillResults(p, 0, "/tmp", .{ .query = .references }, "", try p.gpa.dupe(u8, sorted), null);
const result = p.panes[source.search_pane.?].?;
const file = &result.file.?;
const index = try panes.File.lineIndex(p.gpa, file);
const content = file.content.ptr;
const revision = file.revision;
result.cur_row = 1;
result.cur_col = 0;
result.vsel = .{ .active = true, .explicit = true, .row = 1, .col = 9 };
result.select = true;
const selected = result.vsel;
try panes.Output.fillResults(p, 0, "/tmp", .{ .query = .references }, "new request", try p.gpa.dupe(u8, "a.zig:10:1 second\na.zig:2:1 first\n"), 0);
try expectResultRows(sorted, file);
try std.testing.expectEqual(content, file.content.ptr);
try std.testing.expectEqual(index.ptr, file.line_starts.ptr);
try std.testing.expectEqual(revision, file.revision);
try std.testing.expectEqual(selected, result.vsel);
try std.testing.expect(result.select);
try std.testing.expectEqual(@as(i32, 1), result.cur_row);
try std.testing.expectEqual(@as(usize, 1), source.search_row.?);
}
test "result sorting allocation failures preserve the previous answer and navigation" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
const source = try p.setTestFile("notes\n");
try panes.Output.fillResults(p, 0, "/tmp", .{ .query = .references }, "before", try p.gpa.dupe(u8, "a.zig:1:1 keep\n"), 0);
const id = source.search_pane.?;
const result = p.panes[id].?;
const content = result.file.?.content.ptr;
const revision = result.file.?.revision;
const owner = p.look_walk_owner;
var preview: [4096]u8 = @splat('x');
for (0..2) |failure| {
var failing = std.testing.FailingAllocator.init(p.gpa, .{ .fail_index = failure });
const original_scratch = p.scratch;
p.scratch = .init(failing.allocator());
defer {
p.scratch.deinit();
p.scratch = original_scratch;
}
const incoming = try std.fmt.allocPrint(p.gpa, "z.zig:2:1 {s}\na.zig:1:1 {s}\n", .{ &preview, &preview });
try std.testing.expectError(error.OutOfMemory, panes.Output.fillResults(p, 0, "/tmp", .{ .query = .references }, "after", incoming, 1));
try std.testing.expect(failing.has_induced_failure);
try std.testing.expectEqual(content, result.file.?.content.ptr);
try std.testing.expectEqual(revision, result.file.?.revision);
try std.testing.expectEqualStrings("before", result.file.?.output.?.arg());
try std.testing.expectEqual(id, source.search_pane.?);
try std.testing.expectEqual(@as(usize, 0), source.search_row.?);
try std.testing.expectEqual(@as(usize, 0), p.active);
try std.testing.expectEqual(owner, p.look_walk_owner);
}
}
test "Find sorts whole filenames and next location uses numeric columns" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
const source = try p.setTestFile("");
try panes.Output.fillResults(p, 0, "/tmp", .{ .cmd = .Find }, "", try p.gpa.dupe(u8, "x:2\nx:10\n"), 0);
const result = p.panes[source.search_pane.?].?;
try std.testing.expectEqualStrings("x:10\nx:2\n", result.file.?.content);
try std.testing.expectEqual(@as(usize, 1), source.search_row.?);
const rows = "with space.zig:2:2-3 first\nwith space.zig:2:10-12 next\nwith space.zig:10:1 later";
try std.testing.expectEqual(@as(usize, 1), panes.Output.nextResult(rows, "with space.zig", .{ .line = 2, .col = 3 }));
try std.testing.expectEqual(@as(usize, 2), panes.Output.nextResult(rows, "with space.zig", .{ .line = 2, .col = 10 }));
try std.testing.expectEqual(@as(usize, 0), panes.Output.nextResult(rows, "with space.zig", .{ .line = 10, .col = 1 }));
}
test "result stepping reaches an unterminated last row" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
const source = try p.setTestFile("one\ntwo\n");
const rows = try std.fmt.allocPrint(p.gpa, "{s}:1:1 first\n{s}:2:1 last", .{ source.file.?.path, source.file.?.path });
try panes.Output.fillResults(p, 0, "/tmp", .search, "", rows, 0);
try std.testing.expect(pardes.test_api.searchStep(p, 0, 1));
try std.testing.expectEqual(@as(usize, 1), source.search_row.?);
const result = p.panes[source.search_pane.?].?;
try std.testing.expectEqual(@as(i32, 1), result.cur_row);
}
test "Mini uses OS source precedence and refreshes one independent snapshot" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "build.zig", .data = "x\n" });
var dir_buf: [4096]u8 = undefined;
const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
try p.panes[0].?.setOwnedCwd(dir);
try std.testing.expect(p.executeBuiltinLine(0, "Mini build.zig"));
const id = p.active;
try std.testing.expect(id != 0);
const mini = p.panes[id].?;
try std.testing.expect(mini.terminal == null);
try std.testing.expectEqualStrings("⠁\n", mini.file.?.content);
try std.testing.expect(std.mem.startsWith(u8, mini.file.?.mini.?.source, dir));
const next = p.freeSlot();
const body = mini.file.?.content.ptr;
try std.testing.expect(p.executeBuiltinLine(id, "Mini build.zig"));
try std.testing.expectEqual(id, p.active);
try std.testing.expectEqual(body, mini.file.?.content.ptr);
try std.testing.expectEqual(next, p.freeSlot());
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "build.zig", .data = "xx\n" });
try std.testing.expectEqualStrings("⠁\n", mini.file.?.content);
try std.testing.expect(p.executeBuiltinLine(id, "Mini build.zig"));
try std.testing.expectEqual(id, p.active);
try std.testing.expectEqualStrings("⠉\n", mini.file.?.content);
while (p.nextEffect()) |effect| switch (effect) {
.watch => |watch| try std.testing.expect(watch.pane != id),
else => {},
};
try std.testing.expect(p.executeBuiltinLine(id, "Save"));
try std.testing.expect(mini.prompt == .save);
}
test "Mini dump preserves its exact virtual snapshot and body writes detach colors" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
const source = try p.setTestFile("pub fn start() void {}\n");
var command_buf: [128]u8 = undefined;
const command = try std.fmt.bufPrint(&command_buf, "Mini /virtual/pane/{d}/body", .{source.serial});
try std.testing.expect(p.executeBuiltinLine(0, command));
const id = p.active;
const before = p.panes[id].?.file.?;
try std.testing.expect(before.mini != null);
try p.dumpState();
panes.File.setContent(p, &source.file.?, try p.gpa.dupe(u8, "changed\n"));
const restored = try Pardes.initFromDump(std.testing.allocator, .{ .tty_only = true }, p.dump_out.?);
defer restored.deinit();
const mini = restored.panes[restored.active].?;
try std.testing.expectEqualStrings(before.content, mini.file.?.content);
try std.testing.expectEqualStrings(before.mini.?.source, mini.file.?.mini.?.source);
try std.testing.expectEqualSlices(u8, before.mini.?.colors, mini.file.?.mini.?.colors);
try std.testing.expect(before.content.ptr != mini.file.?.content.ptr);
while (restored.nextEffect()) |effect| switch (effect) {
.watch => |watch| try std.testing.expect(watch.pane != restored.active),
else => {},
};
var body_buf: [128]u8 = undefined;
const body_path = try std.fmt.bufPrint(&body_buf, "/n/self/pane/{d}/body", .{mini.serial});
const body = try pardes.filesystem.read(restored, body_path);
defer restored.gpa.free(body);
try std.testing.expectEqualStrings(before.content, body);
try pardes.filesystem.write(restored, body_path, "replacement\n");
try std.testing.expectEqualStrings("replacement\n", mini.file.?.content);
try std.testing.expect(mini.file.?.mini == null);
}
test "Mini syntax colors survive toggles themes and rendering without source access" {
if (!pardes.syntax.enabled) return error.SkipZigTest;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "demo.zig", .data = "pub fn main() void {\n if (true) return;\n}\n" });
var dir_buf: [4096]u8 = undefined;
const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 });
defer p.deinit();
try p.panes[0].?.setOwnedCwd(dir);
p.settings.colors = false;
try panes.Mini.open(p, 0, "demo.zig");
const id = p.active;
const mini = p.panes[id].?;
const keyword = std.mem.indexOfScalar(u8, mini.file.?.mini.?.colors, @intFromEnum(pardes.syntax.Syn.keyword)) orelse return error.MissingKeywordColor;
// Direct pane construction bypasses the host update's layout sync.
p.update(.{ .resize = .{ .cols = 80, .rows = 24 } });
const position = modal.positionAt(mini.file.?.content, keyword);
const column = panes.File.displayWidth(modal.lineSlice(mini.file.?.content, position.row)[0..position.col]);
try tmp.dir.deleteFile(std.testing.io, "demo.zig");
const colors = mini.file.?.mini.?.colors.ptr;
var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
defer frame.deinit();
for ([_]bool{ false, true, false, true }) |enabled| {
p.settings.colors = enabled;
mini.file.?.syntax_dirty = true;
const surface = try p.render(frame.allocator());
const rect = p.rects[id];
const x = rect.x + config.GUTTER + panes.File.gutterWidth(mini) + @as(u16, @intCast(column));
const y = (if (p.settings.tag_bottom) rect.y else rect.y + pardes.BOX_H) + @as(u16, @intCast(position.row));
if (enabled) try std.testing.expectEqualDeep(pardes.Color{ .rgb = p.theme().kw }, surface.at(x, y).style.fg);
try std.testing.expectEqual(colors, mini.file.?.mini.?.colors.ptr);
try std.testing.expectEqual(@as(usize, 0), mini.file.?.highlights.len);
_ = frame.reset(.retain_capacity);
try std.testing.expect(p.executeBuiltinLine(id, "NextColor"));
}
}
test "Mini restore rejects inconsistent metadata before publishing a pane" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
const id = p.freeSlot().?;
const serial = p.next_serial;
for ([_]pardes.dump.File{
.{ .content = "x", .mini_source = "/source.zig", .mini_colors_b64 = "AA==" },
.{ .content = "x", .origin = "Mini", .mini_source = "/source.zig", .mini_colors_b64 = "" },
.{ .content = "x", .origin = "Mini", .mini_source = "/source.zig", .mini_colors_b64 = "BQ==" },
.{ .content = "x", .origin = "Mini", .mini_colors_b64 = "AA==" },
}) |saved| {
try std.testing.expectError(error.InvalidMini, panes.File.restore(p, id, .{ .kind = .file, .tag = "", .body = "", .file = saved }));
try std.testing.expect(p.panes[id] == null);
try std.testing.expectEqual(serial, p.next_serial);
}
}
test "Mini failures leave its existing snapshot and focus unchanged" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
const source = try p.setTestFile("abc\n");
var path_buf: [128]u8 = undefined;
const path = try std.fmt.bufPrint(&path_buf, "/virtual/pane/{d}/body", .{source.serial});
try panes.Mini.open(p, 0, path);
const id = p.active;
const mini = p.panes[id].?;
const body = mini.file.?.content.ptr;
const next = p.freeSlot();
try std.testing.expectError(error.FileNotFound, panes.Mini.open(p, 0, "/virtual/does-not-exist"));
try std.testing.expectEqual(id, p.active);
try std.testing.expectEqual(body, mini.file.?.content.ptr);
try std.testing.expectEqual(next, p.freeSlot());
}
test "Config prints the startup path and refreshes its one output" {
const path = "/home/pardes-test/.config/pardes/init";
const p = try Pardes.init(std.testing.allocator, .{ .startup_config_path = path });
defer p.deinit();
try std.testing.expect(p.executeBuiltinLine(0, "Config"));
const opened = p.active;
const out = p.panes[opened].?.file.?;
for ([_][]const u8{
"Startup config: " ++ path ++ "\n",
"Theme: orchard\n",
"Shell requested (new panes): " ++ config.default_shell ++ " (default)\n",
"Shell effective (last spawn): (none)\n",
"Shell pending: on\n",
}) |line| try std.testing.expect(std.mem.indexOf(u8, out.content, line) != null);
try std.testing.expectEqualStrings(config.config_buffer, std.fs.path.basename(out.path));
try std.testing.expectEqual(panes.Output.Origin{ .cmd = .Config }, out.output.?.from);
try std.testing.expect(p.executeBuiltinLine(0, "Config"));
try std.testing.expectEqual(opened, p.active);
var buffers: usize = 0;
for (p.panes) |slot| {
const f = (slot orelse continue).file orelse continue;
const origin = (f.output orelse continue).from;
buffers += @intFromBool(std.meta.eql(origin, panes.Output.Origin{ .cmd = .Config }));
}
try std.testing.expectEqual(@as(usize, 1), buffers);
}
test "Config reports the absence of a per-user config path" {
const p = try Pardes.init(std.testing.allocator, .{});
defer p.deinit();
try std.testing.expect(p.executeBuiltinLine(0, "Config"));
const report = p.panes[p.active].?.file.?.content;
try std.testing.expect(std.mem.indexOf(u8, report, "no per-user config path") != null);
}
test "EffectCode lists readable virtual sources used by this backend" {
if (comptime !builtins.EffectCode.enabled) return;
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
try std.testing.expect(p.executeBuiltinLine(0, "EffectCode PanelSlide"));
const out = p.panes[p.active].?.file.?;
try std.testing.expectEqualStrings(config.effect_code_buffer, std.fs.path.basename(out.path));
try std.testing.expectEqual(panes.Output.Origin{ .cmd = .EffectCode }, out.output.?.from);
try std.testing.expectEqualStrings("PanelSlide", out.output.?.arg());
try std.testing.expect(out.content.len < 1024);
try std.testing.expect(std.mem.indexOf(u8, out.content, "/virtual/src/layout.zig\n") != null);
const backend_source = switch (platform) {
.tty => "/virtual/src/tty/panel_compositor.zig",
.gui => if (@import("pardes_config").gui_shader_sources_prebuilt)
"/virtual/shaders/prebuilt/ui.vert.glsl"
else
"/virtual/shaders/ui.vert.glsl",
.macos => "/virtual/src/macos/Sources/ScenePostprocessor.swift",
.web, .esp32p4 => unreachable,
};
try std.testing.expect(std.mem.indexOf(u8, out.content, backend_source) != null);
var paths = std.mem.tokenizeScalar(u8, out.content, '\n');
var count: usize = 0;
while (paths.next()) |path| {
if (!std.mem.startsWith(u8, path, "/virtual/")) continue;
const expected = pardes.filesystem.sourceBytes(path["/virtual/".len..]) orelse return error.MissingEffectSource;
const bytes = try pardes.filesystem.read(p, path);
defer p.gpa.free(bytes);
try std.testing.expectEqualStrings(expected, bytes);
count += 1;
}
try std.testing.expect(count >= 3);
const report = p.active;
p.lookAt(report, backend_source);
try std.testing.expect(p.active != report);
const source = p.panes[p.active].?.file.?;
try std.testing.expectEqualStrings(backend_source, source.path);
try std.testing.expectEqualStrings(pardes.filesystem.sourceBytes(backend_source["/virtual/".len..]).?, source.content);
}
test "every enabled setting builtin mutates the State Config reports" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
try std.testing.expectEqual(config.gui_tagline_font_percent, p.settings.font.tagline_percent);
var font_arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer font_arena.deinit();
var chosen_font: ?[]const u8 = null;
var buf: [512]u8 = undefined;
for (config.Runtime.settings) |setting| {
if (!setting.enabled(builtins.capabilities)) continue;
const command: []const u8 = switch (setting.action) {
.theme => try std.fmt.bufPrint(&buf, "{s} acme", .{setting.word}),
.shell => try std.fmt.bufPrint(&buf, "{s} fish", .{setting.word}),
.tagline_size => try std.fmt.bufPrint(&buf, "{s} 67", .{setting.word}),
.font => continue,
else => setting.word,
};
try std.testing.expect(p.executeBuiltinLine(p.active, command));
}
if (comptime font_picker) {
const before_invalid = p.settings.font.tagline_percent;
try std.testing.expect(p.executeBuiltinLine(p.active, "TaglineSize 0"));
try std.testing.expectEqual(before_invalid, p.settings.font.tagline_percent);
const installed = fonts.list(font_arena.allocator(), null);
if (installed.len > 0) {
chosen_font = installed[0].name;
const command = try std.fmt.bufPrint(&buf, "Font {s}", .{installed[0].name});
try std.testing.expect(p.executeBuiltinLine(p.active, command));
}
}
try std.testing.expect(p.executeBuiltinLine(p.active, "Config"));
const report = p.panes[p.active].?.file.?.content;
for ([_][]const u8{
"Colors: off\n",
"Wrap: off\n",
"Tagbottom: on\n",
"Debug: on\n",
"Theme: acme\n",
"Shell requested (new panes): fish\n",
}) |line| try std.testing.expect(std.mem.indexOf(u8, report, line) != null);
const transition = if (builtins.capabilities.panel_transitions)
try std.fmt.bufPrint(&buf, "Panel transition: {s}\n", .{
config.Runtime.findAction(.{ .transition = p.settings.panel_transition }).?.word,
})
else
"Panel transition: unsupported\n";
try std.testing.expect(std.mem.indexOf(u8, report, transition) != null);
const scene_status = if (builtins.capabilities.scene_shaders) "on" else "unsupported";
for ([_][]const u8{ "Crt", "Ripple", "Glitch" }) |name| {
const line = try std.fmt.bufPrint(&buf, "{s}: {s}\n", .{ name, scene_status });
try std.testing.expect(std.mem.indexOf(u8, report, line) != null);
}
const tagline = if (!builtins.capabilities.tagline_font_size)
"TaglineSize: unsupported\n"
else
try std.fmt.bufPrint(&buf, "TaglineSize: {d}%{s}\n", .{
p.settings.font.tagline_percent,
if (builtins.capabilities.font_picker) "" else " (build-time only)",
});
try std.testing.expect(std.mem.indexOf(u8, report, tagline) != null);
if (chosen_font) |name| {
try std.testing.expect(std.mem.indexOf(u8, report, name) != null);
try std.testing.expect(std.mem.indexOf(u8, report, "Font pending: on\n") != null);
}
}
fn walkFixture(p: *Pardes, id: usize, cwd: []const u8, pattern: []const u8) !usize {
const rows = try p.gpa.dupe(u8,
\\build.zig:1:1 first
\\(mise.toml) and build.zig.zon:3:2-9 two on one row
\\nothing look-able on this row at all
\\uucode_config.zig:7:1 last
\\
);
try panes.Output.fillResults(p, id, cwd, .search, pattern, rows, null);
return p.panes[id].?.search_pane orelse error.MissingResults;
}
const ProjectPaths = struct { cwd: []const u8, boot: []const u8 };
fn projectPaths(cwd_buf: *[4096]u8, path_buf: *[4096]u8) !ProjectPaths {
const raw = std.c.getcwd(cwd_buf, cwd_buf.len) orelse return error.GetCwdFailed;
const cwd = std.mem.span(@as([*:0]u8, @ptrCast(raw)));
return .{ .cwd = cwd, .boot = try std.fmt.bufPrint(path_buf, "{s}/mise.toml", .{cwd}) };
}
fn selectedOutputText(pane: *const pardes.Pane) ?[]const u8 {
const file = pane.file orelse return null;
if (!pane.vsel.active or pane.vsel.row != pane.cur_row or pane.cur_row < 0) return null;
const line = modal.lineSlice(file.content, @intCast(pane.cur_row));
const lo: usize = @intCast(@max(0, @min(pane.vsel.col, pane.cur_col)));
const hi: usize = @intCast(@max(0, @max(pane.vsel.col, pane.cur_col)));
if (lo >= line.len) return "";
return line[lo..@min(line.len, hi + 1)];
}
test "n/N selects one output location per row and opens nothing" {
if (platform == .web) return;
var cwd_buf: [4096]u8 = undefined;
var path_buf: [4096]u8 = undefined;
const paths = try projectPaths(&cwd_buf, &path_buf);
const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 40, .file = paths.boot });
defer p.deinit();
p.update(.{ .resize = .{ .cols = 100, .rows = 40 } });
const rid = try walkFixture(p, p.active, paths.cwd, "one");
const results = p.panes[rid].?;
const panes_before = p.freeSlot();
p.update(.{ .key = .{ .cp = 'n' } });
try std.testing.expectEqual(rid, p.active);
try std.testing.expectEqual(panes_before, p.freeSlot());
try std.testing.expect(results.vsel.active and results.vsel.explicit);
try std.testing.expectEqualStrings("build.zig:1:1", selectedOutputText(results) orelse "");
try std.testing.expectEqual(panes.Output.Grain.line, panes.Output.grain(results.file.?.output));
p.update(.{ .key = .{ .cp = 'n' } });
try std.testing.expectEqual(@as(i32, 1), results.cur_row);
try std.testing.expectEqualStrings("mise.toml", selectedOutputText(results) orelse "");
p.update(.{ .key = .{ .cp = 'n' } });
try std.testing.expectEqual(@as(i32, 3), results.cur_row); // row 2 is not look-able
try std.testing.expectEqualStrings("uucode_config.zig:7:1", selectedOutputText(results) orelse "");
p.update(.{ .key = .{ .cp = 'n' } });
try std.testing.expectEqual(@as(i32, 0), results.cur_row); // ring seam
p.update(.{ .key = .{ .cp = Key.enter } });
try std.testing.expect(p.freeSlot() != panes_before);
try std.testing.expect(std.mem.endsWith(u8, p.panes[p.active].?.file.?.path, "/build.zig"));
try std.testing.expectEqual(panes.Output.Grain.word, panes.Output.grain(p.panes[p.active].?.file.?.output));
}
test "n/N resumes the result output whose Look moved focus away" {
if (platform == .web) return;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{
.sub_path = "look-owner.txt",
.data = "alpha target\nbeta target\ngamma target\n",
});
var path_buf: [256]u8 = undefined;
const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/look-owner.txt", .{tmp.sub_path});
const p = try Pardes.init(std.testing.allocator, .{ .file = path, .cols = 80, .rows = 24 });
defer p.deinit();
try std.testing.expect(p.executeBuiltinLine(0, "Look target"));
const rid = p.panes[0].?.search_pane orelse return error.MissingResults;
const results = p.panes[rid].?;
const first = results.cur_row;
try std.testing.expectEqual(@as(usize, 0), p.active);
try std.testing.expect(results.look_at != null);
try std.testing.expectEqual(results.serial, p.look_walk_owner orelse return error.MissingLookOwner);
p.update(.{ .key = .{ .cp = 'n' } });
try std.testing.expectEqual(rid, p.active);
try std.testing.expectEqual(first + 1, results.cur_row);
p.update(.{ .key = .{ .cp = 'N' } });
try std.testing.expectEqual(first, results.cur_row);
// A later no-match answer has no position to resume and therefore cannot
// steal the provenance established by the Look above.
const owner = results.serial;
const dir = std.fs.path.dirname(path) orelse ".";
try panes.Output.fillResults(
p,
0,
dir,
.search,
"no-such-result",
try p.gpa.dupe(u8, ""),
null,
);
try std.testing.expectEqual(owner, p.look_walk_owner.?);
p.update(.{ .key = .{ .cp = 'n' } });
try std.testing.expectEqual(rid, p.active);
try std.testing.expectEqual(first + 1, results.cur_row);
// Emptying the owner itself keeps its identity as the ring's starting
// point. Deleting it then leaves a stale serial, never an id that can bind
// to the unrelated pane subsequently allocated in the same slot.
try panes.Output.fillResults(p, 0, dir, .search, "target", try p.gpa.dupe(u8, ""), null);
try std.testing.expectEqual(owner, p.look_walk_owner.?);
try std.testing.expect(p.executeBuiltinLine(rid, "Del"));
try std.testing.expect(p.paneBySerial(owner) == null);
const replacement = try p.newShell(rid, "");
try std.testing.expect(replacement.serial != owner);
try std.testing.expect(p.paneBySerial(owner) == null);
}
test "N exactly reverses n across output panes and the ring seam" {
if (platform == .web) return;
var cwd_buf: [4096]u8 = undefined;
var path_buf: [4096]u8 = undefined;
const paths = try projectPaths(&cwd_buf, &path_buf);
const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 40, .file = paths.boot });
defer p.deinit();
p.update(.{ .resize = .{ .cols = 100, .rows = 40 } });
const first = try walkFixture(p, p.active, paths.cwd, "one");
p.update(.{ .key = .{ .cp = 'n', .alt = true } });
const second = try walkFixture(p, p.active, paths.cwd, "two");
try std.testing.expect(first != second);
const Mark = struct { pane: usize, row: i32, col: i32 };
const here = struct {
fn at(pp: *Pardes) Mark {
const pane = pp.panes[pp.active].?;
return .{ .pane = pp.active, .row = pane.cur_row, .col = pane.cur_col };
}
}.at;
p.update(.{ .key = .{ .cp = 'n' } });
const base = here(p);
var trail: [12]Mark = undefined;
for (&trail) |*mark| {
p.update(.{ .key = .{ .cp = 'n' } });
mark.* = here(p);
}
var i = trail.len;
while (i > 0) {
i -= 1;
p.update(.{ .key = .{ .cp = 'N' } });
const want = if (i == 0) base else trail[i - 1];
try std.testing.expectEqual(want, here(p));
}
var saw_first = false;
var saw_second = false;
for (trail) |mark| {
saw_first = saw_first or mark.pane == first;
saw_second = saw_second or mark.pane == second;
}
try std.testing.expect(saw_first and saw_second);
}
test "ThemeSel groups native themes first and skips headings in both directions" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 });
defer p.deinit();
const source = try p.setTestFile("notes\n");
p.setThemeIndex(pardes.themes.len - 1);
const original_theme = p.settings.theme;
try std.testing.expect(p.executeBuiltinLine(0, "ThemeSel"));
const tid = source.search_pane orelse return error.MissingThemeList;
const list = p.panes[tid].?;
const content = list.file.?.content;
try std.testing.expectEqual(@as(usize, 0), p.active);
try std.testing.expectEqual(@as(i32, 0), list.cur_row);
try std.testing.expectEqual(@as(usize, 0), list.file.?.scroll);
try std.testing.expectEqualStrings("# Pardes themes", modal.lineSlice(content, 0));
try std.testing.expectEqualStrings("# Legacy and imported themes", modal.lineSlice(content, pardes.native_theme_count + 2));
try std.testing.expectEqual(@as(usize, pardes.themes.len + 3), std.mem.count(u8, content, "\n"));
for (pardes.themes, 0..) |theme, i| {
const row = i + 1 + @as(usize, if (i >= pardes.native_theme_count) 2 else 0);
const line = modal.lineSlice(content, row);
try std.testing.expect(std.mem.startsWith(u8, line, "Theme "));
try std.testing.expectEqualStrings(theme.name, line["Theme ".len..]);
}
// Opening an imported theme's selector must still begin with the natives.
// Walk across the section break without selecting or executing a heading.
for (pardes.themes[0 .. pardes.native_theme_count + 1]) |theme| {
p.update(.{ .key = .{ .cp = 'n' } });
try std.testing.expectEqual(tid, p.active);
const selected = selectedOutputText(list) orelse return error.MissingThemeSelection;
try std.testing.expectEqualStrings(theme.name, selected["Theme ".len..]);
try std.testing.expectEqual(original_theme, p.settings.theme);
}
p.update(.{ .key = .{ .cp = 'N' } });
const previous = selectedOutputText(list) orelse return error.MissingThemeSelection;
try std.testing.expectEqualStrings(pardes.themes[pardes.native_theme_count - 1].name, previous["Theme ".len..]);
p.update(.{ .key = .{ .cp = Key.tab } });
try std.testing.expectEqual(pardes.native_theme_count - 1, p.settings.theme);
}
test "n/N selects command outputs by whole row" {
if (platform == .web) return;
var cwd_buf: [4096]u8 = undefined;
var path_buf: [4096]u8 = undefined;
const paths = try projectPaths(&cwd_buf, &path_buf);
const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 40, .file = paths.boot });
defer p.deinit();
p.update(.{ .resize = .{ .cols = 100, .rows = 40 } });
try std.testing.expect(p.executeBuiltinLine(p.active, "ThemeSel"));
const tid = p.panes[p.active].?.search_pane orelse return error.MissingThemeList;
const themes = p.panes[tid].?;
try std.testing.expect(panes.Output.fileTraits(themes.file.?.output).commands);
p.update(.{ .key = .{ .cp = 'n' } });
const row0 = std.mem.trimEnd(u8, modal.lineSlice(themes.file.?.content, 1), " \t\r");
try std.testing.expectEqualStrings(row0, selectedOutputText(themes) orelse "");
const theme_before = p.settings.theme;
p.update(.{ .key = .{ .cp = 'n' } });
p.update(.{ .key = .{ .cp = Key.tab } });
try std.testing.expect(p.settings.theme != theme_before);
try std.testing.expect(p.executeBuiltinLine(0, "Look build.zig"));
p.update(.tick);
try std.testing.expect(p.active != 0);
p.update(.{ .key = .{ .cp = 'n' } });
try std.testing.expectEqual(@as(usize, 0), p.active);
}
test "identical result refresh keeps warm content caches without allocation" {
var allocator: std.testing.FailingAllocator = .init(std.testing.allocator, .{});
const p = try Pardes.init(allocator.allocator(), .{ .tty_only = true, .cols = 80, .rows = 24 });
defer p.deinit();
const source = try p.setTestFile("notes\n");
p.settings.colors = true;
const text = "a.zig:1:1 pub fn first() void {}\na.zig:2:1 if (true) return;\n";
try panes.Output.fillResults(p, 0, "/tmp", .{ .query = .references }, "before", try p.gpa.dupe(u8, text), 0);
const id = source.search_pane orelse return error.MissingResults;
const result = p.panes[id].?;
const file = &result.file.?;
result.cur_row = 1;
result.cur_col = 12;
result.vsel = .{ .active = true, .explicit = true, .row = 0, .col = 2 };
result.select = true;
file.scroll = 1;
result.hscroll = 3;
panes.File.refreshHighlights(p);
const index = try panes.File.lineIndex(p.gpa, file);
const styles = try std.testing.allocator.dupe(u8, file.highlights);
defer std.testing.allocator.free(styles);
if (@import("pardes_config").syntax_zig_grammar) try std.testing.expect(styles.len > 0);
const revision = file.revision;
const saved_revision = file.saved_revision;
const body_ptr = file.content.ptr;
const style_ptr = file.highlights.ptr;
const highlight_start = file.highlight_start;
const selected = result.vsel;
const event_node = pardes.filesystem.Node.of(result.serial, .event);
const opened = pardes.filesystem.handle(p, .{ .tag = 0, .op = .open, .node = event_node });
try std.testing.expectEqual(.ok, opened.status);
defer _ = pardes.filesystem.handle(p, .{ .tag = 0, .op = .release, .node = event_node, .handle = opened.handle });
for (0..32) |_| {
const incoming = try p.gpa.dupe(u8, text);
const allocations = allocator.allocations;
const freed = allocator.freed_bytes;
const deallocations = allocator.deallocations;
allocator.fail_index = allocator.alloc_index;
defer allocator.fail_index = std.math.maxInt(usize);
p.active = id;
source.search_pane = null;
source.search_row = null;
p.look_walk_owner = null;
try panes.Output.fillResults(p, 0, "/tmp", .{ .query = .references }, "after", incoming, 1);
try std.testing.expectEqual(revision, file.revision);
try std.testing.expectEqual(saved_revision, file.saved_revision);
try std.testing.expectEqual(body_ptr, file.content.ptr);
try std.testing.expectEqual(index.ptr, file.line_starts.ptr);
try std.testing.expectEqual(style_ptr, file.highlights.ptr);
try std.testing.expectEqual(highlight_start, file.highlight_start);
try std.testing.expectEqualSlices(u8, styles, file.highlights);
try std.testing.expect(!file.syntax_dirty);
panes.File.refreshHighlights(p);
try std.testing.expectEqual(allocations, allocator.allocations);
try std.testing.expectEqual(freed + text.len, allocator.freed_bytes);
try std.testing.expectEqual(deallocations + 1, allocator.deallocations);
try std.testing.expect(!allocator.has_induced_failure);
try std.testing.expectEqual(selected, result.vsel);
try std.testing.expect(result.select);
try std.testing.expectEqual(@as(i32, 1), result.cur_row);
try std.testing.expectEqual(@as(i32, 12), result.cur_col);
try std.testing.expectEqual(@as(usize, 1), file.scroll);
try std.testing.expectEqual(@as(i32, 3), result.hscroll);
try std.testing.expectEqualStrings("after", file.output.?.arg());
try std.testing.expectEqual(@as(usize, 0), p.active);
try std.testing.expectEqual(id, source.search_pane.?);
try std.testing.expectEqual(@as(usize, 1), source.search_row.?);
try std.testing.expectEqual(result.serial, p.look_walk_owner.?);
try std.testing.expect(p.fs.panes[id].events.empty());
}
try panes.Output.fillResults(p, 0, "/tmp", .{ .query = .references }, "changed", try p.gpa.dupe(u8, "short\n"), null);
try std.testing.expectEqual(revision + 1, file.revision);
try std.testing.expectEqualStrings("short\n", file.content);
try std.testing.expectEqualStrings("changed", file.output.?.arg());
try std.testing.expectEqual(@as(usize, 0), file.line_starts.len);
try std.testing.expectEqual(@as(usize, 0), file.highlights.len);
try std.testing.expect(file.syntax_dirty);
try std.testing.expect(!result.vsel.active and !result.select);
try std.testing.expectEqual(@as(i32, 0), result.cur_row);
try std.testing.expectEqual(@as(i32, 0), result.cur_col);
try std.testing.expectEqual(@as(usize, 0), file.scroll);
try std.testing.expectEqual(@as(i32, 0), result.hscroll);
try std.testing.expect(source.search_row == null);
const events = &p.fs.panes[id].events;
try std.testing.expect(std.mem.startsWith(u8, events.peek().?, "KD"));
events.pop();
try std.testing.expect(std.mem.startsWith(u8, events.peek().?, "KI"));
events.pop();
try std.testing.expect(events.empty());
}
test "rejected result refresh frees incoming text and leaves the existing output intact" {
var allocator: std.testing.FailingAllocator = .init(std.testing.allocator, .{});
const p = try Pardes.init(allocator.allocator(), .{ .tty_only = true, .cols = 80, .rows = 24 });
defer p.deinit();
const source = try p.setTestFile("notes\n");
try panes.Output.fillResults(p, 0, "/tmp", .{ .query = .references }, "before", try p.gpa.dupe(u8, "keep\n"), 1);
const id = source.search_pane.?;
const result = p.panes[id].?;
const file = &result.file.?;
const body_ptr = file.content.ptr;
const revision = file.revision;
const free_slot = p.freeSlot();
var oversized: [panes.Output.max_arg + 1]u8 = @splat('x');
for ([_]bool{ false, true }) |oom| {
const incoming = try p.gpa.dupe(u8, "discard\n");
const freed = allocator.freed_bytes;
const deallocations = allocator.deallocations;
if (oom) allocator.fail_index = allocator.alloc_index;
defer allocator.fail_index = std.math.maxInt(usize);
try std.testing.expectError(if (oom) error.OutOfMemory else error.ArgumentTooLong, panes.Output.fillResults(
p,
0,
"/tmp",
if (oom) .search else .{ .query = .references },
if (oom) "new output" else &oversized,
incoming,
0,
));
try std.testing.expectEqual(freed + "discard\n".len, allocator.freed_bytes);
try std.testing.expectEqual(deallocations + 1, allocator.deallocations);
try std.testing.expectEqual(body_ptr, file.content.ptr);
try std.testing.expectEqual(revision, file.revision);
try std.testing.expectEqualStrings("keep\n", file.content);
try std.testing.expectEqualStrings("before", file.output.?.arg());
try std.testing.expectEqual(free_slot, p.freeSlot());
try std.testing.expectEqual(id, source.search_pane.?);
try std.testing.expectEqual(@as(usize, 1), source.search_row.?);
try std.testing.expectEqual(@as(usize, 0), p.active);
}
try std.testing.expect(allocator.has_induced_failure);
}
|