|
| 1 | +import CopilotModel |
| 2 | +import CopilotService |
| 3 | +import XCTest |
| 4 | +import XPCShared |
| 5 | + |
| 6 | +@testable import Service |
| 7 | +@testable import SuggestionInjector |
| 8 | + |
| 9 | +final class CommentBase_RealtimeSuggestionsTests: XCTestCase { |
| 10 | + let mock = MockSuggestionService(completions: []) |
| 11 | + |
| 12 | + override func setUp() async throws { |
| 13 | + await clearEnvironment() |
| 14 | + Environment.createSuggestionService = { [unowned self] _ in self.mock } |
| 15 | + Environment.triggerAction = { _ in fatalError("unimplemented") } |
| 16 | + } |
| 17 | + |
| 18 | + func test_after_prefetch_suggestions_completes_it_should_trigger_command_real_time_suggestions___if_editor_content_is_unchanged_present_the_suggestions( |
| 19 | + ) async throws { |
| 20 | + let service = CommentBaseCommandHandler() |
| 21 | + mock.completions = [ |
| 22 | + completion( |
| 23 | + text: """ |
| 24 | + var name: String |
| 25 | + var age: String |
| 26 | + """, |
| 27 | + range: .init( |
| 28 | + start: .init(line: 1, character: 0), |
| 29 | + end: .init(line: 2, character: 18) |
| 30 | + ) |
| 31 | + ), |
| 32 | + ] |
| 33 | + |
| 34 | + let lines = [ |
| 35 | + "struct Cat {\n", |
| 36 | + "\n", |
| 37 | + "}\n", |
| 38 | + ] |
| 39 | + |
| 40 | + let editor = EditorContent( |
| 41 | + content: lines.joined(), |
| 42 | + lines: lines, |
| 43 | + uti: "", |
| 44 | + cursorPosition: .init(line: 0, character: 17), |
| 45 | + tabSize: 1, |
| 46 | + indentSize: 1, |
| 47 | + usesTabsForIndentation: false |
| 48 | + ) |
| 49 | + |
| 50 | + var triggeredCommand = "" |
| 51 | + Environment.triggerAction = { triggeredCommand = $0 } |
| 52 | + |
| 53 | + _ = try await service.generateRealtimeSuggestions(editor: editor) |
| 54 | + |
| 55 | + XCTAssertEqual(triggeredCommand, "Real-time Suggestions") |
| 56 | + |
| 57 | + let result = try await service.presentRealtimeSuggestions(editor: editor)! |
| 58 | + |
| 59 | + let resultLines = lines.applying(result.modifications) |
| 60 | + |
| 61 | + XCTAssertEqual(resultLines.joined(), result.content) |
| 62 | + XCTAssertEqual(result.content, """ |
| 63 | + struct Cat { |
| 64 | +
|
| 65 | + /*========== Copilot Suggestion 1/1 |
| 66 | + var name: String |
| 67 | + var age: String |
| 68 | + *///======== End of Copilot Suggestion |
| 69 | + } |
| 70 | +
|
| 71 | + """) |
| 72 | + |
| 73 | + XCTAssertEqual(result.newCursor, .init(line: 0, character: 17)) |
| 74 | + } |
| 75 | + |
| 76 | + func test_if_content_is_changed_no_suggestions_will_be_presented() async throws { |
| 77 | + let service = CommentBaseCommandHandler() |
| 78 | + mock.completions = [ |
| 79 | + completion( |
| 80 | + text: """ |
| 81 | + var name: String |
| 82 | + var age: String |
| 83 | + """, |
| 84 | + range: .init( |
| 85 | + start: .init(line: 1, character: 0), |
| 86 | + end: .init(line: 2, character: 18) |
| 87 | + ) |
| 88 | + ), |
| 89 | + ] |
| 90 | + |
| 91 | + let lines = [ |
| 92 | + "struct Cat {\n", |
| 93 | + "\n", |
| 94 | + "}\n", |
| 95 | + ] |
| 96 | + |
| 97 | + var editor = EditorContent( |
| 98 | + content: lines.joined(), |
| 99 | + lines: lines, |
| 100 | + uti: "", |
| 101 | + cursorPosition: .init(line: 0, character: 17), |
| 102 | + tabSize: 1, |
| 103 | + indentSize: 1, |
| 104 | + usesTabsForIndentation: false |
| 105 | + ) |
| 106 | + |
| 107 | + Environment.triggerAction = { _ in } |
| 108 | + |
| 109 | + _ = try await service.generateRealtimeSuggestions(editor: editor) |
| 110 | + |
| 111 | + editor.cursorPosition = .init(line: 1, character: 17) |
| 112 | + |
| 113 | + let result = try await service.presentRealtimeSuggestions(editor: editor) |
| 114 | + |
| 115 | + XCTAssertNil(result) |
| 116 | + } |
| 117 | +} |
0 commit comments