@native-sdk/cli 0.5.3 → 0.6.0

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 (172) hide show
  1. package/build/app.zig +91 -2
  2. package/build.zig +756 -5
  3. package/build.zig.zon +7 -0
  4. package/native-sdk.d.ts +19 -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 +416 -9
  9. package/packages/core/sdk/core.ts +706 -5
  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 +3187 -264
  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 +42 -4
  17. package/skill-data/ts-core/SKILL.md +13 -3
  18. package/src/app_runner/root.zig +112 -10
  19. package/src/app_runner/ts_core_main.zig +4 -0
  20. package/src/automation/layout_fingerprint.zig +160 -0
  21. package/src/automation/protocol.zig +82 -33
  22. package/src/automation/root.zig +1 -0
  23. package/src/automation/server.zig +1 -1
  24. package/src/automation/snapshot.zig +68 -9
  25. package/src/automation/watcher.zig +1 -1
  26. package/src/embed/host.zig +5 -0
  27. package/src/embed/tests.zig +119 -1
  28. package/src/embed/ui_host.zig +13 -0
  29. package/src/platform/linux/gtk_host.c +639 -16
  30. package/src/platform/linux/gtk_host.h +25 -0
  31. package/src/platform/linux/root.zig +351 -7
  32. package/src/platform/macos/appkit_host.h +152 -8
  33. package/src/platform/macos/appkit_host.m +1816 -100
  34. package/src/platform/macos/cef_host.mm +372 -11
  35. package/src/platform/macos/root.zig +379 -11
  36. package/src/platform/null_platform.zig +696 -9
  37. package/src/platform/null_platform_tests.zig +35 -0
  38. package/src/platform/root.zig +42 -0
  39. package/src/platform/types.zig +524 -18
  40. package/src/platform/windows/root.zig +432 -9
  41. package/src/platform/windows/webview2_host.cpp +482 -45
  42. package/src/primitives/app_manifest/comptime_scene.zig +5 -0
  43. package/src/primitives/app_manifest/root.zig +1 -0
  44. package/src/primitives/app_manifest/types.zig +32 -0
  45. package/src/primitives/app_manifest/validation.zig +18 -0
  46. package/src/primitives/canvas/a11y_audit.zig +33 -11
  47. package/src/primitives/canvas/a11y_audit_tests.zig +20 -0
  48. package/src/primitives/canvas/drawing.zig +25 -0
  49. package/src/primitives/canvas/equality.zig +8 -0
  50. package/src/primitives/canvas/events.zig +103 -16
  51. package/src/primitives/canvas/frame.zig +125 -1
  52. package/src/primitives/canvas/layout_audit.zig +22 -5
  53. package/src/primitives/canvas/markdown_hostile_tests.zig +8 -4
  54. package/src/primitives/canvas/render_tests.zig +13 -10
  55. package/src/primitives/canvas/root.zig +48 -0
  56. package/src/primitives/canvas/terminal_box.zig +471 -0
  57. package/src/primitives/canvas/terminal_grid.zig +978 -0
  58. package/src/primitives/canvas/terminal_grid_tests.zig +1093 -0
  59. package/src/primitives/canvas/test_support.zig +1 -0
  60. package/src/primitives/canvas/tests.zig +2 -0
  61. package/src/primitives/canvas/text_tests.zig +39 -4
  62. package/src/primitives/canvas/tokens.zig +115 -12
  63. package/src/primitives/canvas/ui.zig +535 -12
  64. package/src/primitives/canvas/ui_markup.zig +253 -36
  65. package/src/primitives/canvas/ui_markup_binary.zig +4 -1
  66. package/src/primitives/canvas/ui_markup_compiled.zig +195 -9
  67. package/src/primitives/canvas/ui_markup_compiled_tests.zig +188 -8
  68. package/src/primitives/canvas/ui_markup_contract.zig +89 -6
  69. package/src/primitives/canvas/ui_markup_contract_tests.zig +65 -1
  70. package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
  71. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +1 -0
  72. package/src/primitives/canvas/ui_markup_reflect.zig +75 -24
  73. package/src/primitives/canvas/ui_markup_tests.zig +178 -9
  74. package/src/primitives/canvas/ui_markup_view.zig +220 -11
  75. package/src/primitives/canvas/ui_markup_view_tests.zig +553 -28
  76. package/src/primitives/canvas/ui_schema.zig +110 -17
  77. package/src/primitives/canvas/ui_schema_tests.zig +18 -10
  78. package/src/primitives/canvas/ui_tests.zig +141 -9
  79. package/src/primitives/canvas/widget_access.zig +28 -2
  80. package/src/primitives/canvas/widget_anchored_tests.zig +48 -0
  81. package/src/primitives/canvas/widget_invalidation.zig +81 -1
  82. package/src/primitives/canvas/widget_layout.zig +59 -11
  83. package/src/primitives/canvas/widget_layout_tests.zig +125 -4
  84. package/src/primitives/canvas/widget_render.zig +272 -18
  85. package/src/primitives/canvas/widget_render_scroll.zig +108 -27
  86. package/src/primitives/canvas/widget_routing.zig +59 -7
  87. package/src/primitives/canvas/widget_runtime.zig +23 -0
  88. package/src/primitives/canvas/widget_runtime_tests.zig +149 -0
  89. package/src/primitives/canvas/widget_semantics.zig +119 -11
  90. package/src/primitives/canvas/widget_semantics_tests.zig +409 -0
  91. package/src/primitives/canvas/widget_tree.zig +17 -0
  92. package/src/primitives/canvas/widgets.zig +132 -0
  93. package/src/root.zig +23 -0
  94. package/src/runtime/api.zig +76 -5
  95. package/src/runtime/automation_commands.zig +20 -1
  96. package/src/runtime/automation_snapshot.zig +25 -1
  97. package/src/runtime/automation_widget_dispatch.zig +73 -8
  98. package/src/runtime/bridge_payload.zig +8 -0
  99. package/src/runtime/bridge_responses.zig +5 -0
  100. package/src/runtime/builtin_bridge.zig +9 -4
  101. package/src/runtime/canvas_font_tests.zig +439 -2
  102. package/src/runtime/canvas_fonts.zig +61 -6
  103. package/src/runtime/canvas_frame.zig +62 -7
  104. package/src/runtime/canvas_frame_animation_tests.zig +5 -4
  105. package/src/runtime/canvas_frame_helpers.zig +190 -1
  106. package/src/runtime/canvas_frame_invalidation_tests.zig +531 -0
  107. package/src/runtime/canvas_frame_patch_tests.zig +161 -10
  108. package/src/runtime/canvas_frame_retained_tests.zig +5 -2
  109. package/src/runtime/canvas_image_tests.zig +195 -0
  110. package/src/runtime/canvas_images.zig +45 -12
  111. package/src/runtime/canvas_widget_accessibility_tests.zig +1 -1
  112. package/src/runtime/canvas_widget_clipboard_tests.zig +40 -0
  113. package/src/runtime/canvas_widget_context_menu.zig +269 -32
  114. package/src/runtime/canvas_widget_context_menu_tests.zig +640 -14
  115. package/src/runtime/canvas_widget_display.zig +1 -1
  116. package/src/runtime/canvas_widget_event_tests.zig +1823 -0
  117. package/src/runtime/canvas_widget_events.zig +240 -24
  118. package/src/runtime/canvas_widget_runtime.zig +213 -29
  119. package/src/runtime/canvas_widget_scroll_driver_tests.zig +170 -7
  120. package/src/runtime/canvas_widget_scroll_drivers.zig +151 -17
  121. package/src/runtime/canvas_widget_scroll_tests.zig +435 -23
  122. package/src/runtime/canvas_widget_state.zig +6 -0
  123. package/src/runtime/canvas_widget_text_tests.zig +1 -1
  124. package/src/runtime/core.zig +131 -13
  125. package/src/runtime/effects.zig +8136 -180
  126. package/src/runtime/effects_channel_tests.zig +3519 -0
  127. package/src/runtime/effects_fetch_tests.zig +89 -1
  128. package/src/runtime/effects_file_tests.zig +41 -1
  129. package/src/runtime/effects_image_tests.zig +1304 -0
  130. package/src/runtime/effects_pty_tests.zig +1362 -0
  131. package/src/runtime/effects_tests.zig +167 -9
  132. package/src/runtime/effects_video_tests.zig +3609 -0
  133. package/src/runtime/flow.zig +42 -4
  134. package/src/runtime/gpu_surface_events.zig +473 -13
  135. package/src/runtime/media_surface.zig +116 -11
  136. package/src/runtime/media_surface_tests.zig +61 -0
  137. package/src/runtime/pty.zig +1252 -0
  138. package/src/runtime/pty_windows.zig +1780 -0
  139. package/src/runtime/root.zig +36 -0
  140. package/src/runtime/runtime_event_tests.zig +20 -1
  141. package/src/runtime/session_blobs.zig +689 -0
  142. package/src/runtime/session_journal.zig +432 -62
  143. package/src/runtime/session_record.zig +43 -2
  144. package/src/runtime/session_replay.zig +556 -20
  145. package/src/runtime/session_tests.zig +2087 -1
  146. package/src/runtime/shell_layout.zig +7 -0
  147. package/src/runtime/terminal_session.zig +1569 -0
  148. package/src/runtime/terminal_session_tests.zig +450 -0
  149. package/src/runtime/terminal_vt_ghostty.zig +15 -0
  150. package/src/runtime/terminal_vt_stub.zig +13 -0
  151. package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
  152. package/src/runtime/testdata/fonts/OFL.txt +93 -0
  153. package/src/runtime/tests.zig +8 -0
  154. package/src/runtime/ts_core_host.zig +1320 -45
  155. package/src/runtime/ts_core_host_tests.zig +1100 -1
  156. package/src/runtime/ts_ui_app.zig +23 -5
  157. package/src/runtime/ui_app.zig +2444 -69
  158. package/src/runtime/ui_app_tests.zig +3171 -744
  159. package/src/runtime/ui_app_virtual_variable_tests.zig +4 -2
  160. package/src/runtime/ui_app_window_tests.zig +736 -0
  161. package/src/runtime/view.zig +190 -2
  162. package/src/runtime/view_widget_scroll.zig +218 -68
  163. package/src/runtime/view_widget_text.zig +11 -2
  164. package/src/runtime/view_widget_tree.zig +111 -3
  165. package/src/runtime/window_command_bridge_tests.zig +117 -0
  166. package/src/runtime/window_storage.zig +77 -2
  167. package/src/runtime/window_view_runtime.zig +34 -2
  168. package/src/runtime/window_views.zig +62 -1
  169. package/src/terminal_session_tests_root.zig +15 -0
  170. package/src/tooling/manifest.zig +75 -0
  171. package/src/tooling/raw_manifest.zig +2 -0
  172. package/src/tooling/templates.zig +244 -8
