@native-sdk/cli 0.8.3 → 0.9.0

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 (188) hide show
  1. package/build/app.zig +905 -61
  2. package/build.zig +674 -373
  3. package/build.zig.zon +1 -0
  4. package/native-sdk.d.ts +6 -0
  5. package/package.json +12 -11
  6. package/packages/core/compile-surface/core.ts +455 -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/npm_static_spike.mjs +64 -0
  12. package/packages/core/scripts/run_external_core_compiler.mjs +77 -6
  13. package/packages/core/scripts/run_external_service_compiler.mjs +240 -0
  14. package/packages/core/scripts/stage_external_core.mjs +9 -1
  15. package/packages/core/scripts/stage_external_services.mjs +172 -0
  16. package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
  17. package/packages/core/scripts/vendor_service_packages.mjs +189 -0
  18. package/packages/core/sdk/core.d.ts +214 -2
  19. package/packages/core/sdk/core.ts +715 -42
  20. package/packages/core/sdk/events.d.ts +50 -0
  21. package/packages/core/sdk/events.ts +75 -0
  22. package/packages/core/src/checker.ts +528 -15
  23. package/packages/core/src/cli.ts +143 -3
  24. package/packages/core/src/contract.ts +26 -5
  25. package/packages/core/src/devhost.ts +1849 -18
  26. package/packages/core/src/devhost_journal.mjs +181 -0
  27. package/packages/core/src/devhost_resolver.mjs +17 -1
  28. package/packages/core/src/devhost_service_worker.mjs +90 -0
  29. package/packages/core/src/diagnostics.ts +84 -18
  30. package/packages/core/src/frontend.ts +204 -34
  31. package/packages/core/src/modules.ts +129 -80
  32. package/packages/core/src/service_contract.ts +597 -0
  33. package/packages/core/src/sqlite_cli.ts +93 -0
  34. package/packages/core/src/sqlite_codegen.ts +806 -0
  35. package/packages/core/src/sqlite_runtime_policy.ts +364 -0
  36. package/packages/core/src/typed_ast.ts +36 -8
  37. package/skill-data/core/SKILL.md +4 -2
  38. package/skill-data/core/references/project-anatomy.md +3 -2
  39. package/skill-data/core/references/web-engines-packaging-debugging.md +3 -1
  40. package/skill-data/native-ui/SKILL.md +55 -8
  41. package/skill-data/ts-core/SKILL.md +92 -40
  42. package/skill-data/ts-services/SKILL.md +126 -0
  43. package/skill-data/ts-services/references/service-surface.md +603 -0
  44. package/skill-data/zig/SKILL.md +1 -1
  45. package/skills/native-sdk/SKILL.md +1 -1
  46. package/src/app_runner/no_migrations.zig +3 -0
  47. package/src/app_runner/no_services.zig +20 -0
  48. package/src/app_runner/root.zig +104 -4
  49. package/src/app_runner/ts_core_main.zig +268 -4
  50. package/src/automation/snapshot.zig +23 -5
  51. package/src/embed/app_exports.zig +11 -1
  52. package/src/embed/c_api.zig +48 -2
  53. package/src/embed/conversions.zig +4 -5
  54. package/src/embed/host.zig +115 -1
  55. package/src/embed/root.zig +7 -0
  56. package/src/embed/tests.zig +213 -0
  57. package/src/embed/types.zig +51 -0
  58. package/src/embed/ui_host.zig +121 -5
  59. package/src/platform/android/NativeSdkActivity.java +131 -0
  60. package/src/platform/android/android_host.c +182 -1
  61. package/src/platform/android/native_sdk_app.h +17 -0
  62. package/src/platform/ios/native_sdk_app.h +17 -0
  63. package/src/platform/ios/uikit_host.m +115 -0
  64. package/src/platform/linux/cef_host.cpp +1 -1
  65. package/src/platform/linux/gtk_host.c +89 -17
  66. package/src/platform/linux/gtk_host.h +3 -0
  67. package/src/platform/linux/root.zig +41 -18
  68. package/src/platform/macos/appkit_host.h +26 -3
  69. package/src/platform/macos/appkit_host.m +1051 -129
  70. package/src/platform/macos/capture_info_plist.c +25 -0
  71. package/src/platform/macos/cef_host.mm +270 -36
  72. package/src/platform/macos/root.zig +312 -34
  73. package/src/platform/null_platform.zig +281 -19
  74. package/src/platform/null_platform_tests.zig +74 -0
  75. package/src/platform/root.zig +16 -0
  76. package/src/platform/types.zig +279 -15
  77. package/src/platform/windows/root.zig +243 -21
  78. package/src/platform/windows/webview2_host.cpp +464 -46
  79. package/src/primitives/app_manifest/comptime_scene.zig +2 -0
  80. package/src/primitives/app_manifest/root.zig +4 -0
  81. package/src/primitives/app_manifest/tests.zig +3 -0
  82. package/src/primitives/app_manifest/types.zig +34 -0
  83. package/src/primitives/app_manifest/validation.zig +19 -1
  84. package/src/primitives/canvas/code_tests.zig +9 -9
  85. package/src/primitives/canvas/events.zig +12 -11
  86. package/src/primitives/canvas/frame.zig +44 -9
  87. package/src/primitives/canvas/icons/mic.svg +1 -0
  88. package/src/primitives/canvas/icons.zig +2 -0
  89. package/src/primitives/canvas/root.zig +8 -0
  90. package/src/primitives/canvas/text_span_tests.zig +39 -0
  91. package/src/primitives/canvas/ui.zig +23 -4
  92. package/src/primitives/canvas/ui_markup.zig +15 -0
  93. package/src/primitives/canvas/ui_markup_compiled.zig +9 -0
  94. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1 -1
  95. package/src/primitives/canvas/ui_markup_tests.zig +5 -0
  96. package/src/primitives/canvas/ui_markup_view.zig +9 -0
  97. package/src/primitives/canvas/ui_markup_view_tests.zig +9 -6
  98. package/src/primitives/canvas/ui_schema.zig +19 -10
  99. package/src/primitives/canvas/ui_schema_tests.zig +5 -3
  100. package/src/primitives/canvas/ui_tests.zig +18 -4
  101. package/src/primitives/canvas/widget_invalidation.zig +9 -2
  102. package/src/primitives/canvas/widget_render.zig +296 -114
  103. package/src/primitives/canvas/widget_runtime_tests.zig +133 -0
  104. package/src/primitives/canvas/widget_text_input.zig +10 -10
  105. package/src/primitives/canvas/widget_tree.zig +1 -1
  106. package/src/primitives/canvas/widgets.zig +29 -5
  107. package/src/root.zig +56 -0
  108. package/src/runtime/api.zig +11 -0
  109. package/src/runtime/automation_snapshot.zig +4 -0
  110. package/src/runtime/automation_widget_dispatch.zig +1 -0
  111. package/src/runtime/bridge_payload.zig +2 -0
  112. package/src/runtime/bridge_permission_tests.zig +23 -2
  113. package/src/runtime/bridge_responses.zig +3 -1
  114. package/src/runtime/canvas_frame.zig +31 -13
  115. package/src/runtime/canvas_frame_animation_tests.zig +4 -0
  116. package/src/runtime/canvas_frame_invalidation_tests.zig +35 -10
  117. package/src/runtime/canvas_widget_anchored_tests.zig +83 -0
  118. package/src/runtime/canvas_widget_events.zig +69 -24
  119. package/src/runtime/canvas_widget_runtime.zig +101 -6
  120. package/src/runtime/canvas_widget_scroll_driver_tests.zig +47 -4
  121. package/src/runtime/canvas_widget_scroll_drivers.zig +3 -3
  122. package/src/runtime/canvas_widget_semantics_tests.zig +2 -0
  123. package/src/runtime/canvas_widget_split_tree_tests.zig +228 -0
  124. package/src/runtime/canvas_widget_state.zig +6 -1
  125. package/src/runtime/canvas_widget_text_tests.zig +112 -3
  126. package/src/runtime/clock.zig +13 -1
  127. package/src/runtime/core.zig +133 -1
  128. package/src/runtime/credentials_store.zig +115 -0
  129. package/src/runtime/effects.zig +2568 -75
  130. package/src/runtime/effects_audio_capture_tests.zig +251 -0
  131. package/src/runtime/effects_credentials_tests.zig +332 -0
  132. package/src/runtime/effects_host_tests.zig +208 -1
  133. package/src/runtime/effects_relational_tests.zig +314 -0
  134. package/src/runtime/effects_store_tests.zig +211 -0
  135. package/src/runtime/flow.zig +14 -1
  136. package/src/runtime/gpu_surface_events.zig +6 -1
  137. package/src/runtime/native_command_event_tests.zig +42 -0
  138. package/src/runtime/persist_store.zig +666 -0
  139. package/src/runtime/record_store.zig +788 -0
  140. package/src/runtime/relational_store.zig +854 -0
  141. package/src/runtime/root.zig +52 -0
  142. package/src/runtime/runtime_event_tests.zig +70 -0
  143. package/src/runtime/service_host.zig +711 -0
  144. package/src/runtime/service_pool.zig +1314 -0
  145. package/src/runtime/session_blobs.zig +1 -1
  146. package/src/runtime/session_journal.zig +102 -0
  147. package/src/runtime/session_record.zig +177 -0
  148. package/src/runtime/session_replay.zig +109 -4
  149. package/src/runtime/shell_layout.zig +1 -0
  150. package/src/runtime/shell_layout_tests.zig +120 -0
  151. package/src/runtime/sqlite_engine.zig +474 -0
  152. package/src/runtime/state.zig +6 -0
  153. package/src/runtime/system_services.zig +49 -10
  154. package/src/runtime/tests.zig +5 -0
  155. package/src/runtime/ts_core_host.zig +1189 -118
  156. package/src/runtime/ts_core_host_tests.zig +638 -11
  157. package/src/runtime/ts_ui_app.zig +537 -1
  158. package/src/runtime/ui_app.zig +160 -19
  159. package/src/runtime/ui_app_tests.zig +203 -5
  160. package/src/runtime/validation.zig +22 -0
  161. package/src/runtime/view.zig +11 -0
  162. package/src/runtime/view_widget_scroll.zig +56 -5
  163. package/src/runtime/view_widget_text.zig +24 -5
  164. package/src/runtime/view_widget_tree.zig +1 -1
  165. package/src/runtime/window_storage.zig +1 -0
  166. package/src/runtime/window_view_runtime.zig +2 -3
  167. package/src/runtime/window_views.zig +19 -0
  168. package/src/tooling/android.zig +21 -16
  169. package/src/tooling/db.zig +160 -0
  170. package/src/tooling/ios.zig +7 -1
  171. package/src/tooling/manifest.zig +1248 -2
  172. package/src/tooling/package.zig +953 -6
  173. package/src/tooling/raw_manifest.zig +65 -0
  174. package/src/tooling/root.zig +1 -0
  175. package/src/tooling/templates.zig +216 -4
  176. package/src/tooling/ts_core.zig +283 -17
  177. package/src/tooling/verbs.zig +7 -0
  178. package/third_party/sqlite/README.md +13 -0
  179. package/third_party/sqlite/sqlite3.c +269649 -0
  180. package/third_party/sqlite/sqlite3.h +14349 -0
  181. package/tools/corewire/core_abi.zig +14 -2
  182. package/tools/corewire/emit.zig +98 -29
  183. package/tools/corewire/emit_facade.zig +414 -23
  184. package/tools/corewire/emit_profile.zig +6 -2
  185. package/tools/corewire/emit_service.zig +924 -0
  186. package/tools/corewire/main.zig +122 -0
  187. package/tools/corewire/service_contract.zig +250 -0
  188. package/tools/corewire/sidecar.zig +26 -15
