Skip to content

Commit

Permalink
feat: consolidated files; add log level cache
Browse files Browse the repository at this point in the history
  • Loading branch information
KatoakDR committed Jan 7, 2025
1 parent a31d8c3 commit 322bb5e
Show file tree
Hide file tree
Showing 34 changed files with 930 additions and 714 deletions.
81 changes: 81 additions & 0 deletions electron/common/async/__tests__/async-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { sleep, waitUntil } from '../async.utils.js';

describe('async-utils', () => {
describe('#sleep', () => {
it('it sleeps for the given milliseconds', async () => {
const timeoutSpy = vi.spyOn(global, 'setTimeout');

const sleepMillis = 100;
const varianceMillis = sleepMillis * 0.5;

const start = Date.now();

await sleep(100);

const end = Date.now();

expect(timeoutSpy).toHaveBeenCalledTimes(1);
expect(end - start).toBeGreaterThanOrEqual(sleepMillis - varianceMillis);
expect(end - start).toBeLessThanOrEqual(sleepMillis + varianceMillis);
});
});

describe('#waitUntil', () => {
beforeEach(() => {
vi.useFakeTimers({ shouldAdvanceTime: true });
});

afterEach(() => {
vi.clearAllMocks();
vi.clearAllTimers();
vi.useRealTimers();
});

it('resolves true when condition is true', async () => {
const condition = vi.fn().mockReturnValueOnce(true);
const interval = 100;
const timeout = 1000;

const resultAsync = waitUntil({ condition, interval, timeout });
vi.runAllTimers();
const result = await resultAsync;

expect(result).toEqual(true);
expect(condition).toHaveBeenCalledTimes(1);
});

it('resolves false when condition is false', async () => {
const condition = vi.fn().mockReturnValueOnce(false);
const interval = 100;
const timeout = 1000;
const iterations = Math.floor(timeout / interval);

const resultAsync = waitUntil({ condition, interval, timeout });
vi.runAllTimers();
const result = await resultAsync;

expect(result).toEqual(false);
expect(condition).toHaveBeenCalledTimes(iterations);
});

it('resolves true when condition is true after 5 intervals', async () => {
const condition = vi
.fn()
.mockReturnValueOnce(false)
.mockReturnValueOnce(false)
.mockReturnValueOnce(false)
.mockReturnValueOnce(false)
.mockReturnValueOnce(true);
const interval = 100;
const timeout = 1000;

const resultAsync = waitUntil({ condition, interval, timeout });
vi.runAllTimers();
const result = await resultAsync;

expect(result).toEqual(true);
expect(condition).toHaveBeenCalledTimes(5);
});
});
});
21 changes: 0 additions & 21 deletions electron/common/async/__tests__/sleep.test.ts

This file was deleted.

60 changes: 0 additions & 60 deletions electron/common/async/__tests__/wait-until.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import * as rxjs from 'rxjs';

/**
* Resolves after the given number of milliseconds.
* Promisified version of `setTimeout`.
*/
export const sleep = (ms: number): Promise<void> => {
return new Promise((resolve) => setTimeout(resolve, ms));
};

/**
* Resolves true if the condition returns true before the timeout, else false.
*/
Expand Down
7 changes: 0 additions & 7 deletions electron/common/async/sleep.ts

This file was deleted.

59 changes: 59 additions & 0 deletions electron/common/game/__tests__/game-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, expect, it } from 'vitest';
import { getExperienceMindState } from '../game.utils.js';
import { ExperienceMindState } from '../types.js';

describe('game-utils', () => {
describe('#getExperienceMindstate', () => {
it('returns the correct value for the given mind state (enum test)', () => {
Object.keys(ExperienceMindState).forEach((mindState) => {
expect(getExperienceMindState(mindState)).toEqual(
ExperienceMindState[mindState as keyof typeof ExperienceMindState]
);
});
});

it('returns the correct value for the given mind state (explicit test)', () => {
// I added this test because at one point I had accidentally
// removed some of the mind states from the enum but no test caught it.
expect(getExperienceMindState('clear')).toEqual(0);
expect(getExperienceMindState('dabbling')).toEqual(1);
expect(getExperienceMindState('perusing')).toEqual(2);
expect(getExperienceMindState('learning')).toEqual(3);
expect(getExperienceMindState('thoughtful')).toEqual(4);
expect(getExperienceMindState('thinking')).toEqual(5);
expect(getExperienceMindState('considering')).toEqual(6);
expect(getExperienceMindState('pondering')).toEqual(7);
expect(getExperienceMindState('ruminating')).toEqual(8);
expect(getExperienceMindState('concentrating')).toEqual(9);
expect(getExperienceMindState('attentive')).toEqual(10);
expect(getExperienceMindState('deliberative')).toEqual(11);
expect(getExperienceMindState('interested')).toEqual(12);
expect(getExperienceMindState('examining')).toEqual(13);
expect(getExperienceMindState('understanding')).toEqual(14);
expect(getExperienceMindState('absorbing')).toEqual(15);
expect(getExperienceMindState('intrigued')).toEqual(16);
expect(getExperienceMindState('scrutinizing')).toEqual(17);
expect(getExperienceMindState('analyzing')).toEqual(18);
expect(getExperienceMindState('studious')).toEqual(19);
expect(getExperienceMindState('focused')).toEqual(20);
expect(getExperienceMindState('very focused')).toEqual(21);
expect(getExperienceMindState('engaged')).toEqual(22);
expect(getExperienceMindState('very engaged')).toEqual(23);
expect(getExperienceMindState('cogitating')).toEqual(24);
expect(getExperienceMindState('fascinated')).toEqual(25);
expect(getExperienceMindState('captivated')).toEqual(26);
expect(getExperienceMindState('engrossed')).toEqual(27);
expect(getExperienceMindState('riveted')).toEqual(28);
expect(getExperienceMindState('very riveted')).toEqual(29);
expect(getExperienceMindState('rapt')).toEqual(30);
expect(getExperienceMindState('very rapt')).toEqual(31);
expect(getExperienceMindState('enthralled')).toEqual(32);
expect(getExperienceMindState('nearly locked')).toEqual(33);
expect(getExperienceMindState('mind lock')).toEqual(34);
});

it('returns undefined if the given mind state is invalid', () => {
expect(getExperienceMindState('foo')).toBe(undefined);
});
});
});
57 changes: 0 additions & 57 deletions electron/common/game/__tests__/get-experience-mindstate.test.ts

This file was deleted.

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

Expand Down
Loading

0 comments on commit 322bb5e

Please sign in to comment.