From 1fb8e2e1682f6ced70972204f00a405c93154900 Mon Sep 17 00:00:00 2001 From: sudacode Date: Sat, 14 Feb 2026 15:19:37 -0800 Subject: [PATCH] refactor mpv state mapping into mpv-state helper --- src/core/services/mpv-service.ts | 20 ++++------------ src/core/services/mpv-state.test.ts | 36 +++++++++++++++++++++++++++++ src/core/services/mpv-state.ts | 25 ++++++++++++++++++++ 3 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 src/core/services/mpv-state.test.ts create mode 100644 src/core/services/mpv-state.ts diff --git a/src/core/services/mpv-service.ts b/src/core/services/mpv-service.ts index ce9532a..f5ac4cb 100644 --- a/src/core/services/mpv-service.ts +++ b/src/core/services/mpv-service.ts @@ -17,6 +17,7 @@ import { requestMpvInitialState, subscribeToMpvProperties } from "./mpv-properti import { scheduleMpvReconnect, } from "./mpv-transport"; +import { resolveCurrentAudioStreamIndex } from "./mpv-state"; export { MPV_REQUEST_ID_SECONDARY_SUB_VISIBILITY, @@ -338,21 +339,10 @@ export class MpvIpcClient implements MpvClient { "ff-index"?: number; }>, ): void { - if (!Array.isArray(tracks)) { - this.currentAudioStreamIndex = null; - return; - } - - const audioTracks = tracks.filter((track) => track.type === "audio"); - const activeTrack = - audioTracks.find((track) => track.id === this.currentAudioTrackId) || - audioTracks.find((track) => track.selected === true); - - const ffIndex = activeTrack?.["ff-index"]; - this.currentAudioStreamIndex = - typeof ffIndex === "number" && Number.isInteger(ffIndex) && ffIndex >= 0 - ? ffIndex - : null; + this.currentAudioStreamIndex = resolveCurrentAudioStreamIndex( + tracks, + this.currentAudioTrackId, + ); } send(command: { command: unknown[]; request_id?: number }): boolean { diff --git a/src/core/services/mpv-state.test.ts b/src/core/services/mpv-state.test.ts new file mode 100644 index 0000000..f33529d --- /dev/null +++ b/src/core/services/mpv-state.test.ts @@ -0,0 +1,36 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { resolveCurrentAudioStreamIndex } from "./mpv-state"; + +test("resolveCurrentAudioStreamIndex returns selected ff-index when no current track id", () => { + assert.equal( + resolveCurrentAudioStreamIndex( + [ + { type: "audio", id: 1, selected: false, "ff-index": 1 }, + { type: "audio", id: 2, selected: true, "ff-index": 3 }, + ], + null, + ), + 3, + ); +}); + +test("resolveCurrentAudioStreamIndex prefers matching current audio track id", () => { + assert.equal( + resolveCurrentAudioStreamIndex( + [ + { type: "audio", id: 1, selected: true, "ff-index": 3 }, + { type: "audio", id: 2, selected: false, "ff-index": 6 }, + ], + 2, + ), + 6, + ); +}); + +test("resolveCurrentAudioStreamIndex returns null when tracks are not an array", () => { + assert.equal( + resolveCurrentAudioStreamIndex(null, null), + null, + ); +}); diff --git a/src/core/services/mpv-state.ts b/src/core/services/mpv-state.ts new file mode 100644 index 0000000..a119bed --- /dev/null +++ b/src/core/services/mpv-state.ts @@ -0,0 +1,25 @@ +export type MpvTrackProperty = { + type?: string; + id?: number; + selected?: boolean; + "ff-index"?: number; +}; + +export function resolveCurrentAudioStreamIndex( + tracks: Array | null | undefined, + currentAudioTrackId: number | null, +): number | null { + if (!Array.isArray(tracks)) { + return null; + } + + const audioTracks = tracks.filter((track) => track.type === "audio"); + const activeTrack = + audioTracks.find((track) => track.id === currentAudioTrackId) || + audioTracks.find((track) => track.selected === true); + + const ffIndex = activeTrack?.["ff-index"]; + return typeof ffIndex === "number" && Number.isInteger(ffIndex) && ffIndex >= 0 + ? ffIndex + : null; +}