@@ -3,7 +3,8 @@
3
3
  //! platform's `context_menu_action` event resolves to a
4
4
  //! `.canvas_widget_context_menu` runtime event for app-declared menus,
5
5
  //! and the zero-code defaults (editable-text Cut/Copy/Paste/Select All,
6
- //! static-selection Copy) drive the existing clipboard actions.
6
+ //! terminal Copy/Paste, static-selection Copy) drive the existing
7
+ //! clipboard and committed-input actions.
7
8
 
8
9
  const support = @import("test_support.zig");
9
10
  const std = support.std;
@@ -23,18 +24,44 @@ const MenuTestApp = struct {
23
24
  menu_count: u32 = 0,
24
25
  last_menu_target: canvas.ObjectId = 0,
25
26
  last_menu_item_index: usize = 0,
27
+ last_menu_label: [32]u8 = undefined,
28
+ last_menu_label_len: usize = 0,
29
+ shown_count: u32 = 0,
30
+ last_shown_token: u64 = 0,
31
+ last_shown_label: [32]u8 = undefined,
32
+ last_shown_label_len: usize = 0,
26
33
  request_count: u32 = 0,
27
34
  last_request_target: canvas.ObjectId = 0,
35
+ last_request_point: geometry.PointF = .{},
28
36
  last_edit_insert: [64]u8 = undefined,
29
37
  last_edit_insert_len: usize = 0,
38
+ keyboard_count: u32 = 0,
39
+ last_keyboard_phase: canvas.WidgetKeyboardPhase = .key_down,
40
+ last_keyboard_focused_id: ?canvas.ObjectId = null,
41
+ last_keyboard_terminal_paste: bool = false,
42
+ last_keyboard_standalone: bool = false,
30
43
  saw_truncated: bool = false,
44
+ dismissed_count: u32 = 0,
45
+ last_dismissed_token: u64 = 0,
46
+ last_dismissed_label: [32]u8 = undefined,
47
+ last_dismissed_label_len: usize = 0,
48
+ /// When set, the dismissed-notice handler errors AFTER recording
49
+ /// the notice — the app heard it, then blew up.
50
+ dismissal_error: ?anyerror = null,
51
+ /// When set, the dismissed-notice handler closes this view on
52
+ /// window 1 — app code that compacts the runtime's view indices
53
+ /// while a supersession is mid-dispatch.
54
+ close_view_on_dismissal: ?[]const u8 = null,
55
+ /// When set, the dismissed-notice handler right-clicks this view's
56
+ /// declared-menu widget — app code that synchronously presents a
57
+ /// SUPERSEDING menu while a supersession is mid-dispatch.
58
+ present_menu_on_dismissal: ?[]const u8 = null,
31
59
 
32
60
  fn app(self: *@This()) App {
33
61
  return .{ .context = self, .name = "context-menus", .source = platform.WebViewSource.html("<h1>Hello</h1>"), .event_fn = event };
34
62
  }
35
63
 
36
64
  fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
37
- _ = runtime;
38
65
  const self: *@This() = @ptrCast(@alignCast(context));
39
66
  switch (event_value) {
40
67
  .canvas_widget_pointer => self.pointer_count += 1,
@@ -43,12 +70,44 @@ const MenuTestApp = struct {
43
70
  self.menu_count += 1;
44
71
  self.last_menu_target = menu_event.target_id;
45
72
  self.last_menu_item_index = menu_event.item_index;
73
+ const len = @min(menu_event.view_label.len, self.last_menu_label.len);
74
+ @memcpy(self.last_menu_label[0..len], menu_event.view_label[0..len]);
75
+ self.last_menu_label_len = len;
76
+ },
77
+ .canvas_widget_context_menu_shown => |shown_event| {
78
+ self.shown_count += 1;
79
+ self.last_shown_token = shown_event.token;
80
+ const len = @min(shown_event.view_label.len, self.last_shown_label.len);
81
+ @memcpy(self.last_shown_label[0..len], shown_event.view_label[0..len]);
82
+ self.last_shown_label_len = len;
46
83
  },
47
84
  .canvas_widget_context_menu_request => |request_event| {
48
85
  self.request_count += 1;
49
86
  self.last_request_target = request_event.target_id;
87
+ self.last_request_point = request_event.point;
88
+ },
89
+ .canvas_widget_context_menu_dismissed => |dismissed_event| {
90
+ self.dismissed_count += 1;
91
+ self.last_dismissed_token = dismissed_event.token;
92
+ const len = @min(dismissed_event.view_label.len, self.last_dismissed_label.len);
93
+ @memcpy(self.last_dismissed_label[0..len], dismissed_event.view_label[0..len]);
94
+ self.last_dismissed_label_len = len;
95
+ if (self.close_view_on_dismissal) |close_label| {
96
+ self.close_view_on_dismissal = null;
97
+ try runtime.closeView(1, close_label);
98
+ }
99
+ if (self.present_menu_on_dismissal) |present_label| {
100
+ self.present_menu_on_dismissal = null;
101
+ try runtime.dispatchPlatformEvent(self.app(), rightClickOn(present_label, 50, 20));
102
+ }
103
+ if (self.dismissal_error) |err| return err;
50
104
  },
51
105
  .canvas_widget_keyboard => |keyboard_event| {
106
+ self.keyboard_count += 1;
107
+ self.last_keyboard_phase = keyboard_event.keyboard.phase;
108
+ self.last_keyboard_focused_id = keyboard_event.keyboard.focused_id;
109
+ self.last_keyboard_terminal_paste = keyboard_event.terminal_paste;
110
+ self.last_keyboard_standalone = keyboard_event.standalone;
52
111
  if (keyboard_event.keyboard.edit_truncated) self.saw_truncated = true;
53
112
  if (keyboard_event.keyboard.edit) |edit| switch (edit) {
54
113
  .insert_text => |text| {
@@ -65,9 +124,13 @@ const MenuTestApp = struct {
65
124
  };
66
125
 
67
126
  fn rightClick(x: f32, y: f32) platform.Event {
127
+ return rightClickOn("canvas", x, y);
128
+ }
129
+
130
+ fn rightClickOn(label: []const u8, x: f32, y: f32) platform.Event {
68
131
  return .{ .gpu_surface_input = .{
69
132
  .window_id = 1,
70
- .label = "canvas",
133
+ .label = label,
71
134
  .kind = .pointer_down,
72
135
  .button = 1,
73
136
  .x = x,
@@ -126,7 +189,9 @@ test "right click over a widget with a declared menu presents it natively and di
126
189
  // items (ids are 1-based item indices; separators stay separators).
127
190
  try std.testing.expectEqual(@as(usize, 1), harness.null_platform.context_menu_request_count);
128
191
  try std.testing.expectEqualStrings("canvas", harness.null_platform.contextMenuLabel());
129
- try std.testing.expectEqual(@as(u64, 2), harness.null_platform.context_menu_token);
192
+ // The request's correlation token is minted per request (opaque to
193
+ // the platform, which only echoes it on the action event).
194
+ try std.testing.expect(harness.null_platform.context_menu_token != 0);
130
195
  try std.testing.expectEqualDeep(geometry.PointF.init(50, 20), harness.null_platform.context_menu_point);
131
196
  const recorded = harness.null_platform.contextMenuItems();
132
197
  try std.testing.expectEqual(@as(usize, 3), recorded.len);
@@ -142,15 +207,157 @@ test "right click over a widget with a declared menu presents it natively and di
142
207
  try std.testing.expectEqual(@as(u32, 1), app_state.raw_input_count);
143
208
 
144
209
  // Selecting "Delete" dispatches the typed context-menu event.
145
- try harness.runtime.dispatchPlatformEvent(app, menuAction(2, 3));
210
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 3));
146
211
  try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
147
212
  try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_menu_target);
148
213
  try std.testing.expectEqual(@as(usize, 2), app_state.last_menu_item_index);
149
214
 
150
215
  // A dismissal (item 0) resolves silently.
151
216
  try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 20));
152
- try harness.runtime.dispatchPlatformEvent(app, menuAction(2, 0));
217
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 0));
218
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
219
+ }
220
+
221
+ test "a superseding presentation names the dismissed menu's view and keeps the successor selectable" {
222
+ var app_state: MenuTestApp = .{};
223
+ const app = app_state.app();
224
+ const harness = try createMenuHarness(app);
225
+ defer harness.destroy(std.testing.allocator);
226
+
227
+ const first_items = [_]canvas.WidgetContextMenuItem{.{ .label = "Complete" }};
228
+ const second_items = [_]canvas.WidgetContextMenuItem{ .{ .label = "Open" }, .{ .label = "Delete" } };
229
+ const first_row = canvas.Widget{
230
+ .id = 2,
231
+ .kind = .list_item,
232
+ .frame = geometry.RectF.init(10, 10, 200, 40),
233
+ .text = "First",
234
+ .context_menu = &first_items,
235
+ };
236
+ const second_row = canvas.Widget{
237
+ .id = 3,
238
+ .kind = .list_item,
239
+ .frame = geometry.RectF.init(10, 60, 200, 40),
240
+ .text = "Second",
241
+ .context_menu = &second_items,
242
+ };
243
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
244
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{ first_row, second_row } }, geometry.RectF.init(0, 0, 320, 200), &nodes);
245
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
246
+
247
+ // The second presentation supersedes the first: the app hears one
248
+ // dismissed notice carrying the OLD token and the VIEW the dismissed
249
+ // menu was presented on (a raw app tracking per-canvas menu state
250
+ // needs the correlation — the notice must never publish an empty
251
+ // label).
252
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 20));
253
+ const first_token = harness.null_platform.context_menu_token;
254
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 70));
255
+ const second_token = harness.null_platform.context_menu_token;
256
+ try std.testing.expectEqual(@as(u32, 1), app_state.dismissed_count);
257
+ try std.testing.expectEqual(first_token, app_state.last_dismissed_token);
258
+ try std.testing.expectEqualStrings("canvas", app_state.last_dismissed_label[0..app_state.last_dismissed_label_len]);
259
+
260
+ // The replacement pending was committed BEFORE the (fallible)
261
+ // dismissed notice dispatched, so the successor menu on the glass
262
+ // resolves normally.
263
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(second_token, 2));
264
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
265
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.last_menu_target);
266
+ try std.testing.expectEqual(@as(usize, 1), app_state.last_menu_item_index);
267
+ }
268
+
269
+ test "a stale dismissal from a superseded menu never clears the successor's pending request" {
270
+ var app_state: MenuTestApp = .{};
271
+ const app = app_state.app();
272
+ const harness = try createMenuHarness(app);
273
+ defer harness.destroy(std.testing.allocator);
274
+
275
+ // Two rows, each with its own declared menu: the second right-click
276
+ // supersedes the first request while the first menu's deferred
277
+ // dismissal is still in flight (the GTK presenter tears the old menu
278
+ // down one loop turn later).
279
+ const first_items = [_]canvas.WidgetContextMenuItem{.{ .label = "Complete" }};
280
+ const second_items = [_]canvas.WidgetContextMenuItem{ .{ .label = "Open" }, .{ .label = "Delete" } };
281
+ const first_row = canvas.Widget{
282
+ .id = 2,
283
+ .kind = .list_item,
284
+ .frame = geometry.RectF.init(10, 10, 200, 40),
285
+ .text = "First",
286
+ .context_menu = &first_items,
287
+ };
288
+ const second_row = canvas.Widget{
289
+ .id = 3,
290
+ .kind = .list_item,
291
+ .frame = geometry.RectF.init(10, 60, 200, 40),
292
+ .text = "Second",
293
+ .context_menu = &second_items,
294
+ };
295
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
296
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{ first_row, second_row } }, geometry.RectF.init(0, 0, 320, 200), &nodes);
297
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
298
+
299
+ // First request presents, then the second arrives before the first's
300
+ // dismissal is delivered. Each request carries its own minted token.
301
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 20));
302
+ const first_token = harness.null_platform.context_menu_token;
303
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 70));
304
+ const second_token = harness.null_platform.context_menu_token;
305
+ try std.testing.expectEqual(@as(usize, 2), harness.null_platform.context_menu_request_count);
306
+ try std.testing.expect(first_token != second_token);
307
+
308
+ // The superseded request's dismissal arrives late, carrying the OLD
309
+ // token: it is swallowed, never resolving (or clearing) the pending
310
+ // successor.
311
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(first_token, 0));
312
+ try std.testing.expectEqual(@as(u32, 0), app_state.menu_count);
313
+
314
+ // The successor stays pending and resolvable: its own selection
315
+ // dispatches exactly one context-menu event for the second target.
316
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(second_token, 2));
317
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
318
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.last_menu_target);
319
+ try std.testing.expectEqual(@as(usize, 1), app_state.last_menu_item_index);
320
+
321
+ // One event per request: neither token can resolve again.
322
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(first_token, 1));
323
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(second_token, 2));
324
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
325
+ }
326
+
327
+ test "re-clicking the same widget mints a fresh token so the old menu's late dismissal is inert" {
328
+ var app_state: MenuTestApp = .{};
329
+ const app = app_state.app();
330
+ const harness = try createMenuHarness(app);
331
+ defer harness.destroy(std.testing.allocator);
332
+
333
+ // ONE widget: with a widget-id token the two requests would be
334
+ // indistinguishable and the superseded menu's deferred dismissal
335
+ // would resolve (clear) the replacement's pending request.
336
+ const items = [_]canvas.WidgetContextMenuItem{ .{ .label = "Complete" }, .{ .label = "Delete" } };
337
+ const row = canvas.Widget{
338
+ .id = 2,
339
+ .kind = .list_item,
340
+ .frame = geometry.RectF.init(10, 10, 200, 40),
341
+ .text = "Task",
342
+ .context_menu = &items,
343
+ };
344
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
345
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{row} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
346
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
347
+
348
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 20));
349
+ const first_token = harness.null_platform.context_menu_token;
350
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(60, 24));
351
+ const second_token = harness.null_platform.context_menu_token;
352
+ try std.testing.expect(first_token != second_token);
353
+
354
+ // The first menu's deferred dismissal lands after the re-click: its
355
+ // stale token is swallowed and the replacement stays live.
356
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(first_token, 0));
357
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(second_token, 2));
153
358
  try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
