11const { when } = require ( 'jest-when' )
22const Variables = require ( '../../../../lib/plugins/variables' )
3+ const NopCommand = require ( '../../../../lib/nopcommand' )
34
45describe ( 'Variables' , ( ) => {
56 let github
@@ -10,13 +11,13 @@ describe('Variables', () => {
1011 return variables
1112 }
1213
13- function configure ( ) {
14- const log = { debug : console . debug , error : console . error }
14+ function configure ( nop = false ) {
15+ const log = { debug : jest . fn ( ) , error : console . error }
1516 const errors = [ ]
16- return new Variables ( undefined , github , { owner : org , repo } , [ { name : 'test' , value : 'test' } ] , log , errors )
17+ return new Variables ( nop , github , { owner : org , repo } , [ { name : 'test' , value : 'test' } ] , log , errors )
1718 }
1819
19- beforeAll ( ( ) => {
20+ beforeEach ( ( ) => {
2021 github = {
2122 request : jest . fn ( ) . mockReturnValue ( Promise . resolve ( true ) )
2223 }
@@ -76,4 +77,168 @@ describe('Variables', () => {
7677 )
7778 } )
7879 } )
80+
81+ describe ( 'noop mode' , ( ) => {
82+ describe ( 'sync' , ( ) => {
83+ it ( 'should return NopCommands and not make mutating API calls when nop is true' , async ( ) => {
84+ const plugin = configure ( true )
85+
86+ when ( github . request )
87+ . calledWith ( 'GET /repos/:org/:repo/actions/variables' , { org, repo } )
88+ . mockResolvedValue ( {
89+ data : {
90+ variables : [ { name : 'EXISTING_VAR' , value : 'existing-value' } ]
91+ }
92+ } )
93+
94+ const result = await plugin . sync ( )
95+
96+ // Should have made GET call to fetch existing variables
97+ expect ( github . request ) . toHaveBeenCalledWith ( 'GET /repos/:org/:repo/actions/variables' , { org, repo } )
98+
99+ // Should NOT have made any mutating calls (POST, PATCH, DELETE)
100+ expect ( github . request ) . not . toHaveBeenCalledWith (
101+ expect . stringMatching ( / ^ ( P O S T | P A T C H | D E L E T E ) / ) ,
102+ expect . anything ( )
103+ )
104+
105+ // Result should contain NopCommands
106+ expect ( Array . isArray ( result ) ) . toBe ( true )
107+ expect ( result . length ) . toBeGreaterThan ( 0 )
108+ result . forEach ( cmd => expect ( cmd ) . toBeInstanceOf ( NopCommand ) )
109+ } )
110+ } )
111+
112+ describe ( 'add' , ( ) => {
113+ it ( 'should return NopCommand and not make API call when nop is true' , async ( ) => {
114+ const plugin = configure ( true )
115+ const variable = { name : 'NEW_VAR' , value : 'new-value' }
116+
117+ const result = await plugin . add ( variable )
118+
119+ expect ( result ) . toBeInstanceOf ( NopCommand )
120+ expect ( result . plugin ) . toBe ( 'Variables' )
121+ expect ( github . request ) . not . toHaveBeenCalled ( )
122+ } )
123+
124+ it ( 'should make API call when nop is false' , async ( ) => {
125+ const plugin = configure ( false )
126+ const variable = { name : 'NEW_VAR' , value : 'new-value' }
127+
128+ await plugin . add ( variable )
129+
130+ expect ( github . request ) . toHaveBeenCalledWith (
131+ 'POST /repos/:org/:repo/actions/variables' ,
132+ expect . objectContaining ( {
133+ org,
134+ repo,
135+ name : 'NEW_VAR' ,
136+ value : 'new-value'
137+ } )
138+ )
139+ } )
140+ } )
141+
142+ describe ( 'remove' , ( ) => {
143+ it ( 'should return NopCommand and not make API call when nop is true' , async ( ) => {
144+ const plugin = configure ( true )
145+ const existing = { name : 'EXISTING_VAR' , value : 'existing-value' }
146+
147+ const result = await plugin . remove ( existing )
148+
149+ expect ( result ) . toBeInstanceOf ( NopCommand )
150+ expect ( result . plugin ) . toBe ( 'Variables' )
151+ expect ( github . request ) . not . toHaveBeenCalled ( )
152+ } )
153+
154+ it ( 'should make API call when nop is false' , async ( ) => {
155+ const plugin = configure ( false )
156+ const existing = { name : 'EXISTING_VAR' , value : 'existing-value' }
157+
158+ await plugin . remove ( existing )
159+
160+ expect ( github . request ) . toHaveBeenCalledWith (
161+ 'DELETE /repos/:org/:repo/actions/variables/:variable_name' ,
162+ expect . objectContaining ( {
163+ org,
164+ repo,
165+ variable_name : 'EXISTING_VAR'
166+ } )
167+ )
168+ } )
169+ } )
170+
171+ describe ( 'update' , ( ) => {
172+ it ( 'should return NopCommand array and not make API calls when nop is true' , async ( ) => {
173+ const plugin = configure ( true )
174+ const existing = [ { name : 'VAR1' , value : 'old-value' } ]
175+ const updated = [ { name : 'VAR1' , value : 'new-value' } ]
176+
177+ const result = await plugin . update ( existing , updated )
178+
179+ expect ( Array . isArray ( result ) ) . toBe ( true )
180+ expect ( result ) . toHaveLength ( 1 )
181+ expect ( result [ 0 ] ) . toBeInstanceOf ( NopCommand )
182+ expect ( result [ 0 ] . plugin ) . toBe ( 'Variables' )
183+ expect ( github . request ) . not . toHaveBeenCalled ( )
184+ } )
185+
186+ it ( 'should return NopCommand when adding new variable in update with nop true' , async ( ) => {
187+ const plugin = configure ( true )
188+ const existing = [ ]
189+ const updated = [ { name : 'NEW_VAR' , value : 'new-value' } ]
190+
191+ const result = await plugin . update ( existing , updated )
192+
193+ expect ( Array . isArray ( result ) ) . toBe ( true )
194+ expect ( result ) . toHaveLength ( 1 )
195+ expect ( result [ 0 ] ) . toBeInstanceOf ( NopCommand )
196+ expect ( github . request ) . not . toHaveBeenCalled ( )
197+ } )
198+
199+ it ( 'should return NopCommand when deleting variable in update with nop true' , async ( ) => {
200+ const plugin = configure ( true )
201+ const existing = [ { name : 'OLD_VAR' , value : 'old-value' } ]
202+ const updated = [ ]
203+
204+ const result = await plugin . update ( existing , updated )
205+
206+ expect ( Array . isArray ( result ) ) . toBe ( true )
207+ expect ( result ) . toHaveLength ( 1 )
208+ expect ( result [ 0 ] ) . toBeInstanceOf ( NopCommand )
209+ expect ( github . request ) . not . toHaveBeenCalled ( )
210+ } )
211+
212+ it ( 'should return multiple NopCommands for multiple operations with nop true' , async ( ) => {
213+ const plugin = configure ( true )
214+ const existing = [ { name : 'UPDATE_VAR' , value : 'old' } , { name : 'DELETE_VAR' , value : 'delete-me' } ]
215+ const updated = [ { name : 'UPDATE_VAR' , value : 'new' } , { name : 'ADD_VAR' , value : 'added' } ]
216+
217+ const result = await plugin . update ( existing , updated )
218+
219+ expect ( Array . isArray ( result ) ) . toBe ( true )
220+ expect ( result ) . toHaveLength ( 3 ) // 1 update + 1 add + 1 delete
221+ result . forEach ( cmd => expect ( cmd ) . toBeInstanceOf ( NopCommand ) )
222+ expect ( github . request ) . not . toHaveBeenCalled ( )
223+ } )
224+
225+ it ( 'should make API calls when nop is false' , async ( ) => {
226+ const plugin = configure ( false )
227+ const existing = [ { name : 'VAR1' , value : 'old-value' } ]
228+ const updated = [ { name : 'VAR1' , value : 'new-value' } ]
229+
230+ await plugin . update ( existing , updated )
231+
232+ expect ( github . request ) . toHaveBeenCalledWith (
233+ 'PATCH /repos/:org/:repo/actions/variables/:variable_name' ,
234+ expect . objectContaining ( {
235+ org,
236+ repo,
237+ variable_name : 'VAR1' ,
238+ value : 'new-value'
239+ } )
240+ )
241+ } )
242+ } )
243+ } )
79244} )
0 commit comments