@@ -12,6 +12,8 @@ import {RepositoryFile} from '../../src/resources/repository-file'
1212import { randomUUID } from 'crypto'
1313import { Team , Privacy as TeamPrivacy } from '../../src/resources/team'
1414import { RepositoryBranchProtectionRule } from '../../src/resources/repository-branch-protection-rule'
15+ import { toggleArchivedRepos } from '../../src/actions/shared/toggle-archived-repos'
16+ import * as state from '../../src/terraform/state'
1517
1618test ( 'can retrieve resources from YAML schema' , async ( ) => {
1719 const config = Config . FromPath ( )
@@ -385,3 +387,43 @@ test('can add and remove resources through sync', async () => {
385387 resources = config . getAllResources ( )
386388 expect ( resources ) . toHaveLength ( desiredResources . length )
387389} )
390+
391+ test ( 'clears and re-adds repository fields when archiving/unarchiving' , async ( ) => {
392+ let config = Config . FromPath ( )
393+
394+ const unarchivedRepo = config . getResources ( Repository ) . find ( r => ! r . archived ) !
395+
396+ expect ( unarchivedRepo . visibility ) . toBeDefined ( )
397+
398+ unarchivedRepo . archived = true
399+ config . addResource ( unarchivedRepo )
400+
401+ await toggleArchivedRepos ( config )
402+
403+ const archivedRepo = config
404+ . getResources ( Repository )
405+ . find ( r => r . getStateAddress ( ) == unarchivedRepo . getStateAddress ( ) ) !
406+
407+ expect ( archivedRepo . archived ) . toBe ( true )
408+ expect ( archivedRepo . visibility ) . not . toBeDefined ( )
409+
410+ archivedRepo . archived = false
411+ config . addResource ( archivedRepo )
412+
413+ const newState = JSON . parse ( await state . loadState ( ) )
414+ newState . values . root_module . resources . find (
415+ ( r : any ) => r . address == unarchivedRepo . getStateAddress ( )
416+ ) ! . values . archived = true
417+
418+ const loadStateMock = jest . spyOn ( state , 'loadState' )
419+ loadStateMock . mockImplementation ( async ( ) => JSON . stringify ( newState ) )
420+
421+ await toggleArchivedRepos ( config )
422+
423+ const toggledRepo = config
424+ . getResources ( Repository )
425+ . find ( r => r . getStateAddress ( ) == unarchivedRepo . getStateAddress ( ) ) !
426+
427+ expect ( toggledRepo . archived ) . toBe ( false )
428+ expect ( toggledRepo . visibility ) . toBeDefined ( )
429+ } )
0 commit comments