This repository has been archived on 2026-05-02. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
obsidian-plugin-auto-task-s.../test/plugin-lifecycle.test.ts
Felipe M. d113c1c027
All checks were successful
CI / build (push) Successful in 16s
Add optional task count properties with structural root-task detection
Introduces opt-in pending/total task count frontmatter fields and a
"count root tasks only" mode. Root tasks are detected structurally by
walking the list tree, so tasks nested under non-task bullets (e.g.
"- **Action items**:") are correctly classified as root.

Also updates the manifest author metadata.
2026-04-14 09:50:45 +02:00

170 lines
5.8 KiB
TypeScript

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();
});
});
});