diff --git a/__mocks__/obsidian.ts b/__mocks__/obsidian.ts index 0312623..3be20e7 100644 --- a/__mocks__/obsidian.ts +++ b/__mocks__/obsidian.ts @@ -3,6 +3,23 @@ export class Plugin { // Intentionally minimal — tests supply their own `{}` when calling onunload. + // loadData/saveData/addSettingTab are the real base class's job; a test + // that drives the plugin lifecycle assigns them itself. +} + +export class App { + // Marker type only: the modules read `plugin.app`, no test drives it. +} + +export class PluginSettingTab { + app: App; + plugin: Plugin; + containerEl: unknown = {}; + + constructor(app: App, plugin: Plugin) { + this.app = app; + this.plugin = plugin; + } } export class TFile { diff --git a/src/main.ts b/src/main.ts index 7bfd790..094ba33 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,7 @@ import { Plugin } from 'obsidian'; import { ZeroQSettings, QoLModule } from './types'; import { MODULES } from './modules'; -import { DEFAULT_SETTINGS, ZeroQSettingTab, getModuleSettings } from './settings'; +import { ZeroQSettingTab, getModuleSettings } from './settings'; export default class ZeroQoLModulesPlugin extends Plugin { settings: ZeroQSettings; @@ -27,15 +27,20 @@ export default class ZeroQoLModulesPlugin extends Plugin { } async loadSettings() { - this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()) as ZeroQSettings; + // Слияние по модулям, а не `Object.assign({}, DEFAULT_SETTINGS, saved)`: + // у shallow-копии `modules` — та же ссылка, что у DEFAULT_SETTINGS, поэтому + // заполнение пропущенных модулей правила сам модульный дефолт (следующий + // экземпляр плагина получал чужие настройки), а сохранённая запись без + // поля `settings` обнуляла дефолты модуля (ревью 2026-11-29, §7). + const saved = (await this.loadData()) as Partial | null; + this.settings = { modules: {} }; for (const module of MODULES) { - if (!this.settings.modules[module.id]) { - this.settings.modules[module.id] = { - enabled: true, - settings: { ...module.defaultSettings }, - }; - } + const savedCfg = saved?.modules?.[module.id]; + this.settings.modules[module.id] = { + enabled: savedCfg?.enabled ?? true, + settings: { ...module.defaultSettings, ...(savedCfg?.settings ?? {}) }, + }; } } diff --git a/tests/main.test.ts b/tests/main.test.ts new file mode 100644 index 0000000..3df5fd7 --- /dev/null +++ b/tests/main.test.ts @@ -0,0 +1,172 @@ +import { describe, it, expect, vi, afterEach } from 'vitest'; +import ZeroQoLModulesPlugin from '../src/main'; +import { MODULES } from '../src/modules'; +import { DEFAULT_SETTINGS } from '../src/settings'; + +// `src/main.ts` is the one file in src/ with no tests (ревью 2026-11-29, §7): +// it owns the onload/onunload order and the merge of DEFAULT_SETTINGS with +// saved data — the place where a mistake shows up as «модуль включён, но не +// работает» or «настройки слетели». These tests pin that contract. + +// The Obsidian `Plugin` mock in __mocks__/obsidian.ts has no loadData / +// saveData / addSettingTab: the real base class provides them, the mock does +// not. So each test hands the plugin the three methods it uses. +type Harness = ZeroQoLModulesPlugin & { + loadData: () => Promise; + saveData: (data: unknown) => Promise; + addSettingTab: (tab: unknown) => void; +}; + +function makeApp() { + // Modules subscribe in onload: workspace for editor events, vault for rename. + return { + workspace: { on: vi.fn(() => ({})), offref: vi.fn() }, + vault: { on: vi.fn(() => ({})), offref: vi.fn() }, + }; +} + +function makePlugin(saved: unknown = null): Harness { + // The real Plugin base class takes (app, manifest); the test mock is a bare + // class, and the plugin's own constructor is implicit. So construct it as a + // zero-arg class and hand it the two methods it actually calls. + const Ctor = ZeroQoLModulesPlugin as unknown as new () => ZeroQoLModulesPlugin; + const plugin = new Ctor() as Harness; + (plugin as unknown as { app: unknown }).app = makeApp(); + plugin.loadData = vi.fn(async () => saved); + plugin.saveData = vi.fn(async () => undefined); + plugin.addSettingTab = vi.fn(); + return plugin; +} + +function moduleCfg(plugin: ZeroQoLModulesPlugin, id: string) { + return plugin.settings.modules[id]; +} + +afterEach(() => { + vi.restoreAllMocks(); +}); + +describe('ZeroQoLModulesPlugin.loadSettings', () => { + it('fills every registered module with defaults when nothing is saved', async () => { + const plugin = makePlugin(null); + await plugin.loadSettings(); + + expect(Object.keys(plugin.settings.modules).sort()).toEqual( + MODULES.map((m) => m.id).sort(), + ); + for (const module of MODULES) { + const cfg = moduleCfg(plugin, module.id); + expect(cfg.enabled).toBe(true); + expect(cfg.settings).toEqual(module.defaultSettings); + } + }); + + it('keeps saved values and adds defaults for modules the saved data omits', async () => { + const [first, second] = MODULES; + const plugin = makePlugin({ + modules: { + [first.id]: { enabled: false, settings: { custom: 'kept' } }, + }, + }); + await plugin.loadSettings(); + + expect(moduleCfg(plugin, first.id)).toEqual({ + enabled: false, + // saved values sit on top of the module's defaults + settings: { ...first.defaultSettings, custom: 'kept' }, + }); + expect(moduleCfg(plugin, second.id)).toEqual({ + enabled: true, + settings: second.defaultSettings, + }); + }); + + it('restores default keys when saved data has a module without settings', async () => { + const [first] = MODULES; + const plugin = makePlugin({ modules: { [first.id]: { enabled: false } } }); + await plugin.loadSettings(); + + expect(moduleCfg(plugin, first.id).enabled).toBe(false); + expect(moduleCfg(plugin, first.id).settings).toEqual(first.defaultSettings); + }); + + it('does not mutate DEFAULT_SETTINGS, so a second plugin instance sees clean defaults', async () => { + const [first] = MODULES; + const dirty = makePlugin({ + modules: { [first.id]: { enabled: false, settings: { custom: 'leak' } } }, + }); + await dirty.loadSettings(); + + const clean = makePlugin(null); + await clean.loadSettings(); + + expect(moduleCfg(clean, first.id).enabled).toBe(true); + expect(moduleCfg(clean, first.id).settings).toEqual(first.defaultSettings); + expect(DEFAULT_SETTINGS.modules[first.id]).toEqual({ + enabled: true, + settings: first.defaultSettings, + }); + }); +}); + +describe('ZeroQoLModulesPlugin lifecycle', () => { + it('loads only the modules enabled in settings and registers the settings tab', async () => { + const [on, off] = MODULES; + const onloadOn = vi.spyOn(on, 'onload'); + const onloadOff = vi.spyOn(off, 'onload'); + const plugin = makePlugin({ + modules: { + [on.id]: { enabled: true, settings: on.defaultSettings }, + [off.id]: { enabled: false, settings: off.defaultSettings }, + }, + }); + + await plugin.onload(); + + expect(onloadOn).toHaveBeenCalledTimes(1); + expect(onloadOn).toHaveBeenCalledWith(plugin, on.defaultSettings); + expect(onloadOff).not.toHaveBeenCalled(); + expect(plugin.addSettingTab).toHaveBeenCalledTimes(1); + }); + + it('loadModule is idempotent and unloadModule ignores a module that was never loaded', async () => { + const [module] = MODULES; + const onload = vi.spyOn(module, 'onload'); + const onunload = vi.spyOn(module, 'onunload'); + const plugin = makePlugin(null); + await plugin.loadSettings(); + + plugin.loadModule(module); + plugin.loadModule(module); + expect(onload).toHaveBeenCalledTimes(1); + + plugin.unloadModule(MODULES[1]); + expect(onunload).not.toHaveBeenCalled(); + + plugin.unloadModule(module); + plugin.unloadModule(module); + expect(onunload).toHaveBeenCalledTimes(1); + }); + + it('onunload unloads every loaded module once, even if called twice', async () => { + const spies = MODULES.map((m) => vi.spyOn(m, 'onunload')); + const plugin = makePlugin(null); + await plugin.loadSettings(); + for (const m of MODULES) plugin.loadModule(m); + + plugin.onunload(); + plugin.onunload(); + + for (const spy of spies) expect(spy).toHaveBeenCalledTimes(1); + }); + + it('saveSettings writes the settings object it holds', async () => { + const plugin = makePlugin(null); + await plugin.loadSettings(); + plugin.settings.modules[MODULES[0].id].enabled = false; + + await plugin.saveSettings(); + + expect(plugin.saveData).toHaveBeenCalledWith(plugin.settings); + }); +});