import { SSMClient, GetParameterCommand } from '@aws-sdk/client-ssm' import { expect as c_expect } from 'chai' import Configuration from '../../src/Config/Configuration.js' import { mockAwsClient, mockAwsCommand, mockAwsResult } from '../AwsSdkTest.utils.js' import { SampleConfig } from '../Test.utils.js' const SSMMock = mockAwsClient(SSMClient) const config = new Configuration(SampleConfig, 'my-prefix') describe(`Optional remote environment`, () => { // reset mock beforeEach(() => { SSMMock.reset() config['resetCache']() }) test('Does not accept sync get (+ compiler error)', async () => { let err = null try { // @ts-expect-error config.get('TOKEN_SECRET') } catch (e) { err = e } c_expect(err).is.not.null c_expect(err?.['message']).is.not.null }) test('Optional value with null value', async () => { SSMMock.on(mockAwsCommand(GetParameterCommand)).resolves( mockAwsResult({ Parameter: { Value: undefined, }, }) ) const token = await config.asyncGet('TOKEN_SECRET_FALSY') c_expect(token).is.undefined }) test('Optional value with empty response', async () => { SSMMock.on(mockAwsCommand(GetParameterCommand)).resolves(mockAwsResult({})) const token = await config.asyncGet('TOKEN_SECRET_FALSY') c_expect(token).is.undefined }) test('Optional value with valid response', async () => { const value = 'abc' SSMMock.on(mockAwsCommand(GetParameterCommand)).resolves( mockAwsResult({ Parameter: { Value: value, }, }) ) const token = await config.asyncGet('TOKEN_SECRET_FALSY') c_expect(token).is.not.undefined c_expect(token).to.be.equals(value) }) test('Optional value rejection', async () => { SSMMock.on(mockAwsCommand(GetParameterCommand)).rejects(new Error('failed!')) const token = await config.asyncGet('TOKEN_SECRET_FALSY') c_expect(token).is.undefined }) }) describe(`Required remote environment`, () => { // reset mock beforeEach(() => { SSMMock.reset() config['resetCache']() }) test('Does not accept sync get (+ compiler error)', async () => { let err = null try { // @ts-expect-error config.get('TOKEN_SECRET') } catch (e) { err = e } c_expect(err).is.not.null c_expect(err?.['message']).is.not.null }) test('Required value with null value', async () => { SSMMock.on(mockAwsCommand(GetParameterCommand)).resolves( mockAwsResult({ Parameter: { Value: undefined, }, }) ) let err = null try { await config.asyncGet('TOKEN_SECRET') } catch (e) { err = e } c_expect(err).is.not.null c_expect(err?.['message']).is.not.null }) test('Required value with empty response', async () => { SSMMock.on(mockAwsCommand(GetParameterCommand)).resolves(mockAwsResult({})) let err = null try { await config.asyncGet('TOKEN_SECRET') } catch (e) { err = e } c_expect(err).is.not.null c_expect(err?.['message']).is.not.null }) test('Required value with valid response', async () => { const value = 'abc' SSMMock.on(mockAwsCommand(GetParameterCommand)).resolves( mockAwsResult({ Parameter: { Value: value, }, }) ) const token = await config.asyncGet('TOKEN_SECRET') c_expect(token).is.not.undefined c_expect(token).to.be.equals(value) }) test('Required value rejection', async () => { SSMMock.on(mockAwsCommand(GetParameterCommand)).rejects(new Error('failed!')) let err = null try { await config.asyncGet('TOKEN_SECRET') } catch (e) { err = e } c_expect(err).is.not.null c_expect(err?.['message']).is.not.null }) }) describe(`Optional local environment`, () => { beforeEach(() => { config['resetCache']() }) test('Optional value with null value', async () => { const token = config.get('PATH123') c_expect(token).is.undefined }) }) describe(`Required local environment`, () => { beforeEach(() => { config['resetCache']() }) test('Test compiler error', async () => { let err = null try { // @ts-expect-error await config.asyncGet('PATH') } catch (e) { err = e } c_expect(err).is.not.null c_expect(err?.['message']).is.not.null }) test('Required value with null value', async () => { let err = null try { config.get('PATH_FALSY') } catch (e) { err = e } c_expect(err).is.not.null c_expect(err?.['message']).is.not.null }) test('Required value with valid response', async () => { const key = 'PATH' const v = config.get(key) c_expect(v).is.not.null c_expect(v).to.be.equals(process.env[key]) }) }) describe(`Caching test`, () => { test('Does cache previous fetched value', async () => { const value = '123' const key = 'TOKEN_SECRET' // initial fetch SSMMock.on(mockAwsCommand(GetParameterCommand)).resolves( mockAwsResult({ Parameter: { Value: value, }, }) ) const v = await config.asyncGet(key) c_expect(v).is.not.null c_expect(v).to.be.equals(value) // subsequent fetch SSMMock.reset() SSMMock.on(mockAwsCommand(GetParameterCommand)).rejects(new Error('failed!')) const v2 = await config.asyncGet(key) c_expect(v2).is.not.null c_expect(v2).to.be.equals(value) }) }) export {}