@@ -12,6 +12,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
1212import type { GitLabUploadReference } from "@t3tools/contracts" ;
1313import * as GitLabUploadMedia from "./GitLabUploadMedia.ts" ;
1414import * as GitLabCli from "../sourceControl/GitLabCli.ts" ;
15+ import * as VcsProcess from "../vcs/VcsProcess.ts" ;
1516import * as ServerConfig from "../config.ts" ;
1617
1718const reference : GitLabUploadReference = {
@@ -33,6 +34,7 @@ function fixture(
3334 protocol ?: string ;
3435 endpoint ?: string ;
3536 token ?: ( ) => string ;
37+ environment ?: NodeJS . ProcessEnv ;
3638 response ?: (
3739 request : { url : string ; method : string ; headers : Readonly < Record < string , string > > } ,
3840 index : number ,
@@ -59,18 +61,30 @@ function fixture(
5961 } ) ;
6062 const layer = GitLabUploadMedia . layer . pipe (
6163 Layer . provide (
62- Layer . mock ( GitLabCli . GitLabCli ) ( {
63- execute : ( { args } ) => {
64- commands . push ( args ) ;
65- return Effect . succeed (
66- output (
67- args [ 0 ] === "config"
68- ? ( options . protocol ?? "https" )
69- : `REST API Endpoint: ${ options . endpoint ?? "https://api.example/api/v4/" } \nToken found in config file: ${ options . token ?.( ) ?? "private-credential" } ` ,
70- ) ,
71- ) ;
72- } ,
73- } ) ,
64+ GitLabCli . layer . pipe (
65+ Layer . provide (
66+ Layer . mock ( VcsProcess . VcsProcess ) ( {
67+ run : ( { args, env } ) => {
68+ commands . push ( args ) ;
69+ const environment = { ...options . environment , ...env } ;
70+ // glab gives ambient access tokens precedence over the selected host's stored token.
71+ const token =
72+ environment . GITLAB_TOKEN ||
73+ environment . GITLAB_ACCESS_TOKEN ||
74+ environment . OAUTH_TOKEN ||
75+ ( options . token ?.( ) ?? "private-credential" ) ||
76+ ( environment . GLAB_ENABLE_CI_AUTOLOGIN === "true" ? environment . CI_JOB_TOKEN : "" ) ;
77+ return Effect . succeed (
78+ output (
79+ args [ 0 ] === "config"
80+ ? ( options . protocol ?? "https" )
81+ : `REST API Endpoint: ${ options . endpoint ?? "https://api.example/api/v4/" } \nToken found in config file: ${ token } ` ,
82+ ) ,
83+ ) ;
84+ } ,
85+ } ) ,
86+ ) ,
87+ ) ,
7488 ) ,
7589 Layer . provide ( Layer . succeed ( HttpClient . HttpClient , client ) ) ,
7690 Layer . provide (
@@ -82,6 +96,27 @@ function fixture(
8296}
8397
8498describe ( "GitLabUploadMedia" , ( ) => {
99+ it . effect . each ( [ "GITLAB_TOKEN" , "GITLAB_ACCESS_TOKEN" , "OAUTH_TOKEN" , "CI_JOB_TOKEN" ] ) (
100+ "does not use ambient %s for an upload-selected host" ,
101+ ( key ) => {
102+ const environment = { [ key ] : "ambient-secret" , GLAB_ENABLE_CI_AUTOLOGIN : "true" } ;
103+ const configured = fixture ( { environment } ) ;
104+ const unconfigured = fixture ( { environment, token : ( ) => "" } ) ;
105+ return Effect . gen ( function * ( ) {
106+ yield * Effect . gen ( function * ( ) {
107+ const service = yield * GitLabUploadMedia . GitLabUploadMedia ;
108+ expect ( ( yield * service . respond ( reference , { } , "GET" ) ) . status ) . toBe ( 200 ) ;
109+ expect ( configured . requests [ 0 ] ?. headers [ "private-token" ] ) . toBe ( "private-credential" ) ;
110+ } ) . pipe ( Effect . provide ( configured . layer ) ) ;
111+ yield * Effect . gen ( function * ( ) {
112+ const service = yield * GitLabUploadMedia . GitLabUploadMedia ;
113+ expect ( ( yield * service . respond ( reference , { } , "GET" ) ) . status ) . toBe ( 502 ) ;
114+ expect ( unconfigured . requests ) . toHaveLength ( 0 ) ;
115+ } ) . pipe ( Effect . provide ( unconfigured . layer ) ) ;
116+ } ) . pipe ( Effect . scoped ) ;
117+ } ,
118+ ) ;
119+
85120 it . effect ( "streams decoded upload names through the HTTPS API and reuses the credential" , ( ) => {
86121 const f = fixture ( ) ;
87122 return Effect . gen ( function * ( ) {
0 commit comments