|
| 1 | +// |
| 2 | +// Copyright © 2025 Hardcore Engineering Inc. |
| 3 | +// |
| 4 | +// Licensed under the Eclipse Public License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. You may |
| 6 | +// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +import { Settings } from '../../main/settings' |
| 17 | +import { PackedConfig } from '../../main/config' |
| 18 | + |
| 19 | +const createMockStore = (): { get: jest.Mock, set: jest.Mock } => { |
| 20 | + const store: Record<string, any> = {} |
| 21 | + return { |
| 22 | + get: jest.fn((key: string) => store[key]), |
| 23 | + set: jest.fn((key: string, value: any) => { store[key] = value }) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +jest.mock('electron-store', () => { |
| 28 | + return jest.fn() |
| 29 | +}) |
| 30 | + |
| 31 | +describe('Settings', () => { |
| 32 | + let originalEnvironment: NodeJS.ProcessEnv |
| 33 | + let stubStoreInstance: ReturnType<typeof createMockStore> |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + originalEnvironment = process.env |
| 37 | + process.env = { ...originalEnvironment } |
| 38 | + stubStoreInstance = createMockStore() |
| 39 | + jest.clearAllMocks() |
| 40 | + }) |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + process.env = originalEnvironment |
| 44 | + jest.clearAllMocks() |
| 45 | + }) |
| 46 | + |
| 47 | + describe('readServerUrl', () => { |
| 48 | + test('isDev is true', () => { |
| 49 | + const systemUnderTest = new Settings(stubStoreInstance as any, true) |
| 50 | + |
| 51 | + const actualUrl = systemUnderTest.readServerUrl() |
| 52 | + |
| 53 | + expect(actualUrl).toBe('http://huly.local:8087') |
| 54 | + }) |
| 55 | + |
| 56 | + test('isDev is true and FRONT_URL is set', () => { |
| 57 | + process.env.FRONT_URL = 'http://custom-dev.local:3000' |
| 58 | + const systemUnderTest = new Settings(stubStoreInstance as any, true) |
| 59 | + |
| 60 | + const actualUrl = systemUnderTest.readServerUrl() |
| 61 | + |
| 62 | + expect(actualUrl).toBe('http://custom-dev.local:3000') |
| 63 | + }) |
| 64 | + |
| 65 | + test('isDev is false and server is in store', () => { |
| 66 | + const expectedUrl = 'https://stored.server.com' |
| 67 | + stubStoreInstance.get.mockReturnValue(expectedUrl) |
| 68 | + const systemUnderTest = new Settings(stubStoreInstance as any, false) |
| 69 | + |
| 70 | + const actualUrl = systemUnderTest.readServerUrl() |
| 71 | + |
| 72 | + expect(actualUrl).toBe(expectedUrl) |
| 73 | + }) |
| 74 | + |
| 75 | + test('store is empty and packed config exists', () => { |
| 76 | + stubStoreInstance.get.mockReturnValue(undefined) |
| 77 | + const expectedUrl = 'https://packed.server.com' |
| 78 | + const packedConfig: PackedConfig = { server: expectedUrl } |
| 79 | + const systemUnderTest = new Settings(stubStoreInstance as any, false, packedConfig) |
| 80 | + |
| 81 | + const actualUrl = systemUnderTest.readServerUrl() |
| 82 | + |
| 83 | + expect(actualUrl).toBe(expectedUrl) |
| 84 | + }) |
| 85 | + |
| 86 | + test('store and packed config are empty', () => { |
| 87 | + stubStoreInstance.get.mockReturnValue(undefined) |
| 88 | + const expectedUrl = 'https://env.server.com' |
| 89 | + process.env.FRONT_URL = expectedUrl |
| 90 | + const systemUnderTest = new Settings(stubStoreInstance as any, false) |
| 91 | + |
| 92 | + const actualUrl = systemUnderTest.readServerUrl() |
| 93 | + |
| 94 | + expect(actualUrl).toBe(expectedUrl) |
| 95 | + }) |
| 96 | + |
| 97 | + test('all options are unavailable', () => { |
| 98 | + stubStoreInstance.get.mockReturnValue(undefined) |
| 99 | + delete process.env.FRONT_URL |
| 100 | + const systemUnderTest = new Settings(stubStoreInstance as any, false) |
| 101 | + |
| 102 | + const result = systemUnderTest.readServerUrl() |
| 103 | + |
| 104 | + expect(result).toBe('https://huly.app') |
| 105 | + }) |
| 106 | + |
| 107 | + test('all (store, packed config, environment) options are available', () => { |
| 108 | + const expectedUrl = 'https://store.priority.com' |
| 109 | + stubStoreInstance.get.mockReturnValue(expectedUrl) |
| 110 | + process.env.FRONT_URL = 'https://env.server.com' |
| 111 | + const packedConfig: PackedConfig = { server: 'https://packed.server.com' } |
| 112 | + const systemUnderTest = new Settings(stubStoreInstance as any, false, packedConfig) |
| 113 | + |
| 114 | + const actualUrl = systemUnderTest.readServerUrl() |
| 115 | + |
| 116 | + expect(actualUrl).toBe(expectedUrl) |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + describe('read/write', () => { |
| 121 | + let systemUnderTest: Settings |
| 122 | + beforeEach(() => { |
| 123 | + systemUnderTest = new Settings(stubStoreInstance as any, false) |
| 124 | + }) |
| 125 | + |
| 126 | + describe('isMinimizeToTrayEnabled', () => { |
| 127 | + test.each([ |
| 128 | + { storedValue: true, expected: true, description: 'stored value is true' }, |
| 129 | + { storedValue: false, expected: false, description: 'stored value is false' } |
| 130 | + ])('$description', ({ storedValue, expected }) => { |
| 131 | + stubStoreInstance.get.mockReturnValue(storedValue) |
| 132 | + |
| 133 | + const actualValue = systemUnderTest.isMinimizeToTrayEnabled() |
| 134 | + |
| 135 | + expect(actualValue).toBe(expected) |
| 136 | + }) |
| 137 | + |
| 138 | + test.each([ |
| 139 | + { storedValue: undefined, description: 'no value is stored (undefined)' }, |
| 140 | + { storedValue: null, description: 'stored value is null' } |
| 141 | + ])('$description', ({ storedValue }) => { |
| 142 | + stubStoreInstance.get.mockReturnValue(storedValue) |
| 143 | + |
| 144 | + const actualValue = systemUnderTest.isMinimizeToTrayEnabled() |
| 145 | + |
| 146 | + expect(actualValue).toBe(false) |
| 147 | + }) |
| 148 | + }) |
| 149 | + |
| 150 | + describe('setMinimizeToTrayEnabled', () => { |
| 151 | + test.each([ |
| 152 | + { value: true, description: 'stored value is true' }, |
| 153 | + { value: false, description: 'stored value is false' } |
| 154 | + ])('$description', ({ value }) => { |
| 155 | + systemUnderTest.setMinimizeToTrayEnabled(value) |
| 156 | + |
| 157 | + expect(systemUnderTest.isMinimizeToTrayEnabled()).toBe(value) |
| 158 | + }) |
| 159 | + }) |
| 160 | + |
| 161 | + describe('setServerUrl', () => { |
| 162 | + test('write than read', () => { |
| 163 | + const expectedUrl = 'https://new.server.com' |
| 164 | + |
| 165 | + systemUnderTest.setServerUrl(expectedUrl) |
| 166 | + |
| 167 | + expect(systemUnderTest.readServerUrl()).toBe(expectedUrl) |
| 168 | + }) |
| 169 | + }) |
| 170 | + |
| 171 | + describe('getWindowBounds', () => { |
| 172 | + test('read', () => { |
| 173 | + const expectedValue = { x: 100, y: 300, width: 500, height: 700 } |
| 174 | + stubStoreInstance.get.mockReturnValue(expectedValue) |
| 175 | + |
| 176 | + const actulValue = systemUnderTest.getWindowBounds() |
| 177 | + |
| 178 | + expect(actulValue).toEqual(expectedValue) |
| 179 | + }) |
| 180 | + |
| 181 | + test('no bounds are stored', () => { |
| 182 | + stubStoreInstance.get.mockReturnValue(undefined) |
| 183 | + |
| 184 | + expect(systemUnderTest.getWindowBounds()).toBeUndefined() |
| 185 | + }) |
| 186 | + }) |
| 187 | + |
| 188 | + describe('setWindowBounds', () => { |
| 189 | + test('write than read', () => { |
| 190 | + const expectedValue = { x: 100, y: 200, width: 800, height: 600 } |
| 191 | + |
| 192 | + systemUnderTest.setWindowBounds(expectedValue) |
| 193 | + |
| 194 | + expect(systemUnderTest.getWindowBounds()).toBe(expectedValue) |
| 195 | + }) |
| 196 | + }) |
| 197 | + }) |
| 198 | +}) |
0 commit comments