refactor(deadcode): 删 17 个不可达模块并做成可复跑的可达性判定,棘轮回到绿且未降任何门限

台账 DEC-P-2 的死码族一直靠人工 grep 猜,本轮做成 scripts/dead-modules.mjs:
从真实入口(app/**/page|layout|route + Next 约定文件)做 import 闭包,再分类。
**这台仪器本身修了三次才可信,每一次都是"看似合理但会误删活码"**:
1. 只匹配 `from '...'` ⇒ `dynamic(() => import('./GoogleAnalytics'))` 这条边不存在,
   GA 组件被报成"零引用死码"。照那结果删,GA4 会在没有任何渲染断言的情况下从页面消失。
2. 只扫 src/ ⇒ scripts/utils/check-color-contrast.ts import 的 src/lib/color-contrast.ts
   被报成死码(门禁自己的依赖不在图内)。
3. 入口正则写成 /app\/.*(^|\/)layout\.tsx$/ ⇒ 强制 layout.tsx 前有 '/',
   于是 **src/app/layout.tsx 这种根级约定文件根本不算入口**,它 import 的整棵树
   全部误判死码(实测把 home-content-v16、layout 自己都报成死码,129 个假死码)。
4. 相对导入用 path.resolve 解析成绝对路径,而模块表 key 是相对路径 ⇒ 所有 `./x` 边静默丢弃,
   可达数看着像个数其实半瞎。
归一后:入口 65 · 站内模块 489 · 可达 295 · 不可达 194 —— 其中
convention 4 / generated 2 / **library 53(组件库库存,删除是产品决定,本轮不删)** /
test 118 / **dead 仅 17**。前几版报的 48~129 个"死码"绝大多数是假阳性。

删除 17 个(含 5 个专属测试 + 3 个 describe 块 + 1 段孤儿 jest.mock):
sections 下 11 个被页面自己手写实现取代的区块、两个 breadcrumb、NewsDetailClient
(news 详情页实际用的是 news-detail-content-v3)、lib/cms/index.ts 空桶、
design-system.ts。删后 dead-modules 复判 0。

**棘轮被打破,而我拒绝用"降门限"来让它闭嘴**(这是本轮最重要的一个决定):
删完 `npm run test:coverage` EXIT=1,./src/lib/ functions 84.95(门限85)、
./src/lib/constants/ functions 51.51(门限55)。先做了归因 —— 用真闸而不是自拼 config
(我第一版自拼 config 测的"基线"是坏测量:漏跑 prisma generate ⇒ 套件全崩 ⇒ 0% 覆盖率)。
结论:da9a5cb6 上这道闸 EXIT=0 通过,是我打破的;且 design-system.ts 有 100% 覆盖的函数,
**删掉一个"有完整测试的死码"在数学上必然拉低目录均值** —— 门限此前是被"永远不会上线的代码
的测试"撑着的。
处置:一处门限都没改。改为
- 补真测试:getHeroTheme 未知 id 回落 default、交叉引用兜底、常量桶一致性守卫
- 删掉我在 nav-active.ts 里写出的一条**不可达分支**(函数开头已 `if(!href) return false`,
  `target===''` 永不可达;留着它会让覆盖率报告出现"没覆盖到的分支",诱导出一个假测试)
- 修掉一个既有潜伏陷阱:根桶 constants.ts 与子桶 constants/index.ts **两条平行出口不一致**,
  根桶漏了 getHeroTheme / 三个 CrossRefs / commitments / cta-labels / metrics-basis 全部符号
  ⇒ `import { getHeroTheme } from '@/lib/constants'` 会拿到 undefined,调用时才炸、
  静态检查不拦。补齐后 All files functions 82.28 → **87.62**,COVERAGE_EXIT=0。
这条漂移是刚写的桶守卫当场抓出来的(它先以"函数是 undefined"判红),不是我事后编的测试。

门禁实测:type-check EXIT=0 · 全量 jest 通过 · test:coverage EXIT=0(未动任何阈值)·
check:grammar 9/9 与 selftest 9/9 · token-twins · contrast · brand-token 全 EXIT=0
This commit is contained in:
zhangxiang committed 2026-10-17 00:17:40 +08:00
1 parent d9f9ee77cf
commit ffc43fdeb7
32 files changed
+298 -3184

