#include #include #include #include #include #include #include #include #include #include #include namespace { using namespace Void::LanguageServer::Documents; using Void::Testing::expect; using Void::Testing::require; using Kind = DocumentError::Kind; namespace fs = std::filesystem; struct Files { Void::FileSystem::TempDir directory{"vls_bound_inputs_"}; fs::path root = fs::canonical(directory.path()); fs::path path(std::string_view name) const { return root / name; } }; void write(const fs::path &path, std::string_view text) { std::ofstream output{path, std::ios::binary}; output << text; output.close(); require(static_cast(output)); } template T get(std::expected result, std::source_location loc = std::source_location::current()) { require(result.has_value(), loc); return std::move(*result); } template void expect_error(const std::expected &result, Kind kind, std::source_location loc = std::source_location::current()) { require(!result.has_value(), loc); expect(result.error().kind == kind, loc); } auto observe(const DocumentStore &store, const FileOverlayInventory &inventory, const fs::path &path) { return store.observe_bound_source(inventory, path_to_file_uri(path), path); } auto replace(DocumentStore &store, std::string_view uri, std::int32_t version, std::string text) { return store.apply_changes(uri, version, std::array{TextEdit{std::nullopt, std::move(text)}}); } const FileOverlayBinding &binding(const FileOverlayInventory &inventory, std::string_view uri) { const auto entries = inventory.entries(); const auto found = std::ranges::find_if( entries, [&](const auto &entry) { return entry.identity.canonical_uri == uri; }); require(found != entries.end()); return *found; } } // namespace VOID_TEST(BoundInputs, disk_snapshot_uses_caller_bytes_and_exact_bound_names) { Files files; const auto physical = files.path("one.cpp"); const auto logical = files.path("other.cpp"); write(physical, "ambient physical"); write(logical, "ambient logical"); const auto uri = path_to_file_uri(logical); const auto display = "FILE://LOCALHOST" + uri.substr(7); const std::string bytes = "a\U0001f600\r\nb"; for (const auto encoding : {PositionEncoding::utf8, PositionEncoding::utf16}) { unsigned ambient_reads = 0; DocumentStore store{encoding, {}, [&] { ++ambient_reads; }}; auto inventory = get(store.file_overlay_inventory(0)); const auto observation = get(store.observe_bound_source(inventory, display, physical)); expect(!observation.overlay()); expect(observation.identity().canonical_uri == uri); expect(observation.identity().display_uri == display); expect(observation.identity().canonical_path == physical); auto disk = get(store.materialize_bound_disk(observation, bytes)); expect(disk->identity == observation.identity() && disk->text == bytes); expect(disk->origin == DocumentOrigin::disk && !disk->version && disk->encoding == encoding); const auto column = encoding == PositionEncoding::utf8 ? 5u : 3u; expect(disk->offset_to_position(5) == Position{0, column}); expect(disk->position_to_offset({1, 0}) == 7); auto again = get(store.materialize_bound_disk(observation, bytes)); expect(again->revision_id != disk->revision_id && again->same_input_as(*disk)); expect(!get(store.materialize_bound_disk(observation, "different"))->same_input_as(*disk)); expect(get(store.materialize_bound_disk(observation, ""))->text.empty()); expect(store.validate_bound_sources(inventory, std::array{observation}).has_value()); expect(ambient_reads == 0); expect(get(store.resolve_content(physical))->text == "ambient physical"); expect(ambient_reads == 1); } } VOID_TEST(BoundInputs, empty_inventory_detects_a_new_unsaved_header) { Files files; const auto tu = files.path("main.cpp"); const auto header = files.path("optional.hpp"); const auto uri = path_to_file_uri(header); DocumentStore store; require(store.open("untitled:scratch", "cpp", 1, "virtual").has_value()); const auto empty = get(store.file_overlay_inventory(0)); require(empty.entries().empty()); const auto disk = get(observe(store, empty, tu)); const auto new_header = get(store.open(uri, "cpp", 1, "")); expect(!fs::exists(header)); expect_error(store.validate_bound_sources(empty), Kind::stale_read); expect_error(observe(store, empty, tu), Kind::stale_read); expect_error(store.materialize_bound_disk(disk, "old"), Kind::stale_read); const auto current = get(store.file_overlay_inventory(1)); require(current.entries().size() == 1); const auto &entry = binding(current, uri); expect(entry.identity.canonical_path == header && entry.content == new_header); const auto selected = get(observe(store, current, header)); expect(selected.overlay() == new_header && selected.overlay()->text.empty()); expect_error(store.materialize_bound_disk(selected, ""), Kind::already_open); const auto current_tu = get(observe(store, current, tu)); expect_error(store.validate_bound_sources(current, std::array{disk}), Kind::stale_read); expect_error(store.validate_bound_sources(empty, std::array{current_tu}), Kind::stale_read); expect(store.validate_bound_sources(current, std::array{current_tu, selected}).has_value()); } VOID_TEST(BoundInputs, selection_uses_current_membership_and_per_document_revisions) { Files files; const auto a = files.path("early.cpp"); const auto b = files.path("other.hpp"); const auto early = path_to_file_uri(a); const auto other = path_to_file_uri(b); DocumentStore store; const auto first = get(store.open(early, "cpp", 1, "early")); const auto initial = get(store.file_overlay_inventory()); const auto old_a = get(observe(store, initial, a)); const auto second = get(store.open(other, "cpp", 1, "previous")); require(first->overlay_epoch != second->overlay_epoch); const auto census = get(store.file_overlay_inventory()); const auto kept_a = get(observe(store, census, a)); const auto kept_b = get(observe(store, census, b)); expect(kept_a.overlay() == first); expect(store.validate_bound_sources(census, std::array{kept_a, kept_b}).has_value()); expect_error(store.validate_bound_sources(census, std::array{old_a}), Kind::stale_read); const auto updated = get(replace(store, other, 2, "previous")); expect(store.validate_bound_sources(census, std::array{kept_a}).has_value()); expect_error(store.validate_bound_sources(census, std::array{kept_a, kept_b}), Kind::stale_read); expect(binding(census, other).content == second); const auto fresh_b = get(observe(store, census, b)); expect(fresh_b.overlay() == updated && !updated->same_input_as(*second)); expect(store.validate_bound_sources(census, std::array{kept_a, fresh_b}).has_value()); } VOID_TEST(BoundInputs, open_edit_close_rejects_late_bytes_for_an_earlier_absence) { Files files; const auto source = files.path("main.cpp"); const auto uri = path_to_file_uri(source); DocumentStore store; const auto initial = get(store.file_overlay_inventory()); const auto absent = get(observe(store, initial, source)); const auto before = get(store.materialize_bound_disk(absent, "same")); require(store.open(uri, "cpp", 1, "open").has_value()); expect_error(store.materialize_bound_disk(absent, "same"), Kind::stale_read); require(replace(store, uri, 2, "changed").has_value()); require(store.close(uri).has_value()); expect_error(store.materialize_bound_disk(absent, "same"), Kind::stale_read); const auto latest = get(store.file_overlay_inventory()); expect_error(store.validate_bound_sources(latest, std::array{absent}), Kind::stale_read); const auto now_absent = get(observe(store, latest, source)); const auto after = get(store.materialize_bound_disk(now_absent, "same")); expect(!after->same_input_as(*before)); } VOID_TEST(BoundInputs, desynchronization_recovery_and_reopen_recheck_retained_overlays) { Files files; const auto path = files.path("edit.cpp"); const auto uri = path_to_file_uri(path); DocumentStore store; const auto text = get(store.open(uri, "cpp", 7, "initial")); const auto census = get(store.file_overlay_inventory()); const auto original = get(observe(store, census, path)); require(store.mark_desynchronized(uri, std::nullopt, {Kind::malformed_change, "bad range"}) .has_value()); expect_error(store.validate_bound_sources(census, std::array{original}), Kind::desynchronized); expect_error(store.materialize_bound_disk(original, "disk"), Kind::desynchronized); expect_error(observe(store, census, path), Kind::desynchronized); const auto blocked = get(store.file_overlay_inventory()); expect_error(binding(blocked, uri).content, Kind::desynchronized); const auto recovered = get(replace(store, uri, 8, "initial")); const auto fresh = get(observe(store, blocked, path)); expect(fresh.overlay() == recovered && !recovered->same_input_as(*text)); expect_error(store.validate_bound_sources(census, std::array{original}), Kind::stale_read); require(store.close(uri).has_value()); const auto reopened = get(store.open(uri, "cpp", 7, "initial")); expect_error(store.validate_bound_sources(blocked, std::array{fresh}), Kind::stale_read); expect(original.overlay() == text && original.overlay()->text == reopened->text); expect(!original.overlay()->same_input_as(*reopened)); } VOID_TEST(BoundInputs, refused_file_is_inventoried_and_cannot_be_materialized_from_disk) { Files files; const auto a = files.path("kept.cpp"); const auto b = files.path("refused.cpp"); const auto kept = path_to_file_uri(a); const auto refused = path_to_file_uri(b); DocumentStore store{PositionEncoding::utf16, {.max_open_documents = 1, .max_unavailable_documents = 1}}; const auto snapshot = get(store.open(kept, "cpp", 1, "editor")); const auto before = get(store.file_overlay_inventory()); const auto absent = get(observe(store, before, b)); expect_error(store.open(refused, "cpp", 1, "refused bytes"), Kind::resource_limit); expect_error(store.materialize_bound_disk(absent, "disk"), Kind::stale_read); expect_error(store.file_overlay_inventory(1), Kind::resource_limit); const auto census = get(store.file_overlay_inventory(2)); require(census.entries().size() == 2); expect(binding(census, kept).content == snapshot); expect_error(binding(census, refused).content, Kind::unavailable_overlay); expect_error(observe(store, census, b), Kind::unavailable_overlay); require(store.mark_desynchronized(kept, 0, {Kind::invalid_range, "bad"}).has_value()); const auto at_quota = get(store.file_overlay_inventory()); expect_error(binding(at_quota, kept).content, Kind::desynchronized); expect_error(binding(at_quota, refused).content, Kind::unavailable_overlay); expect_error(replace(store, refused, 2, "still refused"), Kind::resource_limit); require(store.close(kept).has_value()); const auto room = get(store.file_overlay_inventory(1)); expect_error(binding(room, refused).content, Kind::unavailable_overlay); const auto restored = get(replace(store, refused, 3, "restored")); expect(get(observe(store, room, b)).overlay() == restored); expect(get(store.resolve_content(b)) == restored); } VOID_TEST(BoundInputs, aliases_keep_their_client_keys_and_block_ambiguous_source_bytes) { Files files; fs::create_directories(files.path("real/sub")); fs::create_directory_symlink(files.path("real/sub"), files.path("link")); const auto physical = files.path("real/x.cpp"); write(physical, "disk"); const auto indirect = path_to_file_uri(files.path("link/../x.cpp")); const auto direct = path_to_file_uri(physical); DocumentStore store; const auto first = get(store.open(indirect, "cpp", 1, "first")); const auto old = get(store.file_overlay_inventory()); const auto one = get(observe(store, old, physical)); expect(one.identity().canonical_uri == direct && one.overlay() == first); const auto indirect_view = get(store.observe_bound_source(old, indirect, physical)); expect(indirect_view.identity().canonical_uri == indirect && indirect.contains("/link/../")); const auto second = get(store.open(direct, "cpp", 1, "second")); const auto both = get(store.file_overlay_inventory()); require(both.entries().size() == 2); for (const auto &key : {direct, indirect}) { const auto &entry = binding(both, key); expect(entry.identity.canonical_path == physical); expect_error(entry.content, Kind::ambiguous_overlay); expect_error(store.observe_bound_source(both, key, physical), Kind::ambiguous_overlay); } expect_error(store.validate_bound_sources(both, std::array{one}), Kind::stale_read); expect(store.snapshot(indirect) == first && store.snapshot(direct) == second); require(store.close(direct).has_value()); expect(get(observe(store, get(store.file_overlay_inventory()), physical)).overlay() == first); } VOID_TEST(BoundInputs, retargeted_lexical_key_cannot_supply_a_different_physical_source) { Files files; const auto a = files.path("a.cpp"); const auto b = files.path("b.cpp"); const auto link = files.path("alias.cpp"); const auto uri = path_to_file_uri(link); write(a, "disk a"); write(b, "disk b"); fs::create_symlink(a, link); DocumentStore store; const auto original = get(store.open(uri, "cpp", 1, "owned a")); const auto census = get(store.file_overlay_inventory()); const auto selected = get(store.observe_bound_source(census, uri, a)); require(fs::remove(link)); fs::create_symlink(b, link); expect_error(store.observe_bound_source(census, uri, b), Kind::stale_read); expect(get(observe(store, census, a)).overlay() == original); expect(!get(observe(store, census, b)).overlay()); // The store checks the supplied binding. The source catalog checks the changed disk name. expect(get(store.observe_bound_source(census, uri, a)).overlay() == original); expect(store.validate_bound_sources(census, std::array{selected}).has_value()); require(store.close(uri).has_value()); const auto next = get(store.file_overlay_inventory()); expect(!get(store.observe_bound_source(next, uri, b)).overlay()); } VOID_TEST(BoundInputs, virtual_marker_overflow_preserves_file_authority) { Files files; const auto a = files.path("open.cpp"); const auto b = files.path("disk.cpp"); const auto uri = path_to_file_uri(a); DocumentStore store{PositionEncoding::utf16, {.max_open_documents = 1, .max_unavailable_documents = 0}}; const auto doc = get(store.open(uri, "cpp", 1, "overlay")); const auto census = get(store.file_overlay_inventory()); const auto active = get(observe(store, census, a)); const auto absent = get(observe(store, census, b)); expect_error(store.open("UnTiTled:Scratch", "cpp", 1, "virtual"), Kind::resource_limit); expect(store.validate_bound_sources(census, std::array{active, absent}).has_value()); const auto fresh = get(store.file_overlay_inventory(1)); expect(binding(fresh, uri).content == doc); expect(get(store.resolve_content(a)) == doc); expect(get(store.materialize_bound_disk(absent, "disk"))->text == "disk"); expect_error(store.close("untitled:Scratch"), Kind::not_open); require(store.close(uri).has_value()); expect(get(store.file_overlay_inventory(0)).entries().empty()); expect(!store.set_position_encoding(PositionEncoding::utf8)); const auto virtual_doc = get(store.open("untitled:Other", "cpp", 1, "virtual")); expect(store.snapshot("untitled:Other") == virtual_doc); require(store.close("untitled:Other").has_value()); } VOID_TEST(BoundInputs, unrecorded_file_or_unclassifiable_open_withholds_file_authority) { Files files; const auto a = files.path("open.cpp"); const auto b = files.path("future.hpp"); const auto uri = path_to_file_uri(a); for (const auto &[refused, reason] : std::array{std::pair{path_to_file_uri(b), Kind::resource_limit}, std::pair{std::string(kMaxDocumentUriBytes + 1, 'x'), Kind::resource_limit}, std::pair{std::string{"no-scheme"}, Kind::invalid_uri}}) { DocumentStore store{PositionEncoding::utf16, {.max_open_documents = 1, .max_unavailable_documents = 0}}; const auto doc = get(store.open(uri, "cpp", 1, "overlay")); const auto census = get(store.file_overlay_inventory()); const auto selected = get(observe(store, census, a)); expect_error(store.open(refused, "cpp", 1, ""), reason); expect_error(store.file_overlay_inventory(), Kind::unavailable_overlay); expect_error(store.validate_bound_sources(census, std::array{selected}), Kind::stale_read); expect(store.snapshot(uri) == doc); require(store.close(uri).has_value()); expect_error(store.file_overlay_inventory(), Kind::unavailable_overlay); expect_error(store.resolve_content(b), Kind::unavailable_overlay); } } VOID_TEST(BoundInputs, unbound_file_blocks_inventory_until_its_client_key_closes) { Files files; const auto bad = path_to_file_uri(files.path("missing/../hidden.cpp")); DocumentStore store; const auto before = get(store.file_overlay_inventory()); const auto open = get(store.open(bad, "cpp", 1, "unbound")); require(open->identity.is_file_uri && open->identity.canonical_path.empty()); expect_error(store.file_overlay_inventory(), Kind::unavailable_overlay); expect_error(store.validate_bound_sources(before), Kind::stale_read); require(store.close(bad).has_value()); const auto restored = get(store.file_overlay_inventory(0)); expect(!get(observe(store, restored, files.path("hidden.cpp"))).overlay()); } VOID_TEST(BoundInputs, retained_inputs_reject_another_store_at_the_same_address) { Files files; const auto file = files.path("open.cpp"); const auto disk = files.path("disk.cpp"); const auto uri = path_to_file_uri(file); std::optional store{std::in_place}; const auto *address = &*store; require(store->open(uri, "cpp", 1, "kept").has_value()); const auto old = get(store->file_overlay_inventory()); const auto active = get(observe(*store, old, file)); const auto absent = get(observe(*store, old, disk)); const auto snapshot = get(store->materialize_bound_disk(absent, "disk\nkept")); store.reset(); expect(active.overlay()->text == "kept" && binding(old, uri).content == active.overlay()); expect(snapshot->line_index.line_count() == 2 && snapshot->text == "disk\nkept"); store.emplace(); require(&*store == address); require(store->open(uri, "cpp", 1, "kept").has_value()); const auto fresh = get(store->file_overlay_inventory()); const auto new_active = get(observe(*store, fresh, file)); require(new_active.overlay()->overlay_epoch == active.overlay()->overlay_epoch); expect_error(store->validate_bound_sources(old), Kind::stale_read); expect_error(observe(*store, old, disk), Kind::stale_read); expect_error(store->materialize_bound_disk(absent, "disk"), Kind::stale_read); expect_error(store->validate_bound_sources(fresh, std::array{active}), Kind::stale_read); expect_error(store->validate_bound_sources(fresh, std::array{absent}), Kind::stale_read); expect(store->validate_bound_sources(fresh, std::array{new_active}).has_value()); } VOID_TEST(BoundInputs, disk_bytes_line_indexes_and_retained_snapshots_share_the_store_budget) { Files files; const auto path = files.path("large.cpp"); const auto uri = path_to_file_uri(path); DocumentStore store{PositionEncoding::utf8, {.max_document_bytes = 6500, .max_snapshot_bytes = 8192}}; { const auto empty = get(store.file_overlay_inventory()); const auto absent = get(observe(store, empty, path)); expect_error(store.materialize_bound_disk(absent, std::string(6501, 'x')), Kind::resource_limit); expect_error(store.materialize_bound_disk(absent, "\xF0\x9F"), Kind::invalid_utf8); expect_error(store.materialize_bound_disk(absent, std::string(2048, '\n')), Kind::resource_limit); std::string reserved = "tiny"; reserved.reserve(8192); expect_error(store.materialize_bound_disk(absent, std::move(reserved)), Kind::resource_limit); const auto kept = get(store.materialize_bound_disk(absent, std::string(6000, 'a'))); expect_error(store.materialize_bound_disk(absent, std::string(6000, 'b')), Kind::resource_limit); expect_error(store.open(uri, "cpp", 1, std::string(6000, 'c')), Kind::resource_limit); const auto refused = get(store.file_overlay_inventory()); expect_error(binding(refused, uri).content, Kind::unavailable_overlay); expect(kept->text.size() == 6000); } { require(replace(store, uri, 2, std::string(6000, 'c')).has_value()); const auto kept = get(store.file_overlay_inventory()); require(store.close(uri).has_value()); const auto empty = get(store.file_overlay_inventory()); const auto absent = get(observe(store, empty, path)); expect_error(store.materialize_bound_disk(absent, std::string(6000, 'a')), Kind::resource_limit); expect(binding(kept, uri).content.value()->text.size() == 6000); } const auto absent = get(observe(store, get(store.file_overlay_inventory()), path)); expect(get(store.materialize_bound_disk(absent, std::string(6000, 'd')))->text.size() == 6000); } VOID_TEST(BoundInputs, invalid_bound_names_and_encoding_changes_have_typed_outcomes) { Files files; const auto path = files.path("main.cpp"); const auto uri = path_to_file_uri(path); DocumentStore store; const auto empty = get(store.file_overlay_inventory(0)); for (const auto *bad : {"untitled:scratch", "broken", "file://remote/x.cpp", "file:///x%2Fy", "file:///dir/", "file:///dir/.", "file:///dir/.."}) { expect_error(store.observe_bound_source(empty, bad, path), Kind::invalid_uri); } for (const auto &bad : {fs::path{}, fs::path{"relative"}, files.path("x/../main.cpp"), files.path("dir/"), files.root / std::string{"a\0b", 3}, files.root / std::string{"bad\xFF"}}) { expect_error(store.observe_bound_source(empty, uri, bad), Kind::invalid_uri); } expect_error( store.observe_bound_source(empty, uri, files.root / std::string(kMaxDocumentUriBytes, 'x')), Kind::resource_limit); const auto absent = get(observe(store, empty, path)); require(store.set_position_encoding(PositionEncoding::utf8)); require(store.set_position_encoding(PositionEncoding::utf16)); expect_error(store.validate_bound_sources(empty), Kind::stale_read); expect_error(store.materialize_bound_disk(absent, ""), Kind::stale_read); }