import safePath, { timedExecution } from '$lib'; import { describe, it, expect } from 'vitest'; describe('safe path', () => { it('reject names with ../', () => { expect(safePath('./uplodas', '../foobar')).toBe(false); }); it('accept names with ./', () => { expect(safePath('./uplodas', './foobar')).toBe(true); }); it('reject names with /', () => { expect(safePath('./uplodas', 'foo/bar')).toBe(false); }); it('accept happy path', () => { expect(safePath('./uplodas', 'foobar')).toBe(true); }); it('accept names starting with `..`', () => { expect(safePath('./uplodas', '..foobar')).toBe(true); }); it('accept names ending with `..`', () => { expect(safePath('./uplodas', 'foobar..')).toBe(true); }); it('accept names starting and ending with `..`', () => { expect(safePath('./uplodas', '..foobar..')).toBe(true); }); }); describe('timedExecution', () => { const asyncIdentity = async (v: T): Promise => v; const identity = (v: T): T => v; it('works with async', async () => { const { executionTime, result } = await timedExecution(() => asyncIdentity(5)); expect(result).toBe(5); // execution time is always positive expect(executionTime).toBeGreaterThanOrEqual(0); }); it('works with sync', async () => { const { executionTime, result } = await timedExecution(() => identity(5)); expect(result).toBe(5); // execution time is always positive expect(executionTime).toBeGreaterThanOrEqual(0); }); });