import assert from 'node:assert/strict'

import {
  resolveInitialKnowledgeFolder,
  resolveKnowledgeFolderIcon
} from '../src/views/scripts/knowledgeFolderSelection.js'

function testFallsBackToFirstFolderWhenCurrentFolderDoesNotExist() {
  const folders = [{ name: '财务知识库' }, { name: '制度政策' }, { name: '差旅规范' }]

  assert.equal(resolveInitialKnowledgeFolder(folders, '差旅规范（旧值）'), '财务知识库')
}

function testKeepsCurrentFolderWhenItStillExists() {
  const folders = [{ name: '财务知识库' }, { name: '制度政策' }, { name: '差旅规范' }]

  assert.equal(resolveInitialKnowledgeFolder(folders, '制度政策'), '制度政策')
}

function testReturnsEmptyStringWhenFoldersAreEmpty() {
  assert.equal(resolveInitialKnowledgeFolder([], '差旅规范'), '')
}

function testUsesOpenIconForActiveFolderOnly() {
  assert.equal(
    resolveKnowledgeFolderIcon({ name: '差旅规范', icon: 'mdi mdi-folder' }, '差旅规范'),
    'mdi mdi-folder-open'
  )
  assert.equal(
    resolveKnowledgeFolderIcon({ name: '制度政策', icon: 'mdi mdi-folder-open' }, '差旅规范'),
    'mdi mdi-folder'
  )
}

function run() {
  testFallsBackToFirstFolderWhenCurrentFolderDoesNotExist()
  testKeepsCurrentFolderWhenItStillExists()
  testReturnsEmptyStringWhenFoldersAreEmpty()
  testUsesOpenIconForActiveFolderOnly()
  console.log('knowledge folder selection tests passed')
}

run()
