const std = @import("std"); const cli = @import("../cli.zig"); const config = @import("../config.zig"); const contracts = @import("../contracts.zig"); const Command = cli.Command; test "parse validate command with dry run flag" { const command = try cli.parseCommand(&.{ "validate", "--dry-run", "scenario.toml" }); switch (command) { .validate => |validate| { try std.testing.expectEqual(contracts.ValidateMode.dry_run, validate.mode); try std.testing.expectEqualStrings("scenario.toml", validate.config_path); try std.testing.expectEqual(@as(?config.WasmBackend, null), validate.wasm_backend); }, else => return error.UnexpectedArgument, } } test "parse version command" { try std.testing.expectEqual(Command.version, try cli.parseCommand(&.{"--version"})); try std.testing.expectEqual(Command.version, try cli.parseCommand(&.{"version"})); try std.testing.expectEqual(cli.HelpCommand.general, (try cli.parseCommand(&.{"--help"})).help); try std.testing.expectEqual(cli.HelpCommand.validate, (try cli.parseCommand(&.{ "validate", "--help" })).help); } test "reject conflicting validate modes" { try std.testing.expectError( error.ConflictingValidateMode, cli.parseCommand(&.{ "validate", "--dry-run", "--with-setup", "scenario.toml" }), ); } test "parse run command" { const command = try cli.parseCommand(&.{ "run", "scenario.toml" }); switch (command) { .run => |run| { try std.testing.expectEqualStrings("scenario.toml", run.config_path); try std.testing.expectEqual(@as(?config.WasmBackend, null), run.wasm_backend); }, else => return error.UnexpectedArgument, } } test "parse validate command with wasm backend override" { const command = try cli.parseCommand(&.{ "validate", "--wasm-backend", "wasm3", "scenario.toml" }); switch (command) { .validate => |validate| { try std.testing.expectEqual(config.WasmBackend.wasm3, validate.wasm_backend.?); try std.testing.expectEqualStrings("scenario.toml", validate.config_path); }, else => return error.UnexpectedArgument, } } test "parse run command with wasm backend override" { const command = try cli.parseCommand(&.{ "run", "--wasm-backend", "wamr", "scenario.toml" }); switch (command) { .run => |run| { try std.testing.expectEqual(config.WasmBackend.wamr, run.wasm_backend.?); try std.testing.expectEqualStrings("scenario.toml", run.config_path); }, else => return error.UnexpectedArgument, } } test "parse distributed run command" { const command = try cli.parseCommand(&.{ "run-distributed", "--agent", "127.0.0.1:9001", "--agent", "127.0.0.1:9002", "scenario.toml", }); switch (command) { .run_distributed => |run_distributed| { try std.testing.expectEqualStrings("scenario.toml", run_distributed.config_path); try std.testing.expectEqual(@as(usize, 2), run_distributed.agent_addresses.len); try std.testing.expectEqualStrings("127.0.0.1:9001", run_distributed.agent_addresses[0]); try std.testing.expectEqualStrings("127.0.0.1:9002", run_distributed.agent_addresses[1]); }, else => return error.UnexpectedArgument, } } test "parse agent command" { const command = try cli.parseCommand(&.{ "agent", "--listen", "127.0.0.1:9001" }); switch (command) { .agent => |agent| { try std.testing.expectEqualStrings("127.0.0.1:9001", agent.listen_address); }, else => return error.UnexpectedArgument, } } test "validate command succeeds for a valid config file" { const source = \\[scenario] \\name = "simple-healthcheck" \\protocol = "http" \\target = "https://example.internal" \\ \\[execution] \\mode = "constant-vus" \\vus = 2 \\duration = "5s" \\ \\[[http.requests]] \\id = 1 \\name = "health" \\method = "GET" \\path = "/health" ; var tmp_dir = std.testing.tmpDir(.{}); defer tmp_dir.cleanup(); try tmp_dir.dir.writeFile(.{ .sub_path = "scenario.toml", .data = source }); const config_path = try std.fmt.allocPrint( std.testing.allocator, ".zig-cache/tmp/{s}/scenario.toml", .{tmp_dir.sub_path}, ); defer std.testing.allocator.free(config_path); var stdout_buffer: [256]u8 = undefined; var stdout_stream = std.io.fixedBufferStream(&stdout_buffer); var stderr_buffer: [256]u8 = undefined; var stderr_stream = std.io.fixedBufferStream(&stderr_buffer); const exit_code = try cli.executeArgs( std.testing.allocator, &.{ "validate", config_path }, stdout_stream.writer(), stderr_stream.writer(), ); try std.testing.expectEqual(contracts.ExitCode.success, exit_code); try std.testing.expectStringStartsWith(stdout_stream.getWritten(), "config is valid"); try std.testing.expectEqualStrings("", stderr_stream.getWritten()); } test "dry run prints request preview" { const source = \\[scenario] \\name = "simple-dry-run" \\protocol = "http" \\target = "https://example.internal" \\ \\[execution] \\mode = "constant-vus" \\vus = 2 \\duration = "5s" \\ \\[[http.requests]] \\id = 1 \\name = "health" \\method = "GET" \\path = "/health" ; var tmp_dir = std.testing.tmpDir(.{}); defer tmp_dir.cleanup(); try tmp_dir.dir.writeFile(.{ .sub_path = "scenario.toml", .data = source }); const config_path = try std.fmt.allocPrint( std.testing.allocator, ".zig-cache/tmp/{s}/scenario.toml", .{tmp_dir.sub_path}, ); defer std.testing.allocator.free(config_path); var stdout_buffer: [512]u8 = undefined; var stdout_stream = std.io.fixedBufferStream(&stdout_buffer); var stderr_buffer: [512]u8 = undefined; var stderr_stream = std.io.fixedBufferStream(&stderr_buffer); const exit_code = try cli.executeArgs( std.testing.allocator, &.{ "validate", "--dry-run", config_path }, stdout_stream.writer(), stderr_stream.writer(), ); try std.testing.expectEqual(contracts.ExitCode.success, exit_code); try std.testing.expectStringStartsWith(stdout_stream.getWritten(), "{\"schema_version\":1,\"kind\":\"dry-run\""); try std.testing.expectEqualStrings("", stderr_stream.getWritten()); } test "version command prints build version" { var stdout_buffer: [128]u8 = undefined; var stdout_stream = std.io.fixedBufferStream(&stdout_buffer); var stderr_buffer: [128]u8 = undefined; var stderr_stream = std.io.fixedBufferStream(&stderr_buffer); const exit_code = try cli.executeArgs( std.testing.allocator, &.{"--version"}, stdout_stream.writer(), stderr_stream.writer(), ); try std.testing.expectEqual(contracts.ExitCode.success, exit_code); try std.testing.expectEqualStrings("xburner 0.1.0\n", stdout_stream.getWritten()); try std.testing.expectEqualStrings("", stderr_stream.getWritten()); } test "validate help prints defaults" { var stdout_buffer: [2048]u8 = undefined; var stdout_stream = std.io.fixedBufferStream(&stdout_buffer); var stderr_buffer: [128]u8 = undefined; var stderr_stream = std.io.fixedBufferStream(&stderr_buffer); const exit_code = try cli.executeArgs( std.testing.allocator, &.{ "validate", "--help" }, stdout_stream.writer(), stderr_stream.writer(), ); try std.testing.expectEqual(contracts.ExitCode.success, exit_code); try std.testing.expect(std.mem.indexOf(u8, stdout_stream.getWritten(), "scenario.recovery.window = 5s") != null); try std.testing.expect(std.mem.indexOf(u8, stdout_stream.getWritten(), "http.max_connections = unlimited") != null); try std.testing.expect(std.mem.indexOf(u8, stdout_stream.getWritten(), "exporters.otlp.retry_max_attempts = 4") != null); try std.testing.expectEqualStrings("", stderr_stream.getWritten()); }