@@ -0,0 +1,251 @@
1
+ //! Microphone/system capture coverage over the fake executor. The native
2
+ //! hosts have platform compile/smoke coverage; these tests pin the shared
3
+ //! typed stream, canonical PCM shape, bounded back-pressure, and lifecycle.
4
+
5
+ const std = @import("std");
6
+ const effects_mod = @import("effects.zig");
7
+ const platform = @import("../platform/root.zig");
8
+
9
+ const testing = std.testing;
10
+
11
+ const Msg = union(enum) {
12
+ capture: effects_mod.EffectAudioCaptureEvent,
13
+ channel: effects_mod.EffectChannelEvent,
14
+ };
15
+
16
+ const Fx = effects_mod.Effects(Msg);
17
+
18
+ fn takeCapture(fx: *Fx, kind: effects_mod.EffectAudioCaptureEventKind) !effects_mod.EffectAudioCaptureEvent {
19
+ const msg = fx.takeMsg() orelse return error.TestExpectedMsg;
20
+ try testing.expect(msg == .capture);
21
+ try testing.expectEqual(kind, msg.capture.kind);
22
+ return msg.capture;
23
+ }
24
+
25
+ test "audio capture fake lifecycle delivers canonical PCM and a stopped terminal" {
26
+ var fx = Fx.init(testing.allocator);
27
+ defer fx.deinit();
28
+ fx.executor = .fake;
29
+
30
+ fx.startAudioCapture(.{
31
+ .key = 71,
32
+ .source = .microphone,
33
+ .sample_rate = 16_000,
34
+ .channels = 1,
35
+ .on_event = Fx.audioCaptureMsg(.capture),
36
+ });
37
+ const started = try takeCapture(&fx, .started);
38
+ try testing.expectEqual(@as(u64, 71), started.key);
39
+ try testing.expectEqual(platform.AudioCaptureSource.microphone, started.source);
40
+ try testing.expectEqual(@as(u32, 16_000), started.sample_rate);
41
+ try testing.expectEqual(@as(u8, 1), started.channels);
42
+
43
+ const pcm = [_]u8{ 0x01, 0x00, 0xff, 0x7f, 0x00, 0x80 };
44
+ try fx.feedAudioCapture(71, 12_345_678, &pcm);
45
+ const data = try takeCapture(&fx, .data);
46
+ try testing.expectEqual(@as(u64, 12), data.timestamp_ms);
47
+ try testing.expectEqual(@as(u32, 3), data.frames);
48
+ try testing.expectEqualSlices(u8, &pcm, data.pcm_s16le);
49
+
50
+ fx.stopAudioCapture(71);
51
+ const stopped = try takeCapture(&fx, .stopped);
52
+ try testing.expectEqual(@as(u64, 71), stopped.key);
53
+ try testing.expectEqual(@as(?Msg, null), fx.takeMsg());
54
+ try testing.expectError(error.EffectNotFound, fx.feedAudioCapture(71, 0, &pcm));
55
+ }
56
+
57
+ test "microphone and system capture run concurrently and source replacement is ordered" {
58
+ var fx = Fx.init(testing.allocator);
59
+ defer fx.deinit();
60
+ fx.executor = .fake;
61
+
62
+ fx.startAudioCapture(.{ .key = 1, .source = .microphone, .on_event = Fx.audioCaptureMsg(.capture) });
63
+ fx.startAudioCapture(.{ .key = 2, .source = .system, .channels = 2, .on_event = Fx.audioCaptureMsg(.capture) });
64
+ try testing.expectEqual(platform.AudioCaptureSource.microphone, (try takeCapture(&fx, .started)).source);
65
+ try testing.expectEqual(platform.AudioCaptureSource.system, (try takeCapture(&fx, .started)).source);
66
+
67
+ // Starting another microphone capture closes key 1 before key 3 starts.
68
+ fx.startAudioCapture(.{ .key = 3, .source = .microphone, .on_event = Fx.audioCaptureMsg(.capture) });
69
+ try testing.expectEqual(@as(u64, 1), (try takeCapture(&fx, .stopped)).key);
70
+ try testing.expectEqual(@as(u64, 3), (try takeCapture(&fx, .started)).key);
71
+
72
+ fx.stopAudioCapture(2);
73
+ fx.stopAudioCapture(3);
74
+ _ = try takeCapture(&fx, .stopped);
75
+ _ = try takeCapture(&fx, .stopped);
76
+ }
77
+
78
+ test "audio capture refuses a key owned by the other source without corrupting it" {
79
+ var fx = Fx.init(testing.allocator);
80
+ defer fx.deinit();
81
+ fx.executor = .fake;
82
+
83
+ fx.startAudioCapture(.{ .key = 33, .source = .system, .on_event = Fx.audioCaptureMsg(.capture) });
84
+ try testing.expectEqual(platform.AudioCaptureSource.system, (try takeCapture(&fx, .started)).source);
85
+
86
+ fx.startAudioCapture(.{ .key = 33, .source = .microphone, .on_event = Fx.audioCaptureMsg(.capture) });
87
+ try testing.expectEqual(@as(u64, 33), (try takeCapture(&fx, .rejected)).key);
88
+
89
+ const pcm = [_]u8{ 1, 0 };
90
+ try fx.feedAudioCapture(33, 1_000_000, &pcm);
91
+ try testing.expectEqual(platform.AudioCaptureSource.system, (try takeCapture(&fx, .data)).source);
92
+ fx.stopAudioCapture(33);
93
+ _ = try takeCapture(&fx, .stopped);
94
+ }
95
+
96
+ test "a refused same-source replacement leaves the current capture running" {
97
+ var fx = Fx.init(testing.allocator);
98
+ defer fx.deinit();
99
+ fx.executor = .fake;
100
+
101
+ fx.startAudioCapture(.{ .key = 35, .source = .microphone, .on_event = Fx.audioCaptureMsg(.capture) });
102
+ _ = try takeCapture(&fx, .started);
103
+
104
+ const occupied = fx.openChannel(.{ .key = 36, .on_event = Fx.channelMsg(.channel) });
105
+ try testing.expect(occupied.live());
106
+
107
+ // The replacement cannot claim key 36. Refusing it must not stop key 35.
108
+ fx.startAudioCapture(.{ .key = 36, .source = .microphone, .on_event = Fx.audioCaptureMsg(.capture) });
109
+ try testing.expectEqual(@as(u64, 36), (try takeCapture(&fx, .rejected)).key);
110
+
111
+ const pcm = [_]u8{ 1, 0 };
112
+ try fx.feedAudioCapture(35, 1_000_000, &pcm);
113
+ try testing.expectEqual(@as(u64, 35), (try takeCapture(&fx, .data)).key);
114
+ try testing.expect(occupied.live());
115
+
116
+ // A same-key duplicate is a refusal too, never an implicit stop.
117
+ fx.startAudioCapture(.{ .key = 35, .source = .microphone, .on_event = Fx.audioCaptureMsg(.capture) });
118
+ try testing.expectEqual(@as(u64, 35), (try takeCapture(&fx, .rejected)).key);
119
+ try fx.feedAudioCapture(35, 2_000_000, &pcm);
120
+ try testing.expectEqual(@as(u64, 35), (try takeCapture(&fx, .data)).key);
121
+
122
+ fx.stopAudioCapture(35);
123
+ _ = try takeCapture(&fx, .stopped);
124
+ fx.closeChannel(36);
125
+ const closed = fx.takeMsg() orelse return error.TestExpectedMsg;
126
+ try testing.expect(closed == .channel);
127
+ try testing.expectEqual(effects_mod.EffectChannelEventKind.closed, closed.channel.kind);
128
+ }
129
+
130
+ test "stopping an unknown audio key leaves an ordinary channel open" {
131
+ var fx = Fx.init(testing.allocator);
132
+ defer fx.deinit();
133
+ fx.executor = .fake;
134
+
135
+ const handle = fx.openChannel(.{ .key = 34, .on_event = Fx.channelMsg(.channel) });
136
+ try testing.expect(handle.live());
137
+
138
+ fx.stopAudioCapture(34);
139
+ try testing.expect(handle.live());
140
+ try testing.expectEqual(effects_mod.ChannelHandle.PostResult.accepted, handle.post("still open"));
141
+
142
+ const data = fx.takeMsg() orelse return error.TestExpectedMsg;
143
+ try testing.expect(data == .channel);
144
+ try testing.expectEqual(effects_mod.EffectChannelEventKind.data, data.channel.kind);
145
+ try testing.expectEqualStrings("still open", data.channel.bytes);
146
+
147
+ fx.closeChannel(34);
148
+ const closed = fx.takeMsg() orelse return error.TestExpectedMsg;
149
+ try testing.expect(closed == .channel);
150
+ try testing.expectEqual(effects_mod.EffectChannelEventKind.closed, closed.channel.kind);
151
+ }
152
+
153
+ test "audio capture validates format and reports bounded queue drops" {
154
+ var fx = Fx.init(testing.allocator);
155
+ defer fx.deinit();
156
+ fx.executor = .fake;
157
+
158
+ fx.startAudioCapture(.{
159
+ .key = 8,
160
+ .source = .system,
161
+ .sample_rate = 44_100,
162
+ .on_event = Fx.audioCaptureMsg(.capture),
163
+ });
164
+ try testing.expectEqual(@as(u64, 8), (try takeCapture(&fx, .rejected)).key);
165
+
166
+ fx.startAudioCapture(.{
167
+ .key = 9,
168
+ .source = .system,
169
+ .max_pending = 1,
170
+ .on_event = Fx.audioCaptureMsg(.capture),
171
+ });
172
+ _ = try takeCapture(&fx, .started);
173
+ const frame = [_]u8{ 0, 0 };
174
+ try fx.feedAudioCapture(9, 1_000_000, &frame);
175
+ try testing.expectError(error.EffectQueueFull, fx.feedAudioCapture(9, 2_000_000, &frame));
176
+ const with_drop = try takeCapture(&fx, .data);
177
+ try testing.expectEqual(@as(u32, 1), with_drop.dropped_pending);
178
+ try testing.expectEqual(@as(u32, 1), with_drop.dropped_total);
179
+ try fx.feedAudioCapture(9, 3_000_000, &frame);
180
+ const after_drop = try takeCapture(&fx, .data);
181
+ try testing.expectEqual(@as(u32, 0), after_drop.dropped_pending);
182
+ try testing.expectEqual(@as(u32, 1), after_drop.dropped_total);
183
+ fx.stopAudioCapture(9);
184
+ _ = try takeCapture(&fx, .stopped);
185
+ }
186
+
187
+ test "audio capture real-executor path binds and quiesces platform services" {
188
+ var host = platform.NullPlatform.init(.{});
189
+ defer host.deinit();
190
+ var host_platform = host.platform();
191
+ var fx = Fx.init(testing.allocator);
192
+ defer fx.deinit();
193
+ fx.bindServices(&host_platform.services);
194
+
195
+ fx.startAudioCapture(.{
196
+ .key = 44,
197
+ .source = .system,
198
+ .sample_rate = 24_000,
199
+ .channels = 2,
200
+ .on_event = Fx.audioCaptureMsg(.capture),
201
+ });
202
+ try testing.expectEqual(platform.AudioCaptureSource.system, (try takeCapture(&fx, .started)).source);
203
+ try testing.expect(host.audio_captures[@intFromEnum(platform.AudioCaptureSource.system)].active);
204
+
205
+ const pcm = [_]u8{ 1, 0, 2, 0 };
206
+ try testing.expectEqual(platform.AudioCapturePushResult.accepted, host.pushAudioCapture(.system, 5_000_000, &pcm));
207
+ const data = try takeCapture(&fx, .data);
208
+ try testing.expectEqual(@as(u32, 1), data.frames);
209
+ try testing.expectEqual(@as(u64, 5), data.timestamp_ms);
210
+
211
+ fx.stopAudioCapture(44);
212
+ _ = try takeCapture(&fx, .stopped);
213
+ const capture = &host.audio_captures[@intFromEnum(platform.AudioCaptureSource.system)];
214
+ try testing.expect(!capture.active);
215
+ try testing.expectEqual(@as(usize, 1), capture.stop_count);
216
+ }
217
+
218
+ test "audio capture teardown quiesces every platform source exactly once" {
219
+ var host = platform.NullPlatform.init(.{});
220
+ defer host.deinit();
221
+ var host_platform = host.platform();
222
+ var fx = Fx.init(testing.allocator);
223
+ defer fx.deinit();
224
+ fx.bindServices(&host_platform.services);
225
+
226
+ fx.startAudioCapture(.{
227
+ .key = 51,
228
+ .source = .microphone,
229
+ .on_event = Fx.audioCaptureMsg(.capture),
230
+ });
231
+ fx.startAudioCapture(.{
232
+ .key = 52,
233
+ .source = .system,
234
+ .on_event = Fx.audioCaptureMsg(.capture),
235
+ });
236
+ try testing.expect(host.audio_captures[@intFromEnum(platform.AudioCaptureSource.microphone)].active);
237
+ try testing.expect(host.audio_captures[@intFromEnum(platform.AudioCaptureSource.system)].active);
238
+
239
+ fx.deinit();
240
+ for (&host.audio_captures) |*capture| {
241
+ try testing.expect(!capture.active);
242
+ try testing.expectEqual(@as(usize, 1), capture.stop_count);
243
+ }
244
+
245
+ // Effects teardown is idempotent: the post-platform owner defer must
246
+ // never call back into services or stop either source a second time.
247
+ fx.deinit();
248
+ for (&host.audio_captures) |*capture| {
249
+ try testing.expectEqual(@as(usize, 1), capture.stop_count);
250
+ }
251
+ }
@@ -0,0 +1,332 @@
1
+ //! Tier-4 credential effect coverage. Secrets travel through the dedicated
2
+ //! keyed worker family, the app identity is the platform service namespace,
3
+ //! permission refusal is loud, and replay reconstructs only a deterministic
4
+ //! same-length placeholder from redacted journal metadata.
5
+
6
+ const std = @import("std");
7
+ const effects_mod = @import("effects.zig");
8
+ const core = @import("core.zig");
9
+ const clock_mod = @import("clock.zig");
10
+ const platform = @import("../platform/root.zig");
11
+
12
+ const Msg = union(enum) { result: effects_mod.EffectCredentialsResult };
13
+ const Fx = effects_mod.Effects(Msg);
14
+
15
+ test "credential test harness opts into hermetic backing" {
16
+ const harness = try core.TestHarness().createWithCredentials(std.testing.allocator, .{});
17
+ defer harness.destroy(std.testing.allocator);
18
+ try std.testing.expect(harness.runtime.options.credentials_enabled);
19
+ try std.testing.expectEqualStrings("dev.native_sdk.app", harness.runtime.options.platform.app_info.bundle_id);
20
+ }
21
+
22
+ fn takeResult(fx: *Fx) !effects_mod.EffectCredentialsResult {
23
+ for (0..100_000) |_| {
24
+ if (fx.takeMsg()) |msg| return msg.result;
25
+ std.Thread.yield() catch {};
26
+ }
27
+ return error.TestExpectedMsg;
28
+ }
29
+
30
+ const BlockingCredentialStore = struct {
31
+ first_entered: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
32
+ release_first: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
33
+ first_exited: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
34
+ abandon_noted: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
35
+ call_count: std.atomic.Value(usize) = std.atomic.Value(usize).init(0),
36
+ mutex: std.atomic.Mutex = .unlocked,
37
+ first_secret: [32]u8 = undefined,
38
+ first_secret_len: usize = 0,
39
+ stored_secret: [32]u8 = undefined,
40
+ stored_secret_len: usize = 0,
41
+
42
+ fn lock(self: *BlockingCredentialStore) void {
43
+ while (!self.mutex.tryLock()) std.atomic.spinLoopHint();
44
+ }
45
+
46
+ fn set(context: ?*anyopaque, credential: platform.Credential) anyerror!void {
47
+ const self: *BlockingCredentialStore = @ptrCast(@alignCast(context));
48
+ const call = self.call_count.fetchAdd(1, .acq_rel);
49
+ if (call == 0) {
50
+ self.lock();
51
+ const first_len = @min(credential.secret.len, self.first_secret.len);
52
+ @memcpy(self.first_secret[0..first_len], credential.secret[0..first_len]);
53
+ self.first_secret_len = first_len;
54
+ self.mutex.unlock();
55
+ self.first_entered.store(true, .release);
56
+ while (!self.release_first.load(.acquire)) std.atomic.spinLoopHint();
57
+ }
58
+ self.lock();
59
+ const stored_len = @min(credential.secret.len, self.stored_secret.len);
60
+ @memcpy(self.stored_secret[0..stored_len], credential.secret[0..stored_len]);
61
+ self.stored_secret_len = stored_len;
62
+ self.mutex.unlock();
63
+ if (call == 0) self.first_exited.store(true, .release);
64
+ }
65
+
66
+ fn noteAbandoned(context: ?*anyopaque) void {
67
+ const self: *BlockingCredentialStore = @ptrCast(@alignCast(context));
68
+ self.abandon_noted.store(true, .release);
69
+ }
70
+
71
+ fn waitFor(value: *std.atomic.Value(bool)) !void {
72
+ for (0..1_000_000) |_| {
73
+ if (value.load(.acquire)) return;
74
+ std.Thread.yield() catch {};
75
+ }
76
+ return error.TestExpectedSignal;
77
+ }
78
+ };
79
+
80
+ test "credential effects round-trip through the hermetic platform backing" {
81
+ var null_platform = platform.NullPlatform.init(.{});
82
+ defer null_platform.deinit();
83
+ var services = null_platform.platform();
84
+ var fx = Fx.init(std.testing.allocator);
85
+ defer fx.deinit();
86
+ fx.bindCredentialsStore(.{
87
+ .services = &services.services,
88
+ .service = "dev.native-sdk.credentials-test",
89
+ .permitted = true,
90
+ });
91
+
92
+ fx.credentialsSet(.{
93
+ .key = 1,
94
+ .credential_key = "api-token",
95
+ .secret = "live-secret",
96
+ .on_result = Fx.credentialsMsg(.result),
97
+ });
98
+ const set = try takeResult(&fx);
99
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.ok, set.outcome);
100
+ try std.testing.expectEqualStrings("dev.native-sdk.credentials-test", null_platform.lastCredentialService());
101
+ try std.testing.expectEqualStrings("api-token", null_platform.lastCredentialAccount());
102
+
103
+ fx.credentialsGet(.{ .key = 2, .credential_key = "api-token", .on_result = Fx.credentialsMsg(.result) });
104
+ const get = try takeResult(&fx);
105
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.ok, get.outcome);
106
+ try std.testing.expectEqualStrings("live-secret", get.bytes);
107
+
108
+ fx.credentialsDelete(.{ .key = 3, .credential_key = "api-token", .on_result = Fx.credentialsMsg(.result) });
109
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.ok, (try takeResult(&fx)).outcome);
110
+ fx.credentialsGet(.{ .key = 4, .credential_key = "api-token", .on_result = Fx.credentialsMsg(.result) });
111
+ const miss = try takeResult(&fx);
112
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.miss, miss.outcome);
113
+ try std.testing.expectEqualStrings("", miss.bytes);
114
+
115
+ // Delete is idempotent even though the platform reports absence.
116
+ fx.credentialsDelete(.{ .key = 5, .credential_key = "api-token", .on_result = Fx.credentialsMsg(.result) });
117
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.ok, (try takeResult(&fx)).outcome);
118
+
119
+ // Uint8Array includes the empty byte string; presence is independent
120
+ // from payload length all the way through the platform adapter.
121
+ fx.credentialsSet(.{
122
+ .key = 6,
123
+ .credential_key = "empty",
124
+ .secret = "",
125
+ .on_result = Fx.credentialsMsg(.result),
126
+ });
127
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.ok, (try takeResult(&fx)).outcome);
128
+ fx.credentialsGet(.{ .key = 7, .credential_key = "empty", .on_result = Fx.credentialsMsg(.result) });
129
+ const empty = try takeResult(&fx);
130
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.ok, empty.outcome);
131
+ try std.testing.expectEqual(@as(usize, 0), empty.bytes.len);
132
+
133
+ // The TypeScript surface accepts Uint8Array, not text: zeros and invalid
134
+ // UTF-8 must survive the complete worker/platform/result path.
135
+ const binary = [_]u8{ 0x00, 0xff, 0x80, 0x41, 0x00 };
136
+ fx.credentialsSet(.{
137
+ .key = 8,
138
+ .credential_key = "binary",
139
+ .secret = &binary,
140
+ .on_result = Fx.credentialsMsg(.result),
141
+ });
142
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.ok, (try takeResult(&fx)).outcome);
143
+ fx.credentialsGet(.{ .key = 9, .credential_key = "binary", .on_result = Fx.credentialsMsg(.result) });
144
+ const binary_get = try takeResult(&fx);
145
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.ok, binary_get.outcome);
146
+ try std.testing.expectEqualSlices(u8, &binary, binary_get.bytes);
147
+ }
148
+
149
+ test "credential permission and bounds failures are staged closed outcomes" {
150
+ try std.testing.expectEqual(@as(usize, 2_560), effects_mod.max_effect_credentials_secret_bytes);
151
+ var null_platform = platform.NullPlatform.init(.{});
152
+ defer null_platform.deinit();
153
+ var services = null_platform.platform();
154
+ var fx = Fx.init(std.testing.allocator);
155
+ defer fx.deinit();
156
+
157
+ // A refused batch remains loud and ordered even though no worker starts.
158
+ fx.credentialsSet(.{ .key = 10, .credential_key = "one", .secret = "a", .on_result = Fx.credentialsMsg(.result) });
159
+ fx.credentialsGet(.{ .key = 11, .credential_key = "two", .on_result = Fx.credentialsMsg(.result) });
160
+ fx.credentialsDelete(.{ .key = 12, .credential_key = "three", .on_result = Fx.credentialsMsg(.result) });
161
+ const denied_set = try takeResult(&fx);
162
+ const denied_get = try takeResult(&fx);
163
+ const denied_delete = try takeResult(&fx);
164
+ try std.testing.expectEqual(@as(u64, 10), denied_set.key);
165
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOperation.set, denied_set.operation);
166
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.denied, denied_set.outcome);
167
+ try std.testing.expectEqual(@as(u64, 11), denied_get.key);
168
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOperation.get, denied_get.operation);
169
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.denied, denied_get.outcome);
170
+ try std.testing.expectEqual(@as(u64, 12), denied_delete.key);
171
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOperation.delete, denied_delete.operation);
172
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.denied, denied_delete.outcome);
173
+
174
+ fx.bindCredentialsStore(.{
175
+ .services = &services.services,
176
+ .service = "dev.native-sdk.credentials-test",
177
+ .permitted = true,
178
+ });
179
+ var oversized_key: [effects_mod.max_effect_credentials_key_bytes + 1]u8 = undefined;
180
+ @memset(&oversized_key, 'k');
181
+ fx.credentialsGet(.{ .key = 13, .credential_key = &oversized_key, .on_result = Fx.credentialsMsg(.result) });
182
+ const bounded = try takeResult(&fx);
183
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.over_bound, bounded.outcome);
184
+
185
+ fx.credentialsGet(.{ .key = 14, .credential_key = "prefix\x00suffix", .on_result = Fx.credentialsMsg(.result) });
186
+ const nul = try takeResult(&fx);
187
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.over_bound, nul.outcome);
188
+
189
+ var oversized_secret: [effects_mod.max_effect_credentials_secret_bytes + 1]u8 = undefined;
190
+ @memset(&oversized_secret, 's');
191
+ fx.credentialsSet(.{ .key = 15, .credential_key = "token", .secret = &oversized_secret, .on_result = Fx.credentialsMsg(.result) });
192
+ const secret = try takeResult(&fx);
193
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.over_bound, secret.outcome);
194
+ }
195
+
196
+ test "real credential replacements coalesce and execute in issue order" {
197
+ var backing: BlockingCredentialStore = .{};
198
+ var services: platform.PlatformServices = .{
199
+ .context = &backing,
200
+ .set_credential_fn = BlockingCredentialStore.set,
201
+ .note_blocking_call_abandoned_fn = BlockingCredentialStore.noteAbandoned,
202
+ };
203
+ var fx = Fx.init(std.testing.allocator);
204
+ defer fx.deinit();
205
+ fx.bindCredentialsStore(.{
206
+ .services = &services,
207
+ .service = "dev.native-sdk.credentials-test",
208
+ .permitted = true,
209
+ });
210
+
211
+ fx.credentialsSet(.{ .key = 40, .credential_key = "token", .secret = "old", .on_result = Fx.credentialsMsg(.result) });
212
+ try BlockingCredentialStore.waitFor(&backing.first_entered);
213
+ fx.credentialsSet(.{ .key = 40, .credential_key = "token", .secret = "new-1", .on_result = Fx.credentialsMsg(.result) });
214
+ fx.credentialsSet(.{ .key = 40, .credential_key = "token", .secret = "new-2", .on_result = Fx.credentialsMsg(.result) });
215
+ fx.credentialsSet(.{ .key = 40, .credential_key = "token", .secret = "new-3", .on_result = Fx.credentialsMsg(.result) });
216
+ fx.credentialsSet(.{ .key = 40, .credential_key = "token", .secret = "new-4", .on_result = Fx.credentialsMsg(.result) });
217
+ fx.credentialsSet(.{ .key = 40, .credential_key = "token", .secret = "new-5", .on_result = Fx.credentialsMsg(.result) });
218
+ try std.testing.expectEqual(@as(usize, 1), backing.call_count.load(.acquire));
219
+
220
+ backing.release_first.store(true, .release);
221
+ const result = try takeResult(&fx);
222
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.ok, result.outcome);
223
+ try std.testing.expectEqual(@as(usize, 2), backing.call_count.load(.acquire));
224
+ backing.lock();
225
+ defer backing.mutex.unlock();
226
+ try std.testing.expectEqualStrings("old", backing.first_secret[0..backing.first_secret_len]);
227
+ try std.testing.expectEqualStrings("new-5", backing.stored_secret[0..backing.stored_secret_len]);
228
+ }
229
+
230
+ test "credential teardown abandons a blocked platform call at its deadline" {
231
+ var backing: BlockingCredentialStore = .{};
232
+ var services: platform.PlatformServices = .{
233
+ .context = &backing,
234
+ .set_credential_fn = BlockingCredentialStore.set,
235
+ .note_blocking_call_abandoned_fn = BlockingCredentialStore.noteAbandoned,
236
+ };
237
+ var fx = Fx.init(std.testing.allocator);
238
+ fx.credentials_join_deadline_ms = 10;
239
+ fx.bindCredentialsStore(.{
240
+ .services = &services,
241
+ .service = "dev.native-sdk.credentials-test",
242
+ .permitted = true,
243
+ });
244
+ fx.credentialsSet(.{ .key = 41, .credential_key = "token", .secret = "blocked", .on_result = Fx.credentialsMsg(.result) });
245
+ try BlockingCredentialStore.waitFor(&backing.first_entered);
246
+
247
+ const start_ns = clock_mod.monotonicNanoseconds();
248
+ fx.deinit();
249
+ const elapsed_ms = (clock_mod.monotonicNanoseconds() - start_ns) / std.time.ns_per_ms;
250
+ try std.testing.expect(elapsed_ms < 2_000);
251
+ try std.testing.expectEqual(@as(u32, 1), fx.abandoned_credentials_workers);
252
+ try std.testing.expect(backing.abandon_noted.load(.acquire));
253
+
254
+ backing.release_first.store(true, .release);
255
+ try BlockingCredentialStore.waitFor(&backing.first_exited);
256
+ }
257
+
258
+ test "credential effects replace by key and own four slots" {
259
+ var null_platform = platform.NullPlatform.init(.{});
260
+ defer null_platform.deinit();
261
+ var services = null_platform.platform();
262
+ var fx = Fx.init(std.testing.allocator);
263
+ defer fx.deinit();
264
+ fx.executor = .fake;
265
+ fx.bindCredentialsStore(.{
266
+ .services = &services.services,
267
+ .service = "dev.native-sdk.credentials-test",
268
+ .permitted = true,
269
+ });
270
+
271
+ fx.credentialsGet(.{ .key = 30, .credential_key = "old", .on_result = Fx.credentialsMsg(.result) });
272
+ fx.credentialsGet(.{ .key = 30, .credential_key = "replacement", .on_result = Fx.credentialsMsg(.result) });
273
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingHostCount());
274
+ try std.testing.expectEqualStrings("replacement", blk: {
275
+ var at: usize = 0;
276
+ const payload = fx.pendingHostAt(0).?.payload;
277
+ const len: usize = std.mem.readInt(u32, payload[0..4], .little);
278
+ at += 4;
279
+ break :blk payload[at .. at + len];
280
+ });
281
+
282
+ // The replacement occupies one credential-reserved slot; three more fit,
283
+ // while the fifth concurrent operation is rejected without borrowing a
284
+ // file/fetch or record-store slot.
285
+ fx.credentialsGet(.{ .key = 31, .credential_key = "a", .on_result = Fx.credentialsMsg(.result) });
286
+ fx.credentialsGet(.{ .key = 32, .credential_key = "b", .on_result = Fx.credentialsMsg(.result) });
287
+ fx.credentialsGet(.{ .key = 33, .credential_key = "c", .on_result = Fx.credentialsMsg(.result) });
288
+ try std.testing.expectEqual(effects_mod.max_credentials_effects, fx.pendingHostCount());
289
+ fx.credentialsGet(.{ .key = 34, .credential_key = "full", .on_result = Fx.credentialsMsg(.result) });
290
+ const rejected = try takeResult(&fx);
291
+ try std.testing.expectEqual(@as(u64, 34), rejected.key);
292
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.rejected, rejected.outcome);
293
+ }
294
+
295
+ test "credential replay delivers a deterministic redacted placeholder" {
296
+ var null_platform = platform.NullPlatform.init(.{});
297
+ defer null_platform.deinit();
298
+ var services = null_platform.platform();
299
+ var fx = Fx.init(std.testing.allocator);
300
+ defer fx.deinit();
301
+ fx.executor = .fake;
302
+ fx.bindCredentialsStore(.{
303
+ .services = &services.services,
304
+ .service = "dev.native-sdk.credentials-test",
305
+ .permitted = true,
306
+ });
307
+
308
+ fx.credentialsGet(.{ .key = 20, .credential_key = "token", .on_result = Fx.credentialsMsg(.result) });
309
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingHostCount());
310
+ const request = fx.pendingHostAt(0).?;
311
+ try std.testing.expectEqualStrings("core.credentials.get", request.name);
312
+
313
+ const digest = [_]u8{0x5a} ** 32;
314
+ try fx.feedCredentialsResult(20, .get, .ok, 12, digest);
315
+ const first = try takeResult(&fx);
316
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.ok, first.outcome);
317
+ try std.testing.expectEqual(@as(usize, 12), first.bytes.len);
318
+ try std.testing.expect(!std.mem.eql(u8, "live-secret", first.bytes));
319
+ var first_copy: [12]u8 = undefined;
320
+ @memcpy(&first_copy, first.bytes);
321
+
322
+ fx.credentialsGet(.{ .key = 21, .credential_key = "token", .on_result = Fx.credentialsMsg(.result) });
323
+ try fx.feedCredentialsResult(21, .get, .ok, 12, digest);
324
+ const second = try takeResult(&fx);
325
+ try std.testing.expectEqualSlices(u8, &first_copy, second.bytes);
326
+
327
+ fx.credentialsGet(.{ .key = 22, .credential_key = "missing", .on_result = Fx.credentialsMsg(.result) });
328
+ try fx.feedCredentialsResult(22, .get, .miss, 0, [_]u8{0} ** 32);
329
+ const miss = try takeResult(&fx);
330
+ try std.testing.expectEqual(effects_mod.EffectCredentialsOutcome.miss, miss.outcome);
331
+ try std.testing.expectEqualStrings("", miss.bytes);
332
+ }