<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
import { deepMerge, isObject, sleep } from '@shared/lib/utils';

describe('isObject', () => {
	it('returns true for plain objects', () => {
		expect(isObject({})).toBe(true);
		expect(isObject({ a: 1 })).toBe(true);
	});

	it('returns false for arrays', () => {
		expect(isObject([])).toBe(false);
	});

	it('returns false for null', () => {
		expect(isObject(null)).toBe(false);
	});

	it('returns false for other types', () => {
		expect(isObject(42)).toBe(false);
		expect(isObject('string')).toBe(false);
		expect(isObject(undefined)).toBe(false);
	});
});

describe('deepMerge', () => {
	it('merges two flat objects', () => {
		const result = deepMerge({ a: 1 }, { b: 2 });
		expect(result).toEqual({ a: 1, b: 2 });
	});

	it('merges deeply nested objects', () => {
		const result = deepMerge({ a: { x: 1 }, b: 2 }, { a: { y: 3 }, c: 4 });
		expect(result).toEqual({ a: { x: 1, y: 3 }, b: 2, c: 4 });
	});

	it('overwrites non-object values', () => {
		const result = deepMerge({ a: 1 }, { a: 2 });
		expect(result).toEqual({ a: 2 });
	});

	it('returns null if any input is not an object', () => {
		expect(deepMerge(null, { a: 1 })).toBeNull();
		expect(deepMerge({ a: 1 }, 123)).toBeNull();
	});
});

describe('sleep', () => {
	it('resolves after given time', async () => {
		const start = Date.now();
		await sleep(50);
		const duration = Date.now() - start;
		// Note: setTimeout can fire slightly early due to timer coalescing,
		// so we allow some buffer in the assertion
		expect(duration).toBeGreaterThanOrEqual(47);
	});
});
