import { describe, expect, it } from 'vitest'
import { addHistory, type HistoryEntry } from '../logic/storage'

describe('addHistory', () => {
  it('inserts new entries at the front', () => {
    const h: HistoryEntry[] = [{ playerCount: 8, roles: { 狼人: 2 } }]
    const out = addHistory(h, { playerCount: 9, roles: { 狼人: 3 } })
    expect(out[0]).toEqual({ playerCount: 9, roles: { 狼人: 3 } })
    expect(out).toHaveLength(2)
  })

  it('deduplicates same (playerCount, roles)', () => {
    const h: HistoryEntry[] = [
      { playerCount: 8, roles: { 狼人: 2, 村民: 6 } },
      { playerCount: 9, roles: { 狼人: 3, 村民: 6 } },
    ]
    const out = addHistory(h, { playerCount: 8, roles: { 狼人: 2, 村民: 6 } })
    expect(out).toHaveLength(2)
    expect(out[0]).toEqual({ playerCount: 8, roles: { 狼人: 2, 村民: 6 } })
    // 9 人配置应该还在
    expect(out[1]).toEqual({ playerCount: 9, roles: { 狼人: 3, 村民: 6 } })
  })

  it('distinguishes entries with same playerCount but different role counts', () => {
    const h: HistoryEntry[] = [{ playerCount: 8, roles: { 狼人: 2 } }]
    const out = addHistory(h, { playerCount: 8, roles: { 狼人: 3 } })
    expect(out).toHaveLength(2)
  })

  it('caps history at 50 entries', () => {
    let h: HistoryEntry[] = []
    for (let i = 0; i < 100; i++) {
      h = addHistory(h, { playerCount: 8, roles: { 狼人: i } })
    }
    expect(h).toHaveLength(50)
    // 最新的（i=99）在最前
    expect(h[0]).toEqual({ playerCount: 8, roles: { 狼人: 99 } })
  })
})
