From 0e04e180bfaf4e6285a043fe89443b9bd5901637 Mon Sep 17 00:00:00 2001 From: Anxo Rodriguez Date: Tue, 31 Oct 2023 20:13:19 +0000 Subject: [PATCH] feat: ensure appData is always correct --- apps/cowswap-frontend/.env | 14 +++---- .../modules/appData/utils/fullAppData.spec.ts | 41 +++++++++++++++++++ .../src/modules/appData/utils/fullAppData.ts | 8 ++-- libs/common-utils/src/environments.ts | 9 ++++ 4 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 apps/cowswap-frontend/src/modules/appData/utils/fullAppData.spec.ts diff --git a/apps/cowswap-frontend/.env b/apps/cowswap-frontend/.env index 56d8c8754f..b610a89414 100644 --- a/apps/cowswap-frontend/.env +++ b/apps/cowswap-frontend/.env @@ -56,13 +56,13 @@ # To set your own `AppData`, change `REACT_APP_FULL_APP_DATA_` # AppData, build yours at https://explorer.cow.fi/appdata -REACT_APP_FULL_APP_DATA_PRODUCTION='{"version":"0.7.0","appCode":"CoW Swap","environment":"production","metadata":{}}' -REACT_APP_FULL_APP_DATA_ENS='{"version":"0.7.0","appCode":"CoW Swap","environment":"ens","metadata":{}}' -REACT_APP_FULL_APP_DATA_BARN='{"version":"0.7.0","appCode":"CoW Swap","environment":"barn","metadata":{}}' -REACT_APP_FULL_APP_DATA_STAGING='{"version":"0.7.0","appCode":"CoW Swap","environment":"staging","metadata":{}}' -REACT_APP_FULL_APP_DATA_PR='{"version":"0.7.0","appCode":"CoW Swap","environment":"pr","metadata":{}}' -REACT_APP_FULL_APP_DATA_DEVELOPMENT='{"version":"0.7.0","appCode":"CoW Swap","environment":"development","metadata":{}}' -REACT_APP_FULL_APP_DATA_LOCAL='{"version":"0.7.0","appCode":"CoW Swap","environment":"local","metadata":{}}' +REACT_APP_FULL_APP_DATA_PRODUCTION='{"version":"0.10.0","appCode":"CoW Swap","environment":"production","metadata":{}}' +REACT_APP_FULL_APP_DATA_ENS='{"version":"0.10.0","appCode":"CoW Swap","environment":"ens","metadata":{}}' +REACT_APP_FULL_APP_DATA_BARN='{"version":"0.10.0","appCode":"CoW Swap","environment":"barn","metadata":{}}' +REACT_APP_FULL_APP_DATA_STAGING='{"version":"0.10.0","appCode":"CoW Swap","environment":"staging","metadata":{}}' +REACT_APP_FULL_APP_DATA_PR='{"version":"0.10.0","appCode":"CoW Swap","environment":"pr","metadata":{}}' +REACT_APP_FULL_APP_DATA_DEVELOPMENT='{"version":"0.10.0","appCode":"CoW Swap","environment":"development","metadata":{}}' +REACT_APP_FULL_APP_DATA_LOCAL='{"version":"0.10.0","appCode":"CoW Swap","environment":"local","metadata":{}}' diff --git a/apps/cowswap-frontend/src/modules/appData/utils/fullAppData.spec.ts b/apps/cowswap-frontend/src/modules/appData/utils/fullAppData.spec.ts new file mode 100644 index 0000000000..01bbaaf420 --- /dev/null +++ b/apps/cowswap-frontend/src/modules/appData/utils/fullAppData.spec.ts @@ -0,0 +1,41 @@ +import { LATEST_APP_DATA_VERSION } from '@cowprotocol/app-data' +import { MetadataApi } from '@cowprotocol/app-data' +import { DEFAULT_APP_CODE } from '@cowprotocol/common-const' +import { ALL_ENVIRONMENTS, EnvironmentName } from '@cowprotocol/common-utils' + +import { getFullAppDataByEnv } from './fullAppData' + +const getAppData = (env: EnvironmentName | undefined) => JSON.parse(getFullAppDataByEnv(env)) +const metadataApi = new MetadataApi() + +describe('getFullAppDataByEnv', () => { + ALL_ENVIRONMENTS.forEach((env) => { + it(`[${env}] AppData is on its last version and has the right content`, () => { + const appData = getAppData(env) + expect(appData).toEqual({ + version: LATEST_APP_DATA_VERSION, + appCode: DEFAULT_APP_CODE, + environment: env, + metadata: {}, + }) + }) + + it(`[${env}] AppData is valid`, async () => { + const appData = getAppData(env) + const result = await metadataApi.validateAppDataDoc(appData) + expect(result).toEqual({ success: true, errors: undefined }) + }) + }) + + it(`[DEFAULT_FULL_APP_DATA] AppData is on its last version and has the right content`, () => { + const appData = getAppData(undefined) + expect(appData).toEqual({ version: LATEST_APP_DATA_VERSION, appCode: DEFAULT_APP_CODE, metadata: {} }) + }) + + it(`[DEFAULT_FULL_APP_DATA] AppData is valid`, async () => { + const appData = getAppData(undefined) + + const result = await metadataApi.validateAppDataDoc(appData) + expect(result).toEqual({ success: true, errors: undefined }) + }) +}) diff --git a/apps/cowswap-frontend/src/modules/appData/utils/fullAppData.ts b/apps/cowswap-frontend/src/modules/appData/utils/fullAppData.ts index 08cdc6fac3..7ec406fb81 100644 --- a/apps/cowswap-frontend/src/modules/appData/utils/fullAppData.ts +++ b/apps/cowswap-frontend/src/modules/appData/utils/fullAppData.ts @@ -1,12 +1,12 @@ -import { environmentName } from '@cowprotocol/common-utils' +import { EnvironmentName, environmentName } from '@cowprotocol/common-utils' import { AppDataInfo } from '../types' import { toKeccak256 } from '../utils/buildAppData' -const DEFAULT_FULL_APP_DATA = '{"version":"0.7.0","appCode":"CoW Swap","metadata":{}}' +const DEFAULT_FULL_APP_DATA = '{"version":"0.10.0","appCode":"CoW Swap","metadata":{}}' let appData: AppDataInfo = (() => { - const fullAppData = _getFullAppData() + const fullAppData = getFullAppDataByEnv(environmentName) return _fromFullAppData(fullAppData) })() @@ -20,7 +20,7 @@ export function updateFullAppData(fullAppData: string | undefined) { } } -function _getFullAppData(): string { +export function getFullAppDataByEnv(environmentName: EnvironmentName | undefined): string { switch (environmentName) { case 'production': return process.env.REACT_APP_FULL_APP_DATA_PRODUCTION || DEFAULT_FULL_APP_DATA diff --git a/libs/common-utils/src/environments.ts b/libs/common-utils/src/environments.ts index 78e7c69871..c71fa39ae2 100644 --- a/libs/common-utils/src/environments.ts +++ b/libs/common-utils/src/environments.ts @@ -49,6 +49,15 @@ const { isLocal, isDev, isPr, isStaging, isProd, isEns, isBarn } = checkEnvironm window.location.pathname ) +export const ALL_ENVIRONMENTS: EnvironmentName[] = [ + 'local', + 'development', + 'pr', + 'staging', + 'production', + 'barn', + 'ens', +] export type EnvironmentName = 'local' | 'development' | 'pr' | 'staging' | 'production' | 'barn' | 'ens' export const environmentName: EnvironmentName | undefined = (function () {