22import * as NodeServices from "@effect/platform-node/NodeServices" ;
33import * as NodeHttpPlatform from "@effect/platform-node/NodeHttpPlatform" ;
44import * as NodeFSP from "node:fs/promises" ;
5- import { AssetPreviewTypeValidationError , ThreadId } from "@t3tools/contracts" ;
5+ import { AssetAccessError , AssetPreviewTypeValidationError , ThreadId } from "@t3tools/contracts" ;
66import { PROJECT_FAVICON_FALLBACK_MARKER } from "@t3tools/shared/projectFavicon" ;
77import { describe , expect , it } from "@effect/vitest" ;
88import * as Crypto from "effect/Crypto" ;
@@ -11,8 +11,10 @@ import * as FileSystem from "effect/FileSystem";
1111import * as Layer from "effect/Layer" ;
1212import * as Path from "effect/Path" ;
1313import * as PlatformError from "effect/PlatformError" ;
14+ import * as Schema from "effect/Schema" ;
1415import * as TestClock from "effect/testing/TestClock" ;
15- import { HttpServerResponse } from "effect/unstable/http" ;
16+ import { HttpClient , HttpClientResponse , HttpServerResponse } from "effect/unstable/http" ;
17+ import { ChildProcessSpawner } from "effect/unstable/process" ;
1618import { vi } from "vite-plus/test" ;
1719
1820import * as ServerSecretStore from "../auth/ServerSecretStore.ts" ;
@@ -25,6 +27,8 @@ import { ASSET_ROUTE_PREFIX, issueAssetUrl, resolveAsset } from "./AssetAccess.t
2527import * as NativeAppIconResolver from "./NativeAppIconResolver.ts" ;
2628import { openMediaFile } from "./MediaFile.ts" ;
2729import { symlinksSupported } from "@t3tools/shared/testing/symlinks" ;
30+ import * as GitHubCli from "../sourceControl/GitHubCli.ts" ;
31+ import { githubMediaResponse } from "./GitHubMediaFetch.ts" ;
2832
2933vi . mock ( "node:fs/promises" , async ( importOriginal ) => {
3034 const actual = await importOriginal < typeof NodeFSP > ( ) ;
@@ -47,6 +51,52 @@ const testLayer = Layer.mergeAll(
4751) . pipe ( Layer . provideMerge ( NodeServices . layer ) ) ;
4852
4953describe ( "AssetAccess" , ( ) => {
54+ it . effect ( "loads private media immediately after login and reuses the found credential" , ( ) => {
55+ let lookups = 0 ;
56+ const authorizations : Array < string | undefined > = [ ] ;
57+ return Effect . gen ( function * ( ) {
58+ const asset = {
59+ url : "https://raw.githubusercontent.com/owner/repo/main/shot.png" ,
60+ cwd : "/repo" ,
61+ expiresAt : Number . MAX_SAFE_INTEGER ,
62+ } ;
63+ expect ( ( yield * githubMediaResponse ( asset , { } ) ) . status ) . toBe ( 404 ) ;
64+ expect ( ( yield * githubMediaResponse ( asset , { } ) ) . status ) . toBe ( 200 ) ;
65+ expect ( ( yield * githubMediaResponse ( asset , { } ) ) . status ) . toBe ( 200 ) ;
66+ expect ( lookups ) . toBe ( 2 ) ;
67+ expect ( authorizations ) . toEqual ( [ undefined , "Bearer signed-in" , "Bearer signed-in" ] ) ;
68+ } ) . pipe (
69+ Effect . provide (
70+ Layer . mock ( GitHubCli . GitHubCli ) ( {
71+ execute : ( ) =>
72+ Effect . sync ( ( ) => ( {
73+ exitCode : ChildProcessSpawner . ExitCode ( 0 ) ,
74+ stdout : ++ lookups === 1 ? "" : "signed-in" ,
75+ stderr : "" ,
76+ stdoutTruncated : false ,
77+ stderrTruncated : false ,
78+ } ) ) ,
79+ } ) ,
80+ ) ,
81+ Effect . provideService (
82+ HttpClient . HttpClient ,
83+ HttpClient . make ( ( request ) => {
84+ authorizations . push ( request . headers . authorization ) ;
85+ return Effect . succeed (
86+ HttpClientResponse . fromWeb (
87+ request ,
88+ new Response ( null , {
89+ status : request . headers . authorization ? 200 : 404 ,
90+ headers : { "content-type" : "image/png" } ,
91+ } ) ,
92+ ) ,
93+ ) ;
94+ } ) ,
95+ ) ,
96+ Effect . scoped ,
97+ ) ;
98+ } ) ;
99+
50100 it . effect ( "issues exact URLs for media and browser documents outside the workspace" , ( ) =>
51101 Effect . gen ( function * ( ) {
52102 const fs = yield * FileSystem . FileSystem ;
@@ -228,7 +278,7 @@ describe("AssetAccess", () => {
228278 suffix . slice ( 0 , separator ) ,
229279 suffix . slice ( separator + 1 ) ,
230280 ) ;
231- if ( ! asset ) throw new Error ( "Expected a resolved media file" ) ;
281+ if ( asset ?. kind !== "file" ) throw new Error ( "Expected a resolved media file" ) ;
232282
233283 yield * fs . rename ( filePath , savedPath ) ;
234284 yield * fs . symlink ( secretPath , filePath ) ;
@@ -391,7 +441,7 @@ describe("AssetAccess", () => {
391441 const name = suffix . slice ( separator + 1 ) ;
392442 yield * fs . writeFileString ( filePath , "in-place edit" ) ;
393443 const edited = yield * resolveAsset ( token , name ) ;
394- if ( ! edited ) throw new Error ( "Expected the edited media file" ) ;
444+ if ( edited ?. kind !== "file" ) throw new Error ( "Expected the edited media file" ) ;
395445 const editedResponse = HttpServerResponse . toWeb ( yield * assetFileResponse ( edited ) ) ;
396446 expect ( yield * Effect . promise ( ( ) => editedResponse . text ( ) ) ) . toBe ( "in-place edit" ) ;
397447
@@ -407,7 +457,7 @@ describe("AssetAccess", () => {
407457 renewedSuffix . slice ( 0 , renewedSeparator ) ,
408458 renewedSuffix . slice ( renewedSeparator + 1 ) ,
409459 ) ;
410- if ( ! renewedAsset ) throw new Error ( "Expected the replacement media file" ) ;
460+ if ( renewedAsset ?. kind !== "file" ) throw new Error ( "Expected the replacement media file" ) ;
411461 const renewedResponse = HttpServerResponse . toWeb ( yield * assetFileResponse ( renewedAsset ) ) ;
412462 expect ( yield * Effect . promise ( ( ) => renewedResponse . text ( ) ) ) . toBe ( "replacement" ) ;
413463 yield * fs . remove ( filePath ) ;
@@ -1062,4 +1112,60 @@ describe("AssetAccess", () => {
10621112 expect ( error . cause ) . toBe ( resolutionCause ) ;
10631113 } ) . pipe ( Effect . provide ( testLayer ) ) ,
10641114 ) ;
1115+
1116+ it . effect ( "serves GitHub-hosted pull request media through the repository's credential" , ( ) =>
1117+ Effect . gen ( function * ( ) {
1118+ const resolve = ( relativeUrl : string ) => {
1119+ const suffix = relativeUrl . slice ( `${ ASSET_ROUTE_PREFIX } /` . length ) ;
1120+ const separator = suffix . indexOf ( "/" ) ;
1121+ return resolveAsset ( suffix . slice ( 0 , separator ) , suffix . slice ( separator + 1 ) ) ;
1122+ } ;
1123+ const issue = ( url : string ) =>
1124+ issueAssetUrl ( { resource : { _tag : "github-media" , cwd : "/repo" , url } } ) ;
1125+
1126+ const attachment = yield * issue (
1127+ "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c" ,
1128+ ) ;
1129+ expect ( attachment . relativeUrl . endsWith ( "/1a1842fb-6383-492f-873c-57aa0033fa6c" ) ) . toBe ( true ) ;
1130+ expect ( yield * resolve ( attachment . relativeUrl ) ) . toEqual ( {
1131+ kind : "github-media" ,
1132+ url : "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c" ,
1133+ cwd : "/repo" ,
1134+ // The signed URL's own expiry, which is how long a client may keep the bytes.
1135+ expiresAt : attachment . expiresAt ,
1136+ } ) ;
1137+
1138+ // A `blob` link addresses the page; only the raw host answers a credential with bytes.
1139+ const committed = yield * issue ( "https://github.com/owner/repo/blob/main/docs/shot.png" ) ;
1140+ expect ( yield * resolve ( committed . relativeUrl ) ) . toMatchObject ( {
1141+ url : "https://raw.githubusercontent.com/owner/repo/main/docs/shot.png" ,
1142+ } ) ;
1143+
1144+ // The pre-`user-attachments` form, Git LFS bytes, and a name no `decodeURIComponent`
1145+ // accepts all arrive from real bodies.
1146+ const legacy = yield * issue ( "https://github.com/owner/repo/assets/45952064/1a1842fb" ) ;
1147+ expect ( yield * resolve ( legacy . relativeUrl ) ) . toMatchObject ( {
1148+ url : "https://github.com/owner/repo/assets/45952064/1a1842fb" ,
1149+ } ) ;
1150+ const lfs = yield * issue ( "https://media.githubusercontent.com/media/owner/repo/main/a.mp4" ) ;
1151+ expect ( yield * resolve ( lfs . relativeUrl ) ) . toMatchObject ( {
1152+ url : "https://media.githubusercontent.com/media/owner/repo/main/a.mp4" ,
1153+ } ) ;
1154+ const awkward = yield * issue ( "https://raw.githubusercontent.com/o/r/main/100%.png" ) ;
1155+ expect ( awkward . relativeUrl . endsWith ( "/100%25.png" ) ) . toBe ( true ) ;
1156+
1157+ for ( const url of [
1158+ "https://example.com/shot.png" ,
1159+ "https://example.com/shot.png?token=private-media-token" ,
1160+ "http://github.com/user-attachments/assets/1a1842fb" ,
1161+ "https://github.com/owner/repo/pull/1" ,
1162+ "https://github.com/owner/repo/blob/main/" ,
1163+ ] ) {
1164+ const error = yield * issue ( url ) . pipe ( Effect . flip ) ;
1165+ expect ( error . _tag ) . toBe ( "AssetGitHubMediaUrlValidationError" ) ;
1166+ const encoded = yield * Schema . encodeEffect ( Schema . fromJsonString ( AssetAccessError ) ) ( error ) ;
1167+ expect ( encoded ) . not . toContain ( url ) ;
1168+ }
1169+ } ) . pipe ( Effect . provide ( testLayer ) ) ,
1170+ ) ;
10651171} ) ;
0 commit comments