359
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_menu_target);
360
+ try std.testing.expectEqual(@as(usize, 1), app_state.last_menu_item_index);
154
361
  }
155
362
 
156
363
  test "right click on editable text presents the default edit menu wired to clipboard actions" {
@@ -198,7 +405,7 @@ test "right click on editable text presents the default edit menu wired to clipb
198
405
  try std.testing.expectEqualStrings("Select All", recorded[4].label);
199
406
 
200
407
  // Copy through the menu: clipboard captures the selection, text stays.
201
- try harness.runtime.dispatchPlatformEvent(app, menuAction(2, 2));
408
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 2));
202
409
  var clipboard_buffer: [256]u8 = undefined;
203
410
  try std.testing.expectEqualStrings("Query", try harness.runtime.readClipboard(&clipboard_buffer));
204
411
  var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
@@ -206,20 +413,20 @@ test "right click on editable text presents the default edit menu wired to clipb
206
413
 
207
414
  // Cut through the menu: clipboard keeps the selection, field empties.
208
415
  try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 30));
209
- try harness.runtime.dispatchPlatformEvent(app, menuAction(2, 1));
416
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 1));
210
417
  try std.testing.expectEqualStrings("Query", try harness.runtime.readClipboard(&clipboard_buffer));
211
418
  retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
