import { test } from '@japa/runner'
import { User } from '../../../../src/mocking/lib/User.js'
import { Kind as UserKind } from '../../../../src/models/store/User.js'
import type { IUser } from '../../../../src/index.js'

test.group('user()', (group) => {
  let user: User

  group.each.setup(() => {
    user = new User()
  })

  test('returns an object', ({ assert }) => {
    const result = user.user()
    assert.typeOf(result, 'object')
  })

  test('has the {property} property of a type {type}')
    .with([
      { property: 'key', type: 'string' },
      { property: 'name', type: 'string' },
      { property: 'email', type: 'array' },
      { property: 'picture', type: 'object' },
      { property: 'provider', type: 'object' },
    ])
    .run(({ assert }, { property, type }) => {
      const result = user.user()
      assert.typeOf(result[property as keyof IUser], type)
    })

  test('has the kind', ({ assert }) => {
    const result = user.user()
    assert.equal(result.kind, UserKind)
  })

  test('has the email array', ({ assert }) => {
    const result = user.user()
    const [email] = result.email!
    assert.typeOf(email.email, 'string')
    assert.typeOf(email.verified, 'boolean')
  })

  test('does not set email when configured', ({ assert }) => {
    const result = user.user({ noEmail: true })
    assert.isEmpty(result.email)
  })

  test('has the picture object', ({ assert }) => {
    const result = user.user()
    const { url } = result.picture!
    assert.typeOf(url, 'string')
  })

  test('does not set picture when configured', ({ assert }) => {
    const result = user.user({ noPicture: true })
    assert.isUndefined(result.picture)
  })

  test('has the provider object', ({ assert }) => {
    const result = user.user()
    const { refreshToken } = result.provider as { refreshToken: string }
    assert.typeOf(refreshToken, 'string')
  })

  test('does not set provider when configured', ({ assert }) => {
    const result = user.user({ noProvider: true })
    assert.isUndefined(result.provider)
  })
})
