refactor(main): move plugins, session actions and subtitle generation into assemblies (#327)

* refactor(main): build overlay geometry and pointer interaction in the overlay assembly

Inject Electron's screen (and the stats-window promotion) into the overlay geometry and
visible-overlay interaction runtimes so they load under bun test, then construct both in
createOverlayAssembly together with the overlay visibility composer and the mpv subtitle
suppression handlers. Replace the three regex tests that pinned the moved Linux input and
shape code with behaviour tests on the runtimes.

* refactor(main): move the autoplay-ready gate into the playback assembly

The autoplay-ready gate, its post-warm subtitle prime, the current-media tokenization gate and
managed local subtitle selection all read playback state, and playback signals through the
gate, so createPlaybackAssembly now builds them. The overlay reaches the gate through a getter.

Replace the last four regex guards with behaviour tests: the gate holds the release until the
visible overlay has measured the line and ignores untokenized lines during warmup (playback),
cached autoplay priming never paints raw first and the sidebar media path is tagged only after
prefetch loads (priming runtime), and dashboard mining overrides the Hachidori Anki server only
when the proxy is on (stats server runtime). main-wiring.test.ts is now empty and removed.

* refactor(main): move the plugin host, panel and windows into a plugins assembly

* refactor(main): move session action dispatch and renderer mpv commands into an assembly

createSessionActionsAssembly owns the session action dispatcher, the secondary-subtitle mode cycle, queueing the clipboard's video, and the mpv commands the overlay renderer sends. The runtime option cycle both paths used is now one function.

* refactor(main): move Whisper subtitle generation into an assembly

* refactor(main): resolve subtitle annotation options in a runtime module

The runtime-option-over-config resolution that the texthooker page and the MeCab startup decision share moves out of main.ts into createAnnotationOptions.

* test(main): cover a non-boolean runtime annotation option value

* docs: describe the plugins, session actions and subtitle generation assemblies

Update ownership for overlay geometry and pointer interaction, the autoplay-ready gate, and annotation options; point the overlay priming entry points at their new modules; the main-wiring regex debt is gone.

* build: export MpvClientEvents so declaration emit can name the plugins assembly's return type
This commit is contained in:
sudacode authored and GitHub committed 2026-11-04 21:54:49 -08:00
1 parent 14ef7e823c
commit f89f78b861
28 files changed
+2005 -1147

No files matched your search

@@ -465,3 +465,121 @@ test('prefetch stays paused until tokenization of an uncached line completes', a
'prefetch:resume',
]);
});
/** Deps whose calls append to `calls`; tests override what they exercise. */
function makeRecordingDeps(
calls: string[],
overrides: Partial<Parameters<typeof createAutoplaySubtitlePrimingRuntime>[0]> = {},
): Parameters<typeof createAutoplaySubtitlePrimingRuntime>[0] {
let currentSubText = '';
return {
getCurrentMediaPath: () => '/media/video.mkv',
getMpvClient: () => null,
setCurrentSubText: (text) => {
currentSubText = text;
calls.push(`set:${text}`);
},
getCurrentSubText: () => currentSubText,
getCurrentSubtitleData: () => null,
getActiveParsedSubtitleCues: () => [],
setActiveParsedSubtitleMediaPath: (mediaPath) => calls.push(`tag:${mediaPath}`),
subtitleProcessingController: {
consumeCachedSubtitle: (text) => {
calls.push(`consume:${text}`);
return null;
},
onSubtitleChange: (text) => {
calls.push(`change:${text}`);
return true;
},
refreshCurrentSubtitle: (text) => {
calls.push(`refresh:${text}`);
return true;
},
notePlainSubtitleEmitted: () => {},
},
emitSubtitlePayload: (payload) =>
calls.push(`emit:${payload.text}:${payload.tokens ? 'annotated' : 'raw'}`),
getSubtitlePrefetchService: () => null,
getLastObservedTimePos: () => 12,
getVisibleOverlayVisible: () => true,
emitSecondarySubtitle: () => {},
initSubtitlePrefetch: async (sourcePath) => {
calls.push(`init:${sourcePath}`);
},
refreshSubtitlePrefetchFromActiveTrack: async () => {},
logDebug: () => {},
...overrides,
};
}
test('a cached line primes with its annotated payload and never paints it raw first', async () => {
const calls: string[] = [];
const mediaPath = '/media/video.mkv';
const cached: SubtitleData = { text: '起動字幕', tokens: [] };
const runtime = createAutoplaySubtitlePrimingRuntime(
makeRecordingDeps(calls, {
getMpvClient: () => ({
connected: true,
currentVideoPath: mediaPath,
requestProperty: async (name) => (name === 'sub-text' ? '起動字幕' : null),
}),
subtitleProcessingController: {
consumeCachedSubtitle: (text) => {
calls.push(`consume:${text}`);
return cached;
},
onSubtitleChange: (text) => {
calls.push(`change:${text}`);
return false;
},
refreshCurrentSubtitle: () => true,
notePlainSubtitleEmitted: () => {},
},
}),
);
await runtime.primeCurrentSubtitleForAutoplay(mediaPath);
assert.deepEqual(calls, [
'set:起動字幕',
// Consumed before the controller hears about the line, which would otherwise schedule
// its own tokenization of it.
'consume:起動字幕',
'change:起動字幕',
'emit:起動字幕:annotated',
]);
});
test('the subtitle sidebar tags its media path only once prefetch has loaded the source', async () => {
const calls: string[] = [];
const runtime = createAutoplaySubtitlePrimingRuntime(makeRecordingDeps(calls));
await runtime.refreshSubtitleSidebarFromSource('/subs/episode.srt', ' /media/other.mkv ');
await runtime.refreshSubtitleSidebarFromSource('/subs/episode.srt');
assert.deepEqual(calls, [
'init:/subs/episode.srt',
'tag:/media/other.mkv',
'init:/subs/episode.srt',
// Without an explicit media path the tag falls back to the current media.
'tag:/media/video.mkv',
]);
});
test('a failed subtitle sidebar prefetch leaves the media path untagged', async () => {
const calls: string[] = [];
const runtime = createAutoplaySubtitlePrimingRuntime(
makeRecordingDeps(calls, {
initSubtitlePrefetch: async () => {
throw new Error('unreadable subtitle file');
},
}),
);
await assert.rejects(
runtime.refreshSubtitleSidebarFromSource('/subs/episode.srt', '/media/other.mkv'),
/unreadable subtitle file/,
);
assert.deepEqual(calls, []);
});