11import { describe , expect , it } from "@effect/vitest" ;
22import * as Effect from "effect/Effect" ;
33import * as Layer from "effect/Layer" ;
4+ import type { PullRequestCheck , PullRequestCheckStatus } from "@t3tools/contracts" ;
45
56import * as AzureDevOpsPullRequestCli from "./AzureDevOpsPullRequestCli.ts" ;
67import {
@@ -129,6 +130,7 @@ describe("getChangeRequest", () => {
129130 Effect . provide (
130131 Layer . mock ( AzureDevOpsPullRequestCli . AzureDevOpsPullRequestCli ) ( {
131132 getPullRequest : ( ) => Effect . succeed ( PULL_REQUEST ) ,
133+ listPolicyChecks : ( ) => Effect . succeed ( [ ] ) ,
132134 listIterations : ( ) =>
133135 Effect . succeed ( [ ITERATION , { ...ITERATION , id : 4 , headCommit : "new-head" } ] ) ,
134136 listIterationChanges : ( ) =>
@@ -163,6 +165,7 @@ describe("getChangeRequest", () => {
163165 Effect . provide (
164166 Layer . mock ( AzureDevOpsPullRequestCli . AzureDevOpsPullRequestCli ) ( {
165167 getPullRequest : ( ) => Effect . succeed ( PULL_REQUEST ) ,
168+ listPolicyChecks : ( ) => Effect . succeed ( [ ] ) ,
166169 listIterations : ( ) =>
167170 state === "missing"
168171 ? Effect . succeed ( [ ] )
@@ -189,6 +192,107 @@ describe("getChangeRequest", () => {
189192 ) ;
190193} ) ;
191194
195+ describe ( "policy checks" , ( ) => {
196+ const REPOSITORY = {
197+ cwd : "/w" ,
198+ repository : "acme/web" ,
199+ host : "dev.azure.com" ,
200+ number : 7 ,
201+ } ;
202+ const check = ( status : PullRequestCheckStatus ) : PullRequestCheck => ( {
203+ name : "PR validation" ,
204+ status,
205+ description : null ,
206+ url : null ,
207+ required : true ,
208+ } ) ;
209+ const readFailure = new AzureDevOpsPullRequestCli . AzureDevOpsPullRequestReadError ( {
210+ command : "az" ,
211+ cwd : "/w" ,
212+ operation : "listPolicyChecks" ,
213+ cause : "unavailable" ,
214+ } ) ;
215+
216+ const provide = (
217+ listPolicyChecks : AzureDevOpsPullRequestCli . AzureDevOpsPullRequestCli [ "Service" ] [ "listPolicyChecks" ] ,
218+ pullRequest = PULL_REQUEST ,
219+ ) =>
220+ make . pipe (
221+ Effect . provide (
222+ Layer . mock ( AzureDevOpsPullRequestCli . AzureDevOpsPullRequestCli ) ( {
223+ getPullRequest : ( ) => Effect . succeed ( pullRequest ) ,
224+ listIterations : ( ) => Effect . succeed ( [ ITERATION ] ) ,
225+ listIterationChanges : ( ) => Effect . succeed ( { changes : [ ] , truncated : false } ) ,
226+ listPolicyChecks,
227+ } ) ,
228+ ) ,
229+ ) ;
230+
231+ it . effect ( "puts the pipeline on the detail, so a watch hears about it" , ( ) =>
232+ Effect . gen ( function * ( ) {
233+ const provider = yield * provide ( ( ) => Effect . succeed ( [ check ( "failure" ) ] ) ) ;
234+ const detail = yield * provider . getChangeRequest ( REPOSITORY ) ;
235+ expect ( detail . checks ) . toEqual ( [ check ( "failure" ) ] ) ;
236+ } ) ,
237+ ) ;
238+
239+ it . effect ( "keeps the detail readable when the policies are not" , ( ) =>
240+ Effect . gen ( function * ( ) {
241+ const provider = yield * provide ( ( ) => Effect . fail ( readFailure ) ) ;
242+ const detail = yield * provider . getChangeRequest ( REPOSITORY ) ;
243+ expect ( detail . checks ) . toEqual ( [ ] ) ;
244+ } ) ,
245+ ) ;
246+
247+ it . effect ( "answers a checks poll with the pull request's state" , ( ) =>
248+ Effect . gen ( function * ( ) {
249+ const provider = yield * provide ( ( ) => Effect . succeed ( [ check ( "pending" ) ] ) ) ;
250+ expect ( yield * provider . getChangeRequestChecks ! ( REPOSITORY ) ) . toEqual ( {
251+ state : "open" ,
252+ checks : [ check ( "pending" ) ] ,
253+ } ) ;
254+ } ) ,
255+ ) ;
256+
257+ it . effect . each ( [
258+ { statuses : [ "success" ] , expected : "passing" } ,
259+ { statuses : [ "success" , "pending" ] , expected : "pending" } ,
260+ { statuses : [ "pending" , "failure" ] , expected : "failing" } ,
261+ { statuses : [ "action-required" ] , expected : "pending" } ,
262+ { statuses : [ "neutral" ] , expected : null } ,
263+ { statuses : [ ] , expected : null } ,
264+ ] as const ) ( "summarizes $statuses as $expected" , ( { statuses, expected } ) =>
265+ Effect . gen ( function * ( ) {
266+ const provider = yield * provide ( ( ) => Effect . succeed ( statuses . map ( check ) ) ) ;
267+ const summary = yield * provider . getChangeRequestSummary ! ( REPOSITORY ) ;
268+ expect ( summary . checksState ) . toBe ( expected ) ;
269+ } ) ,
270+ ) ;
271+
272+ it . effect ( "leaves the last known state alone when the policies cannot be read" , ( ) =>
273+ Effect . gen ( function * ( ) {
274+ const provider = yield * provide ( ( ) => Effect . fail ( readFailure ) ) ;
275+ const summary = yield * provider . getChangeRequestSummary ! ( REPOSITORY ) ;
276+ expect ( summary . checksState ) . toBeUndefined ( ) ;
277+ } ) ,
278+ ) ;
279+
280+ it . effect ( "tells the policy read whether the pull request is a draft" , ( ) =>
281+ Effect . gen ( function * ( ) {
282+ const drafts : Array < boolean > = [ ] ;
283+ const provider = yield * provide (
284+ ( input ) => {
285+ drafts . push ( input . isDraft ) ;
286+ return Effect . succeed ( [ ] ) ;
287+ } ,
288+ { ...PULL_REQUEST , isDraft : true } ,
289+ ) ;
290+ yield * provider . getChangeRequestSummary ! ( REPOSITORY ) ;
291+ expect ( drafts ) . toEqual ( [ true ] ) ;
292+ } ) ,
293+ ) ;
294+ } ) ;
295+
192296describe ( "getDiff reads" , ( ) => {
193297 it . effect ( "holds every reader together to one request's worth of processes" , ( ) =>
194298 Effect . gen ( function * ( ) {
0 commit comments