Files
SubMiner/src/main/assemblies/startup.test.ts
T
sudacode f89f78b861 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
2026-11-04 21:54:49 -08:00

180 lines
6.5 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { DEFAULT_CONFIG } from '../../config/definitions';
import type { ResolvedConfig } from '../../types';
import { createAppState } from '../state';
import { createStartupAssembly, type StartupAssemblyDeps } from './startup';
type MpvClient = NonNullable<ReturnType<typeof createAppState>['mpvClient']>;
// Every callback throws, so a test fails if the assembly calls a dep it should only call later.
function lazy(name: string) {
return (): never => {
throw new Error(`${name} called unexpectedly`);
};
}
/** Builds deps whose every function throws unless a test overrides it. */
function makeDeps(overrides: Partial<StartupAssemblyDeps> = {}): StartupAssemblyDeps {
const thrower = <T extends object>(name: string): T =>
new Proxy({}, { get: (_target, key: string) => lazy(`${name}.${key}`) }) as T;
return {
appState: createAppState({ mpvSocketPath: '/tmp/subminer-startup.sock', texthookerPort: 0 }),
configService: thrower('configService'),
logger: thrower('logger'),
logConfigWarning: lazy('logConfigWarning'),
configDir: '/tmp/subminer-startup-assembly-test',
userDataPath: '/tmp/subminer-startup-assembly-test',
defaultTexthookerPort: 5174,
showErrorBox: lazy('showErrorBox'),
reportFatalError: lazy('reportFatalError'),
requestAppQuit: lazy('requestAppQuit'),
logging: {
setLogLevel: lazy('setLogLevel'),
setLogRotation: lazy('setLogRotation'),
setLogFileToggles: lazy('setLogFileToggles'),
},
overlayManager: thrower('overlayManager'),
overlayModalRuntime: thrower('overlayModalRuntime'),
subtitleWsService: thrower('subtitleWsService'),
annotationSubtitleWsService: thrower('annotationSubtitleWsService'),
texthookerService: thrower('texthookerService'),
configDerivedRuntime: thrower('configDerivedRuntime'),
hasMpvWebsocketPlugin: lazy('hasMpvWebsocketPlugin'),
loadSubtitlePosition: lazy('loadSubtitlePosition'),
setSecondarySubMode: lazy('setSecondarySubMode'),
yomitan: thrower('yomitan'),
stats: { ensureImmersionTrackerStarted: lazy('ensureImmersionTrackerStarted') } as never,
firstRunSetup: thrower('firstRunSetup'),
youtube: thrower('youtube'),
overlay: thrower('overlay'),
shortcuts: thrower('shortcuts'),
settings: { hotReload: thrower('settings.hotReload') },
mining: thrower('mining'),
getAnilist: lazy('getAnilist'),
getJellyfin: lazy('getJellyfin'),
getPlayback: lazy('getPlayback'),
getPluginHost: lazy('getPluginHost'),
getOverlayNotifications: lazy('getOverlayNotifications'),
handleInitialArgs: lazy('handleInitialArgs'),
...overrides,
};
}
/** Deps for the overlay prerequisites, recording what each step does. */
function makePrereqDeps(calls: string[]): Partial<StartupAssemblyDeps> {
const record = (name: string) => () => {
calls.push(name);
};
return {
configService: { getConfig: () => DEFAULT_CONFIG } as StartupAssemblyDeps['configService'],
loadSubtitlePosition: record('loadSubtitlePosition'),
shortcuts: {
refreshCurrentSessionBindings: record('refreshCurrentSessionBindings'),
refreshOverlayShortcuts: record('refreshOverlayShortcuts'),
} as unknown as StartupAssemblyDeps['shortcuts'],
getPlayback: () =>
({
createMpvClient: () => {
calls.push('createMpvClient');
return {} as MpvClient;
},
subtitleProcessingController: {
invalidateTokenizationCache: record('invalidateTokenizationCache'),
},
getSubtitlePrefetchService: () => ({ onSeek: record('prefetch.onSeek') }),
getLastObservedTimePos: () => 12,
}) as unknown as ReturnType<StartupAssemblyDeps['getPlayback']>,
overlay: {
visibilityComposer: {
broadcastRuntimeOptionsChanged: record('broadcastRuntimeOptionsChanged'),
},
} as unknown as StartupAssemblyDeps['overlay'],
};
}
// main.ts builds the assembly before most siblings exist, so construction must stay lazy.
test('createStartupAssembly does not call its deps while constructing', () => {
const startup = createStartupAssembly(makeDeps());
assert.equal(typeof startup.runAppReady, 'function');
});
test('overlay startup prerequisites are created once', () => {
const calls: string[] = [];
const deps = makeDeps(makePrereqDeps(calls));
const startup = createStartupAssembly(deps);
startup.ensureOverlayStartupPrereqs();
deps.appState.subtitlePosition = { yPercent: 10 };
startup.ensureOverlayStartupPrereqs();
assert.deepEqual(calls, [
'loadSubtitlePosition',
'refreshCurrentSessionBindings',
'createMpvClient',
'refreshCurrentSessionBindings',
]);
assert.ok(deps.appState.runtimeOptionsManager);
assert.ok(deps.appState.subtitleTimingTracker);
});
test('a runtime option change refreshes tokenization, prefetch, overlay state and shortcuts', () => {
const calls: string[] = [];
const deps = makeDeps(makePrereqDeps(calls));
createStartupAssembly(deps).ensureOverlayStartupPrereqs();
calls.length = 0;
const manager = deps.appState.runtimeOptionsManager;
assert.ok(manager);
manager.setOptionValue('subtitle.annotation.jlpt', true);
assert.deepEqual(calls, [
'invalidateTokenizationCache',
'prefetch.onSeek',
'broadcastRuntimeOptionsChanged',
'refreshOverlayShortcuts',
]);
});
test('a headless launch without --refresh-known-words runs the initial CLI command', async () => {
let handled = 0;
const deps = makeDeps({
handleInitialArgs: () => {
handled += 1;
},
});
await createStartupAssembly(deps).runHeadlessInitialCommand();
assert.equal(handled, 1);
});
test('a headless known-word refresh fails and quits when AnkiConnect is disabled', async (t) => {
const previousExitCode = process.exitCode;
t.after(() => {
process.exitCode = previousExitCode ?? 0;
});
const config: ResolvedConfig = {
...DEFAULT_CONFIG,
ankiConnect: { ...DEFAULT_CONFIG.ankiConnect, enabled: false },
};
const errors: string[] = [];
let quits = 0;
const deps = makeDeps({
configService: { getConfig: () => config } as StartupAssemblyDeps['configService'],
logger: { error: (message: string) => errors.push(message) } as never,
requestAppQuit: () => {
quits += 1;
},
});
deps.appState.initialArgs = { refreshKnownWords: true } as never;
await createStartupAssembly(deps).runHeadlessInitialCommand();
assert.deepEqual(errors, [
'Headless known-word refresh failed: AnkiConnect integration not enabled',
]);
assert.equal(process.exitCode, 1);
assert.equal(quits, 1);
});