212
419
  try std.testing.expectEqualStrings("", retained.nodes[1].widget.text);
213
420
 
214
421
  // Paste through the menu: the clipboard lands at the caret.
215
422
  try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 30));
216
- try harness.runtime.dispatchPlatformEvent(app, menuAction(2, 3));
423
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 3));
217
424
  retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
218
425
  try std.testing.expectEqualStrings("Query", retained.nodes[1].widget.text);
219
426
 
220
427
  // Select All through the menu re-selects everything.
221
428
  try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 30));
222
- try harness.runtime.dispatchPlatformEvent(app, menuAction(2, 4));
429
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 4));
223
430
  retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
224
431
  try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 0, .focus = 5 }, retained.nodes[1].widget.text_selection.?);
225
432
  }
@@ -253,7 +460,7 @@ test "a near-capacity multi-line context-menu paste sanitizes before it clamps"
253
460
  try harness.runtime.writeClipboard("a\nbc");
254
461
  try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 30));
255
462
  // Paste is the default edit menu's third item.
256
- try harness.runtime.dispatchPlatformEvent(app, menuAction(2, 3));
463
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 3));
257
464
 
258
465
  // Retained editor and the app's stamped edit hear the same whole
259
466
  // sanitized suffix, with no false truncation flag.
@@ -265,6 +472,122 @@ test "a near-capacity multi-line context-menu paste sanitizes before it clamps"
265
472
  try std.testing.expect(!app_state.saw_truncated);
