Skip to content

Commit

Permalink
PR improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoncal committed Mar 19, 2024
1 parent b29f5cc commit dae3229
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 32 deletions.
7 changes: 0 additions & 7 deletions Extensions/Mocks/HAConnection+Mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ public class HAMockConnection: HAConnection {
/// Whether to turn to 'connecting' when a 'send' or 'subscribe' occurs when 'disconnected'
public var automaticallyTransitionToConnecting = true

/// Data request received to be written
public var sttDataRequestReceived: HARequest?

// MARK: - Mock Implementation

public weak var delegate: HAConnectionDelegate?
Expand Down Expand Up @@ -229,8 +226,4 @@ public class HAMockConnection: HAConnection {
CFRunLoopRunInMode(runLoopMode, 1.0, false)
}
}

public func sendSttAudio(_ request: HARequest) {
sttDataRequestReceived = request
}
}
5 changes: 0 additions & 5 deletions Source/HAConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,4 @@ public protocol HAConnection: AnyObject {
initiated: @escaping SubscriptionInitiatedHandler,
handler: @escaping (HACancellable, T) -> Void
) -> HACancellable

/// Send audio to Assist
/// - Parameters:
/// - request: The data request containing sttBinaryHandlerId and data (as base64 string) to be written
func sendSttAudio(_ request: HARequest)
}
16 changes: 5 additions & 11 deletions Source/Internal/HAConnectionImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,6 @@ internal class HAConnectionImpl: HAConnection {
) -> HACancellable {
commonSubscribe(to: request, initiated: initiated, handler: handler)
}

// MARK: - Write

public func sendSttAudio(_ request: HARequest) {
defer { connectAutomaticallyIfNeeded() }
let invocation = HARequestInvocationSingle(request: request) { _ in }
requestController.add(invocation)
}
}

// MARK: -
Expand Down Expand Up @@ -407,7 +399,9 @@ extension HAConnectionImpl {
// 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)
connection?.write(data: audioData) { [weak self] in
self?.responseController.didWrite()
}
}
}

