#include #include #include #include #include #include #include #include #include #include namespace { namespace fs = std::filesystem; namespace D = Void::LanguageServer::Documents; using namespace vls::semantic; using Void::Testing::expect; using Void::Testing::require; struct Files { Void::FileSystem::TempDir temp{"vls_source_catalog_"}; fs::path root = fs::canonical(temp.path()) / "workspace"; fs::path external = root.parent_path() / "external"; SourceScope scope{{root, "workspace-1"}, 3, 5}; D::DocumentStore documents; SourceCatalog catalog; InstalledSourceGrant grant; explicit Files(std::function hook = {}, D::DocumentLimits limits = {}) : documents(D::PositionEncoding::utf16, limits), catalog(documents, std::move(hook)) { fs::create_directories(root / "nested"); fs::create_directory(external); grant = install({root}); } InstalledSourceGrant install(std::initializer_list roots) { auto result = catalog.install(scope, roots); require(result.has_value()); return *result; } D::FileOverlayInventory census() const { auto result = documents.file_overlay_inventory(); require(result.has_value()); return std::move(*result); } auto take(std::initializer_list requests, SourceLimits limits = {}) { return catalog.capture(scope, grant, census(), requests, limits); } static void write(const fs::path &path, std::string_view text) { std::ofstream out(path, std::ios::binary); out << text; out.close(); require(out.good()); } auto open(const fs::path &path, std::string text) { return documents.open(D::path_to_file_uri(path), "cpp", 1, std::move(text)); } }; void failed(const auto &result, SourceIssue issue) { require(!result.has_value()); expect(result.error().issue == issue); } class AccessWatch { Void::System::OwnedFd events_{::inotify_init1(IN_NONBLOCK | IN_CLOEXEC)}; public: explicit AccessWatch(const fs::path &path) { require(events_ && ::inotify_add_watch(events_.get(), path.c_str(), IN_ACCESS) >= 0); } bool accessed() const { alignas(inotify_event) std::array buffer; auto n = ::read(events_.get(), buffer.data(), buffer.size()); require(n > 0 || (n < 0 && errno == EAGAIN)); return n > 0; } }; } // namespace VOID_TEST(SourceCatalogTests, lookup_captures_directories_and_regular_presence_without_reading) { Files f; const auto dir = f.root / "nested", file = dir / "input.hpp"; Files::write(file, "one"); AccessWatch watch(file); auto capture = f.take({{f.root, true, true}, {dir, true, true}, {file, true, true}}); require(capture.has_value()); const auto entries = (*capture)->entries(); expect(entries[0].node == SourceNode::directory && entries[1].node == SourceNode::directory); expect(entries[2].node == SourceNode::regular && !entries[2].snapshot); expect(!watch.accessed()); Files::write(dir / "unrelated.hpp", ""); expect(f.catalog.validate(f.scope, **capture).has_value()); Files::write(file, "two"); failed(f.catalog.validate(f.scope, **capture), SourceIssue::changed); } VOID_TEST(SourceCatalogTests, first_missing_directory_invalidates_deeper_optional_lookup) { Files f; const auto dir = f.root / "future", file = dir / "deeper" / "optional.hpp"; auto capture = f.take({{file, true, true}}); require(capture.has_value()); expect((*capture)->entries().front().node == SourceNode::missing); fs::create_directory(dir); failed(f.catalog.validate(f.scope, **capture), SourceIssue::changed); failed(f.take({{file, true}}), SourceIssue::missing_parent); require(f.open(file, "unsaved").has_value()); failed(f.take({{file, true, true}}), SourceIssue::missing_parent); } VOID_TEST(SourceCatalogTests, captures_disk_and_unsaved_source_in_request_order) { Files f; const auto source = f.root / "source.cpp", header = f.root / "nested" / "α.hpp"; Files::write(source, "saved"); Files::write(header, "disk header"); auto overlay = f.open(header, ""); require(overlay.has_value()); AccessWatch watch(header); auto cap = f.take({{header}, {source}, {header}}); require(cap.has_value()); auto entries = (*cap)->entries(); require(entries.size() == 3); expect(entries[0].snapshot == *overlay && entries[2].snapshot == *overlay); expect(entries[1].snapshot->text == "saved"); expect(entries[1].snapshot->origin == D::DocumentOrigin::disk); expect(entries[0].logical_path == header && entries[0].snapshot->identity.canonical_path == header); expect(!watch.accessed()); expect(f.catalog.validate(f.scope, **cap).has_value()); } VOID_TEST(SourceCatalogTests, absent_optional_leaf_becomes_an_unsaved_then_a_saved_header) { Files f; auto header = f.root / "absent.hpp"; failed(f.take({{header}}), SourceIssue::missing); auto old = f.take({{header, true}}); require(old.has_value()); expect(!(*old)->entries()[0].snapshot); require(f.open(header, "new overlay").has_value()); failed(f.catalog.validate(f.scope, **old), SourceIssue::changed); auto unsaved = f.take({{header}}); require(unsaved.has_value()); expect((*unsaved)->entries()[0].snapshot->text == "new overlay"); require(f.documents.close(D::path_to_file_uri(header)).has_value()); auto absent = f.take({{header, true}}); require(absent.has_value()); Files::write(header, "new disk"); failed(f.catalog.validate(f.scope, **absent), SourceIssue::changed); fs::remove(header); failed(f.catalog.validate(f.scope, **absent), SourceIssue::changed); expect((*unsaved)->entries()[0].snapshot->text == "new overlay"); } VOID_TEST(SourceCatalogTests, binds_job_and_installed_grants_before_reading_shared_files) { Files f; const auto path = f.external / "shared.hpp"; Files::write(path, "shared"); const auto first = f.install({f.root, f.external}); SourceScope other{{f.root, "another-incarnation"}, f.scope.configuration, f.scope.trust}; const auto requests = std::array{SourceRequest{path}}; AccessWatch watch(path); auto attempt = [&](const SourceScope &scope, const InstalledSourceGrant &grant) { return f.catalog.capture(scope, grant, f.census(), requests); }; failed(attempt(other, first), SourceIssue::wrong_authority); auto old_settings = f.scope; ++old_settings.configuration; failed(attempt(old_settings, first), SourceIssue::wrong_authority); auto old_trust = f.scope; ++old_trust.trust; failed(attempt(old_trust, first), SourceIssue::wrong_authority); f.catalog.revoke(); const auto renewed = f.install({f.root, f.external}); failed(attempt(f.scope, first), SourceIssue::wrong_authority); expect(!watch.accessed()); require(attempt(f.scope, renewed).has_value()); expect(watch.accessed()); } VOID_TEST(SourceCatalogTests, retains_the_callers_empty_census_and_only_the_selected_text) { Files f; const auto tu = f.root / "tu.cpp", header = f.root / "new.hpp"; Files::write(tu, "tu"); const auto before = f.census(); auto overlay = f.open(header, "unsaved"); require(overlay.has_value()); std::weak_ptr unrelated = *overlay; overlay->reset(); auto requests = std::array{SourceRequest{tu}}; AccessWatch watch(tu); failed(f.catalog.capture(f.scope, f.grant, before, requests), SourceIssue::changed); expect(!watch.accessed()); auto selected = f.take({{tu}}); require(selected.has_value()); require(f.documents.close(D::path_to_file_uri(header)).has_value()); expect(unrelated.expired()); failed(f.catalog.validate(f.scope, **selected), SourceIssue::changed); } VOID_TEST(SourceCatalogTests, refuses_a_desynchronized_or_refused_overlay_without_reading_disk) { D::DocumentLimits limits; limits.max_open_documents = 1; Files f{{}, limits}; const auto a = f.root / "a.cpp", b = f.root / "b.cpp"; Files::write(a, "disk-a"); Files::write(b, "disk-b"); require(f.open(a, "open").has_value()); require(!f.open(b, "refused").has_value()); AccessWatch first(a), second(b); require(f.documents .mark_desynchronized(D::path_to_file_uri(a), 2, {D::DocumentError::Kind::malformed_change, "bad edit"}) .has_value()); failed(f.take({{a}}), SourceIssue::unavailable_overlay); failed(f.take({{b}}), SourceIssue::unavailable_overlay); expect(!first.accessed() && !second.accessed()); } VOID_TEST(SourceCatalogTests, rejects_another_editor_stores_census_before_disk_access) { Files f; const auto source = f.root / "edited.cpp"; Files::write(source, "disk"); require(f.open(source, "editor").has_value()); D::DocumentStore different_editor; auto empty = different_editor.file_overlay_inventory(); require(empty.has_value()); AccessWatch watch(source); const std::array requests{SourceRequest{source}}; failed(f.catalog.capture(f.scope, f.grant, *empty, requests), SourceIssue::changed); auto correct = f.take({{source}}); require(correct.has_value()); expect((*correct)->entries()[0].snapshot->text == "editor"); expect(!watch.accessed()); } VOID_TEST(SourceCatalogTests, refuses_two_open_uri_aliases_for_one_physical_source) { Files f; auto path = f.root / "target.cpp", alias = f.root / "alias.cpp"; Files::write(path, "disk"); fs::create_symlink(path, alias); require(f.open(path, "one").has_value()); require(f.open(alias, "two").has_value()); failed(f.take({{path}}), SourceIssue::unavailable_overlay); } VOID_TEST(SourceCatalogTests, invalidates_identical_bytes_on_leaf_and_directory_replacement) { Files f; auto parent = f.root / "nested", path = parent / "a.cpp"; Files::write(path, "stable"); f.grant = f.install({f.root, parent, f.external}); auto old = f.take({{path}}); require(old.has_value()); Files::write(parent / "new", "stable"); fs::rename(parent / "new", path); failed(f.catalog.validate(f.scope, **old), SourceIssue::changed); auto fresh = f.take({{path}}); require(fresh.has_value()); fs::rename(parent, f.root / "old"); fs::create_directory(parent); Files::write(path, "new"); failed(f.catalog.validate(f.scope, **fresh), SourceIssue::changed); failed(f.take({{path}}), SourceIssue::changed); expect((*old)->entries()[0].snapshot->text == "stable"); } VOID_TEST(SourceCatalogTests, replacement_of_the_workspace_invalidates_an_external_only_capture) { Files f; f.grant = f.install({f.external}); const auto path = f.external / "header.hpp"; Files::write(path, "header"); auto old = f.take({{path}}); require(old.has_value()); fs::rename(f.root, f.external.parent_path() / "old-workspace"); fs::create_directory(f.root); failed(f.catalog.validate(f.scope, **old), SourceIssue::changed); failed(f.take({{path}}), SourceIssue::changed); } VOID_TEST(SourceCatalogTests, refuses_hard_links_and_special_files_before_reading) { Files f; const auto primary = f.root / "public.hpp", other = f.root / "impl.hpp"; Files::write(primary, "stale"); fs::create_hard_link(primary, other); require(f.open(other, "").has_value()); AccessWatch watch(primary); failed(f.take({{primary}}), SourceIssue::hard_link); expect(!watch.accessed()); const auto fifo = f.root / "fifo"; require(::mkfifo(fifo.c_str(), 0600) == 0); failed(f.take({{fifo}}), SourceIssue::not_regular); failed(f.take({{f.root / "nested"}}), SourceIssue::not_regular); } VOID_TEST(SourceCatalogTests, refuses_unsupported_and_outside_routes_before_source_reads) { Files f; auto outside = f.external / "secret.hpp", link = f.root / "leaf", dir = f.root / "dir"; Files::write(outside, "benign external fixture"); fs::create_symlink(outside, link); fs::create_directory_symlink(f.external, dir); AccessWatch watch(outside); failed(f.take({{outside}}), SourceIssue::outside_scope); failed(f.take({{link, true}}), SourceIssue::unsupported_route); failed(f.take({{dir / "secret.hpp", true}}), SourceIssue::unsupported_route); failed(f.take({{f.root / "nested" / ".." / "leaf"}}), SourceIssue::invalid_path); failed(f.take({{f.root / "missing" / "a.hpp", true}}), SourceIssue::missing_parent); expect(!watch.accessed()); } VOID_TEST(SourceCatalogTests, rejects_conflicting_ancestor_and_descendant_file_names_in_either_order) { Files f; auto parent = f.root / "generated", child = parent / "config.hpp"; require(f.open(child, "unsaved").has_value()); failed(f.take({{parent, true}, {child}}), SourceIssue::conflicting_names); failed(f.take({{child}, {parent, true}}), SourceIssue::conflicting_names); require(f.open(parent, "separate file").has_value()); failed(f.take({{parent}, {child}}), SourceIssue::conflicting_names); failed(f.take({{child}, {parent}}), SourceIssue::conflicting_names); } VOID_TEST(SourceCatalogTests, validates_authority_editor_and_disk_again_after_capture) { std::function change; Files f{[&] { if (change) change(); }}; const auto path = f.root / "a.cpp"; Files::write(path, "before"); change = [&] { Files::write(path, "after"); }; failed(f.take({{path}}), SourceIssue::changed); change = [&] { require(f.open(path, "now unsaved").has_value()); }; failed(f.take({{path}}), SourceIssue::changed); change = [&] { std::array edits{D::TextEdit{std::nullopt, "later"}}; require(f.documents.apply_changes(D::path_to_file_uri(path), 2, edits).has_value()); }; failed(f.take({{path}}), SourceIssue::changed); change = [&] { f.catalog.revoke(); }; failed(f.take({{path}}), SourceIssue::wrong_authority); } VOID_TEST(SourceCatalogTests, enforces_count_bytes_descriptors_and_retained_capture_limits) { Files f; auto a = f.root / "a.cpp", b = f.root / "nested" / "b.hpp"; Files::write(a, "abc"); Files::write(b, "defg"); failed(f.take({{a}, {b}}, {.files = 1}), SourceIssue::resource_limit); failed(f.take({{a}}, {.name_bytes = 1}), SourceIssue::resource_limit); failed(f.take({{a}}, {.file_bytes = 2}), SourceIssue::resource_limit); failed(f.take({{a}, {b}}, {.total_bytes = 6}), SourceIssue::resource_limit); failed(f.take({{b}}, {.descriptors = 1}), SourceIssue::resource_limit); require(f.take({{a}, {b}}, {.total_bytes = 7}).has_value()); std::vector> retained; for (unsigned i = 0; i < 63; ++i) { auto cap = f.take({{a}}); require(cap.has_value()); retained.push_back(std::move(*cap)); } failed(f.take({{a}}), SourceIssue::resource_limit); retained.pop_back(); expect(f.take({{a}}).has_value()); }