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

test.group('File.shortcutTo()', () => {
  test('sets the shortcutTarget property', ({ assert }) => {
    const file = new File()
    const targetId = 'target-file-id'
    file.shortcutTo(targetId)
    assert.equal(file.shortcutTarget, targetId)
  })

  test('returns the file instance', ({ assert }) => {
    const file = new File()
    const targetId = 'target-file-id'
    const result = file.shortcutTo(targetId)
    assert.equal(result, file)
  })

  test('sets isShortcut to true when shortcutTarget is set', ({ assert }) => {
    const file = new File()
    const targetId = 'target-file-id'
    file.shortcutTo(targetId)
    assert.isTrue(file.isShortcut)
  })

  test('sets isShortcut to false when shortcutTarget is undefined', ({ assert }) => {
    const file = new File()
    file.shortcutTarget = undefined
    assert.isFalse(file.isShortcut)
  })

  test('serializes the shortcutTarget and isShortcut properties with toJSON()', ({ assert }) => {
    const file = new File()
    const targetId = 'target-file-id'
    file.shortcutTo(targetId)
    const json = file.toJSON()
    assert.equal(json.shortcutTarget, targetId)
    assert.isTrue(json.isShortcut)
  })

  test('does not serialize the shortcutTarget and isShortcut properties when undefined with toJSON()', ({ assert }) => {
    const file = new File()
    const json = file.toJSON()
    assert.isUndefined(json.shortcutTarget)
    assert.isUndefined(json.isShortcut) // Shouldn't be present when undefined
  })

  test('creates a shortcut from a name with fromName()', ({ assert }) => {
    const file = File.fromName('test-file', 'shortcut')
    const targetId = 'target-file-id'
    file.shortcutTo(targetId)
    assert.equal(file.shortcutTarget, targetId)
    assert.isTrue(file.isShortcut)
  })
})
