Feat/Fix: Added Vitest/Cleaned up duplicate code#15
Feat/Fix: Added Vitest/Cleaned up duplicate code#15TuringProblem wants to merge 2 commits intoRyeL1te:stagingfrom
Conversation
| selectDirectory: async (options: any): Promise<string | null> => | ||
| ipcRenderer.invoke('settings:select-directory', options), | ||
| validateDirectory: async (dirPath: string): Promise<boolean> => | ||
| ipcRenderer.invoke('settings:validate-directory', dirPath), |
There was a problem hiding this comment.
I think the original code for this particular section is more readable, but the updated code is more explicit with types. I don't have a better suggestion on how to make the code typesafe than what has already been written.
We should think about how to make code like this more readable in the future, but this is fine for now.
There was a problem hiding this comment.
I agree with you, I also know we can dive a little deeper so we can find a better solution in terms of readability while still maintaining types. I'll dive deeper in the docs and build types for these so we can prefer the type over verbose version.
| Object.prototype.hasOwnProperty.call( | ||
| incomingField, | ||
| 'value' | ||
| ) |
There was a problem hiding this comment.
nitpick: I would recommend putting the two closing parenthesis and opening brace on line 32.
code is fine though, i'm not going to request changes just for that
There was a problem hiding this comment.
to be honest the original stylistic choice of the hilite codebase is a little rough around the edges. whilst my suggestion would be more consistent, the current format is more readable.
There was a problem hiding this comment.
Yeah again this was most definitely from yarn format - I prefer the first as well. I apologize for having it do that my initially thought was that everyone who is going to be working on the repo in the future would also use yarn format.
We could also setup a .eslintrc if there isn't one already?
| out[sectionKey][field.label] = field.value ?? field.default; | ||
| }); | ||
| }); | ||
| Object.entries(settingsSchema.settings).forEach( |
There was a problem hiding this comment.
All this was from yarn build - I image that we all want the same formatting?
| const others = BrowserWindow.getAllWindows().filter(w => w !== consoleWindowRef); | ||
| if (others.length === 0 && consoleWindowRef && !consoleWindowRef.isDestroyed()) { | ||
| const others = BrowserWindow.getAllWindows().filter( | ||
| w => w !== consoleWindowRef |
There was a problem hiding this comment.
I personally dislike this, again this was from yarn format.
| selectDirectory: async (options: any): Promise<string | null> => | ||
| ipcRenderer.invoke('settings:select-directory', options), | ||
| validateDirectory: async (dirPath: string): Promise<boolean> => | ||
| ipcRenderer.invoke('settings:validate-directory', dirPath), |
There was a problem hiding this comment.
I agree with you, I also know we can dive a little deeper so we can find a better solution in terms of readability while still maintaining types. I'll dive deeper in the docs and build types for these so we can prefer the type over verbose version.
| display: flex; | ||
| height: -webkit-fill-available; | ||
| align-items: anchor-center; | ||
| " |
There was a problem hiding this comment.
nit: i would put closing quote at the end of line 69
| if (warningIcon) { | ||
| warningIcon.classList.remove('warning', 'error'); | ||
| } | ||
| if (warningIcon) warningIcon.classList.remove('warning', 'error'); |
There was a problem hiding this comment.
In the future, I am trying to avoid single-line if/else/loop statements in new code -- always prefer braces.
| closeButton.addEventListener('click', () => { | ||
| window.electron.ipcRenderer.send('close-window'); | ||
| }); | ||
| setupWindowControls(); |
|
|
||
| describe('Storage Management', () => { | ||
| it('should handle cache limit enforcement', () => { | ||
| const enforceMessageLimit = ( |
There was a problem hiding this comment.
The tests should be importing a function from an actual module we're using, and testing that said function operates as it should.
Not all functions are suitable for unit testing, and you may have to change how you do things in some functions in order to unit test properly.
Please remove or update this file to instead import the actual functions you want to unit-test, and then test said functions. Take a look at the sum.test.js example here: https://vitest.dev/guide/
| btn.addEventListener('click', e => { | ||
| const type = (e.target as HTMLElement).dataset.type; | ||
|
|
||
| return type ? this.setFilter(type) : undefined; |
There was a problem hiding this comment.
Please don't return anything here -- as it's currently set up, it's either returning "void" or "undefined".
Just calling setFilter should suffice.
| @@ -0,0 +1,142 @@ | |||
| import { describe, it, expect, beforeEach } from 'vitest'; | |||
|
|
|||
| class MockConsoleManager { | |||
There was a problem hiding this comment.
please have your mock extend from the actual ConsoleManager. then use the base class methods, and verify that the state of the mocked console manager is correct
There was a problem hiding this comment.
edit: not sure if "ConsoleManager" is one of our classes or not. If not, then this mock is fine as-is.
| windowControls.forEach(control => { | ||
| onClickSend(control.selector, control.channel); | ||
| }); | ||
| }; |
There was a problem hiding this comment.
I love this class and the refactor you've done, great work!!
| const button = document.getElementById('minimizeBtn'); | ||
| expect(button).toBeTruthy(); | ||
|
|
||
| helperModule.onClickSend('#minimizeBtn', 'minimize-window'); |
There was a problem hiding this comment.
Good work! This is how the other tests should be set up, too -- calling one of our actual functions.
|
|
||
| describe('Validation', () => { | ||
| it('should validate directory paths', () => { | ||
| const isValidPath = (path: string) => { |
There was a problem hiding this comment.
If possible, please test the actual function "isValidPath" (if it exists) instead of a local function
| }); | ||
|
|
||
| it('should validate number ranges', () => { | ||
| const isValidNumber = (value: number, min: number, max: number) => { |
| expect(safeParse('invalid json')).toBeNull(); | ||
| }); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
please update the local functions here to instead call the actual functions we want to test (when possible)
|
|
||
| describe('Progress Handling', () => { | ||
| it('should round progress to nearest integer', () => { | ||
| const roundProgress = (progress: number) => Math.round(progress); |
There was a problem hiding this comment.
please test the actual "roundProgress" function we use in the code instead of a local function in this test function
| ); | ||
| }); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
For future reference: Instead of using a local function, (e.g., 'handleUpdateDownloaded') we should change the actual "handleUpdateDownloaded" function to be unit-testable.
We might have to write something like a mock "download" that just returns the "successful" data that the function is expecting.
There was a problem hiding this comment.
Overall, please see if you can refactor the tests a bit so that more of our actual functions can be tested. You might have to change the functions we're testing themselves. No pressure, though -- any tests are better than no tests! Just see what you can do.
Also, please fix this:
Please don't return anything here -- as it's currently set up, it's either returning "void" or "undefined".
Just calling setFilter should suffice.
edit: Also forgot to mention, the vast majority of the code was excellent and left in a far more readable state than it began. Very good work!
ash-of-the-meadow
left a comment
There was a problem hiding this comment.
Working on re-reviewing this now
ash-of-the-meadow
left a comment
There was a problem hiding this comment.
Good work! Most of my issues were with the formatting, but that's because our formatter is messed up.
For vitest, some testing's better than no testing. I'd rather have darwin fixed than have perfect tests.
This pull request introduces several improvements and refactors across the codebase, primarily focused on code style consistency, enhanced settings management, and improved testing capabilities. The most significant changes include the addition of Vitest for testing, refactoring for better readability and maintainability, and updates to IPC handlers and settings APIs for more robust inter-process communication.
Testing and Tooling Enhancements:
vitest,@vitest/ui,happy-dom) topackage.json, along with new npm scripts for running tests, coverage, and UI testing. [1] [2]Settings Management and IPC Improvements:
src/main/modules/settingsManagement/index.tsfor improved readability, including better formatting, more robust dynamic default handling, and enhanced IPC handlers for settings operations such as get, set, select directory, and validate directory. [1] [2] [3] [4] [5] [6]src/preload/index.tsto use explicit TypeScript types and improve the interface for settings and screenshot operations.Code Style and Readability Refactors:
src/main/windows/client/index.ts,src/main/modules/screenshotManagement/index.ts, andsrc/main/windows/settings/index.ts. [1] [2] [3] [4] [5] [6]Window and Event Handling Updates:
src/main/index.tsandsrc/main/windows/client/modules/windowEventManagement/index.ts. [1] [2]Settings Schema Robustness:
SettingsSchemaclass insrc/preload/settings.tsto better handle incoming data and preserve validation and values during schema updates.These changes collectively improve the developer experience, code maintainability, and reliability of the application's settings and testing infrastructure.