No files matched your search

@@ -1,95 +0,0 @@
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { NewsDetailClient } from './NewsDetailClient';
jest.mock('next/navigation', () => ({
useRouter: () => ({
push: jest.fn(),
back: jest.fn(),
}),
}));
jest.mock('next/link', () => {
const MockLink = ({ children, href }: { children: React.ReactNode; href: string }) => {
return <a href={href}>{children}</a>;
};
MockLink.displayName = 'MockLink';
return MockLink;
});
jest.mock('framer-motion', () => ({
motion: {
div: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
},
useInView: () => true,
}));
const mockNews = {
id: 'test-news',
title: '测试新闻标题',
category: '公司新闻',
date: '2024-01-01',
excerpt: '这是测试新闻摘要',
content: '这是测试新闻内容',
};
describe('NewsDetailClient', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('Rendering', () => {
it('should render news detail page', () => {
render(<NewsDetailClient news={mockNews as any} />);
const titles = screen.getAllByText('测试新闻标题');
expect(titles.length).toBeGreaterThan(0);
});
it('should render news title', () => {
render(<NewsDetailClient news={mockNews as any} />);
const title = screen.getByRole('heading', { level: 1 });
expect(title).toBeInTheDocument();
expect(title).toHaveTextContent('测试新闻标题');
});
it('should render news category', () => {
render(<NewsDetailClient news={mockNews as any} />);
const categories = screen.getAllByText('公司新闻');
expect(categories.length).toBeGreaterThan(0);
});
it('should render news date', () => {
render(<NewsDetailClient news={mockNews as any} />);
const date = screen.getByText('2024-01-01');
expect(date).toBeInTheDocument();
});
it('should render news excerpt', () => {
render(<NewsDetailClient news={mockNews as any} />);
const excerpt = screen.getByText('这是测试新闻摘要');
expect(excerpt).toBeInTheDocument();
});
it('should render news content', () => {
render(<NewsDetailClient news={mockNews as any} />);
const content = screen.getByText('这是测试新闻内容');
expect(content).toBeInTheDocument();
});
});
describe('Navigation', () => {
it('should have back button', () => {
render(<NewsDetailClient news={mockNews as any} />);
const backButtons = screen.getAllByRole('button', { name: /返回/i });
expect(backButtons.length).toBeGreaterThan(0);
});
});
describe('Accessibility', () => {
it('should have proper heading hierarchy', () => {
render(<NewsDetailClient news={mockNews as any} />);
const h1 = screen.getByRole('heading', { level: 1 });
expect(h1).toBeInTheDocument();
});
});
});
@@ -1,155 +0,0 @@
'use client';
import { StaticLink } from '@/components/ui/static-link';
import { CTAButton } from '@/components/ui/cta-button';
import { CTA_LABELS } from '@/lib/constants/cta-labels';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Card } from '@/components/ui/card';
import { Calendar, ArrowLeft, Newspaper } from 'lucide-react';
import { motion } from 'framer-motion';
import { useReducedMotion } from '@/hooks/use-reduced-motion';
import { NEWS } from '@/lib/constants';
const EASE = [0.25, 1, 0.5, 1] as const;
interface NewsDetailClientProps {
news: typeof NEWS[0];
}
export function NewsDetailClient({ news }: NewsDetailClientProps) {
const shouldReduceMotion = useReducedMotion();
const relatedNews = NEWS
.filter((n) => n.id !== news.id && n.category === news.category)
.slice(0, 3);
return (
<div className="min-h-screen bg-[var(--color-bg-primary)]">
{/* Article Header */}
<section className="pt-32 pb-12">
<div className="container-wide">
{/* Breadcrumb */}
<nav aria-label="breadcrumb" className="flex items-center gap-2 text-sm text-[var(--color-text-muted)] mb-8">
<StaticLink href="/news" className="hover:text-brand-ink transition-colors duration-normal">
新闻动态
</StaticLink>
<span>/</span>
<span className="text-[var(--color-text-primary)] line-clamp-1">{news.title}</span>
</nav>
<motion.div
initial={shouldReduceMotion ? {} : { opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.28, ease: EASE }}
className="max-w-4xl"
>
{/* hover 底色用 12% 软红令牌而非 /20:半透明底叠加页面底色后,
浅色主题下 text-brand-ink 实测 4.18:1(< AA 4.5:1),12% 为 4.80:1;深色分别 6.22 / 6.60。 */}
<Badge className="mb-4 bg-[var(--color-brand-bg)] text-brand-ink hover:bg-brand-soft border-0">
{news.category}
</Badge>
<h1 className="text-3xl sm:text-4xl lg:text-5xl font-semibold text-[var(--color-text-primary)] mb-5 tracking-tight leading-tight">
{news.title}
</h1>
<div className="flex items-center gap-4 text-[var(--color-text-muted)] text-sm">
<div className="flex items-center gap-1.5">
<Calendar className="w-4 h-4" />
{news.date}
</div>
</div>
<div className="ink-divider mt-8" />
</motion.div>
</div>
</section>
{/* Article Body */}
<section className="pb-16 md:pb-24">
<div className="container-wide">
<motion.div
initial={shouldReduceMotion ? {} : { opacity: 0, y: 20 }}
whileInView={{ opacity: 1, y: 0 }}
viewport={{ once: true }}
transition={{ duration: 0.28, ease: EASE }}
className="max-w-4xl"
>
{/* Featured Image */}
{news.image ? (
<div className="aspect-video rounded-xl overflow-hidden mb-10">
<img
src={news.image}
alt={news.title}
className="w-full h-full object-cover"
/>
</div>
) : (
<div className="aspect-video bg-gradient-to-br from-[var(--color-brand-bg)] to-[var(--color-bg-tertiary)] rounded-xl mb-10 flex items-center justify-center">
<Newspaper className="w-16 h-16 text-brand-ink/30" />
</div>
)}
{/* Excerpt */}
<Card className="mb-10 p-6">
<p className="text-lg text-[var(--color-text-muted)] leading-relaxed">
{news.excerpt}
</p>
</Card>
{/* Article Content */}
<div className="prose text-[var(--color-text-primary)] text-base sm:text-lg leading-[1.8] whitespace-pre-line">
{news.content}
</div>
{/* Related News */}
{relatedNews.length > 0 && (
<div className="mt-16 pt-12 border-t border-[var(--color-border-primary)]">
<h2 className="text-2xl font-semibold text-[var(--color-text-primary)] mb-8">相关新闻</h2>
<div className="grid md:grid-cols-3 gap-6">
{relatedNews.map((related) => (
<StaticLink key={related.id} href={`/news/${related.id}`}>
<Card className="overflow-hidden group h-full transition-all duration-normal hover:-translate-y-1 hover:shadow-lg">
<div className="aspect-video overflow-hidden">
{related.image ? (
<img
src={related.image}
alt={related.title}
className="w-full h-full object-cover group-hover:scale-105 transition-transform duration-150"
/>
) : (
<div className="w-full h-full bg-gradient-to-br from-[var(--color-brand-bg)] to-[var(--color-bg-tertiary)] flex items-center justify-center">
<Newspaper className="w-8 h-8 text-brand-ink/30" />
</div>
)}
</div>
<div className="p-4">
<Badge variant="secondary" className="mb-2 text-xs">{related.category}</Badge>
<h3 className="text-base font-semibold text-[var(--color-text-primary)] mb-1 line-clamp-2 group-hover:text-brand-ink transition-colors duration-normal">
{related.title}
</h3>
<p className="text-sm text-[var(--color-text-muted)] line-clamp-2 leading-relaxed">{related.excerpt}</p>
</div>
</Card>
</StaticLink>
))}
</div>
</div>
)}
{/* Navigation */}
<div className="mt-12 flex justify-center gap-4">
<StaticLink href="/news">
<Button variant="outline" size="lg">
<ArrowLeft className="w-4 h-4 mr-2" />
返回新闻列表
</Button>
</StaticLink>
<CTAButton href="/contact" dataTestId="news-slug-detail-cta" className="px-8 py-3 text-base font-medium">
{CTA_LABELS.consult}
</CTAButton>
</div>
</motion.div>
</div>
</section>
</div>
);
}