#include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef _WIN32 #include #include #endif namespace { using namespace Void::LanguageServer::Documents; using Void::Testing::expect; using Void::Testing::expect_eq; using Void::Testing::require; using ErrorKind = DocumentError::Kind; constexpr auto utf8 = PositionEncoding::utf8; constexpr auto utf16 = PositionEncoding::utf16; constexpr std::string_view virtual_uri = "untitled:document.cpp"; template void expect_error(const std::expected &result, ErrorKind kind, std::source_location location = std::source_location::current()) { require(!result.has_value(), location); expect_eq(result.error().kind, kind, location); } template T get(std::expected result, std::source_location location = std::source_location::current()) { require(result.has_value(), location); return std::move(*result); } TextEdit full(std::string text) { return TextEdit{std::nullopt, std::move(text)}; } TextEdit edit(Position start, Position end, std::string text) { return TextEdit{Range{start, end}, std::move(text)}; } #ifndef _WIN32 class TestDirectory { public: TestDirectory() { auto pattern = (std::filesystem::temp_directory_path() / "vls-documents-XXXXXX").string(); const auto *created = ::mkdtemp(pattern.data()); require(created != nullptr); path_ = created; } TestDirectory(const TestDirectory &) = delete; TestDirectory &operator=(const TestDirectory &) = delete; ~TestDirectory() { std::error_code error; std::filesystem::remove_all(path_, error); } std::filesystem::path path(std::string_view name) const { return path_ / name; } private: std::filesystem::path path_; }; void write_file(const std::filesystem::path &path, std::string_view text) { std::ofstream file{path, std::ios::binary}; file.write(text.data(), static_cast(text.size())); file.close(); require(static_cast(file)); } #endif bool matches_fresh_index(const DocumentSnapshot &snapshot) { const LineIndex fresh{snapshot.text}; if (fresh.line_count() != snapshot.line_index.line_count()) { return false; } for (std::uint32_t line = 0; line < fresh.line_count(); ++line) { if (fresh.line_start_offset(line) != snapshot.line_index.line_start_offset(line)) { return false; } } for (std::size_t offset = 0; offset <= snapshot.text.size(); ++offset) { if (fresh.offset_to_position(snapshot.text, offset, snapshot.encoding) != snapshot.offset_to_position(offset)) { return false; } } return true; } } // namespace VOID_TEST(Documents, positions_round_trip_and_clamp_in_both_encodings) { constexpr std::string_view text = "a\u0301\U0001F600\r\n\u03B2\rz\n"; const LineIndex index{text}; struct Boundary { std::size_t byte; Position utf8_position; Position utf16_position; }; constexpr std::array boundaries{ Boundary{0, {0, 0}, {0, 0}}, Boundary{1, {0, 1}, {0, 1}}, Boundary{3, {0, 3}, {0, 2}}, Boundary{7, {0, 7}, {0, 4}}, Boundary{9, {1, 0}, {1, 0}}, Boundary{11, {1, 2}, {1, 1}}, Boundary{12, {2, 0}, {2, 0}}, Boundary{13, {2, 1}, {2, 1}}, Boundary{14, {3, 0}, {3, 0}}, }; for (const auto &point : boundaries) { expect(index.position_to_offset(text, point.utf8_position, utf8) == point.byte); expect(index.position_to_offset(text, point.utf16_position, utf16) == point.byte); expect(index.offset_to_position(text, point.byte, utf8) == point.utf8_position); expect(index.offset_to_position(text, point.byte, utf16) == point.utf16_position); } for (const auto encoding : {utf8, utf16}) { expect(index.position_to_offset(text, {0, kMaxPositionValue}, encoding) == 7); expect(index.position_to_offset(text, {1, 800}, encoding) == 11); expect(index.position_to_offset(text, {3, 800}, encoding) == text.size()); expect(!index.position_to_offset(text, {4, 0}, encoding)); expect(!index.position_to_offset(text, {0, kMaxPositionValue + 1}, encoding)); expect(!index.offset_to_position(text, text.size() + 1, encoding)); expect(!index.offset_to_position(text, 8, encoding)); expect(index.range_to_offsets(text, {{0, 99}, {1, 0}}, encoding) == std::pair{7, 9}); expect(!index.range_to_offsets(text, {{1, 0}, {0, 99}}, encoding)); for (const auto split : {2U, 4U, 5U, 6U, 10U}) { expect(!index.offset_to_position(text, split, encoding)); } } expect(!index.position_to_offset(text, {0, 2}, utf8)); expect(!index.position_to_offset(text, {0, 5}, utf8)); expect(!index.position_to_offset(text, {0, 3}, utf16)); expect(!index.line_start_offset(4)); expect(!index.line_content_end_offset(text, 4)); expect(!index.line_content_end_offset("different", 0)); } VOID_TEST(Documents, empty_text_and_truncated_utf8_have_safe_boundaries) { const LineIndex empty{""}; expect_eq(empty.line_count(), std::size_t{1}); expect(empty.position_to_offset("", {0, kMaxPositionValue}, utf16) == 0); expect(empty.offset_to_position("", 0, utf8) == Position{0, 0}); expect(!empty.offset_to_position("", 1, utf8)); const std::string truncated{"\xF0\x9F"}; const LineIndex broken{truncated}; expect(!broken.position_to_offset(truncated, {0, 900}, utf8)); expect(!broken.position_to_offset(truncated, {0, 900}, utf16)); expect(!broken.offset_to_position(truncated, truncated.size(), utf16)); DocumentStore store; expect_error(store.open(virtual_uri, "cpp", 1, truncated), ErrorKind::invalid_utf8); expect(!store.snapshot(virtual_uri)); auto restored = get(store.apply_changes(virtual_uri, 2, std::array{full("")})); expect(restored->position_to_offset({0, kMaxPositionValue}) == 0); expect(restored->version == 2); } VOID_TEST(Documents, virtual_and_file_uri_spellings_are_validated) { const auto item = normalize_uri("VoId-GeNeRaTeD://host/unit%2Fpart#range"); require(item.has_value()); expect_eq(item->canonical_uri, std::string{"void-generated://host/unit%2Fpart#range"}); expect_eq(item->display_uri, std::string{"VoId-GeNeRaTeD://host/unit%2Fpart#range"}); expect(!item->is_file_uri && item->canonical_path.empty()); expect(client_uri_key("void-generated://host/unit/part#range") != item->canonical_uri); for (const auto invalid : {"no-scheme", "3untitled:x", "untitled:", "untitled:a b", "untitled:x%00y", "untitled:x%ZZ", "file:relative", "file://remote/path", "file:////unc/path", "file:///x%2Fy", "file:///x%5cy", "file:///x%00y", "file:///x%FF", "file:///trailing%", "file:///x?query", "file:///x#fragment", "file:///x\\y"}) { expect(!normalize_uri(invalid)); } expect(!normalize_uri(std::string_view{"untitled:a\0b", 12})); expect(!identity_from_path(std::filesystem::path{std::string{"/a\0b", 4}})); DocumentStore store; auto snapshot = get(store.open("UNTITLED:Scratch", "cpp", -3, "virtual")); expect_eq(snapshot->identity.display_uri, std::string{"UNTITLED:Scratch"}); expect(store.snapshot("untitled:Scratch") == snapshot); expect(!store.snapshot("untitled:scratch")); require(store.close("untitled:Scratch").has_value()); } VOID_TEST(Documents, ordered_edits_preserve_prior_snapshot_text_and_positions) { DocumentStore store; auto original = get(store.open(virtual_uri, "cpp", 5, "a\r\nb\n")); const std::array edits{ edit({0, 1}, {0, 1}, "\u0301\U0001F600"), edit({0, 4}, {1, 0}, "\r"), edit({1, 0}, {1, 0}, "\n"), edit({1, 400}, {1, 400}, "!"), }; auto changed = get(store.apply_changes(virtual_uri, 6, edits)); expect_eq(changed->text, std::string{"a\u0301\U0001F600\r\nb!\n"}); expect(changed->version == 6 && changed->revision_id > original->revision_id); expect(changed->origin == DocumentOrigin::overlay && !changed->same_input_as(*original)); expect(matches_fresh_index(*changed)); expect(store.snapshot(virtual_uri) == changed); expect_eq(original->text, std::string{"a\r\nb\n"}); expect(original->version == 5 && original->position_to_offset({1, 0}) == 3); require(store.close(virtual_uri).has_value()); expect(!store.snapshot(virtual_uri)); expect_eq(changed->text, std::string{"a\u0301\U0001F600\r\nb!\n"}); } VOID_TEST(Documents, edited_line_index_matches_rebuild_across_crlf_splices) { DocumentStore store{utf8}; constexpr std::array inputs{ "", "x", "\r", "\n", "\r\n", "\r\r\n", "x\r\ny\nz\rt", "x\r\U0001F600\n", "\rxx\n"}; constexpr std::array replacements{"", "y", "\r", "\n", "\r\n", "\n\r", "\rhello\n", "\U0001F600"}; for (const std::string_view input : inputs) { const LineIndex base{input}; for (std::size_t begin = 0; begin <= input.size(); ++begin) { const auto start = base.offset_to_position(input, begin, utf8); if (!start) { continue; } for (std::size_t end = begin; end <= input.size(); ++end) { const auto stop = base.offset_to_position(input, end, utf8); if (!stop) { continue; } for (const std::string_view replacement : replacements) { require(store.open(virtual_uri, "cpp", 0, std::string{input}).has_value()); const std::array edits{edit(*start, *stop, std::string{replacement})}; const auto changed = get(store.apply_changes(virtual_uri, 1, edits)); auto expected_text = std::string{input}; expected_text.replace(begin, end - begin, replacement); expect_eq(changed->text, expected_text); expect(matches_fresh_index(*changed)); require(store.close(virtual_uri).has_value()); } } } } } VOID_TEST(Documents, failed_batch_has_no_partial_publication_and_requires_a_newer_replacement) { DocumentStore store; auto original = get(store.open(virtual_uri, "cpp", 8, "base\n")); const std::array invalid{full("unpublished"), edit({3, 0}, {3, 0}, "!")}; expect_error(store.apply_changes(virtual_uri, 12, invalid), ErrorKind::invalid_range); expect(!store.snapshot(virtual_uri) && store.list_overlays().empty()); expect_eq(original->text, std::string{"base\n"}); expect(original->version == 8 && original->position_to_offset({1, 0}) == 5); expect_error(store.apply_changes(virtual_uri, 10, std::array{full("stale")}), ErrorKind::nonmonotonic_version); expect(!store.snapshot(virtual_uri)); expect_error(store.open(virtual_uri, "cpp", 13, "duplicate"), ErrorKind::already_open); const std::array recovery{full("a\U0001F600"), edit({0, 3}, {0, 3}, "!")}; auto restored = get(store.apply_changes(virtual_uri, 13, recovery)); expect(restored->version == 13); expect_eq(restored->text, std::string{"a\U0001F600!"}); expect(restored->revision_id > original->revision_id); require(store.close(virtual_uri).has_value()); auto reopened = get(store.open(virtual_uri, "cpp", -20, "base\n")); expect(reopened->version == -20 && reopened->revision_id > restored->revision_id); expect(!reopened->same_input_as(*original)); expect(reopened->content_hash == original->content_hash); } VOID_TEST(Documents, malformed_and_stale_notifications_discard_readiness) { DocumentStore store; const auto min_version = std::numeric_limits::min(); auto original = get(store.open(virtual_uri, "cpp", min_version, "initial")); expect(original->version == min_version); require(store .mark_desynchronized(virtual_uri, std::nullopt, {ErrorKind::malformed_change, "missing version"}) .has_value()); expect(!store.snapshot(virtual_uri)); require(store.apply_changes(virtual_uri, 8, std::array{full("current")}).has_value()); require(store .mark_desynchronized("UNTITLED:document.cpp", 6, {ErrorKind::malformed_change, "malformed range"}) .has_value()); expect(!store.snapshot(virtual_uri)); expect_error(store.apply_changes(virtual_uri, 7, std::array{full("late")}), ErrorKind::nonmonotonic_version); require( store.mark_desynchronized(virtual_uri, 12, {ErrorKind::malformed_change, "invalid event"}) .has_value()); expect_error(store.apply_changes(virtual_uri, 10, std::array{full("late")}), ErrorKind::nonmonotonic_version); require(store.apply_changes(virtual_uri, 13, std::array{full("ready")}).has_value()); constexpr auto invalid_version = std::int64_t{std::numeric_limits::max()} + 1; expect_error(store.apply_changes(virtual_uri, invalid_version, std::array{full("invalid")}), ErrorKind::invalid_version); expect(!store.snapshot(virtual_uri)); require(store.apply_changes(virtual_uri, 14, std::array{full("ready again")}).has_value()); expect_error(store.open("untitled:bad-version", "cpp", invalid_version, ""), ErrorKind::invalid_version); expect_error( store.mark_desynchronized("untitled:unknown", 15, {ErrorKind::malformed_change, "unknown"}), ErrorKind::not_open); } VOID_TEST(Documents, change_count_aggregate_text_and_document_size_are_bounded) { DocumentStore store{utf16, {.max_document_bytes = 8, .max_changes = 2, .max_change_bytes = 11}}; expect_error(store.open(virtual_uri, "cpp", 1, "123456789"), ErrorKind::resource_limit); expect(!store.snapshot(virtual_uri)); auto original = get(store.apply_changes(virtual_uri, 2, std::array{full("12345678")})); const std::array too_many{full("a"), full("b"), full("c")}; expect_error(store.apply_changes(virtual_uri, 3, too_many), ErrorKind::resource_limit); const std::array too_many_bytes{full("12345678"), full("1234")}; expect_error(store.apply_changes(virtual_uri, 4, too_many_bytes), ErrorKind::resource_limit); const std::array too_large{full("12345678"), edit({0, 8}, {0, 8}, "9")}; expect_error(store.apply_changes(virtual_uri, 5, too_large), ErrorKind::resource_limit); expect(!store.snapshot(virtual_uri)); expect_eq(original->text, std::string{"12345678"}); require(store.apply_changes(virtual_uri, 6, std::array{full("recovery")}).has_value()); } #ifndef _WIN32 VOID_TEST(Documents, prospective_names_preserve_the_actual_parent_traversal) { namespace fs = std::filesystem; TestDirectory temp; write_file(temp.path("x.cpp"), "int actual;\n"); fs::create_symlink("absent", temp.path("dangling")); fs::create_directories(temp.path("real/sub")); fs::create_directory_symlink("real/sub", temp.path("link")); for (const auto *name : {"absent/../x.cpp", "dangling/../x.cpp", "x.cpp/../x.cpp", "dangling"}) { const auto uri = path_to_file_uri(temp.path(name)); auto identity = normalize_uri(uri); require(identity.has_value()); expect(identity->is_file_uri && identity->canonical_path.empty()); expect(client_file_path(uri) == temp.path(name)); } const auto name = temp.path("link/../new/child.cpp"); auto future = identity_from_path(name); require(future.has_value()); expect(future->canonical_path == temp.path("real/new/child.cpp")); } VOID_TEST(Documents, file_aliases_preserve_client_binding_and_reject_ambiguous_lookup) { TestDirectory temp; const auto a = temp.path("first file#.cpp"); const auto b = temp.path("other.cpp"); const auto alias = temp.path("link.cpp"); write_file(a, "first disk"); write_file(b, "other disk"); std::filesystem::create_symlink(a, alias); const auto a_uri = path_to_file_uri(a); const auto a_alt = "FILE://LOCALHOST" + a_uri.substr(7); const auto alias_uri = path_to_file_uri(alias); const auto a_identity = normalize_uri(a_alt); const auto alias_identity = normalize_uri(alias_uri); require(a_identity.has_value() && alias_identity.has_value()); expect_eq(a_identity->canonical_uri, a_uri); expect(a_identity->canonical_uri.contains("first%20file%23.cpp")); expect(a_identity->canonical_path == alias_identity->canonical_path); expect(a_identity->canonical_uri != alias_identity->canonical_uri); expect(client_uri_key("file:" + a_uri.substr(7)) == a_uri); DocumentStore store; auto first = get(store.open(a_alt, "cpp", 1, "first overlay")); expect_eq(first->identity.display_uri, a_alt); expect(store.snapshot(a_uri) == first); expect(get(store.resolve_content(alias, 1)) == first); expect_error(store.open(a_uri, "cpp", 2, "same lexical document"), ErrorKind::already_open); auto second = get(store.open(alias_uri, "cpp", 1, "second overlay")); expect(store.snapshot(alias_uri) == second); expect_error(store.resolve_content(a), ErrorKind::ambiguous_overlay); expect(!store.snapshot_by_path(a)); require(std::filesystem::remove(alias)); std::filesystem::create_symlink(b, alias); expect(store.snapshot(alias_uri) == second); expect_error(store.resolve_content(alias), ErrorKind::stale_read); expect_eq(get(store.resolve_content(b))->text, std::string{"other disk"}); auto updated = get(store.apply_changes(alias_uri, 2, std::array{full("bound to original")})); expect(updated->identity.canonical_path == first->identity.canonical_path); require(store.close(alias_uri).has_value()); expect(get(store.resolve_content(a)) == first); expect_eq(get(store.resolve_content(alias))->text, std::string{"other disk"}); } VOID_TEST(Documents, dot_segments_are_resolved_after_symlinks) { TestDirectory temp; const auto a = temp.path("a"); const auto b = temp.path("b"); std::filesystem::create_directory(a); std::filesystem::create_directories(b / "inner"); write_file(a / "test.cpp", "a"); write_file(b / "test.cpp", "b"); std::filesystem::create_symlink(b / "inner", a / "link"); const auto indirect = a / "link" / ".." / "test.cpp"; const auto indirect_uri = path_to_file_uri(indirect); const auto direct_uri = path_to_file_uri(a / "test.cpp"); DocumentStore store; auto opened = get(store.open(indirect_uri, "cpp", 1, "open b")); expect(opened->identity.canonical_uri.contains("/link/../")); expect(get(store.resolve_content(b / "test.cpp")) == opened); expect_eq(get(store.resolve_content(a / "test.cpp"))->text, std::string{"a"}); expect(!store.snapshot(direct_uri)); } VOID_TEST(Documents, disk_reads_are_typed_bounded_and_have_stable_input_identity) { TestDirectory temp; const auto source = temp.path("disk.cpp"); const auto missing = temp.path("missing.cpp"); const auto invalid = temp.path("invalid.cpp"); const auto fifo = temp.path("named-pipe"); write_file(source, "disk"); write_file(invalid, "\xF0\x9F"); require(::mkfifo(fifo.c_str(), 0600) == 0); DocumentStore store{utf16, {.max_document_bytes = 4}}; expect_error(store.resolve_content(missing), ErrorKind::disk_read_failed); expect_error(store.resolve_content(temp.path("")), ErrorKind::disk_read_failed); expect_error(store.resolve_content(fifo), ErrorKind::disk_read_failed); expect_error(store.resolve_content(invalid), ErrorKind::invalid_utf8); expect_error(store.resolve_content(source, 3), ErrorKind::resource_limit); auto first = get(store.resolve_content(source)); auto second = get(store.resolve_content(source)); expect(first->origin == DocumentOrigin::disk && !first->version); expect(second->revision_id != first->revision_id && second->same_input_as(*first)); expect(first->same_input_as(*second)); const auto alias = temp.path("disk-link.cpp"); std::filesystem::create_symlink(source, alias); expect(get(store.resolve_content(alias))->same_input_as(*first)); write_file(source, "edit"); expect(!get(store.resolve_content(source))->same_input_as(*first)); write_file(source, "disks"); expect_error(store.resolve_content(source, 1000), ErrorKind::resource_limit); write_file(source, "disk"); const auto uri = path_to_file_uri(source); auto overlay = get(store.open(uri, "cpp", 1, "disk")); expect(!overlay->same_input_as(*first)); require(store.close(uri).has_value()); auto after_close = get(store.resolve_content(source)); expect(!after_close->same_input_as(*first) && after_close->overlay_epoch > first->overlay_epoch); } VOID_TEST(Documents, snapshot_budget_counts_retained_references_and_saves_desync_state) { TestDirectory temp; const auto first = temp.path("first.cpp"); const auto second = temp.path("second.cpp"); write_file(first, "first disk"); write_file(second, "second disk"); const auto first_uri = path_to_file_uri(first); const auto second_uri = path_to_file_uri(second); DocumentStore store{utf16, {.max_snapshot_bytes = 8 * 1024}}; const std::string large{std::string(6 * 1024, 'x')}; auto kept = get(store.open(first_uri, "cpp", 1, large)); require(store.mark_desynchronized(first_uri, 12, {ErrorKind::malformed_change, "bad late edit"}) .has_value()); expect_error(store.resolve_content(first), ErrorKind::desynchronized); expect_error(store.apply_changes(first_uri, 10, std::array{full("stale")}), ErrorKind::nonmonotonic_version); require(store.close(first_uri).has_value()); expect_error(store.open(second_uri, "cpp", 1, large), ErrorKind::resource_limit); expect_error(store.resolve_content(second), ErrorKind::unavailable_overlay); expect_eq(kept->text, large); expect(kept->position_to_offset({0, 9000}) == large.size()); kept.reset(); auto restored = get(store.apply_changes(second_uri, 2, std::array{full(large)})); expect(store.snapshot_by_path(second) == restored); expect(restored->version == 2); require(store.close(second_uri).has_value()); restored.reset(); expect_error(store.open(first_uri, "cpp", 1, std::string(2048, '\n')), ErrorKind::resource_limit); expect_error(store.resolve_content(first), ErrorKind::unavailable_overlay); } VOID_TEST(Documents, refused_open_owns_its_content_and_marker_overflow_fails_closed) { TestDirectory temp; const auto a = temp.path("a.cpp"); const auto b = temp.path("b.cpp"); const auto c = temp.path("c.cpp"); write_file(a, "disk a"); write_file(b, "disk b"); write_file(c, "disk c"); const auto a_uri = path_to_file_uri(a); const auto b_uri = path_to_file_uri(b); const auto c_uri = path_to_file_uri(c); DocumentStore store{utf16, {.max_open_documents = 1, .max_unavailable_documents = 1}}; auto a_snapshot = get(store.open(a_uri, "cpp", 1, "overlay a")); expect_error(store.open(b_uri, "cpp", 1, "overlay b"), ErrorKind::resource_limit); expect_error(store.resolve_content(b), ErrorKind::unavailable_overlay); expect_error(store.open(c_uri, "cpp", 1, "overlay c"), ErrorKind::resource_limit); expect_error(store.resolve_content(c), ErrorKind::unavailable_overlay); expect(store.snapshot(a_uri) == a_snapshot); require(store.close(b_uri).has_value()); require(store.close(a_uri).has_value()); expect_error(store.resolve_content(c), ErrorKind::unavailable_overlay); expect_error(store.resolve_content(a), ErrorKind::unavailable_overlay); expect_eq(get(DocumentStore{}.resolve_content(c))->text, std::string{"disk c"}); } VOID_TEST(Documents, late_open_wins_over_successful_and_failed_disk_reads) { TestDirectory temp; const auto existing = temp.path("existing.cpp"); const auto missing = temp.path("missing.cpp"); const auto invalid = temp.path("invalid.cpp"); write_file(existing, "disk input"); write_file(invalid, "\xF0\x9F"); for (const auto &path : {existing, missing, invalid}) { const auto uri = path_to_file_uri(path); unsigned callbacks = 0; DocumentStore store{utf16, {}, [&] { ++callbacks; require(store.open(uri, "cpp", 1, "new overlay").has_value()); }}; auto result = get(store.resolve_content(path)); expect_eq(callbacks, 1U); expect_eq(result->text, std::string{"new overlay"}); expect(result == store.snapshot(uri)); } } VOID_TEST(Documents, late_unavailable_and_desynchronized_opens_preempt_disk) { TestDirectory temp; const auto path = temp.path("real.cpp"); write_file(path, "old text"); const auto uri = path_to_file_uri(path); DocumentStore refused{utf16, {.max_open_documents = 0}, [&] { expect_error(refused.open(uri, "cpp", 1, "overlay"), ErrorKind::resource_limit); }}; expect_error(refused.resolve_content(path), ErrorKind::unavailable_overlay); DocumentStore broken{ utf16, {}, [&] { require(broken.open(uri, "cpp", 1, "overlay").has_value()); require(broken.mark_desynchronized(uri, 2, {ErrorKind::malformed_change, "bad field"}) .has_value()); }}; expect_error(broken.resolve_content(path), ErrorKind::desynchronized); } VOID_TEST(Documents, open_close_aba_rejects_old_success_and_error) { TestDirectory temp; const auto existing = temp.path("exists.cpp"); const auto missing = temp.path("missing.cpp"); write_file(existing, "disk text"); for (const auto &path : {existing, missing}) { const auto uri = path_to_file_uri(path); unsigned callbacks = 0; DocumentStore store{utf16, {}, [&] { ++callbacks; require(store.open(uri, "cpp", 1, "transient").has_value()); require(store.close(uri).has_value()); }}; expect_error(store.resolve_content(path), ErrorKind::stale_read); expect_eq(callbacks, 1U); expect(!store.snapshot(uri)); } } VOID_TEST(Documents, mid_read_modification_and_late_replacement_are_stale) { namespace Files = Void::LanguageServer::Detail; TestDirectory temp; const auto file = temp.path("edited.cpp"); const std::string original(160 * 1024, 'a'); const std::string changed(original.size(), 'b'); write_file(file, original); bool replaced = false; const auto read = Files::read_regular(file, original.size(), false, [&](auto chunk) { if (!replaced) { require(chunk.size() < original.size()); write_file(file, changed); // Force a distinct timestamp even on a filesystem with coarse resolution. std::filesystem::last_write_time(file, std::filesystem::last_write_time(file) - std::chrono::seconds{5}); replaced = true; } return true; }); require(replaced); expect(!read && read.error() == Files::FileError::changed); DocumentStore store{utf16, {}, [&] { const auto next = temp.path("replacement.cpp"); write_file(next, original); std::filesystem::rename(next, file); }}; expect_error(store.resolve_content(file), ErrorKind::stale_read); expect(get(DocumentStore{}.resolve_content(file))->text == original); } VOID_TEST(Documents, disk_reads_revalidate_path_and_encoding_before_return) { TestDirectory temp; const auto before = temp.path("before.cpp"); const auto after = temp.path("after.cpp"); const auto link = temp.path("link.cpp"); write_file(before, "before"); write_file(after, "after"); std::filesystem::create_symlink(before, link); DocumentStore moved{utf16, {}, [&] { require(std::filesystem::remove(link)); std::filesystem::create_symlink(after, link); }}; expect_error(moved.resolve_content(link), ErrorKind::stale_read); unsigned callbacks = 0; DocumentStore negotiation{utf16, {}, [&] { if (++callbacks == 1) { require(negotiation.set_position_encoding(utf8)); } }}; expect_error(negotiation.resolve_content(after), ErrorKind::stale_read); expect(get(negotiation.resolve_content(after))->encoding == utf8); auto open = get(negotiation.open(path_to_file_uri(after), "cpp", 1, "text")); expect(!negotiation.set_position_encoding(utf16)); expect(negotiation.position_encoding() == utf8 && open->encoding == utf8); } #endif