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

test.group('File.toJSON()', (group) => {
  let file: File
  let base: IFile

  group.each.setup(() => {
    file = new File()
    base = {
      kind: 'test-kind',
      info: {
        kind: ThingKind,
        name: 'hello',
        description: 'a desc',
      },
      key: '123',
      parents: ['p1'],
      permissionIds: ['pr1'],
      permissions: [
        {
          addingUser: '123',
          key: '123',
          kind: 'Core#Permission',
          granteeId: '345',
          role: 'commenter',
          type: 'organization',
          itemId: '678',
          depth: 0,
          sourceRule: 'creator_default_owner',
        },
      ],
      lastModified: { byMe: false, time: 1, user: 'u1', name: 'mod-test' },
    }
    file.new({ ...base })
  })

  test('sets the key', ({ assert }) => {
    const result = file.toJSON()
    assert.equal(result.key, base.key)
  })

  test('sets the kind', ({ assert }) => {
    const result = file.toJSON()
    assert.equal(result.kind, base.kind)
  })

  test('sets the info', ({ assert }) => {
    const result = file.toJSON()
    assert.deepEqual(result.info, base.info)
  })

  test('sets the parents', ({ assert }) => {
    const result = file.toJSON()
    assert.deepEqual(result.parents, base.parents)
  })

  test('sets the permissionIds', ({ assert }) => {
    const result = file.toJSON()
    assert.deepEqual(result.permissionIds, base.permissionIds)
  })

  test('sets the lastModified', ({ assert }) => {
    const result = file.toJSON()
    assert.deepEqual(result.lastModified, base.lastModified)
  })

  test('sets the deleted and deletedInfo', ({ assert }) => {
    const iFile = { ...base }
    iFile.deleted = true
    iFile.deletedInfo = { byMe: false, time: 2, user: 'u2', name: 'delete-test' }
    file.new({ ...iFile })
    const result = file.toJSON()
    assert.isTrue(result.deleted)
    assert.deepEqual(result.deletedInfo, iFile.deletedInfo)
  })

  test('does not set the deleted and deletedInfo by default', ({ assert }) => {
    const result = file.toJSON()
    assert.isUndefined(result.deleted)
    assert.isUndefined(result.deletedInfo)
  })

  test('sets the labels', ({ assert }) => {
    const iFile = { ...base }
    iFile.labels = ['l1']
    file.new({ ...iFile })
    const result = file.toJSON()
    assert.deepEqual(result.labels, iFile.labels)
  })

  test('does not set the labels by default', ({ assert }) => {
    const result = file.toJSON()
    assert.isUndefined(result.labels)
  })

  test('sets the iconColor', ({ assert }) => {
    const iFile = { ...base }
    iFile.iconColor = '#c00'
    file.new({ ...iFile })
    const result = file.toJSON()
    assert.deepEqual(result.iconColor, '#c00')
  })

  test('does not set the iconColor by default', ({ assert }) => {
    const result = file.toJSON()
    assert.isUndefined(result.iconColor)
  })
})
