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

test.group('File.setLastModified()', () => {
  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.setLastModified(user)
    assert.isAbove(file.lastModified.time, 0)
  })

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

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

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

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

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

test.group('File::setLastModified()', () => {
  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.setLastModified(file, user)
    assert.isAbove(file.lastModified.time, 0)
  })

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

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

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

  test('throws when no argument', ({ assert }) => {
    assert.throws(() => {
      // @ts-expect-error Undefined for testing
      File.setLastModified(undefined, user)
    })
  })

  test('throws when no user argument', ({ assert }) => {
    const file = new File().toJSON()
    assert.throws(() => {
      // @ts-expect-error Undefined for testing
      File.setLastModified(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.setLastModified(file, {})
    }, 'Invalid value for the user when setting "lastModified".')
  })
})
