File size: 1,960 Bytes
8766bc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { assert, describe, test } from "vitest";
import { prettyBytes, deepCopy, randInt, getNextColor } from "./helpers";

describe("prettyBytes", () => {
	test("handle B", () => {
		assert.equal(prettyBytes(10), "10.0 B");
	});

	test("handles KB", () => {
		assert.equal(prettyBytes(1_300), "1.3 KB");
	});

	test("handles MB", () => {
		assert.equal(prettyBytes(1_300_000), "1.2 MB");
	});

	test("handles GB", () => {
		assert.equal(prettyBytes(1_300_000_123), "1.2 GB");
	});

	test("handles PB", () => {
		assert.equal(prettyBytes(1_300_000_123_000), "1.2 PB");
	});
});

describe("deepCopy", () => {
	test("handle arrays", () => {
		const array = [1, 2, 3];
		const copy = deepCopy(array);
		assert.ok(array !== copy);
		assert.deepEqual(array, copy);
	});

	test("handle objects", () => {
		const obj = { a: 1, b: 2, c: 3 };
		const copy = deepCopy(obj);
		assert.ok(obj !== copy);
		assert.deepEqual(obj, copy);
	});

	test("handle complex structures", () => {
		const obj = {
			a: 1,
			b: {
				a: 1,
				b: {
					a: 1,
					b: { a: 1, b: 2, c: 3 },
					c: [
						1,
						2,
						{ a: 1, b: { a: 1, b: { a: 1, b: 2, c: 3 }, c: [1, 2, 3] } }
					]
				},
				c: 3
			},
			c: 3
		};
		const copy = deepCopy(obj);
		assert.ok(obj !== copy);
		assert.deepEqual(obj, copy);
	});
});

describe("randInt", () => {
	test("returns a random number", () => {
		assert.typeOf(randInt(0, 10), "number");
	});

	test("respects min and max", () => {
		const n = randInt(0, 10);
		assert.ok(n >= 0 && n <= 10);
	});

	test("respects min and max when negative", () => {
		const n = randInt(-100, -10);
		assert.ok(n >= -100 && n <= -10);
	});
});

describe("getNextColor", () => {
	test("returns a color", () => {
		assert.equal(getNextColor(0), "rgba(255, 99, 132, 1)");
	});

	test("returns a color when index is very high", () => {
		assert.ok(
			getNextColor(999999999).match(
				/rgba\([0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}, [0-9]\)/
			)
		);
	});
});