mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-07-30 07:21:32 -07:00
refactor(main): split main.ts into focused runtime modules (#123)
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
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 type { CompiledSessionBinding, ResolvedConfig } from '../../types';
|
||||
import { createSessionBindingsRuntime } from './session-bindings-runtime';
|
||||
|
||||
test('persistSessionBindings logs and does not publish bindings when artifact write fails', () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-session-bindings-runtime-'));
|
||||
const configDir = path.join(root, 'config-file');
|
||||
fs.writeFileSync(configDir, 'not a directory');
|
||||
const calls: string[] = [];
|
||||
const runtime = createSessionBindingsRuntime({
|
||||
configDir,
|
||||
getKeybindings: () => [],
|
||||
getConfiguredShortcuts: () => ({ multiCopyTimeoutMs: 1500 }) as never,
|
||||
getResolvedConfig: () =>
|
||||
({
|
||||
stats: { toggleKey: 's', markWatchedKey: 'w' },
|
||||
}) as ResolvedConfig,
|
||||
getMpvClient: () => null,
|
||||
setSessionBindings: () => calls.push('setSessionBindings'),
|
||||
setSessionBindingsInitialized: () => calls.push('setSessionBindingsInitialized'),
|
||||
logWarn: (message) => calls.push(`warn:${message}`),
|
||||
});
|
||||
|
||||
try {
|
||||
assert.throws(
|
||||
() => runtime.persistSessionBindings([] as CompiledSessionBinding[]),
|
||||
/ENOTDIR|EEXIST/,
|
||||
);
|
||||
assert.deepEqual(calls, ['warn:[session-bindings] Failed to write session bindings artifact']);
|
||||
} finally {
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('persistSessionBindings keeps saved bindings when mpv reload notification fails', () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-session-bindings-runtime-'));
|
||||
const calls: string[] = [];
|
||||
const runtime = createSessionBindingsRuntime({
|
||||
configDir: root,
|
||||
getKeybindings: () => [],
|
||||
getConfiguredShortcuts: () => ({ multiCopyTimeoutMs: 1500 }) as never,
|
||||
getResolvedConfig: () =>
|
||||
({
|
||||
stats: { toggleKey: 's', markWatchedKey: 'w' },
|
||||
}) as ResolvedConfig,
|
||||
getMpvClient: () =>
|
||||
({
|
||||
connected: true,
|
||||
send: () => {
|
||||
throw new Error('mpv unavailable');
|
||||
},
|
||||
}) as never,
|
||||
setSessionBindings: () => calls.push('setSessionBindings'),
|
||||
setSessionBindingsInitialized: () => calls.push('setSessionBindingsInitialized'),
|
||||
logWarn: (message) => calls.push(`warn:${message}`),
|
||||
});
|
||||
|
||||
try {
|
||||
assert.doesNotThrow(() => runtime.persistSessionBindings([] as CompiledSessionBinding[]));
|
||||
assert.deepEqual(calls, [
|
||||
'setSessionBindings',
|
||||
'setSessionBindingsInitialized',
|
||||
'warn:[session-bindings] Failed to notify mpv to reload session bindings',
|
||||
]);
|
||||
} finally {
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user