Add experiment logs to the monitoring page

This commit is contained in:
Twirre
2026-01-28 10:15:58 +00:00
committed by Björn Otgaar
parent 78901ee25b
commit 835de03a29
25 changed files with 619 additions and 124 deletions

View File

@@ -0,0 +1,34 @@
import capitalize from "../../src/utils/capitalize.ts";
describe('capitalize', () => {
it('capitalizes the first letter of a lowercase word', () => {
expect(capitalize('hello')).toBe('Hello');
});
it('keeps the first letter capitalized if already uppercase', () => {
expect(capitalize('Hello')).toBe('Hello');
});
it('handles single character strings', () => {
expect(capitalize('a')).toBe('A');
expect(capitalize('A')).toBe('A');
});
it('returns empty string for empty input', () => {
expect(capitalize('')).toBe('');
});
it('only capitalizes the first letter, leaving the rest unchanged', () => {
expect(capitalize('hELLO')).toBe('HELLO');
expect(capitalize('hello world')).toBe('Hello world');
});
it('handles strings starting with numbers', () => {
expect(capitalize('123abc')).toBe('123abc');
});
it('handles strings starting with special characters', () => {
expect(capitalize('!hello')).toBe('!hello');
expect(capitalize(' hello')).toBe(' hello');
});
});

View File

@@ -0,0 +1,77 @@
import { describe, it, expect, beforeEach, afterEach } from '@jest/globals';
import delayedResolve from "../../src/utils/delayedResolve.ts";
describe('delayedResolve', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
it('returns the resolved value of the promise', async () => {
const resultPromise = delayedResolve(Promise.resolve('hello'), 100);
await jest.advanceTimersByTimeAsync(100);
expect(await resultPromise).toBe('hello');
});
it('waits at least minDelayMs before resolving', async () => {
let resolved = false;
const resultPromise = delayedResolve(Promise.resolve('fast'), 100);
resultPromise.then(() => { resolved = true; });
await jest.advanceTimersByTimeAsync(50);
expect(resolved).toBe(false);
await jest.advanceTimersByTimeAsync(50);
expect(resolved).toBe(true);
});
it('resolves immediately after slow promise if it exceeds minDelayMs', async () => {
let resolved = false;
const slowPromise = new Promise<string>(resolve =>
setTimeout(() => resolve('slow'), 150)
);
const resultPromise = delayedResolve(slowPromise, 50);
resultPromise.then(() => { resolved = true; });
await jest.advanceTimersByTimeAsync(50);
expect(resolved).toBe(false);
await jest.advanceTimersByTimeAsync(100);
expect(resolved).toBe(true);
expect(await resultPromise).toBe('slow');
});
it('propagates rejections from the promise', async () => {
const error = new Error('test error');
const rejectedPromise = Promise.reject(error);
const resultPromise = delayedResolve(rejectedPromise, 100);
const assertion = expect(resultPromise).rejects.toThrow('test error');
await jest.advanceTimersByTimeAsync(100);
await assertion;
});
it('works with different value types', async () => {
const test = async <T>(value: T) => {
const resultPromise = delayedResolve(Promise.resolve(value), 10);
await jest.advanceTimersByTimeAsync(10);
return resultPromise;
};
expect(await test(42)).toBe(42);
expect(await test({ foo: 'bar' })).toEqual({ foo: 'bar' });
expect(await test([1, 2, 3])).toEqual([1, 2, 3]);
expect(await test(null)).toBeNull();
});
it('handles zero delay', async () => {
const resultPromise = delayedResolve(Promise.resolve('instant'), 0);
await jest.advanceTimersByTimeAsync(0);
expect(await resultPromise).toBe('instant');
});
});