Skip to content

Commit

Permalink
utilize singleton behavior for puppeteer and migrate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ronny1982 committed Oct 26, 2024
1 parent 0f78148 commit 63c3cba
Show file tree
Hide file tree
Showing 734 changed files with 1,418 additions and 2,923 deletions.
29 changes: 13 additions & 16 deletions test/PuppeteerFixture.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import * as puppeteer from 'puppeteer-core';
import { AppURL } from './PuppeteerGlobal';
import { AppURL, AppSelector } from './PuppeteerGlobal';

export class PuppeteerFixture {

#browser: puppeteer.Browser;
#page: puppeteer.Page;
static #browser = puppeteer.connect({ browserWSEndpoint: process.env.browserWS });
static #page = this.#browser.then(browser => browser.pages()).then(pages => pages.find(page => page.url() === AppURL));

constructor() {}

protected get Browser() {
return this.#browser;
protected async Reload(): Promise<void> {
const page = await PuppeteerFixture.#page;
await page.reload({ timeout: 5000 });
await page.waitForSelector(AppSelector, { timeout: 7500 });
}

protected get Page() {
return this.#page;
protected async OpenPage(url : string): Promise<puppeteer.Page> {
const page = await (await PuppeteerFixture.#browser).newPage();
await page.goto(url);
return page;
}

public async Connect<T extends this>(): Promise<T> {
this.#browser = await puppeteer.connect({ browserWSEndpoint: process.env.browserWS });
const pages = await this.#browser.pages();
this.#page = pages.find(page => page.url() === AppURL);
await this.#page.reload();
await this.#page.waitForSelector('body div#app main#hakunekoapp', { timeout: 7500 });
return this as T;
protected EvaluateHandle: typeof puppeteer.Page.prototype.evaluateHandle = async (pageFunction, ...args) => {
return (await PuppeteerFixture.#page).evaluateHandle(pageFunction, ...args);
}
}
5 changes: 4 additions & 1 deletion test/PuppeteerGlobal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { exec, spawn } from 'node:child_process';
import * as puppeteer from 'puppeteer-core';

export const AppURL = 'http://localhost:5000/';
export const AppSelector = 'body #app main#hakunekoapp';
const viteExe = path.normalize(path.resolve('node_modules', '.bin', process.platform === 'win32' ? 'vite.cmd' : 'vite'));
const tempDir = path.normalize(path.resolve(os.tmpdir(), 'hakuneko-test', Date.now().toString(32)));
const userDir = path.normalize(path.resolve(tempDir, 'user-data'));
Expand Down Expand Up @@ -71,14 +72,16 @@ async function LaunchNW(): Promise<puppeteer.Browser> {
userDataDir: userDir
});
browser.on('targetcreated', CloseSplashScreen);
console.log(new Date().toISOString(), '➔', 'Remote Debugger:', browser.wsEndpoint());

const start = Date.now();
while(Date.now() - start < 7500) {
const pages = await browser.pages();
const page = pages.find(p => p.url() === AppURL);
if(page) {
await page.reload({ timeout: 5000 });
await page.waitForSelector(AppSelector, { timeout: 7500 });
console.log(new Date().toISOString(), '➔', 'Using Page:', [ page.url() ]);
console.log(new Date().toISOString(), '➔', 'Remote Debugger:', browser.wsEndpoint());
return browser;
} else {
console.log(new Date().toISOString(), '➔', 'Waiting for Page(s):', pages.map(p => p.url()));
Expand Down
10 changes: 6 additions & 4 deletions web/src/cloudflare_e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ export class TestFixture extends PuppeteerFixture {
private page: Page;

public async SetupLocation(url: string): Promise<TestFixture> {
await this.Connect();
this.page = this.page || await super.Browser.newPage();
await this.page.goto(url);
if(this.page) {
await this.page.goto(url);
} else {
this.page = await this.OpenPage(url);
}
return this;
}

Expand All @@ -29,7 +31,7 @@ describe('CloudFlare Bypass', () => {

it.skip('Should bypass JavaScript Challenge', async () => {

const fixture = await new TestFixture().SetupLocation('https://test.cloudscraper.ovh/challenge');
const fixture = await new TestFixture().SetupLocation('https://cloudflare.bot-check.ovh/automatic');

try {
const challenge = await fixture.GetRemoteProperty('form#challenge-form', 'action');
Expand Down
6 changes: 3 additions & 3 deletions web/src/engine/PluginController_e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import type { PluginController } from './PluginController';
export class RemoteFixture extends PuppeteerFixture {

public async GetRemotePluginController(): Promise<JSHandle<PluginController>> {
return super.Page.evaluateHandle(async () => {
return this.EvaluateHandle(async () => {
return window.HakuNeko.PluginController;
});
}

public async GetRemoteWebsitePlugins(): Promise<JSHandle<ReadonlyArray<MediaContainer<MediaContainer<MediaChild>>>>> {
return super.Page.evaluateHandle(async () => {
return this.EvaluateHandle(async () => {
return window.HakuNeko.PluginController.WebsitePlugins;
});
}
Expand All @@ -23,7 +23,7 @@ describe('PluginController', () => {

describe('Website Icons', { concurrent: true }, async () => {

const fixture = await new RemoteFixture().Connect();
const fixture = new RemoteFixture();
const plugins = await fixture.GetRemoteWebsitePlugins();
const icons = await plugins.evaluate(testee => testee.map(website => {
return {
Expand Down
4 changes: 2 additions & 2 deletions web/src/engine/SettingsGlobal_e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Key } from './SettingsGlobal';
export class TestFixture extends PuppeteerFixture {

public async GetRemoteGlobalSettings(): Promise<JSHandle<ISettings>> {
return super.Page.evaluateHandle(async () => {
return this.EvaluateHandle(async () => {
return window.HakuNeko.SettingsManager.OpenScope();
});
}
Expand All @@ -17,7 +17,7 @@ describe('SettingsGlobal', () => {

it('Should be initialized and accessible', async () => {

const fixture = await new TestFixture().Connect();
const fixture = new TestFixture();
const remoteTestee = await fixture.GetRemoteGlobalSettings();

expect(await remoteTestee.evaluate(testee => testee.Get('language').ID)).toBe(Key.Language);
Expand Down
4 changes: 1 addition & 3 deletions web/src/engine/websites/AGCScanlation_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe } from 'vitest';
import { TestFixture, type Config } from '../../../test/WebsitesFixture';

const config: Config = {
Expand All @@ -22,5 +21,4 @@ const config: Config = {
}
};

const fixture = new TestFixture(config);
describe(fixture.Name, async () => (await fixture.Connect()).AssertWebsite());
new TestFixture(config).AssertWebsite();
6 changes: 2 additions & 4 deletions web/src/engine/websites/AGS_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { describe } from 'vitest';
import { TestFixture } from '../../../test/WebsitesFixture';
import { TestFixture } from '../../../test/WebsitesFixture';

const config = {
plugin: {
Expand All @@ -22,5 +21,4 @@ const config = {
}
};

const fixture = new TestFixture(config);
describe(fixture.Name, async () => (await fixture.Connect()).AssertWebsite());
new TestFixture(config).AssertWebsite();
4 changes: 1 addition & 3 deletions web/src/engine/websites/APKomik_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe } from 'vitest';
import { TestFixture, type Config } from '../../../test/WebsitesFixture';

const config: Config = {
Expand All @@ -22,5 +21,4 @@ const config: Config = {
}*/
};

const fixture = new TestFixture(config);
describe(fixture.Name, async () => (await fixture.Connect()).AssertWebsite());
new TestFixture(config).AssertWebsite();
10 changes: 3 additions & 7 deletions web/src/engine/websites/AdonisFansub_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { describe } from 'vitest';
import { TestFixture } from '../../../test/WebsitesFixture';
import { TestFixture } from '../../../test/WebsitesFixture';

const config = {
new TestFixture({
plugin: {
id: 'adonisfansub',
title: 'Adonis Fansub'
Expand All @@ -20,7 +19,4 @@ const config = {
size: 408_328,
type: 'image/jpeg'
}
};

const fixture = new TestFixture(config);
describe(fixture.Name, async () => (await fixture.Connect()).AssertWebsite());
}).AssertWebsite();
6 changes: 2 additions & 4 deletions web/src/engine/websites/AduManga_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { describe } from 'vitest';
import { TestFixture } from '../../../test/WebsitesFixture';
import { TestFixture } from '../../../test/WebsitesFixture';

const config = {
plugin: {
Expand All @@ -22,5 +21,4 @@ const config = {
}
};

const fixture = new TestFixture(config);
describe(fixture.Name, async () => (await fixture.Connect()).AssertWebsite());
new TestFixture(config).AssertWebsite();
4 changes: 1 addition & 3 deletions web/src/engine/websites/Ainzscans_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe } from 'vitest';
import { TestFixture, type Config } from '../../../test/WebsitesFixture';

const config: Config = {
Expand All @@ -22,5 +21,4 @@ const config: Config = {
}
};

const fixture = new TestFixture(config);
describe(fixture.Name, async () => (await fixture.Connect()).AssertWebsite());
new TestFixture(config).AssertWebsite();
8 changes: 3 additions & 5 deletions web/src/engine/websites/Akuma_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { describe } from 'vitest';
import { TestFixture } from '../../../test/WebsitesFixture';
import { TestFixture } from '../../../test/WebsitesFixture';

const fixture = new TestFixture({
new TestFixture({
plugin: {
id: 'akuma',
title: 'Akuma.moe'
Expand All @@ -20,5 +19,4 @@ const fixture = new TestFixture({
size: 223_038,
type: 'image/jpeg'
}
});
describe(fixture.Name, async () => (await fixture.Connect()).AssertWebsite());
}).AssertWebsite();
4 changes: 1 addition & 3 deletions web/src/engine/websites/AllPornComic_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe } from 'vitest';
import { TestFixture } from '../../../test/WebsitesFixture';

const config = {
Expand All @@ -22,5 +21,4 @@ const config = {
}
};

const fixture = new TestFixture(config);
describe(fixture.Name, async () => (await fixture.Connect()).AssertWebsite());
new TestFixture(config).AssertWebsite();
19 changes: 7 additions & 12 deletions web/src/engine/websites/Alphapolis_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe } from 'vitest';
import { TestFixture, type Config } from '../../../test/WebsitesFixture';
import { TestFixture } from '../../../test/WebsitesFixture';

const configOfficial: Config = {
// CASE: Official
new TestFixture({
plugin: {
id: 'alphapolis',
title: 'ALPHAPOLIS (アルファポリス)'
Expand All @@ -20,12 +20,10 @@ const configOfficial: Config = {
size: 375_221,
type: 'image/jpeg'
}
};
}).AssertWebsite();

const fixtureOfficial = new TestFixture(configOfficial);
describe(fixtureOfficial.Name, async () => (await fixtureOfficial.Connect()).AssertWebsite());

const configManga: Config = {
// CASE: Unofficial
new TestFixture({
plugin: {
id: 'alphapolis',
title: 'ALPHAPOLIS (アルファポリス)'
Expand All @@ -44,7 +42,4 @@ const configManga: Config = {
size: 217_919,
type: 'image/jpeg'
}
};

const fixtureManga = new TestFixture(configManga);
describe(fixtureManga.Name, async () => (await fixtureManga.Connect()).AssertWebsite());
}).AssertWebsite();
6 changes: 2 additions & 4 deletions web/src/engine/websites/AmuyScan_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { describe } from 'vitest';
import { TestFixture } from '../../../test/WebsitesFixture';
import { TestFixture } from '../../../test/WebsitesFixture';

const config = {
plugin: {
Expand All @@ -22,5 +21,4 @@ const config = {
}
};

const fixture = new TestFixture(config);
describe(fixture.Name, async () => (await fixture.Connect()).AssertWebsite());
new TestFixture(config).AssertWebsite();
6 changes: 2 additions & 4 deletions web/src/engine/websites/AniGliScans_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { describe } from 'vitest';
import { TestFixture } from '../../../test/WebsitesFixture';
import { TestFixture } from '../../../test/WebsitesFixture';

const config = {
plugin: {
Expand All @@ -22,5 +21,4 @@ const config = {
}
};

const fixture = new TestFixture(config);
describe(fixture.Name, async () => (await fixture.Connect()).AssertWebsite());
new TestFixture(config).AssertWebsite();
4 changes: 1 addition & 3 deletions web/src/engine/websites/Anikiga_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe } from 'vitest';
import { TestFixture } from '../../../test/WebsitesFixture';

const config = {
Expand All @@ -22,5 +21,4 @@ const config = {
}
};

const fixture = new TestFixture(config);
describe(fixture.Name, async () => (await fixture.Connect()).AssertWebsite());
new TestFixture(config).AssertWebsite();
4 changes: 1 addition & 3 deletions web/src/engine/websites/AnimeSama_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe } from 'vitest';
import { TestFixture, type Config } from '../../../test/WebsitesFixture';

const config: Config = {
Expand All @@ -22,5 +21,4 @@ const config: Config = {
}
};

const fixture = new TestFixture(config);
describe(fixture.Name, async () => (await fixture.Connect()).AssertWebsite());
new TestFixture(config).AssertWebsite();
4 changes: 1 addition & 3 deletions web/src/engine/websites/Anisamanga_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe } from 'vitest';
import { TestFixture } from '../../../test/WebsitesFixture';

const config = {
Expand All @@ -22,5 +21,4 @@ const config = {
}
};

const fixture = new TestFixture(config);
describe(fixture.Name, async () => (await fixture.Connect()).AssertWebsite());
new TestFixture(config).AssertWebsite();
6 changes: 2 additions & 4 deletions web/src/engine/websites/AnzManga_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { describe } from 'vitest';
import { TestFixture } from '../../../test/WebsitesFixture';
import { TestFixture } from '../../../test/WebsitesFixture';

const config = {
plugin: {
Expand All @@ -22,5 +21,4 @@ const config = {
}
};

const fixture = new TestFixture(config);
describe(fixture.Name, async () => (await fixture.Connect()).AssertWebsite());
new TestFixture(config).AssertWebsite();
Loading

0 comments on commit 63c3cba

Please sign in to comment.