import { describe, it, expect, jest } from '@jest/globals';
import { render, screen, act } from '@testing-library/react';
import '@testing-library/jest-dom';
// ─── Mocks ───────────────────────────────────────────────────────────────
jest.mock('framer-motion', () => ({
motion: {
div: ({ children, initial, animate, variants, whileInView, whileHover, whileTap, viewport, transition, className, onMouseMove, onMouseEnter, onMouseLeave, onMouseUp, style, ref, href, ...props }: any) => (
href ? (
{children}
) : (
{children}
)
),
p: ({ children, className, ...props }: any) => (
{children}
),
h1: ({ children, className, ...props }: any) => (
{children}
),
h2: ({ children, className, ...props }: any) => (
{children}
),
h3: ({ children, className, ...props }: any) => (
{children}
),
span: ({ children, className, ...props }: any) => (
{children}
),
button: ({ children, onClick, className, ...props }: any) => (
),
a: ({ children, href, className, ...props }: any) => (
{children}
),
li: ({ children, className, ...props }: any) => (
{children}
),
circle: ({ className, cx, cy, r, fill, stroke, strokeWidth, strokeLinecap, strokeDasharray, ...props }: any) => (
),
},
useInView: jest.fn(() => true),
useMotionValue: jest.fn(() => ({ get: () => 0.5, set: jest.fn() })),
useTransform: jest.fn(() => ({ get: () => '0%' })),
useSpring: jest.fn(() => ({ get: () => 0 })),
AnimatePresence: ({ children }: any) => <>{children}>,
}));
jest.mock('@/hooks/use-reduced-motion', () => ({
useReducedMotion: jest.fn(() => false),
}));
jest.mock('./detail-case-study', () => ({
CaseStudyCard: ({ study, index }: any) => (
{study.client}
{study.industry}
),
}));
jest.mock('./detail-certification', () => ({
CertificationList: ({ certifications }: any) => (
{certifications.map((cert: any, i: number) => (
{cert.name}
))}
),
}));
jest.mock('lucide-react', () => {
const icons: Record = {};
const names = [
'ArrowRight', 'Sparkles', 'ChevronDown', 'Zap', 'Shield',
'CheckCircle2', 'MessageCircle', 'Download', 'Package',
'Lightbulb', 'Wrench', 'TrendingUp', 'Clock', 'Award',
'Users', 'Cpu', 'ExternalLink',
];
for (const name of names) {
const Icon = (props: any) => (
);
Icon.displayName = name;
icons[name] = Icon;
}
return icons;
});
// ─── Mock Data ───────────────────────────────────────────────────────────
const mockHeroTheme = {
id: 'erp',
name: 'ERP',
gradientFrom: '#f8f6f3',
gradientTo: '#f0ebe4',
gradientAngle: 135,
accentColor: '#1e3a5f',
accentBg: 'rgba(30, 58, 95, 0.06)',
texture: { type: 'grid' as const, opacity: 0.03 },
layout: 'left' as const,
badge: '企业套装',
};
const mockProduct = {
id: 'erp',
title: 'ERP 企业资源管理系统',
description: '企业资源管理系统',
image: '/images/erp.jpg',
category: 'Enterprise Software',
categoryId: 'enterprise' as const,
status: '已发布' as const,
overview: '覆盖企业全业务流程的数字化管理平台',
features: ['财务管理:总账、应收应付、资产管理', '供应链管理:采购、库存、物流'],
benefits: ['提升运营效率 300%', '降低人力成本 50%'],
process: ['需求调研', '方案设计'],
specs: ['支持 10 万并发', '99.9% 可用性'],
tags: ['ERP', '企业管理'],
heroThemeId: 'erp',
bundle: 'enterprise' as const,
caseStudies: [
// DEC-N4 同闸:卡片型案例必须带授权 + basis=verified 才渲染(门槛见 case-publish-gate)
{ client: '某制造企业', industry: '制造业', challenge: '库存管理混乱', solution: '实施ERP系统', result: '库存周转率提升200%', authorized: true, basis: 'verified' as const },
],
dataProofs: [
{ metric: '效率提升', value: '300%', description: '业务流程效率提升' },
],
certifications: [
{ name: 'ISO 27001', issuer: '国际认证', link: 'https://example.com' },
],
};
// ─── Tests ───────────────────────────────────────────────────────────────
describe('ProductValueSection', () => {
it('should render product title and overview', async () => {
const { ProductValueSection } = await import('./detail-product-value');
render();
const heading = screen.getByRole('heading', { name: new RegExp(mockProduct.title) });
expect(heading).toHaveTextContent('核心能力');
expect(screen.getByText(mockProduct.overview)).toBeInTheDocument();
});
it('should render feature items', async () => {
const { ProductValueSection } = await import('./detail-product-value');
render();
expect(screen.getByText('产品功能')).toBeInTheDocument();
// Features are rendered via FeatureTags which splits on ":"
expect(screen.getByText('财务管理')).toBeInTheDocument();
});
it('should render benefit items', async () => {
const { ProductValueSection } = await import('./detail-product-value');
render();
expect(screen.getByText('核心优势')).toBeInTheDocument();
expect(screen.getByText('提升运营效率 300%')).toBeInTheDocument();
expect(screen.getByText('降低人力成本 50%')).toBeInTheDocument();
});
it('should render spec items', async () => {
const { ProductValueSection } = await import('./detail-product-value');
render();
expect(screen.getByText('技术规格')).toBeInTheDocument();
expect(screen.getByText('支持 10 万并发')).toBeInTheDocument();
});
it('should render data proofs when available', async () => {
const { ProductValueSection } = await import('./detail-product-value');
render();
expect(screen.getByText('以可量化的目标约束交付')).toBeInTheDocument();
});
it('should not render data proofs section when empty', async () => {
const { ProductValueSection } = await import('./detail-product-value');
const productWithoutData = { ...mockProduct, dataProofs: [] };
render();
expect(screen.queryByText('以可量化的目标约束交付')).not.toBeInTheDocument();
});
it('指标计数动画由 effect 驱动并在卸载时取消排队帧(Q-6 回归)', async () => {
// Q-6:动画曾在 render 体内 setState + requestAnimationFrame——每帧 setState 又触发
// 重渲染并重启一条新动画链,且从不 cancelAnimationFrame;卸载后帧回调继续排队。
// 判别面:已排队的帧必须被逐一取消(sched === cancel),旧实现 sched>0 且 cancel=0 必红。
jest.useFakeTimers();
// faithful rAF 桩:cancel 必须真的撤销已排队的回调,否则「卸载后不再排队」无从判别
const pending = new Map>();
let handle = 0;
const sched = jest.fn((cb: FrameRequestCallback) => {
const id = ++handle;
pending.set(
id,
setTimeout(() => {
pending.delete(id);
cb(Date.now());
}, 16),
);
return id;
});
const cancel = jest.fn((id: number) => {
const t = pending.get(id);
if (t) clearTimeout(t);
pending.delete(id);
});
const prevRaf = global.requestAnimationFrame;
const prevCancel = global.cancelAnimationFrame;
global.requestAnimationFrame = sched as unknown as typeof requestAnimationFrame;
global.cancelAnimationFrame = cancel as unknown as typeof cancelAnimationFrame;
try {
const { ProductValueSection } = await import('./detail-product-value');
const { container, unmount } = render();
const card = container.querySelector('.tabular-nums') as HTMLElement;
expect(card.textContent).toBe('0');
await act(async () => {
jest.advanceTimersByTime(200);
});
// 动画真的在推进(中途值),而不是停在 0 或瞬间跳终值
expect(card.textContent).not.toBe('0');
expect(card.textContent).not.toBe('300%');
const scheduledBeforeUnmount = sched.mock.calls.length;
expect(scheduledBeforeUnmount).toBeGreaterThan(0);
unmount();
await act(async () => {
jest.advanceTimersByTime(3000);
});
// 泄漏判别:卸载后动画链必须死透——旧实现(render 期 rAF、从不 cancel)
// 在这里会继续排队每一帧;effect 版在 cleanup 里 cancel 掉最后一帧。
expect(sched.mock.calls.length).toBe(scheduledBeforeUnmount);
} finally {
global.requestAnimationFrame = prevRaf;
global.cancelAnimationFrame = prevCancel;
jest.useRealTimers();
}
});
// ── DEF-06/07:区间/多数字指标不得被拼成一个数 ──
describe('指标计数动画的取值解析(DEF-06/07)', () => {
it('区间值 `3-6个月` 不被渲染成 `36`', async () => {
const { ProductValueSection } = await import('./detail-product-value');
const rangeProduct = {
...mockProduct,
dataProofs: [{ metric: '实施周期', value: '3-6个月', description: '典型交付周期' }],
};
render();
// 旧实现 `value.replace(/[^0-9.]/g,'')` ⇒ 36,`-` 与 `个月` 被丢掉
expect(screen.queryByText('36')).not.toBeInTheDocument();
expect(screen.getByText(/3-6个月/)).toBeInTheDocument();
});
it('DetailTrustSection 的计数器同样不把 `3–6 个月` 拼成 `36`', async () => {
const { DetailTrustSection } = await import('./detail-trust-section');
render(
);
expect(screen.queryByText('36')).not.toBeInTheDocument();
expect(screen.getByText(/3–6 个月/)).toBeInTheDocument();
});
});
it('showDataProofs=false 时整块不渲染(V3 去重)', async () => {
const { ProductValueSection } = await import('./detail-product-value');
render();
expect(screen.queryByText('以可量化的目标约束交付')).not.toBeInTheDocument();
});
});
describe('DetailTrustSection', () => {
it('should render nothing when all props are empty', async () => {
const { DetailTrustSection } = await import('./detail-trust-section');
const { container } = render();
// Component returns null when no content
expect(container.innerHTML).toBe('');
});
it('should render data proofs', async () => {
const { DetailTrustSection } = await import('./detail-trust-section');
render(
);
expect(screen.getByText('目标口径')).toBeInTheDocument();
expect(screen.getByText(mockProduct.dataProofs[0]!.metric)).toBeInTheDocument();
// 未声明 basis 的 dataProofs 必须自动落最弱口径角注,不得裸奔
expect(screen.getByTestId('metrics-basis-note')).toHaveTextContent('目标口径,非既成结果');
});
it('should render case studies', async () => {
const { DetailTrustSection } = await import('./detail-trust-section');
render(
);
expect(screen.getByText('他们已经实现了转型突破')).toBeInTheDocument();
expect(screen.getByTestId('case-study-card')).toBeInTheDocument();
expect(screen.getByTestId('case-client')).toHaveTextContent('某制造企业');
});
// ── DEC-N4 同闸:DetailTrustSection 案例块必须过与首页成果槽同一门槛 ──
describe('案例块反伪造门槛(DEC-N4 同闸)', () => {
const gated = mockProduct.caseStudies[0]!;
it('缺 basis(即便已授权)⇒ 成果主张整块不渲染,且原因被喊出来', async () => {
const { DetailTrustSection } = await import('./detail-trust-section');
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
const { container } = render(
);
expect(screen.queryByText('他们已经实现了转型突破')).not.toBeInTheDocument();
expect(screen.queryByTestId('case-study-card')).not.toBeInTheDocument();
expect(container.querySelector('[data-testid="detail-trust-case-studies"]')).not.toBeInTheDocument();
expect(spy).toHaveBeenCalledWith(expect.stringMatching(/basis === "verified"/));
spy.mockRestore();
});
it('未授权(authorized 不为 true,即便口径已 verified)⇒ 不渲染', async () => {
const { DetailTrustSection } = await import('./detail-trust-section');
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
render(
);
expect(screen.queryByText('他们已经实现了转型突破')).not.toBeInTheDocument();
expect(spy).toHaveBeenCalledWith(expect.stringMatching(/未授权公开/));
spy.mockRestore();
});
it('授权 + verified 口径 ⇒ 渲染(正控制,防"闸恒真/恒假"两种假绿)', async () => {
const { DetailTrustSection } = await import('./detail-trust-section');
render();
expect(screen.getByText('他们已经实现了转型突破')).toBeInTheDocument();
expect(screen.getByTestId('detail-trust-case-studies')).toBeInTheDocument();
});
it('非 verified 口径(target / team-history)同样被闸住——闸要能拒绝"数字真、出处假"', async () => {
const { DetailTrustSection } = await import('./detail-trust-section');
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
render(
);
expect(screen.queryByText('他们已经实现了转型突破')).not.toBeInTheDocument();
expect(screen.queryByTestId('case-study-card')).not.toBeInTheDocument();
expect(spy).toHaveBeenCalledWith(expect.stringMatching(/basis === "verified"/));
spy.mockRestore();
});
});
it('should render certifications', async () => {
const { DetailTrustSection } = await import('./detail-trust-section');
render(
);
expect(screen.getByText('资质认证')).toBeInTheDocument();
expect(screen.getByTestId('certification-list')).toBeInTheDocument();
expect(screen.getByTestId('cert-name')).toHaveTextContent('ISO 27001');
});
it('should render all sections together', async () => {
const { DetailTrustSection } = await import('./detail-trust-section');
render(
);
expect(screen.getByText('目标口径')).toBeInTheDocument();
expect(screen.getByText('他们已经实现了转型突破')).toBeInTheDocument();
expect(screen.getByText('资质认证')).toBeInTheDocument();
});
});
describe('DetailCTASection', () => {
it('should render default title and description', async () => {
const { DetailCTASection } = await import('./detail-cta-section');
render(
);
expect(screen.getByText('准备好开始了吗?')).toBeInTheDocument();
expect(screen.getByText('联系我们,获取专属方案和报价')).toBeInTheDocument();
});
it('should render custom title and description', async () => {
const { DetailCTASection } = await import('./detail-cta-section');
render(
);
expect(screen.getByText('Custom CTA Title')).toBeInTheDocument();
expect(screen.getByText('Custom CTA description')).toBeInTheDocument();
});
it('should render primary action with link', async () => {
const { DetailCTASection } = await import('./detail-cta-section');
render(
);
const link = screen.getByText('免费试用').closest('a');
expect(link).toHaveAttribute('href', '/trial');
});
it('should render secondary action when provided', async () => {
const { DetailCTASection } = await import('./detail-cta-section');
render(
);
expect(screen.getByText('下载资料')).toBeInTheDocument();
const link = screen.getByText('下载资料').closest('a');
expect(link).toHaveAttribute('href', '/download');
});
it('should render free consultation badge', async () => {
const { DetailCTASection } = await import('./detail-cta-section');
render(
);
expect(screen.getByText('免费咨询')).toBeInTheDocument();
});
it('should render SLA text', async () => {
const { DetailCTASection } = await import('./detail-cta-section');
render(
);
// SLA 文案已收进 lib/constants/commitments.ts 单一真源;旧断言硬写「< 2小时 · 7×24」
// 正是被撤下的那条不可验证承诺(7×24 对任何一次夜间延迟都会自破)。
expect(screen.getByText(/工作日 2 小时内回复您的咨询/)).toBeInTheDocument();
});
});
// 注:CrossRecommendGrid 相关测试已随组件删除(2026-08-31,组件生产 0 引用)