/** * 新闻正文结构解析的三条判据(每条都能失败): * 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); } }); });