#include #include #include #include #include #include #include #include namespace { namespace fs = std::filesystem; using namespace vls::index; using Void::Testing::expect; using Void::Testing::require; SymbolRecord record(std::string name, std::string path, std::uint32_t line, SymbolKind kind = SymbolKind::function) { return {std::move(name), kind, Location{std::move(path), line, 0, line, 4}, std::nullopt, {}, "gcc"}; } ContextInput context(std::string name, std::vector records, bool complete = true, std::string source = "/project/unit.cpp") { ContextInput input; input.context = std::move(name); input.records = std::move(records); input.complete = complete; input.sources = {{source, "content-1"}}; input.dependencies = {source}; return input; } std::size_t count(const Result &result) { return result.symbols.size(); } } // namespace VOID_TEST(SymbolIndex, publishes_and_queries) { SymbolIndex index; require(index.publish(context("debug:unit", {record("f", "/project/unit.cpp", 3)}))); expect(index.records() == 1 && index.generation() == 1); const auto result = index.find({.name = "f"}); require(count(result) == 1); expect(result.symbols.front().name == "f"); expect(result.coverage.indexed && result.coverage.complete && !result.truncated); expect(count(index.find({.name = "missing"})) == 0); } VOID_TEST(SymbolIndex, contexts_distinguish_same_spelling) { SymbolIndex index; require(index.publish(context("debug:a", {record("f", "/project/a.cpp", 1)}))); require(index.publish(context("debug:b", {record("f", "/project/b.cpp", 9)}))); expect(count(index.find({.name = "f"})) == 2); const auto filtered = index.find({.name = "f", .context = "debug:b"}); require(count(filtered) == 1); expect(filtered.symbols.front().location.path == "/project/b.cpp"); } VOID_TEST(SymbolIndex, overlay_shadows_and_reconciles) { SymbolIndex index; require(index.publish(context("debug:unit", {record("f", "/project/unit.cpp", 3)}))); require( index.set_overlay("/project/unit.cpp", "content-2", {record("g", "/project/unit.cpp", 7)})); expect(count(index.find({.name = "f"})) == 0); expect(count(index.find({.name = "g"})) == 1); require(index.clear_overlay("/project/unit.cpp")); expect(count(index.find({.name = "f"})) == 1); expect(count(index.find({.name = "g"})) == 0); } VOID_TEST(SymbolIndex, changed_source_marks_incomplete_without_dropping_records) { SymbolIndex index; require(index.publish(context("debug:unit", {record("f", "/project/unit.cpp", 3)}))); expect(index.invalidate("/project/unit.cpp", std::string{"content-2"}) == 1); const auto coverage = index.coverage(); expect(coverage.indexed && !coverage.complete && coverage.incomplete == std::vector{"debug:unit"}); expect(count(index.find({.name = "f"})) == 1); expect(index.invalidate("/project/unit.cpp", std::string{"content-2"}) == 0); } VOID_TEST(SymbolIndex, dependency_invalidation_without_owned_records) { SymbolIndex index; auto input = context("debug:unit", {record("f", "/project/unit.cpp", 3)}); input.dependencies.push_back("/project/header.hpp"); require(index.publish(input)); expect(index.invalidate("/project/header.hpp", std::nullopt) == 1); expect(!index.coverage().complete); } VOID_TEST(SymbolIndex, removed_context_drops_records) { SymbolIndex index; require(index.publish(context("debug:unit", {record("f", "/project/unit.cpp", 3)}))); require(index.remove("debug:unit")); expect(count(index.find({.name = "f"})) == 0); expect(!index.coverage().indexed); expect(!index.remove("debug:unit")); } VOID_TEST(SymbolIndex, generations_retain_readers) { SymbolIndex index; require(index.publish(context("debug:a", {record("a", "/project/a.cpp", 1)}))); const auto first = index.current(); require(first != nullptr); expect(first->number() == 1 && first->symbols().size() == 1); require(index.publish(context("debug:b", {record("b", "/project/b.cpp", 2)}))); expect(index.current()->number() == 2); expect(first->number() == 1 && first->symbols().size() == 1); } VOID_TEST(SymbolIndex, persists_and_reloads) { Void::FileSystem::TempDir temp{"vls_symbol_index_"}; const auto path = temp.path() / "index.json"; SymbolIndex index; require(index.publish(context("debug:unit", {record("f", "/project/unit.cpp", 3)}))); require( index.set_overlay("/project/unit.cpp", "content-2", {record("g", "/project/unit.cpp", 7)})); require(index.save(path)); SymbolIndex loaded; require(loaded.load(path)); expect(count(loaded.find({.name = "f"})) == 0); expect(count(loaded.find({.name = "g"})) == 1); expect(loaded.generation() == 2); } VOID_TEST(SymbolIndex, rejects_incompatible_or_corrupt_snapshots) { Void::FileSystem::TempDir temp{"vls_symbol_index_"}; SymbolIndex index; require(index.publish(context("debug:unit", {record("f", "/project/unit.cpp", 3)}))); const auto path = temp.path() / "index.json"; require(index.save(path)); auto bytes = [&] { std::ifstream stream{path, std::ios::binary}; return std::string{std::istreambuf_iterator{stream}, {}}; }(); auto incompatible = bytes; const auto schema = incompatible.find("\"schema\":2"); require(schema != std::string::npos); incompatible.replace(schema, 10, "\"schema\":9"); { std::ofstream stream{path, std::ios::binary | std::ios::trunc}; stream << incompatible; } expect(!index.load(path)); { std::ofstream stream{path, std::ios::binary | std::ios::trunc}; stream << "not json"; } expect(!index.load(path)); expect(count(index.find({.name = "f"})) == 1); } VOID_TEST(SymbolIndex, rejects_oversized_snapshots) { Void::FileSystem::TempDir temp{"vls_symbol_index_"}; Limits limits; limits.snapshotBytes = 128; SymbolIndex index{limits}; require(index.publish(context("debug:unit", {record("f", "/project/unit.cpp", 3)}))); const auto path = temp.path() / "index.json"; expect(!index.save(path)); expect(!fs::exists(path)); } VOID_TEST(SymbolIndex, limit_truncates_deterministically) { SymbolIndex index; std::vector records; for (int value = 0; value != 5; ++value) records.push_back(record("f" + std::to_string(value), "/project/unit.cpp", static_cast(value))); require(index.publish(context("debug:unit", records))); const auto result = index.find({.name = "f", .substring = true, .limit = 2}); expect(result.truncated && count(result) == 2); expect(result.symbols.front().name == "f0" && result.symbols.back().name == "f1"); } VOID_TEST(SymbolIndex, rejects_invalid_input) { SymbolIndex index; expect(!index.publish(context("debug:unit", {record("", "/project/unit.cpp", 1)}))); expect(!index.publish(context("debug:unit", {record("f", "", 1)}))); Limits limits; limits.contexts = 1; SymbolIndex bounded{limits}; require(bounded.publish(context("debug:a", {record("a", "/project/a.cpp", 1)}))); expect(!bounded.publish(context("debug:b", {record("b", "/project/b.cpp", 2)}))); expect(!bounded.set_overlay("", "content", {})); } VOID_TEST(SymbolIndex, restore_reports_missing_and_loaded) { Void::FileSystem::TempDir temp{"vls_symbol_index_"}; const auto path = temp.path() / "index.json"; SymbolIndex index; const auto missing = index.restore(path); require(missing.has_value()); expect(missing->status == SymbolIndex::LoadStatus::missing); require(index.publish(context("debug:unit", {record("f", "/project/unit.cpp", 3)}))); require(index.save(path)); const auto loaded = index.restore(path); require(loaded.has_value()); expect(loaded->status == SymbolIndex::LoadStatus::loaded && !loaded->quarantine.has_value()); expect(count(index.find({.name = "f"})) == 1); } VOID_TEST(SymbolIndex, quarantines_incompatible_snapshots_and_rebuilds) { Void::FileSystem::TempDir temp{"vls_symbol_index_"}; const auto path = temp.path() / "index.json"; SymbolIndex index; require(index.publish(context("debug:unit", {record("f", "/project/unit.cpp", 3)}))); require(index.save(path)); auto bytes = [&] { std::ifstream stream{path, std::ios::binary}; return std::string{std::istreambuf_iterator{stream}, {}}; }(); const auto schema = bytes.find("\"schema\":2"); require(schema != std::string::npos); bytes.replace(schema, 10, "\"schema\":9"); { std::ofstream stream{path, std::ios::binary | std::ios::trunc}; stream << bytes; } const auto generation = index.generation(); const auto restored = index.restore(path); require(restored.has_value()); expect(restored->status == SymbolIndex::LoadStatus::incompatible); require(restored->quarantine.has_value()); expect(fs::is_regular_file(*restored->quarantine)); expect(!fs::exists(path)); expect(restored->generation > generation); expect(index.records() == 0 && !index.coverage().indexed); } VOID_TEST(SymbolIndex, quarantines_corrupt_snapshots) { Void::FileSystem::TempDir temp{"vls_symbol_index_"}; const auto path = temp.path() / "index.json"; { std::ofstream stream{path, std::ios::binary | std::ios::trunc}; stream << "not json"; } SymbolIndex index; const auto restored = index.restore(path); require(restored.has_value()); expect(restored->status == SymbolIndex::LoadStatus::corrupt); expect(restored->quarantine.has_value()); expect(!fs::exists(path)); } VOID_TEST(SymbolIndex, generation_backend_persists_and_recovers) { Void::FileSystem::TempDir temp{"vls_symbol_index_"}; const auto root = temp.path() / "index"; SymbolIndex index; require(index.publish(context("debug:a", {record("a", "/project/a.cpp", 1)}))); require(index.save_generation(root)); require(index.publish(context("debug:b", {record("b", "/project/b.cpp", 2)}))); require(index.save_generation(root)); SymbolIndex loaded; const auto latest = loaded.restore_generation(root); require(latest.has_value()); expect(latest->status == SymbolIndex::LoadStatus::loaded); expect(count(loaded.find({.name = "b"})) == 1); { std::ofstream stream{root / "snapshot.json", std::ios::binary | std::ios::trunc}; stream << "corrupt"; } SymbolIndex recovered; const auto restored = recovered.restore_generation(root); require(restored.has_value()); expect(restored->status == SymbolIndex::LoadStatus::loaded); expect(count(recovered.find({.name = "a"})) == 1); } VOID_TEST(SymbolIndex, generation_backend_migrates_a_legacy_snapshot) { Void::FileSystem::TempDir temp{"vls_symbol_index_"}; const auto root = temp.path() / "index"; fs::create_directories(root); SymbolIndex index; require(index.publish(context("debug:a", {record("a", "/project/a.cpp", 1)}))); require(index.save(root / "index.json")); SymbolIndex loaded; const auto restored = loaded.restore_generation(root); require(restored.has_value()); expect(restored->status == SymbolIndex::LoadStatus::loaded); expect(fs::exists(root / "CURRENT") && fs::exists(root / "snapshot.json")); } VOID_TEST(SymbolIndex, generation_backend_reports_missing) { Void::FileSystem::TempDir temp{"vls_symbol_index_"}; SymbolIndex index; const auto restored = index.restore_generation(temp.path() / "absent"); require(restored.has_value()); expect(restored->status == SymbolIndex::LoadStatus::missing); } VOID_TEST(SymbolIndex, generation_selector_migrates_from_the_legacy_format) { Void::FileSystem::TempDir temp{"vls_symbol_index_"}; const auto root = temp.path() / "index"; SymbolIndex index; require(index.publish(context("debug:a", {record("a", "/project/a.cpp", 1)}))); require(index.save_generation(root)); { std::ofstream stream{root / "CURRENT", std::ios::binary | std::ios::trunc}; stream << "snapshot.json\n"; } SymbolIndex loaded; const auto restored = loaded.restore_generation(root); require(restored.has_value()); expect(restored->status == SymbolIndex::LoadStatus::loaded); std::ifstream selector{root / "CURRENT", std::ios::binary}; const std::string text{std::istreambuf_iterator{selector}, {}}; expect(text.find("\"schema\":2") != std::string::npos); { std::ofstream stream{root / "CURRENT", std::ios::binary | std::ios::trunc}; stream << "future.json\n"; } SymbolIndex rejected; expect(!rejected.restore_generation(root).has_value()); } VOID_TEST(SymbolIndex, generation_backend_recovers_a_missing_current_file) { Void::FileSystem::TempDir temp{"vls_symbol_index_"}; const auto root = temp.path() / "index"; SymbolIndex index; require(index.publish(context("debug:a", {record("a", "/project/a.cpp", 1)}))); require(index.save_generation(root)); require(index.publish(context("debug:b", {record("b", "/project/b.cpp", 2)}))); require(index.save_generation(root)); fs::remove(root / "snapshot.json"); SymbolIndex recovered; const auto restored = recovered.restore_generation(root); require(restored.has_value()); expect(restored->status == SymbolIndex::LoadStatus::loaded); expect(count(recovered.find({.name = "a"})) == 1); expect(fs::is_regular_file(root / "snapshot.json")); } namespace { std::string file_text(const fs::path &path) { std::ifstream stream{path, std::ios::binary}; require(stream.good()); return {std::istreambuf_iterator{stream}, std::istreambuf_iterator{}}; } void put_text(const fs::path &path, std::string_view text) { std::ofstream stream{path, std::ios::binary | std::ios::trunc}; stream.write(text.data(), static_cast(text.size())); } // Rewrites a schema-2 snapshot as the earlier schema-1 format: no digest. std::string downgrade(std::string bytes) { const auto digest = bytes.find(",\"digest\":\""); require(digest != std::string::npos); const auto end = bytes.find('"', digest + 12); require(end != std::string::npos); bytes.erase(digest, end + 1 - digest); const auto schema = bytes.find("\"schema\":2"); require(schema != std::string::npos); bytes.replace(schema, 10, "\"schema\":1"); return bytes; } } // namespace VOID_TEST(SymbolIndex, snapshot_schema_migration_is_reported_and_rewritten) { Void::FileSystem::TempDir temp{"vls_symbol_index_"}; const auto path = temp.path() / "index.json"; SymbolIndex index; require(index.publish(context("debug:unit", {record("f", "/project/unit.cpp", 3)}))); require(index.save(path)); put_text(path, downgrade(file_text(path))); SymbolIndex loaded; const auto restored = loaded.restore(path); require(restored.has_value()); expect(restored->status == SymbolIndex::LoadStatus::loaded); expect(restored->migrated); expect(count(loaded.find({.name = "f"})) == 1); const auto upgraded = temp.path() / "upgraded.json"; require(loaded.save(upgraded)); const auto bytes = file_text(upgraded); expect(bytes.find("\"schema\":2") != std::string::npos); expect(bytes.find("\"digest\":\"") != std::string::npos); SymbolIndex reread; expect(reread.load(upgraded)); expect(count(reread.find({.name = "f"})) == 1); } VOID_TEST(SymbolIndex, generation_restore_persists_the_migrated_schema) { Void::FileSystem::TempDir temp{"vls_symbol_index_"}; const auto root = temp.path() / "index"; SymbolIndex index; require(index.publish(context("debug:a", {record("a", "/project/a.cpp", 1)}))); require(index.save_generation(root)); put_text(root / "snapshot.json", downgrade(file_text(root / "snapshot.json"))); SymbolIndex loaded; const auto restored = loaded.restore_generation(root); require(restored.has_value()); expect(restored->status == SymbolIndex::LoadStatus::loaded); expect(restored->migrated); expect(count(loaded.find({.name = "a"})) == 1); const auto bytes = file_text(root / "snapshot.json"); expect(bytes.find("\"schema\":2") != std::string::npos); expect(bytes.find("\"digest\":\"") != std::string::npos); expect(fs::is_regular_file(root / "snapshot.prev.json")); SymbolIndex reread; const auto again = reread.restore_generation(root); require(again.has_value()); expect(again->status == SymbolIndex::LoadStatus::loaded); expect(!again->migrated); } VOID_TEST(SymbolIndex, digest_mismatch_is_quarantined_as_corruption) { Void::FileSystem::TempDir temp{"vls_symbol_index_"}; const auto path = temp.path() / "index.json"; SymbolIndex index; require(index.publish(context("debug:unit", {record("f", "/project/unit.cpp", 3)}))); require(index.save(path)); auto bytes = file_text(path); const auto digest = bytes.find("\"digest\":\""); require(digest != std::string::npos); const auto digit = digest + 10; bytes[digit] = bytes[digit] == '0' ? '1' : '0'; put_text(path, bytes); const auto restored = index.restore(path); require(restored.has_value()); expect(restored->status == SymbolIndex::LoadStatus::corrupt); expect(restored->quarantine.has_value()); expect(!fs::exists(path)); } VOID_TEST(SymbolIndex, schema_2_rejects_edited_records) { Void::FileSystem::TempDir temp{"vls_symbol_index_"}; const auto path = temp.path() / "index.json"; SymbolIndex index; require(index.publish(context("debug:unit", {record("f", "/project/unit.cpp", 3)}))); require(index.save(path)); auto bytes = file_text(path); const auto name = bytes.find("\"name\":\"f\""); require(name != std::string::npos); bytes.replace(name, 11, "\"name\":\"\""); put_text(path, bytes); const auto restored = index.restore(path); require(restored.has_value()); // The digest still matches (the edit is a consistent rewrite only by hand), // so either the digest or the record validation rejects it as corrupt. expect(restored->status == SymbolIndex::LoadStatus::corrupt); expect(restored->quarantine.has_value()); }