Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
stephtelolahy committed Jan 6, 2025
1 parent b3869dc commit 94b84ab
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 55 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import CardsData

extension GameState {
static func makeBuilderWithAllCards() -> Builder {
makeBuilder().withCards(Cards.all)
makeBuilder()
.withCards(Cards.all)
}
}

extension GameState.Builder {
func withDummyCards(_ names: [String]) -> Self {
let extraCards = names.reduce(into: [String: Card]()) { partialResult, element in
let dummyCards = names.reduce(into: [String: Card]()) { partialResult, element in
partialResult[element] = .init(name: element)
}
return withCards(extraCards)
return withCards(dummyCards)
}
}
67 changes: 33 additions & 34 deletions WildWestOnline/Data/Cards/Tests/Misc/GameStore+Dispatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,45 @@ func dispatchUntilCompleted(
file: StaticString = #file,
line: UInt = #line
) async throws -> [GameAction] {
let expectation = XCTestExpectation(description: "Game idle")

let store = Store<GameState>(
initial: state,
reducer: GameReducer().reduce,
middlewares: [
Middlewares.updateGame,
Middlewares.performChoices(.init(choices: expectedChoices))
]
) {
expectation.fulfill()
let sut = await createGameStoreWithSideEffects(initialState: state)
var receivedActions: [Action] = []
var receivedErrors: [Error] = []
var cancellables: Set<AnyCancellable> = []
await MainActor.run {
sut.eventPublisher
.sink { receivedActions.append($0) }
.store(in: &cancellables)
sut.errorPublisher
.sink { receivedErrors.append($0) }
.store(in: &cancellables)
}

var subscriptions: Set<AnyCancellable> = []
var ocurredEvents: [GameAction] = []
var ocurredError: Error?
// When
await sut.dispatch(action)

store.eventPublisher.sink { event in
if let gameAction = event as? GameAction,
gameAction.isRenderable {
ocurredEvents.append(gameAction)
}
// Then
guard receivedErrors.isEmpty else {
throw receivedErrors[0]
}
.store(in: &subscriptions)

store.errorPublisher.sink { error in
ocurredError = error
let gameActions = receivedActions.compactMap { action in
if let action = action as? GameAction,
action.isRenderable {
return action
} else {
return nil
}
}
.store(in: &subscriptions)

store.dispatch(action)

let waiter = XCTWaiter()
await waiter.fulfillment(of: [expectation])
subscriptions.removeAll()

if let ocurredError {
throw ocurredError
}
return gameActions
}

return ocurredEvents
@MainActor private func createGameStoreWithSideEffects(initialState: GameState) -> Store<GameState, Void> {
.init(
initialState: initialState,
reducer: gameReducer,
dependencies: ()
)
}

struct Choice {
Expand All @@ -72,7 +70,7 @@ private class ChoicesWrapper {
self.choices = choices
}
}

/*
private extension Middlewares {
static func performChoices(_ expectedChoicesWrapper: ChoicesWrapper) -> Middleware<GameState> {
{ state, _ in
Expand All @@ -94,3 +92,4 @@ private extension Middlewares {
}
}
}
*/
3 changes: 2 additions & 1 deletion WildWestOnline/Data/Cards/Tests/SimulationTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import XCTest
import Redux
import GameCore
import CardsData

/*
struct SimulationTest {
@Test func simulate2PlayersGame_shouldComplete() async throws {
try await simulateGame(playersCount: 3)
Expand Down Expand Up @@ -76,3 +76,4 @@ private class StateWrapper: @unchecked Sendable {
self.value = value
}
}
*/

0 comments on commit 94b84ab

Please sign in to comment.