From 7337fe348bd32d121a645167a255f567865d4fbe Mon Sep 17 00:00:00 2001 From: Guy Date: Sat, 22 Aug 2026 17:33:54 +0300 Subject: [PATCH] fix(tests): wire module tests into zig build test The test step compiles only src/main.zig, which never imports the other modules' test declarations, so 'zig build test' silently runs 0 tests. Add a root test block referencing store/api/domain/config. Fixes latent compile errors in those module tests left by the zig 0.16 migration: std.testing.tmpDir Dir must be wrapped via std_compat.fs.Dir.wrap for writeFile/realpathAlloc/makePath, and ObjectMap.deinit now requires an allocator argument. Also fixes arena leaks in three api.zig auth tests (Context.allocator now backed by a per-test ArenaAllocator) so the suite passes cleanly under the testing allocator. After this change: 18 unit tests run (previously 0). --- src/api.zig | 12 +++++++++--- src/config.zig | 10 +++++----- src/main.zig | 8 ++++++++ src/store.zig | 2 +- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/api.zig b/src/api.zig index 161011e..89e51c7 100644 --- a/src/api.zig +++ b/src/api.zig @@ -1692,12 +1692,14 @@ fn serverError(allocator: std.mem.Allocator) HttpResponse { } test "auth allows health without API token" { + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); var store = try Store.init(std.testing.allocator, ":memory:"); defer store.deinit(); var ctx = Context{ .store = &store, - .allocator = std.testing.allocator, + .allocator = arena.allocator(), .required_api_token = "secret", }; @@ -1706,12 +1708,14 @@ test "auth allows health without API token" { } test "auth rejects protected endpoint without API token" { + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); var store = try Store.init(std.testing.allocator, ":memory:"); defer store.deinit(); var ctx = Context{ .store = &store, - .allocator = std.testing.allocator, + .allocator = arena.allocator(), .required_api_token = "secret", }; @@ -1720,12 +1724,14 @@ test "auth rejects protected endpoint without API token" { } test "auth accepts admin token for protected endpoint" { + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); var store = try Store.init(std.testing.allocator, ":memory:"); defer store.deinit(); var ctx = Context{ .store = &store, - .allocator = std.testing.allocator, + .allocator = arena.allocator(), .required_api_token = "secret", }; diff --git a/src/config.zig b/src/config.zig index e124be4..0c4d2bb 100644 --- a/src/config.zig +++ b/src/config.zig @@ -81,7 +81,7 @@ test "loadFromFile reads config values" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); - try tmp.dir.writeFile(.{ + try std_compat.fs.Dir.wrap(tmp.dir).writeFile(.{ .sub_path = "config.json", .data = \\{ @@ -92,7 +92,7 @@ test "loadFromFile reads config values" { , }); - const cfg_path = try tmp.dir.realpathAlloc(std.testing.allocator, "config.json"); + const cfg_path = try std_compat.fs.Dir.wrap(tmp.dir).realpathAlloc(std.testing.allocator, "config.json"); defer std.testing.allocator.free(cfg_path); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); @@ -108,8 +108,8 @@ test "resolveRelativePaths anchors db to config directory" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); - try tmp.dir.makePath("configs"); - try tmp.dir.writeFile(.{ + try std_compat.fs.Dir.wrap(tmp.dir).makePath("configs"); + try std_compat.fs.Dir.wrap(tmp.dir).writeFile(.{ .sub_path = "configs/config.json", .data = \\{ @@ -118,7 +118,7 @@ test "resolveRelativePaths anchors db to config directory" { , }); - const cfg_path = try tmp.dir.realpathAlloc(std.testing.allocator, "configs/config.json"); + const cfg_path = try std_compat.fs.Dir.wrap(tmp.dir).realpathAlloc(std.testing.allocator, "configs/config.json"); defer std.testing.allocator.free(cfg_path); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); diff --git a/src/main.zig b/src/main.zig index 50a84f6..1adcffe 100644 --- a/src/main.zig +++ b/src/main.zig @@ -233,3 +233,11 @@ fn readHttpRequest(allocator: std.mem.Allocator, stream: *std.Io.net.Stream, max return try allocator.dupe(u8, buffer.items[0..required]); } + +// Pull module test declarations into the test build (zig build test). +test { + _ = @import("store.zig"); + _ = @import("api.zig"); + _ = @import("domain.zig"); + _ = @import("config.zig"); +} diff --git a/src/store.zig b/src/store.zig index 14bbe7d..10c4be9 100644 --- a/src/store.zig +++ b/src/store.zig @@ -2422,7 +2422,7 @@ test "claim respects per-state concurrency limits" { // Set per-state concurrency limit of 2 for "review" var concurrency_map: std.json.ObjectMap = .empty; - defer concurrency_map.deinit(); + defer concurrency_map.deinit(alloc); try concurrency_map.put(alloc, "review", .{ .integer = 2 }); const per_state: std.json.Value = .{ .object = concurrency_map };