import ReactDOMServer from 'react-dom/server'; import { describe,expect,test } from 'vitest'; import type { NotificationChannel,SystemSettings } from '../../shared/api-contract/common'; import { CardIcon } from './cards/components/CardIcon'; import { BatchPhaseIndicator } from './items/components/BatchPhaseIndicator'; import { NotificationChannelList } from './notifications/components/NotificationChannelList'; import { NotificationEventSelector } from './notifications/components/NotificationEventSelector'; import { NotificationSmtpSettings } from './notifications/components/NotificationSmtpSettings'; import { OrderFilterBar } from './orders/components/OrderFilterBar'; import type { AutomationRunIssue,DeferredAutomationIssue } from './rules/api'; import { AutomationIssuePanel } from './rules/components/AutomationIssuePanel'; // render 将 React 展示组件转换为静态 HTML,验证不依赖浏览器 DOM 的渲染分支。 const render = (element: React.ReactElement): string => ReactDOMServer.renderToStaticMarkup(element); // noopEventToggle 是静态渲染测试使用的通知事件回调占位实现。 const noopEventToggle = (): void => undefined; // noopFilterChange 是静态渲染测试使用的订单状态回调占位实现。 const noopFilterChange = (): void => undefined; // resolveAccountName 是静态渲染测试使用的账号展示名称解析器。 const resolveAccountName = (id: string): string => id === 'account-1' ? '测试账号' : id; // noopSearchChange 是静态渲染测试使用的订单搜索回调占位实现。 const noopSearchChange = (): void => undefined; // noopChannelAction 是静态渲染测试使用的通知渠道操作占位实现。 const noopChannelAction = (): void => undefined; // noopRunResolution 是静态渲染测试使用的自动化运行处理占位实现。 const noopRunResolution = (): void => undefined; // noopDeferredResolution 是静态渲染测试使用的延迟任务处理占位实现。 const noopDeferredResolution = (): void => undefined; // noopSmtpUpdate 是静态渲染测试使用的 SMTP 配置更新占位实现。 const noopSmtpUpdate = (): void => undefined; // noopPasswordToggle 是静态渲染测试使用的密码显隐更新占位实现。 const noopPasswordToggle = (): void => undefined; // noopSmtpSave 是静态渲染测试使用的 SMTP 保存占位实现。 const noopSmtpSave = (): void => undefined; describe('前端纯展示组件', /* 当前回调处理无浏览器依赖的展示组件断言。 */ () => { test('卡密图标覆盖各交付类型', /* 当前回调处理库存图标类型分支。 */ () => { expect(render()).toContain('text-blue-500'); expect(render()).toContain('text-purple-500'); expect(render()).toContain('text-blue-500'); expect(render()).toContain('text-gray-500'); }); test('批量阶段指示器只高亮当前阶段', /* 当前回调处理批量发布步骤样式分支。 */ () => { // markup 是预检阶段批量指示器生成的静态 HTML。 const markup = render(); expect(markup).toContain('1 上传'); expect(markup).toContain('2 预检'); expect(markup).toContain('bg-blue-600 text-white'); expect(markup.match(/bg-blue-600 text-white/g)).toHaveLength(1); }); test('通知事件选择器渲染已选与未选事件', /* 当前回调处理通知事件列表的选择状态。 */ () => { // selected 是包含一个已选事件的通知选择器静态 HTML。 const selected = render(); expect(selected).toContain('掉线通知'); expect(selected).toContain('border-brand bg-blue-50'); expect(selected).toContain('系统错误'); expect(selected).toContain('border-gray-100 hover:border-gray-300'); }); test('通知渠道列表覆盖空列表、停用和测试中状态', /* 当前回调处理通知渠道摘要和操作状态。 */ () => { // empty 是没有配置渠道时的空状态静态 HTML。 const empty = render(); expect(empty).toContain('还没有配置任何通知渠道'); // channel 是覆盖配置摘要和停用展示的最小渠道对象。 const channel: NotificationChannel = { id: 'channel-1', name: '测试 Bark', type: 'bark', config: { server_url: 'https://api.day.app', device_key: 'device-key' }, event_types: ['system_error'], enabled: false }; // list 是渠道列表生成的静态 HTML。 const list = render(); expect(list).toContain('测试 Bark'); expect(list).toContain('已停用'); expect(list).toContain('系统错误'); expect(list).toContain('animate-spin'); }); test('SMTP 设置面板覆盖密码显隐和保存中状态', /* 当前回调处理系统邮件配置的展示分支。 */ () => { // smtp 是覆盖 TLS/SSL 和发件人回填的系统邮件配置。 const smtp: SystemSettings = { smtp_server: 'smtp.example.com', smtp_port: 465, smtp_user: 'sender@example.com', smtp_password: 'secret', smtp_use_tls: false, smtp_use_ssl: true }; // hiddenMarkup 是密码隐藏且保存按钮可用时的静态 HTML。 const hiddenMarkup = render(); expect(hiddenMarkup).toContain('type="password"'); expect(hiddenMarkup).toContain('保存 SMTP 配置'); expect(hiddenMarkup).toContain('SMTP 邮件配置'); // savingMarkup 是密码显示且请求执行中的静态 HTML。 const savingMarkup = render(); expect(savingMarkup).toContain('type="text"'); expect(savingMarkup).toContain('保存中...'); expect(savingMarkup).toContain('disabled'); }); test('自动化异常面板渲染运行异常和延迟任务操作', /* 当前回调处理人工核对面板的异常分支。 */ () => { // runIssue 是允许继续、重试和终止的运行异常。 const runIssue: AutomationRunIssue = { id: 1, cookie_id: 'account-1', order_id: 'order-1', trigger_type: 'order_paid', error_message: '发送结果未知', issue_kind: 'external_result_unknown', allowed_resolutions: ['continue', 'retry', 'cancel'], action_cursor: 1, sent_count: 1, updated_at: '2026-08-15T00:00:00Z' }; // deferredIssue 是达到重试上限的延迟任务异常。 const deferredIssue: DeferredAutomationIssue = { id: 2, cookie_id: 'account-1', trigger_type: 'review_missing_timeout', error_message: '任务重试失败', attempt_count: 3, updated_at: '2026-08-15T00:00:00Z' }; // panel 是自动化异常面板生成的静态 HTML。 const panel = render(); expect(panel).toContain('需要人工处理的自动化任务'); expect(panel).toContain('外部动作结果未知'); expect(panel).toContain('已执行,继续下一步'); expect(panel).toContain('未执行,安全重试'); expect(panel).toContain('终止'); expect(panel).toContain('重新入队'); expect(panel).toContain('忽略'); }); test('订单筛选栏渲染状态、账号和关键词输入', /* 当前回调处理订单筛选工具栏的静态结构。 */ () => { // markup 是订单筛选栏生成的静态 HTML。 const markup = render(); expect(markup).toContain('已发货'); expect(markup).toContain('测试账号'); expect(markup).toContain('订单号/商品/买家'); expect(markup).toContain('aria-label="按账号筛选订单"'); }); });