From 8578d6e74700ce10165038bfa4dbebe5ed4d8e96 Mon Sep 17 00:00:00 2001 From: Diogo Ribeiro Date: Tue, 3 Jun 2025 22:52:27 +0100 Subject: [PATCH] Add tests for limitTodos and todoKey --- tests/todoUtils.test.ts | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tests/todoUtils.test.ts diff --git a/tests/todoUtils.test.ts b/tests/todoUtils.test.ts new file mode 100644 index 0000000..1df95c8 --- /dev/null +++ b/tests/todoUtils.test.ts @@ -0,0 +1,45 @@ +import { describe, it, expect } from 'vitest'; +import { limitTodos, todoKey } from '../src/core/todoUtils'; +import { TodoItem } from '../src/parser/types'; + + +describe('limitTodos', () => { + it('filters out invalid todos and limits the result', () => { + const todos: TodoItem[] = [ + { tag: 'TODO', text: 'short', file: 'a.ts', line: 1 }, + { tag: 'FIXME', text: 'Fix the login', file: 'b.ts', line: 2 }, + { tag: 'TODO', text: 'Refactor component', file: 'c.ts', line: 3 }, + { tag: 'BUG', text: 'Another fix', file: 'd.ts', line: 4 }, + ]; + + const limited = limitTodos(todos, 2); + + expect(limited.length).toBe(2); + expect(limited[0].text).toBe('Fix the login'); + expect(limited[1].text).toBe('Refactor component'); + }); +}); + +describe('todoKey', () => { + it('can be used to remove duplicate todos before limiting', () => { + const todos: TodoItem[] = [ + { tag: 'TODO', text: 'duplicate task', file: 'a.ts', line: 1 }, + { tag: 'TODO', text: 'duplicate task', file: 'b.ts', line: 2 }, + { tag: 'TODO', text: 'unique task', file: 'c.ts', line: 3 }, + ]; + + const seen = new Set(); + const unique = todos.filter(t => { + const key = todoKey(t); + if (seen.has(key)) return false; + seen.add(key); + return true; + }); + + const result = limitTodos(unique, 5); + + expect(result.length).toBe(2); + expect(result.map(t => t.text)).toEqual(['duplicate task', 'unique task']); + }); +}); +