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.
This commit is contained in:
SAROND 2024-01-16 17:26:53 +04:00
parent f1eaeb7143
commit 9b30a9e5f6
3 changed files with 8 additions and 6 deletions

View File

@ -510,7 +510,7 @@ export default class YTMusic {
public async getHome(): Promise<HomePageContent[]> { public async getHome(): Promise<HomePageContent[]> {
const results: HomePageContent[] = [] const results: HomePageContent[] = []
const page = await this.constructRequest("browse", { browseId: FE_MUSIC_HOME }) 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) const parsed = Parser.parseMixedContent(content)
parsed && results.push(parsed) parsed && results.push(parsed)
}) })
@ -518,7 +518,7 @@ export default class YTMusic {
let continuation = traverseString(page, "continuation") let continuation = traverseString(page, "continuation")
while (continuation) { while (continuation) {
const nextPage = await this.constructRequest("browse", {}, { continuation }) const nextPage = await this.constructRequest("browse", {}, { continuation })
traverseList(nextPage, "contents").forEach(content => { traverseList(nextPage, "sectionListContinuation", "contents").forEach(content => {
const parsed = Parser.parseMixedContent(content) const parsed = Parser.parseMixedContent(content)
parsed && results.push(parsed) parsed && results.push(parsed)
}) })

View File

@ -39,7 +39,6 @@ export default class AlbumParser {
public static parseSearchResult(item: any): AlbumDetailed { public static parseSearchResult(item: any): AlbumDetailed {
const columns = traverseList(item, "flexColumns", "runs").flat() const columns = traverseList(item, "flexColumns", "runs").flat()
columns.push(item)
// No specific way to identify the title // No specific way to identify the title
const title = columns[0] const title = columns[0]

View File

@ -1,5 +1,5 @@
import { arrayOf, Problem, Type } from "arktype" import { arrayOf, Problem, Type } from "arktype"
import { equal } from "assert" import { equal, ok } from "assert"
import { afterAll, beforeEach, describe, it } from "bun:test" import { afterAll, beforeEach, describe, it } from "bun:test"
import { HomePageContent } from "../@types/types" import { HomePageContent } from "../@types/types"
@ -25,6 +25,7 @@ const expect = (data: any, type: Type) => {
} }
equal(result.problems, undefined) equal(result.problems, undefined)
} }
const ytmusic = new YTMusic() const ytmusic = new YTMusic()
beforeEach(() => { beforeEach(() => {
const index = 0 const index = 0
@ -34,8 +35,10 @@ beforeEach(() => {
describe(`Query: ${FE_MUSIC_HOME}`, () => { describe(`Query: ${FE_MUSIC_HOME}`, () => {
configs.forEach(config => { configs.forEach(config => {
it(`Get ${config.GL} ${config.HL}`, async () => { it(`Get ${config.GL} ${config.HL}`, async () => {
const page = await ytmusic.getHome() const homePageContents = await ytmusic.getHome()
expect(page, arrayOf(HomePageContent)) ok(homePageContents.length)
expect(homePageContents, arrayOf(HomePageContent))
console.log("Length: ", homePageContents.length)
}) })
}) })
}) })