From 325c8d420bd730ca3ade7c716792ecdb136170f0 Mon Sep 17 00:00:00 2001 From: alinarublea Date: Tue, 24 Sep 2024 15:34:27 +0200 Subject: [PATCH] fix: config and content client fixes --- package-lock.json | 4 ++-- .../src/clients/content-client.js | 8 +++---- .../test/clients/content-client.test.js | 8 +++---- .../src/models/site/config.js | 13 +++++++++++ .../test/unit/models/site/config.test.js | 23 +++++++++++++++++++ 5 files changed, 46 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5f2097a6..035e2ab5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18599,7 +18599,7 @@ }, "packages/spacecat-shared-content-client": { "name": "@adobe/spacecat-shared-content-client", - "version": "1.1.2", + "version": "1.1.3", "license": "Apache-2.0", "dependencies": { "@adobe/helix-universal": "5.0.6", @@ -19043,7 +19043,7 @@ }, "packages/spacecat-shared-data-access": { "name": "@adobe/spacecat-shared-data-access", - "version": "1.44.4", + "version": "1.44.5", "license": "Apache-2.0", "dependencies": { "@adobe/spacecat-shared-dynamo": "1.2.5", diff --git a/packages/spacecat-shared-content-client/src/clients/content-client.js b/packages/spacecat-shared-content-client/src/clients/content-client.js index afbcc7b8..8f4852af 100644 --- a/packages/spacecat-shared-content-client/src/clients/content-client.js +++ b/packages/spacecat-shared-content-client/src/clients/content-client.js @@ -58,7 +58,7 @@ const validateSite = (site) => { throw new Error('Site is required'); } - const contentSource = site.getConfig()?.content?.source; + const contentSource = site.getConfig()?.getHlxContentConfig()?.source; if (!isObject(contentSource)) { throw new Error('Site must have a valid content source'); } @@ -181,7 +181,7 @@ export default class ContentClient { const { log = console, env } = context; const config = {}; - const contentSourceType = site.getConfig().content?.source?.type; + const contentSourceType = site.getConfig()?.getHlxContentConfig()?.source?.type; const envMapping = SUPPORTED_CONTENT_SOURCES.get(contentSourceType); if (envMapping) { @@ -195,11 +195,11 @@ export default class ContentClient { constructor(config, site, log) { validateSite(site); - validateConfiguration(config, site.getConfig().content.source.type); + validateConfiguration(config, site.getConfig()?.getHlxContentConfig()?.source?.type); this.log = log; this.config = config; - this.contentSource = site.getConfig().content.source; + this.contentSource = site.getConfig()?.getHlxContentConfig()?.source; this.site = site; this.rawClient = null; } diff --git a/packages/spacecat-shared-content-client/test/clients/content-client.test.js b/packages/spacecat-shared-content-client/test/clients/content-client.test.js index c3f0b19f..4e88242e 100644 --- a/packages/spacecat-shared-content-client/test/clients/content-client.test.js +++ b/packages/spacecat-shared-content-client/test/clients/content-client.test.js @@ -31,12 +31,12 @@ describe('ContentClient', () => { const siteConfigGoogleDrive = { getId: () => 'test-site', - getConfig: () => ({ content: { source: { type: 'drive.google' } } }), + getConfig: () => ({ getHlxContentConfig: () => ({ source: { type: 'drive.google' } }) }), }; const siteConfigOneDrive = { getId: () => 'test-site', - getConfig: () => ({ content: { source: { type: 'onedrive' } } }), + getConfig: () => ({ getHlxContentConfig: () => ({ source: { type: 'onedrive' } }) }), }; const sampleMetadata = new Map( @@ -166,12 +166,12 @@ describe('ContentClient', () => { }); it('throws an error if site has no content source', () => { - const invalidSite = { getConfig: () => ({ }) }; + const invalidSite = { getConfig: () => ({ getHlxContentConfig: () => ({ }) }) }; expect(() => new ContentClient(env, invalidSite, log)).to.throw('Site must have a valid content source'); }); it('throws an error if site\'s content source type is unsupported', () => { - const invalidSite = { getConfig: () => ({ content: { source: {} } }) }; + const invalidSite = { getConfig: () => ({ getHlxContentConfig: () => ({ source: {} }) }) }; expect(() => new ContentClient(env, invalidSite, log)).to.throw('Unsupported content source type: undefined'); }); diff --git a/packages/spacecat-shared-data-access/src/models/site/config.js b/packages/spacecat-shared-data-access/src/models/site/config.js index 4d6869b2..4d620331 100644 --- a/packages/spacecat-shared-data-access/src/models/site/config.js +++ b/packages/spacecat-shared-data-access/src/models/site/config.js @@ -60,6 +60,7 @@ export const Config = (data = {}) => { self.getSlackMentions = (type) => state?.handlers[type]?.mentions?.slack; self.getHandlerConfig = (type) => state?.handlers[type]; self.getHandlers = () => state.handlers; + self.getHlxContentConfig = () => state.content; self.getImports = () => state.imports; self.getExcludedURLs = (type) => state?.handlers[type]?.excludedURLs; self.getManualOverwrites = (type) => state?.handlers[type]?.manualOverwrites; @@ -74,6 +75,17 @@ export const Config = (data = {}) => { }; }; + self.updateHlxContentConfig = (source, path) => { + state.content = { + source, + path, + }; + }; + + self.updateImports = (imports) => { + state.imports = imports; + }; + self.updateSlackMentions = (type, mentions) => { state.handlers = state.handlers || {}; state.handlers[type] = state.handlers[type] || {}; @@ -106,6 +118,7 @@ Config.fromDynamoItem = (dynamoItem) => Config(dynamoItem); Config.toDynamoItem = (config) => ({ slack: config.getSlackConfig(), + content: config.getHlxContentConfig(), handlers: config.getHandlers(), imports: config.getImports(), }); diff --git a/packages/spacecat-shared-data-access/test/unit/models/site/config.test.js b/packages/spacecat-shared-data-access/test/unit/models/site/config.test.js index fc8d7036..cda8e11f 100644 --- a/packages/spacecat-shared-data-access/test/unit/models/site/config.test.js +++ b/packages/spacecat-shared-data-access/test/unit/models/site/config.test.js @@ -120,6 +120,29 @@ describe('Config Tests', () => { config.updateFixedURLs('broken-backlinks', []); expect(config.getFixedURLs('broken-backlinks')).to.be.an('array').that.is.empty; }); + + it('correctly updates the imports array', () => { + const config = Config(); + const imports = [ + { type: 'import1' }, + { type: 'import2' }, + ]; + config.updateImports(imports); + + const updatedImports = config.getImports(); + expect(updatedImports).to.deep.equal(imports); + }); + + it('correctly updates the hlx content configuration', () => { + const config = Config(); + const source = 'source1'; + const path = 'path1'; + config.updateHlxContentConfig(source, path); + + const hlxContentConfig = config.getHlxContentConfig(); + expect(hlxContentConfig.source).to.equal(source); + expect(hlxContentConfig.path).to.equal(path); + }); }); describe('fromDynamoItem Static Method', () => {