@native-sdk/cli 0.5.1 → 0.5.3

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 (98) hide show
  1. package/build.zig +41 -0
  2. package/package.json +9 -9
  3. package/packages/core/package-lock.json +2 -2
  4. package/packages/core/package.json +1 -1
  5. package/packages/core/sdk/events.ts +39 -2
  6. package/packages/core/src/checker.ts +1 -1
  7. package/packages/core/src/emitter.ts +39 -3
  8. package/packages/core/src/infer.ts +53 -7
  9. package/skill-data/automation/SKILL.md +7 -4
  10. package/skill-data/core/references/app-model-runtime.md +3 -0
  11. package/skill-data/native-ui/SKILL.md +3 -2
  12. package/src/app_runner/root.zig +25 -0
  13. package/src/automation/protocol.zig +20 -1
  14. package/src/embed/host.zig +27 -0
  15. package/src/embed/tests.zig +69 -0
  16. package/src/embed/ui_host.zig +4 -0
  17. package/src/platform/macos/appkit_host.h +11 -0
  18. package/src/platform/macos/appkit_host.m +130 -0
  19. package/src/platform/macos/root.zig +34 -0
  20. package/src/platform/null_platform_tests.zig +39 -0
  21. package/src/platform/root.zig +3 -0
  22. package/src/platform/types.zig +118 -4
  23. package/src/primitives/canvas/chart.zig +7 -7
  24. package/src/primitives/canvas/chart_tests.zig +66 -0
  25. package/src/primitives/canvas/commands.zig +48 -0
  26. package/src/primitives/canvas/events.zig +161 -0
  27. package/src/primitives/canvas/font_ttf.zig +155 -8
  28. package/src/primitives/canvas/font_ttf_tests.zig +686 -0
  29. package/src/primitives/canvas/reference.zig +67 -8
  30. package/src/primitives/canvas/render_fingerprints.zig +8 -0
  31. package/src/primitives/canvas/root.zig +26 -0
  32. package/src/primitives/canvas/tokens.zig +170 -7
  33. package/src/primitives/canvas/ui.zig +62 -7
  34. package/src/primitives/canvas/ui_markup.zig +51 -4
  35. package/src/primitives/canvas/ui_markup_binary_tests.zig +25 -0
  36. package/src/primitives/canvas/ui_markup_compiled.zig +49 -4
  37. package/src/primitives/canvas/ui_markup_compiled_tests.zig +160 -0
  38. package/src/primitives/canvas/ui_markup_contract.zig +9 -0
  39. package/src/primitives/canvas/ui_markup_contract_tests.zig +24 -0
  40. package/src/primitives/canvas/ui_markup_tests.zig +73 -0
  41. package/src/primitives/canvas/ui_markup_view.zig +63 -5
  42. package/src/primitives/canvas/ui_markup_view_tests.zig +151 -2
  43. package/src/primitives/canvas/ui_schema.zig +31 -1
  44. package/src/primitives/canvas/ui_schema_tests.zig +11 -11
  45. package/src/primitives/canvas/ui_tests.zig +31 -0
  46. package/src/primitives/canvas/vector.zig +258 -141
  47. package/src/primitives/canvas/widget_access.zig +1 -1
  48. package/src/primitives/canvas/widget_builtin_tests.zig +259 -0
  49. package/src/primitives/canvas/widget_layout.zig +13 -5
  50. package/src/primitives/canvas/widget_layout_tests.zig +82 -0
  51. package/src/primitives/canvas/widget_render.zig +78 -40
  52. package/src/primitives/canvas/widget_render_controls.zig +15 -4
  53. package/src/primitives/canvas/widget_semantics.zig +6 -1
  54. package/src/primitives/canvas/widget_semantics_tests.zig +168 -0
  55. package/src/primitives/canvas/widget_text_input.zig +118 -10
  56. package/src/primitives/canvas/widget_tree.zig +14 -2
  57. package/src/primitives/canvas/widgets.zig +28 -0
  58. package/src/root.zig +5 -0
  59. package/src/runtime/api.zig +13 -0
  60. package/src/runtime/automation_commands.zig +33 -0
  61. package/src/runtime/automation_widget_dispatch.zig +159 -28
  62. package/src/runtime/canvas_font_tests.zig +272 -3
  63. package/src/runtime/canvas_fonts.zig +60 -20
  64. package/src/runtime/canvas_frame.zig +15 -0
  65. package/src/runtime/canvas_frame_helpers.zig +15 -0
  66. package/src/runtime/canvas_image_tests.zig +26 -0
  67. package/src/runtime/canvas_images.zig +33 -7
  68. package/src/runtime/canvas_limits.zig +44 -8
  69. package/src/runtime/canvas_widget_anchored_tests.zig +253 -65
  70. package/src/runtime/canvas_widget_clipboard_tests.zig +123 -0
  71. package/src/runtime/canvas_widget_context_menu.zig +3 -2
  72. package/src/runtime/canvas_widget_context_menu_tests.zig +55 -0
  73. package/src/runtime/canvas_widget_events.zig +1295 -7
  74. package/src/runtime/canvas_widget_floating_tests.zig +2801 -0
  75. package/src/runtime/canvas_widget_runtime.zig +24 -7
  76. package/src/runtime/canvas_widget_scroll_drivers.zig +1 -1
  77. package/src/runtime/canvas_widget_state.zig +88 -6
  78. package/src/runtime/canvas_widget_text_tests.zig +277 -0
  79. package/src/runtime/core.zig +112 -6
  80. package/src/runtime/flow.zig +51 -1
  81. package/src/runtime/gpu_surface_events.zig +51 -5
  82. package/src/runtime/media_surface.zig +726 -0
  83. package/src/runtime/media_surface_tests.zig +1476 -0
  84. package/src/runtime/root.zig +9 -0
  85. package/src/runtime/session_journal.zig +52 -13
  86. package/src/runtime/session_record.zig +166 -0
  87. package/src/runtime/session_tests.zig +575 -0
  88. package/src/runtime/tests.zig +1 -0
  89. package/src/runtime/ts_ui_app.zig +56 -0
  90. package/src/runtime/ui_app.zig +64 -1
  91. package/src/runtime/ui_app_tests.zig +794 -0
  92. package/src/runtime/view.zig +86 -0
  93. package/src/runtime/view_widget_text.zig +18 -1
  94. package/src/runtime/view_widget_tree.zig +339 -20
  95. package/src/runtime/widget_bridge.zig +26 -0
  96. package/src/runtime/window_storage.zig +61 -15
  97. package/src/runtime/window_view_runtime.zig +13 -0
  98. package/src/runtime/window_views.zig +6 -0
