upgrade packages

This commit is contained in:
seavor 2026-04-15 18:06:39 -05:00
parent c62c336a11
commit ae1bc3da38
30 changed files with 1138 additions and 1783 deletions

File diff suppressed because it is too large Load diff

View file

@ -21,25 +21,25 @@
"@bufbuild/protobuf": "^2.11.0",
"@emotion/react": "^11.8.2",
"@emotion/styled": "^11.8.1",
"@mui/icons-material": "^7.3.10",
"@mui/material": "^7.3.10",
"@mui/icons-material": "^9.0.0",
"@mui/material": "^9.0.0",
"@reduxjs/toolkit": "^2.11.2",
"crypto-js": "^4.2.0",
"dexie": "^4.4.2",
"dompurify": "^3.3.3",
"dompurify": "^3.4.0",
"final-form": "^5.0.0",
"final-form-set-field-touched": "^1.0.1",
"i18next": "^26.0.4",
"i18next": "^26.0.5",
"i18next-browser-languagedetector": "^8.2.1",
"i18next-icu": "^2.0.3",
"intl-messageformat": "^11.2.1",
"lodash": "^4.17.21",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"react-final-form": "^7.0.0",
"react-final-form-listeners": "^3.0.0",
"react-i18next": "^17.0.2",
"react-i18next": "^17.0.3",
"react-redux": "^9.2.0",
"react-router-dom": "^7.14.1",
"react-virtualized-auto-sizer": "^2.0.3",
@ -47,10 +47,10 @@
"rxjs": "^7.5.4"
},
"devDependencies": {
"@bufbuild/buf": "^1.67.0",
"@bufbuild/buf": "^1.68.1",
"@bufbuild/protoc-gen-es": "^2.11.0",
"@eslint/js": "^10.0.1",
"@mui/types": "^7.1.3",
"@mui/types": "^9.0.0",
"@testing-library/dom": "^10.4.1",
"@testing-library/jest-dom": "^6.4.0",
"@testing-library/react": "^16.3.2",
@ -58,23 +58,22 @@
"@types/lodash": "^4.14.179",
"@types/node": "^22.19.17",
"@types/prop-types": "^15.7.4",
"@types/react": "18.0.24",
"@types/react-dom": "18.0.8",
"@types/react": "^19.0.0",
"@types/react-dom": "^19.0.0",
"@types/react-virtualized-auto-sizer": "^1.0.1",
"@types/react-window": "^1.8.5",
"@typescript-eslint/eslint-plugin": "^8.58.2",
"@typescript-eslint/parser": "^8.58.2",
"@vitejs/plugin-react": "^5.2.0",
"@vitejs/plugin-react": "^6.0.1",
"@vitest/coverage-v8": "^4.1.4",
"eslint": "^10.2.0",
"fs-extra": "^11.3.4",
"globals": "^17.5.0",
"husky": "^9.1.7",
"jsdom": "^29.0.2",
"typescript": "~5.8",
"typescript": "~6.0",
"typescript-eslint": "^8.58.2",
"vite": "^6.4.2",
"vite-tsconfig-paths": "^5.1.4",
"vite": "^8.0.8",
"vitest": "^4.1.4"
},
"browserslist": {

View file

@ -0,0 +1,48 @@
// Shared lifecycle helpers for test files that need to mutate global state.
//
// The root `setupTests.ts` guards catch leaks even when callers forget to
// clean up, but opt-in helpers make intent explicit at the call site and
// avoid piling cleanup logic onto the shared safety net.
/**
* Temporarily override fields on `window.location` and return a restore fn.
*
* `Object.defineProperty(window, 'location', ...)` is not a `vi.spyOn` target,
* so `vi.restoreAllMocks()` will NOT undo it. Always pair with the returned
* `restore` callback (ideally in `afterEach`).
*/
export function withMockLocation(overrides: Partial<Location>): () => void {
const originalDescriptor = Object.getOwnPropertyDescriptor(window, 'location');
Object.defineProperty(window, 'location', {
value: { ...window.location, ...overrides },
writable: true,
configurable: true,
});
return () => {
if (originalDescriptor) {
Object.defineProperty(window, 'location', originalDescriptor);
}
};
}
/**
* Push an entry onto a shared event-handler registry array and return a
* teardown function that removes exactly that entry.
*
* Used by ProtobufService specs which install temporary handlers into the
* (mocked) `GameEvents` / `RoomEvents` / `SessionEvents` arrays. Manual
* `.push()`/`.pop()` inside a test body corrupts the array if an assertion
* throws between them this helper makes the teardown safe to run in
* `afterEach`.
*/
export function withEventRegistry<T>(registry: T[], entry: T): () => void {
registry.push(entry);
return () => {
const index = registry.lastIndexOf(entry);
if (index !== -1) {
registry.splice(index, 1);
}
};
}

View file

@ -0,0 +1 @@
export { withMockLocation, withEventRegistry } from './globalGuards';

View file

@ -1,42 +1,40 @@
import { Component, CElement } from 'react';
import { ReactElement } from 'react';
import Grid from '@mui/material/Grid';
import './ThreePaneLayout.css';
// @DEPRECATED
// This component sucks balls, dont use it. It will be removed sooner than later.
class ThreePaneLayout extends Component<ThreePaneLayoutProps> {
render() {
return (
<div className="three-pane-layout">
<Grid container rowSpacing={0} columnSpacing={2} className="grid">
<Grid size={{ xs: 12, md: 9, lg: 10 }} className="grid-main">
<Grid className={
'grid-main__top'
+ (this.props.fixedHeight ? ' fixedHeight' : '')
}>
{this.props.top}
</Grid>
<Grid className={
'grid-main__bottom'
+ (this.props.fixedHeight ? ' fixedHeight' : '')
}>
{this.props.bottom}
</Grid>
function ThreePaneLayout(props: ThreePaneLayoutProps) {
return (
<div className="three-pane-layout">
<Grid container rowSpacing={0} columnSpacing={2} className="grid">
<Grid size={{ xs: 12, md: 9, lg: 10 }} className="grid-main">
<Grid className={
'grid-main__top'
+ (props.fixedHeight ? ' fixedHeight' : '')
}>
{props.top}
</Grid>
<Grid size={{ md: 3, lg: 2 }} sx={{ display: { xs: 'none', md: 'block' } }} className="grid-side">
{this.props.side}
<Grid className={
'grid-main__bottom'
+ (props.fixedHeight ? ' fixedHeight' : '')
}>
{props.bottom}
</Grid>
</Grid>
</div>
);
}
<Grid size={{ md: 3, lg: 2 }} sx={{ display: { xs: 'none', md: 'block' } }} className="grid-side">
{props.side}
</Grid>
</Grid>
</div>
);
}
interface ThreePaneLayoutProps {
top: CElement<any, any>,
bottom: CElement<any, any>,
side?: CElement<any, any>,
top: ReactElement,
bottom: ReactElement,
side?: ReactElement,
fixedHeight?: boolean,
}

View file

@ -1,5 +1,5 @@
import * as React from 'react'
import ReactDOM from 'react-dom'
import { createPortal } from 'react-dom'
import Alert from '@mui/material/Alert';
import CheckCircleIcon from '@mui/icons-material/CheckCircle';
@ -46,7 +46,7 @@ function Toast(props) {
return null
}
return ReactDOM.createPortal(
return createPortal(
node,
rootElemRef.current
);

View file

@ -1,4 +1,4 @@
import { Component, Suspense } from 'react';
import { Suspense, useEffect } from 'react';
import { Provider } from 'react-redux';
import { MemoryRouter as Router } from 'react-router-dom';
import CssBaseline from '@mui/material/CssBaseline';
@ -10,33 +10,31 @@ import './AppShell.css';
import { ToastProvider } from '@app/components'
class AppShell extends Component {
componentDidMount() {
function AppShell() {
useEffect(() => {
// @TODO (1)
window.onbeforeunload = () => true;
}
}, []);
handleContextMenu(event) {
const handleContextMenu = (event) => {
event.preventDefault();
}
};
render() {
return (
<Suspense fallback="loading">
<Provider store={store}>
<CssBaseline />
<ToastProvider>
<div className="AppShell" onContextMenu={this.handleContextMenu}>
<Router>
<FeatureDetection />
<Routes />
</Router>
</div>
</ToastProvider>
</Provider>
</Suspense>
);
}
return (
<Suspense fallback="loading">
<Provider store={store}>
<CssBaseline />
<ToastProvider>
<div className="AppShell" onContextMenu={handleContextMenu}>
<Router>
<FeatureDetection />
<Routes />
</Router>
</div>
</ToastProvider>
</Provider>
</Suspense>
);
}
export default AppShell;

View file

@ -1,20 +1,15 @@
// eslint-disable-next-line
import React, { Component } from "react";
import { AuthGuard } from '@app/components';
import Layout from '../Layout/Layout';
import './Decks.css';
class Decks extends Component {
render() {
return (
<Layout>
<AuthGuard />
<span>"Decks"</span>
</Layout>
)
}
function Decks() {
return (
<Layout>
<AuthGuard />
<span>"Decks"</span>
</Layout>
);
}
export default Decks;

View file

@ -1,20 +1,15 @@
// eslint-disable-next-line
import React, { Component } from "react";
import { AuthGuard } from '@app/components';
import Layout from '../Layout/Layout';
import './Game.css';
class Game extends Component {
render() {
return (
<Layout>
<AuthGuard />
<span>"Game"</span>
</Layout>
)
}
function Game() {
return (
<Layout>
<AuthGuard />
<span>"Game"</span>
</Layout>
);
}
export default Game;

View file

@ -5,7 +5,7 @@ import Menu from '@mui/material/Menu';
import MenuItem from '@mui/material/MenuItem';
import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown';
import CloseIcon from '@mui/icons-material/Close';
import MailOutlineRoundedIcon from '@mui/icons-material/MailOutline';
import MailOutlineRoundedIcon from '@mui/icons-material/MailOutlineRounded';
import MenuRoundedIcon from '@mui/icons-material/MenuRounded';
import { AuthenticationService, RoomsService } from '@app/api';
@ -56,7 +56,7 @@ const LeftNav = () => {
}
const handleMenuItemClick = (option: string) => {
const route = RouteEnum[option.toUpperCase()];
const route = App.RouteEnum[option.toUpperCase()];
navigate(generatePath(route));
}
@ -149,10 +149,12 @@ const LeftNav = () => {
keepMounted
open={!!state.anchorEl}
onClose={() => handleMenuClose()}
PaperProps={{
style: {
marginTop: '32px',
width: '20ch',
slotProps={{
paper: {
style: {
marginTop: '32px',
width: '20ch',
},
},
}}
>

View file

@ -1,18 +1,14 @@
// eslint-disable-next-line
import React, { Component } from "react";
import Layout from '../Layout/Layout';
import { AuthGuard } from '@app/components';
class Player extends Component {
render() {
return (
<Layout>
<AuthGuard />
<span>"Player"</span>
</Layout>
)
}
function Player() {
return (
<Layout>
<AuthGuard />
<span>"Player"</span>
</Layout>
);
}
export default Player;

View file

@ -4,7 +4,7 @@ File is adapted from https://github.com/Qeepsake/use-redux-effect under MIT Lice
* @description
*/
import { useRef, useEffect, DependencyList } from 'react'
import { useEffect, useRef, DependencyList } from 'react'
import { useStore } from 'react-redux'
import { castArray } from 'lodash'
@ -14,36 +14,44 @@ import { castArray } from 'lodash'
export type ReduxEffect = (action: any) => void
/**
* Subscribes to redux store events
*
* @param effect
* @param type
* @param deps
*/
* Subscribes to redux store events.
*
* On mount, synchronously inspects the current `state.action` so an action
* dispatched between render and effect-commit is still observed this is
* what lets `<Server />` catch a `JOIN_ROOM` that auto-join fired while the
* route was transitioning.
*/
export function useReduxEffect(
effect: ReduxEffect,
type: string | string[],
deps: DependencyList = [],
): void {
const currentValue = useRef(null);
const store = useStore();
const effectRef = useRef(effect);
const typeRef = useRef(type);
// Persists across StrictMode's mount → unmount → remount cycle so we
// don't re-fire for an action we already handled on the first mount.
const lastHandledCountRef = useRef<number>(-1);
const handleChange = (): void => {
const state: any = store.getState();
const action = state.action;
const previousValue = currentValue.current;
currentValue.current = action.count;
if (
previousValue !== action.count &&
castArray(type).includes(action.type)
) {
effect(action);
}
}
effectRef.current = effect;
typeRef.current = type;
useEffect(() => {
const unsubscribe = store.subscribe(handleChange);
const check = (): void => {
const action = (store.getState() as any).action;
if (!action || action.count === lastHandledCountRef.current) {
return;
}
lastHandledCountRef.current = action.count;
if (castArray(typeRef.current).includes(action.type)) {
effectRef.current(action);
}
};
check();
const unsubscribe = store.subscribe(check);
return (): void => unsubscribe();
}, deps)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps);
}

View file

@ -7,12 +7,12 @@ class I18nBackend {
static BASE_URL = `${import.meta.env.BASE_URL}locales`;
read(language, namespace, callback) {
if (!language[App.Language]) {
if (!language[App.Language as unknown as string]) {
callback(true, null);
return;
}
fetch(`${I18nBackend.BASE_URL}/${language[App.Language]}/${namespace}.json`)
fetch(`${I18nBackend.BASE_URL}/${language[App.Language as unknown as string]}/${namespace}.json`)
.then(resp => resp.json().then(json => callback(null, json)))
.catch(error => callback(error, null));
}

View file

@ -33,8 +33,30 @@ import '@testing-library/jest-dom/vitest';
// `mockImplementation`, it should set it in that test's body and rely on
// the next test overwriting or the global `clearAllMocks` clearing calls —
// it should NOT assume the mock is reset to its factory default automatically.
//
// Global snapshot/restore guards for non-`vi.spyOn` globals that tests mutate
// directly. `vi.restoreAllMocks()` only restores `vi.spyOn` targets, so bare
// `Object.defineProperty` writes on `window.location` and `globalThis.WebSocket`
// reassignments leak between tests unless we explicitly capture and restore them.
let _locationDescriptor: PropertyDescriptor | undefined;
let _originalWebSocket: typeof globalThis.WebSocket | undefined;
beforeEach(() => {
_locationDescriptor = Object.getOwnPropertyDescriptor(window, 'location');
_originalWebSocket = globalThis.WebSocket;
});
afterEach(() => {
vi.clearAllMocks();
vi.restoreAllMocks();
vi.useRealTimers();
const currentLocationDescriptor = Object.getOwnPropertyDescriptor(window, 'location');
if (currentLocationDescriptor !== _locationDescriptor && _locationDescriptor) {
Object.defineProperty(window, 'location', _locationDescriptor);
}
if (globalThis.WebSocket !== _originalWebSocket) {
globalThis.WebSocket = _originalWebSocket as typeof globalThis.WebSocket;
}
});

View file

@ -1,8 +1,11 @@
vi.mock('../store', () => ({ store: { dispatch: vi.fn() } }));
// Use `vi.hoisted` so the mocked `store.dispatch` reference stays stable across
// re-runs of the factory under `isolate: false`. See rooms.dispatch.spec.ts for
// the same pattern and rationale.
const { mockDispatch } = vi.hoisted(() => ({ mockDispatch: vi.fn() }));
vi.mock('../store', () => ({ store: { dispatch: mockDispatch } }));
import { create } from '@bufbuild/protobuf';
import { Data } from '@app/types';
import { store } from '..';
import { Actions } from './game.actions';
import { Dispatch } from './game.dispatch';
import {
@ -12,31 +15,35 @@ import {
makePlayerProperties,
} from './__mocks__/fixtures';
beforeEach(() => {
mockDispatch.mockClear();
});
describe('Dispatch', () => {
it('clearStore dispatches Actions.clearStore()', () => {
Dispatch.clearStore();
expect(store.dispatch).toHaveBeenCalledWith(Actions.clearStore());
expect(mockDispatch).toHaveBeenCalledWith(Actions.clearStore());
});
it('gameJoined dispatches Actions.gameJoined()', () => {
const data = create(Data.Event_GameJoinedSchema, { hostId: 1, playerId: 2 });
Dispatch.gameJoined(data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.gameJoined(data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.gameJoined(data));
});
it('gameLeft dispatches Actions.gameLeft()', () => {
Dispatch.gameLeft(2);
expect(store.dispatch).toHaveBeenCalledWith(Actions.gameLeft(2));
expect(mockDispatch).toHaveBeenCalledWith(Actions.gameLeft(2));
});
it('gameClosed dispatches Actions.gameClosed()', () => {
Dispatch.gameClosed(3);
expect(store.dispatch).toHaveBeenCalledWith(Actions.gameClosed(3));
expect(mockDispatch).toHaveBeenCalledWith(Actions.gameClosed(3));
});
it('gameHostChanged dispatches Actions.gameHostChanged()', () => {
Dispatch.gameHostChanged(1, 7);
expect(store.dispatch).toHaveBeenCalledWith(Actions.gameHostChanged(1, 7));
expect(mockDispatch).toHaveBeenCalledWith(Actions.gameHostChanged(1, 7));
});
it('gameStateChanged dispatches Actions.gameStateChanged()', () => {
@ -44,156 +51,156 @@ describe('Dispatch', () => {
playerList: [], gameStarted: false, activePlayerId: 0, activePhase: 0, secondsElapsed: 0
});
Dispatch.gameStateChanged(1, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.gameStateChanged(1, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.gameStateChanged(1, data));
});
it('playerJoined dispatches Actions.playerJoined()', () => {
const props = makePlayerProperties();
Dispatch.playerJoined(1, props);
expect(store.dispatch).toHaveBeenCalledWith(Actions.playerJoined(1, props));
expect(mockDispatch).toHaveBeenCalledWith(Actions.playerJoined(1, props));
});
it('playerLeft dispatches Actions.playerLeft()', () => {
Dispatch.playerLeft(1, 2, 3);
expect(store.dispatch).toHaveBeenCalledWith(Actions.playerLeft(1, 2, 3));
expect(mockDispatch).toHaveBeenCalledWith(Actions.playerLeft(1, 2, 3));
});
it('playerPropertiesChanged dispatches Actions.playerPropertiesChanged()', () => {
const props = makePlayerProperties();
Dispatch.playerPropertiesChanged(1, 2, props);
expect(store.dispatch).toHaveBeenCalledWith(Actions.playerPropertiesChanged(1, 2, props));
expect(mockDispatch).toHaveBeenCalledWith(Actions.playerPropertiesChanged(1, 2, props));
});
it('kicked dispatches Actions.kicked()', () => {
Dispatch.kicked(1);
expect(store.dispatch).toHaveBeenCalledWith(Actions.kicked(1));
expect(mockDispatch).toHaveBeenCalledWith(Actions.kicked(1));
});
it('cardMoved dispatches Actions.cardMoved()', () => {
const data = create(Data.Event_MoveCardSchema, { cardId: 1 });
Dispatch.cardMoved(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardMoved(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.cardMoved(1, 2, data));
});
it('cardFlipped dispatches Actions.cardFlipped()', () => {
const data = create(Data.Event_FlipCardSchema, { cardId: 1 });
Dispatch.cardFlipped(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardFlipped(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.cardFlipped(1, 2, data));
});
it('cardDestroyed dispatches Actions.cardDestroyed()', () => {
const data = create(Data.Event_DestroyCardSchema, { cardId: 1 });
Dispatch.cardDestroyed(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardDestroyed(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.cardDestroyed(1, 2, data));
});
it('cardAttached dispatches Actions.cardAttached()', () => {
const data = create(Data.Event_AttachCardSchema, { cardId: 1 });
Dispatch.cardAttached(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardAttached(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.cardAttached(1, 2, data));
});
it('tokenCreated dispatches Actions.tokenCreated()', () => {
const data = create(Data.Event_CreateTokenSchema, { cardId: 1 });
Dispatch.tokenCreated(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.tokenCreated(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.tokenCreated(1, 2, data));
});
it('cardAttrChanged dispatches Actions.cardAttrChanged()', () => {
const data = create(Data.Event_SetCardAttrSchema, { cardId: 1 });
Dispatch.cardAttrChanged(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardAttrChanged(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.cardAttrChanged(1, 2, data));
});
it('cardCounterChanged dispatches Actions.cardCounterChanged()', () => {
const data = create(Data.Event_SetCardCounterSchema, { cardId: 1 });
Dispatch.cardCounterChanged(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardCounterChanged(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.cardCounterChanged(1, 2, data));
});
it('arrowCreated dispatches Actions.arrowCreated()', () => {
const data = create(Data.Event_CreateArrowSchema, { arrowInfo: makeArrow() });
Dispatch.arrowCreated(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.arrowCreated(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.arrowCreated(1, 2, data));
});
it('arrowDeleted dispatches Actions.arrowDeleted()', () => {
const data = create(Data.Event_DeleteArrowSchema, { arrowId: 3 });
Dispatch.arrowDeleted(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.arrowDeleted(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.arrowDeleted(1, 2, data));
});
it('counterCreated dispatches Actions.counterCreated()', () => {
const data = create(Data.Event_CreateCounterSchema, { counterInfo: makeCounter() });
Dispatch.counterCreated(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.counterCreated(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.counterCreated(1, 2, data));
});
it('counterSet dispatches Actions.counterSet()', () => {
const data = create(Data.Event_SetCounterSchema, { counterId: 1, value: 10 });
Dispatch.counterSet(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.counterSet(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.counterSet(1, 2, data));
});
it('counterDeleted dispatches Actions.counterDeleted()', () => {
const data = create(Data.Event_DelCounterSchema, { counterId: 1 });
Dispatch.counterDeleted(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.counterDeleted(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.counterDeleted(1, 2, data));
});
it('cardsDrawn dispatches Actions.cardsDrawn()', () => {
const data = create(Data.Event_DrawCardsSchema, { number: 2, cards: [makeCard()] });
Dispatch.cardsDrawn(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardsDrawn(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.cardsDrawn(1, 2, data));
});
it('cardsRevealed dispatches Actions.cardsRevealed()', () => {
const data = create(Data.Event_RevealCardsSchema, { zoneName: 'hand', cards: [] });
Dispatch.cardsRevealed(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardsRevealed(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.cardsRevealed(1, 2, data));
});
it('zoneShuffled dispatches Actions.zoneShuffled()', () => {
const data = create(Data.Event_ShuffleSchema, { zoneName: 'deck', start: 0, end: 39 });
Dispatch.zoneShuffled(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.zoneShuffled(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.zoneShuffled(1, 2, data));
});
it('dieRolled dispatches Actions.dieRolled()', () => {
const data = create(Data.Event_RollDieSchema, { sides: 6, value: 4, values: [4] });
Dispatch.dieRolled(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.dieRolled(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.dieRolled(1, 2, data));
});
it('activePlayerSet dispatches Actions.activePlayerSet()', () => {
Dispatch.activePlayerSet(1, 3);
expect(store.dispatch).toHaveBeenCalledWith(Actions.activePlayerSet(1, 3));
expect(mockDispatch).toHaveBeenCalledWith(Actions.activePlayerSet(1, 3));
});
it('activePhaseSet dispatches Actions.activePhaseSet()', () => {
Dispatch.activePhaseSet(1, 2);
expect(store.dispatch).toHaveBeenCalledWith(Actions.activePhaseSet(1, 2));
expect(mockDispatch).toHaveBeenCalledWith(Actions.activePhaseSet(1, 2));
});
it('turnReversed dispatches Actions.turnReversed()', () => {
Dispatch.turnReversed(1, true);
expect(store.dispatch).toHaveBeenCalledWith(Actions.turnReversed(1, true));
expect(mockDispatch).toHaveBeenCalledWith(Actions.turnReversed(1, true));
});
it('zoneDumped dispatches Actions.zoneDumped()', () => {
const data = create(Data.Event_DumpZoneSchema, { zoneOwnerId: 1, zoneName: 'hand', numberCards: 3, isReversed: false });
Dispatch.zoneDumped(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.zoneDumped(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.zoneDumped(1, 2, data));
});
it('zonePropertiesChanged dispatches Actions.zonePropertiesChanged()', () => {
const data = create(Data.Event_ChangeZonePropertiesSchema, { zoneName: 'deck', alwaysRevealTopCard: true, alwaysLookAtTopCard: false });
Dispatch.zonePropertiesChanged(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.zonePropertiesChanged(1, 2, data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.zonePropertiesChanged(1, 2, data));
});
it('gameSay dispatches Actions.gameSay()', () => {
Dispatch.gameSay(1, 2, 'gg wp');
expect(store.dispatch).toHaveBeenCalledWith(Actions.gameSay(1, 2, 'gg wp'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.gameSay(1, 2, 'gg wp'));
});
});

View file

@ -898,17 +898,20 @@ describe('2J: Turn, phase, and chat', () => {
it('GAME_SAY → appends message with mocked Date.now() as timeReceived', () => {
const state = makeState();
vi.spyOn(Date, 'now').mockReturnValue(123456789);
const result = gamesReducer(state, {
type: Types.GAME_SAY,
gameId: 1,
playerId: 2,
message: 'gg',
});
vi.restoreAllMocks();
const dateNowSpy = vi.spyOn(Date, 'now').mockReturnValue(123456789);
try {
const result = gamesReducer(state, {
type: Types.GAME_SAY,
gameId: 1,
playerId: 2,
message: 'gg',
});
expect(result.games[1].messages).toHaveLength(1);
expect(result.games[1].messages[0]).toEqual({ playerId: 2, message: 'gg', timeReceived: 123456789 });
expect(result.games[1].messages).toHaveLength(1);
expect(result.games[1].messages[0]).toEqual({ playerId: 2, message: 'gg', timeReceived: 123456789 });
} finally {
dateNowSpy.mockRestore();
}
});
});

View file

@ -55,12 +55,15 @@ export function makeGame(
};
}
export function makeMessage(overrides: Partial<Enriched.Message> = {}): Enriched.Message {
export function makeMessage(overrides: Partial<Omit<Enriched.Message, '$typeName' | '$unknown'>> = {}): Enriched.Message {
const { timeReceived = 0, ...protoOverrides } = overrides;
return {
message: 'hello',
messageType: 0,
timeReceived: 0,
...overrides,
...create(Data.Event_RoomSaySchema, {
message: 'hello',
messageType: 0,
...protoOverrides,
}),
timeReceived,
};
}

View file

@ -1,84 +1,95 @@
vi.mock('..', () => ({ store: { dispatch: vi.fn() } }));
// Use `vi.hoisted` so the mocked `store.dispatch` reference stays stable across
// re-runs of the factory under `isolate: false`. Other dispatch specs mock the
// same `..` path with their own factories; under the shared module graph, the
// cache entry for `..` can flip between competing `vi.fn()` instances. Asserting
// against the hoisted `mockDispatch` directly (rather than reaching through
// `store.dispatch`) decouples the assertions from whatever the module cache
// currently resolves `store` to.
const { mockDispatch } = vi.hoisted(() => ({ mockDispatch: vi.fn() }));
vi.mock('..', () => ({ store: { dispatch: mockDispatch } }));
import { store } from '..';
import { Actions } from './rooms.actions';
import { Dispatch } from './rooms.dispatch';
import { makeGame, makeMessage, makeRoom, makeUser } from './__mocks__/rooms-fixtures';
import { App } from '@app/types';
beforeEach(() => {
mockDispatch.mockClear();
});
describe('Dispatch', () => {
it('clearStore dispatches Actions.clearStore()', () => {
Dispatch.clearStore();
expect(store.dispatch).toHaveBeenCalledWith(Actions.clearStore());
expect(mockDispatch).toHaveBeenCalledWith(Actions.clearStore());
});
it('updateRooms dispatches Actions.updateRooms()', () => {
const rooms = [makeRoom()];
Dispatch.updateRooms(rooms);
expect(store.dispatch).toHaveBeenCalledWith(Actions.updateRooms(rooms));
expect(mockDispatch).toHaveBeenCalledWith(Actions.updateRooms(rooms));
});
it('joinRoom dispatches Actions.joinRoom()', () => {
const roomInfo = makeRoom({ roomId: 2 });
Dispatch.joinRoom(roomInfo);
expect(store.dispatch).toHaveBeenCalledWith(Actions.joinRoom(roomInfo));
expect(mockDispatch).toHaveBeenCalledWith(Actions.joinRoom(roomInfo));
});
it('leaveRoom dispatches Actions.leaveRoom()', () => {
Dispatch.leaveRoom(3);
expect(store.dispatch).toHaveBeenCalledWith(Actions.leaveRoom(3));
expect(mockDispatch).toHaveBeenCalledWith(Actions.leaveRoom(3));
});
it('addMessage with message.name falsy → dispatches only Actions.addMessage()', () => {
const message = { ...makeMessage(), name: undefined };
Dispatch.addMessage(1, message);
expect(store.dispatch).toHaveBeenCalledTimes(1);
expect(store.dispatch).toHaveBeenCalledWith(Actions.addMessage(1, message));
expect(mockDispatch).toHaveBeenCalledTimes(1);
expect(mockDispatch).toHaveBeenCalledWith(Actions.addMessage(1, message));
});
it('addMessage with message.name truthy → dispatches Actions.addMessage()', () => {
const message = { ...makeMessage(), name: 'Alice' };
Dispatch.addMessage(1, message);
expect(store.dispatch).toHaveBeenCalledTimes(1);
expect(store.dispatch).toHaveBeenCalledWith(Actions.addMessage(1, message));
expect(mockDispatch).toHaveBeenCalledTimes(1);
expect(mockDispatch).toHaveBeenCalledWith(Actions.addMessage(1, message));
});
it('updateGames dispatches Actions.updateGames()', () => {
const games = [makeGame()];
Dispatch.updateGames(1, games);
expect(store.dispatch).toHaveBeenCalledWith(Actions.updateGames(1, games));
expect(mockDispatch).toHaveBeenCalledWith(Actions.updateGames(1, games));
});
it('userJoined dispatches Actions.userJoined()', () => {
const user = makeUser();
Dispatch.userJoined(1, user);
expect(store.dispatch).toHaveBeenCalledWith(Actions.userJoined(1, user));
expect(mockDispatch).toHaveBeenCalledWith(Actions.userJoined(1, user));
});
it('userLeft dispatches Actions.userLeft()', () => {
Dispatch.userLeft(1, 'Alice');
expect(store.dispatch).toHaveBeenCalledWith(Actions.userLeft(1, 'Alice'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.userLeft(1, 'Alice'));
});
it('sortGames dispatches Actions.sortGames()', () => {
Dispatch.sortGames(1, App.GameSortField.START_TIME, App.SortDirection.ASC);
expect(store.dispatch).toHaveBeenCalledWith(
expect(mockDispatch).toHaveBeenCalledWith(
Actions.sortGames(1, App.GameSortField.START_TIME, App.SortDirection.ASC)
);
});
it('removeMessages dispatches Actions.removeMessages()', () => {
Dispatch.removeMessages(1, 'Alice', 5);
expect(store.dispatch).toHaveBeenCalledWith(Actions.removeMessages(1, 'Alice', 5));
expect(mockDispatch).toHaveBeenCalledWith(Actions.removeMessages(1, 'Alice', 5));
});
it('gameCreated dispatches Actions.gameCreated()', () => {
Dispatch.gameCreated(2);
expect(store.dispatch).toHaveBeenCalledWith(Actions.gameCreated(2));
expect(mockDispatch).toHaveBeenCalledWith(Actions.gameCreated(2));
});
it('joinedGame dispatches Actions.joinedGame()', () => {
Dispatch.joinedGame(1, 5);
expect(store.dispatch).toHaveBeenCalledWith(Actions.joinedGame(1, 5));
expect(mockDispatch).toHaveBeenCalledWith(Actions.joinedGame(1, 5));
});
});

View file

@ -1,6 +1,9 @@
vi.mock('..', () => ({ store: { dispatch: vi.fn() } }));
// Use `vi.hoisted` so the mocked `store.dispatch` reference stays stable across
// re-runs of the factory under `isolate: false`. See rooms.dispatch.spec.ts for
// the same pattern and rationale.
const { mockDispatch } = vi.hoisted(() => ({ mockDispatch: vi.fn() }));
vi.mock('..', () => ({ store: { dispatch: mockDispatch } }));
import { store } from '..';
import { Actions } from './server.actions';
import { Dispatch } from './server.dispatch';
import { App, Data } from '@app/types';
@ -17,378 +20,382 @@ import {
makeWarnListItem,
} from './__mocks__/server-fixtures';
beforeEach(() => {
mockDispatch.mockClear();
});
describe('Dispatch', () => {
it('initialized dispatches Actions.initialized()', () => {
Dispatch.initialized();
expect(store.dispatch).toHaveBeenCalledWith(Actions.initialized());
expect(mockDispatch).toHaveBeenCalledWith(Actions.initialized());
});
it('clearStore dispatches Actions.clearStore()', () => {
Dispatch.clearStore();
expect(store.dispatch).toHaveBeenCalledWith(Actions.clearStore());
expect(mockDispatch).toHaveBeenCalledWith(Actions.clearStore());
});
it('connectionAttempted dispatches Actions.connectionAttempted()', () => {
Dispatch.connectionAttempted();
expect(store.dispatch).toHaveBeenCalledWith(Actions.connectionAttempted());
expect(mockDispatch).toHaveBeenCalledWith(Actions.connectionAttempted());
});
it('loginSuccessful dispatches Actions.loginSuccessful()', () => {
const options = makeLoginSuccessContext();
Dispatch.loginSuccessful(options);
expect(store.dispatch).toHaveBeenCalledWith(Actions.loginSuccessful(options));
expect(mockDispatch).toHaveBeenCalledWith(Actions.loginSuccessful(options));
});
it('loginFailed dispatches Actions.loginFailed()', () => {
Dispatch.loginFailed();
expect(store.dispatch).toHaveBeenCalledWith(Actions.loginFailed());
expect(mockDispatch).toHaveBeenCalledWith(Actions.loginFailed());
});
it('connectionFailed dispatches Actions.connectionFailed()', () => {
Dispatch.connectionFailed();
expect(store.dispatch).toHaveBeenCalledWith(Actions.connectionFailed());
expect(mockDispatch).toHaveBeenCalledWith(Actions.connectionFailed());
});
it('testConnectionSuccessful dispatches Actions.testConnectionSuccessful()', () => {
Dispatch.testConnectionSuccessful();
expect(store.dispatch).toHaveBeenCalledWith(Actions.testConnectionSuccessful());
expect(mockDispatch).toHaveBeenCalledWith(Actions.testConnectionSuccessful());
});
it('testConnectionFailed dispatches Actions.testConnectionFailed()', () => {
Dispatch.testConnectionFailed();
expect(store.dispatch).toHaveBeenCalledWith(Actions.testConnectionFailed());
expect(mockDispatch).toHaveBeenCalledWith(Actions.testConnectionFailed());
});
it('updateBuddyList dispatches Actions.updateBuddyList()', () => {
const list = [makeUser()];
Dispatch.updateBuddyList(list);
expect(store.dispatch).toHaveBeenCalledWith(Actions.updateBuddyList(list));
expect(mockDispatch).toHaveBeenCalledWith(Actions.updateBuddyList(list));
});
it('addToBuddyList dispatches Actions.addToBuddyList()', () => {
const user = makeUser();
Dispatch.addToBuddyList(user);
expect(store.dispatch).toHaveBeenCalledWith(Actions.addToBuddyList(user));
expect(mockDispatch).toHaveBeenCalledWith(Actions.addToBuddyList(user));
});
it('removeFromBuddyList dispatches Actions.removeFromBuddyList()', () => {
Dispatch.removeFromBuddyList('Alice');
expect(store.dispatch).toHaveBeenCalledWith(Actions.removeFromBuddyList('Alice'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.removeFromBuddyList('Alice'));
});
it('updateIgnoreList dispatches Actions.updateIgnoreList()', () => {
const list = [makeUser()];
Dispatch.updateIgnoreList(list);
expect(store.dispatch).toHaveBeenCalledWith(Actions.updateIgnoreList(list));
expect(mockDispatch).toHaveBeenCalledWith(Actions.updateIgnoreList(list));
});
it('addToIgnoreList dispatches Actions.addToIgnoreList()', () => {
const user = makeUser();
Dispatch.addToIgnoreList(user);
expect(store.dispatch).toHaveBeenCalledWith(Actions.addToIgnoreList(user));
expect(mockDispatch).toHaveBeenCalledWith(Actions.addToIgnoreList(user));
});
it('removeFromIgnoreList dispatches Actions.removeFromIgnoreList()', () => {
Dispatch.removeFromIgnoreList('Bob');
expect(store.dispatch).toHaveBeenCalledWith(Actions.removeFromIgnoreList('Bob'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.removeFromIgnoreList('Bob'));
});
it('updateInfo dispatches Actions.updateInfo({ name, version })', () => {
Dispatch.updateInfo('Servatrice', '2.9');
expect(store.dispatch).toHaveBeenCalledWith(Actions.updateInfo({ name: 'Servatrice', version: '2.9' }));
expect(mockDispatch).toHaveBeenCalledWith(Actions.updateInfo({ name: 'Servatrice', version: '2.9' }));
});
it('updateStatus dispatches Actions.updateStatus({ state, description })', () => {
Dispatch.updateStatus(App.StatusEnum.CONNECTED, 'ok');
expect(store.dispatch).toHaveBeenCalledWith(Actions.updateStatus({ state: App.StatusEnum.CONNECTED, description: 'ok' }));
expect(mockDispatch).toHaveBeenCalledWith(Actions.updateStatus({ state: App.StatusEnum.CONNECTED, description: 'ok' }));
});
it('updateUser dispatches Actions.updateUser()', () => {
const user = makeUser();
Dispatch.updateUser(user);
expect(store.dispatch).toHaveBeenCalledWith(Actions.updateUser(user));
expect(mockDispatch).toHaveBeenCalledWith(Actions.updateUser(user));
});
it('updateUsers dispatches Actions.updateUsers()', () => {
const users = [makeUser()];
Dispatch.updateUsers(users);
expect(store.dispatch).toHaveBeenCalledWith(Actions.updateUsers(users));
expect(mockDispatch).toHaveBeenCalledWith(Actions.updateUsers(users));
});
it('userJoined dispatches Actions.userJoined()', () => {
const user = makeUser();
Dispatch.userJoined(user);
expect(store.dispatch).toHaveBeenCalledWith(Actions.userJoined(user));
expect(mockDispatch).toHaveBeenCalledWith(Actions.userJoined(user));
});
it('userLeft dispatches Actions.userLeft()', () => {
Dispatch.userLeft('Carol');
expect(store.dispatch).toHaveBeenCalledWith(Actions.userLeft('Carol'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.userLeft('Carol'));
});
it('viewLogs dispatches Actions.viewLogs()', () => {
const logs = [create(Data.ServerInfo_ChatMessageSchema, { targetType: 'room' })];
Dispatch.viewLogs(logs);
expect(store.dispatch).toHaveBeenCalledWith(Actions.viewLogs(logs));
expect(mockDispatch).toHaveBeenCalledWith(Actions.viewLogs(logs));
});
it('clearLogs dispatches Actions.clearLogs()', () => {
Dispatch.clearLogs();
expect(store.dispatch).toHaveBeenCalledWith(Actions.clearLogs());
expect(mockDispatch).toHaveBeenCalledWith(Actions.clearLogs());
});
it('serverMessage dispatches Actions.serverMessage()', () => {
Dispatch.serverMessage('Welcome!');
expect(store.dispatch).toHaveBeenCalledWith(Actions.serverMessage('Welcome!'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.serverMessage('Welcome!'));
});
it('registrationRequiresEmail dispatches correctly', () => {
Dispatch.registrationRequiresEmail();
expect(store.dispatch).toHaveBeenCalledWith(Actions.registrationRequiresEmail());
expect(mockDispatch).toHaveBeenCalledWith(Actions.registrationRequiresEmail());
});
it('registrationSuccess dispatches correctly', () => {
Dispatch.registrationSuccess();
expect(store.dispatch).toHaveBeenCalledWith(Actions.registrationSuccess());
expect(mockDispatch).toHaveBeenCalledWith(Actions.registrationSuccess());
});
it('registrationFailed passes reason and endTime to action', () => {
Dispatch.registrationFailed('reason', 999);
expect(store.dispatch).toHaveBeenCalledWith(Actions.registrationFailed('reason', 999));
expect(mockDispatch).toHaveBeenCalledWith(Actions.registrationFailed('reason', 999));
});
it('registrationFailed passes reason only when no endTime', () => {
Dispatch.registrationFailed('plain reason');
expect(store.dispatch).toHaveBeenCalledWith(Actions.registrationFailed('plain reason', undefined));
expect(mockDispatch).toHaveBeenCalledWith(Actions.registrationFailed('plain reason', undefined));
});
it('registrationEmailError dispatches correctly', () => {
Dispatch.registrationEmailError('bad');
expect(store.dispatch).toHaveBeenCalledWith(Actions.registrationEmailError('bad'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.registrationEmailError('bad'));
});
it('registrationPasswordError dispatches correctly', () => {
Dispatch.registrationPasswordError('weak');
expect(store.dispatch).toHaveBeenCalledWith(Actions.registrationPasswordError('weak'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.registrationPasswordError('weak'));
});
it('registrationUserNameError dispatches correctly', () => {
Dispatch.registrationUserNameError('taken');
expect(store.dispatch).toHaveBeenCalledWith(Actions.registrationUserNameError('taken'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.registrationUserNameError('taken'));
});
it('accountAwaitingActivation dispatches correctly', () => {
const options = makePendingActivationContext();
Dispatch.accountAwaitingActivation(options);
expect(store.dispatch).toHaveBeenCalledWith(Actions.accountAwaitingActivation(options));
expect(mockDispatch).toHaveBeenCalledWith(Actions.accountAwaitingActivation(options));
});
it('accountActivationSuccess dispatches correctly', () => {
Dispatch.accountActivationSuccess();
expect(store.dispatch).toHaveBeenCalledWith(Actions.accountActivationSuccess());
expect(mockDispatch).toHaveBeenCalledWith(Actions.accountActivationSuccess());
});
it('accountActivationFailed dispatches correctly', () => {
Dispatch.accountActivationFailed();
expect(store.dispatch).toHaveBeenCalledWith(Actions.accountActivationFailed());
expect(mockDispatch).toHaveBeenCalledWith(Actions.accountActivationFailed());
});
it('resetPassword dispatches correctly', () => {
Dispatch.resetPassword();
expect(store.dispatch).toHaveBeenCalledWith(Actions.resetPassword());
expect(mockDispatch).toHaveBeenCalledWith(Actions.resetPassword());
});
it('resetPasswordFailed dispatches correctly', () => {
Dispatch.resetPasswordFailed();
expect(store.dispatch).toHaveBeenCalledWith(Actions.resetPasswordFailed());
expect(mockDispatch).toHaveBeenCalledWith(Actions.resetPasswordFailed());
});
it('resetPasswordChallenge dispatches correctly', () => {
Dispatch.resetPasswordChallenge();
expect(store.dispatch).toHaveBeenCalledWith(Actions.resetPasswordChallenge());
expect(mockDispatch).toHaveBeenCalledWith(Actions.resetPasswordChallenge());
});
it('resetPasswordSuccess dispatches correctly', () => {
Dispatch.resetPasswordSuccess();
expect(store.dispatch).toHaveBeenCalledWith(Actions.resetPasswordSuccess());
expect(mockDispatch).toHaveBeenCalledWith(Actions.resetPasswordSuccess());
});
it('adjustMod dispatches Actions.adjustMod()', () => {
Dispatch.adjustMod('Dan', true, false);
expect(store.dispatch).toHaveBeenCalledWith(Actions.adjustMod('Dan', true, false));
expect(mockDispatch).toHaveBeenCalledWith(Actions.adjustMod('Dan', true, false));
});
it('reloadConfig dispatches correctly', () => {
Dispatch.reloadConfig();
expect(store.dispatch).toHaveBeenCalledWith(Actions.reloadConfig());
expect(mockDispatch).toHaveBeenCalledWith(Actions.reloadConfig());
});
it('shutdownServer dispatches correctly', () => {
Dispatch.shutdownServer();
expect(store.dispatch).toHaveBeenCalledWith(Actions.shutdownServer());
expect(mockDispatch).toHaveBeenCalledWith(Actions.shutdownServer());
});
it('updateServerMessage dispatches correctly', () => {
Dispatch.updateServerMessage();
expect(store.dispatch).toHaveBeenCalledWith(Actions.updateServerMessage());
expect(mockDispatch).toHaveBeenCalledWith(Actions.updateServerMessage());
});
it('accountPasswordChange dispatches correctly', () => {
Dispatch.accountPasswordChange();
expect(store.dispatch).toHaveBeenCalledWith(Actions.accountPasswordChange());
expect(mockDispatch).toHaveBeenCalledWith(Actions.accountPasswordChange());
});
it('accountEditChanged dispatches correctly', () => {
const user = makeUser();
Dispatch.accountEditChanged(user);
expect(store.dispatch).toHaveBeenCalledWith(Actions.accountEditChanged(user));
expect(mockDispatch).toHaveBeenCalledWith(Actions.accountEditChanged(user));
});
it('accountImageChanged dispatches correctly', () => {
const user = makeUser();
Dispatch.accountImageChanged(user);
expect(store.dispatch).toHaveBeenCalledWith(Actions.accountImageChanged(user));
expect(mockDispatch).toHaveBeenCalledWith(Actions.accountImageChanged(user));
});
it('getUserInfo dispatches correctly', () => {
const userInfo = makeUser({ name: 'Frank' });
Dispatch.getUserInfo(userInfo);
expect(store.dispatch).toHaveBeenCalledWith(Actions.getUserInfo(userInfo));
expect(mockDispatch).toHaveBeenCalledWith(Actions.getUserInfo(userInfo));
});
it('notifyUser dispatches correctly', () => {
const notification = create(Data.Event_NotifyUserSchema, { type: 1, warningReason: '', customTitle: '', customContent: '' });
Dispatch.notifyUser(notification);
expect(store.dispatch).toHaveBeenCalledWith(Actions.notifyUser(notification));
expect(mockDispatch).toHaveBeenCalledWith(Actions.notifyUser(notification));
});
it('serverShutdown dispatches correctly', () => {
const data = create(Data.Event_ServerShutdownSchema, { reason: 'maintenance', minutes: 5 });
Dispatch.serverShutdown(data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.serverShutdown(data));
expect(mockDispatch).toHaveBeenCalledWith(Actions.serverShutdown(data));
});
it('userMessage dispatches correctly', () => {
const messageData = create(Data.Event_UserMessageSchema, { senderName: 'Alice', receiverName: 'Bob', message: 'hey' });
Dispatch.userMessage(messageData);
expect(store.dispatch).toHaveBeenCalledWith(Actions.userMessage(messageData));
expect(mockDispatch).toHaveBeenCalledWith(Actions.userMessage(messageData));
});
it('addToList dispatches correctly', () => {
Dispatch.addToList('buddyList', 'Grace');
expect(store.dispatch).toHaveBeenCalledWith(Actions.addToList('buddyList', 'Grace'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.addToList('buddyList', 'Grace'));
});
it('removeFromList dispatches correctly', () => {
Dispatch.removeFromList('buddyList', 'Hank');
expect(store.dispatch).toHaveBeenCalledWith(Actions.removeFromList('buddyList', 'Hank'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.removeFromList('buddyList', 'Hank'));
});
it('banFromServer dispatches correctly', () => {
Dispatch.banFromServer('Ira');
expect(store.dispatch).toHaveBeenCalledWith(Actions.banFromServer('Ira'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.banFromServer('Ira'));
});
it('banHistory dispatches correctly', () => {
const history = [makeBanHistoryItem()];
Dispatch.banHistory('Ira', history);
expect(store.dispatch).toHaveBeenCalledWith(Actions.banHistory('Ira', history));
expect(mockDispatch).toHaveBeenCalledWith(Actions.banHistory('Ira', history));
});
it('warnHistory dispatches correctly', () => {
const history = [makeWarnHistoryItem()];
Dispatch.warnHistory('Jack', history);
expect(store.dispatch).toHaveBeenCalledWith(Actions.warnHistory('Jack', history));
expect(mockDispatch).toHaveBeenCalledWith(Actions.warnHistory('Jack', history));
});
it('warnListOptions dispatches correctly', () => {
const list = [makeWarnListItem()];
Dispatch.warnListOptions(list);
expect(store.dispatch).toHaveBeenCalledWith(Actions.warnListOptions(list));
expect(mockDispatch).toHaveBeenCalledWith(Actions.warnListOptions(list));
});
it('warnUser dispatches correctly', () => {
Dispatch.warnUser('Kelly');
expect(store.dispatch).toHaveBeenCalledWith(Actions.warnUser('Kelly'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.warnUser('Kelly'));
});
it('grantReplayAccess dispatches correctly', () => {
Dispatch.grantReplayAccess(7, 'Moe');
expect(store.dispatch).toHaveBeenCalledWith(Actions.grantReplayAccess(7, 'Moe'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.grantReplayAccess(7, 'Moe'));
});
it('forceActivateUser dispatches correctly', () => {
Dispatch.forceActivateUser('Ned', 'Moe');
expect(store.dispatch).toHaveBeenCalledWith(Actions.forceActivateUser('Ned', 'Moe'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.forceActivateUser('Ned', 'Moe'));
});
it('getAdminNotes dispatches correctly', () => {
Dispatch.getAdminNotes('Ned', 'notes');
expect(store.dispatch).toHaveBeenCalledWith(Actions.getAdminNotes('Ned', 'notes'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.getAdminNotes('Ned', 'notes'));
});
it('updateAdminNotes dispatches correctly', () => {
Dispatch.updateAdminNotes('Ned', 'updated');
expect(store.dispatch).toHaveBeenCalledWith(Actions.updateAdminNotes('Ned', 'updated'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.updateAdminNotes('Ned', 'updated'));
});
it('replayList dispatches correctly', () => {
const list = [makeReplayMatch()];
Dispatch.replayList(list);
expect(store.dispatch).toHaveBeenCalledWith(Actions.replayList(list));
expect(mockDispatch).toHaveBeenCalledWith(Actions.replayList(list));
});
it('replayAdded dispatches correctly', () => {
const match = makeReplayMatch();
Dispatch.replayAdded(match);
expect(store.dispatch).toHaveBeenCalledWith(Actions.replayAdded(match));
expect(mockDispatch).toHaveBeenCalledWith(Actions.replayAdded(match));
});
it('replayModifyMatch dispatches correctly', () => {
Dispatch.replayModifyMatch(5, true);
expect(store.dispatch).toHaveBeenCalledWith(Actions.replayModifyMatch(5, true));
expect(mockDispatch).toHaveBeenCalledWith(Actions.replayModifyMatch(5, true));
});
it('replayDeleteMatch dispatches correctly', () => {
Dispatch.replayDeleteMatch(5);
expect(store.dispatch).toHaveBeenCalledWith(Actions.replayDeleteMatch(5));
expect(mockDispatch).toHaveBeenCalledWith(Actions.replayDeleteMatch(5));
});
it('backendDecks dispatches correctly', () => {
const deckList = makeDeckList();
Dispatch.backendDecks(deckList);
expect(store.dispatch).toHaveBeenCalledWith(Actions.backendDecks(deckList));
expect(mockDispatch).toHaveBeenCalledWith(Actions.backendDecks(deckList));
});
it('deckNewDir dispatches correctly', () => {
Dispatch.deckNewDir('a/b', 'newFolder');
expect(store.dispatch).toHaveBeenCalledWith(Actions.deckNewDir('a/b', 'newFolder'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.deckNewDir('a/b', 'newFolder'));
});
it('deckDelDir dispatches correctly', () => {
Dispatch.deckDelDir('a/b');
expect(store.dispatch).toHaveBeenCalledWith(Actions.deckDelDir('a/b'));
expect(mockDispatch).toHaveBeenCalledWith(Actions.deckDelDir('a/b'));
});
it('deckUpload dispatches correctly', () => {
const treeItem = makeDeckTreeItem();
Dispatch.deckUpload('a/b', treeItem);
expect(store.dispatch).toHaveBeenCalledWith(Actions.deckUpload('a/b', treeItem));
expect(mockDispatch).toHaveBeenCalledWith(Actions.deckUpload('a/b', treeItem));
});
it('deckDelete dispatches correctly', () => {
Dispatch.deckDelete(42);
expect(store.dispatch).toHaveBeenCalledWith(Actions.deckDelete(42));
expect(mockDispatch).toHaveBeenCalledWith(Actions.deckDelete(42));
});
it('gamesOfUser dispatches correctly', () => {
const response = create(Data.Response_GetGamesOfUserSchema, { roomList: [], gameList: [] });
Dispatch.gamesOfUser('alice', response);
expect(store.dispatch).toHaveBeenCalledWith(Actions.gamesOfUser('alice', response));
expect(mockDispatch).toHaveBeenCalledWith(Actions.gamesOfUser('alice', response));
});
it('clearRegistrationErrors dispatches correctly', () => {
Dispatch.clearRegistrationErrors();
expect(store.dispatch).toHaveBeenCalledWith(Actions.clearRegistrationErrors());
expect(mockDispatch).toHaveBeenCalledWith(Actions.clearRegistrationErrors());
});
});

View file

@ -17,14 +17,14 @@ import {
export function login(options: Omit<Enriched.LoginConnectOptions, 'password'>, password?: string, passwordSalt?: string): void {
const { userName, hashedPassword } = options;
const loginConfig: MessageInitShape<typeof Data.Command_LoginSchema> = {
const loginConfig = {
...CLIENT_CONFIG,
clientid: 'webatrice',
userName,
...(passwordSalt
? { hashedPassword: hashedPassword || hashPassword(passwordSalt, password) }
: { password }),
};
} satisfies MessageInitShape<typeof Data.Command_LoginSchema>;
const onLoginError = (message: string, extra?: () => void) => {
updateStatus(App.StatusEnum.DISCONNECTED, message);

View file

@ -1,6 +1,6 @@
import { GamePersistence } from '../../persistence';
import type { Data, Enriched } from '@app/types';
export function joinGame(data: { playerProperties: Data.ServerInfo_PlayerProperties }, meta: Enriched.GameEventMeta): void {
export function joinGame(data: Data.Event_Join, meta: Enriched.GameEventMeta): void {
GamePersistence.playerJoined(meta.gameId, data.playerProperties);
}

View file

@ -1,6 +1,6 @@
import type { Data, Enriched } from '@app/types';
import { GamePersistence } from '../../persistence';
export function playerPropertiesChanged(data: { playerProperties: Data.ServerInfo_PlayerProperties }, meta: Enriched.GameEventMeta): void {
export function playerPropertiesChanged(data: Data.Event_PlayerPropertiesChanged, meta: Enriched.GameEventMeta): void {
GamePersistence.playerPropertiesChanged(meta.gameId, meta.playerId, data.playerProperties);
}

View file

@ -204,6 +204,9 @@ describe('addToList', () => {
beforeEach(() => {
logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
});
afterEach(() => {
logSpy.mockRestore();
});
it('buddy list → addToBuddyList', () => {
const data = create(Data.Event_AddToListSchema, {

View file

@ -1,5 +1,5 @@
vi.mock('@bufbuild/protobuf', () => ({
create: vi.fn((_schema: unknown, fields?: Record<string, unknown>) => ({ ...(fields ?? {}) })),
vi.mock('@bufbuild/protobuf', async (importOriginal) => ({
...(await importOriginal<typeof import('@bufbuild/protobuf')>()),
fromBinary: vi.fn(),
toBinary: vi.fn().mockReturnValue(new Uint8Array()),
hasExtension: vi.fn().mockReturnValue(false),
@ -7,20 +7,6 @@ vi.mock('@bufbuild/protobuf', () => ({
setExtension: vi.fn(),
}));
vi.mock('../../generated/proto/commands_pb', () => ({
CommandContainerSchema: {},
}));
vi.mock('../../generated/proto/server_message_pb', () => ({
ServerMessageSchema: {},
ServerMessage_MessageType: {
RESPONSE: 1,
ROOM_EVENT: 2,
SESSION_EVENT: 3,
GAME_EVENT_CONTAINER: 4,
},
}));
vi.mock('../events', () => ({
GameEvents: [],
RoomEvents: [],
@ -40,6 +26,7 @@ import { GameEvents, RoomEvents, SessionEvents } from '../events';
import type { GameExtensionRegistry } from '../events/game';
import type { RoomExtensionRegistry } from '../events/room';
import type { SessionExtensionRegistry } from '../events/session';
import { withEventRegistry } from '../../__test-utils__';
import { Data } from '@app/types';
@ -53,12 +40,20 @@ type ProtobufInternal = ProtobufService & {
};
let mockSocket: { isOpen: ReturnType<typeof vi.fn>; send: ReturnType<typeof vi.fn> };
let registryTeardowns: Array<() => void>;
beforeEach(() => {
mockSocket = {
isOpen: vi.fn().mockReturnValue(true),
send: vi.fn(),
};
registryTeardowns = [];
});
afterEach(() => {
while (registryTeardowns.length > 0) {
registryTeardowns.pop()!();
}
});
describe('ProtobufService', () => {
@ -348,8 +343,7 @@ describe('ProtobufService', () => {
const mockExt = {} as GenExtension<Data.GameEvent, unknown>;
const payload = { someData: 1 };
// Temporarily override GameEvents for this test
(GameEvents as GameExtensionRegistry).push([mockExt, handler]);
registryTeardowns.push(withEventRegistry(GameEvents as GameExtensionRegistry, [mockExt, handler]));
vi.mocked(hasExtension).mockReturnValue(true);
vi.mocked(getExtension).mockReturnValue(payload);
@ -359,7 +353,6 @@ describe('ProtobufService', () => {
}, {});
expect(handler).toHaveBeenCalledWith(payload, expect.objectContaining({ gameId: 42, playerId: 5 }));
(GameEvents as GameExtensionRegistry).pop();
});
it('defaults gameId and playerId to -1 when undefined', () => {
@ -368,7 +361,7 @@ describe('ProtobufService', () => {
const mockExt = {} as GenExtension<Data.GameEvent, unknown>;
const payload = { someData: 1 };
(GameEvents as GameExtensionRegistry).push([mockExt, handler]);
registryTeardowns.push(withEventRegistry(GameEvents as GameExtensionRegistry, [mockExt, handler]));
vi.mocked(hasExtension).mockReturnValue(true);
vi.mocked(getExtension).mockReturnValue(payload);
@ -378,7 +371,6 @@ describe('ProtobufService', () => {
});
expect(handler).toHaveBeenCalledWith(payload, expect.objectContaining({ gameId: -1, playerId: -1 }));
(GameEvents as GameExtensionRegistry).pop();
});
});
@ -405,7 +397,7 @@ describe('ProtobufService', () => {
const mockExt = {} as GenExtension<Data.RoomEvent, unknown>;
const payload = { roomData: 1 };
(RoomEvents as RoomExtensionRegistry).push([mockExt, handler]);
registryTeardowns.push(withEventRegistry(RoomEvents as RoomExtensionRegistry, [mockExt, handler]));
vi.mocked(hasExtension).mockReturnValue(true);
vi.mocked(getExtension).mockReturnValue(payload);
@ -413,7 +405,6 @@ describe('ProtobufService', () => {
(service as ProtobufInternal).processRoomEvent(event);
expect(handler).toHaveBeenCalledWith(payload, event);
(RoomEvents as RoomExtensionRegistry).pop();
});
});
@ -431,14 +422,13 @@ describe('ProtobufService', () => {
const mockExt = {} as GenExtension<Data.SessionEvent, unknown>;
const payload = { sessionData: 1 };
(SessionEvents as SessionExtensionRegistry).push([mockExt, handler]);
registryTeardowns.push(withEventRegistry(SessionEvents as SessionExtensionRegistry, [mockExt, handler]));
vi.mocked(hasExtension).mockReturnValue(true);
vi.mocked(getExtension).mockReturnValue(payload);
(service as ProtobufInternal).processSessionEvent({ sessionId: 7 });
expect(handler).toHaveBeenCalledWith(payload);
(SessionEvents as SessionExtensionRegistry).pop();
expect(handler).toHaveBeenCalledWith(payload, undefined);
});
});

View file

@ -175,7 +175,7 @@ export class ProtobufService {
}
for (const [ext, handler] of SessionEvents) {
if (hasExtension(event, ext)) {
handler(getExtension(event, ext));
handler(getExtension(event, ext), undefined);
return;
}
}

View file

@ -1,4 +1,5 @@
import { installMockWebSocket } from '../__mocks__/helpers';
import { withMockLocation } from '../../__test-utils__';
import { Mock } from 'vitest';
vi.mock('../WebClient', () => ({
@ -37,6 +38,7 @@ let MockWS: Mock;
let mockInstance: ReturnType<typeof installMockWebSocket>['mockInstance'];
let restoreWebSocket: ReturnType<typeof installMockWebSocket>['restore'];
let mockConfig: WebSocketServiceConfig;
let locationRestores: Array<() => void>;
beforeEach(() => {
vi.useFakeTimers();
@ -49,9 +51,14 @@ beforeEach(() => {
mockConfig = {
keepAliveFn: vi.fn(),
};
locationRestores = [];
});
afterEach(() => {
while (locationRestores.length > 0) {
locationRestores.pop()!();
}
restoreWebSocket();
vi.useRealTimers();
});
@ -88,22 +95,14 @@ describe('WebSocketService', () => {
describe('connect', () => {
it('creates a WebSocket with wss protocol by default', () => {
const service = new WebSocketService(mockConfig);
Object.defineProperty(window, 'location', {
value: { hostname: 'example.com' },
writable: true,
configurable: true,
});
locationRestores.push(withMockLocation({ hostname: 'example.com' }));
service.connect({ host: 'example.com', port: '8080' });
expect(MockWS).toHaveBeenCalledWith('wss://example.com:8080');
});
it('switches to ws protocol when hostname is localhost', () => {
const service = new WebSocketService(mockConfig);
Object.defineProperty(window, 'location', {
value: { hostname: 'localhost' },
writable: true,
configurable: true,
});
locationRestores.push(withMockLocation({ hostname: 'localhost' }));
service.connect({ host: 'somehost', port: '1234' });
expect(MockWS).toHaveBeenCalledWith('ws://somehost:1234');
});
@ -243,22 +242,14 @@ describe('WebSocketService', () => {
describe('testConnect', () => {
it('creates a test WebSocket with correct URL', () => {
const service = new WebSocketService(mockConfig);
Object.defineProperty(window, 'location', {
value: { hostname: 'example.com' },
writable: true,
configurable: true,
});
locationRestores.push(withMockLocation({ hostname: 'example.com' }));
service.testConnect({ host: 'example.com', port: '9000' });
expect(MockWS).toHaveBeenCalledWith('wss://example.com:9000');
});
it('uses ws protocol on localhost', () => {
const service = new WebSocketService(mockConfig);
Object.defineProperty(window, 'location', {
value: { hostname: 'localhost' },
writable: true,
configurable: true,
});
locationRestores.push(withMockLocation({ hostname: 'localhost' }));
service.testConnect({ host: 'h', port: '1' });
expect(MockWS).toHaveBeenCalledWith('ws://h:1');
});

View file

@ -65,7 +65,7 @@ export class WebSocketService {
}
public send(message: Uint8Array): void {
this.socket.send(message);
this.socket.send(message as unknown as ArrayBufferView);
}
private createWebSocket(url: string): WebSocket {

View file

@ -9,9 +9,11 @@ import { create, getExtension } from '@bufbuild/protobuf';
import { handleResponse } from './command-options';
beforeEach(() => {
vi.resetAllMocks();
});
// NOTE: do NOT call `vi.resetAllMocks()` here — under `isolate: false` it
// resets `vi.fn()` implementations set inside other files' `vi.mock(...)`
// factories, which breaks any spec that relied on those factory defaults
// (e.g. ProtobufService.spec.ts expects `hasExtension` to return `false`).
// The root `setupTests.ts` afterEach already calls `vi.clearAllMocks()`.
describe('handleResponse', () => {
it('calls onResponse and returns early when provided', () => {

View file

@ -1,4 +1,5 @@
vi.mock('../../generated/proto/event_server_identification_pb', () => ({
vi.mock('../../generated/proto/event_server_identification_pb', async (importOriginal) => ({
...(await importOriginal<typeof import('../../generated/proto/event_server_identification_pb')>()),
Event_ServerIdentification_ServerOptions: { SupportsPasswordHash: 2 },
}));

View file

@ -1,9 +1,11 @@
import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
plugins: [react(), tsconfigPaths()],
plugins: [react()],
resolve: {
tsconfigPaths: true,
},
publicDir: 'public',
build: {
outDir: 'build',