feat(spotify): report resolved backend + binaryAvailable on /status; share backend resolver [S4.2]

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
saopig1andClaude Opus 4.8 committed 2026-07-03 00:45:55 +08:00
1 parent b672d76634
commit 6a72833c2a
6 files changed
+79 -21

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
import { describe, it, expect } from "vitest";
import { resolveSpotifyBackendKind as pick } from "./backend-select.js";
describe("resolveSpotifyBackendKind", () => {
it("auto: go present -> go-librespot", () => expect(pick("auto", true, true)).toBe("go-librespot"));
it("auto: go absent, rust present -> librespot", () => expect(pick("auto", false, true)).toBe("librespot"));
it("auto: neither -> null", () => expect(pick("auto", false, false)).toBeNull());
it("go-librespot: present -> go-librespot", () => expect(pick("go-librespot", true, true)).toBe("go-librespot"));
it("go-librespot: absent -> null even if rust present", () => expect(pick("go-librespot", false, true)).toBeNull());
it("librespot: present -> librespot", () => expect(pick("librespot", true, true)).toBe("librespot"));
it("librespot: absent -> null even if go present", () => expect(pick("librespot", true, false)).toBeNull());
it("auto default fallthrough matches auto", () => expect(pick("auto", true, false)).toBe("go-librespot"));
});
+25
View File
@@ -0,0 +1,25 @@
/** Which concrete backend runs for a given config + host binary availability. */
export type SpotifyBackendKind = "go-librespot" | "librespot";
/**
* Pure backend selection shared by SpotifyController.chooseBackend() (per-bot)
* and the web /status endpoint (process-wide). Booleans in, no IO — the caller
* supplies platform+binary presence.
*/
export function resolveSpotifyBackendKind(
backend: "auto" | "go-librespot" | "librespot",
goPresent: boolean,
rustPresent: boolean,
): SpotifyBackendKind | null {
switch (backend) {
case "go-librespot":
return goPresent ? "go-librespot" : null;
case "librespot":
return rustPresent ? "librespot" : null;
case "auto":
default:
if (goPresent) return "go-librespot";
if (rustPresent) return "librespot";
return null;
}
}
+10 -14
View File
@@ -29,9 +29,11 @@ import {
type OAuthTokenStore,
} from "./spotify-oauth.js";
import { SpotifyConnectApi } from "./connect-api.js";
/** Which concrete backend the controller will run for this host + config. */
export type SpotifyBackendKind = "go-librespot" | "librespot";
import {
resolveSpotifyBackendKind,
type SpotifyBackendKind,
} from "./backend-select.js";
export type { SpotifyBackendKind }; // keep the name exported for existing importers
/**
* Minimal file-backed OAuth token store used when the caller does not inject a
@@ -156,17 +158,11 @@ export class SpotifyController extends EventEmitter {
* librespot present, else null.
*/
chooseBackend(): SpotifyBackendKind | null {
switch (this.config.backend) {
case "go-librespot":
return this.goPresent() ? "go-librespot" : null;
case "librespot":
return this.rustPresent() ? "librespot" : null;
case "auto":
default:
if (this.goPresent()) return "go-librespot";
if (this.rustPresent()) return "librespot";
return null;
}
return resolveSpotifyBackendKind(
this.config.backend,
this.goPresent(),
this.rustPresent(),
);
}
/** enabled in config AND a backend is selectable (platform + binary present). */