import { describe, expect, it } from 'vitest'
import { ALL_ROLES, DEFAULT_PRESETS, sortedRoles, roleImageUrl, backImageUrl } from '../logic/roles'

describe('ALL_ROLES', () => {
  it('contains all 30 partiverse roles', () => {
    expect(ALL_ROLES).toHaveLength(30)
    // 几个 spot check
    expect(ALL_ROLES).toContain('狼人')
    expect(ALL_ROLES).toContain('女巫')
    expect(ALL_ROLES).toContain('村民')
    expect(ALL_ROLES).toContain('预言家')
  })

  it('has unique role names', () => {
    expect(new Set(ALL_ROLES).size).toBe(ALL_ROLES.length)
  })
})

describe('DEFAULT_PRESETS', () => {
  it.each([8, 9, 10, 12])('preset for %d players sums to player count', (count) => {
    const preset = DEFAULT_PRESETS[count]
    const sum = Object.values(preset).reduce((a, b) => a + b, 0)
    expect(sum).toBe(count)
  })

  it('only references roles from ALL_ROLES', () => {
    for (const preset of Object.values(DEFAULT_PRESETS)) {
      for (const role of Object.keys(preset)) {
        expect(ALL_ROLES).toContain(role)
      }
    }
  })
})

describe('sortedRoles', () => {
  it('puts preset-frequent roles first', () => {
    const sorted = sortedRoles()
    // 狼人/女巫/猎人/守卫/骑士/白狼王 都出现在所有 preset 里，应排在最前
    const top6 = sorted.slice(0, 6)
    for (const r of ['狼人', '女巫', '猎人', '守卫', '骑士', '白狼王']) {
      expect(top6).toContain(r)
    }
  })

  it('returns a permutation of ALL_ROLES', () => {
    expect(sortedRoles().sort()).toEqual([...ALL_ROLES].sort())
  })
})

describe('roleImageUrl', () => {
  it('encodes Chinese role names', () => {
    expect(roleImageUrl('狼人')).toBe(`/werewolf/roles/${encodeURIComponent('狼人')}.JPG`)
  })
})

describe('backImageUrl', () => {
  it('returns the back image path', () => {
    expect(backImageUrl()).toBe('/werewolf/back.jpg')
  })
})
