import { describe, it, expect } from 'vitest'
import { parseAccountJson, isValidEmail, isValidToken } from './json-parser'

describe('parseAccountJson', () => {
  it('should parse valid JSON array', () => {
    const json = JSON.stringify([
      { account: 'test@example.com', password: 'pass123', token: 'token123456' },
    ])
    const result = parseAccountJson(json)

    expect(result.success).toBe(true)
    expect(result.data).toHaveLength(1)
    expect(result.data?.[0].account).toBe('test@example.com')
  })

  it('should parse multiple accounts', () => {
    const json = JSON.stringify([
      { account: 'user1@example.com', password: 'pass1', token: 'token1234567890' },
      { account: 'user2@example.com', password: 'pass2', token: 'token0987654321' },
    ])
    const result = parseAccountJson(json)

    expect(result.success).toBe(true)
    expect(result.data).toHaveLength(2)
  })

  it('should reject non-array JSON', () => {
    const json = JSON.stringify({ account: 'test@example.com' })
    const result = parseAccountJson(json)

    expect(result.success).toBe(false)
    expect(result.error).toContain('数组格式')
  })

  it('should reject empty array', () => {
    const json = JSON.stringify([])
    const result = parseAccountJson(json)

    expect(result.success).toBe(false)
    expect(result.error).toContain('不能为空')
  })

  it('should reject invalid JSON', () => {
    const result = parseAccountJson('not valid json')

    expect(result.success).toBe(false)
    expect(result.error).toContain('解析失败')
  })

  it('should reject missing account field', () => {
    const json = JSON.stringify([{ password: 'pass', token: 'token123456' }])
    const result = parseAccountJson(json)

    expect(result.success).toBe(false)
    expect(result.error).toContain('account')
  })

  it('should reject missing password field', () => {
    const json = JSON.stringify([{ account: 'test@example.com', token: 'token123456' }])
    const result = parseAccountJson(json)

    expect(result.success).toBe(false)
    expect(result.error).toContain('password')
  })

  it('should reject missing token field', () => {
    const json = JSON.stringify([{ account: 'test@example.com', password: 'pass' }])
    const result = parseAccountJson(json)

    expect(result.success).toBe(false)
    expect(result.error).toContain('token')
  })

  it('should reject non-string account', () => {
    const json = JSON.stringify([{ account: 123, password: 'pass', token: 'token123456' }])
    const result = parseAccountJson(json)

    expect(result.success).toBe(false)
    expect(result.error).toContain('account')
  })

  it('should reject null items in array', () => {
    const json = JSON.stringify([null])
    const result = parseAccountJson(json)

    expect(result.success).toBe(false)
    expect(result.error).toContain('有效的对象')
  })

  it('should indicate which record has error', () => {
    const json = JSON.stringify([
      { account: 'valid@example.com', password: 'pass', token: 'token123456' },
      { account: 'invalid', password: 'pass' }, // missing token
    ])
    const result = parseAccountJson(json)

    expect(result.success).toBe(false)
    expect(result.error).toContain('第 2 条')
  })
})

describe('isValidEmail', () => {
  it('should accept valid email', () => {
    expect(isValidEmail('test@example.com')).toBe(true)
    expect(isValidEmail('user.name@domain.co.uk')).toBe(true)
  })

  it('should reject invalid email', () => {
    expect(isValidEmail('invalid')).toBe(false)
    expect(isValidEmail('invalid@')).toBe(false)
    expect(isValidEmail('@domain.com')).toBe(false)
    expect(isValidEmail('')).toBe(false)
  })
})

describe('isValidToken', () => {
  it('should accept valid token', () => {
    expect(isValidToken('eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9')).toBe(true)
    expect(isValidToken('1234567890')).toBe(true)
  })

  it('should reject short token', () => {
    expect(isValidToken('short')).toBe(false)
    expect(isValidToken('')).toBe(false)
  })
})
