mirror of
https://github.com/callumalpass/tasknotes.git
synced 2026-07-22 12:50:26 +00:00
150 lines
5.2 KiB
TypeScript
150 lines
5.2 KiB
TypeScript
/**
|
|
* Unit tests for trigger detection logic
|
|
* Tests the pure function extracted from TaskCreationModal
|
|
*/
|
|
|
|
// Import the function by requiring the module and accessing the function
|
|
// Since it's not exported, we'll test it through the class that uses it
|
|
import { TaskCreationModal } from '../../../src/modals/TaskCreationModal';
|
|
|
|
// Mock the detectSuggestionTrigger function by testing through getSuggestions
|
|
describe('Trigger Detection Logic', () => {
|
|
|
|
it('should detect @ trigger correctly', () => {
|
|
const textBeforeCursor = 'Some text @home';
|
|
|
|
// Test the logic directly by replicating the pure function
|
|
const lastAtIndex = textBeforeCursor.lastIndexOf('@');
|
|
const lastHashIndex = textBeforeCursor.lastIndexOf('#');
|
|
const lastPlusIndex = textBeforeCursor.lastIndexOf('+');
|
|
|
|
let triggerIndex = -1;
|
|
let trigger: '@' | '#' | '+' | null = null;
|
|
|
|
if (lastAtIndex >= lastHashIndex && lastAtIndex >= lastPlusIndex && lastAtIndex !== -1) {
|
|
triggerIndex = lastAtIndex;
|
|
trigger = '@';
|
|
} else if (lastHashIndex >= lastPlusIndex && lastHashIndex !== -1) {
|
|
triggerIndex = lastHashIndex;
|
|
trigger = '#';
|
|
} else if (lastPlusIndex !== -1) {
|
|
triggerIndex = lastPlusIndex;
|
|
trigger = '+';
|
|
}
|
|
|
|
const queryAfterTrigger = triggerIndex !== -1 ? textBeforeCursor.slice(triggerIndex + 1) : '';
|
|
|
|
expect(trigger).toBe('@');
|
|
expect(triggerIndex).toBe(10);
|
|
expect(queryAfterTrigger).toBe('home');
|
|
});
|
|
|
|
it('should detect # trigger correctly', () => {
|
|
const textBeforeCursor = 'Task #urgent';
|
|
|
|
const lastAtIndex = textBeforeCursor.lastIndexOf('@');
|
|
const lastHashIndex = textBeforeCursor.lastIndexOf('#');
|
|
const lastPlusIndex = textBeforeCursor.lastIndexOf('+');
|
|
|
|
let triggerIndex = -1;
|
|
let trigger: '@' | '#' | '+' | null = null;
|
|
|
|
if (lastAtIndex >= lastHashIndex && lastAtIndex >= lastPlusIndex && lastAtIndex !== -1) {
|
|
triggerIndex = lastAtIndex;
|
|
trigger = '@';
|
|
} else if (lastHashIndex >= lastPlusIndex && lastHashIndex !== -1) {
|
|
triggerIndex = lastHashIndex;
|
|
trigger = '#';
|
|
} else if (lastPlusIndex !== -1) {
|
|
triggerIndex = lastPlusIndex;
|
|
trigger = '+';
|
|
}
|
|
|
|
const queryAfterTrigger = triggerIndex !== -1 ? textBeforeCursor.slice(triggerIndex + 1) : '';
|
|
|
|
expect(trigger).toBe('#');
|
|
expect(triggerIndex).toBe(5);
|
|
expect(queryAfterTrigger).toBe('urgent');
|
|
});
|
|
|
|
it('should detect + trigger correctly', () => {
|
|
const textBeforeCursor = 'Project +work';
|
|
|
|
const lastAtIndex = textBeforeCursor.lastIndexOf('@');
|
|
const lastHashIndex = textBeforeCursor.lastIndexOf('#');
|
|
const lastPlusIndex = textBeforeCursor.lastIndexOf('+');
|
|
|
|
let triggerIndex = -1;
|
|
let trigger: '@' | '#' | '+' | null = null;
|
|
|
|
if (lastAtIndex >= lastHashIndex && lastAtIndex >= lastPlusIndex && lastAtIndex !== -1) {
|
|
triggerIndex = lastAtIndex;
|
|
trigger = '@';
|
|
} else if (lastHashIndex >= lastPlusIndex && lastHashIndex !== -1) {
|
|
triggerIndex = lastHashIndex;
|
|
trigger = '#';
|
|
} else if (lastPlusIndex !== -1) {
|
|
triggerIndex = lastPlusIndex;
|
|
trigger = '+';
|
|
}
|
|
|
|
const queryAfterTrigger = triggerIndex !== -1 ? textBeforeCursor.slice(triggerIndex + 1) : '';
|
|
|
|
expect(trigger).toBe('+');
|
|
expect(triggerIndex).toBe(8);
|
|
expect(queryAfterTrigger).toBe('work');
|
|
});
|
|
|
|
it('should return null when no trigger found', () => {
|
|
const textBeforeCursor = 'No triggers here';
|
|
|
|
const lastAtIndex = textBeforeCursor.lastIndexOf('@');
|
|
const lastHashIndex = textBeforeCursor.lastIndexOf('#');
|
|
const lastPlusIndex = textBeforeCursor.lastIndexOf('+');
|
|
|
|
let triggerIndex = -1;
|
|
let trigger: '@' | '#' | '+' | null = null;
|
|
|
|
if (lastAtIndex >= lastHashIndex && lastAtIndex >= lastPlusIndex && lastAtIndex !== -1) {
|
|
triggerIndex = lastAtIndex;
|
|
trigger = '@';
|
|
} else if (lastHashIndex >= lastPlusIndex && lastHashIndex !== -1) {
|
|
triggerIndex = lastHashIndex;
|
|
trigger = '#';
|
|
} else if (lastPlusIndex !== -1) {
|
|
triggerIndex = lastPlusIndex;
|
|
trigger = '+';
|
|
}
|
|
|
|
expect(trigger).toBe(null);
|
|
expect(triggerIndex).toBe(-1);
|
|
});
|
|
|
|
it('should pick most recent trigger when multiple exist', () => {
|
|
const textBeforeCursor = 'Text @home #urgent +proj';
|
|
|
|
const lastAtIndex = textBeforeCursor.lastIndexOf('@');
|
|
const lastHashIndex = textBeforeCursor.lastIndexOf('#');
|
|
const lastPlusIndex = textBeforeCursor.lastIndexOf('+');
|
|
|
|
let triggerIndex = -1;
|
|
let trigger: '@' | '#' | '+' | null = null;
|
|
|
|
if (lastAtIndex >= lastHashIndex && lastAtIndex >= lastPlusIndex && lastAtIndex !== -1) {
|
|
triggerIndex = lastAtIndex;
|
|
trigger = '@';
|
|
} else if (lastHashIndex >= lastPlusIndex && lastHashIndex !== -1) {
|
|
triggerIndex = lastHashIndex;
|
|
trigger = '#';
|
|
} else if (lastPlusIndex !== -1) {
|
|
triggerIndex = lastPlusIndex;
|
|
trigger = '+';
|
|
}
|
|
|
|
const queryAfterTrigger = triggerIndex !== -1 ? textBeforeCursor.slice(triggerIndex + 1) : '';
|
|
|
|
expect(trigger).toBe('+');
|
|
expect(triggerIndex).toBe(19);
|
|
expect(queryAfterTrigger).toBe('proj');
|
|
});
|
|
});
|