import test from 'node:test'
import assert from 'node:assert/strict'
import { filterSourceProjects, findSourceProject, listSourceCategories, sourceProjects } from './sourceProjects.js'

test('sourceProjects exposes visible projects with required display fields', () => {
  assert.ok(sourceProjects.length >= 6)
  sourceProjects.forEach((project) => {
    assert.ok(project.id)
    assert.ok(project.name)
    assert.ok(project.summary)
    assert.ok(project.coverText)
    assert.equal(Object.prototype.hasOwnProperty.call(project, 'sourceSize'), false)
    assert.ok(Array.isArray(project.tags))
    assert.ok(project.tags.length >= 3)
    assert.ok(project.descriptionMd.includes('# '))
  })
})

test('listSourceCategories includes all plus project categories without duplicates', () => {
  const categories = listSourceCategories()

  assert.equal(categories[0].value, 'all')
  assert.equal(categories[0].label, '全部源码')
  assert.deepEqual(new Set(categories.map((category) => category.value)).size, categories.length)
  assert.ok(categories.some((category) => category.value === 'enterprise'))
  assert.ok(categories.some((category) => category.value === 'mall'))
})

test('findSourceProject returns project by id and falls back to null', () => {
  assert.equal(findSourceProject('dms-platform')?.name, '商用车 DMS 管理系统')
  assert.equal(findSourceProject('missing-project'), null)
})

test('filterSourceProjects filters by category and keyword', () => {
  const mallProjects = filterSourceProjects({ category: 'mall' })
  assert.deepEqual(
    mallProjects.map((project) => project.id),
    ['mall-order-system']
  )

  const searchProjects = filterSourceProjects({ category: 'all', keyword: '审批' })
  assert.deepEqual(
    searchProjects.map((project) => project.id),
    ['oa-approval-suite']
  )
})
