Improve logging of provider unit tests

This commit is contained in:
mrjvs
2024-01-17 18:54:42 +01:00
parent 616c8a1e1b
commit 03a628ea34
2 changed files with 102 additions and 97 deletions

View File

@@ -38,7 +38,7 @@
"cli": "ts-node ./src/dev-cli/index.ts", "cli": "ts-node ./src/dev-cli/index.ts",
"test": "vitest run", "test": "vitest run",
"test:watch": "vitest", "test:watch": "vitest",
"test:providers": "cross-env MW_TEST_PROVIDERS=true vitest run", "test:providers": "cross-env MW_TEST_PROVIDERS=true vitest run --reporter verbose",
"test:integration": "node ./tests/cjs && node ./tests/esm && node ./tests/browser", "test:integration": "node ./tests/cjs && node ./tests/esm && node ./tests/browser",
"test:coverage": "vitest run --coverage", "test:coverage": "vitest run --coverage",
"lint": "eslint --ext .ts,.js src/", "lint": "eslint --ext .ts,.js src/",

View File

@@ -53,62 +53,65 @@ export function testSource(ops: TestSourceOptions) {
if (ops.testSuite.length === 0) throw new Error("Test suite must have at least one test"); if (ops.testSuite.length === 0) throw new Error("Test suite must have at least one test");
describe(`source:${ops.source.id}`, () => { describe(`source:${ops.source.id}`, () => {
ops.testSuite.forEach((test, i) => { ops.testSuite.forEach((test, i) => {
async function runTest(providers: ProviderControls) { describe(`test ${i}`, () => {
let hasNotFound = false; async function runTest(providers: ProviderControls) {
let hasError = false; let hasNotFound = false;
let streamCount = 0; let hasError = false;
let embedCount = 0; let streamCount = 0;
try { let embedCount = 0;
const result = await providers.runSourceScraper({ try {
id: ops.source.id, const result = await providers.runSourceScraper({
media: test, id: ops.source.id,
}) media: test,
if (ops.debug) console.log(result); })
streamCount = (result.stream ?? []).length; if (ops.debug) console.log(result);
embedCount = result.embeds.length; streamCount = (result.stream ?? []).length;
} catch (err) { embedCount = result.embeds.length;
if (ops.debug) console.log(err); } catch (err) {
if (err instanceof NotFoundError) if (ops.debug) console.log(err);
hasNotFound = true; if (err instanceof NotFoundError)
else hasNotFound = true;
hasError = true; else
hasError = true;
}
expect(ops.expect.error ?? false).toBe(hasError);
expect(ops.expect.notfound ?? false).toBe(hasNotFound);
expect(ops.expect.streams ?? 0).toBe(streamCount);
expect(ops.expect.embeds ?? 0).toBe(embedCount);
} }
expect(ops.expect.error ?? false).toBe(hasError);
expect(ops.expect.notfound ?? false).toBe(hasNotFound);
expect(ops.expect.streams ?? 0).toBe(streamCount);
expect(ops.expect.embeds ?? 0).toBe(embedCount);
}
if (ops.types.includes('standard')) { if (ops.types.includes('standard')) {
it(`Should pass test ${i} - standard`, async () => { it(`Should pass test ${i} - standard`, async () => {
const providers = makeBaseProviders() const providers = makeBaseProviders()
.addSource(ops.source) .addSource(ops.source)
.build(); .build();
await runTest(providers); await runTest(providers);
}) })
} }
if (ops.types.includes('ip:standard')) { if (ops.types.includes('ip:standard')) {
it(`Should pass test ${i} - standard:ip`, async () => { it(`Should pass test ${i} - standard:ip`, async () => {
const providers = makeBaseProviders() const providers = makeBaseProviders()
.addSource(ops.source) .addSource(ops.source)
.enableConsistentIpForRequests() .enableConsistentIpForRequests()
.build(); .build();
await runTest(providers); await runTest(providers);
}) })
} }
if (ops.types.includes('proxied')) { if (ops.types.includes('proxied')) {
it(`Should pass test ${i} - proxied`, async () => { it(`Should pass test ${i} - proxied`, async () => {
if (!process.env.MOVIE_WEB_PROXY_URL) if (!process.env.MOVIE_WEB_PROXY_URL)
throw new Error("Cant use proxied test without setting MOVIE_WEB_PROXY_URL env"); throw new Error("Cant use proxied test without setting MOVIE_WEB_PROXY_URL env");
const providers = makeBaseProviders() const providers = makeBaseProviders()
.addSource(ops.source) .addSource(ops.source)
.setProxiedFetcher(makeSimpleProxyFetcher(process.env.MOVIE_WEB_PROXY_URL, fetch)) .setProxiedFetcher(makeSimpleProxyFetcher(process.env.MOVIE_WEB_PROXY_URL, fetch))
.build(); .build();
await runTest(providers); await runTest(providers);
}) })
} }
})
}) })
}) })
} }
@@ -117,54 +120,56 @@ export function testEmbed(ops: TestEmbedOptions) {
if (ops.testUrls.length === 0) throw new Error("Test urls must have at least one url"); if (ops.testUrls.length === 0) throw new Error("Test urls must have at least one url");
describe(`embed:${ops.embed.id}`, () => { describe(`embed:${ops.embed.id}`, () => {
ops.testUrls.forEach((test, i) => { ops.testUrls.forEach((test, i) => {
async function runTest(providers: ProviderControls) { describe(`test ${i}`, () => {
let hasError = false; async function runTest(providers: ProviderControls) {
let streamCount = 0; let hasError = false;
try { let streamCount = 0;
const result = await providers.runEmbedScraper({ try {
id: ops.embed.id, const result = await providers.runEmbedScraper({
url: test, id: ops.embed.id,
}) url: test,
if (ops.debug) console.log(result); })
streamCount = (result.stream ?? []).length; if (ops.debug) console.log(result);
} catch (err) { streamCount = (result.stream ?? []).length;
if (ops.debug) console.log(err); } catch (err) {
hasError = true; if (ops.debug) console.log(err);
hasError = true;
}
expect(ops.expect.error ?? false).toBe(hasError);
expect(ops.expect.streams ?? 0).toBe(streamCount);
} }
expect(ops.expect.error ?? false).toBe(hasError);
expect(ops.expect.streams ?? 0).toBe(streamCount);
}
if (ops.types.includes('standard')) { if (ops.types.includes('standard')) {
it(`Should pass test ${i} - standard`, async () => { it(`Should pass test ${i} - standard`, async () => {
const providers = makeBaseEmbedProviders() const providers = makeBaseEmbedProviders()
.addEmbed(ops.embed) .addEmbed(ops.embed)
.build(); .build();
await runTest(providers); await runTest(providers);
}) })
} }
if (ops.types.includes('ip:standard')) { if (ops.types.includes('ip:standard')) {
it(`Should pass test ${i} - standard:ip`, async () => { it(`Should pass test ${i} - standard:ip`, async () => {
const providers = makeBaseEmbedProviders() const providers = makeBaseEmbedProviders()
.addEmbed(ops.embed) .addEmbed(ops.embed)
.enableConsistentIpForRequests() .enableConsistentIpForRequests()
.build(); .build();
await runTest(providers); await runTest(providers);
}) })
} }
if (ops.types.includes('proxied')) { if (ops.types.includes('proxied')) {
it(`Should pass test ${i} - proxied`, async () => { it(`Should pass test ${i} - proxied`, async () => {
if (!process.env.MOVIE_WEB_PROXY_URL) if (!process.env.MOVIE_WEB_PROXY_URL)
throw new Error("Cant use proxied test without setting MOVIE_WEB_PROXY_URL env"); throw new Error("Cant use proxied test without setting MOVIE_WEB_PROXY_URL env");
const providers = makeBaseEmbedProviders() const providers = makeBaseEmbedProviders()
.addEmbed(ops.embed) .addEmbed(ops.embed)
.setProxiedFetcher(makeSimpleProxyFetcher(process.env.MOVIE_WEB_PROXY_URL, fetch)) .setProxiedFetcher(makeSimpleProxyFetcher(process.env.MOVIE_WEB_PROXY_URL, fetch))
.build(); .build();
await runTest(providers); await runTest(providers);
}) })
} }
});
}) })
}) })
} }