Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 HTML_RENDER_MAX_HEIGHT,
5 htmlRenderFrameHeight,
6 htmlRenderReferencesEqual,
7 htmlRenderTheme,
8 htmlRenderThemeFragment,
9 injectHtmlRenderBootstrap,
10 htmlRenderThemeMessage,
11 readHtmlRenderContentHeight,
12 readHtmlRenderLinkRequest,
13 readHtmlRenderReference,
14} from "./htmlRender.ts";
15import { T3_CODE_DARK_THEME_COLORS, T3_CODE_LIGHT_THEME_COLORS } from "./themePalettes.ts";
16import { htmlRenderFromToolItem } from "./toolOutput.ts";
17
18const reference = { attachmentId: "thread-abc-123.html", title: "Chart", height: 420 };
19
20describe("injectHtmlRenderBootstrap", () => {
21 it("puts the theme ahead of the page's own head content", () => {
22 const html =
23 "<!doctype html><html><head><style>:root{--background:red}</style></head><body>x</body></html>";
24 const injected = injectHtmlRenderBootstrap(html);
25 const themeAt = injected.indexOf('<style id="t3-theme">');
26 expect(themeAt).toBeGreaterThan(injected.indexOf("<head>"));
27 expect(themeAt).toBeLessThan(injected.indexOf(":root{--background:red}"));
28 expect(injected).toContain('<meta charset="utf-8">');
29 expect(injected).toContain('name="viewport"');
30 });
31
32 it("wraps fragments without a head and keeps existing meta tags", () => {
33 const fragment =
34 '<meta charset="utf-8"><meta name="viewport" content="width=device-width"><p>hi</p>';
35 const injected = injectHtmlRenderBootstrap(fragment);
36 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
37 expect(injected.match(/charset/g)).toHaveLength(1);
38 expect(injected.match(/name="viewport"/g)).toHaveLength(1);
39 expect(injected.endsWith("<p>hi</p>")).toBe(true);
40 });
41
42 it.each(["textarea", "title", "xmp", "iframe", "noembed", "noframes", "noscript", "plaintext"])(
43 "keeps the bootstrap outside %s content",
44 (tag) => {
45 const fragment = `<${tag}><head><meta name="viewport"></head></${tag}>`;
46 const injected = injectHtmlRenderBootstrap(fragment);
47 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
48 expect(injected.indexOf('<style id="t3-theme">')).toBeLessThan(injected.indexOf(`<${tag}>`));
49 expect(injected).toContain('<meta name="viewport" content="width=device-width');
50 expect(injected.endsWith(fragment)).toBe(true);
51 },
52 );
53
54 it.each([
55 '<template><head><meta name="viewport"></head></template>',
56 '<template><template>inner</template><head><meta name="viewport"></head></template>',
57 ])("keeps the bootstrap outside inert template content: %s", (fragment) => {
58 const injected = injectHtmlRenderBootstrap(fragment);
59 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
60 expect(injected.indexOf('<style id="t3-theme">')).toBeLessThan(injected.indexOf("<template>"));
61 expect(injected).toContain('<meta name="viewport" content="width=device-width');
62 expect(injected.endsWith(fragment)).toBe(true);
63 });
64
65 it("ignores tags written inside comments and scripts", () => {
66 const html =
67 '<!-- copy <head> and <meta name="viewport"> here --><html><head>' +
68 "<script>const tag = '<meta name=\"viewport\">';</script></head><body>x</body></html>";
69 const injected = injectHtmlRenderBootstrap(html);
70 expect(injected.indexOf('<style id="t3-theme">')).toBeGreaterThan(
71 injected.indexOf("<html><head>"),
72 );
73 expect(injected).toContain('<meta name="viewport" content="width=device-width');
74 });
75});
76
77describe("readHtmlRenderLinkRequest", () => {
78 it("accepts only http(s) URLs in an MCP Apps ui/open-link request", () => {
79 const link = (url: unknown) => ({
80 jsonrpc: "2.0",
81 id: 1,
82 method: "ui/open-link",
83 params: { url },
84 });
85 expect(readHtmlRenderLinkRequest(link("https://example.com/a"))).toEqual({
86 id: 1,
87 url: "https://example.com/a",
88 });
89 expect(readHtmlRenderLinkRequest(link("javascript:alert(1)"))).toBeUndefined();
90 expect(readHtmlRenderLinkRequest(link("file:///etc/passwd"))).toBeUndefined();
91 expect(
92 readHtmlRenderLinkRequest({
93 jsonrpc: "2.0",
94 method: "ui/open-link",
95 params: { url: "https://example.com" },
96 }),
97 ).toBeUndefined();
98 expect(
99 readHtmlRenderLinkRequest({ type: "t3-html-render-link", url: "https://example.com" }),
100 ).toBeUndefined();
101 });
102});
103
104describe("readHtmlRenderContentHeight", () => {
105 it("reads only the height of an MCP Apps size-changed notification", () => {
106 const notification = (params: unknown) => ({
107 jsonrpc: "2.0",
108 method: "ui/notifications/size-changed",
109 params,
110 });
111 expect(readHtmlRenderContentHeight(notification({ height: 412 }))).toBe(412);
112 expect(readHtmlRenderContentHeight(notification({ height: "412" }))).toBe(undefined);
113 expect(readHtmlRenderContentHeight(notification({ height: 0 }))).toBe(undefined);
114 expect(readHtmlRenderContentHeight({ ...notification({ height: 412 }), method: "x" })).toBe(
115 undefined,
116 );
117 });
118});
119
120describe("htmlRenderThemeMessage", () => {
121 it("is an MCP Apps host-context-changed notification carrying the theme variables", () => {
122 const theme = htmlRenderTheme(T3_CODE_DARK_THEME_COLORS, "dark");
123 expect(htmlRenderThemeMessage(theme)).toEqual({
124 jsonrpc: "2.0",
125 method: "ui/notifications/host-context-changed",
126 params: { theme: "dark", styles: { variables: theme.variables } },
127 });
128 });
129});
130
131describe("htmlRenderTheme", () => {
132 it("exposes the brand accent as --accent and keeps the fragment decodable", () => {
133 const theme = htmlRenderTheme(T3_CODE_LIGHT_THEME_COLORS, "light");
134 expect(theme.variables["--accent"]).toBe(T3_CODE_LIGHT_THEME_COLORS.accent);
135 expect(theme.variables["--chart-1"]).toBe(T3_CODE_LIGHT_THEME_COLORS.accent);
136 expect(theme.variables["--chart-6"]).toBeDefined();
137 const fragment = htmlRenderThemeFragment(theme);
138 expect(JSON.parse(decodeURIComponent(fragment.slice("#t3-theme=".length)))).toEqual(theme);
139 expect(fragment).not.toContain("&");
140 expect(htmlRenderTheme(T3_CODE_DARK_THEME_COLORS, "dark").variables["--background"]).toBe(
141 T3_CODE_DARK_THEME_COLORS.canvas,
142 );
143 });
144});
145
146describe("readHtmlRenderReference", () => {
147 it("clamps height and rejects malformed references", () => {
148 expect(readHtmlRenderReference({ ...reference, height: 99_999 })?.height).toBe(2000);
149 expect(readHtmlRenderReference({ ...reference, title: " " })?.title).toBe("HTML");
150 expect(readHtmlRenderReference({ ...reference, attachmentId: 4 })).toBeUndefined();
151 expect(readHtmlRenderReference({ ...reference, height: Number.NaN })).toBeUndefined();
152 });
153});
154
155describe("htmlRenderFromToolItem", () => {
156 const result = { htmlRender: reference, message: "Rendered above your reply." };
157
158 it("reads the reference from each provider's result envelope", () => {
159 for (const [toolName, output] of [
160 ["mcp__t3-code__html_render", [{ type: "text", text: JSON.stringify(result) }]],
161 ["t3-code.html_render", { structuredContent: result, content: [] }],
162 ["t3-code-thread_1_html_render", JSON.stringify(result)],
163 ["html_render", result],
164 ] as const) {
165 expect(htmlRenderFromToolItem({ toolName, output })).toEqual(reference);
166 }
167 });
168
169 it("ignores other tools and failed calls", () => {
170 expect(htmlRenderFromToolItem({ toolName: "mcp__t3-code__html_preview", output: result })).toBe(
171 undefined,
172 );
173 expect(htmlRenderFromToolItem({ toolName: "mcp__other__html_render", output: result })).toBe(
174 undefined,
175 );
176 expect(
177 htmlRenderFromToolItem({ toolName: "html_render", output: { ...result, isError: true } }),
178 ).toBeUndefined();
179 });
180});
181
182describe("htmlRenderFrameHeight", () => {
183 const measured = readHtmlRenderReference({
184 ...reference,
185 height: 1500,
186 heights: [
187 [728, 1403],
188 [390, 1290],
189 [1000, 1660],
190 ],
191 })!;
192
193 it("takes the taller neighbor between measured widths and holds the ends", () => {
194 expect(measured.heights?.map(([width]) => width)).toEqual([390, 728, 1000]);
195 expect(htmlRenderFrameHeight(measured, 728)).toBe(1403);
196 expect(htmlRenderFrameHeight(measured, 559)).toBe(1403);
197 expect(htmlRenderFrameHeight(measured, 320)).toBe(1290);
198 });
199
200 it("takes the taller layout when a breakpoint falls between measured widths", () => {
201 // 900px tall below a 600px media query, 450px above it.
202 const responsive = readHtmlRenderReference({
203 ...reference,
204 height: 2000,
205 heights: [
206 [520, 900],
207 [640, 450],
208 ],
209 })!;
210 expect(htmlRenderFrameHeight(responsive, 590)).toBe(900);
211 expect(htmlRenderFrameHeight(responsive, 640)).toBe(450);
212 });
213
214 it("fits a page the client lays out taller than the server measured", () => {
215 // The agent passed contentHeight at the column width, so the page should never scroll.
216 const fitted = { ...measured, height: 1403 };
217 expect(htmlRenderFrameHeight(fitted, 728, 1415)).toBe(1415);
218 expect(htmlRenderFrameHeight(fitted, 1400)).toBe(1660);
219 expect(htmlRenderFrameHeight(fitted, 728, 5000)).toBe(HTML_RENDER_MAX_HEIGHT);
220 });
221
222 it("keeps the agent's height when it asked for a scrolling frame or the page is unmeasured", () => {
223 const scrolling = { ...measured, height: 600 };
224 expect(htmlRenderFrameHeight(scrolling, 728, 1415)).toBe(600);
225 expect(htmlRenderFrameHeight(scrolling, 1400)).toBe(600);
226 expect(htmlRenderFrameHeight(reference, 728)).toBe(reference.height);
227 expect(htmlRenderFrameHeight(reference, 728, 900)).toBe(reference.height);
228 expect(htmlRenderFrameHeight(reference, 728, 300)).toBe(300);
229 });
230
231 it("drops a malformed table and compares tables by value", () => {
232 expect(readHtmlRenderReference({ ...reference, heights: [[728, "x"]] })?.heights).toBe(
233 undefined,
234 );
235 const copy = readHtmlRenderReference(JSON.parse(JSON.stringify(measured)))!;
236 expect(htmlRenderReferencesEqual(measured, copy)).toBe(true);
237 expect(htmlRenderReferencesEqual(measured, { ...copy, heights: [[390, 1290]] })).toBe(false);
238 });
239});