@@ -16,6 +16,11 @@ import * as Tracer from "effect/Tracer";
1616import { HttpClient , HttpClientRequest , HttpRouter , HttpServerResponse } from "effect/http" ;
1717import { openMediaFile } from "./assets/MediaFile.ts" ;
1818
19+ import {
20+ injectMcpAppCsp ,
21+ injectMcpAppThemeBootstrap ,
22+ MCP_APP_MAX_HTML_BYTES ,
23+ } from "@t3tools/shared/mcpApp" ;
1924import { ORCHESTRATION_PROTOCOL_HEADER } from "@t3tools/contracts" ;
2025
2126import * as ServerConfig from "./config.ts" ;
@@ -30,6 +35,70 @@ import {
3035} from "./http.ts" ;
3136import * as ServerHttp from "./http.ts" ;
3237
38+ describe ( "MCP app asset responses" , ( ) => {
39+ it . effect (
40+ "transforms existing snapshots only with explicit render intent and fresh metadata" ,
41+ ( ) =>
42+ Effect . gen ( function * ( ) {
43+ const fs = yield * FileSystem . FileSystem ;
44+ const path = yield * Path . Path ;
45+ const directory = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-mcp-theme-" } ) ;
46+ const file = path . join ( directory , "app.html" ) ;
47+ const html = injectMcpAppCsp (
48+ "<html><head><script>vendor()</script></head><body>☃</body></html>" ,
49+ undefined ,
50+ ) ;
51+ yield * fs . writeFileString ( file , html ) ;
52+ for ( const method of [ "GET" , "HEAD" ] as const ) {
53+ const response = HttpServerResponse . toWeb (
54+ yield * assetFileResponse (
55+ { path : file , mimeType : "text/html" , renderIntent : "mcp-app" } ,
56+ "bytes=0-1" ,
57+ undefined ,
58+ method ,
59+ ) ,
60+ ) ;
61+ const expected = injectMcpAppThemeBootstrap ( html ) ;
62+ expect ( response . status ) . toBe ( 200 ) ;
63+ expect ( response . headers . get ( "content-length" ) ) . toBe (
64+ String ( new TextEncoder ( ) . encode ( expected ) . byteLength ) ,
65+ ) ;
66+ expect ( response . headers . get ( "etag" ) ) . toBeNull ( ) ;
67+ expect ( response . headers . get ( "last-modified" ) ) . toBeNull ( ) ;
68+ expect ( response . headers . get ( "content-security-policy" ) ) . toBe (
69+ "sandbox allow-scripts allow-forms allow-popups allow-downloads" ,
70+ ) ;
71+ expect ( yield * Effect . promise ( ( ) => response . text ( ) ) ) . toBe (
72+ method === "HEAD" ? "" : expected ,
73+ ) ;
74+ }
75+ for ( const download of [ false , true ] ) {
76+ const response = HttpServerResponse . toWeb (
77+ yield * assetFileResponse ( {
78+ path : file ,
79+ mimeType : "text/html" ,
80+ download,
81+ ...( download ? { renderIntent : "mcp-app" as const } : { } ) ,
82+ } ) ,
83+ ) ;
84+ expect ( yield * Effect . promise ( ( ) => response . text ( ) ) ) . toBe ( html ) ;
85+ }
86+ } ) . pipe ( Effect . provide ( layerFileResponse ) ) ,
87+ ) ;
88+
89+ it . effect ( "rejects oversized captures before transforming them" , ( ) =>
90+ Effect . gen ( function * ( ) {
91+ const fs = yield * FileSystem . FileSystem ;
92+ const path = yield * Path . Path ;
93+ const directory = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-mcp-theme-size-" } ) ;
94+ const file = path . join ( directory , "app.html" ) ;
95+ yield * fs . writeFileString ( file , "x" . repeat ( MCP_APP_MAX_HTML_BYTES + 128 * 1024 + 1 ) ) ;
96+ const response = yield * assetFileResponse ( { path : file , renderIntent : "mcp-app" } ) ;
97+ expect ( response . status ) . toBe ( 413 ) ;
98+ } ) . pipe ( Effect . provide ( layerFileResponse ) ) ,
99+ ) ;
100+ } ) ;
101+
33102describe ( "untraced requests" , ( ) => {
34103 it . effect ( "drops the HTTP server span for browser trace exports, query string included" , ( ) => {
35104 const spanNames : Array < string > = [ ] ;
0 commit comments