1- import { afterEach , assert , expect , it , vi } from "@effect/vitest" ;
1+ import { afterEach , assert , describe , expect , it , vi } from "@effect/vitest" ;
22import * as Effect from "effect/Effect" ;
33import * as Layer from "effect/Layer" ;
44import { ChildProcessSpawner } from "effect/process" ;
@@ -965,39 +965,42 @@ layer("GitLabPullRequestCli.layer", (it) => {
965965
966966 it . effect ( "shows a failed child pipeline that left the head pipeline green" , ( ) =>
967967 Effect . gen ( function * ( ) {
968- mockedExecute
969- . mockReturnValueOnce (
970- Effect . succeed (
971- output (
972- mergeRequestJson ( {
973- // A fork's pipeline runs in the source project, so its id is not the target's.
974- head_pipeline : {
975- id : 31 ,
976- project_id : 12 ,
977- status : "success" ,
978- web_url : "https://gitlab.com/bilal/web/-/pipelines/31" ,
979- } ,
980- } ) ,
981- ) ,
982- ) ,
983- )
984- . mockReturnValueOnce (
985- Effect . succeed (
986- output (
987- JSON . stringify ( [
988- {
989- name : "deploy" ,
990- status : "success" ,
991- downstream_pipeline : {
992- status : "failed" ,
993- web_url : "https://gitlab.com/bilal/web/-/pipelines/32" ,
968+ mockedExecute . mockImplementation ( ( request ) => {
969+ switch ( request . args [ 1 ] ) {
970+ case "graphql" :
971+ return Effect . succeed ( output ( "{}" ) ) ;
972+ case "projects/12/pipelines/31/bridges?per_page=100" :
973+ return Effect . succeed (
974+ output (
975+ JSON . stringify ( [
976+ {
977+ name : "deploy" ,
978+ status : "success" ,
979+ downstream_pipeline : {
980+ status : "failed" ,
981+ web_url : "https://gitlab.com/bilal/web/-/pipelines/32" ,
982+ } ,
994983 } ,
995- } ,
996- { name : "docs" , status : "created" , downstream_pipeline : null } ,
997- ] ) ,
998- ) ,
999- ) ,
1000- ) ;
984+ { name : "docs" , status : "created" , downstream_pipeline : null } ,
985+ ] ) ,
986+ ) ,
987+ ) ;
988+ default :
989+ return Effect . succeed (
990+ output (
991+ mergeRequestJson ( {
992+ // A fork's pipeline runs in the source project, so its id is not the target's.
993+ head_pipeline : {
994+ id : 31 ,
995+ project_id : 12 ,
996+ status : "success" ,
997+ web_url : "https://gitlab.com/bilal/web/-/pipelines/31" ,
998+ } ,
999+ } ) ,
1000+ ) ,
1001+ ) ;
1002+ }
1003+ } ) ;
10011004 const cli = yield * GitLabPullRequestCli . GitLabPullRequestCli ;
10021005
10031006 const detail = yield * cli . getMergeRequestDetail ( {
@@ -1010,7 +1013,6 @@ layer("GitLabPullRequestCli.layer", (it) => {
10101013 [ "Pipeline" , "success" , "https://gitlab.com/bilal/web/-/pipelines/31" ] ,
10111014 [ "deploy" , "failure" , "https://gitlab.com/bilal/web/-/pipelines/32" ] ,
10121015 ] ) ;
1013- expect ( argsOfCall ( 1 ) [ 1 ] ) . toBe ( "projects/12/pipelines/31/bridges?per_page=100" ) ;
10141016 } ) ,
10151017 ) ;
10161018
@@ -1020,9 +1022,11 @@ layer("GitLabPullRequestCli.layer", (it) => {
10201022 mergeRequestJson ( { head_pipeline : { id : 31 , project_id : 12 , status : "success" } } ) ,
10211023 ) ;
10221024 const failedChild = { name : "deploy" , downstream_pipeline : { status : "failed" } } ;
1023- mockedExecute
1024- . mockReturnValueOnce ( Effect . succeed ( detailOutput ) )
1025- . mockReturnValueOnce (
1025+ const cli = yield * GitLabPullRequestCli . GitLabPullRequestCli ;
1026+
1027+ for ( const [ scenario , bridges ] of [
1028+ [
1029+ "read fails" ,
10261030 Effect . fail (
10271031 new GitLabCli . GitLabCliCommandError ( {
10281032 operation : "execute" ,
@@ -1031,15 +1035,20 @@ layer("GitLabPullRequestCli.layer", (it) => {
10311035 cause : new Error ( "403 Forbidden" ) ,
10321036 } ) ,
10331037 ) ,
1034- )
1035- . mockReturnValueOnce ( Effect . succeed ( detailOutput ) )
1038+ ] ,
10361039 // A full page may have left out the trigger job whose pipeline failed.
1037- . mockReturnValueOnce (
1040+ [
1041+ "page is full" ,
10381042 Effect . succeed ( output ( JSON . stringify ( Array . from ( { length : 100 } , ( ) => failedChild ) ) ) ) ,
1043+ ] ,
1044+ ] as const ) {
1045+ mockedExecute . mockImplementation ( ( request ) =>
1046+ request . args [ 1 ] === "graphql"
1047+ ? Effect . succeed ( output ( "{}" ) )
1048+ : request . args [ 1 ] ?. includes ( "/bridges" )
1049+ ? bridges
1050+ : Effect . succeed ( detailOutput ) ,
10391051 ) ;
1040- const cli = yield * GitLabPullRequestCli . GitLabPullRequestCli ;
1041-
1042- for ( const scenario of [ "read fails" , "page is full" ] ) {
10431052 const detail = yield * cli . getMergeRequestDetail ( {
10441053 cwd : "/w" ,
10451054 repository : "acme/web" ,
@@ -1388,4 +1397,124 @@ layer("GitLabPullRequestCli.layer", (it) => {
13881397 expect ( result ) . toMatchObject ( { _tag : "Failure" , failure : error } ) ;
13891398 } ) ,
13901399 ) ;
1400+ describe ( "the head pipeline's stages" , ( ) => {
1401+ const pipeline = {
1402+ id : 9 ,
1403+ project_id : 12 ,
1404+ status : "failed" ,
1405+ web_url : "https://gitlab.com/acme/web/-/pipelines/9" ,
1406+ source : "merge_request_event" ,
1407+ } ;
1408+ const stages = JSON . stringify ( {
1409+ data : {
1410+ project : {
1411+ mergeRequest : {
1412+ headPipeline : {
1413+ path : "/acme/web/-/pipelines/9" ,
1414+ stages : {
1415+ nodes : [
1416+ { name : "build" , status : "success" , detailedStatus : { label : "passed" } } ,
1417+ {
1418+ name : "test" ,
1419+ status : "failed" ,
1420+ jobs : {
1421+ nodes : [ { name : "unit" , status : "FAILED" , webPath : "/acme/web/-/jobs/3" } ] ,
1422+ } ,
1423+ } ,
1424+ ] ,
1425+ } ,
1426+ } ,
1427+ } ,
1428+ } ,
1429+ } ,
1430+ } ) ;
1431+
1432+ function answer ( graphql : Effect . Effect < ReturnType < typeof output > , GitLabCli . GitLabCliError > ) {
1433+ mockedExecute . mockImplementation ( ( request ) =>
1434+ request . args [ 1 ] === "graphql"
1435+ ? graphql
1436+ : request . args [ 1 ] ?. includes ( "/bridges" )
1437+ ? Effect . succeed ( output ( "[]" ) )
1438+ : Effect . succeed ( output ( mergeRequestJson ( { head_pipeline : pipeline } ) ) ) ,
1439+ ) ;
1440+ }
1441+
1442+ const read = ( downstreamChecks : boolean ) =>
1443+ GitLabPullRequestCli . GitLabPullRequestCli . pipe (
1444+ Effect . flatMap ( ( cli ) =>
1445+ cli . getMergeRequestDetail ( {
1446+ cwd : "/w" ,
1447+ repository : "group/acme/web" ,
1448+ number : 7 ,
1449+ downstreamChecks,
1450+ } ) ,
1451+ ) ,
1452+ ) ;
1453+
1454+ it . effect ( "shows the stages, and the failed stage as its failed job" , ( ) =>
1455+ Effect . gen ( function * ( ) {
1456+ answer ( Effect . succeed ( output ( stages ) ) ) ;
1457+
1458+ const detail = yield * read ( true ) ;
1459+
1460+ const graphql = mockedExecute . mock . calls . find ( ( [ call ] ) => call . args [ 1 ] === "graphql" ) ;
1461+ assert . isDefined ( graphql ) ;
1462+ expect ( JSON . parse ( graphql [ 0 ] . stdin ?? "" ) . variables ) . toEqual ( {
1463+ fullPath : "group/acme/web" ,
1464+ iid : "7" ,
1465+ } ) ;
1466+ expect ( detail . checks ) . toEqual ( [
1467+ {
1468+ name : "build" ,
1469+ status : "success" ,
1470+ description : "passed" ,
1471+ url : "https://gitlab.com/acme/web/-/pipelines/9" ,
1472+ } ,
1473+ {
1474+ name : "test / unit" ,
1475+ status : "failure" ,
1476+ description : null ,
1477+ url : "https://gitlab.com/acme/web/-/jobs/3" ,
1478+ } ,
1479+ ] ) ;
1480+ } ) ,
1481+ ) ;
1482+
1483+ it . effect ( "keeps the pipeline check where the stages cannot be read" , ( ) =>
1484+ Effect . gen ( function * ( ) {
1485+ answer (
1486+ Effect . fail (
1487+ new GitLabCli . GitLabCliCommandError ( {
1488+ operation : "execute" ,
1489+ command : "glab" ,
1490+ cwd : "/w" ,
1491+ cause : new Error ( "graphql unavailable" ) ,
1492+ } ) ,
1493+ ) ,
1494+ ) ;
1495+
1496+ const detail = yield * read ( true ) ;
1497+
1498+ expect ( detail . checks ) . toEqual ( [
1499+ {
1500+ name : "Pipeline" ,
1501+ status : "failure" ,
1502+ description : "merge_request_event" ,
1503+ url : "https://gitlab.com/acme/web/-/pipelines/9" ,
1504+ } ,
1505+ ] ) ;
1506+ } ) ,
1507+ ) ;
1508+
1509+ it . effect ( "asks for no stages when the caller only needs the merge request" , ( ) =>
1510+ Effect . gen ( function * ( ) {
1511+ answer ( Effect . succeed ( output ( stages ) ) ) ;
1512+
1513+ const detail = yield * read ( false ) ;
1514+
1515+ expect ( mockedExecute ) . toHaveBeenCalledTimes ( 1 ) ;
1516+ expect ( detail . checks . map ( ( check ) => check . name ) ) . toEqual ( [ "Pipeline" ] ) ;
1517+ } ) ,
1518+ ) ;
1519+ } ) ;
13911520} ) ;
0 commit comments