51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
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 <T>(v: T): Promise<T> => v;
|
|
const identity = <T>(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);
|
|
});
|
|
});
|