test: introduce helper function createTestContext

This commit is contained in:
WebStreamr 2025-06-19 11:32:22 +00:00
parent 6f1f04681f
commit 2d4c5a8221
No known key found for this signature in database
27 changed files with 77 additions and 57 deletions

View file

@ -1,13 +1,14 @@
import winston from 'winston';
import { FetcherMock } from '../utils';
import { Context, CountryCode } from '../types';
import { CountryCode } from '../types';
import { ExtractorRegistry } from './ExtractorRegistry';
import { DoodStream } from './DoodStream';
import { createTestContext } from '../test';
const logger = winston.createLogger({ transports: [new winston.transports.Console({ level: 'nope' })] });
const extractorRegistry = new ExtractorRegistry(logger, [new DoodStream(new FetcherMock(`${__dirname}/__fixtures__/DoodStream`))]);
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: {} };
const ctx = createTestContext();
describe('DoodStream', () => {
test('dood.to', async () => {

View file

@ -1,13 +1,14 @@
import winston from 'winston';
import { FetcherMock } from '../utils';
import { Context, CountryCode } from '../types';
import { CountryCode } from '../types';
import { ExtractorRegistry } from './ExtractorRegistry';
import { Dropload } from './Dropload';
import { createTestContext } from '../test';
const logger = winston.createLogger({ transports: [new winston.transports.Console({ level: 'nope' })] });
const extractorRegistry = new ExtractorRegistry(logger, [new Dropload(new FetcherMock(`${__dirname}/__fixtures__/Dropload`))]);
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: {} };
const ctx = createTestContext();
describe('Dropload', () => {
test('dropload.io', async () => {

View file

@ -1,13 +1,14 @@
import winston from 'winston';
import { FetcherMock } from '../utils';
import { Context, CountryCode } from '../types';
import { CountryCode } from '../types';
import { ExtractorRegistry } from './ExtractorRegistry';
import { ExternalUrl } from './ExternalUrl';
import { createTestContext } from '../test';
const logger = winston.createLogger({ transports: [new winston.transports.Console({ level: 'nope' })] });
const extractorRegistry = new ExtractorRegistry(logger, [new ExternalUrl(new FetcherMock(`${__dirname}/__fixtures__/ExternalUrl`))]);
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { includeExternalUrls: 'on' } };
const ctx = createTestContext({ includeExternalUrls: 'on' });
describe('ExternalUrl', () => {
test('404 - not found', async () => {

View file

@ -1,14 +1,15 @@
import winston from 'winston';
import { ExtractorRegistry } from './ExtractorRegistry';
import { Context, CountryCode } from '../types';
import { CountryCode } from '../types';
import { FetcherMock } from '../utils';
import { createExtractors } from './index';
import { createTestContext } from '../test';
const logger = winston.createLogger({ transports: [new winston.transports.Console({ level: 'nope' })] });
const extractorRegistry = new ExtractorRegistry(logger, createExtractors(new FetcherMock(`${__dirname}/__fixtures__/ExtractorRegistry`)));
describe('ExtractorRegistry', () => {
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { de: 'on' } };
const ctx = createTestContext();
test('returns error result from extractor', async () => {
const urlResult = await extractorRegistry.handle(ctx, new URL('https://some-url.test'), CountryCode.de);

View file

@ -1,13 +1,14 @@
import winston from 'winston';
import { FetcherMock } from '../utils';
import { Context, CountryCode } from '../types';
import { CountryCode } from '../types';
import { ExtractorRegistry } from './ExtractorRegistry';
import { Fsst } from './Fsst';
import { createTestContext } from '../test';
const logger = winston.createLogger({ transports: [new winston.transports.Console({ level: 'nope' })] });
const extractorRegistry = new ExtractorRegistry(logger, [new Fsst(new FetcherMock(`${__dirname}/__fixtures__/Fsst`))]);
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: {} };
const ctx = createTestContext();
describe('Fsst', () => {
test('Blood & Sinners', async () => {

View file

@ -1,13 +1,14 @@
import winston from 'winston';
import { FetcherMock } from '../utils';
import { Context, CountryCode } from '../types';
import { CountryCode } from '../types';
import { ExtractorRegistry } from './ExtractorRegistry';
import { KinoGer } from './KinoGer';
import { createTestContext } from '../test';
const logger = winston.createLogger({ transports: [new winston.transports.Console({ level: 'nope' })] });
const extractorRegistry = new ExtractorRegistry(logger, [new KinoGer(new FetcherMock(`${__dirname}/__fixtures__/KinoGer`))]);
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: {} };
const ctx = createTestContext();
describe('KinoGer', () => {
test('Blood & Sinners', async () => {

View file

@ -1,13 +1,14 @@
import winston from 'winston';
import { FetcherMock } from '../utils';
import { Context, CountryCode } from '../types';
import { CountryCode } from '../types';
import { ExtractorRegistry } from './ExtractorRegistry';
import { Soaper } from './Soaper';
import { createTestContext } from '../test';
const logger = winston.createLogger({ transports: [new winston.transports.Console({ level: 'nope' })] });
const extractorRegistry = new ExtractorRegistry(logger, [new Soaper(new FetcherMock(`${__dirname}/__fixtures__/Soaper`))]);
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: {} };
const ctx = createTestContext();
describe('Soaper', () => {
test('Full Metal Jacket', async () => {

View file

@ -1,13 +1,14 @@
import winston from 'winston';
import { FetcherMock } from '../utils';
import { Context, CountryCode } from '../types';
import { CountryCode } from '../types';
import { ExtractorRegistry } from './ExtractorRegistry';
import { SuperVideo } from './SuperVideo';
import { createTestContext } from '../test';
const logger = winston.createLogger({ transports: [new winston.transports.Console({ level: 'nope' })] });
const extractorRegistry = new ExtractorRegistry(logger, [new SuperVideo(new FetcherMock(`${__dirname}/__fixtures__/SuperVideo`))]);
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: {} };
const ctx = createTestContext();
describe('SuperVideo', () => {
test('supervideo.cc /e/', async () => {

View file

@ -1,13 +1,14 @@
import winston from 'winston';
import { FetcherMock } from '../utils';
import { Context, CountryCode } from '../types';
import { CountryCode } from '../types';
import { ExtractorRegistry } from './ExtractorRegistry';
import { VidSrc } from './VidSrc';
import { createTestContext } from '../test';
const logger = winston.createLogger({ transports: [new winston.transports.Console({ level: 'nope' })] });
const extractorRegistry = new ExtractorRegistry(logger, [new VidSrc(new FetcherMock(`${__dirname}/__fixtures__/VidSrc`))]);
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: {} };
const ctx = createTestContext();
describe('VidSrc', () => {
test('Full Metal Jacket', async () => {

View file

@ -1,8 +1,8 @@
import { CineHDPlus } from './CineHDPlus';
import { FetcherMock, ImdbId } from '../utils';
import { Context } from '../types';
import { createTestContext } from '../test';
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { es: 'on', mx: 'on' } };
const ctx = createTestContext({ es: 'on', mx: 'on' });
describe('CineHDPlus', () => {
let handler: CineHDPlus;

View file

@ -1,8 +1,8 @@
import { Cuevana } from './Cuevana';
import { FetcherMock, TmdbId } from '../utils';
import { Context } from '../types';
import { createTestContext } from '../test';
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { es: 'on', mx: 'on' } };
const ctx = createTestContext({ es: 'on', mx: 'on' });
describe('Cuevana', () => {
let handler: Cuevana;

View file

@ -1,8 +1,8 @@
import { Eurostreaming } from './Eurostreaming';
import { FetcherMock, TmdbId } from '../utils';
import { Context } from '../types';
import { createTestContext } from '../test';
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { it: 'on' } };
const ctx = createTestContext({ it: 'on' });
describe('Eurostreaming', () => {
let handler: Eurostreaming;

View file

@ -1,8 +1,8 @@
import { Frembed } from './Frembed';
import { FetcherMock, ImdbId, TmdbId } from '../utils';
import { Context } from '../types';
import { createTestContext } from '../test';
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { fr: 'on' } };
const ctx = createTestContext({ fr: 'on' });
describe('Frembed', () => {
let handler: Frembed;

View file

@ -1,8 +1,8 @@
import { FrenchCloud } from './FrenchCloud';
import { FetcherMock, ImdbId } from '../utils';
import { Context } from '../types';
import { createTestContext } from '../test';
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { fr: 'on' } };
const ctx = createTestContext({ fr: 'on' });
describe('FrenchCloud', () => {
let handler: FrenchCloud;

View file

@ -1,8 +1,8 @@
import { KinoGer } from './KinoGer';
import { FetcherMock, ImdbId } from '../utils';
import { Context } from '../types';
import { createTestContext } from '../test';
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { de: 'on' } };
const ctx = createTestContext({ de: 'on' });
describe('KinoGer', () => {
let handler: KinoGer;

View file

@ -1,8 +1,8 @@
import { MeineCloud } from './MeineCloud';
import { FetcherMock, ImdbId } from '../utils';
import { Context } from '../types';
import { createTestContext } from '../test';
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { de: 'on' } };
const ctx = createTestContext({ de: 'on' });
describe('MeineCloud', () => {
let handler: MeineCloud;

View file

@ -1,8 +1,8 @@
import { MostraGuarda } from './MostraGuarda';
import { FetcherMock, ImdbId } from '../utils';
import { Context } from '../types';
import { createTestContext } from '../test';
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { it: 'on' } };
const ctx = createTestContext({ it: 'on' });
describe('MostraGuarda', () => {
let handler: MostraGuarda;

View file

@ -1,8 +1,8 @@
import { Soaper } from './Soaper';
import { FetcherMock, ImdbId, TmdbId } from '../utils';
import { Context } from '../types';
import { createTestContext } from '../test';
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { en: 'on' } };
const ctx = createTestContext();
describe('Soaper', () => {
let handler: Soaper;

View file

@ -1,8 +1,8 @@
import { StreamKiste } from './StreamKiste';
import { FetcherMock, ImdbId } from '../utils';
import { Context } from '../types';
import { createTestContext } from '../test';
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { de: 'on' } };
const ctx = createTestContext({ de: 'on' });
describe('StreamKiste', () => {
let handler: StreamKiste;

View file

@ -1,8 +1,8 @@
import { VerHdLink } from './VerHdLink';
import { FetcherMock, ImdbId } from '../utils';
import { Context } from '../types';
import { createTestContext } from '../test';
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { es: 'on', mx: 'on' } };
const ctx = createTestContext({ es: 'on', mx: 'on' });
describe('VerHdLink', () => {
let handler: VerHdLink;

View file

@ -1,8 +1,8 @@
import { VidSrc } from './VidSrc';
import { FetcherMock, ImdbId } from '../utils';
import { Context } from '../types';
import { createTestContext } from '../test';
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { en: 'on' } };
const ctx = createTestContext();
describe('VidSrc', () => {
let handler: VidSrc;

10
src/test/index.ts Normal file
View file

@ -0,0 +1,10 @@
import { Config, Context } from '../types';
import { getDefaultConfig } from '../utils';
export const createTestContext = (config?: Config): Context => {
return {
id: 'test',
ip: '0.0.0.0',
config: config ?? getDefaultConfig(),
};
};

View file

@ -1,14 +1,14 @@
import winston from 'winston';
import fetchMock from 'fetch-mock';
import { Fetcher } from './Fetcher';
import { Context } from '../types';
import { BlockedError, HttpError, NotFoundError, QueueIsFullError, TimeoutError, TooManyRequestsError, TooManyTimeoutsError } from '../error';
import { createTestContext } from '../test';
fetchMock.mockGlobal();
const fetcher = new Fetcher(winston.createLogger({ transports: [new winston.transports.Console({ level: 'nope' })] }));
describe('fetch', () => {
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { de: 'on' } };
const ctx = createTestContext();
afterEach(() => {
fetchMock.clearHistory();
@ -27,12 +27,12 @@ describe('fetch', () => {
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en',
'Forwarded': 'for=127.0.0.1',
'Forwarded': 'for=0.0.0.0',
'Priority': 'u=0',
'User-Agent': 'node',
'X-Forwarded-For': '127.0.0.1',
'X-Forwarded-For': '0.0.0.0',
'X-Forwarded-Proto': 'https',
'X-Real-IP': '127.0.0.1',
'X-Real-IP': '0.0.0.0',
},
});
});

View file

@ -3,14 +3,15 @@ import winston from 'winston';
import { createExtractors, Extractor, ExtractorRegistry } from '../extractor';
import { StreamResolver } from './StreamResolver';
import { Source, SourceResult, MeineCloud, MostraGuarda } from '../source';
import { BlockedReason, Context, CountryCode, UrlResult } from '../types';
import { BlockedReason, CountryCode, UrlResult } from '../types';
import { BlockedError, HttpError, NotFoundError, QueueIsFullError, TimeoutError, TooManyTimeoutsError, TooManyRequestsError } from '../error';
import { ImdbId } from './id';
import { FetcherMock } from './FetcherMock';
import { createTestContext } from '../test';
const logger = winston.createLogger({ transports: [new winston.transports.Console({ level: 'nope' })] });
const fetcher = new FetcherMock(`${__dirname}/__fixtures__/StreamResolver`);
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { de: 'on', it: 'on' } };
const ctx = createTestContext({ de: 'on', it: 'on' });
const meineCloud = new MeineCloud(fetcher);
const mostraGuarda = new MostraGuarda(fetcher);

View file

@ -35,7 +35,7 @@ exports[`resolve adds error info 1`] = `
"behaviorHints": {},
"name": "WebStreamr 🇩🇪",
"title": "🔗 hoster.com
❌ Request failed. Request-id: id.",
❌ Request failed. Request-id: test.",
"ytId": "E4WlUXrJgy4",
},
{
@ -56,7 +56,7 @@ exports[`resolve adds error info 1`] = `
"behaviorHints": {},
"name": "WebStreamr 🇩🇪",
"title": "🔗 hoster.com
❌ Request failed with status 500 (Internal Server Error). Request-id: id.",
❌ Request failed with status 500 (Internal Server Error). Request-id: test.",
"ytId": "E4WlUXrJgy4",
},
],
@ -99,7 +99,7 @@ exports[`resolve adds error info 2`] = `
"externalUrl": "https://example2.com/",
"name": "WebStreamr 🇩🇪 ⚠️ external",
"title": "🔗 hoster.com
❌ Request failed. Request-id: id.",
❌ Request failed. Request-id: test.",
},
{
"behaviorHints": {},
@ -120,7 +120,7 @@ exports[`resolve adds error info 2`] = `
"externalUrl": "https://example4.com/",
"name": "WebStreamr 🇩🇪 ⚠️ external",
"title": "🔗 hoster.com
❌ Request failed with status 500 (Internal Server Error). Request-id: id.",
❌ Request failed with status 500 (Internal Server Error). Request-id: test.",
},
],
}
@ -333,7 +333,7 @@ exports[`resolve returns source errors as stream 1`] = `
{
"name": "WebStreamr",
"title": "🔗 MeineCloud
❌ Request failed. Request-id: id.",
❌ Request failed. Request-id: test.",
"ytId": "E4WlUXrJgy4",
},
],

View file

@ -1,10 +1,10 @@
import { getImdbIdFromTmdbId, getTmdbIdFromImdbId } from './tmdb';
import { Context } from '../types';
import { ImdbId, TmdbId } from './id';
import { FetcherMock } from './FetcherMock';
import { createTestContext } from '../test';
const fetcher = new FetcherMock(`${__dirname}/__fixtures__/tmdb`);
const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { de: 'on' } };
const ctx = createTestContext();
describe('getTmdbIdFromImdbId', () => {
test('series', async () => {

View file

@ -25,5 +25,5 @@
"useUnknownInCatchVariables": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.test.ts"]
"exclude": ["node_modules", "src/test", "**/*.test.ts"]
}