7167a43763
核心功能新增 工具套件扩展 JSON 工具(差异比较、格式化、转 YAML/TOML、压缩) Base64 转换器(文本/文件/图像编解码) Markdown ↔ HTML 双向转换 JWT 解析器 文本统计工具 二维码生成/解析(替换库以减小体积) 表单工具(已移除) 表单映射、智能填充、高亮定位功能已移除 架构与工程改进 技术栈升级 引入 @webext-core/messaging 重构消息通信 添加 @dnd-kit 支持功能列表拖拽排序 引入 i18next 实现中英文国际化 使用 MUI 主题系统替代硬编码样式,支持暗色模式 代码质量 统一组件导出为默认导出 提取公共组件(TextInputArea、SwitchButtonGroup、PageHeader、DecodeResultPaper) 使用 useStorageState 钩子统一管理存储状态 添加 PageErrorBoundary 页面级错误边界 实现组件懒加载优化首屏性能 关键重构 路由系统重构 — 支持独立标签页模式,合并路由与功能配置 状态管理优化 — 存储状态增加快照机制、验证器和防抖处理 剪贴板与打印工具 — 统一为 async/await 形式,修复重复触发问题 存储清理器 — 使用 TextEncoder 准确计算 UTF-8 字节数,支持 TB 单位 测试与 CI 测试覆盖 — 为新增工具、公共组件、边界场景补充单元测试 CI 精简 — 移除 Firefox 测试,仅保留 Chrome;修正类型检查命令
134 lines
4.4 KiB
TypeScript
134 lines
4.4 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { clearCookies, formatSize, isRestrictedUrl } from '@/utils/storageCleaner';
|
|
|
|
describe('storageCleaner utils', () => {
|
|
describe('isRestrictedUrl', () => {
|
|
it('should return true for chrome:// URLs', () => {
|
|
expect(isRestrictedUrl('chrome://settings')).toBe(true);
|
|
});
|
|
|
|
it('should return true for chrome-extension:// URLs', () => {
|
|
expect(isRestrictedUrl('chrome-extension://abc123/background.html')).toBe(true);
|
|
});
|
|
|
|
it('should return true for about:// URLs', () => {
|
|
expect(isRestrictedUrl('about:blank')).toBe(true);
|
|
});
|
|
|
|
it('should return true for edge:// URLs', () => {
|
|
expect(isRestrictedUrl('edge://settings')).toBe(true);
|
|
});
|
|
|
|
it('should return true for view-source:// URLs', () => {
|
|
expect(isRestrictedUrl('view-source:https://example.com')).toBe(true);
|
|
});
|
|
|
|
it('should return true for file:// URLs', () => {
|
|
expect(isRestrictedUrl('file:///path/to/file')).toBe(true);
|
|
});
|
|
|
|
it('should return true for data:// URLs', () => {
|
|
expect(isRestrictedUrl('data:text/html,<h1>Hello</h1>')).toBe(true);
|
|
});
|
|
|
|
it('should return false for http:// URLs', () => {
|
|
expect(isRestrictedUrl('http://example.com')).toBe(false);
|
|
});
|
|
|
|
it('should return false for https:// URLs', () => {
|
|
expect(isRestrictedUrl('https://example.com')).toBe(false);
|
|
});
|
|
|
|
it('should return true for undefined URL', () => {
|
|
expect(isRestrictedUrl(undefined)).toBe(true);
|
|
});
|
|
|
|
it('should return true for empty string', () => {
|
|
expect(isRestrictedUrl('')).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('formatSize', () => {
|
|
it('should return "0 B" for 0 bytes', () => {
|
|
expect(formatSize(0)).toBe('0 B');
|
|
});
|
|
|
|
it('should format bytes correctly', () => {
|
|
expect(formatSize(500)).toBe('500 B');
|
|
});
|
|
|
|
it('should format kilobytes correctly', () => {
|
|
expect(formatSize(1024)).toBe('1.0 KB');
|
|
expect(formatSize(1536)).toBe('1.5 KB');
|
|
expect(formatSize(2048)).toBe('2.0 KB');
|
|
});
|
|
|
|
it('should format megabytes correctly', () => {
|
|
expect(formatSize(1048576)).toBe('1.00 MB');
|
|
expect(formatSize(1572864)).toBe('1.50 MB');
|
|
expect(formatSize(5242880)).toBe('5.00 MB');
|
|
});
|
|
|
|
it('should format gigabytes correctly', () => {
|
|
expect(formatSize(1073741824)).toBe('1.00 GB');
|
|
expect(formatSize(2147483648)).toBe('2.00 GB');
|
|
});
|
|
|
|
it('should handle edge cases', () => {
|
|
expect(formatSize(1)).toBe('1 B');
|
|
expect(formatSize(1023)).toBe('1023 B');
|
|
expect(formatSize(1025)).toBe('1.0 KB');
|
|
});
|
|
});
|
|
|
|
describe('clearCookies', () => {
|
|
it('should strip leading dot from cookie domain when removing cookies', async () => {
|
|
const mockCookies = [
|
|
{ name: 'session', domain: '.example.com', path: '/', secure: true, storeId: '0' },
|
|
{ name: 'auth', domain: '.example.com', path: '/api', secure: false, storeId: '0' },
|
|
];
|
|
(chrome.cookies.getAll as any).mockResolvedValue(mockCookies);
|
|
(chrome.cookies.remove as any).mockResolvedValue(undefined);
|
|
|
|
const result = await clearCookies('https://example.com');
|
|
|
|
expect(result).toEqual({ success: true, count: 2 });
|
|
expect(chrome.cookies.remove).toHaveBeenCalledWith({
|
|
url: 'https://example.com/',
|
|
name: 'session',
|
|
storeId: '0',
|
|
});
|
|
expect(chrome.cookies.remove).toHaveBeenCalledWith({
|
|
url: 'http://example.com/api',
|
|
name: 'auth',
|
|
storeId: '0',
|
|
});
|
|
});
|
|
|
|
it('should handle domains without leading dot', async () => {
|
|
const mockCookies = [
|
|
{ name: 'pref', domain: 'sub.example.com', path: '/path', secure: true, storeId: '0' },
|
|
];
|
|
(chrome.cookies.getAll as any).mockResolvedValue(mockCookies);
|
|
(chrome.cookies.remove as any).mockResolvedValue(undefined);
|
|
|
|
const result = await clearCookies('https://sub.example.com');
|
|
|
|
expect(result).toEqual({ success: true, count: 1 });
|
|
expect(chrome.cookies.remove).toHaveBeenCalledWith({
|
|
url: 'https://sub.example.com/path',
|
|
name: 'pref',
|
|
storeId: '0',
|
|
});
|
|
});
|
|
|
|
it('should return error when operation fails', async () => {
|
|
(chrome.cookies.getAll as any).mockRejectedValue(new Error('Permission denied'));
|
|
|
|
const result = await clearCookies('https://example.com');
|
|
|
|
expect(result).toEqual({ success: false, error: 'Error: Permission denied' });
|
|
});
|
|
});
|
|
});
|