@@ -2,12 +2,23 @@ import { SourceControlProviderKind } from "@t3tools/contracts";
22import { assert , it } from "@effect/vitest" ;
33import * as NodeServices from "@effect/platform-node/NodeServices" ;
44import * as DateTime from "effect/DateTime" ;
5+ import * as Deferred from "effect/Deferred" ;
56import * as Effect from "effect/Effect" ;
7+ import * as Fiber from "effect/Fiber" ;
68import * as Layer from "effect/Layer" ;
79import * as Option from "effect/Option" ;
10+ import * as PlatformError from "effect/PlatformError" ;
11+ import * as PubSub from "effect/PubSub" ;
812import * as Redacted from "effect/Redacted" ;
13+ import * as Stream from "effect/Stream" ;
14+ import * as TestClock from "effect/testing/TestClock" ;
915import { ChildProcessSpawner } from "effect/process" ;
10- import { VcsRepositoryDetectionError } from "@t3tools/contracts" ;
16+ import {
17+ DEFAULT_SERVER_SETTINGS ,
18+ VcsProcessSpawnError ,
19+ VcsProcessTimeoutError ,
20+ VcsRepositoryDetectionError ,
21+ } from "@t3tools/contracts" ;
1122
1223import * as ServerSettings from "../serverSettings.ts" ;
1324import * as ServerConfig from "../config.ts" ;
@@ -54,6 +65,8 @@ function makeRegistry(input: {
5465 readonly process ?: Partial < VcsProcess . VcsProcess [ "Service" ] > ;
5566 readonly githubApi ?: Partial < GitHubApi . GitHubApi [ "Service" ] > ;
5667 readonly gitlab ?: Partial < GitLabCli . GitLabCli [ "Service" ] > ;
68+ readonly forgejo ?: Partial < ForgejoCli . ForgejoCli [ "Service" ] > ;
69+ readonly settings ?: Partial < ServerSettings . ServerSettingsService [ "Service" ] > ;
5770 readonly resolve ?: VcsDriverRegistry . VcsDriverRegistry [ "Service" ] [ "resolve" ] ;
5871} ) {
5972 const driver = {
@@ -118,7 +131,9 @@ function makeRegistry(input: {
118131 Layer . mock ( AzureDevOpsPullRequestCli . AzureDevOpsPullRequestCli ) ( { } ) ,
119132 Layer . mock ( BitbucketApi . BitbucketApi ) ( { } ) ,
120133 Layer . mock ( BitbucketPullRequestApi . BitbucketPullRequestApi ) ( { } ) ,
121- ServerSettings . ServerSettingsService . layerTest ( ) ,
134+ input . settings
135+ ? Layer . mock ( ServerSettings . ServerSettingsService ) ( input . settings )
136+ : ServerSettings . ServerSettingsService . layerTest ( ) ,
122137 Layer . mock ( GitHubPullRequestApi . GitHubPullRequestApi ) ( { } ) ,
123138 Layer . mock ( GitHubApi . GitHubApi ) ( {
124139 // No GitHub credential unless a test supplies one, so custom hosts stay unclaimed.
@@ -128,7 +143,10 @@ function makeRegistry(input: {
128143 Layer . mock ( GitVcsDriver . GitVcsDriver ) ( { } ) ,
129144 Layer . mock ( GitLabCli . GitLabCli ) ( input . gitlab ?? { } ) ,
130145 Layer . mock ( GitLabPullRequestCli . GitLabPullRequestCli ) ( { } ) ,
131- Layer . mock ( ForgejoCli . ForgejoCli ) ( { listLogins : ( ) => Effect . succeed ( [ ] ) } ) ,
146+ Layer . mock ( ForgejoCli . ForgejoCli ) ( {
147+ listLogins : ( ) => Effect . succeed ( [ ] ) ,
148+ ...input . forgejo ,
149+ } ) ,
132150 Layer . mock ( GitCafeApi . GitCafeApi ) ( { } ) ,
133151 Layer . mock ( GitCafeCredentials . GitCafeCredentials ) ( {
134152 cliEnv : ( ) => Effect . succeed ( { } ) ,
@@ -459,3 +477,150 @@ it.effect("skips GitHub discovery for the identity resolver's empty base URL", (
459477 assert . deepStrictEqual ( hosts , [ "githubenterprise.dev.example.com" ] ) ;
460478 } ) ,
461479) ;
480+
481+ const notFound = ( command : string , cwd : string ) =>
482+ new VcsProcessSpawnError ( {
483+ operation : "test" ,
484+ command,
485+ cwd,
486+ cause : PlatformError . systemError ( { _tag : "NotFound" , module : "ChildProcess" , method : "spawn" } ) ,
487+ } ) ;
488+
489+ const resolveUnknown = (
490+ registry : SourceControlProviderRegistry . SourceControlProviderRegistry [ "Service" ] ,
491+ repository : string ,
492+ ) =>
493+ registry . resolveHandle ( {
494+ cwd : `/${ repository } ` ,
495+ context : {
496+ provider : {
497+ kind : SourceControlProviderKind . make ( "unknown" ) ,
498+ name : "git.example.internal" ,
499+ baseUrl : "https://git.example.internal" ,
500+ } ,
501+ remoteName : "origin" ,
502+ remoteUrl :
`[email protected] :team/${ repository } .git` , 503+ } ,
504+ } ) ;
505+
506+ it . effect ( "asks once for every checkout of a server no integration claims" , ( ) =>
507+ Effect . gen ( function * ( ) {
508+ const asked : Array < string > = [ ] ;
509+ const release = yield * Deferred . make < void > ( ) ;
510+ const registry = yield * makeRegistry ( {
511+ remotes : [ ] ,
512+ process : {
513+ run : ( { command, args, cwd } ) => {
514+ asked . push ( [ command , ...args ] . join ( " " ) ) ;
515+ return Deferred . await ( release ) . pipe ( Effect . andThen ( Effect . fail ( notFound ( command , cwd ) ) ) ) ;
516+ } ,
517+ } ,
518+ githubApi : {
519+ credential : ( host ) => {
520+ asked . push ( `credential ${ host } ` ) ;
521+ return Effect . fail ( new GitHubCredentials . GitHubNotSignedInError ( { host } ) ) ;
522+ } ,
523+ } ,
524+ forgejo : {
525+ listLogins : ( { command = "tea" , cwd } ) => {
526+ asked . push ( command ) ;
527+ const cause = notFound ( command , cwd ) ;
528+ return Effect . fail ( new ForgejoCli . ForgejoCliError ( { command, cwd, detail : "" , cause } ) ) ;
529+ } ,
530+ } ,
531+ } ) ;
532+
533+ // Three checkouts read while the first probe is still running share it.
534+ const started = yield * Effect . forEach ( [ "a" , "b" , "c" ] , ( ) => Deferred . make < void > ( ) ) ;
535+ const callers = yield * Effect . forEach (
536+ [ "a" , "b" , "c" ] ,
537+ ( repository , index ) =>
538+ Deferred . succeed ( started [ index ] ! , undefined ) . pipe (
539+ Effect . andThen ( resolveUnknown ( registry , repository ) ) ,
540+ ) ,
541+ { concurrency : "unbounded" } ,
542+ ) . pipe ( Effect . forkChild ) ;
543+ yield * Effect . forEach ( started , Deferred . await ) ;
544+ yield * Deferred . succeed ( release , undefined ) ;
545+ const handles = yield * Fiber . join ( callers ) ;
546+ yield * resolveUnknown ( registry , "d" ) ;
547+ assert . deepStrictEqual (
548+ handles . map ( ( handle ) => handle . provider . kind ) ,
549+ [ "unknown" , "unknown" , "unknown" ] ,
550+ ) ;
551+ const once = asked . slice ( ) ;
552+ // glab is asked about the remote's host as the remote spells it.
553+ assert . sameMembers ( once , [
554+ "credential git.example.internal" ,
555+ "glab auth status --hostname Git.Example.Internal" ,
556+ "fj" ,
557+ "tea" ,
558+ ] ) ;
559+
560+ // A login made in a terminal sends no signal, so the server is asked again later.
561+ yield * TestClock . adjust ( "5 minutes" ) ;
562+ yield * resolveUnknown ( registry , "a" ) ;
563+ assert . deepStrictEqual ( asked , [ ...once , ...once ] ) ;
564+ } ) ,
565+ ) ;
566+
567+ it . effect ( "asks again after a check that could not answer" , ( ) =>
568+ Effect . gen ( function * ( ) {
569+ let asks = 0 ;
570+ const registry = yield * makeRegistry ( {
571+ remotes : [ ] ,
572+ process : {
573+ run : ( { command, cwd } ) => {
574+ asks += 1 ;
575+ return Effect . fail (
576+ new VcsProcessTimeoutError ( { operation : "test" , command, cwd, timeoutMs : 1 } ) ,
577+ ) ;
578+ } ,
579+ } ,
580+ } ) ;
581+ yield * resolveUnknown ( registry , "a" ) ;
582+ yield * resolveUnknown ( registry , "b" ) ;
583+ assert . strictEqual ( asks , 2 ) ;
584+ } ) ,
585+ ) ;
586+
587+ it . effect ( "asks again once Settings change" , ( ) =>
588+ Effect . gen ( function * ( ) {
589+ const changes = yield * PubSub . unbounded < typeof DEFAULT_SERVER_SETTINGS > ( ) ;
590+ const handled = yield * Deferred . make < void > ( ) ;
591+ let signedIn = false ;
592+ const registry = yield * makeRegistry ( {
593+ remotes : [ ] ,
594+ settings : {
595+ subscribeChanges : PubSub . subscribe ( changes ) . pipe (
596+ Effect . map ( ( subscription ) =>
597+ // A change is handled once the registry asks for the next one.
598+ Stream . fromSubscription ( subscription ) . pipe (
599+ Stream . flatMap ( ( settings ) =>
600+ Stream . concat (
601+ Stream . make ( settings ) ,
602+ Stream . fromEffectDrain ( Deferred . succeed ( handled , undefined ) ) ,
603+ ) ,
604+ ) ,
605+ ) ,
606+ ) ,
607+ ) ,
608+ } ,
609+ process : { run : ( { command, cwd } ) => Effect . fail ( notFound ( command , cwd ) ) } ,
610+ githubApi : {
611+ credential : ( host ) =>
612+ signedIn
613+ ? Effect . succeed ( { token : Redacted . make ( "token" ) , fingerprint : `${ host } :fingerprint` } )
614+ : Effect . fail ( new GitHubCredentials . GitHubNotSignedInError ( { host } ) ) ,
615+ } ,
616+ } ) ;
617+ assert . strictEqual ( ( yield * resolveUnknown ( registry , "a" ) ) . provider . kind , "unknown" ) ;
618+ signedIn = true ;
619+ assert . strictEqual ( ( yield * resolveUnknown ( registry , "b" ) ) . provider . kind , "unknown" ) ;
620+
621+ // A token saved for the host in Settings.
622+ yield * PubSub . publish ( changes , DEFAULT_SERVER_SETTINGS ) ;
623+ yield * Deferred . await ( handled ) ;
624+ assert . strictEqual ( ( yield * resolveUnknown ( registry , "c" ) ) . provider . kind , "github" ) ;
625+ } ) ,
626+ ) ;
0 commit comments