99 TurnItemId ,
1010} from "@t3tools/contracts" ;
1111import { PROJECT_FAVICON_FALLBACK_MARKER } from "@t3tools/shared/projectFavicon" ;
12+ import { MCP_APP_MAX_HTML_BYTES } from "@t3tools/shared/mcpApp" ;
1213import { describe , expect , it } from "@effect/vitest" ;
1314import * as Crypto from "effect/Crypto" ;
1415import * as DateTime from "effect/DateTime" ;
@@ -30,7 +31,7 @@ import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts";
3031import * as T3ProjectFileLoader from "../project/T3ProjectFileLoader.ts" ;
3132import * as WorkspacePaths from "../workspace/WorkspacePaths.ts" ;
3233import { assetFileResponse } from "../http.ts" ;
33- import { ASSET_ROUTE_PREFIX , issueAssetUrl , resolveAsset } from "./AssetAccess.ts" ;
34+ import { ASSET_ROUTE_PREFIX , issueAssetUrl , readMcpAppHtml , resolveAsset } from "./AssetAccess.ts" ;
3435import * as NativeAppIconResolver from "./NativeAppIconResolver.ts" ;
3536import { openMediaFile } from "./MediaFile.ts" ;
3637import { symlinksSupported } from "@t3tools/shared/testing/symlinks" ;
@@ -121,6 +122,64 @@ const layerTest = Layer.mergeAll(
121122) . pipe ( Layer . provideMerge ( NodeServices . layer ) ) ;
122123
123124describe ( "AssetAccess" , ( ) => {
125+ describe ( "readMcpAppHtml" , ( ) => {
126+ it . effect ( "accepts snapshots at the stored HTML byte limit" , ( ) =>
127+ Effect . gen ( function * ( ) {
128+ const fileSystem = yield * FileSystem . FileSystem ;
129+ const path = yield * Path . Path ;
130+ const directory = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-mcp-html-" } ) ;
131+ const file = path . join ( directory , "app.html" ) ;
132+ const limit = MCP_APP_MAX_HTML_BYTES + 128 * 1024 ;
133+ yield * fileSystem . writeFile ( file , new Uint8Array ( limit ) . fill ( 120 ) ) ;
134+
135+ const bytes = yield * readMcpAppHtml ( file ) ;
136+ expect ( bytes ?. byteLength ) . toBe ( limit ) ;
137+ expect ( bytes ?. every ( ( byte ) => byte === 120 ) ) . toBe ( true ) ;
138+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
139+ ) ;
140+
141+ it . effect ( "rejects snapshots one byte past the limit and much larger files" , ( ) =>
142+ Effect . gen ( function * ( ) {
143+ const fileSystem = yield * FileSystem . FileSystem ;
144+ const path = yield * Path . Path ;
145+ const directory = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-mcp-html-" } ) ;
146+ const file = path . join ( directory , "app.html" ) ;
147+ const limit = MCP_APP_MAX_HTML_BYTES + 128 * 1024 ;
148+ for ( const size of [ limit + 1 , limit * 2 ] ) {
149+ yield * fileSystem . writeFile ( file , new Uint8Array ( size ) . fill ( 120 ) ) ;
150+ expect ( yield * readMcpAppHtml ( file ) ) . toBeNull ( ) ;
151+ }
152+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
153+ ) ;
154+
155+ it . effect ( "preserves UTF-8 bytes across stream chunks and accepts empty snapshots" , ( ) =>
156+ Effect . gen ( function * ( ) {
157+ const fileSystem = yield * FileSystem . FileSystem ;
158+ const path = yield * Path . Path ;
159+ const directory = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-mcp-html-" } ) ;
160+ const file = path . join ( directory , "app.html" ) ;
161+ for ( const html of [ "" , "<body>" + "☃" . repeat ( 100_000 ) + "</body>" ] ) {
162+ const expected = new TextEncoder ( ) . encode ( html ) ;
163+ yield * fileSystem . writeFile ( file , expected ) ;
164+ const bytes = yield * readMcpAppHtml ( file ) ;
165+ expect ( bytes ?. byteLength ) . toBe ( expected . byteLength ) ;
166+ expect ( bytes && new TextDecoder ( ) . decode ( bytes ) ) . toBe ( html ) ;
167+ }
168+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
169+ ) ;
170+
171+ it . effect ( "preserves filesystem failures instead of reporting oversized HTML" , ( ) =>
172+ Effect . gen ( function * ( ) {
173+ const fileSystem = yield * FileSystem . FileSystem ;
174+ const path = yield * Path . Path ;
175+ const directory = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-mcp-html-" } ) ;
176+ const error = yield * readMcpAppHtml ( path . join ( directory , "missing.html" ) ) . pipe ( Effect . flip ) ;
177+ expect ( error . _tag ) . toBe ( "PlatformError" ) ;
178+ expect ( error . reason . _tag ) . toBe ( "NotFound" ) ;
179+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
180+ ) ;
181+ } ) ;
182+
124183 it . effect ( "loads private media immediately after login with the GitHub credential" , ( ) => {
125184 let lookups = 0 ;
126185 const authorizations : Array < string | undefined > = [ ] ;
0 commit comments