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);
  });
});