266
473
  }
267
474
 
475
+ test "right click on a terminal presents Copy and Paste wired to selection and committed input" {
476
+ var app_state: MenuTestApp = .{};
477
+ const app = app_state.app();
478
+ const harness = try createMenuHarness(app);
479
+ defer harness.destroy(std.testing.allocator);
480
+
481
+ var grid = canvas.TerminalGrid{
482
+ .background = canvas.Color.rgba(0, 0, 0, 1),
483
+ .foreground = canvas.Color.rgba(1, 1, 1, 1),
484
+ .cursor_color = canvas.Color.rgba(1, 1, 1, 1),
485
+ .selection_color = canvas.Color.rgba(0, 0.5, 1, 1),
486
+ .screen_text = "alpha beta",
487
+ .selection_text = "beta",
488
+ .selection_active = true,
489
+ };
490
+ const terminal = canvas.Widget{
491
+ .id = 2,
492
+ .kind = .terminal,
493
+ .frame = geometry.RectF.init(12, 16, 280, 120),
494
+ .terminal = .{ .pty = 7, .grid = &grid },
495
+ };
496
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
497
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{terminal} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
498
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
499
+
500
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 40));
501
+
502
+ try std.testing.expectEqual(@as(usize, 1), harness.null_platform.context_menu_request_count);
503
+ const recorded = harness.null_platform.contextMenuItems();
504
+ try std.testing.expectEqual(@as(usize, 2), recorded.len);
505
+ try std.testing.expectEqual(@as(u32, 2), recorded[0].id);
506
+ try std.testing.expectEqualStrings("Copy", recorded[0].label);
507
+ try std.testing.expect(recorded[0].enabled);
508
+ try std.testing.expectEqual(@as(u32, 3), recorded[1].id);
509
+ try std.testing.expectEqualStrings("Paste", recorded[1].label);
510
+ try std.testing.expect(recorded[1].enabled);
511
+ // Secondary-click focus ensures the eventual paste addresses this
512
+ // terminal even when another editor previously owned focus.
513
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_focused_id);
514
+
515
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 2));
516
+ var clipboard_buffer: [64]u8 = undefined;
517
+ try std.testing.expectEqualStrings("beta", try harness.runtime.readClipboard(&clipboard_buffer));
518
+
519
+ const paste = "printf 'ready'\n";
520
+ try harness.runtime.writeClipboard(paste);
521
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 40));
522
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 3));
523
+ try std.testing.expectEqual(canvas.WidgetKeyboardPhase.text_input, app_state.last_keyboard_phase);
524
+ try std.testing.expectEqual(@as(?canvas.ObjectId, 2), app_state.last_keyboard_focused_id);
525
+ try std.testing.expect(app_state.last_keyboard_terminal_paste);
526
+ try std.testing.expect(app_state.last_keyboard_standalone);
527
+ try std.testing.expectEqualStrings(paste, app_state.last_edit_insert[0..app_state.last_edit_insert_len]);
528
+
529
+ // The keyboard shortcut takes the same provenance-sensitive path,
530
+ // but remains part of the gpu-input cycle rather than a standalone
531
+ // native-menu dispatch.
532
+ const shortcut_paste = "echo shortcut\n";
533
+ try harness.runtime.writeClipboard(shortcut_paste);
534
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
535
+ .window_id = 1,
536
+ .label = "canvas",
537
+ .kind = .key_down,
538
+ .key = "v",
539
+ .modifiers = .{ .primary = true, .command = true },
540
+ } });
541
+ try std.testing.expectEqual(canvas.WidgetKeyboardPhase.text_input, app_state.last_keyboard_phase);
542
+ try std.testing.expectEqual(@as(?canvas.ObjectId, 2), app_state.last_keyboard_focused_id);
543
+ try std.testing.expect(app_state.last_keyboard_terminal_paste);
544
+ try std.testing.expect(!app_state.last_keyboard_standalone);
545
+ try std.testing.expectEqualStrings(shortcut_paste, app_state.last_edit_insert[0..app_state.last_edit_insert_len]);
546
+ }
547
+
548
+ test "terminal Paste disables after exit and a pending live menu revalidates before dispatch" {
549
+ var app_state: MenuTestApp = .{};
550
+ const app = app_state.app();
551
+ const harness = try createMenuHarness(app);
552
+ defer harness.destroy(std.testing.allocator);
553
+
554
+ var grid = canvas.TerminalGrid{
555
+ .background = canvas.Color.rgba(0, 0, 0, 1),
556
+ .foreground = canvas.Color.rgba(1, 1, 1, 1),
557
+ .cursor_color = canvas.Color.rgba(1, 1, 1, 1),
558
+ .selection_color = canvas.Color.rgba(0, 0.5, 1, 1),
559
+ };
560
+ const terminal = canvas.Widget{
561
+ .id = 2,
562
+ .kind = .terminal,
563
+ .frame = geometry.RectF.init(12, 16, 280, 120),
564
+ .terminal = .{ .pty = 7, .grid = &grid },
565
+ };
566
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
567
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{terminal} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
568
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
569
+
570
+ try harness.runtime.writeClipboard("must-not-escape");
571
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 40));
572
+ const live_token = harness.null_platform.context_menu_token;
573
+ try std.testing.expect(harness.null_platform.contextMenuItems()[1].enabled);
574
+
575
+ // GTK resolves menus asynchronously: the pty can exit while its
576
+ // live menu remains open. The action-time guard must swallow that
577
+ // stale Paste instead of emitting terminal input that could fall
578
+ // through to an app-level text handler.
579
+ grid.running = false;
580
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(live_token, 3));
581
+ try std.testing.expectEqual(@as(u32, 0), app_state.keyboard_count);
582
+
583
+ // A new menu on the ended terminal describes the same truth up
584
+ // front: Copy remains selection-dependent, Paste is disabled.
585
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 40));
586
+ const ended_items = harness.null_platform.contextMenuItems();
587
+ try std.testing.expectEqual(@as(usize, 2), ended_items.len);
588
+ try std.testing.expect(!ended_items[1].enabled);
589
+ }
590
+
268
591
  test "right click on selected static text presents a copy-only menu" {
269
592
  var app_state: MenuTestApp = .{};
270
593
  const app = app_state.app();
@@ -291,7 +614,7 @@ test "right click on selected static text presents a copy-only menu" {
291
614
  try std.testing.expectEqual(@as(usize, 1), recorded.len);
292
615
  try std.testing.expectEqualStrings("Copy", recorded[0].label);
293
616
 
294
- try harness.runtime.dispatchPlatformEvent(app, menuAction(2, 2));
617
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 2));
295
618
  var clipboard_buffer: [64]u8 = undefined;
