@@ -12,6 +12,7 @@ import { importSettings, exportSettings } from "../importExport"
1212import { ProviderSettingsManager } from "../ProviderSettingsManager"
1313import { ContextProxy } from "../ContextProxy"
1414import { CustomModesManager } from "../CustomModesManager"
15+ import { safeWriteJson } from "../../../utils/safeWriteJson"
1516
1617import type { Mock } from "vitest"
1718
@@ -43,6 +44,8 @@ vi.mock("os", () => ({
4344 homedir : vi . fn ( ( ) => "/mock/home" ) ,
4445} ) )
4546
47+ vi . mock ( "../../../utils/safeWriteJson" )
48+
4649describe ( "importExport" , ( ) => {
4750 let mockProviderSettingsManager : ReturnType < typeof vi . mocked < ProviderSettingsManager > >
4851 let mockContextProxy : ReturnType < typeof vi . mocked < ContextProxy > >
@@ -384,11 +387,10 @@ describe("importExport", () => {
384387 expect ( mockContextProxy . export ) . toHaveBeenCalled ( )
385388 expect ( fs . mkdir ) . toHaveBeenCalledWith ( "/mock/path" , { recursive : true } )
386389
387- expect ( fs . writeFile ) . toHaveBeenCalledWith (
388- "/mock/path/roo-code-settings.json" ,
389- JSON . stringify ( { providerProfiles : mockProviderProfiles , globalSettings : mockGlobalSettings } , null , 2 ) ,
390- "utf-8" ,
391- )
390+ expect ( safeWriteJson ) . toHaveBeenCalledWith ( "/mock/path/roo-code-settings.json" , {
391+ providerProfiles : mockProviderProfiles ,
392+ globalSettings : mockGlobalSettings ,
393+ } )
392394 } )
393395
394396 it ( "should include globalSettings when allowedMaxRequests is null" , async ( ) => {
@@ -417,11 +419,10 @@ describe("importExport", () => {
417419 contextProxy : mockContextProxy ,
418420 } )
419421
420- expect ( fs . writeFile ) . toHaveBeenCalledWith (
421- "/mock/path/roo-code-settings.json" ,
422- JSON . stringify ( { providerProfiles : mockProviderProfiles , globalSettings : mockGlobalSettings } , null , 2 ) ,
423- "utf-8" ,
424- )
422+ expect ( safeWriteJson ) . toHaveBeenCalledWith ( "/mock/path/roo-code-settings.json" , {
423+ providerProfiles : mockProviderProfiles ,
424+ globalSettings : mockGlobalSettings ,
425+ } )
425426 } )
426427
427428 it ( "should handle errors during the export process" , async ( ) => {
@@ -436,7 +437,8 @@ describe("importExport", () => {
436437 } )
437438
438439 mockContextProxy . export . mockResolvedValue ( { mode : "code" } )
439- ; ( fs . writeFile as Mock ) . mockRejectedValue ( new Error ( "Write error" ) )
440+ // Simulate an error during the safeWriteJson operation
441+ ; ( safeWriteJson as Mock ) . mockRejectedValueOnce ( new Error ( "Safe write error" ) )
440442
441443 await exportSettings ( {
442444 providerSettingsManager : mockProviderSettingsManager ,
@@ -447,8 +449,10 @@ describe("importExport", () => {
447449 expect ( mockProviderSettingsManager . export ) . toHaveBeenCalled ( )
448450 expect ( mockContextProxy . export ) . toHaveBeenCalled ( )
449451 expect ( fs . mkdir ) . toHaveBeenCalledWith ( "/mock/path" , { recursive : true } )
450- expect ( fs . writeFile ) . toHaveBeenCalled ( )
452+ expect ( safeWriteJson ) . toHaveBeenCalled ( ) // safeWriteJson is called, but it will throw
451453 // The error is caught and the function exits silently.
454+ // Optionally, ensure no error message was shown if that's part of "silent"
455+ // expect(vscode.window.showErrorMessage).not.toHaveBeenCalled();
452456 } )
453457
454458 it ( "should handle errors during directory creation" , async ( ) => {
@@ -474,7 +478,7 @@ describe("importExport", () => {
474478 expect ( mockProviderSettingsManager . export ) . toHaveBeenCalled ( )
475479 expect ( mockContextProxy . export ) . toHaveBeenCalled ( )
476480 expect ( fs . mkdir ) . toHaveBeenCalled ( )
477- expect ( fs . writeFile ) . not . toHaveBeenCalled ( ) // Should not be called since mkdir failed.
481+ expect ( safeWriteJson ) . not . toHaveBeenCalled ( ) // Should not be called since mkdir failed.
478482 } )
479483
480484 it ( "should use the correct default save location" , async ( ) => {
0 commit comments