Files
swift-composable-architectu…/Examples/CaseStudies/SwiftUICaseStudiesTests/02-Effects-WebSocketTests.swift
Stephen Celis 57e804f1cc Macro bonanza (#2553)
* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Silence test warnings

* wip

* wip

* wip

* update a bunch of docs

* wip

* wip

* fix

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Kill integration tests for now

* wip

* wip

* wip

* wip

* updating docs for @Reducer macro

* replaced more Reducer protocols with @Reducer

* Fixed some broken docc references

* wip

* Some @Reducer docs

* more docs

* convert some old styles to new style

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* bump

* update tutorials to use body

* update tutorials to use DML on destination state enum

* Add diagnostic

* wip

* updated a few more tests

* wip

* wip

* Add another gotcha

* wip

* wip

* wip

* fixes

* wip

* wip

* wip

* wip

* wip

* fix

* wip

* remove for now

* wip

* wip

* updated some docs

* migration guides

* more migration guide

* fix ci

* fix

* soft deprecate all apis using AnyCasePath

* wip

* Fix

* fix tests

* swift-format 509 compatibility

* wip

* wip

* Update Sources/ComposableArchitecture/Macros.swift

Co-authored-by: Mateusz Bąk <bakmatthew@icloud.com>

* wip

* wip

* update optional state case study

* remove initializer

* Don't use @State for BasicsView integration demo

* fix tests

* remove reduce diagnostics for now

* diagnose error not warning

* Update Sources/ComposableArchitecture/Macros.swift

Co-authored-by: Jesse Tipton <jesse@jessetipton.com>

* wip

* move integration tests to cron

* Revert "move integration tests to cron"

This reverts commit f9bdf2f04b.

* disable flakey tests on CI

* wip

* wip

* Revert "Revert "move integration tests to cron""

This reverts commit 66aafa7327.

* fix

* wip

* fix

---------

Co-authored-by: Brandon Williams <mbrandonw@hey.com>
Co-authored-by: Mateusz Bąk <bakmatthew@icloud.com>
Co-authored-by: Brandon Williams <135203+mbrandonw@users.noreply.github.com>
Co-authored-by: Jesse Tipton <jesse@jessetipton.com>
2023-11-13 12:57:35 -08:00

161 lines
4.9 KiB
Swift

import ComposableArchitecture
import XCTest
@testable import SwiftUICaseStudies
@MainActor
final class WebSocketTests: XCTestCase {
func testWebSocketHappyPath() async {
let actions = AsyncStream.makeStream(of: WebSocketClient.Action.self)
let messages = AsyncStream.makeStream(of: Result<WebSocketClient.Message, Error>.self)
let store = TestStore(initialState: WebSocket.State()) {
WebSocket()
} withDependencies: {
$0.continuousClock = ImmediateClock()
$0.webSocket.open = { _, _, _ in actions.stream }
$0.webSocket.receive = { _ in messages.stream }
$0.webSocket.send = { _, _ in }
$0.webSocket.sendPing = { _ in try await Task.never() }
}
// Connect to the socket
await store.send(.connectButtonTapped) {
$0.connectivityState = .connecting
}
actions.continuation.yield(.didOpen(protocol: nil))
await store.receive(\.webSocket.didOpen) {
$0.connectivityState = .connected
}
// Receive a message
messages.continuation.yield(.success(.string("Welcome to echo.pointfree.co")))
await store.receive(\.receivedSocketMessage.success) {
$0.receivedMessages = ["Welcome to echo.pointfree.co"]
}
// Send a message
await store.send(.messageToSendChanged("Hi")) {
$0.messageToSend = "Hi"
}
await store.send(.sendButtonTapped) {
$0.messageToSend = ""
}
await store.receive(\.sendResponse)
// Receive a message
messages.continuation.yield(.success(.string("Hi")))
await store.receive(\.receivedSocketMessage.success) {
$0.receivedMessages = ["Welcome to echo.pointfree.co", "Hi"]
}
// Disconnect from the socket
await store.send(.connectButtonTapped) {
$0.connectivityState = .disconnected
}
await store.finish()
}
func testWebSocketSendFailure() async {
let actions = AsyncStream.makeStream(of: WebSocketClient.Action.self)
let messages = AsyncStream.makeStream(of: Result<WebSocketClient.Message, Error>.self)
let store = TestStore(initialState: WebSocket.State()) {
WebSocket()
} withDependencies: {
$0.continuousClock = ImmediateClock()
$0.webSocket.open = { _, _, _ in actions.stream }
$0.webSocket.receive = { _ in messages.stream }
$0.webSocket.send = { _, _ in
struct SendFailure: Error, Equatable {}
throw SendFailure()
}
$0.webSocket.sendPing = { _ in try await Task.never() }
}
// Connect to the socket
await store.send(.connectButtonTapped) {
$0.connectivityState = .connecting
}
actions.continuation.yield(.didOpen(protocol: nil))
await store.receive(\.webSocket.didOpen) {
$0.connectivityState = .connected
}
// Send a message
await store.send(.messageToSendChanged("Hi")) {
$0.messageToSend = "Hi"
}
await store.send(.sendButtonTapped) {
$0.messageToSend = ""
}
await store.receive(\.sendResponse) {
$0.alert = AlertState {
TextState("Could not send socket message. Connect to the server first, and try again.")
}
}
// Disconnect from the socket
await store.send(.connectButtonTapped) {
$0.connectivityState = .disconnected
}
await store.finish()
}
func testWebSocketPings() async {
let actions = AsyncStream.makeStream(of: WebSocketClient.Action.self)
let clock = TestClock()
var pingsCount = 0
let store = TestStore(initialState: WebSocket.State()) {
WebSocket()
} withDependencies: {
$0.continuousClock = clock
$0.webSocket.open = { _, _, _ in actions.stream }
$0.webSocket.receive = { _ in try await Task.never() }
$0.webSocket.sendPing = { @MainActor _ in pingsCount += 1 }
}
// Connect to the socket
await store.send(.connectButtonTapped) {
$0.connectivityState = .connecting
}
actions.continuation.yield(.didOpen(protocol: nil))
await store.receive(\.webSocket.didOpen) {
$0.connectivityState = .connected
}
// Wait for ping
XCTAssertEqual(pingsCount, 0)
await clock.advance(by: .seconds(10))
XCTAssertEqual(pingsCount, 1)
// Disconnect from the socket
await store.send(.connectButtonTapped) {
$0.connectivityState = .disconnected
}
}
func testWebSocketConnectError() async {
let actions = AsyncStream.makeStream(of: WebSocketClient.Action.self)
let store = TestStore(initialState: WebSocket.State()) {
WebSocket()
} withDependencies: {
$0.continuousClock = ImmediateClock()
$0.webSocket.open = { _, _, _ in actions.stream }
$0.webSocket.receive = { _ in try await Task.never() }
$0.webSocket.sendPing = { _ in try await Task.never() }
}
// Attempt to connect to the socket
await store.send(.connectButtonTapped) {
$0.connectivityState = .connecting
}
actions.continuation.yield(.didClose(code: .internalServerError, reason: nil))
await store.receive(\.webSocket.didClose) {
$0.connectivityState = .disconnected
}
}
}