|
| 1 | +import Foundation |
| 2 | +import XCTest |
| 3 | +import SuggestionModel |
| 4 | + |
| 5 | +@testable import Service |
| 6 | + |
| 7 | +class FilespaceSuggestionInvalidationTests: XCTestCase { |
| 8 | + @ServiceActor |
| 9 | + func prepare(suggestionText: String, cursorPosition: CursorPosition) async throws -> Filespace { |
| 10 | + let (_, filespace) = try await Workspace |
| 11 | + .fetchOrCreateWorkspaceIfNeeded(fileURL: URL(fileURLWithPath: "file/path/to.swift")) |
| 12 | + filespace.suggestions = [ |
| 13 | + .init( |
| 14 | + text: suggestionText, |
| 15 | + position: cursorPosition, |
| 16 | + uuid: "", |
| 17 | + range: .outOfScope, |
| 18 | + displayText: "" |
| 19 | + ) |
| 20 | + ] |
| 21 | + return filespace |
| 22 | + } |
| 23 | + |
| 24 | + func test_text_typing_suggestion_should_be_valid() async throws { |
| 25 | + let filespace = try await prepare( |
| 26 | + suggestionText: "hello man", |
| 27 | + cursorPosition: .init(line: 1, character: 0) |
| 28 | + ) |
| 29 | + let isValid = await filespace.validateSuggestions( |
| 30 | + lines: ["\n", "hell\n", "\n"], |
| 31 | + cursorPosition: .init(line: 1, character: 4) |
| 32 | + ) |
| 33 | + XCTAssertTrue(isValid) |
| 34 | + let suggestion = await filespace.presentingSuggestion |
| 35 | + XCTAssertNotNil(suggestion) |
| 36 | + } |
| 37 | + |
| 38 | + func test_text_typing_suggestion_in_the_middle_should_be_valid() async throws { |
| 39 | + let filespace = try await prepare( |
| 40 | + suggestionText: "hello man", |
| 41 | + cursorPosition: .init(line: 1, character: 0) |
| 42 | + ) |
| 43 | + let isValid = await filespace.validateSuggestions( |
| 44 | + lines: ["\n", "hell man\n", "\n"], |
| 45 | + cursorPosition: .init(line: 1, character: 4) |
| 46 | + ) |
| 47 | + XCTAssertTrue(isValid) |
| 48 | + let suggestion = await filespace.presentingSuggestion |
| 49 | + XCTAssertNotNil(suggestion) |
| 50 | + } |
| 51 | + |
| 52 | + func test_text_cursor_moved_to_another_line_should_invalidate() async throws { |
| 53 | + let filespace = try await prepare( |
| 54 | + suggestionText: "hello man", |
| 55 | + cursorPosition: .init(line: 1, character: 0) |
| 56 | + ) |
| 57 | + let isValid = await filespace.validateSuggestions( |
| 58 | + lines: ["\n", "hell\n", "\n"], |
| 59 | + cursorPosition: .init(line: 2, character: 0) |
| 60 | + ) |
| 61 | + XCTAssertFalse(isValid) |
| 62 | + let suggestion = await filespace.presentingSuggestion |
| 63 | + XCTAssertNil(suggestion) |
| 64 | + } |
| 65 | + |
| 66 | + func test_text_cursor_is_invalid_should_invalidate() async throws { |
| 67 | + let filespace = try await prepare( |
| 68 | + suggestionText: "hello man", |
| 69 | + cursorPosition: .init(line: 100, character: 0) |
| 70 | + ) |
| 71 | + let isValid = await filespace.validateSuggestions( |
| 72 | + lines: ["\n", "hell\n", "\n"], |
| 73 | + cursorPosition: .init(line: 100, character: 4) |
| 74 | + ) |
| 75 | + XCTAssertFalse(isValid) |
| 76 | + let suggestion = await filespace.presentingSuggestion |
| 77 | + XCTAssertNil(suggestion) |
| 78 | + } |
| 79 | + |
| 80 | + func test_line_content_does_not_match_input_should_invalidate() async throws { |
| 81 | + let filespace = try await prepare( |
| 82 | + suggestionText: "hello man", |
| 83 | + cursorPosition: .init(line: 1, character: 0) |
| 84 | + ) |
| 85 | + let isValid = await filespace.validateSuggestions( |
| 86 | + lines: ["\n", "helo\n", "\n"], |
| 87 | + cursorPosition: .init(line: 1, character: 4) |
| 88 | + ) |
| 89 | + XCTAssertFalse(isValid) |
| 90 | + let suggestion = await filespace.presentingSuggestion |
| 91 | + XCTAssertNil(suggestion) |
| 92 | + } |
| 93 | + |
| 94 | + func test_finish_typing_the_whole_single_line_suggestion_should_invalidate() async throws { |
| 95 | + let filespace = try await prepare( |
| 96 | + suggestionText: "hello man", |
| 97 | + cursorPosition: .init(line: 1, character: 0) |
| 98 | + ) |
| 99 | + let isValid = await filespace.validateSuggestions( |
| 100 | + lines: ["\n", "hello man\n", "\n"], |
| 101 | + cursorPosition: .init(line: 1, character: 9) |
| 102 | + ) |
| 103 | + XCTAssertFalse(isValid) |
| 104 | + let suggestion = await filespace.presentingSuggestion |
| 105 | + XCTAssertNil(suggestion) |
| 106 | + } |
| 107 | + |
| 108 | + func test_finish_typing_the_whole_multiple_line_suggestion_should_be_valid() async throws { |
| 109 | + let filespace = try await prepare( |
| 110 | + suggestionText: "hello man\nhow are you?", |
| 111 | + cursorPosition: .init(line: 1, character: 0) |
| 112 | + ) |
| 113 | + let isValid = await filespace.validateSuggestions( |
| 114 | + lines: ["\n", "hello man\n", "\n"], |
| 115 | + cursorPosition: .init(line: 1, character: 9) |
| 116 | + ) |
| 117 | + XCTAssertTrue(isValid) |
| 118 | + let suggestion = await filespace.presentingSuggestion |
| 119 | + XCTAssertNotNil(suggestion) |
| 120 | + } |
| 121 | + |
| 122 | + func test_undo_text_to_a_state_before_the_suggestion_was_generated_should_invalidate() async throws { |
| 123 | + let filespace = try await prepare( |
| 124 | + suggestionText: "hello man", |
| 125 | + cursorPosition: .init(line: 1, character: 5) // generating man from hello |
| 126 | + ) |
| 127 | + let isValid = await filespace.validateSuggestions( |
| 128 | + lines: ["\n", "hell\n", "\n"], |
| 129 | + cursorPosition: .init(line: 1, character: 4) |
| 130 | + ) |
| 131 | + XCTAssertFalse(isValid) |
| 132 | + let suggestion = await filespace.presentingSuggestion |
| 133 | + XCTAssertNil(suggestion) |
| 134 | + } |
| 135 | +} |
| 136 | + |
0 commit comments