1- import { describe , it , before , afterEach } from 'node:test' ;
1+ import { describe , it , before , afterEach , beforeEach } from 'node:test' ;
22import assert from 'assert' ;
33
44import * as sinon from 'sinon' ;
@@ -13,6 +13,9 @@ import {
1313import PRChecker from '../../lib/pr_checker.js' ;
1414
1515import TestCLI from '../fixtures/test_cli.js' ;
16+ import { PRBuild } from '../../lib/ci/build-types/pr_build.js' ;
17+ import { JobParser } from '../../lib/ci/ci_type_parser.js' ;
18+ import PRData from '../../lib/pr_data.js' ;
1619
1720describe ( 'Jenkins' , ( ) => {
1821 const owner = 'nodejs' ;
@@ -199,4 +202,129 @@ describe('Jenkins', () => {
199202 } ) ;
200203 }
201204 } ) ;
205+
206+ describe ( '--check-for-duplicates' , { concurrency : false } , ( ) => {
207+ beforeEach ( ( ) => {
208+ sinon . replace ( PRData . prototype , 'getComments' , sinon . fake . resolves ( ) ) ;
209+ sinon . replace ( PRData . prototype , 'getPR' , sinon . fake . resolves ( ) ) ;
210+ sinon . replace ( JobParser . prototype , 'parse' ,
211+ sinon . fake . returns ( new Map ( ) . set ( 'PR' , { jobid : 123456 } ) ) ) ;
212+ } ) ;
213+ afterEach ( ( ) => {
214+ sinon . restore ( ) ;
215+ } ) ;
216+
217+ const getParameters = ( commitHash ) =>
218+ [
219+ {
220+ _class : 'hudson.model.BooleanParameterValue' ,
221+ name : 'CERTIFY_SAFE' ,
222+ value : true
223+ } ,
224+ {
225+ _class : 'hudson.model.StringParameterValue' ,
226+ name : 'COMMIT_SHA_CHECK' ,
227+ value : commitHash
228+ } ,
229+ {
230+ _class : 'hudson.model.StringParameterValue' ,
231+ name : 'TARGET_GITHUB_ORG' ,
232+ value : 'nodejs'
233+ } ,
234+ {
235+ _class : 'hudson.model.StringParameterValue' ,
236+ name : 'TARGET_REPO_NAME' ,
237+ value : 'node'
238+ } ,
239+ {
240+ _class : 'hudson.model.StringParameterValue' ,
241+ name : 'PR_ID' ,
242+ value : prid
243+ } ,
244+ {
245+ _class : 'hudson.model.StringParameterValue' ,
246+ name : 'REBASE_ONTO' ,
247+ value : '<pr base branch>'
248+ } ,
249+ {
250+ _class : 'com.wangyin.parameter.WHideParameterValue' ,
251+ name : 'DESCRIPTION_SETTER_DESCRIPTION' ,
252+ value : ''
253+ }
254+ ] ;
255+ const mockJenkinsResponse = parameters => ( {
256+ _class : 'com.tikal.jenkins.plugins.multijob.MultiJobBuild' ,
257+ actions : [
258+ { _class : 'hudson.model.CauseAction' } ,
259+ { _class : 'hudson.model.ParametersAction' , parameters } ,
260+ { _class : 'hudson.model.ParametersAction' , parameters } ,
261+ { _class : 'hudson.model.ParametersAction' , parameters } ,
262+ { } ,
263+ { _class : 'hudson.model.CauseAction' } ,
264+ { } ,
265+ { } ,
266+ { } ,
267+ { } ,
268+ { _class : 'hudson.plugins.git.util.BuildData' } ,
269+ { } ,
270+ { } ,
271+ { } ,
272+ { } ,
273+ { _class : 'hudson.model.ParametersAction' , parameters } ,
274+ {
275+ _class : 'hudson.plugins.parameterizedtrigger.BuildInfoExporterAction'
276+ } ,
277+ {
278+ _class : 'com.tikal.jenkins.plugins.multijob.MultiJobTestResults'
279+ } ,
280+ { } ,
281+ { } ,
282+ { } ,
283+ { } ,
284+ { } ,
285+ { } ,
286+ { } ,
287+ { } ,
288+ {
289+ _class : 'org.jenkinsci.plugins.displayurlapi.actions.RunDisplayAction'
290+ }
291+ ]
292+ } ) ;
293+
294+ it ( 'should return false if already started' , async ( ) => {
295+ const cli = new TestCLI ( ) ;
296+ sinon . replace ( PRBuild . prototype , 'getBuildData' ,
297+ sinon . fake . resolves ( mockJenkinsResponse ( getParameters ( 'deadbeef' ) ) ) ) ;
298+
299+ const jobRunner = new RunPRJob ( cli , { } , owner , repo , prid , 'deadbeef' , true ) ;
300+ assert . strictEqual ( await jobRunner . start ( ) , false ) ;
301+ } ) ;
302+ it ( 'should return true when last CI is on a different commit' , async ( ) => {
303+ const cli = new TestCLI ( ) ;
304+ sinon . replace ( PRBuild . prototype , 'getBuildData' ,
305+ sinon . fake . resolves ( mockJenkinsResponse ( getParameters ( '123456789abcdef' ) ) ) ) ;
306+
307+ const request = {
308+ gql : sinon . stub ( ) . returns ( {
309+ repository : {
310+ pullRequest : {
311+ labels : {
312+ nodes : [ ]
313+ }
314+ }
315+ }
316+ } ) ,
317+ fetch : sinon . stub ( )
318+ . callsFake ( ( url , { method, headers, body } ) => {
319+ assert . strictEqual ( url , CI_PR_URL ) ;
320+ assert . strictEqual ( method , 'POST' ) ;
321+ assert . deepStrictEqual ( headers , { 'Jenkins-Crumb' : crumb } ) ;
322+ return Promise . resolve ( { status : 201 } ) ;
323+ } ) ,
324+ json : sinon . stub ( ) . withArgs ( CI_CRUMB_URL ) . resolves ( { crumb } )
325+ } ;
326+ const jobRunner = new RunPRJob ( cli , request , owner , repo , prid , 'deadbeef' , true ) ;
327+ assert . strictEqual ( await jobRunner . start ( ) , true ) ;
328+ } ) ;
329+ } ) ;
202330} ) ;
0 commit comments