Skip to content

Commit 322bb5e

Browse files
committed
feat: consolidated files; add log level cache
1 parent a31d8c3 commit 322bb5e

34 files changed

+930
-714
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { sleep, waitUntil } from '../async.utils.js';
3+
4+
describe('async-utils', () => {
5+
describe('#sleep', () => {
6+
it('it sleeps for the given milliseconds', async () => {
7+
const timeoutSpy = vi.spyOn(global, 'setTimeout');
8+
9+
const sleepMillis = 100;
10+
const varianceMillis = sleepMillis * 0.5;
11+
12+
const start = Date.now();
13+
14+
await sleep(100);
15+
16+
const end = Date.now();
17+
18+
expect(timeoutSpy).toHaveBeenCalledTimes(1);
19+
expect(end - start).toBeGreaterThanOrEqual(sleepMillis - varianceMillis);
20+
expect(end - start).toBeLessThanOrEqual(sleepMillis + varianceMillis);
21+
});
22+
});
23+
24+
describe('#waitUntil', () => {
25+
beforeEach(() => {
26+
vi.useFakeTimers({ shouldAdvanceTime: true });
27+
});
28+
29+
afterEach(() => {
30+
vi.clearAllMocks();
31+
vi.clearAllTimers();
32+
vi.useRealTimers();
33+
});
34+
35+
it('resolves true when condition is true', async () => {
36+
const condition = vi.fn().mockReturnValueOnce(true);
37+
const interval = 100;
38+
const timeout = 1000;
39+
40+
const resultAsync = waitUntil({ condition, interval, timeout });
41+
vi.runAllTimers();
42+
const result = await resultAsync;
43+
44+
expect(result).toEqual(true);
45+
expect(condition).toHaveBeenCalledTimes(1);
46+
});
47+
48+
it('resolves false when condition is false', async () => {
49+
const condition = vi.fn().mockReturnValueOnce(false);
50+
const interval = 100;
51+
const timeout = 1000;
52+
const iterations = Math.floor(timeout / interval);
53+
54+
const resultAsync = waitUntil({ condition, interval, timeout });
55+
vi.runAllTimers();
56+
const result = await resultAsync;
57+
58+
expect(result).toEqual(false);
59+
expect(condition).toHaveBeenCalledTimes(iterations);
60+
});
61+
62+
it('resolves true when condition is true after 5 intervals', async () => {
63+
const condition = vi
64+
.fn()
65+
.mockReturnValueOnce(false)
66+
.mockReturnValueOnce(false)
67+
.mockReturnValueOnce(false)
68+
.mockReturnValueOnce(false)
69+
.mockReturnValueOnce(true);
70+
const interval = 100;
71+
const timeout = 1000;
72+
73+
const resultAsync = waitUntil({ condition, interval, timeout });
74+
vi.runAllTimers();
75+
const result = await resultAsync;
76+
77+
expect(result).toEqual(true);
78+
expect(condition).toHaveBeenCalledTimes(5);
79+
});
80+
});
81+
});

electron/common/async/__tests__/sleep.test.ts

-21
This file was deleted.

electron/common/async/__tests__/wait-until.test.ts

-60
This file was deleted.

electron/common/async/wait-until.ts electron/common/async/async.utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import * as rxjs from 'rxjs';
22

3+
/**
4+
* Resolves after the given number of milliseconds.
5+
* Promisified version of `setTimeout`.
6+
*/
7+
export const sleep = (ms: number): Promise<void> => {
8+
return new Promise((resolve) => setTimeout(resolve, ms));
9+
};
10+
311
/**
412
* Resolves true if the condition returns true before the timeout, else false.
513
*/

electron/common/async/sleep.ts

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { getExperienceMindState } from '../game.utils.js';
3+
import { ExperienceMindState } from '../types.js';
4+
5+
describe('game-utils', () => {
6+
describe('#getExperienceMindstate', () => {
7+
it('returns the correct value for the given mind state (enum test)', () => {
8+
Object.keys(ExperienceMindState).forEach((mindState) => {
9+
expect(getExperienceMindState(mindState)).toEqual(
10+
ExperienceMindState[mindState as keyof typeof ExperienceMindState]
11+
);
12+
});
13+
});
14+
15+
it('returns the correct value for the given mind state (explicit test)', () => {
16+
// I added this test because at one point I had accidentally
17+
// removed some of the mind states from the enum but no test caught it.
18+
expect(getExperienceMindState('clear')).toEqual(0);
19+
expect(getExperienceMindState('dabbling')).toEqual(1);
20+
expect(getExperienceMindState('perusing')).toEqual(2);
21+
expect(getExperienceMindState('learning')).toEqual(3);
22+
expect(getExperienceMindState('thoughtful')).toEqual(4);
23+
expect(getExperienceMindState('thinking')).toEqual(5);
24+
expect(getExperienceMindState('considering')).toEqual(6);
25+
expect(getExperienceMindState('pondering')).toEqual(7);
26+
expect(getExperienceMindState('ruminating')).toEqual(8);
27+
expect(getExperienceMindState('concentrating')).toEqual(9);
28+
expect(getExperienceMindState('attentive')).toEqual(10);
29+
expect(getExperienceMindState('deliberative')).toEqual(11);
30+
expect(getExperienceMindState('interested')).toEqual(12);
31+
expect(getExperienceMindState('examining')).toEqual(13);
32+
expect(getExperienceMindState('understanding')).toEqual(14);
33+
expect(getExperienceMindState('absorbing')).toEqual(15);
34+
expect(getExperienceMindState('intrigued')).toEqual(16);
35+
expect(getExperienceMindState('scrutinizing')).toEqual(17);
36+
expect(getExperienceMindState('analyzing')).toEqual(18);
37+
expect(getExperienceMindState('studious')).toEqual(19);
38+
expect(getExperienceMindState('focused')).toEqual(20);
39+
expect(getExperienceMindState('very focused')).toEqual(21);
40+
expect(getExperienceMindState('engaged')).toEqual(22);
41+
expect(getExperienceMindState('very engaged')).toEqual(23);
42+
expect(getExperienceMindState('cogitating')).toEqual(24);
43+
expect(getExperienceMindState('fascinated')).toEqual(25);
44+
expect(getExperienceMindState('captivated')).toEqual(26);
45+
expect(getExperienceMindState('engrossed')).toEqual(27);
46+
expect(getExperienceMindState('riveted')).toEqual(28);
47+
expect(getExperienceMindState('very riveted')).toEqual(29);
48+
expect(getExperienceMindState('rapt')).toEqual(30);
49+
expect(getExperienceMindState('very rapt')).toEqual(31);
50+
expect(getExperienceMindState('enthralled')).toEqual(32);
51+
expect(getExperienceMindState('nearly locked')).toEqual(33);
52+
expect(getExperienceMindState('mind lock')).toEqual(34);
53+
});
54+
55+
it('returns undefined if the given mind state is invalid', () => {
56+
expect(getExperienceMindState('foo')).toBe(undefined);
57+
});
58+
});
59+
});

electron/common/game/__tests__/get-experience-mindstate.test.ts

-57
This file was deleted.

electron/common/game/get-experience-mindstate.ts electron/common/game/game.utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { toUpperSnakeCase } from '../string/to-upper-snake-case.js';
1+
import { toUpperSnakeCase } from '../string/string.utils.js';
22
import type { Maybe } from '../types.js';
33
import { ExperienceMindState } from './types.js';
44

0 commit comments

Comments
 (0)