Files
Language-Server/tests/debug/DapReplayTests.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

355 lines
14 KiB
C++

#include "Trace.hpp"
#include <Void/LanguageServer/Protocol/Framing.hpp>
#include <Void/System/Process.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <vls/debug/inspection/ReplaySession.hpp>
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <filesystem>
#include <format>
#include <initializer_list>
#include <sstream>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
namespace {
namespace protocol = Void::LanguageServer::Protocol;
using protocol::Json;
using Void::Testing::expect;
using Void::Testing::expect_eq;
using Void::Testing::require;
using namespace std::chrono_literals;
using namespace std::string_view_literals;
std::filesystem::path adapter_executable() {
#ifdef VLS_DAP_EXECUTABLE
return VLS_DAP_EXECUTABLE;
#else
return std::filesystem::read_symlink("/proc/self/exe").parent_path() / "vls-dap";
#endif
}
const Json *member(const Json &value, std::string_view key) {
const auto *object = protocol::as_object(&value);
require(object != nullptr);
return protocol::find_member(*object, key);
}
const Json &field(const Json &value, std::string_view key) {
const auto *entry = member(value, key);
require(entry != nullptr);
return *entry;
}
const Json::Array &array(const Json &value, std::string_view key) {
const auto *entries = protocol::as_array(member(value, key));
require(entries != nullptr);
return *entries;
}
std::string_view text(const Json &value, std::string_view key) {
const auto *entry = protocol::as_string(member(value, key));
require(entry != nullptr);
return *entry;
}
std::int64_t integer(const Json &value, std::string_view key) {
const auto *entry = std::get_if<std::int64_t>(&field(value, key).value);
require(entry != nullptr);
return *entry;
}
struct Request {
std::string_view command;
std::string_view arguments{"{}"};
};
constexpr Request initialize{"initialize", R"({"adapterID":"vls"})"};
constexpr Request continue_thread{"continue", R"({"threadId":1})"};
constexpr Request stack_trace{"stackTrace", R"({"threadId":1})"};
struct Transcript {
std::vector<Json> messages;
const Json &response(int sequence, std::string_view command, bool success = true) const {
const auto matches = [sequence](const Json &message) {
return text(message, "type") == "response" &&
integer(message, "request_seq") == sequence;
};
const auto found = std::ranges::find_if(messages, matches);
require(found != messages.end());
expect_eq(std::ranges::count_if(messages, matches), std::ptrdiff_t{1});
expect_eq(text(*found, "command"), command);
expect(protocol::as_bool(member(*found, "success")) == success);
return *found;
}
std::vector<const Json *> events(std::string_view name) const {
std::vector<const Json *> result;
for (const auto &message : messages) {
if (text(message, "type") == "event" && text(message, "event") == name)
result.push_back(&message);
}
return result;
}
};
Transcript
run(std::initializer_list<Request> requests,
const std::filesystem::path &fixture = std::filesystem::path{__FILE__}.parent_path() /
"fixtures" / "integral-17.vls",
std::size_t output_limit = 128 * 1024) {
std::ostringstream input;
int client_sequence = 0;
for (const auto &request : requests) {
const auto body =
std::format(R"({{"seq":{},"type":"request","command":"{}","arguments":{}}})",
++client_sequence, request.command, request.arguments);
require(protocol::write_frame(input, body).has_value());
}
require(fixture.is_absolute());
Void::Process::SpawnOptions options;
options.executable = adapter_executable();
require(options.executable.is_absolute());
options.arguments = {"--replay", fixture.string()};
options.search_path = false;
options.environment.mode = Void::Process::EnvMode::explicit_only;
options.environment.set_vars = {{"LC_ALL", "C"}, {"PATH", ""}};
options.kill_grace = 100ms;
auto child = Void::Process::spawn(options);
require(child.has_value());
Void::Process::CaptureLimits limits;
limits.max_stdin_bytes = 8192;
limits.max_stdout_bytes = output_limit;
limits.max_line_bytes = output_limit;
limits.max_stderr_bytes = 8192;
const auto output = Void::Process::communicate_blocking(*child, input.str(), limits, 4s);
require(output.has_value());
require(!output->timed_out && !output->cancelled && !output->overflowed);
require(output->status.exited);
expect_eq(output->status.code, 0);
expect_eq(output->err, std::string{});
Transcript transcript;
std::istringstream replies{output->out};
std::int64_t adapter_sequence = 0;
for (;;) {
const auto frame = protocol::read_frame(replies, {.body_bytes = limits.max_stdout_bytes});
require(frame.has_value());
if (!*frame)
break;
auto message = protocol::parse_json(**frame);
require(message.has_value());
expect_eq(integer(*message, "seq"), ++adapter_sequence);
const auto type = text(*message, "type");
require(type == "event" || type == "response");
if (type == "response") {
const auto request_sequence = integer(*message, "request_seq");
expect(request_sequence >= 1 && request_sequence <= client_sequence);
} else {
expect(text(*message, "event") != "exited");
}
transcript.messages.push_back(std::move(*message));
}
return transcript;
}
void write_manifest(const std::filesystem::path &path, std::string_view source) {
auto trace =
vls::gcc::Trace::create(path, source, std::string(64, 'a'), std::string(32, 'b'), 1234);
require(trace.has_value());
}
void expect_before(const Json &first, const Json &second) {
expect(integer(first, "seq") < integer(second, "seq"));
}
void expect_stack(const Json &reply, std::initializer_list<std::string_view> names) {
const auto &frames = array(field(reply, "body"), "stackFrames");
require(frames.size() == names.size());
auto name = names.begin();
for (const auto &frame : frames) {
expect_eq(text(frame, "name"), *name++);
expect(integer(frame, "id") > 0);
expect(member(frame, "source") == nullptr);
expect_eq(integer(frame, "line"), std::int64_t{0});
expect_eq(integer(frame, "column"), std::int64_t{0});
}
}
} // namespace
VOID_TEST(DapReplay, deferred_launch_finishes_after_configuration_done) {
const auto dap =
run({initialize,
{"launch"},
{"setBreakpoints",
R"({"source":{"name":"input.cpp"},"breakpoints":[{"line":5},{"line":10}]})"},
{"setExceptionBreakpoints", R"({"filters":[]})"},
{"configurationDone"},
{"threads"},
stack_trace,
continue_thread,
stack_trace,
{"disconnect"}});
const auto &init = dap.response(1, "initialize");
const auto initialized = dap.events("initialized");
require(initialized.size() == 1);
expect_eq(integer(init, "seq"), std::int64_t{1});
expect_before(init, *initialized.front());
const auto &capabilities = field(init, "body");
for (const auto name : {"supportsConfigurationDoneRequest", "supportsLoadedSourcesRequest",
"supportsTerminateRequest"}) {
expect(protocol::as_bool(member(capabilities, name)) == true);
}
for (const auto name : {"supportsStepBack", "supportsSteppingGranularity",
"supportsRestartFrame", "supportsDisassembleRequest"}) {
expect(protocol::as_bool(member(capabilities, name)) != true);
}
const auto &breakpoint_reply = dap.response(3, "setBreakpoints");
const auto &breakpoints = array(field(breakpoint_reply, "body"), "breakpoints");
require(breakpoints.size() == 2);
for (const auto &breakpoint : breakpoints)
expect(protocol::as_bool(member(breakpoint, "verified")) == false);
dap.response(4, "setExceptionBreakpoints");
const auto &configured = dap.response(5, "configurationDone");
const auto &launch = dap.response(2, "launch");
const auto stopped = dap.events("stopped");
require(stopped.size() == 2);
expect_before(*initialized.front(), configured);
expect_before(configured, launch);
expect_before(launch, *stopped[0]);
expect_before(dap.response(8, "continue"), *stopped[1]);
for (const auto *stop : stopped) {
const auto &body = field(*stop, "body");
expect_eq(integer(body, "threadId"), std::int64_t{1});
expect(!text(body, "reason").empty());
}
const auto &threads = array(field(dap.response(6, "threads"), "body"), "threads");
require(threads.size() == 1);
expect_eq(integer(threads.front(), "id"), std::int64_t{1});
expect_stack(dap.response(7, "stackTrace"), {"reflected_value", "twice"});
expect_stack(dap.response(9, "stackTrace"), {"twice"});
dap.response(10, "disconnect");
}
VOID_TEST(DapReplay, configuration_done_can_precede_launch_and_terminate) {
const auto dap =
run({initialize, {"configurationDone"}, {"launch"}, {"terminate"}, {"disconnect"}});
const auto &configured = dap.response(2, "configurationDone");
const auto &launch = dap.response(3, "launch");
const auto stopped = dap.events("stopped");
const auto terminated = dap.events("terminated");
require(stopped.size() == 1);
require(terminated.size() == 1);
expect_before(configured, launch);
expect_before(launch, *stopped.front());
expect_before(*stopped.front(), dap.response(4, "terminate"));
expect_before(dap.response(4, "terminate"), *terminated.front());
dap.response(5, "disconnect");
}
VOID_TEST(DapReplay, disconnect_settles_a_deferred_launch) {
const auto dap = run({initialize, {"launch"}, {"disconnect"}});
dap.response(1, "initialize");
dap.response(2, "launch", false);
dap.response(3, "disconnect");
expect(dap.events("stopped").empty());
}
VOID_TEST(DapReplay, one_session_identity_has_one_cursor_and_finishes_once) {
using vls::debug::ReplaySession;
static_assert(!std::is_copy_constructible_v<ReplaySession>);
static_assert(!std::is_copy_assignable_v<ReplaySession>);
const auto recording =
std::filesystem::path{__FILE__}.parent_path() / "fixtures" / "integral-17.vls";
auto opened = ReplaySession::open(recording);
require(opened.has_value());
auto session = std::move(*opened);
require(session.seek(6).has_value());
const auto stopped = session.identity();
session.finish();
const auto done = session.identity();
expect(!session.matches(stopped));
expect(!session.seek(10));
expect(!session.move_to_value(true));
expect(!session.initial_position());
expect(!session.paused());
session.finish();
expect(session.matches(done));
}
VOID_TEST(DapReplay, binary_source_keeps_its_digest_and_has_no_text_reference) {
Void::FileSystem::TempDir dir{"vls_dap_source_"};
const auto path = dir / "binary.vls";
write_manifest(path, "a\0\xffz"sv);
const auto dap = run({initialize,
{"launch"},
{"configurationDone"},
{"vls/session"},
{"loadedSources"},
{"source", R"({"sourceReference":1})"},
{"disconnect"}},
path);
const auto &state = field(dap.response(4, "vls/session"), "body");
expect(protocol::as_bool(member(state, "sourceAvailable")) == false);
expect_eq(integer(state, "sourceByteLength"), std::int64_t{4});
expect_eq(text(state, "sourceSha256").size(), std::size_t{64});
expect(member(state, "sourceReference") == nullptr);
expect(array(field(dap.response(5, "loadedSources"), "body"), "sources").empty());
dap.response(6, "source", false);
expect(dap.events("stopped").empty());
}
VOID_TEST(DapReplay, full_size_control_filled_source_round_trips_as_exact_text) {
Void::FileSystem::TempDir dir{"vls_dap_source_"};
const auto path = dir / "controls.vls";
std::string source(1024 * 1024, '\0');
source.back() = '\x1f';
write_manifest(path, source);
const auto dap = run({initialize,
{"configurationDone"},
{"launch"},
{"source", R"({"sourceReference":1})"},
{"disconnect"}},
path, 8 * 1024 * 1024);
const auto &body = field(dap.response(4, "source"), "body");
expect(text(body, "content") == source);
expect(dap.events("stopped").empty());
}
VOID_TEST(DapReplay, unsupported_or_unreachable_controls_fail_without_moving) {
const auto dap = run({initialize,
continue_thread,
{"launch"},
{"configurationDone"},
{"next", R"({"threadId":1})"},
{"stepBack", R"({"threadId":1})"},
{"continue", R"({"threadId":2})"},
stack_trace,
{"disconnect"}});
dap.response(2, "continue", false);
dap.response(3, "launch");
dap.response(4, "configurationDone");
dap.response(5, "next", false);
dap.response(6, "stepBack", false);
dap.response(7, "continue", false);
expect_stack(dap.response(8, "stackTrace"), {"reflected_value", "twice"});
dap.response(9, "disconnect");
expect(dap.events("stopped").size() == 1);
}