Skip to content

Commit

Permalink
Updated tests, added test for channel page navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM committed Jul 2, 2024
1 parent 91395be commit 3ae8843
Showing 1 changed file with 99 additions and 28 deletions.
127 changes: 99 additions & 28 deletions test/compatibility.test.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,71 @@
import expect from 'expect.js';
import puppeteer from 'puppeteer';

describe('compatibility', function () {
context('YouTube', function () {
context('URLs', function () {
it('should redirect a watch_videos URL to a temporary playlist URL', async function () {
const watchVideosUrl = 'https://www.youtube.com/watch_videos?video_ids=dQw4w9WgXcQ,dQw4w9WgXcQ,dQw4w9WgXcQ,dQw4w9WgXcQ';
import expect from "expect.js";
import puppeteer from "puppeteer";

describe("compatibility", function () {
context("YouTube", function () {
context("URLs", function () {
it("should redirect a watch_videos URL to a temporary playlist URL", async function () {
const watchVideosUrl = "https://www.youtube.com/watch_videos?video_ids=dQw4w9WgXcQ,dQw4w9WgXcQ,dQw4w9WgXcQ,dQw4w9WgXcQ";
const redirectedUrl = (await fetch(watchVideosUrl)).url;

// The playlist is temporary, indicated by a TL start in the ID
expect(redirectedUrl).to.contain('https://www.youtube.com/watch?v=dQw4w9WgXcQ&list=TL');
expect(redirectedUrl).to.contain("https://www.youtube.com/watch?v=dQw4w9WgXcQ&list=TL");
});
});

context('events', function () {
it('should contain required data in the yt-navigate-finish event', async function () {
this.timeout(10000);
context("events", function () {
let browser, page;

beforeEach(async () => {
// Common setup: launching a browser and opening a new page
browser = await puppeteer.launch({ headless: true });
page = await browser.newPage();

// Set the SOCS cookie for YouTube (cookie banner)
await page.setCookie({
'name': 'SOCS',
'value': 'CAESEwgDEgk0ODE3Nzk3MjQaAmVuIAEaBgiA_LyaBg', // base64 encoded value
'domain': '.youtube.com',
'path': '/',
'secure': true,
'httpOnly': false
});
});

const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
afterEach(async () => {
await browser.close();
});

it("should contain required data in the yt-navigate-finish event for video pages", async function () {
this.timeout(10000);
let event = {};

// Create a promise that listens for the 'yt-navigate-finish' event
// Create a promise that listens for the "yt-navigate-finish" event
const waitForNavigateFinish = new Promise((resolve, reject) => {
page.on('console', msg => {
if (msg.text().includes('yt-navigate-finish')) {
// Remove the yt-navigate-finish from the string and parse the rest to event
event = JSON.parse(msg.text().replace('yt-navigate-finish ', ''));
page.on("console", msg => {
if (msg.text().includes("yt-navigate-finish")) {
event = JSON.parse(msg.text().replace("yt-navigate-finish ", ""));
resolve();
}
});
});

await page.evaluateOnNewDocument(() => {
document.addEventListener('yt-navigate-finish', function (e) {
document.addEventListener("yt-navigate-finish", function (e) {
const serializableEvent = {
channelId: e.detail?.response?.playerResponse?.videoDetails?.channelId,
channelName: e.detail?.response?.playerResponse?.videoDetails?.author
};
// Now, this object can be safely serialized to JSON
console.log('yt-navigate-finish', JSON.stringify(serializableEvent));
console.log("yt-navigate-finish", JSON.stringify(serializableEvent));
});
});

// Navigate to a YouTube page
await page.goto('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
// Navigate to a YouTube video page
await page.goto("https://www.youtube.com/watch?v=dQw4w9WgXcQ");

const timeout = new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('Timed out waiting for yt-navigate-finish event'));
reject(new Error("Timed out waiting for yt-navigate-finish event"));
}, 5000);
});

Expand All @@ -60,13 +76,68 @@ describe('compatibility', function () {
console.error(error.message);
}

await browser.close();
console.log("The following data was extracted from the yt-navigate-finish event:");
console.log(event);

expect(event.channelId).to.be("UCuAXFkgsw1L7xaCfnd5JJOw");
expect(event.channelName).to.be("Rick Astley");
});

it("should contain required data in the yt-navigate-finish event for channel pages", async function () {
this.timeout(10000);
let event = {};

// Create a promise that listens for the "yt-navigate-finish" event
const waitForNavigateFinish = new Promise((resolve, reject) => {
page.on("console", msg => {
if (msg.text().includes("yt-navigate-finish")) {
event = JSON.parse(msg.text().replace("yt-navigate-finish ", ""));
resolve();
}
});
});

await page.evaluateOnNewDocument(() => {
document.addEventListener("yt-navigate-finish", function (e) {
const serializableEvent = {
eventVersion: "default",
channelId: e.detail?.response?.response?.header?.c4TabbedHeaderRenderer?.channelId,
channelName: e.detail?.response?.response?.header?.c4TabbedHeaderRenderer?.title
};
if (!serializableEvent.channelId) {
serializableEvent.eventVersion = "20240521";
serializableEvent.channelId = e.detail?.endpoint?.browseEndpoint?.browseId;
}
if (!serializableEvent.channelName) {
serializableEvent.eventVersion = "20240521";
serializableEvent.channelName = e.detail?.response?.response?.header?.pageHeaderRenderer?.pageTitle;
}

console.log("yt-navigate-finish", JSON.stringify(serializableEvent));
});
});

// Navigate to a YouTube video page
await page.goto("https://www.youtube.com/@RickAstleyYT");

const timeout = new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error("Timed out waiting for yt-navigate-finish event"));
}, 5000);
});

// Wait for either the event to fire or the timeout
try {
await Promise.race([waitForNavigateFinish, timeout]);
} catch (error) {
console.error(error.message);
}

console.log("The following data was extracted from the yt-navigate-finish event:");
console.log(event);

expect(event.channelId).to.be('UCuAXFkgsw1L7xaCfnd5JJOw');
expect(event.channelName).to.be('Rick Astley');
expect(event.channelId).to.be("UCuAXFkgsw1L7xaCfnd5JJOw");
expect(event.channelName).to.be("Rick Astley");
});
});
});
Expand Down

0 comments on commit 3ae8843

Please sign in to comment.