-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: consolidated files; add log level cache
- Loading branch information
Showing
34 changed files
with
930 additions
and
714 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
electron/common/async/wait-until.ts → electron/common/async/async.utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
electron/common/game/__tests__/get-experience-mindstate.test.ts
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...n/common/game/get-experience-mindstate.ts → electron/common/game/game.utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.