import { describe, it, expect, vi } from 'vitest'; import { Plugin } from 'obsidian'; import { BaseModule } from '../../src/modules/base-module'; // TestModule: minimal concrete subclass to exercise BaseModule behavior. // Exposes a `cleanupCount` accessor so tests can verify private state. class TestModule extends BaseModule { id = 'test-module'; name = 'Test Module'; description = 'Used in BaseModule unit tests'; // Test-only inspector: exposes the size of the private cleanupFns array. get cleanupCount(): number { return (this as unknown as { cleanupFns: (() => void)[] }).cleanupFns.length; } // Surface protected registerCleanup for direct test access. public callRegisterCleanup(fn: () => void): void { this.registerCleanup(fn); } onload(_plugin: Plugin, _moduleSettings: Record): void { // no-op: BaseModule's onload contract is exercised elsewhere. } } function makePlugin(): Plugin { // Hand-rolled minimal mock to avoid pulling the real Obsidian runtime. return {} as Plugin; } describe('BaseModule', () => { it('registerCleanup добавляет функцию в очередь (cleanupFns)', () => { const module = new TestModule(); const cleanup = vi.fn(); module.callRegisterCleanup(cleanup); expect(module.cleanupCount).toBe(1); expect(cleanup).not.toHaveBeenCalled(); }); it('onunload вызывает все зарегистрированные cleanup-функции', () => { const module = new TestModule(); const cleanupA = vi.fn(); const cleanupB = vi.fn(); module.callRegisterCleanup(cleanupA); module.callRegisterCleanup(cleanupB); module.onunload(makePlugin()); expect(cleanupA).toHaveBeenCalledTimes(1); expect(cleanupB).toHaveBeenCalledTimes(1); }); it('onunload очищает очередь после выполнения (cleanupFns === [])', () => { const module = new TestModule(); module.callRegisterCleanup(vi.fn()); module.callRegisterCleanup(vi.fn()); module.callRegisterCleanup(vi.fn()); expect(module.cleanupCount).toBe(3); module.onunload(makePlugin()); expect(module.cleanupCount).toBe(0); }); it('defaultSettings возвращает пустой объект {}', () => { const module = new TestModule(); expect(module.defaultSettings).toEqual({}); // Belt-and-suspenders: identity check for a fresh object each call. expect(module.defaultSettings).not.toBe(module.defaultSettings); }); it('несколько cleanup-функций: registerCleanup можно вызвать несколько раз и все выполнятся при onunload', () => { const module = new TestModule(); const fns = [vi.fn(), vi.fn(), vi.fn(), vi.fn()]; for (const fn of fns) { module.callRegisterCleanup(fn); } expect(module.cleanupCount).toBe(fns.length); module.onunload(makePlugin()); for (const fn of fns) { expect(fn).toHaveBeenCalledTimes(1); } expect(module.cleanupCount).toBe(0); }); });