From 9b30a9e5f6f26d6ae73c3617d023d74048c45b7e Mon Sep 17 00:00:00 2001 From: SAROND Date: Tue, 16 Jan 2024 17:26:53 +0400 Subject: [PATCH] hotfix: correct traversal paths and improve test assertions Fixed incorrect object traversal paths in `YTMusic.getHome` method to properly navigate through nested JSON. Enhanced `getHome.spec.ts` by adding a check for non-empty array of `HomePageContent` and logging the length of the retrieved contents. --- src/YTMusic.ts | 4 ++-- src/parsers/AlbumParser.ts | 1 - src/tests/getHome.spec.ts | 9 ++++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/YTMusic.ts b/src/YTMusic.ts index 55a7555..11fb846 100644 --- a/src/YTMusic.ts +++ b/src/YTMusic.ts @@ -510,7 +510,7 @@ export default class YTMusic { public async getHome(): Promise { const results: HomePageContent[] = [] const page = await this.constructRequest("browse", { browseId: FE_MUSIC_HOME }) - traverseList(page, "contents").forEach(content => { + traverseList(page, "sectionListRenderer", "contents").forEach(content => { const parsed = Parser.parseMixedContent(content) parsed && results.push(parsed) }) @@ -518,7 +518,7 @@ export default class YTMusic { let continuation = traverseString(page, "continuation") while (continuation) { const nextPage = await this.constructRequest("browse", {}, { continuation }) - traverseList(nextPage, "contents").forEach(content => { + traverseList(nextPage, "sectionListContinuation", "contents").forEach(content => { const parsed = Parser.parseMixedContent(content) parsed && results.push(parsed) }) diff --git a/src/parsers/AlbumParser.ts b/src/parsers/AlbumParser.ts index a38c0be..278305e 100644 --- a/src/parsers/AlbumParser.ts +++ b/src/parsers/AlbumParser.ts @@ -39,7 +39,6 @@ export default class AlbumParser { public static parseSearchResult(item: any): AlbumDetailed { const columns = traverseList(item, "flexColumns", "runs").flat() - columns.push(item) // No specific way to identify the title const title = columns[0] diff --git a/src/tests/getHome.spec.ts b/src/tests/getHome.spec.ts index 71241bd..cca7d17 100644 --- a/src/tests/getHome.spec.ts +++ b/src/tests/getHome.spec.ts @@ -1,5 +1,5 @@ import { arrayOf, Problem, Type } from "arktype" -import { equal } from "assert" +import { equal, ok } from "assert" import { afterAll, beforeEach, describe, it } from "bun:test" import { HomePageContent } from "../@types/types" @@ -25,6 +25,7 @@ const expect = (data: any, type: Type) => { } equal(result.problems, undefined) } + const ytmusic = new YTMusic() beforeEach(() => { const index = 0 @@ -34,8 +35,10 @@ beforeEach(() => { describe(`Query: ${FE_MUSIC_HOME}`, () => { configs.forEach(config => { it(`Get ${config.GL} ${config.HL}`, async () => { - const page = await ytmusic.getHome() - expect(page, arrayOf(HomePageContent)) + const homePageContents = await ytmusic.getHome() + ok(homePageContents.length) + expect(homePageContents, arrayOf(HomePageContent)) + console.log("Length: ", homePageContents.length) }) }) })