@native-sdk/cli 0.8.4 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/build/app.zig +1178 -74
  2. package/build.zig +715 -17
  3. package/build.zig.zon +1 -0
  4. package/native-sdk.d.ts +10 -0
  5. package/package.json +12 -11
  6. package/packages/core/compile-surface/core.ts +435 -3
  7. package/packages/core/package-lock.json +11 -11
  8. package/packages/core/package.json +2 -2
  9. package/packages/core/scripts/compiler_typecheck.mjs +116 -15
  10. package/packages/core/scripts/gen_service_surface.mjs +268 -0
  11. package/packages/core/scripts/merge_static_archives.mjs +78 -0
  12. package/packages/core/scripts/npm_static_spike.mjs +64 -0
  13. package/packages/core/scripts/run_external_core_compiler.mjs +122 -6
  14. package/packages/core/scripts/run_external_service_compiler.mjs +280 -0
  15. package/packages/core/scripts/stage_external_core.mjs +9 -1
  16. package/packages/core/scripts/stage_external_services.mjs +172 -0
  17. package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
  18. package/packages/core/scripts/vendor_service_packages.mjs +189 -0
  19. package/packages/core/sdk/core.d.ts +224 -0
  20. package/packages/core/sdk/core.ts +694 -22
  21. package/packages/core/sdk/events.d.ts +89 -0
  22. package/packages/core/sdk/events.ts +128 -0
  23. package/packages/core/src/checker.ts +936 -15
  24. package/packages/core/src/cli.ts +156 -3
  25. package/packages/core/src/contract.ts +28 -5
  26. package/packages/core/src/devhost.ts +1876 -16
  27. package/packages/core/src/devhost_journal.mjs +183 -0
  28. package/packages/core/src/devhost_resolver.mjs +17 -1
  29. package/packages/core/src/devhost_service_worker.mjs +90 -0
  30. package/packages/core/src/diagnostics.ts +179 -22
  31. package/packages/core/src/frontend.ts +210 -34
  32. package/packages/core/src/infer.ts +24 -0
  33. package/packages/core/src/modules.ts +129 -80
  34. package/packages/core/src/service_contract.ts +597 -0
  35. package/packages/core/src/sqlite_cli.ts +93 -0
  36. package/packages/core/src/sqlite_codegen.ts +806 -0
  37. package/packages/core/src/sqlite_runtime_policy.ts +364 -0
  38. package/packages/core/src/typed_ast.ts +36 -8
  39. package/skill-data/automation/SKILL.md +1 -1
  40. package/skill-data/core/references/project-anatomy.md +1 -1
  41. package/skill-data/native-ui/SKILL.md +58 -5
  42. package/skill-data/ts-core/SKILL.md +123 -41
  43. package/skill-data/ts-services/SKILL.md +126 -0
  44. package/skill-data/ts-services/references/service-surface.md +604 -0
  45. package/skill-data/zig/SKILL.md +1 -1
  46. package/skills/native-sdk/SKILL.md +1 -1
  47. package/src/app_runner/no_migrations.zig +3 -0
  48. package/src/app_runner/no_services.zig +20 -0
  49. package/src/app_runner/root.zig +133 -4
  50. package/src/app_runner/ts_core_main.zig +254 -8
  51. package/src/app_runner/ts_core_mobile.zig +241 -0
  52. package/src/automation/protocol.zig +5 -1
  53. package/src/automation/snapshot.zig +31 -11
  54. package/src/embed/app_exports.zig +12 -1
  55. package/src/embed/c_api.zig +48 -2
  56. package/src/embed/conversions.zig +4 -5
  57. package/src/embed/host.zig +109 -1
  58. package/src/embed/root.zig +8 -0
  59. package/src/embed/tests.zig +241 -0
  60. package/src/embed/types.zig +51 -0
  61. package/src/embed/ui_host.zig +185 -5
  62. package/src/platform/android/NativeSdkActivity.java +131 -0
  63. package/src/platform/android/android_host.c +182 -1
  64. package/src/platform/android/native_sdk_app.h +17 -0
  65. package/src/platform/ios/native_sdk_app.h +17 -0
  66. package/src/platform/ios/uikit_host.m +115 -0
  67. package/src/platform/linux/cef_host.cpp +8 -2
  68. package/src/platform/linux/gtk_host.c +289 -22
  69. package/src/platform/linux/gtk_host.h +5 -1
  70. package/src/platform/linux/root.zig +109 -22
  71. package/src/platform/macos/appkit_host.h +24 -7
  72. package/src/platform/macos/appkit_host.m +686 -64
  73. package/src/platform/macos/cef_host.mm +400 -66
  74. package/src/platform/macos/root.zig +304 -47
  75. package/src/platform/null_platform.zig +435 -48
  76. package/src/platform/null_platform_tests.zig +113 -0
  77. package/src/platform/root.zig +19 -0
  78. package/src/platform/types.zig +281 -28
  79. package/src/platform/windows/root.zig +207 -27
  80. package/src/platform/windows/webview2_host.cpp +334 -51
  81. package/src/primitives/app_manifest/comptime_scene.zig +2 -0
  82. package/src/primitives/app_manifest/root.zig +4 -0
  83. package/src/primitives/app_manifest/tests.zig +3 -0
  84. package/src/primitives/app_manifest/types.zig +32 -0
  85. package/src/primitives/app_manifest/validation.zig +19 -1
  86. package/src/root.zig +53 -0
  87. package/src/runtime/api.zig +19 -0
  88. package/src/runtime/automation_commands.zig +35 -2
  89. package/src/runtime/automation_snapshot.zig +32 -20
  90. package/src/runtime/bridge_permission_tests.zig +58 -3
  91. package/src/runtime/bridge_responses.zig +4 -2
  92. package/src/runtime/builtin_bridge.zig +11 -1
  93. package/src/runtime/canvas_frame.zig +18 -11
  94. package/src/runtime/canvas_frame_animation_tests.zig +4 -0
  95. package/src/runtime/clock.zig +13 -1
  96. package/src/runtime/core.zig +145 -7
  97. package/src/runtime/credentials_store.zig +115 -0
  98. package/src/runtime/effects.zig +3701 -231
  99. package/src/runtime/effects_credentials_tests.zig +332 -0
  100. package/src/runtime/effects_file_tests.zig +587 -0
  101. package/src/runtime/effects_host_tests.zig +208 -1
  102. package/src/runtime/effects_notification_tests.zig +11 -0
  103. package/src/runtime/effects_relational_tests.zig +314 -0
  104. package/src/runtime/effects_store_tests.zig +211 -0
  105. package/src/runtime/file_access.zig +400 -0
  106. package/src/runtime/file_effect_crash_helper.zig +28 -0
  107. package/src/runtime/file_effect_crash_tests.zig +37 -0
  108. package/src/runtime/flow.zig +35 -10
  109. package/src/runtime/gpu_surface_events.zig +6 -1
  110. package/src/runtime/native_command_event_tests.zig +86 -2
  111. package/src/runtime/persist_store.zig +666 -0
  112. package/src/runtime/pty.zig +5 -2
  113. package/src/runtime/record_store.zig +788 -0
  114. package/src/runtime/relational_store.zig +854 -0
  115. package/src/runtime/root.zig +54 -0
  116. package/src/runtime/runtime_event_tests.zig +70 -0
  117. package/src/runtime/service_host.zig +711 -0
  118. package/src/runtime/service_pool.zig +1314 -0
  119. package/src/runtime/session_blobs.zig +4 -1
  120. package/src/runtime/session_journal.zig +158 -5
  121. package/src/runtime/session_record.zig +215 -0
  122. package/src/runtime/session_replay.zig +206 -11
  123. package/src/runtime/session_tests.zig +81 -0
  124. package/src/runtime/shell_layout.zig +1 -0
  125. package/src/runtime/shell_layout_tests.zig +120 -0
  126. package/src/runtime/sqlite_engine.zig +474 -0
  127. package/src/runtime/state.zig +16 -0
  128. package/src/runtime/system_services.zig +170 -38
  129. package/src/runtime/tests.zig +4 -0
  130. package/src/runtime/ts_core_host.zig +1067 -95
  131. package/src/runtime/ts_core_host_tests.zig +325 -11
  132. package/src/runtime/ts_ui_app.zig +953 -2
  133. package/src/runtime/ui_app.zig +434 -96
  134. package/src/runtime/ui_app_tests.zig +287 -11
  135. package/src/runtime/ui_app_window_tests.zig +61 -7
  136. package/src/runtime/validation.zig +42 -0
  137. package/src/runtime/view.zig +9 -0
  138. package/src/runtime/window_storage.zig +1 -0
  139. package/src/runtime/window_view_runtime.zig +2 -3
  140. package/src/runtime/window_views.zig +19 -0
  141. package/src/tooling/android.zig +21 -16
  142. package/src/tooling/db.zig +160 -0
  143. package/src/tooling/ios.zig +7 -1
  144. package/src/tooling/manifest.zig +214 -2
  145. package/src/tooling/package.zig +116 -0
  146. package/src/tooling/raw_manifest.zig +30 -0
  147. package/src/tooling/root.zig +1 -0
  148. package/src/tooling/templates.zig +189 -4
  149. package/src/tooling/ts_core.zig +366 -17
  150. package/src/tooling/verbs.zig +7 -0
  151. package/third_party/sqlite/README.md +13 -0
  152. package/third_party/sqlite/sqlite3.c +269649 -0
  153. package/third_party/sqlite/sqlite3.h +14349 -0
  154. package/tools/corewire/core_abi.zig +14 -2
  155. package/tools/corewire/emit.zig +98 -29
  156. package/tools/corewire/emit_facade.zig +442 -27
  157. package/tools/corewire/emit_profile.zig +6 -2
  158. package/tools/corewire/emit_service.zig +924 -0
  159. package/tools/corewire/main.zig +122 -0
  160. package/tools/corewire/service_contract.zig +250 -0
  161. package/tools/corewire/sidecar.zig +26 -15
