import { test } from '@japa/runner'
import { DomainFile, IDomainFile, ThingKind, DomainFileKind, DataDomain } from '../../../src/index.js'

test.group('DomainFile.fromDataDomain()', () => {
  test('sets the kind', ({ assert }) => {
    const p1 = new DataDomain({ info: { name: 'p1' } })
    const result = DomainFile.fromDataDomain(p1)
    assert.equal(result.kind, DomainFileKind)
  })

  test('sets the name', ({ assert }) => {
    const p1 = new DataDomain({ info: { name: 'p1' } })
    const result = DomainFile.fromDataDomain(p1)
    assert.equal(result.info.name, 'p1')
  })

  test('sets the key', ({ assert }) => {
    const p1 = new DataDomain({ info: { name: 'p1' } })
    const result = DomainFile.fromDataDomain(p1)
    assert.equal(result.key, p1.key)
  })

  test('respects schema instead of instance', ({ assert }) => {
    const p1 = new DataDomain({ info: { name: 'p1' } })
    const result = DomainFile.fromDataDomain(p1.toJSON())
    assert.equal(result.info.name, 'p1')
  })
})

test.group('constructor()', () => {
  test('creates a default data file', ({ assert }) => {
    const result = new DomainFile()
    assert.equal(result.kind, DomainFileKind)
    assert.typeOf(result.key, 'string')
    assert.typeOf(result.lastModified, 'object')
  })

  test('creates a data file from the schema values', ({ assert }) => {
    const schema: IDomainFile = {
      kind: DomainFileKind,
      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: 0, user: 'id', name: 'test' },
      labels: ['l1'],
    }
    const result = new DomainFile(schema)
    assert.equal(result.kind, DomainFileKind)
    assert.equal(result.info.name, 'hello')
    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 data file from the JSON schema string', ({ assert }) => {
    const schema: IDomainFile = {
      kind: DomainFileKind,
      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: 0, user: 'id', name: 'test' },
    }
    const result = new DomainFile(schema)
    assert.equal(result.kind, DomainFileKind)
    assert.equal(result.info.name, 'hello')
    assert.equal(result.key, '123')
    assert.deepEqual(result.lastModified, schema.lastModified)
  })
})

test.group('toJSON()', () => {
  test('serializes the kind', ({ assert }) => {
    const file = new DomainFile()
    const result = file.toJSON()
    assert.equal(result.kind, DomainFileKind)
  })

  test('serializes the key', ({ assert }) => {
    const file = new DomainFile()
    const result = file.toJSON()
    assert.equal(result.key, file.key)
  })

  // the "key" test tests whether the parent toJSON() is called.
})