@@ -7,6 +7,7 @@
7
7
 
8
8
  const std = @import("std");
9
9
  const geometry = @import("geometry");
10
+ const canvas = @import("canvas");
10
11
  const app_manifest = @import("app_manifest");
11
12
  const core = @import("core.zig");
12
13
  const ui_app_mod = @import("ui_app.zig");
@@ -32,10 +33,43 @@ const SessionModel = struct {
32
33
  /// pin, without 32 array fields in the equality check.
33
34
  spectrum_count: u32 = 0,
34
35
  band_checksum: u64 = 0,
36
+ /// The search query's `on_input` mirror, zero-padded so the deep
37
+ /// model equality below stays deterministic. The reference session
38
+ /// types into the field and Escape-clears it: the journal records
39
+ /// only the RAW click/type/Escape platform events, so replay must
40
+ /// re-derive the same clear edit the recording's editor applied.
41
+ query: [32]u8 = [_]u8{0} ** 32,
42
+ query_len: usize = 0,
43
+ query_anchor: usize = 0,
44
+ query_focus: usize = 0,
45
+ query_edits: u32 = 0,
46
+ /// A SECOND editable field's mirror: the direct-verb replay tests
47
+ /// need two editors so a composition targeting one can prove it
48
+ /// does not land on whichever field the session left focused.
49
+ name: [32]u8 = [_]u8{0} ** 32,
50
+ name_len: usize = 0,
51
+ name_anchor: usize = 0,
52
+ name_focus: usize = 0,
53
+ name_edits: u32 = 0,
54
+ /// The pinch channel's cumulative zoom (the product of 1 + delta
55
+ /// across change events) plus phase counters: the reference session
56
+ /// pinches once raw and once through the automation verb, so replay
57
+ /// re-deriving the identical product pins the journaled scale field.
58
+ zoom: f32 = 1,
59
+ pinch_begins: u32 = 0,
60
+ pinch_ends: u32 = 0,
35
61
 
36
62
  fn bodyText(self: *const SessionModel) []const u8 {
37
63
  return self.body[0..self.body_len];
38
64
  }
65
+
66
+ fn queryText(self: *const SessionModel) []const u8 {
67
+ return self.query[0..self.query_len];
68
+ }
69
+
70
+ fn nameText(self: *const SessionModel) []const u8 {
71
+ return self.name[0..self.name_len];
72
+ }
39
73
  };
40
74
 
41
75
  const SessionMsg = union(enum) {
@@ -44,6 +78,9 @@ const SessionMsg = union(enum) {
44
78
  start_fetch,
45
79
  start_spawn,
46
80
  start_audio,
81
+ query_edit: canvas.TextInputEvent,
82
+ name_edit: canvas.TextInputEvent,
83
+ pinch: platform.PinchEvent,
47
84
  fetched: effects_mod.EffectResponse,
48
85
  line: effects_mod.EffectLine,
49
86
  exited: effects_mod.EffectExit,
@@ -92,17 +129,59 @@ fn sessionUpdate(model: *SessionModel, msg: SessionMsg, fx: *SessionApp.Effects)
92
129
  for (event.bands) |band| checksum = checksum *% 31 +% band;
93
130
  model.band_checksum = checksum;
94
131
  },
132
+ .query_edit => |edit| applyMirrorEdit(&model.query, &model.query_len, &model.query_anchor, &model.query_focus, &model.query_edits, edit),
133
+ .name_edit => |edit| applyMirrorEdit(&model.name, &model.name_len, &model.name_anchor, &model.name_focus, &model.name_edits, edit),
134
+ .pinch => |pinch| switch (pinch.phase) {
135
+ .begin => model.pinch_begins += 1,
136
+ .change => model.zoom *= (1 + pinch.scale),
137
+ .end => model.pinch_ends += 1,
138
+ },
95
139
  .line => model.line_count += 1,
96
140
  .exited => |exit| model.exit_code = exit.code,
97
141
  .tick => |timer| model.tick_timestamp_ns = timer.timestamp_ns,
98
142
  }
