refactor: extract mpv osd and secondary-sub runtime wiring

This commit is contained in:
2026-02-20 03:33:06 -08:00
parent 2d89dd43f2
commit eef8a7eb41
7 changed files with 136 additions and 41 deletions

View File

@@ -0,0 +1,33 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createMpvOsdRuntimeHandlers } from './mpv-osd-runtime-handlers';
test('mpv osd runtime handlers compose append and osd logging flow', () => {
const calls: string[] = [];
const runtime = createMpvOsdRuntimeHandlers({
appendToMpvLogMainDeps: {
logPath: '/tmp/subminer/mpv.log',
dirname: () => '/tmp/subminer',
mkdirSync: () => {},
appendFileSync: (_targetPath, data) => calls.push(`append:${data.trimEnd()}`),
now: () => new Date('2026-02-20T00:00:00.000Z'),
},
buildShowMpvOsdMainDeps: (appendToMpvLog) => ({
appendToMpvLog,
showMpvOsdRuntime: (_client, text, fallbackLog) => {
calls.push(`show:${text}`);
fallbackLog('fallback');
},
getMpvClient: () => null,
logInfo: (line) => calls.push(`info:${line}`),
}),
});
runtime.showMpvOsd('hello');
assert.deepEqual(calls, [
'append:[2026-02-20T00:00:00.000Z] [OSD] hello',
'show:hello',
'info:fallback',
]);
});

View File

@@ -0,0 +1,29 @@
import { createAppendToMpvLogHandler, createShowMpvOsdHandler } from './mpv-osd-log';
import {
createBuildAppendToMpvLogMainDepsHandler,
createBuildShowMpvOsdMainDepsHandler,
} from './mpv-osd-log-main-deps';
type AppendToMpvLogMainDeps = Parameters<typeof createBuildAppendToMpvLogMainDepsHandler>[0];
type ShowMpvOsdMainDeps = Parameters<typeof createBuildShowMpvOsdMainDepsHandler>[0];
export function createMpvOsdRuntimeHandlers(deps: {
appendToMpvLogMainDeps: AppendToMpvLogMainDeps;
buildShowMpvOsdMainDeps: (appendToMpvLog: (message: string) => void) => ShowMpvOsdMainDeps;
}) {
const appendToMpvLogMainDeps =
createBuildAppendToMpvLogMainDepsHandler(deps.appendToMpvLogMainDeps)();
const appendToMpvLogHandler = createAppendToMpvLogHandler(appendToMpvLogMainDeps);
const appendToMpvLog = (message: string) => appendToMpvLogHandler(message);
const showMpvOsdMainDeps = createBuildShowMpvOsdMainDepsHandler(
deps.buildShowMpvOsdMainDeps(appendToMpvLog),
)();
const showMpvOsdHandler = createShowMpvOsdHandler(showMpvOsdMainDeps);
const showMpvOsd = (text: string) => showMpvOsdHandler(text);
return {
appendToMpvLog,
showMpvOsd,
};
}

View File

@@ -0,0 +1,29 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createCycleSecondarySubModeRuntimeHandler } from './secondary-sub-mode-runtime-handler';
test('secondary sub mode runtime handler composes deps for runtime call', () => {
const calls: string[] = [];
const handleCycleSecondarySubMode = createCycleSecondarySubModeRuntimeHandler({
cycleSecondarySubModeMainDeps: {
getSecondarySubMode: () => 'hidden',
setSecondarySubMode: () => calls.push('set-mode'),
getLastSecondarySubToggleAtMs: () => 10,
setLastSecondarySubToggleAtMs: () => calls.push('set-ts'),
broadcastToOverlayWindows: (channel, mode) => calls.push(`broadcast:${channel}:${mode}`),
showMpvOsd: (text) => calls.push(`osd:${text}`),
},
cycleSecondarySubMode: (deps) => {
deps.setSecondarySubMode('romaji' as never);
deps.broadcastSecondarySubMode('romaji' as never);
deps.showMpvOsd('romaji');
},
});
handleCycleSecondarySubMode();
assert.deepEqual(calls, [
'set-mode',
'broadcast:secondary-subtitle:mode:romaji',
'osd:romaji',
]);
});

View File

@@ -0,0 +1,17 @@
import { createBuildCycleSecondarySubModeMainDepsHandler } from './secondary-sub-mode-main-deps';
type CycleSecondarySubModeMainDeps = Parameters<
typeof createBuildCycleSecondarySubModeMainDepsHandler
>[0];
type CycleSecondarySubModeDeps = ReturnType<
ReturnType<typeof createBuildCycleSecondarySubModeMainDepsHandler>
>;
export function createCycleSecondarySubModeRuntimeHandler(deps: {
cycleSecondarySubModeMainDeps: CycleSecondarySubModeMainDeps;
cycleSecondarySubMode: (deps: CycleSecondarySubModeDeps) => void;
}) {
const buildCycleSecondarySubModeMainDepsHandler =
createBuildCycleSecondarySubModeMainDepsHandler(deps.cycleSecondarySubModeMainDeps);
return () => deps.cycleSecondarySubMode(buildCycleSecondarySubModeMainDepsHandler());
}