Files
Language-Server/tests/LifecycleTests.cpp
T
faumaray da8003f6d4 Implement the VLS roadmap on the native VBS build
Native VBS and CMake builds of the server and every registered suite:
protocol and document sync, VBS/compdb providers, stock GCC diagnostics
(including in-process default profile preparation), the stock occurrence
observer and hover/definition, the bounded Scheduler, C++ module graph,
scan and artifact cache with real CMI production, the context-aware
symbol index with schema-2 snapshot migration, navigation and guarded
rename, reflection provenance with splice-origin extraction, index-backed
completion and signature help, live GCC worker control with a registered
DAP suite, and the Neovim and VS Code clients.

VoidLibrary follows main. The private GCC 16.2.0 frontend is reproduced
from the versioned patches and verified stepping reflection/template code
with projected values 17/34 and 41/82.
2026-12-05 14:06:50 +03:00

131 lines
6.4 KiB
C++

#include "TestSupport.hpp"
VOID_TEST(LifecycleTests, complete_lifecycle_advertises_document_sync) {
vls::Server server;
const auto initialized = test::response(server, test::initialize_request);
const auto &result = test::field(initialized, "result");
test::expect_sync_capabilities(result);
const auto &info = test::field(result, "serverInfo");
Void::Testing::expect_eq(std::get<std::string>(test::field(info, "name").value),
std::string{"VoidLanguageServer"});
Void::Testing::expect_eq(std::get<std::string>(test::field(info, "version").value),
std::string{"0.1.0"});
Void::Testing::expect(!server.handle(test::initialized).response.has_value());
const auto stopped = test::response(server, test::shutdown);
Void::Testing::expect(
std::holds_alternative<std::nullptr_t>(test::field(stopped, "result").value));
const auto exited = server.handle(test::exit);
Void::Testing::expect(!exited.response.has_value());
Void::Testing::require(exited.exit_code.has_value());
Void::Testing::expect_eq(*exited.exit_code, 0);
}
VOID_TEST(LifecycleTests, requests_before_initialize_and_premature_exit) {
vls::Server server;
test::expect_error(server, test::shutdown, -32002);
test::expect_error(server, R"({"jsonrpc":"2.0","id":3,"method":"unknown"})", -32002);
Void::Testing::expect(!server.handle(test::initialized).response.has_value());
auto exited = server.handle(test::exit);
Void::Testing::require(exited.exit_code.has_value());
Void::Testing::expect_eq(*exited.exit_code, 1);
}
VOID_TEST(LifecycleTests, initialize_requires_object_and_only_runs_once) {
vls::Server server;
for (const auto params : {"", ",\"params\":[]", ",\"params\":null", ",\"params\":true"})
test::expect_error(
server, std::string{R"({"jsonrpc":"2.0","id":1,"method":"initialize")"} + params + "}",
-32602);
Void::Testing::expect(!server.handle(R"({"jsonrpc":"2.0","method":"initialize","params":{}})")
.response.has_value());
test::initialize(server);
test::expect_error(server, test::initialize_request, -32600);
}
VOID_TEST(LifecycleTests, unknown_methods_notifications_and_exit_request) {
vls::Server server;
test::initialize(server);
test::expect_error(
server, R"({"jsonrpc":"2.0","id":3,"method":"textDocument/hover","params":{}})", -32601);
test::expect_error(server, R"({"jsonrpc":"2.0","id":4,"method":"exit"})", -32601);
for (const auto text :
{R"({"jsonrpc":"2.0","method":"unknown"})", R"({"jsonrpc":"2.0","method":7})",
R"({"jsonrpc":"wrong","method":"unknown"})", R"({"jsonrpc":"2.0","method":"shutdown"})"})
Void::Testing::expect(!server.handle(text).response.has_value());
Void::Testing::expect_eq(*server.handle(test::exit).exit_code, 1);
}
VOID_TEST(LifecycleTests, post_shutdown_precedes_all_request_methods) {
vls::Server server;
test::initialize(server);
static_cast<void>(test::response(server, test::shutdown));
for (const auto text : {test::initialize_request, test::shutdown,
std::string_view{R"({"jsonrpc":"2.0","id":4,"method":"unknown"})"}})
test::expect_error(server, text, -32600);
Void::Testing::expect_eq(*server.handle(test::exit).exit_code, 0);
}
VOID_TEST(LifecycleTests, malformed_envelopes_and_ids) {
vls::Server server;
for (const auto text :
{"[]", "[{}]", "null", "true", "{}", R"({"jsonrpc":"1.0","id":1,"method":"initialize"})",
R"({"jsonrpc":"2.0","id":null,"method":"initialize"})",
R"({"jsonrpc":"2.0","id":true,"method":"initialize"})",
R"({"jsonrpc":"2.0","id":2147483648,"method":"initialize"})",
R"({"jsonrpc":"2.0","id":-2147483649,"method":"initialize"})",
R"({"jsonrpc":"2.0","id":{},"method":"initialize"})",
R"({"jsonrpc":"2.0","id":[],"method":"initialize"})",
R"({"jsonrpc":"2.0","id":1,"method":false})",
R"({"jsonrpc":"2.0","id":1,"method":"initialize","result":null})",
R"({"jsonrpc":"2.0","id":1,"result":null,"error":{"code":-32600,"message":"bad"}})"})
test::expect_error(server, text, -32600);
const auto string_id =
test::response(server, R"({"jsonrpc":"2.0","id":"\"\u0416\n","method":"unknown"})");
Void::Testing::expect_eq(std::get<std::string>(test::field(string_id, "id").value),
std::string{"\"\xD0\x96\n"});
}
VOID_TEST(LifecycleTests, unmatched_responses_do_not_reply_or_change_lifecycle) {
vls::Server server;
const auto ignore_responses = [&] {
for (const auto text :
{R"({"jsonrpc":"2.0","id":77,"result":{"ok":true}})",
R"({"jsonrpc":"2.0","id":null,"result":null})",
R"({"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}})",
R"({"jsonrpc":"2.0","id":"unknown","error":{"code":-32601,"message":"unknown"}})"}) {
const auto dispatched = server.handle(text);
Void::Testing::expect(!dispatched.response.has_value());
Void::Testing::expect(!dispatched.exit_code.has_value());
}
};
ignore_responses();
test::expect_error(server, test::shutdown, -32002);
test::initialize(server);
ignore_responses();
static_cast<void>(test::response(server, test::shutdown));
ignore_responses();
Void::Testing::expect_eq(*server.handle(test::exit).exit_code, 0);
}
VOID_TEST(LifecycleTests, parse_errors_use_null_id_and_do_not_mutate_state) {
vls::Server server;
const auto malformed = test::response(server, "{bad");
Void::Testing::expect_eq(test::error_code(malformed), std::int64_t{-32700});
Void::Testing::expect(
std::holds_alternative<std::nullptr_t>(test::field(malformed, "id").value));
test::initialize(server);
test::expect_error(server, "{bad", -32700);
static_cast<void>(test::response(server, test::shutdown));
}
VOID_TEST(LifecycleTests, eof_never_reports_success_without_exit) {
for (const auto wire : {std::string{}, test::frame(test::initialize_request),
test::frame(test::initialize_request) + test::frame(test::shutdown)}) {
std::istringstream input{wire};
std::ostringstream output;
std::ostringstream diagnostics;
Void::Testing::expect_eq(vls::run_stdio(input, output, diagnostics), 1);
Void::Testing::expect(!diagnostics.str().empty());
}
}