diff --git a/desktop/src/__test__/main/settings.test.ts b/desktop/src/__test__/main/settings.test.ts index 3db1f225b98..0c6a3564570 100644 --- a/desktop/src/__test__/main/settings.test.ts +++ b/desktop/src/__test__/main/settings.test.ts @@ -159,10 +159,13 @@ describe('Settings', () => { }) describe('setServerUrl', () => { - test('write than read', () => { - const expectedUrl = 'https://new.server.com' - - systemUnderTest.setServerUrl(expectedUrl) + const expectedUrl = 'https://new.server.com' + test.each([ + { serverUrl: 'https://new.server.com' }, + { serverUrl: 'https://new.server.com/' }, + { serverUrl: 'https://new.server.com//' } + ])('write $serverUrl than read', ({ serverUrl }) => { + systemUnderTest.setServerUrl(serverUrl) expect(systemUnderTest.readServerUrl()).toBe(expectedUrl) }) diff --git a/desktop/src/main/settings.ts b/desktop/src/main/settings.ts index 1996b231412..53f9e244aba 100644 --- a/desktop/src/main/settings.ts +++ b/desktop/src/main/settings.ts @@ -32,15 +32,9 @@ export class Settings { } readServerUrl (): string { - if (this.isDev) { - return process.env.FRONT_URL ?? 'http://huly.local:8087' - } - return ( - (this.store as any).get(Settings.SETTINGS_KEY_SERVER) as string ?? - this.packedConfig?.server ?? - process.env.FRONT_URL ?? - 'https://huly.app' - ) + const url = this.extractUrl() + // Motivation: fix existing Huly installations (saved on disk URLs). + return Settings.sanitizeUrl(url) } isMinimizeToTrayEnabled (): boolean { @@ -52,7 +46,8 @@ export class Settings { } setServerUrl (serverUrl: string): void { - (this.store as any).set(Settings.SETTINGS_KEY_SERVER, serverUrl) + const sanitizedUrl: string = Settings.sanitizeUrl(serverUrl) + ;(this.store as any).set(Settings.SETTINGS_KEY_SERVER, sanitizedUrl) } getWindowBounds (): any { @@ -62,4 +57,24 @@ export class Settings { setWindowBounds (bounds: any): void { (this.store as any).set(Settings.SETTINGS_KEY_WINDOW_BOUNDS, bounds) } + + private extractUrl (): string { + if (this.isDev) { + return process.env.FRONT_URL ?? 'http://huly.local:8087' + } + return ( + (this.store as any).get(Settings.SETTINGS_KEY_SERVER) as string ?? + this.packedConfig?.server ?? + process.env.FRONT_URL ?? + 'https://huly.app' + ) + } + + private static sanitizeUrl (serverUrl: string): string { + let cleanUrl: string = serverUrl + while (cleanUrl.endsWith('/')) { + cleanUrl = cleanUrl.slice(0, -1) + } + return cleanUrl + } }