#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { using JsonValue = Void::Json::Strict::Value; using JsonObject = JsonValue::Object; std::optional parse_json(const std::string &line) { auto parsed = Void::Json::Strict::parse(line, 16); if (!parsed) { return std::nullopt; } return std::move(*parsed); } const JsonObject *as_object(const JsonValue &value) { return std::get_if(&value.value); } const JsonValue *field(const JsonObject &object, std::string_view name) { const auto entry = object.find(name); return entry == object.end() ? nullptr : &entry->second; } bool bool_field(const JsonObject &object, std::string_view name, bool fallback = false) { const auto *value = field(object, name); if (value == nullptr) { return fallback; } const auto *flag = std::get_if(&value->value); return flag != nullptr ? *flag : fallback; } std::int64_t int_field(const JsonObject &object, std::string_view name) { const auto *value = field(object, name); if (value == nullptr) { return -1; } const auto *number = std::get_if(&value->value); return number != nullptr ? *number : -1; } std::string str_field(const JsonObject &object, std::string_view name) { const auto *value = field(object, name); if (value == nullptr) { return {}; } const auto *text = std::get_if(&value->value); return text != nullptr ? *text : std::string{}; } std::optional request_object(const std::filesystem::path &socket, std::string_view request_json, std::chrono::milliseconds timeout = std::chrono::seconds{120}) { auto response = Void::Build::Daemon::request(socket, request_json, timeout); if (!response) { return std::nullopt; } auto parsed = parse_json(*response); if (!parsed) { return std::nullopt; } const auto *object = as_object(*parsed); if (object == nullptr) { return std::nullopt; } return *object; } std::vector string_array_field(const JsonObject &object, std::string_view name) { std::vector result; const auto *value = field(object, name); if (value == nullptr) { return result; } const auto *array = std::get_if(&value->value); if (array == nullptr) { return result; } for (const auto &entry : *array) { if (const auto *text = std::get_if(&entry.value)) { result.push_back(*text); } } return result; } bool wait_for_path(const std::filesystem::path &path, std::chrono::milliseconds timeout) { const auto deadline = std::chrono::steady_clock::now() + timeout; while (std::chrono::steady_clock::now() < deadline) { if (std::filesystem::exists(path)) { return true; } std::this_thread::sleep_for(std::chrono::milliseconds{10}); } return std::filesystem::exists(path); } Void::Build::Daemon::DaemonOptions make_options(const std::filesystem::path &root) { Void::Build::Daemon::DaemonOptions options; options.project_root = root / "project"; options.build_root = root / "build"; options.poll_interval = std::chrono::milliseconds{10}; std::error_code ec; std::filesystem::create_directories(options.project_root, ec); std::filesystem::create_directories(options.build_root, ec); return options; } } // namespace VOID_TEST(Daemon, lifecycle_ping_status_gc_shutdown_and_unknown_op) { auto temp = Void::FileSystem::TempDir::open("vbs_daemon_"); Void::Testing::require(temp.has_value()); const auto options = make_options(temp->path()); auto server = Void::Build::Daemon::DaemonServer::create(options); Void::Testing::require(server.has_value()); const auto socket = server->socket_path(); Void::Testing::expect_eq(socket, options.build_root / ".vbs" / "vbs-daemon.sock"); Void::Result::Result run_result; std::jthread worker{[&] { run_result = server->run(); }}; Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5})); auto ping = Void::Build::Daemon::request(socket, R"({"op":"ping"})"); Void::Testing::require(ping.has_value()); const auto ping_json = parse_json(*ping); Void::Testing::require(ping_json.has_value()); const auto *ping_object = as_object(*ping_json); Void::Testing::require(ping_object != nullptr); Void::Testing::expect(bool_field(*ping_object, "ok")); Void::Testing::expect_eq(str_field(*ping_object, "op"), std::string{"ping"}); auto status = Void::Build::Daemon::request(socket, R"({"op":"status"})"); Void::Testing::require(status.has_value()); const auto status_json = parse_json(*status); Void::Testing::require(status_json.has_value()); const auto *status_object = as_object(*status_json); Void::Testing::require(status_object != nullptr); Void::Testing::expect(bool_field(*status_object, "ok")); Void::Testing::expect(int_field(*status_object, "pid") > 0); Void::Testing::expect(int_field(*status_object, "watched_directories") >= 1); Void::Testing::expect(int_field(*status_object, "sequence") >= 0); auto gc = Void::Build::Daemon::request(socket, R"({"op":"gc","max_bytes":0})"); Void::Testing::require(gc.has_value()); const auto gc_json = parse_json(*gc); Void::Testing::require(gc_json.has_value()); const auto *gc_object = as_object(*gc_json); Void::Testing::require(gc_object != nullptr); Void::Testing::expect(bool_field(*gc_object, "ok")); Void::Testing::expect(int_field(*gc_object, "removed_blobs") >= 0); Void::Testing::expect(int_field(*gc_object, "retained_blobs") >= 0); auto unknown = Void::Build::Daemon::request(socket, R"({"op":"not-an-op"})"); Void::Testing::require(unknown.has_value()); const auto unknown_json = parse_json(*unknown); Void::Testing::require(unknown_json.has_value()); const auto *unknown_object = as_object(*unknown_json); Void::Testing::require(unknown_object != nullptr); Void::Testing::expect(!bool_field(*unknown_object, "ok", true)); Void::Testing::expect(field(*unknown_object, "error") != nullptr); auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})"); Void::Testing::require(shutdown.has_value()); const auto shutdown_json = parse_json(*shutdown); Void::Testing::require(shutdown_json.has_value()); const auto *shutdown_object = as_object(*shutdown_json); Void::Testing::require(shutdown_object != nullptr); Void::Testing::expect(bool_field(*shutdown_object, "ok")); worker.join(); Void::Testing::expect(run_result.has_value()); Void::Testing::expect(!std::filesystem::exists(socket)); } VOID_TEST(Daemon, second_create_reports_already_running) { auto temp = Void::FileSystem::TempDir::open("vbs_daemon_"); Void::Testing::require(temp.has_value()); const auto options = make_options(temp->path()); auto first = Void::Build::Daemon::DaemonServer::create(options); Void::Testing::require(first.has_value()); const auto socket = first->socket_path(); Void::Result::Result run_result; std::jthread worker{[&] { run_result = first->run(); }}; Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5})); auto second = Void::Build::Daemon::DaemonServer::create(options); Void::Testing::expect(!second.has_value()); if (!second) { Void::Testing::expect(second.error().message().find("already running") != std::string::npos); } auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})"); Void::Testing::expect(shutdown.has_value()); worker.join(); Void::Testing::expect(run_result.has_value()); Void::Testing::expect(!std::filesystem::exists(socket)); } VOID_TEST(Daemon, describe_evaluates_project_graph) { auto temp = Void::FileSystem::TempDir::open("vbs_daemon_"); Void::Testing::require(temp.has_value()); const auto options = make_options(temp->path()); { std::ofstream descriptor{options.project_root / "build.cpp"}; descriptor << R"cpp(#include void build(Void::Build::Project &project) { auto app = project.executable("app"); app.sources({"main.cpp"}); } )cpp"; Void::Testing::require(descriptor.good()); } { std::ofstream source{options.project_root / "main.cpp"}; source << "int main() { return 0; }\n"; Void::Testing::require(source.good()); } auto server = Void::Build::Daemon::DaemonServer::create(options); Void::Testing::require(server.has_value()); const auto socket = server->socket_path(); Void::Result::Result run_result; std::jthread worker{[&] { run_result = server->run(); }}; Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5})); auto describe = Void::Build::Daemon::request(socket, R"({"op":"describe"})", std::chrono::seconds{120}); Void::Testing::require(describe.has_value()); const auto describe_json = parse_json(*describe); Void::Testing::require(describe_json.has_value()); const auto *describe_object = as_object(*describe_json); Void::Testing::require(describe_object != nullptr); Void::Testing::expect(bool_field(*describe_object, "ok")); Void::Testing::expect_eq(str_field(*describe_object, "op"), std::string{"describe"}); Void::Testing::expect_eq(str_field(*describe_object, "profile"), std::string{"debug"}); Void::Testing::expect(int_field(*describe_object, "targets") >= 1); Void::Testing::expect(int_field(*describe_object, "actions") >= 1); const auto target_names = string_array_field(*describe_object, "target_names"); Void::Testing::expect(std::ranges::find(target_names, std::string{"app"}) != target_names.end()); auto bad_profile = request_object(socket, R"({"op":"describe","profile":"does-not-exist"})"); Void::Testing::require(bad_profile.has_value()); Void::Testing::expect(!bool_field(*bad_profile, "ok", true)); Void::Testing::expect(field(*bad_profile, "error") != nullptr); auto numeric_profile = request_object(socket, R"({"op":"describe","profile":123})"); Void::Testing::require(numeric_profile.has_value()); Void::Testing::expect(!bool_field(*numeric_profile, "ok", true)); Void::Testing::expect(str_field(*numeric_profile, "error").find("must be a string") != std::string::npos); auto empty_profile = request_object(socket, R"({"op":"describe","profile":""})"); Void::Testing::require(empty_profile.has_value()); Void::Testing::expect(!bool_field(*empty_profile, "ok", true)); Void::Testing::expect(str_field(*empty_profile, "error").find("must not be empty") != std::string::npos); const std::string oversized_profile_request{R"({"op":"describe","profile":")" + std::string(129, 'p') + "\"}"}; auto oversized_profile = request_object(socket, oversized_profile_request); Void::Testing::require(oversized_profile.has_value()); Void::Testing::expect(!bool_field(*oversized_profile, "ok", true)); Void::Testing::expect(str_field(*oversized_profile, "error").find("too long") != std::string::npos); auto numeric_compiler = request_object(socket, R"({"op":"describe","compiler":5})"); Void::Testing::require(numeric_compiler.has_value()); Void::Testing::expect(!bool_field(*numeric_compiler, "ok", true)); Void::Testing::expect(str_field(*numeric_compiler, "error").find("must be a string") != std::string::npos); auto invalid_compiler = request_object(socket, R"({"op":"describe","compiler":"msvc"})"); Void::Testing::require(invalid_compiler.has_value()); Void::Testing::expect(!bool_field(*invalid_compiler, "ok", true)); Void::Testing::expect(str_field(*invalid_compiler, "error").find("unknown compiler") != std::string::npos); auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})"); Void::Testing::expect(shutdown.has_value()); worker.join(); Void::Testing::expect(run_result.has_value()); Void::Testing::expect(!std::filesystem::exists(socket)); } VOID_TEST(Daemon, build_executes_project_graph_and_reuses_cache) { auto temp = Void::FileSystem::TempDir::open("vbs_daemon_"); Void::Testing::require(temp.has_value()); const auto options = make_options(temp->path()); { std::ofstream descriptor{options.project_root / "build.cpp"}; descriptor << R"cpp(#include void build(Void::Build::Project &project) { auto app = project.executable("app"); app.sources({"main.cpp"}); } )cpp"; Void::Testing::require(descriptor.good()); } { std::ofstream source{options.project_root / "main.cpp"}; source << "int main() { return 0; }\n"; Void::Testing::require(source.good()); } auto server = Void::Build::Daemon::DaemonServer::create(options); Void::Testing::require(server.has_value()); const auto socket = server->socket_path(); Void::Result::Result run_result; std::jthread worker{[&] { run_result = server->run(); }}; Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5})); auto build = request_object(socket, R"({"op":"build"})"); Void::Testing::require(build.has_value()); Void::Testing::expect(bool_field(*build, "ok")); Void::Testing::expect_eq(str_field(*build, "op"), std::string{"build"}); Void::Testing::expect(int_field(*build, "executed") >= 1); Void::Testing::expect(int_field(*build, "failed") == 0); Void::Testing::expect( std::filesystem::exists(options.build_root / "profiles" / "debug" / "out" / "bin" / "app")); auto cached = request_object(socket, R"({"op":"build"})"); Void::Testing::require(cached.has_value()); Void::Testing::expect(bool_field(*cached, "ok")); Void::Testing::expect(int_field(*cached, "executed") == 0); Void::Testing::expect(int_field(*cached, "cached") >= 1); auto bad_targets = request_object(socket, R"({"op":"build","targets":"app"})"); Void::Testing::require(bad_targets.has_value()); Void::Testing::expect(!bool_field(*bad_targets, "ok", true)); Void::Testing::expect(str_field(*bad_targets, "error").find("must be an array") != std::string::npos); auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})"); Void::Testing::expect(shutdown.has_value()); worker.join(); Void::Testing::expect(run_result.has_value()); Void::Testing::expect(!std::filesystem::exists(socket)); } VOID_TEST(Daemon, describe_empty_project_reports_zero_targets) { auto temp = Void::FileSystem::TempDir::open("vbs_daemon_"); Void::Testing::require(temp.has_value()); const auto options = make_options(temp->path()); { std::ofstream descriptor{options.project_root / "build.cpp"}; descriptor << R"cpp(#include void build(Void::Build::Project &project) {} )cpp"; Void::Testing::require(descriptor.good()); } auto server = Void::Build::Daemon::DaemonServer::create(options); Void::Testing::require(server.has_value()); const auto socket = server->socket_path(); Void::Result::Result run_result; std::jthread worker{[&] { run_result = server->run(); }}; Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5})); auto describe = request_object(socket, R"({"op":"describe"})"); Void::Testing::require(describe.has_value()); Void::Testing::expect(bool_field(*describe, "ok")); Void::Testing::expect_eq(int_field(*describe, "targets"), std::int64_t{0}); Void::Testing::expect_eq(int_field(*describe, "actions"), std::int64_t{0}); Void::Testing::expect(string_array_field(*describe, "target_names").empty()); Void::Testing::expect(field(*describe, "target_names_truncated") == nullptr); auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})"); Void::Testing::expect(shutdown.has_value()); worker.join(); Void::Testing::expect(run_result.has_value()); Void::Testing::expect(!std::filesystem::exists(socket)); } VOID_TEST(Daemon, describe_bounds_target_names_for_large_projects) { auto temp = Void::FileSystem::TempDir::open("vbs_daemon_"); Void::Testing::require(temp.has_value()); const auto options = make_options(temp->path()); { std::ofstream descriptor{options.project_root / "build.cpp"}; descriptor << R"cpp(#include #include void build(Void::Build::Project &project) { for (int i = 0; i < 140; ++i) { auto target = project.executable(std::to_string(i) + std::string(240, 'a')); target.sources({"main.cpp"}); } } )cpp"; Void::Testing::require(descriptor.good()); } { std::ofstream source{options.project_root / "main.cpp"}; source << "int main() { return 0; }\n"; Void::Testing::require(source.good()); } auto server = Void::Build::Daemon::DaemonServer::create(options); Void::Testing::require(server.has_value()); const auto socket = server->socket_path(); Void::Result::Result run_result; std::jthread worker{[&] { run_result = server->run(); }}; Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5})); auto raw = Void::Build::Daemon::request(socket, R"({"op":"describe"})", std::chrono::seconds{120}); Void::Testing::require(raw.has_value()); // 128 names x 200 bytes plus JSON overhead stays well under 32 KiB. Void::Testing::expect(raw->size() < 32U * 1024U); const auto parsed = parse_json(*raw); Void::Testing::require(parsed.has_value()); const auto *object = as_object(*parsed); Void::Testing::require(object != nullptr); Void::Testing::expect(bool_field(*object, "ok")); Void::Testing::expect_eq(int_field(*object, "targets"), std::int64_t{140}); Void::Testing::expect(int_field(*object, "actions") >= 140); const auto names = string_array_field(*object, "target_names"); Void::Testing::expect_eq(names.size(), std::size_t{128}); for (const auto &name : names) { Void::Testing::expect(name.size() <= 200U); } Void::Testing::expect(bool_field(*object, "target_names_truncated")); auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})"); Void::Testing::expect(shutdown.has_value()); worker.join(); Void::Testing::expect(run_result.has_value()); Void::Testing::expect(!std::filesystem::exists(socket)); } VOID_TEST(Daemon, describe_rejects_workspace_root) { auto temp = Void::FileSystem::TempDir::open("vbs_daemon_"); Void::Testing::require(temp.has_value()); const auto options = make_options(temp->path()); { std::ofstream workspace{options.project_root / "workspace.cpp"}; workspace << "void workspace(Void::Build::Workspace &workspace) {}\n"; Void::Testing::require(workspace.good()); } auto server = Void::Build::Daemon::DaemonServer::create(options); Void::Testing::require(server.has_value()); const auto socket = server->socket_path(); Void::Result::Result run_result; std::jthread worker{[&] { run_result = server->run(); }}; Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5})); auto describe = request_object(socket, R"({"op":"describe"})"); Void::Testing::require(describe.has_value()); Void::Testing::expect(!bool_field(*describe, "ok", true)); Void::Testing::expect(str_field(*describe, "error").find("workspace") != std::string::npos); auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})"); Void::Testing::expect(shutdown.has_value()); worker.join(); Void::Testing::expect(run_result.has_value()); Void::Testing::expect(!std::filesystem::exists(socket)); }