Skip to content

Commit 4d53673

Browse files
committed
Fix tests
1 parent 339878c commit 4d53673

6 files changed

Lines changed: 30 additions & 13 deletions

File tree

Core/Package.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ let package = Package(
131131
.target(name: "Logger"),
132132
.target(
133133
name: "OpenAIService",
134-
dependencies: [.product(name: "AsyncAlgorithms", package: "swift-async-algorithms")]
134+
dependencies: [
135+
"Logger",
136+
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms")
137+
]
135138
),
136139
.testTarget(
137140
name: "OpenAIServiceTests",

Core/Sources/OpenAIService/ChatGPTService.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import AsyncAlgorithms
22
import Foundation
3-
import Logger
43

54
public protocol ChatGPTServiceType {
65
func send(content: String, summary: String?) async throws -> AsyncThrowingStream<String, Error>
@@ -64,6 +63,7 @@ public actor ChatGPTService: ChatGPTServiceType, ObservableObject {
6463
didSet { objectWillChange.send() }
6564
}
6665

66+
var uuidGenerator: () -> String = { UUID().uuidString }
6767
var cancelTask: Cancellable?
6868
var buildCompletionStreamAPI: CompletionStreamAPIBuilder = OpenAICompletionStreamAPI.init
6969

@@ -89,7 +89,12 @@ public actor ChatGPTService: ChatGPTServiceType, ObservableObject {
8989
) async throws -> AsyncThrowingStream<String, Error> {
9090
guard !isReceivingMessage else { throw CancellationError() }
9191
guard let url = URL(string: endpoint) else { throw ChatGPTServiceError.endpointIncorrect }
92-
let newMessage = ChatMessage(role: .user, content: content, summary: summary)
92+
let newMessage = ChatMessage(
93+
id: uuidGenerator(),
94+
role: .user,
95+
content: content,
96+
summary: summary
97+
)
9398
history.append(newMessage)
9499

95100
let requestBody = CompletionRequestBody(
@@ -142,7 +147,6 @@ public actor ChatGPTService: ChatGPTServiceType, ObservableObject {
142147
isReceivingMessage = false
143148
continuation.finish(throwing: error)
144149
} catch {
145-
Logger.service.error(error)
146150
history.append(.init(
147151
role: .assistant,
148152
content: error.localizedDescription
@@ -178,6 +182,10 @@ extension ChatGPTService {
178182
func changeBuildCompletionStreamAPI(_ builder: @escaping CompletionStreamAPIBuilder) {
179183
buildCompletionStreamAPI = builder
180184
}
185+
186+
func changeUUIDGenerator(_ generator: @escaping () -> String) {
187+
uuidGenerator = generator
188+
}
181189

182190
func combineHistoryWithSystemPrompt() -> [CompletionRequestBody.Message] {
183191
if history.count > 4 {

Core/Sources/OpenAIService/CompletionStreamAPI.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ protocol CompletionStreamAPI {
1111
}
1212

1313
/// https://platform.openai.com/docs/api-reference/chat/create
14-
struct CompletionRequestBody: Codable {
15-
struct Message: Codable {
14+
struct CompletionRequestBody: Codable, Equatable {
15+
struct Message: Codable, Equatable {
1616
var role: ChatMessage.Role
1717
var content: String
1818
}
19-
19+
2020
var model: String
2121
var messages: [Message]
2222
var temperature: Double?
@@ -76,7 +76,7 @@ struct OpenAICompletionStreamAPI: CompletionStreamAPI {
7676
guard let response = response as? HTTPURLResponse else {
7777
throw ChatGPTServiceError.responseInvalid
7878
}
79-
79+
8080
guard response.statusCode == 200 else {
8181
let text = try await result.lines.reduce(into: "") { partialResult, current in
8282
partialResult += current
@@ -113,5 +113,3 @@ struct OpenAICompletionStreamAPI: CompletionStreamAPI {
113113
)
114114
}
115115
}
116-
117-

Core/Sources/SuggestionWidget/SuggestionWidgetController.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ public final class SuggestionWidgetController {
110110
}
111111

112112
public nonisolated init() {
113+
#warning("TODO: A test is initializing this class for unknown reasons, try a better way to avoid this.")
114+
if ProcessInfo.processInfo.environment["IS_UNIT_TEST"] == "YES" { return }
115+
113116
Task { @MainActor in
114117
activeApplicationMonitorTask = Task { [weak self] in
115118
var previousApp: NSRunningApplication?

Core/Tests/OpenAIServiceTests/ChatGPTServiceTests.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ final class ChatGPTServiceTests: XCTestCase {
3636
func test_success() async throws {
3737
let service = ChatGPTService(systemPrompt: "system", apiKey: "Key")
3838
var apiKey = ""
39+
var idCounter = 0
40+
await service.changeUUIDGenerator {
41+
defer { idCounter += 1 }
42+
return "\(idCounter)"
43+
}
3944
var requestBody: CompletionRequestBody?
4045
await service.changeBuildCompletionStreamAPI { _apiKey, _, _requestBody in
4146
apiKey = _apiKey
@@ -62,8 +67,8 @@ final class ChatGPTServiceTests: XCTestCase {
6267
XCTAssertEqual(all, ["hello", "my", "friends"], "Text stream is correct")
6368
let history = await service.history
6469
XCTAssertEqual(history, [
65-
.init(role: .user, content: "Hello"),
66-
.init(role: .assistant, content: "hellomyfriends", id: "1"),
70+
.init(id: "0", role: .user, content: "Hello"),
71+
.init(id: "1", role: .assistant, content: "hellomyfriends"),
6772
], "History is correctly updated")
6873
}
6974
}

TestPlan.xctestplan

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
},
4949
{
5050
"target" : {
51-
"containerPath" : "container:",
51+
"containerPath" : "container:Core",
5252
"identifier" : "OpenAIServiceTests",
5353
"name" : "OpenAIServiceTests"
5454
}

0 commit comments

Comments
 (0)