add tests

This commit is contained in:
lukasIO 2025-06-18 16:42:04 +02:00
parent 623a9f0eac
commit 633aad8117
5 changed files with 937 additions and 26 deletions

View File

@ -1,4 +1,5 @@
import { randomString } from '@/lib/client-utils';
import { getLiveKitURL } from '@/lib/getLiveKitURL';
import { ConnectionDetails } from '@/lib/types';
import { AccessToken, AccessTokenOptions, VideoGrant } from 'livekit-server-sdk';
import { NextRequest, NextResponse } from 'next/server';
@ -79,19 +80,6 @@ function createParticipantToken(userInfo: AccessTokenOptions, roomName: string)
return at.toJwt();
}
/**
* Get the LiveKit server URL for the given region.
*/
function getLiveKitURL(projectUrl: string, region: string | null): string {
const url = new URL(projectUrl);
if (region && url.hostname.includes('livekit.cloud')) {
const hostParts = url.hostname.split('.');
const regionURL = [hostParts[0], region, ...hostParts].join('.');
url.hostname = regionURL;
}
return url.toString();
}
function getCookieExpirationTime(): string {
var now = new Date();
var time = now.getTime();

29
lib/getLiveKitURL.test.ts Normal file
View File

@ -0,0 +1,29 @@
import { describe, it, expect } from 'vitest';
import { getLiveKitURL } from './getLiveKitURL';
describe('getLiveKitURL', () => {
it('returns the original URL if no region is provided', () => {
const url = 'https://myproject.livekit.cloud';
expect(getLiveKitURL(url, null)).toBe(url + '/');
});
it('inserts the region into livekit.cloud URLs', () => {
const url = 'https://myproject.livekit.cloud';
const region = 'eu';
expect(getLiveKitURL(url, region)).toBe('https://myproject.eu.myproject.livekit.cloud/');
});
it('returns the original URL for non-livekit.cloud hosts, even with region', () => {
const url = 'https://example.com';
const region = 'us';
expect(getLiveKitURL(url, region)).toBe(url + '/');
});
it('handles URLs with paths and query params', () => {
const url = 'https://myproject.livekit.cloud/room?foo=bar';
const region = 'ap';
expect(getLiveKitURL(url, region)).toBe(
'https://myproject.ap.myproject.livekit.cloud/room?foo=bar',
);
});
});

9
lib/getLiveKitURL.ts Normal file
View File

@ -0,0 +1,9 @@
export function getLiveKitURL(projectUrl: string, region: string | null): string {
const url = new URL(projectUrl);
if (region && url.hostname.includes('livekit.cloud')) {
const hostParts = url.hostname.split('.');
const regionURL = [hostParts[0], region, ...hostParts].join('.');
url.hostname = regionURL;
}
return url.toString();
}

View File

@ -8,6 +8,7 @@
"start": "next start",
"lint": "next lint",
"lint:fix": "next lint --fix",
"test": "vitest run",
"format:check": "prettier --check \"**/*.{ts,tsx,md,json}\"",
"format:write": "prettier --write \"**/*.{ts,tsx,md,json}\""
},
@ -31,9 +32,10 @@
"@types/react-dom": "18.3.7",
"eslint": "9.29.0",
"eslint-config-next": "15.3.3",
"prettier": "3.5.3",
"source-map-loader": "^5.0.0",
"typescript": "5.8.3",
"prettier": "3.5.3"
"vitest": "^3.2.4"
},
"engines": {
"node": ">=18"

907
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff