mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-04-25 09:43:49 +02:00
refactor: split common until and add ut
This commit is contained in:
72
src/shared/utils/__tests__/fileUtils.test.js
Normal file
72
src/shared/utils/__tests__/fileUtils.test.js
Normal file
@@ -0,0 +1,72 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import {
|
||||
extractFileId,
|
||||
extractFileVersion,
|
||||
extractVariantVersion
|
||||
} from '../fileUtils';
|
||||
|
||||
describe('extractFileId', () => {
|
||||
it('extracts file ID from a URL', () => {
|
||||
expect(
|
||||
extractFileId(
|
||||
'https://api.vrchat.cloud/file/file_abc123-def/1/file'
|
||||
)
|
||||
).toBe('file_abc123-def');
|
||||
});
|
||||
|
||||
it('extracts file ID from a plain string', () => {
|
||||
expect(extractFileId('file_0123456789abcdef')).toBe(
|
||||
'file_0123456789abcdef'
|
||||
);
|
||||
});
|
||||
|
||||
it('returns empty string when no match', () => {
|
||||
expect(extractFileId('no-match-here')).toBe('');
|
||||
expect(extractFileId('')).toBe('');
|
||||
});
|
||||
|
||||
it('handles null/undefined input', () => {
|
||||
expect(extractFileId(null)).toBe('');
|
||||
expect(extractFileId(undefined)).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractFileVersion', () => {
|
||||
it('extracts version number from file URL', () => {
|
||||
expect(extractFileVersion('/file_abc123/5/file')).toBe('5');
|
||||
});
|
||||
|
||||
it('extracts multi-digit version', () => {
|
||||
expect(extractFileVersion('/file_abc-def-123/123/file')).toBe('123');
|
||||
});
|
||||
|
||||
it('returns empty string when no match', () => {
|
||||
expect(extractFileVersion('no-version')).toBe('');
|
||||
expect(extractFileVersion('')).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractVariantVersion', () => {
|
||||
it('extracts version from query parameter', () => {
|
||||
expect(extractVariantVersion('https://example.com/file?v=42')).toBe(
|
||||
'42'
|
||||
);
|
||||
});
|
||||
|
||||
it('returns 0 when no v parameter', () => {
|
||||
expect(extractVariantVersion('https://example.com/file?other=1')).toBe(
|
||||
'0'
|
||||
);
|
||||
});
|
||||
|
||||
it('returns 0 for empty/null input', () => {
|
||||
expect(extractVariantVersion('')).toBe('0');
|
||||
expect(extractVariantVersion(null)).toBe('0');
|
||||
expect(extractVariantVersion(undefined)).toBe('0');
|
||||
});
|
||||
|
||||
it('returns 0 for invalid URL', () => {
|
||||
expect(extractVariantVersion('not-a-url')).toBe('0');
|
||||
});
|
||||
});
|
||||
88
src/shared/utils/__tests__/platformUtils.test.js
Normal file
88
src/shared/utils/__tests__/platformUtils.test.js
Normal file
@@ -0,0 +1,88 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { getAvailablePlatforms } from '../platformUtils';
|
||||
|
||||
describe('getAvailablePlatforms', () => {
|
||||
it('detects PC platform', () => {
|
||||
const packages = [{ platform: 'standalonewindows' }];
|
||||
expect(getAvailablePlatforms(packages)).toEqual({
|
||||
isPC: true,
|
||||
isQuest: false,
|
||||
isIos: false
|
||||
});
|
||||
});
|
||||
|
||||
it('detects Quest (android) platform', () => {
|
||||
const packages = [{ platform: 'android' }];
|
||||
expect(getAvailablePlatforms(packages)).toEqual({
|
||||
isPC: false,
|
||||
isQuest: true,
|
||||
isIos: false
|
||||
});
|
||||
});
|
||||
|
||||
it('detects iOS platform', () => {
|
||||
const packages = [{ platform: 'ios' }];
|
||||
expect(getAvailablePlatforms(packages)).toEqual({
|
||||
isPC: false,
|
||||
isQuest: false,
|
||||
isIos: true
|
||||
});
|
||||
});
|
||||
|
||||
it('detects multiple platforms', () => {
|
||||
const packages = [
|
||||
{ platform: 'standalonewindows' },
|
||||
{ platform: 'android' },
|
||||
{ platform: 'ios' }
|
||||
];
|
||||
expect(getAvailablePlatforms(packages)).toEqual({
|
||||
isPC: true,
|
||||
isQuest: true,
|
||||
isIos: true
|
||||
});
|
||||
});
|
||||
|
||||
it('skips non-standard/non-security variants', () => {
|
||||
const packages = [
|
||||
{ platform: 'standalonewindows', variant: 'custom_variant' },
|
||||
{ platform: 'android', variant: 'standard' }
|
||||
];
|
||||
expect(getAvailablePlatforms(packages)).toEqual({
|
||||
isPC: false,
|
||||
isQuest: true,
|
||||
isIos: false
|
||||
});
|
||||
});
|
||||
|
||||
it('allows security variant', () => {
|
||||
const packages = [
|
||||
{ platform: 'standalonewindows', variant: 'security' }
|
||||
];
|
||||
expect(getAvailablePlatforms(packages)).toEqual({
|
||||
isPC: true,
|
||||
isQuest: false,
|
||||
isIos: false
|
||||
});
|
||||
});
|
||||
|
||||
it('returns all false for empty array', () => {
|
||||
expect(getAvailablePlatforms([])).toEqual({
|
||||
isPC: false,
|
||||
isQuest: false,
|
||||
isIos: false
|
||||
});
|
||||
});
|
||||
|
||||
it('returns all false for non-object input', () => {
|
||||
expect(getAvailablePlatforms('string')).toEqual({
|
||||
isPC: false,
|
||||
isQuest: false,
|
||||
isIos: false
|
||||
});
|
||||
});
|
||||
|
||||
it('throws for null input (typeof null === "object" but not iterable)', () => {
|
||||
expect(() => getAvailablePlatforms(null)).toThrow();
|
||||
});
|
||||
});
|
||||
47
src/shared/utils/__tests__/urlUtils.test.js
Normal file
47
src/shared/utils/__tests__/urlUtils.test.js
Normal file
@@ -0,0 +1,47 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { getFaviconUrl, replaceVrcPackageUrl } from '../urlUtils';
|
||||
|
||||
describe('getFaviconUrl', () => {
|
||||
it('returns favicon URL for valid URL', () => {
|
||||
expect(getFaviconUrl('https://vrchat.com/home')).toBe(
|
||||
'https://icons.duckduckgo.com/ip2/vrchat.com.ico'
|
||||
);
|
||||
});
|
||||
|
||||
it('extracts host from complex URL', () => {
|
||||
expect(getFaviconUrl('https://store.steampowered.com/app/12345')).toBe(
|
||||
'https://icons.duckduckgo.com/ip2/store.steampowered.com.ico'
|
||||
);
|
||||
});
|
||||
|
||||
it('returns empty string for empty input', () => {
|
||||
expect(getFaviconUrl('')).toBe('');
|
||||
expect(getFaviconUrl(null)).toBe('');
|
||||
expect(getFaviconUrl(undefined)).toBe('');
|
||||
});
|
||||
|
||||
it('returns empty string for invalid URL', () => {
|
||||
expect(getFaviconUrl('not-a-url')).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('replaceVrcPackageUrl', () => {
|
||||
it('replaces api.vrchat.cloud with vrchat.com', () => {
|
||||
expect(
|
||||
replaceVrcPackageUrl('https://api.vrchat.cloud/api/1/file/123')
|
||||
).toBe('https://vrchat.com/api/1/file/123');
|
||||
});
|
||||
|
||||
it('returns URL unchanged if no match', () => {
|
||||
expect(replaceVrcPackageUrl('https://example.com/test')).toBe(
|
||||
'https://example.com/test'
|
||||
);
|
||||
});
|
||||
|
||||
it('returns empty string for empty/null input', () => {
|
||||
expect(replaceVrcPackageUrl('')).toBe('');
|
||||
expect(replaceVrcPackageUrl(null)).toBe('');
|
||||
expect(replaceVrcPackageUrl(undefined)).toBe('');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user