@native-sdk/cli 0.5.2 → 0.5.4
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.
- package/build/app.zig +41 -1
- package/build.zig +264 -3
- package/native-sdk.d.ts +17 -1
- package/package.json +9 -9
- package/packages/core/package-lock.json +2 -2
- package/packages/core/package.json +1 -1
- package/packages/core/rt/rt.zig +138 -3
- package/packages/core/sdk/core.ts +225 -3
- package/packages/core/sdk/events.ts +39 -2
- package/packages/core/src/checker.ts +1 -1
- package/packages/core/src/emitter.ts +2625 -262
- package/packages/core/src/infer.ts +53 -7
- package/skill-data/automation/SKILL.md +7 -4
- package/skill-data/core/references/app-model-runtime.md +3 -0
- package/skill-data/native-ui/SKILL.md +4 -2
- package/skill-data/ts-core/SKILL.md +9 -1
- package/src/app_runner/root.zig +106 -1
- package/src/app_runner/ts_core_main.zig +4 -0
- package/src/automation/protocol.zig +20 -1
- package/src/embed/host.zig +27 -0
- package/src/embed/tests.zig +187 -0
- package/src/embed/ui_host.zig +4 -0
- package/src/platform/linux/gtk_host.c +112 -3
- package/src/platform/linux/root.zig +71 -0
- package/src/platform/macos/appkit_host.h +59 -2
- package/src/platform/macos/appkit_host.m +663 -34
- package/src/platform/macos/cef_host.mm +262 -10
- package/src/platform/macos/root.zig +126 -4
- package/src/platform/null_platform.zig +298 -5
- package/src/platform/null_platform_tests.zig +39 -0
- package/src/platform/root.zig +36 -0
- package/src/platform/types.zig +247 -8
- package/src/platform/windows/root.zig +134 -0
- package/src/platform/windows/webview2_host.cpp +152 -40
- package/src/primitives/app_manifest/comptime_scene.zig +5 -0
- package/src/primitives/app_manifest/root.zig +1 -0
- package/src/primitives/app_manifest/types.zig +32 -0
- package/src/primitives/canvas/chart.zig +7 -7
- package/src/primitives/canvas/chart_tests.zig +66 -0
- package/src/primitives/canvas/commands.zig +48 -0
- package/src/primitives/canvas/events.zig +161 -0
- package/src/primitives/canvas/font_ttf.zig +155 -8
- package/src/primitives/canvas/font_ttf_tests.zig +686 -0
- package/src/primitives/canvas/reference.zig +67 -8
- package/src/primitives/canvas/render_fingerprints.zig +8 -0
- package/src/primitives/canvas/root.zig +25 -0
- package/src/primitives/canvas/tests.zig +1 -0
- package/src/primitives/canvas/ui.zig +46 -9
- package/src/primitives/canvas/ui_markup.zig +82 -34
- package/src/primitives/canvas/ui_markup_binary.zig +4 -1
- package/src/primitives/canvas/ui_markup_binary_tests.zig +25 -0
- package/src/primitives/canvas/ui_markup_compiled.zig +63 -11
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +88 -0
- package/src/primitives/canvas/ui_markup_contract.zig +10 -1
- package/src/primitives/canvas/ui_markup_contract_tests.zig +50 -0
- package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
- package/src/primitives/canvas/ui_markup_tests.zig +55 -9
- package/src/primitives/canvas/ui_markup_view.zig +81 -14
- package/src/primitives/canvas/ui_markup_view_tests.zig +219 -7
- package/src/primitives/canvas/ui_schema.zig +29 -0
- package/src/primitives/canvas/ui_schema_tests.zig +11 -11
- package/src/primitives/canvas/ui_tests.zig +31 -0
- package/src/primitives/canvas/vector.zig +258 -141
- package/src/primitives/canvas/widget_access.zig +1 -1
- package/src/primitives/canvas/widget_builtin_tests.zig +129 -0
- package/src/primitives/canvas/widget_layout.zig +9 -4
- package/src/primitives/canvas/widget_render.zig +78 -40
- package/src/primitives/canvas/widget_render_controls.zig +15 -4
- package/src/primitives/canvas/widget_semantics.zig +6 -1
- package/src/primitives/canvas/widget_semantics_tests.zig +168 -0
- package/src/primitives/canvas/widget_text_input.zig +118 -10
- package/src/primitives/canvas/widgets.zig +16 -0
- package/src/root.zig +12 -0
- package/src/runtime/api.zig +15 -0
- package/src/runtime/automation_commands.zig +33 -0
- package/src/runtime/automation_widget_dispatch.zig +64 -0
- package/src/runtime/bridge_payload.zig +7 -0
- package/src/runtime/bridge_responses.zig +5 -0
- package/src/runtime/builtin_bridge.zig +9 -4
- package/src/runtime/canvas_font_tests.zig +711 -5
- package/src/runtime/canvas_fonts.zig +120 -25
- package/src/runtime/canvas_frame_helpers.zig +15 -0
- package/src/runtime/canvas_image_tests.zig +221 -0
- package/src/runtime/canvas_images.zig +78 -19
- package/src/runtime/canvas_limits.zig +44 -8
- package/src/runtime/canvas_widget_clipboard_tests.zig +123 -0
- package/src/runtime/canvas_widget_context_menu_tests.zig +55 -0
- package/src/runtime/canvas_widget_events.zig +24 -4
- package/src/runtime/canvas_widget_runtime.zig +24 -7
- package/src/runtime/canvas_widget_state.zig +6 -1
- package/src/runtime/canvas_widget_text_tests.zig +277 -0
- package/src/runtime/core.zig +148 -9
- package/src/runtime/effects.zig +1664 -43
- package/src/runtime/effects_fetch_tests.zig +89 -1
- package/src/runtime/effects_file_tests.zig +41 -1
- package/src/runtime/effects_image_tests.zig +1304 -0
- package/src/runtime/effects_tests.zig +153 -0
- package/src/runtime/flow.zig +44 -0
- package/src/runtime/gpu_surface_events.zig +7 -0
- package/src/runtime/media_surface.zig +726 -0
- package/src/runtime/media_surface_tests.zig +1476 -0
- package/src/runtime/root.zig +20 -0
- package/src/runtime/session_blobs.zig +689 -0
- package/src/runtime/session_journal.zig +111 -20
- package/src/runtime/session_record.zig +27 -2
- package/src/runtime/session_replay.zig +168 -16
- package/src/runtime/session_tests.zig +2118 -4
- package/src/runtime/shell_layout.zig +7 -0
- package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
- package/src/runtime/testdata/fonts/OFL.txt +93 -0
- package/src/runtime/tests.zig +3 -0
- package/src/runtime/ts_core_host.zig +439 -4
- package/src/runtime/ts_core_host_tests.zig +41 -0
- package/src/runtime/ts_ui_app.zig +63 -0
- package/src/runtime/ui_app.zig +169 -6
- package/src/runtime/ui_app_tests.zig +334 -0
- package/src/runtime/ui_app_window_tests.zig +468 -0
- package/src/runtime/window_command_bridge_tests.zig +117 -0
- package/src/runtime/window_storage.zig +32 -0
- package/src/runtime/window_views.zig +61 -1
- package/src/tooling/manifest.zig +75 -0
- package/src/tooling/raw_manifest.zig +2 -0
- package/src/tooling/templates.zig +219 -1
|
@@ -51,6 +51,13 @@ const SessionModel = struct {
|
|
|
51
51
|
name_anchor: usize = 0,
|
|
52
52
|
name_focus: usize = 0,
|
|
53
53
|
name_edits: u32 = 0,
|
|
54
|
+
/// The pinch channel's cumulative zoom (the product of 1 + delta
|
|
55
|
+
/// across change events) plus phase counters: the reference session
|
|
56
|
+
/// pinches once raw and once through the automation verb, so replay
|
|
57
|
+
/// re-deriving the identical product pins the journaled scale field.
|
|
58
|
+
zoom: f32 = 1,
|
|
59
|
+
pinch_begins: u32 = 0,
|
|
60
|
+
pinch_ends: u32 = 0,
|
|
54
61
|
|
|
55
62
|
fn bodyText(self: *const SessionModel) []const u8 {
|
|
56
63
|
return self.body[0..self.body_len];
|
|
@@ -68,11 +75,13 @@ const SessionModel = struct {
|
|
|
68
75
|
const SessionMsg = union(enum) {
|
|
69
76
|
increment,
|
|
70
77
|
stamp,
|
|
78
|
+
quit,
|
|
71
79
|
start_fetch,
|
|
72
80
|
start_spawn,
|
|
73
81
|
start_audio,
|
|
74
82
|
query_edit: canvas.TextInputEvent,
|
|
75
83
|
name_edit: canvas.TextInputEvent,
|
|
84
|
+
pinch: platform.PinchEvent,
|
|
76
85
|
fetched: effects_mod.EffectResponse,
|
|
77
86
|
line: effects_mod.EffectLine,
|
|
78
87
|
exited: effects_mod.EffectExit,
|
|
@@ -86,6 +95,7 @@ fn sessionUpdate(model: *SessionModel, msg: SessionMsg, fx: *SessionApp.Effects)
|
|
|
86
95
|
switch (msg) {
|
|
87
96
|
.increment => model.count += 1,
|
|
88
97
|
.stamp => model.stamp_ms = fx.wallMs(),
|
|
98
|
+
.quit => fx.quitApp(),
|
|
89
99
|
.start_fetch => fx.fetch(.{
|
|
90
100
|
.key = 1,
|
|
91
101
|
.url = "http://journal.invalid/data",
|
|
@@ -123,6 +133,11 @@ fn sessionUpdate(model: *SessionModel, msg: SessionMsg, fx: *SessionApp.Effects)
|
|
|
123
133
|
},
|
|
124
134
|
.query_edit => |edit| applyMirrorEdit(&model.query, &model.query_len, &model.query_anchor, &model.query_focus, &model.query_edits, edit),
|
|
125
135
|
.name_edit => |edit| applyMirrorEdit(&model.name, &model.name_len, &model.name_anchor, &model.name_focus, &model.name_edits, edit),
|
|
136
|
+
.pinch => |pinch| switch (pinch.phase) {
|
|
137
|
+
.begin => model.pinch_begins += 1,
|
|
138
|
+
.change => model.zoom *= (1 + pinch.scale),
|
|
139
|
+
.end => model.pinch_ends += 1,
|
|
140
|
+
},
|
|
126
141
|
.line => model.line_count += 1,
|
|
127
142
|
.exited => |exit| model.exit_code = exit.code,
|
|
128
143
|
.tick => |timer| model.tick_timestamp_ns = timer.timestamp_ns,
|
|
@@ -162,7 +177,13 @@ fn sessionView(ui: *SessionApp.Ui, model: *const SessionModel) SessionApp.Ui.Nod
|
|
|
162
177
|
.placeholder = "Name",
|
|
163
178
|
.on_input = SessionApp.Ui.inputMsg(.name_edit),
|
|
164
179
|
}, .{}),
|
|
180
|
+
// A static multi-line source the paste replay pin copies from:
|
|
181
|
+
// the journal records only the raw select-all/copy/paste
|
|
182
|
+
// platform events, so the multi-line clipboard bytes and the
|
|
183
|
+
// sanitized single-line insert both re-derive on replay.
|
|
184
|
+
ui.el(.textarea, .{ .text = "line one\nline two" }, .{}),
|
|
165
185
|
ui.text(.{}, ui.fmt("Query {s} ({d}) Name {s} ({d})", .{ model.queryText(), model.query_edits, model.nameText(), model.name_edits })),
|
|
186
|
+
ui.text(.{}, ui.fmt("Zoom {d:.4} ({d}/{d})", .{ model.zoom, model.pinch_begins, model.pinch_ends })),
|
|
166
187
|
ui.button(.{ .on_press = .increment }, "Increment"),
|
|
167
188
|
});
|
|
168
189
|
}
|
|
@@ -170,6 +191,7 @@ fn sessionView(ui: *SessionApp.Ui, model: *const SessionModel) SessionApp.Ui.Nod
|
|
|
170
191
|
fn sessionCommand(name: []const u8) ?SessionMsg {
|
|
171
192
|
if (std.mem.eql(u8, name, "session.increment")) return .increment;
|
|
172
193
|
if (std.mem.eql(u8, name, "session.stamp")) return .stamp;
|
|
194
|
+
if (std.mem.eql(u8, name, "session.quit")) return .quit;
|
|
173
195
|
if (std.mem.eql(u8, name, "session.fetch")) return .start_fetch;
|
|
174
196
|
if (std.mem.eql(u8, name, "session.spawn")) return .start_spawn;
|
|
175
197
|
if (std.mem.eql(u8, name, "session.audio")) return .start_audio;
|
|
@@ -188,6 +210,10 @@ const session_windows = [_]app_manifest.ShellWindow{.{
|
|
|
188
210
|
}};
|
|
189
211
|
const session_scene: app_manifest.ShellConfig = .{ .windows = &session_windows };
|
|
190
212
|
|
|
213
|
+
fn sessionPinch(pinch: platform.PinchEvent) ?SessionMsg {
|
|
214
|
+
return .{ .pinch = pinch };
|
|
215
|
+
}
|
|
216
|
+
|
|
191
217
|
fn sessionOptions() SessionApp.Options {
|
|
192
218
|
return .{
|
|
193
219
|
.name = "session-demo",
|
|
@@ -196,6 +222,7 @@ fn sessionOptions() SessionApp.Options {
|
|
|
196
222
|
.update_fx = sessionUpdate,
|
|
197
223
|
.view = sessionView,
|
|
198
224
|
.on_command = sessionCommand,
|
|
225
|
+
.on_pinch = sessionPinch,
|
|
199
226
|
};
|
|
200
227
|
}
|
|
201
228
|
|
|
@@ -337,6 +364,98 @@ fn recordReferenceSession(gpa: std.mem.Allocator, buffer: *JournalBuffer, web_la
|
|
|
337
364
|
try std.testing.expectEqualStrings("", app_state.model.queryText());
|
|
338
365
|
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
339
366
|
|
|
367
|
+
// A recorded MULTI-LINE PASTE replays to the identical sanitized
|
|
368
|
+
// value: copy the textarea's two lines (select-all + copy — raw
|
|
369
|
+
// journaled inputs that rebuild the clipboard on replay), then paste
|
|
370
|
+
// into the single-line search field. Sanitization is deterministic
|
|
371
|
+
// derivation at the apply seam, so the replayed editor, the model
|
|
372
|
+
// mirror, and the fingerprint all land on the same stripped bytes.
|
|
373
|
+
var textarea_frame: ?geometry.RectF = null;
|
|
374
|
+
for ((try harness.runtime.canvasWidgetLayout(1, canvas_label)).nodes) |node| {
|
|
375
|
+
if (node.widget.kind == .textarea) textarea_frame = node.frame;
|
|
376
|
+
}
|
|
377
|
+
const source_frame = textarea_frame.?;
|
|
378
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
|
|
379
|
+
.window_id = 1,
|
|
380
|
+
.label = canvas_label,
|
|
381
|
+
.kind = .pointer_down,
|
|
382
|
+
.x = source_frame.x + source_frame.width * 0.5,
|
|
383
|
+
.y = source_frame.y + source_frame.height * 0.5,
|
|
384
|
+
} });
|
|
385
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
|
|
386
|
+
.window_id = 1,
|
|
387
|
+
.label = canvas_label,
|
|
388
|
+
.kind = .key_down,
|
|
389
|
+
.key = "a",
|
|
390
|
+
.modifiers = .{ .primary = true, .command = true },
|
|
391
|
+
} });
|
|
392
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
|
|
393
|
+
.window_id = 1,
|
|
394
|
+
.label = canvas_label,
|
|
395
|
+
.kind = .key_down,
|
|
396
|
+
.key = "c",
|
|
397
|
+
.modifiers = .{ .primary = true, .command = true },
|
|
398
|
+
} });
|
|
399
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
|
|
400
|
+
.window_id = 1,
|
|
401
|
+
.label = canvas_label,
|
|
402
|
+
.kind = .pointer_down,
|
|
403
|
+
.x = field_frame.x + field_frame.width * 0.5,
|
|
404
|
+
.y = field_frame.y + field_frame.height * 0.5,
|
|
405
|
+
} });
|
|
406
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
|
|
407
|
+
.window_id = 1,
|
|
408
|
+
.label = canvas_label,
|
|
409
|
+
.kind = .key_down,
|
|
410
|
+
.key = "v",
|
|
411
|
+
.modifiers = .{ .primary = true, .command = true },
|
|
412
|
+
} });
|
|
413
|
+
try std.testing.expectEqualStrings("line oneline two", app_state.model.queryText());
|
|
414
|
+
|
|
415
|
+
// A trackpad pinch, twice over: the raw journaled phase stream (the
|
|
416
|
+
// macOS host shape — begin, two deltas, end; the +25% then -25%
|
|
417
|
+
// deltas compound as a PRODUCT, 1.25 * 0.75 = 0.9375, never a sum's
|
|
418
|
+
// 1.0), then the automation verb's synthesized gesture, which
|
|
419
|
+
// journals the same leaf gpu_surface_input events. Replay must
|
|
420
|
+
// re-derive the identical cumulative zoom from the journaled scale
|
|
421
|
+
// fields alone. Every value here is binary-exact in f32, so the
|
|
422
|
+
// model equality below is exact, not approximate.
|
|
423
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
|
|
424
|
+
.window_id = 1,
|
|
425
|
+
.label = canvas_label,
|
|
426
|
+
.kind = .pinch_begin,
|
|
427
|
+
.x = 200,
|
|
428
|
+
.y = 150,
|
|
429
|
+
} });
|
|
430
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
|
|
431
|
+
.window_id = 1,
|
|
432
|
+
.label = canvas_label,
|
|
433
|
+
.kind = .pinch_change,
|
|
434
|
+
.x = 200,
|
|
435
|
+
.y = 150,
|
|
436
|
+
.scale = 0.25,
|
|
437
|
+
} });
|
|
438
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
|
|
439
|
+
.window_id = 1,
|
|
440
|
+
.label = canvas_label,
|
|
441
|
+
.kind = .pinch_change,
|
|
442
|
+
.x = 200,
|
|
443
|
+
.y = 150,
|
|
444
|
+
.scale = -0.25,
|
|
445
|
+
} });
|
|
446
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
|
|
447
|
+
.window_id = 1,
|
|
448
|
+
.label = canvas_label,
|
|
449
|
+
.kind = .pinch_end,
|
|
450
|
+
.x = 200,
|
|
451
|
+
.y = 150,
|
|
452
|
+
} });
|
|
453
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
454
|
+
var pinch_buffer: [64]u8 = undefined;
|
|
455
|
+
const pinch_command = try std.fmt.bufPrint(&pinch_buffer, "widget-pinch {s} 1.5 120 80", .{canvas_label});
|
|
456
|
+
try harness.runtime.dispatchAutomationCommand(app, pinch_command);
|
|
457
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
458
|
+
|
|
340
459
|
recorder.finish();
|
|
341
460
|
try std.testing.expect(!recorder.failed);
|
|
342
461
|
|
|
@@ -389,10 +508,16 @@ test "a recorded session replays to identical model state and fingerprints" {
|
|
|
389
508
|
try std.testing.expect(recorded.model.stamp_ms != 0);
|
|
390
509
|
try std.testing.expectEqual(@as(u32, 1), recorded.model.spectrum_count);
|
|
391
510
|
try std.testing.expect(recorded.model.band_checksum != 0);
|
|
392
|
-
// The typed insert
|
|
393
|
-
// model's `on_input` mirror
|
|
394
|
-
|
|
395
|
-
try std.testing.
|
|
511
|
+
// The typed insert, the DERIVED Escape-clear, and the sanitized
|
|
512
|
+
// multi-line paste all reached the model's `on_input` mirror — the
|
|
513
|
+
// paste landing STRIPPED of its line breaks (single-line rule).
|
|
514
|
+
try std.testing.expectEqual(@as(u32, 3), recorded.model.query_edits);
|
|
515
|
+
try std.testing.expectEqualStrings("line oneline two", recorded.model.queryText());
|
|
516
|
+
// Both pinch gestures reached the model: the raw stream's product
|
|
517
|
+
// (1.25 * 0.75 = 0.9375) times the verb's exact 1.5.
|
|
518
|
+
try std.testing.expectEqual(@as(u32, 2), recorded.model.pinch_begins);
|
|
519
|
+
try std.testing.expectEqual(@as(u32, 2), recorded.model.pinch_ends);
|
|
520
|
+
try std.testing.expectEqual(@as(f32, 1.40625), recorded.model.zoom);
|
|
396
521
|
|
|
397
522
|
const replayed = try replayIntoFreshApp(gpa, buffer.journalBytes(), true);
|
|
398
523
|
try std.testing.expect(replayed.report.ok());
|
|
@@ -877,3 +1002,1992 @@ test "cross-platform journals are refused at the v1 bar" {
|
|
|
877
1002
|
});
|
|
878
1003
|
try std.testing.expectError(error.ReplayPlatformMismatch, result);
|
|
879
1004
|
}
|
|
1005
|
+
|
|
1006
|
+
test "a menu-bar lifecycle session (policy hide, Dock reopen, quit) records and replays its window states" {
|
|
1007
|
+
const gpa = std.testing.allocator;
|
|
1008
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
1009
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
1010
|
+
buffer.len = 0;
|
|
1011
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
1012
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
1013
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
1014
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "session-demo", .window_width = 400, .window_height = 300 });
|
|
1015
|
+
|
|
1016
|
+
// Record: the host reports a .hide-policy window's whole lifecycle
|
|
1017
|
+
// as ordinary frame events — the hide (open stays true, hidden
|
|
1018
|
+
// flips), the Dock reopen's re-show, a second hide, and the tray
|
|
1019
|
+
// Quit's app_shutdown. The verbs themselves never journal; these
|
|
1020
|
+
// journaled platform events ARE the record.
|
|
1021
|
+
{
|
|
1022
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
1023
|
+
defer harness.destroy(gpa);
|
|
1024
|
+
harness.null_platform.gpu_surfaces = true;
|
|
1025
|
+
harness.runtime.options.session_recorder = recorder;
|
|
1026
|
+
const app_state = try gpa.create(SessionApp);
|
|
1027
|
+
defer gpa.destroy(app_state);
|
|
1028
|
+
app_state.* = SessionApp.init(std.heap.page_allocator, .{}, sessionOptions());
|
|
1029
|
+
defer app_state.deinit();
|
|
1030
|
+
app_state.effects.executor = .fake;
|
|
1031
|
+
const app = app_state.app();
|
|
1032
|
+
try harness.start(app);
|
|
1033
|
+
|
|
1034
|
+
const player_frame = geometry.RectF.init(0, 0, 400, 300);
|
|
1035
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .window_frame_changed = .{
|
|
1036
|
+
.id = 2,
|
|
1037
|
+
.label = "player",
|
|
1038
|
+
.title = "Player",
|
|
1039
|
+
.frame = player_frame,
|
|
1040
|
+
.open = true,
|
|
1041
|
+
.focused = true,
|
|
1042
|
+
} });
|
|
1043
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .window_frame_changed = .{
|
|
1044
|
+
.id = 2,
|
|
1045
|
+
.label = "player",
|
|
1046
|
+
.title = "Player",
|
|
1047
|
+
.frame = player_frame,
|
|
1048
|
+
.open = true,
|
|
1049
|
+
.focused = false,
|
|
1050
|
+
.hidden = true,
|
|
1051
|
+
} });
|
|
1052
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
1053
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .window_frame_changed = .{
|
|
1054
|
+
.id = 2,
|
|
1055
|
+
.label = "player",
|
|
1056
|
+
.title = "Player",
|
|
1057
|
+
.frame = player_frame,
|
|
1058
|
+
.open = true,
|
|
1059
|
+
.focused = true,
|
|
1060
|
+
.hidden = false,
|
|
1061
|
+
} });
|
|
1062
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .window_frame_changed = .{
|
|
1063
|
+
.id = 2,
|
|
1064
|
+
.label = "player",
|
|
1065
|
+
.title = "Player",
|
|
1066
|
+
.frame = player_frame,
|
|
1067
|
+
.open = true,
|
|
1068
|
+
.focused = false,
|
|
1069
|
+
.hidden = true,
|
|
1070
|
+
} });
|
|
1071
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
1072
|
+
var recording_windows: [platform.max_windows]platform.WindowInfo = undefined;
|
|
1073
|
+
for (harness.runtime.listWindows(&recording_windows)) |info| {
|
|
1074
|
+
if (info.id == 2) try std.testing.expect(info.hidden);
|
|
1075
|
+
}
|
|
1076
|
+
// The quit verb's consequence: the host emits app_shutdown —
|
|
1077
|
+
// dispatching it runs the exactly-once stop hook AND seals the
|
|
1078
|
+
// recording's journal, exactly like a real quit.
|
|
1079
|
+
try harness.runtime.dispatchPlatformEvent(app, .app_shutdown);
|
|
1080
|
+
try std.testing.expect(recorder.finished);
|
|
1081
|
+
try std.testing.expect(!recorder.failed);
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
// Replay into a fresh app: every window state lands identically —
|
|
1085
|
+
// ending hidden, exactly as recorded — and the sealed journal
|
|
1086
|
+
// replays clean through the shutdown.
|
|
1087
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
1088
|
+
defer harness.destroy(gpa);
|
|
1089
|
+
harness.null_platform.gpu_surfaces = true;
|
|
1090
|
+
const app_state = try gpa.create(SessionApp);
|
|
1091
|
+
defer gpa.destroy(app_state);
|
|
1092
|
+
app_state.* = SessionApp.init(std.heap.page_allocator, .{}, sessionOptions());
|
|
1093
|
+
defer app_state.deinit();
|
|
1094
|
+
const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
1095
|
+
.verify = true,
|
|
1096
|
+
.require_same_platform = false,
|
|
1097
|
+
});
|
|
1098
|
+
try std.testing.expect(report.ok());
|
|
1099
|
+
var replayed_windows: [platform.max_windows]platform.WindowInfo = undefined;
|
|
1100
|
+
var found = false;
|
|
1101
|
+
for (harness.runtime.listWindows(&replayed_windows)) |info| {
|
|
1102
|
+
if (info.id != 2) continue;
|
|
1103
|
+
found = true;
|
|
1104
|
+
try std.testing.expect(info.open);
|
|
1105
|
+
try std.testing.expect(info.hidden);
|
|
1106
|
+
}
|
|
1107
|
+
try std.testing.expect(found);
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
test "a recorded quit journals the requesting command before the shutdown that seals it" {
|
|
1111
|
+
// The quit verb is requested MID DISPATCH: the command's update
|
|
1112
|
+
// returns fx.quitApp() while the command event still sits on the
|
|
1113
|
+
// recorder's staging stack. Hosts must therefore QUEUE the stop so
|
|
1114
|
+
// app_shutdown emits on the NEXT loop turn — a synchronous emit
|
|
1115
|
+
// nests the shutdown dispatch inside the command's, the nested
|
|
1116
|
+
// commit + finish() seals the journal, the outer commit no-ops,
|
|
1117
|
+
// and the journal loses the very command (and model mutation) that
|
|
1118
|
+
// quit the app. This test drives the whole verb chain through the
|
|
1119
|
+
// modeled host's queued seam and pins the contract: BOTH records
|
|
1120
|
+
// land, command first, and the session replays
|
|
1121
|
+
// fingerprint-identical.
|
|
1122
|
+
const gpa = std.testing.allocator;
|
|
1123
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
1124
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
1125
|
+
buffer.len = 0;
|
|
1126
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
1127
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
1128
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
1129
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "session-demo", .window_width = 400, .window_height = 300 });
|
|
1130
|
+
|
|
1131
|
+
var recorded_model: SessionModel = undefined;
|
|
1132
|
+
var recorded_fingerprint: u64 = 0;
|
|
1133
|
+
{
|
|
1134
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
1135
|
+
defer harness.destroy(gpa);
|
|
1136
|
+
harness.null_platform.gpu_surfaces = true;
|
|
1137
|
+
harness.runtime.options.session_recorder = recorder;
|
|
1138
|
+
const app_state = try gpa.create(SessionApp);
|
|
1139
|
+
defer gpa.destroy(app_state);
|
|
1140
|
+
// The REAL executor: fx.quitApp() must reach the platform's
|
|
1141
|
+
// quit seam (the fake executor stops at the mirror count).
|
|
1142
|
+
app_state.* = SessionApp.init(std.heap.page_allocator, .{}, sessionOptions());
|
|
1143
|
+
defer app_state.deinit();
|
|
1144
|
+
const app = app_state.app();
|
|
1145
|
+
try harness.start(app);
|
|
1146
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
1147
|
+
.label = canvas_label,
|
|
1148
|
+
.size = geometry.SizeF.init(400, 300),
|
|
1149
|
+
.scale_factor = 2,
|
|
1150
|
+
.frame_index = 1,
|
|
1151
|
+
.timestamp_ns = 1_000_000,
|
|
1152
|
+
} });
|
|
1153
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "session.increment", .window_id = 1 } });
|
|
1154
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
1155
|
+
|
|
1156
|
+
// The tray-Quit shape: the command's update returns the quit
|
|
1157
|
+
// verb, which reaches the modeled host DURING this dispatch.
|
|
1158
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "session.quit", .window_id = 1 } });
|
|
1159
|
+
try std.testing.expectEqual(@as(u32, 1), harness.null_platform.quit_request_count);
|
|
1160
|
+
// The quit dispatch alone must NOT have sealed the journal:
|
|
1161
|
+
// the host queued the stop instead of emitting synchronously.
|
|
1162
|
+
try std.testing.expect(!recorder.finished);
|
|
1163
|
+
|
|
1164
|
+
// The queued stop's loop turn: drain the deferred shutdown and
|
|
1165
|
+
// dispatch it — exactly once — which seals the journal.
|
|
1166
|
+
const shutdown_event = harness.null_platform.takeQueuedQuit() orelse return error.TestUnexpectedResult;
|
|
1167
|
+
try harness.runtime.dispatchPlatformEvent(app, shutdown_event);
|
|
1168
|
+
try std.testing.expect(harness.null_platform.takeQueuedQuit() == null);
|
|
1169
|
+
try std.testing.expect(recorder.finished);
|
|
1170
|
+
try std.testing.expect(!recorder.failed);
|
|
1171
|
+
|
|
1172
|
+
recorded_model = app_state.model;
|
|
1173
|
+
recorded_fingerprint = harness.runtime.sessionStateFingerprint();
|
|
1174
|
+
try std.testing.expectEqual(@as(u32, 1), recorded_model.count);
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
// The journal contains BOTH the quitting command and the shutdown,
|
|
1178
|
+
// in that order — the assertion a synchronous (nested) emit fails.
|
|
1179
|
+
{
|
|
1180
|
+
var reader = try journal.Reader.init(buffer.journalBytes());
|
|
1181
|
+
var saw_quit_command = false;
|
|
1182
|
+
var saw_shutdown = false;
|
|
1183
|
+
while (try reader.next()) |record| {
|
|
1184
|
+
if (record != .event) continue;
|
|
1185
|
+
switch (record.event) {
|
|
1186
|
+
.menu_command => |command| {
|
|
1187
|
+
if (std.mem.eql(u8, command.name, "session.quit")) saw_quit_command = true;
|
|
1188
|
+
},
|
|
1189
|
+
.app_shutdown => {
|
|
1190
|
+
try std.testing.expect(saw_quit_command);
|
|
1191
|
+
saw_shutdown = true;
|
|
1192
|
+
},
|
|
1193
|
+
else => {},
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
try std.testing.expect(saw_quit_command);
|
|
1197
|
+
try std.testing.expect(saw_shutdown);
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
// And the sealed session replays fingerprint-identical into a
|
|
1201
|
+
// fresh app — the quitting command's model mutation included.
|
|
1202
|
+
const replayed = try replayIntoFreshApp(gpa, buffer.journalBytes(), true);
|
|
1203
|
+
try std.testing.expect(replayed.report.ok());
|
|
1204
|
+
try std.testing.expectEqual(@as(u32, 1), replayed.model.count);
|
|
1205
|
+
try std.testing.expectEqualDeep(recorded_model, replayed.model);
|
|
1206
|
+
try std.testing.expectEqual(recorded_fingerprint, replayed.fingerprint);
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
/// The quit-from-start boot: the app's `init_fx` returns the quit verb
|
|
1210
|
+
/// during the installing canvas frame — the dispatch the macOS hosts
|
|
1211
|
+
/// deliver synchronously BEFORE [NSApp run] exists.
|
|
1212
|
+
fn sessionQuitOnBoot(model: *SessionModel, fx: *SessionApp.Effects) void {
|
|
1213
|
+
_ = model;
|
|
1214
|
+
fx.quitApp();
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
fn quitOnBootOptions() SessionApp.Options {
|
|
1218
|
+
var options = sessionOptions();
|
|
1219
|
+
options.init_fx = sessionQuitOnBoot;
|
|
1220
|
+
return options;
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
test "a quit from the app's boot dispatch journals the start turn before the shutdown that seals it" {
|
|
1224
|
+
// A VALID quit can arrive before the host's run loop exists:
|
|
1225
|
+
// App.start's update — `init_fx`, and a TS core's boot command with
|
|
1226
|
+
// it — runs inside the FIRST canvas frame dispatch, which the macOS
|
|
1227
|
+
// hosts deliver synchronously before [NSApp run]. Pre-run there is
|
|
1228
|
+
// no queue turn to defer to, so a host that falls back to an INLINE
|
|
1229
|
+
// shutdown emit nests the shutdown dispatch inside the very boot
|
|
1230
|
+
// dispatch that requested it — the recorder seals the journal
|
|
1231
|
+
// before the boot turn commits, the start of the session is lost,
|
|
1232
|
+
// and replay refuses the recording. The contract this test pins
|
|
1233
|
+
// through the modeled host's pending-then-drain seam (quitApp only
|
|
1234
|
+
// ARMS quit_pending; takeQueuedQuit hands the deferred shutdown to
|
|
1235
|
+
// dispatch as its own top-level turn — exactly the hosts' pre-run
|
|
1236
|
+
// pendingPreRunStop drain): the requesting boot turn seals nothing,
|
|
1237
|
+
// the journal carries the start event AND the shutdown in that
|
|
1238
|
+
// order, and the session replays fingerprint-identical.
|
|
1239
|
+
const gpa = std.testing.allocator;
|
|
1240
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
1241
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
1242
|
+
buffer.len = 0;
|
|
1243
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
1244
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
1245
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
1246
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "session-demo", .window_width = 400, .window_height = 300 });
|
|
1247
|
+
|
|
1248
|
+
var recorded_model: SessionModel = undefined;
|
|
1249
|
+
var recorded_fingerprint: u64 = 0;
|
|
1250
|
+
{
|
|
1251
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
1252
|
+
defer harness.destroy(gpa);
|
|
1253
|
+
harness.null_platform.gpu_surfaces = true;
|
|
1254
|
+
harness.runtime.options.session_recorder = recorder;
|
|
1255
|
+
const app_state = try gpa.create(SessionApp);
|
|
1256
|
+
defer gpa.destroy(app_state);
|
|
1257
|
+
// The REAL executor: the boot fx.quitApp() must reach the
|
|
1258
|
+
// platform's quit seam.
|
|
1259
|
+
app_state.* = SessionApp.init(std.heap.page_allocator, .{}, quitOnBootOptions());
|
|
1260
|
+
defer app_state.deinit();
|
|
1261
|
+
const app = app_state.app();
|
|
1262
|
+
try harness.start(app);
|
|
1263
|
+
|
|
1264
|
+
// The installing canvas frame — the pre-run synchronous
|
|
1265
|
+
// dispatch. init_fx runs inside it and returns the quit verb.
|
|
1266
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
1267
|
+
.label = canvas_label,
|
|
1268
|
+
.size = geometry.SizeF.init(400, 300),
|
|
1269
|
+
.scale_factor = 2,
|
|
1270
|
+
.frame_index = 1,
|
|
1271
|
+
.timestamp_ns = 1_000_000,
|
|
1272
|
+
} });
|
|
1273
|
+
try std.testing.expectEqual(@as(u32, 1), harness.null_platform.quit_request_count);
|
|
1274
|
+
// The boot dispatch that carried the quit has returned, and the
|
|
1275
|
+
// journal is NOT sealed: the quit is parked, never emitted
|
|
1276
|
+
// inline inside its own requesting dispatch.
|
|
1277
|
+
try std.testing.expect(!recorder.finished);
|
|
1278
|
+
|
|
1279
|
+
// The drain turn — the hosts' post-dispatch top-level
|
|
1280
|
+
// emitShutdown + stop — seals the journal exactly once.
|
|
1281
|
+
const shutdown_event = harness.null_platform.takeQueuedQuit() orelse return error.TestUnexpectedResult;
|
|
1282
|
+
try harness.runtime.dispatchPlatformEvent(app, shutdown_event);
|
|
1283
|
+
try std.testing.expect(harness.null_platform.takeQueuedQuit() == null);
|
|
1284
|
+
try std.testing.expect(recorder.finished);
|
|
1285
|
+
try std.testing.expect(!recorder.failed);
|
|
1286
|
+
|
|
1287
|
+
recorded_model = app_state.model;
|
|
1288
|
+
recorded_fingerprint = harness.runtime.sessionStateFingerprint();
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
// The journal contains BOTH the start event and the shutdown, in
|
|
1292
|
+
// that order — the assertion an inline (nested) pre-run emit fails:
|
|
1293
|
+
// nesting seals the journal before the boot turn commits, so the
|
|
1294
|
+
// start of the session never lands.
|
|
1295
|
+
{
|
|
1296
|
+
var reader = try journal.Reader.init(buffer.journalBytes());
|
|
1297
|
+
var saw_start = false;
|
|
1298
|
+
var saw_shutdown = false;
|
|
1299
|
+
while (try reader.next()) |record| {
|
|
1300
|
+
if (record != .event) continue;
|
|
1301
|
+
switch (record.event) {
|
|
1302
|
+
.app_start => saw_start = true,
|
|
1303
|
+
.app_shutdown => {
|
|
1304
|
+
try std.testing.expect(saw_start);
|
|
1305
|
+
saw_shutdown = true;
|
|
1306
|
+
},
|
|
1307
|
+
else => {},
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
try std.testing.expect(saw_start);
|
|
1311
|
+
try std.testing.expect(saw_shutdown);
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
// And the sealed boot-quit session replays fingerprint-identical
|
|
1315
|
+
// into a fresh app whose init_fx quits again on replay.
|
|
1316
|
+
{
|
|
1317
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
1318
|
+
defer harness.destroy(gpa);
|
|
1319
|
+
harness.null_platform.gpu_surfaces = true;
|
|
1320
|
+
const app_state = try gpa.create(SessionApp);
|
|
1321
|
+
defer gpa.destroy(app_state);
|
|
1322
|
+
app_state.* = SessionApp.init(std.heap.page_allocator, .{}, quitOnBootOptions());
|
|
1323
|
+
defer app_state.deinit();
|
|
1324
|
+
const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
1325
|
+
.verify = true,
|
|
1326
|
+
.require_same_platform = false,
|
|
1327
|
+
});
|
|
1328
|
+
try std.testing.expect(report.ok());
|
|
1329
|
+
try std.testing.expectEqualDeep(recorded_model, app_state.model);
|
|
1330
|
+
try std.testing.expectEqual(recorded_fingerprint, harness.runtime.sessionStateFingerprint());
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
// --------------------------------------------- image loads and the blob store
|
|
1335
|
+
|
|
1336
|
+
const session_blobs = @import("session_blobs.zig");
|
|
1337
|
+
|
|
1338
|
+
const image_canvas_label = "image-session-canvas";
|
|
1339
|
+
|
|
1340
|
+
const ImageSessionModel = struct {
|
|
1341
|
+
results: u32 = 0,
|
|
1342
|
+
loaded: u32 = 0,
|
|
1343
|
+
failed: u32 = 0,
|
|
1344
|
+
rejected: u32 = 0,
|
|
1345
|
+
last_width: usize = 0,
|
|
1346
|
+
last_height: usize = 0,
|
|
1347
|
+
last_outcome_name: [24]u8 = @splat(' '),
|
|
1348
|
+
last_outcome_len: usize = 0,
|
|
1349
|
+
/// Chatty-subprocess line counter, plus its value at the moment
|
|
1350
|
+
/// each image terminal lands: the pair makes delivery ORDER a
|
|
1351
|
+
/// model fact — a replay that reorders an image terminal around
|
|
1352
|
+
/// the recorded lines diverges here, not just in pixel state.
|
|
1353
|
+
lines_seen: u32 = 0,
|
|
1354
|
+
lines_before_image: u32 = 0,
|
|
1355
|
+
/// Armed by `.arm_chain`: the NEXT loaded cover terminal (id 21)
|
|
1356
|
+
/// answers by loading a second image (id 25) from inside its own
|
|
1357
|
+
/// update — the chained-load shape whose fast completion must not
|
|
1358
|
+
/// deliver in the drain pass that issued it.
|
|
1359
|
+
chain_on_cover: bool = false,
|
|
1360
|
+
/// Cross-family same-key probes (the fetch and spawn arms below
|
|
1361
|
+
/// reuse the cover's key 21): terminal counts with rejections
|
|
1362
|
+
/// tallied separately, view-pinned so the fingerprint checkpoints
|
|
1363
|
+
/// hold the whole Msg stream, rejections included.
|
|
1364
|
+
responses: u32 = 0,
|
|
1365
|
+
responses_rejected: u32 = 0,
|
|
1366
|
+
exits: u32 = 0,
|
|
1367
|
+
exits_rejected: u32 = 0,
|
|
1368
|
+
files: u32 = 0,
|
|
1369
|
+
/// Armed by `.arm_fetch_chain` / `.arm_file_chain`: the NEXT
|
|
1370
|
+
/// terminal of that family answers by reissuing the SAME key from
|
|
1371
|
+
/// inside its own update (one-shot) — the poll/reload idiom whose
|
|
1372
|
+
/// acceptance must hold identically live and under replay.
|
|
1373
|
+
chain_next_fetch: bool = false,
|
|
1374
|
+
chain_next_file: bool = false,
|
|
1375
|
+
|
|
1376
|
+
fn outcomeName(self: *const ImageSessionModel) []const u8 {
|
|
1377
|
+
return self.last_outcome_name[0..self.last_outcome_len];
|
|
1378
|
+
}
|
|
1379
|
+
};
|
|
1380
|
+
|
|
1381
|
+
const ImageSessionMsg = union(enum) {
|
|
1382
|
+
load_cover,
|
|
1383
|
+
load_cover_ff,
|
|
1384
|
+
load_cover_again,
|
|
1385
|
+
load_broken,
|
|
1386
|
+
load_invalid,
|
|
1387
|
+
load_hostless,
|
|
1388
|
+
arm_chain,
|
|
1389
|
+
arm_fetch_chain,
|
|
1390
|
+
arm_file_chain,
|
|
1391
|
+
cancel_cover,
|
|
1392
|
+
start_chatty,
|
|
1393
|
+
fetch_cover,
|
|
1394
|
+
spawn_cover,
|
|
1395
|
+
read_note,
|
|
1396
|
+
line: effects_mod.EffectLine,
|
|
1397
|
+
image: effects_mod.EffectImageResult,
|
|
1398
|
+
response: effects_mod.EffectResponse,
|
|
1399
|
+
exit: effects_mod.EffectExit,
|
|
1400
|
+
file: effects_mod.EffectFileResult,
|
|
1401
|
+
};
|
|
1402
|
+
|
|
1403
|
+
const ImageSessionApp = ui_app_mod.UiApp(ImageSessionModel, ImageSessionMsg);
|
|
1404
|
+
|
|
1405
|
+
fn imageSessionUpdate(model: *ImageSessionModel, msg: ImageSessionMsg, fx: *ImageSessionApp.Effects) void {
|
|
1406
|
+
switch (msg) {
|
|
1407
|
+
.load_cover => fx.loadImage(.{ .id = 21, .path = "art/cover.png", .on_result = ImageSessionApp.Effects.imageMsg(.image) }),
|
|
1408
|
+
// The cover load with NO handler — fire-and-forget. Its
|
|
1409
|
+
// terminal delivers no Msg, but an executor-truth terminal
|
|
1410
|
+
// (a start failure) must still journal and occupy the id
|
|
1411
|
+
// through its staged window, exactly like the handled form.
|
|
1412
|
+
.load_cover_ff => fx.loadImage(.{ .id = 21, .path = "art/cover.png", .on_result = null }),
|
|
1413
|
+
// A second id over the SAME bytes: the journal's blob store
|
|
1414
|
+
// must hold ONE blob for both records.
|
|
1415
|
+
.load_cover_again => fx.loadImage(.{ .id = 22, .path = "art/cover.png", .on_result = ImageSessionApp.Effects.imageMsg(.image) }),
|
|
1416
|
+
.load_broken => fx.loadImage(.{ .id = 23, .path = "art/broken.png", .on_result = ImageSessionApp.Effects.imageMsg(.image) }),
|
|
1417
|
+
// Id 0 is refused loop-side: a `.rejected` record that must
|
|
1418
|
+
// REGENERATE under replay rather than feed.
|
|
1419
|
+
.load_invalid => fx.loadImage(.{ .id = 0, .path = "art/cover.png", .on_result = ImageSessionApp.Effects.imageMsg(.image) }),
|
|
1420
|
+
// A URL that PASSES loop-side validation ("http:hostless"
|
|
1421
|
+
// parses with an http scheme) and is refused INSIDE the worker
|
|
1422
|
+
// (`std.http` cannot build a request without a host — no
|
|
1423
|
+
// network is ever touched): a `.rejected` terminal that is
|
|
1424
|
+
// worker truth, so its journaled record must FEED under replay
|
|
1425
|
+
// rather than be mistaken for regenerable validation.
|
|
1426
|
+
.load_hostless => fx.loadImage(.{ .id = 24, .url = "http:hostless", .on_result = ImageSessionApp.Effects.imageMsg(.image) }),
|
|
1427
|
+
// The chatty subprocess whose line results share the
|
|
1428
|
+
// completion queue with the image terminal.
|
|
1429
|
+
.start_chatty => fx.spawn(.{
|
|
1430
|
+
.key = 31,
|
|
1431
|
+
.argv = &.{ "chatty", "--emit" },
|
|
1432
|
+
.on_line = ImageSessionApp.Effects.lineMsg(.line),
|
|
1433
|
+
}),
|
|
1434
|
+
.arm_chain => model.chain_on_cover = true,
|
|
1435
|
+
.arm_fetch_chain => model.chain_next_fetch = true,
|
|
1436
|
+
.arm_file_chain => model.chain_next_file = true,
|
|
1437
|
+
// A file read on its own key (26 — never colliding with the
|
|
1438
|
+
// image/fetch/spawn probes above).
|
|
1439
|
+
.read_note => fx.readFile(.{ .key = 26, .path = "notes/session.txt", .on_result = ImageSessionApp.Effects.fileMsg(.file) }),
|
|
1440
|
+
// Aimed at the cover's id: against a running load this marks
|
|
1441
|
+
// it cancelled; against a staged start-failure rejection (no
|
|
1442
|
+
// slot exists) it is a no-op and the rejection stands.
|
|
1443
|
+
.cancel_cover => fx.cancel(21),
|
|
1444
|
+
// The cover's key from another family: the effect families
|
|
1445
|
+
// share one key space, so these collide with image id 21.
|
|
1446
|
+
.fetch_cover => fx.fetch(.{ .key = 21, .url = "http://gallery.test/cover.png", .on_response = ImageSessionApp.Effects.responseMsg(.response) }),
|
|
1447
|
+
.spawn_cover => fx.spawn(.{ .key = 21, .argv = &.{"refresh-cover"}, .on_exit = ImageSessionApp.Effects.exitMsg(.exit) }),
|
|
1448
|
+
.line => model.lines_seen += 1,
|
|
1449
|
+
.response => |response| {
|
|
1450
|
+
model.responses += 1;
|
|
1451
|
+
if (response.outcome == .rejected) model.responses_rejected += 1;
|
|
1452
|
+
// The armed poll idiom: answer the cover fetch's terminal
|
|
1453
|
+
// by refetching the SAME key from inside update. Pure
|
|
1454
|
+
// model logic, so record and replay both issue it at
|
|
1455
|
+
// exactly this dispatch — acceptance is the executor-side
|
|
1456
|
+
// agreement under test.
|
|
1457
|
+
if (model.chain_next_fetch and response.key == 21) {
|
|
1458
|
+
model.chain_next_fetch = false;
|
|
1459
|
+
fx.fetch(.{ .key = 21, .url = "http://gallery.test/cover.png", .on_response = ImageSessionApp.Effects.responseMsg(.response) });
|
|
1460
|
+
}
|
|
1461
|
+
},
|
|
1462
|
+
.exit => |exit| {
|
|
1463
|
+
model.exits += 1;
|
|
1464
|
+
if (exit.reason == .rejected) model.exits_rejected += 1;
|
|
1465
|
+
},
|
|
1466
|
+
.file => |result| {
|
|
1467
|
+
model.files += 1;
|
|
1468
|
+
// The armed reload idiom, the fetch chain's file twin.
|
|
1469
|
+
if (model.chain_next_file and result.key == 26) {
|
|
1470
|
+
model.chain_next_file = false;
|
|
1471
|
+
fx.readFile(.{ .key = 26, .path = "notes/session.txt", .on_result = ImageSessionApp.Effects.fileMsg(.file) });
|
|
1472
|
+
}
|
|
1473
|
+
},
|
|
1474
|
+
.image => |result| {
|
|
1475
|
+
// The armed chain: answering the cover's loaded terminal by
|
|
1476
|
+
// starting the NEXT load, from inside update — pure model
|
|
1477
|
+
// logic, so record and replay both issue it at exactly this
|
|
1478
|
+
// dispatch. Whether its completion may be consumed in the
|
|
1479
|
+
// same drain pass is the executor-side question under test.
|
|
1480
|
+
if (model.chain_on_cover and result.id == 21 and result.outcome == .loaded) {
|
|
1481
|
+
model.chain_on_cover = false;
|
|
1482
|
+
fx.loadImage(.{ .id = 25, .path = "art/chained.png", .on_result = ImageSessionApp.Effects.imageMsg(.image) });
|
|
1483
|
+
}
|
|
1484
|
+
model.lines_before_image = model.lines_seen;
|
|
1485
|
+
model.results += 1;
|
|
1486
|
+
switch (result.outcome) {
|
|
1487
|
+
.loaded => model.loaded += 1,
|
|
1488
|
+
.rejected => model.rejected += 1,
|
|
1489
|
+
else => model.failed += 1,
|
|
1490
|
+
}
|
|
1491
|
+
model.last_width = result.width;
|
|
1492
|
+
model.last_height = result.height;
|
|
1493
|
+
const name = @tagName(result.outcome);
|
|
1494
|
+
const len = @min(name.len, model.last_outcome_name.len);
|
|
1495
|
+
@memcpy(model.last_outcome_name[0..len], name[0..len]);
|
|
1496
|
+
model.last_outcome_len = len;
|
|
1497
|
+
},
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
fn imageSessionView(ui: *ImageSessionApp.Ui, model: *const ImageSessionModel) ImageSessionApp.Ui.Node {
|
|
1502
|
+
// The semantic tree carries the image-derived model state, so the
|
|
1503
|
+
// fingerprint checkpoints PIN the Msg stream: a replay that
|
|
1504
|
+
// delivered different outcomes or dimensions mismatches here.
|
|
1505
|
+
return ui.column(.{ .gap = 4, .padding = 8 }, .{
|
|
1506
|
+
ui.text(.{}, ui.fmt("{d} results, {d} loaded, {d} failed, {d} rejected", .{ model.results, model.loaded, model.failed, model.rejected })),
|
|
1507
|
+
ui.text(.{}, ui.fmt("last {s} {d}x{d}", .{ model.outcomeName(), model.last_width, model.last_height })),
|
|
1508
|
+
ui.text(.{}, ui.fmt("{d} lines, {d} before image", .{ model.lines_seen, model.lines_before_image })),
|
|
1509
|
+
ui.text(.{}, ui.fmt("{d}/{d} responses, {d}/{d} exits rejected", .{ model.responses_rejected, model.responses, model.exits_rejected, model.exits })),
|
|
1510
|
+
ui.text(.{}, ui.fmt("{d} files", .{model.files})),
|
|
1511
|
+
});
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
fn imageSessionCommand(name: []const u8) ?ImageSessionMsg {
|
|
1515
|
+
if (std.mem.eql(u8, name, "image.cover")) return .load_cover;
|
|
1516
|
+
if (std.mem.eql(u8, name, "image.cover-ff")) return .load_cover_ff;
|
|
1517
|
+
if (std.mem.eql(u8, name, "image.again")) return .load_cover_again;
|
|
1518
|
+
if (std.mem.eql(u8, name, "image.broken")) return .load_broken;
|
|
1519
|
+
if (std.mem.eql(u8, name, "image.invalid")) return .load_invalid;
|
|
1520
|
+
if (std.mem.eql(u8, name, "image.hostless")) return .load_hostless;
|
|
1521
|
+
if (std.mem.eql(u8, name, "image.chain")) return .arm_chain;
|
|
1522
|
+
if (std.mem.eql(u8, name, "image.fetch-chain")) return .arm_fetch_chain;
|
|
1523
|
+
if (std.mem.eql(u8, name, "image.file-chain")) return .arm_file_chain;
|
|
1524
|
+
if (std.mem.eql(u8, name, "image.read-note")) return .read_note;
|
|
1525
|
+
if (std.mem.eql(u8, name, "image.cancel")) return .cancel_cover;
|
|
1526
|
+
if (std.mem.eql(u8, name, "image.chatty")) return .start_chatty;
|
|
1527
|
+
if (std.mem.eql(u8, name, "image.fetch-cover")) return .fetch_cover;
|
|
1528
|
+
if (std.mem.eql(u8, name, "image.spawn-cover")) return .spawn_cover;
|
|
1529
|
+
return null;
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1532
|
+
const image_session_views = [_]app_manifest.ShellView{
|
|
1533
|
+
.{ .label = image_canvas_label, .kind = .gpu_surface, .fill = true, .gpu_backend = .metal },
|
|
1534
|
+
};
|
|
1535
|
+
const image_session_windows = [_]app_manifest.ShellWindow{.{
|
|
1536
|
+
.label = "main",
|
|
1537
|
+
.title = "Image Session",
|
|
1538
|
+
.width = 400,
|
|
1539
|
+
.height = 300,
|
|
1540
|
+
.views = &image_session_views,
|
|
1541
|
+
}};
|
|
1542
|
+
const image_session_scene: app_manifest.ShellConfig = .{ .windows = &image_session_windows };
|
|
1543
|
+
|
|
1544
|
+
fn imageSessionOptions() ImageSessionApp.Options {
|
|
1545
|
+
return .{
|
|
1546
|
+
.name = "image-session-demo",
|
|
1547
|
+
.scene = image_session_scene,
|
|
1548
|
+
.canvas_label = image_canvas_label,
|
|
1549
|
+
.update_fx = imageSessionUpdate,
|
|
1550
|
+
.view = imageSessionView,
|
|
1551
|
+
.on_command = imageSessionCommand,
|
|
1552
|
+
};
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
fn imageSessionPng(buffer: []u8) []const u8 {
|
|
1556
|
+
var pixels: [6 * 5 * 4]u8 = undefined;
|
|
1557
|
+
var seed: u8 = 23;
|
|
1558
|
+
for (&pixels) |*byte| {
|
|
1559
|
+
byte.* = seed;
|
|
1560
|
+
seed = seed *% 31 +% 7;
|
|
1561
|
+
}
|
|
1562
|
+
var writer = std.Io.Writer.fixed(buffer);
|
|
1563
|
+
canvas.png.writeRgba8(&writer, 6, 5, &pixels) catch unreachable;
|
|
1564
|
+
return writer.buffered();
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
const RecordedImageSession = struct {
|
|
1568
|
+
model: ImageSessionModel,
|
|
1569
|
+
fingerprint: u64,
|
|
1570
|
+
};
|
|
1571
|
+
|
|
1572
|
+
/// Record the image reference session: two loads of the same bytes
|
|
1573
|
+
/// (one blob, two records), one decode failure (its bytes journal
|
|
1574
|
+
/// too), and one loop-side rejection (regenerates at replay).
|
|
1575
|
+
fn recordImageSession(gpa: std.mem.Allocator, buffer: *JournalBuffer, store: *session_blobs.MemoryBlobStore) !RecordedImageSession {
|
|
1576
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
1577
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
1578
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
1579
|
+
recorder.blob_sink = store.sink();
|
|
1580
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "image-session-demo", .window_width = 400, .window_height = 300 });
|
|
1581
|
+
|
|
1582
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
1583
|
+
defer harness.destroy(gpa);
|
|
1584
|
+
harness.null_platform.gpu_surfaces = true;
|
|
1585
|
+
harness.null_platform.image_decode = true;
|
|
1586
|
+
harness.runtime.options.session_recorder = recorder;
|
|
1587
|
+
|
|
1588
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
1589
|
+
defer gpa.destroy(app_state);
|
|
1590
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
1591
|
+
defer app_state.deinit();
|
|
1592
|
+
app_state.effects.executor = .fake;
|
|
1593
|
+
const app = app_state.app();
|
|
1594
|
+
|
|
1595
|
+
try harness.start(app);
|
|
1596
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
1597
|
+
.label = image_canvas_label,
|
|
1598
|
+
.size = geometry.SizeF.init(400, 300),
|
|
1599
|
+
.scale_factor = 2,
|
|
1600
|
+
.frame_index = 1,
|
|
1601
|
+
.timestamp_ns = 1_000_000,
|
|
1602
|
+
} });
|
|
1603
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
1604
|
+
|
|
1605
|
+
var png_buffer: [4096]u8 = undefined;
|
|
1606
|
+
const png = imageSessionPng(&png_buffer);
|
|
1607
|
+
|
|
1608
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.cover", .window_id = 1 } });
|
|
1609
|
+
try app_state.effects.feedImageBytes(21, png);
|
|
1610
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
1611
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
1612
|
+
|
|
1613
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.again", .window_id = 1 } });
|
|
1614
|
+
try app_state.effects.feedImageBytes(22, png);
|
|
1615
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
1616
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
1617
|
+
|
|
1618
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.broken", .window_id = 1 } });
|
|
1619
|
+
try app_state.effects.feedImageBytes(23, "these bytes are no image");
|
|
1620
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
1621
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
1622
|
+
|
|
1623
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.invalid", .window_id = 1 } });
|
|
1624
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
1625
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
1626
|
+
|
|
1627
|
+
// Both decoded loads are live in the recording runtime.
|
|
1628
|
+
try std.testing.expect(harness.runtime.registeredCanvasImage(21) != null);
|
|
1629
|
+
try std.testing.expect(harness.runtime.registeredCanvasImage(22) != null);
|
|
1630
|
+
|
|
1631
|
+
recorder.finish();
|
|
1632
|
+
try std.testing.expect(!recorder.failed);
|
|
1633
|
+
return .{
|
|
1634
|
+
.model = app_state.model,
|
|
1635
|
+
.fingerprint = harness.runtime.sessionStateFingerprint(),
|
|
1636
|
+
};
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
test "image loads record into the blob store (deduplicated) and replay byte-identical, offline" {
|
|
1640
|
+
const gpa = std.testing.allocator;
|
|
1641
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
1642
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
1643
|
+
buffer.len = 0;
|
|
1644
|
+
var store = session_blobs.MemoryBlobStore.init(gpa);
|
|
1645
|
+
defer store.deinit();
|
|
1646
|
+
|
|
1647
|
+
const recorded = try recordImageSession(gpa, buffer, &store);
|
|
1648
|
+
try std.testing.expectEqual(@as(u32, 4), recorded.model.results);
|
|
1649
|
+
try std.testing.expectEqual(@as(u32, 2), recorded.model.loaded);
|
|
1650
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.failed);
|
|
1651
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.rejected);
|
|
1652
|
+
|
|
1653
|
+
// Same bytes twice = ONE blob; the broken bytes are a second.
|
|
1654
|
+
try std.testing.expectEqual(@as(usize, 2), store.count);
|
|
1655
|
+
try std.testing.expectEqual(@as(usize, 1), store.dedup_hits);
|
|
1656
|
+
|
|
1657
|
+
// Replay into a fresh app: the journal plus the blob store are the
|
|
1658
|
+
// WHOLE world — no files are read (the fake paths never existed),
|
|
1659
|
+
// no network is touched, and every fingerprint checkpoint matches.
|
|
1660
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
1661
|
+
defer harness.destroy(gpa);
|
|
1662
|
+
harness.null_platform.gpu_surfaces = true;
|
|
1663
|
+
// NO manual decoder enablement here: the replay side installs its
|
|
1664
|
+
// decode seam through the SAME call `runSessionReplay` makes, in
|
|
1665
|
+
// the arm this codec-less test tier reaches (the strict test-PNG
|
|
1666
|
+
// fallback). Replay tests once flipped `image_decode` by hand while
|
|
1667
|
+
// the production runner installed no decoder at all — this test
|
|
1668
|
+
// holds the runner's construction path itself to the re-register
|
|
1669
|
+
// proof below.
|
|
1670
|
+
platform.installHeadlessImageCodec("null", &harness.null_platform, &harness.runtime.options.platform.services);
|
|
1671
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
1672
|
+
defer gpa.destroy(app_state);
|
|
1673
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
1674
|
+
defer app_state.deinit();
|
|
1675
|
+
|
|
1676
|
+
const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
1677
|
+
.verify = true,
|
|
1678
|
+
.require_same_platform = false,
|
|
1679
|
+
.blobs = store.source(),
|
|
1680
|
+
});
|
|
1681
|
+
try std.testing.expect(report.ok());
|
|
1682
|
+
try std.testing.expect(report.checkpoints_verified > 0);
|
|
1683
|
+
// Two loaded terminals and the decode failure feed; the rejection
|
|
1684
|
+
// regenerates from the same loop-side validation.
|
|
1685
|
+
try std.testing.expectEqual(@as(u64, 3), report.effects_fed);
|
|
1686
|
+
try std.testing.expectEqual(@as(u64, 1), report.effects_skipped);
|
|
1687
|
+
try std.testing.expectEqualDeep(recorded.model, app_state.model);
|
|
1688
|
+
try std.testing.expectEqual(recorded.fingerprint, harness.runtime.sessionStateFingerprint());
|
|
1689
|
+
|
|
1690
|
+
// The replayed runtime re-registered the recorded bytes: the
|
|
1691
|
+
// pixels are drawable offline, straight from the blob store.
|
|
1692
|
+
const replay_registered = harness.runtime.registeredCanvasImage(21).?;
|
|
1693
|
+
try std.testing.expectEqual(@as(usize, 6), replay_registered.width);
|
|
1694
|
+
try std.testing.expectEqual(@as(usize, 5), replay_registered.height);
|
|
1695
|
+
try std.testing.expect(harness.runtime.registeredCanvasImage(22) != null);
|
|
1696
|
+
try std.testing.expect(harness.runtime.registeredCanvasImage(23) == null);
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
/// Record the undelivered-window reference session: load id 21, feed
|
|
1700
|
+
/// its bytes (terminal queued, slot `.draining`, delivery still
|
|
1701
|
+
/// ahead), then — before any drain — an UNRELATED dispatch loads id
|
|
1702
|
+
/// 21 again. That reload must reject on BOTH sides: under replay the
|
|
1703
|
+
/// first request is still a parked `.running` fake at that dispatch
|
|
1704
|
+
/// (its recorded terminal feeds at the journaled delivery position),
|
|
1705
|
+
/// so a live executor that accepted the reload would journal a
|
|
1706
|
+
/// different Msg stream than replay derives — accepted live, rejected
|
|
1707
|
+
/// replayed, with the extra regenerated rejection diverging the model.
|
|
1708
|
+
fn recordWindowReloadSession(gpa: std.mem.Allocator, buffer: *JournalBuffer, store: *session_blobs.MemoryBlobStore) !RecordedImageSession {
|
|
1709
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
1710
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
1711
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
1712
|
+
recorder.blob_sink = store.sink();
|
|
1713
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "image-session-demo", .window_width = 400, .window_height = 300 });
|
|
1714
|
+
|
|
1715
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
1716
|
+
defer harness.destroy(gpa);
|
|
1717
|
+
harness.null_platform.gpu_surfaces = true;
|
|
1718
|
+
harness.null_platform.image_decode = true;
|
|
1719
|
+
harness.runtime.options.session_recorder = recorder;
|
|
1720
|
+
|
|
1721
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
1722
|
+
defer gpa.destroy(app_state);
|
|
1723
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
1724
|
+
defer app_state.deinit();
|
|
1725
|
+
app_state.effects.executor = .fake;
|
|
1726
|
+
const app = app_state.app();
|
|
1727
|
+
|
|
1728
|
+
try harness.start(app);
|
|
1729
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
1730
|
+
.label = image_canvas_label,
|
|
1731
|
+
.size = geometry.SizeF.init(400, 300),
|
|
1732
|
+
.scale_factor = 2,
|
|
1733
|
+
.frame_index = 1,
|
|
1734
|
+
.timestamp_ns = 1_000_000,
|
|
1735
|
+
} });
|
|
1736
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
1737
|
+
|
|
1738
|
+
var png_buffer: [4096]u8 = undefined;
|
|
1739
|
+
const png = imageSessionPng(&png_buffer);
|
|
1740
|
+
|
|
1741
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.cover", .window_id = 1 } });
|
|
1742
|
+
try app_state.effects.feedImageBytes(21, png);
|
|
1743
|
+
// NO wake between the feed and the next command: the terminal is
|
|
1744
|
+
// queued but undelivered when the reload dispatch lands — the
|
|
1745
|
+
// window under test.
|
|
1746
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.cover", .window_id = 1 } });
|
|
1747
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
1748
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
1749
|
+
|
|
1750
|
+
recorder.finish();
|
|
1751
|
+
try std.testing.expect(!recorder.failed);
|
|
1752
|
+
return .{
|
|
1753
|
+
.model = app_state.model,
|
|
1754
|
+
.fingerprint = harness.runtime.sessionStateFingerprint(),
|
|
1755
|
+
};
|
|
1756
|
+
}
|
|
1757
|
+
|
|
1758
|
+
test "a reload inside the undelivered terminal window rejects identically live and under replay" {
|
|
1759
|
+
const gpa = std.testing.allocator;
|
|
1760
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
1761
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
1762
|
+
buffer.len = 0;
|
|
1763
|
+
var store = session_blobs.MemoryBlobStore.init(gpa);
|
|
1764
|
+
defer store.deinit();
|
|
1765
|
+
|
|
1766
|
+
const recorded = try recordWindowReloadSession(gpa, buffer, &store);
|
|
1767
|
+
// The live side already rejected the in-window reload: one loaded
|
|
1768
|
+
// terminal for the first request, one rejection for the reload.
|
|
1769
|
+
try std.testing.expectEqual(@as(u32, 2), recorded.model.results);
|
|
1770
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.loaded);
|
|
1771
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.rejected);
|
|
1772
|
+
|
|
1773
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
1774
|
+
defer harness.destroy(gpa);
|
|
1775
|
+
harness.null_platform.gpu_surfaces = true;
|
|
1776
|
+
platform.installHeadlessImageCodec("null", &harness.null_platform, &harness.runtime.options.platform.services);
|
|
1777
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
1778
|
+
defer gpa.destroy(app_state);
|
|
1779
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
1780
|
+
defer app_state.deinit();
|
|
1781
|
+
|
|
1782
|
+
const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
1783
|
+
.verify = true,
|
|
1784
|
+
.require_same_platform = false,
|
|
1785
|
+
.blobs = store.source(),
|
|
1786
|
+
});
|
|
1787
|
+
try std.testing.expect(report.ok());
|
|
1788
|
+
try std.testing.expect(report.checkpoints_verified > 0);
|
|
1789
|
+
// The loaded terminal feeds; the in-window rejection regenerates
|
|
1790
|
+
// from the replayed duplicate check itself — the agreement pin.
|
|
1791
|
+
try std.testing.expectEqual(@as(u64, 1), report.effects_fed);
|
|
1792
|
+
try std.testing.expectEqual(@as(u64, 1), report.effects_skipped);
|
|
1793
|
+
try std.testing.expectEqualDeep(recorded.model, app_state.model);
|
|
1794
|
+
try std.testing.expectEqual(recorded.fingerprint, harness.runtime.sessionStateFingerprint());
|
|
1795
|
+
}
|
|
1796
|
+
|
|
1797
|
+
/// Record the cross-family window reference session: a FETCH of key 21
|
|
1798
|
+
/// completes (terminal queued, slot `.draining`, delivery still ahead),
|
|
1799
|
+
/// then — before any drain — a dispatch loads IMAGE id 21. The families
|
|
1800
|
+
/// share one key space, so that load must reject on BOTH sides: under
|
|
1801
|
+
/// replay the fetch is still a parked `.running` fake at that dispatch
|
|
1802
|
+
/// (its recorded response feeds at the journaled delivery position),
|
|
1803
|
+
/// and the parked fake is what rejects the image load there.
|
|
1804
|
+
fn recordCrossFamilyWindowSession(gpa: std.mem.Allocator, buffer: *JournalBuffer, store: *session_blobs.MemoryBlobStore) !RecordedImageSession {
|
|
1805
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
1806
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
1807
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
1808
|
+
recorder.blob_sink = store.sink();
|
|
1809
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "image-session-demo", .window_width = 400, .window_height = 300 });
|
|
1810
|
+
|
|
1811
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
1812
|
+
defer harness.destroy(gpa);
|
|
1813
|
+
harness.null_platform.gpu_surfaces = true;
|
|
1814
|
+
harness.null_platform.image_decode = true;
|
|
1815
|
+
harness.runtime.options.session_recorder = recorder;
|
|
1816
|
+
|
|
1817
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
1818
|
+
defer gpa.destroy(app_state);
|
|
1819
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
1820
|
+
defer app_state.deinit();
|
|
1821
|
+
app_state.effects.executor = .fake;
|
|
1822
|
+
const app = app_state.app();
|
|
1823
|
+
|
|
1824
|
+
try harness.start(app);
|
|
1825
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
1826
|
+
.label = image_canvas_label,
|
|
1827
|
+
.size = geometry.SizeF.init(400, 300),
|
|
1828
|
+
.scale_factor = 2,
|
|
1829
|
+
.frame_index = 1,
|
|
1830
|
+
.timestamp_ns = 1_000_000,
|
|
1831
|
+
} });
|
|
1832
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
1833
|
+
|
|
1834
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.fetch-cover", .window_id = 1 } });
|
|
1835
|
+
try app_state.effects.feedResponse(21, 200, "cover-bytes");
|
|
1836
|
+
// NO wake between the feed and the next command: the fetch terminal
|
|
1837
|
+
// is queued but undelivered when the image load lands.
|
|
1838
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.cover", .window_id = 1 } });
|
|
1839
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
1840
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
1841
|
+
|
|
1842
|
+
recorder.finish();
|
|
1843
|
+
try std.testing.expect(!recorder.failed);
|
|
1844
|
+
return .{
|
|
1845
|
+
.model = app_state.model,
|
|
1846
|
+
.fingerprint = harness.runtime.sessionStateFingerprint(),
|
|
1847
|
+
};
|
|
1848
|
+
}
|
|
1849
|
+
|
|
1850
|
+
test "an image load inside another family's undelivered window rejects identically live and under replay" {
|
|
1851
|
+
const gpa = std.testing.allocator;
|
|
1852
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
1853
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
1854
|
+
buffer.len = 0;
|
|
1855
|
+
var store = session_blobs.MemoryBlobStore.init(gpa);
|
|
1856
|
+
defer store.deinit();
|
|
1857
|
+
|
|
1858
|
+
const recorded = try recordCrossFamilyWindowSession(gpa, buffer, &store);
|
|
1859
|
+
// The live side already rejected the in-window image load: one ok
|
|
1860
|
+
// response for the fetch, one regenerable rejection for the load.
|
|
1861
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.responses);
|
|
1862
|
+
try std.testing.expectEqual(@as(u32, 0), recorded.model.responses_rejected);
|
|
1863
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.results);
|
|
1864
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.rejected);
|
|
1865
|
+
try std.testing.expectEqual(@as(u32, 0), recorded.model.loaded);
|
|
1866
|
+
|
|
1867
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
1868
|
+
defer harness.destroy(gpa);
|
|
1869
|
+
harness.null_platform.gpu_surfaces = true;
|
|
1870
|
+
platform.installHeadlessImageCodec("null", &harness.null_platform, &harness.runtime.options.platform.services);
|
|
1871
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
1872
|
+
defer gpa.destroy(app_state);
|
|
1873
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
1874
|
+
defer app_state.deinit();
|
|
1875
|
+
|
|
1876
|
+
const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
1877
|
+
.verify = true,
|
|
1878
|
+
.require_same_platform = false,
|
|
1879
|
+
.blobs = store.source(),
|
|
1880
|
+
});
|
|
1881
|
+
try std.testing.expect(report.ok());
|
|
1882
|
+
try std.testing.expect(report.checkpoints_verified > 0);
|
|
1883
|
+
// The response feeds; the in-window image rejection regenerates
|
|
1884
|
+
// from the replayed cross-family occupancy check itself.
|
|
1885
|
+
try std.testing.expectEqual(@as(u64, 1), report.effects_fed);
|
|
1886
|
+
try std.testing.expectEqual(@as(u64, 1), report.effects_skipped);
|
|
1887
|
+
try std.testing.expectEqualDeep(recorded.model, app_state.model);
|
|
1888
|
+
try std.testing.expectEqual(recorded.fingerprint, harness.runtime.sessionStateFingerprint());
|
|
1889
|
+
}
|
|
1890
|
+
|
|
1891
|
+
/// The mirror of `recordCrossFamilyWindowSession`: IMAGE id 21 completes
|
|
1892
|
+
/// (terminal queued, undelivered), then — before any drain — a dispatch
|
|
1893
|
+
/// SPAWNS key 21. The spawn must reject on both sides for the same
|
|
1894
|
+
/// reason: under replay the image request is still a parked `.running`
|
|
1895
|
+
/// fake until its recorded terminal feeds.
|
|
1896
|
+
fn recordSpawnInImageWindowSession(gpa: std.mem.Allocator, buffer: *JournalBuffer, store: *session_blobs.MemoryBlobStore) !RecordedImageSession {
|
|
1897
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
1898
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
1899
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
1900
|
+
recorder.blob_sink = store.sink();
|
|
1901
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "image-session-demo", .window_width = 400, .window_height = 300 });
|
|
1902
|
+
|
|
1903
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
1904
|
+
defer harness.destroy(gpa);
|
|
1905
|
+
harness.null_platform.gpu_surfaces = true;
|
|
1906
|
+
harness.null_platform.image_decode = true;
|
|
1907
|
+
harness.runtime.options.session_recorder = recorder;
|
|
1908
|
+
|
|
1909
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
1910
|
+
defer gpa.destroy(app_state);
|
|
1911
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
1912
|
+
defer app_state.deinit();
|
|
1913
|
+
app_state.effects.executor = .fake;
|
|
1914
|
+
const app = app_state.app();
|
|
1915
|
+
|
|
1916
|
+
try harness.start(app);
|
|
1917
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
1918
|
+
.label = image_canvas_label,
|
|
1919
|
+
.size = geometry.SizeF.init(400, 300),
|
|
1920
|
+
.scale_factor = 2,
|
|
1921
|
+
.frame_index = 1,
|
|
1922
|
+
.timestamp_ns = 1_000_000,
|
|
1923
|
+
} });
|
|
1924
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
1925
|
+
|
|
1926
|
+
var png_buffer: [4096]u8 = undefined;
|
|
1927
|
+
const png = imageSessionPng(&png_buffer);
|
|
1928
|
+
|
|
1929
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.cover", .window_id = 1 } });
|
|
1930
|
+
try app_state.effects.feedImageBytes(21, png);
|
|
1931
|
+
// NO wake between the feed and the next command: the image terminal
|
|
1932
|
+
// is queued but undelivered when the spawn lands.
|
|
1933
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.spawn-cover", .window_id = 1 } });
|
|
1934
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
1935
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
1936
|
+
|
|
1937
|
+
recorder.finish();
|
|
1938
|
+
try std.testing.expect(!recorder.failed);
|
|
1939
|
+
return .{
|
|
1940
|
+
.model = app_state.model,
|
|
1941
|
+
.fingerprint = harness.runtime.sessionStateFingerprint(),
|
|
1942
|
+
};
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
test "a spawn inside an image's undelivered window rejects identically live and under replay" {
|
|
1946
|
+
const gpa = std.testing.allocator;
|
|
1947
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
1948
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
1949
|
+
buffer.len = 0;
|
|
1950
|
+
var store = session_blobs.MemoryBlobStore.init(gpa);
|
|
1951
|
+
defer store.deinit();
|
|
1952
|
+
|
|
1953
|
+
const recorded = try recordSpawnInImageWindowSession(gpa, buffer, &store);
|
|
1954
|
+
// The live side already rejected the in-window spawn: one loaded
|
|
1955
|
+
// image terminal, one regenerable spawn rejection.
|
|
1956
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.results);
|
|
1957
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.loaded);
|
|
1958
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.exits);
|
|
1959
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.exits_rejected);
|
|
1960
|
+
|
|
1961
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
1962
|
+
defer harness.destroy(gpa);
|
|
1963
|
+
harness.null_platform.gpu_surfaces = true;
|
|
1964
|
+
platform.installHeadlessImageCodec("null", &harness.null_platform, &harness.runtime.options.platform.services);
|
|
1965
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
1966
|
+
defer gpa.destroy(app_state);
|
|
1967
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
1968
|
+
defer app_state.deinit();
|
|
1969
|
+
|
|
1970
|
+
const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
1971
|
+
.verify = true,
|
|
1972
|
+
.require_same_platform = false,
|
|
1973
|
+
.blobs = store.source(),
|
|
1974
|
+
});
|
|
1975
|
+
try std.testing.expect(report.ok());
|
|
1976
|
+
try std.testing.expect(report.checkpoints_verified > 0);
|
|
1977
|
+
// The loaded terminal feeds; the in-window spawn rejection
|
|
1978
|
+
// regenerates from the replayed occupancy check itself.
|
|
1979
|
+
try std.testing.expectEqual(@as(u64, 1), report.effects_fed);
|
|
1980
|
+
try std.testing.expectEqual(@as(u64, 1), report.effects_skipped);
|
|
1981
|
+
try std.testing.expectEqualDeep(recorded.model, app_state.model);
|
|
1982
|
+
try std.testing.expectEqual(recorded.fingerprint, harness.runtime.sessionStateFingerprint());
|
|
1983
|
+
}
|
|
1984
|
+
|
|
1985
|
+
/// Record the start-failure window reference session: the channel
|
|
1986
|
+
/// allocator fails exactly at image 21's staged source buffer, so the
|
|
1987
|
+
/// load is refused with executor truth (a NON-regenerating staged
|
|
1988
|
+
/// rejection) before any slot is claimed — then, before any drain, a
|
|
1989
|
+
/// second dispatch reloads id 21. The reload must reject on BOTH
|
|
1990
|
+
/// sides: under replay the failed request allocates its own buffer
|
|
1991
|
+
/// and PARKS (the fake executor never touches the failing seam), so
|
|
1992
|
+
/// the id is held there until the journaled terminal feeds at the
|
|
1993
|
+
/// recorded delivery position — live admission holds it through the
|
|
1994
|
+
/// staged window for the same span.
|
|
1995
|
+
/// Fails exactly one allocation — the next one matching the staged
|
|
1996
|
+
/// image source buffer's distinctive size — and delegates everything
|
|
1997
|
+
/// else. `std.testing.FailingAllocator` fails every allocation from
|
|
1998
|
+
/// its fail index on, which would take the same dispatch's view
|
|
1999
|
+
/// rebuild down with the load; this failure is surgical.
|
|
2000
|
+
const ImageBufferFailingAllocator = struct {
|
|
2001
|
+
backing: std.mem.Allocator,
|
|
2002
|
+
armed: bool = false,
|
|
2003
|
+
|
|
2004
|
+
const vtable: std.mem.Allocator.VTable = .{
|
|
2005
|
+
.alloc = alloc,
|
|
2006
|
+
.resize = resize,
|
|
2007
|
+
.remap = remap,
|
|
2008
|
+
.free = free,
|
|
2009
|
+
};
|
|
2010
|
+
|
|
2011
|
+
fn allocator(self: *ImageBufferFailingAllocator) std.mem.Allocator {
|
|
2012
|
+
return .{ .ptr = self, .vtable = &vtable };
|
|
2013
|
+
}
|
|
2014
|
+
|
|
2015
|
+
fn alloc(ptr: *anyopaque, len: usize, alignment: std.mem.Alignment, ret_addr: usize) ?[*]u8 {
|
|
2016
|
+
const self: *ImageBufferFailingAllocator = @ptrCast(@alignCast(ptr));
|
|
2017
|
+
if (self.armed and len == effects_mod.max_effect_image_bytes + 1) {
|
|
2018
|
+
self.armed = false;
|
|
2019
|
+
return null;
|
|
2020
|
+
}
|
|
2021
|
+
return self.backing.vtable.alloc(self.backing.ptr, len, alignment, ret_addr);
|
|
2022
|
+
}
|
|
2023
|
+
|
|
2024
|
+
fn resize(ptr: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) bool {
|
|
2025
|
+
const self: *ImageBufferFailingAllocator = @ptrCast(@alignCast(ptr));
|
|
2026
|
+
return self.backing.vtable.resize(self.backing.ptr, memory, alignment, new_len, ret_addr);
|
|
2027
|
+
}
|
|
2028
|
+
|
|
2029
|
+
fn remap(ptr: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) ?[*]u8 {
|
|
2030
|
+
const self: *ImageBufferFailingAllocator = @ptrCast(@alignCast(ptr));
|
|
2031
|
+
return self.backing.vtable.remap(self.backing.ptr, memory, alignment, new_len, ret_addr);
|
|
2032
|
+
}
|
|
2033
|
+
|
|
2034
|
+
fn free(ptr: *anyopaque, memory: []u8, alignment: std.mem.Alignment, ret_addr: usize) void {
|
|
2035
|
+
const self: *ImageBufferFailingAllocator = @ptrCast(@alignCast(ptr));
|
|
2036
|
+
self.backing.vtable.free(self.backing.ptr, memory, alignment, ret_addr);
|
|
2037
|
+
}
|
|
2038
|
+
};
|
|
2039
|
+
|
|
2040
|
+
fn recordStartFailureWindowSession(gpa: std.mem.Allocator, buffer: *JournalBuffer, store: *session_blobs.MemoryBlobStore) !RecordedImageSession {
|
|
2041
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
2042
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
2043
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
2044
|
+
recorder.blob_sink = store.sink();
|
|
2045
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "image-session-demo", .window_width = 400, .window_height = 300 });
|
|
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.image_decode = true;
|
|
2051
|
+
harness.runtime.options.session_recorder = recorder;
|
|
2052
|
+
|
|
2053
|
+
var failing: ImageBufferFailingAllocator = .{ .backing = std.heap.page_allocator };
|
|
2054
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2055
|
+
defer gpa.destroy(app_state);
|
|
2056
|
+
app_state.* = ImageSessionApp.init(failing.allocator(), .{}, imageSessionOptions());
|
|
2057
|
+
defer app_state.deinit();
|
|
2058
|
+
app_state.effects.executor = .fake;
|
|
2059
|
+
const app = app_state.app();
|
|
2060
|
+
|
|
2061
|
+
try harness.start(app);
|
|
2062
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
2063
|
+
.label = image_canvas_label,
|
|
2064
|
+
.size = geometry.SizeF.init(400, 300),
|
|
2065
|
+
.scale_factor = 2,
|
|
2066
|
+
.frame_index = 1,
|
|
2067
|
+
.timestamp_ns = 1_000_000,
|
|
2068
|
+
} });
|
|
2069
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2070
|
+
|
|
2071
|
+
// Fail exactly the staged source buffer's allocation inside the
|
|
2072
|
+
// loadImage this dispatch issues.
|
|
2073
|
+
failing.armed = true;
|
|
2074
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.cover", .window_id = 1 } });
|
|
2075
|
+
try std.testing.expect(!failing.armed);
|
|
2076
|
+
// NO wake before the reload: the staged rejection is undelivered
|
|
2077
|
+
// when the second load lands — the window under test.
|
|
2078
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.cover", .window_id = 1 } });
|
|
2079
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
2080
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2081
|
+
|
|
2082
|
+
recorder.finish();
|
|
2083
|
+
try std.testing.expect(!recorder.failed);
|
|
2084
|
+
return .{
|
|
2085
|
+
.model = app_state.model,
|
|
2086
|
+
.fingerprint = harness.runtime.sessionStateFingerprint(),
|
|
2087
|
+
};
|
|
2088
|
+
}
|
|
2089
|
+
|
|
2090
|
+
test "a reload inside a start-failure's staged window rejects identically live and under replay" {
|
|
2091
|
+
const gpa = std.testing.allocator;
|
|
2092
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
2093
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
2094
|
+
buffer.len = 0;
|
|
2095
|
+
var store = session_blobs.MemoryBlobStore.init(gpa);
|
|
2096
|
+
defer store.deinit();
|
|
2097
|
+
|
|
2098
|
+
const recorded = try recordStartFailureWindowSession(gpa, buffer, &store);
|
|
2099
|
+
// The live side delivered two rejections: the start failure's own
|
|
2100
|
+
// terminal (executor truth) and the in-window reload's refusal.
|
|
2101
|
+
try std.testing.expectEqual(@as(u32, 2), recorded.model.results);
|
|
2102
|
+
try std.testing.expectEqual(@as(u32, 2), recorded.model.rejected);
|
|
2103
|
+
try std.testing.expectEqual(@as(u32, 0), recorded.model.loaded);
|
|
2104
|
+
|
|
2105
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2106
|
+
defer harness.destroy(gpa);
|
|
2107
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2108
|
+
platform.installHeadlessImageCodec("null", &harness.null_platform, &harness.runtime.options.platform.services);
|
|
2109
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2110
|
+
defer gpa.destroy(app_state);
|
|
2111
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
2112
|
+
defer app_state.deinit();
|
|
2113
|
+
|
|
2114
|
+
const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
2115
|
+
.verify = true,
|
|
2116
|
+
.require_same_platform = false,
|
|
2117
|
+
.blobs = store.source(),
|
|
2118
|
+
});
|
|
2119
|
+
try std.testing.expect(report.ok());
|
|
2120
|
+
try std.testing.expect(report.checkpoints_verified > 0);
|
|
2121
|
+
// The start failure's terminal FEEDS (the replayed request parked
|
|
2122
|
+
// instead of failing); the reload's refusal regenerates from the
|
|
2123
|
+
// replayed occupancy check itself.
|
|
2124
|
+
try std.testing.expectEqual(@as(u64, 1), report.effects_fed);
|
|
2125
|
+
try std.testing.expectEqual(@as(u64, 1), report.effects_skipped);
|
|
2126
|
+
try std.testing.expectEqualDeep(recorded.model, app_state.model);
|
|
2127
|
+
try std.testing.expectEqual(recorded.fingerprint, harness.runtime.sessionStateFingerprint());
|
|
2128
|
+
}
|
|
2129
|
+
|
|
2130
|
+
/// Record the cancelled-start-failure reference session: the channel
|
|
2131
|
+
/// allocator fails at image 21's staged source buffer, so the load is
|
|
2132
|
+
/// refused with executor truth (a staged `.rejected`, NO slot claimed)
|
|
2133
|
+
/// — then, before the staged terminal drains, the app cancels id 21.
|
|
2134
|
+
/// Live, the cancel finds nothing to mark (no slot exists) and is a
|
|
2135
|
+
/// no-op: the session delivers and journals `.rejected`. Under replay
|
|
2136
|
+
/// the same load PARKS as a running fake slot (the fake executor
|
|
2137
|
+
/// never touches the failing seam), so the same cancel finds a slot
|
|
2138
|
+
/// to mark — the timing-shifted mark must not rewrite the journaled
|
|
2139
|
+
/// terminal when it feeds.
|
|
2140
|
+
fn recordCancelledStartFailureSession(gpa: std.mem.Allocator, buffer: *JournalBuffer, store: *session_blobs.MemoryBlobStore) !RecordedImageSession {
|
|
2141
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
2142
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
2143
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
2144
|
+
recorder.blob_sink = store.sink();
|
|
2145
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "image-session-demo", .window_width = 400, .window_height = 300 });
|
|
2146
|
+
|
|
2147
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2148
|
+
defer harness.destroy(gpa);
|
|
2149
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2150
|
+
harness.null_platform.image_decode = true;
|
|
2151
|
+
harness.runtime.options.session_recorder = recorder;
|
|
2152
|
+
|
|
2153
|
+
var failing: ImageBufferFailingAllocator = .{ .backing = std.heap.page_allocator };
|
|
2154
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2155
|
+
defer gpa.destroy(app_state);
|
|
2156
|
+
app_state.* = ImageSessionApp.init(failing.allocator(), .{}, imageSessionOptions());
|
|
2157
|
+
defer app_state.deinit();
|
|
2158
|
+
app_state.effects.executor = .fake;
|
|
2159
|
+
const app = app_state.app();
|
|
2160
|
+
|
|
2161
|
+
try harness.start(app);
|
|
2162
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
2163
|
+
.label = image_canvas_label,
|
|
2164
|
+
.size = geometry.SizeF.init(400, 300),
|
|
2165
|
+
.scale_factor = 2,
|
|
2166
|
+
.frame_index = 1,
|
|
2167
|
+
.timestamp_ns = 1_000_000,
|
|
2168
|
+
} });
|
|
2169
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2170
|
+
|
|
2171
|
+
// Fail exactly the staged source buffer's allocation inside the
|
|
2172
|
+
// loadImage this dispatch issues.
|
|
2173
|
+
failing.armed = true;
|
|
2174
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.cover", .window_id = 1 } });
|
|
2175
|
+
try std.testing.expect(!failing.armed);
|
|
2176
|
+
// The cancel lands while the staged rejection is still
|
|
2177
|
+
// undelivered — the window under test.
|
|
2178
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.cancel", .window_id = 1 } });
|
|
2179
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
2180
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2181
|
+
|
|
2182
|
+
recorder.finish();
|
|
2183
|
+
try std.testing.expect(!recorder.failed);
|
|
2184
|
+
return .{
|
|
2185
|
+
.model = app_state.model,
|
|
2186
|
+
.fingerprint = harness.runtime.sessionStateFingerprint(),
|
|
2187
|
+
};
|
|
2188
|
+
}
|
|
2189
|
+
|
|
2190
|
+
test "a cancel that lost to a start failure live cannot rewrite the fed rejection under replay" {
|
|
2191
|
+
const gpa = std.testing.allocator;
|
|
2192
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
2193
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
2194
|
+
buffer.len = 0;
|
|
2195
|
+
var store = session_blobs.MemoryBlobStore.init(gpa);
|
|
2196
|
+
defer store.deinit();
|
|
2197
|
+
|
|
2198
|
+
const recorded = try recordCancelledStartFailureSession(gpa, buffer, &store);
|
|
2199
|
+
// Live truth: one terminal, `.rejected` — the cancel was a no-op.
|
|
2200
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.results);
|
|
2201
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.rejected);
|
|
2202
|
+
try std.testing.expectEqual(@as(u32, 0), recorded.model.failed);
|
|
2203
|
+
try std.testing.expectEqual(@as(u32, 0), recorded.model.loaded);
|
|
2204
|
+
|
|
2205
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2206
|
+
defer harness.destroy(gpa);
|
|
2207
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2208
|
+
platform.installHeadlessImageCodec("null", &harness.null_platform, &harness.runtime.options.platform.services);
|
|
2209
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2210
|
+
defer gpa.destroy(app_state);
|
|
2211
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
2212
|
+
defer app_state.deinit();
|
|
2213
|
+
|
|
2214
|
+
const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
2215
|
+
.verify = true,
|
|
2216
|
+
.require_same_platform = false,
|
|
2217
|
+
.blobs = store.source(),
|
|
2218
|
+
});
|
|
2219
|
+
// The start failure's terminal FEEDS (the replayed request parked
|
|
2220
|
+
// instead of failing) — and feeds VERBATIM: the replayed cancel
|
|
2221
|
+
// marked the parked fake slot, but a fed terminal is executor
|
|
2222
|
+
// truth, so the journaled `.rejected` must land untouched.
|
|
2223
|
+
try std.testing.expectEqualDeep(recorded.model, app_state.model);
|
|
2224
|
+
try std.testing.expectEqual(recorded.fingerprint, harness.runtime.sessionStateFingerprint());
|
|
2225
|
+
try std.testing.expectEqual(@as(u64, 1), report.effects_fed);
|
|
2226
|
+
try std.testing.expect(report.ok());
|
|
2227
|
+
try std.testing.expect(report.checkpoints_verified > 0);
|
|
2228
|
+
}
|
|
2229
|
+
|
|
2230
|
+
/// Record the fire-and-forget start-failure reference session: the
|
|
2231
|
+
/// channel allocator fails at image 21's staged source buffer inside
|
|
2232
|
+
/// a load that carries NO handler. The failure is executor truth, so
|
|
2233
|
+
/// it must journal and hold the id through its staged window even
|
|
2234
|
+
/// though no Msg will ever deliver from it: under replay the same
|
|
2235
|
+
/// load allocates its own buffer and PARKS (the fake executor never
|
|
2236
|
+
/// touches the failing seam), and ONLY the journaled terminal's feed
|
|
2237
|
+
/// retires that parked fake — with no record, the id and a slot stay
|
|
2238
|
+
/// occupied replay-side forever and the post-drain reload diverges.
|
|
2239
|
+
fn recordFireAndForgetStartFailureSession(gpa: std.mem.Allocator, buffer: *JournalBuffer, store: *session_blobs.MemoryBlobStore) !RecordedImageSession {
|
|
2240
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
2241
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
2242
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
2243
|
+
recorder.blob_sink = store.sink();
|
|
2244
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "image-session-demo", .window_width = 400, .window_height = 300 });
|
|
2245
|
+
|
|
2246
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2247
|
+
defer harness.destroy(gpa);
|
|
2248
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2249
|
+
harness.null_platform.image_decode = true;
|
|
2250
|
+
harness.runtime.options.session_recorder = recorder;
|
|
2251
|
+
|
|
2252
|
+
var failing: ImageBufferFailingAllocator = .{ .backing = std.heap.page_allocator };
|
|
2253
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2254
|
+
defer gpa.destroy(app_state);
|
|
2255
|
+
app_state.* = ImageSessionApp.init(failing.allocator(), .{}, imageSessionOptions());
|
|
2256
|
+
defer app_state.deinit();
|
|
2257
|
+
app_state.effects.executor = .fake;
|
|
2258
|
+
const app = app_state.app();
|
|
2259
|
+
|
|
2260
|
+
try harness.start(app);
|
|
2261
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
2262
|
+
.label = image_canvas_label,
|
|
2263
|
+
.size = geometry.SizeF.init(400, 300),
|
|
2264
|
+
.scale_factor = 2,
|
|
2265
|
+
.frame_index = 1,
|
|
2266
|
+
.timestamp_ns = 1_000_000,
|
|
2267
|
+
} });
|
|
2268
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2269
|
+
|
|
2270
|
+
// Fail exactly the staged source buffer's allocation inside the
|
|
2271
|
+
// handlerless loadImage this dispatch issues.
|
|
2272
|
+
failing.armed = true;
|
|
2273
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.cover-ff", .window_id = 1 } });
|
|
2274
|
+
try std.testing.expect(!failing.armed);
|
|
2275
|
+
// NO wake yet: a HANDLED reload inside the staged window must
|
|
2276
|
+
// reject — the silent failure's occupancy, the window under test.
|
|
2277
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.cover", .window_id = 1 } });
|
|
2278
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
2279
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2280
|
+
|
|
2281
|
+
// The window ended at the drain: the same id parks and loads.
|
|
2282
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.cover", .window_id = 1 } });
|
|
2283
|
+
var png_buffer: [4096]u8 = undefined;
|
|
2284
|
+
try app_state.effects.feedImageBytes(21, imageSessionPng(&png_buffer));
|
|
2285
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
2286
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2287
|
+
|
|
2288
|
+
recorder.finish();
|
|
2289
|
+
try std.testing.expect(!recorder.failed);
|
|
2290
|
+
return .{
|
|
2291
|
+
.model = app_state.model,
|
|
2292
|
+
.fingerprint = harness.runtime.sessionStateFingerprint(),
|
|
2293
|
+
};
|
|
2294
|
+
}
|
|
2295
|
+
|
|
2296
|
+
test "a fire-and-forget start failure journals and replays: the parked fake retires with no Msg" {
|
|
2297
|
+
const gpa = std.testing.allocator;
|
|
2298
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
2299
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
2300
|
+
buffer.len = 0;
|
|
2301
|
+
var store = session_blobs.MemoryBlobStore.init(gpa);
|
|
2302
|
+
defer store.deinit();
|
|
2303
|
+
|
|
2304
|
+
const recorded = try recordFireAndForgetStartFailureSession(gpa, buffer, &store);
|
|
2305
|
+
// Live truth: the fire-and-forget terminal delivered NO Msg — the
|
|
2306
|
+
// model saw only the in-window reload's rejection and the
|
|
2307
|
+
// post-drain reload's load.
|
|
2308
|
+
try std.testing.expectEqual(@as(u32, 2), recorded.model.results);
|
|
2309
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.rejected);
|
|
2310
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.loaded);
|
|
2311
|
+
|
|
2312
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2313
|
+
defer harness.destroy(gpa);
|
|
2314
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2315
|
+
platform.installHeadlessImageCodec("null", &harness.null_platform, &harness.runtime.options.platform.services);
|
|
2316
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2317
|
+
defer gpa.destroy(app_state);
|
|
2318
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
2319
|
+
defer app_state.deinit();
|
|
2320
|
+
|
|
2321
|
+
const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
2322
|
+
.verify = true,
|
|
2323
|
+
.require_same_platform = false,
|
|
2324
|
+
.blobs = store.source(),
|
|
2325
|
+
});
|
|
2326
|
+
try std.testing.expect(report.ok());
|
|
2327
|
+
try std.testing.expect(report.checkpoints_verified > 0);
|
|
2328
|
+
// The fire-and-forget terminal FEEDS (retiring the parked fake,
|
|
2329
|
+
// delivering no Msg through its absent handler), and so does the
|
|
2330
|
+
// post-drain load; the in-window rejection regenerates from the
|
|
2331
|
+
// replayed occupancy check itself.
|
|
2332
|
+
try std.testing.expectEqual(@as(u64, 2), report.effects_fed);
|
|
2333
|
+
try std.testing.expectEqual(@as(u64, 1), report.effects_skipped);
|
|
2334
|
+
try std.testing.expectEqualDeep(recorded.model, app_state.model);
|
|
2335
|
+
try std.testing.expectEqual(recorded.fingerprint, harness.runtime.sessionStateFingerprint());
|
|
2336
|
+
}
|
|
2337
|
+
|
|
2338
|
+
/// Record the handler-retry reference session: a fetch terminal whose
|
|
2339
|
+
/// handler refetches the SAME key from inside update, and a file
|
|
2340
|
+
/// terminal whose handler rereads its key the same way. Delivery
|
|
2341
|
+
/// retires the slot BEFORE the terminal Msg reaches update on both
|
|
2342
|
+
/// sides — the recording's drain and the replay's fed drain — so the
|
|
2343
|
+
/// retry parks as a fresh effect on both, and its own terminal feeds
|
|
2344
|
+
/// at the recorded position.
|
|
2345
|
+
fn recordHandlerRetrySession(gpa: std.mem.Allocator, buffer: *JournalBuffer, store: *session_blobs.MemoryBlobStore) !RecordedImageSession {
|
|
2346
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
2347
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
2348
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
2349
|
+
recorder.blob_sink = store.sink();
|
|
2350
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "image-session-demo", .window_width = 400, .window_height = 300 });
|
|
2351
|
+
|
|
2352
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2353
|
+
defer harness.destroy(gpa);
|
|
2354
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2355
|
+
harness.null_platform.image_decode = true;
|
|
2356
|
+
harness.runtime.options.session_recorder = recorder;
|
|
2357
|
+
|
|
2358
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2359
|
+
defer gpa.destroy(app_state);
|
|
2360
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
2361
|
+
defer app_state.deinit();
|
|
2362
|
+
app_state.effects.executor = .fake;
|
|
2363
|
+
const app = app_state.app();
|
|
2364
|
+
|
|
2365
|
+
try harness.start(app);
|
|
2366
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
2367
|
+
.label = image_canvas_label,
|
|
2368
|
+
.size = geometry.SizeF.init(400, 300),
|
|
2369
|
+
.scale_factor = 2,
|
|
2370
|
+
.frame_index = 1,
|
|
2371
|
+
.timestamp_ns = 1_000_000,
|
|
2372
|
+
} });
|
|
2373
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2374
|
+
|
|
2375
|
+
// Fetch: terminal delivered, handler refetches key 21 — accepted.
|
|
2376
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.fetch-chain", .window_id = 1 } });
|
|
2377
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.fetch-cover", .window_id = 1 } });
|
|
2378
|
+
try app_state.effects.feedResponse(21, 200, "cover-v1");
|
|
2379
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
2380
|
+
try std.testing.expectEqual(@as(usize, 1), app_state.effects.pendingFetchCount());
|
|
2381
|
+
try app_state.effects.feedResponse(21, 200, "cover-v2");
|
|
2382
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
2383
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2384
|
+
|
|
2385
|
+
// File: terminal delivered, handler rereads key 26 — accepted.
|
|
2386
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.file-chain", .window_id = 1 } });
|
|
2387
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.read-note", .window_id = 1 } });
|
|
2388
|
+
try app_state.effects.feedFileResult(26, .ok, "note-v1");
|
|
2389
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
2390
|
+
try std.testing.expectEqual(@as(usize, 1), app_state.effects.pendingFileCount());
|
|
2391
|
+
try app_state.effects.feedFileResult(26, .ok, "note-v2");
|
|
2392
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
2393
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2394
|
+
|
|
2395
|
+
recorder.finish();
|
|
2396
|
+
try std.testing.expect(!recorder.failed);
|
|
2397
|
+
return .{
|
|
2398
|
+
.model = app_state.model,
|
|
2399
|
+
.fingerprint = harness.runtime.sessionStateFingerprint(),
|
|
2400
|
+
};
|
|
2401
|
+
}
|
|
2402
|
+
|
|
2403
|
+
test "a same-key retry from a fetch or file terminal handler is accepted live and under replay" {
|
|
2404
|
+
const gpa = std.testing.allocator;
|
|
2405
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
2406
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
2407
|
+
buffer.len = 0;
|
|
2408
|
+
var store = session_blobs.MemoryBlobStore.init(gpa);
|
|
2409
|
+
defer store.deinit();
|
|
2410
|
+
|
|
2411
|
+
const recorded = try recordHandlerRetrySession(gpa, buffer, &store);
|
|
2412
|
+
// Live truth: both retries were accepted — two responses and two
|
|
2413
|
+
// file results, no rejections anywhere.
|
|
2414
|
+
try std.testing.expectEqual(@as(u32, 2), recorded.model.responses);
|
|
2415
|
+
try std.testing.expectEqual(@as(u32, 0), recorded.model.responses_rejected);
|
|
2416
|
+
try std.testing.expectEqual(@as(u32, 2), recorded.model.files);
|
|
2417
|
+
|
|
2418
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2419
|
+
defer harness.destroy(gpa);
|
|
2420
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2421
|
+
platform.installHeadlessImageCodec("null", &harness.null_platform, &harness.runtime.options.platform.services);
|
|
2422
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2423
|
+
defer gpa.destroy(app_state);
|
|
2424
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
2425
|
+
defer app_state.deinit();
|
|
2426
|
+
|
|
2427
|
+
const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
2428
|
+
.verify = true,
|
|
2429
|
+
.require_same_platform = false,
|
|
2430
|
+
.blobs = store.source(),
|
|
2431
|
+
});
|
|
2432
|
+
try std.testing.expect(report.ok());
|
|
2433
|
+
try std.testing.expect(report.checkpoints_verified > 0);
|
|
2434
|
+
// All four terminals are executor truth and feed; the replayed
|
|
2435
|
+
// retries park fresh (delivery retired the slot before the handler
|
|
2436
|
+
// ran) and their feeds retire them in turn — nothing regenerates.
|
|
2437
|
+
try std.testing.expectEqual(@as(u64, 4), report.effects_fed);
|
|
2438
|
+
try std.testing.expectEqual(@as(u64, 0), report.effects_skipped);
|
|
2439
|
+
try std.testing.expectEqualDeep(recorded.model, app_state.model);
|
|
2440
|
+
try std.testing.expectEqual(recorded.fingerprint, harness.runtime.sessionStateFingerprint());
|
|
2441
|
+
}
|
|
2442
|
+
|
|
2443
|
+
/// Record the queue-saturation reference session: a chatty spawn and
|
|
2444
|
+
/// an image load whose results journal as ONE unbroken run — 64 line
|
|
2445
|
+
/// records (the completion queue's whole capacity) followed by the
|
|
2446
|
+
/// loaded image terminal. Results journal in delivery order, so a
|
|
2447
|
+
/// LIVE recording reaches this shape whenever workers refill the
|
|
2448
|
+
/// queue while the loop drains one pass. The fake executor feeds from
|
|
2449
|
+
/// the loop thread and cannot overfill a single pass, so the first 32
|
|
2450
|
+
/// lines drain through a wake whose EVENT record is withheld (the
|
|
2451
|
+
/// runtime's recorder hook is cleared for exactly that dispatch; the
|
|
2452
|
+
/// drained results still journal through the channel's bound journal)
|
|
2453
|
+
/// — splicing two drain passes into the one run a live recorder
|
|
2454
|
+
/// writes. Replay must then fit 65 consecutive feeds through the
|
|
2455
|
+
/// 64-entry queue without losing the image bytes or reordering the
|
|
2456
|
+
/// image terminal around the lines.
|
|
2457
|
+
fn recordSaturatedImageSession(gpa: std.mem.Allocator, buffer: *JournalBuffer, store: *session_blobs.MemoryBlobStore) !RecordedImageSession {
|
|
2458
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
2459
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
2460
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
2461
|
+
recorder.blob_sink = store.sink();
|
|
2462
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "image-session-demo", .window_width = 400, .window_height = 300 });
|
|
2463
|
+
|
|
2464
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2465
|
+
defer harness.destroy(gpa);
|
|
2466
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2467
|
+
harness.null_platform.image_decode = true;
|
|
2468
|
+
harness.runtime.options.session_recorder = recorder;
|
|
2469
|
+
|
|
2470
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2471
|
+
defer gpa.destroy(app_state);
|
|
2472
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
2473
|
+
defer app_state.deinit();
|
|
2474
|
+
app_state.effects.executor = .fake;
|
|
2475
|
+
const app = app_state.app();
|
|
2476
|
+
|
|
2477
|
+
try harness.start(app);
|
|
2478
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
2479
|
+
.label = image_canvas_label,
|
|
2480
|
+
.size = geometry.SizeF.init(400, 300),
|
|
2481
|
+
.scale_factor = 2,
|
|
2482
|
+
.frame_index = 1,
|
|
2483
|
+
.timestamp_ns = 1_000_000,
|
|
2484
|
+
} });
|
|
2485
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2486
|
+
|
|
2487
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.chatty", .window_id = 1 } });
|
|
2488
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.cover", .window_id = 1 } });
|
|
2489
|
+
|
|
2490
|
+
var line_buffer: [16]u8 = undefined;
|
|
2491
|
+
var index: u32 = 0;
|
|
2492
|
+
while (index < 32) : (index += 1) {
|
|
2493
|
+
try app_state.effects.feedLine(31, try std.fmt.bufPrint(&line_buffer, "line-{d}", .{index}));
|
|
2494
|
+
}
|
|
2495
|
+
// The spliced drain: results journal, the wake event does not.
|
|
2496
|
+
harness.runtime.options.session_recorder = null;
|
|
2497
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
2498
|
+
harness.runtime.options.session_recorder = recorder;
|
|
2499
|
+
|
|
2500
|
+
while (index < 64) : (index += 1) {
|
|
2501
|
+
try app_state.effects.feedLine(31, try std.fmt.bufPrint(&line_buffer, "line-{d}", .{index}));
|
|
2502
|
+
}
|
|
2503
|
+
var png_buffer: [4096]u8 = undefined;
|
|
2504
|
+
try app_state.effects.feedImageBytes(21, imageSessionPng(&png_buffer));
|
|
2505
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
2506
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2507
|
+
|
|
2508
|
+
// The recording itself saw every line BEFORE the image terminal,
|
|
2509
|
+
// with the decoded pixels registered.
|
|
2510
|
+
try std.testing.expectEqual(@as(u32, 64), app_state.model.lines_seen);
|
|
2511
|
+
try std.testing.expectEqual(@as(u32, 64), app_state.model.lines_before_image);
|
|
2512
|
+
try std.testing.expect(harness.runtime.registeredCanvasImage(21) != null);
|
|
2513
|
+
|
|
2514
|
+
recorder.finish();
|
|
2515
|
+
try std.testing.expect(!recorder.failed);
|
|
2516
|
+
return .{
|
|
2517
|
+
.model = app_state.model,
|
|
2518
|
+
.fingerprint = harness.runtime.sessionStateFingerprint(),
|
|
2519
|
+
};
|
|
2520
|
+
}
|
|
2521
|
+
|
|
2522
|
+
test "a drain pass larger than the completion queue replays in order with the image bytes intact" {
|
|
2523
|
+
const gpa = std.testing.allocator;
|
|
2524
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
2525
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
2526
|
+
buffer.len = 0;
|
|
2527
|
+
var store = session_blobs.MemoryBlobStore.init(gpa);
|
|
2528
|
+
defer store.deinit();
|
|
2529
|
+
|
|
2530
|
+
const recorded = try recordSaturatedImageSession(gpa, buffer, &store);
|
|
2531
|
+
|
|
2532
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2533
|
+
defer harness.destroy(gpa);
|
|
2534
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2535
|
+
platform.installHeadlessImageCodec("null", &harness.null_platform, &harness.runtime.options.platform.services);
|
|
2536
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2537
|
+
defer gpa.destroy(app_state);
|
|
2538
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
2539
|
+
defer app_state.deinit();
|
|
2540
|
+
|
|
2541
|
+
const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
2542
|
+
.verify = true,
|
|
2543
|
+
.require_same_platform = false,
|
|
2544
|
+
.blobs = store.source(),
|
|
2545
|
+
});
|
|
2546
|
+
try std.testing.expect(report.ok());
|
|
2547
|
+
try std.testing.expect(report.checkpoints_verified > 0);
|
|
2548
|
+
// Every one of the run's 65 records fed — 64 lines plus the image
|
|
2549
|
+
// terminal — with nothing skipped and nothing dropped.
|
|
2550
|
+
try std.testing.expectEqual(@as(u64, 65), report.effects_fed);
|
|
2551
|
+
try std.testing.expectEqual(@as(u64, 0), report.effects_skipped);
|
|
2552
|
+
// Delivery ORDER survived saturation: the image terminal applied
|
|
2553
|
+
// after all 64 recorded lines, exactly as recorded.
|
|
2554
|
+
try std.testing.expectEqual(@as(u32, 64), app_state.model.lines_before_image);
|
|
2555
|
+
try std.testing.expectEqualDeep(recorded.model, app_state.model);
|
|
2556
|
+
try std.testing.expectEqual(recorded.fingerprint, harness.runtime.sessionStateFingerprint());
|
|
2557
|
+
// The recorded bytes survived saturation: the replayed runtime
|
|
2558
|
+
// re-registered the journaled pixels, drawable offline.
|
|
2559
|
+
const replay_registered = harness.runtime.registeredCanvasImage(21) orelse return error.TestExpectedRegisteredImage;
|
|
2560
|
+
try std.testing.expectEqual(@as(usize, 6), replay_registered.width);
|
|
2561
|
+
try std.testing.expectEqual(@as(usize, 5), replay_registered.height);
|
|
2562
|
+
}
|
|
2563
|
+
|
|
2564
|
+
/// Record the chained same-wake reference session: the cover load's
|
|
2565
|
+
/// terminal (id 21) makes update start a SECOND load (id 25) that
|
|
2566
|
+
/// completes while the first wake's drain pass is still running — the
|
|
2567
|
+
/// fake executor's instant-load convention, the deterministic mirror of
|
|
2568
|
+
/// a real local-path load finishing before the pass ends. The drain's
|
|
2569
|
+
/// causal boundary must hold id 25's delivery to the NEXT wake, so the
|
|
2570
|
+
/// journal reads [effect 21][wake][effect 25][wake] and replay's
|
|
2571
|
+
/// file-order feed always finds the parked request. Without the
|
|
2572
|
+
/// boundary the journal reads [effect 21][effect 25][wake] and replay
|
|
2573
|
+
/// reaches record 25 before the dispatch that creates its request —
|
|
2574
|
+
/// the false `ReplayEffectDivergence` this session exists to pin.
|
|
2575
|
+
fn recordChainedImageSession(gpa: std.mem.Allocator, buffer: *JournalBuffer, store: *session_blobs.MemoryBlobStore) !RecordedImageSession {
|
|
2576
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
2577
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
2578
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
2579
|
+
recorder.blob_sink = store.sink();
|
|
2580
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "image-session-demo", .window_width = 400, .window_height = 300 });
|
|
2581
|
+
|
|
2582
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2583
|
+
defer harness.destroy(gpa);
|
|
2584
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2585
|
+
harness.null_platform.image_decode = true;
|
|
2586
|
+
harness.runtime.options.session_recorder = recorder;
|
|
2587
|
+
|
|
2588
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2589
|
+
defer gpa.destroy(app_state);
|
|
2590
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
2591
|
+
defer app_state.deinit();
|
|
2592
|
+
app_state.effects.executor = .fake;
|
|
2593
|
+
const app = app_state.app();
|
|
2594
|
+
|
|
2595
|
+
try harness.start(app);
|
|
2596
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
2597
|
+
.label = image_canvas_label,
|
|
2598
|
+
.size = geometry.SizeF.init(400, 300),
|
|
2599
|
+
.scale_factor = 2,
|
|
2600
|
+
.frame_index = 1,
|
|
2601
|
+
.timestamp_ns = 1_000_000,
|
|
2602
|
+
} });
|
|
2603
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2604
|
+
|
|
2605
|
+
var png_buffer: [4096]u8 = undefined;
|
|
2606
|
+
const png = imageSessionPng(&png_buffer);
|
|
2607
|
+
|
|
2608
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.chain", .window_id = 1 } });
|
|
2609
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.cover", .window_id = 1 } });
|
|
2610
|
+
try app_state.effects.feedImageBytes(21, png);
|
|
2611
|
+
// Arm the instant completion for the load update issues MID-DRAIN:
|
|
2612
|
+
// when the wake below delivers 21's terminal and update starts
|
|
2613
|
+
// load 25, the fake executor completes it before update returns.
|
|
2614
|
+
app_state.effects.fake_instant_image_bytes = png;
|
|
2615
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
2616
|
+
app_state.effects.fake_instant_image_bytes = null;
|
|
2617
|
+
// Id 25's terminal was already queued during the first wake; the
|
|
2618
|
+
// boundary held it, and this wake (its producer's nudge, live)
|
|
2619
|
+
// delivers it.
|
|
2620
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
2621
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2622
|
+
|
|
2623
|
+
// Both chained loads decoded and registered in the recording run.
|
|
2624
|
+
try std.testing.expectEqual(@as(u32, 2), app_state.model.results);
|
|
2625
|
+
try std.testing.expectEqual(@as(u32, 2), app_state.model.loaded);
|
|
2626
|
+
try std.testing.expect(harness.runtime.registeredCanvasImage(21) != null);
|
|
2627
|
+
try std.testing.expect(harness.runtime.registeredCanvasImage(25) != null);
|
|
2628
|
+
|
|
2629
|
+
recorder.finish();
|
|
2630
|
+
try std.testing.expect(!recorder.failed);
|
|
2631
|
+
return .{
|
|
2632
|
+
.model = app_state.model,
|
|
2633
|
+
.fingerprint = harness.runtime.sessionStateFingerprint(),
|
|
2634
|
+
};
|
|
2635
|
+
}
|
|
2636
|
+
|
|
2637
|
+
test "a chained same-wake image load journals causally and replays without false divergence" {
|
|
2638
|
+
const gpa = std.testing.allocator;
|
|
2639
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
2640
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
2641
|
+
buffer.len = 0;
|
|
2642
|
+
var store = session_blobs.MemoryBlobStore.init(gpa);
|
|
2643
|
+
defer store.deinit();
|
|
2644
|
+
|
|
2645
|
+
const recorded = try recordChainedImageSession(gpa, buffer, &store);
|
|
2646
|
+
|
|
2647
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2648
|
+
defer harness.destroy(gpa);
|
|
2649
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2650
|
+
platform.installHeadlessImageCodec("null", &harness.null_platform, &harness.runtime.options.platform.services);
|
|
2651
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2652
|
+
defer gpa.destroy(app_state);
|
|
2653
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
2654
|
+
defer app_state.deinit();
|
|
2655
|
+
|
|
2656
|
+
// Without the drain boundary this errors `ReplayEffectDivergence`:
|
|
2657
|
+
// record 25 would precede the wake whose dispatch creates request 25.
|
|
2658
|
+
const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
2659
|
+
.verify = true,
|
|
2660
|
+
.require_same_platform = false,
|
|
2661
|
+
.blobs = store.source(),
|
|
2662
|
+
});
|
|
2663
|
+
try std.testing.expect(report.ok());
|
|
2664
|
+
try std.testing.expect(report.checkpoints_verified > 0);
|
|
2665
|
+
try std.testing.expectEqual(@as(u64, 2), report.effects_fed);
|
|
2666
|
+
try std.testing.expectEqual(@as(u64, 0), report.effects_skipped);
|
|
2667
|
+
try std.testing.expectEqualDeep(recorded.model, app_state.model);
|
|
2668
|
+
try std.testing.expectEqual(recorded.fingerprint, harness.runtime.sessionStateFingerprint());
|
|
2669
|
+
// Same bytes twice = one blob, and both replayed registrations hold
|
|
2670
|
+
// the recorded pixels, offline.
|
|
2671
|
+
try std.testing.expectEqual(@as(usize, 1), store.count);
|
|
2672
|
+
try std.testing.expect(harness.runtime.registeredCanvasImage(21) != null);
|
|
2673
|
+
try std.testing.expect(harness.runtime.registeredCanvasImage(25) != null);
|
|
2674
|
+
|
|
2675
|
+
// The journal's event boundaries are causal: no image record may sit
|
|
2676
|
+
// ahead of the event during whose dispatch its request was created.
|
|
2677
|
+
// Concretely, the two image records must be separated by at least
|
|
2678
|
+
// one event record — record 25 lands under the SECOND wake.
|
|
2679
|
+
var reader = try journal.Reader.init(buffer.journalBytes());
|
|
2680
|
+
var image_records: usize = 0;
|
|
2681
|
+
var events_between = true;
|
|
2682
|
+
var last_was_image = false;
|
|
2683
|
+
while (try reader.next()) |record| switch (record) {
|
|
2684
|
+
.effect => |effect| {
|
|
2685
|
+
if (effect.kind != .image) continue;
|
|
2686
|
+
image_records += 1;
|
|
2687
|
+
if (last_was_image) events_between = false;
|
|
2688
|
+
last_was_image = true;
|
|
2689
|
+
},
|
|
2690
|
+
.event => last_was_image = false,
|
|
2691
|
+
else => {},
|
|
2692
|
+
};
|
|
2693
|
+
try std.testing.expectEqual(@as(usize, 2), image_records);
|
|
2694
|
+
try std.testing.expect(events_between);
|
|
2695
|
+
}
|
|
2696
|
+
|
|
2697
|
+
/// Record a session whose single image load passes every loop-side
|
|
2698
|
+
/// check and is refused by the REAL executor's worker: "http:hostless"
|
|
2699
|
+
/// parses with an http scheme (the loop's whole validation) but has no
|
|
2700
|
+
/// host, so building the request fails inside the worker before any
|
|
2701
|
+
/// connection is attempted — a `.rejected` terminal with worker
|
|
2702
|
+
/// provenance. The recording therefore holds a rejected image record
|
|
2703
|
+
/// that the replayed `loadImage` can NOT regenerate (the fake executor
|
|
2704
|
+
/// parks the request), which replay must feed from the journal.
|
|
2705
|
+
fn recordWorkerRejectedImageSession(gpa: std.mem.Allocator, buffer: *JournalBuffer) !RecordedImageSession {
|
|
2706
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
2707
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
2708
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
2709
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "image-session-demo", .window_width = 400, .window_height = 300 });
|
|
2710
|
+
|
|
2711
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2712
|
+
defer harness.destroy(gpa);
|
|
2713
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2714
|
+
harness.null_platform.image_decode = true;
|
|
2715
|
+
harness.runtime.options.session_recorder = recorder;
|
|
2716
|
+
|
|
2717
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2718
|
+
defer gpa.destroy(app_state);
|
|
2719
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
2720
|
+
defer app_state.deinit();
|
|
2721
|
+
const app = app_state.app();
|
|
2722
|
+
|
|
2723
|
+
try harness.start(app);
|
|
2724
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
2725
|
+
.label = image_canvas_label,
|
|
2726
|
+
.size = geometry.SizeF.init(400, 300),
|
|
2727
|
+
.scale_factor = 2,
|
|
2728
|
+
.frame_index = 1,
|
|
2729
|
+
.timestamp_ns = 1_000_000,
|
|
2730
|
+
} });
|
|
2731
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2732
|
+
|
|
2733
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "image.hostless", .window_id = 1 } });
|
|
2734
|
+
// The real worker delivers asynchronously: pump its wake nudges
|
|
2735
|
+
// into `.wake` dispatches (each journals like any platform event)
|
|
2736
|
+
// until the terminal Msg lands.
|
|
2737
|
+
const io = std.testing.io;
|
|
2738
|
+
var waited_ms: usize = 0;
|
|
2739
|
+
while (app_state.model.results < 1) : (waited_ms += 10) {
|
|
2740
|
+
if (waited_ms >= 20_000) return error.TestTimedOut;
|
|
2741
|
+
var nudged = false;
|
|
2742
|
+
while (harness.null_platform.takeWake()) |_| nudged = true;
|
|
2743
|
+
if (nudged) try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
2744
|
+
try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(10), .awake);
|
|
2745
|
+
}
|
|
2746
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
2747
|
+
|
|
2748
|
+
recorder.finish();
|
|
2749
|
+
try std.testing.expect(!recorder.failed);
|
|
2750
|
+
return .{
|
|
2751
|
+
.model = app_state.model,
|
|
2752
|
+
.fingerprint = harness.runtime.sessionStateFingerprint(),
|
|
2753
|
+
};
|
|
2754
|
+
}
|
|
2755
|
+
|
|
2756
|
+
test "a worker-refused image load journals as executor truth and feeds under replay" {
|
|
2757
|
+
const gpa = std.testing.allocator;
|
|
2758
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
2759
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
2760
|
+
buffer.len = 0;
|
|
2761
|
+
|
|
2762
|
+
const recorded = try recordWorkerRejectedImageSession(gpa, buffer);
|
|
2763
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.results);
|
|
2764
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.model.rejected);
|
|
2765
|
+
|
|
2766
|
+
// Replay offline into a fresh app: the journaled rejected terminal
|
|
2767
|
+
// is worker truth — the fake executor parks the replayed request,
|
|
2768
|
+
// so the record must FEED (never skip) for the Msg to be delivered
|
|
2769
|
+
// at all. No blob store is needed: a rejection carries no bytes.
|
|
2770
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2771
|
+
defer harness.destroy(gpa);
|
|
2772
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2773
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2774
|
+
defer gpa.destroy(app_state);
|
|
2775
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
2776
|
+
defer app_state.deinit();
|
|
2777
|
+
|
|
2778
|
+
const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
2779
|
+
.verify = true,
|
|
2780
|
+
.require_same_platform = false,
|
|
2781
|
+
});
|
|
2782
|
+
try std.testing.expect(report.ok());
|
|
2783
|
+
try std.testing.expect(report.checkpoints_verified > 0);
|
|
2784
|
+
try std.testing.expectEqual(@as(u64, 1), report.effects_fed);
|
|
2785
|
+
try std.testing.expectEqual(@as(u64, 0), report.effects_skipped);
|
|
2786
|
+
try std.testing.expectEqualDeep(recorded.model, app_state.model);
|
|
2787
|
+
try std.testing.expectEqual(recorded.fingerprint, harness.runtime.sessionStateFingerprint());
|
|
2788
|
+
}
|
|
2789
|
+
|
|
2790
|
+
test "a journal referencing blobs refuses to replay without its blob store" {
|
|
2791
|
+
const gpa = std.testing.allocator;
|
|
2792
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
2793
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
2794
|
+
buffer.len = 0;
|
|
2795
|
+
var store = session_blobs.MemoryBlobStore.init(gpa);
|
|
2796
|
+
defer store.deinit();
|
|
2797
|
+
_ = try recordImageSession(gpa, buffer, &store);
|
|
2798
|
+
|
|
2799
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2800
|
+
defer harness.destroy(gpa);
|
|
2801
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2802
|
+
harness.null_platform.image_decode = true;
|
|
2803
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2804
|
+
defer gpa.destroy(app_state);
|
|
2805
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
2806
|
+
defer app_state.deinit();
|
|
2807
|
+
|
|
2808
|
+
const result = session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
2809
|
+
.verify = false,
|
|
2810
|
+
.require_same_platform = false,
|
|
2811
|
+
});
|
|
2812
|
+
try std.testing.expectError(error.ReplayMissingBlob, result);
|
|
2813
|
+
}
|
|
2814
|
+
|
|
2815
|
+
/// Zero the `image_blob_len` field — the LAST eight bytes of the effect
|
|
2816
|
+
/// payload, see `journal.encodeEffect` — of the first journaled
|
|
2817
|
+
/// `.loaded` image record, in place. Framing and every other field stay
|
|
2818
|
+
/// valid, so the journal reader decodes the record fine: only replay's
|
|
2819
|
+
/// record-consistency gate can catch the damage. Returns whether a
|
|
2820
|
+
/// record was damaged.
|
|
2821
|
+
fn zeroFirstLoadedImageBlobLen(bytes: []u8) bool {
|
|
2822
|
+
var pos: usize = journal.preamble_len;
|
|
2823
|
+
while (bytes.len - pos >= 5) {
|
|
2824
|
+
const kind = bytes[pos];
|
|
2825
|
+
const len = std.mem.readInt(u32, bytes[pos + 1 ..][0..4], .little);
|
|
2826
|
+
const payload = bytes[pos + 5 .. pos + 5 + len];
|
|
2827
|
+
pos += 5 + len;
|
|
2828
|
+
if (kind != @intFromEnum(journal.RecordKind.effect)) continue;
|
|
2829
|
+
const record = journal.decodeEffect(payload) catch continue;
|
|
2830
|
+
if (record.kind != .image or record.image_outcome != .loaded or record.image_blob_len == 0) continue;
|
|
2831
|
+
@memset(payload[payload.len - 8 ..], 0);
|
|
2832
|
+
return true;
|
|
2833
|
+
}
|
|
2834
|
+
return false;
|
|
2835
|
+
}
|
|
2836
|
+
|
|
2837
|
+
/// Overwrite the decoded-dimension fields of the first journaled image
|
|
2838
|
+
/// record whose outcome matches, in place — `zeroFirstLoadedImageBlobLen`'s
|
|
2839
|
+
/// sibling. Per `journal.encodeEffect`, the effect payload ends with
|
|
2840
|
+
/// image_width (u64), image_height (u64), image_blob_hash (16 bytes),
|
|
2841
|
+
/// image_blob_len (u64), so the dims live 40 and 32 bytes from the end.
|
|
2842
|
+
/// Framing and every other field stay valid: only replay's
|
|
2843
|
+
/// record-consistency gate can catch the damage. Returns whether a
|
|
2844
|
+
/// record was damaged.
|
|
2845
|
+
fn patchFirstImageDims(bytes: []u8, outcome: effects_mod.EffectImageOutcome, width: u64, height: u64) bool {
|
|
2846
|
+
var pos: usize = journal.preamble_len;
|
|
2847
|
+
while (bytes.len - pos >= 5) {
|
|
2848
|
+
const kind = bytes[pos];
|
|
2849
|
+
const len = std.mem.readInt(u32, bytes[pos + 1 ..][0..4], .little);
|
|
2850
|
+
const payload = bytes[pos + 5 .. pos + 5 + len];
|
|
2851
|
+
pos += 5 + len;
|
|
2852
|
+
if (kind != @intFromEnum(journal.RecordKind.effect)) continue;
|
|
2853
|
+
const record = journal.decodeEffect(payload) catch continue;
|
|
2854
|
+
if (record.kind != .image or record.image_outcome != outcome) continue;
|
|
2855
|
+
std.mem.writeInt(u64, payload[payload.len - 40 ..][0..8], width, .little);
|
|
2856
|
+
std.mem.writeInt(u64, payload[payload.len - 32 ..][0..8], height, .little);
|
|
2857
|
+
return true;
|
|
2858
|
+
}
|
|
2859
|
+
return false;
|
|
2860
|
+
}
|
|
2861
|
+
|
|
2862
|
+
/// Record an image session, hand-damage the first record matching
|
|
2863
|
+
/// `outcome` to claim `width`x`height`, and replay it with the blob
|
|
2864
|
+
/// store present — the shared body of the dimension-damage tests below.
|
|
2865
|
+
fn replayWithPatchedImageDims(outcome: effects_mod.EffectImageOutcome, width: u64, height: u64) !session_replay.ReplayReport {
|
|
2866
|
+
const gpa = std.testing.allocator;
|
|
2867
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
2868
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
2869
|
+
buffer.len = 0;
|
|
2870
|
+
var store = session_blobs.MemoryBlobStore.init(gpa);
|
|
2871
|
+
defer store.deinit();
|
|
2872
|
+
_ = try recordImageSession(gpa, buffer, &store);
|
|
2873
|
+
|
|
2874
|
+
try std.testing.expect(patchFirstImageDims(buffer.bytes[0..buffer.len], outcome, width, height));
|
|
2875
|
+
|
|
2876
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2877
|
+
defer harness.destroy(gpa);
|
|
2878
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2879
|
+
harness.null_platform.image_decode = true;
|
|
2880
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2881
|
+
defer gpa.destroy(app_state);
|
|
2882
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
2883
|
+
defer app_state.deinit();
|
|
2884
|
+
|
|
2885
|
+
return session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
2886
|
+
.verify = false,
|
|
2887
|
+
.require_same_platform = false,
|
|
2888
|
+
.blobs = store.source(),
|
|
2889
|
+
});
|
|
2890
|
+
}
|
|
2891
|
+
|
|
2892
|
+
test "an image record claiming .loaded with absurd dimensions refuses replay as damage" {
|
|
2893
|
+
// maxInt width would otherwise ride the fed path into the app's Msg
|
|
2894
|
+
// verbatim (and, on the TS host, into an `@intCast` to an
|
|
2895
|
+
// i64-classed arm field - a safety panic).
|
|
2896
|
+
const result = replayWithPatchedImageDims(.loaded, std.math.maxInt(u64), 5);
|
|
2897
|
+
try std.testing.expectError(error.ReplayDamagedRecord, result);
|
|
2898
|
+
}
|
|
2899
|
+
|
|
2900
|
+
test "an image record claiming dimensions on a non-loaded outcome refuses replay as damage" {
|
|
2901
|
+
// The recorder writes decoded dimensions only on .loaded; every
|
|
2902
|
+
// other outcome journals 0x0.
|
|
2903
|
+
const result = replayWithPatchedImageDims(.decode_failed, 4, 4);
|
|
2904
|
+
try std.testing.expectError(error.ReplayDamagedRecord, result);
|
|
2905
|
+
}
|
|
2906
|
+
|
|
2907
|
+
test "an image record claiming .loaded past the pixel budget refuses replay as damage" {
|
|
2908
|
+
// 1024x1024 RGBA8 is 4 MiB - past the registered-image slot bound
|
|
2909
|
+
// a live .loaded can never exceed.
|
|
2910
|
+
const result = replayWithPatchedImageDims(.loaded, 1024, 1024);
|
|
2911
|
+
try std.testing.expectError(error.ReplayDamagedRecord, result);
|
|
2912
|
+
}
|
|
2913
|
+
|
|
2914
|
+
test "an image record whose claimed pixel count wraps u64 refuses replay as damage" {
|
|
2915
|
+
// 2^32 x 2^32 wraps the u64 pixel product to 0 - the budget check
|
|
2916
|
+
// must not be fooled by the overflow.
|
|
2917
|
+
const result = replayWithPatchedImageDims(.loaded, 1 << 32, 1 << 32);
|
|
2918
|
+
try std.testing.expectError(error.ReplayDamagedRecord, result);
|
|
2919
|
+
}
|
|
2920
|
+
|
|
2921
|
+
test "an image record claiming .loaded with a zero-length blob refuses replay as damage" {
|
|
2922
|
+
const gpa = std.testing.allocator;
|
|
2923
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
2924
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
2925
|
+
buffer.len = 0;
|
|
2926
|
+
var store = session_blobs.MemoryBlobStore.init(gpa);
|
|
2927
|
+
defer store.deinit();
|
|
2928
|
+
_ = try recordImageSession(gpa, buffer, &store);
|
|
2929
|
+
|
|
2930
|
+
// Hand-damage the journal: a `.loaded` image record now claims a
|
|
2931
|
+
// zero-length blob. A recorder can never produce this (it journals
|
|
2932
|
+
// `.loaded` only after the bytes decoded and registered), so replay
|
|
2933
|
+
// must refuse the record as damage — with the blob store PRESENT
|
|
2934
|
+
// and intact, proving the refusal is the record-consistency gate,
|
|
2935
|
+
// not `ReplayMissingBlob`, and never a silent pixel-less "loaded".
|
|
2936
|
+
try std.testing.expect(zeroFirstLoadedImageBlobLen(buffer.bytes[0..buffer.len]));
|
|
2937
|
+
|
|
2938
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
2939
|
+
defer harness.destroy(gpa);
|
|
2940
|
+
harness.null_platform.gpu_surfaces = true;
|
|
2941
|
+
harness.null_platform.image_decode = true;
|
|
2942
|
+
const app_state = try gpa.create(ImageSessionApp);
|
|
2943
|
+
defer gpa.destroy(app_state);
|
|
2944
|
+
app_state.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
|
|
2945
|
+
defer app_state.deinit();
|
|
2946
|
+
|
|
2947
|
+
const result = session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
2948
|
+
.verify = false,
|
|
2949
|
+
.require_same_platform = false,
|
|
2950
|
+
.blobs = store.source(),
|
|
2951
|
+
});
|
|
2952
|
+
try std.testing.expectError(error.ReplayDamagedRecord, result);
|
|
2953
|
+
}
|
|
2954
|
+
|
|
2955
|
+
test "recording an image result without a blob store fails the journal loudly" {
|
|
2956
|
+
const gpa = std.testing.allocator;
|
|
2957
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
2958
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
2959
|
+
buffer.len = 0;
|
|
2960
|
+
|
|
2961
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
2962
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
2963
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
2964
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "image-session-demo" });
|
|
2965
|
+
recorder.recordEffect(.{ .kind = .image, .key = 21, .payload = "encoded bytes", .image_outcome = .loaded });
|
|
2966
|
+
try std.testing.expect(recorder.failed);
|
|
2967
|
+
_ = gpa;
|
|
2968
|
+
}
|
|
2969
|
+
|
|
2970
|
+
test "image effect records round-trip the blob address through the journal codec" {
|
|
2971
|
+
var buffer: [4096]u8 = undefined;
|
|
2972
|
+
var hash: [effects_mod.effect_image_blob_hash_len]u8 = undefined;
|
|
2973
|
+
for (&hash, 0..) |*byte, index| byte.* = @intCast(index * 3 + 1);
|
|
2974
|
+
const encoded = try journal.encodeEffect(.{
|
|
2975
|
+
.kind = .image,
|
|
2976
|
+
.key = 21,
|
|
2977
|
+
.status = 200,
|
|
2978
|
+
.image_outcome = .loaded,
|
|
2979
|
+
.image_width = 640,
|
|
2980
|
+
.image_height = 480,
|
|
2981
|
+
.image_blob_hash = hash,
|
|
2982
|
+
.image_blob_len = 12_345,
|
|
2983
|
+
}, &buffer);
|
|
2984
|
+
const decoded = try journal.decodeEffect(encoded);
|
|
2985
|
+
try std.testing.expectEqual(effects_mod.EffectResultKind.image, decoded.kind);
|
|
2986
|
+
try std.testing.expectEqual(@as(u64, 21), decoded.key);
|
|
2987
|
+
try std.testing.expectEqual(@as(u16, 200), decoded.status);
|
|
2988
|
+
try std.testing.expectEqual(effects_mod.EffectImageOutcome.loaded, decoded.image_outcome);
|
|
2989
|
+
try std.testing.expectEqual(@as(u64, 640), decoded.image_width);
|
|
2990
|
+
try std.testing.expectEqual(@as(u64, 480), decoded.image_height);
|
|
2991
|
+
try std.testing.expectEqualSlices(u8, &hash, &decoded.image_blob_hash);
|
|
2992
|
+
try std.testing.expectEqual(@as(u64, 12_345), decoded.image_blob_len);
|
|
2993
|
+
}
|