99
143
  }
100
144
 
145
+ /// One editable-field mirror step: apply `edit` to the zero-padded
146
+ /// text + selection mirror the deep model equality checks compare.
147
+ fn applyMirrorEdit(store: *[32]u8, len: *usize, anchor: *usize, focus: *usize, edits: *u32, edit: canvas.TextInputEvent) void {
148
+ var scratch: [32]u8 = undefined;
149
+ const next = (canvas.TextEditState{
150
+ .text = store[0..len.*],
151
+ .selection = .{ .anchor = anchor.*, .focus = focus.* },
152
+ }).apply(edit, &scratch) catch return;
153
+ var out = [_]u8{0} ** 32;
154
+ const out_len = @min(next.text.len, out.len);
155
+ std.mem.copyForwards(u8, out[0..out_len], next.text[0..out_len]);
156
+ store.* = out;
157
+ len.* = out_len;
158
+ anchor.* = next.selection.anchor;
159
+ focus.* = next.selection.focus;
160
+ edits.* += 1;
161
+ }
162
+
101
163
  fn sessionView(ui: *SessionApp.Ui, model: *const SessionModel) SessionApp.Ui.Node {
102
164
  return ui.column(.{ .gap = 8, .padding = 12 }, .{
103
165
  ui.text(.{}, ui.fmt("Count {d}", .{model.count})),
104
166
  ui.text(.{}, ui.fmt("Body {s} ({d})", .{ model.bodyText(), model.fetch_status })),
105
167
  ui.text(.{}, ui.fmt("Lines {d} Exit {d} Tick {d} Stamp {d}", .{ model.line_count, model.exit_code, model.tick_timestamp_ns, model.stamp_ms })),
168
+ ui.el(.search_field, .{
169
+ .text = model.queryText(),
170
+ .placeholder = "Search",
171
+ .on_input = SessionApp.Ui.inputMsg(.query_edit),
172
+ }, .{}),
173
+ ui.el(.text_field, .{
174
+ .text = model.nameText(),
175
+ .placeholder = "Name",
176
+ .on_input = SessionApp.Ui.inputMsg(.name_edit),
177
+ }, .{}),
178
+ // A static multi-line source the paste replay pin copies from:
179
+ // the journal records only the raw select-all/copy/paste
180
+ // platform events, so the multi-line clipboard bytes and the
181
+ // sanitized single-line insert both re-derive on replay.
182
+ ui.el(.textarea, .{ .text = "line one\nline two" }, .{}),
183
+ ui.text(.{}, ui.fmt("Query {s} ({d}) Name {s} ({d})", .{ model.queryText(), model.query_edits, model.nameText(), model.name_edits })),
184
+ ui.text(.{}, ui.fmt("Zoom {d:.4} ({d}/{d})", .{ model.zoom, model.pinch_begins, model.pinch_ends })),
106
185
  ui.button(.{ .on_press = .increment }, "Increment"),
107
186
  });
108
187
  }
@@ -128,6 +207,10 @@ const session_windows = [_]app_manifest.ShellWindow{.{
128
207
  }};
129
208
  const session_scene: app_manifest.ShellConfig = .{ .windows = &session_windows };
130
209
 
