57ea4d9858
- **docs**: 完善组件注释、README 目录结构及 AGENTS.md 文档。 - **refactor**: - 提取通用 `PageHeader`、`Button`、`DashboardCard` 及 `ErrorBoundary` 组件。 - 重构消息通信机制,采用 `@webext-core/messaging` 实现类型安全。 - 将全局通知系统重构为 `SnackbarProvider` (后合并至 `GlobalSnackbar`)。 - 迁移样式系统至 MUI 主题,移除冗余 CSS。 - 优化路由配置,支持独立标签页模式及页面懒加载。 - 移除未使用文件、URL 工具及表单映射相关功能。 - **feat**: - 新增配置导出功能(JSON)及状态提示。 - 新增侧边栏状态变化通知机制。 - 新增文本统计及 JWT 解析工具。 - 优化二维码生成与解析逻辑,换用更轻量的 `qrious` 和 `qr-scanner`。 - 增强高亮器功能,支持闪烁效果及 Shadow DOM 穿透。 - **style**: 优化仪表盘响应式网格布局及 UI 细节。 - **fix**: 修复 `useStorageState` 依赖缺失及路由初始化性能问题。 - **test**: 更新单元测试以覆盖新增的工具函数及功能特性。
59 lines
2.0 KiB
TypeScript
59 lines
2.0 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { getTextStats, formatByteSize } from '../textStatistics';
|
|
|
|
describe('textStatistics utils', () => {
|
|
describe('getTextStats', () => {
|
|
it('should return zeros for empty text', () => {
|
|
const stats = getTextStats('');
|
|
expect(stats).toEqual({ characters: 0, words: 0, lines: 0, bytes: 0 });
|
|
});
|
|
|
|
it('should count characters correctly', () => {
|
|
expect(getTextStats('abc').characters).toBe(3);
|
|
expect(getTextStats('a b c').characters).toBe(5);
|
|
expect(getTextStats('你好').characters).toBe(2);
|
|
});
|
|
|
|
it('should count English words correctly', () => {
|
|
expect(getTextStats('hello world').words).toBe(2);
|
|
expect(getTextStats(' hello world ').words).toBe(2);
|
|
expect(getTextStats('hello, world!').words).toBe(2);
|
|
});
|
|
|
|
it('should count Chinese words correctly', () => {
|
|
// "你好世界" 在 Intl.Segmenter 中通常被识别为 "你好" 和 "世界" 两个词
|
|
const stats = getTextStats('你好世界');
|
|
expect(stats.words).toBe(2);
|
|
});
|
|
|
|
it('should count mixed language words correctly', () => {
|
|
const stats = getTextStats('Hello 你好');
|
|
// "Hello" (1) + "你好" (1) = 2
|
|
expect(stats.words).toBe(2);
|
|
});
|
|
|
|
it('should count lines correctly', () => {
|
|
expect(getTextStats('line1\nline2').lines).toBe(2);
|
|
expect(getTextStats('line1\nline2\n').lines).toBe(3);
|
|
});
|
|
|
|
it('should count bytes correctly (UTF-8)', () => {
|
|
expect(getTextStats('abc').bytes).toBe(3);
|
|
expect(getTextStats('你好').bytes).toBe(6); // UTF-8 中每个常用汉字占 3 字节
|
|
});
|
|
|
|
it('should handle special cases', () => {
|
|
expect(getTextStats(' ').words).toBe(0);
|
|
expect(getTextStats('\n\n\n').lines).toBe(4);
|
|
expect(getTextStats('\n\n\n').words).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('formatByteSize', () => {
|
|
it('should format bytes correctly', () => {
|
|
expect(formatByteSize(100)).toBe('100 Bytes');
|
|
expect(formatByteSize(0)).toBe('0 Bytes');
|
|
});
|
|
});
|
|
});
|