mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-02 19:02:50 +08:00
fix(netease): warn about missing playlist track details
After all batches succeed, log one warning with the playlist ID and expected, matched, and missing track counts. Return the matched tracks in playlist order.
This commit is contained in:
3 files changed
+23
-9
No files matched your search
+1
-1
@@ -67,7 +67,7 @@ async function main() {
|
||||
);
|
||||
await apiServer.start();
|
||||
|
||||
const neteaseProvider = new NeteaseProvider(apiServer.getNeteaseBaseUrl());
|
||||
const neteaseProvider = new NeteaseProvider(apiServer.getNeteaseBaseUrl(), logger);
|
||||
const bilibiliProvider = new BiliBiliProvider();
|
||||
const localProvider = new LocalMusicProvider(LOCAL_AUDIO_DIR);
|
||||
const kugouProvider = new KugouProvider();
|
||||
|
||||
@@ -239,6 +239,7 @@ describe("NeteaseProvider.search pagination", () => {
|
||||
|
||||
describe("NeteaseProvider.getPlaylistSongs (#200)", () => {
|
||||
function playlistFixture(size: number) {
|
||||
const warn = vi.fn();
|
||||
const ids = Array.from({ length: size }, (_, i) => i + 1);
|
||||
const get = vi.fn().mockResolvedValue({
|
||||
data: { playlist: { trackIds: ids.map(id => ({ id })) } },
|
||||
@@ -246,16 +247,16 @@ describe("NeteaseProvider.getPlaylistSongs (#200)", () => {
|
||||
const post = vi.fn(async (_path: string, body: { ids: string }) => ({
|
||||
data: { songs: body.ids.split(",").reverse().map(id => ({ id: Number(id) })) },
|
||||
}));
|
||||
const provider = new NeteaseProvider("http://x");
|
||||
provider.setCookie("MUSIC_U=test");
|
||||
const provider = new NeteaseProvider("http://x", { warn }).withCookie("MUSIC_U=test");
|
||||
(provider as any).api = { get, post };
|
||||
return { provider, ids, get, post };
|
||||
return { provider, ids, get, post, warn };
|
||||
}
|
||||
|
||||
it.each([0, 12, 1000, 1360, 2000])("loads %i tracks in playlist order", async size => {
|
||||
const { provider, ids, get, post } = playlistFixture(size);
|
||||
const { provider, ids, get, post, warn } = playlistFixture(size);
|
||||
const songs = await provider.getPlaylistSongs("920292129");
|
||||
expect(songs.map(song => song.id)).toEqual(ids.map(String));
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
expect(get.mock.calls).toEqual([["/playlist/detail", {
|
||||
params: { id: "920292129", cookie: "MUSIC_U=test" },
|
||||
}]]);
|
||||
@@ -268,17 +269,21 @@ describe("NeteaseProvider.getPlaylistSongs (#200)", () => {
|
||||
});
|
||||
|
||||
it("continues past unavailable tracks in an earlier batch", async () => {
|
||||
const { provider, post } = playlistFixture(1002);
|
||||
const { provider, post, warn } = playlistFixture(1002);
|
||||
post.mockResolvedValueOnce({ data: { songs: [{ id: 1 }] } });
|
||||
const songs = await provider.getPlaylistSongs("920292129");
|
||||
expect(songs.map(song => song.id)).toEqual(["1", "1001", "1002"]);
|
||||
expect(warn.mock.calls).toEqual([[{
|
||||
playlistId: "920292129", expectedCount: 1002, matchedCount: 3, missingCount: 999,
|
||||
}, "NetEase playlist track IDs missing song details"]]);
|
||||
});
|
||||
|
||||
it("propagates a failed batch instead of returning a partial playlist", async () => {
|
||||
const { provider, post } = playlistFixture(1360);
|
||||
const { provider, post, warn } = playlistFixture(1360);
|
||||
post.mockResolvedValueOnce({ data: { songs: [{ id: 1 }] } })
|
||||
.mockRejectedValueOnce(new Error("sidecar unavailable"));
|
||||
await expect(provider.getPlaylistSongs("920292129")).rejects.toThrow("sidecar unavailable");
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+11
-2
@@ -1,5 +1,6 @@
|
||||
import { normalizeCookie } from "./cookie-normalize.js";
|
||||
import axios, { type AxiosInstance } from "axios";
|
||||
import type { Logger } from "../logger.js";
|
||||
import { RISK_CONTROL_CODE, RiskControlError, SongUrlUnavailable } from "./provider.js";
|
||||
import type { PlayFailure } from "./provider.js";
|
||||
import type {
|
||||
@@ -236,7 +237,7 @@ export class NeteaseProvider implements MusicProvider {
|
||||
private quality = "exhigh";
|
||||
private readonly baseUrl: string;
|
||||
|
||||
constructor(baseUrl: string) {
|
||||
constructor(baseUrl: string, private readonly logger: Pick<Logger, "warn"> = console) {
|
||||
this.baseUrl = baseUrl;
|
||||
this.api = axios.create({
|
||||
baseURL: baseUrl,
|
||||
@@ -384,6 +385,14 @@ export class NeteaseProvider implements MusicProvider {
|
||||
if (song) songs.push(song);
|
||||
}
|
||||
}
|
||||
if (songs.length < trackIds.length) {
|
||||
this.logger.warn({
|
||||
playlistId,
|
||||
expectedCount: trackIds.length,
|
||||
matchedCount: songs.length,
|
||||
missingCount: trackIds.length - songs.length,
|
||||
}, "NetEase playlist track IDs missing song details");
|
||||
}
|
||||
return songs;
|
||||
}
|
||||
|
||||
@@ -533,7 +542,7 @@ export class NeteaseProvider implements MusicProvider {
|
||||
* a web user's personal FM uses their own taste instead of the shared login.
|
||||
*/
|
||||
withCookie(cookie: string): NeteaseProvider {
|
||||
const view = new NeteaseProvider(this.baseUrl);
|
||||
const view = new NeteaseProvider(this.baseUrl, this.logger);
|
||||
view.setQuality(this.quality);
|
||||
view.setCookie(cookie);
|
||||
return view;
|
||||
|
||||
Reference in new issue
Block a user