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:
zhangxiang committed 2026-09-28 10:48:07 +08:00
1 parent 040951c0a3
commit a366bd1400
224 files changed
+8938 -5579

No files matched your search

+66 -13
View File
@@ -70,6 +70,32 @@ describe('Analytics', () => {
const result = analytics.getStoredPreferences();
expect(result).toBeNull();
});
// Q-10③:localStorage 可能被篡改/写坏;同意判定必须严格,真值非布尔不得当作已同意(fail-closed)。
it('coerces truthy non-boolean consent fields to false instead of granting', () => {
localStorageMock.setItem(
'novalon-cookie-preferences',
JSON.stringify({ necessary: false, analytics: 'yes', marketing: 1, functionality: true })
);
const result = analytics.getStoredPreferences();
expect(result).toEqual({
necessary: true, // 必要类恒真,不受存储值影响
analytics: false, // 'yes' 非严格 true ⇒ 不授予
marketing: false, // 1 非严格 true ⇒ 不授予
functionality: true,
});
});
it('ignores a non-object payload (array/number) rather than trusting it', () => {
localStorageMock.setItem('novalon-cookie-preferences', '[1,2,3]');
const result = analytics.getStoredPreferences();
expect(result).toEqual({
necessary: true,
analytics: false,
marketing: false,
functionality: false,
});
});
});
describe('storePreferences', () => {
@@ -88,6 +114,8 @@ describe('Analytics', () => {
throw new Error('Storage full');
});
// 本地 spy:jest.setup.js 只静音 info/debug,warn 不再是全局 mock
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
analytics.storePreferences({
necessary: true,
analytics: true,
@@ -95,7 +123,8 @@ describe('Analytics', () => {
functionality: true,
});
expect(console.warn).toHaveBeenCalled();
expect(warnSpy).toHaveBeenCalled();
warnSpy.mockRestore();
});
});
@@ -153,8 +182,10 @@ describe('Analytics', () => {
});
it('should not call gtag when not available', () => {
analytics.trackEvent('click', 'engagement', 'button-1', 1);
// No gtag available, should not throw
expect((window as any).gtag).toBeUndefined();
expect(() =>
analytics.trackEvent('click', 'engagement', 'button-1', 1)
).not.toThrow();
});
});
@@ -192,8 +223,12 @@ describe('Analytics', () => {
});
it('should warn when gtag is not available', () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
analytics.trackError('TEST', 'No gtag');
expect(console.warn).toHaveBeenCalled();
expect(warnSpy).toHaveBeenCalled();
warnSpy.mockRestore();
});
it('should set fatal to "false" string when not fatal', () => {
@@ -205,6 +240,17 @@ describe('Analytics', () => {
const callArgs = gtag.mock.calls[0]?.[2] as Record<string, unknown>;
expect(callArgs?.fatal).toBe('false');
});
it('should not echo tracked errors to the console', () => {
const gtag = jest.fn();
(window as any).gtag = gtag;
const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
analytics.trackError('API_ERROR', 'Failed to fetch', true);
expect(logSpy).not.toHaveBeenCalled();
logSpy.mockRestore();
});
});
describe('trackPageView', () => {
@@ -221,7 +267,8 @@ describe('Analytics', () => {
});
it('should not throw when gtag is not available', () => {
analytics.trackPageView('/', 'Home');
expect((window as any).gtag).toBeUndefined();
expect(() => analytics.trackPageView('/', 'Home')).not.toThrow();
});
});
@@ -326,28 +373,34 @@ describe('Analytics', () => {
const callArgs = gtag.mock.calls[0]?.[2] as Record<string, unknown>;
expect(callArgs?.value).toBe(1);
});
it('should keep an explicit value of 0', () => {
const gtag = jest.fn();
(window as any).gtag = gtag;
analytics.trackConversion('zero_value_conversion', 0);
const callArgs = gtag.mock.calls[0]?.[2] as Record<string, unknown>;
expect(callArgs?.value).toBe(0);
});
});
describe('trackOutboundLink', () => {
it('should call gtag with outbound link data', () => {
it('should send exactly one outbound_click event', () => {
const gtag = jest.fn();
(window as any).gtag = gtag;
analytics.trackOutboundLink('https://example.com', 'Example');
// 旧实现同时发 outbound_click 与 click,GA4 里每次外跳被双计。
expect(gtag).toHaveBeenCalledTimes(1);
expect(gtag).toHaveBeenCalledWith('event', 'outbound_click', {
event_category: 'engagement',
event_label: 'https://example.com',
event_value: undefined,
send_to: 'G-TEST123',
});
expect(gtag).toHaveBeenCalledWith('event', 'click', {
event_category: 'outbound',
event_label: 'https://example.com',
transport_type: 'beacon',
send_to: 'G-TEST123',
});
expect(gtag).not.toHaveBeenCalledWith('event', 'click', expect.anything());
});
});