refactor: migrate battle reducer to RTK slice#2632
refactor: migrate battle reducer to RTK slice#2632KagamiChan wants to merge 2 commits intomasterfrom
Conversation
Moves battle handling behind a middleware-driven slice to drop the legacy store-arg reducer and make behavior easier to test.
Run battle slice middleware after next(action) so it reads updated info/sortie state; add regression test covering same-action upstream updates.
There was a problem hiding this comment.
Pull request overview
Migrates the legacy battle reducer to a Redux Toolkit slice and introduces middleware to translate existing @@Response/kcsapi/* actions into slice actions, with Jest tests added for core battle/result behaviors.
Changes:
- Replaced the legacy
views/redux/battle.esreducer with an RTK slice inviews/redux/battle.ts. - Added
views/redux/middlewares/battle-slice.tsand wired it into the Redux store. - Added Jest tests covering map/battle/result flows and a minimal
poi-lib-battleTS shim.
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| views/redux/reducer-factory.es | Switches battle reducer wiring to battleSlice.reducer. |
| views/redux/create-store.es | Adds battleSliceMiddleware to the store middleware chain. |
| views/redux/middlewares/battle-slice.ts | Bridges legacy @@Response/kcsapi/* actions into RTK slice actions. |
| views/redux/battle.ts | New RTK slice implementation + dispatchBattleResult subscriber helper. |
| views/redux/battle.es | Removes legacy battle reducer implementation. |
| views/redux/tests/battle.spec.ts | Adds tests for battle slice + middleware + dispatchBattleResult behavior. |
| shims/vendor/poi-lib-battle.d.ts | Adds minimal typings shim for poi-lib-battle to keep TS strict in tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| case '@@Response/kcsapi/api_req_map/start': | ||
| store.dispatch( | ||
| battleActions.mapStart({ | ||
| body: a.body, | ||
| postBody: a.postBody, | ||
| }), | ||
| ) |
There was a problem hiding this comment.
battleSliceMiddleware reads body/postBody from top-level fields (a.body/a.postBody). Actions created via RTK createAction (used widely elsewhere in the repo) typically carry these on action.payload instead; in that case this dispatch would pass undefined postBody and battleSlice.mapStart will throw when accessing postBody.api_deck_id. Consider normalizing by reading from action.payload (with fallback to top-level) before dispatching slice actions.
| store.dispatch( | ||
| battleActions.battle({ | ||
| body: a.body, | ||
| path: a.path, | ||
| time: a.time, | ||
| rootState: store.getState() as unknown, | ||
| }), |
There was a problem hiding this comment.
Same as mapStart: battle dispatch uses a.body/a.path/a.time, but RTK action creators provide these under action.payload. Consider pulling body/path/time from action.payload when present (or switching the middleware to exclusively use payload), so the bridge works for both onGameResponse actions and createAction-produced actions.
| it('dispatches @@BattleResult and a browser event for valid result', () => { | ||
| const dispatch = jest.fn() | ||
|
|
||
| const win = { dispatchEvent: jest.fn() } | ||
| // @ts-expect-error jest env is not a browser; provide minimal window stub | ||
| globalThis.window = win | ||
|
|
||
| const result = { | ||
| valid: true, | ||
| deckShipId: [1], | ||
| deckHp: [1], | ||
| deckInitHp: [2], | ||
| enemyShipId: [501], | ||
| enemyHp: [10], | ||
| } | ||
|
|
||
| dispatchBattleResult(dispatch, result) |
There was a problem hiding this comment.
This test suite relies on CustomEvent being present in the Jest runtime when calling dispatchBattleResult, but it only stubs globalThis.window. To make the test deterministic across environments, consider stubbing/polyfilling globalThis.CustomEvent here (and restoring any previous window/CustomEvent after the test).
What
views/redux/battle.esreducer logic to an RTK slice inviews/redux/battle.ts.views/redux/middlewares/battle-slice.tsto bridge existing@@Response/kcsapi/*actions into the slice reducers.Why
Notes
poi-lib-battled.ts shim to keep TS strict without pulling runtime code into tests.