From f731bba80d51261dd96dec91db61e43f43e76374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Guedes?= Date: Wed, 30 Apr 2025 15:22:37 -0300 Subject: [PATCH 1/4] adding implementation --- CHANGELOG.md | 9 +++++++-- src/modules/featureFlag/featureFlagDecider.ts | 8 ++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf5f7cb37..9dba11df7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- The URL is now shown as a log when opening browser windows. + ## [4.1.0] - 2024-08-01 ### Changed + - Pack toolbelt using nodejs 20 - Allow specific builder config to override the common config for pinned dependencies @@ -2134,13 +2139,13 @@ I know you're excited, yeah, gimme a hug homie <3 **TL;DR:** You don't need to type the sandbox name on `watch` or set the cookies anymore. -# +# **First big important note:** If you have any credentials cached, please `logout` and `login` again. **Second big important note:** Delete the previous `vtex_workspace` and `vtex_sandbox` cookies that you have setted before. -# +# - [`#58`](https://github.com/vtex/toolbelt/issues/58) - [`#48`](https://github.com/vtex/toolbelt/issues/48) (closed due to deprecation) diff --git a/src/modules/featureFlag/featureFlagDecider.ts b/src/modules/featureFlag/featureFlagDecider.ts index 865c0e475..0fcdbc20b 100644 --- a/src/modules/featureFlag/featureFlagDecider.ts +++ b/src/modules/featureFlag/featureFlagDecider.ts @@ -3,12 +3,20 @@ import open from 'open' import { ErrorReport } from '../../api/error/ErrorReport' import { ErrorKinds } from '../../api/error/ErrorKinds' import opn from 'opn' +import logger from '../../api/logger' +import chalk from 'chalk' export async function switchOpen(url: string, options) { try { const configClient = ToolbeltConfig.createClient() + console.log(configClient.getGlobalConfig) const { featureFlags } = await configClient.getGlobalConfig() + console.log(featureFlags) + + logger.info('Your browser should open automatically. You can also use the url below.') + logger.info(`${chalk.cyan(url)}`) + if (featureFlags.FEATURE_FLAG_NEW_OPEN_PACKAGE) { return open(url, options) } From c1b4e274eb0de5a38bd5bd4e1a22ebf14b68343b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Guedes?= Date: Wed, 30 Apr 2025 15:29:43 -0300 Subject: [PATCH 2/4] removing console.logs --- src/modules/featureFlag/featureFlagDecider.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/modules/featureFlag/featureFlagDecider.ts b/src/modules/featureFlag/featureFlagDecider.ts index 0fcdbc20b..c1521ce65 100644 --- a/src/modules/featureFlag/featureFlagDecider.ts +++ b/src/modules/featureFlag/featureFlagDecider.ts @@ -9,11 +9,8 @@ import chalk from 'chalk' export async function switchOpen(url: string, options) { try { const configClient = ToolbeltConfig.createClient() - console.log(configClient.getGlobalConfig) const { featureFlags } = await configClient.getGlobalConfig() - console.log(featureFlags) - logger.info('Your browser should open automatically. You can also use the url below.') logger.info(`${chalk.cyan(url)}`) From cfe0cbb829bca845da33c0570a6f1ed7a3b2c5cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Guedes?= Date: Mon, 5 May 2025 12:27:38 -0300 Subject: [PATCH 3/4] adding tests --- .../featureFlag/featureFlagDecider.test.ts | 112 ++++++++++++++++++ .../clients/IOClients/apps/ToolbeltConfig.ts | 4 +- src/api/error/ErrorReport.ts | 4 +- 3 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 src/__tests__/modules/featureFlag/featureFlagDecider.test.ts diff --git a/src/__tests__/modules/featureFlag/featureFlagDecider.test.ts b/src/__tests__/modules/featureFlag/featureFlagDecider.test.ts new file mode 100644 index 000000000..7a8ab6048 --- /dev/null +++ b/src/__tests__/modules/featureFlag/featureFlagDecider.test.ts @@ -0,0 +1,112 @@ +import chalk from 'chalk' +import { switchOpen } from '../../../modules/featureFlag/featureFlagDecider' + +jest.mock('open') +jest.mock('opn') +jest.mock('../../../api/clients/IOClients/apps/ToolbeltConfig') +jest.mock('../../../api/logger') +jest.mock('../../../api/error/ErrorReport') +import open from 'open' +import opn from 'opn' +import { GlobalConfig, ToolbeltConfig, VersionCheckRes } from '../../../api/clients/IOClients/apps/ToolbeltConfig' +import logger from '../../../api/logger' +import { IOClientFactory } from '../../../api/clients/IOClients/IOClientFactory' +import { ErrorReport, LogToUserOptions, CustomErrorReportBaseConstructorArgs } from '../../../api/error/ErrorReport' + +const getGlobalConfigMock = jest.fn() +const logErrorForUserMock = jest.fn() + +class ToolbeltConfigMock extends ToolbeltConfig { + public async versionValidate(_: string) { + return { minVersion: '', validVersion: true, message: '' } as VersionCheckRes + } + + public async getGlobalConfig(): Promise { + return getGlobalConfigMock() + } +} + +class ErrorReportMock extends ErrorReport { + public logErrorForUser(_?: LogToUserOptions) { + logErrorForUserMock() + return this + } +} + +describe('featureFlagDecider', () => { + beforeAll(() => { + const clientMock = new ToolbeltConfigMock(IOClientFactory.createIOContext(), {}) + jest.spyOn(ToolbeltConfig, 'createClient').mockReturnValue(clientMock) + + const errorMock = new ErrorReportMock({ shouldRemoteReport: false } as CustomErrorReportBaseConstructorArgs) + jest.spyOn(ErrorReport, 'createAndMaybeRegisterOnTelemetry').mockReturnValue(errorMock) + }) + + beforeEach(() => { + getGlobalConfigMock.mockClear() + logErrorForUserMock.mockClear() + }) + + describe('switchOpen', () => { + let url: string + let options: object + + beforeAll(() => { + url = 'https://vtex.myvtex.com' + options = {} + }) + + test('should log properly', async () => { + getGlobalConfigMock.mockReturnValue({ + featureFlags: { + FEATURE_FLAG_NEW_OPEN_PACKAGE: true, + }, + }) + + await switchOpen(url, options) + + expect(getGlobalConfigMock).toHaveBeenCalled() + expect(logger.info).toHaveBeenCalledTimes(2) + expect(logger.info).lastCalledWith(`${chalk.cyan(url)}`) + }) + + test('should use open when FEATURE_FLAG_NEW_OPEN_PACKAGE is true', async () => { + getGlobalConfigMock.mockReturnValue({ + featureFlags: { + FEATURE_FLAG_NEW_OPEN_PACKAGE: true, + }, + }) + + await switchOpen(url, options) + + expect(getGlobalConfigMock).toHaveBeenCalled() + expect(open).toHaveBeenCalled() + }) + + test('should use opn when FEATURE_FLAG_NEW_OPEN_PACKAGE is false', async () => { + getGlobalConfigMock.mockReturnValue({ + featureFlags: { + FEATURE_FLAG_NEW_OPEN_PACKAGE: false, + }, + }) + + await switchOpen(url, options) + + expect(getGlobalConfigMock).toHaveBeenCalled() + expect(opn).toHaveBeenCalled() + }) + + test('should show a debug log in case of errors', async () => { + const errorMsg = 'fake error' + getGlobalConfigMock.mockImplementation(() => { + throw new Error(errorMsg) + }) + + await switchOpen(url, options) + + expect(getGlobalConfigMock).toHaveBeenCalled() + expect(ErrorReport.createAndMaybeRegisterOnTelemetry).toHaveBeenCalled() + expect(ErrorReport) + }) + }) +}) diff --git a/src/api/clients/IOClients/apps/ToolbeltConfig.ts b/src/api/clients/IOClients/apps/ToolbeltConfig.ts index 1a6db463a..9845ef306 100644 --- a/src/api/clients/IOClients/apps/ToolbeltConfig.ts +++ b/src/api/clients/IOClients/apps/ToolbeltConfig.ts @@ -2,13 +2,13 @@ import { InstanceOptions, IOClient, IOContext } from '@vtex/api' import { NodeToRender } from '@vtex/toolbelt-message-renderer' import { IOClientFactory } from '../IOClientFactory' -interface VersionCheckRes { +export interface VersionCheckRes { minVersion: string validVersion: boolean message: string } -interface GlobalConfig { +export interface GlobalConfig { config: Record messages: Record featureFlags: Record diff --git a/src/api/error/ErrorReport.ts b/src/api/error/ErrorReport.ts index 0b373089b..f1513be9b 100644 --- a/src/api/error/ErrorReport.ts +++ b/src/api/error/ErrorReport.ts @@ -17,7 +17,7 @@ interface CustomErrorReportCreateArgs extends ErrorReportCreateArgs { shouldRemoteReport?: boolean } -interface CustomErrorReportBaseConstructorArgs extends ErrorReportBaseConstructorArgs { +export interface CustomErrorReportBaseConstructorArgs extends ErrorReportBaseConstructorArgs { shouldRemoteReport: boolean } @@ -31,7 +31,7 @@ interface ErrorEnv { } type ErrorLogLevel = 'error' | 'debug' -interface LogToUserOptions { +export interface LogToUserOptions { coreLogLevelDefault?: ErrorLogLevel requestDataLogLevelDefault?: ErrorLogLevel logLevels?: { From ada8a43d465939142b4c2053f1b0d608d9e39ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Guedes?= Date: Mon, 19 May 2025 11:38:45 -0300 Subject: [PATCH 4/4] update changelog to include version --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66d98c785..d2bb77b4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased] +## [4.1.1] + ### Updated + - Extend VBase client, integrating the `getConflicts` and the `resolveConflict` endpoints. ### Fixed