Skip to content

Commit f8f5a49

Browse files
committed
Fix unit tests
1 parent 0864650 commit f8f5a49

7 files changed

Lines changed: 79 additions & 35 deletions

File tree

Core/Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ let package = Package(
183183
"SystemInfoChatContextCollector",
184184

185185
.product(name: "ChatContextCollector", package: "Tool"),
186+
.product(name: "PromptToCode", package: "Tool"),
186187
.product(name: "AppMonitoring", package: "Tool"),
187188
.product(name: "Parsing", package: "swift-parsing"),
188189
.product(name: "OpenAIService", package: "Tool"),

Core/Tests/ServiceTests/FilespaceSuggestionInvalidationTests.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ class FilespaceSuggestionInvalidationTests: XCTestCase {
1414
range: CursorRange
1515
) async throws -> Filespace {
1616
let pool = WorkspacePool()
17-
let (_, filespace) = try await pool
18-
.fetchOrCreateWorkspaceAndFilespace(fileURL: URL(fileURLWithPath: "file/path/to.swift"))
17+
let (_, filespace) = try await pool.fetchOrCreateWorkspaceAndFilespace(
18+
fileURL: URL(fileURLWithPath: "file/path/to.swift"),
19+
checkIfFileExists: false
20+
)
1921
filespace.suggestions = [
2022
.init(
2123
id: "",
@@ -120,7 +122,7 @@ class FilespaceSuggestionInvalidationTests: XCTestCase {
120122
let suggestion = filespace.presentingSuggestion
121123
XCTAssertNotNil(suggestion)
122124
}
123-
125+
124126
func test_typing_not_according_to_suggestion_should_invalidate() async throws {
125127
let lines = ["\n", "hello ma\n", "\n"]
126128
let filespace = try await prepare(

Tool/Sources/OpenAIService/ChatGPTService.swift

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,17 @@ public protocol ChatGPTServiceType {
105105

106106
public class ChatGPTService: ChatGPTServiceType {
107107
public var configuration: ChatGPTConfiguration
108+
public var utilityConfiguration: ChatGPTConfiguration
108109
public var functionProvider: ChatGPTFunctionProvider
109110

110111
public init(
111112
configuration: ChatGPTConfiguration = UserPreferenceChatGPTConfiguration(),
113+
utilityConfiguration: ChatGPTConfiguration =
114+
UserPreferenceChatGPTConfiguration(chatModelKey: \.preferredChatModelIdForUtilities),
112115
functionProvider: ChatGPTFunctionProvider = NoChatGPTFunctionProvider()
113116
) {
114117
self.configuration = configuration
118+
self.utilityConfiguration = utilityConfiguration
115119
self.functionProvider = functionProvider
116120
}
117121

@@ -141,7 +145,15 @@ public class ChatGPTService: ChatGPTServiceType {
141145

142146
if !pendingToolCalls.isEmpty {
143147
if configuration.runFunctionsAutomatically {
144-
var toolCallStatuses = [String: String]()
148+
var toolCallStatuses = [String: String]() {
149+
didSet {
150+
if toolCallStatuses != oldValue {
151+
continuation.yield(.status(
152+
Array(toolCallStatuses.values).sorted()
153+
))
154+
}
155+
}
156+
}
145157
for toolCall in pendingToolCalls {
146158
let id = toolCall.id
147159
for await response in await runFunctionCall(
@@ -158,15 +170,11 @@ public class ChatGPTService: ChatGPTServiceType {
158170
))
159171
case let .status(status):
160172
toolCallStatuses[id] = status
161-
continuation
162-
.yield(.status(Array(toolCallStatuses.values)))
163173
}
164174
}
165175
toolCallStatuses[id] = nil
166-
continuation
167-
.yield(.status(Array(toolCallStatuses.values)))
168176
}
169-
continuation.yield(.status([]))
177+
toolCallStatuses = [:]
170178
} else {
171179
if !configuration.runFunctionsAutomatically {
172180
continuation.yield(.toolCalls(pendingToolCalls))
@@ -190,7 +198,15 @@ public class ChatGPTService: ChatGPTServiceType {
190198

191199
case let .partialToolCalls(toolCalls):
192200
guard configuration.runFunctionsAutomatically else { break }
193-
var toolCallStatuses = [String: String]()
201+
var toolCallStatuses = [String: String]() {
202+
didSet {
203+
if toolCallStatuses != oldValue {
204+
continuation.yield(.status(
205+
Array(toolCallStatuses.values).sorted()
206+
))
207+
}
208+
}
209+
}
194210
for toolCall in toolCalls.keys.sorted() {
195211
if let toolCallValue = toolCalls[toolCall] {
196212
for await status in await prepareFunctionCall(
@@ -199,8 +215,6 @@ public class ChatGPTService: ChatGPTServiceType {
199215
sourceMessageId: sourceMessageId
200216
) {
201217
toolCallStatuses[toolCallValue.id] = status
202-
continuation
203-
.yield(.status(Array(toolCallStatuses.values)))
204218
}
205219
}
206220
}
@@ -489,9 +503,7 @@ extension ChatGPTService {
489503

490504
let service = ChatGPTService(
491505
configuration: OverridingChatGPTConfiguration(
492-
overriding: UserPreferenceChatGPTConfiguration(
493-
chatModelKey: \.preferredChatModelIdForUtilities
494-
),
506+
overriding: utilityConfiguration,
495507
with: .init(temperature: 0)
496508
),
497509
functionProvider: NoChatGPTFunctionProvider()

Tool/Sources/Workspace/Workspace.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public final class Workspace {
6666
"Can't find workspace."
6767
}
6868
}
69-
69+
7070
public struct CantFindFileError: Error, LocalizedError {
7171
public var fileURL: URL
7272
public var errorDescription: String? {
@@ -124,13 +124,19 @@ public final class Workspace {
124124
}
125125

126126
@WorkspaceActor
127-
public func createFilespaceIfNeeded(fileURL: URL) throws -> Filespace {
127+
public func createFilespaceIfNeeded(
128+
fileURL: URL,
129+
checkIfFileExists: Bool = true
130+
) throws -> Filespace {
128131
let extensionName = fileURL.pathExtension
129132
if ["xcworkspace", "xcodeproj"].contains(extensionName) {
130133
throw UnsupportedFileError(extensionName: extensionName)
131134
}
132135
var isDirectory: ObjCBool = false
133-
if !FileManager.default.fileExists(atPath: fileURL.path, isDirectory: &isDirectory) {
136+
if checkIfFileExists, !FileManager.default.fileExists(
137+
atPath: fileURL.path,
138+
isDirectory: &isDirectory
139+
) {
134140
throw CantFindFileError(fileURL: fileURL)
135141
}
136142
if isDirectory.boolValue {

Tool/Sources/Workspace/WorkspacePool.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,29 @@ public class WorkspacePool {
8585
}
8686

8787
@WorkspaceActor
88-
public func fetchOrCreateWorkspaceAndFilespace(fileURL: URL) async throws
88+
public func fetchOrCreateWorkspaceAndFilespace(
89+
fileURL: URL,
90+
checkIfFileExists: Bool = true
91+
) async throws
8992
-> (workspace: Workspace, filespace: Filespace)
9093
{
9194
// If we can get the workspace URL directly.
9295
if let currentWorkspaceURL = await XcodeInspector.shared.safe.realtimeActiveWorkspaceURL {
9396
if let existed = workspaces[currentWorkspaceURL] {
9497
// Reuse the existed workspace.
95-
let filespace = try existed.createFilespaceIfNeeded(fileURL: fileURL)
98+
let filespace = try existed.createFilespaceIfNeeded(
99+
fileURL: fileURL,
100+
checkIfFileExists: checkIfFileExists
101+
)
96102
return (existed, filespace)
97103
}
98104

99105
let new = createNewWorkspace(workspaceURL: currentWorkspaceURL)
100106
workspaces[currentWorkspaceURL] = new
101-
let filespace = try new.createFilespaceIfNeeded(fileURL: fileURL)
107+
let filespace = try new.createFilespaceIfNeeded(
108+
fileURL: fileURL,
109+
checkIfFileExists: checkIfFileExists
110+
)
102111
return (new, filespace)
103112
}
104113

@@ -132,12 +141,15 @@ public class WorkspacePool {
132141
return createNewWorkspace(workspaceURL: workspaceURL)
133142
}()
134143

135-
let filespace = try workspace.createFilespaceIfNeeded(fileURL: fileURL)
144+
let filespace = try workspace.createFilespaceIfNeeded(
145+
fileURL: fileURL,
146+
checkIfFileExists: checkIfFileExists
147+
)
136148
workspaces[workspaceURL] = workspace
137149
workspace.refreshUpdateTime()
138150
return (workspace, filespace)
139151
}
140-
152+
141153
throw Workspace.CantFindWorkspaceError()
142154
}
143155

Tool/Tests/OpenAIServiceTests/AutoManagedChatGPTMemoryRetrievedContentTests.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ class AutoManagedChatGPTMemoryRetrievedContentTests: XCTestCase {
2020

2121
let memory = AutoManagedChatGPTMemory(
2222
systemPrompt: "",
23-
configuration: UserPreferenceChatGPTConfiguration(),
23+
configuration: UserPreferenceChatGPTConfiguration().overriding {
24+
$0.maxTokens = 999999
25+
},
2426
functionProvider: EmptyFunctionProvider()
2527
)
2628

@@ -93,7 +95,9 @@ class AutoManagedChatGPTMemoryRetrievedContentTests: XCTestCase {
9395

9496
let memory = AutoManagedChatGPTMemory(
9597
systemPrompt: "",
96-
configuration: UserPreferenceChatGPTConfiguration(),
98+
configuration: UserPreferenceChatGPTConfiguration().overriding {
99+
$0.maxTokens = 999999
100+
},
97101
functionProvider: EmptyFunctionProvider()
98102
)
99103

@@ -162,7 +166,9 @@ class AutoManagedChatGPTMemoryRetrievedContentTests: XCTestCase {
162166

163167
let memory = AutoManagedChatGPTMemory(
164168
systemPrompt: "",
165-
configuration: UserPreferenceChatGPTConfiguration(),
169+
configuration: UserPreferenceChatGPTConfiguration().overriding {
170+
$0.maxTokens = 999999
171+
},
166172
functionProvider: EmptyFunctionProvider()
167173
)
168174

@@ -203,7 +209,9 @@ class AutoManagedChatGPTMemoryRetrievedContentTests: XCTestCase {
203209

204210
let memory = AutoManagedChatGPTMemory(
205211
systemPrompt: "",
206-
configuration: UserPreferenceChatGPTConfiguration(),
212+
configuration: UserPreferenceChatGPTConfiguration().overriding {
213+
$0.maxTokens = 999999
214+
},
207215
functionProvider: EmptyFunctionProvider()
208216
)
209217

Tool/Tests/OpenAIServiceTests/ChatGPTServiceTests.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,16 @@ class ChatGPTServiceTests: XCTestCase {
173173

174174
let response = try await stream.asArray()
175175
XCTAssertEqual(response, [
176-
.status("start foo 1"),
177-
.status("start foo 2"),
178-
.status("start foo 3"),
179-
.status("start bar 1"),
180-
.status("start bar 2"),
181-
.status("start bar 3"),
182-
.status("foo hi"),
183-
.status("bar bye"),
176+
.status(["start foo 1"]),
177+
.status(["start foo 2"]),
178+
.status(["start foo 3"]),
179+
.status(["start bar 1", "start foo 3"]),
180+
.status(["start bar 2", "start foo 3"]),
181+
.status(["start bar 3", "start foo 3"]),
182+
.status(["foo hi"]),
183+
.status([]),
184+
.status(["bar bye"]),
185+
.status([]),
184186
.partialText("hello"),
185187
.partialText(" "),
186188
.partialText("world"),
@@ -262,6 +264,7 @@ class ChatGPTServiceTests: XCTestCase {
262264
configuration: EmptyConfiguration().overriding {
263265
$0.runFunctionsAutomatically = true
264266
},
267+
utilityConfiguration: EmptyConfiguration(),
265268
functionProvider: FunctionProvider()
266269
)
267270
return service.send(memory)

0 commit comments

Comments
 (0)