11import * as core from '@actions/core' ;
22import * as github from '@actions/github' ;
33import nock from 'nock' ;
4- import { deleteBranch } from '../src/index' ;
4+ import { deleteBranch , run } from '../src/index' ;
55
66// Mock the modules
77jest . mock ( '@actions/core' ) ;
@@ -63,4 +63,122 @@ describe('GitHub Action Integration', () => {
6363
6464 expect ( scope . isDone ( ) ) . toBe ( true ) ;
6565 } ) ;
66+ } ) ;
67+
68+ describe ( 'run function' , ( ) => {
69+ beforeEach ( ( ) => {
70+ jest . clearAllMocks ( ) ;
71+ nock . cleanAll ( ) ;
72+
73+ // Reset environment
74+ delete process . env . GITHUB_REPOSITORY ;
75+ delete process . env . NODE_ENV ;
76+
77+ // Mock core.getInput
78+ ( core . getInput as jest . Mock ) . mockImplementation ( ( name : string ) => {
79+ const inputs : Record < string , string > = {
80+ 'access-token' : 'test-token' ,
81+ 'repo' : 'test-repo' ,
82+ 'branch' : 'test-branch' ,
83+ 'base-uri' : 'https://app.launchdarkly.com' ,
84+ 'force' : 'false'
85+ } ;
86+ return inputs [ name ] || '' ;
87+ } ) ;
88+
89+ // Mock core.getBooleanInput
90+ ( core . getBooleanInput as jest . Mock ) . mockImplementation ( ( name : string ) => {
91+ const inputs : Record < string , boolean > = {
92+ 'force' : false
93+ } ;
94+ return inputs [ name ] || false ;
95+ } ) ;
96+
97+ // Mock github context
98+ ( github . context as any ) = {
99+ eventName : 'delete' ,
100+ payload : {
101+ ref_type : 'branch' ,
102+ ref : 'test-branch' ,
103+ repository : {
104+ name : 'test-repo'
105+ }
106+ }
107+ } ;
108+ } ) ;
109+
110+ afterEach ( ( ) => {
111+ nock . cleanAll ( ) ;
112+ } ) ;
113+
114+ it ( 'should skip non-branch delete events by default' , async ( ) => {
115+ ( github . context as any ) . payload . ref_type = 'tag' ;
116+
117+ await run ( ) ;
118+
119+ expect ( core . info ) . toHaveBeenCalledWith ( 'Skipping non-branch delete event (ref_type=tag). Use force: true to override.' ) ;
120+ expect ( core . setFailed ) . not . toHaveBeenCalled ( ) ;
121+ } ) ;
122+
123+ it ( 'should proceed with non-branch delete events when force-delete is true' , async ( ) => {
124+ ( github . context as any ) . payload . ref_type = 'tag' ;
125+ ( core . getBooleanInput as jest . Mock ) . mockReturnValue ( true ) ;
126+
127+ const scope = nock ( 'https://app.launchdarkly.com' )
128+ . post ( '/api/v2/code-refs/repositories/test-repo/branch-delete-tasks' )
129+ . reply ( 200 , { success : true } ) ;
130+
131+ await run ( ) ;
132+
133+ expect ( core . info ) . toHaveBeenCalledWith ( "Deleting LaunchDarkly Code Refs branch 'test-branch' in repo 'test-repo'..." ) ;
134+ expect ( core . info ) . toHaveBeenCalledWith ( '✅ LaunchDarkly Code Refs branch delete queued successfully.' ) ;
135+ expect ( scope . isDone ( ) ) . toBe ( true ) ;
136+ } ) ;
137+
138+ it ( 'should proceed with branch delete events normally' , async ( ) => {
139+ ( github . context as any ) . payload . ref_type = 'branch' ;
140+
141+ const scope = nock ( 'https://app.launchdarkly.com' )
142+ . post ( '/api/v2/code-refs/repositories/test-repo/branch-delete-tasks' )
143+ . reply ( 200 , { success : true } ) ;
144+
145+ await run ( ) ;
146+
147+ expect ( core . info ) . toHaveBeenCalledWith ( "Deleting LaunchDarkly Code Refs branch 'test-branch' in repo 'test-repo'..." ) ;
148+ expect ( core . info ) . toHaveBeenCalledWith ( '✅ LaunchDarkly Code Refs branch delete queued successfully.' ) ;
149+ expect ( scope . isDone ( ) ) . toBe ( true ) ;
150+ } ) ;
151+
152+ it ( 'should handle missing repository key' , async ( ) => {
153+ ( core . getInput as jest . Mock ) . mockImplementation ( ( name : string ) => {
154+ const inputs : Record < string , string > = {
155+ 'access-token' : 'test-token' ,
156+ 'branch' : 'test-branch'
157+ } ;
158+ return inputs [ name ] || '' ;
159+ } ) ;
160+
161+ ( github . context as any ) . payload . repository = null ;
162+ delete process . env . GITHUB_REPOSITORY ;
163+
164+ await run ( ) ;
165+
166+ expect ( core . setFailed ) . toHaveBeenCalledWith ( 'Action failed: Repository key not found' ) ;
167+ } ) ;
168+
169+ it ( 'should handle missing branch ref' , async ( ) => {
170+ ( core . getInput as jest . Mock ) . mockImplementation ( ( name : string ) => {
171+ const inputs : Record < string , string > = {
172+ 'access-token' : 'test-token' ,
173+ 'repo' : 'test-repo'
174+ } ;
175+ return inputs [ name ] || '' ;
176+ } ) ;
177+
178+ ( github . context as any ) . payload . ref = null ;
179+
180+ await run ( ) ;
181+
182+ expect ( core . setFailed ) . toHaveBeenCalledWith ( 'Action failed: Branch ref not found' ) ;
183+ } ) ;
66184} ) ;
0 commit comments