@@ -6,6 +6,7 @@ import * as Effect from "effect/Effect";
66import * as FileSystem from "effect/FileSystem" ;
77import * as Layer from "effect/Layer" ;
88import * as Option from "effect/Option" ;
9+ import * as Schema from "effect/Schema" ;
910import * as TestClock from "effect/testing/TestClock" ;
1011import {
1112 HttpClient ,
@@ -21,6 +22,8 @@ import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
2122import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts" ;
2223import type * as VcsDriver from "../vcs/VcsDriver.ts" ;
2324
25+ const isBitbucketRepositoryLocatorError = Schema . is ( BitbucketApi . BitbucketRepositoryLocatorError ) ;
26+
2427const bitbucketPullRequest = {
2528 id : 42 ,
2629 title : "Add Bitbucket provider" ,
@@ -104,7 +107,7 @@ function makeLayer(input: {
104107 } satisfies Partial < GitVcsDriver . GitVcsDriver [ "Service" ] > ;
105108
106109 const driver = {
107- listRemotes : ( ) =>
110+ listRemotes : vi . fn ( ( ) =>
108111 Effect . succeed ( {
109112 remotes : [
110113 {
@@ -120,8 +123,26 @@ function makeLayer(input: {
120123 expiresAt : Option . none ( ) ,
121124 } ,
122125 } ) ,
126+ ) ,
123127 } satisfies Partial < VcsDriver . VcsDriver [ "Service" ] > ;
124128
129+ const resolve = vi . fn ( ( ) =>
130+ Effect . succeed ( {
131+ kind : "git" as const ,
132+ repository : {
133+ kind : "git" as const ,
134+ rootPath : "/repo" ,
135+ metadataPath : null ,
136+ freshness : {
137+ source : "live-local" as const ,
138+ observedAt : DateTime . makeUnsafe ( "1970-01-01T00:00:00.000Z" ) ,
139+ expiresAt : Option . none ( ) ,
140+ } ,
141+ } ,
142+ driver : driver as unknown as VcsDriver . VcsDriver [ "Service" ] ,
143+ } ) ,
144+ ) ;
145+
125146 const layer = BitbucketApi . layer . pipe (
126147 Layer . provide (
127148 Layer . succeed (
@@ -131,21 +152,7 @@ function makeLayer(input: {
131152 ) ,
132153 Layer . provide (
133154 Layer . mock ( VcsDriverRegistry . VcsDriverRegistry ) ( {
134- resolve : ( ) =>
135- Effect . succeed ( {
136- kind : "git" ,
137- repository : {
138- kind : "git" ,
139- rootPath : "/repo" ,
140- metadataPath : null ,
141- freshness : {
142- source : "live-local" as const ,
143- observedAt : DateTime . makeUnsafe ( "1970-01-01T00:00:00.000Z" ) ,
144- expiresAt : Option . none ( ) ,
145- } ,
146- } ,
147- driver : driver as unknown as VcsDriver . VcsDriver [ "Service" ] ,
148- } ) ,
155+ resolve,
149156 } ) ,
150157 ) ,
151158 Layer . provide ( Layer . mock ( GitVcsDriver . GitVcsDriver ) ( git ) ) ,
@@ -164,7 +171,7 @@ function makeLayer(input: {
164171 Layer . provideMerge ( NodeServices . layer ) ,
165172 ) ;
166173
167- return { execute, git : gitMock , layer } ;
174+ return { execute, git : gitMock , resolve , listRemotes : driver . listRemotes , layer } ;
168175}
169176
170177it . effect ( "parses pull request responses from the Bitbucket REST API" , ( ) => {
@@ -338,6 +345,102 @@ it.effect("reads repository clone URLs and default branch", () => {
338345 } ) . pipe ( Effect . provide ( layer ) ) ;
339346} ) ;
340347
348+ it . effect . each ( [ false , true ] ) (
349+ "rejects invalid explicit repositories before fallback, context=%s" ,
350+ ( withContext ) => {
351+ const { execute, resolve, listRemotes, layer } = makeLayer ( {
352+ response : ( ) => Response . json ( repositoryJson ) ,
353+ } ) ;
354+
355+ return Effect . gen ( function * ( ) {
356+ const bitbucket = yield * BitbucketApi . BitbucketApi ;
357+ for ( const repository of [ "t3code" , "" , " \t " ] ) {
358+ const error = yield * bitbucket
359+ . getRepositoryCloneUrls ( {
360+ cwd : "/repo" ,
361+ repository,
362+ ...( withContext
363+ ? {
364+ context : {
365+ provider : {
366+ kind : "bitbucket" as const ,
367+ name : "Bitbucket" ,
368+ baseUrl : "https://bitbucket.org" ,
369+ } ,
370+ remoteName : "origin" ,
371+ remoteUrl :
"[email protected] :pingdotgg/t3code.git" , 372+ } ,
373+ }
374+ : { } ) ,
375+ } )
376+ . pipe ( Effect . match ( { onFailure : ( error ) => error , onSuccess : ( ) => null } ) ) ;
377+
378+ assert . instanceOf ( error , BitbucketApi . BitbucketRepositoryLocatorError ) ;
379+ assert . strictEqual (
380+ isBitbucketRepositoryLocatorError ( error ) ? error . repository : null ,
381+ repository ,
382+ ) ;
383+ assert . strictEqual ( resolve . mock . calls . length , 0 ) ;
384+ assert . strictEqual ( listRemotes . mock . calls . length , 0 ) ;
385+ assert . strictEqual ( execute . mock . calls . length , 0 ) ;
386+ }
387+ } ) . pipe ( Effect . provide ( layer ) ) ;
388+ } ,
389+ ) ;
390+
391+ it . effect ( "prefers an explicit repository and uses context when the repository is omitted" , ( ) => {
392+ const { execute, resolve, listRemotes, layer } = makeLayer ( {
393+ response : ( request ) =>
394+ Response . json ( request . url . endsWith ( "/branching-model" ) ? { } : repositoryJson ) ,
395+ } ) ;
396+
397+ return Effect . gen ( function * ( ) {
398+ const bitbucket = yield * BitbucketApi . BitbucketApi ;
399+ const context = {
400+ provider : { kind : "bitbucket" as const , name : "Bitbucket" , baseUrl : "https://bitbucket.org" } ,
401+ remoteName : "origin" ,
402+ remoteUrl :
"[email protected] :another/context.git" , 403+ } ;
404+ for ( const repository of [ "pingdotgg/t3code" , "https://bitbucket.org/pingdotgg/t3code.git" ] ) {
405+ yield * bitbucket . getRepositoryCloneUrls ( { cwd : "/repo" , context, repository } ) ;
406+ }
407+ yield * bitbucket . getDefaultBranch ( { cwd : "/repo" , context } ) ;
408+
409+ assert . deepStrictEqual (
410+ execute . mock . calls . map ( ( [ request ] ) => request . url ) . toSorted ( ) ,
411+ [
412+ "https://api.test.local/2.0/repositories/pingdotgg/t3code" ,
413+ "https://api.test.local/2.0/repositories/pingdotgg/t3code" ,
414+ "https://api.test.local/2.0/repositories/another/context" ,
415+ "https://api.test.local/2.0/repositories/another/context/branching-model" ,
416+ ] . toSorted ( ) ,
417+ ) ;
418+ assert . strictEqual ( resolve . mock . calls . length , 0 ) ;
419+ assert . strictEqual ( listRemotes . mock . calls . length , 0 ) ;
420+ } ) . pipe ( Effect . provide ( layer ) ) ;
421+ } ) ;
422+
423+ it . effect ( "discovers the repository from cwd remotes when the repository is omitted" , ( ) => {
424+ const { execute, resolve, listRemotes, layer } = makeLayer ( {
425+ response : ( request ) =>
426+ Response . json ( request . url . endsWith ( "/branching-model" ) ? { } : repositoryJson ) ,
427+ } ) ;
428+
429+ return Effect . gen ( function * ( ) {
430+ const bitbucket = yield * BitbucketApi . BitbucketApi ;
431+ const branch = yield * bitbucket . getDefaultBranch ( { cwd : "/repo" } ) ;
432+
433+ assert . strictEqual ( branch , "main" ) ;
434+ assert . deepStrictEqual ( resolve . mock . calls , [ [ { cwd : "/repo" } ] ] ) ;
435+ assert . deepStrictEqual ( listRemotes . mock . calls , [ [ "/repo" ] ] ) ;
436+ assert . isTrue (
437+ execute . mock . calls . every ( ( [ request ] ) =>
438+ request . url . startsWith ( "https://api.test.local/2.0/repositories/pingdotgg/t3code" ) ,
439+ ) ,
440+ ) ;
441+ } ) . pipe ( Effect . provide ( layer ) ) ;
442+ } ) ;
443+
341444it . effect (
342445 "prefers the Bitbucket branching model development branch as the default PR target" ,
343446 ( ) => {
0 commit comments