Files
scrabble-game/ui/src/lib/chatlimit.test.ts
T
Ilia Denisov 02681ae9e0
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 12s
CI / ui (pull_request) Successful in 45s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m8s
feat(chat): limit in-game chat to one message per turn
Enforce one chat message per turn on both ends. The backend rejects a
second message in the same turn (ErrChatAlreadySentThisTurn -> 409
chat_already_sent), keyed on the move-driven turn start (turn_started_at).
The UI derives "already wrote this turn" from the message list against
GameView.lastActivityUnix (no counter, survives reopening, resets on turn
change), hides the field behind a short caption once the limit is reached,
and now reloads the game state on turn/game-state events so the toggle and
the limit follow the live game. Enter is gated on !busy to avoid a
double-send in the in-flight window.

Backend: new game.TurnStartedAt; social GameReader gains it; PostMessage
enforces the limit reusing lastMessageAt. UI: new lib/chatlimit.ts pure
logic + unit tests; Chat/ChatScreen wiring; chat.sentThisTurn and
error.chat_already_sent i18n (en/ru); extended chat e2e. Docs: FUNCTIONAL
(+ru), ARCHITECTURE, UI_DESIGN.
2026-06-14 20:18:58 +02:00

42 lines
1.6 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { canSendChat, sentThisTurn, turnMessageLimit } from './chatlimit';
import type { ChatMessage } from './model';
function msg(senderId: string, kind: string, createdAtUnix: number): ChatMessage {
return { id: `${senderId}-${kind}-${createdAtUnix}`, gameId: 'g1', senderId, kind, body: kind === 'nudge' ? '' : 'hi', createdAtUnix };
}
const ME = 'me';
const TURN_START = 1000;
describe('turnMessageLimit', () => {
it('allows one message on your turn and none on the opponent\'s', () => {
expect(turnMessageLimit(true)).toBe(1);
expect(turnMessageLimit(false)).toBe(0);
});
});
describe('sentThisTurn', () => {
it('counts my messages posted at or after the turn start', () => {
const messages = [msg(ME, 'message', TURN_START), msg(ME, 'message', TURN_START + 5)];
expect(sentThisTurn(messages, ME, TURN_START)).toBe(2);
});
it('ignores my messages from a previous turn', () => {
expect(sentThisTurn([msg(ME, 'message', TURN_START - 1)], ME, TURN_START)).toBe(0);
});
it("ignores other players' messages and my own nudges", () => {
const messages = [msg('ann', 'message', TURN_START + 1), msg(ME, 'nudge', TURN_START + 2)];
expect(sentThisTurn(messages, ME, TURN_START)).toBe(0);
});
});
describe('canSendChat', () => {
it("is false on the opponent's turn", () => {
expect(canSendChat(false, 0)).toBe(false);
});
it('is true on your turn before sending and false after one message', () => {
expect(canSendChat(true, 0)).toBe(true);
expect(canSendChat(true, 1)).toBe(false);
});
});