import { test } from '@japa/runner'
import { File, IUser } from '../../../../src/index.js'

test.group('File.setDeleted()', () => {
  const user: IUser = {
    key: '123',
    kind: 'Core#User',
    name: 'a1',
    email: [],
    created: 0,
    updated: 0,
  }

  test('sets the time', ({ assert }) => {
    const file = new File()
    file.setDeleted(user)
    assert.isAbove(file.deletedInfo!.time, 0)
  })

  test('sets the user', ({ assert }) => {
    const file = new File()
    file.setDeleted(user)
    assert.equal(file.deletedInfo!.user, user.key)
  })

  test('sets the name', ({ assert }) => {
    const file = new File()
    file.setDeleted(user)
    assert.equal(file.deletedInfo!.name, user.name)
  })

  test('sets the byMe', ({ assert }) => {
    const file = new File()
    file.setDeleted(user)
    assert.isFalse(file.deletedInfo!.byMe)
  })

  test('sets the deleted', ({ assert }) => {
    const file = new File()
    file.setDeleted(user)
    assert.isTrue(file.deleted)
  })

  test('throws when no argument', ({ assert }) => {
    const file = new File()
    assert.throws(() => {
      // @ts-expect-error Undefined for testing
      file.setDeleted(undefined)
    }, 'The user is required.')
  })

  test('throws when invalid user', ({ assert }) => {
    const file = new File()
    assert.throws(() => {
      // @ts-expect-error Invalid type for testing
      file.setDeleted({})
    }, 'Invalid value for the user when setting "deletedInfo".')
  })
})

test.group('File::setDeleted()', () => {
  const user: IUser = {
    key: '123',
    kind: 'Core#User',
    name: 'a1',
    email: [],
    created: 0,
    updated: 0,
  }

  test('sets the time', ({ assert }) => {
    const file = new File().toJSON()
    File.setDeleted(file, user)
    assert.isAbove(file.deletedInfo!.time, 0)
  })

  test('sets the user', ({ assert }) => {
    const file = new File().toJSON()
    File.setDeleted(file, user)
    assert.equal(file.deletedInfo!.user, user.key)
  })

  test('sets the name', ({ assert }) => {
    const file = new File().toJSON()
    File.setDeleted(file, user)
    assert.equal(file.deletedInfo!.name, user.name)
  })

  test('sets the byMe', ({ assert }) => {
    const file = new File().toJSON()
    File.setDeleted(file, user)
    assert.isFalse(file.deletedInfo!.byMe)
  })

  test('sets the deleted', ({ assert }) => {
    const file = new File().toJSON()
    File.setDeleted(file, user)
    assert.isTrue(file.deleted)
  })

  test('throws when no file argument', ({ assert }) => {
    assert.throws(() => {
      // @ts-expect-error Invalid type for testing
      File.setDeleted(undefined, user)
    }, 'The file is required.')
  })

  test('throws when no user argument', ({ assert }) => {
    const file = new File().toJSON()
    assert.throws(() => {
      // @ts-expect-error Invalid type for testing
      File.setDeleted(file, undefined)
    }, 'The user is required.')
  })

  test('throws when invalid user', ({ assert }) => {
    const file = new File().toJSON()
    assert.throws(() => {
      // @ts-expect-error Invalid type for testing
      File.setDeleted(file, {})
    }, 'Invalid value for the user when setting "deletedInfo".')
  })
})
