Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
stephtelolahy committed Jan 11, 2025
1 parent ac21b1a commit 57982a7
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions WildWestOnline/Data/Cards/Tests/SimulationTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,19 @@ struct SimulationTest {
}
}

@MainActor private func createGameStoreWithAIAgent(initialState: GameState) -> Store<GameState, Void> {
@MainActor private func createGameStoreWithAIAgent(initialState: GameState) -> Store<GameState, GameStoreDependencies> {
.init(
initialState: initialState,
reducer: { state, action, dependencies in
.group([
try loggerReducer(state: &state, action: action, dependencies: dependencies),
try gameReducer(state: &state, action: action, dependencies: dependencies),
try updateGameReducer(state: &state, action: action, dependencies: dependencies),
try playAIMoveReducer(state: &state, action: action, dependencies: dependencies)
try loggerReducer(state: &state, action: action, dependencies: ()),
try gameReducer(state: &state, action: action, dependencies: ()),
try updateGameReducer(state: &state, action: action, dependencies: ()),
try playAIMoveReducer(state: &state, action: action, dependencies: ()),
try verifyStateReducer(state: &state, action: action, dependencies: dependencies.stateHolder)
])
},
dependencies: ()
dependencies: .init(stateHolder: .init(value: initialState))
)
}

Expand All @@ -61,30 +62,39 @@ private func loggerReducer(
}
}

/*
private extension Middlewares {
static func verifyState(_ prevState: StateWrapper) -> Middleware<GameState> {
{ state, action in
DispatchQueue.main.async {
guard let nextState = try? GameReducer().reduce(prevState.value, action) else {
fatalError("Failed reducing \(action)")
}
private func verifyStateReducer(
state: inout GameState,
action: Action,
dependencies: StateHolder
) throws -> Effect {
let state = state
return .run {
await verifyState(state: state, action: action, prevState: dependencies)
}
}

assert(nextState == state, "Inconsistent state after applying \(action)")
private struct GameStoreDependencies {
let stateHolder: StateHolder
}

prevState.value = nextState
}
private func verifyState(
state: GameState,
action: Action,
prevState: StateHolder
) async -> Action? {
var nextState = prevState.value
_ = try? gameReducer(state: &nextState, action: action, dependencies: ())

return nil
}
}
assert(nextState == state, "Inconsistent state after applying \(action)")

prevState.value = nextState
return nil
}

private class StateWrapper: @unchecked Sendable {
private class StateHolder: @unchecked Sendable {
var value: GameState

init(value: GameState) {
self.value = value
}
}
*/

0 comments on commit 57982a7

Please sign in to comment.