Skip to content

Commit

Permalink
PR Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoncal committed Mar 27, 2024
1 parent da9887e commit 80d4c99
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
8 changes: 6 additions & 2 deletions Source/Internal/HAConnectionImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ extension HAConnectionImpl {
}
}

private func sendWrite(_ sttBinaryHandlerId: UInt8, audioDataString: String?) {
private func sendWrite(identifier: HARequestIdentifier?, sttBinaryHandlerId: UInt8, audioDataString: String?) {
// If there is no audioData, handlerID will be the payload alone indicating end of audio
var audioData = Data(base64Encoded: audioDataString ?? "") ?? Data()

Expand All @@ -415,7 +415,11 @@ extension HAConnectionImpl {
case let .rest(method, command):
sendRest(identifier: identifier!, request: request, method: method, command: command)
case let .sttData(data):
sendWrite(data.rawValue, audioDataString: request.data["audioData"] as? String)
sendWrite(
identifier: identifier,
sttBinaryHandlerId: data.rawValue,
audioDataString: request.data["audioData"] as? String
)
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Source/Requests/HASttData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ public struct HASttHandlerId: Hashable {

public extension HATypedRequest {
/// Send binary stream STT data
/// - Parameters:
/// - sttHandlerId: Handler Id provided by run-start event from Assist pipeline
/// - audioDataBase64Encoded: Audio data base 64 encoded
/// - Returns: A typed request that can be sent via `HAConnection`
static func sendSttData(sttHandlerId: UInt8, audioDataBase64Encoded: String) -> HATypedRequest<HAResponseVoid> {
.init(request: .init(type: .sttData(.init(rawValue: sttHandlerId)), data: [
"audioData": audioDataBase64Encoded
"audioData": audioDataBase64Encoded,
]))
}
}
20 changes: 19 additions & 1 deletion Tests/HAConnectionImpl.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,7 @@ internal class HAConnectionImplTests: XCTestCase {
})
}

func testWriteSttRequestCommand() {
func testSendSttRequestCommand() {
let expectedData = "Fake data".data(using: .utf8)!
let request = HARequest(
type: .sttData(.init(rawValue: 1)),
Expand All @@ -1536,6 +1536,24 @@ internal class HAConnectionImplTests: XCTestCase {
XCTAssertEqual(request.type.command, "")
XCTAssertEqual(request.type < request2.type, false)
}

func testSendSttRequestSentSuccessful() throws {
let expectation = self.expectation(description: "completion")
responseController.phase = .command(version: "2024.4")
_ = connection.send(.sendSttData(sttHandlerId: 1, audioDataBase64Encoded: ""), completion: { _ in
expectation.fulfill()
})
let added = try XCTUnwrap(requestController.added.first as? HARequestInvocationSingle)
added.resolve(.success(.empty))
waitForExpectations(timeout: 10.0)
}

func testSendSttDataTypedRequest() {
let request: HATypedRequest<HAResponseVoid> = .sendSttData(sttHandlerId: 1, audioDataBase64Encoded: "a")

XCTAssertEqual(request.request.data as? [String: String], ["audioData": "a"])
XCTAssertEqual(request.request.type, .sttData(.init(rawValue: 1)))
}
}

extension WebSocketEvent: Equatable {
Expand Down

0 comments on commit 80d4c99

Please sign in to comment.