296
619
  try std.testing.expectEqualStrings("Release", try harness.runtime.readClipboard(&clipboard_buffer));
297
620
  }
@@ -326,8 +649,9 @@ test "a declared menu on a presenter-less host becomes a fallback request, not a
326
649
  const app = app_state.app();
327
650
  const harness = try TestHarness().create(std.testing.allocator, .{});
328
651
  harness.null_platform.gpu_surfaces = true;
329
- // Model a host without a native menu presenter (Linux GTK, Windows
330
- // Win32 today): the service is null and the feature reports false.
652
+ // Model a host without a native menu presenter (the mobile toolkit
653
+ // hosts and embed hosts today): the service is null and the feature
654
+ // reports false.
331
655
  // Service POINTERS are captured at init, so re-capture after the
332
656
  // flip (feature FLAGS like gpu_surfaces read live through context).
333
657
  harness.null_platform.context_menus = false;
@@ -362,6 +686,9 @@ test "a declared menu on a presenter-less host becomes a fallback request, not a
362
686
  try std.testing.expectEqual(@as(usize, 0), harness.null_platform.context_menu_request_count);
363
687
  try std.testing.expectEqual(@as(u32, 1), app_state.request_count);
364
688
  try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_request_target);
689
+ // The click point rides the request: the fallback surface anchors at
690
+ // the pointer, not a target-widget edge.
691
+ try std.testing.expectEqualDeep(geometry.PointF.init(50, 20), app_state.last_request_point);
365
692
  // Never the hold alternative: a declared menu consumed the press.
366
693
  try std.testing.expectEqual(@as(u32, 0), app_state.pointer_count);
367
694
 
@@ -421,6 +748,305 @@ test "the widget-context-menu verb dispatches selections through context_menu_ac
421
748
  try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
422
749
  }
423
750
 
