From 35c2606fa2467016778c0ebecf26bafd5e4910d0 Mon Sep 17 00:00:00 2001 From: Michael Landis Date: Fri, 20 Dec 2024 10:14:47 -0800 Subject: [PATCH] test: add unit test and integration test setup Adds integration test setup and a unit test over the client. --- .github/workflows/build.yml | 5 +++ package.json | 2 - src/index.ts | 1 + test/client.test.ts | 17 ++++++++ test/integration-setup.ts | 82 +++++++++++++++++++++++++++++++++++++ test/test.ts | 1 - 6 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 test/client.test.ts create mode 100644 test/integration-setup.ts delete mode 100644 test/test.ts diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ee99cb9..1764ee5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -24,6 +24,9 @@ jobs: node: [16, 18, 20] name: Test on Node ${{ matrix.node }} runs-on: ubuntu-latest + env: + MOMENTO_API_KEY_REGION_1: ${{ secrets.ALPHA_TEST_AUTH_TOKEN }} + MOMENTO_API_KEY_REGION_2: ${{ secrets.ALPHA_TEST_AUTH_TOKEN }} steps: - name: Setup repo @@ -46,3 +49,5 @@ jobs: - name: Lint run: npm run lint + - name: Test + run: npm run test diff --git a/package.json b/package.json index 8ea36d1..28a0d58 100644 --- a/package.json +++ b/package.json @@ -15,8 +15,6 @@ "scripts": { "prebuild": "eslint . --ext .ts", "test": "jest --maxWorkers 1", - "integration-test": "jest integration --maxWorkers 1", - "unit-test": "jest unit", "lint": "eslint . --ext .ts", "format": "eslint . --ext .ts --fix", "watch": "tsc -w", diff --git a/src/index.ts b/src/index.ts index 42f6e5f..14ca6e4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ export { DefaultMomentoLoggerFactory, DefaultMomentoLogger, DefaultMomentoLoggerLevel, + InvalidArgumentError, MomentoLogger, MomentoLoggerFactory, NoopMomentoLogger, diff --git a/test/client.test.ts b/test/client.test.ts new file mode 100644 index 0000000..f420886 --- /dev/null +++ b/test/client.test.ts @@ -0,0 +1,17 @@ +import { + Configurations, + InvalidArgumentError, + MultiRegionCacheWriterClient, +} from '../src'; + +describe('client-setup', () => { + it('should throw an exception when no regions are provided', () => { + expect(() => { + new MultiRegionCacheWriterClient({ + credentialProviders: {}, + configuration: Configurations.Laptop.latest(), + defaultTtlSeconds: 60, + }); + }).toThrow(InvalidArgumentError); + }); +}); diff --git a/test/integration-setup.ts b/test/integration-setup.ts new file mode 100644 index 0000000..2fd267e --- /dev/null +++ b/test/integration-setup.ts @@ -0,0 +1,82 @@ +import {v4} from 'uuid'; +import {CacheClientProps} from '@gomomento/sdk/dist/src/cache-client-props'; +import { + CacheClient, + CacheConfiguration, + Configurations, + CreateCache, + CredentialProvider, + DeleteCache, + MomentoErrorCode, +} from '@gomomento/sdk'; +import { + IMultiRegionCacheWriterClient, + MultiRegionCacheWriterClient, +} from '../src'; + +export function testCacheName(): string { + const name = process.env.CACHE_NAME || 'js-integration-test-default'; + return name + v4(); +} + +function regionalMomentoClientForTesting(regionEnvVarName: string) { + const configuration: CacheConfiguration = Configurations.Laptop.latest(); + const IntegrationTestCacheClientProps: CacheClientProps = { + configuration, + credentialProvider: CredentialProvider.fromEnvironmentVariable({ + environmentVariableName: regionEnvVarName, + }), + defaultTtlSeconds: 60, + }; + return new CacheClient(IntegrationTestCacheClientProps); +} + +export function SetupIntegrationTest(): { + client: IMultiRegionCacheWriterClient; + cacheName: string; +} { + const cacheName = testCacheName(); + const regionEnvVarNames = [ + 'MOMENTO_API_KEY_REGION_1', + 'MOMENTO_API_KEY_REGION_2', + ]; + + beforeAll(async () => { + for (const regionEnvVarName of regionEnvVarNames) { + const momento = regionalMomentoClientForTesting(regionEnvVarName); + const createResponse = await momento.createCache(cacheName); + if (createResponse instanceof CreateCache.Error) { + if ( + createResponse.errorCode() !== + MomentoErrorCode.CACHE_ALREADY_EXISTS_ERROR + ) { + throw createResponse.innerException(); + } + } + } + }); + + afterAll(async () => { + for (const regionEnvVarName of regionEnvVarNames) { + const momento = regionalMomentoClientForTesting(regionEnvVarName); + const deleteResponse = await momento.deleteCache(cacheName); + if (deleteResponse instanceof DeleteCache.Error) { + throw deleteResponse.innerException(); + } + } + }); + + const client = new MultiRegionCacheWriterClient({ + credentialProviders: { + 'region-1': CredentialProvider.fromEnvVar('MOMENTO_API_KEY_REGION_1'), + 'region-2': CredentialProvider.fromEnvVar('MOMENTO_API_KEY_REGION_2'), + }, + configuration: Configurations.Laptop.latest(), + defaultTtlSeconds: 60, + }); + + return { + client, + cacheName, + }; +} diff --git a/test/test.ts b/test/test.ts deleted file mode 100644 index 70b786d..0000000 --- a/test/test.ts +++ /dev/null @@ -1 +0,0 @@ -// TODO