@@ -924,6 +924,87 @@ describe("DiffViewProvider", () => {
924924 )
925925 } )
926926
927+ it ( "revertChanges() closes every matching target tab in a mixed tab set before unlinking the created file" , async ( ) => {
928+ // The mixed set the single-tab fixtures cannot express: an unrelated dirty tab for another
929+ // file, the composite diff tab for the target, and a plain tab for the target, interleaved
930+ // across two groups. A sweep that stopped at the first match would close one of the two
931+ // target tabs and unlink the file underneath the other, and each single-tab row passes on
932+ // its own either way.
933+ const editor = buildActiveDiffEditor ( { isDirty : true } )
934+ const originalTabs = Object . getOwnPropertyDescriptor ( vscode . window . tabGroups , "all" )
935+ const originalDocs = Object . getOwnPropertyDescriptor ( vscode . workspace , "textDocuments" )
936+ // tabGroups.close() is declared to take a tab, a tab group, or a group list. The sweep under
937+ // test hands it tabs; the collector takes the type close() itself declares, so the recorded
938+ // objects need no cast before the assertions below name them.
939+ const closedTargets : ( vscode . Tab | vscode . TabGroup | readonly vscode . TabGroup [ ] ) [ ] = [ ]
940+ const closeSpy = vi . spyOn ( vscode . window . tabGroups , "close" ) . mockImplementation ( async ( target ) => {
941+ closedTargets . push ( target )
942+ return true
943+ } )
944+ // Both target tabs are dirty, which is what keeps closeAllDiffViews() out of the way: it
945+ // skips dirty tabs, so the only sweep that can close them is this one.
946+ const diffTab = {
947+ input : makeDiffTabInput ( DIFF_VIEW_URI_SCHEME , mockTargetPath ) ,
948+ isDirty : true ,
949+ label : "mock-target-file.ts (Changes)" ,
950+ }
951+ const targetPlainTab = {
952+ input : Object . assign ( new vscode . TabInputText ( makeUri ( mockTargetPath ) ) , {
953+ uri : makeUri ( mockTargetPath ) ,
954+ } ) ,
955+ isDirty : true ,
956+ label : "mock-target-file.ts" ,
957+ }
958+ const unrelatedDirtyTab = {
959+ input : Object . assign ( new vscode . TabInputText ( makeUri ( `${ mockCwd } /unrelated-dirty.ts` ) ) , {
960+ uri : makeUri ( `${ mockCwd } /unrelated-dirty.ts` ) ,
961+ } ) ,
962+ isDirty : true ,
963+ label : "unrelated-dirty.ts" ,
964+ }
965+ Object . defineProperty ( vscode . window . tabGroups , "all" , {
966+ get : ( ) => [ { tabs : [ unrelatedDirtyTab , diffTab ] } , { tabs : [ targetPlainTab ] } ] ,
967+ configurable : true ,
968+ } )
969+ Object . defineProperty ( vscode . workspace , "textDocuments" , {
970+ get : ( ) => [ editor . document ] ,
971+ configurable : true ,
972+ } )
973+ vi . mocked ( vscode . workspace . applyEdit ) . mockResolvedValue ( true )
974+ Object . assign ( diffViewProvider , {
975+ isEditing : true ,
976+ relPath : "mock-target-file.ts" ,
977+ activeDiffEditor : editor ,
978+ editType : "create" ,
979+ createdDirs : [ ] ,
980+ originalContent : "" ,
981+ writeCommitted : false ,
982+ } )
983+ try {
984+ await expect ( diffViewProvider . revertChanges ( ) ) . resolves . toBeUndefined ( )
985+ } finally {
986+ if ( originalTabs ) {
987+ Object . defineProperty ( vscode . window . tabGroups , "all" , originalTabs )
988+ }
989+ if ( originalDocs ) {
990+ Object . defineProperty ( vscode . workspace , "textDocuments" , originalDocs )
991+ }
992+ }
993+ // The complete closed set: both target tabs, in either order, and nothing else.
994+ expect ( closedTargets . length ) . toBe ( 2 )
995+ expect ( closedTargets ) . toContainEqual ( diffTab )
996+ expect ( closedTargets ) . toContainEqual ( targetPlainTab )
997+ expect ( closedTargets ) . not . toContainEqual ( unrelatedDirtyTab )
998+ expect ( closeSpy ) . toHaveBeenCalledTimes ( 2 )
999+ expect ( vscode . workspace . applyEdit ) . toHaveBeenCalledTimes ( 2 )
1000+ expect ( fs . unlink ) . toHaveBeenCalledTimes ( 1 )
1001+ expect ( fs . unlink ) . toHaveBeenCalledWith ( mockTargetPath )
1002+ // Both closes, not merely one of them, precede the unlink.
1003+ expect ( Math . max ( ...closeSpy . mock . invocationCallOrder ) ) . toBeLessThan (
1004+ ( fs . unlink as unknown as Mock ) . mock . invocationCallOrder [ 0 ] ,
1005+ )
1006+ } )
1007+
9271008 it ( "revertChanges() keeps the created file when the dirty diff tab refuses to close" , async ( ) => {
9281009 // Fail closed: a refused close means unapproved content is still on screen, so the
9291010 // unlink must not run. Deleting the file under a tab the user has not dismissed loses
0 commit comments