import { test } from '@japa/runner'
import { ILicense, License, LicenseKind, Property } from '../../../src/index.js'

test.group('Default project initialization', () => {
  test('initializes a default rule', ({ assert }) => {
    const result = new License()
    assert.equal(result.kind, LicenseKind, 'sets the kind property')
    assert.isUndefined(result.url, 'does not set the url property')
    assert.isUndefined(result.name, 'does not set the name property')
    assert.isUndefined(result.content, 'does not set the content property')
  })
})

test.group('From schema initialization', (group) => {
  let base: ILicense
  group.each.setup(() => {
    base = {
      kind: LicenseKind,
    }
  })

  test('sets the url property', ({ assert }) => {
    const init: ILicense = { ...base, ...{ url: 'test' } }
    const rule = new License(init)
    assert.equal(rule.url, 'test')
  })

  test('sets the name property', ({ assert }) => {
    const init: ILicense = { ...base, ...{ name: 'test' } }
    const rule = new License(init)
    assert.equal(rule.name, 'test')
  })

  test('sets the content property', ({ assert }) => {
    const init: ILicense = { ...base, ...{ content: 'test' } }
    const rule = new License(JSON.stringify(init))
    assert.equal(rule.content, 'test')
  })
})

test.group('fromUrl()', () => {
  test('sets the url', ({ assert }) => {
    const result = License.fromUrl('https://')
    assert.equal(result.url, 'https://')
  })

  test('sets the name', ({ assert }) => {
    const result = License.fromUrl('https://', 'test')
    assert.equal(result.name, 'test')
  })
})

test.group('fromContent()', () => {
  test('sets the content', ({ assert }) => {
    const result = License.fromContent('# The license')
    assert.equal(result.content, '# The license')
  })

  test('sets the name', ({ assert }) => {
    const result = License.fromContent('https://', 'test')
    assert.equal(result.name, 'test')
  })
})

test.group('toJSON()', () => {
  test('serializes the url value', ({ assert }) => {
    const rule = License.fromUrl('https://')
    const result = rule.toJSON()
    assert.equal(result.url, 'https://')
  })

  test('serializes the name value', ({ assert }) => {
    const rule = License.fromUrl('https://', 'test')
    const result = rule.toJSON()
    assert.equal(result.name, 'test')
  })

  test('serializes the content value', ({ assert }) => {
    const rule = License.fromContent('test')
    const result = rule.toJSON()
    assert.equal(result.content, 'test')
  })

  test('serializes the kind value', ({ assert }) => {
    const rule = License.fromUrl('https://')
    const result = rule.toJSON()
    assert.equal(result.kind, LicenseKind)
  })
})

test.group('License.isLicense()', () => {
  test('returns false when no input', ({ assert }) => {
    const result = License.isLicense(undefined)
    assert.isFalse(result)
  })

  test('returns false when invalid type', ({ assert }) => {
    const instance = Property.String('abc')
    const result = License.isLicense(instance)
    assert.isFalse(result)
  })

  test('returns true when HttpRequest type', ({ assert }) => {
    const instance = new License()
    const result = License.isLicense(instance)
    assert.isTrue(result)
  })
})
