From e55a1a432dc2a9a049b8b8cfd667b25775a35f00 Mon Sep 17 00:00:00 2001 From: ebadiere Date: Thu, 24 Oct 2024 16:38:53 -0600 Subject: [PATCH] fix: Cleaned up file and env var evaluation. Signed-off-by: ebadiere --- .../config/hbarSpendingPlanConfigService.ts | 21 ++++++++----------- .../hbarSpendingPlanConfigService.spec.ts | 8 ++----- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/packages/relay/src/lib/config/hbarSpendingPlanConfigService.ts b/packages/relay/src/lib/config/hbarSpendingPlanConfigService.ts index 9e96ea2a2..76ddf82e4 100644 --- a/packages/relay/src/lib/config/hbarSpendingPlanConfigService.ts +++ b/packages/relay/src/lib/config/hbarSpendingPlanConfigService.ts @@ -110,21 +110,18 @@ export class HbarSpendingPlanConfigService { */ private loadSpendingPlansConfig(): SpendingPlanConfig[] { const configPath = findConfig(this.SPENDING_PLANS_CONFIG); - if (!configPath || !fs.existsSync(configPath)) { - try { - if (this.SPENDING_PLANS_CONFIG) { - return JSON.parse(this.SPENDING_PLANS_CONFIG) as SpendingPlanConfig[]; - } - throw new Error('SPENDING_PLANS_CONFIG is undefined'); - } catch (error: any) { - throw new Error(`Failed to parse JSON from HBAR_SPENDING_PLANS_CONFIG: ${error.message}`); - } + if (!this.SPENDING_PLANS_CONFIG) { + throw new Error('SPENDING_PLANS_CONFIG is undefined'); } try { - const rawData = fs.readFileSync(configPath, 'utf-8'); - return JSON.parse(rawData) as SpendingPlanConfig[]; + // Try to parse the value as a file path + if (configPath && fs.existsSync(configPath)) { + return JSON.parse(fs.readFileSync(configPath, 'utf-8')) as SpendingPlanConfig[]; + } + // If it's not a valid file path, try to parse it directly as JSON + return JSON.parse(this.SPENDING_PLANS_CONFIG) as SpendingPlanConfig[]; } catch (error: any) { - throw new Error(`Failed to parse JSON from ${configPath}: ${error.message}`); + throw new Error(`Failed to parse SPENDING_PLANS_CONFIG: ${error.message}`); } } diff --git a/packages/relay/tests/lib/config/hbarSpendingPlanConfigService.spec.ts b/packages/relay/tests/lib/config/hbarSpendingPlanConfigService.spec.ts index 82a8b03e2..3e7b01420 100644 --- a/packages/relay/tests/lib/config/hbarSpendingPlanConfigService.spec.ts +++ b/packages/relay/tests/lib/config/hbarSpendingPlanConfigService.spec.ts @@ -110,9 +110,6 @@ describe('HbarSpendingPlanConfigService', function () { }); describe('populatePreconfiguredSpendingPlans', function () { - // overrideEnvsInMochaDescribe({ HBAR_SPENDING_PLANS_CONFIG: spendingPlansConfig }); - // overrideEnvsInMochaDescribe({ HBAR_SPENDING_PLANS_CONFIG: spendingPlansConfigFile }); - describe('negative scenarios', function () { it('should not throw an error if the configuration file is not found', async function () { await expect(hbarSpendingPlanConfigService.populatePreconfiguredSpendingPlans()).not.to.be.rejected; @@ -121,7 +118,7 @@ describe('HbarSpendingPlanConfigService', function () { it('should throw an error if configuration file is not a parsable JSON', async function () { sinon.stub(fs, 'readFileSync').returns('invalid JSON'); await expect(hbarSpendingPlanConfigService.populatePreconfiguredSpendingPlans()).to.be.rejectedWith( - `Failed to parse JSON from ${path}: Unexpected token 'i', "invalid JSON" is not valid JSON`, + `Failed to parse SPENDING_PLANS_CONFIG: Unexpected token 'i', "invalid JSON" is not valid JSON`, ); }); @@ -568,7 +565,6 @@ describe('HbarSpendingPlanConfigService', function () { overrideEnvsInMochaDescribe({ HBAR_SPENDING_PLANS_CONFIG: 'invalid JSON' }); it('should throw an error if environment variable contains invalid JSON', async function () { - console.log('process.env.HBAR_SPENDING_PLANS_CONFIG', process.env.HBAR_SPENDING_PLANS_CONFIG); hbarSpendingPlanConfigService = await reloadHBarSpendingPlanConfigService( hbarSpendingPlanConfigService, logger, @@ -577,7 +573,7 @@ describe('HbarSpendingPlanConfigService', function () { ipAddressHbarSpendingPlanRepository, ); await expect(hbarSpendingPlanConfigService.populatePreconfiguredSpendingPlans()).to.be.rejectedWith( - /Failed to parse JSON from HBAR_SPENDING_PLANS_CONFIG: /, + /Failed to parse SPENDING_PLANS_CONFIG: /, ); }); });