@native-sdk/cli 0.5.4 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/build/app.zig +50 -1
  2. package/build.zig +588 -32
  3. package/build.zig.zon +7 -0
  4. package/native-sdk.d.ts +3 -1
  5. package/package.json +9 -9
  6. package/packages/core/package-lock.json +2 -2
  7. package/packages/core/package.json +1 -1
  8. package/packages/core/rt/rt.zig +280 -8
  9. package/packages/core/sdk/core.ts +481 -2
  10. package/packages/core/sdk/events.ts +17 -8
  11. package/packages/core/src/checker.ts +305 -2
  12. package/packages/core/src/diagnostics.ts +12 -0
  13. package/packages/core/src/emitter.ts +599 -3
  14. package/packages/core/src/typed_ast.ts +51 -4
  15. package/packages/core/src/types.ts +34 -1
  16. package/skill-data/native-ui/SKILL.md +39 -2
  17. package/skill-data/ts-core/SKILL.md +4 -2
  18. package/src/app_runner/root.zig +32 -10
  19. package/src/automation/layout_fingerprint.zig +160 -0
  20. package/src/automation/protocol.zig +82 -33
  21. package/src/automation/root.zig +1 -0
  22. package/src/automation/server.zig +1 -1
  23. package/src/automation/snapshot.zig +68 -9
  24. package/src/automation/watcher.zig +1 -1
  25. package/src/embed/host.zig +5 -0
  26. package/src/embed/ui_host.zig +13 -0
  27. package/src/platform/linux/gtk_host.c +527 -13
  28. package/src/platform/linux/gtk_host.h +25 -0
  29. package/src/platform/linux/root.zig +280 -7
  30. package/src/platform/macos/appkit_host.h +104 -6
  31. package/src/platform/macos/appkit_host.m +1368 -74
  32. package/src/platform/macos/cef_host.mm +110 -1
  33. package/src/platform/macos/root.zig +287 -7
  34. package/src/platform/null_platform.zig +398 -4
  35. package/src/platform/null_platform_tests.zig +35 -0
  36. package/src/platform/root.zig +9 -0
  37. package/src/platform/types.zig +386 -14
  38. package/src/platform/windows/root.zig +298 -9
  39. package/src/platform/windows/webview2_host.cpp +330 -5
  40. package/src/primitives/app_manifest/validation.zig +18 -0
  41. package/src/primitives/canvas/a11y_audit.zig +33 -11
  42. package/src/primitives/canvas/a11y_audit_tests.zig +20 -0
  43. package/src/primitives/canvas/drawing.zig +25 -0
  44. package/src/primitives/canvas/equality.zig +8 -0
  45. package/src/primitives/canvas/events.zig +103 -16
  46. package/src/primitives/canvas/frame.zig +125 -1
  47. package/src/primitives/canvas/layout_audit.zig +22 -5
  48. package/src/primitives/canvas/markdown_hostile_tests.zig +8 -4
  49. package/src/primitives/canvas/render_tests.zig +13 -10
  50. package/src/primitives/canvas/root.zig +48 -0
  51. package/src/primitives/canvas/terminal_box.zig +471 -0
  52. package/src/primitives/canvas/terminal_grid.zig +978 -0
  53. package/src/primitives/canvas/terminal_grid_tests.zig +1093 -0
  54. package/src/primitives/canvas/test_support.zig +1 -0
  55. package/src/primitives/canvas/tests.zig +1 -0
  56. package/src/primitives/canvas/text_tests.zig +39 -4
  57. package/src/primitives/canvas/tokens.zig +115 -12
  58. package/src/primitives/canvas/ui.zig +524 -5
  59. package/src/primitives/canvas/ui_markup.zig +195 -2
  60. package/src/primitives/canvas/ui_markup_compiled.zig +172 -1
  61. package/src/primitives/canvas/ui_markup_compiled_tests.zig +133 -8
  62. package/src/primitives/canvas/ui_markup_contract.zig +88 -5
  63. package/src/primitives/canvas/ui_markup_contract_tests.zig +39 -1
  64. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +1 -0
  65. package/src/primitives/canvas/ui_markup_reflect.zig +75 -24
  66. package/src/primitives/canvas/ui_markup_tests.zig +149 -0
  67. package/src/primitives/canvas/ui_markup_view.zig +191 -0
  68. package/src/primitives/canvas/ui_markup_view_tests.zig +403 -19
  69. package/src/primitives/canvas/ui_schema.zig +97 -17
  70. package/src/primitives/canvas/ui_schema_tests.zig +18 -11
  71. package/src/primitives/canvas/ui_tests.zig +141 -9
  72. package/src/primitives/canvas/widget_access.zig +28 -2
  73. package/src/primitives/canvas/widget_anchored_tests.zig +48 -0
  74. package/src/primitives/canvas/widget_invalidation.zig +81 -1
  75. package/src/primitives/canvas/widget_layout.zig +59 -11
  76. package/src/primitives/canvas/widget_layout_tests.zig +125 -4
  77. package/src/primitives/canvas/widget_render.zig +272 -18
  78. package/src/primitives/canvas/widget_render_scroll.zig +108 -27
  79. package/src/primitives/canvas/widget_routing.zig +59 -7
  80. package/src/primitives/canvas/widget_runtime.zig +23 -0
  81. package/src/primitives/canvas/widget_runtime_tests.zig +149 -0
  82. package/src/primitives/canvas/widget_semantics.zig +119 -11
  83. package/src/primitives/canvas/widget_semantics_tests.zig +409 -0
  84. package/src/primitives/canvas/widget_tree.zig +17 -0
  85. package/src/primitives/canvas/widgets.zig +132 -0
  86. package/src/root.zig +16 -0
  87. package/src/runtime/api.zig +76 -6
  88. package/src/runtime/automation_commands.zig +20 -1
  89. package/src/runtime/automation_snapshot.zig +25 -1
  90. package/src/runtime/automation_widget_dispatch.zig +73 -8
  91. package/src/runtime/bridge_payload.zig +1 -0
  92. package/src/runtime/canvas_frame.zig +62 -7
  93. package/src/runtime/canvas_frame_animation_tests.zig +5 -4
  94. package/src/runtime/canvas_frame_helpers.zig +190 -1
  95. package/src/runtime/canvas_frame_invalidation_tests.zig +531 -0
  96. package/src/runtime/canvas_frame_patch_tests.zig +161 -10
  97. package/src/runtime/canvas_frame_retained_tests.zig +5 -2
  98. package/src/runtime/canvas_widget_accessibility_tests.zig +1 -1
  99. package/src/runtime/canvas_widget_clipboard_tests.zig +40 -0
  100. package/src/runtime/canvas_widget_context_menu.zig +269 -32
  101. package/src/runtime/canvas_widget_context_menu_tests.zig +640 -14
  102. package/src/runtime/canvas_widget_display.zig +1 -1
  103. package/src/runtime/canvas_widget_event_tests.zig +1823 -0
  104. package/src/runtime/canvas_widget_events.zig +268 -26
  105. package/src/runtime/canvas_widget_runtime.zig +213 -29
  106. package/src/runtime/canvas_widget_scroll_driver_tests.zig +170 -7
  107. package/src/runtime/canvas_widget_scroll_drivers.zig +151 -17
  108. package/src/runtime/canvas_widget_scroll_tests.zig +435 -23
  109. package/src/runtime/canvas_widget_state.zig +6 -0
  110. package/src/runtime/canvas_widget_text_tests.zig +1 -1
  111. package/src/runtime/core.zig +73 -2
  112. package/src/runtime/effects.zig +6389 -54
  113. package/src/runtime/effects_channel_tests.zig +3519 -0
  114. package/src/runtime/effects_pty_tests.zig +1362 -0
  115. package/src/runtime/effects_tests.zig +14 -9
  116. package/src/runtime/effects_video_tests.zig +3609 -0
  117. package/src/runtime/flow.zig +25 -4
  118. package/src/runtime/gpu_surface_events.zig +475 -15
  119. package/src/runtime/media_surface.zig +116 -11
  120. package/src/runtime/media_surface_tests.zig +61 -0
  121. package/src/runtime/pty.zig +1252 -0
  122. package/src/runtime/pty_windows.zig +1780 -0
  123. package/src/runtime/root.zig +25 -0
  124. package/src/runtime/runtime_event_tests.zig +20 -1
  125. package/src/runtime/session_journal.zig +367 -61
  126. package/src/runtime/session_record.zig +16 -0
  127. package/src/runtime/session_replay.zig +395 -11
  128. package/src/runtime/session_tests.zig +114 -19
  129. package/src/runtime/terminal_session.zig +1569 -0
  130. package/src/runtime/terminal_session_tests.zig +450 -0
  131. package/src/runtime/terminal_vt_ghostty.zig +15 -0
  132. package/src/runtime/terminal_vt_stub.zig +13 -0
  133. package/src/runtime/tests.zig +6 -0
  134. package/src/runtime/ts_core_host.zig +980 -140
  135. package/src/runtime/ts_core_host_tests.zig +1059 -1
  136. package/src/runtime/ts_ui_app.zig +16 -5
  137. package/src/runtime/ui_app.zig +2338 -67
  138. package/src/runtime/ui_app_tests.zig +3171 -744
  139. package/src/runtime/ui_app_virtual_variable_tests.zig +4 -2
  140. package/src/runtime/ui_app_window_tests.zig +268 -0
  141. package/src/runtime/view.zig +190 -2
  142. package/src/runtime/view_widget_scroll.zig +218 -68
  143. package/src/runtime/view_widget_text.zig +11 -2
  144. package/src/runtime/view_widget_tree.zig +111 -3
  145. package/src/runtime/window_storage.zig +45 -2
  146. package/src/runtime/window_view_runtime.zig +34 -2
  147. package/src/runtime/window_views.zig +1 -0
  148. package/src/terminal_session_tests_root.zig +15 -0
  149. package/src/tooling/templates.zig +25 -7
