From 29b4e149e9506106acbbc7e45ff8c53427031a5b Mon Sep 17 00:00:00 2001 From: tabarra <1808295+tabarra@users.noreply.github.com> Date: Thu, 23 May 2024 02:27:35 -0300 Subject: [PATCH] wip(perf): added stubs to vitest environments --- .../PerformanceCollector/perfParser.test.ts | 1 + core/extras/helpers.test.ts | 1 + core/extras/testEnv.ts | 60 +++++++++++++++++++ core/vitest.config.ts | 14 +++++ 4 files changed, 76 insertions(+) create mode 100644 core/extras/testEnv.ts create mode 100644 core/vitest.config.ts diff --git a/core/components/PerformanceCollector/perfParser.test.ts b/core/components/PerformanceCollector/perfParser.test.ts index 567c23c79..ec21d8f0a 100644 --- a/core/components/PerformanceCollector/perfParser.test.ts +++ b/core/components/PerformanceCollector/perfParser.test.ts @@ -1,3 +1,4 @@ +import '@extras/testEnv'; import { test, expect, it, suite } from 'vitest'; import { arePerfBoundariesValid, parseRawPerf } from './perfParser'; diff --git a/core/extras/helpers.test.ts b/core/extras/helpers.test.ts index 84ca50be4..9a76f5780 100644 --- a/core/extras/helpers.test.ts +++ b/core/extras/helpers.test.ts @@ -1,3 +1,4 @@ +import '@extras/testEnv'; import { test, expect } from 'vitest'; import * as helpers from './helpers'; diff --git a/core/extras/testEnv.ts b/core/extras/testEnv.ts new file mode 100644 index 000000000..f6dcb1674 --- /dev/null +++ b/core/extras/testEnv.ts @@ -0,0 +1,60 @@ +import path from "path"; +import { vi } from "vitest"; + +// Stubbing env vars +vi.stubEnv('TERM', 'xterm-256color'); +vi.stubEnv('FORCE_COLOR', '3'); +vi.stubEnv('TXADMIN_DEV_SRC_PATH', path.join(process.cwd(), '..')); +vi.stubEnv('TXADMIN_DEV_VITE_URL', 'http://localhost:40122'); + + +// Stubbing globals +vi.stubGlobal('ExecuteCommand', (commandString: string) => { + //noop +}); +vi.stubGlobal('GetConvar', (varName: string, defaultValue: string) => { + if (varName === 'version') { + return 'v1.0.0.9998'; + } else if (varName === 'citizen_root') { + return path.join(process.env.TXADMIN_DEV_FXSERVER_PATH!,); + } else if (varName === 'txAdminDevMode') { + return 'false'; + } else if (varName === 'txAdminVerbose') { + return 'false'; + } else { + return defaultValue; + } + +}); +vi.stubGlobal('GetCurrentResourceName', () => { + return 'monitor'; +}); +vi.stubGlobal('GetPasswordHash', (password: string) => { + return 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; +}); +vi.stubGlobal('GetResourceMetadata', (resourceName: string, metadataKey: string, index: number) => { + if (resourceName === 'monitor' && metadataKey === 'version' && index === 0) { + return 'v9.9.9'; + } else { + throw new Error(`not implemented`); + } +}); +vi.stubGlobal('GetResourcePath', (resourceName: string) => { + if (resourceName === 'monitor') { + return path.join(__dirname, '..', '..'); + } else { + throw new Error(`not implemented`); + } +}); +vi.stubGlobal('IsDuplicityVersion', () => { + return true; +}); +vi.stubGlobal('VerifyPasswordHash', (password: string, hash: string) => { + return true; +}); +vi.stubGlobal('ScanResourceRoot', (rootPath: string, callback: (data: object) => void) => { + throw new Error(`not implemented`); +}); +vi.stubGlobal('Intl.getCanonicalLocales', (locales?: string | readonly string[] | undefined) => { + return Array.isArray(locales) ? locales : [locales]; +}); diff --git a/core/vitest.config.ts b/core/vitest.config.ts new file mode 100644 index 000000000..de03b6828 --- /dev/null +++ b/core/vitest.config.ts @@ -0,0 +1,14 @@ +import path from 'path'; +import { defineConfig } from 'vitest/config'; + +export default defineConfig({ + test: { + alias: { + //must match the tsconfig paths + "@shared": path.resolve(__dirname, "../shared"), + "@extras": path.resolve(__dirname, "./extras"), + "@core": path.resolve(__dirname, "./"), + }, + + }, +});