import { test } from '@japa/runner'
import { Folder, FolderKind, IFolder, ThingKind } from '../../../../src/index.js'

const organization = 'test-organization'

test.group('Folder.fromName()', () => {
  test('sets the kind', ({ assert }) => {
    const result = Folder.fromName('name', organization)
    assert.equal(result.kind, FolderKind)
  })

  test('sets the organization', ({ assert }) => {
    const result = Folder.fromName('name', organization)
    assert.equal(result.organization, organization)
  })

  test('sets the name', ({ assert }) => {
    const result = Folder.fromName('name', organization)
    assert.equal(result.info.name, 'name')
  })

  test('sets the passed owner', ({ assert }) => {
    const result = Folder.fromName('name', organization, 'me')
    assert.equal(result.owner, 'me')
  })
})

test.group('Folder.constructor()', () => {
  test('creates a default Folder', ({ assert }) => {
    const result = new Folder()
    assert.equal(result.kind, FolderKind)
    assert.isUndefined(result.owner)
    assert.typeOf(result.key, 'string')
    assert.typeOf(result.lastModified, 'object')
  })

  test('creates a Folder from the schema values', ({ assert }) => {
    const schema: IFolder = {
      kind: FolderKind,
      organization,
      info: {
        kind: ThingKind,
        name: 'hello',
        description: 'a desc',
      },
      owner: 'me',
      key: '123',
      parents: ['p1'],
      permissionIds: ['pr1'],
      permissions: [
        {
          addingUser: '123',
          key: '123',
          kind: 'Core#Permission',
          granteeId: '345',
          role: 'commenter',
          type: 'organization',
          itemId: '678',
        },
      ],
      lastModified: { byMe: false, time: 0, user: 'id', name: 'test' },
      labels: ['l1'],
    }
    const result = new Folder(schema)
    assert.equal(result.kind, FolderKind)
    assert.equal(result.organization, organization)
    assert.equal(result.info.name, 'hello')
    assert.equal(result.owner, 'me')
    assert.equal(result.key, '123')
    assert.deepEqual(result.parents, ['p1'])
    assert.deepEqual(result.permissionIds, ['pr1'])
    assert.typeOf(result.permissions, 'array')
    assert.deepEqual(result.lastModified, schema.lastModified)
    assert.deepEqual(result.labels, ['l1'])
  })

  test('creates a Folder from the JSON schema string', ({ assert }) => {
    const schema: IFolder = {
      kind: FolderKind,
      organization,
      info: {
        kind: ThingKind,
        name: 'hello',
        description: 'a desc',
      },
      owner: 'me',
      key: '123',
      parents: ['p1'],
      permissionIds: ['pr1'],
      permissions: [
        {
          addingUser: '123',
          key: '123',
          kind: 'Core#Permission',
          granteeId: '345',
          role: 'commenter',
          type: 'organization',
          itemId: '678',
        },
      ],
      lastModified: { byMe: false, time: 0, user: 'id', name: 'test' },
    }
    const result = new Folder(JSON.stringify(schema))
    assert.equal(result.kind, FolderKind)
    assert.equal(result.organization, organization)
    assert.equal(result.info.name, 'hello')
    assert.equal(result.owner, 'me')
    assert.equal(result.key, '123')
    assert.deepEqual(result.lastModified, schema.lastModified)
  })

  test('throws when invalid schema', ({ assert }) => {
    assert.throws(() => {
      new Folder(
        JSON.stringify({
          name: 'a name',
        })
      )
    })
  })
})