@@ -10,6 +10,7 @@
10
10
  const std = @import("std");
11
11
  const geometry = @import("geometry");
12
12
  const app_manifest = @import("app_manifest");
13
+ const platform = @import("../platform/root.zig");
13
14
  const core = @import("core.zig");
14
15
  const ui_app_mod = @import("ui_app.zig");
15
16
  const effects_mod = @import("effects.zig");
@@ -64,6 +65,11 @@ const HostMsg = union(enum) {
64
65
  ask_other,
65
66
  ask_oversized,
66
67
  ask_colliding,
68
+ query_launch_at_login,
69
+ enable_launch_at_login,
70
+ credential_prefixed_request,
71
+ open_external_url,
72
+ reveal_path,
67
73
  replace,
68
74
  drop,
69
75
  send,
@@ -84,6 +90,14 @@ const timer_key: u64 = 9;
84
90
  // Set by tests before dispatching `.ask`/`.replace`.
85
91
  var test_payload: []const u8 = "";
86
92
 
93
+ fn failLaunchAtLoginStatus(_: ?*anyopaque) anyerror!platform.LaunchAtLoginStatus {
94
+ return error.LaunchAtLoginFailed;
95
+ }
96
+
97
+ fn failSetLaunchAtLogin(_: ?*anyopaque, _: bool) anyerror!platform.LaunchAtLoginStatus {
98
+ return error.LaunchAtLoginFailed;
99
+ }
100
+
87
101
  fn hostUpdate(model: *HostModel, msg: HostMsg, fx: *HostEffects) void {
88
102
  switch (msg) {
89
103
  .ask => fx.hostRequest(.{
@@ -110,6 +124,25 @@ fn hostUpdate(model: *HostModel, msg: HostMsg, fx: *HostEffects) void {
110
124
  .name = "svc.echo",
111
125
  .on_result = HostEffects.hostMsg(.host_result),
112
126
  }),
127
+ .query_launch_at_login => fx.hostRequest(.{
128
+ .key = ask_key,
129
+ .name = "native-sdk.launch-at-login.status",
130
+ .on_result = HostEffects.hostMsg(.host_result),
131
+ }),
132
+ .enable_launch_at_login => fx.hostRequest(.{
133
+ .key = ask_key,
134
+ .name = "native-sdk.launch-at-login.set",
135
+ .payload = &.{1},
136
+ .on_result = HostEffects.hostMsg(.host_result),
137
+ }),
138
+ .credential_prefixed_request => fx.hostRequest(.{
139
+ .key = ask_key,
140
+ .name = "core.credentials.probe",
141
+ .payload = &.{1},
142
+ .on_result = HostEffects.hostMsg(.host_result),
143
+ }),
144
+ .open_external_url => fx.hostSend("native-sdk.os.openUrl", "https://example.com/docs"),
145
+ .reveal_path => fx.hostSend("native-sdk.os.revealPath", "/tmp/native-sdk.log"),
113
146
  .replace => fx.hostRequest(.{
114
147
  .key = ask_key,
115
148
  .name = "svc.echo",
@@ -235,6 +268,85 @@ test "fake executor parks host requests and feeds results back as msgs" {
235
268
  try std.testing.expectError(error.EffectNotFound, fx.feedHostResult(ask_key, true, ""));
236
269
  }
237
270
 
271
+ test "native launch-at-login requests use the platform service without a generic host binding" {
272
+ var h = try Harness.create();
273
+ defer h.destroy();
274
+
275
+ h.harness.null_platform.launch_at_login_status = .requires_approval;
276
+ try h.app_state.dispatch(&h.harness.runtime, 1, .query_launch_at_login);
277
+ try h.wake();
278
+ try std.testing.expectEqual(@as(u32, 1), h.app_state.model.ok_count);
279
+ try std.testing.expectEqualStrings("requires_approval", h.app_state.model.bytesPrefix());
280
+
281
+ try h.app_state.dispatch(&h.harness.runtime, 1, .enable_launch_at_login);
282
+ try h.wake();
283
+ try std.testing.expectEqual(@as(u32, 2), h.app_state.model.ok_count);
284
+ try std.testing.expectEqualStrings("enabled", h.app_state.model.bytesPrefix());
285
+ try std.testing.expectEqual(@as(u32, 1), h.harness.null_platform.launch_at_login_set_count);
286
+ }
287
+
288
+ test "native launch-at-login set preserves an unsupported platform result" {
289
+ var h = try Harness.create();
290
+ defer h.destroy();
291
+
292
+ // Model a host without the setter after the effects channel has bound
293
+ // the runtime-owned services value. Unsupported is a capability result,
294
+ // not an attempted registration failure.
295
+ h.harness.runtime.options.platform.services.set_launch_at_login_fn = null;
296
+ try h.app_state.dispatch(&h.harness.runtime, 1, .enable_launch_at_login);
297
+ try h.wake();
298
+
299
+ try std.testing.expectEqual(@as(u32, 1), h.app_state.model.err_count);
300
+ try std.testing.expectEqualStrings("unsupported", h.app_state.model.bytesPrefix());
301
+ try std.testing.expectEqual(@as(u32, 0), h.harness.null_platform.launch_at_login_set_count);
302
+ }
303
+
304
+ test "native launch-at-login requests distinguish operation failures from unsupported" {
305
+ var h = try Harness.create();
306
+ defer h.destroy();
307
+
308
+ h.harness.runtime.options.platform.services.launch_at_login_status_fn = failLaunchAtLoginStatus;
309
+ try h.app_state.dispatch(&h.harness.runtime, 1, .query_launch_at_login);
310
+ try h.wake();
311
+ try std.testing.expectEqual(@as(u32, 1), h.app_state.model.err_count);
312
+ try std.testing.expectEqualStrings("failed", h.app_state.model.bytesPrefix());
313
+
314
+ h.harness.runtime.options.platform.services.set_launch_at_login_fn = failSetLaunchAtLogin;
315
+ try h.app_state.dispatch(&h.harness.runtime, 1, .enable_launch_at_login);
316
+ try h.wake();
317
+ try std.testing.expectEqual(@as(u32, 2), h.app_state.model.err_count);
318
+ try std.testing.expectEqualStrings("failed", h.app_state.model.bytesPrefix());
319
+ }
320
+
321
+ test "native system commands use runtime validation and platform services" {
322
+ var h = try Harness.create();
323
+ defer h.destroy();
324
+ const fx = &h.app_state.effects;
325
+
326
+ const allowed_urls = [_][]const u8{"https://example.com/*"};
327
+ h.harness.runtime.options.security.navigation.external_links = .{
328
+ .action = .open_system_browser,
329
+ .allowed_urls = &allowed_urls,
330
+ };
331
+ try h.app_state.dispatch(&h.harness.runtime, 1, .open_external_url);
332
+ try h.app_state.dispatch(&h.harness.runtime, 1, .reveal_path);
333
+ try std.testing.expectEqualStrings("https://example.com/docs", h.harness.null_platform.lastExternalUrl());
334
+ try std.testing.expectEqualStrings("/tmp/native-sdk.log", h.harness.null_platform.lastRevealedPath());
335
+
336
+ h.harness.null_platform.local_time_offset_minutes = -360;
337
+ var time_payload: [16]u8 = undefined;
338
+ std.mem.writeInt(u64, time_payload[0..8], @bitCast(@as(f64, 2)), .little);
339
+ std.mem.writeInt(u64, time_payload[8..16], @bitCast(@as(f64, 1_704_164_645_000)), .little);
340
+ fx.hostRequest(.{
341
+ .key = ask_key,
342
+ .name = "native-sdk.time.formatLocal",
343
+ .payload = &time_payload,
344
+ .on_result = HostEffects.hostMsg(.host_result),
345
+ });
346
+ try h.wake();
347
+ try std.testing.expectEqualStrings("2024-01-01 21:04:05", h.app_state.model.bytesPrefix());
348
+ }
349
+
238
350
  test "re-issuing a live host key replaces the pending request and drops the undelivered result" {
239
351
  var h = try Harness.create();
240
352
  defer h.destroy();
@@ -385,6 +497,55 @@ test "host journal records carry the route in code and mark rejections regenerab
385
497
  try std.testing.expectEqual(effects_mod.EffectExitReason.rejected, Capture.records[2].exit_reason);
386
498
  }
387
499
 
500
+ test "Zig persistence restore is a staged, journaled Msg and replay ignores live storage" {
501
+ const PersistMsg = union(enum) { restored: effects_mod.EffectPersistResult };
502
+ const PersistEffects = effects_mod.Effects(PersistMsg);
503
+ const Capture = struct {
504
+ var count: usize = 0;
505
+ var outcome: effects_mod.EffectPersistOutcome = .none;
506
+ var bytes: [32]u8 = undefined;
507
+ var bytes_len: usize = 0;
508
+
509
+ fn note(_: *anyopaque, record: effects_mod.EffectResultRecord) void {
510
+ count += 1;
511
+ outcome = record.persist_outcome;
512
+ bytes_len = record.payload.len;
513
+ @memcpy(bytes[0..bytes_len], record.payload);
514
+ }
515
+ };
516
+
517
+ Capture.count = 0;
518
+ var context: u8 = 0;
519
+ var live = PersistEffects.init(std.testing.allocator);
520
+ defer live.deinit();
521
+ live.bindJournal(.{ .context = &context, .record_fn = Capture.note });
522
+ var source = [_]u8{ 's', 'n', 'a', 'p' };
523
+ live.deliverPersistRestore(.ok, &source, PersistEffects.persistMsg(.restored));
524
+ @memset(&source, 'x');
525
+
526
+ const live_msg = live.takeMsg() orelse return error.TestExpectedMsg;
527
+ try std.testing.expectEqual(effects_mod.EffectPersistOutcome.ok, live_msg.restored.outcome);
528
+ try std.testing.expectEqualStrings("snap", live_msg.restored.bytes);
529
+ try std.testing.expectEqual(@as(usize, 1), Capture.count);
530
+ try std.testing.expectEqual(effects_mod.EffectPersistOutcome.ok, Capture.outcome);
531
+ try std.testing.expectEqualStrings("snap", Capture.bytes[0..Capture.bytes_len]);
532
+
533
+ Capture.count = 0;
534
+ var replay = PersistEffects.init(std.testing.allocator);
535
+ defer replay.deinit();
536
+ replay.bindJournal(.{ .context = &context, .record_fn = Capture.note });
537
+ replay.armReplay();
538
+ var replay_source = "recorded".*;
539
+ try replay.pushReplayPersist(.ok, &replay_source);
540
+ @memset(&replay_source, 'x');
541
+ replay.deliverPersistRestore(.ok, "live bytes must lose", PersistEffects.persistMsg(.restored));
542
+ const replay_msg = replay.takeMsg() orelse return error.TestExpectedMsg;
543
+ try std.testing.expectEqual(effects_mod.EffectPersistOutcome.ok, replay_msg.restored.outcome);
544
+ try std.testing.expectEqualStrings("recorded", replay_msg.restored.bytes);
545
+ try std.testing.expectEqual(@as(usize, 0), Capture.count);
546
+ try replay.settleReplayFeeds();
547
+ }
548
+
388
549
  // ------------------------------------------------------------ real executor
389
550
 
390
551
  test "real-mode host calls ride the binding and answer through the feed" {
@@ -408,6 +569,8 @@ test "real-mode host calls ride the binding and answer through the feed" {
408
569
  _ = payload;
409
570
  if (std.mem.eql(u8, name, "svc.echo")) {
410
571
  bound.?.feedHostResult(key, true, "echoed") catch unreachable;
572
+ } else if (std.mem.eql(u8, name, "core.credentials.probe")) {
573
+ bound.?.feedHostResult(key, false, "host-routed") catch unreachable;
411
574
  }
412
575
  // svc.other never answers — it stays in flight for the
413
576
  // cancel below.
@@ -441,13 +604,57 @@ test "real-mode host calls ride the binding and answer through the feed" {
441
604
  try std.testing.expectEqual(@as(u32, 1), h.app_state.model.ok_count);
442
605
  try std.testing.expectEqualStrings("echoed", h.app_state.model.bytesPrefix());
443
606
 
607
+ // A generic request that happens to use the credential prefix remains a
608
+ // generic host call. It must never fall through to launch-at-login.
609
+ try h.app_state.dispatch(&h.harness.runtime, 1, .credential_prefixed_request);
610
+ try h.wake();
611
+ try std.testing.expectEqualStrings("host-routed", h.app_state.model.bytesPrefix());
612
+ try std.testing.expectEqual(@as(u32, 0), h.harness.null_platform.launch_at_login_set_count);
613
+
444
614
  // Cancelling an unanswered request notifies the host, silently.
445
615
  try h.app_state.dispatch(&h.harness.runtime, 1, .ask_other);
446
616
  fx.cancelHostRequest(other_key);
447
617
  try h.wake();
448
618
  try std.testing.expectEqual(@as(usize, 1), Stub.cancel_count);
449
619
  try std.testing.expectEqual(other_key, Stub.last_cancelled);
450
- try std.testing.expectEqual(@as(u32, 1), h.app_state.model.result_count);
620
+ try std.testing.expectEqual(@as(u32, 2), h.app_state.model.result_count);
621
+ }
622
+
623
+ test "service bindings reject a duplicate live key without replacing the first request" {
624
+ var h = try Harness.create();
625
+ defer h.destroy();
626
+ const fx = &h.app_state.effects;
627
+
628
+ const Stub = struct {
629
+ var request_count: usize = 0;
630
+ fn send(_: *anyopaque, _: []const u8, _: []const u8) void {}
631
+ fn request(_: *anyopaque, _: []const u8, _: u64, _: []const u8) void {
632
+ request_count += 1;
633
+ }
634
+ };
635
+ Stub.request_count = 0;
636
+ var context: u8 = 0;
637
+ fx.bindHostCalls(.{
638
+ .context = &context,
639
+ .send_fn = Stub.send,
640
+ .request_fn = Stub.request,
641
+ .reject_duplicate_keys = true,
642
+ });
643
+
644
+ test_payload = "first";
645
+ try h.app_state.dispatch(&h.harness.runtime, 1, .ask);
646
+ test_payload = "duplicate";
647
+ try h.app_state.dispatch(&h.harness.runtime, 1, .replace);
648
+ try std.testing.expectEqual(@as(usize, 1), Stub.request_count);
649
+ try h.wake();
650
+ try std.testing.expectEqual(@as(u32, 1), h.app_state.model.err_count);
651
+ try std.testing.expectEqualStrings("rejected", h.app_state.model.bytesPrefix());
652
+
653
+ // The first occupancy survived the refusal and can still complete.
654
+ try fx.feedHostResult(ask_key, true, "first-answer");
655
+ try h.wake();
656
+ try std.testing.expectEqual(@as(u32, 1), h.app_state.model.ok_count);
657
+ try std.testing.expectEqualStrings("first-answer", h.app_state.model.bytesPrefix());
451
658
  }
452
659
 
453
660
  test "real-mode requests without bound host services reject through the err route" {
@@ -18,15 +18,21 @@ test "notification effect delivers validated fields to the platform service" {
18
18
  fx.bindServices(&host.services);
19
19
 
20
20
  fx.showNotification(.{
21
+ .id = "build-status",
21
22
  .title = "Build finished",
22
23
  .subtitle = "native-sdk",
23
24
  .body = "All checks passed.",
25
+ .action_label = "Open",
26
+ .action_command = "build.open",
24
27
  });
25
28
 
26
29
  try std.testing.expectEqual(@as(usize, 1), null_platform.notificationCount());
27
30
  try std.testing.expectEqualStrings("Build finished", null_platform.lastNotificationTitle());
28
31
  try std.testing.expectEqualStrings("native-sdk", null_platform.lastNotificationSubtitle());
29
32
  try std.testing.expectEqualStrings("All checks passed.", null_platform.lastNotificationBody());
33
+ try std.testing.expectEqualStrings("build-status", null_platform.lastNotificationId());
34
+ try std.testing.expectEqualStrings("Open", null_platform.lastNotificationActionLabel());
35
+ try std.testing.expectEqualStrings("build.open", null_platform.lastNotificationActionCommand());
30
36
  }
31
37
 
32
38
  test "notification effect rejects malformed and over-bound requests" {
@@ -37,9 +43,14 @@ test "notification effect rejects malformed and over-bound requests" {
37
43
  fx.bindServices(&host.services);
38
44
 
39
45
  const long_title = [_]u8{'x'} ** (platform.max_notification_title_bytes + 1);
46
+ const long_id = [_]u8{'x'} ** (platform.max_notification_id_bytes + 1);
40
47
  fx.showNotification(.{ .title = "" });
41
48
  fx.showNotification(.{ .title = "bad\x00title" });
42
49
  fx.showNotification(.{ .title = &long_title });
50
+ fx.showNotification(.{ .id = &long_id, .title = "Bad id" });
51
+ fx.showNotification(.{ .title = "Missing command", .action_label = "Open" });
52
+ fx.showNotification(.{ .title = "Missing label", .action_command = "build.open" });
53
+ fx.showNotification(.{ .title = "Invalid command", .action_label = "Open", .action_command = "../open" });
43
54
 
44
55
  try std.testing.expectEqual(@as(usize, 0), null_platform.notificationCount());
45
56
  }
@@ -0,0 +1,314 @@
1
+ //! Tier-3 relational effect coverage: SQLite executes synchronously on the
2
+ //! host side, but rows and terminals remain routed, ordered Msg values.
3
+
4
+ const std = @import("std");
5
+ const effects_mod = @import("effects.zig");
6
+ const relational_store = @import("relational_store.zig");
7
+
8
+ const Msg = union(enum) { db: effects_mod.EffectDbResult };
9
+ const Fx = effects_mod.Effects(Msg);
10
+
11
+ fn takeResult(fx: *Fx) !effects_mod.EffectDbResult {
12
+ return if (fx.takeMsg()) |msg| msg.db else error.TestExpectedMsg;
13
+ }
14
+
15
+ test "relational effects deliver encoded pages then a done terminal" {
16
+ var database = try relational_store.Database.openMemory(std.testing.allocator);
17
+ defer database.deinit();
18
+ var fx = Fx.init(std.testing.allocator);
19
+ defer fx.deinit();
20
+ fx.bindRelationalStore(database.binding());
21
+
22
+ fx.dbExec(.{ .key = 1, .statements = &.{
23
+ .{ .sql = "CREATE TABLE note(id INTEGER PRIMARY KEY, title TEXT NOT NULL);" },
24
+ .{ .sql = "INSERT INTO note(id,title) VALUES(?1,?2);", .params = &.{ .{ .integer = 1 }, .{ .text = "one" } } },
25
+ .{ .sql = "INSERT INTO note(id,title) VALUES(?1,?2);", .params = &.{ .{ .integer = 2 }, .{ .text = "two" } } },
26
+ }, .on_result = Fx.dbMsg(.db) });
27
+ const wrote = try takeResult(&fx);
28
+ try std.testing.expectEqual(effects_mod.EffectDbResultKind.exec, wrote.kind);
29
+ try std.testing.expectEqual(effects_mod.EffectDbOutcome.ok, wrote.outcome);
30
+
31
+ fx.dbQuery(.{
32
+ .key = 2,
33
+ .sql = "SELECT id,title FROM note ORDER BY id;",
34
+ .on_result = Fx.dbMsg(.db),
35
+ });
36
+ const page = try takeResult(&fx);
37
+ try std.testing.expectEqual(effects_mod.EffectDbResultKind.page, page.kind);
38
+ try std.testing.expectEqual(effects_mod.EffectDbOutcome.ok, page.outcome);
39
+ try std.testing.expectEqual(@as(u32, 2), std.mem.readInt(u32, page.bytes[0..4], .little));
40
+ try std.testing.expectEqual(@as(u32, 2), std.mem.readInt(u32, page.bytes[4..8], .little));
41
+ const done = try takeResult(&fx);
42
+ try std.testing.expectEqual(effects_mod.EffectDbResultKind.done, done.kind);
43
+ try std.testing.expectEqual(effects_mod.EffectDbOutcome.ok, done.outcome);
44
+ try std.testing.expectEqual(@as(usize, 0), done.bytes.len);
45
+ }
46
+
47
+ test "relational query pagination emits every row without truncation" {
48
+ var database = try relational_store.Database.openMemory(std.testing.allocator);
49
+ defer database.deinit();
50
+ var fx = Fx.init(std.testing.allocator);
51
+ defer fx.deinit();
52
+ fx.bindRelationalStore(database.binding());
53
+
54
+ fx.dbQuery(.{
55
+ .key = 22,
56
+ .sql = "WITH RECURSIVE n(value) AS (VALUES(1) UNION ALL SELECT value + 1 FROM n WHERE value < 300) SELECT value FROM n;",
57
+ .on_result = Fx.dbMsg(.db),
58
+ });
59
+ const first = try takeResult(&fx);
60
+ try std.testing.expectEqual(effects_mod.EffectDbResultKind.page, first.kind);
61
+ try std.testing.expectEqual(@as(u32, 256), std.mem.readInt(u32, first.bytes[4..8], .little));
62
+ try std.testing.expect(first.bytes.len <= relational_store.max_page_bytes);
63
+ const second = try takeResult(&fx);
64
+ try std.testing.expectEqual(effects_mod.EffectDbResultKind.page, second.kind);
65
+ try std.testing.expectEqual(@as(u32, 44), std.mem.readInt(u32, second.bytes[4..8], .little));
66
+ try std.testing.expect(second.bytes.len <= relational_store.max_page_bytes);
67
+ const done = try takeResult(&fx);
68
+ try std.testing.expectEqual(effects_mod.EffectDbResultKind.done, done.kind);
69
+ try std.testing.expect(fx.takeMsg() == null);
70
+ }
71
+
72
+ test "relational queries reject whole results beyond the total row bound" {
73
+ var database = try relational_store.Database.openMemory(std.testing.allocator);
74
+ defer database.deinit();
75
+ var fx = Fx.init(std.testing.allocator);
76
+ defer fx.deinit();
77
+ fx.bindRelationalStore(database.binding());
78
+
79
+ var sql_buffer: [256]u8 = undefined;
80
+ const sql = try std.fmt.bufPrint(
81
+ &sql_buffer,
82
+ "WITH RECURSIVE n(value) AS (VALUES(1) UNION ALL SELECT value + 1 FROM n WHERE value < {d}) SELECT value FROM n;",
83
+ .{relational_store.max_result_rows + 1},
84
+ );
85
+ fx.dbQuery(.{ .key = 24, .sql = sql, .on_result = Fx.dbMsg(.db) });
86
+ const rejected = try takeResult(&fx);
87
+ try std.testing.expectEqual(effects_mod.EffectDbResultKind.done, rejected.kind);
88
+ try std.testing.expectEqual(effects_mod.EffectDbOutcome.rejected, rejected.outcome);
89
+ try std.testing.expectEqual(@as(usize, 0), rejected.bytes.len);
90
+ try std.testing.expect(fx.takeMsg() == null);
91
+ }
92
+
93
+ test "relational exec rejects TEMP schema objects" {
94
+ var database = try relational_store.Database.openMemory(std.testing.allocator);
95
+ defer database.deinit();
96
+ var fx = Fx.init(std.testing.allocator);
97
+ defer fx.deinit();
98
+ fx.bindRelationalStore(database.binding());
99
+
100
+ fx.dbExec(.{
101
+ .key = 25,
102
+ .statements = &.{.{ .sql = "CREATE TEMP TABLE session_note(id INTEGER PRIMARY KEY) STRICT;" }},
103
+ .on_result = Fx.dbMsg(.db),
104
+ });
105
+ const rejected = try takeResult(&fx);
106
+ try std.testing.expectEqual(effects_mod.EffectDbResultKind.exec, rejected.kind);
107
+ try std.testing.expectEqual(effects_mod.EffectDbOutcome.misuse, rejected.outcome);
108
+ }
109
+
110
+ test "callback-less relational queries release staged page storage" {
111
+ var database = try relational_store.Database.openMemory(std.testing.allocator);
112
+ defer database.deinit();
113
+ var fx = Fx.init(std.testing.allocator);
114
+ defer fx.deinit();
115
+ fx.bindRelationalStore(database.binding());
116
+
117
+ fx.dbQuery(.{
118
+ .key = 23,
119
+ .sql = "SELECT zeroblob(4096) AS payload;",
120
+ });
121
+ try std.testing.expect(fx.takeMsg() == null);
122
+ }
123
+
124
+ test "relational exec rolls back the whole command and reports constraint" {
125
+ var database = try relational_store.Database.openMemory(std.testing.allocator);
126
+ defer database.deinit();
127
+ var fx = Fx.init(std.testing.allocator);
128
+ defer fx.deinit();
129
+ fx.bindRelationalStore(database.binding());
130
+
131
+ fx.dbExec(.{ .key = 3, .statements = &.{.{ .sql = "CREATE TABLE item(id INTEGER PRIMARY KEY);" }}, .on_result = Fx.dbMsg(.db) });
132
+ _ = try takeResult(&fx);
133
+ fx.dbExec(.{ .key = 4, .statements = &.{
134
+ .{ .sql = "INSERT INTO item(id) VALUES(?1);", .params = &.{.{ .integer = 1 }} },
135
+ .{ .sql = "INSERT INTO item(id) VALUES(?1);", .params = &.{.{ .integer = 1 }} },
136
+ }, .on_result = Fx.dbMsg(.db) });
137
+ const failed = try takeResult(&fx);
138
+ try std.testing.expectEqual(effects_mod.EffectDbOutcome.constraint, failed.outcome);
139
+
140
+ fx.dbQuery(.{ .key = 5, .sql = "SELECT id FROM item;", .on_result = Fx.dbMsg(.db) });
141
+ const page = try takeResult(&fx);
142
+ try std.testing.expectEqual(@as(u32, 0), std.mem.readInt(u32, page.bytes[4..8], .little));
143
+ _ = try takeResult(&fx);
144
+ }
145
+
146
+ test "query keys replace and cancel silently while exec duplicates reject loudly" {
147
+ var fx = Fx.init(std.testing.allocator);
148
+ defer fx.deinit();
149
+ fx.executor = .fake;
150
+
151
+ fx.dbQuery(.{ .key = 10, .sql = "SELECT 1;", .on_result = Fx.dbMsg(.db) });
152
+ fx.dbQuery(.{ .key = 10, .sql = "SELECT 2;", .on_result = Fx.dbMsg(.db) });
153
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingDbCount());
154
+ fx.cancelDbQuery(10);
155
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingDbCount());
156
+ try std.testing.expect(fx.takeMsg() == null);
157
+
158
+ const statements = [_]effects_mod.EffectDbStatement{.{ .sql = "CREATE TABLE duplicate(id INTEGER);" }};
159
+ fx.dbExec(.{ .key = 11, .statements = &statements, .on_result = Fx.dbMsg(.db) });
160
+ fx.dbExec(.{ .key = 11, .statements = &statements, .on_result = Fx.dbMsg(.db) });
161
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingDbCount());
162
+ const rejected = try takeResult(&fx);
163
+ try std.testing.expectEqual(effects_mod.EffectDbResultKind.exec, rejected.kind);
164
+ try std.testing.expectEqual(effects_mod.EffectDbOutcome.rejected, rejected.outcome);
165
+ }
166
+
167
+ test "replacing a real relational query purges its staged pages" {
168
+ var database = try relational_store.Database.openMemory(std.testing.allocator);
169
+ defer database.deinit();
170
+ var fx = Fx.init(std.testing.allocator);
171
+ defer fx.deinit();
172
+ fx.bindRelationalStore(database.binding());
173
+
174
+ fx.dbQuery(.{
175
+ .key = 12,
176
+ .sql = "WITH RECURSIVE n(value) AS (VALUES(1) UNION ALL SELECT value + 1 FROM n WHERE value < 300) SELECT value FROM n;",
177
+ .on_result = Fx.dbMsg(.db),
178
+ });
179
+ fx.dbQuery(.{ .key = 12, .sql = "SELECT 2 AS value;", .on_result = Fx.dbMsg(.db) });
180
+ const page = try takeResult(&fx);
181
+ try std.testing.expectEqual(effects_mod.EffectDbResultKind.page, page.kind);
182
+ try std.testing.expectEqual(@as(u32, 1), std.mem.readInt(u32, page.bytes[4..8], .little));
183
+ const done = try takeResult(&fx);
184
+ try std.testing.expectEqual(effects_mod.EffectDbResultKind.done, done.kind);
185
+ try std.testing.expect(fx.takeMsg() == null);
186
+ }
187
+
188
+ test "relational effects have a dedicated bounded slot family" {
189
+ var fx = Fx.init(std.testing.allocator);
190
+ defer fx.deinit();
191
+ fx.executor = .fake;
192
+
193
+ for (0..effects_mod.max_db_effects) |index| {
194
+ fx.dbQuery(.{ .key = 100 + index, .sql = "SELECT 1;", .on_result = Fx.dbMsg(.db) });
195
+ }
196
+ try std.testing.expectEqual(effects_mod.max_db_effects, fx.pendingDbCount());
197
+ fx.dbQuery(.{ .key = 1_000, .sql = "SELECT 1;", .on_result = Fx.dbMsg(.db) });
198
+ const rejected = try takeResult(&fx);
199
+ try std.testing.expectEqual(effects_mod.EffectDbOutcome.rejected, rejected.outcome);
200
+ }
201
+
202
+ test "relational pages and terminals cross the journal boundary verbatim" {
203
+ const Capture = struct {
204
+ var records: [4]effects_mod.EffectResultRecord = undefined;
205
+ var payloads: [4][relational_store.max_page_bytes]u8 = undefined;
206
+ var count: usize = 0;
207
+
208
+ fn note(_: *anyopaque, record: effects_mod.EffectResultRecord) void {
209
+ records[count] = record;
210
+ @memcpy(payloads[count][0..record.payload.len], record.payload);
211
+ records[count].payload = payloads[count][0..record.payload.len];
212
+ count += 1;
213
+ }
214
+ };
215
+ Capture.count = 0;
216
+ var database = try relational_store.Database.openMemory(std.testing.allocator);
217
+ defer database.deinit();
218
+ var fx = Fx.init(std.testing.allocator);
219
+ defer fx.deinit();
220
+ fx.bindRelationalStore(database.binding());
221
+ var context: u8 = 0;
222
+ fx.bindJournal(.{ .context = &context, .record_fn = Capture.note });
223
+
224
+ fx.dbQuery(.{ .key = 40, .sql = "SELECT 7 AS answer;", .on_result = Fx.dbMsg(.db) });
225
+ const page = try takeResult(&fx);
226
+ try std.testing.expectEqual(@as(usize, 1), Capture.count);
227
+ try std.testing.expectEqual(effects_mod.EffectResultKind.db, Capture.records[0].kind);
228
+ try std.testing.expectEqual(effects_mod.EffectDbResultKind.page, effects_mod.dbKindFromJournalCode(Capture.records[0].code).?);
229
+ try std.testing.expectEqual(effects_mod.EffectDbOutcome.ok, effects_mod.dbOutcomeFromJournalCode(Capture.records[0].code).?);
230
+ try std.testing.expectEqualSlices(u8, page.bytes, Capture.records[0].payload);
231
+ const done = try takeResult(&fx);
232
+ try std.testing.expectEqual(@as(usize, 2), Capture.count);
233
+ try std.testing.expectEqual(effects_mod.EffectDbResultKind.done, effects_mod.dbKindFromJournalCode(Capture.records[1].code).?);
234
+ try std.testing.expectEqual(effects_mod.EffectDbOutcome.ok, effects_mod.dbOutcomeFromJournalCode(Capture.records[1].code).?);
235
+ try std.testing.expectEqual(@as(usize, 0), done.bytes.len);
236
+ }
237
+
238
+ test "replay validates relational result shape before feeding it" {
239
+ var fx = Fx.init(std.testing.allocator);
240
+ defer fx.deinit();
241
+ fx.executor = .fake;
242
+ fx.dbQuery(.{ .key = 50, .sql = "SELECT 1;", .on_result = Fx.dbMsg(.db) });
243
+
244
+ var oversized: [relational_store.max_page_bytes + 1]u8 = @splat(0);
245
+ try std.testing.expectError(error.ReplayDamagedRecord, fx.feedDbResult(50, .page, .ok, &oversized));
246
+ try std.testing.expectError(error.ReplayDamagedRecord, fx.feedDbResult(50, .page, .corrupt, ""));
247
+ try std.testing.expectError(error.ReplayDamagedRecord, fx.feedDbResult(50, .done, .ok, "payload"));
248
+ try std.testing.expectError(error.ReplayDamagedRecord, fx.feedDbResult(50, .exec, .ok, ""));
249
+ const bad_tag_page = [_]u8{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 9 };
250
+ try std.testing.expectError(error.ReplayDamagedRecord, fx.feedDbResult(50, .page, .ok, &bad_tag_page));
251
+
252
+ const empty_page = [_]u8{ 0, 0, 0, 0, 0, 0, 0, 0 };
253
+ try fx.feedDbResult(50, .page, .ok, &empty_page);
254
+ try fx.feedDbResult(50, .done, .ok, "");
255
+ try std.testing.expectEqual(effects_mod.EffectDbResultKind.page, (try takeResult(&fx)).kind);
256
+ try std.testing.expectEqual(effects_mod.EffectDbResultKind.done, (try takeResult(&fx)).kind);
257
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingDbCount());
258
+ }
259
+
260
+ test "live relational queries invalidate by table and coalesce until flush" {
261
+ var database = try relational_store.Database.openMemory(std.testing.allocator);
262
+ defer database.deinit();
263
+ var fx = Fx.init(std.testing.allocator);
264
+ defer fx.deinit();
265
+ fx.bindRelationalStore(database.binding());
266
+
267
+ fx.dbExec(.{ .key = 60, .statements = &.{
268
+ .{ .sql = "CREATE TABLE note(id INTEGER PRIMARY KEY) STRICT;" },
269
+ .{ .sql = "CREATE TABLE unrelated(id INTEGER PRIMARY KEY) STRICT;" },
270
+ }, .on_result = Fx.dbMsg(.db) });
271
+ _ = try takeResult(&fx);
272
+ fx.dbSubscribe(.{
273
+ .key = 61,
274
+ .sql = "SELECT id FROM note ORDER BY id;",
275
+ .tables = &.{"note"},
276
+ .on_result = Fx.dbMsg(.db),
277
+ });
278
+ const initial_page = try takeResult(&fx);
279
+ try std.testing.expectEqual(@as(u32, 0), std.mem.readInt(u32, initial_page.bytes[4..8], .little));
280
+ _ = try takeResult(&fx);
281
+
282
+ fx.dbExec(.{ .key = 62, .statements = &.{.{ .sql = "INSERT INTO note(id) VALUES(1);" }}, .on_result = Fx.dbMsg(.db) });
283
+ fx.dbExec(.{ .key = 63, .statements = &.{.{ .sql = "INSERT INTO note(id) VALUES(2);" }}, .on_result = Fx.dbMsg(.db) });
284
+ _ = try takeResult(&fx);
285
+ _ = try takeResult(&fx);
286
+ try std.testing.expect(fx.takeMsg() == null);
287
+ fx.flushDbSubscriptions();
288
+ const refreshed = try takeResult(&fx);
289
+ try std.testing.expectEqual(@as(u32, 2), std.mem.readInt(u32, refreshed.bytes[4..8], .little));
290
+ _ = try takeResult(&fx);
291
+ try std.testing.expect(fx.takeMsg() == null);
292
+
293
+ fx.dbExec(.{ .key = 64, .statements = &.{.{ .sql = "INSERT INTO unrelated(id) VALUES(1);" }}, .on_result = Fx.dbMsg(.db) });
294
+ _ = try takeResult(&fx);
295
+ fx.flushDbSubscriptions();
296
+ try std.testing.expect(fx.takeMsg() == null);
297
+ fx.dbUnsubscribe(61);
298
+ }
299
+
300
+ test "replay keeps a live relational slot parked across repeated deliveries" {
301
+ var fx = Fx.init(std.testing.allocator);
302
+ defer fx.deinit();
303
+ fx.executor = .fake;
304
+ fx.dbSubscribe(.{ .key = 70, .sql = "SELECT 1 AS value;", .tables = &.{"note"}, .on_result = Fx.dbMsg(.db) });
305
+ const page = [_]u8{ 1, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 'v', 'a', 'l', 'u', 'e' };
306
+ try fx.feedDbResult(70, .page, .ok, &page);
307
+ try fx.feedDbResult(70, .done, .ok, "");
308
+ try fx.feedDbResult(70, .page, .ok, &page);
309
+ try fx.feedDbResult(70, .done, .ok, "");
310
+ for (0..4) |_| _ = try takeResult(&fx);
311
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingDbCount());
312
+ fx.dbUnsubscribe(70);
313
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingDbCount());
314
+ }