diff --git a/src/backend/index.ts b/src/backend/index.ts index d97aeead..fe72ce99 100644 --- a/src/backend/index.ts +++ b/src/backend/index.ts @@ -24,6 +24,7 @@ dayjs.extend(relativeTime); dayjs.extend(duration); dayjs.extend(timezone); +// eslint-disable-next-line prefer-arrow-functions/prefer-arrow-functions (async function () { const scheduler = new ToadScheduler() @@ -118,6 +119,12 @@ const configDir = process.env.CONFIG_DIR || path.resolve(projectDir, `./config`) const scrobbleSources = root.get('sources'); await scrobbleSources.buildSourcesFromConfig([]); + for(const source of scrobbleSources.sources) { + if(!source.isReady()) { + await source.initialize(); + } + } + scrobbleSources.logger.info('Finished initializing sources'); // check ambiguous client/source types like this for now const lastfmSources = scrobbleSources.getByType('lastfm'); diff --git a/src/backend/sources/ScrobbleSources.ts b/src/backend/sources/ScrobbleSources.ts index 555e1059..218b10b7 100644 --- a/src/backend/sources/ScrobbleSources.ts +++ b/src/backend/sources/ScrobbleSources.ts @@ -646,12 +646,5 @@ export default class ScrobbleSources { return; } this.sources.push(newSource); - if(!newSource.isReady()) { - if ((await newSource.initialize()) === false) { - this.logger.error(`${type} (${name}) source failed to initialize. Source needs to be successfully initialized before activity capture can begin.`); - } - } else { - newSource.logger.info('Fully Initialized!'); - } } } diff --git a/src/backend/tests/component/component.test.ts b/src/backend/tests/component/component.test.ts index 67fb8582..6e4437ab 100644 --- a/src/backend/tests/component/component.test.ts +++ b/src/backend/tests/component/component.test.ts @@ -23,7 +23,6 @@ describe('Play Transforms', function () { beforeEach(function() { component.config = {}; - // @ts-expect-error should be built on every test component.transformRules = undefined; }); diff --git a/src/backend/tests/config/config.test.ts b/src/backend/tests/config/config.test.ts new file mode 100644 index 00000000..44f4ef80 --- /dev/null +++ b/src/backend/tests/config/config.test.ts @@ -0,0 +1,94 @@ +import { after, before, describe, it } from 'mocha'; +import chai, { assert, expect } from 'chai'; +import asPromised from 'chai-as-promised'; +import withLocalTmpDir from 'with-local-tmp-dir'; +import {constants, copyFile, access} from 'node:fs/promises'; +import path from "path"; +import {projectDir} from '../../common/index.js'; +import ScrobbleClients from '../../scrobblers/ScrobbleClients.js'; +import ScrobbleSources from '../../sources/ScrobbleSources.js'; +import EventEmitter from "events"; +import {loggerTest, loggerDebug} from '@foxxmd/logging'; +import { clientTypes, SourceType, sourceTypes } from '../../common/infrastructure/Atomic.js'; +import { Notifiers } from '../../notifier/Notifiers.js'; + +chai.use(asPromised); + +const samplePath = (name: string) => path.resolve(projectDir, 'config', `${name}.json.example`); + +describe('Sample Configs', function () { + + describe('Exist', function() { + describe('Source Configs', function () { + for(const componentType of sourceTypes) { + it(`Sample ${componentType}.json exists`, async function () { + await access(samplePath(componentType), constants.F_OK); + }); + } + + }); + describe('Client Configs', function () { + for(const componentType of clientTypes) { + it(`Sample ${componentType}.json exists`, async function () { + await access(samplePath(componentType), constants.F_OK); + }); + } + }); + }); + + describe('Parse and Validate Correctly', function () { + + describe('Source Configs', function () { + let reset: any; + + beforeEach(async function() { + reset = await withLocalTmpDir({unsafeCleanup: true}); + }); + + afterEach(async function() { + await reset(); + }); + + for(const componentType of sourceTypes) { + + //trueName = componentType; + it(`Sample ${componentType}.json parses and validates`, async function () { + + let emitter = new EventEmitter(); + await copyFile(samplePath(componentType), `${componentType}.json`); + const sources = new ScrobbleSources(emitter, { + localUrl: new URL('http://example.com'), + configDir: process.cwd(), + version: 'test' + }, loggerDebug); + + await sources.buildSourcesFromConfig(); + expect(sources.sources).length(1); + }); + } + }); + + describe('Client Configs', function () { + let reset: any; + + beforeEach(async function() { + reset = await withLocalTmpDir({unsafeCleanup: true}); + }); + + afterEach(async function() { + await reset(); + }); + + for(const componentType of clientTypes) { + it(`Sample ${componentType}.json parses and validates`, async function () { + + let emitter = new EventEmitter(); + await copyFile(samplePath(componentType), `${componentType}.json`); + const clients = new ScrobbleClients(emitter, new EventEmitter, new URL('http://example.com'), process.cwd(), loggerTest); + await clients.buildClientsFromConfig(new Notifiers(new EventEmitter, new EventEmitter, new EventEmitter, loggerTest)); + expect(clients.clients).length(1); + }); + } + }); + }); +});