210
+ fn sessionPinch(pinch: platform.PinchEvent) ?SessionMsg {
211
+ return .{ .pinch = pinch };
212
+ }
213
+
131
214
  fn sessionOptions() SessionApp.Options {
132
215
  return .{
133
216
  .name = "session-demo",
@@ -136,6 +219,7 @@ fn sessionOptions() SessionApp.Options {
136
219
  .update_fx = sessionUpdate,
137
220
  .view = sessionView,
138
221
  .on_command = sessionCommand,
222
+ .on_pinch = sessionPinch,
139
223
  };
140
224
  }
141
225
 
@@ -242,6 +326,133 @@ fn recordReferenceSession(gpa: std.mem.Allocator, buffer: *JournalBuffer, web_la
242
326
  try harness.runtime.dispatchPlatformEvent(app, .wake);
243
327
  try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
244
328
 
329
+ // An Escape-clear in the search field is DERIVED input state: the
330
+ // journal records only the raw click/type/Escape platform events,
331
+ // and replay must re-derive the same clear edit the recording's
332
+ // editor applied — model mirror, retained editor, and fingerprint
333
+ // all landing identically.
334
+ const layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
335
+ var search_frame: ?geometry.RectF = null;
336
+ for (layout.nodes) |node| {
337
+ if (node.widget.kind == .search_field) search_frame = node.frame;
338
+ }
339
+ const field_frame = search_frame.?;
340
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
341
+ .window_id = 1,
342
+ .label = canvas_label,
343
+ .kind = .pointer_down,
344
+ .x = field_frame.x + field_frame.width * 0.5,
345
+ .y = field_frame.y + field_frame.height * 0.5,
346
+ } });
347
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
348
+ .window_id = 1,
349
+ .label = canvas_label,
350
+ .kind = .text_input,
351
+ .text = "glass",
352
+ } });
353
+ try std.testing.expectEqualStrings("glass", app_state.model.queryText());
354
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
355
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
356
+ .window_id = 1,
357
+ .label = canvas_label,
358
+ .kind = .key_down,
359
+ .key = "escape",
360
+ } });
361
+ try std.testing.expectEqualStrings("", app_state.model.queryText());
362
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
363
+
364
+ // A recorded MULTI-LINE PASTE replays to the identical sanitized
365
+ // value: copy the textarea's two lines (select-all + copy — raw
366
+ // journaled inputs that rebuild the clipboard on replay), then paste
367
+ // into the single-line search field. Sanitization is deterministic
368
+ // derivation at the apply seam, so the replayed editor, the model
369
+ // mirror, and the fingerprint all land on the same stripped bytes.
370
+ var textarea_frame: ?geometry.RectF = null;
371
+ for ((try harness.runtime.canvasWidgetLayout(1, canvas_label)).nodes) |node| {
372
+ if (node.widget.kind == .textarea) textarea_frame = node.frame;
373
+ }
374
+ const source_frame = textarea_frame.?;
375
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
376
+ .window_id = 1,
377
+ .label = canvas_label,
378
+ .kind = .pointer_down,
379
+ .x = source_frame.x + source_frame.width * 0.5,
380
+ .y = source_frame.y + source_frame.height * 0.5,
381
+ } });
382
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
383
+ .window_id = 1,
384
+ .label = canvas_label,
385
+ .kind = .key_down,
386
+ .key = "a",
387
+ .modifiers = .{ .primary = true, .command = true },
388
+ } });
389
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
390
+ .window_id = 1,
391
+ .label = canvas_label,
392
+ .kind = .key_down,
393
+ .key = "c",
394
+ .modifiers = .{ .primary = true, .command = true },
395
+ } });
396
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
397
+ .window_id = 1,
398
+ .label = canvas_label,
399
+ .kind = .pointer_down,
400
+ .x = field_frame.x + field_frame.width * 0.5,
401
+ .y = field_frame.y + field_frame.height * 0.5,
402
+ } });
403
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
404
+ .window_id = 1,
405
+ .label = canvas_label,
406
+ .kind = .key_down,
407
+ .key = "v",
408
+ .modifiers = .{ .primary = true, .command = true },
409
+ } });
410
+ try std.testing.expectEqualStrings("line oneline two", app_state.model.queryText());
411
+
412
+ // A trackpad pinch, twice over: the raw journaled phase stream (the
413
+ // macOS host shape — begin, two deltas, end; the +25% then -25%
414
+ // deltas compound as a PRODUCT, 1.25 * 0.75 = 0.9375, never a sum's
415
+ // 1.0), then the automation verb's synthesized gesture, which
416
+ // journals the same leaf gpu_surface_input events. Replay must
417
+ // re-derive the identical cumulative zoom from the journaled scale
418
+ // fields alone. Every value here is binary-exact in f32, so the
419
+ // model equality below is exact, not approximate.
420
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
421
+ .window_id = 1,
422
+ .label = canvas_label,
423
+ .kind = .pinch_begin,
424
+ .x = 200,
425
+ .y = 150,
426
+ } });
427
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
428
+ .window_id = 1,
429
+ .label = canvas_label,
430
+ .kind = .pinch_change,
431
+ .x = 200,
432
+ .y = 150,
433
+ .scale = 0.25,
434
+ } });
435
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
436
+ .window_id = 1,
437
+ .label = canvas_label,
438
+ .kind = .pinch_change,
439
+ .x = 200,
440
+ .y = 150,
441
+ .scale = -0.25,
442
+ } });
443
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
444
+ .window_id = 1,
445
+ .label = canvas_label,
446
+ .kind = .pinch_end,
447
+ .x = 200,
448
+ .y = 150,
449
+ } });
450
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
451
+ var pinch_buffer: [64]u8 = undefined;
452
+ const pinch_command = try std.fmt.bufPrint(&pinch_buffer, "widget-pinch {s} 1.5 120 80", .{canvas_label});
453
+ try harness.runtime.dispatchAutomationCommand(app, pinch_command);
454
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
455
+
245
456
  recorder.finish();
