22
33import fs from "fs/promises"
44import * as path from "path"
5- import os from "os"
65
76import * as vscode from "vscode"
87
@@ -37,6 +36,7 @@ jest.mock("os", () => ({
3736describe ( "importExport" , ( ) => {
3837 let mockProviderSettingsManager : jest . Mocked < ProviderSettingsManager >
3938 let mockContextProxy : jest . Mocked < ContextProxy >
39+ let mockExtensionContext : jest . Mocked < vscode . ExtensionContext >
4040
4141 beforeEach ( ( ) => {
4242 // Reset all mocks
@@ -55,6 +55,15 @@ describe("importExport", () => {
5555 setValue : jest . fn ( ) ,
5656 export : jest . fn ( ) . mockImplementation ( ( ) => Promise . resolve ( { } ) ) ,
5757 } as unknown as jest . Mocked < ContextProxy >
58+
59+ const map = new Map < string , string > ( )
60+
61+ mockExtensionContext = {
62+ secrets : {
63+ get : jest . fn ( ) . mockImplementation ( ( key : string ) => map . get ( key ) ) ,
64+ store : jest . fn ( ) . mockImplementation ( ( key : string , value : string ) => map . set ( key , value ) ) ,
65+ } ,
66+ } as unknown as jest . Mocked < vscode . ExtensionContext >
5867 } )
5968
6069 describe ( "importSettings" , ( ) => {
@@ -161,9 +170,7 @@ describe("importExport", () => {
161170
162171 // Invalid content (missing required fields)
163172 const mockInvalidContent = JSON . stringify ( {
164- providerProfiles : {
165- apiConfigs : { } ,
166- } ,
173+ providerProfiles : { apiConfigs : { } } ,
167174 globalSettings : { } ,
168175 } )
169176
@@ -219,6 +226,38 @@ describe("importExport", () => {
219226 expect ( mockProviderSettingsManager . import ) . not . toHaveBeenCalled ( )
220227 expect ( mockContextProxy . setValues ) . not . toHaveBeenCalled ( )
221228 } )
229+
230+ it ( "should not clobber existing api configs" , async ( ) => {
231+ const providerSettingsManager = new ProviderSettingsManager ( mockExtensionContext )
232+ await providerSettingsManager . saveConfig ( "openai" , { apiProvider : "openai" , id : "openai" } )
233+
234+ const configs = await providerSettingsManager . listConfig ( )
235+ expect ( configs [ 0 ] . name ) . toBe ( "default" )
236+ expect ( configs [ 1 ] . name ) . toBe ( "openai" )
237+ ; ( vscode . window . showOpenDialog as jest . Mock ) . mockResolvedValue ( [ { fsPath : "/mock/path/settings.json" } ] )
238+
239+ const mockFileContent = JSON . stringify ( {
240+ globalSettings : { mode : "code" } ,
241+ providerProfiles : {
242+ currentApiConfigName : "anthropic" ,
243+ apiConfigs : { default : { apiProvider : "anthropic" as const , id : "anthropic" } } ,
244+ } ,
245+ } )
246+
247+ ; ( fs . readFile as jest . Mock ) . mockResolvedValue ( mockFileContent )
248+
249+ mockContextProxy . export . mockResolvedValue ( { mode : "code" } )
250+
251+ const result = await importSettings ( {
252+ providerSettingsManager,
253+ contextProxy : mockContextProxy ,
254+ } )
255+
256+ expect ( result . success ) . toBe ( true )
257+ expect ( result . providerProfiles ?. apiConfigs [ "openai" ] ) . toBeDefined ( )
258+ expect ( result . providerProfiles ?. apiConfigs [ "default" ] ) . toBeDefined ( )
259+ expect ( result . providerProfiles ?. apiConfigs [ "default" ] . apiProvider ) . toBe ( "anthropic" )
260+ } )
222261 } )
223262
224263 describe ( "exportSettings" , ( ) => {
0 commit comments