751
+ test "the widget-context-menu verb's synthetic selection survives an erroring dismissal handler" {
752
+ var app_state: MenuTestApp = .{};
753
+ const app = app_state.app();
754
+ const harness = try createMenuHarness(app);
755
+ defer harness.destroy(std.testing.allocator);
756
+
757
+ const first_items = [_]canvas.WidgetContextMenuItem{.{ .label = "Complete" }};
758
+ const second_items = [_]canvas.WidgetContextMenuItem{ .{ .label = "Open" }, .{ .label = "Delete" } };
759
+ const first_row = canvas.Widget{
760
+ .id = 2,
761
+ .kind = .list_item,
762
+ .frame = geometry.RectF.init(10, 10, 200, 40),
763
+ .text = "First",
764
+ .context_menu = &first_items,
765
+ };
766
+ const second_row = canvas.Widget{
767
+ .id = 3,
768
+ .kind = .list_item,
769
+ .frame = geometry.RectF.init(10, 60, 200, 40),
770
+ .text = "Second",
771
+ .context_menu = &second_items,
772
+ };
773
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
774
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{ first_row, second_row } }, geometry.RectF.init(0, 0, 320, 200), &nodes);
775
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
776
+
777
+ // A real menu is on the glass for the first row when the verb
778
+ // supersedes it — and the app's dismissed handler errors under the
779
+ // harness's `.propagate` policy.
780
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 20));
781
+ const first_token = harness.null_platform.context_menu_token;
782
+ app_state.dismissal_error = error.DismissalHandlerBlewUp;
783
+
784
+ // The verb's synthetic selection is its request's ONLY outcome (no
785
+ // native menu presents), so the erroring notice must not skip it:
786
+ // the selection dispatches, the notice error still propagates, and
787
+ // no error path leaves a pending token with no presented menu and
788
+ // no delivered outcome.
789
+ try std.testing.expectError(error.DismissalHandlerBlewUp, harness.runtime.dispatchAutomationCommand(app, "widget-context-menu canvas 3 0"));
790
+ try std.testing.expectEqual(@as(u32, 1), app_state.dismissed_count);
791
+ try std.testing.expectEqual(first_token, app_state.last_dismissed_token);
792
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
793
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.last_menu_target);
794
+ try std.testing.expectEqual(@as(usize, 0), app_state.last_menu_item_index);
795
+ try std.testing.expect(harness.runtime.canvas_widget_context_menu_pending == null);
796
+ }
797
+
798
+ /// Three same-window views with a declared-menu widget on the first
799
+ /// two: alpha holds the superseded menu, beta the superseding action,
800
+ /// and gamma is the view a stale index would land on after alpha's
801
+ /// removal shifts the array down.
802
+ fn createCompactionHarness(app: App) !*TestHarness() {
803
+ const harness = try TestHarness().create(std.testing.allocator, .{});
804
+ harness.null_platform.gpu_surfaces = true;
805
+ try harness.start(app);
806
+ for ([_][]const u8{ "alpha", "beta", "gamma" }) |label| {
807
+ _ = try harness.runtime.createView(.{
808
+ .window_id = 1,
809
+ .label = label,
810
+ .kind = .gpu_surface,
811
+ .frame = geometry.RectF.init(0, 0, 320, 200),
812
+ });
813
+ }
814
+ const alpha_items = [_]canvas.WidgetContextMenuItem{.{ .label = "Complete" }};
815
+ const beta_items = [_]canvas.WidgetContextMenuItem{ .{ .label = "Open" }, .{ .label = "Delete" } };
816
+ const alpha_row = canvas.Widget{
817
+ .id = 2,
818
+ .kind = .list_item,
819
+ .frame = geometry.RectF.init(10, 10, 200, 40),
820
+ .text = "First",
821
+ .context_menu = &alpha_items,
822
+ };
823
+ const beta_row = canvas.Widget{
824
+ .id = 3,
825
+ .kind = .list_item,
826
+ .frame = geometry.RectF.init(10, 10, 200, 40),
827
+ .text = "Second",
828
+ .context_menu = &beta_items,
829
+ };
830
+ var alpha_nodes: [2]canvas.WidgetLayoutNode = undefined;
831
+ const alpha_layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{alpha_row} }, geometry.RectF.init(0, 0, 320, 200), &alpha_nodes);
832
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "alpha", alpha_layout);
833
+ var beta_nodes: [2]canvas.WidgetLayoutNode = undefined;
834
+ const beta_layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{beta_row} }, geometry.RectF.init(0, 0, 320, 200), &beta_nodes);
835
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "beta", beta_layout);
836
+ return harness;
837
+ }
838
+
839
+ test "the automation menu verb names its view from the request after the dismissal handler closes another view" {
840
+ var app_state: MenuTestApp = .{};
841
+ const app = app_state.app();
842
+ const harness = try createCompactionHarness(app);
843
+ defer harness.destroy(std.testing.allocator);
844
+
845
+ // Alpha's menu is pending when the verb supersedes it from beta —
846
+ // and the app's dismissal handler closes alpha, shifting beta and
847
+ // gamma down one index mid-dispatch.
848
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("alpha", 50, 20));
849
+ const first_token = harness.null_platform.context_menu_token;
850
+ app_state.close_view_on_dismissal = "alpha";
851
+
852
+ try harness.runtime.dispatchAutomationCommand(app, "widget-context-menu beta 3 0");
853
+
854
+ // The notice named the superseded menu and ran the closure...
855
+ try std.testing.expectEqual(@as(u32, 1), app_state.dismissed_count);
856
+ try std.testing.expectEqual(first_token, app_state.last_dismissed_token);
857
+ try std.testing.expectEqual(@as(usize, 2), harness.runtime.view_count);
858
+ // ...and the synthetic selection still dispatched against BETA:
859
+ // the event names its view from the request's own copy, never a
860
+ // compacted index (which now points at gamma). No pending request
861
+ // survives.
862
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
863
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.last_menu_target);
864
+ try std.testing.expectEqual(@as(usize, 0), app_state.last_menu_item_index);
865
+ try std.testing.expectEqualStrings("beta", app_state.last_menu_label[0..app_state.last_menu_label_len]);
866
+ try std.testing.expect(harness.runtime.canvas_widget_context_menu_pending == null);
867
+ }
868
+
869
+ test "the automation menu verb refuses by name when the dismissal handler presents a superseding menu" {
870
+ var app_state: MenuTestApp = .{};
871
+ const app = app_state.app();
872
+ const harness = try createCompactionHarness(app);
873
+ defer harness.destroy(std.testing.allocator);
874
+
875
+ // Alpha's menu is pending; the verb targets alpha's own item, but
876
+ // the dismissal notice for the superseded presentation right-clicks
877
+ // beta — app code that synchronously presents a SUPERSEDING menu,
878
+ // replacing the verb's freshly armed request before its synthetic
879
+ // action dispatches.
880
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("alpha", 50, 20));
881
+ app_state.present_menu_on_dismissal = "beta";
882
+ try std.testing.expectError(
883
+ error.ContextMenuSuperseded,
884
+ harness.runtime.dispatchAutomationCommand(app, "widget-context-menu alpha 2 0"),
885
+ );
886
+
887
+ // Never a silent success: the requested item did not dispatch, and
888
+ // the driver heard why. Both supersessions were announced (the
889
+ // right-click's menu, then the verb's own).
890
+ try std.testing.expectEqual(@as(u32, 0), app_state.menu_count);
891
+ try std.testing.expectEqual(@as(u32, 2), app_state.dismissed_count);
892
+
893
+ // The handler's successor menu is on the glass with its own pending
894
+ // request — nothing orphaned, still resolvable.
895
+ try std.testing.expectEqual(@as(usize, 2), harness.null_platform.context_menu_request_count);
896
+ const successor_token = harness.null_platform.context_menu_token;
897
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
898
+ .window_id = 1,
899
+ .view_label = "beta",
900
+ .token = successor_token,
901
+ .item_id = 2,
902
+ } });
903
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
904
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.last_menu_target);
905
+ try std.testing.expectEqual(@as(usize, 1), app_state.last_menu_item_index);
906
+ }
907
+
908
+ test "the automation menu verb refuses by name when the dismissal handler closes its target view" {
909
+ var app_state: MenuTestApp = .{};
910
+ const app = app_state.app();
911
+ const harness = try createCompactionHarness(app);
912
+ defer harness.destroy(std.testing.allocator);
913
+
914
+ // Alpha's menu is pending; the verb targets beta, but the dismissal
915
+ // notice for the superseded presentation closes BETA — the verb's
916
+ // own target view. Its armed request can never resolve, so the verb
917
+ // disarms it and refuses by name instead of reporting success while
918
+ // the action dispatch silently drops the selection.
919
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("alpha", 50, 20));
920
+ app_state.close_view_on_dismissal = "beta";
921
+ try std.testing.expectError(
922
+ error.ContextMenuViewClosed,
923
+ harness.runtime.dispatchAutomationCommand(app, "widget-context-menu beta 3 0"),
924
+ );
925
+
926
+ try std.testing.expectEqual(@as(u32, 1), app_state.dismissed_count);
927
+ try std.testing.expectEqual(@as(u32, 0), app_state.menu_count);
928
+ try std.testing.expectEqual(@as(usize, 2), harness.runtime.view_count);
929
+ // No orphan: the unresolvable request is disarmed with the refusal.
930
+ try std.testing.expect(harness.runtime.canvas_widget_context_menu_pending == null);
931
+ }
932
+
933
+ test "a presentation whose view the dismissal handler closes is disarmed and never announced" {
934
+ var app_state: MenuTestApp = .{};
935
+ const app = app_state.app();
936
+ const harness = try createCompactionHarness(app);
937
+ defer harness.destroy(std.testing.allocator);
938
+
939
+ // Menu A is pending on alpha; right-clicking beta presents menu B,
940
+ // and A's dismissal notice closes BETA — the view B presented on.
941
+ // B's request can never resolve, so it disarms without announcing:
942
+ // a shown event would promise a snapshot for a menu whose action
943
+ // can never be delivered.
944
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("alpha", 50, 20));
945
+ try std.testing.expectEqual(@as(u32, 1), app_state.shown_count);
946
+ app_state.close_view_on_dismissal = "beta";
947
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("beta", 50, 20));
948
+ const dead_token = harness.null_platform.context_menu_token;
949
+
950
+ try std.testing.expectEqual(@as(usize, 2), harness.null_platform.context_menu_request_count);
951
+ try std.testing.expectEqual(@as(u32, 1), app_state.dismissed_count);
952
+ try std.testing.expectEqual(@as(u32, 1), app_state.shown_count);
953
+ try std.testing.expectEqual(@as(usize, 2), harness.runtime.view_count);
954
+ try std.testing.expect(harness.runtime.canvas_widget_context_menu_pending == null);
955
+
956
+ // A late action carrying the dead request's token is inert...
957
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(dead_token, 1));
958
+ try std.testing.expectEqual(@as(u32, 0), app_state.menu_count);
959
+
960
+ // ...and a fresh presentation arms cleanly.
961
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("alpha", 50, 20));
962
+ try std.testing.expectEqual(@as(u32, 2), app_state.shown_count);
963
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
964
+ .window_id = 1,
965
+ .view_label = "alpha",
966
+ .token = harness.null_platform.context_menu_token,
967
+ .item_id = 1,
968
+ } });
969
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
970
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_menu_target);
971
+ }
972
+
973
+ test "a presentation superseded mid-notice by the dismissal handler's own menu announces nothing" {
974
+ var app_state: MenuTestApp = .{};
975
+ const app = app_state.app();
976
+ const harness = try createCompactionHarness(app);
977
+ defer harness.destroy(std.testing.allocator);
978
+
979
+ // Menu A is pending on alpha. Right-clicking beta presents menu B,
980
+ // whose supersession notice for A synchronously re-presents alpha's
981
+ // menu (C) — replacing B's freshly committed request before B's
982
+ // shown event dispatches.
983
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("alpha", 50, 20));
984
+ try std.testing.expectEqual(@as(u32, 1), app_state.shown_count);
985
+ app_state.present_menu_on_dismissal = "alpha";
986
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("beta", 50, 20));
987
+
988
+ // Three presentations reached the platform (A, B, C), but only TWO
989
+ // announced themselves: B was superseded mid-notice, so its late
990
+ // shown event must not overwrite the successor's snapshot — the
991
+ // last announcement is C's, carrying C's token on alpha's canvas.
992
+ try std.testing.expectEqual(@as(usize, 3), harness.null_platform.context_menu_request_count);
993
+ try std.testing.expectEqual(@as(u32, 2), app_state.shown_count);
994
+ const successor_token = harness.null_platform.context_menu_token;
995
+ try std.testing.expectEqual(successor_token, app_state.last_shown_token);
996
+ try std.testing.expectEqualStrings("alpha", app_state.last_shown_label[0..app_state.last_shown_label_len]);
997
+ // Both supersessions were announced: A's (by B), then B's (by C).
998
+ try std.testing.expectEqual(@as(u32, 2), app_state.dismissed_count);
999
+
1000
+ // The successor resolves normally against its own token.
1001
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
1002
+ .window_id = 1,
1003
+ .view_label = "alpha",
1004
+ .token = successor_token,
1005
+ .item_id = 1,
1006
+ } });
1007
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
1008
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_menu_target);
1009
+ try std.testing.expectEqual(@as(usize, 0), app_state.last_menu_item_index);
1010
+ try std.testing.expect(harness.runtime.canvas_widget_context_menu_pending == null);
1011
+ }
1012
+
1013
+ test "a superseding presentation's shown event names the presenting view after the dismissal handler closes another" {
1014
+ var app_state: MenuTestApp = .{};
1015
+ const app = app_state.app();
1016
+ const harness = try createCompactionHarness(app);
1017
+ defer harness.destroy(std.testing.allocator);
1018
+
1019
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("alpha", 50, 20));
1020
+ try std.testing.expectEqual(@as(u32, 1), app_state.shown_count);
1021
+ try std.testing.expectEqualStrings("alpha", app_state.last_shown_label[0..app_state.last_shown_label_len]);
1022
+
1023
+ // Beta's presentation supersedes alpha's; the dismissal handler
1024
+ // closes alpha, compacting the view array while the shown event is
1025
+ // still to be dispatched.
1026
+ app_state.close_view_on_dismissal = "alpha";
1027
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("beta", 50, 20));
1028
+ const second_token = harness.null_platform.context_menu_token;
1029
+ try std.testing.expectEqual(@as(u32, 1), app_state.dismissed_count);
1030
+ // The shown event describes the menu on the glass from the request
1031
+ // itself: beta's canvas, beta's token — never the view a stale
1032
+ // index lands on after the compaction.
1033
+ try std.testing.expectEqual(@as(u32, 2), app_state.shown_count);
1034
+ try std.testing.expectEqual(second_token, app_state.last_shown_token);
1035
+ try std.testing.expectEqualStrings("beta", app_state.last_shown_label[0..app_state.last_shown_label_len]);
1036
+
1037
+ // The successor resolves normally on its own view.
1038
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
1039
+ .window_id = 1,
1040
+ .view_label = "beta",
1041
+ .token = second_token,
1042
+ .item_id = 2,
1043
+ } });
1044
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
1045
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.last_menu_target);
1046
+ try std.testing.expectEqual(@as(usize, 1), app_state.last_menu_item_index);
1047
+ try std.testing.expectEqualStrings("beta", app_state.last_menu_label[0..app_state.last_menu_label_len]);
1048
+ }
1049
+
424
1050
  test "automation snapshots list each widget's declared context-menu items in invocable order" {
425
1051
  var app_state: MenuTestApp = .{};
426
1052
  const app = app_state.app();