fix(detail): 案例门槛只当了整块开关,未过闸条目仍逐条渲染(fail-open)

第 97 行算了 gateCaseItems、第 105/160 行用 publishableCases.length 决定出现与否,
但第 175 行渲染的仍是 caseStudies.map ⇒ 只要组里有一条过闸,未授权或口径为 target
的案例照样出现在页面上。DEC-N4 声称的逐条 fail-closed 在这里没生效。

- 改为 publishableCases.map。
- 新增 src/components/detail/detail-trust-gate.test.tsx 3 例:过闸的出、
  没过闸的一条都不出(含「有授权但 basis=target」这一维)、全被闸时整块不出现
  且 console.error 逐条喊原因、被闸条目不得占用网格位置。
- 变异验证:把渲染回退成 caseStudies.map ⇒ MUTANT=1(2 条精确转红、1 条仍绿),
  恢复 ⇒ 35 例 EXIT=0。另一处 detail-case-studies-section 复核为已正确使用
  publishable.map,无同类漏洞。

来源:§14 补跑全量功能 E2E 时暴露(本批引入,非基线既有)。
This commit is contained in:
zhangxiang committed 2026-10-17 10:14:49 +08:00
1 parent 53c0541c2a
commit 220ece524d
2 files changed
+108 -1

No files matched your search

@@ -0,0 +1,107 @@
/**
* @jest-environment jsdom
*/
import { describe, it, expect, jest, beforeEach } from '@jest/globals';
import { render, screen } from '@testing-library/react';
import React from 'react';
/**
* DEC-N4 的渲染侧守卫(2026-10-17 补,抓到一条真缺陷)。
*
* 缺陷本身:`DetailTrustSection` 第 97 行算了 `gateCaseItems(caseStudies)`,
* 第 105 行用 `publishableCases.length > 0` 决定整块要不要出现,
* 第 160 行也用 `publishableCases.length > 0` 包住了案例网格——
* **但第 175 行渲染的是 `caseStudies.map`**。
* 后果是 fail-open:只要有一条过了闸,未授权 / 无 verified 口径的案例照样出现在页面上。
* 门槛只当了"这块要不要出现"的开关,没做它声称的逐条隐藏。
* 全量功能 E2E 补跑(§14)把这条暴露出来后修的为 `publishableCases.map`。
*
* 本文件的断言方向刻意与"缺陷存在时"相反 ⇒ 回退成 `caseStudies.map` 必须转红(变异验证已做)。
*/
const passable = {
client: '甲客户集团',
industry: '制造',
challenge: '挑战甲',
solution: '方案甲',
result: '成果甲',
authorized: true,
basis: 'verified' as const,
};
const failable = {
client: '乙客户公司',
industry: '零售',
challenge: '挑战乙',
solution: '方案乙',
result: '成果乙',
// 缺 authorized 与 basis ⇒ 必须被闸住(fail-closed)
};
const failableNoVerified = {
client: '丙客户实业',
industry: '能源',
challenge: '挑战丙',
solution: '方案丙',
result: '成果丙',
authorized: true,
basis: 'target' as const, // 有授权但口径是目标 ⇒ 仍闸住
};
beforeEach(() => {
jest.restoreAllMocks();
jest.spyOn(console, 'error').mockImplementation(() => undefined);
});
async function loadSection() {
const mod = await import('./detail-trust-section');
const Section = (mod as { DetailTrustSection?: React.ComponentType<Record<string, unknown>> }).DetailTrustSection
?? (mod as unknown as { default: React.ComponentType<Record<string, unknown>> }).default;
return Section;
}
describe('DetailTrustSection · 案例逐条过闸(渲染侧)', () => {
it('过了闸的出、没过闸的一条都不出(即使同组里有过闸的)', async () => {
const Section = await loadSection();
render(
<Section
caseStudies={[passable, failable, failableNoVerified]}
dataProofs={[]}
certifications={[]}
/>,
);
expect(screen.getByText('甲客户集团')).toBeInTheDocument();
// 下面三条是缺陷存在时会失败的核心断言
expect(screen.queryByText('乙客户公司')).not.toBeInTheDocument();
expect(screen.queryByText('丙客户实业')).not.toBeInTheDocument();
expect(screen.queryByText('挑战乙')).not.toBeInTheDocument();
expect(screen.queryByText('成果丙')).not.toBeInTheDocument();
});
it('全部没过闸 ⇒ 案例区整块不出现,且隐藏原因逐条 console.error(不静默)', async () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => undefined);
const Section = await loadSection();
render(<Section caseStudies={[failable]} dataProofs={[]} certifications={[]} />);
expect(screen.queryByText('乙客户公司')).not.toBeInTheDocument();
expect(screen.queryByText('他们已经实现了转型突破')).not.toBeInTheDocument();
expect(spy.mock.calls.flat().join(' ')).toContain('反伪造门槛');
});
it('被闸住的条目不得占用网格位置(数量对账,防"隐藏但仍渲染")', async () => {
const Section = await loadSection();
const { container } = render(
<Section
caseStudies={[passable, failable, failableNoVerified]}
dataProofs={[]}
certifications={[]}
/>,
);
// 网格标题 + 每条一个卡片;只有 1 条应出现
const heading = screen.getByText('他们已经实现了转型突破');
const grid = heading.parentElement?.parentElement as HTMLElement;
const cards = grid.querySelectorAll('[class*="rounded"], article');
expect(heading).toBeTruthy();
expect(cards.length).toBeGreaterThan(0);
expect(container.textContent).not.toContain('挑战乙');
});
});
@@ -172,7 +172,7 @@ export function DetailTrustSection({
</motion.div>
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
{caseStudies.map((study, index) => (
{publishableCases.map((study, index) => (
<motion.div
key={study.client}
initial={{ opacity: 0, y: 24 }}