Skip to content

chore: Rewrite ModelResponseEventsStreamInterpreterTests #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Tests/OpenAITests/ChatQueryCodingTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Test.swift
// ChatQueryCodingTests.swift
// OpenAI
//
// Created by Oleksii Nezhyborets on 20.05.2025.
Expand Down
53 changes: 29 additions & 24 deletions Tests/OpenAITests/ModelResponseEventsStreamInterpreterTests.swift
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
//
// Test.swift
// OpenAI
//  ModelResponseEventsStreamInterpreterTests.swift
//  OpenAI
//
// Created by Oleksii Nezhyborets on 10.04.2025.
//  Created by Oleksii Nezhyborets on 10.04.2025.
//

import Testing
import XCTest
@testable import OpenAI

@MainActor
struct Test {
final class ModelResponseEventsStreamInterpreterTests: XCTestCase {
private let interpreter = ModelResponseEventsStreamInterpreter()
@Test(.timeLimit(.minutes(1)))
func parseApiError() async throws {
var error: Error!
await withCheckedContinuation { continuation in
interpreter.setCallbackClosures { result in
} onError: { apiError in
Task {
await MainActor.run {
error = apiError
continuation.resume()
}

func testParseApiError() async throws {
let expectation = XCTestExpectation(description: "API Error callback received")
var receivedError: Error?

interpreter.setCallbackClosures { result in
// This closure is for successful results, which we don't expect here
XCTFail("Unexpected successful result received")
} onError: { apiError in
Task {
await MainActor.run {
receivedError = apiError
expectation.fulfill() // Fulfill the expectation when the error is received
}
}

interpreter.processData(
MockServerSentEvent.chatCompletionError()
)
}

#expect(error is APIErrorResponse)

interpreter.processData(
MockServerSentEvent.chatCompletionError()
)

// Wait for the expectation to be fulfilled, with a timeout
await fulfillment(of: [expectation], timeout: 1.0)

// Assert that an error was received and that it is of the expected type
XCTAssertNotNil(receivedError, "Expected an error to be received, but got nil.")
XCTAssertTrue(receivedError is APIErrorResponse, "Expected received error to be of type APIErrorResponse.")
}
}