Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 encodeHtmlRenderOutline,
5 HTML_RENDER_MAX_HEIGHT,
6 htmlRenderFrameHeight,
7 htmlRenderOutlineBoxes,
8 htmlRenderReferencesEqual,
9 htmlRenderTheme,
10 htmlRenderThemeFragment,
11 injectHtmlRenderBootstrap,
12 htmlRenderThemeMessage,
13 readHtmlRenderContentHeight,
14 readHtmlRenderImageRequest,
15 readHtmlRenderKeyDown,
16 readHtmlRenderLinkRequest,
17 readHtmlRenderReference,
18} from "./htmlRender.ts";
19import { T3_CODE_DARK_THEME_COLORS, T3_CODE_LIGHT_THEME_COLORS } from "./themePalettes.ts";
20import { htmlRenderFromToolItem } from "./toolOutput.ts";
21
22const reference = { attachmentId: "thread-abc-123.html", title: "Chart", height: 420 };
23
24describe("injectHtmlRenderBootstrap", () => {
25 it("puts the theme ahead of the page's own head content", () => {
26 const html =
27 "<!doctype html><html><head><style>:root{--background:red}</style></head><body>x</body></html>";
28 const injected = injectHtmlRenderBootstrap(html);
29 const themeAt = injected.indexOf('<style id="t3-theme">');
30 expect(themeAt).toBeGreaterThan(injected.indexOf("<head>"));
31 expect(themeAt).toBeLessThan(injected.indexOf(":root{--background:red}"));
32 expect(injected).toContain('<meta charset="utf-8">');
33 expect(injected).toContain('name="viewport"');
34 });
35
36 it("wraps fragments without a head and keeps existing meta tags", () => {
37 const fragment =
38 '<meta charset="utf-8"><meta name="viewport" content="width=device-width"><p>hi</p>';
39 const injected = injectHtmlRenderBootstrap(fragment);
40 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
41 expect(injected.match(/charset/g)).toHaveLength(1);
42 expect(injected.match(/name="viewport"/g)).toHaveLength(1);
43 expect(injected.endsWith("<p>hi</p>")).toBe(true);
44 });
45
46 it.each(["textarea", "title", "xmp", "iframe", "noembed", "noframes", "noscript", "plaintext"])(
47 "keeps the bootstrap outside %s content",
48 (tag) => {
49 const fragment = `<${tag}><head><meta name="viewport"></head></${tag}>`;
50 const injected = injectHtmlRenderBootstrap(fragment);
51 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
52 expect(injected.indexOf('<style id="t3-theme">')).toBeLessThan(injected.indexOf(`<${tag}>`));
53 expect(injected).toContain('<meta name="viewport" content="width=device-width');
54 expect(injected.endsWith(fragment)).toBe(true);
55 },
56 );
57
58 it.each([
59 '<template><head><meta name="viewport"></head></template>',
60 '<template><template>inner</template><head><meta name="viewport"></head></template>',
61 ])("keeps the bootstrap outside inert template content: %s", (fragment) => {
62 const injected = injectHtmlRenderBootstrap(fragment);
63 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
64 expect(injected.indexOf('<style id="t3-theme">')).toBeLessThan(injected.indexOf("<template>"));
65 expect(injected).toContain('<meta name="viewport" content="width=device-width');
66 expect(injected.endsWith(fragment)).toBe(true);
67 });
68
69 it("ignores tags written inside comments and scripts", () => {
70 const html =
71 '<!-- copy <head> and <meta name="viewport"> here --><html><head>' +
72 "<script>const tag = '<meta name=\"viewport\">';</script></head><body>x</body></html>";
73 const injected = injectHtmlRenderBootstrap(html);
74 expect(injected.indexOf('<style id="t3-theme">')).toBeGreaterThan(
75 injected.indexOf("<html><head>"),
76 );
77 expect(injected).toContain('<meta name="viewport" content="width=device-width');
78 });
79});
80
81describe("readHtmlRenderLinkRequest", () => {
82 it("accepts only http(s) URLs in an MCP Apps ui/open-link request", () => {
83 const link = (url: unknown, modifiers = {}) => ({
84 jsonrpc: "2.0",
85 id: 1,
86 method: "ui/open-link",
87 params: { url, ...modifiers },
88 });
89 expect(readHtmlRenderLinkRequest(link("https://example.com/a"))).toEqual({
90 id: 1,
91 url: "https://example.com/a",
92 metaKey: false,
93 ctrlKey: false,
94 });
95 expect(
96 readHtmlRenderLinkRequest(link("https://example.com/a", { metaKey: true, ctrlKey: "yes" })),
97 ).toEqual({ id: 1, url: "https://example.com/a", metaKey: true, ctrlKey: false });
98 expect(readHtmlRenderLinkRequest(link("javascript:alert(1)"))).toBeUndefined();
99 expect(readHtmlRenderLinkRequest(link("file:///etc/passwd"))).toBeUndefined();
100 expect(
101 readHtmlRenderLinkRequest({
102 jsonrpc: "2.0",
103 method: "ui/open-link",
104 params: { url: "https://example.com" },
105 }),
106 ).toBeUndefined();
107 expect(
108 readHtmlRenderLinkRequest({ type: "t3-html-render-link", url: "https://example.com" }),
109 ).toBeUndefined();
110 });
111});
112
113describe("readHtmlRenderImageRequest", () => {
114 it("accepts http(s) and inline image URLs in an open-image notification", () => {
115 const image = (params: unknown) => ({
116 jsonrpc: "2.0",
117 method: "t3/notifications/open-image",
118 params,
119 });
120 expect(
121 readHtmlRenderImageRequest(image({ src: "data:image/png;base64,AA", name: " Chart " })),
122 ).toEqual({
123 src: "data:image/png;base64,AA",
124 name: "Chart",
125 });
126 expect(readHtmlRenderImageRequest(image({ src: "https://example.com/a.png" }))).toEqual({
127 src: "https://example.com/a.png",
128 name: "Image",
129 });
130 expect(readHtmlRenderImageRequest(image({ src: "javascript:alert(1)" }))).toBeUndefined();
131 expect(readHtmlRenderImageRequest(image({ src: "data:text/html,<p>" }))).toBeUndefined();
132 expect(readHtmlRenderImageRequest(image({ src: "blob:null/1" }))).toBeUndefined();
133 });
134});
135
136describe("readHtmlRenderKeyDown", () => {
137 it("reads the key of a keydown notification and nothing else", () => {
138 const notification = (params: unknown) => ({
139 jsonrpc: "2.0",
140 method: "t3/notifications/keydown",
141 params,
142 });
143 expect(readHtmlRenderKeyDown(notification({ key: "k", code: "KeyK", metaKey: true }))).toEqual({
144 key: "k",
145 code: "KeyK",
146 metaKey: true,
147 ctrlKey: false,
148 shiftKey: false,
149 altKey: false,
150 repeat: false,
151 });
152 expect(readHtmlRenderKeyDown(notification({ key: 1, code: "KeyK" }))).toBeUndefined();
153 expect(
154 readHtmlRenderKeyDown({ jsonrpc: "2.0", method: "ui/open-link", params: { key: "k" } }),
155 ).toBeUndefined();
156 });
157});
158
159describe("readHtmlRenderContentHeight", () => {
160 it("reads only the height of an MCP Apps size-changed notification", () => {
161 const notification = (params: unknown) => ({
162 jsonrpc: "2.0",
163 method: "ui/notifications/size-changed",
164 params,
165 });
166 expect(readHtmlRenderContentHeight(notification({ height: 412 }))).toBe(412);
167 expect(readHtmlRenderContentHeight(notification({ height: "412" }))).toBe(undefined);
168 expect(readHtmlRenderContentHeight(notification({ height: 0 }))).toBe(undefined);
169 expect(readHtmlRenderContentHeight({ ...notification({ height: 412 }), method: "x" })).toBe(
170 undefined,
171 );
172 });
173});
174
175describe("htmlRenderThemeMessage", () => {
176 it("is an MCP Apps host-context-changed notification carrying the theme variables", () => {
177 const theme = htmlRenderTheme(T3_CODE_DARK_THEME_COLORS, "dark");
178 expect(htmlRenderThemeMessage(theme)).toEqual({
179 jsonrpc: "2.0",
180 method: "ui/notifications/host-context-changed",
181 params: { theme: "dark", styles: { variables: theme.variables } },
182 });
183 });
184});
185
186describe("htmlRenderTheme", () => {
187 it("exposes the brand accent as --accent and keeps the fragment decodable", () => {
188 const theme = htmlRenderTheme(T3_CODE_LIGHT_THEME_COLORS, "light");
189 expect(theme.variables["--accent"]).toBe(T3_CODE_LIGHT_THEME_COLORS.accent);
190 expect(theme.variables["--chart-1"]).toBe(T3_CODE_LIGHT_THEME_COLORS.accent);
191 expect(theme.variables["--chart-6"]).toBeDefined();
192 const fragment = htmlRenderThemeFragment(theme);
193 expect(JSON.parse(decodeURIComponent(fragment.slice("#t3-theme=".length)))).toEqual(theme);
194 expect(fragment).not.toContain("&");
195 expect(htmlRenderTheme(T3_CODE_DARK_THEME_COLORS, "dark").variables["--background"]).toBe(
196 T3_CODE_DARK_THEME_COLORS.canvas,
197 );
198 });
199});
200
201describe("readHtmlRenderReference", () => {
202 it("clamps height and rejects malformed references", () => {
203 expect(readHtmlRenderReference({ ...reference, height: 99_999 })?.height).toBe(2000);
204 expect(readHtmlRenderReference({ ...reference, title: " " })?.title).toBe("HTML");
205 expect(readHtmlRenderReference({ ...reference, attachmentId: 4 })).toBeUndefined();
206 expect(readHtmlRenderReference({ ...reference, height: Number.NaN })).toBeUndefined();
207 });
208});
209
210describe("htmlRenderFromToolItem", () => {
211 const result = { htmlRender: reference, message: "Rendered above your reply." };
212
213 it("reads the reference from each provider's result envelope", () => {
214 for (const [toolName, output] of [
215 ["mcp__t3-code__html_render", [{ type: "text", text: JSON.stringify(result) }]],
216 ["t3-code.html_render", { structuredContent: result, content: [] }],
217 ["t3-code-thread_1_html_render", JSON.stringify(result)],
218 ["html_render", result],
219 ] as const) {
220 expect(htmlRenderFromToolItem({ toolName, output })).toEqual(reference);
221 }
222 });
223
224 it("ignores other tools and failed calls", () => {
225 expect(htmlRenderFromToolItem({ toolName: "mcp__t3-code__html_preview", output: result })).toBe(
226 undefined,
227 );
228 expect(htmlRenderFromToolItem({ toolName: "mcp__other__html_render", output: result })).toBe(
229 undefined,
230 );
231 expect(
232 htmlRenderFromToolItem({ toolName: "html_render", output: { ...result, isError: true } }),
233 ).toBeUndefined();
234 });
235});
236
237describe("htmlRenderFrameHeight", () => {
238 const measured = readHtmlRenderReference({
239 ...reference,
240 height: 1500,
241 heights: [
242 [728, 1403],
243 [390, 1290],
244 [1000, 1660],
245 ],
246 })!;
247
248 it("takes the taller neighbor between measured widths and holds the ends", () => {
249 expect(measured.heights?.map(([width]) => width)).toEqual([390, 728, 1000]);
250 expect(htmlRenderFrameHeight(measured, 728)).toBe(1403);
251 expect(htmlRenderFrameHeight(measured, 559)).toBe(1403);
252 expect(htmlRenderFrameHeight(measured, 320)).toBe(1290);
253 });
254
255 it("takes the taller layout when a breakpoint falls between measured widths", () => {
256 const responsive = readHtmlRenderReference({
257 ...reference,
258 height: 2000,
259 heights: [
260 [520, 900],
261 [640, 450],
262 ],
263 })!;
264 expect(htmlRenderFrameHeight(responsive, 590)).toBe(900);
265 expect(htmlRenderFrameHeight(responsive, 640)).toBe(450);
266 });
267
268 it("fits a page the client lays out taller than the server measured", () => {
269 const fitted = { ...measured, height: 1403 };
270 expect(htmlRenderFrameHeight(fitted, 728, 1415)).toBe(1415);
271 expect(htmlRenderFrameHeight(fitted, 1400)).toBe(1660);
272 expect(htmlRenderFrameHeight(fitted, 728, 5000)).toBe(HTML_RENDER_MAX_HEIGHT);
273 });
274
275 it("keeps the agent's height when it asked for a scrolling frame or the page is unmeasured", () => {
276 const scrolling = { ...measured, height: 600 };
277 expect(htmlRenderFrameHeight(scrolling, 728, 1415)).toBe(600);
278 expect(htmlRenderFrameHeight(scrolling, 1400)).toBe(600);
279 expect(htmlRenderFrameHeight(reference, 728)).toBe(reference.height);
280 expect(htmlRenderFrameHeight(reference, 728, 900)).toBe(reference.height);
281 expect(htmlRenderFrameHeight(reference, 728, 300)).toBe(300);
282 });
283
284 it("drops a malformed table and compares tables by value", () => {
285 expect(readHtmlRenderReference({ ...reference, heights: [[728, "x"]] })?.heights).toBe(
286 undefined,
287 );
288 const copy = readHtmlRenderReference(JSON.parse(JSON.stringify(measured)))!;
289 expect(htmlRenderReferencesEqual(measured, copy)).toBe(true);
290 expect(htmlRenderReferencesEqual(measured, { ...copy, heights: [[390, 1290]] })).toBe(false);
291 });
292});
293
294describe("htmlRenderOutlineBoxes", () => {
295 const outline = encodeHtmlRenderOutline([
296 { kind: "text", x: 0, y: 0, width: 300, height: 40, lines: 2 },
297 { kind: "media", x: 0, y: 48, width: 728, height: 300, lines: 1 },
298 { kind: "media", x: 2, y: 2, width: 0, height: 10, lines: 1 },
299 ]);
300 const outlined = readHtmlRenderReference({
301 ...reference,
302 heights: [
303 [364, 1600],
304 [728, 800],
305 ],
306 outline,
307 })!;
308
309 it("round-trips through a tool result and stretches to the client's width and height", () => {
310 expect(outline).toBe("t0,0,75,10,2;m0,12,182,75,1");
311 expect(htmlRenderOutlineBoxes(outlined, 728)).toEqual([
312 { kind: "text", x: 0, y: 0, width: 300, height: 40, lines: 2 },
313 { kind: "media", x: 0, y: 48, width: 728, height: 300, lines: 1 },
314 ]);
315 expect(htmlRenderOutlineBoxes(outlined, 364)[1]).toEqual({
316 kind: "media",
317 x: 0,
318 y: 96,
319 width: 364,
320 height: 600,
321 lines: 1,
322 });
323 });
324
325 it("drops a malformed or unmeasured outline", () => {
326 expect(readHtmlRenderReference({ ...outlined, outline: "x1,2" })?.outline).toBeUndefined();
327 expect(
328 readHtmlRenderReference({ ...outlined, outline: "t0,0,10,10,0" })?.outline,
329 ).toBeUndefined();
330 expect(
331 readHtmlRenderReference({ ...outlined, outline: "m0,0,0,10,1" })?.outline,
332 ).toBeUndefined();
333 expect(readHtmlRenderReference({ ...reference, outline })?.outline).toBeUndefined();
334 expect(htmlRenderOutlineBoxes(reference, 728)).toEqual([]);
335 expect(htmlRenderReferencesEqual(outlined, { ...outlined, outline: "m0,0,1,1,1" })).toBe(false);
336 });
337});