From a6b8f2232f76d2e01bb70c3b4117d820fbca8e14 Mon Sep 17 00:00:00 2001 From: ransome1 Date: Fri, 22 Sep 2023 14:41:51 +0200 Subject: [PATCH] Enhanced the logic of recurring todos which have a threshold date (https://github.com/ransome1/sleek/wiki/Deferred-todos-(t:)), added test cases for this logic --- CONTRIBUTING.md | 102 +-------------------- src/__tests__/__mock__/recurrence.txt | 5 +- src/__tests__/main/CreateRecurringTodo.tsx | 30 +++++- src/main/main.ts | 1 + src/main/modules/CreateRecurringTodo.ts | 20 ++-- 5 files changed, 44 insertions(+), 114 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3c571532..ee1a2d15 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,103 +1,3 @@ # Contribution Guidelines -Thank you for your interest in contributing to the sleek project! We welcome contributions from the community to help improve and enhance the project. To ensure a smooth collaboration, please review and follow these guidelines. - -## Table of Contents - -1. [Getting Started](#getting-started) -2. [Contributing](#contributing) - - [Reporting Issues](#reporting-issues) - - [Feature Requests](#feature-requests) - - [Pull Requests](#pull-requests) - - [GitHub Issue Backlog](#github-issue-backlog) -3. [Development Setup](#development-setup) -4. [Testing](#testing) -5. [Coding Guidelines](#coding-guidelines) -6. [Commit Guidelines](#commit-guidelines) -7. [Code of Conduct](#code-of-conduct) - -## Getting Started - -Before contributing, make sure you have the latest version of Node.js and npm installed on your system. - -## Contributing - -### Reporting Issues - -If you encounter any issues or bugs while using sleek, please open an issue on the GitHub repository. When reporting issues, please provide detailed information about the problem, including steps to reproduce and any error messages or logs if applicable. - -### Feature Requests - -If you have ideas for new features or improvements, we encourage you to open a feature request issue on GitHub. Describe the desired functionality or improvement in detail to help us understand and evaluate the suggestion. - -### Pull Requests - -We welcome pull requests for bug fixes, enhancements, or new features. To contribute code, please follow these steps: - -1. Fork the repository and create a new branch for your feature or bug fix. -2. Make your changes in the new branch, adhering to the [coding guidelines](#coding-guidelines). -3. Write tests using [Jest](https://jestjs.io/) to cover your changes. -4. Ensure that your code and tests pass by running `npm test`. -5. Commit your changes using [proper commit guidelines](#commit-guidelines). -6. Push your branch to your forked repository. -7. Open a pull request against the main repository's `master` branch. -8. Provide a clear description of your changes and the problem they solve. - -We will review your pull request as soon as possible. Your contributions are greatly appreciated! - -### GitHub Issue Backlog - -Before starting work on sleek, we recommend reviewing the GitHub Issue backlog to align your contributions with existing issues. This helps avoid duplication of efforts and ensures that your work aligns with the project roadmap. If you find an issue that you would like to contribute to, please indicate your interest by commenting on the issue. - -## Development Setup - -To set up the development environment and run sleek locally, follow these steps: - -1. Clone the repository: `git clone https://github.com/your-username/sleek.git` -2. Install project dependencies: `npm install` -3. Start the development server: `npm start` - -## Testing - -sleek uses [Jest](https://jestjs.io/) as the testing framework. To run tests, use the following command: `npm test` - -To contribute to the testing efforts, consider the following: - -- Write tests to cover new features or changes in functionality. -- Ensure that existing tests pass before making new contributions. -- Aim for high test coverage to maintain code quality. - -## Coding Guidelines - -To maintain code consistency, readability, and quality, please adhere to the following guidelines: - -- Write lean and efficient code. Avoid unnecessary complexity or redundant logic. -- Follow the [React coding style guide](https://reactjs.org/docs/style-guide.html) for writing React components. -- Use meaningful and descriptive variable and function names. -- Write clear and concise comments when necessary. -- Prioritize code readability over cleverness. -- Consider the user interface and user experience when adding new functionality. Strive for an intuitive and visually appealing interface. - -## Commit Guidelines - -When making commits, please follow these guidelines: - -- Use descriptive and meaningful commit messages. -- Separate the subject from the body with a blank line. -- Start the subject with a verb in the imperative mood (e.g., "Add," "Fix," "Update"). -- Keep the subject line short and concise (preferably 50 characters or less). -- Provide additional details in the body if needed. -- Reference relevant issues or pull requests in the commit message if applicable. - -Example: -``` -Add new feature - -Implement a new feature that allows users to customize settings. - -Fixes #123 -``` - -Thank you for taking the time to review these contributing guidelines. We appreciate your contributions and look forward to collaborating with you! If you have any questions, feel free to reach out to us. - -Happy coding and testing! +Please find sleek's contribution guidelines here: https://github.com/ransome1/sleek/wiki/Contributing \ No newline at end of file diff --git a/src/__tests__/__mock__/recurrence.txt b/src/__tests__/__mock__/recurrence.txt index 1b8b2266..b72fcaff 100644 --- a/src/__tests__/__mock__/recurrence.txt +++ b/src/__tests__/__mock__/recurrence.txt @@ -4,4 +4,7 @@ 2023-09-22 Line 1 rec:2m due:2023-11-22 2023-09-22 Line 1 rec:+1d due:2023-09-24 2023-09-22 Line 1 rec:7w due:2023-11-10 -2023-07-21 Line 1 rec:+1b due:2023-07-24 \ No newline at end of file +2023-07-21 Line 1 due:2023-07-24 rec:+1b +2021-01-01 taxes are due in one year t:2022-03-30 due:2022-04-30 rec:+1y +2023-09-22 Water plants @home +quick due:2023-09-29 t:2023-09-19 rec:1w +2023-09-22 Line 1 rec:+1d t:2023-09-20 due:2023-09-23 \ No newline at end of file diff --git a/src/__tests__/main/CreateRecurringTodo.tsx b/src/__tests__/main/CreateRecurringTodo.tsx index a3b5fe97..8b21022b 100644 --- a/src/__tests__/main/CreateRecurringTodo.tsx +++ b/src/__tests__/main/CreateRecurringTodo.tsx @@ -26,7 +26,9 @@ const dateToday = dayjs(new Date()); const dateTodayString = dayjs(dateToday).format('YYYY-MM-DD'); const dateTomorrowString = dateToday.add(1, 'day').format('YYYY-MM-DD'); const dateDayAfterTomorrowString = dateToday.add(2, 'day').format('YYYY-MM-DD'); -const dateInOneWeek = dateToday.add(7, 'day').format('YYYY-MM-DD'); +const dateInOneWeek = dateToday.add(7, 'day'); +const dateInOneWeekString = dateInOneWeek.format('YYYY-MM-DD'); +const dateInOneWeekMinus10String = dateInOneWeek.subtract(10, 'day').format('YYYY-MM-DD'); const dateTodayInTwoMonths = dateToday.add(2, 'month').format('YYYY-MM-DD'); const dateTodayInSevenWeeks = dateToday.add(7, 'week').format('YYYY-MM-DD'); @@ -45,7 +47,7 @@ describe('Create recurring todos', () => { test('Should add a new todo with due date set to next week', async () => { const recurringTodo = await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:w`, 'w'); const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8'); - expect(fileContent.split('\n').pop()).toEqual(dateTodayString + ' Line 1 rec:w due:' + dateInOneWeek); + expect(fileContent.split('\n').pop()).toEqual(dateTodayString + ' Line 1 rec:w due:' + dateInOneWeekString); }); test('Should add a new todo with due date set to today in 2 months', async () => { @@ -66,10 +68,28 @@ describe('Create recurring todos', () => { expect(fileContent.split('\n').pop()).toEqual(dateTodayString + ' Line 1 rec:7w due:' + dateTodayInSevenWeeks); }); - test('Should add a new todo with due date set to next possible business day', async () => { - const recurringTodo = await createRecurringTodo(`x 2023-07-21 2023-07-21 Line 1 rec:+1b`, '+1b'); + test('Should add a new todo with due date set to next possible business day, based off a todo which already contains a due date', async () => { + const recurringTodo = await createRecurringTodo(`x 2023-07-21 2023-07-21 Line 1 due:2023-07-21 rec:+1b`, '+1b'); const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8'); - expect(fileContent.split('\n').pop()).toEqual('2023-07-21 Line 1 rec:+1b due:2023-07-24'); + expect(fileContent.split('\n').pop()).toEqual('2023-07-21 Line 1 due:2023-07-24 rec:+1b'); }); + + test('Should add a new todo adding a strict recurrence of one year to due date and threshold date', async () => { + const recurringTodo = await createRecurringTodo(`x 2021-01-01 2021-01-01 taxes are due in one year t:2021-03-30 due:2021-04-30 rec:+1y`, '+1y'); + const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8'); + expect(fileContent.split('\n').pop()).toEqual('2021-01-01 taxes are due in one year t:2022-03-30 due:2022-04-30 rec:+1y'); + }); + + test('Should add a new todo adding a non-strict recurrence of one week to due date and threshold date', async () => { + const recurringTodo = await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Water plants @home +quick due:2021-07-19 t:2021-07-09 rec:1w`, '1w'); + const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8'); + expect(fileContent.split('\n').pop()).toEqual(`${dateTodayString} Water plants @home +quick due:${dateInOneWeekString} t:${dateInOneWeekMinus10String} rec:1w`); + }); + + test('Should add a new todo adding a strict recurrence of one day to due date and threshold date, while the todo initially only has a threshold date', async () => { + const recurringTodo = await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:+1d t:2023-09-19`, '1+d'); + const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8'); + expect(fileContent.split('\n').pop()).toEqual(`${dateTodayString} Line 1 rec:+1d t:2023-09-20 due:${dateTomorrowString}`); + }); }); \ No newline at end of file diff --git a/src/main/main.ts b/src/main/main.ts index 4c54b6f6..5137a350 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -76,6 +76,7 @@ const createWindow = async() => { fs.readFile(customStylesPath, 'utf8', (err, data) => { if (!err) { mainWindow.webContents.insertCSS(data); + console.error('Styles injected found in CSS file:', customStylesPath); } else { console.error('Error reading the CSS file:', err); } diff --git a/src/main/modules/CreateRecurringTodo.ts b/src/main/modules/CreateRecurringTodo.ts index 2f0b3d8b..fc358920 100644 --- a/src/main/modules/CreateRecurringTodo.ts +++ b/src/main/modules/CreateRecurringTodo.ts @@ -46,13 +46,19 @@ const createRecurringTodo = async (string: string, recurrence: string): Promise< const recurringTodoObject = new Item(string); const completedDate = recurringTodoObject.completed(); if (recurrence && completedDate) { - const strictRecurrence = recurrence.startsWith('+'); - const recurrenceInterval = strictRecurrence ? recurrence.slice(1) : recurrence; - const oldDueDate = recurringTodoObject?.extensions()?.find((item) => item.key === 'due'); - const targetDate = strictRecurrence && oldDueDate?.value - ? addRecurrenceToDate(dayjs(oldDueDate.value, 'YYYY-MM-DD').toDate(), recurrenceInterval) - : addRecurrenceToDate(completedDate, recurrenceInterval); - recurringTodoObject.setExtension('due', dayjs(targetDate).format('YYYY-MM-DD')); + const strictRecurrence: boolean = recurrence.startsWith('+'); + const recurrenceInterval: any = strictRecurrence ? recurrence.slice(1) : recurrence; + const oldDueDate: any = recurringTodoObject?.extensions()?.find((item) => item.key === 'due')?.value; + const oldThresholdDate: any = recurringTodoObject?.extensions()?.find((item) => item.key === 't')?.value; + const daysBetween = dayjs(oldDueDate, 'YYYY-MM-DD').diff(oldThresholdDate, 'day'); + const newDueDate = strictRecurrence + ? addRecurrenceToDate(dayjs(oldDueDate).toDate(), recurrenceInterval) + : addRecurrenceToDate(dayjs(completedDate).toDate(), recurrenceInterval); + const newThresholdDate = strictRecurrence + ? addRecurrenceToDate(dayjs(oldThresholdDate).toDate(), recurrenceInterval) + : dayjs(newDueDate).subtract(daysBetween, 'day').toDate(); + if(completedDate) recurringTodoObject.setExtension('due', dayjs(newDueDate).format('YYYY-MM-DD')); + if(oldThresholdDate) recurringTodoObject.setExtension('t', dayjs(newThresholdDate).format('YYYY-MM-DD')); recurringTodoObject.setComplete(false); recurringTodoObject.setCompleted(null); await writeTodoObjectToFile(-1, recurringTodoObject.toString(), false);