Compare commits

...
10 Commits
Author SHA1 Message Date
sudacode c18644392d fix(overlay): stop Hyprland/Sway tracker polls from blocking the main thread (#289) 2026-10-11 01:02:02 -07:00
sudacode 9c351a8603 fix(overlay): poll cheap playback state in the open subtitle sidebar (#290) 2026-10-11 01:02:02 -07:00
sudacode 6c295586cb fix(hachidori): close an open pane after its attention is released 2026-10-11 00:54:01 -07:00
sudacode 0523beef2b fix(hachidori): treat only an open pane as an open popup for lookup controls 2026-10-11 00:44:07 -07:00
sudacode 06fc578830 Merge remote-tracking branch 'origin/main' into add-hachidori-backend
# Conflicts:
#	src/renderer/handlers/keyboard.test.ts
2026-10-11 00:35:19 -07:00
sudacode 7108e086ba fix(overlay): keep keyboard mode from swallowing modal and text-field keys (#291) 2026-10-11 00:33:47 -07:00
sudacode 994483ccd6 build(hachidori): bump fork to a52694c (LF checkouts on Windows) 2026-10-10 22:39:29 -07:00
sudacode 6350ee928d build(hachidori): bump fork to 15babde
- Update vendor/hachidori submodule from 343e861 to 15babde
- Remove stale generated release/release-notes.md from v0.20.1
2026-10-10 21:21:46 -07:00
sudacode 78a9b6346f fix(jellyfin): make terminal play work with a running app and report progress (#288) 2026-10-10 17:27:48 -07:00
sudacode 78eca52b7e test: add headless end-to-end harness (#286)
* test: add headless end-to-end harness and CI job

Boot the built app against a real mpv, a synthetic clip, a fixture Yomitan
dictionary, and a fake AnkiConnect inside a private Xvfb display, then assert
on the overlay DOM and the AnkiConnect requests. Adds the test:e2e lane, an
e2e session CLI for exploratory checks, and a three-platform CI job.

* ci: set e2e temp dir from a step

* test: make e2e session startup robust on slow machines

Bun caps node:test hooks at 5s, so boot the session inside the first test.
Keep pausing until the app's resume retries stop, read cue tokens in one
round trip, and resolve Electron through its package entry so CI downloads
the binary.

* test: harden e2e harness for CI runners

Fail fast when mpv cannot be spawned, read every Yomitan popup frame instead
of trusting target order, and put the chocolatey mpv directory on PATH.

* test: add e2e failure diagnostics and mark Windows job experimental

Report what the overlay showed when a cue never renders, dispose the session
when the overlay cannot be opened, skip popup frames that vanish mid-poll,
and turn off AniSkip lookups in the harness config.

* test: read the lookup popup entry from textContent

* test: capture renderer console and main-side subtitle state in e2e failures

* test: expose the Electron main-process inspector to the e2e harness

Adds session.mainProcess() and 'e2e eval main', and reports main's window
list alongside the overlay state when a cue never renders.

* test: tap subtitle sends and receipts in e2e failure diagnostics

* test: probe alternate main-to-renderer paths in e2e failure diagnostics

* test: select the visible overlay layer, not the primed modal window

Windows and macOS pre-create a hidden modal window that loads the same
renderer document, and the harness could attach to it instead of the
visible overlay. Drops the delivery taps that found this.

* ci: run the Windows e2e job as a normal matrix entry

* test: address review on e2e harness

Slice the app log at a byte offset before decoding, create the session state
file owner-only since it carries the inspector URL, and keep the cue failure
when inspector diagnostics are unavailable.

* test: create the e2e session state file exclusively
2026-10-05 22:00:58 -07:00
74 changed files with 3170 additions and 1168 deletions

No files matched your search

@@ -27,6 +27,7 @@ Do not use hidden wrapper commands. Verification commands are owned by `package.
- Launcher or mpv plugin: `bun run test:launcher` or `bun run test:env`, based on the behavior changed
- Runtime compatibility or dist-sensitive wiring: `bun run test:runtime:compat`
- Stats dashboard: `bun run test:stats`
- Overlay, renderer, mpv, or mining behavior in the running app: `bun run build`, then `bun run test:e2e`
- Build/release scripts: `bun run test:scripts`
For substantial changes, use the full gate documented in `AGENTS.md` and `docs/workflow/verification.md`.
+76
View File
@@ -9,3 +9,79 @@ on:
jobs:
build-test-audit:
uses: ./.github/workflows/quality-gate.yml
# Boots the built app with a real mpv on each platform. Kept outside the
# reusable quality gate so releases do not depend on it while the lane is new.
e2e:
name: E2E (${{ matrix.os }})
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
timeout-minutes: 30
permissions:
contents: read
defaults:
run:
shell: bash
env:
SUBMINER_E2E_KEEP: '1'
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: true
persist-credentials: false
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.3.5
- name: Point temp files at an uploadable directory
run: |
# os.tmpdir() follows these, so the kept session logs land under runner.temp.
dir="$RUNNER_TEMP/e2e"
mkdir -p "$dir"
{ echo "TMPDIR=$dir"; echo "TEMP=$dir"; echo "TMP=$dir"; } >> "$GITHUB_ENV"
- name: Install mpv and ffmpeg (Linux)
if: runner.os == 'Linux'
run: |
# Only Ubuntu sources are needed; unrelated runner repositories can be unavailable.
apt_sources=(-o Dir::Etc::sourcelist=sources.list.d/ubuntu.sources -o Dir::Etc::sourceparts=-)
sudo apt-get "${apt_sources[@]}" update
sudo apt-get "${apt_sources[@]}" install -y mpv ffmpeg xvfb
- name: Install mpv and ffmpeg (macOS)
if: runner.os == 'macOS'
run: brew install mpv ffmpeg
- name: Install mpv and ffmpeg (Windows)
if: runner.os == 'Windows'
run: |
choco install mpv ffmpeg -y --no-progress
# The mpv package adds no shim, so put its directory on PATH for later steps.
mpv_exe="$(find /c/ProgramData/chocolatey/lib /c/ProgramData/chocolatey/bin "/c/Program Files" /c/tools -maxdepth 6 -iname mpv.exe 2>/dev/null | head -n 1)"
test -n "$mpv_exe"
cygpath -w "$(dirname "$mpv_exe")" >> "$GITHUB_PATH"
- name: Install dependencies
run: |
bun install --frozen-lockfile
cd stats && bun install --frozen-lockfile
- name: Build
run: bun run build
- name: End-to-end suite
run: bun run test:e2e
- name: Upload session logs (on failure)
if: failure()
uses: actions/upload-artifact@v4
with:
name: e2e-logs-${{ matrix.os }}
path: ${{ runner.temp }}/e2e/subminer-e2e-*/*.log
if-no-files-found: ignore
+1
View File
@@ -45,6 +45,7 @@ Start here, then leave this file.
- Launcher/plugin: `bun run test:launcher` or `bun run test:env`
- Runtime-compat / dist-sensitive: `bun run test:runtime:compat`
- Stats dashboard UI (`stats/`): `bun run test:stats`
- Overlay / renderer / mpv / mining behavior in the running app: `bun run build`, then `bun run test:e2e` (headless, sandboxed; `bun run e2e start` for exploratory checks)
- Build/release scripts (`scripts/**`): `bun run test:scripts`
- Internal docs, `AGENTS.md`, or repo skills: `bun run test:docs:kb`
- User-facing `docs-site/`: `bun run docs:test`, then `bun run docs:build`
+4
View File
@@ -0,0 +1,4 @@
type: internal
area: testing
- Added a headless end-to-end harness (`bun run test:e2e`, `bun run e2e`) that boots the built app with a real mpv, a synthetic clip, a fixture Yomitan dictionary, and a fake AnkiConnect on an offscreen display.
+7
View File
@@ -0,0 +1,7 @@
type: fixed
area: jellyfin
- Fixed `subminer jellyfin -p` reporting "No Jellyfin libraries found." when SubMiner was already running (for example right after `subminer jellyfin` login). The app now hands library, item, and preview-auth results to the launcher directly instead of through log files, so picker image previews work again and real errors (expired session, unreachable server) are shown instead of an empty list.
- The rofi "Jellyfin Search (optional)" step after picking a library is now a compact input box with a "Type to search, or press Enter to browse all" hint instead of a blank list.
- Playing from the terminal (`subminer jellyfin -p`) now reports watch progress to Jellyfin even when cast discovery is off, so Jellyfin remembers where you stopped and marks finished episodes as played. Previously progress was only sent while the cast connection was active.
- When `subminer jellyfin -p` has to start SubMiner itself, the app now shows its tray icon and quits when you close mpv, the same as playing a local file. Before, it had no tray icon and kept running hidden after playback. A SubMiner that was already running is still reused and left running.
+4
View File
@@ -0,0 +1,4 @@
type: docs
area: jellyfin
- Corrected the Jellyfin docs: SubMiner only joins the cast menu at startup when `startupWarmups.jellyfinRemoteSession` is on (off by default) along with `jellyfin.remoteControlAutoConnect`. Otherwise tick **Jellyfin Discovery** in the tray.
+4
View File
@@ -0,0 +1,4 @@
type: fixed
area: overlay
- Keyboard-only mode no longer swallows `h`, `l`, and the left/right arrows while a modal is open or a text field has focus, so typing into fields like the Jimaku or TsukiHime title works and no longer seeks mpv. Pressing `Ctrl/Cmd+Shift+Y` (or the controller toggle) inside a modal no longer turns on a hidden keyboard-only mode in the modal window.
+4
View File
@@ -0,0 +1,4 @@
type: fixed
area: overlay
- The open subtitle sidebar no longer re-fetches the whole subtitle list and re-queries mpv about 12 times a second. It polls only the current time and line, and reloads the list when the subtitle track, file, or parsed cues change (plus a slow ~2 s fallback), cutting idle sidebar IPC and renderer work during playback.
+5
View File
@@ -0,0 +1,5 @@
type: fixed
area: overlay
- Hyprland and Sway window tracking no longer freezes the app when `hyprctl` or `swaymsg` is slow or hangs: polls now run in the background with a timeout and no longer pile up.
- The Hyprland/Sway trackers read each mpv process's command line once instead of spawning `ps` on every poll.
+12 -12
View File
@@ -520,18 +520,18 @@ In external-profile mode, SubMiner only reads the profile. It does not open its
Log in to a Jellyfin server, browse libraries, and play or cast to SubMiner. Login tokens are stored encrypted, not in this file. See [Jellyfin integration](/jellyfin-integration).
| Key | Default | What it does |
| ----------------------------------- | -------------------------------- | ----------------------------------------------- |
| `jellyfin.enabled` | `false` | Enable Jellyfin |
| `jellyfin.serverUrl` | `""` | Server URL, for example `http://localhost:8096` |
| `jellyfin.username` | `""` | Default username for `subminer jellyfin -l` |
| `jellyfin.remoteControlEnabled` | `true` | Let Jellyfin apps cast to SubMiner |
| `jellyfin.remoteControlAutoConnect` | `true` | Connect the cast session on startup |
| `jellyfin.autoAnnounce` | `false` | Announce SubMiner as a cast target on connect |
| `jellyfin.pullPictures` | `false` | Fetch posters for launcher pickers |
| `jellyfin.iconCacheDir` | `"/tmp/subminer-jellyfin-icons"` | Poster cache folder |
| `jellyfin.directPlayPreferred` | `true` | Try direct play before transcoding |
| `jellyfin.transcodeVideoCodec` | `"h264"` | Codec requested when transcoding |
| Key | Default | What it does |
| ----------------------------------- | -------------------------------- | --------------------------------------------------------------------------------------- |
| `jellyfin.enabled` | `false` | Enable Jellyfin |
| `jellyfin.serverUrl` | `""` | Server URL, for example `http://localhost:8096` |
| `jellyfin.username` | `""` | Default username for `subminer jellyfin -l` |
| `jellyfin.remoteControlEnabled` | `true` | Let Jellyfin apps cast to SubMiner |
| `jellyfin.remoteControlAutoConnect` | `true` | Connect the cast session on startup (also needs `startupWarmups.jellyfinRemoteSession`) |
| `jellyfin.autoAnnounce` | `false` | Announce SubMiner as a cast target on connect |
| `jellyfin.pullPictures` | `false` | Fetch posters for launcher pickers |
| `jellyfin.iconCacheDir` | `"/tmp/subminer-jellyfin-icons"` | Poster cache folder |
| `jellyfin.directPlayPreferred` | `true` | Try direct play before transcoding |
| `jellyfin.transcodeVideoCodec` | `"h264"` | Codec requested when transcoding |
### Discord rich presence
+10 -10
View File
@@ -14,7 +14,7 @@ SubMiner stores an encrypted session token, not your password, and turns the int
## Casting from Jellyfin
After you sign in, SubMiner connects to Jellyfin at startup and shows up in the cast ("Play on") menu under your computer's hostname. To connect for the current session only, tick **Jellyfin Discovery** in the tray menu.
After you sign in, tick **Jellyfin Discovery** in the tray menu. SubMiner then shows up in the cast ("Play on") menu under your computer's hostname until it quits. To connect every time SubMiner starts, turn on `startupWarmups.jellyfinRemoteSession` and leave `remoteControlAutoConnect` on.
1. In the Jellyfin web or mobile app, start playing an episode.
2. Open the cast menu and pick your computer.
@@ -34,20 +34,20 @@ subminer jellyfin -p # fzf picker; `jf` is an alias for `jellyfin`
subminer -R jellyfin -p # rofi picker
```
Sign in first. See [Launcher script](/launcher-script) for the other `jellyfin` subcommands.
Sign in first. Playback resumes from Jellyfin's saved position, and watch progress syncs back the same way it does for casting, whether or not discovery is on. If SubMiner is already running, the picker uses it and leaves it running. Otherwise the launcher starts SubMiner and quits it when you close mpv, the same as playing a local file. See [Launcher script](/launcher-script) for the other `jellyfin` subcommands.
## Options
All options are under **Settings > Integrations > Jellyfin**, or `jellyfin` in `config.jsonc`. See [Configuration](/configuration#jellyfin) for the full list and defaults.
| Key | What it does |
| -------------------------- | -------------------------------------------------------------------- |
| `enabled` | Turns the integration on. Set for you when you sign in. |
| `serverUrl` | Your Jellyfin server. Filled in when you sign in. |
| `remoteControlEnabled` | Lets SubMiner act as a cast target. |
| `remoteControlAutoConnect` | Connects at startup. Turn off to start discovery from the tray. |
| `autoAnnounce` | Re-announces the device on connect. Try it if SubMiner appears late. |
| `transcodeVideoCodec` | Video codec requested when Jellyfin transcodes. |
| Key | What it does |
| -------------------------- | ---------------------------------------------------------------------- |
| `enabled` | Turns the integration on. Set for you when you sign in. |
| `serverUrl` | Your Jellyfin server. Filled in when you sign in. |
| `remoteControlEnabled` | Lets SubMiner act as a cast target. |
| `remoteControlAutoConnect` | Connects at startup when `startupWarmups.jellyfinRemoteSession` is on. |
| `autoAnnounce` | Re-announces the device on connect. Try it if SubMiner appears late. |
| `transcodeVideoCodec` | Video codec requested when Jellyfin transcodes. |
For headless setups, `SUBMINER_JELLYFIN_ACCESS_TOKEN` and `SUBMINER_JELLYFIN_USER_ID` supply a session without the sign-in window. Treat the token store and `config.jsonc` as secrets.
+5 -1
View File
@@ -63,7 +63,11 @@ the plugin artifact, persistent config, or session help.
The subtitle sidebar consumes parsed cues through `SubtitleSidebarSnapshot`. Its `sourceKey`
identifies the media and subtitle source so renderer selections are invalidated on source changes,
including changes whose cue text and timings are identical. Native selection and clean clipboard
including changes whose cue text and timings are identical. While open, the sidebar polls the
cheap `SubtitleSidebarPlaybackState` (cached time, current line, `sourceKey`, `cuesRevision`; no
mpv round-trips) every 80 ms for active-cue tracking. It fetches the full snapshot, which resolves
the source over mpv IPC and carries every cue plus config, only on open, config reload, a
`sourceKey`/`cuesRevision` change, and a ~2 s fallback. Native selection and clean clipboard
serialization live in `src/renderer/modals/subtitle-sidebar-selection.ts`. Electron lets standard
Copy input reach the renderer, where sidebar selection takes priority over the live-subtitle binding.
The preload bridge writes selections through Electron's clipboard API so copying does not depend
+51 -1
View File
@@ -3,7 +3,7 @@
# Verification
Status: active
Last verified: 2026-08-13
Last verified: 2026-10-05
Owner: Kyle Yasuda
Read when: selecting the right verification lane for a change
@@ -58,6 +58,8 @@ bun run docs:build
- Launcher/plugin: `bun run test:launcher` or `bun run test:env`
- Runtime-compat / compiled behavior after `bun run build`: `bun run test:runtime:compat`
- Stats dashboard UI: `bun run test:stats`
- Overlay, renderer, mpv, or mining behavior that unit tests cannot show: `bun run build`,
then `bun run test:e2e` (see [Headless End-to-End Harness](#headless-end-to-end-harness))
- Build/release scripts (`scripts/**`): `bun run test:scripts`
- Packaging: build the platform package, then run `bun run test:package <resources-directory>`.
On headless Linux: `xvfb-run -a bun run test:package release/linux-unpacked/resources`.
@@ -94,6 +96,54 @@ bun run docs:build
- This is not a full Electron UI startup check. It does not require a display and
makes no claims about renderer, window, tray, or mpv behavior.
## Headless End-to-End Harness
`bun run test:e2e` boots the built app against a real mpv and asserts on what the
overlay renders and what reaches AnkiConnect. Use it instead of a live desktop test
whenever the behavior does not depend on a specific compositor.
- Needs an existing `bun run build`, plus `mpv` and `ffmpeg` on `PATH` (and `Xvfb` on
Linux). It is not part of `test:fast` or the handoff gate. The `e2e` job in
`.github/workflows/ci.yml` runs it on Linux, macOS, and Windows for pull requests and
`main`, outside the reusable quality gate, and uploads the session logs when it fails.
- Every run is sandboxed in a temp dir: its own config and Electron profile, mpv IPC
socket, and a fake AnkiConnect on a free port. It never touches the user's running
SubMiner, Anki, or stats database. It is not offline: the app still asks AniList for
cover art matching the clip's title.
- Media is synthetic: an ffmpeg test-pattern clip, a three-cue Japanese `.srt`, and a
tiny Yomitan dictionary covering those cues (`e2e/harness/fixtures.ts`). The harness
imports the dictionary on a first boot, then restarts the app for the scenario.
- Display: Linux runs everything inside a private Xvfb server, so no window reaches the
desktop. macOS and Windows have no offscreen display server, so windows open in the
current session there; `SUBMINER_E2E_DISPLAY=host` forces the same on Linux. The
CI job runs all three. On macOS and Windows the app pre-creates a hidden modal window
that loads the same renderer document, so window matching must include the
`layer=visible` query (`OVERLAY_PAGE`).
- The app is driven over the Chrome DevTools Protocol (DOM queries, input events,
per-window screenshots), mpv over its JSON IPC socket, and app commands through the
normal CLI handoff (`--mine-sentence`, `--toggle-subtitle-sidebar`, ...).
- Scenarios live in `e2e/*.e2e.test.ts` and share one session per file through
`startE2eSession()`. Pass `config` to override settings for a scenario file.
- On a failed startup the temp dir is kept and its path is in the error. Set
`SUBMINER_E2E_KEEP=1` to keep it after any run (`app-stdio.log`, `app.log`, `mpv.log`).
For exploratory checks, keep one session up and poke at it:
```bash
bun run e2e start & # prints the session JSON, stays up until stopped
bun run e2e mpv seek 2 absolute+exact
bun run e2e eval overlay 'document.getElementById("subtitleRoot").textContent'
bun run e2e shot overlay /tmp/overlay.png
bun run e2e app --mine-sentence
bun run e2e anki # AnkiConnect requests received so far
bun run e2e stop
```
Out of scope: compositor-specific behavior (Hyprland placement, stacking, fullscreen,
click-through), real GPU video output, and global shortcuts. Xvfb runs without a window
manager or compositor, so the transparent overlay is not composited over the video;
check overlay visuals with per-window screenshots, and use the live desktop for the rest.
## Dependency Audit Policy
- `bun audit --audit-level high` blocks the reusable quality gate.
+160
View File
@@ -0,0 +1,160 @@
// Keeps one isolated e2e session running so a change can be poked at by hand
// (or by an agent) without touching the real desktop:
//
// bun run e2e start boot and stay up until `stop` or Ctrl-C
// bun run e2e eval <page> <expression> evaluate JS in a window (or "main" for the main process)
// bun run e2e shot <page> <file.png> save a screenshot of a window
// bun run e2e mpv <command> [args...] send an mpv IPC command (args parsed as JSON)
// bun run e2e app <args...> run a SubMiner CLI command against the instance
// bun run e2e anki print the AnkiConnect requests received so far
// bun run e2e stop
//
// <page> is a URL fragment: "overlay" is shorthand for the overlay window.
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { connectCdp, connectCdpPage, listCdpTargets } from './harness/cdp';
import { connectMpv } from './harness/mpv';
import { OVERLAY_PAGE, runAppCommand, startE2eSession, type AppTarget } from './harness/session';
import { waitUntil } from './harness/wait';
type SessionState = {
pid: number;
root: string;
display: string;
cdpPort: number;
inspectorUrl: string;
mpvSocketPath: string;
ankiUrl: string;
target: AppTarget;
};
const stateFile = path.join(os.tmpdir(), 'subminer-e2e-session.json');
function readState(): SessionState {
if (!fs.existsSync(stateFile)) {
throw new Error('No e2e session is running. Start one with `bun run e2e start`.');
}
return JSON.parse(fs.readFileSync(stateFile, 'utf8')) as SessionState;
}
function isAlive(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
function parseJsonOrString(value: string): unknown {
try {
return JSON.parse(value);
} catch {
return value;
}
}
async function connectNamedPage(state: SessionState, pageName: string) {
if (pageName === 'main') return connectCdp(state.inspectorUrl, 'main process');
const urlPart = pageName === 'overlay' ? OVERLAY_PAGE : pageName;
const targets = await listCdpTargets(state.cdpPort);
const target = targets.find((candidate) => candidate.url.includes(urlPart));
if (!target) {
const open = targets.map((candidate) => ` ${candidate.type} ${candidate.url.slice(0, 100)}`);
throw new Error(`No window matches "${pageName}". Open targets:\n${open.join('\n')}`);
}
return connectCdpPage(target);
}
async function withPage<T>(
state: SessionState,
pageName: string,
use: (page: Awaited<ReturnType<typeof connectCdpPage>>) => Promise<T>,
): Promise<T> {
const page = await connectNamedPage(state, pageName);
try {
return await use(page);
} finally {
page.close();
}
}
async function start(): Promise<void> {
if (fs.existsSync(stateFile) && isAlive(readState().pid)) {
throw new Error('An e2e session is already running. Stop it with `bun run e2e stop`.');
}
const session = await startE2eSession();
const state: SessionState = {
pid: process.pid,
root: session.root,
display: session.display,
cdpPort: session.cdpPort,
inspectorUrl: session.inspectorUrl,
mpvSocketPath: session.mpvSocketPath,
ankiUrl: session.anki.url,
target: session.target,
};
// The state file grants main-process code evaluation via inspectorUrl, so keep it
// owner-only. 'wx' creates it exclusively: it fails rather than follow a symlink
// planted in the shared temp dir after the stale file is removed.
fs.rmSync(stateFile, { force: true });
fs.writeFileSync(stateFile, JSON.stringify(state, null, 2), { mode: 0o600, flag: 'wx' });
console.log(JSON.stringify(state, null, 2));
await new Promise<void>((resolve) => {
process.once('SIGINT', resolve);
process.once('SIGTERM', resolve);
});
fs.rmSync(stateFile, { force: true });
await session.dispose();
}
async function main(): Promise<void> {
const [command, ...args] = process.argv.slice(2);
if (command === 'start') return start();
const state = readState();
switch (command) {
case 'eval': {
const [pageName, expression] = args;
if (!pageName || !expression) throw new Error('Usage: eval <page> <expression>');
const value = await withPage(state, pageName, (page) => page.evaluate<unknown>(expression));
console.log(JSON.stringify(value, null, 2));
return;
}
case 'shot': {
const [pageName, outputPath] = args;
if (!pageName || !outputPath) throw new Error('Usage: shot <page> <file.png>');
fs.writeFileSync(outputPath, await withPage(state, pageName, (page) => page.screenshot()));
return;
}
case 'mpv': {
if (args.length === 0) throw new Error('Usage: mpv <command> [args...]');
const mpv = await connectMpv(state.mpvSocketPath);
try {
console.log(JSON.stringify((await mpv.command(...args.map(parseJsonOrString))) ?? null));
} finally {
mpv.close();
}
return;
}
case 'app':
await runAppCommand(state.target, args);
return;
case 'anki':
console.log(JSON.stringify(await (await fetch(state.ankiUrl)).json(), null, 2));
return;
case 'stop':
process.kill(state.pid, 'SIGTERM');
await waitUntil(() => !isAlive(state.pid), { description: 'e2e session to exit' });
return;
default:
throw new Error(`Unknown command "${command ?? ''}". See the header of e2e/cli.ts.`);
}
}
main().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : error);
process.exit(1);
});
+133
View File
@@ -0,0 +1,133 @@
import { waitUntil, type Truthy, type WaitOptions } from './wait';
export type CdpTarget = {
type: string;
title: string;
url: string;
webSocketDebuggerUrl: string;
};
export type CdpPage = {
/** Sends a raw DevTools protocol command, for anything the helpers below do not cover. */
send: (method: string, params?: Record<string, unknown>) => Promise<Record<string, unknown>>;
/** Evaluates an expression in the page, awaiting promises, and returns its JSON value. */
evaluate: <T>(expression: string) => Promise<T>;
/** Polls an expression until it is truthy and returns that value. */
waitFor: <T>(expression: string, options: WaitOptions) => Promise<Truthy<T>>;
/** Console output and uncaught exceptions seen since connecting, oldest first. */
console: string[];
/** PNG of the page as rendered by Chromium; needs no OS screen-capture permission. */
screenshot: () => Promise<Buffer>;
close: () => void;
};
type CdpReply = {
id?: number;
method?: string;
params?: Record<string, unknown>;
error?: { message: string };
result?: Record<string, unknown>;
};
type RemoteObject = { value?: unknown; description?: string };
// Renders a console/exception event as one log line.
function formatConsoleEvent(method: string, params: Record<string, unknown>): string | null {
if (method === 'Runtime.consoleAPICalled') {
const args = Array.isArray(params.args) ? (params.args as RemoteObject[]) : [];
const text = args.map((arg) => arg.description ?? JSON.stringify(arg.value)).join(' ');
return `console.${String(params.type)}: ${text}`;
}
if (method === 'Runtime.exceptionThrown') {
const details = params.exceptionDetails as
| { text?: string; exception?: RemoteObject }
| undefined;
return `exception: ${details?.exception?.description ?? details?.text ?? 'unknown'}`;
}
if (method === 'Log.entryAdded') {
const entry = params.entry as { level?: string; source?: string; text?: string } | undefined;
return `${String(entry?.source)}.${String(entry?.level)}: ${String(entry?.text)}`;
}
return null;
}
type EvaluateResult = {
result: { value?: unknown };
exceptionDetails?: { text: string; exception?: { description?: string } };
};
export async function listCdpTargets(port: number): Promise<CdpTarget[]> {
const response = await fetch(`http://127.0.0.1:${port}/json/list`);
return (await response.json()) as CdpTarget[];
}
export function connectCdpPage(target: CdpTarget): Promise<CdpPage> {
return connectCdp(target.webSocketDebuggerUrl, target.url);
}
/** Connects to any DevTools endpoint: a renderer page or Electron's main-process inspector. */
export async function connectCdp(webSocketDebuggerUrl: string, label: string): Promise<CdpPage> {
const socket = new WebSocket(webSocketDebuggerUrl);
await new Promise<void>((resolve, reject) => {
socket.addEventListener('open', () => resolve(), { once: true });
socket.addEventListener('error', () => reject(new Error(`CDP connection to ${label} failed`)), {
once: true,
});
});
const pending = new Map<number, (reply: CdpReply) => void>();
const consoleLines: string[] = [];
let nextId = 1;
socket.addEventListener('message', (event) => {
const reply: CdpReply = JSON.parse(String(event.data));
if (reply.id === undefined) {
if (reply.method) {
const line = formatConsoleEvent(reply.method, reply.params ?? {});
if (line) consoleLines.push(line);
}
return;
}
pending.get(reply.id)?.(reply);
pending.delete(reply.id);
});
const send = (method: string, params: Record<string, unknown> = {}) =>
new Promise<Record<string, unknown>>((resolve, reject) => {
const id = nextId++;
pending.set(id, (reply) => {
if (reply.error) reject(new Error(`CDP ${method} failed: ${reply.error.message}`));
else resolve(reply.result ?? {});
});
socket.send(JSON.stringify({ id, method, params }));
});
const evaluate = async <T>(expression: string): Promise<T> => {
const reply = (await send('Runtime.evaluate', {
expression,
returnByValue: true,
awaitPromise: true,
})) as EvaluateResult;
if (reply.exceptionDetails) {
const { text, exception } = reply.exceptionDetails;
throw new Error(`Page evaluation threw: ${exception?.description ?? text}`);
}
return reply.result.value as T;
};
await send('Runtime.enable');
// The Node inspector behind Electron's main process has no Log domain.
await send('Log.enable').catch(() => undefined);
return {
send,
evaluate,
console: consoleLines,
waitFor: <T>(expression: string, options: WaitOptions) =>
waitUntil(() => evaluate<T>(expression), options),
screenshot: async () => {
const reply = await send('Page.captureScreenshot', { format: 'png' });
return Buffer.from(String(reply.data), 'base64');
},
close: () => socket.close(),
};
}
+75
View File
@@ -0,0 +1,75 @@
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import { stopProcess, type EnvDelta } from './process';
export const E2E_SCREEN = { width: 1280, height: 720 } as const;
export type E2eDisplay = {
kind: 'xvfb' | 'host';
env: EnvDelta;
stop: () => Promise<void>;
};
// Linux gets a private Xvfb server so no window ever reaches the real desktop.
// macOS and Windows have no offscreen display server, so windows open in the
// current GUI session there. SUBMINER_E2E_DISPLAY=host forces that on Linux too.
export async function startDisplay(): Promise<E2eDisplay> {
const requested = process.env.SUBMINER_E2E_DISPLAY?.trim().toLowerCase();
if (requested && requested !== 'host' && requested !== 'xvfb') {
throw new Error(`SUBMINER_E2E_DISPLAY must be "host" or "xvfb", got "${requested}"`);
}
const kind = requested ?? (process.platform === 'linux' ? 'xvfb' : 'host');
if (kind === 'host') {
return { kind, env: { set: {}, unset: [] }, stop: async () => {} };
}
return startXvfb();
}
async function startXvfb(): Promise<E2eDisplay> {
// -displayfd makes Xvfb pick a free display number and write it to fd 3 once
// it accepts connections, so there is no port guessing and no startup sleep.
const child = spawn(
'Xvfb',
[
'-displayfd',
'3',
'-screen',
'0',
`${E2E_SCREEN.width}x${E2E_SCREEN.height}x24`,
'-nolisten',
'tcp',
],
{ stdio: ['ignore', 'ignore', 'ignore', 'pipe'] },
);
const displayFd = child.stdio[3];
if (!displayFd) throw new Error('Xvfb did not expose its display fd');
const displayNumber = await Promise.race([
once(displayFd, 'data').then(([chunk]) => String(chunk).trim()),
once(child, 'error').then(([error]) => {
throw new Error(
`Could not start Xvfb (${String(error)}). Install it or set SUBMINER_E2E_DISPLAY=host.`,
);
}),
once(child, 'exit').then(([code]) => {
throw new Error(`Xvfb exited during startup with code ${String(code)}`);
}),
]);
return {
kind: 'xvfb',
env: {
set: { DISPLAY: `:${displayNumber}`, XDG_SESSION_TYPE: 'x11' },
// Without these the app and mpv would pick the real Wayland session, or
// take the compositor-specific code paths of the desktop running the tests.
unset: [
'WAYLAND_DISPLAY',
'HYPRLAND_INSTANCE_SIGNATURE',
'SWAYSOCK',
'XDG_CURRENT_DESKTOP',
'XDG_SESSION_DESKTOP',
],
},
stop: () => stopProcess(child),
};
}
+106
View File
@@ -0,0 +1,106 @@
import { once } from 'node:events';
import http from 'node:http';
import type { AddressInfo } from 'node:net';
export type AnkiRequest = { action: string; params: Record<string, unknown> };
export type FakeAnki = {
url: string;
/** Every AnkiConnect action received so far, with `multi` batches flattened. */
requests: AnkiRequest[];
stop: () => Promise<void>;
};
export const FAKE_ANKI_DECK = 'E2E';
export const FAKE_ANKI_MODEL = 'Lapis';
const MODEL_FIELDS = [
'Expression',
'ExpressionFurigana',
'ExpressionReading',
'ExpressionAudio',
'SelectionText',
'MainDefinition',
'Sentence',
'SentenceFurigana',
'SentenceAudio',
'Picture',
'Glossary',
'IsWordAndSentenceCard',
'IsClickCard',
'IsSentenceCard',
'IsAudioCard',
'MiscInfo',
];
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
// A recording AnkiConnect stand-in: an empty collection with one deck and one
// Lapis-shaped note type. Scenarios assert on `requests`, not on stored notes.
export async function startFakeAnki(): Promise<FakeAnki> {
const requests: AnkiRequest[] = [];
let nextNoteId = 1000;
const handle = (body: unknown): unknown => {
if (!isRecord(body) || typeof body.action !== 'string') return null;
const params = isRecord(body.params) ? body.params : {};
if (body.action === 'multi') {
return Array.isArray(params.actions) ? params.actions.map(handle) : [];
}
requests.push({ action: body.action, params });
switch (body.action) {
case 'version':
return 6;
case 'deckNames':
return [FAKE_ANKI_DECK];
case 'modelNames':
return [FAKE_ANKI_MODEL];
case 'modelFieldNames':
return MODEL_FIELDS;
case 'addNote':
return nextNoteId++;
case 'findNotes':
case 'notesInfo':
return [];
case 'storeMediaFile':
return params.filename ?? null;
default:
return null;
}
};
const server = http.createServer((request, response) => {
response.setHeader('content-type', 'application/json');
// GET exposes the request log so the session CLI can read it from outside.
if (request.method === 'GET') {
response.end(JSON.stringify(requests));
return;
}
const chunks: Buffer[] = [];
request.on('data', (chunk: Buffer) => chunks.push(chunk));
request.on('end', () => {
let body: unknown = null;
try {
body = JSON.parse(Buffer.concat(chunks).toString('utf8'));
} catch {
// Malformed bodies get the same null result AnkiConnect gives unknown actions.
}
response.end(JSON.stringify({ result: handle(body), error: null }));
});
});
server.listen(0, '127.0.0.1');
await once(server, 'listening');
const { port } = server.address() as AddressInfo;
return {
url: `http://127.0.0.1:${port}`,
requests,
stop: async () => {
server.closeAllConnections();
server.close();
await once(server, 'close');
},
};
}
+92
View File
@@ -0,0 +1,92 @@
import { execFile } from 'node:child_process';
import fs from 'node:fs';
import { promisify } from 'node:util';
import { writeStoredZipAsync } from '../../src/shared/stored-zip';
const execFileAsync = promisify(execFile);
export const FIXTURE_DICTIONARY_TITLE = 'SubMiner E2E Fixture';
export const FIXTURE_CLIP_SECONDS = 15;
/** Cues of the synthetic clip. Seek to `at` to land inside a cue. */
export const FIXTURE_CUES = [
{ start: 1, end: 4, at: 2, text: '今日はいい天気ですね。' },
{ start: 5, end: 8, at: 6, text: '猫が好きです。' },
{ start: 9, end: 12, at: 10, text: '明日は学校に行きます。' },
] as const;
// [term, reading, part of speech, gloss] for every content word in FIXTURE_CUES.
const FIXTURE_TERMS = [
['今日', 'きょう', 'n', 'today'],
['いい', 'いい', 'adj-i', 'good'],
['天気', 'てんき', 'n', 'weather'],
['猫', 'ねこ', 'n', 'cat'],
['好き', 'すき', 'adj-na', 'liked'],
['明日', 'あした', 'n', 'tomorrow'],
['学校', 'がっこう', 'n', 'school'],
['行く', 'いく', 'v5', 'to go'],
] as const;
function srtTimestamp(seconds: number): string {
return `00:00:${String(seconds).padStart(2, '0')},000`;
}
export function writeFixtureSubtitles(outputPath: string): void {
const blocks = FIXTURE_CUES.map(
(cue, index) =>
`${index + 1}\n${srtTimestamp(cue.start)} --> ${srtTimestamp(cue.end)}\n${cue.text}\n`,
);
fs.writeFileSync(outputPath, blocks.join('\n'), 'utf8');
}
/** Renders a deterministic test-pattern clip with a sine tone, so no real media is needed. */
export async function generateFixtureClip(outputPath: string): Promise<void> {
await execFileAsync('ffmpeg', [
'-nostdin',
'-v',
'error',
'-y',
'-f',
'lavfi',
'-i',
`testsrc2=size=640x360:rate=24:duration=${FIXTURE_CLIP_SECONDS}`,
'-f',
'lavfi',
'-i',
`sine=frequency=440:duration=${FIXTURE_CLIP_SECONDS}`,
'-c:v',
'libx264',
'-preset',
'ultrafast',
'-pix_fmt',
'yuv420p',
'-c:a',
'aac',
outputPath,
]);
}
/** Writes a minimal Yomitan term dictionary so the tokenizer has real entries to match. */
export async function buildFixtureDictionary(outputPath: string): Promise<void> {
const index = {
title: FIXTURE_DICTIONARY_TITLE,
revision: '1',
format: 3,
author: 'SubMiner',
description: 'Synthetic dictionary for the e2e harness.',
};
const termBank = FIXTURE_TERMS.map(([term, reading, partOfSpeech, gloss], sequence) => [
term,
reading,
'',
partOfSpeech,
0,
[gloss],
sequence + 1,
'',
]);
await writeStoredZipAsync(outputPath, [
{ name: 'index.json', data: Buffer.from(JSON.stringify(index), 'utf8') },
{ name: 'term_bank_1.json', data: Buffer.from(JSON.stringify(termBank), 'utf8') },
]);
}
+95
View File
@@ -0,0 +1,95 @@
import { spawn, type ChildProcess } from 'node:child_process';
import { once } from 'node:events';
import fs from 'node:fs';
import net from 'node:net';
import type { E2eDisplay } from './display';
import { E2E_SCREEN } from './display';
import { applyEnvDelta } from './process';
import { waitUntil } from './wait';
export type MpvClient = {
/** Sends one mpv JSON IPC command and resolves with its `data` field. */
command: (...args: unknown[]) => Promise<unknown>;
close: () => void;
};
type MpvReply = { request_id?: number; error?: string; data?: unknown };
export async function startMpv(options: {
socketPath: string;
mediaPath: string;
subtitlePath: string;
logPath: string;
display: E2eDisplay;
}): Promise<ChildProcess> {
const log = fs.openSync(options.logPath, 'w');
const child = spawn(
'mpv',
[
'--no-config',
'--no-terminal',
'--pause',
'--keep-open=yes',
'--ao=null',
`--geometry=${E2E_SCREEN.width}x${E2E_SCREEN.height}+0+0`,
// Xvfb has no GPU; the software X11 output always works there.
...(options.display.kind === 'xvfb' ? ['--vo=x11'] : []),
`--input-ipc-server=${options.socketPath}`,
`--sub-file=${options.subtitlePath}`,
options.mediaPath,
],
{ env: applyEnvDelta(process.env, options.display.env), stdio: ['ignore', log, log] },
);
fs.closeSync(log);
await Promise.race([
once(child, 'spawn'),
once(child, 'error').then(([error]) => {
throw new Error(`Could not start mpv (${String(error)}). Is it on PATH?`);
}),
]);
return child;
}
function tryConnect(socketPath: string): Promise<net.Socket | null> {
return new Promise((resolve) => {
const socket = net.connect(socketPath);
socket.once('connect', () => resolve(socket));
socket.once('error', () => resolve(null));
});
}
export async function connectMpv(socketPath: string): Promise<MpvClient> {
const socket = await waitUntil(() => tryConnect(socketPath), {
description: `mpv IPC socket ${socketPath}`,
});
const pending = new Map<number, (reply: MpvReply) => void>();
let nextRequestId = 1;
let buffer = '';
socket.setEncoding('utf8');
socket.on('data', (chunk: string) => {
buffer += chunk;
const lines = buffer.split('\n');
buffer = lines.pop() ?? '';
for (const line of lines) {
if (!line.trim()) continue;
const reply: MpvReply = JSON.parse(line);
if (reply.request_id === undefined) continue;
pending.get(reply.request_id)?.(reply);
pending.delete(reply.request_id);
}
});
return {
command: (...args) =>
new Promise((resolve, reject) => {
const requestId = nextRequestId++;
pending.set(requestId, (reply) => {
if (reply.error === 'success') resolve(reply.data);
else reject(new Error(`mpv ${JSON.stringify(args)} failed: ${reply.error}`));
});
socket.write(`${JSON.stringify({ command: args, request_id: requestId })}\n`);
}),
close: () => socket.destroy(),
};
}
+24
View File
@@ -0,0 +1,24 @@
import type { ChildProcess } from 'node:child_process';
import { once } from 'node:events';
export type EnvDelta = { set: Record<string, string>; unset: string[] };
export function applyEnvDelta(base: NodeJS.ProcessEnv, ...deltas: EnvDelta[]): NodeJS.ProcessEnv {
const env = { ...base };
for (const delta of deltas) {
for (const name of delta.unset) delete env[name];
Object.assign(env, delta.set);
}
return env;
}
/** Terminates a child and waits for it to exit, escalating to SIGKILL after `graceMs`. */
export async function stopProcess(child: ChildProcess, graceMs = 5_000): Promise<void> {
// A child that never spawned (missing binary) has no pid and never emits exit.
if (child.pid === undefined || child.exitCode !== null || child.signalCode !== null) return;
const exited = once(child, 'exit');
child.kill();
const timer = setTimeout(() => child.kill('SIGKILL'), graceMs);
await exited;
clearTimeout(timer);
}
+332
View File
@@ -0,0 +1,332 @@
import { spawn, type ChildProcess } from 'node:child_process';
import { once } from 'node:events';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { connectCdp, connectCdpPage, listCdpTargets, type CdpPage } from './cdp';
import { startDisplay, type E2eDisplay } from './display';
import { FAKE_ANKI_DECK, FAKE_ANKI_MODEL, startFakeAnki, type FakeAnki } from './fake-anki';
import { buildFixtureDictionary, generateFixtureClip, writeFixtureSubtitles } from './fixtures';
import { connectMpv, startMpv, type MpvClient } from './mpv';
import { applyEnvDelta, stopProcess, type EnvDelta } from './process';
import { waitUntil } from './wait';
import {
shouldForceX11ElectronBackend,
X11_ELECTRON_BOOTSTRAP_ENV,
} from '../../src/core/utils/electron-backend';
const repoRoot = path.resolve(__dirname, '..', '..');
/** URL fragments identifying the app's windows among the CDP targets. The modal
* window loads the same document with `layer=modal`, so the layer is required. */
export const OVERLAY_PAGE = 'renderer/index.html?layer=visible';
const YOMITAN_SETTINGS_PAGE = 'settings.html';
type JsonObject = { [key: string]: JsonValue };
type JsonValue = string | number | boolean | null | JsonValue[] | JsonObject;
export type E2eSessionOptions = {
/** Deep-merged over the harness config, for scenarios that need other settings. */
config?: JsonObject;
};
export type AppTarget = {
/** Environment changes that point a process at the isolated instance. */
env: EnvDelta;
/** Electron/Chromium switches every launch needs. */
electronArgs: string[];
/** Receives stdout and stderr of every launch. */
logPath: string;
};
export type E2eSession = {
/** Temp dir holding the isolated config, fixtures, sockets, and logs. */
root: string;
display: E2eDisplay['kind'];
cdpPort: number;
/** DevTools endpoint of the Electron main process (Node inspector). */
inspectorUrl: string;
mpvSocketPath: string;
/** How to start a process that talks to this isolated instance. */
target: AppTarget;
mpv: MpvClient;
anki: FakeAnki;
/** Connects to the first window or frame whose URL contains `urlPart`, waiting for it to exist. */
openPage: (urlPart: string) => Promise<CdpPage>;
/** Evaluates in the main process; reach modules via `process.mainModule.require`. */
mainProcess: () => Promise<CdpPage>;
/** Runs a CLI command (e.g. `--mine-sentence`) against the running instance. */
runAppCommand: (...args: string[]) => Promise<void>;
dispose: () => Promise<void>;
};
function isJsonObject(value: JsonValue | undefined): value is JsonObject {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function mergeConfig(base: JsonObject, overrides: JsonObject): JsonObject {
const merged = { ...base };
for (const [key, value] of Object.entries(overrides)) {
const existing = merged[key];
merged[key] =
isJsonObject(existing) && isJsonObject(value) ? mergeConfig(existing, value) : value;
}
return merged;
}
// Everything that could reach outside the sandbox (network services, the real
// Anki, ports the user's own SubMiner holds) is off or pointed at a fake.
function buildConfig(mpvSocketPath: string, ankiUrl: string): JsonObject {
return {
mpv: { socketPath: mpvSocketPath, aniskipEnabled: false },
ankiConnect: {
url: ankiUrl,
deck: FAKE_ANKI_DECK,
proxy: { enabled: false },
isLapis: { enabled: true, sentenceCardModel: FAKE_ANKI_MODEL },
},
stats: { autoStartServer: false },
updates: { enabled: false },
discordPresence: { enabled: false },
websocket: { enabled: false },
annotationWebsocket: { enabled: false },
texthooker: { launchAtStartup: false },
};
}
// Outside Electron the package's entry point is the path of its binary, and
// resolving it downloads the binary when an install skipped that step (CI).
function resolveElectronBinary(): string {
const binary: unknown = require('electron');
if (typeof binary !== 'string') throw new Error('Could not resolve the Electron binary path');
return binary;
}
// Electron announces its main-process inspector on stderr; `offset` skips earlier launches.
function readInspectorUrl(logPath: string, byteOffset: number): string | null {
const tail = fs.readFileSync(logPath).subarray(byteOffset).toString('utf8');
return /Debugger listening on (ws:\/\/\S+)/.exec(tail)?.[1] ?? null;
}
function readDevToolsPort(portFile: string): number | null {
try {
const port = Number(fs.readFileSync(portFile, 'utf8').split('\n')[0]);
return port > 0 ? port : null;
} catch {
return null;
}
}
function spawnApp(target: AppTarget, electronArgs: string[], args: string[]): ChildProcess {
const log = fs.openSync(target.logPath, 'a');
const child = spawn(
resolveElectronBinary(),
[...target.electronArgs, ...electronArgs, repoRoot, ...args],
{ cwd: repoRoot, env: applyEnvDelta(process.env, target.env), stdio: ['ignore', log, log] },
);
fs.closeSync(log);
return child;
}
/** Runs a CLI command against the running instance and waits for it to finish. */
export async function runAppCommand(target: AppTarget, args: string[]): Promise<void> {
const child = spawnApp(target, [], args);
const timer = setTimeout(() => child.kill('SIGKILL'), 30_000);
const [code] = await once(child, 'exit');
clearTimeout(timer);
if (code !== 0) {
throw new Error(
`SubMiner ${args.join(' ')} exited with ${String(code)}; see ${target.logPath}`,
);
}
}
async function openPage(cdpPort: number, urlPart: string): Promise<CdpPage> {
const target = await waitUntil(
async () =>
(await listCdpTargets(cdpPort)).find((candidate) => candidate.url.includes(urlPart)),
{ description: `app window matching "${urlPart}"`, timeoutMs: 30_000 },
);
const page = await connectCdpPage(target);
// A target is listed before its document exists; callers expect a usable DOM.
await page.waitFor(`document.readyState === "complete"`, {
description: `"${urlPart}" document to load`,
});
return page;
}
export async function startE2eSession(options: E2eSessionOptions = {}): Promise<E2eSession> {
if (!fs.existsSync(path.join(repoRoot, 'dist', 'main-entry.js'))) {
throw new Error('dist/ is missing. Run `bun run build` before the e2e harness.');
}
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-e2e-'));
const configHome = path.join(root, 'config');
const userDataDir = path.join(configHome, 'SubMiner');
const mediaPath = path.join(root, 'clip.mkv');
const subtitlePath = path.join(root, 'clip.srt');
const dictionaryPath = path.join(root, 'fixture-dictionary.zip');
const mpvSocketPath =
process.platform === 'win32'
? `\\\\.\\pipe\\${path.basename(root)}-mpv`
: path.join(root, 'mpv.sock');
// Teardown steps, run in reverse. Also used to unwind a failed startup.
const teardown: Array<() => Promise<void> | void> = [];
const dispose = async (): Promise<void> => {
for (const step of teardown.reverse()) await step();
teardown.length = 0;
if (process.env.SUBMINER_E2E_KEEP !== '1') {
fs.rmSync(root, { recursive: true, force: true });
}
};
try {
fs.mkdirSync(userDataDir, { recursive: true });
writeFixtureSubtitles(subtitlePath);
await Promise.all([generateFixtureClip(mediaPath), buildFixtureDictionary(dictionaryPath)]);
const display = await startDisplay();
teardown.push(display.stop);
const anki = await startFakeAnki();
teardown.push(anki.stop);
const config = mergeConfig(buildConfig(mpvSocketPath, anki.url), options.config ?? {});
fs.writeFileSync(path.join(userDataDir, 'config.jsonc'), JSON.stringify(config, null, 2));
const env: EnvDelta = {
set: {
...display.env.set,
// The app derives its config dir, Electron profile, and control socket from these.
...(process.platform === 'win32'
? { APPDATA: configHome }
: { XDG_CONFIG_HOME: configHome, XDG_DATA_HOME: path.join(root, 'data') }),
SUBMINER_APP_LOG: path.join(root, 'app.log'),
// Stay attached instead of re-spawning detached, so the harness owns the process.
SUBMINER_BACKGROUND_CHILD: '1',
},
unset: [...display.env.unset, 'ELECTRON_RUN_AS_NODE'],
};
// --enable-logging routes renderer console output into the app log too.
const electronArgs = ['--password-store=basic', '--enable-logging=stderr'];
// Outside Hyprland/Sway the app re-spawns itself detached onto the X11
// backend. Start it there directly so the process we spawn is the app.
if (shouldForceX11ElectronBackend(applyEnvDelta(process.env, env))) {
env.set[X11_ELECTRON_BOOTSTRAP_ENV] = '1';
electronArgs.push('--ozone-platform=x11');
}
// CI containers and runners lack the user namespaces Chromium's sandbox needs.
if (process.platform === 'linux' && process.env.CI) electronArgs.push('--no-sandbox');
const target: AppTarget = { env, electronArgs, logPath: path.join(root, 'app-stdio.log') };
const launchApp = async (
args: string[],
): Promise<{ child: ChildProcess; cdpPort: number; inspectorUrl: string }> => {
// With port 0 Chromium and Node each pick a free port; Chromium records
// its port in the profile dir, Node prints its endpoint to stderr.
const portFile = path.join(userDataDir, 'DevToolsActivePort');
fs.rmSync(portFile, { force: true });
const logOffset = fs.existsSync(target.logPath) ? fs.statSync(target.logPath).size : 0;
const child = spawnApp(
target,
['--remote-debugging-port=0', '--inspect=0'],
[...args, '--log-level', 'debug'],
);
const stop = () => stopProcess(child);
teardown.push(stop);
const cdpPort = await waitUntil(
() => {
if (child.exitCode !== null) {
throw new Error(`SubMiner exited with code ${child.exitCode} during startup`);
}
return readDevToolsPort(portFile);
},
{ description: 'SubMiner DevTools port', timeoutMs: 30_000 },
);
const inspectorUrl = await waitUntil(() => readInspectorUrl(target.logPath, logOffset), {
description: 'SubMiner main-process inspector endpoint',
});
return { child, cdpPort, inspectorUrl };
};
// First boot: import the fixture dictionary through Yomitan's settings page.
// Without one the tokenizer finds nothing and first-run setup takes over.
const seeder = await launchApp(['--yomitan']);
const settings = await openPage(seeder.cdpPort, YOMITAN_SETTINGS_PAGE);
await settings.waitFor('globalThis.__subminerYomitanSettingsAutomation?.ready === true', {
description: 'Yomitan settings automation bridge',
});
const archive = fs.readFileSync(dictionaryPath).toString('base64');
await settings.evaluate(
`globalThis.__subminerYomitanSettingsAutomation
.importDictionaryArchiveBase64(${JSON.stringify(archive)}, "fixture-dictionary.zip")
.then(() => true)`,
);
settings.close();
await stopProcess(seeder.child);
const mpvProcess = await startMpv({
socketPath: mpvSocketPath,
mediaPath,
subtitlePath,
logPath: path.join(root, 'mpv.log'),
display,
});
teardown.push(() => stopProcess(mpvProcess));
const mpv = await connectMpv(mpvSocketPath);
teardown.push(mpv.close);
const app = await launchApp(['--start']);
// mpv starts paused and the app resumes it once the overlay and tokenizer
// are ready, so that resume marks the end of startup.
await waitUntil(async () => (await mpv.command('get_property', 'pause')) === false, {
description: 'SubMiner to resume playback after overlay startup',
timeoutMs: 30_000,
});
// Hand scenarios a paused player. The app retries its resume for a moment,
// so keep pausing until the pause has held for longer than that retry window.
let pausedSince: number | null = null;
await waitUntil(
async () => {
if ((await mpv.command('get_property', 'pause')) !== true) {
pausedSince = null;
await mpv.command('set_property', 'pause', true);
return false;
}
pausedSince ??= Date.now();
return Date.now() - pausedSince >= 1_500;
},
{ description: 'playback to stay paused after startup', timeoutMs: 40_000 },
);
const pages: CdpPage[] = [];
teardown.push(() => pages.forEach((page) => page.close()));
return {
root,
display: display.kind,
cdpPort: app.cdpPort,
inspectorUrl: app.inspectorUrl,
mpvSocketPath,
target,
mpv,
anki,
openPage: async (urlPart) => {
const page = await openPage(app.cdpPort, urlPart);
pages.push(page);
return page;
},
mainProcess: async () => {
const page = await connectCdp(app.inspectorUrl, 'main process');
pages.push(page);
return page;
},
runAppCommand: (...args) => runAppCommand(target, args),
dispose,
};
} catch (error) {
// Keep the logs: a failed startup is exactly when they are needed.
process.env.SUBMINER_E2E_KEEP = '1';
await dispose();
throw new Error(`E2E session failed to start (logs kept in ${root})`, { cause: error });
}
}
+23
View File
@@ -0,0 +1,23 @@
export type WaitOptions = {
description: string;
timeoutMs?: number;
intervalMs?: number;
};
export type Truthy<T> = Exclude<T, false | 0 | '' | null | undefined>;
/** Polls `probe` until it returns a truthy value, then returns that value. */
export async function waitUntil<T>(
probe: () => T | Promise<T>,
{ description, timeoutMs = 15_000, intervalMs = 100 }: WaitOptions,
): Promise<Truthy<T>> {
const deadline = Date.now() + timeoutMs;
for (;;) {
const value = await probe();
if (value) return value as Truthy<T>;
if (Date.now() >= deadline) {
throw new Error(`Timed out after ${timeoutMs}ms waiting for ${description}`);
}
await new Promise((resolve) => setTimeout(resolve, intervalMs));
}
}
+245
View File
@@ -0,0 +1,245 @@
import assert from 'node:assert/strict';
import test, { after } from 'node:test';
import { connectCdpPage, listCdpTargets, type CdpPage } from './harness/cdp';
import { FAKE_ANKI_DECK, FAKE_ANKI_MODEL } from './harness/fake-anki';
import { FIXTURE_CUES } from './harness/fixtures';
import { OVERLAY_PAGE, startE2eSession, type E2eSession } from './harness/session';
import { waitUntil } from './harness/wait';
type Started = { session: E2eSession; overlay: CdpPage };
// Bun caps node:test hooks at 5s whatever their timeout option says, so the
// session boots inside whichever test runs first and every test budgets for it.
const TEST_TIMEOUT_MS = 120_000;
let started: Promise<Started> | undefined;
function useSession(): Promise<Started> {
started ??= (async () => {
const session = await startE2eSession();
try {
return { session, overlay: await session.openPage(OVERLAY_PAGE) };
} catch (error) {
await session.dispose();
throw error;
}
})();
return started;
}
after(async () => {
const running = await started?.catch(() => undefined);
await running?.session.dispose();
});
type RenderedToken = { surface: string; headword: string | null };
// Parks playback inside a cue and waits for the overlay to show that line.
async function showCue(cue: (typeof FIXTURE_CUES)[number]): Promise<RenderedToken[]> {
const { session, overlay } = await useSession();
await session.mpv.command('seek', cue.at, 'absolute+exact');
// One expression reads the line and its tokens together, so a re-render
// between two round trips cannot hand back a half-updated subtitle.
return overlay
.waitFor<RenderedToken[] | false>(
`(() => {
const root = document.getElementById('subtitleRoot');
const words = Array.from(root.querySelectorAll('.word'), (word) => ({
surface: word.textContent,
headword: word.getAttribute('data-headword'),
}));
return root.textContent === ${JSON.stringify(cue.text)} && words.length > 0 && words;
})()`,
{ description: `overlay to render "${cue.text}" tokenized` },
)
.catch(async (error: unknown) => {
// Say what the overlay was showing, what main thinks the line is, and
// what the renderer logged, so a dropped update can be placed.
const state = await overlay.evaluate<string>(
`(async () => JSON.stringify({
visibility: document.visibilityState,
text: document.getElementById('subtitleRoot').textContent,
html: document.getElementById('subtitleRoot').innerHTML.slice(0, 200),
mainLine: (await window.electronAPI.getCurrentSubtitle())?.text,
errorToast: document.getElementById('overlayErrorToast')?.textContent,
}))()`,
);
// Diagnostics must not replace the cue failure if the inspector is gone too.
const windows = await session
.mainProcess()
.then((main) =>
main.evaluate<string>(
`JSON.stringify(process.mainModule.require('electron').BrowserWindow.getAllWindows().map((w) => ({
id: w.id,
title: w.getTitle(),
url: w.webContents.getURL().split('/').pop(),
visible: w.isVisible(),
loading: w.webContents.isLoading(),
documentLoaded: w.__subminerOverlayDocumentLoaded,
contentReady: w.__subminerOverlayContentReady,
})))`,
),
)
.catch((inspectorError: unknown) => `unavailable (${String(inspectorError)})`);
const targets = (await listCdpTargets(session.cdpPort))
.filter((t) => t.type === 'page')
.map((t) => t.url.split('/').pop());
const log = overlay.console.slice(-20).join('\n');
throw new Error(
`Overlay state: ${state}\nMain windows: ${windows}\nPage targets: ${JSON.stringify(targets)}\nRenderer console:\n${log}`,
{ cause: error },
);
});
}
function hasToken(tokens: RenderedToken[], surface: string, headword: string): boolean {
return tokens.some((token) => token.surface === surface && token.headword === headword);
}
test(
'overlay renders the current mpv subtitle line as dictionary tokens',
{ timeout: TEST_TIMEOUT_MS },
async () => {
const tokens = await showCue(FIXTURE_CUES[0]);
assert.ok(hasToken(tokens, '今日', '今日'), JSON.stringify(tokens));
assert.ok(hasToken(tokens, '天気', '天気'), JSON.stringify(tokens));
},
);
test(
'seeking to another cue replaces the line and deinflects verbs to their headword',
{ timeout: TEST_TIMEOUT_MS },
async () => {
const tokens = await showCue(FIXTURE_CUES[2]);
assert.ok(hasToken(tokens, '行きます', '行く'), JSON.stringify(tokens));
},
);
test(
'mining a sentence card sends the line with generated audio and image to Anki',
{ timeout: TEST_TIMEOUT_MS },
async () => {
const { session } = await useSession();
const cue = FIXTURE_CUES[1];
await showCue(cue);
session.anki.requests.length = 0;
await session.runAppCommand('--mine-sentence');
const update = await waitUntil(
() => session.anki.requests.find((request) => request.action === 'updateNoteFields'),
{ description: 'media fields to be written to the mined note', timeoutMs: 30_000 },
);
const added = session.anki.requests.filter((request) => request.action === 'addNote');
assert.equal(added.length, 1);
assert.partialDeepStrictEqual(added[0]?.params.note, {
deckName: FAKE_ANKI_DECK,
modelName: FAKE_ANKI_MODEL,
fields: { Sentence: cue.text },
});
const stored = session.anki.requests
.filter((request) => request.action === 'storeMediaFile')
.map((request) => String(request.params.filename));
assert.ok(
stored.some((name) => name.endsWith('.mp3')),
`no audio in ${stored.join(', ')}`,
);
assert.ok(
stored.some((name) => name.endsWith('.jpg')),
`no image in ${stored.join(', ')}`,
);
const updatedFields = JSON.stringify(update.params);
for (const filename of stored) assert.ok(updatedFields.includes(filename), filename);
},
);
test(
'open subtitle sidebar lists the parsed cues and follows playback',
{ timeout: TEST_TIMEOUT_MS },
async () => {
const { session, overlay } = await useSession();
await showCue(FIXTURE_CUES[0]);
await session.runAppCommand('--toggle-subtitle-sidebar');
await overlay.waitFor(
`document.querySelectorAll('#subtitleSidebarList .subtitle-sidebar-item').length === ${FIXTURE_CUES.length}`,
{ description: 'sidebar to list every fixture cue' },
);
for (const [index, cue] of FIXTURE_CUES.entries()) {
await session.mpv.command('seek', cue.at, 'absolute+exact');
await overlay.waitFor(
`document.querySelector('#subtitleSidebarList .subtitle-sidebar-item.active')?.dataset.index === '${index}'`,
{ description: `sidebar to highlight cue ${index}` },
);
}
await session.runAppCommand('--toggle-subtitle-sidebar');
await overlay.waitFor(
`document.getElementById('subtitleSidebarModal').classList.contains('hidden')`,
{ description: 'sidebar to close' },
);
},
);
// Runs last: it leaves the lookup popup open over the overlay.
test(
'holding Shift over a word opens the Yomitan popup with its dictionary entry',
{ timeout: TEST_TIMEOUT_MS },
async () => {
const { session, overlay } = await useSession();
await showCue(FIXTURE_CUES[0]);
const word = await overlay.evaluate<{ x: number; y: number }>(
`(() => {
const words = Array.from(document.querySelectorAll('#subtitleRoot .word'));
const box = words.find((word) => word.textContent === '天気').getBoundingClientRect();
return { x: box.x + box.width / 2, y: box.y + box.height / 2 };
})()`,
);
// Yomitan's default scan modifier is Shift, and it scans on mouse movement,
// so approach the word, hold Shift, then move across it.
const shift = { key: 'Shift', code: 'ShiftLeft', windowsVirtualKeyCode: 16 };
const move = (x: number, modifiers = 0) =>
overlay.send('Input.dispatchMouseEvent', { type: 'mouseMoved', x, y: word.y, modifiers });
await move(word.x - 30);
await overlay.send('Input.dispatchKeyEvent', { type: 'rawKeyDown', ...shift, modifiers: 8 });
await move(word.x - 2, 8);
await move(word.x, 8);
await overlay.waitFor(
`document.querySelector('[data-subminer-yomitan-popup-visible="true"]') !== null`,
{ description: 'overlay to mark the Yomitan popup visible' },
);
// Yomitan keeps more than one popup frame around, so read whichever
// one holds the entry instead of trusting target order.
let seen: string[] = [];
const entry = await waitUntil(
async () => {
const targets = (await listCdpTargets(session.cdpPort)).filter((target) =>
target.url.includes('popup.html'),
);
seen = [];
for (const target of targets) {
// A frame can go away between listing and connecting; skip it.
const frame = await connectCdpPage(target).catch(() => undefined);
if (!frame) continue;
// textContent, not innerText: the entry counts once it is in the DOM,
// even where a GPU-less runner never lays the frame out.
const text = await frame.evaluate<string>('document.body?.textContent ?? ""');
frame.close();
seen.push(text);
}
return seen.find((text) => text.includes('weather'));
},
{ description: 'a popup frame to show the fixture dictionary entry' },
).catch((error: unknown) => {
const summary = seen.map((text) => text.replace(/\s+/g, ' ').trim().slice(0, 200));
throw new Error(`Popup frames held: ${JSON.stringify(summary)}`, { cause: error });
});
await overlay.send('Input.dispatchKeyEvent', { type: 'keyUp', ...shift });
assert.match(entry, /天気/);
},
);
+82 -75
View File
@@ -1,7 +1,5 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {
buildForwardedJellyfinAppArgs,
@@ -11,25 +9,16 @@ import {
hasStoredJellyfinSession,
parseEpisodePathFromDisplay,
parseJellyfinErrorFromAppOutput,
parseJellyfinItemsFromAppOutput,
parseJellyfinLibrariesFromAppOutput,
parseJellyfinAppReply,
parseJellyfinItemsReply,
parseJellyfinLibrariesReply,
parseJellyfinPreviewAuthResponse,
readUtf8FileAppendedSince,
runJellyfinPlayMenuWithDeps,
shouldRetryWithStartForNoRunningInstance,
} from './jellyfin.js';
import { makeLauncherArgs } from './test-support/args.js';
import { withEnv } from './test-support/env.js';
function withTempDir<T>(fn: (dir: string) => T): T {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-jellyfin-test-'));
try {
return fn(dir);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
}
class HandoffComplete extends Error {}
type PlayMenuDeps = NonNullable<Parameters<typeof runJellyfinPlayMenuWithDeps>[4]>;
@@ -68,6 +57,16 @@ async function runPlayMenu(options: {
handoff.appArgs = appArgs;
throw new HandoffComplete();
},
isAppManagedByLauncher: () => false,
runAppCommandCaptureOutput: () => {
throw new Error('unexpected owned-app handoff');
},
waitForUnixSocketClosed: async () => {
throw new Error('unexpected wait for mpv to close');
},
exitProcess: () => {
throw new Error('unexpected exit');
},
log: () => {},
...options.overrides,
};
@@ -105,41 +104,71 @@ test('Jellyfin playback reuses a ready mpv socket instead of launching idle mpv'
assert.deepEqual(appArgs, ['--start', '--jellyfin-play', '--jellyfin-item-id=item-123']);
});
test('parseJellyfinLibrariesFromAppOutput parses prefixed library lines', () => {
const parsed = parseJellyfinLibrariesFromAppOutput(`
[subminer] - 2026-03-01 13:10:34 - INFO - [main] Jellyfin library: Anime [lib1] (tvshows)
[subminer] - 2026-03-01 13:10:35 - INFO - [main] Jellyfin library: Movies [lib2] (movies)
`);
test('Jellyfin playback in an app the picker started waits for mpv to close before exiting', async () => {
const events: string[] = [];
const { appArgs } = await runPlayMenu({
socketReady: true,
overrides: {
isAppManagedByLauncher: () => true,
runAppCommandCaptureOutput: (_appPath, appArgs) => {
events.push(`handoff ${appArgs.join(' ')}`);
return { status: 0, stdout: '', stderr: '' };
},
waitForUnixSocketClosed: async (socketPath) => {
events.push(`wait ${socketPath}`);
},
exitProcess: (code) => {
events.push(`exit ${code}`);
throw new HandoffComplete();
},
},
});
assert.deepEqual(parsed, [
assert.equal(appArgs, null);
assert.deepEqual(events, [
'handoff --start --jellyfin-play --jellyfin-item-id=item-123',
'wait /tmp/subminer.sock',
'exit 0',
]);
});
test('parseJellyfinAppReply maps library and item replies to picker entries', () => {
const libraries = parseJellyfinAppReply(
JSON.stringify({
libraries: [
{ id: 'lib1', name: 'Anime', collectionType: 'tvshows' },
{ id: 'lib1', name: 'Duplicate', collectionType: 'tvshows' },
{ name: 'No id' },
],
}),
);
assert.ok(libraries?.ok);
assert.deepEqual(parseJellyfinLibrariesReply(libraries.payload), [
{ id: 'lib1', name: 'Anime', kind: 'tvshows' },
{ id: 'lib2', name: 'Movies', kind: 'movies' },
]);
const items = parseJellyfinAppReply(
JSON.stringify({ items: [{ id: 'movie-1', title: 'Movie [Alt]', type: 'Movie' }] }),
);
assert.ok(items?.ok);
assert.deepEqual(parseJellyfinItemsReply(items.payload), [
{ id: 'movie-1', name: 'Movie [Alt]', type: 'Movie', display: 'Movie [Alt]' },
]);
assert.equal(parseJellyfinLibrariesReply(items.payload), null);
});
test('parseJellyfinItemsFromAppOutput parses item title/id/type tuples', () => {
const parsed = parseJellyfinItemsFromAppOutput(`
[subminer] - 2026-03-01 13:10:34 - INFO - [main] Jellyfin item: Solo Leveling S01E10 [item-10] (Episode)
[subminer] - 2026-03-01 13:10:35 - INFO - [main] Jellyfin item: Movie [Alt] [movie-1] (Movie)
`);
assert.deepEqual(parsed, [
{
id: 'item-10',
name: 'Solo Leveling S01E10',
type: 'Episode',
display: 'Solo Leveling S01E10',
},
{
id: 'movie-1',
name: 'Movie [Alt]',
type: 'Movie',
display: 'Movie [Alt]',
},
]);
test('parseJellyfinAppReply surfaces app errors and rejects partial files', () => {
assert.deepEqual(
parseJellyfinAppReply(
JSON.stringify({ error: 'Missing Jellyfin session. Run --jellyfin-login first.' }),
),
{ ok: false, error: 'Missing Jellyfin session. Run `subminer jellyfin -l` to log in again.' },
);
assert.equal(parseJellyfinAppReply(''), null);
assert.equal(parseJellyfinAppReply('{"libraries": ['), null);
});
test('buildForwardedJellyfinAppArgs forces app log level for parseable list output', () => {
test('buildForwardedJellyfinAppArgs appends server, password store, and log level', () => {
const forwarded = buildForwardedJellyfinAppArgs(
{
jellyfinServer: 'https://jf.example.test/',
@@ -184,13 +213,11 @@ test('parseJellyfinErrorFromAppOutput extracts main runtime error lines', () =>
});
test('parseJellyfinPreviewAuthResponse parses valid structured response payload', () => {
const parsed = parseJellyfinPreviewAuthResponse(
JSON.stringify({
serverUrl: 'http://pve-main:8096/',
accessToken: 'token-123',
userId: 'user-1',
}),
);
const parsed = parseJellyfinPreviewAuthResponse({
serverUrl: 'http://pve-main:8096/',
accessToken: 'token-123',
userId: 'user-1',
});
assert.deepEqual(parsed, {
serverUrl: 'http://pve-main:8096',
@@ -199,17 +226,13 @@ test('parseJellyfinPreviewAuthResponse parses valid structured response payload'
});
});
test('parseJellyfinPreviewAuthResponse returns null for invalid payloads', () => {
assert.equal(parseJellyfinPreviewAuthResponse(''), null);
assert.equal(parseJellyfinPreviewAuthResponse('{not json}'), null);
test('parseJellyfinPreviewAuthResponse returns null without a token', () => {
assert.equal(
parseJellyfinPreviewAuthResponse(
JSON.stringify({
serverUrl: 'http://pve-main:8096',
accessToken: '',
userId: 'user-1',
}),
),
parseJellyfinPreviewAuthResponse({
serverUrl: 'http://pve-main:8096',
accessToken: '',
userId: 'user-1',
}),
null,
);
});
@@ -238,22 +261,6 @@ test('shouldRetryWithStartForNoRunningInstance matches expected app lifecycle er
);
});
test('readUtf8FileAppendedSince treats offset as bytes and survives multibyte logs', () => {
withTempDir((root) => {
const logPath = path.join(root, 'SubMiner.log');
const prefix = '[subminer] こんにちは\n';
const suffix = '[subminer] Jellyfin library: Movies [lib2] (movies)\n';
fs.writeFileSync(logPath, `${prefix}${suffix}`, 'utf8');
const byteOffset = Buffer.byteLength(prefix, 'utf8');
const fromByteOffset = readUtf8FileAppendedSince(logPath, byteOffset);
assert.match(fromByteOffset, /Jellyfin library: Movies \[lib2\] \(movies\)/);
const fromBeyondEnd = readUtf8FileAppendedSince(logPath, byteOffset + 9999);
assert.match(fromBeyondEnd, /Jellyfin library: Movies \[lib2\] \(movies\)/);
});
});
test('parseEpisodePathFromDisplay extracts series and season from episode display titles', () => {
assert.deepEqual(
parseEpisodePathFromDisplay('KONOSUBA S01E03 A Panty Treasure in This Right Hand!'),
+195 -222
View File
@@ -10,7 +10,8 @@ import type {
JellyfinGroupEntry,
} from './types.js';
import { shouldForwardLogLevel } from './types.js';
import { log, fail, getMpvLogPath } from './log.js';
import type { JellyfinPreviewAuthPayload } from '../src/shared/jellyfin-cli-response.js';
import { log, fail } from './log.js';
import { nowMs } from './time.js';
import { commandExists, resolvePathMaybe, sleep } from './util.js';
import {
@@ -23,11 +24,14 @@ import {
import { loadLauncherJellyfinConfig } from './config.js';
import { resolveLauncherMainConfigPath } from './config/shared-config-reader.js';
import {
adoptLauncherStartedApp,
isAppManagedByLauncher,
runAppCommandWithInheritLogged,
runAppCommandCaptureOutput,
launchAppStartDetached,
launchAppBackgroundDetached,
launchMpvIdleDetached,
resolveLauncherRuntimePluginPath,
waitForUnixSocketClosed,
waitForUnixSocketReady,
} from './mpv.js';
import { ensureLinuxRuntimePluginAvailable } from './runtime-plugin-preflight.js';
@@ -52,6 +56,10 @@ type JellyfinPlayMenuDeps = {
launchMpvIdleDetached: typeof launchMpvIdleDetached;
resolveLauncherRuntimePluginPath: typeof resolveLauncherRuntimePluginPath;
runAppCommandWithInheritLogged: typeof runAppCommandWithInheritLogged;
isAppManagedByLauncher: typeof isAppManagedByLauncher;
runAppCommandCaptureOutput: typeof runAppCommandCaptureOutput;
waitForUnixSocketClosed: typeof waitForUnixSocketClosed;
exitProcess: (code: number) => never;
log: typeof log;
};
@@ -75,6 +83,10 @@ const defaultJellyfinPlayMenuDeps: JellyfinPlayMenuDeps = {
launchMpvIdleDetached,
resolveLauncherRuntimePluginPath,
runAppCommandWithInheritLogged,
isAppManagedByLauncher,
runAppCommandCaptureOutput,
waitForUnixSocketClosed,
exitProcess: (code) => process.exit(code),
log,
};
@@ -171,76 +183,77 @@ function stripAnsi(value: string): string {
return value.replace(ANSI_ESCAPE_PATTERN, '');
}
function parseNamedJellyfinRecord(payload: string): {
name: string;
id: string;
type: string;
} | null {
const typeClose = payload.lastIndexOf(')');
if (typeClose !== payload.length - 1) return null;
// Reply the app writes to --jellyfin-response-path; see src/shared/jellyfin-cli-response.ts.
export type JellyfinAppReply =
| { ok: true; payload: Record<string, unknown> }
| { ok: false; error: string };
const typeOpen = payload.lastIndexOf(' (');
if (typeOpen <= 0 || typeOpen >= typeClose) return null;
const idClose = payload.lastIndexOf(']', typeOpen);
if (idClose <= 0) return null;
const idOpen = payload.lastIndexOf(' [', idClose);
if (idOpen <= 0 || idOpen >= idClose) return null;
const name = payload.slice(0, idOpen).trim();
const id = payload.slice(idOpen + 2, idClose).trim();
const type = payload.slice(typeOpen + 2, typeClose).trim();
if (!name || !id || !type) return null;
return { name, id, type };
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
export function parseJellyfinLibrariesFromAppOutput(output: string): JellyfinLibraryEntry[] {
function readString(record: Record<string, unknown>, key: string): string {
const value = record[key];
return typeof value === 'string' ? value.trim() : '';
}
export function parseJellyfinAppReply(raw: string): JellyfinAppReply | null {
let parsed: unknown;
try {
parsed = JSON.parse(raw);
} catch {
return null;
}
if (!isRecord(parsed)) return null;
const error = readString(parsed, 'error');
if (error) return { ok: false, error: normalizeJellyfinAppError(error) };
return { ok: true, payload: parsed };
}
export function parseJellyfinLibrariesReply(
payload: Record<string, unknown>,
): JellyfinLibraryEntry[] | null {
if (!Array.isArray(payload.libraries)) return null;
const libraries: JellyfinLibraryEntry[] = [];
const seenIds = new Set<string>();
for (const rawLine of output.split(/\r?\n/)) {
const line = stripAnsi(rawLine);
const markerIndex = line.indexOf('Jellyfin library:');
if (markerIndex < 0) continue;
const payload = line.slice(markerIndex + 'Jellyfin library:'.length).trim();
const parsed = parseNamedJellyfinRecord(payload);
if (!parsed || seenIds.has(parsed.id)) continue;
seenIds.add(parsed.id);
for (const entry of payload.libraries) {
if (!isRecord(entry)) continue;
const id = readString(entry, 'id');
if (!id || seenIds.has(id)) continue;
seenIds.add(id);
libraries.push({
id: parsed.id,
name: parsed.name,
kind: parsed.type,
id,
name: readString(entry, 'name') || 'Untitled',
kind: readString(entry, 'collectionType') || 'unknown',
});
}
return libraries;
}
export function parseJellyfinItemsFromAppOutput(output: string): JellyfinItemEntry[] {
export function parseJellyfinItemsReply(
payload: Record<string, unknown>,
): JellyfinItemEntry[] | null {
if (!Array.isArray(payload.items)) return null;
const items: JellyfinItemEntry[] = [];
const seenIds = new Set<string>();
for (const rawLine of output.split(/\r?\n/)) {
const line = stripAnsi(rawLine);
const markerIndex = line.indexOf('Jellyfin item:');
if (markerIndex < 0) continue;
const payload = line.slice(markerIndex + 'Jellyfin item:'.length).trim();
const parsed = parseNamedJellyfinRecord(payload);
if (!parsed || seenIds.has(parsed.id)) continue;
seenIds.add(parsed.id);
items.push({
id: parsed.id,
name: parsed.name,
type: parsed.type,
display: parsed.name,
});
for (const entry of payload.items) {
if (!isRecord(entry)) continue;
const id = readString(entry, 'id');
if (!id || seenIds.has(id)) continue;
seenIds.add(id);
const title = readString(entry, 'title') || 'Untitled';
items.push({ id, name: title, type: readString(entry, 'type'), display: title });
}
return items;
}
function normalizeJellyfinAppError(message: string): string {
if (message.includes('Missing Jellyfin session')) {
return 'Missing Jellyfin session. Run `subminer jellyfin -l` to log in again.';
}
return message;
}
export function parseJellyfinErrorFromAppOutput(output: string): string {
const lines = output.split(/\r?\n/).map((line) => stripAnsi(line).trim());
for (let i = lines.length - 1; i >= 0; i -= 1) {
@@ -260,41 +273,20 @@ export function parseJellyfinErrorFromAppOutput(output: string): string {
}
if (line.includes('Missing Jellyfin session')) {
return 'Missing Jellyfin session. Run `subminer jellyfin -l` to log in again.';
return normalizeJellyfinAppError(line);
}
}
return '';
}
type JellyfinPreviewAuthResponse = {
serverUrl: string;
accessToken: string;
userId: string;
};
export function parseJellyfinPreviewAuthResponse(raw: string): JellyfinPreviewAuthResponse | null {
if (!raw || raw.trim().length === 0) return null;
let parsed: unknown;
try {
parsed = JSON.parse(raw);
} catch {
return null;
}
if (!parsed || typeof parsed !== 'object') return null;
const candidate = parsed as Record<string, unknown>;
const serverUrl = sanitizeServerUrl(
typeof candidate.serverUrl === 'string' ? candidate.serverUrl : '',
);
const accessToken = typeof candidate.accessToken === 'string' ? candidate.accessToken.trim() : '';
const userId = typeof candidate.userId === 'string' ? candidate.userId.trim() : '';
export function parseJellyfinPreviewAuthResponse(
payload: Record<string, unknown>,
): JellyfinPreviewAuthPayload | null {
const serverUrl = sanitizeServerUrl(readString(payload, 'serverUrl'));
const accessToken = readString(payload, 'accessToken');
const userId = readString(payload, 'userId');
if (!serverUrl || !accessToken) return null;
return {
serverUrl,
accessToken,
userId,
};
return { serverUrl, accessToken, userId };
}
export function shouldRetryWithStartForNoRunningInstance(errorMessage: string): boolean {
@@ -312,19 +304,6 @@ export function hasStoredJellyfinSession(
return exists(deriveJellyfinTokenStorePath(configPath));
}
export function readUtf8FileAppendedSince(logPath: string, offsetBytes: number): string {
try {
const buffer = fs.readFileSync(logPath);
if (buffer.length === 0) return '';
const normalizedOffset =
Number.isFinite(offsetBytes) && offsetBytes >= 0 ? Math.floor(offsetBytes) : 0;
const startOffset = normalizedOffset > buffer.length ? 0 : normalizedOffset;
return buffer.subarray(startOffset).toString('utf8');
} catch {
return '';
}
}
export function parseEpisodePathFromDisplay(
display: string,
): { seriesName: string; seasonNumber: number } | null {
@@ -421,145 +400,79 @@ export function buildForwardedJellyfinAppArgs(args: Args, appArgs: string[]): st
return forwarded;
}
async function runAppJellyfinListCommand(
appPath: string,
args: Args,
appArgs: string[],
label: string,
): Promise<string> {
const attempt = await runAppJellyfinCommand(appPath, args, appArgs, label);
if (attempt.status !== 0) {
const message = attempt.output.trim();
fail(message || `${label} failed.`);
const JELLYFIN_APP_REPLY_TIMEOUT_MS = 60_000;
const JELLYFIN_PREVIEW_AUTH_TIMEOUT_MS = 15_000;
function readJellyfinAppReply(responsePath: string): JellyfinAppReply | null {
try {
return parseJellyfinAppReply(fs.readFileSync(responsePath, 'utf8'));
} catch {
return null;
}
if (attempt.error) {
fail(attempt.error);
}
return attempt.output;
}
async function runAppJellyfinCommand(
// Runs a Jellyfin command in the app and waits for the reply it writes to a temp file. The helper
// process usually hands the command to an already-running instance and exits at once with no
// output, so the reply file is the only result channel that works in both cases.
async function requestJellyfinReplyFromApp(
appPath: string,
args: Args,
appArgs: string[],
label: string,
): Promise<{ status: number; output: string; error: string; logOffset: number }> {
const forwardedBase = buildForwardedJellyfinAppArgs(args, appArgs);
timeoutMs: number,
): Promise<JellyfinAppReply> {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-jf-reply-'));
const responsePath = path.join(tmpDir, 'response.json');
const forwarded = buildForwardedJellyfinAppArgs(args, [
...appArgs,
`--jellyfin-response-path=${responsePath}`,
]);
const readLogAppendedSince = (offset: number): string => {
const logPath = getMpvLogPath();
return readUtf8FileAppendedSince(logPath, offset);
};
const hasCommandSignal = (output: string): boolean => {
if (label === 'jellyfin-libraries') {
return (
output.includes('Jellyfin library:') || output.includes('No Jellyfin libraries found.')
);
}
if (label === 'jellyfin-items') {
return (
output.includes('Jellyfin item:') ||
output.includes('No Jellyfin items found for the selected library/search.')
);
}
if (label === 'jellyfin-preview-auth') {
return output.includes('Jellyfin preview auth written.');
}
return output.trim().length > 0;
};
const runOnce = (): { status: number; output: string; error: string; logOffset: number } => {
const forwarded = [...forwardedBase];
const logPath = getMpvLogPath();
let logOffset = 0;
try {
if (fs.existsSync(logPath)) {
logOffset = fs.statSync(logPath).size;
}
} catch {
logOffset = 0;
}
const runOnce = (): { status: number; output: string; error: string } => {
log('debug', args.logLevel, `${label}: launching app with args: ${forwarded.join(' ')}`);
const result = runAppCommandCaptureOutput(appPath, forwarded);
log('debug', args.logLevel, `${label}: app command exited with status ${result.status}`);
let output = `${result.stdout || ''}\n${result.stderr || ''}\n${readLogAppendedSince(logOffset)}`;
let error = parseJellyfinErrorFromAppOutput(output);
return { status: result.status, output, error, logOffset };
const output = `${result.stdout || ''}\n${result.stderr || ''}`;
return { status: result.status, output, error: parseJellyfinErrorFromAppOutput(output) };
};
let retriedAfterStart = false;
let attempt = runOnce();
if (shouldRetryWithStartForNoRunningInstance(attempt.error)) {
log('debug', args.logLevel, `${label}: starting app detached, then retrying command`);
launchAppStartDetached(appPath, args.logLevel);
await sleep(1000);
retriedAfterStart = true;
attempt = runOnce();
}
if (attempt.status === 0 && !attempt.error && !hasCommandSignal(attempt.output)) {
// When app is already running, command handling happens in the primary process and log
// lines can land slightly after the helper process exits.
const settleWindowMs = (() => {
if (label === 'jellyfin-items') {
return retriedAfterStart ? 45000 : 30000;
}
return retriedAfterStart ? 12000 : 4000;
})();
const settleDeadline = nowMs() + settleWindowMs;
const settleOffset = attempt.logOffset;
while (nowMs() < settleDeadline) {
await sleep(100);
const settledOutput = readLogAppendedSince(settleOffset);
if (!settledOutput.trim()) {
continue;
}
attempt.output = `${attempt.output}\n${settledOutput}`;
attempt.error = parseJellyfinErrorFromAppOutput(attempt.output);
if (attempt.error || hasCommandSignal(attempt.output)) {
break;
}
}
}
return attempt;
}
async function requestJellyfinPreviewAuthFromApp(
appPath: string,
args: Args,
): Promise<JellyfinPreviewAuthResponse | null> {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-jf-preview-auth-'));
const responsePath = path.join(tmpDir, 'response.json');
try {
const attempt = await runAppJellyfinCommand(
appPath,
args,
['--jellyfin-preview-auth', `--jellyfin-response-path=${responsePath}`],
'jellyfin-preview-auth',
);
if (attempt.status !== 0 || attempt.error) {
return null;
let attempt = runOnce();
if (shouldRetryWithStartForNoRunningInstance(attempt.error)) {
// Start it the way `subminer app` does: the app outlives this picker and the playback, so it
// needs its tray to be visible and quittable.
log(
'debug',
args.logLevel,
`${label}: starting app in the background, then retrying command`,
);
launchAppBackgroundDetached(appPath, args.logLevel);
adoptLauncherStartedApp(appPath, args);
// A cold start (an AppImage mount, first-run setup) can take a while to claim the instance
// lock, so keep retrying until the app takes the command or the request deadline passes.
const startDeadline = nowMs() + timeoutMs;
do {
await sleep(1000);
attempt = runOnce();
} while (shouldRetryWithStartForNoRunningInstance(attempt.error) && nowMs() < startDeadline);
}
// A written reply is authoritative; the exit status and log lines only explain a missing one.
const earlyReply = readJellyfinAppReply(responsePath);
if (earlyReply) return earlyReply;
if (attempt.status !== 0) {
return { ok: false, error: attempt.error || attempt.output.trim() || `${label} failed.` };
}
if (attempt.error) {
return { ok: false, error: attempt.error };
}
const deadline = nowMs() + 4000;
const deadline = nowMs() + timeoutMs;
while (nowMs() < deadline) {
try {
if (fs.existsSync(responsePath)) {
const raw = fs.readFileSync(responsePath, 'utf8');
const parsed = parseJellyfinPreviewAuthResponse(raw);
if (parsed) {
return parsed;
}
}
} catch {
// retry until timeout
}
const reply = readJellyfinAppReply(responsePath);
if (reply) return reply;
await sleep(100);
}
return null;
return { ok: false, error: `Timed out waiting for SubMiner to answer ${label}.` };
} finally {
try {
fs.rmSync(tmpDir, { recursive: true, force: true });
@@ -569,19 +482,57 @@ async function requestJellyfinPreviewAuthFromApp(
}
}
async function requestJellyfinPreviewAuthFromApp(
appPath: string,
args: Args,
): Promise<JellyfinPreviewAuthPayload | null> {
const reply = await requestJellyfinReplyFromApp(
appPath,
args,
['--jellyfin-preview-auth'],
'jellyfin-preview-auth',
JELLYFIN_PREVIEW_AUTH_TIMEOUT_MS,
);
if (!reply.ok) {
log('debug', args.logLevel, `jellyfin-preview-auth: ${reply.error}`);
return null;
}
return parseJellyfinPreviewAuthResponse(reply.payload);
}
async function requestJellyfinListFromApp<T>(
appPath: string,
args: Args,
appArgs: string[],
label: string,
parse: (payload: Record<string, unknown>) => T[] | null,
): Promise<T[]> {
const reply = await requestJellyfinReplyFromApp(
appPath,
args,
appArgs,
label,
JELLYFIN_APP_REPLY_TIMEOUT_MS,
);
if (!reply.ok) fail(reply.error);
const entries = parse(reply.payload);
if (!entries) fail(`Unexpected reply from SubMiner for ${label}.`);
return entries;
}
async function resolveJellyfinSelectionViaApp(
appPath: string,
args: Args,
session: JellyfinSessionConfig,
themePath: string | null = null,
): Promise<string> {
const listLibrariesOutput = await runAppJellyfinListCommand(
const libraries = await requestJellyfinListFromApp(
appPath,
args,
['--jellyfin-libraries'],
'jellyfin-libraries',
parseJellyfinLibrariesReply,
);
const libraries = parseJellyfinLibrariesFromAppOutput(listLibrariesOutput);
if (libraries.length === 0) {
fail('No Jellyfin libraries found.');
}
@@ -655,8 +606,13 @@ async function resolveJellyfinSelectionViaApp(
if (includeItemTypes) {
itemArgs.push(`--jellyfin-include-item-types=${includeItemTypes}`);
}
const output = await runAppJellyfinListCommand(appPath, args, itemArgs, 'jellyfin-items');
return parseJellyfinItemsFromAppOutput(output);
return requestJellyfinListFromApp(
appPath,
args,
itemArgs,
'jellyfin-items',
parseJellyfinItemsReply,
);
};
let rootItems =
@@ -1106,5 +1062,22 @@ export async function runJellyfinPlayMenuWithDeps(
const forwarded = ['--start', '--jellyfin-play', `--jellyfin-item-id=${itemId}`];
if (shouldForwardLogLevel(args.logLevel)) forwarded.push('--log-level', args.logLevel);
if (args.passwordStore) forwarded.push('--password-store', args.passwordStore);
deps.runAppCommandWithInheritLogged(appPath, forwarded, args.logLevel, 'jellyfin-play');
if (!deps.isAppManagedByLauncher()) {
// A running app keeps running after playback; a cold `--start --jellyfin-play` makes the app
// quit with mpv on its own.
deps.runAppCommandWithInheritLogged(appPath, forwarded, args.logLevel, 'jellyfin-play');
}
// The picker started the app, so this playback owns it, as with local playback: hand over the
// item, wait for mpv to close, then exit, which stops the app.
const handoff = deps.runAppCommandCaptureOutput(appPath, forwarded);
if (handoff.status !== 0) {
fail(handoff.error?.message || handoff.stderr.trim() || 'Failed to start Jellyfin playback.');
}
deps.log(
'debug',
args.logLevel,
'jellyfin-play: waiting for mpv to close before stopping the app',
);
await deps.waitForUnixSocketClosed(mpvSocketPath);
return deps.exitProcess(0);
}
+22
View File
@@ -1121,6 +1121,21 @@ function clearOverlayManagedByLauncher(): void {
state.overlayManagedByLauncher = false;
}
// For commands that start the app ahead of playback (the Jellyfin picker): the launcher now owns
// it, as with local playback, and stops it when the launcher exits for any reason, including a
// cancelled picker or Ctrl+C.
export function adoptLauncherStartedApp(appPath: string, args: Args): void {
if (state.overlayManagedByLauncher) return;
markOverlayManagedByLauncher(appPath);
process.once('exit', () => stopManagedOverlayApp(args));
process.once('SIGINT', () => process.exit(130));
process.once('SIGTERM', () => process.exit(143));
}
export function isAppManagedByLauncher(): boolean {
return state.overlayManagedByLauncher;
}
function isAppAlreadyRunning(appPath: string, logLevel: LogLevel): boolean {
const result = runSyncAppCommand(appPath, ['--app-ping'], false);
if (result.error) {
@@ -1723,6 +1738,13 @@ export async function canConnectUnixSocket(socketPath: string): Promise<boolean>
});
}
// Resolves once mpv stops accepting connections on its IPC socket, i.e. the player was closed.
export async function waitForUnixSocketClosed(socketPath: string, pollMs = 1000): Promise<void> {
while (await canConnectUnixSocket(socketPath)) {
await sleep(pollMs);
}
}
export async function waitForUnixSocketReady(
socketPath: string,
timeoutMs: number,
+19 -3
View File
@@ -119,11 +119,27 @@ export async function promptOptionalJellyfinSearch(
themePath: string | null = null,
): Promise<string> {
if (useRofi && commandExists('rofi')) {
const rofiArgs = ['-dmenu', '-i', '-p', formatRofiPrompt('Jellyfin Search (optional)')];
// Input-only prompt: the shipped theme pins a tall window and has no message widget, so
// drop the listview (an empty row there reads as "no results") and show the hint instead.
const inputOnlyTheme =
'window { height: 0; } mainbox { children: [inputbar, message]; } ' +
'message { background-color: transparent; padding: 10px 18px 4px 18px; } ' +
'textbox { background-color: transparent; }';
const rofiArgs = [
'-dmenu',
'-i',
'-p',
formatRofiPrompt('Jellyfin Search (optional)'),
'-mesg',
'Type to search, or press Enter to browse all',
];
if (themePath) {
rofiArgs.push('-theme', themePath);
rofiArgs.push('-theme', themePath, '-theme-str', inputOnlyTheme);
} else {
rofiArgs.push('-theme-str', 'configuration { font: "Noto Sans CJK JP Regular 8";}');
rofiArgs.push(
'-theme-str',
`configuration { font: "Noto Sans CJK JP Regular 8";} ${inputOnlyTheme}`,
);
}
const result = spawnSync('rofi', rofiArgs, {
input: '\n',
+2
View File
@@ -63,6 +63,8 @@
"test:launcher:unit:src": "bun scripts/run-test-lane.mjs bun-launcher-unit",
"test:scripts": "bun scripts/run-test-lane.mjs scripts",
"test:stats": "bun scripts/run-test-lane.mjs stats",
"test:e2e": "bun scripts/run-test-lane.mjs e2e --jobs 1",
"e2e": "bun e2e/cli.ts",
"test:env": "bun run test:launcher:smoke:src && bun run test:plugin:src && bun run test:immersion:sqlite:src",
"test:runtime:compat": "bun run test:smoke:dist",
"test": "bun run test:fast",
-66
View File
@@ -1,66 +0,0 @@
## Highlights
### Added
- **YouTube Browser Window**:
- Browse YouTube in a window that keeps your login across restarts. Open it with `subminer youtube` (or `subminer yt`), or choose **Browse YouTube** from the tray.
- Clicking a video plays it in mpv. Middle-click, Shift/Ctrl-click, or the right-click menu adds it to mpv's playlist.
- Queued videos get the same Japanese subtitle setup when they start playing.
- **Whisper Subtitles for YouTube**: Set `youtube.subtitleSource` to `whisper` to transcribe YouTube videos with Whisper instead of using YouTube's captions. The subtitle generation modal (`Ctrl+Shift+G`) now also works on YouTube videos.
- **Scroll Wheel and Mouse Bindings**: `keybindings` now accepts scroll wheel keys, and the settings key editor can capture them. Mouse button and wheel bindings from mpv now work while the cursor is over the overlay.
- **Run Commands From Session Help**: Press `Enter` or double-click a row in session help to run that command during playback.
### Fixed
- **YouTube**:
- Japanese subtitles no longer fail with "HTTP 429" on videos that also have a machine-translated Japanese track.
- On Windows, the subtitle generation modal now works during YouTube playback.
- On Windows, playing a video queued from the YouTube browser window no longer clears the rest of mpv's queue.
- **Jellyfin**:
- The Japanese subtitle track is selected as soon as it downloads, instead of waiting for every other track.
- Japanese and English image-based subtitles (PGS, DVD, DVB) are now selected automatically.
- Timing review previews and mined card audio use the audio track you are playing.
- **Hyprland Overlay**: The overlay no longer interrupts mpv when it goes fullscreen. With `mpv.backend: x11`, the overlay stays clickable after mpv goes fullscreen.
- **mpv Key Bindings**: Keys from mpv's defaults and your `input.conf` (such as `9` and `0` for volume) now work while the overlay has focus, even when the overlay loads before SubMiner connects to mpv.
- **Media Timing Review**: Trim handles are now thin lines on the clip edges, and the original subtitle boundaries are plain orange bars. On macOS with Bluetooth headphones such as AirPods, the preview cursor now moves smoothly.
- **Stats**:
- Each mined card is counted once instead of twice.
- Overview totals, charts, the calendar, and the vocabulary summary update right after you delete sessions.
- Word and kanji dates stay correct after deletes and pruning, and the new-words charts now agree with the vocabulary summary. Existing data is repaired in the background on the next launch.
- **Yomitan Profile Safety**: Electron is updated to 43.7.7, and SubMiner now refuses to load your Yomitan data with an older or unsupported runtime. Automatic character-dictionary updates stop if a profile that had dictionaries suddenly reports none.
- **Security Updates**: Updated Electron and undici to resolve security advisories.
- **AppImage in Sandboxes**: The AppImage now opens a window when launched inside a sandbox such as `firejail --appimage`.
- **Ctrl+C Crash**: SubMiner no longer crashes with "write EPIPE" when you stop the terminal command that started it with Ctrl+C.
### Docs
- **Docs Site**: The docs site is shorter and easier to scan, and the configuration reference now has a table of keys and defaults for each section. Outdated pages were corrected, and the Arch Linux MeCab install now uses the AUR package `mecab-git`.
- **Stats**: Added docs on how the Overview refreshes after you delete sessions and how discovery dates are kept after pruning.
## What's Changed
- fix(storage): protect Yomitan profiles across Electron runtime changes by @ksyasuda in #211
- fix(jellyfin): select subtitles faster and survive failed track downloads by @ksyasuda in #270
- fix(jellyfin): use the current audio track for media generation by @ksyasuda in #271
- feat(youtube): add YouTube browser window and Whisper subtitle source by @ksyasuda in #273
- fix(overlay): thin centered trim handles and label-free subtitle boundaries in timing review by @ksyasuda in #274
- feat(overlay): run session help commands with Enter or double-click by @ksyasuda in #275
- fix(overlay): keep overlay mapped during Hyprland/Sway fullscreen by @ksyasuda in #276
- fix(linux): skip AppImage mount keepalive on sandbox squashfs mounts by @ksyasuda in #277
- fix(overlay): reload imported mpv keys after mpv connects by @ksyasuda in #278
- feat(overlay): forward mpv mouse button and wheel bindings by @ksyasuda in #279
- fix(stats): count mined cards once and keep lexical stats exact after deletes by @ksyasuda in #280
- fix(youtube): keep mpv queue when advancing to a queued video on Windows by @ksyasuda in #281
- fix(subtitles): allow Whisper generation during Windows YouTube stream playback by @ksyasuda in #282
## Installation
See the README and docs/installation guide for full setup steps.
## Assets
- Linux: `SubMiner.AppImage`
- macOS: `SubMiner-*.dmg` and `SubMiner-*.zip`
- Windows: `SubMiner-*.exe` and `SubMiner-*-win.zip`
- Optional extras: `subminer-assets.tar.gz`, the `subminer` launcher, and the Windows `subminer.cmd` launcher
- Bun corresponding source: `bun-v1.3.5-source.tar.gz` and its `.sha256` file
Both launcher downloads use Bun included with the SubMiner app. Download `subminer` on Linux or macOS and `subminer.cmd` on Windows.
The app bundles an unmodified Bun 1.3.5 runtime. Bun is MIT licensed and statically links JavaScriptCore (LGPL 2.0) and TinyCC (LGPL 2.1). License texts and third-party notices ship inside the app under `resources/bun/licenses`, and the source archive above contains the matching Bun, WebKit, and dependency sources for relinking.
+1
View File
@@ -12,6 +12,7 @@ paths=(
".prettierrc.json"
".github"
"build"
"e2e"
"launcher"
"scripts"
"src"
+6
View File
@@ -35,6 +35,12 @@ export const testLanes: Record<string, TestLane> = {
roots: ['scripts'],
include: ['.test.ts'],
},
// Boots the built app, mpv, and a fake AnkiConnect on an offscreen display.
// Slow and dependent on mpv/ffmpeg/Xvfb, so it stays out of test:fast.
e2e: {
roots: ['e2e'],
include: ['.e2e.test.ts'],
},
stats: {
roots: ['stats/src'],
include: ['.test.ts', '.test.tsx'],
+2 -1
View File
@@ -929,9 +929,10 @@ test('handleCliCommand runs jellyfin command dispatcher', async () => {
const { deps, calls } = createDeps();
handleCliCommand(makeArgs({ jellyfinLibraries: true }), 'initial', deps);
handleCliCommand(makeArgs({ jellyfinSubtitles: true }), 'initial', deps);
handleCliCommand(makeArgs({ jellyfinPreviewAuth: true }), 'second-instance', deps);
await new Promise((resolve) => setImmediate(resolve));
const runJellyfinCallCount = calls.filter((value) => value === 'runJellyfinCommand').length;
assert.equal(runJellyfinCallCount, 2);
assert.equal(runJellyfinCallCount, 3);
});
test('handleCliCommand reports jellyfin command errors to OSD', async () => {
+2 -1
View File
@@ -771,7 +771,8 @@ export function handleCliCommand(
args.jellyfinItems ||
args.jellyfinSubtitles ||
args.jellyfinPlay ||
args.jellyfinRemoteAnnounce
args.jellyfinRemoteAnnounce ||
args.jellyfinPreviewAuth
) {
runAsyncWithOsd(
() => deps.runJellyfinCommand(args),
+5 -1
View File
@@ -131,7 +131,11 @@ export {
resolvePlaybackPlan as resolveJellyfinPlaybackPlanRuntime,
ticksToSeconds as jellyfinTicksToSecondsRuntime,
} from './jellyfin';
export { buildJellyfinTimelinePayload, JellyfinRemoteSessionService } from './jellyfin-remote';
export { JellyfinRemoteSessionService } from './jellyfin-remote';
export {
buildJellyfinTimelinePayload,
JellyfinPlaybackReporter,
} from './jellyfin-playback-reporter';
export {
broadcastRuntimeOptionsChangedRuntime,
createOverlayManager,
+2
View File
@@ -86,7 +86,9 @@ function createControllerConfigFixture() {
function createSubtitleSidebarSnapshotFixture(): SubtitleSidebarSnapshot {
return {
sourceKey: 'test-subtitles',
cuesRevision: 1,
cues: [],
currentTimeSec: null,
currentSubtitle: { text: '', startTime: null, endTime: null },
config: {
enabled: false,
+11
View File
@@ -12,6 +12,7 @@ import type {
RuntimeOptionId,
RuntimeOptionValue,
SubtitleMiningContext,
SubtitleSidebarPlaybackState,
SubtitleSidebarSnapshot,
SubtitlePosition,
SubsyncManualRunRequest,
@@ -79,6 +80,7 @@ export interface IpcServiceDeps {
getCurrentSubtitleRaw: () => string;
getCurrentSubtitleAss: () => string;
getSubtitleSidebarSnapshot?: () => Promise<SubtitleSidebarSnapshot>;
getSubtitleSidebarPlaybackState?: () => SubtitleSidebarPlaybackState;
getSubtitleSidebarOpen?: () => boolean;
getPlaybackPaused: () => boolean | null | Promise<boolean | null>;
getSubtitlePosition: () => unknown;
@@ -369,6 +371,7 @@ export interface IpcDepsRuntimeOptions {
getCurrentSubtitleRaw: () => string;
getCurrentSubtitleAss: () => string;
getSubtitleSidebarSnapshot?: () => Promise<SubtitleSidebarSnapshot>;
getSubtitleSidebarPlaybackState?: () => SubtitleSidebarPlaybackState;
getSubtitleSidebarOpen?: () => boolean;
getPlaybackPaused: () => boolean | null | Promise<boolean | null>;
getSubtitlePosition: () => unknown;
@@ -455,6 +458,7 @@ export function createIpcDepsRuntime(options: IpcDepsRuntimeOptions): IpcService
getCurrentSubtitleRaw: options.getCurrentSubtitleRaw,
getCurrentSubtitleAss: options.getCurrentSubtitleAss,
getSubtitleSidebarSnapshot: options.getSubtitleSidebarSnapshot,
getSubtitleSidebarPlaybackState: options.getSubtitleSidebarPlaybackState,
getSubtitleSidebarOpen: options.getSubtitleSidebarOpen ?? (() => false),
getPlaybackPaused: options.getPlaybackPaused,
getSubtitlePosition: options.getSubtitlePosition,
@@ -742,6 +746,13 @@ export function registerIpcHandlers(deps: IpcServiceDeps, ipc: IpcMainRegistrar
return await deps.getSubtitleSidebarSnapshot();
});
ipc.handle(IPC_CHANNELS.request.getSubtitleSidebarPlaybackState, () => {
if (!deps.getSubtitleSidebarPlaybackState) {
throw new Error('Subtitle sidebar playback state is unavailable.');
}
return deps.getSubtitleSidebarPlaybackState();
});
ipc.handle(IPC_CHANNELS.request.getSubtitleSidebarOpen, () => {
return deps.getSubtitleSidebarOpen?.() ?? false;
});
@@ -0,0 +1,101 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {
buildJellyfinTimelinePayload,
JellyfinPlaybackReporter,
} from './jellyfin-playback-reporter';
type FetchCall = { input: string; init: RequestInit };
function createReporter(respond: (input: string) => Response, logWarn?: (message: string) => void) {
const calls: FetchCall[] = [];
const reporter = new JellyfinPlaybackReporter({
serverUrl: 'http://jellyfin.local/',
accessToken: 'token-1',
deviceId: 'device-1',
clientName: 'SubMiner',
clientVersion: '1.2.3',
deviceName: 'host',
fetchImpl: (async (input, init) => {
calls.push({ input: String(input), init: init ?? {} });
return respond(String(input));
}) as typeof fetch,
logWarn,
});
return { reporter, calls };
}
test('reportProgress posts the timeline payload as this device and treats failure as non-fatal', async () => {
let fail = false;
const { reporter, calls } = createReporter(
() => new Response(fail ? 'boom' : null, { status: fail ? 500 : 200 }),
);
const state = {
itemId: 'movie-2',
positionTicks: 123456,
isPaused: true,
volumeLevel: 33,
audioStreamIndex: 1,
subtitleStreamIndex: 2,
};
assert.equal(await reporter.reportProgress(state), true);
fail = true;
assert.equal(await reporter.reportProgress({ itemId: 'movie-2', positionTicks: 999 }), false);
const call = calls[0]!;
assert.equal(call.input, 'http://jellyfin.local/Sessions/Playing/Progress');
assert.deepEqual(
JSON.parse(String(call.init.body)),
JSON.parse(JSON.stringify(buildJellyfinTimelinePayload(state))),
);
assert.equal(
(call.init.headers as Record<string, string>).Authorization,
'MediaBrowser Client="SubMiner", Device="host", DeviceId="device-1", Version="1.2.3", Token="token-1"',
);
});
test('timeline payload omits websocket-only event names', () => {
const payload = buildJellyfinTimelinePayload({
itemId: 'movie-2',
positionTicks: 123456,
eventName: 'TimeUpdate',
});
assert.equal('EventName' in payload, false);
});
test('reportStopped posts final position and explicit non-failed state', async () => {
const { reporter, calls } = createReporter(() => new Response(null, { status: 200 }));
assert.equal(
await reporter.reportStopped({ itemId: 'movie-stop', positionTicks: 7654321 }),
true,
);
assert.equal(calls[0]!.input, 'http://jellyfin.local/Sessions/Playing/Stopped');
const posted = JSON.parse(String(calls[0]!.init.body));
assert.equal(posted.PositionTicks, 7654321);
assert.equal(posted.Failed, false);
});
test('warns once per failing endpoint until it recovers', async () => {
const warnings: string[] = [];
let status = 400;
const { reporter } = createReporter(
() => new Response(null, { status }),
(message) => warnings.push(message),
);
const state = { itemId: 'item-1', positionTicks: 10, playMethod: 'DirectPlay' };
assert.equal(await reporter.reportStopped(state), false);
assert.equal(await reporter.reportStopped(state), false);
assert.equal(warnings.length, 1);
assert.match(warnings[0]!, /Sessions\/Playing\/Stopped/);
status = 200;
assert.equal(await reporter.reportStopped(state), true);
status = 500;
assert.equal(await reporter.reportStopped(state), false);
assert.equal(warnings.length, 2);
});
@@ -0,0 +1,155 @@
export interface JellyfinTimelinePlaybackState {
itemId: string;
mediaSourceId?: string;
positionTicks?: number;
playbackStartTimeTicks?: number;
isPaused?: boolean;
isMuted?: boolean;
canSeek?: boolean;
volumeLevel?: number;
playbackRate?: number;
playMethod?: string;
audioStreamIndex?: number | null;
subtitleStreamIndex?: number | null;
playlistItemId?: string | null;
eventName?: string;
failed?: boolean;
}
export interface JellyfinTimelinePayload {
ItemId: string;
MediaSourceId?: string;
PositionTicks: number;
PlaybackStartTimeTicks: number;
IsPaused: boolean;
IsMuted: boolean;
CanSeek: boolean;
VolumeLevel: number;
PlaybackRate: number;
PlayMethod: string;
AudioStreamIndex?: number | null;
SubtitleStreamIndex?: number | null;
PlaylistItemId?: string | null;
Failed?: boolean;
}
export interface JellyfinPlaybackReporterOptions {
serverUrl: string;
accessToken: string;
deviceId: string;
clientName?: string;
clientVersion?: string;
deviceName?: string;
fetchImpl?: typeof fetch;
logWarn?: (message: string, details?: unknown) => void;
}
const JELLYFIN_REPORT_TIMEOUT_MS = 5000;
function clampVolume(value: number | undefined): number {
if (typeof value !== 'number' || !Number.isFinite(value)) return 100;
return Math.max(0, Math.min(100, Math.round(value)));
}
function normalizeTicks(value: number | undefined): number {
if (typeof value !== 'number' || !Number.isFinite(value)) return 0;
return Math.max(0, Math.floor(value));
}
function asNullableInteger(value: number | null | undefined): number | null {
if (typeof value !== 'number' || !Number.isInteger(value)) return null;
return value;
}
export function buildJellyfinTimelinePayload(
state: JellyfinTimelinePlaybackState,
): JellyfinTimelinePayload {
return {
ItemId: state.itemId,
MediaSourceId: state.mediaSourceId,
PositionTicks: normalizeTicks(state.positionTicks),
PlaybackStartTimeTicks: normalizeTicks(state.playbackStartTimeTicks),
IsPaused: state.isPaused === true,
IsMuted: state.isMuted === true,
CanSeek: state.canSeek !== false,
VolumeLevel: clampVolume(state.volumeLevel),
PlaybackRate:
typeof state.playbackRate === 'number' && Number.isFinite(state.playbackRate)
? state.playbackRate
: 1,
PlayMethod: state.playMethod || 'DirectPlay',
AudioStreamIndex: asNullableInteger(state.audioStreamIndex),
SubtitleStreamIndex: asNullableInteger(state.subtitleStreamIndex),
PlaylistItemId: state.playlistItemId,
Failed: state.failed,
};
}
// Authenticated JSON POSTs as this device. Playback timeline reports (start, progress, stop)
// are plain HTTP, so they work whether or not the cast-control websocket is connected; that
// is how Jellyfin learns the resume position and marks items played for every playback.
export class JellyfinPlaybackReporter {
readonly authHeader: string;
private readonly serverUrl: string;
private readonly fetchImpl: typeof fetch;
private readonly logWarn?: (message: string, details?: unknown) => void;
private readonly failedRequestPaths = new Set<string>();
constructor(options: JellyfinPlaybackReporterOptions) {
this.serverUrl = options.serverUrl.trim().replace(/\/+$/, '');
this.fetchImpl = options.fetchImpl ?? fetch;
this.logWarn = options.logWarn;
const clientName = options.clientName || 'SubMiner';
const clientVersion = options.clientVersion || '0.1.0';
const deviceName = options.deviceName || clientName;
this.authHeader = `MediaBrowser Client="${clientName}", Device="${deviceName}", DeviceId="${options.deviceId}", Version="${clientVersion}", Token="${options.accessToken}"`;
}
reportPlaying(state: JellyfinTimelinePlaybackState): Promise<boolean> {
return this.postJson('/Sessions/Playing', buildJellyfinTimelinePayload(state));
}
reportProgress(state: JellyfinTimelinePlaybackState): Promise<boolean> {
return this.postJson('/Sessions/Playing/Progress', buildJellyfinTimelinePayload(state));
}
reportStopped(state: JellyfinTimelinePlaybackState): Promise<boolean> {
return this.postJson('/Sessions/Playing/Stopped', {
...buildJellyfinTimelinePayload(state),
Failed: state.failed === true,
});
}
async postJson(path: string, payload: unknown): Promise<boolean> {
try {
const response = await this.fetchImpl(`${this.serverUrl}${path}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: this.authHeader,
},
body: JSON.stringify(payload),
// The stop report waits for in-flight progress reports, so a hung request must not
// hold it back indefinitely.
signal: AbortSignal.timeout(JELLYFIN_REPORT_TIMEOUT_MS),
});
this.noteRequestOutcome(path, response.ok ? null : `HTTP ${response.status}`);
return response.ok;
} catch (error) {
this.noteRequestOutcome(path, error);
return false;
}
}
// Warn once per path while it keeps failing so a rejected stop report is visible in the
// log without a warning per progress tick.
private noteRequestOutcome(path: string, failure: unknown): void {
if (failure === null) {
this.failedRequestPaths.delete(path);
return;
}
if (this.failedRequestPaths.has(path)) return;
this.failedRequestPaths.add(path);
this.logWarn?.(`Jellyfin request failed: POST ${path}`, failure);
}
}
+1 -128
View File
@@ -1,6 +1,6 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { buildJellyfinTimelinePayload, JellyfinRemoteSessionService } from './jellyfin-remote';
import { JellyfinRemoteSessionService } from './jellyfin-remote';
class FakeWebSocket {
private listeners: Record<string, Array<(...args: unknown[]) => void>> = {};
@@ -240,105 +240,6 @@ test('Jellyfin remote stop prevents further reconnect/network activity', () => {
assert.equal(service.isConnected(), false);
});
test('reportProgress posts timeline payload and treats failure as non-fatal', async () => {
const sockets: FakeWebSocket[] = [];
const fetchCalls: Array<{ input: string; init: RequestInit }> = [];
let shouldFailTimeline = false;
const service = new JellyfinRemoteSessionService({
serverUrl: 'http://jellyfin.local',
accessToken: 'token-4',
deviceId: 'device-4',
webSocketFactory: () => {
const socket = new FakeWebSocket();
sockets.push(socket);
return socket as unknown as any;
},
fetchImpl: (async (input, init) => {
fetchCalls.push({ input: String(input), init: init ?? {} });
if (String(input).endsWith('/Sessions/Playing/Progress') && shouldFailTimeline) {
return new Response('boom', { status: 500 });
}
return new Response(null, { status: 200 });
}) as typeof fetch,
});
service.start();
sockets[0]!.emit('open');
await new Promise((resolve) => setTimeout(resolve, 0));
const expectedPayload = buildJellyfinTimelinePayload({
itemId: 'movie-2',
positionTicks: 123456,
isPaused: true,
volumeLevel: 33,
audioStreamIndex: 1,
subtitleStreamIndex: 2,
});
const expectedPostedPayload = Object.fromEntries(
Object.entries(structuredClone(expectedPayload)).filter(([, value]) => value !== undefined),
);
const ok = await service.reportProgress({
itemId: 'movie-2',
positionTicks: 123456,
isPaused: true,
volumeLevel: 33,
audioStreamIndex: 1,
subtitleStreamIndex: 2,
});
shouldFailTimeline = true;
const failed = await service.reportProgress({
itemId: 'movie-2',
positionTicks: 999,
});
const timelineCall = fetchCalls.find((call) => call.input.endsWith('/Sessions/Playing/Progress'));
assert.ok(timelineCall);
assert.equal(ok, true);
assert.equal(failed, false);
assert.ok(typeof timelineCall.init.body === 'string');
assert.deepEqual(JSON.parse(String(timelineCall.init.body)), expectedPostedPayload);
});
test('timeline payload omits websocket-only event names', () => {
const payload = buildJellyfinTimelinePayload({
itemId: 'movie-2',
positionTicks: 123456,
eventName: 'TimeUpdate',
});
assert.equal('EventName' in payload, false);
});
test('reportStopped posts final position and explicit non-failed state', async () => {
const fetchCalls: Array<{ input: string; init: RequestInit }> = [];
const service = new JellyfinRemoteSessionService({
serverUrl: 'http://jellyfin.local',
accessToken: 'token-stop-payload',
deviceId: 'device-stop-payload',
webSocketFactory: () => new FakeWebSocket() as unknown as any,
fetchImpl: (async (input, init) => {
fetchCalls.push({ input: String(input), init: init ?? {} });
return new Response(null, { status: 200 });
}) as typeof fetch,
});
const ok = await service.reportStopped({
itemId: 'movie-stop',
positionTicks: 7654321,
failed: false,
});
const stoppedCall = fetchCalls.find((call) => call.input.endsWith('/Sessions/Playing/Stopped'));
assert.equal(ok, true);
assert.ok(stoppedCall);
assert.ok(typeof stoppedCall.init.body === 'string');
const posted = JSON.parse(String(stoppedCall.init.body));
assert.equal(posted.PositionTicks, 7654321);
assert.equal(posted.Failed, false);
});
test('advertiseNow validates server registration using Sessions endpoint', async () => {
const sockets: FakeWebSocket[] = [];
const calls: string[] = [];
@@ -451,34 +352,6 @@ test('reconnects when the server stops answering keep-alives', () => {
assert.equal(sockets.length, 2);
});
test('warns once per failing timeline endpoint until it recovers', async () => {
const warnings: string[] = [];
let status = 400;
const service = new JellyfinRemoteSessionService({
serverUrl: 'http://jellyfin.local',
accessToken: 'token-warn',
deviceId: 'device-warn',
webSocketFactory: () => new FakeWebSocket() as unknown as any,
fetchImpl: (async () => new Response(null, { status })) as typeof fetch,
logWarn: (message) => {
warnings.push(message);
},
});
const state = { itemId: 'item-1', positionTicks: 10, playMethod: 'DirectPlay' };
assert.equal(await service.reportStopped(state), false);
assert.equal(await service.reportStopped(state), false);
assert.equal(warnings.length, 1);
assert.match(warnings[0]!, /Sessions\/Playing\/Stopped/);
status = 200;
assert.equal(await service.reportStopped(state), true);
status = 500;
assert.equal(await service.reportStopped(state), false);
assert.equal(warnings.length, 2);
});
test('ignores messages from a superseded socket', () => {
const sockets: FakeWebSocket[] = [];
const playPayloads: unknown[] = [];
+15 -145
View File
@@ -1,45 +1,11 @@
import WebSocket from 'ws';
import { JellyfinPlaybackReporter } from './jellyfin-playback-reporter';
export interface JellyfinRemoteSessionMessage {
MessageType?: string;
Data?: unknown;
}
export interface JellyfinTimelinePlaybackState {
itemId: string;
mediaSourceId?: string;
positionTicks?: number;
playbackStartTimeTicks?: number;
isPaused?: boolean;
isMuted?: boolean;
canSeek?: boolean;
volumeLevel?: number;
playbackRate?: number;
playMethod?: string;
audioStreamIndex?: number | null;
subtitleStreamIndex?: number | null;
playlistItemId?: string | null;
eventName?: string;
failed?: boolean;
}
export interface JellyfinTimelinePayload {
ItemId: string;
MediaSourceId?: string;
PositionTicks: number;
PlaybackStartTimeTicks: number;
IsPaused: boolean;
IsMuted: boolean;
CanSeek: boolean;
VolumeLevel: number;
PlaybackRate: number;
PlayMethod: string;
AudioStreamIndex?: number | null;
SubtitleStreamIndex?: number | null;
PlaylistItemId?: string | null;
Failed?: boolean;
}
interface JellyfinRemoteSocket {
on(event: 'open', listener: () => void): this;
on(event: 'close', listener: () => void): this;
@@ -99,16 +65,6 @@ function normalizeServerUrl(serverUrl: string): string {
return serverUrl.trim().replace(/\/+$/, '');
}
function clampVolume(value: number | undefined): number {
if (typeof value !== 'number' || !Number.isFinite(value)) return 100;
return Math.max(0, Math.min(100, Math.round(value)));
}
function normalizeTicks(value: number | undefined): number {
if (typeof value !== 'number' || !Number.isFinite(value)) return 0;
return Math.max(0, Math.floor(value));
}
function parseMessageData(value: unknown): unknown {
if (typeof value !== 'string') return value;
const trimmed = value.trim();
@@ -137,11 +93,6 @@ function parseInboundMessage(rawData: unknown): JellyfinRemoteSessionMessage | n
}
}
function asNullableInteger(value: number | null | undefined): number | null {
if (typeof value !== 'number' || !Number.isInteger(value)) return null;
return value;
}
function createDefaultCapabilities(): {
PlayableMediaTypes: string;
SupportedCommands: string;
@@ -155,40 +106,6 @@ function createDefaultCapabilities(): {
};
}
function buildAuthorizationHeader(params: {
clientName: string;
deviceName: string;
clientVersion: string;
deviceId: string;
accessToken: string;
}): string {
return `MediaBrowser Client="${params.clientName}", Device="${params.deviceName}", DeviceId="${params.deviceId}", Version="${params.clientVersion}", Token="${params.accessToken}"`;
}
export function buildJellyfinTimelinePayload(
state: JellyfinTimelinePlaybackState,
): JellyfinTimelinePayload {
return {
ItemId: state.itemId,
MediaSourceId: state.mediaSourceId,
PositionTicks: normalizeTicks(state.positionTicks),
PlaybackStartTimeTicks: normalizeTicks(state.playbackStartTimeTicks),
IsPaused: state.isPaused === true,
IsMuted: state.isMuted === true,
CanSeek: state.canSeek !== false,
VolumeLevel: clampVolume(state.volumeLevel),
PlaybackRate:
typeof state.playbackRate === 'number' && Number.isFinite(state.playbackRate)
? state.playbackRate
: 1,
PlayMethod: state.playMethod || 'DirectPlay',
AudioStreamIndex: asNullableInteger(state.audioStreamIndex),
SubtitleStreamIndex: asNullableInteger(state.subtitleStreamIndex),
PlaylistItemId: state.playlistItemId,
Failed: state.failed,
};
}
export class JellyfinRemoteSessionService {
private readonly serverUrl: string;
private readonly accessToken: string;
@@ -210,6 +127,8 @@ export class JellyfinRemoteSessionService {
SupportsMediaControl: boolean;
};
private readonly authHeader: string;
// Shares this device's identity for capability posts; playback reports use their own reporter.
private readonly http: JellyfinPlaybackReporter;
private readonly onConnected?: () => void;
private readonly onDisconnected?: () => void;
private readonly logWarn?: (message: string, details?: unknown) => void;
@@ -217,7 +136,6 @@ export class JellyfinRemoteSessionService {
private keepAliveTimeoutMs: number;
private keepAliveTimer: ReturnType<typeof setTimeout> | null = null;
private lastInboundAtMs = 0;
private readonly failedRequestPaths = new Set<string>();
private readonly reconnectBaseDelayMs: number;
private readonly reconnectMaxDelayMs: number;
@@ -243,19 +161,20 @@ export class JellyfinRemoteSessionService {
...createDefaultCapabilities(),
...(options.capabilities ?? {}),
};
const clientName = options.clientName || 'SubMiner';
const clientVersion = options.clientVersion || '0.1.0';
const deviceName = options.deviceName || clientName;
this.authHeader = buildAuthorizationHeader({
clientName,
deviceName,
clientVersion,
deviceId: this.deviceId,
this.logWarn = options.logWarn;
this.http = new JellyfinPlaybackReporter({
serverUrl: this.serverUrl,
accessToken: this.accessToken,
deviceId: this.deviceId,
clientName: options.clientName,
clientVersion: options.clientVersion,
deviceName: options.deviceName,
fetchImpl: this.fetchImpl,
logWarn: this.logWarn,
});
this.authHeader = this.http.authHeader;
this.onConnected = options.onConnected;
this.onDisconnected = options.onDisconnected;
this.logWarn = options.logWarn;
this.now = options.getNow ?? Date.now;
this.keepAliveTimeoutMs = Math.max(
1000,
@@ -298,21 +217,6 @@ export class JellyfinRemoteSessionService {
return this.isRegisteredOnServer();
}
public async reportPlaying(state: JellyfinTimelinePlaybackState): Promise<boolean> {
return this.postTimeline('/Sessions/Playing', buildJellyfinTimelinePayload(state));
}
public async reportProgress(state: JellyfinTimelinePlaybackState): Promise<boolean> {
return this.postTimeline('/Sessions/Playing/Progress', buildJellyfinTimelinePayload(state));
}
public async reportStopped(state: JellyfinTimelinePlaybackState): Promise<boolean> {
return this.postTimeline('/Sessions/Playing/Stopped', {
...buildJellyfinTimelinePayload(state),
Failed: state.failed === true,
});
}
private connectSocket(): void {
if (!this.running) return;
if (this.reconnectTimer) {
@@ -441,9 +345,9 @@ export class JellyfinRemoteSessionService {
private async postCapabilities(): Promise<void> {
const payload = this.capabilities;
const fullEndpointOk = await this.postJson('/Sessions/Capabilities/Full', payload);
const fullEndpointOk = await this.http.postJson('/Sessions/Capabilities/Full', payload);
if (fullEndpointOk) return;
await this.postJson('/Sessions/Capabilities', payload);
await this.http.postJson('/Sessions/Capabilities', payload);
}
private async isRegisteredOnServer(): Promise<boolean> {
@@ -462,40 +366,6 @@ export class JellyfinRemoteSessionService {
}
}
private async postTimeline(path: string, payload: JellyfinTimelinePayload): Promise<boolean> {
return this.postJson(path, payload);
}
private async postJson(path: string, payload: unknown): Promise<boolean> {
try {
const response = await this.fetchImpl(`${this.serverUrl}${path}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: this.authHeader,
},
body: JSON.stringify(payload),
});
this.noteRequestOutcome(path, response.ok ? null : `HTTP ${response.status}`);
return response.ok;
} catch (error) {
this.noteRequestOutcome(path, error);
return false;
}
}
// Warn once per path while it keeps failing so a rejected stop report is visible in the
// log without a warning per progress tick.
private noteRequestOutcome(path: string, failure: unknown): void {
if (failure === null) {
this.failedRequestPaths.delete(path);
return;
}
if (this.failedRequestPaths.has(path)) return;
this.failedRequestPaths.add(path);
this.logWarn?.(`Jellyfin remote request failed: POST ${path}`, failure);
}
private handleInboundMessage(socket: JellyfinRemoteSocket, rawData: unknown): void {
const message = parseInboundMessage(rawData);
if (!message) return;
+60 -84
View File
@@ -58,6 +58,7 @@ import {
shouldAutoLoadSecondarySubTrackForJellyfinPlayback,
} from './main/runtime/jellyfin-remote-playback';
import { getAppControlSocketPath } from './shared/app-control';
import { writeJellyfinCliResponse } from './shared/jellyfin-cli-response';
import {
type CancelLinuxMpvFullscreenOverlayRefreshBurst,
clearLinuxMpvFullscreenOverlayRefreshTimeouts,
@@ -100,8 +101,10 @@ import type {
SessionActionDispatchRequest,
SecondarySubMode,
SubtitleData,
SubtitleCue,
SubtitleMiningContext,
SubtitlePosition,
SubtitleSidebarPlaybackState,
WindowGeometry,
} from './types';
import { OPEN_ANKI_CARD_ACTION_ID } from './types';
@@ -331,6 +334,7 @@ import {
setOverlayDebugVisualizationEnabledRuntime,
setVisibleOverlayVisible as setVisibleOverlayVisibleCore,
showMpvOsdRuntime,
JellyfinPlaybackReporter,
tokenizeSubtitle as tokenizeSubtitleCore,
triggerFieldGrouping as triggerFieldGroupingCore,
upsertYomitanDictionarySettings,
@@ -409,6 +413,7 @@ import {
DEFAULT_JELLYFIN_CLIENT_NAME,
DEFAULT_JELLYFIN_CLIENT_VERSION,
createHostDerivedJellyfinDeviceId,
resolveJellyfinRemoteDeviceName,
} from './main/runtime/jellyfin-device-identity';
import {
clearJellyfinAuthSessionAndRefreshTray as clearJellyfinAuthSessionAndRefreshTrayRuntime,
@@ -2102,6 +2107,33 @@ const linuxOverlayModeRuntime = createLinuxOverlayModeRuntime({
logDebug: (message) => logger.debug(message),
});
let subtitleSidebarRequestedOpen = false;
let subtitleSidebarCuesRef: SubtitleCue[] | null = null;
let subtitleSidebarCuesRevision = 0;
/**
* Cheap sidebar state polled ~12x/s while the sidebar is open. Reads only cached state, no
* mpv round-trips. `cuesRevision` bumps whenever a new parsed cue array is swapped in, so a
* re-parse of the same source still tells the renderer to fetch the full snapshot.
*/
function getSubtitleSidebarPlaybackState(): SubtitleSidebarPlaybackState {
if (appState.activeParsedSubtitleCues !== subtitleSidebarCuesRef) {
subtitleSidebarCuesRef = appState.activeParsedSubtitleCues;
subtitleSidebarCuesRevision += 1;
}
return {
sourceKey: JSON.stringify([
appState.activeParsedSubtitleMediaPath,
appState.activeParsedSubtitleSource,
]),
cuesRevision: subtitleSidebarCuesRevision,
currentTimeSec: appState.mpvClient?.currentTimePos ?? null,
currentSubtitle: {
text: appState.currentSubText,
startTime: appState.mpvClient?.currentSubStart ?? null,
endTime: appState.mpvClient?.currentSubEnd ?? null,
},
};
}
const SEEK_THRESHOLD_SECONDS = 3;
const EXPLICIT_SEEK_INTENT_TTL_MS = 2000;
let explicitSeekIntentExpiresAtMs = 0;
@@ -3395,9 +3427,17 @@ const {
setLastProgressAtMs: (value) => {
jellyfinRemoteLastProgressAtMs = value;
},
reportPlaying: (payload) => {
void appState.jellyfinRemoteSession?.reportPlaying(payload);
},
// Same device identity as the cast session, so Jellyfin attributes reports to one session.
createPlaybackReporter: ({ session, clientInfo }) =>
new JellyfinPlaybackReporter({
serverUrl: session.serverUrl,
accessToken: session.accessToken,
deviceId: clientInfo.deviceId,
clientName: clientInfo.clientName,
clientVersion: clientInfo.clientVersion,
deviceName: resolveJellyfinRemoteDeviceName({ hostName: os.hostname() }),
logWarn: (message, details) => logger.warn(message, details),
}),
showMpvOsd: (text) => {
overlayNotificationsRuntime.showConfiguredStatusNotification(text, { title: 'Jellyfin' });
},
@@ -3419,7 +3459,6 @@ const {
clearActivePlayback: () => {
activeJellyfinRemotePlayback = null;
},
getSession: () => appState.jellyfinRemoteSession,
getNow: () => Date.now(),
getLastProgressAtMs: () => jellyfinRemoteLastProgressAtMs,
setLastProgressAtMs: (value) => {
@@ -3448,10 +3487,7 @@ const {
listJellyfinItemsRuntime(session, clientInfo, params),
listJellyfinSubtitleTracks: (session, clientInfo, itemId) =>
listJellyfinSubtitleTracksRuntime(session, clientInfo, itemId),
writeJellyfinPreviewAuth: (responsePath, payload) => {
fs.mkdirSync(path.dirname(responsePath), { recursive: true });
fs.writeFileSync(responsePath, JSON.stringify(payload, null, 2), 'utf-8');
},
writeJellyfinResponse: writeJellyfinCliResponse,
logInfo: (message) => logger.info(message),
},
handleJellyfinPlayCommandMainDeps: {
@@ -3488,6 +3524,7 @@ const {
},
runJellyfinCommandMainDeps: {
defaultServerUrl: DEFAULT_CONFIG.jellyfin.serverUrl,
writeJellyfinResponse: writeJellyfinCliResponse,
},
maybeFocusExistingJellyfinSetupWindowMainDeps: {
getSetupWindow: () => appState.jellyfinSetupWindow,
@@ -6023,26 +6060,19 @@ const { registerIpcRuntimeHandlers } = composeIpcRuntimeHandlers({
getCurrentSubtitleRaw: () => appState.currentSubText,
getCurrentSubtitleAss: () => appState.currentSubAssText,
getSubtitleSidebarOpen: () => subtitleSidebarRequestedOpen,
getSubtitleSidebarPlaybackState: () => getSubtitleSidebarPlaybackState(),
// Full snapshot: resolves the active source over mpv IPC and re-parses it when it changed.
// The renderer fetches it on open, on source/cue changes, and on a slow fallback cadence.
getSubtitleSidebarSnapshot: async () => {
const currentSubtitle = {
text: appState.currentSubText,
startTime: appState.mpvClient?.currentSubStart ?? null,
endTime: appState.mpvClient?.currentSubEnd ?? null,
};
const currentTimeSec = appState.mpvClient?.currentTimePos ?? null;
const config = configService.getConfig().subtitleSidebar;
const cachedSnapshot = () => ({
...getSubtitleSidebarPlaybackState(),
cues: appState.activeParsedSubtitleCues,
config,
});
const client = appState.mpvClient;
if (!client?.connected) {
return {
sourceKey: JSON.stringify([
appState.activeParsedSubtitleMediaPath,
appState.activeParsedSubtitleSource,
]),
cues: appState.activeParsedSubtitleCues,
currentTimeSec,
currentSubtitle,
config,
};
return cachedSnapshot();
}
try {
@@ -6056,16 +6086,7 @@ const { registerIpcRuntimeHandlers } = composeIpcRuntimeHandlers({
]);
const videoPath = typeof videoPathRaw === 'string' ? videoPathRaw : '';
if (!videoPath) {
return {
sourceKey: JSON.stringify([
appState.activeParsedSubtitleMediaPath,
appState.activeParsedSubtitleSource,
]),
cues: appState.activeParsedSubtitleCues,
currentTimeSec,
currentSubtitle,
config,
};
return cachedSnapshot();
}
if (
shouldUseCachedYoutubeParsedCues({
@@ -6074,16 +6095,7 @@ const { registerIpcRuntimeHandlers } = composeIpcRuntimeHandlers({
cachedCueCount: appState.activeParsedSubtitleCues.length,
})
) {
return {
sourceKey: JSON.stringify([
appState.activeParsedSubtitleMediaPath,
appState.activeParsedSubtitleSource,
]),
cues: appState.activeParsedSubtitleCues,
currentTimeSec,
currentSubtitle,
config,
};
return cachedSnapshot();
}
const resolvedSource = await resolveActiveSubtitleSidebarSourceHandler({
@@ -6094,30 +6106,12 @@ const { registerIpcRuntimeHandlers } = composeIpcRuntimeHandlers({
videoPath,
});
if (!resolvedSource) {
return {
sourceKey: JSON.stringify([
appState.activeParsedSubtitleMediaPath,
appState.activeParsedSubtitleSource,
]),
cues: appState.activeParsedSubtitleCues,
currentTimeSec,
currentSubtitle,
config,
};
return cachedSnapshot();
}
try {
if (appState.activeParsedSubtitleSource === resolvedSource.sourceKey) {
return {
sourceKey: JSON.stringify([
appState.activeParsedSubtitleMediaPath,
appState.activeParsedSubtitleSource,
]),
cues: appState.activeParsedSubtitleCues,
currentTimeSec,
currentSubtitle,
config,
};
return cachedSnapshot();
}
const content = await loadSubtitleSourceText(resolvedSource.path);
@@ -6125,30 +6119,12 @@ const { registerIpcRuntimeHandlers } = composeIpcRuntimeHandlers({
appState.activeParsedSubtitleCues = cues;
appState.activeParsedSubtitleSource = resolvedSource.sourceKey;
appState.activeParsedSubtitleMediaPath = videoPath || null;
return {
sourceKey: JSON.stringify([
appState.activeParsedSubtitleMediaPath,
appState.activeParsedSubtitleSource,
]),
cues,
currentTimeSec,
currentSubtitle,
config,
};
return cachedSnapshot();
} finally {
await resolvedSource.cleanup?.();
}
} catch {
return {
sourceKey: JSON.stringify([
appState.activeParsedSubtitleMediaPath,
appState.activeParsedSubtitleSource,
]),
cues: appState.activeParsedSubtitleCues,
currentTimeSec,
currentSubtitle,
config,
};
return cachedSnapshot();
}
},
getPlaybackPaused: () =>
+2
View File
@@ -74,6 +74,7 @@ export interface MainIpcRuntimeServiceDepsParams {
getCurrentSubtitleRaw: IpcDepsRuntimeOptions['getCurrentSubtitleRaw'];
getCurrentSubtitleAss: IpcDepsRuntimeOptions['getCurrentSubtitleAss'];
getSubtitleSidebarSnapshot?: IpcDepsRuntimeOptions['getSubtitleSidebarSnapshot'];
getSubtitleSidebarPlaybackState?: IpcDepsRuntimeOptions['getSubtitleSidebarPlaybackState'];
getSubtitleSidebarOpen?: IpcDepsRuntimeOptions['getSubtitleSidebarOpen'];
getPlaybackPaused: IpcDepsRuntimeOptions['getPlaybackPaused'];
focusMainWindow?: IpcDepsRuntimeOptions['focusMainWindow'];
@@ -277,6 +278,7 @@ export function createMainIpcRuntimeServiceDeps(
getCurrentSubtitleRaw: params.getCurrentSubtitleRaw,
getCurrentSubtitleAss: params.getCurrentSubtitleAss,
getSubtitleSidebarSnapshot: params.getSubtitleSidebarSnapshot,
getSubtitleSidebarPlaybackState: params.getSubtitleSidebarPlaybackState,
getSubtitleSidebarOpen: params.getSubtitleSidebarOpen,
getPlaybackPaused: params.getPlaybackPaused,
getSubtitlePosition: params.getSubtitlePosition,
@@ -44,7 +44,6 @@ export type JellyfinRemoteComposerOptions = ComposerInputs<{
>[0]['jellyfinTicksToSeconds'];
getActivePlayback: JellyfinRemoteGeneralMainDeps['getActivePlayback'];
clearActivePlayback: JellyfinRemoteProgressMainDeps['clearActivePlayback'];
getSession: JellyfinRemoteProgressMainDeps['getSession'];
getNow: JellyfinRemoteProgressMainDeps['getNow'];
getLastProgressAtMs: Parameters<
typeof createBuildReportJellyfinRemoteProgressMainDepsHandler
@@ -74,7 +73,6 @@ export function composeJellyfinRemoteHandlers(
createBuildReportJellyfinRemoteProgressMainDepsHandler({
getActivePlayback: options.getActivePlayback,
clearActivePlayback: options.clearActivePlayback,
getSession: options.getSession,
getMpvClient: options.getMpvClient,
getNow: options.getNow,
getLastProgressAtMs: options.getLastProgressAtMs,
@@ -87,7 +85,6 @@ export function composeJellyfinRemoteHandlers(
createBuildReportJellyfinRemoteStoppedMainDepsHandler({
getActivePlayback: options.getActivePlayback,
clearActivePlayback: options.clearActivePlayback,
getSession: options.getSession,
getMpvClient: options.getMpvClient,
getNow: options.getNow,
ticksPerSecond: options.ticksPerSecond,
+22 -13
View File
@@ -1,6 +1,7 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { createHandleJellyfinListCommands } from './jellyfin-cli-list';
import type { JellyfinCliResponse } from '../../shared/jellyfin-cli-response';
const baseSession = {
serverUrl: 'http://localhost',
@@ -24,7 +25,7 @@ test('list handler no-ops when no list command is set', async () => {
listJellyfinLibraries: async () => [],
listJellyfinItems: async () => [],
listJellyfinSubtitleTracks: async () => [],
writeJellyfinPreviewAuth: () => {},
writeJellyfinResponse: () => {},
logInfo: () => {},
});
@@ -42,13 +43,14 @@ test('list handler no-ops when no list command is set', async () => {
assert.equal(handled, false);
});
test('list handler logs libraries', async () => {
test('list handler logs libraries and writes them to the response path', async () => {
const logs: string[] = [];
const writes: Array<{ path: string; response: JellyfinCliResponse }> = [];
const handler = createHandleJellyfinListCommands({
listJellyfinLibraries: async () => [{ id: 'lib1', name: 'Anime', collectionType: 'tvshows' }],
listJellyfinItems: async () => [],
listJellyfinSubtitleTracks: async () => [],
writeJellyfinPreviewAuth: () => {},
writeJellyfinResponse: (path, response) => writes.push({ path, response }),
logInfo: (message) => logs.push(message),
});
@@ -57,6 +59,7 @@ test('list handler logs libraries', async () => {
jellyfinLibraries: true,
jellyfinItems: false,
jellyfinSubtitles: false,
jellyfinResponsePath: '/tmp/reply.json',
} as never,
session: baseSession,
clientInfo: baseClientInfo,
@@ -65,6 +68,12 @@ test('list handler logs libraries', async () => {
assert.equal(handled, true);
assert.ok(logs.some((line) => line.includes('Jellyfin library: Anime [lib1] (tvshows)')));
assert.deepEqual(writes, [
{
path: '/tmp/reply.json',
response: { libraries: [{ id: 'lib1', name: 'Anime', collectionType: 'tvshows' }] },
},
]);
});
test('list handler resolves items using default library id', async () => {
@@ -72,6 +81,7 @@ test('list handler resolves items using default library id', async () => {
let usedRecursive: boolean | undefined;
let usedIncludeItemTypes: string | undefined;
const logs: string[] = [];
const writes: JellyfinCliResponse[] = [];
const handler = createHandleJellyfinListCommands({
listJellyfinLibraries: async () => [],
listJellyfinItems: async (_session, _clientInfo, params) => {
@@ -81,7 +91,7 @@ test('list handler resolves items using default library id', async () => {
return [{ id: 'item1', title: 'Episode 1', type: 'Episode' }];
},
listJellyfinSubtitleTracks: async () => [],
writeJellyfinPreviewAuth: () => {},
writeJellyfinResponse: (_path, response) => writes.push(response),
logInfo: (message) => logs.push(message),
});
@@ -95,6 +105,7 @@ test('list handler resolves items using default library id', async () => {
jellyfinLimit: 10,
jellyfinRecursive: false,
jellyfinIncludeItemTypes: 'Series,Movie,Folder',
jellyfinResponsePath: '/tmp/reply.json',
} as never,
session: baseSession,
clientInfo: baseClientInfo,
@@ -108,6 +119,7 @@ test('list handler resolves items using default library id', async () => {
assert.equal(usedRecursive, false);
assert.equal(usedIncludeItemTypes, 'Series,Movie,Folder');
assert.ok(logs.some((line) => line.includes('Jellyfin item: Episode 1 [item1] (Episode)')));
assert.deepEqual(writes, [{ items: [{ id: 'item1', title: 'Episode 1', type: 'Episode' }] }]);
});
test('list handler throws when items command has no library id', async () => {
@@ -115,7 +127,7 @@ test('list handler throws when items command has no library id', async () => {
listJellyfinLibraries: async () => [],
listJellyfinItems: async () => [],
listJellyfinSubtitleTracks: async () => [],
writeJellyfinPreviewAuth: () => {},
writeJellyfinResponse: () => {},
logInfo: () => {},
});
@@ -144,7 +156,7 @@ test('list handler logs subtitle urls only when requested', async () => {
{ index: 1, deliveryUrl: 'http://localhost/sub1.srt', language: 'eng' },
{ index: 2, language: 'jpn' },
],
writeJellyfinPreviewAuth: () => {},
writeJellyfinResponse: () => {},
logInfo: (message) => logs.push(message),
});
@@ -170,7 +182,7 @@ test('list handler throws when subtitle command has no item id', async () => {
listJellyfinLibraries: async () => [],
listJellyfinItems: async () => [],
listJellyfinSubtitleTracks: async () => [],
writeJellyfinPreviewAuth: () => {},
writeJellyfinResponse: () => {},
logInfo: () => {},
});
@@ -190,16 +202,13 @@ test('list handler throws when subtitle command has no item id', async () => {
});
test('list handler writes preview auth payload to response path', async () => {
const writes: Array<{
path: string;
payload: { serverUrl: string; accessToken: string; userId: string };
}> = [];
const writes: Array<{ path: string; payload: JellyfinCliResponse }> = [];
const logs: string[] = [];
const handler = createHandleJellyfinListCommands({
listJellyfinLibraries: async () => [],
listJellyfinItems: async () => [],
listJellyfinSubtitleTracks: async () => [],
writeJellyfinPreviewAuth: (responsePath, payload) => {
writeJellyfinResponse: (responsePath, payload) => {
writes.push({ path: responsePath, payload });
},
logInfo: (message) => logs.push(message),
@@ -234,7 +243,7 @@ test('list handler throws when preview auth command has no response path', async
listJellyfinLibraries: async () => [],
listJellyfinItems: async () => [],
listJellyfinSubtitleTracks: async () => [],
writeJellyfinPreviewAuth: () => {},
writeJellyfinResponse: () => {},
logInfo: () => {},
});
+18 -9
View File
@@ -1,4 +1,5 @@
import type { CliArgs } from '../../cli/args';
import type { JellyfinCliResponse } from '../../shared/jellyfin-cli-response';
type JellyfinSession = {
serverUrl: string;
@@ -17,12 +18,6 @@ type JellyfinConfig = {
defaultLibraryId: string;
};
type JellyfinPreviewAuthPayload = {
serverUrl: string;
accessToken: string;
userId: string;
};
export function createHandleJellyfinListCommands(deps: {
listJellyfinLibraries: (
session: JellyfinSession,
@@ -56,7 +51,7 @@ export function createHandleJellyfinListCommands(deps: {
deliveryUrl?: string | null;
}>
>;
writeJellyfinPreviewAuth: (responsePath: string, payload: JellyfinPreviewAuthPayload) => void;
writeJellyfinResponse: (responsePath: string, response: JellyfinCliResponse) => void;
logInfo: (message: string) => void;
}) {
return async (params: {
@@ -66,13 +61,13 @@ export function createHandleJellyfinListCommands(deps: {
jellyfinConfig: JellyfinConfig;
}): Promise<boolean> => {
const { args, session, clientInfo, jellyfinConfig } = params;
const responsePath = args.jellyfinResponsePath?.trim();
if (args.jellyfinPreviewAuth) {
const responsePath = args.jellyfinResponsePath?.trim();
if (!responsePath) {
throw new Error('Missing --jellyfin-response-path for --jellyfin-preview-auth.');
}
deps.writeJellyfinPreviewAuth(responsePath, {
deps.writeJellyfinResponse(responsePath, {
serverUrl: session.serverUrl,
accessToken: session.accessToken,
userId: session.userId,
@@ -83,6 +78,15 @@ export function createHandleJellyfinListCommands(deps: {
if (args.jellyfinLibraries) {
const libraries = await deps.listJellyfinLibraries(session, clientInfo);
if (responsePath) {
deps.writeJellyfinResponse(responsePath, {
libraries: libraries.map((library) => ({
id: library.id,
name: library.name,
collectionType: library.collectionType || library.type || 'unknown',
})),
});
}
if (libraries.length === 0) {
deps.logInfo('No Jellyfin libraries found.');
return true;
@@ -109,6 +113,11 @@ export function createHandleJellyfinListCommands(deps: {
recursive: args.jellyfinRecursive,
includeItemTypes: args.jellyfinIncludeItemTypes,
});
if (responsePath) {
deps.writeJellyfinResponse(responsePath, {
items: items.map((item) => ({ id: item.id, title: item.title, type: item.type })),
});
}
if (items.length === 0) {
deps.logInfo('No Jellyfin items found for the selected library/search.');
return true;
+2 -2
View File
@@ -32,8 +32,8 @@ export function createBuildHandleJellyfinListCommandsMainDepsHandler(
deps.listJellyfinItems(session, clientInfo, params),
listJellyfinSubtitleTracks: (session, clientInfo, itemId) =>
deps.listJellyfinSubtitleTracks(session, clientInfo, itemId),
writeJellyfinPreviewAuth: (responsePath, payload) =>
deps.writeJellyfinPreviewAuth(responsePath, payload),
writeJellyfinResponse: (responsePath, response) =>
deps.writeJellyfinResponse(responsePath, response),
logInfo: (message: string) => deps.logInfo(message),
});
}
@@ -1,4 +1,5 @@
import type { CliArgs } from '../../cli/args';
import type { JellyfinCliResponse } from '../../shared/jellyfin-cli-response';
type JellyfinConfigBase = {
serverUrl?: string;
@@ -37,6 +38,7 @@ export type RunJellyfinCommandMainDeps<TClientInfo, TConfig extends JellyfinConf
clientInfo: TClientInfo;
jellyfinConfig: TConfig;
}) => Promise<boolean>;
writeJellyfinResponse: (responsePath: string, response: JellyfinCliResponse) => void;
};
export function createBuildRunJellyfinCommandMainDepsHandler<
@@ -51,5 +53,7 @@ export function createBuildRunJellyfinCommandMainDepsHandler<
handleRemoteAnnounceCommand: (args: CliArgs) => deps.handleRemoteAnnounceCommand(args),
handleListCommands: (params) => deps.handleListCommands(params),
handlePlayCommand: (params) => deps.handlePlayCommand(params),
writeJellyfinResponse: (responsePath, response) =>
deps.writeJellyfinResponse(responsePath, response),
});
}
@@ -66,6 +66,7 @@ test('run jellyfin command returns after auth branch handles command', async ()
calls.push('list');
return false;
},
writeJellyfinResponse: () => {},
handlePlayCommand: async () => {
calls.push('play');
return false;
@@ -76,7 +77,8 @@ test('run jellyfin command returns after auth branch handles command', async ()
assert.deepEqual(calls, ['auth']);
});
test('run jellyfin command throws when session missing after auth', async () => {
test('run jellyfin command throws when session missing and writes the error reply', async () => {
const writes: Array<{ path: string; response: unknown }> = [];
const run = createRunJellyfinCommandHandler({
getJellyfinConfig: () => ({ serverUrl: '', accessToken: '', userId: '' }),
defaultServerUrl: '',
@@ -84,10 +86,20 @@ test('run jellyfin command throws when session missing after auth', async () =>
handleAuthCommands: async () => false,
handleRemoteAnnounceCommand: async () => false,
handleListCommands: async () => false,
writeJellyfinResponse: (path, response) => writes.push({ path, response }),
handlePlayCommand: async () => false,
});
await assert.rejects(() => run(createArgs()), /Missing Jellyfin session/);
await assert.rejects(
() => run(createArgs({ jellyfinResponsePath: '/tmp/reply.json' })),
/Missing Jellyfin session/,
);
assert.deepEqual(writes, [
{
path: '/tmp/reply.json',
response: { error: 'Missing Jellyfin session. Run --jellyfin-login first.' },
},
]);
});
test('run jellyfin command dispatches remote/list/play in order until handled', async () => {
@@ -116,6 +128,7 @@ test('run jellyfin command dispatches remote/list/play in order until handled',
seenServerUrls.push(session.serverUrl);
return true;
},
writeJellyfinResponse: () => {},
handlePlayCommand: async () => {
calls.push('play');
return false;
+22 -1
View File
@@ -1,4 +1,5 @@
import type { CliArgs } from '../../cli/args';
import type { JellyfinCliResponse } from '../../shared/jellyfin-cli-response';
type JellyfinConfigBase = {
serverUrl?: string;
@@ -40,8 +41,9 @@ export function createRunJellyfinCommandHandler<
clientInfo: TClientInfo;
jellyfinConfig: TConfig;
}) => Promise<boolean>;
writeJellyfinResponse: (responsePath: string, response: JellyfinCliResponse) => void;
}) {
return async (args: CliArgs): Promise<void> => {
const run = async (args: CliArgs): Promise<void> => {
const jellyfinConfig = deps.getJellyfinConfig();
const serverUrl =
args.jellyfinServer?.trim() || jellyfinConfig.serverUrl || deps.defaultServerUrl;
@@ -97,4 +99,23 @@ export function createRunJellyfinCommandHandler<
return;
}
};
// A launcher waiting on --jellyfin-response-path gets the failure reason instead of a timeout.
return async (args: CliArgs): Promise<void> => {
try {
await run(args);
} catch (error) {
const responsePath = args.jellyfinResponsePath?.trim();
if (responsePath) {
try {
deps.writeJellyfinResponse(responsePath, {
error: error instanceof Error ? error.message : String(error),
});
} catch {
// Keep the original failure; the launcher falls back to its timeout.
}
}
throw error;
}
};
}
@@ -18,7 +18,7 @@ export function createBuildPlayJellyfinItemInMpvMainDepsHandler(
preloadExternalSubtitles: (params) => deps.preloadExternalSubtitles(params),
setActivePlayback: (state) => deps.setActivePlayback(state),
setLastProgressAtMs: (value: number) => deps.setLastProgressAtMs(value),
reportPlaying: (payload) => deps.reportPlaying(payload),
createPlaybackReporter: (params) => deps.createPlaybackReporter(params),
showMpvOsd: (text: string) => deps.showMpvOsd(text),
recordJellyfinPlaybackMetadata: deps.recordJellyfinPlaybackMetadata
? (metadata) => deps.recordJellyfinPlaybackMetadata!(metadata)
@@ -1,6 +1,7 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import type { JellyfinPlaybackPlan } from '../../core/services/jellyfin';
import type { JellyfinTimelinePlaybackState } from '../../core/services/jellyfin-playback-reporter';
import { createPlayJellyfinItemInMpvHandler } from './jellyfin-playback-launch';
type Deps = Parameters<typeof createPlayJellyfinItemInMpvHandler>[0];
@@ -46,7 +47,13 @@ function makeHarness(options: { plan?: Partial<JellyfinPlaybackPlan>; deps?: Par
const commands: Array<Array<string | number>> = [];
const events: string[] = [];
const activeStates: Array<Parameters<Deps['setActivePlayback']>[0]> = [];
const reports: Array<Parameters<Deps['reportPlaying']>[0]> = [];
const reports: JellyfinTimelinePlaybackState[] = [];
const reporterParams: Array<Parameters<Deps['createPlaybackReporter']>[0]> = [];
const reporter = {
reportPlaying: async (state: JellyfinTimelinePlaybackState) => void reports.push(state),
reportProgress: async () => {},
reportStopped: async () => {},
};
const handler = createPlayJellyfinItemInMpvHandler({
ensureMpvConnectedForPlayback: async () => true,
@@ -67,7 +74,10 @@ function makeHarness(options: { plan?: Partial<JellyfinPlaybackPlan>; deps?: Par
events.push(`active:${String(state.loadedMediaPath)}`);
},
setLastProgressAtMs: () => {},
reportPlaying: (payload) => reports.push(payload),
createPlaybackReporter: (params) => {
reporterParams.push(params);
return reporter;
},
showMpvOsd: () => {},
...options.deps,
});
@@ -77,6 +87,8 @@ function makeHarness(options: { plan?: Partial<JellyfinPlaybackPlan>; deps?: Par
events,
activeStates,
reports,
reporter,
reporterParams,
play: (params: Partial<PlayParams> = {}) =>
handler({
session: baseSession,
@@ -141,6 +153,8 @@ test('playback handler disables mpv subtitle selection, then loads media and rep
assert.equal(harness.activeStates.length, 1);
assert.equal(harness.activeStates[0]?.playMethod, 'DirectPlay');
assert.equal(harness.activeStates[0]?.lastKnownPositionSeconds, 1.2);
assert.equal(harness.activeStates[0]?.reporter, harness.reporter);
assert.deepEqual(harness.reporterParams, [{ session: baseSession, clientInfo: baseClientInfo }]);
assert.equal(harness.reports.length, 1);
assert.equal(harness.reports[0]?.eventName, 'start');
assert.equal(harness.reports[0]?.positionTicks, 12_000_000);
+14 -11
View File
@@ -1,6 +1,7 @@
import type { JellyfinAuthSession, JellyfinPlaybackPlan } from '../../core/services/jellyfin';
import type { JellyfinConfig } from '../../types';
import type { MpvRuntimeClientLike } from '../../core/services/mpv';
import type { JellyfinPlaybackReporterLike } from './jellyfin-remote-commands';
type JellyfinClientInfo = {
clientName: string;
@@ -10,6 +11,7 @@ type JellyfinClientInfo = {
type ActivePlaybackState = {
itemId: string;
reporter: JellyfinPlaybackReporterLike;
mediaSourceId: undefined;
audioStreamIndex?: number | null;
subtitleStreamIndex?: number | null;
@@ -134,16 +136,12 @@ export function createPlayJellyfinItemInMpvHandler(deps: {
}) => void | Promise<void>;
setActivePlayback: (state: ActivePlaybackState) => void;
setLastProgressAtMs: (value: number) => void;
reportPlaying: (payload: {
itemId: string;
mediaSourceId: undefined;
playMethod: 'DirectPlay' | 'Transcode';
positionTicks?: number;
isPaused?: boolean;
audioStreamIndex?: number | null;
subtitleStreamIndex?: number | null;
eventName: 'start';
}) => void;
// Reports go out as the device and account that started this playback, for CLI play and
// casting alike, so Jellyfin keeps the resume point and played state either way.
createPlaybackReporter: (params: {
session: JellyfinAuthSession;
clientInfo: JellyfinClientInfo;
}) => JellyfinPlaybackReporterLike;
showMpvOsd: (text: string) => void;
recordJellyfinPlaybackMetadata?: (
metadata: JellyfinPlaybackStatsMetadata,
@@ -210,8 +208,13 @@ export function createPlayJellyfinItemInMpvHandler(deps: {
itemId: params.itemId,
}),
);
const reporter = deps.createPlaybackReporter({
session: params.session,
clientInfo: params.clientInfo,
});
deps.setActivePlayback({
itemId: params.itemId,
reporter,
mediaSourceId: undefined,
audioStreamIndex: plan.audioStreamIndex,
subtitleStreamIndex: plan.subtitleStreamIndex,
@@ -237,7 +240,7 @@ export function createPlayJellyfinItemInMpvHandler(deps: {
);
deps.showVisibleOverlay();
deps.reportPlaying({
void reporter.reportPlaying({
itemId: params.itemId,
mediaSourceId: undefined,
playMethod,
@@ -9,6 +9,12 @@ import {
type JellyfinRemotePlayHandlerDeps,
} from './jellyfin-remote-commands';
const noopReporter = {
reportPlaying: async () => {},
reportProgress: async () => {},
reportStopped: async () => {},
};
test('getConfiguredJellyfinSession returns null for incomplete config', () => {
assert.equal(
getConfiguredJellyfinSession({
@@ -122,7 +128,11 @@ test('createHandleJellyfinRemotePlay logs and skips payload without item id', as
test('createHandleJellyfinRemotePlay ignores duplicate play for active item', async () => {
const { handlePlay, played } = makePlayHandler({
getActivePlayback: () => ({ itemId: 'item-1', playMethod: 'DirectPlay' }),
getActivePlayback: () => ({
itemId: 'item-1',
playMethod: 'DirectPlay',
reporter: noopReporter,
}),
});
await handlePlay({ ItemIds: ['item-1'] });
@@ -207,6 +217,7 @@ test('createHandleJellyfinRemoteGeneralCommand mutates active playback indices',
const playback: ActiveJellyfinRemotePlaybackState = {
itemId: 'item-1',
playMethod: 'DirectPlay',
reporter: noopReporter,
audioStreamIndex: null,
subtitleStreamIndex: null,
};
@@ -1,5 +1,18 @@
import type { JellyfinTimelinePlaybackState } from '../../core/services/jellyfin-playback-reporter';
type JellyfinTimelineReport = (state: JellyfinTimelinePlaybackState) => Promise<unknown>;
export type JellyfinPlaybackReporterLike = {
reportPlaying: JellyfinTimelineReport;
reportProgress: JellyfinTimelineReport;
reportStopped: JellyfinTimelineReport;
};
export type ActiveJellyfinRemotePlaybackState = {
itemId: string;
// Bound at launch to the session that started playback, so reports flow for CLI play and
// casting alike, independent of the cast-control websocket.
reporter: JellyfinPlaybackReporterLike;
mediaSourceId?: string;
audioStreamIndex?: number | null;
subtitleStreamIndex?: number | null;
@@ -53,7 +53,6 @@ export function createBuildReportJellyfinRemoteProgressMainDepsHandler(
return (): JellyfinRemoteProgressReporterDeps => ({
getActivePlayback: () => deps.getActivePlayback(),
clearActivePlayback: () => deps.clearActivePlayback(),
getSession: () => deps.getSession(),
getMpvClient: () => deps.getMpvClient(),
getNow: () => deps.getNow(),
getLastProgressAtMs: () => deps.getLastProgressAtMs(),
@@ -70,7 +69,6 @@ export function createBuildReportJellyfinRemoteStoppedMainDepsHandler(
return (): JellyfinRemoteStoppedReporterDeps => ({
getActivePlayback: () => deps.getActivePlayback(),
clearActivePlayback: () => deps.clearActivePlayback(),
getSession: () => deps.getSession(),
getMpvClient: () => deps.getMpvClient(),
getNow: deps.getNow ? () => deps.getNow?.() ?? Date.now() : undefined,
ticksPerSecond: deps.ticksPerSecond,
+103 -167
View File
@@ -8,26 +8,31 @@ import {
secondsToJellyfinTicks,
shouldAutoLoadSecondarySubTrackForJellyfinPlayback,
} from './jellyfin-remote-playback';
import type { ActiveJellyfinRemotePlaybackState } from './jellyfin-remote-commands';
import type {
ActiveJellyfinRemotePlaybackState,
JellyfinPlaybackReporterLike,
} from './jellyfin-remote-commands';
type ProgressDeps = Parameters<typeof createReportJellyfinRemoteProgressHandler>[0];
type StoppedDeps = Parameters<typeof createReportJellyfinRemoteStoppedHandler>[0];
type Session = NonNullable<ReturnType<ProgressDeps['getSession']>>;
const TICKS_PER_SECOND = 10_000_000;
const makeReporter = (
overrides: Partial<JellyfinPlaybackReporterLike> = {},
): JellyfinPlaybackReporterLike => ({
reportPlaying: async () => {},
reportProgress: async () => {},
reportStopped: async () => {},
...overrides,
});
const makePlayback = (
overrides: Partial<ActiveJellyfinRemotePlaybackState> = {},
): ActiveJellyfinRemotePlaybackState => ({
itemId: 'item-1',
playMethod: 'DirectPlay',
...overrides,
});
const makeSession = (overrides: Partial<Session> = {}): Session => ({
isConnected: () => true,
reportProgress: async () => {},
reportStopped: async () => {},
reporter: makeReporter(),
...overrides,
});
@@ -35,7 +40,6 @@ function makeProgressDeps(overrides: Partial<ProgressDeps> = {}): ProgressDeps {
return {
getActivePlayback: () => makePlayback(),
clearActivePlayback: () => {},
getSession: () => makeSession(),
getMpvClient: () => ({ requestProperty: async () => 1 }),
getNow: () => 5000,
getLastProgressAtMs: () => 0,
@@ -51,7 +55,6 @@ function makeStoppedDeps(overrides: Partial<StoppedDeps> = {}): StoppedDeps {
return {
getActivePlayback: () => makePlayback({ loadedMediaPath: 'https://stream.example/video.m3u8' }),
clearActivePlayback: () => {},
getSession: () => makeSession(),
getMpvClient: () => ({ currentTimePos: 0 }),
ticksPerSecond: TICKS_PER_SECOND,
logDebug: () => {},
@@ -69,29 +72,21 @@ test('shouldAutoLoadSecondarySubTrackForJellyfinPlayback suppresses generic seco
assert.equal(shouldAutoLoadSecondarySubTrackForJellyfinPlayback(null, '/tmp/local.mkv'), true);
assert.equal(
shouldAutoLoadSecondarySubTrackForJellyfinPlayback(
{ itemId: 'item-1', playMethod: 'DirectPlay', loadedMediaPath: null },
makePlayback({ loadedMediaPath: null }),
'http://pve-main:8096/Videos/item/stream',
),
false,
);
assert.equal(
shouldAutoLoadSecondarySubTrackForJellyfinPlayback(
{
itemId: 'item-1',
playMethod: 'DirectPlay',
loadedMediaPath: 'http://pve-main:8096/Videos/item/stream',
},
makePlayback({ loadedMediaPath: 'http://pve-main:8096/Videos/item/stream' }),
'http://pve-main:8096/Videos/item/stream',
),
false,
);
assert.equal(
shouldAutoLoadSecondarySubTrackForJellyfinPlayback(
{
itemId: 'item-1',
playMethod: 'DirectPlay',
loadedMediaPath: 'http://pve-main:8096/Videos/item/stream',
},
makePlayback({ loadedMediaPath: 'http://pve-main:8096/Videos/item/stream' }),
'/tmp/local.mkv',
),
true,
@@ -100,19 +95,21 @@ test('shouldAutoLoadSecondarySubTrackForJellyfinPlayback suppresses generic seco
test('createReportJellyfinRemoteProgressHandler reports playback progress', async () => {
let lastProgressAtMs = 0;
const reportPayloads: Array<{ itemId: string; positionTicks: number; isPaused: boolean }> = [];
const reportPayloads: Array<{ itemId: string; positionTicks?: number; isPaused?: boolean }> = [];
const reportProgress = createReportJellyfinRemoteProgressHandler(
makeProgressDeps({
getSession: () =>
makeSession({
reportProgress: async (payload) => {
reportPayloads.push({
itemId: payload.itemId,
positionTicks: payload.positionTicks,
isPaused: payload.isPaused,
});
},
getActivePlayback: () =>
makePlayback({
reporter: makeReporter({
reportProgress: async (payload) => {
reportPayloads.push({
itemId: payload.itemId,
positionTicks: payload.positionTicks,
isPaused: payload.isPaused,
});
},
}),
}),
getMpvClient: () => ({
requestProperty: async (name: string) => (name === 'time-pos' ? 2.5 : true),
@@ -135,43 +132,18 @@ test('createReportJellyfinRemoteProgressHandler reports playback progress', asyn
assert.equal(lastProgressAtMs, 5000);
});
test('createReportJellyfinRemoteProgressHandler reports while remote websocket is disconnected', async () => {
const reportPayloads: Array<{ positionTicks: number; isPaused: boolean }> = [];
const reportProgress = createReportJellyfinRemoteProgressHandler(
makeProgressDeps({
getSession: () =>
makeSession({
isConnected: () => false,
reportProgress: async (payload) => {
reportPayloads.push({
positionTicks: payload.positionTicks,
isPaused: payload.isPaused,
});
},
}),
getMpvClient: () => ({
currentTimePos: 42,
requestProperty: async (name: string) => (name === 'pause' ? false : 42),
}),
}),
);
await reportProgress(true);
assert.deepEqual(reportPayloads, [{ positionTicks: 420_000_000, isPaused: false }]);
});
test('createReportJellyfinRemoteProgressHandler normalizes mpv pause strings', async () => {
const reportPayloads: Array<{ isPaused: boolean }> = [];
const reportPayloads: Array<{ isPaused?: boolean }> = [];
const reportProgress = createReportJellyfinRemoteProgressHandler(
makeProgressDeps({
getSession: () =>
makeSession({
reportProgress: async (payload) => {
reportPayloads.push({ isPaused: payload.isPaused });
},
getActivePlayback: () =>
makePlayback({
reporter: makeReporter({
reportProgress: async (payload) => {
reportPayloads.push({ isPaused: payload.isPaused });
},
}),
}),
getMpvClient: () => ({
requestProperty: async (name: string) => (name === 'pause' ? 'yes' : 3),
@@ -188,11 +160,13 @@ test('createReportJellyfinRemoteProgressHandler respects debounce interval', asy
let called = false;
const reportProgress = createReportJellyfinRemoteProgressHandler(
makeProgressDeps({
getSession: () =>
makeSession({
reportProgress: async () => {
called = true;
},
getActivePlayback: () =>
makePlayback({
reporter: makeReporter({
reportProgress: async () => {
called = true;
},
}),
}),
getNow: () => 4000,
getLastProgressAtMs: () => 3500,
@@ -207,18 +181,20 @@ test('createReportJellyfinRemoteProgressHandler reports mpv seek jumps during de
let now = 5000;
let lastProgressAtMs = 0;
let position = 10;
const reportPayloads: Array<{ positionTicks: number; eventName: string }> = [];
const reportPayloads: Array<{ positionTicks?: number; eventName?: string }> = [];
const reportProgress = createReportJellyfinRemoteProgressHandler(
makeProgressDeps({
getSession: () =>
makeSession({
reportProgress: async (payload) => {
reportPayloads.push({
positionTicks: payload.positionTicks,
eventName: payload.eventName,
});
},
getActivePlayback: () =>
makePlayback({
reporter: makeReporter({
reportProgress: async (payload) => {
reportPayloads.push({
positionTicks: payload.positionTicks,
eventName: payload.eventName,
});
},
}),
}),
getMpvClient: () => ({
currentTimePos: position,
@@ -253,20 +229,23 @@ test('createReportJellyfinRemoteStoppedHandler reports stop and clears playback'
} | null = null;
const reportStopped = createReportJellyfinRemoteStoppedHandler(
makeStoppedDeps({
getActivePlayback: () => makePlayback({ itemId: 'item-2', playMethod: 'Transcode' }),
getActivePlayback: () =>
makePlayback({
itemId: 'item-2',
playMethod: 'Transcode',
reporter: makeReporter({
reportStopped: async (payload) => {
stoppedPayload = {
itemId: payload.itemId,
positionTicks: payload.positionTicks,
failed: payload.failed,
};
},
}),
}),
clearActivePlayback: () => {
cleared = true;
},
getSession: () =>
makeSession({
reportStopped: async (payload) => {
stoppedPayload = {
itemId: payload.itemId,
positionTicks: payload.positionTicks,
failed: payload.failed,
};
},
}),
getMpvClient: () => ({
currentTimePos: 12.5,
requestProperty: async () => {
@@ -289,16 +268,18 @@ test('createReportJellyfinRemoteStoppedHandler clears aborted playback that neve
let cleared = false;
const reportStopped = createReportJellyfinRemoteStoppedHandler(
makeStoppedDeps({
getActivePlayback: () => makePlayback({ loadedMediaPath: null }),
getActivePlayback: () =>
makePlayback({
loadedMediaPath: null,
reporter: makeReporter({
reportStopped: async () => {
throw new Error('should not report stopped for unloaded media');
},
}),
}),
clearActivePlayback: () => {
cleared = true;
},
getSession: () =>
makeSession({
reportStopped: async () => {
throw new Error('should not report stopped for unloaded media');
},
}),
getMpvClient: () => null,
}),
);
@@ -308,43 +289,6 @@ test('createReportJellyfinRemoteStoppedHandler clears aborted playback that neve
assert.equal(cleared, true);
});
test('createReportJellyfinRemoteStoppedHandler reports stop while remote websocket is disconnected', async () => {
let cleared = false;
let stoppedPayload: {
itemId: string;
positionTicks?: number;
failed?: boolean;
} | null = null;
const reportStopped = createReportJellyfinRemoteStoppedHandler(
makeStoppedDeps({
clearActivePlayback: () => {
cleared = true;
},
getSession: () =>
makeSession({
isConnected: () => false,
reportStopped: async (payload) => {
stoppedPayload = {
itemId: payload.itemId,
positionTicks: payload.positionTicks,
failed: payload.failed,
};
},
}),
getMpvClient: () => ({ currentTimePos: 12.5 }),
}),
);
await reportStopped();
assert.deepEqual(stoppedPayload, {
itemId: 'item-1',
positionTicks: 125_000_000,
failed: false,
});
assert.equal(cleared, true);
});
test('createReportJellyfinRemoteStoppedHandler uses cached position after mpv unload reset', async () => {
let cleared = false;
const calls: Array<{ event: string; positionTicks?: number }> = [];
@@ -354,19 +298,18 @@ test('createReportJellyfinRemoteStoppedHandler uses cached position after mpv un
makePlayback({
loadedMediaPath: 'https://stream.example/video.m3u8',
lastKnownPositionSeconds: 72.25,
reporter: makeReporter({
reportProgress: async (payload) => {
calls.push({ event: 'progress', positionTicks: payload.positionTicks });
},
reportStopped: async (payload) => {
calls.push({ event: 'stopped', positionTicks: payload.positionTicks });
},
}),
}),
clearActivePlayback: () => {
cleared = true;
},
getSession: () =>
makeSession({
reportProgress: async (payload) => {
calls.push({ event: 'progress', positionTicks: payload.positionTicks });
},
reportStopped: async (payload) => {
calls.push({ event: 'stopped', positionTicks: payload.positionTicks });
},
}),
}),
);
@@ -423,16 +366,15 @@ test('createReportJellyfinRemoteStoppedHandler ignores startup stop churn before
makePlayback({
loadedMediaPath: 'https://stream.example/video.m3u8',
stopReportsAfterMs: 20_000,
reporter: makeReporter({
reportStopped: async () => {
stopped = true;
},
}),
}),
clearActivePlayback: () => {
cleared = true;
},
getSession: () =>
makeSession({
reportStopped: async () => {
stopped = true;
},
}),
getNow: () => 12_000,
}),
);
@@ -445,34 +387,28 @@ test('createReportJellyfinRemoteStoppedHandler ignores startup stop churn before
test('createReportJellyfinRemoteStoppedHandler clears playback before reporting and waits for in-flight progress', async () => {
const tracker = createJellyfinRemoteReportTracker();
let playback: { itemId: string; playMethod: 'DirectPlay'; loadedMediaPath: string } | null = {
itemId: 'item-1',
playMethod: 'DirectPlay',
loadedMediaPath: 'http://pve-main:8096/Videos/item-1/stream',
};
const calls: string[] = [];
let releaseProgress: () => void = () => undefined;
const progressGate = new Promise<void>((resolve) => {
releaseProgress = resolve;
});
const session = {
isConnected: () => true,
reportProgress: async ({ eventName }: { eventName: string }) => {
calls.push(`progress:${eventName}:${playback ? 'active' : 'cleared'}`);
if (calls.length === 1) await progressGate;
return true;
},
reportStopped: async () => {
calls.push(`stopped:${playback ? 'active' : 'cleared'}`);
return true;
},
};
let playback: ActiveJellyfinRemotePlaybackState | null = makePlayback({
loadedMediaPath: 'http://pve-main:8096/Videos/item-1/stream',
reporter: makeReporter({
reportProgress: async ({ eventName }) => {
calls.push(`progress:${eventName}:${playback ? 'active' : 'cleared'}`);
if (calls.length === 1) await progressGate;
},
reportStopped: async () => {
calls.push(`stopped:${playback ? 'active' : 'cleared'}`);
},
}),
});
const shared = {
getActivePlayback: () => playback,
clearActivePlayback: () => {
playback = null;
},
getSession: () => session,
getMpvClient: () => ({ currentTimePos: 42 }),
ticksPerSecond: 10_000_000,
logDebug: () => undefined,
+3 -38
View File
@@ -1,29 +1,5 @@
import type { ActiveJellyfinRemotePlaybackState } from './jellyfin-remote-commands';
type JellyfinRemoteSessionLike = {
isConnected: () => boolean;
reportProgress: (payload: {
itemId: string;
mediaSourceId?: string;
positionTicks: number;
isPaused: boolean;
playMethod: 'DirectPlay' | 'Transcode';
audioStreamIndex?: number | null;
subtitleStreamIndex?: number | null;
eventName: 'TimeUpdate';
}) => Promise<unknown>;
reportStopped: (payload: {
itemId: string;
mediaSourceId?: string;
positionTicks?: number;
failed?: boolean;
playMethod: 'DirectPlay' | 'Transcode';
audioStreamIndex?: number | null;
subtitleStreamIndex?: number | null;
eventName: 'stop';
}) => Promise<unknown>;
};
type MpvClientLike = {
currentTimePos?: number;
requestProperty?: (name: string) => Promise<unknown>;
@@ -160,7 +136,6 @@ export function createJellyfinRemoteReportTracker(): JellyfinRemoteReportTracker
export type JellyfinRemoteProgressReporterDeps = {
getActivePlayback: () => ActiveJellyfinRemotePlaybackState | null;
clearActivePlayback: () => void;
getSession: () => JellyfinRemoteSessionLike | null;
getMpvClient: () => MpvClientLike | null;
getNow: () => number;
getLastProgressAtMs: () => number;
@@ -179,9 +154,6 @@ export function createReportJellyfinRemoteProgressHandler(
const report = async (force: boolean): Promise<void> => {
const playback = deps.getActivePlayback();
if (!playback) return;
const session = deps.getSession();
// Timeline posts are HTTP requests; keep them flowing while the remote websocket reconnects.
if (!session) return;
const now = deps.getNow();
try {
const mpvClient = deps.getMpvClient();
@@ -201,7 +173,7 @@ export function createReportJellyfinRemoteProgressHandler(
return;
}
const paused = await mpvClient?.requestProperty?.('pause');
await session.reportProgress({
await playback.reporter.reportProgress({
itemId: playback.itemId,
mediaSourceId: playback.mediaSourceId,
positionTicks: secondsToJellyfinTicks(positionSeconds, deps.ticksPerSecond),
@@ -228,7 +200,6 @@ export function createReportJellyfinRemoteProgressHandler(
export type JellyfinRemoteStoppedReporterDeps = {
getActivePlayback: () => ActiveJellyfinRemotePlaybackState | null;
clearActivePlayback: () => void;
getSession: () => JellyfinRemoteSessionLike | null;
getMpvClient: () => MpvClientLike | null;
getNow?: () => number;
ticksPerSecond: number;
@@ -252,12 +223,6 @@ export function createReportJellyfinRemoteStoppedHandler(deps: JellyfinRemoteSto
) {
return;
}
const session = deps.getSession();
// Timeline posts are HTTP requests; keep them flowing while the remote websocket reconnects.
if (!session) {
deps.clearActivePlayback();
return;
}
// Clear before any network call so progress ticks fired during the stop find nothing to
// report, then let reports already in flight finish so none can arrive after the stop.
deps.clearActivePlayback();
@@ -267,7 +232,7 @@ export function createReportJellyfinRemoteStoppedHandler(deps: JellyfinRemoteSto
const positionSeconds = resolveReportablePositionSeconds(playback, observedPositionSeconds);
const positionTicks = secondsToJellyfinTicks(positionSeconds, deps.ticksPerSecond);
try {
await session.reportProgress({
await playback.reporter.reportProgress({
itemId: playback.itemId,
mediaSourceId: playback.mediaSourceId,
positionTicks,
@@ -280,7 +245,7 @@ export function createReportJellyfinRemoteStoppedHandler(deps: JellyfinRemoteSto
} catch (error) {
deps.logDebug('Failed to report Jellyfin remote final progress', error);
}
const reported = await session.reportStopped({
const reported = await playback.reporter.reportStopped({
itemId: playback.itemId,
mediaSourceId: playback.mediaSourceId,
positionTicks,
+2
View File
@@ -294,6 +294,8 @@ const electronAPI: ElectronAPI = {
ipcRenderer.invoke(IPC_CHANNELS.request.getSubtitleSidebarOpen),
getSubtitleSidebarSnapshot: () =>
ipcRenderer.invoke(IPC_CHANNELS.request.getSubtitleSidebarSnapshot),
getSubtitleSidebarPlaybackState: () =>
ipcRenderer.invoke(IPC_CHANNELS.request.getSubtitleSidebarPlaybackState),
copySubtitleSidebarSelection: async (text: unknown) => {
if (typeof text !== 'string') throw new TypeError('Subtitle selection must be text.');
clipboard.writeText(text);
+137 -7
View File
@@ -153,6 +153,10 @@ function installKeyboardTestGlobals() {
let selectionAddCount = 0;
let popupVisible = false;
let hachidoriPaneOpen = false;
const hachidoriHost = {
shadowRoot: { querySelectorAll: () => [{ hidden: false }] },
};
const popupIframe = {
tagName: 'IFRAME',
@@ -287,7 +291,10 @@ function installKeyboardTestGlobals() {
listeners.push(listener);
documentListeners.set(type, listeners);
},
querySelectorAll: () => {
querySelectorAll: (selector: string) => {
if (selector === HACHIDORI_HOST_SELECTOR) {
return hachidoriPaneOpen ? [hachidoriHost] : [];
}
if (popupVisible) {
return [popupIframe];
}
@@ -316,8 +323,9 @@ function installKeyboardTestGlobals() {
shiftKey?: boolean;
repeat?: boolean;
target?: unknown;
}): void {
}): boolean {
const listeners = documentListeners.get('keydown') ?? [];
let prevented = false;
const keyboardEvent = {
key: event.key,
code: event.code,
@@ -326,12 +334,15 @@ function installKeyboardTestGlobals() {
altKey: event.altKey ?? false,
shiftKey: event.shiftKey ?? false,
repeat: event.repeat ?? false,
preventDefault: () => {},
preventDefault: () => {
prevented = true;
},
target: event.target ?? null,
};
for (const listener of listeners) {
listener(keyboardEvent);
}
return prevented;
}
function dispatchMousedown(event: {
@@ -453,6 +464,9 @@ function installKeyboardTestGlobals() {
setPopupVisible: (value: boolean) => {
popupVisible = value;
},
setHachidoriPaneOpen: (value: boolean) => {
hachidoriPaneOpen = value;
},
setStatsToggleKey: (value: string) => {
statsToggleKey = value;
},
@@ -485,6 +499,12 @@ function installKeyboardTestGlobals() {
: null;
return target;
},
createTextInputTarget: () => {
const target = new TestElement();
target.tagName = 'INPUT';
target.closest = (selector: string) => (selector.includes('input') ? target : null);
return target;
},
setGetSessionBindings: (value: () => Promise<CompiledSessionBinding[]>) => {
getSessionBindingsImpl = value;
},
@@ -514,6 +534,7 @@ function createKeyboardHandlerHarness() {
let openControllerDebugCount = 0;
let playlistBrowserKeydownCount = 0;
let changelogKeydownCount = 0;
let jimakuKeydownCount = 0;
const createWordNode = (left: number) => ({
classList: createClassList(),
@@ -549,7 +570,10 @@ function createKeyboardHandlerHarness() {
handleCharacterDictionaryKeydown: () => false,
handleSubsyncKeydown: () => false,
handleKikuKeydown: () => false,
handleJimakuKeydown: () => false,
handleJimakuKeydown: () => {
jimakuKeydownCount += 1;
return false;
},
handleTsukihimeKeydown: () => false,
handleMediaTimingReviewKeydown: () => {
mediaTimingReviewKeydownCount += 1;
@@ -590,6 +614,7 @@ function createKeyboardHandlerHarness() {
openControllerDebugCount: () => openControllerDebugCount,
playlistBrowserKeydownCount: () => playlistBrowserKeydownCount,
changelogKeydownCount: () => changelogKeydownCount,
jimakuKeydownCount: () => jimakuKeydownCount,
setWordCount: (count: number) => {
wordNodes = Array.from({ length: count }, (_, index) => createWordNode(10 + index * 70));
},
@@ -1273,6 +1298,57 @@ test('Hachidori attention without an open popup pane still forwards imported mpv
}
});
test('keyboard mode: lookup toggle opens a lookup during Hachidori attention without a pane', async () => {
const { ctx, handlers, testGlobals } = createKeyboardHandlerHarness();
try {
await handlers.setupMpvInputForwarding();
handlers.handleKeyboardModeToggleRequested();
ctx.state.keyboardSelectedWordIndex = 1;
handlers.syncKeyboardTokenSelection();
ctx.state.hachidoriReaderSeen = true;
ctx.state.yomitanPopupVisible = true;
handlers.handleLookupWindowToggleRequested();
await wait(0);
assert.equal(ctx.dom.subtitleRoot.classList.contains('has-selection'), true);
assert.deepEqual(
testGlobals.commandEvents.filter((event) => event.type === 'setVisible'),
[],
);
} finally {
ctx.state.keyboardDrivenModeEnabled = false;
testGlobals.restore();
}
});
test('keyboard mode: lookup toggle closes an open Hachidori pane after attention is released', async () => {
const { ctx, handlers, testGlobals } = createKeyboardHandlerHarness();
try {
await handlers.setupMpvInputForwarding();
handlers.handleKeyboardModeToggleRequested();
// A selection drag leaves the pane open while the Yomitan-shaped visible marker clears.
ctx.state.hachidoriReaderSeen = true;
ctx.state.yomitanPopupVisible = false;
testGlobals.setHachidoriPaneOpen(true);
handlers.handleLookupWindowToggleRequested();
await wait(0);
assert.deepEqual(
testGlobals.commandEvents.filter(
(event) => event.type === 'setVisible' || event.type === 'clearActiveTextSource',
),
[{ type: 'setVisible', visible: false }, { type: 'clearActiveTextSource' }],
);
} finally {
ctx.state.keyboardDrivenModeEnabled = false;
testGlobals.restore();
}
});
test('configured subtitle-jump keybinding preserves pause when pause state is unknown', async () => {
const { handlers, testGlobals } = createKeyboardHandlerHarness();
@@ -1482,6 +1558,59 @@ test('keyboard mode: changelog modal handles h/l fold keys before lookup control
}
});
const KEYBOARD_MODE_LOOKUP_KEYS = [
{ key: 'h', code: 'KeyH' },
{ key: 'l', code: 'KeyL' },
{ key: 'ArrowLeft', code: 'ArrowLeft' },
{ key: 'ArrowRight', code: 'ArrowRight' },
];
test('keyboard mode: an open modal receives h/l/arrows instead of lookup controls', async () => {
const { ctx, testGlobals, handlers, jimakuKeydownCount } = createKeyboardHandlerHarness();
try {
await handlers.setupMpvInputForwarding();
handlers.handleKeyboardModeToggleRequested();
ctx.state.jimakuModalOpen = true;
ctx.state.keyboardSelectedWordIndex = 1;
const titleInput = testGlobals.createTextInputTarget();
for (const key of KEYBOARD_MODE_LOOKUP_KEYS) {
const prevented = testGlobals.dispatchKeydown({ ...key, target: titleInput });
assert.equal(prevented, false, `${key.code} must reach the focused modal input`);
}
await wait(0);
assert.equal(jimakuKeydownCount(), KEYBOARD_MODE_LOOKUP_KEYS.length);
assert.equal(ctx.state.keyboardSelectedWordIndex, 1);
assert.deepEqual(testGlobals.mpvCommands, []);
} finally {
testGlobals.restore();
}
});
test('keyboard mode: h/l/arrows typed into a text field are not consumed by lookup controls', async () => {
const { ctx, testGlobals, handlers, setWordCount } = createKeyboardHandlerHarness();
try {
await handlers.setupMpvInputForwarding();
handlers.handleKeyboardModeToggleRequested();
// No words means lookup controls would seek mpv to the adjacent subtitle.
setWordCount(0);
const input = testGlobals.createTextInputTarget();
for (const key of KEYBOARD_MODE_LOOKUP_KEYS) {
const prevented = testGlobals.dispatchKeydown({ ...key, target: input });
assert.equal(prevented, false, `${key.code} must reach the text field`);
}
await wait(0);
assert.deepEqual(testGlobals.mpvCommands, []);
} finally {
testGlobals.restore();
}
});
test('keyboard mode: configured stats toggle works even while popup is open', async () => {
const { handlers, testGlobals } = createKeyboardHandlerHarness();
@@ -1748,7 +1877,7 @@ test('keyboard mode: visible-layer Ctrl+Shift+Y should not be toggled by rendere
}
});
test('keyboard mode: modal-layer Ctrl+Shift+Y still toggles via renderer keydown', async () => {
test('keyboard mode: modal layer never enables keyboard mode', async () => {
const { ctx, handlers, testGlobals } = createKeyboardHandlerHarness();
try {
@@ -1756,9 +1885,10 @@ test('keyboard mode: modal-layer Ctrl+Shift+Y still toggles via renderer keydown
ctx.platform.isModalLayer = true;
testGlobals.dispatchKeydown({ key: 'Y', code: 'KeyY', ctrlKey: true, shiftKey: true });
assert.equal(ctx.state.keyboardDrivenModeEnabled, true);
assert.equal(ctx.state.keyboardDrivenModeEnabled, false);
testGlobals.dispatchKeydown({ key: 'Y', code: 'KeyY', ctrlKey: true, shiftKey: true });
// Controller toggle path.
handlers.handleKeyboardModeToggleRequested();
assert.equal(ctx.state.keyboardDrivenModeEnabled, false);
} finally {
ctx.state.keyboardDrivenModeEnabled = false;
+30 -32
View File
@@ -8,6 +8,7 @@ import {
HACHIDORI_HOST_SELECTOR,
YOMITAN_POPUP_COMMAND_EVENT,
YOMITAN_POPUP_HOST_SELECTOR,
isHachidoriPopupOpen,
isLookupPopupOpen,
isYomitanPopupVisible,
isYomitanPopupIframe,
@@ -408,11 +409,6 @@ export function createKeyboardHandlers(
return e.ctrlKey || e.metaKey;
}
function isKeyboardDrivenModeToggle(e: KeyboardEvent): boolean {
const isYKey = e.code === 'KeyY' || e.key.toLowerCase() === 'y';
return isPrimaryModifierPressed(e) && !e.altKey && e.shiftKey && isYKey && !e.repeat;
}
function isLookupWindowToggle(e: KeyboardEvent): boolean {
const isYKey = e.code === 'KeyY' || e.key.toLowerCase() === 'y';
return isPrimaryModifierPressed(e) && !e.altKey && !e.shiftKey && isYKey && !e.repeat;
@@ -525,8 +521,7 @@ export function createKeyboardHandlers(
pendingSelectionAnchorAfterSubtitleSeek = null;
resetSelectionToStartOnNextSubtitleSync = false;
const shouldRefreshLookup =
pendingLookupRefreshAfterSubtitleSeek &&
(ctx.state.yomitanPopupVisible || isYomitanPopupVisible(document));
pendingLookupRefreshAfterSubtitleSeek && isLookupPopupOpen(ctx.state, document);
pendingLookupRefreshAfterSubtitleSeek = false;
if (shouldRefreshLookup) {
queueMicrotask(() => {
@@ -718,7 +713,7 @@ export function createKeyboardHandlers(
if (lookupScanFallbackTimer !== null) clearTimeout(lookupScanFallbackTimer);
lookupScanFallbackTimer = setTimeout(() => {
lookupScanFallbackTimer = null;
if (ctx.state.yomitanPopupVisible || isYomitanPopupVisible(document)) {
if (isLookupPopupOpen(ctx.state, document)) {
return;
}
// Dispatch directly on the selected token span; when overlay pointer-events are disabled,
@@ -728,7 +723,10 @@ export function createKeyboardHandlers(
return true;
}
// Keyboard mode drives the visible overlay's subtitle words. The modal layer hides
// subtitles, so enabling it there only lets arrows/H/L seek mpv from behind a modal.
function handleKeyboardModeToggleRequested(): void {
if (ctx.platform.isModalLayer) return;
toggleKeyboardDrivenMode();
}
@@ -745,15 +743,22 @@ export function createKeyboardHandlers(
}
function handleLookupWindowToggleRequested(): void {
if (ctx.state.yomitanPopupVisible || isYomitanPopupVisible(document)) {
if (isLookupPopupOpen(ctx.state, document)) {
closeLookupWindow();
return;
}
triggerLookupForSelectedWord();
}
// Closes any popup state: an open Hachidori pane (its attention may already be
// released) and attention without a pane, so callers like the Kiku modal
// opener leave nothing behind.
function closeLookupWindow(): boolean {
if (!ctx.state.yomitanPopupVisible && !isYomitanPopupVisible(document)) {
if (
!ctx.state.yomitanPopupVisible &&
!isYomitanPopupVisible(document) &&
!isHachidoriPopupOpen(document)
) {
return false;
}
@@ -777,7 +782,7 @@ export function createKeyboardHandlers(
return false;
}
const popupVisible = ctx.state.yomitanPopupVisible || isYomitanPopupVisible(document);
const popupVisible = isLookupPopupOpen(ctx.state, document);
const result = moveKeyboardSelection(delta);
if (result === 'no-words') {
seekAdjacentSubtitleAndQueueSelection(delta, popupVisible);
@@ -798,7 +803,7 @@ export function createKeyboardHandlers(
code: string,
repeat: boolean = true,
): boolean {
if (!ctx.state.yomitanPopupVisible && !isYomitanPopupVisible(document)) {
if (!isLookupPopupOpen(ctx.state, document)) {
return false;
}
dispatchYomitanPopupKeydown(key, code, [], repeat);
@@ -806,7 +811,7 @@ export function createKeyboardHandlers(
}
function mineSelectedFromController(): boolean {
if (!ctx.state.yomitanPopupVisible && !isYomitanPopupVisible(document)) {
if (!isLookupPopupOpen(ctx.state, document)) {
return false;
}
dispatchYomitanPopupMineSelected();
@@ -814,7 +819,7 @@ export function createKeyboardHandlers(
}
function cyclePopupAudioSourceForController(direction: -1 | 1): boolean {
if (!ctx.state.yomitanPopupVisible && !isYomitanPopupVisible(document)) {
if (!isLookupPopupOpen(ctx.state, document)) {
return false;
}
dispatchYomitanPopupCycleAudioSource(direction);
@@ -822,7 +827,7 @@ export function createKeyboardHandlers(
}
function playCurrentAudioForController(): boolean {
if (!ctx.state.yomitanPopupVisible && !isYomitanPopupVisible(document)) {
if (!isLookupPopupOpen(ctx.state, document)) {
return false;
}
dispatchYomitanPopupPlayCurrentAudio();
@@ -830,7 +835,7 @@ export function createKeyboardHandlers(
}
function scrollPopupByController(deltaX: number, deltaY: number): boolean {
if (!ctx.state.yomitanPopupVisible && !isYomitanPopupVisible(document)) {
if (!isLookupPopupOpen(ctx.state, document)) {
return false;
}
dispatchYomitanPopupScrollBy(deltaX, deltaY);
@@ -857,7 +862,7 @@ export function createKeyboardHandlers(
if (!ctx.state.keyboardDrivenModeEnabled) {
return;
}
if (!ctx.state.yomitanPopupVisible && !isYomitanPopupVisible(document)) {
if (!isLookupPopupOpen(ctx.state, document)) {
return;
}
restoreOverlayKeyboardFocus();
@@ -902,7 +907,7 @@ export function createKeyboardHandlers(
}
const key = e.code;
const popupVisible = ctx.state.yomitanPopupVisible || isYomitanPopupVisible(document);
const popupVisible = isLookupPopupOpen(ctx.state, document);
if (key === 'ArrowLeft' || key === 'KeyH') {
const result = moveKeyboardSelection(-1);
if (result === 'start-boundary' || result === 'no-words') {
@@ -1149,12 +1154,6 @@ export function createKeyboardHandlers(
return;
}
if (isKeyboardDrivenModeToggle(e) && ctx.platform.isModalLayer) {
e.preventDefault();
handleKeyboardModeToggleRequested();
return;
}
if (isLookupWindowToggle(e)) {
e.preventDefault();
handleLookupWindowToggleRequested();
@@ -1167,19 +1166,11 @@ export function createKeyboardHandlers(
}
}
// Ahead of the keyboard-driven lookup controls: the changelog modal binds
// arrows/H/L for folding, and those would otherwise move the subtitle word
// selection (and seek mpv) behind the open modal.
if (ctx.state.changelogModalOpen) {
options.handleChangelogKeydown(e);
return;
}
if (handleKeyboardDrivenModeLookupControls(e)) {
e.preventDefault();
return;
}
if (ctx.state.runtimeOptionsModalOpen) {
options.handleRuntimeOptionsKeydown(e);
return;
@@ -1227,6 +1218,13 @@ export function createKeyboardHandlers(
return;
}
// After open modals and text fields, which own arrows/H/L; before Yomitan popup
// keybinds so word navigation keeps working while the popup is open.
if (handleKeyboardDrivenModeLookupControls(e)) {
e.preventDefault();
return;
}
if (handlePendingNumericSelection(e)) {
return;
}
@@ -15,6 +15,7 @@ export async function setup() {
const commands: unknown[] = [];
const snapshot: SubtitleSidebarSnapshot = {
sourceKey: 'episode-1:track-1',
cuesRevision: 1,
cues: [
{ text: '最初の台詞', startTime: 0, endTime: 1 },
{ text: '同じ台詞\n二行目', startTime: 1, endTime: 2 },
@@ -39,6 +40,7 @@ export async function setup() {
Object.defineProperty(window, 'electronAPI', {
value: {
getSubtitleSidebarSnapshot: async () => snapshot,
getSubtitleSidebarPlaybackState: async () => snapshot,
getSessionBindings: async () => [
{
sourcePath: 'keybindings[0].key',
@@ -64,6 +66,7 @@ export async function setup() {
} satisfies Pick<
ElectronAPI,
| 'getSubtitleSidebarSnapshot'
| 'getSubtitleSidebarPlaybackState'
| 'getSessionBindings'
| 'getConfiguredShortcuts'
| 'getStatsToggleKey'
+123 -9
View File
@@ -1,7 +1,11 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import type { ElectronAPI, SubtitleSidebarSnapshot } from '../../types';
import type {
ElectronAPI,
SubtitleSidebarPlaybackState,
SubtitleSidebarSnapshot,
} from '../../types';
import { createRendererState } from '../state.js';
import {
applySidebarCssDeclarations,
@@ -68,7 +72,12 @@ function createCueRow() {
function createListStub() {
return {
innerHTML: '',
get innerHTML() {
return '';
},
set innerHTML(_html: string) {
this.children.length = 0;
},
children: [] as ReturnType<typeof createCueRow>[],
appendChild(child: ReturnType<typeof createCueRow>) {
child.offsetTop = this.children.length * child.clientHeight;
@@ -180,17 +189,20 @@ test.afterEach(() => {
/**
* Builds a sidebar modal over fake window/document/dom globals. The snapshot served to the
* modal can be changed with `setSnapshot`. Globals are restored (and polling stopped) after
* each test.
* modal (and the playback state derived from it) can be changed with `setSnapshot`; `poll`
* runs the next scheduled playback tick. Globals are restored after each test.
*/
function createSidebarHarness(options: SidebarHarnessOptions = {}) {
let snapshot: SubtitleSidebarSnapshot = {
sourceKey: 'test-subtitles',
cuesRevision: 1,
cues: options.cues ?? [{ startTime: 1, endTime: 2, text: 'first' }],
currentSubtitle: options.currentSubtitle ?? { text: 'first', startTime: 1, endTime: 2 },
currentTimeSec: 'currentTimeSec' in options ? options.currentTimeSec : 1.1,
currentTimeSec: 'currentTimeSec' in options ? (options.currentTimeSec ?? null) : 1.1,
config: { ...DEFAULT_SIDEBAR_CONFIG, ...options.config },
};
const requests = { snapshot: 0, playback: 0 };
let nextPoll: (() => Promise<void>) | null = null;
const mpvCommands: MpvCommand[] = [];
const ignoreMouseCalls: Array<[boolean, { forward?: boolean } | undefined]> = [];
@@ -208,7 +220,15 @@ function createSidebarHarness(options: SidebarHarnessOptions = {}) {
addEventListener: windowEvents.addEventListener,
removeEventListener: windowEvents.removeEventListener,
electronAPI: {
getSubtitleSidebarSnapshot: async () => snapshot,
getSubtitleSidebarSnapshot: async () => {
requests.snapshot += 1;
return snapshot;
},
getSubtitleSidebarPlaybackState: async (): Promise<SubtitleSidebarPlaybackState> => {
requests.playback += 1;
const { sourceKey, cuesRevision, currentTimeSec, currentSubtitle } = snapshot;
return { sourceKey, cuesRevision, currentTimeSec, currentSubtitle };
},
getPlaybackPaused: async () => false,
sendMpvCommand: (command: MpvCommand) => {
mpvCommands.push(command);
@@ -281,11 +301,16 @@ function createSidebarHarness(options: SidebarHarnessOptions = {}) {
onVisibilityChanged: (visible) => {
visibilityChanges.push(visible);
},
schedule: (callback) => {
nextPoll = callback;
return () => {
if (nextPoll === callback) nextPoll = null;
};
},
...options.modalOptions,
});
harnessCleanups.push(() => {
// Closing stops the snapshot poll so it cannot fire against the next test's globals.
modal.closeSubtitleSidebarModal();
modal.disposeDomEvents();
restoreGlobals();
@@ -307,6 +332,13 @@ function createSidebarHarness(options: SidebarHarnessOptions = {}) {
windowEvents,
contentEvents,
modalEvents,
requests,
/** Runs the pending playback poll tick, if one is scheduled. */
async poll() {
const tick = nextPoll;
nextPoll = null;
await tick?.();
},
setSnapshot(patch: SnapshotPatch) {
snapshot = { ...snapshot, ...patch, config: { ...snapshot.config, ...patch.config } };
},
@@ -675,7 +707,7 @@ test('subtitle sidebar jumps to first resolved active cue, then resumes smooth a
currentSubtitle: { text: 'line-9', startTime: 18, endTime: 19.5 },
currentTimeSec: 18.1,
});
await h.modal.refreshSubtitleSidebarSnapshot();
await h.poll();
assert.equal(h.state.subtitleSidebarActiveCueIndex, 9);
assert.equal(h.cueList.scrollTop, 260);
@@ -685,12 +717,94 @@ test('subtitle sidebar jumps to first resolved active cue, then resumes smooth a
currentSubtitle: { text: 'line-10', startTime: 20, endTime: 21.5 },
currentTimeSec: 20.1,
});
await h.modal.refreshSubtitleSidebarSnapshot();
await h.poll();
assert.equal(h.state.subtitleSidebarActiveCueIndex, 10);
assert.deepEqual(h.cueList.scrollToCalls.at(-1), { top: 300, behavior: 'smooth' });
});
test('open subtitle sidebar follows playback from cheap polls without refetching cues or re-measuring', async () => {
let measurements = 0;
const h = createSidebarHarness({
cues: Array.from({ length: 4 }, (_, index) => ({
startTime: index * 2,
endTime: index * 2 + 1.5,
text: `line-${index}`,
})),
currentSubtitle: { text: 'line-0', startTime: 0, endTime: 1.5 },
currentTimeSec: 0.1,
config: { layout: 'embedded', maxWidth: 360 },
contentWidth: () => {
measurements += 1;
return 360;
},
});
await h.modal.openSubtitleSidebarModal();
const snapshotsAfterOpen = h.requests.snapshot;
const measurementsAfterOpen = measurements;
// Inside the lookahead window the upcoming cue activates before mpv reports its text.
h.setSnapshot({
currentSubtitle: { text: '', startTime: null, endTime: null },
currentTimeSec: 1.9,
});
await h.poll();
assert.equal(h.state.subtitleSidebarActiveCueIndex, 1);
h.setSnapshot({
currentSubtitle: { text: 'line-2', startTime: 4, endTime: 5.5 },
currentTimeSec: 4.1,
});
await h.poll();
assert.equal(h.state.subtitleSidebarActiveCueIndex, 2);
assert.equal(h.cueList.children[2]!.classList.contains('active'), true);
assert.equal(h.cueList.children[1]!.classList.contains('active'), false);
assert.equal(h.requests.playback, 2);
assert.equal(h.requests.snapshot, snapshotsAfterOpen);
assert.equal(measurements, measurementsAfterOpen);
});
for (const change of [
{ name: 'subtitle source', patch: { sourceKey: 'other-subtitles' } },
{ name: 'parsed cue revision', patch: { cuesRevision: 2 } },
]) {
test(`open subtitle sidebar refetches and re-renders cues when the ${change.name} changes`, async () => {
const h = createSidebarHarness();
await h.modal.openSubtitleSidebarModal();
const snapshotsAfterOpen = h.requests.snapshot;
h.setSnapshot({
...change.patch,
cues: [
{ startTime: 1, endTime: 2, text: 'replacement' },
{ startTime: 3, endTime: 4, text: 'second' },
],
});
await h.poll();
assert.equal(h.requests.snapshot, snapshotsAfterOpen + 1);
assert.deepEqual(
h.cueList.children.map((row) => (row.children[1] as { textContent: string }).textContent),
['replacement', 'second'],
);
});
}
test('open subtitle sidebar picks up changes main did not flag on a slow full-snapshot fallback', async () => {
const h = createSidebarHarness();
await h.modal.openSubtitleSidebarModal();
h.setSnapshot({ config: { enabled: false } });
for (let polls = 0; h.state.subtitleSidebarModalOpen && polls < 100; polls += 1) {
await h.poll();
}
assert.equal(h.state.subtitleSidebarModalOpen, false);
assert.equal(h.requests.snapshot, 2);
assert.ok(h.requests.playback > 1);
});
test('subtitle sidebar closes and resumes a hover pause', async () => {
const h = createSidebarHarness({ config: { pauseVideoOnHover: true } });
h.modal.wireDomEvents();
+46 -19
View File
@@ -18,7 +18,11 @@ import {
const MANUAL_SCROLL_HOLD_MS = 1500;
const ACTIVE_CUE_LOOKAHEAD_SEC = 0.18;
const SNAPSHOT_POLL_INTERVAL_MS = 80;
// While open, the sidebar polls cheap cached playback state for active-cue tracking and only
// fetches the full snapshot (mpv source resolution + every cue + config) when the source or
// parsed cues changed, plus this fallback in case main missed a change.
const PLAYBACK_POLL_INTERVAL_MS = 80;
const FULL_SNAPSHOT_FALLBACK_POLLS = 25;
const EMBEDDED_SIDEBAR_MIN_WIDTH_PX = 240;
const EMBEDDED_SIDEBAR_MAX_RATIO = 0.45;
const appliedSidebarCssKeys = new WeakMap<HTMLElement, Set<string>>();
@@ -205,9 +209,18 @@ export function createSubtitleSidebarModal(
modalStateReader: Pick<ModalStateReader, 'isAnyModalOpen'>;
onVisibilityChanged?: (visible: boolean) => void;
shouldRestoreOpenOnStartup?: () => Promise<boolean>;
/** Schedules a poll tick and returns its cancel function. Defaults to `setTimeout`. */
schedule?: (callback: () => Promise<void>, delayMs: number) => () => void;
},
) {
let snapshotPollInterval: ReturnType<typeof setTimeout> | null = null;
const schedule =
options.schedule ??
((callback: () => Promise<void>, delayMs: number) => {
const timer = setTimeout(callback, delayMs);
return () => clearTimeout(timer);
});
let cancelPlaybackPoll: (() => void) | null = null;
let pollsSinceFullSnapshot = 0;
let lastAppliedVideoMarginRatio: number | null = null;
let subtitleSidebarHoverRequestId = 0;
let disposeDomEvents: (() => void) | null = null;
@@ -217,6 +230,7 @@ export function createSubtitleSidebarModal(
let subtitleSidebarPauseHeldByYomitanPopup = false;
let lastSubtitleSidebarLookupCueIndex = -1;
let subtitleSourceKey: string | null = null;
let subtitleCuesRevision: number | null = null;
function restoreEmbeddedSidebarPassthrough(): void {
syncOverlayMouseIgnoreState(ctx);
@@ -575,6 +589,8 @@ export function createSubtitleSidebarModal(
async function refreshSnapshot(): Promise<SubtitleSidebarSnapshot> {
const snapshot = await window.electronAPI.getSubtitleSidebarSnapshot();
pollsSinceFullSnapshot = 0;
subtitleCuesRevision = snapshot.cuesRevision;
if (snapshot.sourceKey !== subtitleSourceKey) {
clearSubtitleSidebarSelection(ctx.dom.subtitleSidebarList);
lastSubtitleSidebarLookupCueIndex = -1;
@@ -589,8 +605,8 @@ export function createSubtitleSidebarModal(
ctx.state.subtitleSidebarModalOpen = false;
ctx.dom.subtitleSidebarModal.classList.add('hidden');
ctx.dom.subtitleSidebarModal.setAttribute('aria-hidden', 'true');
stopSnapshotPolling();
updateActiveCue(null, snapshot.currentTimeSec ?? null);
stopPlaybackPolling();
updateActiveCue(null, snapshot.currentTimeSec);
setStatus('Subtitle sidebar disabled in config.');
syncEmbeddedSidebarLayout();
if (!ctx.state.isOverSubtitle && !options.modalStateReader.isAnyModalOpen()) {
@@ -608,38 +624,49 @@ export function createSubtitleSidebarModal(
renderCueList();
}
}
updateActiveCue(snapshot.currentSubtitle, snapshot.currentTimeSec ?? null);
updateActiveCue(snapshot.currentSubtitle, snapshot.currentTimeSec);
syncEmbeddedSidebarLayout();
return snapshot;
}
function startSnapshotPolling(): void {
stopSnapshotPolling();
async function pollPlaybackOnce(): Promise<void> {
pollsSinceFullSnapshot += 1;
const playback = await window.electronAPI.getSubtitleSidebarPlaybackState();
if (
playback.sourceKey !== subtitleSourceKey ||
playback.cuesRevision !== subtitleCuesRevision ||
pollsSinceFullSnapshot >= FULL_SNAPSHOT_FALLBACK_POLLS
) {
await refreshSnapshot();
return;
}
updateActiveCue(playback.currentSubtitle, playback.currentTimeSec);
}
function startPlaybackPolling(): void {
stopPlaybackPolling();
const pollOnce = async (): Promise<void> => {
try {
await refreshSnapshot();
await pollPlaybackOnce();
} catch {
// Keep polling; a transient IPC failure should not stop updates.
}
if (!ctx.state.subtitleSidebarModalOpen) {
snapshotPollInterval = null;
cancelPlaybackPoll = null;
return;
}
snapshotPollInterval = setTimeout(pollOnce, SNAPSHOT_POLL_INTERVAL_MS);
cancelPlaybackPoll = schedule(pollOnce, PLAYBACK_POLL_INTERVAL_MS);
};
snapshotPollInterval = setTimeout(pollOnce, SNAPSHOT_POLL_INTERVAL_MS);
cancelPlaybackPoll = schedule(pollOnce, PLAYBACK_POLL_INTERVAL_MS);
}
function stopSnapshotPolling(): void {
if (!snapshotPollInterval) {
return;
}
clearTimeout(snapshotPollInterval);
snapshotPollInterval = null;
function stopPlaybackPolling(): void {
cancelPlaybackPoll?.();
cancelPlaybackPoll = null;
}
async function openSubtitleSidebarModal(): Promise<void> {
@@ -663,7 +690,7 @@ export function createSubtitleSidebarModal(
renderCueList();
syncActiveCueClasses(-1);
maybeAutoScrollActiveCue(-1, 'auto', true);
startSnapshotPolling();
startPlaybackPolling();
syncEmbeddedSidebarLayout();
restoreEmbeddedSidebarPassthrough();
window.electronAPI.notifyOverlayModalOpened?.('subtitle-sidebar');
@@ -693,7 +720,7 @@ export function createSubtitleSidebarModal(
ctx.state.subtitleSidebarModalOpen = false;
ctx.dom.subtitleSidebarModal.classList.add('hidden');
ctx.dom.subtitleSidebarModal.setAttribute('aria-hidden', 'true');
stopSnapshotPolling();
stopPlaybackPolling();
syncEmbeddedSidebarLayout();
if (!ctx.state.isOverSubtitle && !options.modalStateReader.isAnyModalOpen()) {
ctx.dom.overlay.classList.remove('interactive');
+1
View File
@@ -68,6 +68,7 @@ export const IPC_CHANNELS = {
getCurrentSubtitleRaw: 'get-current-subtitle-raw',
getCurrentSubtitleAss: 'get-current-subtitle-ass',
getSubtitleSidebarSnapshot: 'get-subtitle-sidebar-snapshot',
getSubtitleSidebarPlaybackState: 'get-subtitle-sidebar-playback-state',
getSubtitleSidebarOpen: 'get-subtitle-sidebar-open',
getPlaybackPaused: 'get-playback-paused',
getSubtitlePosition: 'get-subtitle-position',
+41
View File
@@ -0,0 +1,41 @@
import fs from 'node:fs';
import path from 'node:path';
// Structured replies the app writes to `--jellyfin-response-path`. The launcher passes a temp
// path and waits for the file instead of scraping logs, because the command usually runs in an
// already-running app instance whose output never reaches the launcher's helper process.
export type JellyfinCliLibrary = {
id: string;
name: string;
collectionType: string;
};
export type JellyfinCliItem = {
id: string;
title: string;
type: string;
};
export type JellyfinPreviewAuthPayload = {
serverUrl: string;
accessToken: string;
userId: string;
};
export type JellyfinCliResponse =
| { libraries: JellyfinCliLibrary[] }
| { items: JellyfinCliItem[] }
| { error: string }
| JellyfinPreviewAuthPayload;
// Writes via rename so a polling reader never parses a half-written file.
export function writeJellyfinCliResponse(
responsePath: string,
response: JellyfinCliResponse,
): void {
fs.mkdirSync(path.dirname(responsePath), { recursive: true });
const tempPath = `${responsePath}.${process.pid}.tmp`;
fs.writeFileSync(tempPath, JSON.stringify(response), 'utf-8');
fs.renameSync(tempPath, responsePath);
}
+2
View File
@@ -55,6 +55,7 @@ import type {
SubtitleData,
SubtitleMiningContext,
SubtitlePosition,
SubtitleSidebarPlaybackState,
SubtitleSidebarSnapshot,
SubtitleRendererStyleConfig,
SubtitleStyleConfig,
@@ -477,6 +478,7 @@ export interface ElectronAPI {
getCurrentSubtitleRaw: () => Promise<string>;
getCurrentSubtitleAss: () => Promise<string>;
getSubtitleSidebarSnapshot: () => Promise<SubtitleSidebarSnapshot>;
getSubtitleSidebarPlaybackState: () => Promise<SubtitleSidebarPlaybackState>;
copySubtitleSidebarSelection: (text: string) => Promise<void>;
getSubtitleSidebarOpen: () => Promise<boolean>;
getPlaybackPaused: () => Promise<boolean | null>;
+13 -3
View File
@@ -226,15 +226,25 @@ export interface SubtitleData {
endTime?: number | null;
}
export interface SubtitleSidebarSnapshot {
/**
* Per-tick sidebar state, served from main's cached state without mpv round-trips. When
* `sourceKey` or `cuesRevision` differs from the last full snapshot's, the renderer fetches a
* new `SubtitleSidebarSnapshot`.
*/
export interface SubtitleSidebarPlaybackState {
sourceKey: string | null;
cues: SubtitleCue[];
currentTimeSec?: number | null;
/** Bumps whenever main swaps in a different parsed cue array. */
cuesRevision: number;
currentTimeSec: number | null;
currentSubtitle: {
text: string;
startTime: number | null;
endTime: number | null;
};
}
export interface SubtitleSidebarSnapshot extends SubtitleSidebarPlaybackState {
cues: SubtitleCue[];
config: SubtitleSidebarSnapshotConfig;
}
+109 -11
View File
@@ -179,21 +179,119 @@ test('resolveHyprlandWindowGeometry uses monitor bounds for client-requested ful
});
});
test('HyprlandWindowTracker re-emits focus callback on active window events for z-order refresh', () => {
type PendingCommand = {
command: string;
args: string[];
resolve: (output: string) => void;
};
// Runner whose commands stay pending until the test resolves them.
function makeDeferredRunner() {
const pending: PendingCommand[] = [];
const runCommand = (command: string, args: string[]) =>
new Promise<string>((resolve) => {
pending.push({ command, args, resolve });
});
return { pending, runCommand };
}
function flush(): Promise<void> {
return new Promise((resolve) => setImmediate(resolve));
}
test('HyprlandWindowTracker re-emits focus callback on active window events for z-order refresh', async () => {
const calls: string[] = [];
const tracker = new HyprlandWindowTracker();
const privateTracker = tracker as unknown as {
handleSocketEvent: (event: string) => void;
pollGeometry: () => void;
};
privateTracker.pollGeometry = () => {
calls.push('poll');
};
const tracker = new HyprlandWindowTracker(undefined, async (command, args) => {
calls.push(`${command} ${args.join(' ')}`);
return '[]';
});
tracker.onWindowFocusChange = (focused) => {
calls.push(`focus:${focused}`);
};
privateTracker.handleSocketEvent('activewindowv2>>0xmpv');
(tracker as unknown as { handleSocketEvent: (event: string) => void }).handleSocketEvent(
'activewindowv2>>0xmpv',
);
await flush();
assert.deepEqual(calls, ['poll', 'focus:false']);
assert.deepEqual(calls, ['hyprctl -j clients', 'focus:false']);
});
test('HyprlandWindowTracker coalesces polls requested while hyprctl is still running', async () => {
const { pending, runCommand } = makeDeferredRunner();
const tracker = new HyprlandWindowTracker(undefined, runCommand);
const polls = [tracker.refreshNow(), tracker.refreshNow(), tracker.refreshNow()];
await flush();
assert.equal(pending.length, 1);
pending[0]!.resolve(JSON.stringify([makeClient({ at: [10, 20] })]));
await flush();
assert.equal(pending.length, 2);
pending[1]!.resolve(JSON.stringify([makeClient({ at: [30, 40] })]));
await Promise.all(polls);
assert.equal(pending.length, 2);
assert.deepEqual(tracker.getGeometry(), { x: 30, y: 40, width: 1280, height: 720 });
});
test('HyprlandWindowTracker ignores a poll that resolves after stop', async () => {
const { pending, runCommand } = makeDeferredRunner();
const tracker = new HyprlandWindowTracker(undefined, runCommand);
let found = 0;
tracker.onWindowFound = () => {
found += 1;
};
const poll = tracker.refreshNow();
tracker.stop();
await flush();
pending[0]!.resolve(JSON.stringify([makeClient()]));
await poll;
assert.equal(found, 0);
assert.equal(tracker.getGeometry(), null);
});
test('HyprlandWindowTracker reads each mpv command line once and re-reads after the pid leaves', async () => {
let clients = [makeClient({ pid: 10 })];
const psPids: string[] = [];
const tracker = new HyprlandWindowTracker('/tmp/subminer.sock', async (command, args) => {
if (command === 'hyprctl') {
return JSON.stringify(clients);
}
psPids.push(args[1]!);
return 'mpv --input-ipc-server=/tmp/subminer.sock video.mkv\n';
});
await tracker.refreshNow();
await tracker.refreshNow();
assert.equal(tracker.isTracking(), true);
assert.deepEqual(psPids, ['10']);
clients = [];
await tracker.refreshNow();
clients = [makeClient({ pid: 10 })];
await tracker.refreshNow();
assert.deepEqual(psPids, ['10', '10']);
});
test('HyprlandWindowTracker still tracks mpv when ps fails for another pid, and retries that pid', async () => {
const psPids: string[] = [];
const tracker = new HyprlandWindowTracker('/tmp/subminer.sock', async (command, args) => {
if (command === 'hyprctl') {
return JSON.stringify([makeClient({ pid: 10 }), makeClient({ pid: 20 })]);
}
psPids.push(args[1]!);
if (args[1] === '20') {
throw new Error('ps exited 1');
}
return 'mpv --input-ipc-server=/tmp/subminer.sock video.mkv\n';
});
await tracker.refreshNow();
assert.equal(tracker.isTracking(), true);
await tracker.refreshNow();
assert.deepEqual(psPids.sort(), ['10', '20', '20']);
});
+73 -45
View File
@@ -17,8 +17,13 @@
*/
import * as net from 'net';
import { execSync } from 'child_process';
import { BaseWindowTracker } from './base-tracker';
import {
createCoalescedTask,
execFileWithTimeout,
ProcessCommandLineCache,
type CommandRunner,
} from './linux-poll';
import { createLogger } from '../logger';
import type { WindowGeometry } from '../types';
@@ -98,16 +103,20 @@ function isMpvClassName(value: string | undefined): boolean {
return value.trim().toLowerCase().includes('mpv');
}
export function selectHyprlandMpvWindow(
clients: HyprlandClient[],
options: SelectHyprlandMpvWindowOptions,
): HyprlandClient | null {
const visibleMpvWindows = clients.filter(
function filterVisibleMpvWindows(clients: HyprlandClient[]): HyprlandClient[] {
return clients.filter(
(client) =>
(isMpvClassName(client.class) || isMpvClassName(client.initialClass)) &&
client.mapped !== false &&
client.hidden !== true,
);
}
export function selectHyprlandMpvWindow(
clients: HyprlandClient[],
options: SelectHyprlandMpvWindowOptions,
): HyprlandClient | null {
const visibleMpvWindows = filterVisibleMpvWindows(clients);
if (!options.targetMpvSocketPath) {
return preferActiveHyprlandWindow(visibleMpvWindows, options.activeWindowAddress);
@@ -221,20 +230,30 @@ export class HyprlandWindowTracker extends BaseWindowTracker {
private pollTimeouts: Array<ReturnType<typeof setTimeout>> = [];
private eventSocket: net.Socket | null = null;
private readonly targetMpvSocketPath: string | null;
private readonly runCommand: CommandRunner;
private readonly commandLines: ProcessCommandLineCache;
private activeWindowAddress: string | null = null;
private stopped = false;
// Interval ticks, focus events, and geometry bursts all funnel through here,
// so a slow hyprctl never has more than one poll in flight.
private readonly pollGeometry = createCoalescedTask(() => this.pollGeometryOnce());
constructor(targetMpvSocketPath?: string) {
constructor(targetMpvSocketPath?: string, runCommand: CommandRunner = execFileWithTimeout) {
super();
this.targetMpvSocketPath = targetMpvSocketPath?.trim() || null;
this.runCommand = runCommand;
this.commandLines = new ProcessCommandLineCache(runCommand);
}
start(): void {
this.pollInterval = setInterval(() => this.pollGeometry(), 250);
this.pollGeometry();
this.stopped = false;
this.pollInterval = setInterval(() => void this.pollGeometry(), 250);
void this.pollGeometry();
this.connectEventSocket();
}
stop(): void {
this.stopped = true;
if (this.pollInterval) {
clearInterval(this.pollInterval);
this.pollInterval = null;
@@ -249,6 +268,10 @@ export class HyprlandWindowTracker extends BaseWindowTracker {
}
}
override refreshNow(): Promise<void> {
return this.pollGeometry();
}
private connectEventSocket(): void {
const hyprlandSig = process.env.HYPRLAND_INSTANCE_SIGNATURE;
if (!hyprlandSig) {
@@ -297,10 +320,12 @@ export class HyprlandWindowTracker extends BaseWindowTracker {
if (name === 'activewindowv2') {
const wasFocused = this.isTargetWindowFocused();
this.activeWindowAddress = data || null;
this.pollGeometry();
if (this.isTargetWindowFocused() === wasFocused) {
this.onWindowFocusChange?.(this.isTargetWindowFocused());
}
void this.pollGeometry().then(() => {
// Re-emit unchanged focus so the overlay still refreshes its z-order.
if (!this.stopped && this.isTargetWindowFocused() === wasFocused) {
this.onWindowFocusChange?.(this.isTargetWindowFocused());
}
});
return;
}
@@ -320,7 +345,7 @@ export class HyprlandWindowTracker extends BaseWindowTracker {
this.pollTimeouts = [0, 50, 150, 300].map((delayMs) => {
const pollTimeout = setTimeout(() => {
this.pollTimeouts = this.pollTimeouts.filter((timeout) => timeout !== pollTimeout);
this.pollGeometry();
void this.pollGeometry();
}, delayMs);
return pollTimeout;
});
@@ -329,56 +354,59 @@ export class HyprlandWindowTracker extends BaseWindowTracker {
}
}
private pollGeometry(): void {
try {
const output = execSync('hyprctl -j clients', { encoding: 'utf-8' });
const clients = parseHyprctlClients(output);
if (!clients) {
this.updateGeometry(null);
return;
}
const mpvWindow = this.findTargetWindow(clients);
private async pollGeometryOnce(): Promise<void> {
if (this.stopped) {
return;
}
let mpvWindow: HyprlandClient | null;
let monitors: HyprlandMonitor[] | null = null;
try {
const clients = parseHyprctlClients(await this.runCommand('hyprctl', ['-j', 'clients']));
mpvWindow = clients ? await this.findTargetWindow(clients) : null;
if (mpvWindow) {
const focused = !this.activeWindowAddress || mpvWindow.address === this.activeWindowAddress;
this.updateGeometry(
resolveHyprlandWindowGeometry(mpvWindow, this.getHyprlandMonitors(mpvWindow)),
focused,
);
this.updateTargetWindowFocused(focused);
} else {
this.updateGeometry(null);
monitors = await this.getHyprlandMonitors(mpvWindow);
}
} catch (err) {
// hyprctl not available or failed - silent fail
} catch {
// hyprctl/ps not available, timed out, or failed - keep the last geometry
return;
}
if (this.stopped) {
return;
}
if (mpvWindow) {
const focused = !this.activeWindowAddress || mpvWindow.address === this.activeWindowAddress;
this.updateGeometry(resolveHyprlandWindowGeometry(mpvWindow, monitors), focused);
this.updateTargetWindowFocused(focused);
} else {
this.updateGeometry(null);
}
}
private findTargetWindow(clients: HyprlandClient[]): HyprlandClient | null {
private async findTargetWindow(clients: HyprlandClient[]): Promise<HyprlandClient | null> {
// Resolve command lines up front so window selection itself stays synchronous.
const commandLines = this.targetMpvSocketPath
? await this.commandLines.lookup(
filterVisibleMpvWindows(clients).flatMap((client) => (client.pid ? [client.pid] : [])),
)
: null;
return selectHyprlandMpvWindow(clients, {
targetMpvSocketPath: this.targetMpvSocketPath,
activeWindowAddress: this.activeWindowAddress,
getWindowCommandLine: (pid) => this.getWindowCommandLine(pid),
getWindowCommandLine: (pid) => commandLines?.get(pid) ?? null,
});
}
private getHyprlandMonitors(client: HyprlandClient): HyprlandMonitor[] | null {
private async getHyprlandMonitors(client: HyprlandClient): Promise<HyprlandMonitor[] | null> {
if (!isHyprlandFullscreenClient(client)) {
return null;
}
try {
const output = execSync('hyprctl -j monitors', { encoding: 'utf-8' });
return parseHyprctlMonitors(output);
return parseHyprctlMonitors(await this.runCommand('hyprctl', ['-j', 'monitors']));
} catch {
return null;
}
}
private getWindowCommandLine(pid: number): string | null {
const commandLine = execSync(`ps -p ${pid} -o args=`, {
encoding: 'utf-8',
}).trim();
return commandLine || null;
}
}
+113
View File
@@ -0,0 +1,113 @@
/*
SubMiner - All-in-one sentence mining overlay
Copyright (C) 2024 sudacode
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { execFile } from 'child_process';
// Shared polling helpers for the Hyprland and Sway trackers, which poll a
// compositor CLI on a short interval from the Electron main thread.
export type CommandRunner = (command: string, args: string[]) => Promise<string>;
const COMMAND_TIMEOUT_MS = 1500;
// Runs a command off the main thread. The timeout (SIGKILL, so a wedged client
// can't ignore it) keeps a hung compositor from stalling the poll loop forever.
export function execFileWithTimeout(command: string, args: string[]): Promise<string> {
return new Promise((resolve, reject) => {
execFile(
command,
args,
{ encoding: 'utf-8', timeout: COMMAND_TIMEOUT_MS, killSignal: 'SIGKILL' },
(error, stdout) => {
if (error) {
reject(error);
return;
}
resolve(stdout);
},
);
});
}
// Wraps an async task so overlapping calls coalesce: at most one run is in
// flight, and every call made during it shares a single follow-up run. The
// returned promise settles after a run that started no earlier than the call.
export function createCoalescedTask(task: () => Promise<void>): () => Promise<void> {
let active: Promise<void> | null = null;
let queued: Promise<void> | null = null;
const run = (): Promise<void> => {
const current: Promise<void> = task().finally(() => {
if (active === current) {
active = null;
}
});
active = current;
return current;
};
return () => {
if (queued) {
return queued;
}
if (!active) {
return run();
}
const next = (): Promise<void> => {
queued = null;
return run();
};
queued = active.then(next, next);
return queued;
};
}
// Caches process command lines by pid. A pid's argv doesn't change, so each
// pid costs one `ps` spawn. Pids missing from the latest lookup are evicted,
// which keeps the cache bounded and re-reads a pid if it gets reused.
export class ProcessCommandLineCache {
private readonly commandLines = new Map<number, string>();
constructor(private readonly runCommand: CommandRunner) {}
// A pid whose `ps` call fails (e.g. it exited mid-poll) is left out of the
// result instead of failing the lookup. Empty results aren't cached, so that
// pid is retried on the next poll.
async lookup(pids: readonly number[]): Promise<ReadonlyMap<number, string>> {
const wanted = new Set(pids);
for (const pid of this.commandLines.keys()) {
if (!wanted.has(pid)) {
this.commandLines.delete(pid);
}
}
await Promise.all(
[...wanted]
.filter((pid) => !this.commandLines.has(pid))
.map(async (pid) => {
const commandLine = (
await this.runCommand('ps', ['-p', String(pid), '-o', 'args=']).catch(() => '')
).trim();
if (commandLine) {
this.commandLines.set(pid, commandLine);
}
}),
);
return this.commandLines;
}
}
+23
View File
@@ -0,0 +1,23 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { SwayWindowTracker } from './sway-tracker';
test('SwayWindowTracker tracks the mpv window bound to the target socket', async () => {
const tree = {
nodes: [
{ pid: 10, app_id: 'mpv', rect: { x: 0, y: 0, width: 800, height: 600 } },
{ pid: 20, app_id: 'mpv', rect: { x: 100, y: 50, width: 1280, height: 720 } },
],
};
const commandLines = new Map([
['10', 'mpv --input-ipc-server=/tmp/other.sock a.mkv'],
['20', 'mpv --input-ipc-server=/tmp/subminer.sock b.mkv'],
]);
const tracker = new SwayWindowTracker('/tmp/subminer.sock', async (command, args) =>
command === 'swaymsg' ? JSON.stringify(tree) : (commandLines.get(args[1]!) ?? ''),
);
await tracker.refreshNow();
assert.deepEqual(tracker.getGeometry(), { x: 100, y: 50, width: 1280, height: 720 });
});
+55 -38
View File
@@ -16,8 +16,13 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { execSync } from 'child_process';
import { BaseWindowTracker } from './base-tracker';
import {
createCoalescedTask,
execFileWithTimeout,
ProcessCommandLineCache,
type CommandRunner,
} from './linux-poll';
interface SwayRect {
x: number;
@@ -38,24 +43,36 @@ interface SwayNode {
export class SwayWindowTracker extends BaseWindowTracker {
private pollInterval: ReturnType<typeof setInterval> | null = null;
private readonly targetMpvSocketPath: string | null;
private readonly runCommand: CommandRunner;
private readonly commandLines: ProcessCommandLineCache;
private stopped = false;
private readonly pollGeometry = createCoalescedTask(() => this.pollGeometryOnce());
constructor(targetMpvSocketPath?: string) {
constructor(targetMpvSocketPath?: string, runCommand: CommandRunner = execFileWithTimeout) {
super();
this.targetMpvSocketPath = targetMpvSocketPath?.trim() || null;
this.runCommand = runCommand;
this.commandLines = new ProcessCommandLineCache(runCommand);
}
start(): void {
this.pollInterval = setInterval(() => this.pollGeometry(), 250);
this.pollGeometry();
this.stopped = false;
this.pollInterval = setInterval(() => void this.pollGeometry(), 250);
void this.pollGeometry();
}
stop(): void {
this.stopped = true;
if (this.pollInterval) {
clearInterval(this.pollInterval);
this.pollInterval = null;
}
}
override refreshNow(): Promise<void> {
return this.pollGeometry();
}
private collectMpvWindows(node: SwayNode): SwayNode[] {
const windows: SwayNode[] = [];
if (node.app_id === 'mpv' || node.window_properties?.class === 'mpv') {
@@ -77,56 +94,56 @@ export class SwayWindowTracker extends BaseWindowTracker {
return windows;
}
private findTargetSocketWindow(node: SwayNode): SwayNode | null {
private async findTargetSocketWindow(node: SwayNode): Promise<SwayNode | null> {
const windows = this.collectMpvWindows(node);
if (!this.targetMpvSocketPath) {
return windows[0] || null;
}
return windows.find((candidate) => this.isWindowForTargetSocket(candidate)) || null;
const commandLines = await this.commandLines.lookup(
windows.flatMap((window) => (window.pid ? [window.pid] : [])),
);
return (
windows.find((candidate) => {
const commandLine = candidate.pid ? commandLines.get(candidate.pid) : undefined;
return commandLine !== undefined && this.matchesTargetSocket(commandLine);
}) || null
);
}
private isWindowForTargetSocket(node: SwayNode): boolean {
if (!node.pid) {
return false;
}
const commandLine = this.getWindowCommandLine(node.pid);
if (!commandLine) {
return false;
}
private matchesTargetSocket(commandLine: string): boolean {
return (
commandLine.includes(`--input-ipc-server=${this.targetMpvSocketPath}`) ||
commandLine.includes(`--input-ipc-server ${this.targetMpvSocketPath}`)
);
}
private getWindowCommandLine(pid: number): string | null {
const commandLine = execSync(`ps -p ${pid} -o args=`, {
encoding: 'utf-8',
}).trim();
return commandLine || null;
}
private async pollGeometryOnce(): Promise<void> {
if (this.stopped) {
return;
}
private pollGeometry(): void {
let mpvWindow: SwayNode | null;
try {
const output = execSync('swaymsg -t get_tree', { encoding: 'utf-8' });
const tree: SwayNode = JSON.parse(output);
const mpvWindow = this.findTargetSocketWindow(tree);
const tree = JSON.parse(await this.runCommand('swaymsg', ['-t', 'get_tree'])) as SwayNode;
mpvWindow = await this.findTargetSocketWindow(tree);
} catch {
// swaymsg/ps not available, timed out, or failed - keep the last geometry
return;
}
if (this.stopped) {
return;
}
if (mpvWindow && mpvWindow.rect) {
this.updateGeometry({
x: mpvWindow.rect.x,
y: mpvWindow.rect.y,
width: mpvWindow.rect.width,
height: mpvWindow.rect.height,
});
} else {
this.updateGeometry(null);
}
} catch (err) {
// swaymsg not available or failed - silent fail
if (mpvWindow && mpvWindow.rect) {
this.updateGeometry({
x: mpvWindow.rect.x,
y: mpvWindow.rect.y,
width: mpvWindow.rect.width,
height: mpvWindow.rect.height,
});
} else {
this.updateGeometry(null);
}
}
}
+1 -1
View File
@@ -7,6 +7,6 @@
"declarationMap": false,
"sourceMap": false
},
"include": ["src/**/*", "launcher/**/*.ts", "scripts/*.ts"],
"include": ["src/**/*", "launcher/**/*.ts", "scripts/*.ts", "e2e/**/*.ts"],
"exclude": ["node_modules", "dist", "vendor", "scripts/*.test.ts"]
}