@native-sdk/cli 0.4.3 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,242 @@
1
+ //! The runtime half of the native-only-host contract: a build whose
2
+ //! app.zon declared no web use ships without the embedded web layer
3
+ //! (`Options.web_layer = false`), and every path that would create a
4
+ //! webview fails fast with `error.WebViewLayerNotBuilt` and its teaching
5
+ //! message — never a platform call into a host whose web layer was
6
+ //! compiled out, and never a misleading not-found.
7
+
8
+ const support = @import("test_support.zig");
9
+ const std = support.std;
10
+ const geometry = support.geometry;
11
+ const app_manifest = support.app_manifest;
12
+ const automation = support.automation;
13
+ const platform = support.platform;
14
+ const App = support.App;
15
+ const Event = support.Event;
16
+ const Runtime = support.Runtime;
17
+ const TestHarness = support.TestHarness;
18
+
19
+ const teaching_needle = "built without the web layer";
20
+
21
+ /// A native-only app shape: default empty source, one canvas scene.
22
+ const canvas_scene_views = [_]app_manifest.ShellView{
23
+ .{ .label = "canvas", .kind = .gpu_surface, .fill = true },
24
+ };
25
+ const canvas_scene_windows = [_]app_manifest.ShellWindow{
26
+ .{ .label = "main", .views = &canvas_scene_views },
27
+ };
28
+
29
+ const CanvasApp = struct {
30
+ fn app(self: *@This()) App {
31
+ return .{ .context = self, .name = "native-only", .scene_fn = scene };
32
+ }
33
+
34
+ fn scene(context: *anyopaque) anyerror!app_manifest.ShellConfig {
35
+ _ = context;
36
+ return .{ .windows = &canvas_scene_windows };
37
+ }
38
+ };
39
+
40
+ test "native-only runtime starts a canvas scene and refuses webview creation with the teaching error" {
41
+ const harness = try TestHarness().create(std.testing.allocator, .{});
42
+ defer harness.destroy(std.testing.allocator);
43
+ // The null platform's gpu_surface support is opt-in; the fixture
44
+ // scene is a canvas window, so switch it on like a real host.
45
+ harness.null_platform.gpu_surfaces = true;
46
+ harness.runtime.options.web_layer = false;
47
+ var app_state: CanvasApp = .{};
48
+ try harness.start(app_state.app());
49
+
50
+ // The canvas scene booted without touching the web layer.
51
+ try std.testing.expectEqual(@as(usize, 0), harness.null_platform.webview_count);
52
+
53
+ // Direct webview view creation (the shell/bridge choke point).
54
+ try std.testing.expectError(error.WebViewLayerNotBuilt, harness.runtime.createView(.{
55
+ .window_id = 1,
56
+ .label = "preview",
57
+ .kind = .webview,
58
+ .frame = geometry.RectF.init(0, 0, 320, 240),
59
+ .url = "https://example.com",
60
+ }));
61
+
62
+ // A window whose source would materialize a main webview.
63
+ try std.testing.expectError(error.WebViewLayerNotBuilt, harness.runtime.createWindow(.{
64
+ .label = "second",
65
+ .title = "Second",
66
+ .default_frame = geometry.RectF.init(0, 0, 400, 300),
67
+ .source = platform.WebViewSource.url("https://example.com"),
68
+ }));
69
+ try std.testing.expectEqual(@as(usize, 0), harness.null_platform.webview_count);
70
+ }
71
+
72
+ test "native-only runtime answers webview bridge verbs with the teaching error" {
73
+ const harness = try TestHarness().create(std.testing.allocator, .{});
74
+ defer harness.destroy(std.testing.allocator);
75
+ harness.null_platform.gpu_surfaces = true;
76
+ harness.runtime.options.web_layer = false;
77
+ harness.runtime.options.js_window_api = true;
78
+ const origins = [_][]const u8{"zero://inline"};
79
+ harness.runtime.options.security.navigation.allowed_origins = &origins;
80
+ var app_state: CanvasApp = .{};
81
+ try harness.start(app_state.app());
82
+
83
+ try harness.runtime.dispatchPlatformEvent(app_state.app(), .{ .bridge_message = .{
84
+ .bytes = "{\"id\":\"1\",\"command\":\"native-sdk.webview.create\",\"payload\":{\"label\":\"preview\",\"url\":\"https://example.com\",\"frame\":{\"width\":300,\"height\":200}}}",
85
+ .origin = "zero://inline",
86
+ .window_id = 1,
87
+ } });
88
+ const response = harness.null_platform.lastBridgeResponse();
89
+ try std.testing.expect(std.mem.indexOf(u8, response, "\"ok\":false") != null);
90
+ try std.testing.expect(std.mem.indexOf(u8, response, teaching_needle) != null);
91
+ try std.testing.expectEqual(@as(usize, 0), harness.null_platform.webview_count);
92
+ }
93
+
94
+ test "native-only runtime fails fast when a source app reaches webview startup" {
95
+ const SourceApp = struct {
96
+ fn app(self: *@This()) App {
97
+ return .{ .context = self, .name = "source-app", .source = platform.WebViewSource.html("<p>web</p>") };
98
+ }
99
+ };
100
+
101
+ const harness = try TestHarness().create(std.testing.allocator, .{});
102
+ defer harness.destroy(std.testing.allocator);
103
+ harness.runtime.options.web_layer = false;
104
+ var app_state: SourceApp = .{};
105
+ try std.testing.expectError(error.WebViewLayerNotBuilt, harness.start(app_state.app()));
106
+ }
107
+
108
+ /// The scripted platform loop for the automation-driven session below:
109
+ /// startup, one frame, then two dropbox commands written from the loop's
110
+ /// own thread (like a driver writing while the app runs) and drained by
111
+ /// the following frames — deterministic, no watcher timing involved.
112
+ const NativeOnlyAutomationLoop = struct {
113
+ null_platform: *platform.NullPlatform,
114
+ automation_dir: []const u8,
115
+
116
+ fn run(context: *anyopaque, handler: platform.EventHandler, handler_context: *anyopaque) anyerror!void {
117
+ const self: *NativeOnlyAutomationLoop = @ptrCast(@alignCast(context));
118
+ try handler(handler_context, .app_start);
119
+ try handler(handler_context, .{ .surface_resized = self.null_platform.surface_value });
120
+ try handler(handler_context, .frame_requested);
121
+
122
+ var path_buffer: [160]u8 = undefined;
123
+ try std.Io.Dir.cwd().writeFile(std.testing.io, .{
124
+ .sub_path = try std.fmt.bufPrint(&path_buffer, "{s}/command-1.txt", .{self.automation_dir}),
125
+ .data = "menu-command probe.normal\n",
126
+ });
127
+ try std.Io.Dir.cwd().writeFile(std.testing.io, .{
128
+ .sub_path = try std.fmt.bufPrint(&path_buffer, "{s}/command-2.txt", .{self.automation_dir}),
129
+ .data = "menu-command probe.webview\n",
130
+ });
131
+ try handler(handler_context, .frame_requested);
132
+ try handler(handler_context, .frame_requested);
133
+ try handler(handler_context, .app_shutdown);
134
+ }
135
+ };
136
+
137
+ /// A native-only canvas app driven entirely through automation: a
138
+ /// normal command mutates state, and a command that tries to create a
139
+ /// webview records the gate's answer.
140
+ const NativeOnlyAutomationApp = struct {
141
+ normal_commands: usize = 0,
142
+ webview_error: ?anyerror = null,
143
+
144
+ fn scene(context: *anyopaque) anyerror!app_manifest.ShellConfig {
145
+ _ = context;
146
+ return .{ .windows = &canvas_scene_windows };
147
+ }
148
+
149
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
150
+ const self: *NativeOnlyAutomationApp = @ptrCast(@alignCast(context));
151
+ if (event_value != .command) return;
152
+ if (std.mem.eql(u8, event_value.command.name, "probe.normal")) {
153
+ self.normal_commands += 1;
154
+ return;
155
+ }
156
+ if (std.mem.eql(u8, event_value.command.name, "probe.webview")) {
157
+ _ = runtime.createView(.{
158
+ .window_id = 1,
159
+ .label = "preview",
160
+ .kind = .webview,
161
+ .frame = geometry.RectF.init(0, 0, 320, 240),
162
+ .url = "https://example.com",
163
+ }) catch |err| {
164
+ self.webview_error = err;
165
+ return;
166
+ };
167
+ self.webview_error = null;
168
+ }
169
+ }
170
+
171
+ fn app(self: *NativeOnlyAutomationApp) App {
172
+ return .{
173
+ .context = self,
174
+ .name = "native-only-automation",
175
+ .scene_fn = scene,
176
+ .event_fn = event,
177
+ };
178
+ }
179
+ };
180
+
181
+ test "automation drives a native-only session: commands work, webview creation teaches" {
182
+ // Pid-suffixed dropbox (this test compiles into more than one test
183
+ // binary; parallel copies must not share a directory).
184
+ var dir_buffer: [64]u8 = undefined;
185
+ const automation_dir = try automation.watcher.testDirectory(&dir_buffer, ".zig-cache/test-automation-web-layer");
186
+ var cwd = std.Io.Dir.cwd();
187
+ cwd.deleteTree(std.testing.io, automation_dir) catch {};
188
+ try cwd.createDirPath(std.testing.io, automation_dir);
189
+ defer cwd.deleteTree(std.testing.io, automation_dir) catch {};
190
+
191
+ // Heap-hosted: NullPlatform and Runtime are both multi-megabyte.
192
+ const null_platform = try std.heap.page_allocator.create(platform.NullPlatform);
193
+ defer std.heap.page_allocator.destroy(null_platform);
194
+ null_platform.* = platform.NullPlatform.init(.{});
195
+ null_platform.gpu_surfaces = true;
196
+ var loop: NativeOnlyAutomationLoop = .{ .null_platform = null_platform, .automation_dir = automation_dir };
197
+
198
+ var platform_value = null_platform.platform();
199
+ platform_value.run_fn = NativeOnlyAutomationLoop.run;
200
+ platform_value.context = &loop;
201
+
202
+ const runtime = try std.heap.page_allocator.create(Runtime);
203
+ defer std.heap.page_allocator.destroy(runtime);
204
+ Runtime.initAt(runtime, .{
205
+ .platform = platform_value,
206
+ .automation = automation.Server.init(std.testing.io, automation_dir, "WebLayerGate"),
207
+ .web_layer = false,
208
+ });
209
+
210
+ var app_state: NativeOnlyAutomationApp = .{};
211
+ try runtime.run(app_state.app());
212
+
213
+ // Normal operation: the automation-driven command reached the app.
214
+ try std.testing.expectEqual(@as(usize, 1), app_state.normal_commands);
215
+ // The automation-driven webview attempt got the teaching error, and
216
+ // no webview ever reached the platform host.
217
+ try std.testing.expectEqual(@as(?anyerror, error.WebViewLayerNotBuilt), app_state.webview_error);
218
+ try std.testing.expectEqual(@as(usize, 0), null_platform.webview_count);
219
+ }
220
+
221
+ test "web-layer builds keep every webview path working (control)" {
222
+ const SourceApp = struct {
223
+ fn app(self: *@This()) App {
224
+ return .{ .context = self, .name = "source-app", .source = platform.WebViewSource.html("<p>web</p>") };
225
+ }
226
+ };
227
+
228
+ const harness = try TestHarness().create(std.testing.allocator, .{});
229
+ defer harness.destroy(std.testing.allocator);
230
+ var app_state: SourceApp = .{};
231
+ try harness.start(app_state.app());
232
+ const webview_origins = [_][]const u8{"https://example.com"};
233
+ harness.runtime.options.security.navigation.allowed_origins = &webview_origins;
234
+ _ = try harness.runtime.createView(.{
235
+ .window_id = 1,
236
+ .label = "preview",
237
+ .kind = .webview,
238
+ .frame = geometry.RectF.init(0, 0, 320, 240),
239
+ .url = "https://example.com",
240
+ });
241
+ try std.testing.expectEqual(@as(usize, 1), harness.null_platform.webview_count);
242
+ }
@@ -70,6 +70,11 @@ pub fn RuntimeWindowStorage(comptime Runtime: type) type {
70
70
  errdefer Self.removeWindowAt(self, index);
71
71
  errdefer if (native_created) self.options.platform.services.closeWindow(id) catch {};
72
72
 
73
+ // Materializing a window source means creating its main
74
+ // webview — refused before the native window exists when the
75
+ // build has no web layer, so the error names the real cause.
76
+ if (self.windows[index].source != null and !self.options.web_layer) return error.WebViewLayerNotBuilt;
77
+
73
78
  const window_options = options.windowOptions(id, self.windows[index].info.label);
74
79
  const native_info = try self.options.platform.services.createWindow(window_options);
75
80
  native_created = true;
@@ -321,6 +321,7 @@ pub fn RuntimeWindowViewRuntime(comptime Runtime: type) type {
321
321
  }
322
322
 
323
323
  pub fn createWebViewView(self: *Runtime, options: platform.ViewOptions) !platform.ViewInfo {
324
+ if (!self.options.web_layer) return error.WebViewLayerNotBuilt;
324
325
  try validateChildWebViewLabel(options.label);
325
326
  try Self.validateWebViewUrl(self, options.url);
326
327
  if (!isValidWebViewFrame(options.frame)) return error.InvalidWebViewOptions;
@@ -67,7 +67,13 @@ pub fn resolveFrameworkRoot(allocator: std.mem.Allocator, io: std.Io, env_map: *
67
67
  if (env_map.get("NATIVE_SDK_PATH")) |path| {
68
68
  if (path.len > 0 and hasFrameworkRoot(allocator, io, path)) {
69
69
  if (std.fs.path.isAbsolute(path)) return try allocator.dupe(u8, path);
70
- return try std.Io.Dir.cwd().realPathFileAlloc(io, path, allocator);
70
+ // realPathFileAlloc returns a sentinel-terminated allocation
71
+ // (length + 1 bytes); this function's contract is a plain
72
+ // slice the caller frees, so re-dupe without the sentinel or
73
+ // that free is off by one byte.
74
+ const real_path = try std.Io.Dir.cwd().realPathFileAlloc(io, path, allocator);
75
+ defer allocator.free(real_path);
76
+ return try allocator.dupe(u8, real_path);
71
77
  }
72
78
  }
73
79
 
@@ -24,6 +24,10 @@ pub const Metadata = struct {
24
24
  capabilities: []const []const u8 = &.{},
25
25
  bridge_commands: []const BridgeCommandMetadata = &.{},
26
26
  web_engine: []const u8 = "system",
27
+ /// Whether the app ships the embedded web layer: "auto" (default,
28
+ /// inferred from the manifest's web declarations), "include", or
29
+ /// "exclude". See `webLayer` for the inference.
30
+ webview_layer: []const u8 = "auto",
27
31
  /// The built-in theme pack the app selects (`theme = "geist"`).
28
32
  /// Optional — absent keeps the house register. Validated against
29
33
  /// the known pack names so a typo is a check error, never a silent
@@ -52,6 +56,7 @@ pub const Metadata = struct {
52
56
  if (self.theme) |value| allocator.free(value);
53
57
  allocator.free(self.version);
54
58
  allocator.free(self.web_engine);
59
+ allocator.free(self.webview_layer);
55
60
  allocator.free(self.cef.dir);
56
61
  for (self.icons) |value| allocator.free(value);
57
62
  if (self.icons.len > 0) allocator.free(self.icons);
@@ -399,6 +404,7 @@ pub fn validateFile(allocator: std.mem.Allocator, io: std.Io, path: []const u8)
399
404
  const url_schemes = parseUrlSchemes(allocator, metadata.url_schemes) catch return .{ .ok = false, .message = "app.zon URL schemes are invalid" };
400
405
  defer allocator.free(url_schemes);
401
406
  const manifest_web_engine = parseWebEngine(metadata.web_engine) catch return .{ .ok = false, .message = "app.zon web engine is invalid" };
407
+ const manifest_webview_layer = parseWebViewLayer(metadata.webview_layer) catch return .{ .ok = false, .message = "app.zon webview_layer is invalid - expected \"auto\", \"include\", or \"exclude\"" };
402
408
  const platform_settings = parsePlatformSettings(allocator, metadata.platforms) catch return .{ .ok = false, .message = "app.zon platforms are invalid" };
403
409
  defer allocator.free(platform_settings);
404
410
 
@@ -419,9 +425,16 @@ pub fn validateFile(allocator: std.mem.Allocator, io: std.Io, path: []const u8)
419
425
  .file_associations = file_associations,
420
426
  .url_schemes = url_schemes,
421
427
  .cef = .{ .dir = metadata.cef.dir, .auto_install = metadata.cef.auto_install },
428
+ .webview_layer = manifest_webview_layer,
422
429
  .package = .{ .web_engine = manifest_web_engine },
423
430
  };
424
- app_manifest.validateManifest(manifest) catch return .{ .ok = false, .message = "manifest fields failed semantic validation" };
431
+ app_manifest.validateManifest(manifest) catch |err| return .{
432
+ .ok = false,
433
+ .message = switch (err) {
434
+ error.WebViewLayerConflict => web_layer_conflict_message,
435
+ else => "manifest fields failed semantic validation",
436
+ },
437
+ };
425
438
  return .{ .ok = true, .message = "app.zon is valid" };
426
439
  }
427
440
 
@@ -474,6 +487,7 @@ pub fn parseText(allocator: std.mem.Allocator, source: []const u8) !Metadata {
474
487
  .capabilities = try duplicateStringList(allocator, raw.capabilities),
475
488
  .bridge_commands = try convertRawBridgeCommands(allocator, raw.bridge.commands),
476
489
  .web_engine = try allocator.dupe(u8, raw.web_engine),
490
+ .webview_layer = try allocator.dupe(u8, raw.webview_layer),
477
491
  .cef = .{
478
492
  .dir = try allocator.dupe(u8, raw.cef.dir),
479
493
  .auto_install = raw.cef.auto_install,
@@ -1318,6 +1332,113 @@ fn parseWebEngine(value: []const u8) !app_manifest.WebEngine {
1318
1332
  return error.InvalidWebEngine;
1319
1333
  }
1320
1334
 
1335
+ fn parseWebViewLayer(value: []const u8) !app_manifest.WebViewLayer {
1336
+ if (std.mem.eql(u8, value, "auto")) return .auto;
1337
+ if (std.mem.eql(u8, value, "include")) return .include;
1338
+ if (std.mem.eql(u8, value, "exclude")) return .exclude;
1339
+ return error.InvalidWebViewLayer;
1340
+ }
1341
+
1342
+ /// The teaching message every boundary prints for the same
1343
+ /// contradiction: a manifest that excludes the web layer while declaring
1344
+ /// web content.
1345
+ pub const web_layer_conflict_message = "app.zon sets .webview_layer = \"exclude\" but the app declares web content (a .frontend block, the \"webview\" capability, a .shell webview view, or the Chromium web engine - from .web_engine or --web-engine) - remove the web declarations or drop the exclude";
1346
+
1347
+ /// The same contradiction arriving through the CLI flag instead of the
1348
+ /// manifest field: `--web-layer exclude` against an app that declares
1349
+ /// web content.
1350
+ pub const web_layer_flag_conflict_message = "--web-layer exclude contradicts the app's web declarations (a .frontend block, the \"webview\" capability, a .shell webview view, or the Chromium web engine - from .web_engine or --web-engine) - remove the web declarations or drop the flag";
1351
+
1352
+ /// Why the web layer is (or is not) in the build, for verdict lines —
1353
+ /// the shared contract's reason set.
1354
+ pub const WebLayerReason = app_manifest.web_layer.Reason;
1355
+
1356
+ /// The layer setting a boundary resolves before deciding: "auto",
1357
+ /// "include", or "exclude" — the shared contract's input enum, exported
1358
+ /// for the CLI's `--web-layer` flag.
1359
+ pub const WebViewLayerSetting = app_manifest.WebViewLayer;
1360
+
1361
+ /// Parse a `--web-layer` flag value (auto|include|exclude), via the
1362
+ /// shared contract so the flag and the app.zon field accept exactly the
1363
+ /// same vocabulary.
1364
+ pub const parseWebViewLayerSetting = app_manifest.web_layer.parseWebViewLayer;
1365
+
1366
+ pub const WebLayer = struct {
1367
+ enabled: bool,
1368
+ reason: WebLayerReason,
1369
+ /// Whether the deciding include/exclude came from the CLI's
1370
+ /// `--web-layer` flag rather than app.zon's `.webview_layer`; only
1371
+ /// meaningful for the declared_include/declared_exclude reasons.
1372
+ from_flag: bool = false,
1373
+
1374
+ /// The parenthesized half of a verdict line: `web layer: none
1375
+ /// (inferred)` / `web layer: webview2 (declared: capabilities)`.
1376
+ pub fn sourceText(self: WebLayer) []const u8 {
1377
+ return switch (self.reason) {
1378
+ .inferred_native_only => "inferred: nothing in app.zon declares web use",
1379
+ .declared_exclude => if (self.from_flag) "declared: --web-layer exclude" else "declared: .webview_layer = \"exclude\"",
1380
+ .capability => "declared: capabilities",
1381
+ .frontend => "declared: .frontend",
1382
+ .shell_webview => "declared: .shell webview view",
1383
+ .chromium_engine => "declared: the Chromium web engine (.web_engine or --web-engine)",
1384
+ .declared_include => if (self.from_flag) "declared: --web-layer include" else "declared: .webview_layer = \"include\"",
1385
+ // Only the build graph's lenient parse can produce this
1386
+ // reason; parsed metadata always reaches this fn readable.
1387
+ .unreadable_manifest => "kept: app.zon could not be parsed",
1388
+ };
1389
+ }
1390
+ };
1391
+
1392
+ pub const WebLayerError = error{ InvalidWebViewLayer, WebViewLayerConflict };
1393
+
1394
+ /// The CLI-side adapter over the shared web-layer contract
1395
+ /// (app_manifest.web_layer): the same declare-to-use rule the build
1396
+ /// graph and the runner apply, fed the engine this boundary RESOLVED —
1397
+ /// `--web-engine` orelse app.zon, already resolved by the CLI's verb
1398
+ /// handlers. `.webview_layer = "include"|"exclude"` overrides, and an
1399
+ /// exclude that contradicts a web declaration (including a resolved
1400
+ /// Chromium engine) is refused.
1401
+ pub fn webLayer(metadata: Metadata, resolved_engine: web_engine_tool.Engine) WebLayerError!WebLayer {
1402
+ return webLayerResolved(metadata, resolved_engine, null);
1403
+ }
1404
+
1405
+ /// `webLayer` with the CLI's `--web-layer` flag in play: the flag beats
1406
+ /// app.zon's `.webview_layer` exactly as `-Dweb-layer` beats it in the
1407
+ /// build graph (effective setting = flag orelse manifest), so the build
1408
+ /// graphs can forward their resolved decision and hand-run packages can
1409
+ /// override the field without editing app.zon. An exclude flag against
1410
+ /// a web declaration is the same refused conflict as a manifest exclude.
1411
+ pub fn webLayerResolved(metadata: Metadata, resolved_engine: web_engine_tool.Engine, layer_flag: ?WebViewLayerSetting) WebLayerError!WebLayer {
1412
+ const manifest_setting = parseWebViewLayer(metadata.webview_layer) catch return error.InvalidWebViewLayer;
1413
+ const engine: app_manifest.WebEngine = switch (resolved_engine) {
1414
+ .system => .system,
1415
+ .chromium => .chromium,
1416
+ };
1417
+ const decision = app_manifest.web_layer.infer(metadata, engine, layer_flag orelse manifest_setting) catch return error.WebViewLayerConflict;
1418
+ if (layer_flag != null) {
1419
+ // The flag decides, but the verdict line keeps app.zon's own
1420
+ // richer reason whenever the flag merely confirms what the
1421
+ // manifest already decides: the build graphs forward their
1422
+ // resolved decision on every `zig build package`, and the common
1423
+ // case must keep reporting "declared: capabilities", not the
1424
+ // forwarded flag. Only a flag that CHANGES the outcome names
1425
+ // itself as the cause.
1426
+ if (app_manifest.web_layer.infer(metadata, engine, manifest_setting)) |manifest_decision| {
1427
+ if (manifest_decision.enabled == decision.enabled) {
1428
+ return .{ .enabled = manifest_decision.enabled, .reason = manifest_decision.reason };
1429
+ }
1430
+ } else |_| {}
1431
+ return .{ .enabled = decision.enabled, .reason = decision.reason, .from_flag = true };
1432
+ }
1433
+ return .{ .enabled = decision.enabled, .reason = decision.reason };
1434
+ }
1435
+
1436
+ /// The web-layer verdict for callers with no engine flag in play
1437
+ /// (`native check`): the manifest's own engine is the resolved engine.
1438
+ pub fn webLayerFromManifest(metadata: Metadata) WebLayerError!WebLayer {
1439
+ return webLayer(metadata, web_engine_tool.Engine.parse(metadata.web_engine) orelse .system);
1440
+ }
1441
+
1321
1442
  fn validateRelativePath(path: []const u8) !void {
1322
1443
  if (path.len == 0) return error.InvalidPath;
1323
1444
  if (path[0] == '/' or path[0] == '\\') return error.InvalidPath;
@@ -1920,6 +2041,151 @@ test "validate accepts a square icon source and prebuilt containers" {
1920
2041
  try std.testing.expect(result.ok);
1921
2042
  }
1922
2043
 
2044
+ test "web layer inference is declare-to-use over parsed metadata" {
2045
+ // Nothing declared: native-only.
2046
+ const canvas_capabilities = [_][]const u8{ "native_views", "gpu_surfaces" };
2047
+ const canvas: Metadata = .{ .id = "dev.example.canvas", .name = "canvas", .version = "1.0.0", .capabilities = &canvas_capabilities };
2048
+ const canvas_layer = try webLayerFromManifest(canvas);
2049
+ try std.testing.expect(!canvas_layer.enabled);
2050
+ try std.testing.expectEqual(WebLayerReason.inferred_native_only, canvas_layer.reason);
2051
+
2052
+ // Each web declaration flips the inference.
2053
+ const webview_capabilities = [_][]const u8{"webview"};
2054
+ const by_capability = try webLayerFromManifest(.{ .id = "dev.example.a", .name = "a", .version = "1.0.0", .capabilities = &webview_capabilities });
2055
+ try std.testing.expect(by_capability.enabled);
2056
+ try std.testing.expectEqual(WebLayerReason.capability, by_capability.reason);
2057
+
2058
+ const by_frontend = try webLayerFromManifest(.{ .id = "dev.example.b", .name = "b", .version = "1.0.0", .frontend = .{} });
2059
+ try std.testing.expect(by_frontend.enabled);
2060
+ try std.testing.expectEqual(WebLayerReason.frontend, by_frontend.reason);
2061
+
2062
+ const shell_views = [_]ShellViewMetadata{.{ .label = "content", .kind = "webview", .url = "zero://app/index.html" }};
2063
+ const shell_windows = [_]ShellWindowMetadata{.{ .label = "main", .views = &shell_views }};
2064
+ const by_shell = try webLayerFromManifest(.{ .id = "dev.example.c", .name = "c", .version = "1.0.0", .shell = .{ .windows = &shell_windows } });
2065
+ try std.testing.expect(by_shell.enabled);
2066
+ try std.testing.expectEqual(WebLayerReason.shell_webview, by_shell.reason);
2067
+
2068
+ // The Chromium engine is web intent; the system default alone is not.
2069
+ const by_chromium = try webLayerFromManifest(.{ .id = "dev.example.d", .name = "d", .version = "1.0.0", .web_engine = "chromium" });
2070
+ try std.testing.expect(by_chromium.enabled);
2071
+ try std.testing.expectEqual(WebLayerReason.chromium_engine, by_chromium.reason);
2072
+
2073
+ // The RESOLVED engine decides, not the raw manifest value: a system
2074
+ // manifest packaged with `--web-engine chromium` ships the layer.
2075
+ const by_resolved_chromium = try webLayer(.{ .id = "dev.example.i", .name = "i", .version = "1.0.0" }, .chromium);
2076
+ try std.testing.expect(by_resolved_chromium.enabled);
2077
+ try std.testing.expectEqual(WebLayerReason.chromium_engine, by_resolved_chromium.reason);
2078
+
2079
+ // Explicit overrides.
2080
+ const included = try webLayerFromManifest(.{ .id = "dev.example.e", .name = "e", .version = "1.0.0", .webview_layer = "include" });
2081
+ try std.testing.expect(included.enabled);
2082
+ const excluded = try webLayerFromManifest(.{ .id = "dev.example.f", .name = "f", .version = "1.0.0", .webview_layer = "exclude" });
2083
+ try std.testing.expect(!excluded.enabled);
2084
+ try std.testing.expectEqual(WebLayerReason.declared_exclude, excluded.reason);
2085
+
2086
+ // Contradictions and typos are refused, never resolved silently —
2087
+ // including an exclude against a flag-resolved Chromium engine.
2088
+ try std.testing.expectError(error.WebViewLayerConflict, webLayerFromManifest(.{ .id = "dev.example.g", .name = "g", .version = "1.0.0", .capabilities = &webview_capabilities, .webview_layer = "exclude" }));
2089
+ try std.testing.expectError(error.WebViewLayerConflict, webLayer(.{ .id = "dev.example.j", .name = "j", .version = "1.0.0", .webview_layer = "exclude" }, .chromium));
2090
+ try std.testing.expectError(error.InvalidWebViewLayer, webLayerFromManifest(.{ .id = "dev.example.h", .name = "h", .version = "1.0.0", .webview_layer = "never" }));
2091
+ }
2092
+
2093
+ test "web layer --web-layer flag beats the manifest field like -Dweb-layer does" {
2094
+ const canvas_capabilities = [_][]const u8{ "native_views", "gpu_surfaces" };
2095
+ const canvas: Metadata = .{ .id = "dev.example.canvas", .name = "canvas", .version = "1.0.0", .capabilities = &canvas_capabilities };
2096
+ const webview_capabilities = [_][]const u8{"webview"};
2097
+ const web: Metadata = .{ .id = "dev.example.web", .name = "web", .version = "1.0.0", .capabilities = &webview_capabilities };
2098
+
2099
+ // An include flag that changes the outcome names itself as the cause.
2100
+ const forced_in = try webLayerResolved(canvas, .system, .include);
2101
+ try std.testing.expect(forced_in.enabled);
2102
+ try std.testing.expectEqual(WebLayerReason.declared_include, forced_in.reason);
2103
+ try std.testing.expectEqualStrings("declared: --web-layer include", forced_in.sourceText());
2104
+
2105
+ // A flag that merely confirms the inference keeps the manifest's own
2106
+ // richer reason, so graph-forwarded packages report like hand-run ones.
2107
+ const confirmed = try webLayerResolved(web, .system, .include);
2108
+ try std.testing.expect(confirmed.enabled);
2109
+ try std.testing.expectEqual(WebLayerReason.capability, confirmed.reason);
2110
+ try std.testing.expectEqualStrings("declared: capabilities", confirmed.sourceText());
2111
+ const confirmed_off = try webLayerResolved(canvas, .system, .exclude);
2112
+ try std.testing.expect(!confirmed_off.enabled);
2113
+ try std.testing.expectEqual(WebLayerReason.inferred_native_only, confirmed_off.reason);
2114
+
2115
+ // An exclude flag against a web declaration is the same refused
2116
+ // conflict as a manifest exclude — including a resolved Chromium engine.
2117
+ try std.testing.expectError(error.WebViewLayerConflict, webLayerResolved(web, .system, .exclude));
2118
+ try std.testing.expectError(error.WebViewLayerConflict, webLayerResolved(canvas, .chromium, .exclude));
2119
+
2120
+ // `--web-layer auto` overrides a manifest exclude back to inference,
2121
+ // exactly as `-Dweb-layer=auto` does in the build graph.
2122
+ const reopened = try webLayerResolved(.{ .id = "dev.example.k", .name = "k", .version = "1.0.0", .capabilities = &webview_capabilities, .webview_layer = "exclude" }, .system, .auto);
2123
+ try std.testing.expect(reopened.enabled);
2124
+ try std.testing.expectEqual(WebLayerReason.capability, reopened.reason);
2125
+
2126
+ // No flag: identical to `webLayer` (the manifest field decides).
2127
+ const plain = try webLayerResolved(canvas, .system, null);
2128
+ try std.testing.expect(!plain.enabled);
2129
+ try std.testing.expectEqual(WebLayerReason.inferred_native_only, plain.reason);
2130
+
2131
+ // The flag parser shares the contract's vocabulary.
2132
+ try std.testing.expectEqual(WebViewLayerSetting.include, parseWebViewLayerSetting("include").?);
2133
+ try std.testing.expectEqual(WebViewLayerSetting.exclude, parseWebViewLayerSetting("exclude").?);
2134
+ try std.testing.expectEqual(WebViewLayerSetting.auto, parseWebViewLayerSetting("auto").?);
2135
+ try std.testing.expectEqual(null, parseWebViewLayerSetting("never"));
2136
+ }
2137
+
2138
+ test "validate rejects a web-declaring manifest that excludes the web layer" {
2139
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
2140
+ defer arena.deinit();
2141
+ const gpa = arena.allocator();
2142
+ var cwd = std.Io.Dir.cwd();
2143
+ const root = ".zig-cache/test-validate-web-layer-conflict";
2144
+ try cwd.deleteTree(std.testing.io, root);
2145
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2146
+ try cwd.createDirPath(std.testing.io, root);
2147
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/app.zon", .data =
2148
+ \\.{
2149
+ \\ .id = "dev.example.app",
2150
+ \\ .name = "demo",
2151
+ \\ .version = "1.0.0",
2152
+ \\ .capabilities = .{"webview"},
2153
+ \\ .webview_layer = "exclude",
2154
+ \\}
2155
+ });
2156
+
2157
+ const result = try validateFile(gpa, std.testing.io, root ++ "/app.zon");
2158
+ try std.testing.expect(!result.ok);
2159
+ try std.testing.expect(std.mem.indexOf(u8, result.message, ".webview_layer = \"exclude\"") != null);
2160
+ try std.testing.expect(std.mem.indexOf(u8, result.message, "declares web content") != null);
2161
+
2162
+ // A native-only manifest with the same exclude is valid.
2163
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/app.zon", .data =
2164
+ \\.{
2165
+ \\ .id = "dev.example.app",
2166
+ \\ .name = "demo",
2167
+ \\ .version = "1.0.0",
2168
+ \\ .capabilities = .{"gpu_surfaces"},
2169
+ \\ .webview_layer = "exclude",
2170
+ \\}
2171
+ });
2172
+ const native_only = try validateFile(gpa, std.testing.io, root ++ "/app.zon");
2173
+ try std.testing.expect(native_only.ok);
2174
+
2175
+ // A typo in the setting is its own teaching error.
2176
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/app.zon", .data =
2177
+ \\.{
2178
+ \\ .id = "dev.example.app",
2179
+ \\ .name = "demo",
2180
+ \\ .version = "1.0.0",
2181
+ \\ .webview_layer = "never",
2182
+ \\}
2183
+ });
2184
+ const invalid = try validateFile(gpa, std.testing.io, root ++ "/app.zon");
2185
+ try std.testing.expect(!invalid.ok);
2186
+ try std.testing.expect(std.mem.indexOf(u8, invalid.message, "webview_layer is invalid") != null);
2187
+ }
2188
+
1923
2189
  test "manifest validates the theme pack name" {
1924
2190
  const metadata = try parseText(std.testing.allocator,
1925
2191
  \\.{