diff --git a/package.json b/package.json index fea8f60..869f29d 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "docs:build": "vitepress build docs", "docs:preview": "vitepress preview docs --host 0.0.0.0 --port 4173 --strictPort", "test:config": "pnpm run build && node --test dist/config/config.test.js", - "test:core": "pnpm run build && node --test dist/cli/args.test.js dist/cli/help.test.js dist/core/services/cli-command-service.test.js dist/core/services/numeric-shortcut-session-service.test.js dist/core/services/secondary-subtitle-service.test.js dist/core/services/mpv-render-metrics-service.test.js dist/core/services/mpv-runtime-service.test.js dist/core/services/runtime-options-runtime-service.test.js dist/core/services/overlay-modal-restore-service.test.js dist/core/services/runtime-config-service.test.js dist/core/services/overlay-bridge-runtime-service.test.js dist/core/services/overlay-visibility-facade-service.test.js dist/core/services/overlay-broadcast-runtime-service.test.js dist/core/services/app-ready-runtime-service.test.js dist/core/services/app-shutdown-runtime-service.test.js dist/core/services/mpv-client-deps-runtime-service.test.js dist/core/services/app-lifecycle-deps-runtime-service.test.js", + "test:core": "pnpm run build && node --test dist/cli/args.test.js dist/cli/help.test.js dist/core/services/cli-command-service.test.js dist/core/services/numeric-shortcut-session-service.test.js dist/core/services/secondary-subtitle-service.test.js dist/core/services/mpv-render-metrics-service.test.js dist/core/services/mpv-runtime-service.test.js dist/core/services/runtime-options-runtime-service.test.js dist/core/services/overlay-modal-restore-service.test.js dist/core/services/runtime-config-service.test.js dist/core/services/overlay-bridge-runtime-service.test.js dist/core/services/overlay-visibility-facade-service.test.js dist/core/services/overlay-broadcast-runtime-service.test.js dist/core/services/app-ready-runtime-service.test.js dist/core/services/app-shutdown-runtime-service.test.js dist/core/services/mpv-client-deps-runtime-service.test.js dist/core/services/app-lifecycle-deps-runtime-service.test.js dist/core/services/runtime-options-manager-runtime-service.test.js", "test:subtitle": "pnpm run build && node --test dist/subtitle/stages.test.js dist/subtitle/pipeline.test.js", "generate:config-example": "pnpm run build && node dist/generate-config-example.js", "start": "pnpm run build && electron . --start", diff --git a/src/core/services/runtime-options-manager-runtime-service.test.ts b/src/core/services/runtime-options-manager-runtime-service.test.ts new file mode 100644 index 0000000..a49bd17 --- /dev/null +++ b/src/core/services/runtime-options-manager-runtime-service.test.ts @@ -0,0 +1,25 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { createRuntimeOptionsManagerRuntimeService } from "./runtime-options-manager-runtime-service"; + +test("createRuntimeOptionsManagerRuntimeService wires patch + options changed callbacks", () => { + const patches: unknown[] = []; + const changedSnapshots: unknown[] = []; + const manager = createRuntimeOptionsManagerRuntimeService({ + getAnkiConfig: () => ({ + behavior: { autoUpdateNewCards: true }, + isKiku: { fieldGrouping: "manual" }, + }), + applyAnkiPatch: (patch) => { + patches.push(patch); + }, + onOptionsChanged: (options) => { + changedSnapshots.push(options); + }, + }); + + const result = manager.setOptionValue("anki.autoUpdateNewCards", false); + assert.equal(result.ok, true); + assert.equal(patches.length > 0, true); + assert.equal(changedSnapshots.length > 0, true); +}); diff --git a/src/core/services/runtime-options-manager-runtime-service.ts b/src/core/services/runtime-options-manager-runtime-service.ts new file mode 100644 index 0000000..0f550f4 --- /dev/null +++ b/src/core/services/runtime-options-manager-runtime-service.ts @@ -0,0 +1,17 @@ +import { RuntimeOptionsManager } from "../../runtime-options"; +import { AnkiConnectConfig, RuntimeOptionState } from "../../types"; + +export interface RuntimeOptionsManagerRuntimeDeps { + getAnkiConfig: () => AnkiConnectConfig; + applyAnkiPatch: (patch: Partial) => void; + onOptionsChanged: (options: RuntimeOptionState[]) => void; +} + +export function createRuntimeOptionsManagerRuntimeService( + deps: RuntimeOptionsManagerRuntimeDeps, +): RuntimeOptionsManager { + return new RuntimeOptionsManager(deps.getAnkiConfig, { + applyAnkiPatch: deps.applyAnkiPatch, + onOptionsChanged: deps.onOptionsChanged, + }); +} diff --git a/src/main.ts b/src/main.ts index 6e71c2c..048e123 100644 --- a/src/main.ts +++ b/src/main.ts @@ -205,6 +205,7 @@ import { runAppReadyRuntimeService } from "./core/services/app-ready-runtime-ser import { runAppShutdownRuntimeService } from "./core/services/app-shutdown-runtime-service"; import { createMpvIpcClientDepsRuntimeService } from "./core/services/mpv-client-deps-runtime-service"; import { createAppLifecycleDepsRuntimeService } from "./core/services/app-lifecycle-deps-runtime-service"; +import { createRuntimeOptionsManagerRuntimeService } from "./core/services/runtime-options-manager-runtime-service"; import { runSubsyncManualFromIpcRuntimeService, triggerSubsyncFromConfigRuntimeService, @@ -537,20 +538,18 @@ if (initialArgs.generateConfig && !shouldStartApp(initialArgs)) { ); }, initRuntimeOptionsManager: () => { - runtimeOptionsManager = new RuntimeOptionsManager( - () => configService.getConfig().ankiConnect, - { - applyAnkiPatch: (patch) => { - if (ankiIntegration) { - ankiIntegration.applyRuntimeConfigPatch(patch); - } - }, - onOptionsChanged: () => { - broadcastRuntimeOptionsChanged(); - refreshOverlayShortcuts(); - }, + runtimeOptionsManager = createRuntimeOptionsManagerRuntimeService({ + getAnkiConfig: () => configService.getConfig().ankiConnect, + applyAnkiPatch: (patch) => { + if (ankiIntegration) { + ankiIntegration.applyRuntimeConfigPatch(patch); + } }, - ); + onOptionsChanged: () => { + broadcastRuntimeOptionsChanged(); + refreshOverlayShortcuts(); + }, + }); }, setSecondarySubMode: (mode) => { secondarySubMode = mode;