Batch 2 的实现本体。补的是「内容」不是「语法」:`/news` 与 `/insights` 此前各只有 250 字左右的正文。 ① 两篇公开正文 238/313 字 → 914/758 字,全部从既有真源派生(METHODOLOGY 四阶段 / SERVICES 四组 / PRODUCTS 六款 / commitments.ts 响应承诺), **零新增客户案例、零新增效果数字**。 ② 详情页正文由整块 `whitespace-pre-line` 改为小标题 + 段落 (`parseArticleBlocks`,三条判据:单行 + ≤20 字 + 不以句末标点收尾 —— 误判成标题比漏判更糟)。 ③ 正文末尾新增站内公开入口(`NEWS_RELATED_LINKS`)。此前详情页是死胡同, 只有「返回列表」与「预约咨询」。href 由单测**双向**反查:既命中 PRODUCTS/SERVICES 常量,又要求文件系统里 page.tsx 真实存在。 ④ 阅读时长由正文字数实时算出(450 字/分钟),不手写不落库。 ⑤ `/insights` 卡片显式写出「收录于」哪个原生栏目。 **不新增 `/insights/[slug]`** —— 聚合页只能读、不能拥有别的模型的实体, 复制内容会造成 canonical 冲突。 判别性测试:`news-article.test.ts` 11 例 + `news-detail-content-v3.test.tsx` 4 例 (负对照:关掉解析器 ⇒ 3 条转红)+ e2e UJ-04/UJ-10 三条新断言 (负对照:注回整块渲染后 UJ-04 转红)。
121 lines
5.3 KiB
TypeScript
121 lines
5.3 KiB
TypeScript
/**
|
||
* 新闻正文结构解析的三条判据(每条都能失败):
|
||
* 1. 小标题被识别;
|
||
* 2. **以标点收尾的长句不会被误判成标题**(误判的代价:一整句正文被放大成 h2);
|
||
* 3. 空输入 / 只有空白的输入返回空数组(不是 `[{kind:'paragraph',text:''}]`)。
|
||
*
|
||
* 另附两个契约守卫(来自 Batch 2「内页证据密度」):
|
||
* - 每篇正文的厚度下限 —— 防止后续改动把正文悄悄退回一二百字;
|
||
* - 相关入口的 href 必须指向**真实存在的路由**(新造一个 /services/xxx 是零编造)。
|
||
*/
|
||
import { describe, it, expect } from '@jest/globals';
|
||
import { existsSync } from 'fs';
|
||
import path from 'path';
|
||
import { parseArticleBlocks, articleCharCount, TITLE_MAX_LEN } from './news-article';
|
||
import { NEWS, NEWS_RELATED_LINKS } from './constants/news';
|
||
import { PRODUCTS } from './constants/products';
|
||
import { SERVICES } from './constants/services';
|
||
|
||
const REPO_ROOT = path.resolve(__dirname, '../..');
|
||
|
||
describe('parseArticleBlocks', () => {
|
||
it('识别小标题与段落,并保持源文顺序', () => {
|
||
const blocks = parseArticleBlocks('我们是谁\n\n这是一段正文,讲我们是谁。\n\n我们怎么工作\n\n分四个阶段推进。');
|
||
expect(blocks.map((b) => b.kind)).toEqual(['heading', 'paragraph', 'heading', 'paragraph']);
|
||
expect(blocks[0]).toEqual({ kind: 'heading', text: '我们是谁' });
|
||
});
|
||
|
||
it('以句末标点收尾的短块**不得**被判成标题(否则整句变 h2)', () => {
|
||
// 长度 13 字 ≤ TITLE_MAX_LEN,唯一能阻止它变成标题的就是「以标点收尾」这条判据。
|
||
const text = '这四个阶段都要求书面产物。';
|
||
expect(text.length).toBeLessThanOrEqual(TITLE_MAX_LEN);
|
||
const blocks = parseArticleBlocks(text);
|
||
expect(blocks).toHaveLength(1);
|
||
expect(blocks[0]!.kind).toBe('paragraph');
|
||
});
|
||
|
||
it('跨行的块不得被判成标题(标题不换行)', () => {
|
||
const blocks = parseArticleBlocks('短句\n另一个短句');
|
||
expect(blocks).toHaveLength(1);
|
||
expect(blocks[0]!.kind).toBe('paragraph');
|
||
});
|
||
|
||
it('超过长度上限的块不得被判成标题', () => {
|
||
const long = '一'.repeat(TITLE_MAX_LEN + 1);
|
||
const blocks = parseArticleBlocks(long);
|
||
expect(blocks[0]!.kind).toBe('paragraph');
|
||
});
|
||
|
||
it('空输入 / 纯空白返回空数组,不返回一个空段落', () => {
|
||
expect(parseArticleBlocks('')).toEqual([]);
|
||
expect(parseArticleBlocks(null)).toEqual([]);
|
||
expect(parseArticleBlocks(undefined)).toEqual([]);
|
||
expect(parseArticleBlocks(' \n\n \n ')).toEqual([]);
|
||
});
|
||
});
|
||
|
||
describe('articleCharCount', () => {
|
||
it('去掉空白后计字符(不把缩进算成正文厚度)', () => {
|
||
expect(articleCharCount('一 二\n\t三')).toBe(3);
|
||
expect(articleCharCount('')).toBe(0);
|
||
});
|
||
});
|
||
|
||
describe('NEWS 正文厚度与相关入口契约', () => {
|
||
/** 下限取 600:现有两篇实测都在 900+,门槛是「防退回」而不是「凑字数」。 */
|
||
const MIN_BODY_CHARS = 600;
|
||
|
||
it('每篇正文都达到厚度下限(防后续改动悄悄退回短正文)', () => {
|
||
for (const item of NEWS) {
|
||
const n = articleCharCount(item.content);
|
||
expect({ id: item.id, chars: n >= MIN_BODY_CHARS }).toEqual({ id: item.id, chars: true });
|
||
}
|
||
});
|
||
|
||
it('每篇正文都含至少两个小标题(结构真的解析出来了,不是一整坨段落)', () => {
|
||
for (const item of NEWS) {
|
||
const headings = parseArticleBlocks(item.content).filter((b) => b.kind === 'heading');
|
||
expect({ id: item.id, headings: headings.length >= 2 }).toEqual({ id: item.id, headings: true });
|
||
}
|
||
});
|
||
|
||
it('相关入口的 href 全部指向真实存在的页面(零编造:不新造路由)', () => {
|
||
const productHrefs = new Set(PRODUCTS.map((p) => `/products/${p.id}`));
|
||
const serviceHrefs = new Set(SERVICES.map((s) => `/services/${s.id}`));
|
||
const staticHrefs = new Set(['/methodology', '/products', '/services', '/insights', '/news', '/contact', '/about']);
|
||
|
||
const unknown = NEWS_RELATED_LINKS
|
||
.map((l) => l.href)
|
||
// 动态详情路由:在 products 常量里能找到对应 id 才算存在
|
||
.filter((href) => {
|
||
const m = /^\/(products|services)\/(.+)$/.exec(href);
|
||
if (m) {
|
||
return m[1] === 'products' ? !productHrefs.has(href) : !serviceHrefs.has(href);
|
||
}
|
||
return !staticHrefs.has(href);
|
||
});
|
||
expect(unknown).toEqual([]);
|
||
});
|
||
|
||
it('相关入口引用的页面文件真实存在(App Router 里没这个目录就是 404)', () => {
|
||
const toDir = (href: string) => {
|
||
const m = /^\/(products|services)\/(.+)$/.exec(href);
|
||
if (m) return path.join(REPO_ROOT, 'src/app/(marketing)', m[1]!, '[id]');
|
||
return path.join(REPO_ROOT, 'src/app/(marketing)', href.replace(/^\//, ''));
|
||
};
|
||
for (const link of NEWS_RELATED_LINKS) {
|
||
const dir = toDir(link.href);
|
||
expect({ href: link.href, exists: existsSync(path.join(dir, 'page.tsx')) }).toEqual({
|
||
href: link.href,
|
||
exists: true,
|
||
});
|
||
}
|
||
});
|
||
|
||
it('相关入口每条都写了 reason(不留只有一个标题的空壳链接)', () => {
|
||
for (const link of NEWS_RELATED_LINKS) {
|
||
expect(link.reason.trim().length).toBeGreaterThan(4);
|
||
}
|
||
});
|
||
});
|