Skip to content

Commit 3632ead

Browse files
committed
Add FilespaceSuggestionInvalidationTests
1 parent 9e55656 commit 3632ead

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

Core/Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ let package = Package(
106106
"SuggestionInjector",
107107
"XPCShared",
108108
"Environment",
109+
"SuggestionModel",
109110
.product(name: "Preferences", package: "Tool"),
110111
]
111112
),

Core/Tests/ServiceTests/Environment.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import XPCShared
2323
}
2424

2525
Environment.triggerAction = { _ in }
26+
27+
Environment.guessProjectRootURLForFile = { $0 }
2628
}
2729

2830
func completion(text: String, range: CursorRange, uuid: String = "") -> CodeSuggestion {
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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

Comments
 (0)