@@ -0,0 +1,1093 @@
1
+ //! Terminal grid painter tests: snapshot-in, display-list-out. The
2
+ //! snapshots are hand-built (the painter's contract is resolved cell
3
+ //! data — no emulator here), so every assertion pins painter policy:
4
+ //! real text runs, run merging, geometric box drawing, the selection
5
+ //! wash, the cursor register, and the three row-atomic budgets.
6
+
7
+ const std = @import("std");
8
+ const canvas = @import("root.zig");
9
+ const geometry = @import("geometry");
10
+ const grid_model = @import("terminal_grid.zig");
11
+ const box = @import("terminal_box.zig");
12
+
13
+ const testing = std.testing;
14
+
15
+ const white = canvas.Color.rgba(1, 1, 1, 1);
16
+ const black = canvas.Color.rgba(0, 0, 0, 1);
17
+ const red = canvas.Color.rgba(1, 0, 0, 1);
18
+ const blue = canvas.Color.rgba(0, 0, 1, 1);
19
+
20
+ fn cell(cp: u21, cluster: []const u8, fg: canvas.Color) grid_model.TerminalCell {
21
+ return .{ .cp = cp, .cluster = cluster, .fg = fg };
22
+ }
23
+
24
+ fn asciiRow(comptime text: []const u8, fg: canvas.Color) [text.len]grid_model.TerminalCell {
25
+ var cells: [text.len]grid_model.TerminalCell = undefined;
26
+ inline for (text, 0..) |ch, index| {
27
+ cells[index] = if (ch == ' ')
28
+ .{}
29
+ else
30
+ cell(ch, text[index .. index + 1], fg);
31
+ }
32
+ return cells;
33
+ }
34
+
35
+ fn baseGrid(rows: []const grid_model.TerminalRow) grid_model.TerminalGrid {
36
+ return .{
37
+ .rows = rows,
38
+ .background = black,
39
+ .foreground = white,
40
+ .cursor_color = blue,
41
+ .selection_color = blue,
42
+ };
43
+ }
44
+
45
+ fn paintInto(grid: grid_model.TerminalGrid, commands: []canvas.CanvasCommand, options: grid_model.TerminalPaintOptions) !canvas.Builder {
46
+ var builder = canvas.Builder.init(commands);
47
+ try grid_model.paint(grid, &builder, options);
48
+ return builder;
49
+ }
50
+
51
+ test "the painter emits merged text runs with per-run colors" {
52
+ const row_cells = comptime asciiRow("hi red", white);
53
+ var cells = row_cells;
54
+ // Recolor the "red" run.
55
+ cells[3].fg = red;
56
+ cells[4].fg = red;
57
+ cells[5].fg = red;
58
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &cells }};
59
+
60
+ var commands: [256]canvas.CanvasCommand = undefined;
61
+ var builder = try paintInto(baseGrid(&rows), &commands, .{
62
+ .frame = geometry.RectF.init(0, 0, 400, 100),
63
+ .tokens = .{},
64
+ });
65
+ const list = builder.displayList();
66
+
67
+ var saw_hi = false;
68
+ var saw_red = false;
69
+ for (list.commands) |command| {
70
+ switch (command) {
71
+ .draw_text => |text| {
72
+ if (std.mem.eql(u8, text.text, "hi")) {
73
+ saw_hi = true;
74
+ try testing.expectEqual(white.r, text.color.r);
75
+ }
76
+ if (std.mem.eql(u8, text.text, "red")) {
77
+ saw_red = true;
78
+ try testing.expectEqual(red.r, text.color.r);
79
+ try testing.expectEqual(red.g, text.color.g);
80
+ }
81
+ },
82
+ else => {},
83
+ }
84
+ }
85
+ try testing.expect(saw_hi);
86
+ try testing.expect(saw_red);
87
+ }
88
+
89
+ test "wide cells advance two columns and spacers carry no ink" {
90
+ // "你!" — the wide cell occupies columns 0-1, '!' lands at column 2.
91
+ const cells = [_]grid_model.TerminalCell{
92
+ .{ .cp = 0x4F60, .cluster = "\xe4\xbd\xa0", .fg = white, .wide = .wide },
93
+ .{ .wide = .spacer },
94
+ cell('!', "!", white),
95
+ };
96
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &cells }};
97
+
98
+ var commands: [64]canvas.CanvasCommand = undefined;
99
+ var builder = try paintInto(baseGrid(&rows), &commands, .{
100
+ .frame = geometry.RectF.init(0, 0, 400, 100),
101
+ .tokens = .{},
102
+ });
103
+ const list = builder.displayList();
104
+
105
+ const tokens: canvas.DesignTokens = .{};
106
+ const metrics = grid_model.cellMetrics(tokens);
107
+ var saw_bang = false;
108
+ for (list.commands) |command| {
109
+ switch (command) {
110
+ .draw_text => |text| {
111
+ if (std.mem.eql(u8, text.text, "!")) {
112
+ saw_bang = true;
113
+ // One wide cluster before it: x = 2 cells.
114
+ try testing.expectApproxEqAbs(metrics.width * 2, text.origin.x, 0.01);
115
+ }
116
+ },
117
+ else => {},
118
+ }
119
+ }
120
+ try testing.expect(saw_bang);
121
+ }
122
+
123
+ test "box-drawing cells render as geometry, never text" {
124
+ const cells = [_]grid_model.TerminalCell{
125
+ .{ .cp = 0x2500, .fg = white }, // ─
126
+ .{ .cp = 0x2500, .fg = white },
127
+ .{ .cp = 0x2502, .fg = white }, // │
128
+ };
129
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &cells }};
130
+
131
+ var commands: [64]canvas.CanvasCommand = undefined;
132
+ var builder = try paintInto(baseGrid(&rows), &commands, .{
133
+ .frame = geometry.RectF.init(0, 0, 400, 100),
134
+ .tokens = .{},
135
+ });
136
+ const list = builder.displayList();
137
+
138
+ var text_commands: usize = 0;
139
+ var fills: usize = 0;
140
+ for (list.commands) |command| {
141
+ switch (command) {
142
+ .draw_text => text_commands += 1,
143
+ .fill_rect => fills += 1,
144
+ else => {},
145
+ }
146
+ }
147
+ try testing.expectEqual(@as(usize, 0), text_commands);
148
+ // Background + the merged ─ run + the │ bar at least.
149
+ try testing.expect(fills >= 3);
150
+ }
151
+
152
+ test "the selection wash and keyboard caret paint from snapshot state" {
153
+ const row_cells = comptime asciiRow("select me", white);
154
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &row_cells, .selection = .{ 0, 5 } }};
155
+ var grid = baseGrid(&rows);
156
+ grid.select_head = .{ .x = 5, .y = 0 };
157
+
158
+ var commands: [64]canvas.CanvasCommand = undefined;
159
+ var builder = try paintInto(grid, &commands, .{
160
+ .frame = geometry.RectF.init(0, 0, 400, 100),
161
+ .tokens = .{},
162
+ });
163
+ const list = builder.displayList();
164
+
165
+ var saw_wash = false;
166
+ var saw_caret = false;
167
+ for (list.commands) |command| {
168
+ switch (command) {
169
+ .fill_rect => |fill| {
170
+ if (fill.fill == .color and fill.fill.color.a > 0.29 and fill.fill.color.a < 0.31) saw_wash = true;
171
+ },
172
+ .stroke_rect => saw_caret = true,
173
+ else => {},
174
+ }
175
+ }
176
+ try testing.expect(saw_wash);
177
+ try testing.expect(saw_caret);
178
+ }
179
+
180
+ test "the cursor register: filled only while focused and live, hollow otherwise" {
181
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &.{} }};
182
+ var grid = baseGrid(&rows);
183
+ grid.cursor = .{ .x = 0, .y = 0 };
184
+
185
+ var commands: [16]canvas.CanvasCommand = undefined;
186
+ var builder = try paintInto(grid, &commands, .{
187
+ .frame = geometry.RectF.init(0, 0, 100, 40),
188
+ .tokens = .{},
189
+ .focused = true,
190
+ });
191
+ var running_alpha: f32 = 0;
192
+ for (builder.displayList().commands) |command| {
193
+ switch (command) {
194
+ .fill_rect => |fill| {
195
+ if (fill.fill == .color and fill.fill.color.b == blue.b and fill.fill.color.a < 1) running_alpha = fill.fill.color.a;
196
+ },
197
+ else => {},
198
+ }
199
+ }
200
+ try testing.expectApproxEqAbs(@as(f32, 0.45), running_alpha, 0.001);
201
+
202
+ var blurred_commands: [16]canvas.CanvasCommand = undefined;
203
+ var blurred_builder = try paintInto(grid, &blurred_commands, .{
204
+ .frame = geometry.RectF.init(0, 0, 100, 40),
205
+ .tokens = .{},
206
+ .focused = false,
207
+ });
208
+ var blurred_alpha: f32 = 0;
209
+ for (blurred_builder.displayList().commands) |command| {
210
+ switch (command) {
211
+ .stroke_rect => |stroke| {
212
+ if (stroke.stroke.fill == .color and stroke.stroke.fill.color.b == blue.b) {
213
+ blurred_alpha = stroke.stroke.fill.color.a;
214
+ try testing.expectEqual(@as(f32, 1), stroke.stroke.width);
215
+ }
216
+ },
217
+ else => {},
218
+ }
219
+ }
220
+ try testing.expectApproxEqAbs(@as(f32, 0.45), blurred_alpha, 0.001);
221
+
222
+ grid.running = false;
223
+ var ended_commands: [16]canvas.CanvasCommand = undefined;
224
+ var ended_builder = try paintInto(grid, &ended_commands, .{
225
+ .frame = geometry.RectF.init(0, 0, 100, 40),
226
+ .tokens = .{},
227
+ .focused = true,
228
+ });
229
+ var ended_alpha: f32 = 0;
230
+ for (ended_builder.displayList().commands) |command| {
231
+ switch (command) {
232
+ .stroke_rect => |stroke| {
233
+ if (stroke.stroke.fill == .color and stroke.stroke.fill.color.b == blue.b) {
234
+ ended_alpha = stroke.stroke.fill.color.a;
235
+ try testing.expectEqual(@as(f32, 1), stroke.stroke.width);
236
+ }
237
+ },
238
+ else => {},
239
+ }
240
+ }
241
+ try testing.expectApproxEqAbs(@as(f32, 0.22), ended_alpha, 0.001);
242
+ }
243
+
244
+ test "the terminal widget cursor follows logical focus independently of the outer ring" {
245
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &.{} }};
246
+ var grid = baseGrid(&rows);
247
+ grid.cursor = .{ .x = 0, .y = 0 };
248
+ const terminal = canvas.Widget{
249
+ .id = 9,
250
+ .kind = .terminal,
251
+ .frame = geometry.RectF.init(0, 0, 200, 100),
252
+ .terminal = .{ .pty = 1, .grid = &grid },
253
+ };
254
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
255
+ const layout = try canvas.layoutWidgetTree(terminal, terminal.frame, &nodes);
256
+ const cursor_id = grid_model.paintIdBase(terminal.id) + 0x61_0002;
257
+
258
+ // Logical focus fills the cursor even when the modality-specific
259
+ // outer ring is quiet.
260
+ var focused_commands: [32]canvas.CanvasCommand = undefined;
261
+ var focused_builder = canvas.Builder.init(&focused_commands);
262
+ try layout.emitDisplayListWithState(&focused_builder, .{}, .{ .focused_id = terminal.id });
263
+ var saw_filled_cursor = false;
264
+ var saw_outer_ring = false;
265
+ for (focused_builder.displayList().commands) |command| {
266
+ switch (command) {
267
+ .fill_rect => |fill| if (fill.id == cursor_id) {
268
+ saw_filled_cursor = true;
269
+ },
270
+ .stroke_rect => |stroke| if (stroke.id != cursor_id) {
271
+ saw_outer_ring = true;
272
+ },
273
+ else => {},
274
+ }
275
+ }
276
+ try testing.expect(saw_filled_cursor);
277
+ try testing.expect(!saw_outer_ring);
278
+
279
+ var blurred_commands: [32]canvas.CanvasCommand = undefined;
280
+ var blurred_builder = canvas.Builder.init(&blurred_commands);
281
+ try layout.emitDisplayListWithState(&blurred_builder, .{}, .{
282
+ .keyboard_active = false,
283
+ .focused_id = terminal.id,
284
+ .focus_visible_id = terminal.id,
285
+ });
286
+ var saw_hollow_cursor = false;
287
+ var kept_outer_ring = false;
288
+ for (blurred_builder.displayList().commands) |command| {
289
+ switch (command) {
290
+ .stroke_rect => |stroke| if (stroke.id == cursor_id) {
291
+ saw_hollow_cursor = true;
292
+ } else {
293
+ kept_outer_ring = true;
294
+ },
295
+ else => {},
296
+ }
297
+ }
298
+ try testing.expect(saw_hollow_cursor);
299
+ // Key-window/app activity gates the terminal's ownership cue only;
300
+ // retained focus-visible chrome keeps its normal restoration state.
301
+ try testing.expect(kept_outer_ring);
302
+ }
303
+
304
+ test "the scrollback thumb paints only while the viewport is in history" {
305
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &.{} }};
306
+ var grid = baseGrid(&rows);
307
+
308
+ // Pinned to the bottom: no thumb.
309
+ grid.scrollbar = .{ .offset = 76, .len = 24, .total = 100 };
310
+ var pinned_commands: [16]canvas.CanvasCommand = undefined;
311
+ var pinned = try paintInto(grid, &pinned_commands, .{
312
+ .frame = geometry.RectF.init(0, 0, 100, 200),
313
+ .tokens = .{},
314
+ });
315
+ const pinned_count = pinned.displayList().commands.len;
316
+
317
+ // Scrolled into history: exactly one extra fill (the thumb).
318
+ grid.scrollbar = .{ .offset = 10, .len = 24, .total = 100 };
319
+ var history_commands: [16]canvas.CanvasCommand = undefined;
320
+ var history = try paintInto(grid, &history_commands, .{
321
+ .frame = geometry.RectF.init(0, 0, 100, 200),
322
+ .tokens = .{},
323
+ });
324
+ try testing.expectEqual(pinned_count + 1, history.displayList().commands.len);
325
+ }
326
+
327
+ test "the command budget degrades row-wise, never overflowing the frame" {
328
+ // Three rows of alternating colors (no run merging), tight budget.
329
+ var cells: [3][8]grid_model.TerminalCell = undefined;
330
+ for (&cells) |*row_cells| {
331
+ for (row_cells, 0..) |*entry, index| {
332
+ entry.* = cell('x', "x", if (index % 2 == 0) white else red);
333
+ entry.bg = if (index % 2 == 0) blue else null;
334
+ }
335
+ }
336
+ const rows = [_]grid_model.TerminalRow{
337
+ .{ .cells = &cells[0] },
338
+ .{ .cells = &cells[1] },
339
+ .{ .cells = &cells[2] },
340
+ };
341
+
342
+ var commands: [512]canvas.CanvasCommand = undefined;
343
+ var builder = try paintInto(baseGrid(&rows), &commands, .{
344
+ .frame = geometry.RectF.init(0, 0, 400, 200),
345
+ .tokens = .{},
346
+ .command_budget = 60,
347
+ });
348
+ // Never exceeds the budget; the later rows dropped whole.
349
+ try testing.expect(builder.displayList().commands.len <= 60);
350
+ }
351
+
352
+ test "the text budget stops before a row it cannot hold whole" {
353
+ const row_a = comptime asciiRow("aaaa", white);
354
+ const row_b = comptime asciiRow("bbbb", white);
355
+ const rows = [_]grid_model.TerminalRow{
356
+ .{ .cells = &row_a },
357
+ .{ .cells = &row_b },
358
+ };
359
+
360
+ var commands: [64]canvas.CanvasCommand = undefined;
361
+ var builder = try paintInto(baseGrid(&rows), &commands, .{
362
+ .frame = geometry.RectF.init(0, 0, 400, 200),
363
+ .tokens = .{},
364
+ // Reserve all but 6 bytes of the store: row a (4 bytes) fits,
365
+ // row b would cross, so it drops whole.
366
+ .text_reserve = canvas.max_display_list_text_bytes - 6,
367
+ });
368
+ var texts: usize = 0;
369
+ var saw_a = false;
370
+ for (builder.displayList().commands) |command| {
371
+ switch (command) {
372
+ .draw_text => |text| {
373
+ texts += 1;
374
+ if (std.mem.eql(u8, text.text, "aaaa")) saw_a = true;
375
+ },
376
+ else => {},
377
+ }
378
+ }
379
+ try testing.expectEqual(@as(usize, 1), texts);
380
+ try testing.expect(saw_a);
381
+ }
382
+
383
+ test "the glyph budget stops before the row whose new code points cross it" {
384
+ // Row a introduces 4 distinct glyphs, row b 4 more.
385
+ const row_a = comptime asciiRow("abcd", white);
386
+ const row_b = comptime asciiRow("efgh", white);
387
+ const rows = [_]grid_model.TerminalRow{
388
+ .{ .cells = &row_a },
389
+ .{ .cells = &row_b },
390
+ };
391
+
392
+ var commands: [64]canvas.CanvasCommand = undefined;
393
+ var builder = try paintInto(baseGrid(&rows), &commands, .{
394
+ .frame = geometry.RectF.init(0, 0, 400, 200),
395
+ .tokens = .{},
396
+ // Atlas-entry units: each new code point charges four subpixel
397
+ // variants, so row a (4 cps = 16 entries) fits a 24-entry budget
398
+ // and row b (16 more) crosses it.
399
+ .glyph_budget = 24,
400
+ });
401
+ var saw_a = false;
402
+ var saw_b = false;
403
+ for (builder.displayList().commands) |command| {
404
+ switch (command) {
405
+ .draw_text => |text| {
406
+ if (std.mem.eql(u8, text.text, "abcd")) saw_a = true;
407
+ if (std.mem.eql(u8, text.text, "efgh")) saw_b = true;
408
+ },
409
+ else => {},
410
+ }
411
+ }
412
+ try testing.expect(saw_a);
413
+ try testing.expect(!saw_b);
414
+ }
415
+
416
+ test "a wide row of mergeable box glyphs paints under the widget budget" {
417
+ // 320 identical `─` cells merge to ONE geometry command, so the cost
418
+ // estimate must not charge nine per column and skip the row.
419
+ var box_cells: [320]grid_model.TerminalCell = undefined;
420
+ for (&box_cells) |*c| c.* = .{ .cp = 0x2500, .fg = white };
421
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &box_cells }};
422
+
423
+ var commands: [2048]canvas.CanvasCommand = undefined;
424
+ var builder = try paintInto(baseGrid(&rows), &commands, .{
425
+ .frame = geometry.RectF.init(0, 0, 2600, 40),
426
+ .tokens = .{},
427
+ .command_budget = 1792,
428
+ });
429
+ var box_fills: usize = 0;
430
+ for (builder.displayList().commands) |command| {
431
+ if (command == .fill_rect) box_fills += 1;
432
+ }
433
+ // Background + the single merged bar (+ no wash: no selection). The
434
+ // row painted rather than being skipped for a bogus 2,880 estimate.
435
+ try testing.expect(box_fills >= 2);
436
+ }
437
+
438
+ test "the text preflight counts referenced sibling text already in the list" {
439
+ // A prior widget's REFERENCED draw_text (not builder-owned) counts
440
+ // against the per-view text budget, so the grid must degrade against
441
+ // it. Pre-emit a referenced-text command consuming most of the view
442
+ // budget, then a terminal row that would cross the ceiling drops.
443
+ const filler = "x" ** (canvas.max_display_list_text_bytes - 4);
444
+ const row = comptime asciiRow("cells", white);
445
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &row }};
446
+
447
+ var commands: [64]canvas.CanvasCommand = undefined;
448
+ var builder = canvas.Builder.init(&commands);
449
+ // Referenced text (a slice not from allocTextBytes), as a sibling
450
+ // text widget emits.
451
+ try builder.drawText(.{ .id = 1, .font_id = 2, .size = 12, .origin = geometry.PointF.init(0, 0), .color = white, .text = filler });
452
+ try testing.expectEqual(@as(usize, 0), builder.text_byte_len); // referenced, not builder-owned
453
+
454
+ try grid_model.paint(baseGrid(&rows), &builder, .{
455
+ .frame = geometry.RectF.init(0, 0, 400, 100),
456
+ .tokens = .{},
457
+ });
458
+ // Only 4 bytes of headroom remained, so the 5-byte row dropped: no
459
+ // grid text command, and the frame's total text stays within budget.
460
+ for (builder.displayList().commands) |command| {
461
+ switch (command) {
462
+ .draw_text => |t| try testing.expect(!std.mem.eql(u8, t.text, "cells")),
463
+ else => {},
464
+ }
465
+ }
466
+ }
467
+
468
+ test "the prologue budget check accounts for commands already in the builder" {
469
+ // A builder already near an absolute command ceiling must degrade to
470
+ // nothing rather than emit the prologue and overrun. Pre-fill the
471
+ // builder, then paint with a budget at the current length.
472
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &.{} }};
473
+ var commands: [16]canvas.CanvasCommand = undefined;
474
+ var builder = canvas.Builder.init(&commands);
475
+ for (0..10) |i| {
476
+ try builder.fillRect(.{ .id = 1000 + i, .rect = geometry.RectF.init(0, 0, 1, 1), .fill = .{ .color = white } });
477
+ }
478
+ const before = builder.len;
479
+ try grid_model.paint(baseGrid(&rows), &builder, .{
480
+ .frame = geometry.RectF.init(0, 0, 100, 40),
481
+ .tokens = .{},
482
+ // No room for the prologue above the already-consumed commands.
483
+ .command_budget = before + 2,
484
+ });
485
+ // Nothing emitted (and no unbalanced clip): the len is unchanged.
486
+ try testing.expectEqual(before, builder.len);
487
+ }
488
+
489
+ test "id_base near the u64 ceiling wraps instead of trapping" {
490
+ // paintIdBase spans the full u64 space, so every emitted id offset
491
+ // must wrap. An id_base whose spread lands near maxInt paints a
492
+ // nonempty row without an overflow trap in safe builds.
493
+ const cells = [_]grid_model.TerminalCell{
494
+ cell('x', "x", white),
495
+ .{ .cp = 0x256C, .fg = white }, // ╬ — the eight-command joint
496
+ .{ .cp = 0x256D, .fg = white }, // ╭ — a rounded corner (path elements)
497
+ };
498
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &cells, .selection = .{ 0, 2 } }};
499
+ var grid = baseGrid(&rows);
500
+ grid.cursor = .{ .x = 0, .y = 0 };
501
+ grid.select_head = .{ .x = 0, .y = 0 };
502
+
503
+ var commands: [128]canvas.CanvasCommand = undefined;
504
+ var builder = canvas.Builder.init(&commands);
505
+ // Any id_base is legal; the painter must not trap on the wrap.
506
+ try grid_model.paint(grid, &builder, .{
507
+ .frame = geometry.RectF.init(0, 0, 120, 40),
508
+ .tokens = .{},
509
+ .id_base = 0xffff_ffff_ffff_ffff,
510
+ });
511
+ try testing.expect(builder.displayList().commands.len > 0);
512
+ }
513
+
514
+ test "an underlined merged double run stays within the command budget" {
515
+ // A merged ═ paints TWO bars plus its underline; the cost estimate
516
+ // must charge all three or a row of unmergeable underlined doubles
517
+ // (alternating colors break every merge) overruns the ceiling.
518
+ var cells: [320]grid_model.TerminalCell = undefined;
519
+ for (&cells, 0..) |*c, i| {
520
+ c.* = .{ .cp = 0x2550, .fg = if (i % 2 == 0) white else red, .underline = true };
521
+ }
522
+ const rows = [_]grid_model.TerminalRow{
523
+ .{ .cells = &cells },
524
+ .{ .cells = &cells },
525
+ };
526
+
527
+ var commands: [2048]canvas.CanvasCommand = undefined;
528
+ var builder = try paintInto(baseGrid(&rows), &commands, .{
529
+ .frame = geometry.RectF.init(0, 0, 2600, 80),
530
+ .tokens = .{},
531
+ .command_budget = 1000,
532
+ });
533
+ // Whatever painted, the ceiling held.
534
+ try testing.expect(builder.displayList().commands.len <= 1000);
535
+ }
536
+
537
+ test "an anonymous grid keeps the unkeyed convention: every command id 0" {
538
+ const cells = [_]grid_model.TerminalCell{
539
+ cell('x', "x", white),
540
+ .{ .cp = 0x256C, .fg = white }, // ╬
541
+ .{ .cp = 0x2596, .fg = white }, // quadrant
542
+ .{ .cp = 0x256D, .fg = white }, // rounded corner
543
+ };
544
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &cells, .selection = .{ 0, 1 } }};
545
+ var grid = baseGrid(&rows);
546
+ grid.cursor = .{ .x = 0, .y = 0 };
547
+
548
+ var commands: [128]canvas.CanvasCommand = undefined;
549
+ var builder = try paintInto(grid, &commands, .{
550
+ .frame = geometry.RectF.init(0, 0, 200, 40),
551
+ .tokens = .{},
552
+ .id_base = 0,
553
+ });
554
+ for (builder.displayList().commands) |command| {
555
+ const id = switch (command) {
556
+ .fill_rect => |c| c.id,
557
+ .stroke_rect => |c| c.id,
558
+ .draw_text => |c| c.id,
559
+ .push_clip => |c| c.id,
560
+ .stroke_path => |c| c.id,
561
+ .draw_line => |c| c.id,
562
+ else => continue,
563
+ };
564
+ try testing.expectEqual(@as(canvas.ObjectId, 0), id);
565
+ }
566
+ }
567
+
568
+ test "two keyed grids in one builder never share a nonzero command id" {
569
+ const row_cells = comptime asciiRow("ab", white);
570
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &row_cells }};
571
+ var grid = baseGrid(&rows);
572
+ grid.cursor = .{ .x = 0, .y = 0 };
573
+
574
+ var commands: [128]canvas.CanvasCommand = undefined;
575
+ var builder = canvas.Builder.init(&commands);
576
+ // The adversarial pair: id B chosen so B's base lands inside A's
577
+ // old wide-offset namespace. Under the strided scheme both stay
578
+ // disjoint by construction.
579
+ try grid_model.paint(grid, &builder, .{ .frame = geometry.RectF.init(0, 0, 100, 40), .tokens = .{}, .id_base = 1 });
580
+ try grid_model.paint(grid, &builder, .{ .frame = geometry.RectF.init(0, 60, 100, 40), .tokens = .{}, .id_base = 0x64dd_ccf4_0000_0001 });
581
+ var seen: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
582
+ defer seen.deinit(testing.allocator);
583
+ for (builder.displayList().commands) |command| {
584
+ const id = switch (command) {
585
+ .fill_rect => |c| c.id,
586
+ .stroke_rect => |c| c.id,
587
+ .draw_text => |c| c.id,
588
+ .push_clip => |c| c.id,
589
+ else => continue,
590
+ };
591
+ if (id == 0) continue;
592
+ for (seen.items) |prior| try testing.expect(prior != id);
593
+ try seen.append(testing.allocator, id);
594
+ }
595
+ }
596
+
597
+ test "pure-double corners draw nested L joins, never through-bars" {
598
+ const cells = [_]grid_model.TerminalCell{.{ .cp = 0x2554, .fg = white }}; // ╔
599
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &cells }};
600
+
601
+ var commands: [32]canvas.CanvasCommand = undefined;
602
+ var builder = try paintInto(baseGrid(&rows), &commands, .{
603
+ .frame = geometry.RectF.init(0, 0, 40, 40),
604
+ .tokens = .{},
605
+ });
606
+ // Background + exactly four bars (two nested Ls).
607
+ var bar_fills: usize = 0;
608
+ var min_x: f32 = 1000;
609
+ var min_y: f32 = 1000;
610
+ for (builder.displayList().commands) |command| {
611
+ switch (command) {
612
+ .fill_rect => |fill| {
613
+ // Skip the full-bleed background.
614
+ if (fill.rect.width >= 40) continue;
615
+ bar_fills += 1;
616
+ min_x = @min(min_x, fill.rect.x);
617
+ min_y = @min(min_y, fill.rect.y);
618
+ },
619
+ else => {},
620
+ }
621
+ }
622
+ try testing.expectEqual(@as(usize, 4), bar_fills);
623
+ // ╔ opens down+right: no bar reaches the cell's left or top edge —
624
+ // the old through-bars did (their stubs crossed the joint).
625
+ try testing.expect(min_x > 0);
626
+ try testing.expect(min_y > 0);
627
+ }
628
+
629
+ test "a wide row of one-bar box pieces paints under the widget budget" {
630
+ // 198 columns of │ emit one bar each; the cost estimate must charge
631
+ // what they paint, not a flat joint worst case that rejects the row.
632
+ var cells: [198]grid_model.TerminalCell = undefined;
633
+ for (&cells) |*c| c.* = .{ .cp = 0x2502, .fg = white };
634
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &cells }};
635
+
636
+ var commands: [2048]canvas.CanvasCommand = undefined;
637
+ var builder = try paintInto(baseGrid(&rows), &commands, .{
638
+ .frame = geometry.RectF.init(0, 0, 1700, 40),
639
+ .tokens = .{},
640
+ .command_budget = 1792,
641
+ });
642
+ var bars: usize = 0;
643
+ for (builder.displayList().commands) |command| {
644
+ switch (command) {
645
+ .fill_rect => |fill| {
646
+ if (fill.rect.width < 40) bars += 1;
647
+ },
648
+ else => {},
649
+ }
650
+ }
651
+ try testing.expectEqual(@as(usize, 198), bars);
652
+ }
653
+
654
+ test "a row adding no text paints even when siblings spent the text share" {
655
+ // Earlier widgets already sit past the grid's reserved text share;
656
+ // an all-box row ADDS no text, so it must paint — the ceiling bounds
657
+ // what the grid adds, never what siblings spent.
658
+ const filler = "x" ** (canvas.max_display_list_text_bytes - 100);
659
+ var cells: [8]grid_model.TerminalCell = undefined;
660
+ for (&cells) |*c| c.* = .{ .cp = 0x2500, .fg = white };
661
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &cells }};
662
+
663
+ var commands: [64]canvas.CanvasCommand = undefined;
664
+ var builder = canvas.Builder.init(&commands);
665
+ try builder.drawText(.{ .id = 1, .font_id = 2, .size = 12, .origin = geometry.PointF.init(0, 0), .color = white, .text = filler });
666
+
667
+ try grid_model.paint(baseGrid(&rows), &builder, .{
668
+ .frame = geometry.RectF.init(0, 0, 100, 40),
669
+ .tokens = .{},
670
+ // The default widget reserve leaves less than the filler already
671
+ // consumed; the box row adds zero text and must still paint.
672
+ .text_reserve = grid_model.widget_text_reserve,
673
+ });
674
+ var box_fills: usize = 0;
675
+ for (builder.displayList().commands) |command| {
676
+ switch (command) {
677
+ .fill_rect => |fill| {
678
+ if (fill.rect.width < 100) box_fills += 1;
679
+ },
680
+ else => {},
681
+ }
682
+ }
683
+ try testing.expect(box_fills >= 1);
684
+ }
685
+
686
+ test "a combining-mark cluster paints alone; its neighbor keeps its cell origin" {
687
+ // "e" + combining acute in cell 0, "!" in cell 1: the cluster must
688
+ // paint as its own run and "!" must start at exactly one cell width
689
+ // — a layout that advanced the mark by a full glyph inside a merged
690
+ // run would have shifted "!" off the grid.
691
+ const cells = [_]grid_model.TerminalCell{
692
+ .{ .cp = 'e', .cluster = "e\u{0301}", .fg = white },
693
+ cell('!', "!", white),
694
+ };
695
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &cells }};
696
+
697
+ var commands: [32]canvas.CanvasCommand = undefined;
698
+ var builder = try paintInto(baseGrid(&rows), &commands, .{
699
+ .frame = geometry.RectF.init(0, 0, 100, 40),
700
+ .tokens = .{},
701
+ });
702
+ const tokens: canvas.DesignTokens = .{};
703
+ const metrics = grid_model.cellMetrics(tokens);
704
+ var saw_cluster = false;
705
+ var saw_bang = false;
706
+ for (builder.displayList().commands) |command| {
707
+ switch (command) {
708
+ .draw_text => |text| {
709
+ if (std.mem.eql(u8, text.text, "e\u{0301}")) {
710
+ saw_cluster = true;
711
+ try testing.expectApproxEqAbs(@as(f32, 0), text.origin.x, 0.01);
712
+ }
713
+ if (std.mem.eql(u8, text.text, "!")) {
714
+ saw_bang = true;
715
+ try testing.expectApproxEqAbs(metrics.width, text.origin.x, 0.01);
716
+ }
717
+ },
718
+ else => {},
719
+ }
720
+ }
721
+ try testing.expect(saw_cluster);
722
+ try testing.expect(saw_bang);
723
+ }
724
+
725
+ test "the widget diff reports paint damage for a changed bound grid" {
726
+ const row_cells = comptime asciiRow("a", white);
727
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &row_cells }};
728
+ const grid_a = baseGrid(&rows);
729
+ var grid_b = baseGrid(&rows);
730
+ grid_b.cursor = .{ .x = 0, .y = 0 };
731
+
732
+ var widget = canvas.Widget{
733
+ .id = 11,
734
+ .kind = .terminal,
735
+ .frame = geometry.RectF.init(0, 0, 200, 100),
736
+ .terminal = .{ .pty = 1, .grid = &grid_a },
737
+ };
738
+ var nodes_a: [4]canvas.WidgetLayoutNode = undefined;
739
+ const layout_a = try canvas.layoutWidgetTree(widget, geometry.RectF.init(0, 0, 200, 100), &nodes_a);
740
+ widget.terminal.grid = &grid_b;
741
+ var nodes_b: [4]canvas.WidgetLayoutNode = undefined;
742
+ const layout_b = try canvas.layoutWidgetTree(widget, geometry.RectF.init(0, 0, 200, 100), &nodes_b);
743
+
744
+ var output: [8]canvas.WidgetInvalidation = undefined;
745
+ const changes = try layout_a.diff(layout_b, &output);
746
+ var saw_paint_dirty = false;
747
+ for (changes) |change| {
748
+ if (change.id == 11 and change.paint_dirty) saw_paint_dirty = true;
749
+ }
750
+ try testing.expect(saw_paint_dirty);
751
+ }
752
+
753
+ test "clampGrid trades rows for columns under the cell ceiling" {
754
+ const clamped = grid_model.clampGrid(400, 100);
755
+ try testing.expectEqual(@as(u16, grid_model.max_cols), clamped.x);
756
+ try testing.expect(@as(usize, clamped.x) * @as(usize, clamped.y) <= grid_model.max_cells);
757
+ const tiny = grid_model.clampGrid(0, 0);
758
+ try testing.expectEqual(@as(u16, 2), tiny.x);
759
+ try testing.expectEqual(@as(u16, 2), tiny.y);
760
+ }
761
+
762
+ test "the path reserve holds one maximal filled-line chart series" {
763
+ // A 256-point filled line strokes its polyline and fills its area
764
+ // (points plus closure vertices): the reserve must cover both, or a
765
+ // terminal of rounded corners starves the chart into
766
+ // ChartPathElementListFull.
767
+ try testing.expect(grid_model.widget_path_reserve >= 2 * canvas.max_chart_points_per_series + 3);
768
+ }
769
+
770
+ /// A mono face with a WIDER pitch than the estimator's 0.6 em — what
771
+ /// macOS resolves the mono id to when Geist Mono is absent (the system
772
+ /// monospaced face, 0.618 em). Everything mono measures at this pitch,
773
+ /// so cells and runs both derive from it.
774
+ const wide_pitch_em: f32 = 0.62;
775
+
776
+ fn measureWidePitch(context: ?*anyopaque, font_id: canvas.FontId, size: f32, text: []const u8) f32 {
777
+ _ = context;
778
+ _ = font_id;
779
+ var count: f32 = 0;
780
+ var index: usize = 0;
781
+ while (index < text.len) : (index += 1) {
782
+ if (text[index] & 0xc0 != 0x80) count += 1;
783
+ }
784
+ return count * size * wide_pitch_em;
785
+ }
786
+
787
+ const wide_pitch_provider = canvas.TextMeasureProvider{ .measure_fn = measureWidePitch };
788
+
789
+ test "a full-width row's runs declare bounds that cover their ink" {
790
+ // The regression: a merged run's raster extent is its own declared
791
+ // bounds, and an estimator-only bound (0.6 em) falls short of a
792
+ // wider host face's ink — the row's last cell sheared off at the
793
+ // widget edge. Cells and bounds must both come from the measurement
794
+ // the host inks with.
795
+ const tokens = canvas.DesignTokens{ .text_measure = &wide_pitch_provider };
796
+ const metrics = grid_model.cellMetrics(tokens);
797
+ try testing.expectApproxEqAbs(
798
+ tokens.typography.label_size * wide_pitch_em,
799
+ metrics.width,
800
+ 0.001,
801
+ );
802
+
803
+ const row_cells = comptime asciiRow("0123456789012345678901234567890123456789", white);
804
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &row_cells }};
805
+ const frame = geometry.RectF.init(
806
+ 0,
807
+ 0,
808
+ metrics.width * @as(f32, @floatFromInt(row_cells.len)),
809
+ metrics.height * 2,
810
+ );
811
+
812
+ var commands: [512]canvas.CanvasCommand = undefined;
813
+ var builder = try paintInto(baseGrid(&rows), &commands, .{
814
+ .frame = frame,
815
+ .tokens = tokens,
816
+ });
817
+
818
+ var saw_run = false;
819
+ for (builder.displayList().commands) |command| {
820
+ switch (command) {
821
+ .draw_text => |text| {
822
+ saw_run = true;
823
+ const bounds = canvas.CanvasCommand{ .draw_text = text };
824
+ const rect = bounds.bounds() orelse return error.MissingTextBounds;
825
+ const cells: f32 = @floatFromInt(text.text.len);
826
+ const ink_right = text.origin.x + cells * metrics.width;
827
+ try testing.expect(rect.x + rect.width >= ink_right);
828
+ // The run's own frame has to hold it too — a row that
829
+ // fits the grid's columns can never need more width
830
+ // than the cells it was measured into.
831
+ try testing.expect(ink_right <= frame.x + frame.width + 0.001);
832
+ },
833
+ else => {},
834
+ }
835
+ }
836
+ try testing.expect(saw_run);
837
+ }
838
+
839
+ test "cell metrics derive from the mono face and never collapse" {
840
+ const metrics = grid_model.cellMetrics(.{});
841
+ try testing.expect(metrics.width > 0);
842
+ try testing.expect(metrics.height >= metrics.font_size);
843
+ }
844
+
845
+ test "the terminal widget paints its bound grid and the honest empty surface unbound" {
846
+ const row_cells = comptime asciiRow("bound", white);
847
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &row_cells }};
848
+ const grid = baseGrid(&rows);
849
+
850
+ // A realistic per-view builder (the widget path floors a builder at
851
+ // or under the reserve to a degrade-only budget).
852
+ var commands: [2048]canvas.CanvasCommand = undefined;
853
+ var builder = canvas.Builder.init(&commands);
854
+ const bound = canvas.Widget{
855
+ .id = 7,
856
+ .kind = .terminal,
857
+ .frame = geometry.RectF.init(0, 0, 400, 200),
858
+ .terminal = .{ .pty = 1, .grid = &grid },
859
+ };
860
+ try canvas.emitWidgetTree(&builder, bound, .{});
861
+ var saw_text = false;
862
+ for (builder.displayList().commands) |command| {
863
+ switch (command) {
864
+ .draw_text => |text| {
865
+ if (std.mem.eql(u8, text.text, "bound")) saw_text = true;
866
+ },
867
+ else => {},
868
+ }
869
+ }
870
+ try testing.expect(saw_text);
871
+
872
+ // Unbound: one background fill, no text, never a hole.
873
+ var empty_commands: [16]canvas.CanvasCommand = undefined;
874
+ var empty_builder = canvas.Builder.init(&empty_commands);
875
+ const unbound = canvas.Widget{
876
+ .id = 8,
877
+ .kind = .terminal,
878
+ .frame = geometry.RectF.init(0, 0, 400, 200),
879
+ };
880
+ try canvas.emitWidgetTree(&empty_builder, unbound, .{});
881
+ var fills: usize = 0;
882
+ var texts: usize = 0;
883
+ for (empty_builder.displayList().commands) |command| {
884
+ switch (command) {
885
+ .fill_rect => fills += 1,
886
+ .draw_text => texts += 1,
887
+ else => {},
888
+ }
889
+ }
890
+ try testing.expectEqual(@as(usize, 1), fills);
891
+ try testing.expectEqual(@as(usize, 0), texts);
892
+ }
893
+
894
+ test "a wide cheap terminal paints its rows under the widget command budget" {
895
+ // 198 columns of plain ASCII across several rows: the per-row cost
896
+ // preflight must let these cheap rows paint (a flat worst-case-per-
897
+ // column reserve would seat none). Regression for the over-reserve
898
+ // that blanked wide grids.
899
+ var storage: [6][198]grid_model.TerminalCell = undefined;
900
+ for (&storage) |*row_cells| {
901
+ for (row_cells) |*entry| entry.* = cell('a', "a", white);
902
+ }
903
+ var rows: [6]grid_model.TerminalRow = undefined;
904
+ for (&rows, 0..) |*r, i| r.* = .{ .cells = &storage[i] };
905
+
906
+ var commands: [2048]canvas.CanvasCommand = undefined;
907
+ var builder = try paintInto(baseGrid(&rows), &commands, .{
908
+ .frame = geometry.RectF.init(0, 0, 1600, 200),
909
+ .tokens = .{},
910
+ .command_budget = 1792,
911
+ });
912
+ var text_rows: usize = 0;
913
+ for (builder.displayList().commands) |command| {
914
+ switch (command) {
915
+ .draw_text => |t| if (std.mem.eql(u8, t.text, "a" ** 198)) {
916
+ text_rows += 1;
917
+ },
918
+ else => {},
919
+ }
920
+ }
921
+ // Every row's run merges to one draw_text; all six must paint.
922
+ try testing.expectEqual(@as(usize, 6), text_rows);
923
+ }
924
+
925
+ test "a focused bound terminal's ring id never collides with a grid command" {
926
+ const row_cells = comptime asciiRow("shell", white);
927
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &row_cells }};
928
+ const grid = baseGrid(&rows);
929
+
930
+ var commands: [2048]canvas.CanvasCommand = undefined;
931
+ var builder = canvas.Builder.init(&commands);
932
+ const focused = canvas.Widget{
933
+ // A high-entropy id in the range where the multiplicative spread
934
+ // could alias a widgetPartId ring slot.
935
+ .id = 0x2af6_89b9_153d_e19a,
936
+ .kind = .terminal,
937
+ .frame = geometry.RectF.init(0, 0, 400, 200),
938
+ .state = .{ .focused = true },
939
+ .terminal = .{ .pty = 1, .grid = &grid },
940
+ };
941
+ try canvas.emitWidgetTree(&builder, focused, .{});
942
+ var ring_id: canvas.ObjectId = 0;
943
+ for (builder.displayList().commands) |command| {
944
+ if (command == .stroke_rect) ring_id = command.stroke_rect.id;
945
+ }
946
+ try testing.expect(ring_id != 0);
947
+ // No other command shares the ring's id.
948
+ var collisions: usize = 0;
949
+ for (builder.displayList().commands) |command| {
950
+ const id = switch (command) {
951
+ .fill_rect => |c| c.id,
952
+ .draw_text => |c| c.id,
953
+ .push_clip => |c| c.id,
954
+ .stroke_path => |c| c.id,
955
+ else => continue,
956
+ };
957
+ if (id == ring_id) collisions += 1;
958
+ }
959
+ try testing.expectEqual(@as(usize, 0), collisions);
960
+ }
961
+
962
+ test "a focused terminal wears the house focus ring" {
963
+ var commands: [16]canvas.CanvasCommand = undefined;
964
+ var builder = canvas.Builder.init(&commands);
965
+ const focused = canvas.Widget{
966
+ .id = 9,
967
+ .kind = .terminal,
968
+ .frame = geometry.RectF.init(0, 0, 400, 200),
969
+ .state = .{ .focused = true },
970
+ };
971
+ try canvas.emitWidgetTree(&builder, focused, .{});
972
+ var rings: usize = 0;
973
+ for (builder.displayList().commands) |command| {
974
+ switch (command) {
975
+ .stroke_rect => rings += 1,
976
+ else => {},
977
+ }
978
+ }
979
+ try testing.expectEqual(@as(usize, 1), rings);
980
+ }
981
+
982
+ test "the terminal widget's register: focusable, press-claiming, I-beam, editable-text role" {
983
+ const widget_access = @import("widget_access.zig");
984
+ const widget_semantics = @import("widget_semantics.zig");
985
+ const widget = canvas.Widget{ .id = 3, .kind = .terminal };
986
+ try testing.expect(canvas.widgetKindHitTarget(.terminal));
987
+ try testing.expect(widget_access.isFocusable(widget));
988
+ try testing.expect(canvas.widgetClaimsPress(widget));
989
+ try testing.expectEqual(canvas.WidgetCursor.text, canvas.cursorForWidgetTarget(.terminal, .{}));
990
+ try testing.expectEqual(canvas.WidgetRole.textbox, widget_semantics.semanticRole(widget));
991
+ // Deliberately NOT a text-input kind: the emulator owns the editing
992
+ // model, so the TextBuffer pipeline must never claim it.
993
+ try testing.expect(!canvas.widgetTextInputKind(.terminal));
994
+ }
995
+
996
+ test "rounded-corner path elements survive the builder's lifetime" {
997
+ // The stroke_path command's elements must be builder-owned, not a
998
+ // stack local: after paint returns, reading them back (the retained
999
+ // renderer's path) must still see the move/line/quad verbs.
1000
+ const cells = [_]grid_model.TerminalCell{.{ .cp = 0x256D, .fg = white }}; // ╭
1001
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &cells }};
1002
+
1003
+ var commands: [64]canvas.CanvasCommand = undefined;
1004
+ var builder = try paintInto(baseGrid(&rows), &commands, .{
1005
+ .frame = geometry.RectF.init(0, 0, 40, 40),
1006
+ .tokens = .{},
1007
+ });
1008
+ var saw_path = false;
1009
+ for (builder.displayList().commands) |command| {
1010
+ switch (command) {
1011
+ .stroke_path => |path| {
1012
+ saw_path = true;
1013
+ try testing.expectEqual(@as(usize, 3), path.elements.len);
1014
+ try testing.expectEqual(canvas.PathVerb.move_to, path.elements[0].verb);
1015
+ try testing.expectEqual(canvas.PathVerb.quad_to, path.elements[2].verb);
1016
+ },
1017
+ else => {},
1018
+ }
1019
+ }
1020
+ try testing.expect(saw_path);
1021
+ }
1022
+
1023
+ test "adjacent double-cross cells never collide command ids" {
1024
+ // ╬ emits up to eight commands; the per-column id stride must be at
1025
+ // least eight so neighbors never share an object id.
1026
+ const cells = [_]grid_model.TerminalCell{
1027
+ .{ .cp = 0x256C, .fg = white }, // ╬
1028
+ .{ .cp = 0x256C, .fg = white },
1029
+ .{ .cp = 0x256C, .fg = white },
1030
+ };
1031
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &cells }};
1032
+
1033
+ var commands: [128]canvas.CanvasCommand = undefined;
1034
+ var builder = try paintInto(baseGrid(&rows), &commands, .{
1035
+ .frame = geometry.RectF.init(0, 0, 120, 40),
1036
+ .tokens = .{},
1037
+ .id_base = 1,
1038
+ });
1039
+ var seen: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
1040
+ defer seen.deinit(testing.allocator);
1041
+ for (builder.displayList().commands) |command| {
1042
+ const id = switch (command) {
1043
+ .fill_rect => |c| c.id,
1044
+ .stroke_rect => |c| c.id,
1045
+ .draw_text => |c| c.id,
1046
+ .push_clip => |c| c.id,
1047
+ else => continue,
1048
+ };
1049
+ if (id == 0) continue;
1050
+ for (seen.items) |prior| try testing.expect(prior != id);
1051
+ try seen.append(testing.allocator, id);
1052
+ }
1053
+ }
1054
+
1055
+ test "the text preflight accounts for bytes earlier widgets already consumed" {
1056
+ // A grid sharing the builder with a widget that already spent most
1057
+ // of the text store must degrade against the REMAINING space, never
1058
+ // a fresh store — otherwise its rows pass preflight and then lose
1059
+ // text when allocTextBytes fails on the shared counter.
1060
+ const row = comptime asciiRow("cells", white);
1061
+ const rows = [_]grid_model.TerminalRow{.{ .cells = &row }};
1062
+
1063
+ var commands: [64]canvas.CanvasCommand = undefined;
1064
+ var builder = canvas.Builder.init(&commands);
1065
+ // Pre-consume all but 2 bytes of the store, as an earlier widget
1066
+ // would.
1067
+ const filler = [_]u8{'x'} ** (canvas.max_display_list_text_bytes - 2);
1068
+ _ = try builder.allocTextBytes(&filler);
1069
+
1070
+ try grid_model.paint(baseGrid(&rows), &builder, .{
1071
+ .frame = geometry.RectF.init(0, 0, 400, 100),
1072
+ .tokens = .{},
1073
+ });
1074
+ // The row (5 bytes) could not fit the 2 remaining, so it dropped
1075
+ // whole: no grid text command, and the shared counter never
1076
+ // overflowed.
1077
+ try testing.expect(builder.text_byte_len <= canvas.max_display_list_text_bytes);
1078
+ for (builder.displayList().commands) |command| {
1079
+ switch (command) {
1080
+ .draw_text => |text| try testing.expect(!std.mem.eql(u8, text.text, "cells")),
1081
+ else => {},
1082
+ }
1083
+ }
1084
+ }
1085
+
1086
+ test "box drawing classifies the block and ignores neighbors" {
1087
+ try testing.expect(box.isBoxDrawing(0x2500));
1088
+ try testing.expect(box.isBoxDrawing(0x259F));
1089
+ try testing.expect(!box.isBoxDrawing(0x24FF));
1090
+ try testing.expect(!box.isBoxDrawing(0x25A0));
1091
+ try testing.expect(box.mergesHorizontally(0x2500));
1092
+ try testing.expect(!box.mergesHorizontally(0x2502));
1093
+ }