diff --git a/__mocks__/factories/noteFactory.js b/__mocks__/factories/noteFactory.js new file mode 100644 index 000000000..4ebac9431 --- /dev/null +++ b/__mocks__/factories/noteFactory.js @@ -0,0 +1,55 @@ +export const noteWIthOpenAndCancelledTasks = { + paragraphs: [ + { type: 'title', lineIndex: 0, content: 'Title', headingLevel: 1 }, + { type: 'empty', lineIndex: 1 }, + { type: 'title', lineIndex: 2, content: 'Tasks for 3.4.22', headingLevel: 2 }, + { type: 'open', lineIndex: 3, content: 'task 1' }, + { type: 'title', lineIndex: 4, content: 'Journal for 3.4.22' }, + { type: 'list', lineIndex: 5, content: 'first journal entry' }, + { type: 'list', lineIndex: 6, content: 'second journal entry' }, + { type: 'empty', lineIndex: 7 }, + { type: 'title', lineIndex: 8, content: 'Done ...', headingLevel: 2 }, + { type: 'title', lineIndex: 9, content: 'Cancelled', headingLevel: 2 }, + { type: 'cancelled', lineIndex: 10, content: 'task cancelled' }, + ], +} + +export const noteWIthDoneAndScheduledTasks = { + paragraphs: [ + { type: 'title', lineIndex: 0, content: 'Title', headingLevel: 1 }, + { type: 'empty', lineIndex: 1 }, + { type: 'title', lineIndex: 2, content: 'Tasks for 3.4.22', headingLevel: 2 }, + { type: 'scheduled', lineIndex: 3, content: 'task 1' }, + { type: 'title', lineIndex: 4, content: 'Journal for 3.4.22' }, + { type: 'list', lineIndex: 5, content: 'first journal entry' }, + { type: 'list', lineIndex: 6, content: 'second journal entry' }, + { type: 'empty', lineIndex: 7 }, + { type: 'title', lineIndex: 8, content: 'Done ...', headingLevel: 2 }, + { type: 'done', lineIndex: 10, content: 'task finished' }, + ], + } + + export const noteWithOpenAndDoneTasks = { + paragraphs: [ + { type: 'title', lineIndex: 0, content: 'Title', headingLevel: 1 }, + { type: 'empty', lineIndex: 1 }, + { type: 'open', lineIndex: 2, content: 'Open task 1' }, + { type: 'open', lineIndex: 3, content: 'Open task 2' }, + { type: 'done', lineIndex: 4, content: 'Done task' }, + ], + } + +export const noteWithOneTaskOfEachType = { + paragraphs: [ + { type: 'title', lineIndex: 0, content: 'Title', headingLevel: 1 }, + { type: 'empty', lineIndex: 1 }, + { type: 'open', lineIndex: 2, content: 'Open task 1' }, + { type: 'scheduled', lineIndex: 3, content: 'Scheduled task 2' }, + { type: 'done', lineIndex: 4, content: 'Done task' }, + { type: 'cancelled', lineIndex: 5, content: 'Cancelled task' }, + { type: 'checklist', lineIndex: 6, content: 'checklist' }, + { type: 'checklistDone', lineIndex: 7, content: 'checklistDone' }, + { type: 'checklistCancelled', lineIndex: 8, content: 'checklistCancelled' }, + { type: 'checklistScheduled', lineIndex: 9, content: 'checklistScheduled' }, + ], +} \ No newline at end of file diff --git a/helpers/__tests__/tasks.test..js b/helpers/__tests__/tasks.test..js new file mode 100644 index 000000000..a3e2281f0 --- /dev/null +++ b/helpers/__tests__/tasks.test..js @@ -0,0 +1,78 @@ +/* global jest, describe, test, expect, beforeAll, afterAll, beforeEach, afterEach */ +import { noteWIthOpenAndCancelledTasks, noteWIthDoneAndScheduledTasks, noteWithOpenAndDoneTasks, noteWithOneTaskOfEachType } from '@mocks/factories/noteFactory' + +import { getAllTasksFromCurrentNote } from '../tasks.js' +import * as sorting from '@helpers/sorting' +import { CustomConsole } from '@jest/console' // see note below +import { Calendar, Clipboard, CommandBar, DataStore, Editor, NotePlan, simpleFormatter, Paragraph /* Note, mockWasCalledWithString, Paragraph */ } from '@mocks/index' + +beforeAll(() => { + global.Calendar = Calendar + global.Clipboard = Clipboard + global.CommandBar = CommandBar + global.DataStore = DataStore + global.Editor = Editor + global.NotePlan = NotePlan + global.console = new CustomConsole(process.stdout, process.stderr, simpleFormatter) // minimize log footprint + DataStore.settings['_logLevel'] = 'none' //change this to DEBUG to get more logging (or 'none' for none) +}) + +describe('mlevison.GenAITaskChecker', () => { + describe('test getTasksByType', () => { + test('prove Open and Cancelled Tasks found in sample note', () => { + const foundTasks = sorting.getTasksByType(noteWIthOpenAndCancelledTasks.paragraphs) + + expect(foundTasks.open.length).toBe(1) + expect(foundTasks.done.length).toBe(0) + expect(foundTasks.cancelled.length).toBe(1) + expect(foundTasks.scheduled.length).toBe(0) + expect(foundTasks.checklist.length).toBe(0) + expect(foundTasks.checklistDone.length).toBe(0) + expect(foundTasks.checklistCancelled.length).toBe(0) + expect(foundTasks.checklistScheduled.length).toBe(0) + }) + + test('prove Done Tasks found in sample note', () => { + const foundTasks = sorting.getTasksByType(noteWIthDoneAndScheduledTasks.paragraphs) + + expect(foundTasks.open.length).toBe(0) + expect(foundTasks.done.length).toBe(1) + expect(foundTasks.cancelled.length).toBe(0) + expect(foundTasks.scheduled.length).toBe(1) + expect(foundTasks.checklist.length).toBe(0) + expect(foundTasks.checklistDone.length).toBe(0) + expect(foundTasks.checklistCancelled.length).toBe(0) + expect(foundTasks.checklistScheduled.length).toBe(0) + }) + }) + + describe('getAllTasksFromCurrentNote', () => { + test('returns open and done tasks from current note', () => { + const tasks = getAllTasksFromCurrentNote(noteWithOpenAndDoneTasks) + expect(tasks.length).toBe(3) + expect(tasks[0].content).toBe('Open task 1') + expect(tasks[1].content).toBe('Open task 2') + expect(tasks[2].content).toBe('Done task') + }) + + test('returns open and cancelled tasks from current note', () => { + const tasks = getAllTasksFromCurrentNote(noteWIthOpenAndCancelledTasks) + expect(tasks.length).toBe(2) + expect(tasks[0].content).toBe('task 1') + expect(tasks[1].content).toBe('task cancelled') + }) + + test('returns one of each task types from current note', () => { + const tasks = getAllTasksFromCurrentNote(noteWithOneTaskOfEachType) + expect(tasks.length).toBe(8) + expect(tasks[0].content).toBe('Open task 1') + expect(tasks[1].content).toBe('Scheduled task 2') + expect(tasks[2].content).toBe('Done task') + expect(tasks[3].content).toBe('Cancelled task') + expect(tasks[4].content).toBe('checklist') + expect(tasks[5].content).toBe('checklistScheduled') + expect(tasks[6].content).toBe('checklistDone') + expect(tasks[7].content).toBe('checklistCancelled') + }) + }) +}) diff --git a/helpers/tasks.js b/helpers/tasks.js new file mode 100644 index 000000000..651661742 --- /dev/null +++ b/helpers/tasks.js @@ -0,0 +1,10 @@ +import { getTasksByType } from './sorting' + +export function getAllTasksFromCurrentNote(currentNote) { + // This function will extract all tasks from the current note and return them grouped by type + // It uses the getTasksByType function to categorize the tasks + const groupedTasks = getTasksByType(currentNote.paragraphs) + + return groupedTasks.open.concat(groupedTasks.scheduled, groupedTasks.done, groupedTasks.cancelled, groupedTasks.checklist, groupedTasks.checklistScheduled, + groupedTasks.checklistDone, groupedTasks.checklistCancelled) +} \ No newline at end of file