@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,3609 @@
1
+ //! Video-effect coverage: `fx.loadVideo` and the transport commands
2
+ //! (`playVideo`/`pauseVideo`/`stopVideo`/`seekVideo`/`setVideoVolume`/
3
+ //! `setVideoMuted`/`setVideoLoop`) through the fake executor
4
+ //! (deterministic request/feed round trips, rejection classes) and the
5
+ //! real executor against the null platform's fake player — the same
6
+ //! `PlatformServices` seam AVFoundation serves on macOS. One channel,
7
+ //! key-identified events, the media-surface claim released on
8
+ //! stop/replace/failure, decoded frames reaching the surface through
9
+ //! the platform sink, and a recorded session replaying byte-identical
10
+ //! with no producer and no player behind it.
11
+
12
+ const std = @import("std");
13
+ const geometry = @import("geometry");
14
+ const app_manifest = @import("app_manifest");
15
+ const core = @import("core.zig");
16
+ const ui_app_model = @import("ui_app.zig");
17
+ const effects_mod = @import("effects.zig");
18
+ const platform = @import("../platform/root.zig");
19
+ const session_record = @import("session_record.zig");
20
+ const session_replay = @import("session_replay.zig");
21
+ const journal = @import("session_journal.zig");
22
+
23
+ const canvas_label = "video-canvas";
24
+
25
+ const video_views = [_]app_manifest.ShellView{
26
+ .{ .label = canvas_label, .kind = .gpu_surface, .fill = true, .gpu_backend = .metal },
27
+ };
28
+ const video_windows = [_]app_manifest.ShellWindow{.{
29
+ .label = "main",
30
+ .title = "Video",
31
+ .width = 400,
32
+ .height = 300,
33
+ .views = &video_views,
34
+ }};
35
+ const video_scene: app_manifest.ShellConfig = .{ .windows = &video_windows };
36
+
37
+ const VideoModel = struct {
38
+ event_count: usize = 0,
39
+ chain_next: bool = false,
40
+ probed_active: ?bool = null,
41
+ last_kind: ?effects_mod.EffectVideoEventKind = null,
42
+ last_key: u64 = 0,
43
+ last_position_ms: u64 = 0,
44
+ last_duration_ms: u64 = 0,
45
+ last_playing: bool = false,
46
+ last_buffering: bool = false,
47
+ last_width: u64 = 0,
48
+ last_height: u64 = 0,
49
+ completed_count: usize = 0,
50
+
51
+ fn record(model: *VideoModel, event: effects_mod.EffectVideo) void {
52
+ model.event_count += 1;
53
+ model.last_kind = event.kind;
54
+ model.last_key = event.key;
55
+ model.last_position_ms = event.position_ms;
56
+ model.last_duration_ms = event.duration_ms;
57
+ model.last_playing = event.playing;
58
+ model.last_buffering = event.buffering;
59
+ if (event.width > 0) model.last_width = event.width;
60
+ if (event.height > 0) model.last_height = event.height;
61
+ if (event.kind == .completed) model.completed_count += 1;
62
+ }
63
+ };
64
+
65
+ const VideoMsg = union(enum) {
66
+ load,
67
+ arm_chain,
68
+ load_url,
69
+ load_url_only,
70
+ load_no_source,
71
+ load_rejected_burst,
72
+ load_zero_surface,
73
+ load_reserved_surface,
74
+ load_bad_scheme,
75
+ load_paused,
76
+ load_url_paused,
77
+ load_looping,
78
+ load_then_stop,
79
+ load_then_stream,
80
+ load_then_probe,
81
+ play_then_probe,
82
+ load_source_burst,
83
+ play,
84
+ pause,
85
+ stop,
86
+ seek_half,
87
+ quiet,
88
+ mute_on,
89
+ loop_on,
90
+ video_event: effects_mod.EffectVideo,
91
+ };
92
+
93
+ const VideoApp = ui_app_model.UiApp(VideoModel, VideoMsg);
94
+ const VideoEffects = VideoApp.Effects;
95
+
96
+ const clip_key: u64 = 61;
97
+ const clip_surface: u64 = 907;
98
+ /// Past the shared pending ring's capacity (32), so a burst proves the
99
+ /// video stage never evicts a promised terminal.
100
+ const rejected_burst_count: usize = 40;
101
+ const clip_path = "assets/clips/orchard-flyover.mp4";
102
+ const clip_url = "https://media.example.test/clips/orchard-flyover.mp4";
103
+
104
+ fn videoUpdate(model: *VideoModel, msg: VideoMsg, fx: *VideoEffects) void {
105
+ switch (msg) {
106
+ .load => fx.loadVideo(.{
107
+ .key = clip_key,
108
+ .surface = clip_surface,
109
+ .path = clip_path,
110
+ .on_event = VideoEffects.videoMsg(.video_event),
111
+ }),
112
+ .arm_chain => model.chain_next = true,
113
+ // The full cascade shape: local path first, url fallback.
114
+ .load_url => fx.loadVideo(.{
115
+ .key = clip_key,
116
+ .surface = clip_surface,
117
+ .path = clip_path,
118
+ .url = clip_url,
119
+ .on_event = VideoEffects.videoMsg(.video_event),
120
+ }),
121
+ // URL-only: no local probe at all.
122
+ .load_url_only => fx.loadVideo(.{
123
+ .key = clip_key,
124
+ .surface = clip_surface,
125
+ .url = clip_url,
126
+ .on_event = VideoEffects.videoMsg(.video_event),
127
+ }),
128
+ .load_no_source => fx.loadVideo(.{
129
+ .key = clip_key,
130
+ .surface = clip_surface,
131
+ .on_event = VideoEffects.videoMsg(.video_event),
132
+ }),
133
+ // One dispatch, more refusals than the shared pending ring
134
+ // holds: every `.rejected` terminal is a load call's only
135
+ // answer and must survive to the next drain (the non-lossy
136
+ // video stage's contract).
137
+ .load_rejected_burst => for (0..rejected_burst_count) |_| fx.loadVideo(.{
138
+ .key = clip_key,
139
+ .surface = clip_surface,
140
+ .on_event = VideoEffects.videoMsg(.video_event),
141
+ }),
142
+ .load_zero_surface => fx.loadVideo(.{
143
+ .key = clip_key,
144
+ .surface = 0,
145
+ .path = clip_path,
146
+ .on_event = VideoEffects.videoMsg(.video_event),
147
+ }),
148
+ .load_reserved_surface => fx.loadVideo(.{
149
+ .key = clip_key,
150
+ .surface = @import("canvas").media_surface_image_id_bit | 7,
151
+ .path = clip_path,
152
+ .on_event = VideoEffects.videoMsg(.video_event),
153
+ }),
154
+ .load_bad_scheme => fx.loadVideo(.{
155
+ .key = clip_key,
156
+ .surface = clip_surface,
157
+ .url = "file:///etc/passwd",
158
+ .on_event = VideoEffects.videoMsg(.video_event),
159
+ }),
160
+ .load_paused => fx.loadVideo(.{
161
+ .key = clip_key,
162
+ .surface = clip_surface,
163
+ .path = clip_path,
164
+ .autoplay = false,
165
+ .on_event = VideoEffects.videoMsg(.video_event),
166
+ }),
167
+ .load_url_paused => fx.loadVideo(.{
168
+ .key = clip_key,
169
+ .surface = clip_surface,
170
+ .url = clip_url,
171
+ .autoplay = false,
172
+ .on_event = VideoEffects.videoMsg(.video_event),
173
+ }),
174
+ .load_looping => fx.loadVideo(.{
175
+ .key = clip_key,
176
+ .surface = clip_surface,
177
+ .path = clip_path,
178
+ .loop = true,
179
+ .muted = true,
180
+ .on_event = VideoEffects.videoMsg(.video_event),
181
+ }),
182
+ // The batch shape: one dispatch loads and immediately stops.
183
+ // On an assets-absent host the load's synchronous `.failed` is
184
+ // staged before the stop runs — the terminal outlives the
185
+ // playback it reports on.
186
+ .load_then_stop => {
187
+ fx.loadVideo(.{
188
+ .key = clip_key,
189
+ .surface = clip_surface,
190
+ .path = clip_path,
191
+ .on_event = VideoEffects.videoMsg(.video_event),
192
+ });
193
+ fx.stopVideo();
194
+ },
195
+ // The snapshot probe: an update that inspects the channel
196
+ // right after its own load. A synchronous refusal resets the
197
+ // channel before loadVideo returns, and the probe must read
198
+ // the same answer live and replayed.
199
+ .load_then_probe => {
200
+ fx.loadVideo(.{
201
+ .key = clip_key,
202
+ .surface = clip_surface,
203
+ .path = clip_path,
204
+ .on_event = VideoEffects.videoMsg(.video_event),
205
+ });
206
+ model.probed_active = fx.videoSnapshot().active;
207
+ },
208
+ // The post-completion resume probe: play against a retired
209
+ // player resets the channel before playVideo returns, and the
210
+ // probe must read the same answer live and replayed.
211
+ .play_then_probe => {
212
+ fx.playVideo();
213
+ model.probed_active = fx.videoSnapshot().active;
214
+ },
215
+ // The replace shape under the same app key: the first load's
216
+ // synchronous `.failed` must keep its own identity while the
217
+ // replacing stream plays on — the key alone cannot tell the
218
+ // two loads apart.
219
+ .load_then_stream => {
220
+ fx.loadVideo(.{
221
+ .key = clip_key,
222
+ .surface = clip_surface,
223
+ .path = clip_path,
224
+ .on_event = VideoEffects.videoMsg(.video_event),
225
+ });
226
+ fx.loadVideo(.{
227
+ .key = clip_key,
228
+ .surface = clip_surface,
229
+ .url = clip_url,
230
+ .on_event = VideoEffects.videoMsg(.video_event),
231
+ });
232
+ },
233
+ // One dispatch, more resolved loads than the replay-side
234
+ // cascade-resolution queue holds inline: loads per dispatch are
235
+ // unbounded by contract, so every recorded resolution must
236
+ // survive to its replayed load.
237
+ .load_source_burst => for (0..effects_mod.max_effect_replay_video_source_entries + 1) |_| fx.loadVideo(.{
238
+ .key = clip_key,
239
+ .surface = clip_surface,
240
+ .path = clip_path,
241
+ .url = clip_url,
242
+ .autoplay = false,
243
+ .on_event = VideoEffects.videoMsg(.video_event),
244
+ }),
245
+ .play => fx.playVideo(),
246
+ .pause => fx.pauseVideo(),
247
+ .stop => fx.stopVideo(),
248
+ .seek_half => fx.seekVideo(45_000),
249
+ .quiet => fx.setVideoVolume(0.25),
250
+ .mute_on => fx.setVideoMuted(true),
251
+ .loop_on => fx.setVideoLoop(true),
252
+ .video_event => |event| {
253
+ model.record(event);
254
+ // The playlist shape: the completion handler starts the
255
+ // next clip from inside its own dispatch.
256
+ if (model.chain_next and event.kind == .completed) {
257
+ model.chain_next = false;
258
+ fx.loadVideo(.{
259
+ .key = clip_key + 1,
260
+ .surface = clip_surface,
261
+ .path = clip_path,
262
+ .on_event = VideoEffects.videoMsg(.video_event),
263
+ });
264
+ }
265
+ },
266
+ }
267
+ }
268
+
269
+ fn videoCommand(name: []const u8) ?VideoMsg {
270
+ if (std.mem.eql(u8, name, "video.load")) return .load;
271
+ if (std.mem.eql(u8, name, "video.arm-chain")) return .arm_chain;
272
+ if (std.mem.eql(u8, name, "video.load-url")) return .load_url;
273
+ if (std.mem.eql(u8, name, "video.load-then-stop")) return .load_then_stop;
274
+ if (std.mem.eql(u8, name, "video.load-then-probe")) return .load_then_probe;
275
+ if (std.mem.eql(u8, name, "video.play-then-probe")) return .play_then_probe;
276
+ if (std.mem.eql(u8, name, "video.load-then-stream")) return .load_then_stream;
277
+ if (std.mem.eql(u8, name, "video.load-source-burst")) return .load_source_burst;
278
+ if (std.mem.eql(u8, name, "video.seek-half")) return .seek_half;
279
+ if (std.mem.eql(u8, name, "video.load-zero-surface")) return .load_zero_surface;
280
+ return null;
281
+ }
282
+
283
+ fn videoView(ui: *VideoApp.Ui, model: *const VideoModel) VideoApp.Ui.Node {
284
+ return ui.column(.{ .gap = 4, .padding = 8 }, .{
285
+ ui.text(.{}, ui.fmt("{d} events", .{model.event_count})),
286
+ ui.mediaSurface(.{ .image = clip_surface, .width = 128, .height = 72 }),
287
+ ui.button(.{ .on_press = .load }, "Load"),
288
+ ui.button(.{ .on_press = .pause }, "Pause"),
289
+ ui.button(.{ .on_press = .stop }, "Stop"),
290
+ });
291
+ }
292
+
293
+ const Harness = struct {
294
+ harness: *core.TestHarness(),
295
+ app_state: *VideoApp,
296
+ app: core.App,
297
+
298
+ const Config = struct {
299
+ /// false models a staged host without a video decoder (Windows/
300
+ /// Linux today): the services are nulled BEFORE the platform
301
+ /// value is captured, the same shape the real hosts wire.
302
+ video_playback: bool = true,
303
+ /// false models the assets-absent machine: every local load
304
+ /// answers VideoSourceNotFound, sending the cascade to the URL.
305
+ video_local_files: bool = true,
306
+ };
307
+
308
+ fn create() !Harness {
309
+ return createConfigured(.{});
310
+ }
311
+
312
+ fn createConfigured(config: Config) !Harness {
313
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
314
+ errdefer harness.destroy(std.testing.allocator);
315
+ harness.null_platform.gpu_surfaces = true;
316
+ harness.null_platform.video_playback = config.video_playback;
317
+ harness.null_platform.video_local_files = config.video_local_files;
318
+ // The harness snapshots the services at create; re-capture so
319
+ // the toggles above null the service fns the runtime hands the
320
+ // effects channel — the same wiring a real decoder-less host
321
+ // ships.
322
+ harness.runtime.options.platform = harness.null_platform.platform();
323
+ const app_state = try std.testing.allocator.create(VideoApp);
324
+ errdefer std.testing.allocator.destroy(app_state);
325
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
326
+ .name = "effects-video",
327
+ .scene = video_scene,
328
+ .canvas_label = canvas_label,
329
+ .update_fx = videoUpdate,
330
+ .view = videoView,
331
+ .on_command = videoCommand,
332
+ });
333
+ const app = app_state.app();
334
+ try harness.start(app);
335
+ try dispatchFrame(harness, app, 1);
336
+ try std.testing.expect(app_state.installed);
337
+ return .{ .harness = harness, .app_state = app_state, .app = app };
338
+ }
339
+
340
+ fn destroy(self: *Harness) void {
341
+ self.app_state.deinit();
342
+ std.testing.allocator.destroy(self.app_state);
343
+ self.harness.destroy(std.testing.allocator);
344
+ }
345
+
346
+ fn drainWakes(self: *Harness) !void {
347
+ var nudged = false;
348
+ while (self.harness.null_platform.takeWake()) |_| nudged = true;
349
+ if (nudged) try self.harness.runtime.dispatchPlatformEvent(self.app, .wake);
350
+ }
351
+ };
352
+
353
+ fn dispatchFrame(harness: *core.TestHarness(), app: core.App, frame_index: u64) !void {
354
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
355
+ .label = canvas_label,
356
+ .size = geometry.SizeF.init(400, 300),
357
+ .scale_factor = 1,
358
+ .frame_index = frame_index,
359
+ .timestamp_ns = frame_index * 1_000_000,
360
+ .nonblank = true,
361
+ } });
362
+ }
363
+
364
+ /// A tiny solid RGBA8 frame (2x2) in one color.
365
+ fn solidFrame(rgba: [4]u8) [16]u8 {
366
+ var frame: [16]u8 = undefined;
367
+ inline for (0..4) |pixel| @memcpy(frame[pixel * 4 .. pixel * 4 + 4], &rgba);
368
+ return frame;
369
+ }
370
+
371
+ // ------------------------------------------------------------ fake executor
372
+
373
+ test "the reserved video surface id survives the f64 wire" {
374
+ // The TS tier names surfaces in f64 (Cmd.videoLoad), and a
375
+ // source-less <video> with custom controls is fed by the app's own
376
+ // load naming exactly this id — so it must sit inside the
377
+ // exact-integer window, clear of the derived-texture namespace,
378
+ // and round-trip the wire unchanged.
379
+ const id = @import("canvas").video_playback_surface_id;
380
+ try std.testing.expect(id >= 1);
381
+ try std.testing.expect(id < (1 << 53));
382
+ try std.testing.expect(id & @import("canvas").media_surface_image_id_bit == 0);
383
+ const wire: f64 = @floatFromInt(id);
384
+ try std.testing.expectEqual(id, @as(u64, @intFromFloat(wire)));
385
+ }
386
+
387
+ test "fake executor records the load request and feeds events back as msgs" {
388
+ var h = try Harness.create();
389
+ defer h.destroy();
390
+ const fx = &h.app_state.effects;
391
+ fx.executor = .fake;
392
+
393
+ // Load: the request is recorded whole, not executed — nothing
394
+ // touches the platform player and no surface claim is taken.
395
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load);
396
+ const request = fx.pendingVideo().?;
397
+ try std.testing.expectEqual(clip_key, request.key);
398
+ try std.testing.expectEqual(clip_surface, request.surface);
399
+ try std.testing.expectEqualStrings(clip_path, request.path);
400
+ try std.testing.expect(request.playing);
401
+ try std.testing.expect(!request.looping);
402
+ try std.testing.expect(!request.muted);
403
+ try std.testing.expectEqual(@as(usize, 0), h.harness.null_platform.video_load_count);
404
+
405
+ // The loaded acknowledgment carries the real dimensions and
406
+ // duration into update.
407
+ try fx.feedVideoEvent(.loaded, 0, 92_500, true, false, 1280, 720);
408
+ try h.harness.runtime.dispatchPlatformEvent(h.app, .wake);
409
+ try std.testing.expectEqual(@as(usize, 1), h.app_state.model.event_count);
410
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.loaded, h.app_state.model.last_kind.?);
411
+ try std.testing.expectEqual(clip_key, h.app_state.model.last_key);
412
+ try std.testing.expectEqual(@as(u64, 92_500), h.app_state.model.last_duration_ms);
413
+ try std.testing.expectEqual(@as(u64, 1280), h.app_state.model.last_width);
414
+ try std.testing.expectEqual(@as(u64, 720), h.app_state.model.last_height);
415
+
416
+ // Position ticks advance the mirrors the snapshot reports.
417
+ try fx.feedVideoEvent(.position, 1_500, 92_500, true, false, 0, 0);
418
+ try h.harness.runtime.dispatchPlatformEvent(h.app, .wake);
419
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.position, h.app_state.model.last_kind.?);
420
+ try std.testing.expectEqual(@as(u64, 1_500), h.app_state.model.last_position_ms);
421
+ try std.testing.expect(h.app_state.model.last_playing);
422
+ try std.testing.expectEqual(@as(u64, 1_500), fx.videoSnapshot().position_ms);
423
+ // The dimension mirrors persist from the loaded acknowledgment.
424
+ try std.testing.expectEqual(@as(u64, 1280), fx.videoSnapshot().width);
425
+
426
+ // Completion fires once, pinned to the duration, playback stopped.
427
+ try fx.feedVideoEvent(.completed, 92_500, 92_500, false, false, 0, 0);
428
+ try h.harness.runtime.dispatchPlatformEvent(h.app, .wake);
429
+ try std.testing.expectEqual(@as(usize, 1), h.app_state.model.completed_count);
430
+ try std.testing.expectEqual(@as(u64, 92_500), h.app_state.model.last_position_ms);
431
+ try std.testing.expect(!h.app_state.model.last_playing);
432
+ try std.testing.expect(fx.videoSnapshot().active);
433
+ try std.testing.expect(!fx.videoSnapshot().playing);
434
+ }
435
+
436
+ test "fake transport commands move the mirrors without a platform" {
437
+ var h = try Harness.create();
438
+ defer h.destroy();
439
+ const fx = &h.app_state.effects;
440
+ fx.executor = .fake;
441
+
442
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load);
443
+ try fx.feedVideoEvent(.loaded, 0, 90_000, true, false, 640, 360);
444
+ try h.harness.runtime.dispatchPlatformEvent(h.app, .wake);
445
+
446
+ try h.app_state.dispatch(&h.harness.runtime, 1, .pause);
447
+ try std.testing.expect(!fx.videoSnapshot().playing);
448
+ try h.app_state.dispatch(&h.harness.runtime, 1, .play);
449
+ try std.testing.expect(fx.videoSnapshot().playing);
450
+ try h.app_state.dispatch(&h.harness.runtime, 1, .seek_half);
451
+ try std.testing.expectEqual(@as(u64, 45_000), fx.videoSnapshot().position_ms);
452
+ try h.app_state.dispatch(&h.harness.runtime, 1, .quiet);
453
+ try std.testing.expectEqual(@as(f32, 0.25), fx.videoSnapshot().volume);
454
+ try h.app_state.dispatch(&h.harness.runtime, 1, .mute_on);
455
+ try std.testing.expect(fx.videoSnapshot().muted);
456
+ try h.app_state.dispatch(&h.harness.runtime, 1, .loop_on);
457
+ try std.testing.expect(fx.videoSnapshot().looping);
458
+
459
+ // Stop clears the channel; late feeds report EffectNotFound.
460
+ try h.app_state.dispatch(&h.harness.runtime, 1, .stop);
461
+ try std.testing.expect(fx.pendingVideo() == null);
462
+ try std.testing.expect(!fx.videoSnapshot().active);
463
+ try std.testing.expectError(error.EffectNotFound, fx.feedVideoEvent(.position, 46_000, 90_000, true, false, 0, 0));
464
+ }
465
+
466
+ test "load requests that cannot run are rejected loudly, never silently" {
467
+ var h = try Harness.create();
468
+ defer h.destroy();
469
+ const fx = &h.app_state.effects;
470
+ fx.executor = .fake;
471
+
472
+ const rejects = [_]VideoMsg{ .load_no_source, .load_zero_surface, .load_reserved_surface, .load_bad_scheme };
473
+ for (rejects, 1..) |msg, expected| {
474
+ try h.app_state.dispatch(&h.harness.runtime, 1, msg);
475
+ try h.drainWakes();
476
+ try std.testing.expectEqual(expected, h.app_state.model.event_count);
477
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.rejected, h.app_state.model.last_kind.?);
478
+ try std.testing.expectEqual(clip_key, h.app_state.model.last_key);
479
+ try std.testing.expect(fx.pendingVideo() == null);
480
+ }
481
+ }
482
+
483
+ test "a rejection burst past the pending ring's capacity delivers every terminal" {
484
+ var h = try Harness.create();
485
+ defer h.destroy();
486
+ const fx = &h.app_state.effects;
487
+ fx.executor = .fake;
488
+
489
+ // One update issues more refused loads than the shared pending
490
+ // ring holds. Each `.rejected` is that call's ONLY terminal —
491
+ // none may be evicted on the way to the drain.
492
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load_rejected_burst);
493
+ var drains: usize = 0;
494
+ while (h.app_state.model.event_count < rejected_burst_count) : (drains += 1) {
495
+ if (drains > rejected_burst_count) break;
496
+ try h.drainWakes();
497
+ try h.harness.runtime.dispatchPlatformEvent(h.app, .wake);
498
+ }
499
+ try std.testing.expectEqual(rejected_burst_count, h.app_state.model.event_count);
500
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.rejected, h.app_state.model.last_kind.?);
501
+ try std.testing.expect(fx.pendingVideo() == null);
502
+ }
503
+
504
+ // ------------------------------------------------------------ real executor
505
+
506
+ test "real executor drives the platform player and events round-trip" {
507
+ var h = try Harness.create();
508
+ defer h.destroy();
509
+ const np = &h.harness.null_platform;
510
+ try np.setVideoMeta("orchard-flyover.mp4", 92_500, 1280, 720);
511
+
512
+ // Load claims the surface, loads the platform's single player, and
513
+ // autoplay starts it.
514
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load);
515
+ try std.testing.expectEqual(@as(usize, 1), np.video_load_count);
516
+ try std.testing.expectEqual(@as(usize, 1), np.video_play_count);
517
+ try std.testing.expectEqualStrings(clip_path, np.video.path());
518
+ try std.testing.expect(np.video.playing);
519
+
520
+ // The loaded acknowledgment arrives as a platform event, exactly as
521
+ // a live host would deliver it after the load call returned.
522
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.takeVideoLoaded().?);
523
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.loaded, h.app_state.model.last_kind.?);
524
+ try std.testing.expectEqual(@as(u64, 92_500), h.app_state.model.last_duration_ms);
525
+ try std.testing.expectEqual(@as(u64, 1280), h.app_state.model.last_width);
526
+ try std.testing.expectEqual(@as(u64, 720), h.app_state.model.last_height);
527
+
528
+ // Position ticks advance on the test's explicit clock, never on
529
+ // their own.
530
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.advanceVideo(500).?);
531
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.advanceVideo(500).?);
532
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.position, h.app_state.model.last_kind.?);
533
+ try std.testing.expectEqual(@as(u64, 1_000), h.app_state.model.last_position_ms);
534
+
535
+ // The automation snapshot reports playback honestly.
536
+ const playing_snapshot = h.harness.runtime.automationSnapshot("Video").video.?;
537
+ try std.testing.expectEqual(clip_key, playing_snapshot.key);
538
+ try std.testing.expectEqual(clip_surface, playing_snapshot.surface);
539
+ try std.testing.expect(playing_snapshot.playing);
540
+ try std.testing.expectEqual(@as(u64, 1_000), playing_snapshot.position_ms);
541
+ try std.testing.expectEqual(@as(u64, 92_500), playing_snapshot.duration_ms);
542
+ try std.testing.expectEqual(@as(u64, 1280), playing_snapshot.width);
543
+ try std.testing.expectEqual(@as(u64, 720), playing_snapshot.height);
544
+
545
+ // Pause freezes the platform player; ticks stop with it.
546
+ try h.app_state.dispatch(&h.harness.runtime, 1, .pause);
547
+ try std.testing.expectEqual(@as(usize, 1), np.video_pause_count);
548
+ try std.testing.expect(np.advanceVideo(500) == null);
549
+ try h.app_state.dispatch(&h.harness.runtime, 1, .play);
550
+
551
+ // Seek moves the platform position; mute and loop reach the player.
552
+ try h.app_state.dispatch(&h.harness.runtime, 1, .seek_half);
553
+ try std.testing.expectEqual(@as(u64, 45_000), np.video.position_ms);
554
+ try h.app_state.dispatch(&h.harness.runtime, 1, .mute_on);
555
+ try std.testing.expect(np.video.muted);
556
+ try h.app_state.dispatch(&h.harness.runtime, 1, .quiet);
557
+ try std.testing.expectEqual(@as(f32, 0.25), np.video.volume);
558
+
559
+ // Advancing past the end delivers the one completion, and the
560
+ // platform player retires with it (retire-before-emit, the live
561
+ // hosts' teardown order) — not merely paused.
562
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.advanceVideo(60_000).?);
563
+ try std.testing.expectEqual(@as(usize, 1), h.app_state.model.completed_count);
564
+ try std.testing.expectEqual(@as(u64, 92_500), h.app_state.model.last_position_ms);
565
+ try std.testing.expect(!h.app_state.model.last_playing);
566
+ try std.testing.expect(!np.video.loaded);
567
+ try std.testing.expect(np.advanceVideo(500) == null);
568
+
569
+ // Stop unloads; the snapshot goes honestly idle (null, not zeros).
570
+ try h.app_state.dispatch(&h.harness.runtime, 1, .stop);
571
+ try std.testing.expectEqual(@as(usize, 1), np.video_stop_count);
572
+ try std.testing.expect(!np.video.loaded);
573
+ try std.testing.expect(h.harness.runtime.automationSnapshot("Video").video == null);
574
+ }
575
+
576
+ test "a looping playback wraps at the end and never completes" {
577
+ var h = try Harness.create();
578
+ defer h.destroy();
579
+ const np = &h.harness.null_platform;
580
+ try np.setVideoMeta("orchard-flyover.mp4", 10_000, 640, 360);
581
+
582
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load_looping);
583
+ // The load options reached the platform player.
584
+ try std.testing.expect(np.video.looping);
585
+ try std.testing.expect(np.video.muted);
586
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.takeVideoLoaded().?);
587
+
588
+ // Advancing past the end wraps: a position tick, still playing,
589
+ // and no completion — a looping video never ends.
590
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.advanceVideo(10_500).?);
591
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.position, h.app_state.model.last_kind.?);
592
+ try std.testing.expectEqual(@as(u64, 500), h.app_state.model.last_position_ms);
593
+ try std.testing.expect(h.app_state.model.last_playing);
594
+ try std.testing.expectEqual(@as(usize, 0), h.app_state.model.completed_count);
595
+ }
596
+
597
+ test "transport after a non-looping completion refuses like a live host" {
598
+ var h = try Harness.create();
599
+ defer h.destroy();
600
+ const np = &h.harness.null_platform;
601
+ try np.setVideoMeta("orchard-flyover.mp4", 10_000, 640, 360);
602
+
603
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load);
604
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.takeVideoLoaded().?);
605
+
606
+ // The natural end retires the platform player before the completion
607
+ // emits, so every later transport call meets the same absent player
608
+ // a live host has after its teardown.
609
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.advanceVideo(10_500).?);
610
+ try std.testing.expectEqual(@as(usize, 1), h.app_state.model.completed_count);
611
+ try std.testing.expect(!np.video.loaded);
612
+
613
+ // Seek against the retired player: the completion latch refuses
614
+ // it deterministically before the platform is even asked (the
615
+ // retired player could only have refused), so the call never
616
+ // arrives and the CHANNEL mirror stays at the completion's
617
+ // terminal position — a dead seek must not scrub the snapshot and
618
+ // the house slider away from the frame actually on the glass.
619
+ try h.app_state.dispatch(&h.harness.runtime, 1, .seek_half);
620
+ try std.testing.expectEqual(@as(usize, 0), np.video_seek_count);
621
+ try std.testing.expectEqual(@as(u64, 0), np.video.position_ms);
622
+ try std.testing.expectEqual(@as(u64, 10_000), h.app_state.effects.videoSnapshot().position_ms);
623
+ try h.app_state.dispatch(&h.harness.runtime, 1, .pause);
624
+ try std.testing.expect(np.advanceVideo(500) == null);
625
+ try std.testing.expectEqual(@as(usize, 1), h.app_state.model.completed_count);
626
+
627
+ // Play cannot restart a retired player: the platform refuses and
628
+ // the channel degrades to one `.failed` event — the resume path is
629
+ // a fresh load, on every backend.
630
+ try h.app_state.dispatch(&h.harness.runtime, 1, .play);
631
+ try h.drainWakes();
632
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.failed, h.app_state.model.last_kind.?);
633
+ try std.testing.expectEqual(clip_key, h.app_state.model.last_key);
634
+ try std.testing.expect(h.harness.runtime.automationSnapshot("Video").video == null);
635
+ }
636
+
637
+ test "decoded frames reach the claimed surface through the platform sink" {
638
+ var h = try Harness.create();
639
+ defer h.destroy();
640
+ const np = &h.harness.null_platform;
641
+
642
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load);
643
+
644
+ // The platform's decode callback pushes one frame through the sink
645
+ // the load handed over; the compositor's next frame adopts it.
646
+ const teal = solidFrame(.{ 0, 128, 128, 255 });
647
+ try np.pushVideoFrame(2, 2, &teal);
648
+ try dispatchFrame(h.harness, h.app, 2);
649
+ const adopted = h.harness.runtime.adoptedMediaSurfaceTexture(clip_surface).?;
650
+ try std.testing.expectEqual(@as(usize, 2), adopted.width);
651
+ try std.testing.expectEqual(@as(usize, 2), adopted.height);
652
+
653
+ // Stop releases the claim. A decode thread still holding the sink
654
+ // (the platform unloads asynchronously) pushes into inert
655
+ // process-lived memory and hears the released claim — and the
656
+ // runtime purges the adopted frame with the video release: the
657
+ // stopped surface returns to its placeholder (its contain stamp
658
+ // leaves with the snapshot, so a retained frame would composite
659
+ // distorted under "no playback" chrome).
660
+ const stale_sink = np.video.sink;
661
+ try h.app_state.dispatch(&h.harness.runtime, 1, .stop);
662
+ const late = solidFrame(.{ 9, 9, 9, 255 });
663
+ try std.testing.expectError(error.MediaSurfaceReleased, stale_sink.push(2, 2, &late));
664
+ try std.testing.expect(h.harness.runtime.adoptedMediaSurfaceTexture(clip_surface) == null);
665
+ }
666
+
667
+ test "a new load replaces the playback whole: player, claim, and key" {
668
+ var h = try Harness.create();
669
+ defer h.destroy();
670
+ const np = &h.harness.null_platform;
671
+
672
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load);
673
+ const first_sink_push = solidFrame(.{ 1, 2, 3, 255 });
674
+ try np.pushVideoFrame(2, 2, &first_sink_push);
675
+
676
+ // The second load must re-claim the SAME surface — impossible
677
+ // unless the first claim was released — and stop the first
678
+ // platform playback on the way.
679
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load_paused);
680
+ try std.testing.expectEqual(@as(usize, 1), np.video_stop_count);
681
+ try std.testing.expectEqual(@as(usize, 2), np.video_load_count);
682
+ try std.testing.expect(!np.video.playing);
683
+ // The replacement's sink is live; frames flow to the same surface.
684
+ const second_sink_push = solidFrame(.{ 4, 5, 6, 255 });
685
+ try np.pushVideoFrame(2, 2, &second_sink_push);
686
+ // autoplay=false: the load left the player paused; play starts it.
687
+ try h.app_state.dispatch(&h.harness.runtime, 1, .play);
688
+ try std.testing.expect(np.video.playing);
689
+ }
690
+
691
+ test "a surface another producer holds fails the load loudly" {
692
+ var h = try Harness.create();
693
+ defer h.destroy();
694
+
695
+ const holder = try h.harness.runtime.acquireMediaSurfaceProducer(clip_surface);
696
+ defer holder.release();
697
+
698
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load);
699
+ try h.drainWakes();
700
+ try std.testing.expectEqual(@as(usize, 1), h.app_state.model.event_count);
701
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.failed, h.app_state.model.last_kind.?);
702
+ try std.testing.expectEqual(clip_key, h.app_state.model.last_key);
703
+ // The platform player was never asked: the claim failed first.
704
+ try std.testing.expectEqual(@as(usize, 0), h.harness.null_platform.video_load_count);
705
+ }
706
+
707
+ test "a platform without video playback degrades to one failed event" {
708
+ // Model a staged host (Windows/Linux today): the services are
709
+ // absent and the feature reports false, so playback fails loudly
710
+ // through the Msg loop instead of crashing or silently no-opping.
711
+ var h = try Harness.createConfigured(.{ .video_playback = false });
712
+ defer h.destroy();
713
+ try std.testing.expect(!h.harness.runtime.supports(.video_playback));
714
+
715
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load);
716
+ try h.drainWakes();
717
+ try std.testing.expectEqual(@as(usize, 1), h.app_state.model.event_count);
718
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.failed, h.app_state.model.last_kind.?);
719
+ try std.testing.expectEqual(clip_key, h.app_state.model.last_key);
720
+ try std.testing.expect(h.harness.runtime.automationSnapshot("Video").video == null);
721
+ }
722
+
723
+ test "a missing local file falls through to the url and streams" {
724
+ var h = try Harness.createConfigured(.{ .video_local_files = false });
725
+ defer h.destroy();
726
+ const np = &h.harness.null_platform;
727
+ const fx = &h.app_state.effects;
728
+ try np.setVideoMeta("orchard-flyover.mp4", 92_500, 1280, 720);
729
+
730
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load_url);
731
+ // The local path was honestly tried (and missing) before the URL
732
+ // resolved — resolution order is pinned, not assumed.
733
+ try std.testing.expectEqual(@as(usize, 1), np.video_load_count);
734
+ try std.testing.expectEqual(@as(usize, 1), np.video_load_url_count);
735
+ try std.testing.expectEqualStrings(clip_url, np.video.path());
736
+ try std.testing.expectEqual(effects_mod.EffectVideoSource.stream, fx.videoSnapshot().source);
737
+ // A fresh stream has no bytes yet: buffering starts true
738
+ // optimistically, and the loaded acknowledgment clears it.
739
+ try std.testing.expect(fx.videoSnapshot().buffering);
740
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.takeVideoLoaded().?);
741
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.loaded, h.app_state.model.last_kind.?);
742
+ try std.testing.expect(!fx.videoSnapshot().buffering);
743
+
744
+ // A mid-stream stall rides a position tick with buffering=true; the
745
+ // Msg payload and the snapshot both report it, and the next healthy
746
+ // tick clears it.
747
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.stallVideo().?);
748
+ try std.testing.expect(h.app_state.model.last_buffering);
749
+ try std.testing.expect(fx.videoSnapshot().buffering);
750
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.advanceVideo(500).?);
751
+ try std.testing.expect(!h.app_state.model.last_buffering);
752
+ try std.testing.expect(!fx.videoSnapshot().buffering);
753
+ }
754
+
755
+ test "url-only playback skips the local probe entirely" {
756
+ var h = try Harness.create();
757
+ defer h.destroy();
758
+ const np = &h.harness.null_platform;
759
+
760
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load_url_only);
761
+ try std.testing.expectEqual(@as(usize, 0), np.video_load_count);
762
+ try std.testing.expectEqual(@as(usize, 1), np.video_load_url_count);
763
+ try std.testing.expectEqual(effects_mod.EffectVideoSource.stream, h.app_state.effects.videoSnapshot().source);
764
+ }
765
+
766
+ test "a platform straggler after stop is swallowed, never misattributed" {
767
+ var h = try Harness.create();
768
+ defer h.destroy();
769
+ const np = &h.harness.null_platform;
770
+
771
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load);
772
+ const loaded = np.takeVideoLoaded().?;
773
+ try h.app_state.dispatch(&h.harness.runtime, 1, .stop);
774
+ // The loaded event from before the stop arrives late: no Msg, no
775
+ // model change.
776
+ const before = h.app_state.model.event_count;
777
+ try h.harness.runtime.dispatchPlatformEvent(h.app, loaded);
778
+ try std.testing.expectEqual(before, h.app_state.model.event_count);
779
+ }
780
+
781
+ test "a replaced playback's queued terminal never resets the replacement" {
782
+ var h = try Harness.create();
783
+ defer h.destroy();
784
+ const np = &h.harness.null_platform;
785
+ try np.setVideoMeta("orchard-flyover.mp4", 92_500, 1280, 720);
786
+
787
+ // Playback A dies — but its `.failed` event is still queued when
788
+ // the update replaces it with playback B (same source, fresh
789
+ // load): the single player's classic straggler race.
790
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load);
791
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.takeVideoLoaded().?);
792
+ const stale_failed = np.failVideo().?;
793
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load);
794
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.takeVideoLoaded().?);
795
+ const events_before = h.app_state.model.event_count;
796
+
797
+ // A's stale terminal arrives: the load token names A, so the
798
+ // channel swallows it — no Msg, no reset, B keeps its claim and
799
+ // its mirrors.
800
+ try h.harness.runtime.dispatchPlatformEvent(h.app, stale_failed);
801
+ try std.testing.expectEqual(events_before, h.app_state.model.event_count);
802
+ const fx = &h.app_state.effects;
803
+ try std.testing.expect(fx.videoSnapshot().active);
804
+ try std.testing.expect(fx.videoSnapshot().playing);
805
+ try std.testing.expectEqual(@as(u64, 92_500), fx.videoSnapshot().duration_ms);
806
+ }
807
+
808
+ test "a paused fresh stream is paused, not buffering; pause ends a stall's wait" {
809
+ var h = try Harness.create();
810
+ defer h.destroy();
811
+ const np = &h.harness.null_platform;
812
+ const fx = &h.app_state.effects;
813
+
814
+ // A stream loaded with autoplay=false: buffering means an
815
+ // un-paused stream waiting for bytes, and a paused load is not
816
+ // waiting for anything — play() is what starts the wait.
817
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load_url_paused);
818
+ try std.testing.expectEqual(effects_mod.EffectVideoSource.stream, fx.videoSnapshot().source);
819
+ try std.testing.expect(!fx.videoSnapshot().playing);
820
+ try std.testing.expect(!fx.videoSnapshot().buffering);
821
+
822
+ // An autoplaying stream starts its wait immediately: buffering
823
+ // optimistic until the platform's events take over.
824
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load_url_only);
825
+ try std.testing.expect(fx.videoSnapshot().playing);
826
+ try std.testing.expect(fx.videoSnapshot().buffering);
827
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.takeVideoLoaded().?);
828
+
829
+ // Mid-stream stall, then pause: the pause ends the wait by
830
+ // definition, and no conforming host emits a pause acknowledgment
831
+ // to clear the flag - the command mirror does it.
832
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.stallVideo().?);
833
+ try std.testing.expect(fx.videoSnapshot().buffering);
834
+ try h.app_state.dispatch(&h.harness.runtime, 1, .pause);
835
+ try std.testing.expect(!fx.videoSnapshot().playing);
836
+ try std.testing.expect(!fx.videoSnapshot().buffering);
837
+ }
838
+
839
+ test "a looping advance past u64 wraps to the exact residue" {
840
+ var h = try Harness.create();
841
+ defer h.destroy();
842
+ const np = &h.harness.null_platform;
843
+ try np.setVideoMeta("orchard-flyover.mp4", 92_500, 1280, 720);
844
+
845
+ try h.app_state.dispatch(&h.harness.runtime, 1, .load_looping);
846
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.takeVideoLoaded().?);
847
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.advanceVideo(500).?);
848
+
849
+ // A hostile or fuzzing delta past u64 must wrap on the true sum,
850
+ // not on a saturated one - the modulo runs in wider arithmetic.
851
+ const huge = std.math.maxInt(u64);
852
+ const expected: u64 = @intCast((@as(u128, 500) + huge) % 92_500);
853
+ const wrapped = np.advanceVideo(huge).?;
854
+ try std.testing.expectEqual(expected, wrapped.video.position_ms);
855
+ try std.testing.expect(wrapped.video.playing);
856
+ }
857
+
858
+ test "quit while playing: the stop hook silences video and releases the claim through the live platform" {
859
+ // The desktop runner's exit ordering, the audio quit test's twin:
860
+ // the platform dies BEFORE the app's deferred deinit runs, so the
861
+ // stop hook must stop playback and release the surface claim
862
+ // through the still-alive services, and the late deinit must
863
+ // answer inert.
864
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
865
+ errdefer harness.destroy(std.testing.allocator);
866
+ harness.null_platform.gpu_surfaces = true;
867
+ const app_state = try std.testing.allocator.create(VideoApp);
868
+ errdefer std.testing.allocator.destroy(app_state);
869
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
870
+ .name = "effects-video-quit",
871
+ .scene = video_scene,
872
+ .canvas_label = canvas_label,
873
+ .update_fx = videoUpdate,
874
+ .view = videoView,
875
+ });
876
+ errdefer app_state.deinit();
877
+ const app = app_state.app();
878
+ try harness.start(app);
879
+
880
+ try app_state.dispatch(&harness.runtime, 1, .load);
881
+ try std.testing.expect(app_state.effects.videoSnapshot().active);
882
+ try std.testing.expect(harness.null_platform.video.playing);
883
+
884
+ try harness.stop(app);
885
+ try std.testing.expectEqual(@as(usize, 1), harness.null_platform.video_stop_count);
886
+ try std.testing.expect(!harness.null_platform.video.loaded);
887
+ try std.testing.expect(app_state.effects.services == null);
888
+ try std.testing.expect(!app_state.effects.videoSnapshot().active);
889
+ // The claim went with the stop: the surface is claimable again.
890
+ const reclaim = try harness.runtime.acquireMediaSurfaceProducer(clip_surface);
891
+ reclaim.release();
892
+
893
+ harness.destroy(std.testing.allocator);
894
+ app_state.deinit();
895
+ std.testing.allocator.destroy(app_state);
896
+ }
897
+
898
+ // ------------------------------------------- declarative <video> element
899
+
900
+ const canvas = @import("canvas");
901
+
902
+ const DeclModel = struct {
903
+ show: bool = true,
904
+ second: bool = false,
905
+ malformed: bool = false,
906
+ muted: bool = false,
907
+ loop: bool = false,
908
+ };
909
+
910
+ const DeclMsg = union(enum) { toggle_show, use_second, use_malformed, toggle_muted, toggle_loop, hijack_key, noop };
911
+
912
+ const DeclApp = ui_app_model.UiApp(DeclModel, DeclMsg);
913
+ const DeclEffects = DeclApp.Effects;
914
+
915
+ fn declUpdate(model: *DeclModel, msg: DeclMsg, fx: *DeclEffects) void {
916
+ switch (msg) {
917
+ // A manual load deliberately carrying the reconciler's derived
918
+ // key for "assets/clips/one.mp4": the key is a pure function of
919
+ // the source string, so a collision is expressible — ownership
920
+ // must ride the load token.
921
+ .hijack_key => fx.loadVideo(.{
922
+ .key = std.hash.Wyhash.hash(0x76696465, "assets/clips/one.mp4") | 1,
923
+ .surface = canvas.video_playback_surface_id,
924
+ .path = "assets/clips/one.mp4",
925
+ }),
926
+ .toggle_show => model.show = !model.show,
927
+ .use_second => model.second = true,
928
+ .use_malformed => model.malformed = true,
929
+ .toggle_muted => model.muted = !model.muted,
930
+ .toggle_loop => model.loop = !model.loop,
931
+ .noop => {},
932
+ }
933
+ }
934
+
935
+ /// `declView` without the video element: the recording twin for the
936
+ /// zero-record divergence pin below — a session recorded with this
937
+ /// view journals NO effect records at all.
938
+ fn declViewNone(ui: *DeclApp.Ui, model: *const DeclModel) DeclApp.Ui.Node {
939
+ _ = model;
940
+ return ui.column(.{ .padding = 8 }, .{ui.text(.{}, "no video")});
941
+ }
942
+
943
+ /// A view declaring the playback through `ui.video` (what `<video
944
+ /// src=... controls/>` lowers to in both markup engines): presence IS
945
+ /// playback, and the transport chrome is runtime-consumed.
946
+ fn declView(ui: *DeclApp.Ui, model: *const DeclModel) DeclApp.Ui.Node {
947
+ if (!model.show) {
948
+ return ui.column(.{ .padding = 8 }, .{ui.text(.{}, "no video")});
949
+ }
950
+ return ui.column(.{ .padding = 8 }, .{
951
+ ui.video(.{
952
+ // The malformed arm is a URL the video loader's scheme
953
+ // gate refuses (`std.Uri.parse` fails on it).
954
+ .src = if (model.malformed) "https://[" else if (model.second) "assets/clips/two.mp4" else "assets/clips/one.mp4",
955
+ .controls = true,
956
+ .muted = model.muted,
957
+ .loop = model.loop,
958
+ .width = 320,
959
+ .height = 220,
960
+ .label = "Clip",
961
+ }),
962
+ });
963
+ }
964
+
965
+ const DeclHarness = struct {
966
+ harness: *core.TestHarness(),
967
+ app_state: *DeclApp,
968
+ app: core.App,
969
+
970
+ fn create() !DeclHarness {
971
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
972
+ errdefer harness.destroy(std.testing.allocator);
973
+ harness.null_platform.gpu_surfaces = true;
974
+ try harness.null_platform.setVideoMeta("one.mp4", 90_000, 640, 360);
975
+ try harness.null_platform.setVideoMeta("two.mp4", 60_000, 640, 360);
976
+ const app_state = try std.testing.allocator.create(DeclApp);
977
+ errdefer std.testing.allocator.destroy(app_state);
978
+ app_state.* = DeclApp.init(std.heap.page_allocator, .{}, .{
979
+ .name = "effects-video-decl",
980
+ .scene = video_scene,
981
+ .canvas_label = canvas_label,
982
+ .update_fx = declUpdate,
983
+ .view = declView,
984
+ });
985
+ const app = app_state.app();
986
+ try harness.start(app);
987
+ try dispatchFrame(harness, app, 1);
988
+ try std.testing.expect(app_state.installed);
989
+ return .{ .harness = harness, .app_state = app_state, .app = app };
990
+ }
991
+
992
+ fn destroy(self: *DeclHarness) void {
993
+ self.app_state.deinit();
994
+ std.testing.allocator.destroy(self.app_state);
995
+ self.harness.destroy(std.testing.allocator);
996
+ }
997
+
998
+ /// The laid-out frame of the first chrome control carrying `verb`,
999
+ /// straight from the runtime's retained layout.
1000
+ fn controlFrame(self: *DeclHarness, verb: canvas.VideoControlVerb) !geometry.RectF {
1001
+ const layout = try self.harness.runtime.canvasWidgetLayout(1, canvas_label);
1002
+ for (layout.nodes) |node| {
1003
+ if (node.widget.video_control == verb) return node.frame.normalized();
1004
+ }
1005
+ return error.TestUnexpectedResult;
1006
+ }
1007
+
1008
+ fn clickAt(self: *DeclHarness, x: f32, y: f32) !void {
1009
+ try self.harness.runtime.dispatchPlatformEvent(self.app, .{ .gpu_surface_input = .{
1010
+ .window_id = 1,
1011
+ .label = canvas_label,
1012
+ .kind = .pointer_down,
1013
+ .x = x,
1014
+ .y = y,
1015
+ } });
1016
+ try self.harness.runtime.dispatchPlatformEvent(self.app, .{ .gpu_surface_input = .{
1017
+ .window_id = 1,
1018
+ .label = canvas_label,
1019
+ .kind = .pointer_up,
1020
+ .x = x,
1021
+ .y = y,
1022
+ } });
1023
+ }
1024
+ };
1025
+
1026
+ test "a declared <video src> loads on first rebuild, reloads on src change, and stops on removal" {
1027
+ var h = try DeclHarness.create();
1028
+ defer h.destroy();
1029
+ const np = &h.harness.null_platform;
1030
+ const fx = &h.app_state.effects;
1031
+
1032
+ // The installing rebuild reconciled the declaration into the
1033
+ // channel: one load of the declared path onto the framework-owned
1034
+ // playback surface, autoplay honored. The captured declaration is
1035
+ // COPIED out of the build arena (a later failed rebuild resets the
1036
+ // arena; the reconcile can run from a window close before the next
1037
+ // success), so its src must alias the app-owned buffer.
1038
+ try std.testing.expectEqual(
1039
+ @intFromPtr(&h.app_state.video_build_src_buffer),
1040
+ @intFromPtr(h.app_state.video_build_declaration.?.src.ptr),
1041
+ );
1042
+ try std.testing.expectEqual(@as(usize, 1), np.video_load_count);
1043
+ try std.testing.expectEqualStrings("assets/clips/one.mp4", np.video.path());
1044
+ try std.testing.expect(np.video.playing);
1045
+ try std.testing.expect(fx.videoSnapshot().active);
1046
+ try std.testing.expectEqual(canvas.video_playback_surface_id, fx.videoSnapshot().surface);
1047
+
1048
+ // A rebuild with the unchanged declaration never reloads — an
1049
+ // unchanged src must not restart the playback.
1050
+ try h.app_state.dispatch(&h.harness.runtime, 1, .noop);
1051
+ try std.testing.expectEqual(@as(usize, 1), np.video_load_count);
1052
+
1053
+ // Same-src flag deltas apply in place AND republish the runtime
1054
+ // mirror in the same reconcile: an automation snapshot taken right
1055
+ // after the flip must already report the new value.
1056
+ try h.app_state.dispatch(&h.harness.runtime, 1, .toggle_muted);
1057
+ try std.testing.expectEqual(@as(usize, 1), np.video_load_count);
1058
+ try std.testing.expect(np.video.muted);
1059
+ try std.testing.expect(fx.videoSnapshot().muted);
1060
+ try std.testing.expect(h.harness.runtime.video_muted);
1061
+ try h.app_state.dispatch(&h.harness.runtime, 1, .toggle_loop);
1062
+ try std.testing.expect(np.video.looping);
1063
+ try std.testing.expect(h.harness.runtime.video_looping);
1064
+ try h.app_state.dispatch(&h.harness.runtime, 1, .toggle_muted);
1065
+ try std.testing.expect(!h.harness.runtime.video_muted);
1066
+
1067
+ // A changed src replaces the playback whole (loadVideo's replace
1068
+ // semantics: the first playback stops on the way).
1069
+ try h.app_state.dispatch(&h.harness.runtime, 1, .use_second);
1070
+ try std.testing.expectEqual(@as(usize, 2), np.video_load_count);
1071
+ try std.testing.expectEqualStrings("assets/clips/two.mp4", np.video.path());
1072
+ try std.testing.expectEqual(@as(usize, 1), np.video_stop_count);
1073
+
1074
+ // The element leaving the view ends the playback: declarative
1075
+ // ownership stops what it declared.
1076
+ try h.app_state.dispatch(&h.harness.runtime, 1, .toggle_show);
1077
+ try std.testing.expectEqual(@as(usize, 2), np.video_stop_count);
1078
+ try std.testing.expect(!np.video.loaded);
1079
+ try std.testing.expect(!fx.videoSnapshot().active);
1080
+
1081
+ // Redeclaring it loads afresh.
1082
+ try h.app_state.dispatch(&h.harness.runtime, 1, .toggle_show);
1083
+ try std.testing.expectEqual(@as(usize, 3), np.video_load_count);
1084
+ try std.testing.expect(np.video.loaded);
1085
+ }
1086
+
1087
+ /// The retained media-surface widget node for the framework playback
1088
+ /// surface, from the runtime's laid-out tree.
1089
+ fn playbackSurfaceNode(harness: *core.TestHarness()) !canvas.WidgetLayoutNode {
1090
+ const layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
1091
+ for (layout.nodes) |node| {
1092
+ if (node.widget.kind == .media_surface and node.widget.image_id == canvas.video_playback_surface_id) return node;
1093
+ }
1094
+ return error.TestUnexpectedResult;
1095
+ }
1096
+
1097
+ /// Assert the retained display list draws the playback surface as a
1098
+ /// contain fit: the BLACK letterbox field across the whole widget
1099
+ /// frame, the deterministic placeholder confined to the quad, and the
1100
+ /// texture quad at the engine's fitted rect for the reported stream
1101
+ /// dimensions.
1102
+ fn expectPlaybackSurfaceFit(harness: *core.TestHarness(), frame: geometry.RectF, stream_width: f32, stream_height: f32) !void {
1103
+ const expected = canvas.containDestinationRect(frame, stream_width, stream_height);
1104
+ const display_list = try harness.runtime.canvasDisplayList(1, canvas_label);
1105
+ var saw_field = false;
1106
+ var saw_placeholder = false;
1107
+ var saw_quad = false;
1108
+ for (display_list.commands) |command| switch (command) {
1109
+ .fill_rounded_rect => |fill| {
1110
+ if (std.meta.eql(fill.rect, frame)) {
1111
+ try std.testing.expectEqual(canvas.Fill{ .color = canvas.Color.rgba8(0, 0, 0, 255) }, fill.fill);
1112
+ saw_field = true;
1113
+ } else if (std.meta.eql(fill.rect, expected)) {
1114
+ try std.testing.expectEqual(
1115
+ canvas.Fill{ .color = canvas.mediaSurfacePlaceholderColor(canvas.video_playback_surface_id) },
1116
+ fill.fill,
1117
+ );
1118
+ saw_placeholder = true;
1119
+ }
1120
+ },
1121
+ .draw_image => |draw| {
1122
+ if (draw.image_id != canvas.mediaSurfaceTextureImageId(canvas.video_playback_surface_id)) continue;
1123
+ // The engine's quad is the fit; the draw stretches into it.
1124
+ try std.testing.expectEqual(canvas.ImageFit.stretch, draw.fit);
1125
+ try std.testing.expectEqual(expected, draw.dst);
1126
+ saw_quad = true;
1127
+ },
1128
+ else => {},
1129
+ };
1130
+ try std.testing.expect(saw_field);
1131
+ try std.testing.expect(saw_placeholder);
1132
+ try std.testing.expect(saw_quad);
1133
+ }
1134
+
1135
+ test "the video surface letterboxes from the LOADED report and re-fits on source replacement" {
1136
+ // The paint-level pin of the contain-fit pipeline end to end: the
1137
+ // null platform's fake decoder reports stream dimensions at LOADED
1138
+ // (setVideoMeta), the snapshot feeds the rebuild, and the emitted
1139
+ // canvas frame carries the black backdrop plus the fitted quad —
1140
+ // the exact geometry every host composites.
1141
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
1142
+ defer harness.destroy(std.testing.allocator);
1143
+ harness.null_platform.gpu_surfaces = true;
1144
+ // one.mp4 is WIDE (2:1) — into the squarer surface box it must
1145
+ // letterbox (bars top/bottom); two.mp4 is TALL (1:2) — the same box
1146
+ // pillarboxes it (bars left/right).
1147
+ try harness.null_platform.setVideoMeta("one.mp4", 90_000, 480, 240);
1148
+ try harness.null_platform.setVideoMeta("two.mp4", 60_000, 240, 480);
1149
+ const app_state = try std.testing.allocator.create(DeclApp);
1150
+ defer std.testing.allocator.destroy(app_state);
1151
+ app_state.* = DeclApp.init(std.heap.page_allocator, .{}, .{
1152
+ .name = "effects-video-fit",
1153
+ .scene = video_scene,
1154
+ .canvas_label = canvas_label,
1155
+ .update_fx = declUpdate,
1156
+ .view = declView,
1157
+ });
1158
+ defer app_state.deinit();
1159
+ const app = app_state.app();
1160
+ try harness.start(app);
1161
+ try dispatchFrame(harness, app, 1);
1162
+ const np = &harness.null_platform;
1163
+
1164
+ // Pre-LOADED: no reported dimensions — the surface draw must stay
1165
+ // the full-frame placeholder (no guessed fit, no divide-by-zero).
1166
+ const surface_frame = (try playbackSurfaceNode(harness)).frame.normalized();
1167
+ try std.testing.expect(surface_frame.width > 0 and surface_frame.height > 0);
1168
+ {
1169
+ const display_list = try harness.runtime.canvasDisplayList(1, canvas_label);
1170
+ var saw_quad = false;
1171
+ for (display_list.commands) |command| switch (command) {
1172
+ .draw_image => |draw| {
1173
+ if (draw.image_id != canvas.mediaSurfaceTextureImageId(canvas.video_playback_surface_id)) continue;
1174
+ try std.testing.expectEqual(surface_frame, draw.dst.normalized());
1175
+ saw_quad = true;
1176
+ },
1177
+ else => {},
1178
+ };
1179
+ try std.testing.expect(saw_quad);
1180
+ }
1181
+
1182
+ // LOADED lands the 480x240 report: the rebuilt frame letterboxes —
1183
+ // full width, fitted height, black bars above and below.
1184
+ try harness.runtime.dispatchPlatformEvent(app, np.takeVideoLoaded().?);
1185
+ try app_state.dispatch(&harness.runtime, 1, .noop);
1186
+ const wide_frame = (try playbackSurfaceNode(harness)).frame.normalized();
1187
+ try expectPlaybackSurfaceFit(harness, wide_frame, 480, 240);
1188
+ const wide_quad = canvas.containDestinationRect(wide_frame, 480, 240);
1189
+ try std.testing.expectEqual(wide_frame.width, wide_quad.width);
1190
+ try std.testing.expect(wide_quad.height < wide_frame.height - 1);
1191
+
1192
+ // Replacing the source re-fits from the NEW report: the tall clip
1193
+ // pillarboxes the same box — full height, fitted width, bars left
1194
+ // and right.
1195
+ try app_state.dispatch(&harness.runtime, 1, .use_second);
1196
+ try harness.runtime.dispatchPlatformEvent(app, np.takeVideoLoaded().?);
1197
+ try app_state.dispatch(&harness.runtime, 1, .noop);
1198
+ const tall_frame = (try playbackSurfaceNode(harness)).frame.normalized();
1199
+ try expectPlaybackSurfaceFit(harness, tall_frame, 240, 480);
1200
+ const tall_quad = canvas.containDestinationRect(tall_frame, 240, 480);
1201
+ try std.testing.expectEqual(tall_frame.height, tall_quad.height);
1202
+ try std.testing.expect(tall_quad.width < tall_frame.width - 1);
1203
+ }
1204
+
1205
+ test "the reconciler never stops a manual playback sharing its derived key" {
1206
+ var h = try DeclHarness.create();
1207
+ defer h.destroy();
1208
+ const np = &h.harness.null_platform;
1209
+ const fx = &h.app_state.effects;
1210
+
1211
+ // An update replaces the declared playback with its OWN load that
1212
+ // happens to carry the reconciler's derived key. Ownership is the
1213
+ // load token, not the key: removing the declaration must leave the
1214
+ // manual playback running.
1215
+ try h.app_state.dispatch(&h.harness.runtime, 1, .hijack_key);
1216
+ try std.testing.expect(fx.videoSnapshot().active);
1217
+ const stops_before = np.video_stop_count;
1218
+ try h.app_state.dispatch(&h.harness.runtime, 1, .toggle_show);
1219
+ try std.testing.expectEqual(stops_before, np.video_stop_count);
1220
+ try std.testing.expect(fx.videoSnapshot().active);
1221
+ try std.testing.expect(np.video.loaded);
1222
+ }
1223
+
1224
+ test "a refused declaration never takes ownership from the playback still running" {
1225
+ var h = try DeclHarness.create();
1226
+ defer h.destroy();
1227
+ const np = &h.harness.null_platform;
1228
+ const fx = &h.app_state.effects;
1229
+
1230
+ // A is playing from the installing rebuild's declaration.
1231
+ try std.testing.expect(np.video.playing);
1232
+ try std.testing.expectEqualStrings("assets/clips/one.mp4", np.video.path());
1233
+
1234
+ // The declaration changes to a source the loader's own gates
1235
+ // refuse: the reconciler must not commit it — A keeps playing and
1236
+ // remains the tracked owner.
1237
+ try h.app_state.dispatch(&h.harness.runtime, 1, .use_malformed);
1238
+ try std.testing.expect(fx.videoSnapshot().active);
1239
+ try std.testing.expectEqualStrings("assets/clips/one.mp4", np.video.path());
1240
+ try std.testing.expectEqual(@as(usize, 1), np.video_load_count);
1241
+
1242
+ // Removing the element stops the playback the reconciler actually
1243
+ // owns — a committed refused src would hash the wrong key here and
1244
+ // leave A running forever.
1245
+ try h.app_state.dispatch(&h.harness.runtime, 1, .toggle_show);
1246
+ try std.testing.expect(!np.video.loaded);
1247
+ try std.testing.expect(!fx.videoSnapshot().active);
1248
+ }
1249
+
1250
+ test "the house chrome toggle pauses and resumes the platform player without an app Msg" {
1251
+ var h = try DeclHarness.create();
1252
+ defer h.destroy();
1253
+ const np = &h.harness.null_platform;
1254
+ const fx = &h.app_state.effects;
1255
+
1256
+ // The loaded acknowledgment reaches the channel with no app
1257
+ // handler bound: the runtime-owned arm publishes and rebuilds, so
1258
+ // the chrome renders enabled against the live duration.
1259
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.takeVideoLoaded().?);
1260
+ try std.testing.expectEqual(@as(u64, 90_000), fx.videoSnapshot().duration_ms);
1261
+
1262
+ // A release on the play/pause control drives the channel directly.
1263
+ const toggle_frame = try h.controlFrame(.toggle);
1264
+ try h.clickAt(toggle_frame.x + toggle_frame.width / 2, toggle_frame.y + toggle_frame.height / 2);
1265
+ try std.testing.expectEqual(@as(usize, 1), np.video_pause_count);
1266
+ try std.testing.expect(!fx.videoSnapshot().playing);
1267
+
1268
+ // The rebuild that followed re-rendered the chrome from the moved
1269
+ // mirrors: the glyph is back to play.
1270
+ const paused_frame = try h.controlFrame(.toggle);
1271
+ const layout = try h.harness.runtime.canvasWidgetLayout(1, canvas_label);
1272
+ for (layout.nodes) |node| {
1273
+ if (node.widget.video_control == .toggle) {
1274
+ try std.testing.expectEqualStrings("play", node.widget.icon);
1275
+ }
1276
+ }
1277
+
1278
+ // Pressing again resumes (autoplay's start was the first play).
1279
+ try h.clickAt(paused_frame.x + paused_frame.width / 2, paused_frame.y + paused_frame.height / 2);
1280
+ try std.testing.expectEqual(@as(usize, 2), np.video_play_count);
1281
+ try std.testing.expect(fx.videoSnapshot().playing);
1282
+ }
1283
+
1284
+ test "keyboard activation of the house chrome toggle drives the channel like the pointer" {
1285
+ var h = try DeclHarness.create();
1286
+ defer h.destroy();
1287
+ const np = &h.harness.null_platform;
1288
+ const fx = &h.app_state.effects;
1289
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.takeVideoLoaded().?);
1290
+ try std.testing.expect(fx.videoSnapshot().playing);
1291
+
1292
+ // Target the toggle control exactly as the runtime's focus routing
1293
+ // would: the control advertises Play/Pause to focus and
1294
+ // accessibility, so Enter/Space must act, never be consumed
1295
+ // silently.
1296
+ const layout = try h.harness.runtime.canvasWidgetLayout(1, canvas_label);
1297
+ var target: ?canvas.WidgetFocusTarget = null;
1298
+ for (layout.nodes, 0..) |node, index| {
1299
+ if (node.widget.video_control == .toggle) {
1300
+ target = .{
1301
+ .id = node.widget.id,
1302
+ .kind = node.widget.kind,
1303
+ .bounds = node.frame,
1304
+ .index = index,
1305
+ .state = node.widget.state,
1306
+ };
1307
+ }
1308
+ }
1309
+ try h.harness.runtime.dispatchEvent(h.app, .{ .canvas_widget_keyboard = .{
1310
+ .window_id = 1,
1311
+ .view_label = canvas_label,
1312
+ .keyboard = .{ .phase = .key_down, .focused_id = target.?.id, .key = "enter" },
1313
+ .target = target,
1314
+ } });
1315
+ try std.testing.expectEqual(@as(usize, 1), np.video_pause_count);
1316
+ try std.testing.expect(!fx.videoSnapshot().playing);
1317
+
1318
+ // Space resumes, and the chrome re-rendered from the moved mirrors.
1319
+ try h.harness.runtime.dispatchEvent(h.app, .{ .canvas_widget_keyboard = .{
1320
+ .window_id = 1,
1321
+ .view_label = canvas_label,
1322
+ .keyboard = .{ .phase = .key_down, .focused_id = target.?.id, .key = "space" },
1323
+ .target = target,
1324
+ } });
1325
+ try std.testing.expectEqual(@as(usize, 2), np.video_play_count);
1326
+ try std.testing.expect(fx.videoSnapshot().playing);
1327
+ }
1328
+
1329
+ test "keyboard steps on the house seek slider drive the channel" {
1330
+ var h = try DeclHarness.create();
1331
+ defer h.destroy();
1332
+ const np = &h.harness.null_platform;
1333
+ const fx = &h.app_state.effects;
1334
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.takeVideoLoaded().?);
1335
+ try std.testing.expect(fx.videoSnapshot().duration_ms > 0);
1336
+
1337
+ // An arrow step on the focused seek slider is a set_value intent —
1338
+ // no widget change event exists on this path — and must seek the
1339
+ // channel like the pointer scrub, never move the thumb
1340
+ // optimistically for the next tick to snap back.
1341
+ const layout = try h.harness.runtime.canvasWidgetLayout(1, canvas_label);
1342
+ var target: ?canvas.WidgetFocusTarget = null;
1343
+ for (layout.nodes, 0..) |node, index| {
1344
+ if (node.widget.video_control == .scrub) {
1345
+ target = .{
1346
+ .id = node.widget.id,
1347
+ .kind = node.widget.kind,
1348
+ .bounds = node.frame,
1349
+ .index = index,
1350
+ .state = node.widget.state,
1351
+ };
1352
+ }
1353
+ }
1354
+ try h.harness.runtime.dispatchEvent(h.app, .{ .canvas_widget_keyboard = .{
1355
+ .window_id = 1,
1356
+ .view_label = canvas_label,
1357
+ .keyboard = .{ .phase = .key_down, .focused_id = target.?.id, .key = "arrowright" },
1358
+ .target = target,
1359
+ } });
1360
+ try std.testing.expectEqual(@as(usize, 1), np.video_seek_count);
1361
+ try std.testing.expect(np.video.position_ms > 0);
1362
+ try std.testing.expect(fx.videoSnapshot().position_ms > 0);
1363
+ }
1364
+
1365
+ test "a src change renders the replacement's transport state in the same build" {
1366
+ var h = try DeclHarness.create();
1367
+ defer h.destroy();
1368
+ const np = &h.harness.null_platform;
1369
+ const fx = &h.app_state.effects;
1370
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.takeVideoLoaded().?);
1371
+
1372
+ // Pause the declared playback: the chrome shows Play.
1373
+ const toggle = try h.controlFrame(.toggle);
1374
+ try h.clickAt(toggle.center().x, toggle.center().y);
1375
+ try std.testing.expect(!fx.videoSnapshot().playing);
1376
+
1377
+ // The declaration switches to an autoplaying replacement. The
1378
+ // reconcile runs after the build installs, so without a repass the
1379
+ // just-installed chrome would still advertise Play while its
1380
+ // control would PAUSE the new playback — label and action must
1381
+ // agree before any platform event arrives.
1382
+ try h.app_state.dispatch(&h.harness.runtime, 1, .use_second);
1383
+ try std.testing.expect(fx.videoSnapshot().playing);
1384
+ const layout = try h.harness.runtime.canvasWidgetLayout(1, canvas_label);
1385
+ var saw_toggle = false;
1386
+ for (layout.nodes) |node| {
1387
+ if (node.widget.video_control == .toggle) {
1388
+ saw_toggle = true;
1389
+ try std.testing.expectEqualStrings("pause", node.widget.icon);
1390
+ }
1391
+ }
1392
+ try std.testing.expect(saw_toggle);
1393
+ }
1394
+
1395
+ test "the house chrome toggle restarts a finished playback from the start" {
1396
+ var h = try DeclHarness.create();
1397
+ defer h.destroy();
1398
+ const np = &h.harness.null_platform;
1399
+ const fx = &h.app_state.effects;
1400
+
1401
+ // Run the declared playback to its natural end: the player
1402
+ // retires, the completion latches, and the scrub goes dead while
1403
+ // Play stays live.
1404
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.takeVideoLoaded().?);
1405
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.advanceVideo(95_000).?);
1406
+ try std.testing.expect(fx.videoSnapshot().completed);
1407
+ try std.testing.expect(!np.video.loaded);
1408
+ const layout = try h.harness.runtime.canvasWidgetLayout(1, canvas_label);
1409
+ for (layout.nodes) |node| {
1410
+ if (node.widget.video_control == .scrub) try std.testing.expect(node.widget.state.disabled);
1411
+ if (node.widget.video_control == .toggle) try std.testing.expect(!node.widget.state.disabled);
1412
+ }
1413
+
1414
+ // Play on the finished playback: a fresh load of the same source,
1415
+ // not a failed event.
1416
+ const loads_before = np.video_load_count;
1417
+ const toggle = try h.controlFrame(.toggle);
1418
+ try h.clickAt(toggle.center().x, toggle.center().y);
1419
+ try std.testing.expectEqual(loads_before + 1, np.video_load_count);
1420
+ try std.testing.expect(np.video.loaded);
1421
+ try std.testing.expect(np.video.playing);
1422
+ try std.testing.expect(fx.videoSnapshot().active);
1423
+ try std.testing.expect(!fx.videoSnapshot().completed);
1424
+ try std.testing.expect(fx.videoSnapshot().playing);
1425
+
1426
+ // The restart minted a fresh load identity and declarative
1427
+ // ownership re-captured it: flag deltas still apply, and removing
1428
+ // the element still stops the playback the reconciler restarted.
1429
+ try h.app_state.dispatch(&h.harness.runtime, 1, .toggle_muted);
1430
+ try std.testing.expect(np.video.muted);
1431
+ try std.testing.expect(fx.videoSnapshot().muted);
1432
+ try h.app_state.dispatch(&h.harness.runtime, 1, .toggle_show);
1433
+ try std.testing.expect(!fx.videoSnapshot().active);
1434
+ try std.testing.expect(!np.video.loaded);
1435
+ }
1436
+
1437
+ test "the house chrome slider seeks proportionally into the playback" {
1438
+ var h = try DeclHarness.create();
1439
+ defer h.destroy();
1440
+ const np = &h.harness.null_platform;
1441
+
1442
+ // The duration arrives with the loaded acknowledgment; the seek
1443
+ // maps the slider fraction onto it.
1444
+ try h.harness.runtime.dispatchPlatformEvent(h.app, np.takeVideoLoaded().?);
1445
+
1446
+ // A rail click at three quarters seeks to three quarters of the
1447
+ // 90s duration — the proportional mapping, not the raw fraction.
1448
+ const rail = try h.controlFrame(.scrub);
1449
+ try h.harness.runtime.dispatchPlatformEvent(h.app, .{ .gpu_surface_input = .{
1450
+ .window_id = 1,
1451
+ .label = canvas_label,
1452
+ .kind = .pointer_down,
1453
+ .x = rail.x + rail.width * 0.75,
1454
+ .y = rail.y + rail.height / 2,
1455
+ } });
1456
+ try std.testing.expectEqual(@as(usize, 1), np.video_seek_count);
1457
+ try std.testing.expectApproxEqAbs(@as(f64, 67_500), @as(f64, @floatFromInt(np.video.position_ms)), 2_000);
1458
+ try h.harness.runtime.dispatchPlatformEvent(h.app, .{ .gpu_surface_input = .{
1459
+ .window_id = 1,
1460
+ .label = canvas_label,
1461
+ .kind = .pointer_up,
1462
+ .x = rail.x + rail.width * 0.75,
1463
+ .y = rail.y + rail.height / 2,
1464
+ } });
1465
+ }
1466
+
1467
+ // ------------------------------------------------------- record / replay
1468
+
1469
+ const JournalBuffer = struct {
1470
+ bytes: [256 * 1024]u8 = undefined,
1471
+ len: usize = 0,
1472
+
1473
+ fn sink(self: *JournalBuffer) session_record.RecorderSink {
1474
+ return .{ .context = self, .write_fn = write };
1475
+ }
1476
+
1477
+ fn write(context: *anyopaque, bytes: []const u8) anyerror!void {
1478
+ const self: *JournalBuffer = @ptrCast(@alignCast(context));
1479
+ if (self.len + bytes.len > self.bytes.len) return error.NoSpaceLeft;
1480
+ @memcpy(self.bytes[self.len .. self.len + bytes.len], bytes);
1481
+ self.len += bytes.len;
1482
+ }
1483
+
1484
+ fn journalBytes(self: *const JournalBuffer) []const u8 {
1485
+ return self.bytes[0..self.len];
1486
+ }
1487
+ };
1488
+
1489
+ test "a recorded video session replays byte-identical with no producer and no player" {
1490
+ const gpa = std.testing.allocator;
1491
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
1492
+ defer std.heap.page_allocator.destroy(buffer);
1493
+ buffer.len = 0;
1494
+
1495
+ // Record: a real playback on the null platform's player, decoded
1496
+ // frames pushed through the sink between presents, per-frame
1497
+ // fingerprint checkpoints.
1498
+ var recorded_model: VideoModel = undefined;
1499
+ var recorded_fingerprint: u64 = 0;
1500
+ {
1501
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
1502
+ defer std.heap.page_allocator.destroy(recorder);
1503
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
1504
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
1505
+
1506
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
1507
+ defer harness.destroy(gpa);
1508
+ harness.null_platform.gpu_surfaces = true;
1509
+ harness.runtime.options.session_recorder = recorder;
1510
+ try harness.null_platform.setVideoMeta("orchard-flyover.mp4", 92_500, 1280, 720);
1511
+
1512
+ const app_state = try gpa.create(VideoApp);
1513
+ defer gpa.destroy(app_state);
1514
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
1515
+ .name = "effects-video",
1516
+ .scene = video_scene,
1517
+ .canvas_label = canvas_label,
1518
+ .update_fx = videoUpdate,
1519
+ .view = videoView,
1520
+ .on_command = videoCommand,
1521
+ });
1522
+ defer app_state.deinit();
1523
+ const app = app_state.app();
1524
+ try harness.start(app);
1525
+ try dispatchFrame(harness, app, 1);
1526
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
1527
+
1528
+ const np = &harness.null_platform;
1529
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load", .window_id = 1, .view_label = canvas_label } });
1530
+ try harness.runtime.dispatchPlatformEvent(app, np.takeVideoLoaded().?);
1531
+ var frame_index: u64 = 2;
1532
+ var shade: u8 = 20;
1533
+ while (frame_index < 6) : (frame_index += 1) {
1534
+ const frame = solidFrame(.{ shade, 0, shade, 255 });
1535
+ try np.pushVideoFrame(2, 2, &frame);
1536
+ shade +%= 40;
1537
+ try harness.runtime.dispatchPlatformEvent(app, np.advanceVideo(500).?);
1538
+ try dispatchFrame(harness, app, frame_index);
1539
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
1540
+ }
1541
+ try std.testing.expect(app_state.model.event_count > 0);
1542
+ try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(clip_surface) != null);
1543
+
1544
+ recorder.finish();
1545
+ try std.testing.expect(!recorder.failed);
1546
+ recorded_model = app_state.model;
1547
+ recorded_fingerprint = harness.runtime.sessionStateFingerprint();
1548
+ }
1549
+
1550
+ // Replay into a fresh runtime and app with NO producer attached and
1551
+ // NO platform player behind the events (video_playback=false): the
1552
+ // journaled effect records are the whole Msg source, every
1553
+ // checkpoint verifies, and the final fingerprint matches — texture
1554
+ // contents were never part of the session's identity.
1555
+ {
1556
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
1557
+ defer harness.destroy(gpa);
1558
+ harness.null_platform.gpu_surfaces = true;
1559
+ harness.null_platform.video_playback = false;
1560
+ harness.runtime.options.platform = harness.null_platform.platform();
1561
+
1562
+ const app_state = try gpa.create(VideoApp);
1563
+ defer gpa.destroy(app_state);
1564
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
1565
+ .name = "effects-video",
1566
+ .scene = video_scene,
1567
+ .canvas_label = canvas_label,
1568
+ .update_fx = videoUpdate,
1569
+ .view = videoView,
1570
+ .on_command = videoCommand,
1571
+ });
1572
+ defer app_state.deinit();
1573
+
1574
+ const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
1575
+ .verify = true,
1576
+ .require_same_platform = false,
1577
+ });
1578
+ try std.testing.expect(report.ok());
1579
+ try std.testing.expect(report.events_replayed > 0);
1580
+ try std.testing.expect(report.checkpoints_verified > 0);
1581
+ try std.testing.expect(report.effects_fed > 0);
1582
+ try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(clip_surface) == null);
1583
+ try std.testing.expectEqual(@as(usize, 0), harness.null_platform.video_load_count);
1584
+ try std.testing.expectEqualDeep(recorded_model, app_state.model);
1585
+ try std.testing.expectEqual(recorded_fingerprint, harness.runtime.sessionStateFingerprint());
1586
+ }
1587
+ }
1588
+
1589
+ test "a handler-less house-chrome session replays with live mirrors and identical fingerprints" {
1590
+ const gpa = std.testing.allocator;
1591
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
1592
+ defer std.heap.page_allocator.destroy(buffer);
1593
+ buffer.len = 0;
1594
+
1595
+ // Record: the declarative shape — presence loads the playback, no
1596
+ // app Msg handler anywhere, so the only video effect record is the
1597
+ // Msg-less `.video_load` cascade resolution; the journaled
1598
+ // platform `.video` events are the transport's record. The
1599
+ // chrome's time readouts render from the channel mirrors, so they
1600
+ // are part of every frame's fingerprint.
1601
+ var recorded_position: u64 = 0;
1602
+ var recorded_fingerprint: u64 = 0;
1603
+ {
1604
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
1605
+ defer std.heap.page_allocator.destroy(recorder);
1606
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
1607
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video-decl", .window_width = 400, .window_height = 300 });
1608
+
1609
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
1610
+ defer harness.destroy(gpa);
1611
+ harness.null_platform.gpu_surfaces = true;
1612
+ harness.runtime.options.session_recorder = recorder;
1613
+ try harness.null_platform.setVideoMeta("one.mp4", 90_000, 640, 360);
1614
+ try harness.null_platform.setVideoMeta("two.mp4", 60_000, 640, 360);
1615
+
1616
+ const app_state = try gpa.create(DeclApp);
1617
+ defer gpa.destroy(app_state);
1618
+ app_state.* = DeclApp.init(std.heap.page_allocator, .{}, .{
1619
+ .name = "effects-video-decl",
1620
+ .scene = video_scene,
1621
+ .canvas_label = canvas_label,
1622
+ .update_fx = declUpdate,
1623
+ .view = declView,
1624
+ });
1625
+ defer app_state.deinit();
1626
+ const app = app_state.app();
1627
+ try harness.start(app);
1628
+ try dispatchFrame(harness, app, 1);
1629
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
1630
+
1631
+ const np = &harness.null_platform;
1632
+ try harness.runtime.dispatchPlatformEvent(app, np.takeVideoLoaded().?);
1633
+ var frame_index: u64 = 2;
1634
+ while (frame_index < 6) : (frame_index += 1) {
1635
+ try harness.runtime.dispatchPlatformEvent(app, np.advanceVideo(500).?);
1636
+ try dispatchFrame(harness, app, frame_index);
1637
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
1638
+ }
1639
+ try std.testing.expectEqual(@as(u64, 2_000), app_state.effects.videoSnapshot().position_ms);
1640
+
1641
+ recorder.finish();
1642
+ try std.testing.expect(!recorder.failed);
1643
+ recorded_position = app_state.effects.videoSnapshot().position_ms;
1644
+ recorded_fingerprint = harness.runtime.sessionStateFingerprint();
1645
+ }
1646
+
1647
+ // Replay on a decoder-less host: the reconciler regenerates the
1648
+ // load (fake, parked), the fed `.video_load` record steers the
1649
+ // source mirror, and the replayed platform events steer the rest —
1650
+ // the chrome repaints identically, checkpoint by checkpoint, with
1651
+ // no Msg-bearing effect records anywhere.
1652
+ {
1653
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
1654
+ defer harness.destroy(gpa);
1655
+ harness.null_platform.gpu_surfaces = true;
1656
+ harness.null_platform.video_playback = false;
1657
+ harness.runtime.options.platform = harness.null_platform.platform();
1658
+
1659
+ const app_state = try gpa.create(DeclApp);
1660
+ defer gpa.destroy(app_state);
1661
+ app_state.* = DeclApp.init(std.heap.page_allocator, .{}, .{
1662
+ .name = "effects-video-decl",
1663
+ .scene = video_scene,
1664
+ .canvas_label = canvas_label,
1665
+ .update_fx = declUpdate,
1666
+ .view = declView,
1667
+ });
1668
+ defer app_state.deinit();
1669
+
1670
+ const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
1671
+ .verify = true,
1672
+ .require_same_platform = false,
1673
+ });
1674
+ try std.testing.expect(report.ok());
1675
+ try std.testing.expect(report.checkpoints_verified > 0);
1676
+ try std.testing.expectEqual(@as(u64, 1), report.effects_fed);
1677
+ try std.testing.expectEqual(recorded_position, app_state.effects.videoSnapshot().position_ms);
1678
+ try std.testing.expectEqual(recorded_fingerprint, harness.runtime.sessionStateFingerprint());
1679
+ }
1680
+ }
1681
+
1682
+ test "a recorded mid-playback failure replays its Msg and fingerprints identically" {
1683
+ const gpa = std.testing.allocator;
1684
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
1685
+ defer std.heap.page_allocator.destroy(buffer);
1686
+ buffer.len = 0;
1687
+
1688
+ // Record: a real playback that dies mid-stream. The app HAS a
1689
+ // handler bound, so the `.failed` delivery journals an effect
1690
+ // record — and the platform `.failed` event that steered the
1691
+ // mirrors journals right behind it. Replay must deliver that Msg:
1692
+ // the platform event's mirror apply resets the channel first, so
1693
+ // a delivery resolved against the live channel at drain time
1694
+ // would find nothing and silently drop what the recording
1695
+ // dispatched.
1696
+ var recorded_model: VideoModel = undefined;
1697
+ var recorded_fingerprint: u64 = 0;
1698
+ {
1699
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
1700
+ defer std.heap.page_allocator.destroy(recorder);
1701
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
1702
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
1703
+
1704
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
1705
+ defer harness.destroy(gpa);
1706
+ harness.null_platform.gpu_surfaces = true;
1707
+ harness.runtime.options.session_recorder = recorder;
1708
+ try harness.null_platform.setVideoMeta("orchard-flyover.mp4", 92_500, 1280, 720);
1709
+
1710
+ const app_state = try gpa.create(VideoApp);
1711
+ defer gpa.destroy(app_state);
1712
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
1713
+ .name = "effects-video",
1714
+ .scene = video_scene,
1715
+ .canvas_label = canvas_label,
1716
+ .update_fx = videoUpdate,
1717
+ .view = videoView,
1718
+ .on_command = videoCommand,
1719
+ });
1720
+ defer app_state.deinit();
1721
+ const app = app_state.app();
1722
+ try harness.start(app);
1723
+ try dispatchFrame(harness, app, 1);
1724
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
1725
+
1726
+ const np = &harness.null_platform;
1727
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load", .window_id = 1, .view_label = canvas_label } });
1728
+ try harness.runtime.dispatchPlatformEvent(app, np.takeVideoLoaded().?);
1729
+ try harness.runtime.dispatchPlatformEvent(app, np.advanceVideo(500).?);
1730
+ try dispatchFrame(harness, app, 2);
1731
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
1732
+ // The stream dies: one `.failed`, channel gone.
1733
+ try harness.runtime.dispatchPlatformEvent(app, np.failVideo().?);
1734
+ try dispatchFrame(harness, app, 3);
1735
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
1736
+
1737
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.failed, app_state.model.last_kind.?);
1738
+ try std.testing.expect(!app_state.effects.videoSnapshot().active);
1739
+
1740
+ recorder.finish();
1741
+ try std.testing.expect(!recorder.failed);
1742
+ recorded_model = app_state.model;
1743
+ recorded_fingerprint = harness.runtime.sessionStateFingerprint();
1744
+ }
1745
+
1746
+ // Replay offline: the journaled `.failed` effect record must reach
1747
+ // `update` even though the replayed platform event resets the
1748
+ // channel before the drain delivers it.
1749
+ {
1750
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
1751
+ defer harness.destroy(gpa);
1752
+ harness.null_platform.gpu_surfaces = true;
1753
+ harness.null_platform.video_playback = false;
1754
+ harness.runtime.options.platform = harness.null_platform.platform();
1755
+
1756
+ const app_state = try gpa.create(VideoApp);
1757
+ defer gpa.destroy(app_state);
1758
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
1759
+ .name = "effects-video",
1760
+ .scene = video_scene,
1761
+ .canvas_label = canvas_label,
1762
+ .update_fx = videoUpdate,
1763
+ .view = videoView,
1764
+ .on_command = videoCommand,
1765
+ });
1766
+ defer app_state.deinit();
1767
+
1768
+ const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
1769
+ .verify = true,
1770
+ .require_same_platform = false,
1771
+ });
1772
+ try std.testing.expect(report.ok());
1773
+ try std.testing.expect(report.effects_fed > 0);
1774
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.failed, app_state.model.last_kind.?);
1775
+ try std.testing.expectEqualDeep(recorded_model, app_state.model);
1776
+ try std.testing.expectEqual(recorded_fingerprint, harness.runtime.sessionStateFingerprint());
1777
+ }
1778
+ }
1779
+
1780
+ test "a batched load and stop replays the recorded synchronous terminal" {
1781
+ const gpa = std.testing.allocator;
1782
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
1783
+ defer std.heap.page_allocator.destroy(buffer);
1784
+ buffer.len = 0;
1785
+
1786
+ // Record on an assets-absent host: `Cmd.batch([videoLoad, videoStop])`
1787
+ // in one dispatch. The load's synchronous `.failed` stages inside
1788
+ // that dispatch and drains at the next wake — AFTER the stop has
1789
+ // retired the playback it reports on — so its record must route by
1790
+ // the load's identity at replay, where the fake load succeeds and
1791
+ // the stop (not a failure) is what retires the channel.
1792
+ var recorded_model: VideoModel = undefined;
1793
+ var recorded_fingerprint: u64 = 0;
1794
+ {
1795
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
1796
+ defer std.heap.page_allocator.destroy(recorder);
1797
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
1798
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
1799
+
1800
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
1801
+ defer harness.destroy(gpa);
1802
+ harness.null_platform.gpu_surfaces = true;
1803
+ harness.null_platform.video_local_files = false;
1804
+ harness.runtime.options.platform = harness.null_platform.platform();
1805
+ harness.runtime.options.session_recorder = recorder;
1806
+
1807
+ const app_state = try gpa.create(VideoApp);
1808
+ defer gpa.destroy(app_state);
1809
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
1810
+ .name = "effects-video",
1811
+ .scene = video_scene,
1812
+ .canvas_label = canvas_label,
1813
+ .update_fx = videoUpdate,
1814
+ .view = videoView,
1815
+ .on_command = videoCommand,
1816
+ });
1817
+ defer app_state.deinit();
1818
+ const app = app_state.app();
1819
+ try harness.start(app);
1820
+ try dispatchFrame(harness, app, 1);
1821
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
1822
+
1823
+ const np = &harness.null_platform;
1824
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load-then-stop", .window_id = 1, .view_label = canvas_label } });
1825
+ while (np.takeWake()) |_| {}
1826
+ try harness.runtime.dispatchPlatformEvent(app, .wake);
1827
+ try dispatchFrame(harness, app, 2);
1828
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
1829
+
1830
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.failed, app_state.model.last_kind.?);
1831
+ try std.testing.expectEqual(clip_key, app_state.model.last_key);
1832
+ try std.testing.expect(!app_state.effects.videoSnapshot().active);
1833
+
1834
+ recorder.finish();
1835
+ try std.testing.expect(!recorder.failed);
1836
+ recorded_model = app_state.model;
1837
+ recorded_fingerprint = harness.runtime.sessionStateFingerprint();
1838
+ }
1839
+
1840
+ // Replay offline: the fed `.failed` record finds its load already
1841
+ // retired by the replayed stop and still delivers the recorded Msg
1842
+ // with the recorded identity — never `EffectNotFound`, never a
1843
+ // binding to whatever the channel holds at feed time.
1844
+ {
1845
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
1846
+ defer harness.destroy(gpa);
1847
+ harness.null_platform.gpu_surfaces = true;
1848
+ harness.null_platform.video_playback = false;
1849
+ harness.runtime.options.platform = harness.null_platform.platform();
1850
+
1851
+ const app_state = try gpa.create(VideoApp);
1852
+ defer gpa.destroy(app_state);
1853
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
1854
+ .name = "effects-video",
1855
+ .scene = video_scene,
1856
+ .canvas_label = canvas_label,
1857
+ .update_fx = videoUpdate,
1858
+ .view = videoView,
1859
+ .on_command = videoCommand,
1860
+ });
1861
+ defer app_state.deinit();
1862
+
1863
+ const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
1864
+ .verify = true,
1865
+ .require_same_platform = false,
1866
+ });
1867
+ try std.testing.expect(report.ok());
1868
+ try std.testing.expect(report.effects_fed > 0);
1869
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.failed, app_state.model.last_kind.?);
1870
+ try std.testing.expectEqual(clip_key, app_state.model.last_key);
1871
+ try std.testing.expectEqualDeep(recorded_model, app_state.model);
1872
+ try std.testing.expectEqual(recorded_fingerprint, harness.runtime.sessionStateFingerprint());
1873
+ }
1874
+ }
1875
+
1876
+ test "a replaced load's terminal keeps its own identity under replay" {
1877
+ const gpa = std.testing.allocator;
1878
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
1879
+ defer std.heap.page_allocator.destroy(buffer);
1880
+ buffer.len = 0;
1881
+
1882
+ // Record on an assets-absent host: one dispatch loads a local clip
1883
+ // (synchronous `.failed`, staged) and immediately replaces it with
1884
+ // a URL stream UNDER THE SAME APP KEY. The staged terminal belongs
1885
+ // to the first load; at replay the fake first load succeeds and is
1886
+ // replaced in place, so only the journaled load identity — not the
1887
+ // key, which both loads share — can route the record to it. A feed
1888
+ // bound to the channel at feed time would reset the stream the
1889
+ // recording kept playing.
1890
+ var recorded_model: VideoModel = undefined;
1891
+ var recorded_fingerprint: u64 = 0;
1892
+ {
1893
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
1894
+ defer std.heap.page_allocator.destroy(recorder);
1895
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
1896
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
1897
+
1898
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
1899
+ defer harness.destroy(gpa);
1900
+ harness.null_platform.gpu_surfaces = true;
1901
+ harness.null_platform.video_local_files = false;
1902
+ harness.runtime.options.platform = harness.null_platform.platform();
1903
+ harness.runtime.options.session_recorder = recorder;
1904
+ try harness.null_platform.setVideoMeta("orchard-flyover.mp4", 92_500, 1280, 720);
1905
+
1906
+ const app_state = try gpa.create(VideoApp);
1907
+ defer gpa.destroy(app_state);
1908
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
1909
+ .name = "effects-video",
1910
+ .scene = video_scene,
1911
+ .canvas_label = canvas_label,
1912
+ .update_fx = videoUpdate,
1913
+ .view = videoView,
1914
+ .on_command = videoCommand,
1915
+ });
1916
+ defer app_state.deinit();
1917
+ const app = app_state.app();
1918
+ try harness.start(app);
1919
+ try dispatchFrame(harness, app, 1);
1920
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
1921
+
1922
+ const np = &harness.null_platform;
1923
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load-then-stream", .window_id = 1, .view_label = canvas_label } });
1924
+ while (np.takeWake()) |_| {}
1925
+ try harness.runtime.dispatchPlatformEvent(app, .wake);
1926
+ try harness.runtime.dispatchPlatformEvent(app, np.takeVideoLoaded().?);
1927
+ try harness.runtime.dispatchPlatformEvent(app, np.advanceVideo(500).?);
1928
+ try dispatchFrame(harness, app, 2);
1929
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
1930
+
1931
+ // Both deliveries arrived — the first load's failure, then the
1932
+ // stream's acknowledgment and tick — and the stream plays on.
1933
+ try std.testing.expect(app_state.model.event_count >= 3);
1934
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.position, app_state.model.last_kind.?);
1935
+ try std.testing.expect(app_state.effects.videoSnapshot().active);
1936
+ try std.testing.expect(app_state.effects.videoSnapshot().playing);
1937
+
1938
+ recorder.finish();
1939
+ try std.testing.expect(!recorder.failed);
1940
+ recorded_model = app_state.model;
1941
+ recorded_fingerprint = harness.runtime.sessionStateFingerprint();
1942
+ }
1943
+
1944
+ // Replay offline: the first load's `.failed` routes to the retired
1945
+ // first load and the stream's records to the live channel — same
1946
+ // Msg stream, same mirrors, stream still active at the end.
1947
+ {
1948
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
1949
+ defer harness.destroy(gpa);
1950
+ harness.null_platform.gpu_surfaces = true;
1951
+ harness.null_platform.video_playback = false;
1952
+ harness.runtime.options.platform = harness.null_platform.platform();
1953
+
1954
+ const app_state = try gpa.create(VideoApp);
1955
+ defer gpa.destroy(app_state);
1956
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
1957
+ .name = "effects-video",
1958
+ .scene = video_scene,
1959
+ .canvas_label = canvas_label,
1960
+ .update_fx = videoUpdate,
1961
+ .view = videoView,
1962
+ .on_command = videoCommand,
1963
+ });
1964
+ defer app_state.deinit();
1965
+
1966
+ const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
1967
+ .verify = true,
1968
+ .require_same_platform = false,
1969
+ });
1970
+ try std.testing.expect(report.ok());
1971
+ try std.testing.expect(report.effects_fed > 0);
1972
+ try std.testing.expect(app_state.effects.videoSnapshot().active);
1973
+ try std.testing.expectEqualDeep(recorded_model, app_state.model);
1974
+ try std.testing.expectEqual(recorded_fingerprint, harness.runtime.sessionStateFingerprint());
1975
+ }
1976
+ }
1977
+
1978
+ /// Overwrite the `video_width` field of the first journaled `.video`
1979
+ /// record carrying `kind`, in place. Per `journal.encodeEffect` the
1980
+ /// width sits 26 bytes before the pty trailer (width, height, token,
1981
+ /// source, handled), and the pty fields (kind 1, signal 4, drops 4,
1982
+ /// blob hash 16, blob len 8 = 33 bytes) end the payload.
1983
+ fn patchFirstVideoWidth(bytes: []u8, kind: effects_mod.EffectVideoEventKind, width: u64) bool {
1984
+ var pos: usize = journal.preamble_len;
1985
+ while (bytes.len - pos >= 5) {
1986
+ const record_kind = bytes[pos];
1987
+ const len = std.mem.readInt(u32, bytes[pos + 1 ..][0..4], .little);
1988
+ const payload = bytes[pos + 5 .. pos + 5 + len];
1989
+ pos += 5 + len;
1990
+ if (record_kind != @intFromEnum(journal.RecordKind.effect)) continue;
1991
+ const record = journal.decodeEffect(payload) catch continue;
1992
+ if (record.kind != .video or record.video_kind != kind) continue;
1993
+ std.mem.writeInt(u64, payload[payload.len - 59 ..][0..8], width, .little);
1994
+ return true;
1995
+ }
1996
+ return false;
1997
+ }
1998
+
1999
+ test "a failed record claiming dimensions refuses replay as damage" {
2000
+ const gpa = std.testing.allocator;
2001
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
2002
+ defer std.heap.page_allocator.destroy(buffer);
2003
+ buffer.len = 0;
2004
+
2005
+ // Record a mid-stream failure, then hand a width onto its record:
2006
+ // no failure path ever measured a frame, and a synchronously
2007
+ // failed load has no platform event behind it to cross-check, so
2008
+ // the shape gate must refuse the payload outright.
2009
+ {
2010
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
2011
+ defer std.heap.page_allocator.destroy(recorder);
2012
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
2013
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
2014
+
2015
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2016
+ defer harness.destroy(gpa);
2017
+ harness.null_platform.gpu_surfaces = true;
2018
+ harness.runtime.options.session_recorder = recorder;
2019
+ try harness.null_platform.setVideoMeta("orchard-flyover.mp4", 92_500, 1280, 720);
2020
+
2021
+ const app_state = try gpa.create(VideoApp);
2022
+ defer gpa.destroy(app_state);
2023
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
2024
+ .name = "effects-video",
2025
+ .scene = video_scene,
2026
+ .canvas_label = canvas_label,
2027
+ .update_fx = videoUpdate,
2028
+ .view = videoView,
2029
+ .on_command = videoCommand,
2030
+ });
2031
+ defer app_state.deinit();
2032
+ const app = app_state.app();
2033
+ try harness.start(app);
2034
+ try dispatchFrame(harness, app, 1);
2035
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load", .window_id = 1, .view_label = canvas_label } });
2036
+ try harness.runtime.dispatchPlatformEvent(app, harness.null_platform.takeVideoLoaded().?);
2037
+ try harness.runtime.dispatchPlatformEvent(app, harness.null_platform.failVideo().?);
2038
+ try dispatchFrame(harness, app, 2);
2039
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
2040
+ recorder.finish();
2041
+ try std.testing.expect(!recorder.failed);
2042
+ }
2043
+
2044
+ try std.testing.expect(patchFirstVideoWidth(buffer.bytes[0..buffer.len], .failed, 640));
2045
+
2046
+ {
2047
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2048
+ defer harness.destroy(gpa);
2049
+ harness.null_platform.gpu_surfaces = true;
2050
+ harness.null_platform.video_playback = false;
2051
+ harness.runtime.options.platform = harness.null_platform.platform();
2052
+
2053
+ const app_state = try gpa.create(VideoApp);
2054
+ defer gpa.destroy(app_state);
2055
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
2056
+ .name = "effects-video",
2057
+ .scene = video_scene,
2058
+ .canvas_label = canvas_label,
2059
+ .update_fx = videoUpdate,
2060
+ .view = videoView,
2061
+ .on_command = videoCommand,
2062
+ });
2063
+ defer app_state.deinit();
2064
+
2065
+ const result = session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
2066
+ .verify = false,
2067
+ .require_same_platform = false,
2068
+ });
2069
+ try std.testing.expectError(error.ReplayDamagedRecord, result);
2070
+ }
2071
+ }
2072
+
2073
+ /// Overwrite the `video_kind` byte of the first journaled `.video`
2074
+ /// record, in place — `patchFirstVideoLoadKind`'s delivery-record twin.
2075
+ fn patchFirstVideoKind(bytes: []u8, kind: effects_mod.EffectVideoEventKind) bool {
2076
+ var pos: usize = journal.preamble_len;
2077
+ while (bytes.len - pos >= 5) {
2078
+ const record_kind = bytes[pos];
2079
+ const len = std.mem.readInt(u32, bytes[pos + 1 ..][0..4], .little);
2080
+ const payload = bytes[pos + 5 .. pos + 5 + len];
2081
+ pos += 5 + len;
2082
+ if (record_kind != @intFromEnum(journal.RecordKind.effect)) continue;
2083
+ const record = journal.decodeEffect(payload) catch continue;
2084
+ if (record.kind != .video) continue;
2085
+ payload[payload.len - 78] = @intFromEnum(kind);
2086
+ return true;
2087
+ }
2088
+ return false;
2089
+ }
2090
+
2091
+ /// Overwrite the `video_kind` byte of the first journaled `.video_load`
2092
+ /// record, in place. Per `journal.encodeEffect` the video fields are
2093
+ /// the 45 bytes before the 33-byte pty trailer and `video_kind` is the
2094
+ /// first of them. Framing and every other field stay valid: only
2095
+ /// replay's outcome gate can catch the value. Returns whether a record
2096
+ /// was damaged.
2097
+ fn patchFirstVideoLoadKind(bytes: []u8, kind: effects_mod.EffectVideoEventKind) bool {
2098
+ var pos: usize = journal.preamble_len;
2099
+ while (bytes.len - pos >= 5) {
2100
+ const record_kind = bytes[pos];
2101
+ const len = std.mem.readInt(u32, bytes[pos + 1 ..][0..4], .little);
2102
+ const payload = bytes[pos + 5 .. pos + 5 + len];
2103
+ pos += 5 + len;
2104
+ if (record_kind != @intFromEnum(journal.RecordKind.effect)) continue;
2105
+ const record = journal.decodeEffect(payload) catch continue;
2106
+ if (record.kind != .video_load) continue;
2107
+ payload[payload.len - 78] = @intFromEnum(kind);
2108
+ return true;
2109
+ }
2110
+ return false;
2111
+ }
2112
+
2113
+ test "a delivered record with altered scalars refuses replay as divergence" {
2114
+ const gpa = std.testing.allocator;
2115
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
2116
+ defer std.heap.page_allocator.destroy(buffer);
2117
+ buffer.len = 0;
2118
+
2119
+ // Record one delivered acknowledgment, then alter its position
2120
+ // around the intact identity: the recorder journals every delivery
2121
+ // verbatim from the platform event that produced it, so the
2122
+ // pairing must refuse the mismatch instead of handing the app a
2123
+ // Msg the mirrors contradict.
2124
+ {
2125
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
2126
+ defer std.heap.page_allocator.destroy(recorder);
2127
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
2128
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
2129
+
2130
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2131
+ defer harness.destroy(gpa);
2132
+ harness.null_platform.gpu_surfaces = true;
2133
+ harness.runtime.options.session_recorder = recorder;
2134
+ try harness.null_platform.setVideoMeta("orchard-flyover.mp4", 92_500, 1280, 720);
2135
+
2136
+ const app_state = try gpa.create(VideoApp);
2137
+ defer gpa.destroy(app_state);
2138
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
2139
+ .name = "effects-video",
2140
+ .scene = video_scene,
2141
+ .canvas_label = canvas_label,
2142
+ .update_fx = videoUpdate,
2143
+ .view = videoView,
2144
+ .on_command = videoCommand,
2145
+ });
2146
+ defer app_state.deinit();
2147
+ const app = app_state.app();
2148
+ try harness.start(app);
2149
+ try dispatchFrame(harness, app, 1);
2150
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load", .window_id = 1, .view_label = canvas_label } });
2151
+ try harness.runtime.dispatchPlatformEvent(app, harness.null_platform.takeVideoLoaded().?);
2152
+ try harness.runtime.dispatchPlatformEvent(app, harness.null_platform.advanceVideo(500).?);
2153
+ try dispatchFrame(harness, app, 2);
2154
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
2155
+ recorder.finish();
2156
+ try std.testing.expect(!recorder.failed);
2157
+ }
2158
+
2159
+ try std.testing.expect(patchFirstVideoPosition(buffer.bytes[0..buffer.len], 7_777));
2160
+
2161
+ {
2162
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2163
+ defer harness.destroy(gpa);
2164
+ harness.null_platform.gpu_surfaces = true;
2165
+ harness.null_platform.video_playback = false;
2166
+ harness.runtime.options.platform = harness.null_platform.platform();
2167
+
2168
+ const app_state = try gpa.create(VideoApp);
2169
+ defer gpa.destroy(app_state);
2170
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
2171
+ .name = "effects-video",
2172
+ .scene = video_scene,
2173
+ .canvas_label = canvas_label,
2174
+ .update_fx = videoUpdate,
2175
+ .view = videoView,
2176
+ .on_command = videoCommand,
2177
+ });
2178
+ defer app_state.deinit();
2179
+
2180
+ const result = session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
2181
+ .verify = false,
2182
+ .require_same_platform = false,
2183
+ });
2184
+ try std.testing.expectError(error.ReplayEffectDivergence, result);
2185
+ }
2186
+ }
2187
+
2188
+ test "an impossible journaled source shape fails the replayed load" {
2189
+ // A URL-only request can never resolve .local: the consume refuses
2190
+ // and latches the divergence the finish check reports.
2191
+ var fx = VideoEffects.init(std.testing.allocator);
2192
+ defer fx.deinit();
2193
+ fx.armReplay();
2194
+ fx.pushReplayVideoSource(clip_key, 1, .local, false);
2195
+ fx.loadVideo(.{
2196
+ .key = clip_key,
2197
+ .surface = clip_surface,
2198
+ .url = clip_url,
2199
+ .on_event = VideoEffects.videoMsg(.video_event),
2200
+ });
2201
+ try std.testing.expectError(error.ReplayVideoDivergence, fx.finishReplay());
2202
+ }
2203
+
2204
+ test "a delivered record re-stamped as a rejection refuses replay as damage" {
2205
+ const gpa = std.testing.allocator;
2206
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
2207
+ defer std.heap.page_allocator.destroy(buffer);
2208
+ buffer.len = 0;
2209
+
2210
+ // Record one delivered acknowledgment, then re-stamp its record as
2211
+ // .rejected while its minted token stays: the recorder stamps token
2212
+ // 0 exactly on loop-side rejections, so this shape can only be a
2213
+ // hand-edited journal — and skipping it as a regeneration would
2214
+ // silently omit the recorded delivery.
2215
+ {
2216
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
2217
+ defer std.heap.page_allocator.destroy(recorder);
2218
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
2219
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
2220
+
2221
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2222
+ defer harness.destroy(gpa);
2223
+ harness.null_platform.gpu_surfaces = true;
2224
+ harness.runtime.options.session_recorder = recorder;
2225
+ try harness.null_platform.setVideoMeta("orchard-flyover.mp4", 92_500, 1280, 720);
2226
+
2227
+ const app_state = try gpa.create(VideoApp);
2228
+ defer gpa.destroy(app_state);
2229
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
2230
+ .name = "effects-video",
2231
+ .scene = video_scene,
2232
+ .canvas_label = canvas_label,
2233
+ .update_fx = videoUpdate,
2234
+ .view = videoView,
2235
+ .on_command = videoCommand,
2236
+ });
2237
+ defer app_state.deinit();
2238
+ const app = app_state.app();
2239
+ try harness.start(app);
2240
+ try dispatchFrame(harness, app, 1);
2241
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load", .window_id = 1, .view_label = canvas_label } });
2242
+ try harness.runtime.dispatchPlatformEvent(app, harness.null_platform.takeVideoLoaded().?);
2243
+ try dispatchFrame(harness, app, 2);
2244
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
2245
+ recorder.finish();
2246
+ try std.testing.expect(!recorder.failed);
2247
+ }
2248
+
2249
+ try std.testing.expect(patchFirstVideoKind(buffer.bytes[0..buffer.len], .rejected));
2250
+
2251
+ {
2252
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2253
+ defer harness.destroy(gpa);
2254
+ harness.null_platform.gpu_surfaces = true;
2255
+ harness.null_platform.video_playback = false;
2256
+ harness.runtime.options.platform = harness.null_platform.platform();
2257
+
2258
+ const app_state = try gpa.create(VideoApp);
2259
+ defer gpa.destroy(app_state);
2260
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
2261
+ .name = "effects-video",
2262
+ .scene = video_scene,
2263
+ .canvas_label = canvas_label,
2264
+ .update_fx = videoUpdate,
2265
+ .view = videoView,
2266
+ .on_command = videoCommand,
2267
+ });
2268
+ defer app_state.deinit();
2269
+
2270
+ const result = session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
2271
+ .verify = false,
2272
+ .require_same_platform = false,
2273
+ });
2274
+ try std.testing.expectError(error.ReplayDamagedRecord, result);
2275
+ }
2276
+ }
2277
+
2278
+ test "a video load record claiming a non-outcome kind refuses replay as damage" {
2279
+ const gpa = std.testing.allocator;
2280
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
2281
+ defer std.heap.page_allocator.destroy(buffer);
2282
+ buffer.len = 0;
2283
+
2284
+ // Record one resolved load, then hand-damage its `.video_load`
2285
+ // record's outcome: the recorder writes .loaded or .failed and
2286
+ // nothing else, so a .position there is damage, never a shape to
2287
+ // interpret.
2288
+ {
2289
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
2290
+ defer std.heap.page_allocator.destroy(recorder);
2291
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
2292
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
2293
+
2294
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2295
+ defer harness.destroy(gpa);
2296
+ harness.null_platform.gpu_surfaces = true;
2297
+ harness.runtime.options.session_recorder = recorder;
2298
+ try harness.null_platform.setVideoMeta("orchard-flyover.mp4", 92_500, 1280, 720);
2299
+
2300
+ const app_state = try gpa.create(VideoApp);
2301
+ defer gpa.destroy(app_state);
2302
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
2303
+ .name = "effects-video",
2304
+ .scene = video_scene,
2305
+ .canvas_label = canvas_label,
2306
+ .update_fx = videoUpdate,
2307
+ .view = videoView,
2308
+ .on_command = videoCommand,
2309
+ });
2310
+ defer app_state.deinit();
2311
+ const app = app_state.app();
2312
+ try harness.start(app);
2313
+ try dispatchFrame(harness, app, 1);
2314
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load", .window_id = 1, .view_label = canvas_label } });
2315
+ try harness.runtime.dispatchPlatformEvent(app, harness.null_platform.takeVideoLoaded().?);
2316
+ try dispatchFrame(harness, app, 2);
2317
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
2318
+ recorder.finish();
2319
+ try std.testing.expect(!recorder.failed);
2320
+ }
2321
+
2322
+ try std.testing.expect(patchFirstVideoLoadKind(buffer.bytes[0..buffer.len], .position));
2323
+
2324
+ {
2325
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2326
+ defer harness.destroy(gpa);
2327
+ harness.null_platform.gpu_surfaces = true;
2328
+ harness.null_platform.video_playback = false;
2329
+ harness.runtime.options.platform = harness.null_platform.platform();
2330
+
2331
+ const app_state = try gpa.create(VideoApp);
2332
+ defer gpa.destroy(app_state);
2333
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
2334
+ .name = "effects-video",
2335
+ .scene = video_scene,
2336
+ .canvas_label = canvas_label,
2337
+ .update_fx = videoUpdate,
2338
+ .view = videoView,
2339
+ .on_command = videoCommand,
2340
+ });
2341
+ defer app_state.deinit();
2342
+
2343
+ const result = session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
2344
+ .verify = false,
2345
+ .require_same_platform = false,
2346
+ });
2347
+ try std.testing.expectError(error.ReplayDamagedRecord, result);
2348
+ }
2349
+ }
2350
+
2351
+ /// Overwrite the `video_position_ms` field of the first journaled
2352
+ /// video effect record, in place. Per `journal.encodeEffect` the video
2353
+ /// fields are the 45 bytes before the 33-byte pty trailer — video_kind
2354
+ /// (1), position (8), duration (8), playing (1), buffering (1), width
2355
+ /// (8), height (8), token (8), source (1), handled (1) — so the
2356
+ /// position lives 77 bytes from the end.
2357
+ /// Framing and every other field stay valid: only replay's damage gate
2358
+ /// can catch the value. Returns whether a record was damaged.
2359
+ fn patchFirstVideoPosition(bytes: []u8, position: u64) bool {
2360
+ var pos: usize = journal.preamble_len;
2361
+ while (bytes.len - pos >= 5) {
2362
+ const kind = bytes[pos];
2363
+ const len = std.mem.readInt(u32, bytes[pos + 1 ..][0..4], .little);
2364
+ const payload = bytes[pos + 5 .. pos + 5 + len];
2365
+ pos += 5 + len;
2366
+ if (kind != @intFromEnum(journal.RecordKind.effect)) continue;
2367
+ const record = journal.decodeEffect(payload) catch continue;
2368
+ if (record.kind != .video) continue;
2369
+ std.mem.writeInt(u64, payload[payload.len - 77 ..][0..8], position, .little);
2370
+ return true;
2371
+ }
2372
+ return false;
2373
+ }
2374
+
2375
+ test "a video record claiming a past-2^53 scalar refuses replay as damage" {
2376
+ const gpa = std.testing.allocator;
2377
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
2378
+ defer std.heap.page_allocator.destroy(buffer);
2379
+ buffer.len = 0;
2380
+
2381
+ // Record a real playback session (the byte-identical test's shape,
2382
+ // shortened), then hand-damage the first video record's position
2383
+ // to a value no recorder ever writes.
2384
+ {
2385
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
2386
+ defer std.heap.page_allocator.destroy(recorder);
2387
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
2388
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
2389
+
2390
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2391
+ defer harness.destroy(gpa);
2392
+ harness.null_platform.gpu_surfaces = true;
2393
+ harness.runtime.options.session_recorder = recorder;
2394
+ try harness.null_platform.setVideoMeta("orchard-flyover.mp4", 92_500, 1280, 720);
2395
+
2396
+ const app_state = try gpa.create(VideoApp);
2397
+ defer gpa.destroy(app_state);
2398
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
2399
+ .name = "effects-video",
2400
+ .scene = video_scene,
2401
+ .canvas_label = canvas_label,
2402
+ .update_fx = videoUpdate,
2403
+ .view = videoView,
2404
+ .on_command = videoCommand,
2405
+ });
2406
+ defer app_state.deinit();
2407
+ const app = app_state.app();
2408
+ try harness.start(app);
2409
+ try dispatchFrame(harness, app, 1);
2410
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
2411
+
2412
+ const np = &harness.null_platform;
2413
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load", .window_id = 1, .view_label = canvas_label } });
2414
+ try harness.runtime.dispatchPlatformEvent(app, np.takeVideoLoaded().?);
2415
+ try harness.runtime.dispatchPlatformEvent(app, np.advanceVideo(500).?);
2416
+ try dispatchFrame(harness, app, 2);
2417
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
2418
+ recorder.finish();
2419
+ try std.testing.expect(!recorder.failed);
2420
+ }
2421
+
2422
+ try std.testing.expect(patchFirstVideoPosition(buffer.bytes[0..buffer.len], @as(u64, 1) << 53));
2423
+
2424
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2425
+ defer harness.destroy(gpa);
2426
+ harness.null_platform.gpu_surfaces = true;
2427
+ harness.null_platform.video_playback = false;
2428
+ harness.runtime.options.platform = harness.null_platform.platform();
2429
+ const app_state = try gpa.create(VideoApp);
2430
+ defer gpa.destroy(app_state);
2431
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
2432
+ .name = "effects-video",
2433
+ .scene = video_scene,
2434
+ .canvas_label = canvas_label,
2435
+ .update_fx = videoUpdate,
2436
+ .view = videoView,
2437
+ .on_command = videoCommand,
2438
+ });
2439
+ defer app_state.deinit();
2440
+
2441
+ const result = session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
2442
+ .verify = false,
2443
+ .require_same_platform = false,
2444
+ });
2445
+ try std.testing.expectError(error.ReplayDamagedRecord, result);
2446
+ }
2447
+
2448
+ // --------------------------------------- multi-window declarations
2449
+
2450
+ const PromoModel = struct { show_a: bool = true };
2451
+
2452
+ const PromoMsg = union(enum) { noop };
2453
+
2454
+ const PromoApp = ui_app_model.UiApp(PromoModel, PromoMsg);
2455
+
2456
+ fn promoUpdate(model: *PromoModel, msg: PromoMsg) void {
2457
+ _ = model;
2458
+ switch (msg) {
2459
+ .noop => {},
2460
+ }
2461
+ }
2462
+
2463
+ fn promoView(ui: *PromoApp.Ui, model: *const PromoModel) PromoApp.Ui.Node {
2464
+ _ = model;
2465
+ return ui.column(.{ .gap = 4, .padding = 8 }, .{
2466
+ ui.text(.{}, "main"),
2467
+ });
2468
+ }
2469
+
2470
+ fn promoWindows(model: *const PromoModel, scratch: *PromoApp.WindowsScratch) []const PromoApp.WindowDescriptor {
2471
+ var count: usize = 0;
2472
+ if (model.show_a) {
2473
+ scratch.windows[count] = .{
2474
+ .label = "win-a",
2475
+ .canvas_label = "win-a-canvas",
2476
+ .title = "A",
2477
+ .width = 320,
2478
+ .height = 240,
2479
+ };
2480
+ count += 1;
2481
+ }
2482
+ scratch.windows[count] = .{
2483
+ .label = "win-b",
2484
+ .canvas_label = "win-b-canvas",
2485
+ .title = "B",
2486
+ .width = 320,
2487
+ .height = 240,
2488
+ };
2489
+ count += 1;
2490
+ return scratch.windows[0..count];
2491
+ }
2492
+
2493
+ fn promoWindowView(ui: *PromoApp.Ui, model: *const PromoModel, window_label: []const u8) PromoApp.Ui.Node {
2494
+ _ = model;
2495
+ const src = if (std.mem.eql(u8, window_label, "win-a")) "assets/clips/a.mp4" else "assets/clips/b.mp4";
2496
+ return ui.column(.{ .gap = 4, .padding = 8 }, .{
2497
+ // win-a carries the house chrome so the slot repass has
2498
+ // transport controls to keep honest.
2499
+ ui.video(.{ .src = src, .controls = std.mem.eql(u8, window_label, "win-a"), .width = 128, .height = 72 }),
2500
+ });
2501
+ }
2502
+
2503
+ test "a closed window's declared video promotes the next window's declaration at once" {
2504
+ const gpa = std.testing.allocator;
2505
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2506
+ defer harness.destroy(gpa);
2507
+ harness.null_platform.gpu_surfaces = true;
2508
+ const np = &harness.null_platform;
2509
+ try np.setVideoMeta("a.mp4", 30_000, 640, 360);
2510
+ try np.setVideoMeta("b.mp4", 45_000, 640, 360);
2511
+
2512
+ const app_state = try gpa.create(PromoApp);
2513
+ defer gpa.destroy(app_state);
2514
+ app_state.* = PromoApp.init(std.heap.page_allocator, .{}, .{
2515
+ .name = "effects-video-promo",
2516
+ .scene = video_scene,
2517
+ .canvas_label = canvas_label,
2518
+ .update = promoUpdate,
2519
+ .view = promoView,
2520
+ .windows_fn = promoWindows,
2521
+ .window_view = promoWindowView,
2522
+ });
2523
+ defer app_state.deinit();
2524
+ const app = app_state.app();
2525
+ try harness.start(app);
2526
+ try dispatchFrame(harness, app, 1);
2527
+
2528
+ // Both windows exist; install their canvases so the slot builds
2529
+ // run and record their declarations.
2530
+ var window_a: u64 = 0;
2531
+ var window_b: u64 = 0;
2532
+ {
2533
+ var buffer: [platform.max_windows]platform.WindowInfo = undefined;
2534
+ for (harness.runtime.listWindows(&buffer)) |info| {
2535
+ if (std.mem.eql(u8, info.label, "win-a")) window_a = info.id;
2536
+ if (std.mem.eql(u8, info.label, "win-b")) window_b = info.id;
2537
+ }
2538
+ }
2539
+ try std.testing.expect(window_a != 0 and window_b != 0);
2540
+ inline for (.{ .{ "win-a-canvas", 0 }, .{ "win-b-canvas", 1 } }) |entry| {
2541
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
2542
+ .window_id = if (entry[1] == 0) window_a else window_b,
2543
+ .label = entry[0],
2544
+ .size = geometry.SizeF.init(320, 240),
2545
+ .scale_factor = 1,
2546
+ .frame_index = 1,
2547
+ .timestamp_ns = 2_000_000,
2548
+ .nonblank = true,
2549
+ } });
2550
+ }
2551
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "noop", .window_id = 1, .view_label = canvas_label } });
2552
+
2553
+ // The first declaring window owns the single player.
2554
+ try std.testing.expect(std.mem.endsWith(u8, np.video.path(), "a.mp4"));
2555
+
2556
+ // The slot's FIRST installed build already shows the activated
2557
+ // transport: the reconcile runs after the slot tree installs, and
2558
+ // without the repass its chrome would advertise the pre-activation
2559
+ // snapshot — a disabled Play over a playing video — until some
2560
+ // later platform event.
2561
+ {
2562
+ const slot_layout = try harness.runtime.canvasWidgetLayout(window_a, "win-a-canvas");
2563
+ var saw_toggle = false;
2564
+ for (slot_layout.nodes) |node| {
2565
+ if (node.widget.video_control == .toggle) {
2566
+ saw_toggle = true;
2567
+ try std.testing.expect(!node.widget.state.disabled);
2568
+ try std.testing.expectEqualStrings("pause", node.widget.icon);
2569
+ }
2570
+ }
2571
+ try std.testing.expect(saw_toggle);
2572
+ }
2573
+
2574
+ // The user closes window A: no on_close Msg, no rebuild — the
2575
+ // retained declaration from window B must promote on the spot,
2576
+ // not wait for an unrelated rebuild.
2577
+ const close_event = np.userCloseWindow(window_a).?;
2578
+ try harness.runtime.dispatchPlatformEvent(app, close_event);
2579
+ try std.testing.expect(std.mem.endsWith(u8, np.video.path(), "b.mp4"));
2580
+ try std.testing.expect(np.video.playing);
2581
+ }
2582
+
2583
+ test "a completion handler's chained load replays in recorded order" {
2584
+ const gpa = std.testing.allocator;
2585
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
2586
+ defer std.heap.page_allocator.destroy(buffer);
2587
+ buffer.len = 0;
2588
+
2589
+ // Record the playlist shape: clip A completes, its handler loads
2590
+ // clip B from inside the completion dispatch, and B's `.loaded`
2591
+ // acknowledgment is the very next platform event — no frame
2592
+ // between. Replay must run the completion Msg DURING the replayed
2593
+ // completed event, or B's loaded event would be swallowed against
2594
+ // A's token and the whole tail would diverge.
2595
+ var recorded_model: VideoModel = undefined;
2596
+ var recorded_fingerprint: u64 = 0;
2597
+ {
2598
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
2599
+ defer std.heap.page_allocator.destroy(recorder);
2600
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
2601
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
2602
+
2603
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2604
+ defer harness.destroy(gpa);
2605
+ harness.null_platform.gpu_surfaces = true;
2606
+ harness.runtime.options.session_recorder = recorder;
2607
+ try harness.null_platform.setVideoMeta("orchard-flyover.mp4", 3_000, 1280, 720);
2608
+
2609
+ const app_state = try gpa.create(VideoApp);
2610
+ defer gpa.destroy(app_state);
2611
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
2612
+ .name = "effects-video",
2613
+ .scene = video_scene,
2614
+ .canvas_label = canvas_label,
2615
+ .update_fx = videoUpdate,
2616
+ .view = videoView,
2617
+ .on_command = videoCommand,
2618
+ });
2619
+ defer app_state.deinit();
2620
+ const app = app_state.app();
2621
+ try harness.start(app);
2622
+ try dispatchFrame(harness, app, 1);
2623
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
2624
+
2625
+ const np = &harness.null_platform;
2626
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.arm-chain", .window_id = 1, .view_label = canvas_label } });
2627
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load", .window_id = 1, .view_label = canvas_label } });
2628
+ try harness.runtime.dispatchPlatformEvent(app, np.takeVideoLoaded().?);
2629
+ // Run A to its natural end: the completion Msg loads B inside
2630
+ // its own dispatch, and B's loaded ack replays as the very
2631
+ // next event.
2632
+ try harness.runtime.dispatchPlatformEvent(app, np.advanceVideo(3_000).?);
2633
+ try harness.runtime.dispatchPlatformEvent(app, np.takeVideoLoaded().?);
2634
+ try dispatchFrame(harness, app, 2);
2635
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
2636
+
2637
+ try std.testing.expectEqual(@as(usize, 1), app_state.model.completed_count);
2638
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.loaded, app_state.model.last_kind.?);
2639
+ try std.testing.expectEqual(clip_key + 1, app_state.model.last_key);
2640
+
2641
+ recorder.finish();
2642
+ try std.testing.expect(!recorder.failed);
2643
+ recorded_model = app_state.model;
2644
+ recorded_fingerprint = harness.runtime.sessionStateFingerprint();
2645
+ }
2646
+
2647
+ {
2648
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2649
+ defer harness.destroy(gpa);
2650
+ harness.null_platform.gpu_surfaces = true;
2651
+ harness.null_platform.video_playback = false;
2652
+ harness.runtime.options.platform = harness.null_platform.platform();
2653
+
2654
+ const app_state = try gpa.create(VideoApp);
2655
+ defer gpa.destroy(app_state);
2656
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
2657
+ .name = "effects-video",
2658
+ .scene = video_scene,
2659
+ .canvas_label = canvas_label,
2660
+ .update_fx = videoUpdate,
2661
+ .view = videoView,
2662
+ .on_command = videoCommand,
2663
+ });
2664
+ defer app_state.deinit();
2665
+
2666
+ const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
2667
+ .verify = true,
2668
+ .require_same_platform = false,
2669
+ });
2670
+ try std.testing.expect(report.ok());
2671
+ try std.testing.expect(report.effects_fed > 0);
2672
+ try std.testing.expectEqual(@as(usize, 1), app_state.model.completed_count);
2673
+ try std.testing.expectEqual(clip_key + 1, app_state.model.last_key);
2674
+ try std.testing.expectEqualDeep(recorded_model, app_state.model);
2675
+ try std.testing.expectEqual(recorded_fingerprint, harness.runtime.sessionStateFingerprint());
2676
+ // Retired identities release as the replay advances — at most
2677
+ // the final clip's park (whose closing pass the session's end
2678
+ // cut off) may remain; a playlist never accumulates one entry
2679
+ // per clip (the drain-boundary sweep's contract, pinned
2680
+ // directly in "retired video identities release once their
2681
+ // delivery window closes").
2682
+ try std.testing.expect(app_state.effects.retired_video_len <= 1);
2683
+ }
2684
+ }
2685
+
2686
+ test "a recorded handled event without its journaled result fails the finish check" {
2687
+ // The recorder journals every handled delivery immediately before
2688
+ // the event that dispatched it — a recorded event arriving with a
2689
+ // handler bound and NO fed record before it means the journal was
2690
+ // truncated or hand-edited, and the Msg the recording dispatched
2691
+ // would silently vanish.
2692
+ var fx = VideoEffects.init(std.testing.allocator);
2693
+ defer fx.deinit();
2694
+ fx.armReplay();
2695
+ fx.pushReplayVideoSource(clip_key, 1, .local, false);
2696
+ fx.loadVideo(.{
2697
+ .key = clip_key,
2698
+ .surface = clip_surface,
2699
+ .path = clip_path,
2700
+ .on_event = VideoEffects.videoMsg(.video_event),
2701
+ });
2702
+ const msg = fx.takeVideoMsg(.{
2703
+ .kind = .loaded,
2704
+ .token = 1,
2705
+ .position_ms = 0,
2706
+ .duration_ms = 92_500,
2707
+ .playing = true,
2708
+ .width = 1280,
2709
+ .height = 720,
2710
+ });
2711
+ try std.testing.expect(msg == null);
2712
+ try std.testing.expectError(error.ReplayVideoDivergence, fx.finishReplay());
2713
+ }
2714
+
2715
+ test "an undelivered fed video result fails the finish check" {
2716
+ // A fed record is one recorded delivery, and the event that
2717
+ // consumed it live follows it in every honest journal — so a fed
2718
+ // entry still staged at the journal's end is truncation or
2719
+ // hand-editing, never success.
2720
+ var fx = VideoEffects.init(std.testing.allocator);
2721
+ defer fx.deinit();
2722
+ fx.armReplay();
2723
+ fx.pushReplayVideoSource(clip_key, 1, .local, false);
2724
+ fx.loadVideo(.{
2725
+ .key = clip_key,
2726
+ .surface = clip_surface,
2727
+ .path = clip_path,
2728
+ .on_event = VideoEffects.videoMsg(.video_event),
2729
+ });
2730
+ try fx.feedVideoRecord(clip_key, 1, true, .position, 500, 92_500, true, false, 0, 0);
2731
+ try std.testing.expectError(error.ReplayVideoDivergence, fx.finishReplay());
2732
+
2733
+ // Delivered, the finish is silent again.
2734
+ var boundary = fx.drainBoundary();
2735
+ const msg = fx.takeMsgWithin(&boundary) orelse return error.TestExpectedMsg;
2736
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.position, msg.video_event.kind);
2737
+ try fx.finishReplay();
2738
+ }
2739
+
2740
+ test "retired video identities release at the next drain-pass boundary" {
2741
+ // Bare channel, replay-armed: replaced and stopped loads park their
2742
+ // identities; a park a journaled record references is consumed by
2743
+ // the feed itself, and a never-referenced leftover releases at the
2744
+ // first pass boundary after parking (its record, had one existed,
2745
+ // would have fed before that pass's event dispatched) — the sweep
2746
+ // that keeps a long replayed playlist from accumulating an entry
2747
+ // per clip.
2748
+ var fx = VideoEffects.init(std.testing.allocator);
2749
+ defer fx.deinit();
2750
+ fx.armReplay();
2751
+
2752
+ fx.pushReplayVideoSource(clip_key, 1, .local, false);
2753
+ fx.loadVideo(.{
2754
+ .key = clip_key,
2755
+ .surface = clip_surface,
2756
+ .path = clip_path,
2757
+ .on_event = VideoEffects.videoMsg(.video_event),
2758
+ });
2759
+ fx.pushReplayVideoSource(clip_key + 1, 2, .local, false);
2760
+ fx.loadVideo(.{
2761
+ .key = clip_key + 1,
2762
+ .surface = clip_surface,
2763
+ .path = clip_path,
2764
+ .on_event = VideoEffects.videoMsg(.video_event),
2765
+ });
2766
+ try std.testing.expectEqual(@as(usize, 1), fx.retired_video_len);
2767
+
2768
+ // A journaled record for the replaced load consumes the park (the
2769
+ // deterministic token sequence: the first load minted token 1).
2770
+ try fx.feedVideoRecord(clip_key, 1, true, .failed, 0, 0, false, false, 0, 0);
2771
+ try std.testing.expectEqual(@as(usize, 0), fx.retired_video_len);
2772
+
2773
+ // A stopped load with no record behind it releases at the next
2774
+ // boundary instead of lingering for the session.
2775
+ fx.stopVideo();
2776
+ try std.testing.expectEqual(@as(usize, 1), fx.retired_video_len);
2777
+ _ = fx.drainBoundary();
2778
+ try std.testing.expectEqual(@as(usize, 0), fx.retired_video_len);
2779
+ }
2780
+
2781
+ test "the cascade's resolved source replays without the recording host's files" {
2782
+ const gpa = std.testing.allocator;
2783
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
2784
+ defer std.heap.page_allocator.destroy(buffer);
2785
+ buffer.len = 0;
2786
+
2787
+ // Record on an assets-absent host: a load carrying both a local
2788
+ // path and a url falls through to the url and streams — a
2789
+ // resolution only the recording host's filesystem could make. The
2790
+ // journaled `.video_load` record carries it, so replay's fake load
2791
+ // (which cannot probe for the file) still mirrors `.stream` with
2792
+ // the optimistic buffering flag instead of reporting the requested
2793
+ // shape as `.local`.
2794
+ var recorded_model: VideoModel = undefined;
2795
+ var recorded_source: effects_mod.EffectVideoSource = .local;
2796
+ var recorded_fingerprint: u64 = 0;
2797
+ {
2798
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
2799
+ defer std.heap.page_allocator.destroy(recorder);
2800
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
2801
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
2802
+
2803
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2804
+ defer harness.destroy(gpa);
2805
+ harness.null_platform.gpu_surfaces = true;
2806
+ harness.null_platform.video_local_files = false;
2807
+ harness.runtime.options.platform = harness.null_platform.platform();
2808
+ harness.runtime.options.session_recorder = recorder;
2809
+ try harness.null_platform.setVideoMeta("orchard-flyover.mp4", 92_500, 1280, 720);
2810
+
2811
+ const app_state = try gpa.create(VideoApp);
2812
+ defer gpa.destroy(app_state);
2813
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
2814
+ .name = "effects-video",
2815
+ .scene = video_scene,
2816
+ .canvas_label = canvas_label,
2817
+ .update_fx = videoUpdate,
2818
+ .view = videoView,
2819
+ .on_command = videoCommand,
2820
+ });
2821
+ defer app_state.deinit();
2822
+ const app = app_state.app();
2823
+ try harness.start(app);
2824
+ try dispatchFrame(harness, app, 1);
2825
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load-url", .window_id = 1, .view_label = canvas_label } });
2826
+ try harness.runtime.dispatchPlatformEvent(app, harness.null_platform.takeVideoLoaded().?);
2827
+ try dispatchFrame(harness, app, 2);
2828
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
2829
+
2830
+ try std.testing.expectEqual(@as(usize, 1), harness.null_platform.video_load_url_count);
2831
+ try std.testing.expectEqual(effects_mod.EffectVideoSource.stream, app_state.effects.videoSnapshot().source);
2832
+
2833
+ recorder.finish();
2834
+ try std.testing.expect(!recorder.failed);
2835
+ recorded_model = app_state.model;
2836
+ recorded_source = app_state.effects.videoSnapshot().source;
2837
+ recorded_fingerprint = harness.runtime.sessionStateFingerprint();
2838
+ }
2839
+
2840
+ // Replay offline: same source mirror, same everything.
2841
+ {
2842
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2843
+ defer harness.destroy(gpa);
2844
+ harness.null_platform.gpu_surfaces = true;
2845
+ harness.null_platform.video_playback = false;
2846
+ harness.runtime.options.platform = harness.null_platform.platform();
2847
+
2848
+ const app_state = try gpa.create(VideoApp);
2849
+ defer gpa.destroy(app_state);
2850
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
2851
+ .name = "effects-video",
2852
+ .scene = video_scene,
2853
+ .canvas_label = canvas_label,
2854
+ .update_fx = videoUpdate,
2855
+ .view = videoView,
2856
+ .on_command = videoCommand,
2857
+ });
2858
+ defer app_state.deinit();
2859
+
2860
+ const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
2861
+ .verify = true,
2862
+ .require_same_platform = false,
2863
+ });
2864
+ try std.testing.expect(report.ok());
2865
+ try std.testing.expect(report.effects_fed > 0);
2866
+ try std.testing.expectEqual(recorded_source, app_state.effects.videoSnapshot().source);
2867
+ try std.testing.expectEqual(effects_mod.EffectVideoSource.stream, app_state.effects.videoSnapshot().source);
2868
+ try std.testing.expectEqualDeep(recorded_model, app_state.model);
2869
+ try std.testing.expectEqual(recorded_fingerprint, harness.runtime.sessionStateFingerprint());
2870
+ }
2871
+ }
2872
+
2873
+ test "replayed video records must name the load at their position" {
2874
+ // The reminted token sequence pairs records with replayed loads by
2875
+ // POSITION; the journaled key is the cross-check that the load at
2876
+ // that position is the load the recording issued. A mismatch — a
2877
+ // hand-edited journal or a timeline that diverged before the feed
2878
+ // — refuses instead of delivering through the wrong playback.
2879
+ var fx = VideoEffects.init(std.testing.allocator);
2880
+ defer fx.deinit();
2881
+ fx.armReplay();
2882
+
2883
+ fx.pushReplayVideoSource(clip_key, 1, .local, false);
2884
+ fx.loadVideo(.{
2885
+ .key = clip_key,
2886
+ .surface = clip_surface,
2887
+ .path = clip_path,
2888
+ .on_event = VideoEffects.videoMsg(.video_event),
2889
+ });
2890
+ fx.pushReplayVideoSource(clip_key + 1, 2, .local, false);
2891
+ fx.loadVideo(.{
2892
+ .key = clip_key + 1,
2893
+ .surface = clip_surface,
2894
+ .path = clip_path,
2895
+ .on_event = VideoEffects.videoMsg(.video_event),
2896
+ });
2897
+
2898
+ // Wrong key against the retired first load (token 1).
2899
+ try std.testing.expectError(error.EffectNotFound, fx.feedVideoRecord(clip_key + 5, 1, true, .failed, 0, 0, false, false, 0, 0));
2900
+ // Wrong key against the live load (token 2).
2901
+ try std.testing.expectError(error.EffectNotFound, fx.feedVideoRecord(clip_key + 5, 2, true, .position, 0, 0, true, false, 0, 0));
2902
+ // The honest pairing still delivers.
2903
+ try fx.feedVideoRecord(clip_key + 1, 2, true, .position, 500, 92_500, true, false, 0, 0);
2904
+ }
2905
+
2906
+ test "unclaimed or misclaimed cascade resolutions fail the finish check" {
2907
+ // A structurally valid journal whose `.video_load` records the
2908
+ // replayed timeline never consumed — or consumed under a different
2909
+ // key — is divergence, not success (`finishReplay`, run by the
2910
+ // replayer at the journal's end).
2911
+ {
2912
+ var fx = VideoEffects.init(std.testing.allocator);
2913
+ defer fx.deinit();
2914
+ fx.armReplay();
2915
+ fx.pushReplayVideoSource(clip_key, 999, .stream, false);
2916
+ try std.testing.expectError(error.ReplayVideoDivergence, fx.finishReplay());
2917
+ }
2918
+ {
2919
+ var fx = VideoEffects.init(std.testing.allocator);
2920
+ defer fx.deinit();
2921
+ fx.armReplay();
2922
+ // The record names key clip_key+7 at position (token) 1, but
2923
+ // the replayed dispatch loads clip_key: the consume latches the
2924
+ // divergence the void-returning loadVideo cannot raise.
2925
+ fx.pushReplayVideoSource(clip_key + 7, 1, .stream, false);
2926
+ fx.loadVideo(.{
2927
+ .key = clip_key,
2928
+ .surface = clip_surface,
2929
+ .path = clip_path,
2930
+ .on_event = VideoEffects.videoMsg(.video_event),
2931
+ });
2932
+ try std.testing.expectError(error.ReplayVideoDivergence, fx.finishReplay());
2933
+ }
2934
+ {
2935
+ // The clean shape: consumed by the load it named, finish is
2936
+ // silent.
2937
+ var fx = VideoEffects.init(std.testing.allocator);
2938
+ defer fx.deinit();
2939
+ fx.armReplay();
2940
+ fx.pushReplayVideoSource(clip_key, 1, .stream, false);
2941
+ fx.loadVideo(.{
2942
+ .key = clip_key,
2943
+ .surface = clip_surface,
2944
+ .path = clip_path,
2945
+ .url = clip_url,
2946
+ .on_event = VideoEffects.videoMsg(.video_event),
2947
+ });
2948
+ try std.testing.expectEqual(effects_mod.EffectVideoSource.stream, fx.videoSnapshot().source);
2949
+ try fx.finishReplay();
2950
+
2951
+ // An EXTRA load the recording never issued — no record at its
2952
+ // position — is divergence too: every non-rejected recorded
2953
+ // load journals one, so absence proves the timelines differ.
2954
+ fx.loadVideo(.{
2955
+ .key = clip_key + 1,
2956
+ .surface = clip_surface,
2957
+ .url = clip_url,
2958
+ .on_event = VideoEffects.videoMsg(.video_event),
2959
+ });
2960
+ try std.testing.expectError(error.ReplayVideoDivergence, fx.finishReplay());
2961
+ }
2962
+ }
2963
+
2964
+ test "a load burst past the resolution queue's inline capacity replays whole" {
2965
+ const gpa = std.testing.allocator;
2966
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
2967
+ defer std.heap.page_allocator.destroy(buffer);
2968
+ buffer.len = 0;
2969
+
2970
+ // Record on an assets-absent host: one dispatch resolves more
2971
+ // loads than the replay-side cascade-resolution queue holds
2972
+ // inline (each falls through to the url). All the records land
2973
+ // before the dispatch's event, so replay queues every one of them
2974
+ // first — the queue must spill, never refuse.
2975
+ var recorded_model: VideoModel = undefined;
2976
+ var recorded_fingerprint: u64 = 0;
2977
+ {
2978
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
2979
+ defer std.heap.page_allocator.destroy(recorder);
2980
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
2981
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
2982
+
2983
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
2984
+ defer harness.destroy(gpa);
2985
+ harness.null_platform.gpu_surfaces = true;
2986
+ harness.null_platform.video_local_files = false;
2987
+ harness.runtime.options.platform = harness.null_platform.platform();
2988
+ harness.runtime.options.session_recorder = recorder;
2989
+
2990
+ const app_state = try gpa.create(VideoApp);
2991
+ defer gpa.destroy(app_state);
2992
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
2993
+ .name = "effects-video",
2994
+ .scene = video_scene,
2995
+ .canvas_label = canvas_label,
2996
+ .update_fx = videoUpdate,
2997
+ .view = videoView,
2998
+ .on_command = videoCommand,
2999
+ });
3000
+ defer app_state.deinit();
3001
+ const app = app_state.app();
3002
+ try harness.start(app);
3003
+ try dispatchFrame(harness, app, 1);
3004
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load-source-burst", .window_id = 1, .view_label = canvas_label } });
3005
+ try dispatchFrame(harness, app, 2);
3006
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
3007
+
3008
+ try std.testing.expectEqual(effects_mod.max_effect_replay_video_source_entries + 1, harness.null_platform.video_load_url_count);
3009
+ try std.testing.expectEqual(effects_mod.EffectVideoSource.stream, app_state.effects.videoSnapshot().source);
3010
+
3011
+ recorder.finish();
3012
+ try std.testing.expect(!recorder.failed);
3013
+ recorded_model = app_state.model;
3014
+ recorded_fingerprint = harness.runtime.sessionStateFingerprint();
3015
+ }
3016
+
3017
+ // Replay offline: no false divergence at the inline capacity, and
3018
+ // the surviving load's resolved source lands.
3019
+ {
3020
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
3021
+ defer harness.destroy(gpa);
3022
+ harness.null_platform.gpu_surfaces = true;
3023
+ harness.null_platform.video_playback = false;
3024
+ harness.runtime.options.platform = harness.null_platform.platform();
3025
+
3026
+ const app_state = try gpa.create(VideoApp);
3027
+ defer gpa.destroy(app_state);
3028
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
3029
+ .name = "effects-video",
3030
+ .scene = video_scene,
3031
+ .canvas_label = canvas_label,
3032
+ .update_fx = videoUpdate,
3033
+ .view = videoView,
3034
+ .on_command = videoCommand,
3035
+ });
3036
+ defer app_state.deinit();
3037
+
3038
+ const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
3039
+ .verify = true,
3040
+ .require_same_platform = false,
3041
+ });
3042
+ try std.testing.expect(report.ok());
3043
+ try std.testing.expectEqual(effects_mod.EffectVideoSource.stream, app_state.effects.videoSnapshot().source);
3044
+ try std.testing.expectEqualDeep(recorded_model, app_state.model);
3045
+ try std.testing.expectEqual(recorded_fingerprint, harness.runtime.sessionStateFingerprint());
3046
+ }
3047
+ }
3048
+
3049
+ test "a zero-record recording still fails replay when the timeline loads video" {
3050
+ const gpa = std.testing.allocator;
3051
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
3052
+ defer std.heap.page_allocator.destroy(buffer);
3053
+ buffer.len = 0;
3054
+
3055
+ // Record a session that journals NO effect records at all: no
3056
+ // video element anywhere, just frames.
3057
+ {
3058
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
3059
+ defer std.heap.page_allocator.destroy(recorder);
3060
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
3061
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video-decl", .window_width = 400, .window_height = 300 });
3062
+
3063
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
3064
+ defer harness.destroy(gpa);
3065
+ harness.null_platform.gpu_surfaces = true;
3066
+ harness.runtime.options.session_recorder = recorder;
3067
+
3068
+ const app_state = try gpa.create(DeclApp);
3069
+ defer gpa.destroy(app_state);
3070
+ app_state.* = DeclApp.init(std.heap.page_allocator, .{}, .{
3071
+ .name = "effects-video-decl",
3072
+ .scene = video_scene,
3073
+ .canvas_label = canvas_label,
3074
+ .update_fx = declUpdate,
3075
+ .view = declViewNone,
3076
+ });
3077
+ defer app_state.deinit();
3078
+ const app = app_state.app();
3079
+ try harness.start(app);
3080
+ try dispatchFrame(harness, app, 1);
3081
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
3082
+ recorder.finish();
3083
+ try std.testing.expect(!recorder.failed);
3084
+ }
3085
+
3086
+ // Replay into a build whose view DOES declare a video: the
3087
+ // reconciler issues a load the recording never journaled. Zero
3088
+ // records were fed, so only the unconditional end-of-journal
3089
+ // consistency check can catch the divergence — and it must.
3090
+ {
3091
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
3092
+ defer harness.destroy(gpa);
3093
+ harness.null_platform.gpu_surfaces = true;
3094
+ harness.null_platform.video_playback = false;
3095
+ harness.runtime.options.platform = harness.null_platform.platform();
3096
+
3097
+ const app_state = try gpa.create(DeclApp);
3098
+ defer gpa.destroy(app_state);
3099
+ app_state.* = DeclApp.init(std.heap.page_allocator, .{}, .{
3100
+ .name = "effects-video-decl",
3101
+ .scene = video_scene,
3102
+ .canvas_label = canvas_label,
3103
+ .update_fx = declUpdate,
3104
+ .view = declView,
3105
+ });
3106
+ defer app_state.deinit();
3107
+
3108
+ const result = session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
3109
+ .verify = false,
3110
+ .require_same_platform = false,
3111
+ });
3112
+ try std.testing.expectError(error.ReplayEffectDivergence, result);
3113
+ }
3114
+ }
3115
+
3116
+ test "a post-completion seek keeps the terminal position under replay" {
3117
+ const gpa = std.testing.allocator;
3118
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
3119
+ defer std.heap.page_allocator.destroy(buffer);
3120
+ buffer.len = 0;
3121
+
3122
+ // Record: the playback completes naturally (the host retires the
3123
+ // player, retire-before-emit), then a later dispatch scrubs. The
3124
+ // retired player refuses the seek, so the mirrors keep the
3125
+ // terminal position.
3126
+ var recorded_model: VideoModel = undefined;
3127
+ var recorded_fingerprint: u64 = 0;
3128
+ {
3129
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
3130
+ defer std.heap.page_allocator.destroy(recorder);
3131
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
3132
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
3133
+
3134
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
3135
+ defer harness.destroy(gpa);
3136
+ harness.null_platform.gpu_surfaces = true;
3137
+ harness.runtime.options.session_recorder = recorder;
3138
+ try harness.null_platform.setVideoMeta("orchard-flyover.mp4", 92_500, 1280, 720);
3139
+
3140
+ const app_state = try gpa.create(VideoApp);
3141
+ defer gpa.destroy(app_state);
3142
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
3143
+ .name = "effects-video",
3144
+ .scene = video_scene,
3145
+ .canvas_label = canvas_label,
3146
+ .update_fx = videoUpdate,
3147
+ .view = videoView,
3148
+ .on_command = videoCommand,
3149
+ });
3150
+ defer app_state.deinit();
3151
+ const app = app_state.app();
3152
+ try harness.start(app);
3153
+ try dispatchFrame(harness, app, 1);
3154
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
3155
+
3156
+ const np = &harness.null_platform;
3157
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load", .window_id = 1, .view_label = canvas_label } });
3158
+ try harness.runtime.dispatchPlatformEvent(app, np.takeVideoLoaded().?);
3159
+ try harness.runtime.dispatchPlatformEvent(app, np.advanceVideo(93_000).?);
3160
+ try std.testing.expectEqual(@as(usize, 1), app_state.model.completed_count);
3161
+ // The scrub after the natural end: refused by the retired
3162
+ // player, position pinned at the duration.
3163
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.seek-half", .window_id = 1, .view_label = canvas_label } });
3164
+ try std.testing.expectEqual(@as(u64, 92_500), app_state.effects.videoSnapshot().position_ms);
3165
+ try dispatchFrame(harness, app, 2);
3166
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
3167
+
3168
+ recorder.finish();
3169
+ try std.testing.expect(!recorder.failed);
3170
+ recorded_model = app_state.model;
3171
+ recorded_fingerprint = harness.runtime.sessionStateFingerprint();
3172
+ }
3173
+
3174
+ // Replay offline: the fake channel refuses the same seek — the
3175
+ // completion latched the retire — so the mirrors and the chrome
3176
+ // land exactly where the recording left them.
3177
+ {
3178
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
3179
+ defer harness.destroy(gpa);
3180
+ harness.null_platform.gpu_surfaces = true;
3181
+ harness.null_platform.video_playback = false;
3182
+ harness.runtime.options.platform = harness.null_platform.platform();
3183
+
3184
+ const app_state = try gpa.create(VideoApp);
3185
+ defer gpa.destroy(app_state);
3186
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
3187
+ .name = "effects-video",
3188
+ .scene = video_scene,
3189
+ .canvas_label = canvas_label,
3190
+ .update_fx = videoUpdate,
3191
+ .view = videoView,
3192
+ .on_command = videoCommand,
3193
+ });
3194
+ defer app_state.deinit();
3195
+
3196
+ const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
3197
+ .verify = true,
3198
+ .require_same_platform = false,
3199
+ });
3200
+ try std.testing.expect(report.ok());
3201
+ try std.testing.expectEqual(@as(u64, 92_500), app_state.effects.videoSnapshot().position_ms);
3202
+ try std.testing.expectEqualDeep(recorded_model, app_state.model);
3203
+ try std.testing.expectEqual(recorded_fingerprint, harness.runtime.sessionStateFingerprint());
3204
+ }
3205
+ }
3206
+
3207
+ test "a regenerated rejection never reorders a paired delivery under replay" {
3208
+ const gpa = std.testing.allocator;
3209
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
3210
+ defer std.heap.page_allocator.destroy(buffer);
3211
+ buffer.len = 0;
3212
+
3213
+ // Record: playback A is live and handled; a dispatch stages a
3214
+ // loop-side rejection (an invalid load, which leaves A untouched);
3215
+ // A's next position event arrives BEFORE the wake that drains the
3216
+ // rejection. Live order: loaded, position (synchronous at its
3217
+ // event), rejected (at the wake).
3218
+ var recorded_model: VideoModel = undefined;
3219
+ var recorded_fingerprint: u64 = 0;
3220
+ {
3221
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
3222
+ defer std.heap.page_allocator.destroy(recorder);
3223
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
3224
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
3225
+
3226
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
3227
+ defer harness.destroy(gpa);
3228
+ harness.null_platform.gpu_surfaces = true;
3229
+ harness.runtime.options.session_recorder = recorder;
3230
+ try harness.null_platform.setVideoMeta("orchard-flyover.mp4", 92_500, 1280, 720);
3231
+
3232
+ const app_state = try gpa.create(VideoApp);
3233
+ defer gpa.destroy(app_state);
3234
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
3235
+ .name = "effects-video",
3236
+ .scene = video_scene,
3237
+ .canvas_label = canvas_label,
3238
+ .update_fx = videoUpdate,
3239
+ .view = videoView,
3240
+ .on_command = videoCommand,
3241
+ });
3242
+ defer app_state.deinit();
3243
+ const app = app_state.app();
3244
+ try harness.start(app);
3245
+ try dispatchFrame(harness, app, 1);
3246
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
3247
+
3248
+ const np = &harness.null_platform;
3249
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load", .window_id = 1, .view_label = canvas_label } });
3250
+ try harness.runtime.dispatchPlatformEvent(app, np.takeVideoLoaded().?);
3251
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load-zero-surface", .window_id = 1, .view_label = canvas_label } });
3252
+ try harness.runtime.dispatchPlatformEvent(app, np.advanceVideo(500).?);
3253
+ // The position delivered synchronously at its own event, ahead
3254
+ // of the staged rejection still awaiting its wake.
3255
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.position, app_state.model.last_kind.?);
3256
+ while (np.takeWake()) |_| {}
3257
+ try harness.runtime.dispatchPlatformEvent(app, .wake);
3258
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.rejected, app_state.model.last_kind.?);
3259
+ try dispatchFrame(harness, app, 2);
3260
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
3261
+
3262
+ recorder.finish();
3263
+ try std.testing.expect(!recorder.failed);
3264
+ recorded_model = app_state.model;
3265
+ recorded_fingerprint = harness.runtime.sessionStateFingerprint();
3266
+ }
3267
+
3268
+ // Replay offline: the regenerated rejection stages ahead of the
3269
+ // fed position record, but the position must still pair with its
3270
+ // platform event and dispatch synchronously — same Msg order, same
3271
+ // model, same fingerprints.
3272
+ {
3273
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
3274
+ defer harness.destroy(gpa);
3275
+ harness.null_platform.gpu_surfaces = true;
3276
+ harness.null_platform.video_playback = false;
3277
+ harness.runtime.options.platform = harness.null_platform.platform();
3278
+
3279
+ const app_state = try gpa.create(VideoApp);
3280
+ defer gpa.destroy(app_state);
3281
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
3282
+ .name = "effects-video",
3283
+ .scene = video_scene,
3284
+ .canvas_label = canvas_label,
3285
+ .update_fx = videoUpdate,
3286
+ .view = videoView,
3287
+ .on_command = videoCommand,
3288
+ });
3289
+ defer app_state.deinit();
3290
+
3291
+ const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
3292
+ .verify = true,
3293
+ .require_same_platform = false,
3294
+ });
3295
+ try std.testing.expect(report.ok());
3296
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.rejected, app_state.model.last_kind.?);
3297
+ try std.testing.expectEqualDeep(recorded_model, app_state.model);
3298
+ try std.testing.expectEqual(recorded_fingerprint, harness.runtime.sessionStateFingerprint());
3299
+ }
3300
+ }
3301
+
3302
+ test "a synchronous load failure resets the snapshot identically under replay" {
3303
+ const gpa = std.testing.allocator;
3304
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
3305
+ defer std.heap.page_allocator.destroy(buffer);
3306
+ buffer.len = 0;
3307
+
3308
+ // Record on an assets-absent host: the update loads and probes the
3309
+ // snapshot in the same dispatch. The refusal reset the channel
3310
+ // before loadVideo returned, so the probe reads inactive.
3311
+ var recorded_model: VideoModel = undefined;
3312
+ var recorded_fingerprint: u64 = 0;
3313
+ {
3314
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
3315
+ defer std.heap.page_allocator.destroy(recorder);
3316
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
3317
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
3318
+
3319
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
3320
+ defer harness.destroy(gpa);
3321
+ harness.null_platform.gpu_surfaces = true;
3322
+ harness.null_platform.video_local_files = false;
3323
+ harness.runtime.options.platform = harness.null_platform.platform();
3324
+ harness.runtime.options.session_recorder = recorder;
3325
+
3326
+ const app_state = try gpa.create(VideoApp);
3327
+ defer gpa.destroy(app_state);
3328
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
3329
+ .name = "effects-video",
3330
+ .scene = video_scene,
3331
+ .canvas_label = canvas_label,
3332
+ .update_fx = videoUpdate,
3333
+ .view = videoView,
3334
+ .on_command = videoCommand,
3335
+ });
3336
+ defer app_state.deinit();
3337
+ const app = app_state.app();
3338
+ try harness.start(app);
3339
+ try dispatchFrame(harness, app, 1);
3340
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load-then-probe", .window_id = 1, .view_label = canvas_label } });
3341
+ try std.testing.expectEqual(@as(?bool, false), app_state.model.probed_active);
3342
+ while (harness.null_platform.takeWake()) |_| {}
3343
+ try harness.runtime.dispatchPlatformEvent(app, .wake);
3344
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.failed, app_state.model.last_kind.?);
3345
+ try dispatchFrame(harness, app, 2);
3346
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
3347
+
3348
+ recorder.finish();
3349
+ try std.testing.expect(!recorder.failed);
3350
+ recorded_model = app_state.model;
3351
+ recorded_fingerprint = harness.runtime.sessionStateFingerprint();
3352
+ }
3353
+
3354
+ // Replay offline: the journaled load resolution carries the
3355
+ // refusal, so the fake load resets at the same instant and the
3356
+ // replayed probe reads the same inactive snapshot.
3357
+ {
3358
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
3359
+ defer harness.destroy(gpa);
3360
+ harness.null_platform.gpu_surfaces = true;
3361
+ harness.null_platform.video_playback = false;
3362
+ harness.runtime.options.platform = harness.null_platform.platform();
3363
+
3364
+ const app_state = try gpa.create(VideoApp);
3365
+ defer gpa.destroy(app_state);
3366
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
3367
+ .name = "effects-video",
3368
+ .scene = video_scene,
3369
+ .canvas_label = canvas_label,
3370
+ .update_fx = videoUpdate,
3371
+ .view = videoView,
3372
+ .on_command = videoCommand,
3373
+ });
3374
+ defer app_state.deinit();
3375
+
3376
+ const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
3377
+ .verify = true,
3378
+ .require_same_platform = false,
3379
+ });
3380
+ try std.testing.expect(report.ok());
3381
+ try std.testing.expectEqual(@as(?bool, false), app_state.model.probed_active);
3382
+ try std.testing.expectEqualDeep(recorded_model, app_state.model);
3383
+ try std.testing.expectEqual(recorded_fingerprint, harness.runtime.sessionStateFingerprint());
3384
+ }
3385
+ }
3386
+
3387
+ test "stopping a stream cancels only its own staged answers" {
3388
+ // Two loads share one public key (the TS tier routes every load of
3389
+ // one event arm through the same tag-derived key); the replaced
3390
+ // first load still owes its staged terminal when the second is
3391
+ // stopped. The cancel is token-scoped: B's stop never silences A.
3392
+ var fx = VideoEffects.init(std.testing.allocator);
3393
+ defer fx.deinit();
3394
+ fx.executor = .fake;
3395
+
3396
+ fx.loadVideo(.{
3397
+ .key = clip_key,
3398
+ .surface = clip_surface,
3399
+ .path = clip_path,
3400
+ .on_event = VideoEffects.videoMsg(.video_event),
3401
+ });
3402
+ // A's terminal stages (token 1)...
3403
+ try fx.feedVideoEvent(.failed, 0, 0, false, false, 0, 0);
3404
+ // ...then B replaces under the same key (token 2), stages its own
3405
+ // tick, and is stopped: only B's answers cancel.
3406
+ fx.loadVideo(.{
3407
+ .key = clip_key,
3408
+ .surface = clip_surface,
3409
+ .url = clip_url,
3410
+ .on_event = VideoEffects.videoMsg(.video_event),
3411
+ });
3412
+ try fx.feedVideoEvent(.position, 500, 92_500, true, false, 0, 0);
3413
+ fx.stopVideoCancel(fx.videoMintedToken());
3414
+
3415
+ var boundary = fx.drainBoundary();
3416
+ const msg = fx.takeMsgWithin(&boundary) orelse return error.TestExpectedMsg;
3417
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.failed, msg.video_event.kind);
3418
+ try std.testing.expectEqual(clip_key, msg.video_event.key);
3419
+ try std.testing.expect(fx.takeMsgWithin(&boundary) == null);
3420
+ }
3421
+
3422
+ test "a post-completion play resets the snapshot identically under replay" {
3423
+ const gpa = std.testing.allocator;
3424
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
3425
+ defer std.heap.page_allocator.destroy(buffer);
3426
+ buffer.len = 0;
3427
+
3428
+ // Record: the playback completes naturally (the host retires the
3429
+ // player), then an update calls play and probes the snapshot in
3430
+ // the same dispatch. The refusal reset the channel before
3431
+ // playVideo returned, so the probe reads inactive, and the
3432
+ // `.failed` terminal delivers at the next wake.
3433
+ var recorded_model: VideoModel = undefined;
3434
+ var recorded_fingerprint: u64 = 0;
3435
+ {
3436
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
3437
+ defer std.heap.page_allocator.destroy(recorder);
3438
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
3439
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
3440
+
3441
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
3442
+ defer harness.destroy(gpa);
3443
+ harness.null_platform.gpu_surfaces = true;
3444
+ harness.runtime.options.session_recorder = recorder;
3445
+ try harness.null_platform.setVideoMeta("orchard-flyover.mp4", 10_000, 640, 360);
3446
+
3447
+ const app_state = try gpa.create(VideoApp);
3448
+ defer gpa.destroy(app_state);
3449
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
3450
+ .name = "effects-video",
3451
+ .scene = video_scene,
3452
+ .canvas_label = canvas_label,
3453
+ .update_fx = videoUpdate,
3454
+ .view = videoView,
3455
+ .on_command = videoCommand,
3456
+ });
3457
+ defer app_state.deinit();
3458
+ const app = app_state.app();
3459
+ try harness.start(app);
3460
+ try dispatchFrame(harness, app, 1);
3461
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
3462
+
3463
+ const np = &harness.null_platform;
3464
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load", .window_id = 1, .view_label = canvas_label } });
3465
+ try harness.runtime.dispatchPlatformEvent(app, np.takeVideoLoaded().?);
3466
+ try harness.runtime.dispatchPlatformEvent(app, np.advanceVideo(10_500).?);
3467
+ try std.testing.expectEqual(@as(usize, 1), app_state.model.completed_count);
3468
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.play-then-probe", .window_id = 1, .view_label = canvas_label } });
3469
+ try std.testing.expectEqual(@as(?bool, false), app_state.model.probed_active);
3470
+ while (np.takeWake()) |_| {}
3471
+ try harness.runtime.dispatchPlatformEvent(app, .wake);
3472
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.failed, app_state.model.last_kind.?);
3473
+ try dispatchFrame(harness, app, 2);
3474
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
3475
+
3476
+ recorder.finish();
3477
+ try std.testing.expect(!recorder.failed);
3478
+ recorded_model = app_state.model;
3479
+ recorded_fingerprint = harness.runtime.sessionStateFingerprint();
3480
+ }
3481
+
3482
+ // Replay offline: the fake channel latched the completion, so the
3483
+ // replayed play resets at the same instant and the probe reads the
3484
+ // same inactive snapshot; the journaled terminal delivers itself.
3485
+ {
3486
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
3487
+ defer harness.destroy(gpa);
3488
+ harness.null_platform.gpu_surfaces = true;
3489
+ harness.null_platform.video_playback = false;
3490
+ harness.runtime.options.platform = harness.null_platform.platform();
3491
+
3492
+ const app_state = try gpa.create(VideoApp);
3493
+ defer gpa.destroy(app_state);
3494
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
3495
+ .name = "effects-video",
3496
+ .scene = video_scene,
3497
+ .canvas_label = canvas_label,
3498
+ .update_fx = videoUpdate,
3499
+ .view = videoView,
3500
+ .on_command = videoCommand,
3501
+ });
3502
+ defer app_state.deinit();
3503
+
3504
+ const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
3505
+ .verify = true,
3506
+ .require_same_platform = false,
3507
+ });
3508
+ try std.testing.expect(report.ok());
3509
+ try std.testing.expectEqual(@as(?bool, false), app_state.model.probed_active);
3510
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.failed, app_state.model.last_kind.?);
3511
+ try std.testing.expect(!app_state.effects.videoSnapshot().active);
3512
+ try std.testing.expectEqualDeep(recorded_model, app_state.model);
3513
+ try std.testing.expectEqual(recorded_fingerprint, harness.runtime.sessionStateFingerprint());
3514
+ }
3515
+ }
3516
+
3517
+ test "a past-window host readout clamps at delivery and replays clamped" {
3518
+ const gpa = std.testing.allocator;
3519
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
3520
+ defer std.heap.page_allocator.destroy(buffer);
3521
+ buffer.len = 0;
3522
+
3523
+ // Record against a host reporting a duration past the exact-integer
3524
+ // delivery window (the platform seam accepts any u64): the channel
3525
+ // clamps at delivery, so the Msg, the mirrors, and the journaled
3526
+ // record all carry the window's ceiling — an honest recording, not
3527
+ // damage.
3528
+ const past_window: u64 = effects_mod.max_effect_video_scalar_exclusive * 2;
3529
+ const clamped: u64 = effects_mod.max_effect_video_scalar_exclusive - 1;
3530
+ var recorded_model: VideoModel = undefined;
3531
+ var recorded_fingerprint: u64 = 0;
3532
+ {
3533
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
3534
+ defer std.heap.page_allocator.destroy(recorder);
3535
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
3536
+ recorder.begin(.{ .platform_name = "test", .app_name = "effects-video", .window_width = 400, .window_height = 300 });
3537
+
3538
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
3539
+ defer harness.destroy(gpa);
3540
+ harness.null_platform.gpu_surfaces = true;
3541
+ harness.runtime.options.session_recorder = recorder;
3542
+ try harness.null_platform.setVideoMeta("orchard-flyover.mp4", past_window, 1280, 720);
3543
+
3544
+ const app_state = try gpa.create(VideoApp);
3545
+ defer gpa.destroy(app_state);
3546
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
3547
+ .name = "effects-video",
3548
+ .scene = video_scene,
3549
+ .canvas_label = canvas_label,
3550
+ .update_fx = videoUpdate,
3551
+ .view = videoView,
3552
+ .on_command = videoCommand,
3553
+ });
3554
+ defer app_state.deinit();
3555
+ const app = app_state.app();
3556
+ try harness.start(app);
3557
+ try dispatchFrame(harness, app, 1);
3558
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
3559
+
3560
+ const np = &harness.null_platform;
3561
+ try harness.runtime.dispatchPlatformEvent(app, .{ .native_command = .{ .name = "video.load", .window_id = 1, .view_label = canvas_label } });
3562
+ try harness.runtime.dispatchPlatformEvent(app, np.takeVideoLoaded().?);
3563
+ try dispatchFrame(harness, app, 2);
3564
+ try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
3565
+
3566
+ try std.testing.expectEqual(effects_mod.EffectVideoEventKind.loaded, app_state.model.last_kind.?);
3567
+ try std.testing.expectEqual(clamped, app_state.model.last_duration_ms);
3568
+ try std.testing.expectEqual(clamped, app_state.effects.videoSnapshot().duration_ms);
3569
+
3570
+ recorder.finish();
3571
+ try std.testing.expect(!recorder.failed);
3572
+ recorded_model = app_state.model;
3573
+ recorded_fingerprint = harness.runtime.sessionStateFingerprint();
3574
+ }
3575
+
3576
+ // Replay offline: the record carries the clamped value, so the
3577
+ // damage gate (which refuses scalars at or past the window) admits
3578
+ // the honest recording, and the replayed platform event clamps
3579
+ // identically on its way into the mirrors.
3580
+ {
3581
+ const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
3582
+ defer harness.destroy(gpa);
3583
+ harness.null_platform.gpu_surfaces = true;
3584
+ harness.null_platform.video_playback = false;
3585
+ harness.runtime.options.platform = harness.null_platform.platform();
3586
+
3587
+ const app_state = try gpa.create(VideoApp);
3588
+ defer gpa.destroy(app_state);
3589
+ app_state.* = VideoApp.init(std.heap.page_allocator, .{}, .{
3590
+ .name = "effects-video",
3591
+ .scene = video_scene,
3592
+ .canvas_label = canvas_label,
3593
+ .update_fx = videoUpdate,
3594
+ .view = videoView,
3595
+ .on_command = videoCommand,
3596
+ });
3597
+ defer app_state.deinit();
3598
+
3599
+ const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
3600
+ .verify = true,
3601
+ .require_same_platform = false,
3602
+ });
3603
+ try std.testing.expect(report.ok());
3604
+ try std.testing.expect(report.effects_fed > 0);
3605
+ try std.testing.expectEqual(clamped, app_state.model.last_duration_ms);
3606
+ try std.testing.expectEqualDeep(recorded_model, app_state.model);
3607
+ try std.testing.expectEqual(recorded_fingerprint, harness.runtime.sessionStateFingerprint());
3608
+ }
3609
+ }