import test from 'node:test';
import assert from 'node:assert/strict';
import {
  getFallbackHelpMessage,
  getFallbackHelpSuggestions,
  searchHelpArticles,
  shouldShowGenericHelpSuggestions,
} from './wenfan-help-search';
import { WENFAN_HELP_ARTICLES } from './wenfan-help-articles';

test('searchHelpArticles matches common product creation phrasing', () => {
  const result = searchHelpArticles('产品怎么新建，要填什么字段', WENFAN_HELP_ARTICLES);

  assert.equal(result[0]?.article.id, 'product-create');
});

test('searchHelpArticles matches demand adoption and version inclusion synonyms', () => {
  const result = searchHelpArticles('需求怎么纳入版本号里面', WENFAN_HELP_ARTICLES);

  assert.equal(result[0]?.article.id, 'version-requirement-include');
});

test('searchHelpArticles matches dev task test submission phrasing', () => {
  const result = searchHelpArticles('开发任务怎么提测', WENFAN_HELP_ARTICLES);

  assert.equal(result[0]?.article.id, 'dev-task-workflow');
});

test('searchHelpArticles matches bug creation from failed test cases', () => {
  const result = searchHelpArticles('测试用例失败了怎么提bug', WENFAN_HELP_ARTICLES);

  assert.equal(result[0]?.article.id, 'bug-workflow');
});

test('bug help article explains bug creation only from failed test cases', () => {
  const article = WENFAN_HELP_ARTICLES.find((item) => item.id === 'bug-workflow');

  assert.ok(article);
  assert.match(article.entry, /测试用例/);
  assert.match(article.entry, /提 BUG|提 Bug/);
  assert.doesNotMatch(article.entry, /Bug Tab -> 新建 Bug/);
  assert.ok(article.steps.some((step) => /失败|不通过/.test(step) && /提 BUG|提 Bug/.test(step)));
  assert.ok(article.notes.some((note) => /不支持|不能/.test(note) && /Bug Tab/.test(note)));
});

test('searchHelpArticles keeps related article ids from the matched article', () => {
  const result = searchHelpArticles('AI拆解测试用例在哪里', WENFAN_HELP_ARTICLES);

  assert.equal(result[0]?.article.id, 'ai-decompose');
  assert.ok(result[0]?.relatedArticles.some((article) => article.id === 'product-plan'));
});

test('searchHelpArticles returns no article for short or unknown input', () => {
  assert.deepEqual(searchHelpArticles('?', WENFAN_HELP_ARTICLES), []);
  assert.deepEqual(searchHelpArticles('完全无关的问题', WENFAN_HELP_ARTICLES), []);
});

test('getFallbackHelpSuggestions returns starter examples when no article matches', () => {
  assert.deepEqual(getFallbackHelpSuggestions().slice(0, 3), [
    '怎么新建产品？',
    '怎么把需求纳入版本？',
    '开发任务怎么提测？',
  ]);
});

test('getFallbackHelpMessage removes generic prompt wording when generic suggestions are hidden', () => {
  assert.match(getFallbackHelpMessage(true), /常见问题/);
  assert.doesNotMatch(getFallbackHelpMessage(false), /常见问题/);
  assert.match(getFallbackHelpMessage(false), /更具体的系统关键词/);
});

test('shouldShowGenericHelpSuggestions hides generic prompts after the first user follow-up', () => {
  assert.equal(shouldShowGenericHelpSuggestions(0), true);
  assert.equal(shouldShowGenericHelpSuggestions(1), true);
  assert.equal(shouldShowGenericHelpSuggestions(2), false);
  assert.equal(shouldShowGenericHelpSuggestions(5), false);
});
