85 lines
2.7 KiB
TypeScript
85 lines
2.7 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { hashString, md5, computeFingerprint } from '../src/utils/hash';
|
|
|
|
describe('hash utils', () => {
|
|
describe('hashString', () => {
|
|
it('should return a positive number', () => {
|
|
const result = hashString('test');
|
|
expect(result).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('should return consistent results for same input', () => {
|
|
const result1 = hashString('test');
|
|
const result2 = hashString('test');
|
|
expect(result1).toBe(result2);
|
|
});
|
|
|
|
it('should return different results for different inputs', () => {
|
|
const result1 = hashString('test1');
|
|
const result2 = hashString('test2');
|
|
expect(result1).not.toBe(result2);
|
|
});
|
|
});
|
|
|
|
describe('md5', () => {
|
|
it('should return a hex string', () => {
|
|
const result = md5('test');
|
|
expect(result).toMatch(/^[a-f0-9]+$/i);
|
|
});
|
|
|
|
it('should return consistent results for same input', () => {
|
|
const result1 = md5('test');
|
|
const result2 = md5('test');
|
|
expect(result1).toBe(result2);
|
|
});
|
|
|
|
it('should return different results for different inputs', () => {
|
|
const result1 = md5('test1');
|
|
const result2 = md5('test2');
|
|
expect(result1).not.toBe(result2);
|
|
});
|
|
|
|
it('should handle empty string', () => {
|
|
const result = md5('');
|
|
expect(result).toBeDefined();
|
|
expect(result.length).toBeGreaterThan(0);
|
|
});
|
|
});
|
|
|
|
describe('computeFingerprint', () => {
|
|
it('should generate fingerprint for error', () => {
|
|
const frames = [
|
|
{ filename: 'app.js', lineno: 10 },
|
|
{ filename: 'main.js', lineno: 5 },
|
|
];
|
|
const result = computeFingerprint('TypeError', 'test error', frames);
|
|
expect(result).toBeDefined();
|
|
expect(result.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('should normalize message by removing numbers', () => {
|
|
const frames: { filename: string; lineno?: number }[] = [];
|
|
const result1 = computeFingerprint('Error', 'User 123 not found', frames);
|
|
const result2 = computeFingerprint('Error', 'User 456 not found', frames);
|
|
expect(result1).toBe(result2);
|
|
});
|
|
|
|
it('should only use first 3 frames', () => {
|
|
const frames = [
|
|
{ filename: 'a.js', lineno: 1 },
|
|
{ filename: 'b.js', lineno: 2 },
|
|
{ filename: 'c.js', lineno: 3 },
|
|
{ filename: 'd.js', lineno: 4 },
|
|
{ filename: 'e.js', lineno: 5 },
|
|
];
|
|
const result = computeFingerprint('Error', 'message', frames);
|
|
expect(result).toBeDefined();
|
|
});
|
|
|
|
it('should handle empty frames', () => {
|
|
const result = computeFingerprint('Error', 'message', []);
|
|
expect(result).toBeDefined();
|
|
});
|
|
});
|
|
});
|