import test from 'node:test'
import assert from 'node:assert/strict'
import { inferSkillDraftFromMessages } from './chatWorkflow.js'

test('infers an explicit Skill name from runtime output', () => {
  const draft = inferSkillDraftFromMessages([
    { role: 'user', content: '我要做一个合同审查助手' },
    { role: 'assistant', content: '## Skill 名称：合同风险审查助手\n\n### 适用场景\n用于审查合同条款。' },
  ])
  assert.equal(draft.name, '合同风险审查助手')
  assert.match(draft.description, /适用场景/)
})

test('uses source file metadata when runtime output has no title', () => {
  const draft = inferSkillDraftFromMessages([
    { role: 'user', content: '请基于这份 SOP 创建 Skill' },
  ], {
    sourceFile: { name: '采购流程SOP' },
    sourceFilePath: '供应链 / 采购',
  })
  assert.equal(draft.name, '采购流程SOP Skill')
  assert.match(draft.description, /供应链 \/ 采购/)
})

test('prefers the main runtime result over auxiliary file sections', () => {
  const draft = inferSkillDraftFromMessages([
    { role: 'user', content: '帮我做一个 PRD Skill' },
    { role: 'assistant', content: '```markdown\n# AI 产品 PRD 撰写助手\n## 简介\n帮助撰写 PRD。\n```' },
    { role: 'assistant', content: 'examples.md：\n```markdown\n# 示例\n```' },
  ])
  assert.equal(draft.name, 'AI 产品 PRD 撰写助手')
})

test('accepts user-pasted markdown as publication content', () => {
  const draft = inferSkillDraftFromMessages([
    { role: 'user', content: '# 合同风险审查助手\n\n## 简介\n审查合同风险。' },
  ])
  assert.equal(draft.name, '合同风险审查助手')
  assert.match(draft.description, /审查合同风险/)
})

test('extracts the quoted name from a create request without surrounding wording', () => {
  const draft = inferSkillDraftFromMessages([
    { role: 'user', content: '创建一个名为“流程文件审核”的 Skill。用于审核 SOP 和制度流程。' },
    { role: 'assistant', content: '已完成“流程文件审核”Skill 创建。' },
  ])
  assert.equal(draft.name, '流程文件审核')
})
