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 d0b8faf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
17 changes: 14 additions & 3 deletions Source/Internal/HAConnectionImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -392,15 +392,22 @@ 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()

// Prefix audioData with handler ID so the API can map the binary data
audioData.insert(sttBinaryHandlerId, at: 0)
workQueue.async { [connection] in
connection?.write(data: audioData) { [weak self] in
self?.responseController.didWrite()
guard let self else { return }
self.responseController.didWrite()
if let identifier, let request = self.requestController.single(for: identifier) {
callbackQueue.async {
request.resolve(.success(.empty))

Check warning on line 407 in Source/Internal/HAConnectionImpl.swift

View check run for this annotation

Codecov / codecov/patch

Source/Internal/HAConnectionImpl.swift#L406-L407

Added lines #L406 - L407 were not covered by tests
}
requestController.clear(invocation: request)

Check warning on line 409 in Source/Internal/HAConnectionImpl.swift

View check run for this annotation

Codecov / codecov/patch

Source/Internal/HAConnectionImpl.swift#L409

Added line #L409 was not covered by tests
}
}
}
}
Expand All @@ -415,7 +422,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 d0b8faf

Please sign in to comment.