246
457
  try std.testing.expect(!recorder.failed);
247
458
 
@@ -294,6 +505,16 @@ test "a recorded session replays to identical model state and fingerprints" {
294
505
  try std.testing.expect(recorded.model.stamp_ms != 0);
295
506
  try std.testing.expectEqual(@as(u32, 1), recorded.model.spectrum_count);
296
507
  try std.testing.expect(recorded.model.band_checksum != 0);
508
+ // The typed insert, the DERIVED Escape-clear, and the sanitized
509
+ // multi-line paste all reached the model's `on_input` mirror — the
510
+ // paste landing STRIPPED of its line breaks (single-line rule).
511
+ try std.testing.expectEqual(@as(u32, 3), recorded.model.query_edits);
512
+ try std.testing.expectEqualStrings("line oneline two", recorded.model.queryText());
513
+ // Both pinch gestures reached the model: the raw stream's product
514
+ // (1.25 * 0.75 = 0.9375) times the verb's exact 1.5.
515
+ try std.testing.expectEqual(@as(u32, 2), recorded.model.pinch_begins);
516
+ try std.testing.expectEqual(@as(u32, 2), recorded.model.pinch_ends);
517
+ try std.testing.expectEqual(@as(f32, 1.40625), recorded.model.zoom);
297
518
 
298
519
  const replayed = try replayIntoFreshApp(gpa, buffer.journalBytes(), true);
299
520
  try std.testing.expect(replayed.report.ok());
@@ -327,6 +548,360 @@ test "a native-only session records and replays like a web-layer one" {
327
548
  try std.testing.expectEqual(recorded.fingerprint, replayed.fingerprint);
328
549
  }
329
550
 
551
+ test "accessibility actions journal once and replay without double-dispatch" {
552
+ // A journaled `widget_accessibility_action` re-runs its verb on
553
+ // replay, and the verb synthesizes REAL platform events (press its
554
+ // Enter key, set_text a select-all plus a text input). If those
555
+ // children also landed in the journal, replay would dispatch them
556
+ // twice — the stray child arrives first, against the focus the
557
+ // PREVIOUS action left behind, so a repeated press increments an
558
+ // extra time and a repeated set_text edits the field extra times.
559
+ // The recorder journals outer-wins: exactly one record per action,
560
+ // and live/replay model counters match exactly.
561
+ const gpa = std.testing.allocator;
562
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
563
+ defer std.heap.page_allocator.destroy(buffer);
564
+ buffer.len = 0;
565
+
566
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
567
+ defer std.heap.page_allocator.destroy(recorder);
568
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
569
+ recorder.begin(.{ .platform_name = "test", .app_name = "session-demo", .window_width = 400, .window_height = 300 });
570
+
571
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
572
+ defer harness.destroy(gpa);
573
+ harness.null_platform.gpu_surfaces = true;
574
+ harness.runtime.options.session_recorder = recorder;
575
+
576
+ const app_state = try gpa.create(SessionApp);
577
+ defer gpa.destroy(app_state);
578
+ app_state.* = SessionApp.init(std.heap.page_allocator, .{}, sessionOptions());
579
+ defer app_state.deinit();
580
+ app_state.effects.executor = .fake;
581
+ const app = app_state.app();
582
+
583
+ try harness.start(app);
584
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
585
+ .label = canvas_label,
586
+ .size = geometry.SizeF.init(400, 300),
587
+ .scale_factor = 2,
588
+ .frame_index = 1,
589
+ .timestamp_ns = 1_000_000,
590
+ } });
591
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
592
+
593
+ const layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
594
+ var button_id: canvas.ObjectId = 0;
595
+ var field_id: canvas.ObjectId = 0;
596
+ for (layout.nodes) |node| {
597
+ if (node.widget.kind == .button) button_id = node.widget.id;
598
+ if (node.widget.kind == .search_field) field_id = node.widget.id;
599
+ }
600
+ try std.testing.expect(button_id != 0);
601
+ try std.testing.expect(field_id != 0);
602
+
603
+ // Two AX presses: the second is the doubling witness — with the
604
+ // synthesized Enter also journaled, replay delivers it against the
605
+ // already-focused button and the count lands at 3, not 2.
606
+ try harness.runtime.dispatchPlatformEvent(app, .{ .widget_accessibility_action = .{
607
+ .window_id = 1,
608
+ .label = canvas_label,
609
+ .id = button_id,
610
+ .action = .press,
611
+ } });
612
+ try harness.runtime.dispatchPlatformEvent(app, .{ .widget_accessibility_action = .{
613
+ .window_id = 1,
614
+ .label = canvas_label,
615
+ .id = button_id,
616
+ .action = .press,
617
+ } });
618
+ try std.testing.expectEqual(@as(u32, 2), app_state.model.count);
619
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
620
+
621
+ // Two AX set_texts: the second one's stray select-all + text input
622
+ // would hit the focused field on replay and inflate `query_edits`.
623
+ try harness.runtime.dispatchPlatformEvent(app, .{ .widget_accessibility_action = .{
624
+ .window_id = 1,
625
+ .label = canvas_label,
626
+ .id = field_id,
627
+ .action = .set_text,
628
+ .text = "glass",
629
+ } });
630
+ try harness.runtime.dispatchPlatformEvent(app, .{ .widget_accessibility_action = .{
631
+ .window_id = 1,
632
+ .label = canvas_label,
633
+ .id = field_id,
634
+ .action = .set_text,
635
+ .text = "brass",
636
+ } });
637
+ try std.testing.expectEqualStrings("brass", app_state.model.queryText());
638
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
639
+ const edits_after_set_text = app_state.model.query_edits;
640
+
641
+ // An IME composition through the DIRECT verb surface (the embed
642
+ // host's `widgetAction` path): no platform event arrives, so the
643
+ // dispatch stages its own synthetic `widget_accessibility_action`
644
+ // record — outer-wins exactly like the platform path — and the
645
+ // synthesized ime children stay out of the journal. Replay re-runs
646
+ // the verb, focus included.
647
+ _ = try harness.runtime.dispatchCanvasWidgetAccessibilityAction(app, 1, canvas_label, .{
648
+ .id = field_id,
649
+ .action = .set_composition,
650
+ .text = "ne",
651
+ });
652
+ _ = try harness.runtime.dispatchCanvasWidgetAccessibilityAction(app, 1, canvas_label, .{
653
+ .id = field_id,
654
+ .action = .commit_composition,
655
+ });
656
+ try std.testing.expectEqualStrings("brassne", app_state.model.queryText());
657
+ // Both composition edits reached the model's `on_input` mirror.
658
+ try std.testing.expect(app_state.model.query_edits > edits_after_set_text);
659
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
660
+
661
+ recorder.finish();
662
+ try std.testing.expect(!recorder.failed);
663
+ const recorded_model = app_state.model;
664
+ const recorded_fingerprint = harness.runtime.sessionStateFingerprint();
665
+
666
+ // The journal carries one record per AX action — platform events
667
+ // AND direct verb calls — and none of their synthesized key/text/ime
668
+ // children.
669
+ var reader = try journal.Reader.init(buffer.journalBytes());
670
+ var action_records: usize = 0;
671
+ while (try reader.next()) |record| {
672
+ if (record != .event) continue;
673
+ switch (record.event) {
674
+ .widget_accessibility_action => action_records += 1,
675
+ .gpu_surface_input => |input| switch (input.kind) {
676
+ .ime_set_composition, .ime_commit_composition, .ime_cancel_composition, .key_down, .text_input => return error.SynthesizedChildJournaled,
677
+ else => {},
678
+ },
679
+ else => {},
680
+ }
681
+ }
682
+ try std.testing.expectEqual(@as(usize, 6), action_records);
683
+
684
+ // Replay into a fresh app: every counter must match exactly — a
685
+ // double-dispatched child shows up as +1 on `count` or extra
686
+ // `query_edits`.
687
+ const replayed = try replayIntoFreshApp(gpa, buffer.journalBytes(), true);
688
+ try std.testing.expect(replayed.report.ok());
689
+ try std.testing.expectEqual(@as(u32, 2), replayed.model.count);
690
+ try std.testing.expectEqual(recorded_model.query_edits, replayed.model.query_edits);
691
+ try std.testing.expectEqualDeep(recorded_model, replayed.model);
692
+ try std.testing.expectEqual(recorded_fingerprint, replayed.fingerprint);
693
+ }
694
+
695
+ /// Shared scaffold for the direct-verb replay tests: recorder, harness,
696
+ /// started app, one presented frame, and the two editors' widget ids.
697
+ const DirectVerbSession = struct {
698
+ recorder: *session_record.SessionRecorder,
699
+ harness: *core.TestHarness(),
700
+ app_state: *SessionApp,
701
+ app: core.App,
702
+ search_id: canvas.ObjectId,
703
+ name_id: canvas.ObjectId,
704
+
705
+ fn start(gpa: std.mem.Allocator, buffer: *JournalBuffer) !DirectVerbSession {
706
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
707
+ errdefer std.heap.page_allocator.destroy(recorder);
708
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
709
+ recorder.begin(.{ .platform_name = "test", .app_name = "session-demo", .window_width = 400, .window_height = 300 });
710
+
711
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
712
+ errdefer harness.destroy(gpa);
713
+ harness.null_platform.gpu_surfaces = true;
714
+ harness.runtime.options.session_recorder = recorder;
715
+
716
+ const app_state = try gpa.create(SessionApp);
717
+ errdefer gpa.destroy(app_state);
718
+ app_state.* = SessionApp.init(std.heap.page_allocator, .{}, sessionOptions());
719
+ app_state.effects.executor = .fake;
720
+ const app = app_state.app();
721
+
722
+ try harness.start(app);
723
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
724
+ .label = canvas_label,
725
+ .size = geometry.SizeF.init(400, 300),
726
+ .scale_factor = 2,
727
+ .frame_index = 1,
728
+ .timestamp_ns = 1_000_000,
729
+ } });
730
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
731
+
732
+ const layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
733
+ var search_id: canvas.ObjectId = 0;
734
+ var name_id: canvas.ObjectId = 0;
735
+ for (layout.nodes) |node| {
736
+ if (node.widget.kind == .search_field) search_id = node.widget.id;
737
+ if (node.widget.kind == .text_field) name_id = node.widget.id;
738
+ }
739
+ try std.testing.expect(search_id != 0);
740
+ try std.testing.expect(name_id != 0);
741
+
742
+ return .{
743
+ .recorder = recorder,
744
+ .harness = harness,
745
+ .app_state = app_state,
746
+ .app = app,
747
+ .search_id = search_id,
748
+ .name_id = name_id,
749
+ };
750
+ }
751
+
752
+ fn destroy(self: *const DirectVerbSession, gpa: std.mem.Allocator) void {
753
+ self.app_state.deinit();
754
+ gpa.destroy(self.app_state);
755
+ self.harness.destroy(gpa);
756
+ std.heap.page_allocator.destroy(self.recorder);
757
+ }
758
+ };
759
+
760
+ /// The direct-verb journal must carry the AX action records and NONE of
761
+ /// their synthesized children — a child record is exactly the stray
762
+ /// input that replays against the wrong focus.
763
+ fn expectActionOnlyJournal(journal_bytes: []const u8, expected_actions: usize) !void {
764
+ var reader = try journal.Reader.init(journal_bytes);
765
+ var action_records: usize = 0;
766
+ while (try reader.next()) |record| {
767
+ if (record != .event) continue;
768
+ switch (record.event) {
769
+ .widget_accessibility_action => action_records += 1,
770
+ .gpu_surface_input => |input| switch (input.kind) {
771
+ .ime_set_composition, .ime_commit_composition, .ime_cancel_composition, .key_down, .text_input => return error.SynthesizedChildJournaled,
772
+ else => {},
773
+ },
774
+ else => {},
775
+ }
776
+ }
777
+ try std.testing.expectEqual(expected_actions, action_records);
778
+ }
779
+
780
+ test "a direct-verb composition first in a session replays onto its target editor" {
781
+ // The P1 this pins: a direct-verb call (embed `widgetAction`,
782
+ // automation `widget_action`) journaled only its synthesized ime
783
+ // children — untargeted inputs routed by focus — while the verb's
784
+ // own focus write never reached the journal. A composition as the
785
+ // FIRST action of a session therefore replayed against a fresh
786
+ // runtime with NO focused editor and the text vanished (the old
787
+ // test passed only because earlier journaled AX records happened to
788
+ // re-focus the same field). The synthetic outer record replays the
789
+ // verb, which re-runs the focus.
790
+ const gpa = std.testing.allocator;
791
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
792
+ defer std.heap.page_allocator.destroy(buffer);
793
+ buffer.len = 0;
794
+
795
+ const session = try DirectVerbSession.start(gpa, buffer);
796
+ defer session.destroy(gpa);
797
+
798
+ _ = try session.harness.runtime.dispatchCanvasWidgetAccessibilityAction(session.app, 1, canvas_label, .{
799
+ .id = session.search_id,
800
+ .action = .set_composition,
801
+ .text = "ne",
802
+ });
803
+ _ = try session.harness.runtime.dispatchCanvasWidgetAccessibilityAction(session.app, 1, canvas_label, .{
804
+ .id = session.search_id,
805
+ .action = .commit_composition,
806
+ });
807
+ try std.testing.expectEqualStrings("ne", session.app_state.model.queryText());
808
+ try session.harness.runtime.dispatchPlatformEvent(session.app, .frame_requested);
809
+
810
+ session.recorder.finish();
811
+ try std.testing.expect(!session.recorder.failed);
812
+ try expectActionOnlyJournal(buffer.journalBytes(), 2);
813
+
814
+ const replayed = try replayIntoFreshApp(gpa, buffer.journalBytes(), true);
815
+ try std.testing.expect(replayed.report.ok());
816
+ try std.testing.expectEqualStrings("ne", replayed.model.queryText());
817
+ try std.testing.expectEqualDeep(session.app_state.model, replayed.model);
818
+ try std.testing.expectEqual(session.harness.runtime.sessionStateFingerprint(), replayed.fingerprint);
819
+ }
820
+
821
+ test "a direct-verb composition targets its editor while another field holds focus" {
822
+ // Focus field B (a real journaled click), then compose into field A
823
+ // through the direct verb surface. Live, the verb re-focuses A; a
824
+ // replay that only saw the ime children would deliver them to B —
825
+ // the field the session left focused — writing the text into the
826
+ // wrong editor. The outer record re-runs the verb against A.
827
+ const gpa = std.testing.allocator;
828
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
829
+ defer std.heap.page_allocator.destroy(buffer);
830
+ buffer.len = 0;
831
+
832
+ const session = try DirectVerbSession.start(gpa, buffer);
833
+ defer session.destroy(gpa);
834
+
835
+ const layout = try session.harness.runtime.canvasWidgetLayout(1, canvas_label);
836
+ const search_frame = layout.findById(session.search_id).?.frame;
837
+ try session.harness.runtime.dispatchPlatformEvent(session.app, .{ .gpu_surface_input = .{
838
+ .window_id = 1,
839
+ .label = canvas_label,
840
+ .kind = .pointer_down,
841
+ .x = search_frame.x + search_frame.width * 0.5,
842
+ .y = search_frame.y + search_frame.height * 0.5,
843
+ } });
844
+ try session.harness.runtime.dispatchPlatformEvent(session.app, .frame_requested);
845
+
846
+ _ = try session.harness.runtime.dispatchCanvasWidgetAccessibilityAction(session.app, 1, canvas_label, .{
847
+ .id = session.name_id,
848
+ .action = .set_composition,
849
+ .text = "ada",
850
+ });
851
+ _ = try session.harness.runtime.dispatchCanvasWidgetAccessibilityAction(session.app, 1, canvas_label, .{
852
+ .id = session.name_id,
853
+ .action = .commit_composition,
854
+ });
855
+ try std.testing.expectEqualStrings("ada", session.app_state.model.nameText());
856
+ try std.testing.expectEqualStrings("", session.app_state.model.queryText());
857
+ try session.harness.runtime.dispatchPlatformEvent(session.app, .frame_requested);
858
+
859
+ session.recorder.finish();
860
+ try std.testing.expect(!session.recorder.failed);
861
+ try expectActionOnlyJournal(buffer.journalBytes(), 2);
862
+
863
+ const replayed = try replayIntoFreshApp(gpa, buffer.journalBytes(), true);
864
+ try std.testing.expect(replayed.report.ok());
865
+ // The composition landed on A, and B stayed clean — the wrong-focus
866
+ // failure writes "ada" into `query` instead.
867
+ try std.testing.expectEqualStrings("ada", replayed.model.nameText());
868
+ try std.testing.expectEqualStrings("", replayed.model.queryText());
869
+ try std.testing.expectEqualDeep(session.app_state.model, replayed.model);
870
+ try std.testing.expectEqual(session.harness.runtime.sessionStateFingerprint(), replayed.fingerprint);
871
+ }
872
+
873
+ test "a direct-surface set_text first in a session replays onto its target editor" {
874
+ // Same hole, set_text shape: the direct verb journaled a select-all
875
+ // key plus a text input — both routed by focus — so first-in-session
876
+ // they replayed into nothing. One outer record, replayed through the
877
+ // verb, re-runs focus + select-all + insert exactly as live.
878
+ const gpa = std.testing.allocator;
879
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
880
+ defer std.heap.page_allocator.destroy(buffer);
881
+ buffer.len = 0;
882
+
883
+ const session = try DirectVerbSession.start(gpa, buffer);
884
+ defer session.destroy(gpa);
885
+
886
+ _ = try session.harness.runtime.dispatchCanvasWidgetAccessibilityAction(session.app, 1, canvas_label, .{
887
+ .id = session.search_id,
888
+ .action = .set_text,
889
+ .text = "glass",
890
+ });
891
+ try std.testing.expectEqualStrings("glass", session.app_state.model.queryText());
892
+ try session.harness.runtime.dispatchPlatformEvent(session.app, .frame_requested);
893
+
894
+ session.recorder.finish();
895
+ try std.testing.expect(!session.recorder.failed);
896
+ try expectActionOnlyJournal(buffer.journalBytes(), 1);
897
+
898
+ const replayed = try replayIntoFreshApp(gpa, buffer.journalBytes(), true);
899
+ try std.testing.expect(replayed.report.ok());
900
+ try std.testing.expectEqualStrings("glass", replayed.model.queryText());
901
+ try std.testing.expectEqualDeep(session.app_state.model, replayed.model);
902
+ try std.testing.expectEqual(session.harness.runtime.sessionStateFingerprint(), replayed.fingerprint);
903
+ }
904
+
330
905
  test "a truncated journal is refused loudly" {
331
906
  const gpa = std.testing.allocator;
332
907
  const buffer = try std.heap.page_allocator.create(JournalBuffer);
@@ -14,6 +14,7 @@ test {
14
14
  _ = @import("ui_app_huge_model_tests.zig");
15
15
  _ = @import("ui_app_window_tests.zig");
16
16
  _ = @import("canvas_image_tests.zig");
17
+ _ = @import("media_surface_tests.zig");
17
18
  _ = @import("canvas_font_tests.zig");
18
19
  _ = @import("effects_tests.zig");
19
20
  _ = @import("effects_fetch_tests.zig");