@@ -14,6 +14,120 @@ const asset = (url: string) => ({
1414} ) ;
1515
1616describe ( "repository GitHub media credentials" , ( ) => {
17+ it . effect . each ( [
18+ { fallback : false , enabled : true , unsupportedPrefix : false } ,
19+ { fallback : true , enabled : true , unsupportedPrefix : false } ,
20+ { fallback : true , enabled : false , unsupportedPrefix : false } ,
21+ { fallback : true , enabled : true , unsupportedPrefix : true } ,
22+ ] ) (
23+ "uses LFS repository context through redirects: fallback $fallback, opt-in $enabled, unsupported prefix $unsupportedPrefix" ,
24+ ( { fallback, enabled, unsupportedPrefix } ) => {
25+ const scopes : Array < string | undefined > = [ ] ;
26+ const authorizations : Array < string | undefined > = [ ] ;
27+ const redirects = [
28+ "https://raw.githubusercontent.com/OwnerA/Repo/main/image.png" ,
29+ "https://media.githubusercontent.com/media/ownera/repo/main/image.png" ,
30+ unsupportedPrefix
31+ ? "https://media.githubusercontent.com/OwnerA/Repo/main/image.png"
32+ : "https://media.githubusercontent.com/media/OwnerB/Repo/main/image.png" ,
33+ ] ;
34+ return Effect . gen ( function * ( ) {
35+ const response = yield * githubMediaResponse (
36+ asset ( "https://media.githubusercontent.com/media/OwnerA/Repo/main/image.png" ) ,
37+ { } ,
38+ ) ;
39+ expect ( response . status ) . toBe ( 200 ) ;
40+ expect ( scopes ) . toEqual ( [ "OwnerA/Repo" ] ) ;
41+ expect ( authorizations ) . toEqual ( [
42+ "Bearer fake-owner-a" ,
43+ "Bearer fake-owner-a" ,
44+ "Bearer fake-owner-a" ,
45+ enabled ? undefined : "Bearer fake-owner-a" ,
46+ ] ) ;
47+ } ) . pipe (
48+ Effect . provide (
49+ Layer . mock ( GitHubCredentials . GitHubCredentials ) ( {
50+ get : ( host , repository ) => {
51+ scopes . push ( repository ) ;
52+ return Effect . succeed ( {
53+ host,
54+ ...( fallback ? { } : { repository : "ownera/repo" } ) ,
55+ source : fallback ? "settings" : "git" ,
56+ fingerprint : "fake-a" ,
57+ token : Redacted . make ( "fake-owner-a" ) ,
58+ } ) ;
59+ } ,
60+ } ) ,
61+ ) ,
62+ Effect . provideService (
63+ HttpClient . HttpClient ,
64+ HttpClient . make ( ( request ) => {
65+ const location = redirects [ authorizations . length ] ;
66+ authorizations . push ( request . headers . authorization ) ;
67+ return Effect . succeed (
68+ HttpClientResponse . fromWeb (
69+ request ,
70+ new Response ( null , {
71+ status : location === undefined ? 200 : 302 ,
72+ headers : location === undefined ? { "content-type" : "image/png" } : { location } ,
73+ } ) ,
74+ ) ,
75+ ) ;
76+ } ) ,
77+ ) ,
78+ Effect . provideService ( HostProcess . Environment , {
79+ T3CODE_GITHUB_USE_GIT_CREDENTIALS : enabled ? "1" : "0" ,
80+ } ) ,
81+ Effect . scoped ,
82+ ) ;
83+ } ,
84+ ) ;
85+
86+ it . effect . each ( [
87+ { path : "raw.githubusercontent.com/media/Repo/main/image.png" , repository : "media/Repo" } ,
88+ { path : "media.githubusercontent.com/OwnerA/Repo/main/image.png" , repository : undefined } ,
89+ { path : "media.githubusercontent.com/Media/OwnerA/Repo/main/image.png" , repository : undefined } ,
90+ { path : "media.githubusercontent.com/media/OwnerA/Repo" , repository : undefined } ,
91+ { path : "media.githubusercontent.com/media//Repo/main/image.png" , repository : undefined } ,
92+ {
93+ path : "media.githubusercontent.com/media/Owner%2FA/Repo/main/image.png" ,
94+ repository : undefined ,
95+ } ,
96+ {
97+ path : "media.githubusercontent.com/media/OwnerA/-Repo/main/image.png" ,
98+ repository : undefined ,
99+ } ,
100+ ] ) ( "requires the LFS prefix only on its own host: $path" , ( { path, repository } ) =>
101+ Effect . gen ( function * ( ) {
102+ yield * githubMediaResponse ( asset ( `https://${ path } ` ) , { } ) ;
103+ } ) . pipe (
104+ Effect . provide (
105+ Layer . mock ( GitHubCredentials . GitHubCredentials ) ( {
106+ get : ( host , selected ) => {
107+ expect ( selected ) . toBe ( repository ) ;
108+ return Effect . succeed ( {
109+ host,
110+ source : "settings" ,
111+ fingerprint : "default" ,
112+ token : Redacted . make ( "fake-default" ) ,
113+ } ) ;
114+ } ,
115+ } ) ,
116+ ) ,
117+ Effect . provideService (
118+ HttpClient . HttpClient ,
119+ HttpClient . make ( ( request ) => {
120+ expect ( request . headers . authorization ) . toBe ( "Bearer fake-default" ) ;
121+ return Effect . succeed (
122+ HttpClientResponse . fromWeb ( request , new Response ( null , { status : 404 } ) ) ,
123+ ) ;
124+ } ) ,
125+ ) ,
126+ Effect . provideService ( HostProcess . Environment , { T3CODE_GITHUB_USE_GIT_CREDENTIALS : "1" } ) ,
127+ Effect . scoped ,
128+ ) ,
129+ ) ;
130+
17131 it . effect . each ( [ true , false ] ) (
18132 "keeps legacy fallback redirect behavior only with opt-in disabled: %s" ,
19133 ( enabled ) => {
0 commit comments