import { describe, expect, it } from 'vitest';

import { translateFailureReason } from '@/lib/failureReasonMessages';

describe('translateFailureReason', () => {
  it('translates known search and contact reason codes', () => {
    expect(translateFailureReason('no_search_candidates')).toBe('没有搜索候选商品');
    expect(translateFailureReason('contact_navigation_failed')).toBe('联系方式页导航失败');
    expect(translateFailureReason('browser_target_crashed')).toBe('搜索浏览器崩溃，未进入结果页');
  });

  it('keeps Chinese business reasons unchanged', () => {
    expect(translateFailureReason('缺少规格型号和材质，无法准确搜索')).toBe('缺少规格型号和材质，无法准确搜索');
  });

  it('translates mixed Chinese and English internal database failures', () => {
    expect(translateFailureReason(
      '[keyword_process] upsert 失败: there is no unique or exclusion constraint matching the ON CONFLICT specification',
    )).toBe('关键词生成结果保存失败，请检查数据库约束或稍后重试');
  });

  it('hides unknown English reason codes behind a Chinese fallback', () => {
    expect(translateFailureReason('future_internal_reason_code')).toBe('系统已记录失败原因，请稍后重试或查看任务日志');
    expect(translateFailureReason('An upstream browser worker crashed')).toBe('系统已记录失败原因，请稍后重试或查看任务日志');
  });

  it('formats dynamic HTTP and image download failures', () => {
    expect(translateFailureReason('http_502')).toBe('1688 页面请求失败（HTTP 502）');
    expect(translateFailureReason('image-download-failed:404')).toBe('图片下载失败（HTTP 404）');
  });
});
