@@ -1252,4 +1252,129 @@ describe("ClineProvider", () => {
12521252 )
12531253 } )
12541254 } )
1255+
1256+ describe ( "upsertApiConfiguration" , ( ) => {
1257+ test ( "handles error in upsertApiConfiguration gracefully" , async ( ) => {
1258+ provider . resolveWebviewView ( mockWebviewView )
1259+ const messageHandler = ( mockWebviewView . webview . onDidReceiveMessage as jest . Mock ) . mock . calls [ 0 ] [ 0 ]
1260+
1261+ // Mock ConfigManager methods to simulate error
1262+ provider . configManager = {
1263+ setModeConfig : jest . fn ( ) . mockRejectedValue ( new Error ( "Failed to update mode config" ) ) ,
1264+ listConfig : jest
1265+ . fn ( )
1266+ . mockResolvedValue ( [ { name : "test-config" , id : "test-id" , apiProvider : "anthropic" } ] ) ,
1267+ } as any
1268+
1269+ // Mock getState to provide necessary data
1270+ jest . spyOn ( provider , "getState" ) . mockResolvedValue ( {
1271+ mode : "code" ,
1272+ currentApiConfigName : "test-config" ,
1273+ } as any )
1274+
1275+ // Trigger updateApiConfiguration
1276+ await messageHandler ( {
1277+ type : "upsertApiConfiguration" ,
1278+ text : "test-config" ,
1279+ apiConfiguration : {
1280+ apiProvider : "anthropic" ,
1281+ apiKey : "test-key" ,
1282+ } ,
1283+ } )
1284+
1285+ // Verify error was logged and user was notified
1286+ expect ( mockOutputChannel . appendLine ) . toHaveBeenCalledWith (
1287+ expect . stringContaining ( "Error create new api configuration" ) ,
1288+ )
1289+ expect ( vscode . window . showErrorMessage ) . toHaveBeenCalledWith ( "Failed to create api configuration" )
1290+ } )
1291+
1292+ test ( "handles successful upsertApiConfiguration" , async ( ) => {
1293+ provider . resolveWebviewView ( mockWebviewView )
1294+ const messageHandler = ( mockWebviewView . webview . onDidReceiveMessage as jest . Mock ) . mock . calls [ 0 ] [ 0 ]
1295+
1296+ // Mock ConfigManager methods
1297+ provider . configManager = {
1298+ saveConfig : jest . fn ( ) . mockResolvedValue ( undefined ) ,
1299+ listConfig : jest
1300+ . fn ( )
1301+ . mockResolvedValue ( [ { name : "test-config" , id : "test-id" , apiProvider : "anthropic" } ] ) ,
1302+ } as any
1303+
1304+ const testApiConfig = {
1305+ apiProvider : "anthropic" as const ,
1306+ apiKey : "test-key" ,
1307+ }
1308+
1309+ // Trigger upsertApiConfiguration
1310+ await messageHandler ( {
1311+ type : "upsertApiConfiguration" ,
1312+ text : "test-config" ,
1313+ apiConfiguration : testApiConfig ,
1314+ } )
1315+
1316+ // Verify config was saved
1317+ expect ( provider . configManager . saveConfig ) . toHaveBeenCalledWith ( "test-config" , testApiConfig )
1318+
1319+ // Verify state updates
1320+ expect ( mockContext . globalState . update ) . toHaveBeenCalledWith ( "listApiConfigMeta" , [
1321+ { name : "test-config" , id : "test-id" , apiProvider : "anthropic" } ,
1322+ ] )
1323+ expect ( mockContext . globalState . update ) . toHaveBeenCalledWith ( "currentApiConfigName" , "test-config" )
1324+
1325+ // Verify state was posted to webview
1326+ expect ( mockPostMessage ) . toHaveBeenCalledWith ( expect . objectContaining ( { type : "state" } ) )
1327+ } )
1328+
1329+ test ( "handles buildApiHandler error in updateApiConfiguration" , async ( ) => {
1330+ provider . resolveWebviewView ( mockWebviewView )
1331+ const messageHandler = ( mockWebviewView . webview . onDidReceiveMessage as jest . Mock ) . mock . calls [ 0 ] [ 0 ]
1332+
1333+ // Mock buildApiHandler to throw an error
1334+ const { buildApiHandler } = require ( "../../../api" )
1335+ ; ( buildApiHandler as jest . Mock ) . mockImplementationOnce ( ( ) => {
1336+ throw new Error ( "API handler error" )
1337+ } )
1338+
1339+ // Mock ConfigManager methods
1340+ provider . configManager = {
1341+ saveConfig : jest . fn ( ) . mockResolvedValue ( undefined ) ,
1342+ listConfig : jest
1343+ . fn ( )
1344+ . mockResolvedValue ( [ { name : "test-config" , id : "test-id" , apiProvider : "anthropic" } ] ) ,
1345+ } as any
1346+
1347+ // Setup mock Cline instance
1348+ const mockCline = {
1349+ api : undefined ,
1350+ abortTask : jest . fn ( ) ,
1351+ }
1352+ // @ts -ignore - accessing private property for testing
1353+ provider . cline = mockCline
1354+
1355+ const testApiConfig = {
1356+ apiProvider : "anthropic" as const ,
1357+ apiKey : "test-key" ,
1358+ }
1359+
1360+ // Trigger upsertApiConfiguration
1361+ await messageHandler ( {
1362+ type : "upsertApiConfiguration" ,
1363+ text : "test-config" ,
1364+ apiConfiguration : testApiConfig ,
1365+ } )
1366+
1367+ // Verify error handling
1368+ expect ( mockOutputChannel . appendLine ) . toHaveBeenCalledWith (
1369+ expect . stringContaining ( "Error create new api configuration" ) ,
1370+ )
1371+ expect ( vscode . window . showErrorMessage ) . toHaveBeenCalledWith ( "Failed to create api configuration" )
1372+
1373+ // Verify state was still updated
1374+ expect ( mockContext . globalState . update ) . toHaveBeenCalledWith ( "listApiConfigMeta" , [
1375+ { name : "test-config" , id : "test-id" , apiProvider : "anthropic" } ,
1376+ ] )
1377+ expect ( mockContext . globalState . update ) . toHaveBeenCalledWith ( "currentApiConfigName" , "test-config" )
1378+ } )
1379+ } )
12551380} )
0 commit comments