Skip to content

Commit 3021047

Browse files
committed
add test
1 parent 788dfe1 commit 3021047

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Tests/OpenAITests/MockServerSentEvent.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,17 @@ struct MockServerSentEvent {
2020
static func chatCompletionError() -> Data {
2121
"{\n \"error\": {\n \"message\": \"The model `o3-mini` does not exist or you do not have access to it.\",\n \"type\": \"invalid_request_error\",\n \"param\": null,\n \"code\": \"model_not_found\"\n }\n}\n".data(using: .utf8)!
2222
}
23+
24+
static func responseOutputTextDelta(
25+
itemId: String = "msg_1",
26+
outputIndex: Int = 0,
27+
contentIndex: Int = 0,
28+
delta: String = "Hi",
29+
sequenceNumber: Int = 1
30+
) -> Data {
31+
let json = """
32+
{"type":"response.output_text.delta","output_index":\(outputIndex),"item_id":"\(itemId)","content_index":\(contentIndex),"delta":"\(delta)","sequence_number":\(sequenceNumber)}
33+
"""
34+
return "data: \(json)\n\n".data(using: .utf8)!
35+
}
2336
}

Tests/OpenAITests/ModelResponseEventsStreamInterpreterTests.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,48 @@ final class ModelResponseEventsStreamInterpreterTests: XCTestCase {
3939
XCTAssertNotNil(receivedError, "Expected an error to be received, but got nil.")
4040
XCTAssertTrue(receivedError is APIErrorResponse, "Expected received error to be of type APIErrorResponse.")
4141
}
42+
43+
func testParsesOutputTextDeltaUsingPayloadType() async throws {
44+
let expectation = XCTestExpectation(description: "OutputText delta event received")
45+
var receivedEvent: ResponseStreamEvent?
46+
47+
interpreter.setCallbackClosures { event in
48+
Task {
49+
await MainActor.run {
50+
receivedEvent = event
51+
expectation.fulfill()
52+
}
53+
}
54+
} onError: { error in
55+
XCTFail("Unexpected error received: \(error)")
56+
}
57+
58+
interpreter.processData(
59+
MockServerSentEvent.responseOutputTextDelta(
60+
itemId: "msg_1",
61+
outputIndex: 0,
62+
contentIndex: 0,
63+
delta: "Hi",
64+
sequenceNumber: 1
65+
)
66+
)
67+
68+
await fulfillment(of: [expectation], timeout: 1.0)
69+
70+
guard let receivedEvent else {
71+
XCTFail("No event received")
72+
return
73+
}
74+
75+
switch receivedEvent {
76+
case .outputText(.delta(let deltaEvent)):
77+
XCTAssertEqual(deltaEvent.itemId, "msg_1")
78+
XCTAssertEqual(deltaEvent.outputIndex, 0)
79+
XCTAssertEqual(deltaEvent.contentIndex, 0)
80+
XCTAssertEqual(deltaEvent.delta, "Hi")
81+
XCTAssertEqual(deltaEvent.sequenceNumber, 1)
82+
default:
83+
XCTFail("Expected .outputText(.delta), got \(receivedEvent)")
84+
}
85+
}
4286
}

0 commit comments

Comments
 (0)