mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-05 16:22:26 -08:00
test: add tests for regex, mimeTypes, pronouns, useDebounce, useThrottle
This commit is contained in:
1 parent
bfe766ead9
commit
b2696f4f31
5 files changed
+454
No files matched your search
@@ -0,0 +1,100 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { renderHook, act } from '@testing-library/react';
|
||||
import { useDebounce } from './useDebounce';
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
describe('useDebounce', () => {
|
||||
it('does not call callback before wait time elapses', () => {
|
||||
const fn = vi.fn();
|
||||
const { result } = renderHook(() => useDebounce(fn, { wait: 200 }));
|
||||
|
||||
act(() => {
|
||||
result.current('a');
|
||||
});
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(199);
|
||||
});
|
||||
|
||||
expect(fn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('calls callback after wait time elapses', () => {
|
||||
const fn = vi.fn();
|
||||
const { result } = renderHook(() => useDebounce(fn, { wait: 200 }));
|
||||
|
||||
act(() => {
|
||||
result.current('a');
|
||||
});
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(200);
|
||||
});
|
||||
|
||||
expect(fn).toHaveBeenCalledOnce();
|
||||
expect(fn).toHaveBeenCalledWith('a');
|
||||
});
|
||||
|
||||
it('resets the timer on each successive call', () => {
|
||||
const fn = vi.fn();
|
||||
const { result } = renderHook(() => useDebounce(fn, { wait: 200 }));
|
||||
|
||||
act(() => {
|
||||
result.current('first');
|
||||
});
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(150);
|
||||
result.current('second');
|
||||
});
|
||||
|
||||
// 150ms into the reset timer — should not have fired yet
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(150);
|
||||
});
|
||||
|
||||
expect(fn).not.toHaveBeenCalled();
|
||||
|
||||
// Complete the 200ms wait after the second call
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(50);
|
||||
});
|
||||
|
||||
expect(fn).toHaveBeenCalledOnce();
|
||||
expect(fn).toHaveBeenCalledWith('second');
|
||||
});
|
||||
|
||||
it('only fires once after rapid successive calls', () => {
|
||||
const fn = vi.fn();
|
||||
const { result } = renderHook(() => useDebounce(fn, { wait: 100 }));
|
||||
|
||||
act(() => {
|
||||
result.current(1);
|
||||
result.current(2);
|
||||
result.current(3);
|
||||
vi.advanceTimersByTime(100);
|
||||
});
|
||||
|
||||
expect(fn).toHaveBeenCalledOnce();
|
||||
expect(fn).toHaveBeenCalledWith(3);
|
||||
});
|
||||
|
||||
it('fires immediately on first call when immediate option is set', () => {
|
||||
const fn = vi.fn();
|
||||
const { result } = renderHook(() => useDebounce(fn, { wait: 200, immediate: true }));
|
||||
|
||||
act(() => {
|
||||
result.current('go');
|
||||
});
|
||||
|
||||
expect(fn).toHaveBeenCalledOnce();
|
||||
expect(fn).toHaveBeenCalledWith('go');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { renderHook, act } from '@testing-library/react';
|
||||
import { useThrottle } from './useThrottle';
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
describe('useThrottle', () => {
|
||||
it('fires once after the wait period even when called multiple times', () => {
|
||||
const fn = vi.fn();
|
||||
const { result } = renderHook(() => useThrottle(fn, { wait: 200 }));
|
||||
|
||||
act(() => {
|
||||
result.current('a');
|
||||
result.current('b');
|
||||
result.current('c');
|
||||
});
|
||||
|
||||
expect(fn).not.toHaveBeenCalled();
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(200);
|
||||
});
|
||||
|
||||
expect(fn).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('fires with the latest args when called multiple times within the wait', () => {
|
||||
const fn = vi.fn();
|
||||
const { result } = renderHook(() => useThrottle(fn, { wait: 200 }));
|
||||
|
||||
act(() => {
|
||||
result.current('first');
|
||||
result.current('second');
|
||||
result.current('third');
|
||||
});
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(200);
|
||||
});
|
||||
|
||||
expect(fn).toHaveBeenCalledWith('third');
|
||||
});
|
||||
|
||||
it('does not fire before the wait period ends', () => {
|
||||
const fn = vi.fn();
|
||||
const { result } = renderHook(() => useThrottle(fn, { wait: 300 }));
|
||||
|
||||
act(() => {
|
||||
result.current('x');
|
||||
vi.advanceTimersByTime(299);
|
||||
});
|
||||
|
||||
expect(fn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('allows a new invocation after the wait period resets', () => {
|
||||
const fn = vi.fn();
|
||||
const { result } = renderHook(() => useThrottle(fn, { wait: 100 }));
|
||||
|
||||
act(() => {
|
||||
result.current('first-burst');
|
||||
vi.advanceTimersByTime(100);
|
||||
});
|
||||
|
||||
act(() => {
|
||||
result.current('second-burst');
|
||||
vi.advanceTimersByTime(100);
|
||||
});
|
||||
|
||||
expect(fn).toHaveBeenCalledTimes(2);
|
||||
expect(fn).toHaveBeenNthCalledWith(1, 'first-burst');
|
||||
expect(fn).toHaveBeenNthCalledWith(2, 'second-burst');
|
||||
});
|
||||
|
||||
it('fires immediately on first call when immediate option is set', () => {
|
||||
const fn = vi.fn();
|
||||
const { result } = renderHook(() => useThrottle(fn, { wait: 200, immediate: true }));
|
||||
|
||||
act(() => {
|
||||
result.current('now');
|
||||
});
|
||||
|
||||
expect(fn).toHaveBeenCalledOnce();
|
||||
expect(fn).toHaveBeenCalledWith('now');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,78 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
getBlobSafeMimeType,
|
||||
mimeTypeToExt,
|
||||
getFileNameExt,
|
||||
getFileNameWithoutExt,
|
||||
FALLBACK_MIMETYPE,
|
||||
} from './mimeTypes';
|
||||
|
||||
describe('getBlobSafeMimeType', () => {
|
||||
it('passes through known image types', () => {
|
||||
expect(getBlobSafeMimeType('image/jpeg')).toBe('image/jpeg');
|
||||
expect(getBlobSafeMimeType('image/png')).toBe('image/png');
|
||||
expect(getBlobSafeMimeType('image/webp')).toBe('image/webp');
|
||||
});
|
||||
|
||||
it('passes through known video and audio types', () => {
|
||||
expect(getBlobSafeMimeType('video/mp4')).toBe('video/mp4');
|
||||
expect(getBlobSafeMimeType('audio/mpeg')).toBe('audio/mpeg');
|
||||
});
|
||||
|
||||
it('converts video/quicktime to video/mp4', () => {
|
||||
expect(getBlobSafeMimeType('video/quicktime')).toBe('video/mp4');
|
||||
});
|
||||
|
||||
it('returns fallback for unknown mime types', () => {
|
||||
expect(getBlobSafeMimeType('application/x-unknown')).toBe(FALLBACK_MIMETYPE);
|
||||
expect(getBlobSafeMimeType('image/bmp')).toBe(FALLBACK_MIMETYPE);
|
||||
});
|
||||
|
||||
it('strips charset parameter before checking allowlist', () => {
|
||||
expect(getBlobSafeMimeType('text/plain; charset=utf-8')).toBe('text/plain');
|
||||
});
|
||||
|
||||
it('returns fallback for non-string input', () => {
|
||||
// @ts-expect-error — testing runtime safety for external data
|
||||
expect(getBlobSafeMimeType(null)).toBe(FALLBACK_MIMETYPE);
|
||||
// @ts-expect-error
|
||||
expect(getBlobSafeMimeType(42)).toBe(FALLBACK_MIMETYPE);
|
||||
});
|
||||
});
|
||||
|
||||
describe('mimeTypeToExt', () => {
|
||||
it.each([
|
||||
['image/jpeg', 'jpeg'],
|
||||
['image/png', 'png'],
|
||||
['video/mp4', 'mp4'],
|
||||
['audio/ogg', 'ogg'],
|
||||
['application/pdf', 'pdf'],
|
||||
['text/plain', 'plain'],
|
||||
])('%s → %s', (mimeType, expected) => {
|
||||
expect(mimeTypeToExt(mimeType)).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFileNameExt', () => {
|
||||
it.each([
|
||||
['photo.jpg', 'jpg'],
|
||||
['archive.tar.gz', 'gz'],
|
||||
['readme.MD', 'MD'],
|
||||
// No dot: lastIndexOf returns -1, slice(0) returns the full string
|
||||
['noextension', 'noextension'],
|
||||
])('%s → "%s"', (filename, expected) => {
|
||||
expect(getFileNameExt(filename)).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFileNameWithoutExt', () => {
|
||||
it.each([
|
||||
['photo.jpg', 'photo'],
|
||||
['archive.tar.gz', 'archive.tar'],
|
||||
['noextension', 'noextension'],
|
||||
['.gitignore', '.gitignore'],
|
||||
['.hidden.txt', '.hidden'],
|
||||
])('%s → "%s"', (filename, expected) => {
|
||||
expect(getFileNameWithoutExt(filename)).toBe(expected);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,89 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { parsePronounsInput, filterPronounsByLanguage } from './pronouns';
|
||||
|
||||
describe('parsePronounsInput', () => {
|
||||
it('parses a single pronoun without a language prefix', () => {
|
||||
expect(parsePronounsInput('he/him')).toEqual([{ summary: 'he/him', language: 'en' }]);
|
||||
});
|
||||
|
||||
it('parses multiple comma-separated pronouns', () => {
|
||||
expect(parsePronounsInput('he/him,she/her')).toEqual([
|
||||
{ summary: 'he/him', language: 'en' },
|
||||
{ summary: 'she/her', language: 'en' },
|
||||
]);
|
||||
});
|
||||
|
||||
it('parses a pronoun with a language prefix', () => {
|
||||
expect(parsePronounsInput('de:er/ihm')).toEqual([{ language: 'de', summary: 'er/ihm' }]);
|
||||
});
|
||||
|
||||
it('trims whitespace around entries', () => {
|
||||
expect(parsePronounsInput(' he/him , she/her ')).toEqual([
|
||||
{ summary: 'he/him', language: 'en' },
|
||||
{ summary: 'she/her', language: 'en' },
|
||||
]);
|
||||
});
|
||||
|
||||
it('truncates summary to 16 characters', () => {
|
||||
const longSummary = 'this/is/way/too/long';
|
||||
const result = parsePronounsInput(longSummary);
|
||||
expect(result[0]?.summary).toHaveLength(16);
|
||||
expect(result[0]?.summary).toBe('this/is/way/too/');
|
||||
});
|
||||
|
||||
it('falls back to "en" when language prefix is empty', () => {
|
||||
expect(parsePronounsInput(':he/him')).toEqual([{ language: 'en', summary: 'he/him' }]);
|
||||
});
|
||||
|
||||
it('returns empty array for empty string', () => {
|
||||
expect(parsePronounsInput('')).toEqual([]);
|
||||
});
|
||||
|
||||
it.each([null, undefined, 42 as unknown as string])(
|
||||
'returns empty array for non-string input: %s',
|
||||
(input) => {
|
||||
expect(parsePronounsInput(input as string)).toEqual([]);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
describe('filterPronounsByLanguage', () => {
|
||||
const pronouns = [
|
||||
{ summary: 'he/him', language: 'en' },
|
||||
{ summary: 'er/ihm', language: 'de' },
|
||||
{ summary: 'il/lui', language: 'fr' },
|
||||
];
|
||||
|
||||
it('returns all pronouns when filtering is disabled', () => {
|
||||
const result = filterPronounsByLanguage(pronouns, false, ['en']);
|
||||
expect(result).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('filters to matching language when enabled', () => {
|
||||
const result = filterPronounsByLanguage(pronouns, true, ['de']);
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]?.language).toBe('de');
|
||||
});
|
||||
|
||||
it('returns all pronouns when no entries match (fallthrough)', () => {
|
||||
const result = filterPronounsByLanguage(pronouns, true, ['ja']);
|
||||
expect(result).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('matches multiple languages', () => {
|
||||
const result = filterPronounsByLanguage(pronouns, true, ['en', 'fr']);
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result.map((p) => p.language)).toEqual(['en', 'fr']);
|
||||
});
|
||||
|
||||
it('is case-insensitive for language matching', () => {
|
||||
const result = filterPronounsByLanguage(pronouns, true, ['EN']);
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]?.language).toBe('en');
|
||||
});
|
||||
|
||||
it('returns empty array for non-array input', () => {
|
||||
// @ts-expect-error — testing runtime safety
|
||||
expect(filterPronounsByLanguage(null, true, ['en'])).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { sanitizeForRegex, EMAIL_REGEX, URL_REG } from './regex';
|
||||
|
||||
describe('sanitizeForRegex', () => {
|
||||
it('returns normal alphanumeric strings unchanged', () => {
|
||||
expect(sanitizeForRegex('hello123')).toBe('hello123');
|
||||
});
|
||||
|
||||
it.each([
|
||||
['|', '\\|'],
|
||||
['\\', '\\\\'],
|
||||
['{', '\\{'],
|
||||
['}', '\\}'],
|
||||
['(', '\\('],
|
||||
[')', '\\)'],
|
||||
['[', '\\['],
|
||||
[']', '\\]'],
|
||||
['^', '\\^'],
|
||||
['$', '\\$'],
|
||||
['+', '\\+'],
|
||||
['*', '\\*'],
|
||||
['?', '\\?'],
|
||||
['.', '\\.'],
|
||||
['-', '\\x2d'],
|
||||
])('escapes special char %s', (input, expected) => {
|
||||
expect(sanitizeForRegex(input)).toBe(expected);
|
||||
});
|
||||
|
||||
it('escapes all special chars in a complex string', () => {
|
||||
const result = sanitizeForRegex('a.b+c?d');
|
||||
expect(result).toBe('a\\.b\\+c\\?d');
|
||||
});
|
||||
|
||||
it('produces a valid regex that matches the original string literally', () => {
|
||||
const input = 'foo.bar (baz)+';
|
||||
const safePattern = sanitizeForRegex(input);
|
||||
const reg = new RegExp(safePattern);
|
||||
expect(reg.test(input)).toBe(true);
|
||||
// Without escaping, `.` would match any char — make sure it's literal
|
||||
expect(reg.test('fooXbar (baz)+')).toBe(false);
|
||||
});
|
||||
|
||||
it('handles empty string', () => {
|
||||
expect(sanitizeForRegex('')).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('EMAIL_REGEX', () => {
|
||||
it.each([
|
||||
'[email protected]',
|
||||
'[email protected]',
|
||||
'[email protected]',
|
||||
'[email protected]',
|
||||
])('matches valid email: %s', (email) => {
|
||||
expect(EMAIL_REGEX.test(email)).toBe(true);
|
||||
});
|
||||
|
||||
it.each(['notanemail', '@nodomain.com', 'missing-at-sign.com', 'two@@at.com'])(
|
||||
'rejects invalid email: %s',
|
||||
(email) => {
|
||||
expect(EMAIL_REGEX.test(email)).toBe(false);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
describe('URL_REG', () => {
|
||||
it('matches a simple http URL', () => {
|
||||
const matches = 'visit http://example.com today'.match(URL_REG);
|
||||
expect(matches).not.toBeNull();
|
||||
expect(matches?.[0]).toBe('http://example.com');
|
||||
});
|
||||
|
||||
it('matches a simple https URL', () => {
|
||||
const matches = 'go to https://example.com/path?q=1'.match(URL_REG);
|
||||
expect(matches?.[0]).toBe('https://example.com/path?q=1');
|
||||
});
|
||||
|
||||
it('finds multiple URLs in a string', () => {
|
||||
const matches = 'https://one.com and https://two.org'.match(URL_REG);
|
||||
expect(matches).toHaveLength(2);
|
||||
expect(matches?.[0]).toBe('https://one.com');
|
||||
expect(matches?.[1]).toBe('https://two.org');
|
||||
});
|
||||
|
||||
it('does not match plain text without a scheme', () => {
|
||||
const matches = 'just some text without a link'.match(URL_REG);
|
||||
expect(matches).toBeNull();
|
||||
});
|
||||
|
||||
it('strips trailing punctuation from matched URL', () => {
|
||||
// The pattern uses a negative lookbehind to exclude trailing punctuation
|
||||
const matches = 'see https://example.com.'.match(URL_REG);
|
||||
expect(matches?.[0]).not.toMatch(/\.$/);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user