refactor(main): move core layers into assemblies (#310)

* refactor(main): move first-run setup into an assembly

* refactor(main): move playback and subtitle pipeline state into an assembly

Moves the mpv runtime composer wiring together with the subtitle processing, prefetch, sidebar playback-state, seek-intent, secondary-subtitle and autoplay-priming state it mutates into createPlaybackAssembly. Replaces the main-wiring regex tests for the moved code with behaviour tests that drive real mpv events through a fake client.

* refactor(main): move overlay windows and visibility into an assembly

* refactor(main): read Electron screen lazily in the overlay assembly deps

* docs: describe overlay, playback, and first-run setup assemblies

* test(playback): clear scheduled prefetch refresh in warm-tokenization tests
This commit is contained in:
sudacode authored and GitHub committed 2026-10-31 22:04:42 -07:00
1 parent 804fb8933e
commit af44735bb8
11 files changed
+2726 -1689

No files matched your search

@@ -0,0 +1,67 @@
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import {
createDefaultSetupState,
getSetupStatePath,
writeSetupState,
} from '../../shared/setup-state';
import { createFirstRunSetupAssembly, type FirstRunSetupAssemblyDeps } from './first-run-setup';
// Every callback throws, so a test fails if the assembly calls a dep it should only call later.
function makeDeps(configDir: string): FirstRunSetupAssemblyDeps {
const lazy = (name: string) => (): never => {
throw new Error(`${name} called unexpectedly`);
};
return {
appState: {
firstRunSetupCompleted: false,
firstRunSetupWindow: null,
backgroundMode: false,
initialArgs: null,
},
configService: { getConfig: lazy('getConfig'), patchRawConfig: lazy('patchRawConfig') },
logger: { error: lazy('error'), info: lazy('info') },
configDir,
userDataPath: configDir,
dictionaryBackend: 'yomitan',
ensureYomitanExtensionLoaded: lazy('ensureYomitanExtensionLoaded'),
getYomitanParserRuntimeDeps: lazy('getYomitanParserRuntimeDeps'),
createCommandLineLauncherRuntimeOptions: lazy('createCommandLineLauncherRuntimeOptions'),
getAppPath: lazy('getAppPath'),
trashItem: lazy('trashItem'),
writeShortcutLink: lazy('writeShortcutLink'),
createBrowserWindow: lazy('createBrowserWindow'),
openDictionarySettings: lazy('openDictionarySettings'),
openConfigSettingsWindow: lazy('openConfigSettingsWindow'),
onSetupStateChanged: lazy('onSetupStateChanged'),
requestAppQuit: lazy('requestAppQuit'),
};
}
function makeConfigDir(t: { after: (fn: () => void) => void }): string {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-first-run-assembly-test-'));
t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
return dir;
}
// main.ts builds the assembly before most siblings exist, so construction must stay lazy.
test('createFirstRunSetupAssembly does not call its deps while constructing', (t) => {
const firstRunSetup = createFirstRunSetupAssembly(makeDeps(makeConfigDir(t)));
assert.equal(typeof firstRunSetup.service.isSetupCompleted, 'function');
assert.equal(typeof firstRunSetup.openWindow, 'function');
});
test('openWindow skips a completed setup unless forced', (t) => {
const configDir = makeConfigDir(t);
writeSetupState(getSetupStatePath(configDir), {
...createDefaultSetupState(),
status: 'completed',
});
const firstRunSetup = createFirstRunSetupAssembly(makeDeps(configDir));
firstRunSetup.openWindow();
assert.throws(() => firstRunSetup.openWindow(true), /createBrowserWindow called unexpectedly/);
});