fix(qa): 第五轮系统性质量保障缺陷修复 + 单元/集成测试
安全/鉴权:登出清服务端 httpOnly 令牌、登录 IP 限流前置 bcrypt、停用账号 令牌在 6 个内联 role handler 失效、admin/items 存在性 oracle 认证前置、 同意偏好 sanitize fail-closed、api-crypto 按 HTTP 方法判体。 CMS:workflow 状态更新+审计收进交互式 $transaction(失败即回滚)、编辑器 richtext 走 textarea 防换行损毁、array/object 子字段按作用域读写防污染、 about/contact/erp-upgrade 补 ISR revalidate 与路由映射。 UI/a11y:AnimatedCounter 入视口前显起点、CLS 累计上报、MetricCard 动画入 effect 并卸载取消 rAF、后台状态四态映射/搜索防抖+序号守卫/媒体单 input/ 吞错改横幅/表单 label-aria 关联。 新增对应判别性单测与 pagination/rate-limit/client-ip/sanitize/validate-content-data 等模块及 tests-integration 真库集成层。
This commit is contained in:
1 parent
040951c0a3
commit
a366bd1400
224 files changed
+8938
-5579
No files matched your search
@@ -168,7 +168,7 @@ export function Footer() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="border-t border-dark-border pt-6 pb-[calc(8rem+env(safe-area-inset-bottom,0px))] md:pb-8">
|
||||
<div className="border-t border-dark-border pt-6 pb-8">
|
||||
<div className="flex flex-col md:flex-row justify-between items-start md:items-center gap-6 mb-6">
|
||||
<p className="text-dark-text-secondary text-xs">
|
||||
© {new Date().getFullYear()} {config.name}. 版权所有.
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { describe, it, expect, jest, beforeEach, afterEach } from '@jest/globals';
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
|
||||
import { render, screen, fireEvent, waitFor, within, act } from '@testing-library/react';
|
||||
import '@testing-library/jest-dom';
|
||||
|
||||
const mockPush = jest.fn();
|
||||
const mockReplace = jest.fn();
|
||||
const mockUseFocusTrap = jest.fn((_isActive: boolean, _options?: unknown) => ({ current: null }));
|
||||
|
||||
jest.mock('next/navigation', () => ({
|
||||
usePathname: jest.fn(() => '/'),
|
||||
@@ -128,7 +129,7 @@ jest.mock('@/lib/constants', () => ({
|
||||
}));
|
||||
|
||||
jest.mock('@/hooks/use-focus-trap', () => ({
|
||||
useFocusTrap: () => ({ current: null }),
|
||||
useFocusTrap: (isActive: boolean, options?: unknown) => mockUseFocusTrap(isActive, options),
|
||||
}));
|
||||
|
||||
jest.mock('@/components/layout/mega-dropdown', () => ({
|
||||
@@ -236,12 +237,71 @@ describe('Header', () => {
|
||||
it('should have proper ARIA attributes on menu button', () => {
|
||||
render(<Header />);
|
||||
const menuButton = screen.getByTestId('mobile-menu-button');
|
||||
|
||||
|
||||
expect(menuButton).toHaveAttribute('aria-expanded', 'false');
|
||||
expect(menuButton).toHaveAttribute('aria-controls', 'mobile-menu');
|
||||
expect(menuButton).toHaveAttribute('aria-label', '打开菜单');
|
||||
});
|
||||
|
||||
it('should not expose a dangling aria-controls target while the drawer is closed', () => {
|
||||
render(<Header />);
|
||||
const menuButton = screen.getByTestId('mobile-menu-button');
|
||||
|
||||
// aria-controls 指向只存在于 {isOpen && …} 内的 id;关闭时必须撤掉,
|
||||
// 否则 AT 解析到一个不存在的节点。
|
||||
expect(menuButton).not.toHaveAttribute('aria-controls');
|
||||
expect(document.getElementById('mobile-menu')).toBeNull();
|
||||
});
|
||||
|
||||
it('should point aria-controls at a real element once the drawer is open', async () => {
|
||||
render(<Header />);
|
||||
const menuButton = screen.getByTestId('mobile-menu-button');
|
||||
|
||||
fireEvent.click(menuButton);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(menuButton).toHaveAttribute('aria-controls', 'mobile-menu');
|
||||
});
|
||||
const target = document.getElementById(menuButton.getAttribute('aria-controls') as string);
|
||||
expect(target).not.toBeNull();
|
||||
expect(target).toBe(screen.getByTestId('mobile-navigation'));
|
||||
});
|
||||
|
||||
it('should hand Escape back to the drawer owner through useFocusTrap onEscape', async () => {
|
||||
render(<Header />);
|
||||
const menuButton = screen.getByTestId('mobile-menu-button');
|
||||
fireEvent.click(menuButton);
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId('mobile-navigation')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
const options = mockUseFocusTrap.mock.calls
|
||||
.map((call) => call[1] as { onEscape?: () => void } | undefined)
|
||||
.find((call) => typeof call?.onEscape === 'function');
|
||||
expect(options?.onEscape).toBeInstanceOf(Function);
|
||||
|
||||
// 焦点归还/关闭由调用方决定:onEscape 必须真的关闭抽屉。
|
||||
act(() => {
|
||||
options?.onEscape?.();
|
||||
});
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByTestId('mobile-navigation')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('should render the theme toggle inside the mobile drawer with a >=44px target', async () => {
|
||||
render(<Header />);
|
||||
fireEvent.click(screen.getByTestId('mobile-menu-button'));
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId('mobile-navigation')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
const drawer = screen.getByTestId('mobile-navigation');
|
||||
const toggle = within(drawer).getByTestId('theme-toggle');
|
||||
// w-11 / h-11 = 2.75rem = 44px(Tailwind 默认刻度)
|
||||
expect(toggle).toHaveClass('w-11');
|
||||
expect(toggle).toHaveClass('h-11');
|
||||
});
|
||||
|
||||
it('should update aria-expanded when menu is toggled', async () => {
|
||||
render(<Header />);
|
||||
const menuButton = screen.getByTestId('mobile-menu-button');
|
||||
|
||||
@@ -18,7 +18,9 @@ function HeaderContent() {
|
||||
const [isScrolled, setIsScrolled] = useState(false);
|
||||
const [openDropdown, setOpenDropdown] = useState<string | null>(null);
|
||||
const pathname = usePathname();
|
||||
const focusTrapRef = useFocusTrap<HTMLDivElement>(isOpen);
|
||||
const focusTrapRef = useFocusTrap<HTMLDivElement>(isOpen, {
|
||||
onEscape: () => setIsOpen(false),
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const handleScroll = () => {
|
||||
@@ -86,8 +88,8 @@ function HeaderContent() {
|
||||
animate={{
|
||||
height: isScrolled ? 64 : 80,
|
||||
}}
|
||||
transition={{ duration: 0.3, ease: [0.22, 1, 0.36, 1] }}
|
||||
className="fixed top-0 left-0 right-0 z-50 transition-colors duration-300 bg-bg-primary border-b border-border-primary"
|
||||
transition={{ duration: 0.28, ease: [0.22, 1, 0.36, 1] }}
|
||||
className="fixed top-0 left-0 right-0 z-50 transition-colors duration-normal bg-bg-primary border-b border-border-primary"
|
||||
>
|
||||
<div className="max-w-container mx-auto px-4 sm:px-6 lg:px-10">
|
||||
<div className="flex items-center justify-between h-16 md:h-full">
|
||||
@@ -103,7 +105,7 @@ function HeaderContent() {
|
||||
alt={config.name}
|
||||
width={160}
|
||||
height={40}
|
||||
className="logo-on-light relative transition-all duration-300 ease-out group-hover/logo:scale-[1.02] w-auto h-8 md:h-10"
|
||||
className="logo-on-light relative transition-all duration-normal ease-out group-hover/logo:scale-[1.02] w-auto h-8 md:h-10"
|
||||
loading="eager"
|
||||
priority
|
||||
suppressHydrationWarning
|
||||
@@ -113,7 +115,7 @@ function HeaderContent() {
|
||||
alt={config.name}
|
||||
width={160}
|
||||
height={40}
|
||||
className="logo-on-dark relative transition-all duration-300 ease-out group-hover/logo:scale-[1.02] w-auto h-8 md:h-10"
|
||||
className="logo-on-dark relative transition-all duration-normal ease-out group-hover/logo:scale-[1.02] w-auto h-8 md:h-10"
|
||||
loading="eager"
|
||||
suppressHydrationWarning
|
||||
/>
|
||||
@@ -141,7 +143,7 @@ function HeaderContent() {
|
||||
onClick={handleNavClick}
|
||||
className={cn(
|
||||
'relative inline-flex items-center px-3 py-2 text-sm font-medium',
|
||||
'transition-all duration-300 ease-out',
|
||||
'transition-all duration-normal ease-out',
|
||||
isActive(item)
|
||||
? 'text-text-primary'
|
||||
: 'text-text-secondary hover:text-text-primary hover:bg-bg-secondary'
|
||||
@@ -152,7 +154,7 @@ function HeaderContent() {
|
||||
<span
|
||||
className={cn(
|
||||
'absolute -bottom-0.5 left-3 right-3 h-0.5 bg-brand',
|
||||
'transition-all duration-300 ease-out',
|
||||
'transition-all duration-normal ease-out',
|
||||
isActive(item)
|
||||
? 'opacity-100 scale-x-100'
|
||||
: 'opacity-0 scale-x-0'
|
||||
@@ -180,11 +182,12 @@ function HeaderContent() {
|
||||
</div>
|
||||
|
||||
<button
|
||||
className="md:hidden p-2 -mr-2 text-text-secondary hover:text-text-primary hover:bg-bg-secondary rounded-sm transition-all duration-300 ease-out active:scale-95"
|
||||
onClick={() => setIsOpen(!isOpen)}
|
||||
className="md:hidden p-2 -mr-2 text-text-secondary hover:text-text-primary hover:bg-bg-secondary rounded-sm transition-all duration-normal ease-out active:scale-95"
|
||||
onClick={() => setIsOpen((prev) => !prev)}
|
||||
onKeyDown={handleKeyDown}
|
||||
aria-expanded={isOpen}
|
||||
aria-controls="mobile-menu"
|
||||
/* 面板仅在 isOpen 时挂载:关闭时不给 aria-controls,避免指向不存在的 id。 */
|
||||
aria-controls={isOpen ? 'mobile-menu' : undefined}
|
||||
aria-label={isOpen ? '关闭菜单' : '打开菜单'}
|
||||
data-testid="mobile-menu-button"
|
||||
style={{ minWidth: '44px', minHeight: '44px' }}
|
||||
@@ -221,6 +224,12 @@ function HeaderContent() {
|
||||
aria-label="移动端导航"
|
||||
data-testid="mobile-navigation"
|
||||
>
|
||||
{/* 桌面端 ThemeToggle 在 `hidden md:flex` 容器内,移动端只有抽屉可达;
|
||||
w-11 h-11 = 44px,满足触摸目标下限。放在 nav 之外,避免混入导航链接列表。 */}
|
||||
<div className="max-w-container mx-auto px-4 sm:px-6 pt-6 flex items-center justify-between">
|
||||
<span className="text-sm text-text-secondary">外观主题</span>
|
||||
<ThemeToggle className="w-11 h-11" />
|
||||
</div>
|
||||
<nav className="max-w-container mx-auto px-4 sm:px-6 py-6">
|
||||
{config.mainNav.map((item, index) => (
|
||||
<motion.div
|
||||
@@ -234,7 +243,7 @@ function HeaderContent() {
|
||||
onClick={handleNavClick}
|
||||
className={`
|
||||
block px-4 py-4 text-base font-medium
|
||||
transition-all duration-300 ease-out
|
||||
transition-all duration-normal ease-out
|
||||
${isActive(item)
|
||||
? 'text-text-primary bg-bg-secondary'
|
||||
: 'text-text-secondary hover:text-text-primary hover:bg-bg-secondary'
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
export { Header } from './header';
|
||||
export { Footer } from './footer';
|
||||
export { Breadcrumb } from './breadcrumb';
|
||||
export { MobileTabBar } from './mobile-tab-bar';
|
||||
export { MegaDropdown } from './mega-dropdown';
|
||||
export { PageNav } from './page-nav';
|
||||
export { ClientLayout } from './client-layout';
|
||||
@@ -0,0 +1,88 @@
|
||||
import { describe, it, expect, jest, beforeEach } from '@jest/globals';
|
||||
import { render, screen, fireEvent } from '@testing-library/react';
|
||||
import '@testing-library/jest-dom';
|
||||
import { MegaDropdown } from './mega-dropdown';
|
||||
import type { MegaDropdownGroup } from '@/lib/site-config';
|
||||
|
||||
jest.mock('framer-motion', () => ({
|
||||
motion: {
|
||||
div: ({ children, className, ...props }: { children: React.ReactNode; className?: string; [key: string]: unknown }) => (
|
||||
<div className={className} {...props}>
|
||||
{children}
|
||||
</div>
|
||||
),
|
||||
},
|
||||
AnimatePresence: ({ children }: { children: React.ReactNode }) => <>{children}</>,
|
||||
}));
|
||||
|
||||
jest.mock('@/components/ui/static-link', () => {
|
||||
const MockStaticLink = ({ children, href, ...props }: { children: React.ReactNode; href: string; [key: string]: unknown }) => (
|
||||
<a href={href} {...props}>
|
||||
{children}
|
||||
</a>
|
||||
);
|
||||
MockStaticLink.displayName = 'MockStaticLink';
|
||||
return { StaticLink: MockStaticLink };
|
||||
});
|
||||
|
||||
const GROUPS: MegaDropdownGroup[] = [
|
||||
{
|
||||
id: 'enterprise',
|
||||
title: '企业套装',
|
||||
items: [{ id: 'erp', title: 'ERP', description: '企业资源计划', href: '/products/erp' }],
|
||||
},
|
||||
];
|
||||
|
||||
function renderDropdown(overrides: Partial<Parameters<typeof MegaDropdown>[0]> = {}) {
|
||||
const props = {
|
||||
label: '产品',
|
||||
groups: GROUPS,
|
||||
isOpen: true,
|
||||
onToggle: jest.fn(),
|
||||
onOpen: jest.fn(),
|
||||
onClose: jest.fn(),
|
||||
...overrides,
|
||||
};
|
||||
render(<MegaDropdown {...props} />);
|
||||
return props;
|
||||
}
|
||||
|
||||
describe('MegaDropdown 键盘可达性(验收 B-10)', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('Escape 关闭面板并把焦点交还触发按钮', () => {
|
||||
const props = renderDropdown();
|
||||
const trigger = screen.getByRole('button', { name: /产品/ });
|
||||
trigger.focus();
|
||||
|
||||
fireEvent.keyDown(trigger, { key: 'Escape' });
|
||||
|
||||
expect(props.onClose).toHaveBeenCalledTimes(1);
|
||||
expect(trigger).toHaveFocus();
|
||||
});
|
||||
|
||||
it('面板内的链接上按 Escape 同样关闭', () => {
|
||||
const props = renderDropdown();
|
||||
fireEvent.keyDown(screen.getByRole('link', { name: /ERP/ }), { key: 'Escape' });
|
||||
expect(props.onClose).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('面板未打开时 Escape 不触发关闭', () => {
|
||||
const props = renderDropdown({ isOpen: false });
|
||||
fireEvent.keyDown(screen.getByRole('button', { name: /产品/ }), { key: 'Escape' });
|
||||
expect(props.onClose).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('焦点移出容器时关闭,容器内部移动不关闭', () => {
|
||||
const props = renderDropdown();
|
||||
const trigger = screen.getByRole('button', { name: /产品/ });
|
||||
|
||||
fireEvent.blur(trigger, { relatedTarget: trigger });
|
||||
expect(props.onClose).not.toHaveBeenCalled();
|
||||
|
||||
fireEvent.blur(trigger, { relatedTarget: document.body });
|
||||
expect(props.onClose).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { useRef, useEffect } from 'react';
|
||||
import { useRef, useEffect, type KeyboardEvent, type FocusEvent } from 'react';
|
||||
import { ChevronDown, Lock, Sparkles } from 'lucide-react';
|
||||
import { StaticLink } from '@/components/ui/static-link';
|
||||
import { AnimatePresence, motion } from 'framer-motion';
|
||||
@@ -18,6 +18,7 @@ interface MegaDropdownProps {
|
||||
|
||||
export function MegaDropdown({ label, groups, isOpen, isActive, onToggle, onOpen, onClose }: MegaDropdownProps) {
|
||||
const dropdownRef = useRef<HTMLDivElement>(null);
|
||||
const triggerRef = useRef<HTMLButtonElement>(null);
|
||||
const timeoutRef = useRef<ReturnType<typeof setTimeout>>(undefined);
|
||||
|
||||
const handleMouseEnter = () => {
|
||||
@@ -29,18 +30,41 @@ export function MegaDropdown({ label, groups, isOpen, isActive, onToggle, onOpen
|
||||
timeoutRef.current = setTimeout(onClose, 300);
|
||||
};
|
||||
|
||||
// 键盘与无指针设备(验收 B-10):Escape 关闭并把焦点还给触发按钮;
|
||||
// 焦点移出整个下拉容器时关闭,否则面板会以「看不见但仍在 DOM 里」的状态滞留。
|
||||
const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
|
||||
if (event.key !== 'Escape' || !isOpen) return;
|
||||
event.stopPropagation();
|
||||
onClose();
|
||||
triggerRef.current?.focus();
|
||||
};
|
||||
|
||||
const handleBlur = (event: FocusEvent<HTMLDivElement>) => {
|
||||
if (!isOpen) return;
|
||||
if (event.relatedTarget instanceof Node && event.currentTarget.contains(event.relatedTarget)) return;
|
||||
onClose();
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
return () => clearTimeout(timeoutRef.current);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div ref={dropdownRef} className="relative" onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave}>
|
||||
<div
|
||||
ref={dropdownRef}
|
||||
className="relative"
|
||||
onMouseEnter={handleMouseEnter}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
onKeyDown={handleKeyDown}
|
||||
onBlur={handleBlur}
|
||||
>
|
||||
<div className="-mx-2 px-2 py-2">
|
||||
<button
|
||||
ref={triggerRef}
|
||||
onClick={onToggle}
|
||||
className={`
|
||||
relative flex items-center gap-1 px-3 py-2 text-sm font-medium
|
||||
transition-all duration-300 ease-out
|
||||
transition-all duration-normal ease-out
|
||||
${isOpen || isActive
|
||||
? 'text-text-primary'
|
||||
: 'text-text-secondary hover:text-text-primary hover:bg-bg-secondary'
|
||||
@@ -57,7 +81,7 @@ export function MegaDropdown({ label, groups, isOpen, isActive, onToggle, onOpen
|
||||
<span
|
||||
className={`
|
||||
absolute -bottom-0.5 left-3 right-3 h-0.5 bg-brand
|
||||
transition-all duration-300 ease-out
|
||||
transition-all duration-normal ease-out
|
||||
${isActive
|
||||
? 'opacity-100 scale-x-100'
|
||||
: 'opacity-0 scale-x-0'
|
||||
@@ -129,7 +153,7 @@ export function MegaDropdown({ label, groups, isOpen, isActive, onToggle, onOpen
|
||||
<StaticLink
|
||||
key={item.id}
|
||||
href={item.href}
|
||||
className="block p-3 border border-transparent transition-all duration-300 ease-out group hover:border-border-primary hover:bg-bg-secondary"
|
||||
className="block p-3 border border-transparent transition-all duration-normal ease-out group hover:border-border-primary hover:bg-bg-secondary"
|
||||
>
|
||||
{content}
|
||||
</StaticLink>
|
||||
|
||||
@@ -54,13 +54,13 @@ export function MobileTabBar() {
|
||||
<div className="relative flex flex-col items-center justify-center py-2">
|
||||
<Icon
|
||||
className={cn(
|
||||
'w-6 h-6 transition-colors duration-300',
|
||||
'w-6 h-6 transition-colors duration-normal',
|
||||
active ? 'text-brand-ink' : 'text-text-muted group-hover:text-text-secondary'
|
||||
)}
|
||||
/>
|
||||
<span
|
||||
className={cn(
|
||||
'text-xs mt-1 transition-colors duration-300',
|
||||
'text-xs mt-1 transition-colors duration-normal',
|
||||
active ? 'text-brand-ink font-medium' : 'text-text-muted'
|
||||
)}
|
||||
>
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
'use client';
|
||||
|
||||
import { StaticLink } from '@/components/ui/static-link';
|
||||
import { ChevronRight, Home } from 'lucide-react';
|
||||
|
||||
interface BreadcrumbItem {
|
||||
label: string;
|
||||
href?: string;
|
||||
}
|
||||
|
||||
interface PageNavProps {
|
||||
items: BreadcrumbItem[];
|
||||
}
|
||||
|
||||
export function PageNav({ items }: PageNavProps) {
|
||||
return (
|
||||
<nav aria-label="breadcrumb" className="flex items-center gap-[3px] md:gap-1 text-[11px] md:text-sm text-[var(--color-text-subtle)] mb-2 md:mb-8 -ml-0.5">
|
||||
<StaticLink href="/" className="flex items-center w-fit hover:text-[var(--color-brand)] transition-colors" aria-label="返回首页">
|
||||
<Home className="w-2.5 h-2.5 md:w-3.5 md:h-3.5" />
|
||||
</StaticLink>
|
||||
{items.map((item, index) => {
|
||||
const isLast = index === items.length - 1;
|
||||
return (
|
||||
<div key={index} className="flex items-center gap-[3px] md:gap-1">
|
||||
<ChevronRight className="w-2.5 h-2.5 md:w-3.5 md:h-3.5 text-[var(--color-border-secondary)]" />
|
||||
{isLast || !item.href ? (
|
||||
<span className={isLast ? 'text-[var(--color-text-primary)] font-medium' : ''}>
|
||||
{item.label}
|
||||
</span>
|
||||
) : (
|
||||
<StaticLink
|
||||
href={item.href}
|
||||
className="hover:text-[var(--color-brand)] transition-colors"
|
||||
>
|
||||
{item.label}
|
||||
</StaticLink>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</nav>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user