import test from 'node:test'
import assert from 'node:assert/strict'
import JSZip from 'jszip'

import {
  buildSkillPackageBlob,
  buildSkillPackageEntries,
  safeSkillPackageFilename,
} from './skillPackage.js'

test('safeSkillPackageFilename returns a zip filename', () => {
  assert.equal(safeSkillPackageFilename('合同/风险:审查助手'), '合同_风险_审查助手.zip')
  assert.equal(safeSkillPackageFilename(''), 'skill-package.zip')
})

test('buildSkillPackageEntries always includes SKILL.md', () => {
  const entries = buildSkillPackageEntries({
    name: '合同风险审查助手',
    description: '用于审查合同条款。',
  })

  assert.equal(entries[0].path, 'SKILL.md')
  assert.match(entries[0].content, /# 合同风险审查助手/)
  assert.match(entries[0].content, /用于审查合同条款/)
})

test('buildSkillPackageEntries extracts named markdown code fences as auxiliary files', () => {
  const entries = buildSkillPackageEntries({
    name: '创建助手',
    description: [
      '# 创建助手',
      '',
      '```markdown filename=examples.md',
      '# 示例',
      '```',
      '',
      '```md title="rubric.md"',
      '# 评分卡',
      '```',
    ].join('\n'),
  })

  assert.deepEqual(entries.map(e => e.path), ['SKILL.md', 'examples.md', 'rubric.md'])
})

test('buildSkillPackageEntries extracts file fences introduced by prose labels', () => {
  const entries = buildSkillPackageEntries({
    name: 'AI 产品 PRD 撰写助手',
    description: [
      '先写 SKILL.md：',
      '```markdown',
      '# AI 产品 PRD 撰写助手',
      '## 简介',
      '帮助写 PRD。',
      '```',
      '',
      '继续写 examples.md：',
      '```markdown',
      '# 示例',
      '## 示例一',
      '```',
      '',
      '再补充 rubric.md：',
      '```markdown',
      '# 质量标准',
      '```',
    ].join('\n'),
  })

  assert.deepEqual(entries.map(e => e.path), ['SKILL.md', 'examples.md', 'rubric.md'])
  assert.match(entries.find(e => e.path === 'SKILL.md').content, /AI 产品 PRD 撰写助手/)
  assert.match(entries.find(e => e.path === 'examples.md').content, /示例一/)
})

test('buildSkillPackageEntries preserves nested code fences inside tilde file fences', () => {
  const entries = buildSkillPackageEntries({
    name: '客户投诉分级验证 Skill',
    description: [
      'SKILL.md：',
      '~~~markdown',
      '# 客户投诉分级验证 Skill',
      '## 简介',
      '处理投诉。',
      '## 触发条件',
      '用户提供投诉文本。',
      '## 核心指令',
      '判断等级。',
      '~~~',
      '',
      'examples.md：',
      '~~~markdown',
      '# 示例',
      '## 示例一',
      '```json',
      '{"level":"P0","reason":"阻断业务"}',
      '```',
      '~~~',
      '',
      'rubric.md：',
      '~~~markdown',
      '# 评分标准',
      '- 等级准确。',
      '~~~',
    ].join('\n'),
  })

  assert.deepEqual(entries.map(e => e.path), ['SKILL.md', 'examples.md', 'rubric.md'])
  assert.doesNotMatch(entries.find(e => e.path === 'SKILL.md').content, /~~~/)
  assert.match(entries.find(e => e.path === 'examples.md').content, /```json/)
  assert.match(entries.find(e => e.path === 'examples.md').content, /"level":"P0"/)
  assert.doesNotMatch(entries.find(e => e.path === 'examples.md').content, /~~~/)
  assert.match(entries.find(e => e.path === 'rubric.md').content, /等级准确/)
  assert.doesNotMatch(entries.find(e => e.path === 'rubric.md').content, /~~~/)
})

test('buildSkillPackageEntries extracts plain file sections when fences are omitted', () => {
  const entries = buildSkillPackageEntries({
    name: '客户投诉分级与响应助手',
    description: [
      '以下是三个文件的完整内容：',
      '',
      'SKILL.md',
      '# 客户投诉分级与响应助手',
      '## 简介',
      '处理客户投诉。',
      '',
      'examples.md',
      '# 示例',
      '## 示例一',
      '客户投诉核心功能不可用。',
      '',
      'rubric.md',
      '# 评分标准',
      '- 等级判断准确。',
    ].join('\n'),
  })

  assert.deepEqual(entries.map(e => e.path), ['SKILL.md', 'examples.md', 'rubric.md'])
  assert.match(entries.find(e => e.path === 'SKILL.md').content, /处理客户投诉/)
  assert.match(entries.find(e => e.path === 'examples.md').content, /示例一/)
  assert.match(entries.find(e => e.path === 'rubric.md').content, /等级判断准确/)
})

test('buildSkillPackageEntries extracts emphasized markdown file labels', () => {
  const entries = buildSkillPackageEntries({
    name: '客户投诉分级与响应助手',
    description: [
      '**SKILL.md：**',
      '# 客户投诉分级与响应助手',
      '## 简介',
      '处理客户投诉。',
      '',
      '**examples.md：**',
      '# 示例',
      '客户投诉核心功能不可用。',
      '',
      '**rubric.md：**',
      '# 评分标准',
      '- 等级判断准确。',
    ].join('\n'),
  })

  assert.deepEqual(entries.map(e => e.path), ['SKILL.md', 'examples.md', 'rubric.md'])
})

test('buildSkillPackageEntries extracts numbered markdown file labels', () => {
  const entries = buildSkillPackageEntries({
    name: '客户投诉分级与响应助手',
    description: [
      '生成文件',
      '1. SKILL.md',
      '# 客户投诉分级与响应助手',
      '## 简介',
      '处理客户投诉。',
      '',
      '2. examples.md',
      '# 示例',
      '客户投诉核心功能不可用。',
      '',
      '3. rubric.md',
      '# 评分标准',
      '- 等级判断准确。',
    ].join('\n'),
  })

  assert.deepEqual(entries.map(e => e.path), ['SKILL.md', 'examples.md', 'rubric.md'])
})

test('buildSkillPackageEntries preserves explicit subdirectories and JSON files', () => {
  const entries = buildSkillPackageEntries({
    name: '带配置 Skill',
    description: [
      '```json path=config/schema.json',
      '{"type":"object"}',
      '```',
      '```md path=docs/examples.md',
      '# 示例',
      '```',
    ].join('\n'),
  })

  assert.deepEqual(entries.map(e => e.path), ['SKILL.md', 'config/schema.json', 'docs/examples.md'])
})

test('buildSkillPackageBlob creates a readable zip with SKILL.md', async () => {
  const entries = buildSkillPackageEntries({
    name: '合同风险审查助手',
    description: 'examples.md：\n```markdown\n# 示例\n```',
  })
  const blob = await buildSkillPackageBlob({ entries })
  const zip = await JSZip.loadAsync(await blob.arrayBuffer())
  const skill = await zip.file('SKILL.md').async('string')

  assert.match(skill, /合同风险审查助手/)
  assert.equal(await zip.file('examples.md').async('string'), '# 示例\n')
})
