refactor(代码重复): 抽取共享工具函数与 UI 组件,消除多处重复实现

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
雨霖铃
2026-06-19 20:55:39 +08:00
parent 47d74f999e
commit 0f3060dcf3
24 changed files with 386 additions and 309 deletions
+4 -4
View File
@@ -77,15 +77,15 @@ describe('chromeStorage', () => {
describe('get 类型签名', () => {
it('无默认值时应推断为可选返回类型', () => {
const getWithoutDefault = () => storageUtil.get('app/theme');
expectTypeOf<ReturnType<typeof getWithoutDefault>>().toEqualTypeOf<
const _getWithoutDefault = () => storageUtil.get('app/theme');
expectTypeOf<ReturnType<typeof _getWithoutDefault>>().toEqualTypeOf<
Promise<string | undefined>
>();
});
it('有默认值时应推断为确定返回类型', () => {
const getWithDefault = () => storageUtil.get('app/theme', 'light');
expectTypeOf<ReturnType<typeof getWithDefault>>().toEqualTypeOf<Promise<string>>();
const _getWithDefault = () => storageUtil.get('app/theme', 'light');
expectTypeOf<ReturnType<typeof _getWithDefault>>().toEqualTypeOf<Promise<string>>();
});
});
@@ -0,0 +1,49 @@
import {
isRestrictedUrl,
isUnsupportedPageUrl,
RESTRICTED_PROTOCOLS,
} from '@/utils/restrictedUrls';
describe('restrictedUrls', () => {
describe('isRestrictedUrl', () => {
it('应识别受限协议页面', () => {
expect(isRestrictedUrl('chrome://settings')).toBe(true);
expect(isRestrictedUrl('chrome-extension://abc123/background.html')).toBe(true);
expect(isRestrictedUrl('about:blank')).toBe(true);
expect(isRestrictedUrl('edge://settings')).toBe(true);
expect(isRestrictedUrl('brave://settings')).toBe(true);
expect(isRestrictedUrl('view-source:https://example.com')).toBe(true);
expect(isRestrictedUrl('file:///path/to/file')).toBe(true);
expect(isRestrictedUrl('data:text/html,<h1>Hello</h1>')).toBe(true);
});
it('应允许普通 http/https 页面', () => {
expect(isRestrictedUrl('http://example.com')).toBe(false);
expect(isRestrictedUrl('https://example.com')).toBe(false);
});
it('空 URL 应视为受限', () => {
expect(isRestrictedUrl(undefined)).toBe(true);
expect(isRestrictedUrl('')).toBe(true);
});
});
describe('isUnsupportedPageUrl', () => {
it('应通过 protocol 精确匹配识别受限页面', () => {
expect(isUnsupportedPageUrl('chrome://newtab/')).toBe(true);
expect(isUnsupportedPageUrl('brave://settings/')).toBe(true);
expect(isUnsupportedPageUrl('https://example.com')).toBe(false);
});
it('无效 URL 应视为不支持', () => {
expect(isUnsupportedPageUrl(undefined)).toBe(true);
expect(isUnsupportedPageUrl('not-a-url')).toBe(true);
});
});
it('RESTRICTED_PROTOCOLS 应包含 storage cleaner 与右键恢复所需协议', () => {
expect(RESTRICTED_PROTOCOLS).toEqual(
expect.arrayContaining(['brave:', 'view-source:', 'file:', 'data:']),
);
});
});
+33
View File
@@ -0,0 +1,33 @@
import { getSyncSnapshot } from '@/utils/syncSnapshot';
describe('getSyncSnapshot', () => {
beforeEach(() => {
localStorage.clear();
vi.spyOn(console, 'error').mockImplementation(() => {});
});
afterEach(() => {
vi.restoreAllMocks();
});
it('无快照时应返回默认值', () => {
expect(getSyncSnapshot('app/test-key', 'default')).toBe('default');
});
it('应读取并解析合法 JSON 快照', () => {
localStorage.setItem('snapshot/app/test-key', JSON.stringify('saved'));
expect(getSyncSnapshot('app/test-key', 'default')).toBe('saved');
});
it('validator 失败时应回退到默认值', () => {
localStorage.setItem('snapshot/app/test-key', JSON.stringify('invalid'));
const isNumber = (val: unknown): val is number => typeof val === 'number';
expect(getSyncSnapshot('app/test-key', 0, isNumber)).toBe(0);
});
it('非法 JSON 时应回退到默认值并记录错误', () => {
localStorage.setItem('snapshot/app/test-key', '{invalid');
expect(getSyncSnapshot('app/test-key', 'fallback')).toBe('fallback');
expect(console.error).toHaveBeenCalled();
});
});