import AutoStatusPlugin from '../src/main'; import { App, TFile } from './mock_obsidian'; describe('Plugin Lifecycle', () => { let plugin: AutoStatusPlugin; let mockApp: App; beforeEach(() => { mockApp = new App(); plugin = new AutoStatusPlugin(mockApp as any, {} as any); plugin.settings = { propertyName: 'Pending tasks', enableAutoProcessing: true, excludeFolders: [], includeFolders: [], debounceDelay: 500, enableTaskCounts: false, pendingTasksCountProperty: 'Pending tasks count', totalTasksCountProperty: 'Total tasks count', countRootTasksOnly: false, }; }); describe('onload', () => { test('should initialize plugin correctly', async () => { plugin.loadSettings = jest.fn().mockResolvedValue(undefined); plugin.addCommand = jest.fn(); plugin.addSettingTab = jest.fn(); plugin.registerEvent = jest.fn(); await plugin.onload(); expect(plugin.loadSettings).toHaveBeenCalled(); expect(plugin.addCommand).toHaveBeenCalledTimes(2); expect(plugin.addSettingTab).toHaveBeenCalled(); expect(plugin.registerEvent).toHaveBeenCalledTimes(2); }); test('should register vault modify event', async () => { plugin.loadSettings = jest.fn().mockResolvedValue(undefined); plugin.addCommand = jest.fn(); plugin.addSettingTab = jest.fn(); plugin.registerEvent = jest.fn(); await plugin.onload(); expect(mockApp.vault.on).toHaveBeenCalledWith('modify', expect.any(Function)); expect(plugin.registerEvent).toHaveBeenCalledWith(mockApp.vault.on('modify', expect.any(Function))); }); test('should register workspace editor-change event', async () => { plugin.loadSettings = jest.fn().mockResolvedValue(undefined); plugin.addCommand = jest.fn(); plugin.addSettingTab = jest.fn(); plugin.registerEvent = jest.fn(); await plugin.onload(); expect(mockApp.workspace.on).toHaveBeenCalledWith('editor-change', expect.any(Function)); expect(plugin.registerEvent).toHaveBeenCalledWith(mockApp.workspace.on('editor-change', expect.any(Function))); }); test('should add process current note command', async () => { plugin.loadSettings = jest.fn().mockResolvedValue(undefined); plugin.addCommand = jest.fn(); plugin.addSettingTab = jest.fn(); plugin.registerEvent = jest.fn(); await plugin.onload(); expect(plugin.addCommand).toHaveBeenCalledWith( expect.objectContaining({ id: 'process-current-note', name: 'Process current note for task status', callback: expect.any(Function), }), ); }); test('should add process all notes command', async () => { plugin.loadSettings = jest.fn().mockResolvedValue(undefined); plugin.addCommand = jest.fn(); plugin.addSettingTab = jest.fn(); plugin.registerEvent = jest.fn(); await plugin.onload(); expect(plugin.addCommand).toHaveBeenCalledWith( expect.objectContaining({ id: 'process-all-notes', name: 'Process all notes for task status', callback: expect.any(Function), }), ); }); }); describe('commands', () => { test('process current note command should work', async () => { const file = new TFile('test.md'); mockApp.workspace.getActiveFile = jest.fn().mockReturnValue(file); plugin.processFile = jest.fn().mockResolvedValue(undefined); plugin.loadSettings = jest.fn().mockResolvedValue(undefined); plugin.addCommand = jest.fn(); plugin.addSettingTab = jest.fn(); plugin.registerEvent = jest.fn(); await plugin.onload(); // Get the callback from the addCommand call const addCommandCalls = (plugin.addCommand as jest.Mock).mock.calls; const processCurrentNoteCall = addCommandCalls.find(call => call[0].id === 'process-current-note'); expect(processCurrentNoteCall).toBeDefined(); // Execute the callback await processCurrentNoteCall[0].callback(); expect(mockApp.workspace.getActiveFile).toHaveBeenCalled(); expect(plugin.processFile).toHaveBeenCalledWith(file); }); test('process current note command should handle no active file', async () => { mockApp.workspace.getActiveFile = jest.fn().mockReturnValue(null); plugin.processFile = jest.fn().mockResolvedValue(undefined); plugin.loadSettings = jest.fn().mockResolvedValue(undefined); plugin.addCommand = jest.fn(); plugin.addSettingTab = jest.fn(); plugin.registerEvent = jest.fn(); await plugin.onload(); // Get the callback from the addCommand call const addCommandCalls = (plugin.addCommand as jest.Mock).mock.calls; const processCurrentNoteCall = addCommandCalls.find(call => call[0].id === 'process-current-note'); expect(processCurrentNoteCall).toBeDefined(); // Execute the callback await processCurrentNoteCall[0].callback(); expect(mockApp.workspace.getActiveFile).toHaveBeenCalled(); expect(plugin.processFile).not.toHaveBeenCalled(); }); test('process all notes command should work', async () => { plugin.processAllNotes = jest.fn().mockResolvedValue(undefined); plugin.loadSettings = jest.fn().mockResolvedValue(undefined); plugin.addCommand = jest.fn(); plugin.addSettingTab = jest.fn(); plugin.registerEvent = jest.fn(); await plugin.onload(); // Get the callback from the addCommand call const addCommandCalls = (plugin.addCommand as jest.Mock).mock.calls; const processAllNotesCall = addCommandCalls.find(call => call[0].id === 'process-all-notes'); expect(processAllNotesCall).toBeDefined(); // Execute the callback await processAllNotesCall[0].callback(); expect(plugin.processAllNotes).toHaveBeenCalled(); }); }); });