@native-sdk/cli 0.4.4 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/app.zig +371 -5
- package/build/ts_run.mjs +102 -0
- package/build.zig +255 -1
- package/build.zig.zon +1 -0
- package/package.json +19 -10
- package/packages/core/package-lock.json +44 -0
- package/packages/core/package.json +38 -0
- package/packages/core/rt/rt.zig +1579 -0
- package/packages/core/sdk/bytes_text_methods.d.ts +88 -0
- package/packages/core/sdk/core.ts +694 -0
- package/packages/core/sdk/events.ts +137 -0
- package/packages/core/sdk/text.ts +519 -0
- package/packages/core/src/checker.ts +1869 -0
- package/packages/core/src/cli.ts +68 -0
- package/packages/core/src/devhost.ts +291 -0
- package/packages/core/src/devhost_resolver.mjs +22 -0
- package/packages/core/src/diagnostics.ts +417 -0
- package/packages/core/src/emitter.ts +8815 -0
- package/packages/core/src/infer.ts +1515 -0
- package/packages/core/src/modules.ts +173 -0
- package/packages/core/src/ownership.ts +740 -0
- package/packages/core/src/text_polyfill.ts +337 -0
- package/packages/core/src/text_tables.ts +408 -0
- package/packages/core/src/transpile.ts +146 -0
- package/packages/core/src/typed_ast.ts +546 -0
- package/packages/core/src/types.ts +815 -0
- package/skill-data/native-ui/SKILL.md +14 -0
- package/skill-data/ts-core/SKILL.md +493 -0
- package/skill-data/zig/SKILL.md +10 -0
- package/src/app_runner/root.zig +24 -0
- package/src/app_runner/ts_core_main.zig +231 -0
- package/src/platform/windows/webview2_host.cpp +24 -0
- package/src/primitives/app_manifest/comptime_scene.zig +341 -0
- package/src/primitives/app_manifest/root.zig +5 -0
- package/src/primitives/canvas/commands.zig +14 -7
- package/src/primitives/canvas/layout_audit.zig +5 -5
- package/src/primitives/canvas/lazy_tls.zig +73 -0
- package/src/primitives/canvas/render_generic_resources.zig +22 -15
- package/src/primitives/canvas/root.zig +11 -0
- package/src/primitives/canvas/tests.zig +1 -0
- package/src/primitives/canvas/text_atlas.zig +40 -32
- package/src/primitives/canvas/text_layout_cache.zig +29 -22
- package/src/primitives/canvas/text_measure_cache.zig +56 -41
- package/src/primitives/canvas/text_spans.zig +39 -26
- package/src/primitives/canvas/tokens.zig +69 -0
- package/src/primitives/canvas/ui.zig +125 -0
- package/src/primitives/canvas/ui_markup.zig +22 -3
- package/src/primitives/canvas/ui_markup_compiled.zig +97 -7
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +275 -0
- package/src/primitives/canvas/ui_markup_contract.zig +68 -14
- package/src/primitives/canvas/ui_markup_contract_tests.zig +257 -2
- package/src/primitives/canvas/ui_markup_reflect.zig +214 -0
- package/src/primitives/canvas/ui_markup_view.zig +105 -1
- package/src/primitives/canvas/ui_markup_view_tests.zig +336 -0
- package/src/primitives/canvas/ui_schema.zig +8 -0
- package/src/primitives/canvas/ui_schema_tests.zig +5 -4
- package/src/primitives/canvas/widget_builtin_tests.zig +309 -5
- package/src/primitives/canvas/widget_invalidation.zig +14 -7
- package/src/primitives/canvas/widget_layout.zig +138 -2
- package/src/primitives/canvas/widget_metrics.zig +16 -8
- package/src/primitives/canvas/widget_render.zig +25 -15
- package/src/primitives/canvas/widget_render_controls.zig +15 -9
- package/src/primitives/canvas/widget_runtime_tests.zig +44 -0
- package/src/root.zig +7 -0
- package/src/runtime/canvas_frame.zig +57 -36
- package/src/runtime/canvas_images.zig +9 -3
- package/src/runtime/canvas_widget_control_tests.zig +5 -6
- package/src/runtime/canvas_widget_runtime.zig +7 -4
- package/src/runtime/core.zig +16 -0
- package/src/runtime/effects.zig +1525 -133
- package/src/runtime/effects_fetch_tests.zig +42 -0
- package/src/runtime/effects_file_tests.zig +220 -0
- package/src/runtime/effects_host_tests.zig +594 -0
- package/src/runtime/effects_tests.zig +287 -0
- package/src/runtime/root.zig +18 -0
- package/src/runtime/session_replay.zig +7 -1
- package/src/runtime/tests.zig +2 -0
- package/src/runtime/ts_core_host.zig +1505 -0
- package/src/runtime/ts_core_host_tests.zig +1408 -0
- package/src/runtime/ts_ui_app.zig +469 -0
- package/src/runtime/ui_app.zig +189 -38
- package/src/runtime/ui_app_tests.zig +221 -0
- package/src/runtime/ui_app_window_tests.zig +182 -0
- package/src/runtime/view_canvas.zig +24 -17
- package/src/runtime/view_widget_tree.zig +2 -2
- package/src/tooling/codesign.zig +212 -105
- package/src/tooling/doctor.zig +78 -0
- package/src/tooling/manifest.zig +47 -0
- package/src/tooling/package.zig +435 -20
- package/src/tooling/raw_manifest.zig +15 -0
- package/src/tooling/root.zig +1 -0
- package/src/tooling/templates.zig +777 -34
- package/src/tooling/ts_core.zig +1099 -0
- package/src/tooling/verbs.zig +105 -2
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
//! Host-request coverage: `fx.hostRequest`/`fx.hostSend` — the generic
|
|
2
|
+
//! named host call behind transpiled cores' command wire — through the
|
|
3
|
+
//! fake executor (deterministic request/feed round trips, replace and
|
|
4
|
+
//! silent-cancel key discipline, rejection passthrough) and the real
|
|
5
|
+
//! executor against a stub `HostCallBinding`. Plus the record/replay
|
|
6
|
+
//! pin: a session driving requests and fx timers through a full UiApp
|
|
7
|
+
//! journals `.host` results and replays to identical state without a
|
|
8
|
+
//! host call.
|
|
9
|
+
|
|
10
|
+
const std = @import("std");
|
|
11
|
+
const geometry = @import("geometry");
|
|
12
|
+
const app_manifest = @import("app_manifest");
|
|
13
|
+
const core = @import("core.zig");
|
|
14
|
+
const ui_app_mod = @import("ui_app.zig");
|
|
15
|
+
const effects_mod = @import("effects.zig");
|
|
16
|
+
const session_record = @import("session_record.zig");
|
|
17
|
+
const session_replay = @import("session_replay.zig");
|
|
18
|
+
|
|
19
|
+
const canvas_label = "host-canvas";
|
|
20
|
+
|
|
21
|
+
const host_views = [_]app_manifest.ShellView{
|
|
22
|
+
.{ .label = canvas_label, .kind = .gpu_surface, .fill = true, .gpu_backend = .metal },
|
|
23
|
+
};
|
|
24
|
+
const host_windows = [_]app_manifest.ShellWindow{.{
|
|
25
|
+
.label = "main",
|
|
26
|
+
.title = "Host",
|
|
27
|
+
.width = 400,
|
|
28
|
+
.height = 300,
|
|
29
|
+
.views = &host_views,
|
|
30
|
+
}};
|
|
31
|
+
const host_scene: app_manifest.ShellConfig = .{ .windows = &host_windows };
|
|
32
|
+
|
|
33
|
+
const max_recorded_bytes = 96;
|
|
34
|
+
|
|
35
|
+
const HostModel = struct {
|
|
36
|
+
result_count: u32 = 0,
|
|
37
|
+
ok_count: u32 = 0,
|
|
38
|
+
err_count: u32 = 0,
|
|
39
|
+
last_key: u64 = 0,
|
|
40
|
+
tick_count: u32 = 0,
|
|
41
|
+
tick_timestamp_ns: u64 = 0,
|
|
42
|
+
// Payload proof: the result slice is drain scratch, so the model
|
|
43
|
+
// copies a bounded prefix of what it keeps.
|
|
44
|
+
bytes_len: usize = 0,
|
|
45
|
+
bytes_prefix: [max_recorded_bytes]u8 = [_]u8{0} ** max_recorded_bytes,
|
|
46
|
+
bytes_prefix_len: usize = 0,
|
|
47
|
+
|
|
48
|
+
fn record(model: *HostModel, result: effects_mod.EffectHostResult) void {
|
|
49
|
+
model.result_count += 1;
|
|
50
|
+
if (result.ok) model.ok_count += 1 else model.err_count += 1;
|
|
51
|
+
model.last_key = result.key;
|
|
52
|
+
model.bytes_len = result.bytes.len;
|
|
53
|
+
model.bytes_prefix_len = @min(result.bytes.len, max_recorded_bytes);
|
|
54
|
+
@memcpy(model.bytes_prefix[0..model.bytes_prefix_len], result.bytes[0..model.bytes_prefix_len]);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
fn bytesPrefix(model: *const HostModel) []const u8 {
|
|
58
|
+
return model.bytes_prefix[0..model.bytes_prefix_len];
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const HostMsg = union(enum) {
|
|
63
|
+
ask,
|
|
64
|
+
ask_other,
|
|
65
|
+
ask_oversized,
|
|
66
|
+
ask_colliding,
|
|
67
|
+
replace,
|
|
68
|
+
drop,
|
|
69
|
+
send,
|
|
70
|
+
start_timer,
|
|
71
|
+
hold_file,
|
|
72
|
+
host_result: effects_mod.EffectHostResult,
|
|
73
|
+
file_result: effects_mod.EffectFileResult,
|
|
74
|
+
tick: effects_mod.EffectTimer,
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const HostApp = ui_app_mod.UiApp(HostModel, HostMsg);
|
|
78
|
+
const HostEffects = HostApp.Effects;
|
|
79
|
+
|
|
80
|
+
const ask_key: u64 = 77;
|
|
81
|
+
const other_key: u64 = 78;
|
|
82
|
+
const timer_key: u64 = 9;
|
|
83
|
+
|
|
84
|
+
// Set by tests before dispatching `.ask`/`.replace`.
|
|
85
|
+
var test_payload: []const u8 = "";
|
|
86
|
+
|
|
87
|
+
fn hostUpdate(model: *HostModel, msg: HostMsg, fx: *HostEffects) void {
|
|
88
|
+
switch (msg) {
|
|
89
|
+
.ask => fx.hostRequest(.{
|
|
90
|
+
.key = ask_key,
|
|
91
|
+
.name = "svc.echo",
|
|
92
|
+
.payload = test_payload,
|
|
93
|
+
.on_result = HostEffects.hostMsg(.host_result),
|
|
94
|
+
}),
|
|
95
|
+
.ask_other => fx.hostRequest(.{
|
|
96
|
+
.key = other_key,
|
|
97
|
+
.name = "svc.other",
|
|
98
|
+
.on_result = HostEffects.hostMsg(.host_result),
|
|
99
|
+
}),
|
|
100
|
+
.ask_oversized => fx.hostRequest(.{
|
|
101
|
+
.key = ask_key,
|
|
102
|
+
.name = "svc.echo",
|
|
103
|
+
.payload = oversized_payload,
|
|
104
|
+
.on_result = HostEffects.hostMsg(.host_result),
|
|
105
|
+
}),
|
|
106
|
+
// A host key colliding with a running FILE effect's key: the
|
|
107
|
+
// request must reject, not replace a foreign effect.
|
|
108
|
+
.ask_colliding => fx.hostRequest(.{
|
|
109
|
+
.key = 500,
|
|
110
|
+
.name = "svc.echo",
|
|
111
|
+
.on_result = HostEffects.hostMsg(.host_result),
|
|
112
|
+
}),
|
|
113
|
+
.replace => fx.hostRequest(.{
|
|
114
|
+
.key = ask_key,
|
|
115
|
+
.name = "svc.echo",
|
|
116
|
+
.payload = test_payload,
|
|
117
|
+
.on_result = HostEffects.hostMsg(.host_result),
|
|
118
|
+
}),
|
|
119
|
+
.drop => fx.cancelHostRequest(ask_key),
|
|
120
|
+
.send => fx.hostSend("svc.beep", "ping"),
|
|
121
|
+
.start_timer => fx.startTimer(.{
|
|
122
|
+
.key = timer_key,
|
|
123
|
+
.interval_ms = 100,
|
|
124
|
+
.on_fire = HostEffects.timerMsg(.tick),
|
|
125
|
+
}),
|
|
126
|
+
.hold_file => fx.readFile(.{
|
|
127
|
+
.key = 500,
|
|
128
|
+
.path = "/tmp/host-collision-probe",
|
|
129
|
+
.on_result = HostEffects.fileMsg(.file_result),
|
|
130
|
+
}),
|
|
131
|
+
.host_result => |result| model.record(result),
|
|
132
|
+
.file_result => {},
|
|
133
|
+
.tick => |timer| {
|
|
134
|
+
model.tick_count += 1;
|
|
135
|
+
model.tick_timestamp_ns = timer.timestamp_ns;
|
|
136
|
+
},
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
var oversized_payload: []const u8 = "";
|
|
141
|
+
|
|
142
|
+
fn hostView(ui: *HostApp.Ui, model: *const HostModel) HostApp.Ui.Node {
|
|
143
|
+
return ui.column(.{ .gap = 4, .padding = 8 }, .{
|
|
144
|
+
ui.text(.{}, ui.fmt("{d} results", .{model.result_count})),
|
|
145
|
+
ui.button(.{ .on_press = .ask }, "Ask"),
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
fn hostCommand(name: []const u8) ?HostMsg {
|
|
150
|
+
if (std.mem.eql(u8, name, "host.ask")) return .ask;
|
|
151
|
+
if (std.mem.eql(u8, name, "host.oversized")) return .ask_oversized;
|
|
152
|
+
if (std.mem.eql(u8, name, "host.timer")) return .start_timer;
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
fn hostOptions() HostApp.Options {
|
|
157
|
+
return .{
|
|
158
|
+
.name = "effects-host",
|
|
159
|
+
.scene = host_scene,
|
|
160
|
+
.canvas_label = canvas_label,
|
|
161
|
+
.update_fx = hostUpdate,
|
|
162
|
+
.view = hostView,
|
|
163
|
+
.on_command = hostCommand,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const Harness = struct {
|
|
168
|
+
harness: *core.TestHarness(),
|
|
169
|
+
app_state: *HostApp,
|
|
170
|
+
app: core.App,
|
|
171
|
+
|
|
172
|
+
fn create() !Harness {
|
|
173
|
+
const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
|
|
174
|
+
errdefer harness.destroy(std.testing.allocator);
|
|
175
|
+
harness.null_platform.gpu_surfaces = true;
|
|
176
|
+
const app_state = try std.testing.allocator.create(HostApp);
|
|
177
|
+
errdefer std.testing.allocator.destroy(app_state);
|
|
178
|
+
app_state.* = HostApp.init(std.heap.page_allocator, .{}, hostOptions());
|
|
179
|
+
const app = app_state.app();
|
|
180
|
+
try harness.start(app);
|
|
181
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
182
|
+
.label = canvas_label,
|
|
183
|
+
.size = geometry.SizeF.init(400, 300),
|
|
184
|
+
.scale_factor = 1,
|
|
185
|
+
.frame_index = 1,
|
|
186
|
+
.timestamp_ns = 1_000_000,
|
|
187
|
+
} });
|
|
188
|
+
try std.testing.expect(app_state.installed);
|
|
189
|
+
return .{ .harness = harness, .app_state = app_state, .app = app };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
fn destroy(self: *Harness) void {
|
|
193
|
+
self.app_state.deinit();
|
|
194
|
+
std.testing.allocator.destroy(self.app_state);
|
|
195
|
+
self.harness.destroy(std.testing.allocator);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
fn wake(self: *Harness) !void {
|
|
199
|
+
try self.harness.runtime.dispatchPlatformEvent(self.app, .wake);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
// ------------------------------------------------------------ fake executor
|
|
204
|
+
|
|
205
|
+
test "fake executor parks host requests and feeds results back as msgs" {
|
|
206
|
+
var h = try Harness.create();
|
|
207
|
+
defer h.destroy();
|
|
208
|
+
const fx = &h.app_state.effects;
|
|
209
|
+
fx.executor = .fake;
|
|
210
|
+
|
|
211
|
+
test_payload = "lookup-me";
|
|
212
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .ask);
|
|
213
|
+
try std.testing.expectEqual(@as(usize, 1), fx.pendingHostCount());
|
|
214
|
+
const request = fx.pendingHostAt(0).?;
|
|
215
|
+
try std.testing.expectEqual(ask_key, request.key);
|
|
216
|
+
try std.testing.expectEqualStrings("svc.echo", request.name);
|
|
217
|
+
try std.testing.expectEqualStrings("lookup-me", request.payload);
|
|
218
|
+
|
|
219
|
+
// The ok route delivers the result bytes.
|
|
220
|
+
try fx.feedHostResult(ask_key, true, "the-answer");
|
|
221
|
+
try h.wake();
|
|
222
|
+
try std.testing.expectEqual(@as(u32, 1), h.app_state.model.result_count);
|
|
223
|
+
try std.testing.expectEqual(@as(u32, 1), h.app_state.model.ok_count);
|
|
224
|
+
try std.testing.expectEqual(ask_key, h.app_state.model.last_key);
|
|
225
|
+
try std.testing.expectEqualStrings("the-answer", h.app_state.model.bytesPrefix());
|
|
226
|
+
try std.testing.expectEqual(@as(usize, 0), fx.pendingHostCount());
|
|
227
|
+
|
|
228
|
+
// The err route passes through as fed, and the key is terminal.
|
|
229
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .ask);
|
|
230
|
+
try fx.feedHostResult(ask_key, false, "not found");
|
|
231
|
+
try h.wake();
|
|
232
|
+
try std.testing.expectEqual(@as(u32, 2), h.app_state.model.result_count);
|
|
233
|
+
try std.testing.expectEqual(@as(u32, 1), h.app_state.model.err_count);
|
|
234
|
+
try std.testing.expectEqualStrings("not found", h.app_state.model.bytesPrefix());
|
|
235
|
+
try std.testing.expectError(error.EffectNotFound, fx.feedHostResult(ask_key, true, ""));
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
test "re-issuing a live host key replaces the pending request and drops the undelivered result" {
|
|
239
|
+
var h = try Harness.create();
|
|
240
|
+
defer h.destroy();
|
|
241
|
+
const fx = &h.app_state.effects;
|
|
242
|
+
fx.executor = .fake;
|
|
243
|
+
|
|
244
|
+
test_payload = "first";
|
|
245
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .ask);
|
|
246
|
+
// Replace while in flight: still exactly one pending, new payload.
|
|
247
|
+
test_payload = "second";
|
|
248
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .replace);
|
|
249
|
+
try std.testing.expectEqual(@as(usize, 1), fx.pendingHostCount());
|
|
250
|
+
try std.testing.expectEqualStrings("second", fx.pendingHostAt(0).?.payload);
|
|
251
|
+
|
|
252
|
+
// Replace after the answer was fed but before it drained: the
|
|
253
|
+
// queued result dies with the old occupancy.
|
|
254
|
+
try fx.feedHostResult(ask_key, true, "stale-answer");
|
|
255
|
+
test_payload = "third";
|
|
256
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .replace);
|
|
257
|
+
try h.wake();
|
|
258
|
+
try std.testing.expectEqual(@as(u32, 0), h.app_state.model.result_count);
|
|
259
|
+
|
|
260
|
+
// Only the live occupancy's answer delivers.
|
|
261
|
+
try fx.feedHostResult(ask_key, true, "fresh-answer");
|
|
262
|
+
try h.wake();
|
|
263
|
+
try std.testing.expectEqual(@as(u32, 1), h.app_state.model.result_count);
|
|
264
|
+
try std.testing.expectEqualStrings("fresh-answer", h.app_state.model.bytesPrefix());
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
test "cancelHostRequest drops silently, and the generic cancel routes to it" {
|
|
268
|
+
var h = try Harness.create();
|
|
269
|
+
defer h.destroy();
|
|
270
|
+
const fx = &h.app_state.effects;
|
|
271
|
+
fx.executor = .fake;
|
|
272
|
+
|
|
273
|
+
// Silent drop: no terminal of any kind, and the key is gone.
|
|
274
|
+
test_payload = "pending";
|
|
275
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .ask);
|
|
276
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .drop);
|
|
277
|
+
try std.testing.expectEqual(@as(usize, 0), fx.pendingHostCount());
|
|
278
|
+
try h.wake();
|
|
279
|
+
try std.testing.expectEqual(@as(u32, 0), h.app_state.model.result_count);
|
|
280
|
+
try std.testing.expectError(error.EffectNotFound, fx.feedHostResult(ask_key, true, ""));
|
|
281
|
+
|
|
282
|
+
// A cancel landing after the answer (fed, undrained) still drops.
|
|
283
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .ask);
|
|
284
|
+
try fx.feedHostResult(ask_key, true, "raced");
|
|
285
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .drop);
|
|
286
|
+
try h.wake();
|
|
287
|
+
try std.testing.expectEqual(@as(u32, 0), h.app_state.model.result_count);
|
|
288
|
+
|
|
289
|
+
// The generic `cancel` keeps the host contract for host keys:
|
|
290
|
+
// silent, no `.cancelled` terminal.
|
|
291
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .ask);
|
|
292
|
+
fx.cancel(ask_key);
|
|
293
|
+
try h.wake();
|
|
294
|
+
try std.testing.expectEqual(@as(usize, 0), fx.pendingHostCount());
|
|
295
|
+
try std.testing.expectEqual(@as(u32, 0), h.app_state.model.result_count);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
test "host requests that cannot run are rejected loudly, never silently" {
|
|
299
|
+
var h = try Harness.create();
|
|
300
|
+
defer h.destroy();
|
|
301
|
+
const fx = &h.app_state.effects;
|
|
302
|
+
fx.executor = .fake;
|
|
303
|
+
|
|
304
|
+
// Over-bound payload.
|
|
305
|
+
const big = try std.testing.allocator.alloc(u8, effects_mod.max_effect_host_payload_bytes + 1);
|
|
306
|
+
defer std.testing.allocator.free(big);
|
|
307
|
+
@memset(big, 'p');
|
|
308
|
+
oversized_payload = big;
|
|
309
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .ask_oversized);
|
|
310
|
+
try h.wake();
|
|
311
|
+
try std.testing.expectEqual(@as(u32, 1), h.app_state.model.err_count);
|
|
312
|
+
try std.testing.expectEqualStrings("rejected", h.app_state.model.bytesPrefix());
|
|
313
|
+
try std.testing.expectEqual(@as(usize, 0), fx.pendingHostCount());
|
|
314
|
+
|
|
315
|
+
// A key held by a running effect of another kind rejects instead
|
|
316
|
+
// of replacing the foreign effect.
|
|
317
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .hold_file);
|
|
318
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .ask_colliding);
|
|
319
|
+
try h.wake();
|
|
320
|
+
try std.testing.expectEqual(@as(u32, 2), h.app_state.model.err_count);
|
|
321
|
+
try std.testing.expectEqual(@as(usize, 1), fx.pendingFileCount());
|
|
322
|
+
|
|
323
|
+
// A result over the budget delivers the err route with a teaching
|
|
324
|
+
// message, never a cut payload passing for the whole one.
|
|
325
|
+
test_payload = "";
|
|
326
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .ask);
|
|
327
|
+
const huge = try std.testing.allocator.alloc(u8, effects_mod.max_effect_host_result_bytes + 1);
|
|
328
|
+
defer std.testing.allocator.free(huge);
|
|
329
|
+
@memset(huge, 'r');
|
|
330
|
+
try fx.feedHostResult(ask_key, true, huge);
|
|
331
|
+
try h.wake();
|
|
332
|
+
try std.testing.expectEqual(@as(u32, 3), h.app_state.model.err_count);
|
|
333
|
+
try std.testing.expectEqualStrings("host result over budget", h.app_state.model.bytesPrefix());
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
test "host journal records carry the route in code and mark rejections regenerable" {
|
|
337
|
+
var h = try Harness.create();
|
|
338
|
+
defer h.destroy();
|
|
339
|
+
const fx = &h.app_state.effects;
|
|
340
|
+
fx.executor = .fake;
|
|
341
|
+
|
|
342
|
+
const Capture = struct {
|
|
343
|
+
var records: [8]effects_mod.EffectResultRecord = undefined;
|
|
344
|
+
var payloads: [8][64]u8 = undefined;
|
|
345
|
+
var count: usize = 0;
|
|
346
|
+
fn note(context: *anyopaque, record: effects_mod.EffectResultRecord) void {
|
|
347
|
+
_ = context;
|
|
348
|
+
records[count] = record;
|
|
349
|
+
const len = @min(record.payload.len, 64);
|
|
350
|
+
@memcpy(payloads[count][0..len], record.payload[0..len]);
|
|
351
|
+
records[count].payload = payloads[count][0..len];
|
|
352
|
+
count += 1;
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
Capture.count = 0;
|
|
356
|
+
var context: u8 = 0;
|
|
357
|
+
fx.bindJournal(.{ .context = &context, .record_fn = Capture.note });
|
|
358
|
+
|
|
359
|
+
test_payload = "";
|
|
360
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .ask);
|
|
361
|
+
try fx.feedHostResult(ask_key, true, "yes");
|
|
362
|
+
try h.wake();
|
|
363
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .ask);
|
|
364
|
+
try fx.feedHostResult(ask_key, false, "no");
|
|
365
|
+
try h.wake();
|
|
366
|
+
const big = try std.testing.allocator.alloc(u8, effects_mod.max_effect_host_payload_bytes + 1);
|
|
367
|
+
defer std.testing.allocator.free(big);
|
|
368
|
+
@memset(big, 'p');
|
|
369
|
+
oversized_payload = big;
|
|
370
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .ask_oversized);
|
|
371
|
+
try h.wake();
|
|
372
|
+
|
|
373
|
+
try std.testing.expectEqual(@as(usize, 3), Capture.count);
|
|
374
|
+
// ok: kind .host, code 0, delivered as .exited (feedable).
|
|
375
|
+
try std.testing.expectEqual(effects_mod.EffectResultKind.host, Capture.records[0].kind);
|
|
376
|
+
try std.testing.expectEqual(@as(i32, 0), Capture.records[0].code);
|
|
377
|
+
try std.testing.expectEqual(effects_mod.EffectExitReason.exited, Capture.records[0].exit_reason);
|
|
378
|
+
try std.testing.expectEqualStrings("yes", Capture.records[0].payload);
|
|
379
|
+
// err: code 1, still feedable.
|
|
380
|
+
try std.testing.expectEqual(@as(i32, 1), Capture.records[1].code);
|
|
381
|
+
try std.testing.expectEqual(effects_mod.EffectExitReason.exited, Capture.records[1].exit_reason);
|
|
382
|
+
try std.testing.expectEqualStrings("no", Capture.records[1].payload);
|
|
383
|
+
// rejection: code 1 AND marked regenerable — replay never feeds it.
|
|
384
|
+
try std.testing.expectEqual(@as(i32, 1), Capture.records[2].code);
|
|
385
|
+
try std.testing.expectEqual(effects_mod.EffectExitReason.rejected, Capture.records[2].exit_reason);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// ------------------------------------------------------------ real executor
|
|
389
|
+
|
|
390
|
+
test "real-mode host calls ride the binding and answer through the feed" {
|
|
391
|
+
var h = try Harness.create();
|
|
392
|
+
defer h.destroy();
|
|
393
|
+
const fx = &h.app_state.effects;
|
|
394
|
+
|
|
395
|
+
const Stub = struct {
|
|
396
|
+
var bound: ?*HostEffects = null;
|
|
397
|
+
var send_count: usize = 0;
|
|
398
|
+
var cancel_count: usize = 0;
|
|
399
|
+
var last_cancelled: u64 = 0;
|
|
400
|
+
fn send(context: *anyopaque, name: []const u8, payload: []const u8) void {
|
|
401
|
+
_ = context;
|
|
402
|
+
_ = name;
|
|
403
|
+
_ = payload;
|
|
404
|
+
send_count += 1;
|
|
405
|
+
}
|
|
406
|
+
fn request(context: *anyopaque, name: []const u8, key: u64, payload: []const u8) void {
|
|
407
|
+
_ = context;
|
|
408
|
+
_ = payload;
|
|
409
|
+
if (std.mem.eql(u8, name, "svc.echo")) {
|
|
410
|
+
bound.?.feedHostResult(key, true, "echoed") catch unreachable;
|
|
411
|
+
}
|
|
412
|
+
// svc.other never answers — it stays in flight for the
|
|
413
|
+
// cancel below.
|
|
414
|
+
}
|
|
415
|
+
fn cancelNotice(context: *anyopaque, key: u64) void {
|
|
416
|
+
_ = context;
|
|
417
|
+
cancel_count += 1;
|
|
418
|
+
last_cancelled = key;
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
Stub.bound = fx;
|
|
422
|
+
Stub.send_count = 0;
|
|
423
|
+
Stub.cancel_count = 0;
|
|
424
|
+
var context: u8 = 0;
|
|
425
|
+
fx.bindHostCalls(.{
|
|
426
|
+
.context = &context,
|
|
427
|
+
.send_fn = Stub.send,
|
|
428
|
+
.request_fn = Stub.request,
|
|
429
|
+
.cancel_fn = Stub.cancelNotice,
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
// Sends reach the binding directly.
|
|
433
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .send);
|
|
434
|
+
try std.testing.expectEqual(@as(usize, 1), Stub.send_count);
|
|
435
|
+
|
|
436
|
+
// A request round-trips: binding answers synchronously, the drain
|
|
437
|
+
// delivers on the next wake.
|
|
438
|
+
test_payload = "hello";
|
|
439
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .ask);
|
|
440
|
+
try h.wake();
|
|
441
|
+
try std.testing.expectEqual(@as(u32, 1), h.app_state.model.ok_count);
|
|
442
|
+
try std.testing.expectEqualStrings("echoed", h.app_state.model.bytesPrefix());
|
|
443
|
+
|
|
444
|
+
// Cancelling an unanswered request notifies the host, silently.
|
|
445
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .ask_other);
|
|
446
|
+
fx.cancelHostRequest(other_key);
|
|
447
|
+
try h.wake();
|
|
448
|
+
try std.testing.expectEqual(@as(usize, 1), Stub.cancel_count);
|
|
449
|
+
try std.testing.expectEqual(other_key, Stub.last_cancelled);
|
|
450
|
+
try std.testing.expectEqual(@as(u32, 1), h.app_state.model.result_count);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
test "real-mode requests without bound host services reject through the err route" {
|
|
454
|
+
var h = try Harness.create();
|
|
455
|
+
defer h.destroy();
|
|
456
|
+
|
|
457
|
+
test_payload = "";
|
|
458
|
+
try h.app_state.dispatch(&h.harness.runtime, 1, .ask);
|
|
459
|
+
try h.wake();
|
|
460
|
+
try std.testing.expectEqual(@as(u32, 1), h.app_state.model.err_count);
|
|
461
|
+
try std.testing.expectEqualStrings("rejected", h.app_state.model.bytesPrefix());
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// -------------------------------------------------------- record / replay
|
|
465
|
+
|
|
466
|
+
const JournalBuffer = struct {
|
|
467
|
+
bytes: [256 * 1024]u8 = undefined,
|
|
468
|
+
len: usize = 0,
|
|
469
|
+
|
|
470
|
+
fn sink(self: *JournalBuffer) session_record.RecorderSink {
|
|
471
|
+
return .{ .context = self, .write_fn = write };
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
fn write(context: *anyopaque, bytes: []const u8) anyerror!void {
|
|
475
|
+
const self: *JournalBuffer = @ptrCast(@alignCast(context));
|
|
476
|
+
if (self.len + bytes.len > self.bytes.len) return error.NoSpaceLeft;
|
|
477
|
+
@memcpy(self.bytes[self.len .. self.len + bytes.len], bytes);
|
|
478
|
+
self.len += bytes.len;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
fn journalBytes(self: *const JournalBuffer) []const u8 {
|
|
482
|
+
return self.bytes[0..self.len];
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
/// Record the reference host session: a request answered ok, one
|
|
487
|
+
/// answered err, a rejected over-budget request (regenerates on
|
|
488
|
+
/// replay), and an fx-timer fire via its platform timer event.
|
|
489
|
+
fn recordHostSession(gpa: std.mem.Allocator, buffer: *JournalBuffer, big: []const u8) !HostModel {
|
|
490
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
491
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
492
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
493
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "effects-host", .window_width = 400, .window_height = 300 });
|
|
494
|
+
|
|
495
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
496
|
+
defer harness.destroy(gpa);
|
|
497
|
+
harness.null_platform.gpu_surfaces = true;
|
|
498
|
+
harness.runtime.options.session_recorder = recorder;
|
|
499
|
+
|
|
500
|
+
const app_state = try gpa.create(HostApp);
|
|
501
|
+
defer gpa.destroy(app_state);
|
|
502
|
+
app_state.* = HostApp.init(std.heap.page_allocator, .{}, hostOptions());
|
|
503
|
+
defer app_state.deinit();
|
|
504
|
+
app_state.effects.executor = .fake;
|
|
505
|
+
const app = app_state.app();
|
|
506
|
+
|
|
507
|
+
try harness.start(app);
|
|
508
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
509
|
+
.label = canvas_label,
|
|
510
|
+
.size = geometry.SizeF.init(400, 300),
|
|
511
|
+
.scale_factor = 2,
|
|
512
|
+
.frame_index = 1,
|
|
513
|
+
.timestamp_ns = 1_000_000,
|
|
514
|
+
} });
|
|
515
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
516
|
+
|
|
517
|
+
// Request answered ok.
|
|
518
|
+
test_payload = "recorded";
|
|
519
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "host.ask", .window_id = 1 } });
|
|
520
|
+
try app_state.effects.feedHostResult(ask_key, true, "journal-me");
|
|
521
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
522
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
523
|
+
|
|
524
|
+
// Request answered err.
|
|
525
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "host.ask", .window_id = 1 } });
|
|
526
|
+
try app_state.effects.feedHostResult(ask_key, false, "declined");
|
|
527
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
528
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
529
|
+
|
|
530
|
+
// A rejection: journaled as regenerable, never fed on replay.
|
|
531
|
+
oversized_payload = big;
|
|
532
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "host.oversized", .window_id = 1 } });
|
|
533
|
+
try harness.runtime.dispatchPlatformEvent(app, .wake);
|
|
534
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
535
|
+
|
|
536
|
+
// An fx timer armed and fired through its platform timer id — a
|
|
537
|
+
// journaled platform event, like every timer fire.
|
|
538
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "host.timer", .window_id = 1 } });
|
|
539
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .timer = .{
|
|
540
|
+
.id = effects_mod.effect_timer_platform_id_base,
|
|
541
|
+
.timestamp_ns = 55_000_000,
|
|
542
|
+
} });
|
|
543
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
544
|
+
|
|
545
|
+
recorder.finish();
|
|
546
|
+
try std.testing.expect(!recorder.failed);
|
|
547
|
+
return app_state.model;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
test "a recorded session with host requests and fx timers replays identically" {
|
|
551
|
+
const gpa = std.testing.allocator;
|
|
552
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
553
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
554
|
+
buffer.len = 0;
|
|
555
|
+
|
|
556
|
+
const big = try gpa.alloc(u8, effects_mod.max_effect_host_payload_bytes + 1);
|
|
557
|
+
defer gpa.free(big);
|
|
558
|
+
@memset(big, 'p');
|
|
559
|
+
|
|
560
|
+
const recorded = try recordHostSession(gpa, buffer, big);
|
|
561
|
+
try std.testing.expectEqual(@as(u32, 3), recorded.result_count);
|
|
562
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.ok_count);
|
|
563
|
+
try std.testing.expectEqual(@as(u32, 2), recorded.err_count);
|
|
564
|
+
try std.testing.expectEqual(@as(u32, 1), recorded.tick_count);
|
|
565
|
+
try std.testing.expectEqual(@as(u64, 55_000_000), recorded.tick_timestamp_ns);
|
|
566
|
+
try std.testing.expectEqualStrings("rejected", recorded.bytesPrefix());
|
|
567
|
+
|
|
568
|
+
// Determinism pin: recording the same driven session twice yields
|
|
569
|
+
// byte-identical journals.
|
|
570
|
+
const second = try std.heap.page_allocator.create(JournalBuffer);
|
|
571
|
+
defer std.heap.page_allocator.destroy(second);
|
|
572
|
+
second.len = 0;
|
|
573
|
+
_ = try recordHostSession(gpa, second, big);
|
|
574
|
+
try std.testing.expectEqualSlices(u8, buffer.journalBytes(), second.journalBytes());
|
|
575
|
+
|
|
576
|
+
// Replay into a fresh app: the journaled .host results feed the
|
|
577
|
+
// stub executor (the rejection regenerates instead), no host runs.
|
|
578
|
+
const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
|
|
579
|
+
defer harness.destroy(gpa);
|
|
580
|
+
harness.null_platform.gpu_surfaces = true;
|
|
581
|
+
const app_state = try gpa.create(HostApp);
|
|
582
|
+
defer gpa.destroy(app_state);
|
|
583
|
+
app_state.* = HostApp.init(std.heap.page_allocator, .{}, hostOptions());
|
|
584
|
+
defer app_state.deinit();
|
|
585
|
+
|
|
586
|
+
const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
587
|
+
.verify = true,
|
|
588
|
+
.require_same_platform = false,
|
|
589
|
+
});
|
|
590
|
+
try std.testing.expect(report.ok());
|
|
591
|
+
// The ok and err answers fed; the rejection did not.
|
|
592
|
+
try std.testing.expectEqual(@as(u64, 2), report.effects_fed);
|
|
593
|
+
try std.testing.expectEqualDeep(recorded, app_state.model);
|
|
594
|
+
}
|