refactor(main): fold remaining main-deps builders into their handlers (#308)

* refactor(main): drop remaining pass-through main-deps builders

Jellyfin client-info/setup-window/remote, overlay shortcuts/visibility/
layout/window-factory, config hot-reload, and startup bootstrap factory
builders only forwarded deps. Callers now pass deps to the handlers
directly; createWatchConfigPathHandler moves into config-hot-reload-handlers.

* refactor(main): fold small-logic main-deps builders into their handlers

Tray click, mpv client factory, startup bootstrap exit callbacks, overlay
runtime options, and dictionary roots/runtime builders move their logic
into the owning handler or call site. The two duplicated dictionary-root
builders become one createBuildDictionaryRootsHandler(vendorDirName).

* refactor(main): fold cli command context builders into the factory

createCliCommandContextFactory now adapts appState and main services to
the CLI command context deps directly (socket-path change notification,
texthooker port/websocket URL, browser-open default). The pass-through
cli-command-context-main-deps and cli-command-context-deps layers are
removed; their behavioural tests now run against the factory's context.

* refactor(main): fold subtitle tokenization builder into the mpv composer

The tokenizer deps builder's logic (known-word lookup recording and the
character-dictionary gate on name matching) becomes
buildTokenizerRuntimeOptions in mpv-runtime-composer, tested through the
composer. The MeCab check and dictionary prewarm handlers move to
subtitle-tokenization-warmup.ts, and the unused BuiltMainDeps type is
removed from composer contracts.

* refactor(main): drop redundant tokenizer deps keys and cast

* refactor(main): end the main-deps builder pattern

mpv-main-event-main-deps becomes mpv-main-event-binding-deps
(createMpvMainEventBindingDeps), returning the binding deps directly
instead of a thunk; it owns the subtitle-recording state. The will-quit
cleanup adapter likewise returns its deps directly, and the pass-through
app-lifecycle activate builders are removed.

* docs: drop references to main-deps builders
This commit is contained in:
sudacode authored and GitHub committed 2026-10-31 14:07:07 -07:00
1 parent 2a0e931325
commit 90fc7cf940
63 files changed
+1282 -2480

No files matched your search

@@ -0,0 +1,175 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import {
createMecabTokenizerAndCheckHandler,
createPrewarmSubtitleDictionariesHandler,
} from './subtitle-tokenization-warmup';
function createDeferred(): {
promise: Promise<void>;
resolve: () => void;
} {
let resolve!: () => void;
const promise = new Promise<void>((nextResolve) => {
resolve = nextResolve;
});
return { promise, resolve };
}
test('mecab tokenizer check creates tokenizer once and runs availability check', async () => {
const calls: string[] = [];
type Tokenizer = { id: string };
let tokenizer: Tokenizer | null = null;
const run = createMecabTokenizerAndCheckHandler<Tokenizer>({
getMecabTokenizer: () => tokenizer,
setMecabTokenizer: (next) => {
tokenizer = next;
calls.push('set');
},
createMecabTokenizer: () => {
calls.push('create');
return { id: 'mecab' };
},
checkAvailability: async () => {
calls.push('check');
},
});
await run();
await run();
assert.deepEqual(calls, ['create', 'set', 'check', 'check']);
});
test('dictionary prewarm runs both dictionary loaders', async () => {
const calls: string[] = [];
const prewarm = createPrewarmSubtitleDictionariesHandler({
ensureJlptDictionaryLookup: async () => {
calls.push('jlpt');
},
ensureFrequencyDictionaryLookup: async () => {
calls.push('freq');
},
});
await prewarm();
assert.deepEqual(calls.sort(), ['freq', 'jlpt']);
});
test('dictionary prewarm shows OSD spinner while loading and completion when loaded', async () => {
const osdMessages: string[] = [];
const clearedTimers: unknown[] = [];
let tick: (() => void) | null = null;
const jlptDeferred = createDeferred();
const freqDeferred = createDeferred();
const prewarm = createPrewarmSubtitleDictionariesHandler({
ensureJlptDictionaryLookup: async () => jlptDeferred.promise,
ensureFrequencyDictionaryLookup: async () => freqDeferred.promise,
shouldShowOsdNotification: () => true,
showMpvOsd: (message) => {
osdMessages.push(message);
},
setInterval: (callback) => {
tick = callback;
return 1;
},
clearInterval: (timer) => {
clearedTimers.push(timer);
},
});
const prewarmPromise = prewarm({ showLoadingOsd: true });
assert.deepEqual(osdMessages, ['Loading subtitle annotations |']);
if (!tick) {
throw new Error('expected loading spinner tick callback');
}
const tickCallback: () => void = tick;
tickCallback();
assert.deepEqual(osdMessages, [
'Loading subtitle annotations |',
'Loading subtitle annotations /',
]);
jlptDeferred.resolve();
freqDeferred.resolve();
await prewarmPromise;
assert.deepEqual(osdMessages, [
'Loading subtitle annotations |',
'Loading subtitle annotations /',
'Subtitle annotations loaded',
]);
assert.deepEqual(clearedTimers, [1]);
});
test('dictionary prewarm can show OSD while awaiting background-started load', async () => {
const osdMessages: string[] = [];
const jlptDeferred = createDeferred();
const freqDeferred = createDeferred();
const prewarm = createPrewarmSubtitleDictionariesHandler({
ensureJlptDictionaryLookup: async () => jlptDeferred.promise,
ensureFrequencyDictionaryLookup: async () => freqDeferred.promise,
shouldShowOsdNotification: () => true,
showMpvOsd: (message) => {
osdMessages.push(message);
},
setInterval: () => 1,
clearInterval: () => undefined,
});
const backgroundWarmupPromise = prewarm();
const tokenizationWarmupPromise = prewarm({ showLoadingOsd: true });
assert.deepEqual(osdMessages, ['Loading subtitle annotations |']);
jlptDeferred.resolve();
freqDeferred.resolve();
await backgroundWarmupPromise;
await tokenizationWarmupPromise;
assert.deepEqual(osdMessages, ['Loading subtitle annotations |', 'Subtitle annotations loaded']);
});
test('dictionary prewarm shows OSD when loading indicator is requested even if notification predicate is disabled', async () => {
const osdMessages: string[] = [];
const prewarm = createPrewarmSubtitleDictionariesHandler({
ensureJlptDictionaryLookup: async () => undefined,
ensureFrequencyDictionaryLookup: async () => undefined,
shouldShowOsdNotification: () => false,
showMpvOsd: (message) => {
osdMessages.push(message);
},
});
await prewarm({ showLoadingOsd: true });
assert.deepEqual(osdMessages, ['Loading subtitle annotations |', 'Subtitle annotations loaded']);
});
test('dictionary prewarm clears loading OSD timer even if notifications are disabled before completion', async () => {
const clearedTimers: unknown[] = [];
const jlptDeferred = createDeferred();
const freqDeferred = createDeferred();
let shouldShowNotification = true;
const prewarm = createPrewarmSubtitleDictionariesHandler({
ensureJlptDictionaryLookup: async () => jlptDeferred.promise,
ensureFrequencyDictionaryLookup: async () => freqDeferred.promise,
shouldShowOsdNotification: () => shouldShowNotification,
showMpvOsd: () => undefined,
setInterval: () => 'loading-timer',
clearInterval: (timer) => {
clearedTimers.push(timer);
},
});
const prewarmPromise = prewarm({ showLoadingOsd: true });
shouldShowNotification = false;
jlptDeferred.resolve();
freqDeferred.resolve();
await prewarmPromise;
assert.deepEqual(clearedTimers, ['loading-timer']);
});