import assert from 'node:assert/strict'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import test from 'node:test'

import {
  buildBundledSkillManifest,
  bundledSkillId,
  isBundledSkillPathExcluded,
  parseSkillMetadata,
} from './bundled-skills-utils.mjs'

test('bundled skill filter removes machine state but keeps distributable templates', () => {
  const excluded = [
    'linear/.git/config',
    'skill/node_modules/pkg/index.js',
    'skill/__pycache__/helper.pyc',
    'skill/.DS_Store',
    'skill/.env',
    'skill/.env.local',
    'skill/private.pem',
    'skill/cache.sqlite',
  ]
  for (const file of excluded) assert.equal(isBundledSkillPathExcluded(file), true, file)

  const included = [
    'linear/.env.schema',
    'skill/.env.example',
    'skill/SKILL.md',
    'skill/assets/template.xlsx',
    '.system/skill-creator/scripts/init_skill.py',
  ]
  for (const file of included) assert.equal(isBundledSkillPathExcluded(file), false, file)
})

test('skill metadata uses frontmatter and marks system skills without name collisions', () => {
  const regular = parseSkillMetadata('---\nname: report-writer\ndescription: Write reports.\n---\n# Report\n', 'report-writer/SKILL.md')
  assert.equal(regular.name, 'report-writer')
  assert.equal(regular.description, 'Write reports.')
  assert.equal(regular.system, false)

  const system = parseSkillMetadata('---\nname: skill-creator\ndescription: Create skills.\n---\n', '.system/skill-creator/SKILL.md')
  assert.equal(system.name, '系统 · skill-creator')
  assert.equal(system.system, true)
})

test('skill metadata unfolds multiline YAML descriptions', () => {
  const metadata = parseSkillMetadata(
    '---\nname: audit\ndescription: >\n  Review workflow files.\n  Return a scored report.\n---\n',
    'audit/SKILL.md',
  )
  assert.equal(metadata.description, 'Review workflow files. Return a scored report.')
})

test('manifest discovers regular and system skills with deterministic ids and package roots', () => {
  const root = fs.mkdtempSync(path.join(os.tmpdir(), 'aipros-bundled-skills-'))
  try {
    fs.mkdirSync(path.join(root, 'alpha', 'assets'), { recursive: true })
    fs.writeFileSync(path.join(root, 'alpha', 'SKILL.md'), '---\nname: alpha\ndescription: Alpha skill.\n---\n')
    fs.writeFileSync(path.join(root, 'alpha', 'assets', 'prompt.txt'), 'prompt')
    fs.mkdirSync(path.join(root, '.system', 'alpha'), { recursive: true })
    fs.writeFileSync(path.join(root, '.system', 'alpha', 'SKILL.md'), '---\nname: alpha\ndescription: System alpha.\n---\n')

    const manifest = buildBundledSkillManifest(root, { createdAt: '2026-07-20T00:00:00.000Z' })
    assert.equal(manifest.skillCount, 2)
    assert.equal(manifest.createdAt, '2026-07-20T00:00:00.000Z')
    assert.deepEqual(manifest.skills.map(skill => skill.sourcePath), ['.system/alpha', 'alpha'])
    assert.deepEqual(manifest.skills.map(skill => skill.packageRoot), ['.system/alpha', 'alpha'])
    assert.notEqual(manifest.skills[0].id, manifest.skills[1].id)
    assert.equal(manifest.skills[0].id, bundledSkillId('.system/alpha'))
    assert.equal(manifest.skills[1].files, 2)
  } finally {
    fs.rmSync(root, { recursive: true, force: true })
  }
})