Expand All @@ -420,8 +414,8 @@ extension HAConnectionImpl {
sendWebSocket(identifier: identifier, request: request, command: command)
case let .rest(method, command):
sendRest(identifier: identifier!, request: request, method: method, command: command)
case let .sttData(sttBinaryHandlerId):
sendWrite(sttBinaryHandlerId, audioDataString: request.data["audioData"] as? String)
case let .sttData(data):
sendWrite(data.sttBinaryHandlerId, audioDataString: request.data["audioData"] as? String)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ internal struct HARequestControllerAllowedSendKind: OptionSet {

static let webSocket: Self = .init(rawValue: 0b1)
static let rest: Self = .init(rawValue: 0b10)
static let sttData: Self = .init(rawValue: 0b10)
static let sttData: Self = .init(rawValue: 0b11)
static let all: Self = [.webSocket, .rest, .sttData]

func allows(requestType: HARequestType) -> Bool {
Expand Down
5 changes: 5 additions & 0 deletions Source/Internal/ResponseController/HAResponseController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ internal protocol HAResponseController: AnyObject {
var phase: HAResponseControllerPhase { get }

func reset()
func didWrite()
func didReceive(event: Starscream.WebSocketEvent)
func didReceive(
for identifier: HARequestIdentifier,
Expand Down Expand Up @@ -187,4 +188,8 @@ internal class HAResponseControllerImpl: HAResponseController {
}
}
}

func didWrite() {
HAGlobal.log(.info, "Data written")

Check warning on line 193 in Source/Internal/ResponseController/HAResponseController.swift

View check run for this annotation

Codecov / codecov/patch

Source/Internal/ResponseController/HAResponseController.swift#L192-L193

Added lines #L192 - L193 were not covered by tests
}
}
2 changes: 1 addition & 1 deletion Source/Requests/HARequestType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public enum HARequestType: Hashable, Comparable, ExpressibleByStringLiteral {
/// Sent over REST, the HTTP method to use and the post-`api/` path
case rest(HAHTTPMethod, String)
/// Sent over WebSocket, the stt binary handler id
case sttData(UInt8)
case sttData(HASttData)

/// Create a WebSocket request type by string literal
/// - Parameter value: The name of the WebSocket command
Expand Down
4 changes: 4 additions & 0 deletions Source/Requests/HASttData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// Write audio data to websocket, sttBinaryHandlerId is provided by run-start in Assist pipeline
public struct HASttData: Hashable {
var sttBinaryHandlerId: UInt8
}
1 change: 1 addition & 0 deletions Tests/FakeEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal class FakeEngine: Engine {

func write(data: Data, opcode: FrameOpCode, completion: (() -> Void)?) {
events.append(.writeData(data, opcode: opcode))
completion?()
}

func write(string: String, completion: (() -> Void)?) {
Expand Down
34 changes: 28 additions & 6 deletions Tests/HAConnectionImpl.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1463,29 +1463,47 @@ internal class HAConnectionImplTests: XCTestCase {
}

func testWriteDataRequestAddsToRequestController() {
connection.connectAutomatically = true
let expectedData = "Fake data".data(using: .utf8)!
let request = HARequest(
type: .sttData(1),
type: .sttData(.init(sttBinaryHandlerId: 1)),
data: [
"audioData": expectedData.base64EncodedString(),
]
)
connection.connect()
connection.sendSttAudio(request)
connection.send(request) { _ in }
XCTAssertNotNil(requestController.added.first(where: { invocation in
if case let .sttData(sttBinaryHandlerId) = invocation.request.type {
return sttBinaryHandlerId == 1 && invocation.request.data["audioData"] as? String == expectedData
return sttBinaryHandlerId == .init(sttBinaryHandlerId: 1) && invocation.request
.data["audioData"] as? String == expectedData
.base64EncodedString()
}
return false
}))
}

func testWriteDataRequestsCallCompletion() {
let expectation = expectation(description: "Waiting for completion")
let expectedData = "Fake data".data(using: .utf8)!
let request = HARequest(
type: .sttData(.init(sttBinaryHandlerId: 1)),
data: [
"audioData": expectedData.base64EncodedString(),
]
)
connection.connect()
responseController.receivedWaitExpectation = expectation
connection.requestController(requestController, didPrepareRequest: request, with: .init(integerLiteral: 1))

wait(for: [expectation], timeout: 5.0)
}

func testSendRawWithSttRequest() {
connection.connect()
let expectedData = "Fake data".data(using: .utf8)!
let request = HARequest(
type: .sttData(1),
type: .sttData(.init(sttBinaryHandlerId: 1)),
data: [
"audioData": expectedData.base64EncodedString(),
]
Expand All @@ -1503,13 +1521,13 @@ internal class HAConnectionImplTests: XCTestCase {
func testWriteSttRequestCommand() {
let expectedData = "Fake data".data(using: .utf8)!
let request = HARequest(
type: .sttData(1),
type: .sttData(.init(sttBinaryHandlerId: 1)),
data: [
"audioData": expectedData.base64EncodedString(),
]
)
let request2 = HARequest(
type: .sttData(2),
type: .sttData(.init(sttBinaryHandlerId: 1)),
data: [
"audioData": expectedData.base64EncodedString(),
]
Expand Down Expand Up @@ -1671,6 +1689,10 @@ private class FakeHAResponseController: HAResponseController {
receivedWaitExpectation?.fulfill()
}

func didWrite() {
receivedWaitExpectation?.fulfill()
}

var receivedRestWaitExpectation: XCTestExpectation?
var receivedRest: [Swift.Result<(HTTPURLResponse, Data?), Error>] = []
func didReceive(for identifier: HARequestIdentifier, response: Swift.Result<(HTTPURLResponse, Data?), Error>) {
Expand Down
2 changes: 1 addition & 1 deletion Tests/HARequestController.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ internal class HARequestControllerTests: XCTestCase {

func testAddingWriteRequestAllowed() {
delegate.allowedSendKinds = .all
controller.add(.init(request: .init(type: .sttData(1))))
controller.add(.init(request: .init(type: .sttData(.init(sttBinaryHandlerId: 1)))))
XCTAssertEqual(delegate.didPrepare.count, 1)
}

Expand Down
11 changes: 11 additions & 0 deletions Tests/HAResponseController.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,17 @@ private extension HAResponseControllerTests {

XCTAssertEqual(delegate.lastPhase, expectingPhase)
}

func testDidWriteEventLogs() {
let expectation = expectation(description: "Receive log")
HAGlobal.log = { level, message in
XCTAssertEqual(level, .info)
XCTAssertEqual(message, "Data written")
expectation.fulfill()
}
controller.didWrite()
wait(for: [expectation], timeout: 2)
}
}

private class FakeHAResponseControllerDelegate: HAResponseControllerDelegate {
Expand Down

0 comments on commit dae3229

Please sign in to comment.