diff --git a/packages/cli/src/config/settings.test.ts b/packages/cli/src/config/settings.test.ts index 9550932c9..af421c5a3 100644 --- a/packages/cli/src/config/settings.test.ts +++ b/packages/cli/src/config/settings.test.ts @@ -48,6 +48,7 @@ import { USER_SETTINGS_PATH, // This IS the mocked path. getSystemSettingsPath, getSystemDefaultsPath, + SettingScope, SETTINGS_DIRECTORY_NAME, // This is from the original module, but used by the mock. type Settings, loadEnvironment, @@ -2334,6 +2335,85 @@ describe('Settings Loading and Merging', () => { }); }); + describe('setValue persistence', () => { + it('preserves models added to settings.json after startup when updating model.name', () => { + (mockFsExistsSync as Mock).mockReturnValue(true); + + const initialUserSettingsContent = { + [SETTINGS_VERSION_KEY]: SETTINGS_VERSION, + modelProviders: { + openai: [ + { + id: 'existing-model', + name: 'Existing Model', + baseUrl: 'https://example.com/v1', + envKey: 'OPENAI_API_KEY', + }, + ], + }, + model: { + name: 'existing-model', + }, + }; + + const externallyModifiedUserSettingsContent = { + [SETTINGS_VERSION_KEY]: SETTINGS_VERSION, + modelProviders: { + openai: [ + { + id: 'existing-model', + name: 'Existing Model', + baseUrl: 'https://example.com/v1', + envKey: 'OPENAI_API_KEY', + }, + { + id: 'manually-added-model', + name: 'Manually Added Model', + baseUrl: 'https://example.com/v1', + envKey: 'OPENAI_API_KEY', + }, + ], + }, + model: { + name: 'existing-model', + }, + }; + + let currentUserSettingsContent = JSON.stringify( + initialUserSettingsContent, + ); + + (fs.readFileSync as Mock).mockImplementation( + (p: fs.PathOrFileDescriptor) => { + if (p === USER_SETTINGS_PATH) { + return currentUserSettingsContent; + } + return '{}'; + }, + ); + + const settings = loadSettings(MOCK_WORKSPACE_DIR); + currentUserSettingsContent = JSON.stringify( + externallyModifiedUserSettingsContent, + ); + + settings.setValue( + SettingScope.User, + 'model.name', + 'manually-added-model', + ); + + const writeCall = (fs.writeFileSync as Mock).mock.calls.at(-1); + expect(writeCall).toBeDefined(); + + const writtenContent = JSON.parse(String(writeCall?.[1])); + expect(writtenContent.model.name).toBe('manually-added-model'); + expect(writtenContent.modelProviders.openai).toEqual( + externallyModifiedUserSettingsContent.modelProviders.openai, + ); + }); + }); + describe('loadEnvironment', () => { function setup({ isFolderTrustEnabled = true,