This repository has been archived on 2024-10-26. You can view files and clone it, but cannot push or open issues or pull requests.
Files
fotochallenge/src/lib/index.test.ts
Valentin Brandl 15de87432b
All checks were successful
/ Misc Linters (pull_request) Successful in 21s
/ Build App (pull_request) Successful in 1m27s
Typing
2024-08-29 10:37:38 +02:00

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);
});
});