@@ -243,4 +243,71 @@ describe('#directory context connections', () => {
243243 expect ( fs . existsSync ( path . join ( connectionsFolder , 'includedConnection.json' ) ) ) . to . equal ( true ) ;
244244 expect ( fs . existsSync ( path . join ( connectionsFolder , 'excludedConnection.json' ) ) ) . to . equal ( false ) ;
245245 } ) ;
246+
247+ it ( 'should preserve pre-existing files for excluded connections on re-dump' , async ( ) => {
248+ const dir = path . join ( testDataDir , 'directory' , 'connectionsDumpExclude' ) ;
249+ cleanThenMkdir ( dir ) ;
250+
251+ // Simulate a prior export that wrote the excluded connection's file
252+ const connectionsFolder = path . join ( dir , constants . CONNECTIONS_DIRECTORY ) ;
253+ fs . ensureDirSync ( connectionsFolder ) ;
254+ fs . writeFileSync (
255+ path . join ( connectionsFolder , 'excludedConnection.json' ) ,
256+ '{"name":"excludedConnection"}'
257+ ) ;
258+
259+ const context = new Context ( { AUTH0_INPUT_FILE : dir } , mockMgmtClient ( ) ) ;
260+ context . assets . connections = [ { name : 'includedConnection' , strategy : 'waad' } ] ;
261+ context . assets . exclude = { connections : [ 'excludedConnection' ] } ;
262+
263+ await handler . dump ( context ) ;
264+
265+ expect ( fs . existsSync ( path . join ( connectionsFolder , 'includedConnection.json' ) ) ) . to . equal ( true ) ;
266+ expect ( fs . existsSync ( path . join ( connectionsFolder , 'excludedConnection.json' ) ) ) . to . equal ( true ) ;
267+ } ) ;
268+
269+ it ( 'should remove stale connection files when connections are removed from source' , async ( ) => {
270+ const dir = path . join ( testDataDir , 'directory' , 'connectionsStaleFiles' ) ;
271+ cleanThenMkdir ( dir ) ;
272+
273+ // Simulate a previous export that created connection files
274+ const connectionsFolder = path . join ( dir , constants . CONNECTIONS_DIRECTORY ) ;
275+ fs . ensureDirSync ( connectionsFolder ) ;
276+ fs . writeFileSync ( path . join ( connectionsFolder , 'facebook.json' ) , '{"name":"facebook"}' ) ;
277+ fs . writeFileSync ( path . join ( connectionsFolder , 'github.json' ) , '{"name":"github"}' ) ;
278+
279+ const context = new Context ( { AUTH0_INPUT_FILE : dir } , mockMgmtClient ( ) ) ;
280+ // Re-export with only one connection (facebook removed)
281+ context . assets . connections = [ { name : 'github' , strategy : 'github' } ] ;
282+
283+ await handler . dump ( context ) ;
284+
285+ expect ( fs . existsSync ( path . join ( connectionsFolder , 'github.json' ) ) ) . to . equal ( true ) ;
286+ expect ( fs . existsSync ( path . join ( connectionsFolder , 'facebook.json' ) ) ) . to . equal ( false ) ;
287+ } ) ;
288+
289+ it ( 'should remove all stale connection files when all connections are removed from source' , async ( ) => {
290+ const dir = path . join ( testDataDir , 'directory' , 'connectionsAllStale' ) ;
291+ cleanThenMkdir ( dir ) ;
292+
293+ // Simulate a previous export that created connection files
294+ const connectionsFolder = path . join ( dir , constants . CONNECTIONS_DIRECTORY ) ;
295+ fs . ensureDirSync ( connectionsFolder ) ;
296+ fs . writeFileSync ( path . join ( connectionsFolder , 'facebook.json' ) , '{"name":"facebook"}' ) ;
297+ fs . writeFileSync ( path . join ( connectionsFolder , 'github.json' ) , '{"name":"github"}' ) ;
298+
299+ const context = new Context ( { AUTH0_INPUT_FILE : dir } , mockMgmtClient ( ) ) ;
300+ // Re-export with zero connections
301+ context . assets . connections = [ ] ;
302+
303+ await handler . dump ( context ) ;
304+
305+ const remainingJsonFiles = fs . readdirSync ( connectionsFolder ) . filter ( ( f ) => f . endsWith ( '.json' ) ) ;
306+ expect ( remainingJsonFiles ) . to . deep . equal ( [ ] ) ;
307+
308+ // Verify parse of now-empty directory returns [] not null (enabling deletions on import)
309+ const parseContext = new Context ( { AUTH0_INPUT_FILE : dir } , mockMgmtClient ( ) ) ;
310+ await parseContext . loadAssetsFromLocal ( ) ;
311+ expect ( parseContext . assets . connections ) . to . deep . equal ( [ ] ) ;
312+ } ) ;
246313} ) ;
0 commit comments