Add basic provider unit testing

This commit is contained in:
mrjvs 2024-01-17 18:03:10 +01:00
parent 9798659af8
commit 528e2774b5
4 changed files with 141 additions and 7 deletions

View File

@ -1,7 +0,0 @@
import { describe, expect, it } from "vitest";
describe('abc', () => {
it('should do thing', () => {
expect(true).toBe(false);
})
})

View File

@ -0,0 +1,88 @@
import { ScrapeMedia } from "@/entrypoint/utils/media";
import { Sourcerer } from "@/providers/base";
import { buildProviders } from "@/entrypoint/builder";
import { describe, expect, it } from "vitest";
import { makeStandardFetcher } from "@/fetchers/standardFetch";
import { ProviderControls } from "@/entrypoint/controls";
import { NotFoundError } from "@/utils/errors";
import { targets } from "@/entrypoint/utils/targets";
import { getAllEmbedMetaSorted } from "@/entrypoint/utils/meta";
import { getBuiltinEmbeds } from "@/entrypoint/providers";
export type TestTypes = 'standard' | 'ip:standard';
export interface TestSourceOptions {
source: Sourcerer;
testSuite: ScrapeMedia[];
types: TestTypes[];
debug?: boolean;
expect: {
embeds?: number;
streams?: number;
error?: boolean;
notfound?: boolean;
}
}
// TODO add proxy support
function makeBaseProviders() {
const builder = buildProviders()
.setTarget(targets.ANY)
.setFetcher(makeStandardFetcher(fetch));
const embeds = getBuiltinEmbeds();
embeds.forEach(embed => builder.addEmbed(embed));
return builder;
}
export function testSource(ops: TestSourceOptions) {
if (ops.testSuite.length === 0) throw new Error("Test suite must have at least one test");
describe(`source:${ops.source.id}`, () => {
ops.testSuite.forEach((test, i) => {
async function runTest(providers: ProviderControls) {
let hasNotFound = false;
let hasError = false;
let streamCount = 0;
let embedCount = 0;
try {
const result = await providers.runSourceScraper({
id: ops.source.id,
media: test,
})
if (ops.debug) console.log(result);
streamCount = (result.stream ?? []).length;
embedCount = result.embeds.length;
} catch (err) {
if (ops.debug) console.log(err);
if (err instanceof NotFoundError)
hasNotFound = 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);
}
if (ops.types.includes('standard')) {
it(`Should pass test ${i} - standard`, async () => {
const providers = makeBaseProviders()
.addSource(ops.source)
.build();
await runTest(providers);
})
}
if (ops.types.includes('ip:standard')) {
it(`Should pass test ${i} - standard:ip`, async () => {
const providers = makeBaseProviders()
.addSource(ops.source)
.enableConsistentIpForRequests()
.build();
await runTest(providers);
})
}
})
})
}

View File

@ -0,0 +1,23 @@
import { describe, expect, it } from "vitest";
import { testSource } from "./providerUtils";
import { lookmovieScraper } from "@/providers/sources/lookmovie";
import { testMedia } from "./testMedia";
import { showboxScraper } from "@/providers/sources/showbox";
testSource({
source: lookmovieScraper,
testSuite: [testMedia.arcane, testMedia.hamilton],
types: ['ip:standard'],
expect: {
streams: 1,
}
})
testSource({
source: showboxScraper,
testSuite: [testMedia.arcane, testMedia.hamilton],
types: ['standard'],
expect: {
embeds: 1,
}
})

View File

@ -0,0 +1,30 @@
import { ScrapeMedia } from "@/entrypoint/utils/media";
function makeMedia(media: ScrapeMedia): ScrapeMedia {
return media;
}
export const testMedia = {
arcane: makeMedia({
type: "show",
title: "Arcane",
tmdbId: "94605",
releaseYear: 2021,
episode: {
number: 1,
tmdbId: '1953812',
},
season: {
number: 1,
tmdbId: '134187',
},
imdbId: 'tt11126994'
}),
hamilton: makeMedia({
type: 'movie',
tmdbId: '556574',
imdbId: 'tt8503618',
releaseYear: 2020,
title: 'Hamilton'
})
}