Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

htmlRender.test.ts

244 lines10.1 KB

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, modifiers = {}) => ({
80 jsonrpc: "2.0",
81 id: 1,
82 method: "ui/open-link",
83 params: { url, ...modifiers },
84 });
85 expect(readHtmlRenderLinkRequest(link("https://example.com/a"))).toEqual({
86 id: 1,
87 url: "https://example.com/a",
88 metaKey: false,
89 ctrlKey: false,
90 });
91 expect(
92 readHtmlRenderLinkRequest(link("https://example.com/a", { metaKey: true, ctrlKey: "yes" })),
93 ).toEqual({ id: 1, url: "https://example.com/a", metaKey: true, ctrlKey: false });
94 expect(readHtmlRenderLinkRequest(link("javascript:alert(1)"))).toBeUndefined();
95 expect(readHtmlRenderLinkRequest(link("file:///etc/passwd"))).toBeUndefined();
96 expect(
97 readHtmlRenderLinkRequest({
98 jsonrpc: "2.0",
99 method: "ui/open-link",
100 params: { url: "https://example.com" },
101 }),
102 ).toBeUndefined();
103 expect(
104 readHtmlRenderLinkRequest({ type: "t3-html-render-link", url: "https://example.com" }),
105 ).toBeUndefined();
106 });
107});
108​
109describe("readHtmlRenderContentHeight", () => {
110 it("reads only the height of an MCP Apps size-changed notification", () => {
111 const notification = (params: unknown) => ({
112 jsonrpc: "2.0",
113 method: "ui/notifications/size-changed",
114 params,
115 });
116 expect(readHtmlRenderContentHeight(notification({ height: 412 }))).toBe(412);
117 expect(readHtmlRenderContentHeight(notification({ height: "412" }))).toBe(undefined);
118 expect(readHtmlRenderContentHeight(notification({ height: 0 }))).toBe(undefined);
119 expect(readHtmlRenderContentHeight({ ...notification({ height: 412 }), method: "x" })).toBe(
120 undefined,
121 );
122 });
123});
124​
125describe("htmlRenderThemeMessage", () => {
126 it("is an MCP Apps host-context-changed notification carrying the theme variables", () => {
127 const theme = htmlRenderTheme(T3_CODE_DARK_THEME_COLORS, "dark");
128 expect(htmlRenderThemeMessage(theme)).toEqual({
129 jsonrpc: "2.0",
130 method: "ui/notifications/host-context-changed",
131 params: { theme: "dark", styles: { variables: theme.variables } },
132 });
133 });
134});
135​
136describe("htmlRenderTheme", () => {
137 it("exposes the brand accent as --accent and keeps the fragment decodable", () => {
138 const theme = htmlRenderTheme(T3_CODE_LIGHT_THEME_COLORS, "light");
139 expect(theme.variables["--accent"]).toBe(T3_CODE_LIGHT_THEME_COLORS.accent);
140 expect(theme.variables["--chart-1"]).toBe(T3_CODE_LIGHT_THEME_COLORS.accent);
141 expect(theme.variables["--chart-6"]).toBeDefined();
142 const fragment = htmlRenderThemeFragment(theme);
143 expect(JSON.parse(decodeURIComponent(fragment.slice("#t3-theme=".length)))).toEqual(theme);
144 expect(fragment).not.toContain("&");
145 expect(htmlRenderTheme(T3_CODE_DARK_THEME_COLORS, "dark").variables["--background"]).toBe(
146 T3_CODE_DARK_THEME_COLORS.canvas,
147 );
148 });
149});
150​
151describe("readHtmlRenderReference", () => {
152 it("clamps height and rejects malformed references", () => {
153 expect(readHtmlRenderReference({ ...reference, height: 99_999 })?.height).toBe(2000);
154 expect(readHtmlRenderReference({ ...reference, title: " " })?.title).toBe("HTML");
155 expect(readHtmlRenderReference({ ...reference, attachmentId: 4 })).toBeUndefined();
156 expect(readHtmlRenderReference({ ...reference, height: Number.NaN })).toBeUndefined();
157 });
158});
159​
160describe("htmlRenderFromToolItem", () => {
161 const result = { htmlRender: reference, message: "Rendered above your reply." };
162​
163 it("reads the reference from each provider's result envelope", () => {
164 for (const [toolName, output] of [
165 ["mcp__t3-code__html_render", [{ type: "text", text: JSON.stringify(result) }]],
166 ["t3-code.html_render", { structuredContent: result, content: [] }],
167 ["t3-code-thread_1_html_render", JSON.stringify(result)],
168 ["html_render", result],
169 ] as const) {
170 expect(htmlRenderFromToolItem({ toolName, output })).toEqual(reference);
171 }
172 });
173​
174 it("ignores other tools and failed calls", () => {
175 expect(htmlRenderFromToolItem({ toolName: "mcp__t3-code__html_preview", output: result })).toBe(
176 undefined,
177 );
178 expect(htmlRenderFromToolItem({ toolName: "mcp__other__html_render", output: result })).toBe(
179 undefined,
180 );
181 expect(
182 htmlRenderFromToolItem({ toolName: "html_render", output: { ...result, isError: true } }),
183 ).toBeUndefined();
184 });
185});
186​
187describe("htmlRenderFrameHeight", () => {
188 const measured = readHtmlRenderReference({
189 ...reference,
190 height: 1500,
191 heights: [
192 [728, 1403],
193 [390, 1290],
194 [1000, 1660],
195 ],
196 })!;
197​
198 it("takes the taller neighbor between measured widths and holds the ends", () => {
199 expect(measured.heights?.map(([width]) => width)).toEqual([390, 728, 1000]);
200 expect(htmlRenderFrameHeight(measured, 728)).toBe(1403);
201 expect(htmlRenderFrameHeight(measured, 559)).toBe(1403);
202 expect(htmlRenderFrameHeight(measured, 320)).toBe(1290);
203 });
204​
205 it("takes the taller layout when a breakpoint falls between measured widths", () => {
206 // 900px tall below a 600px media query, 450px above it.
207 const responsive = readHtmlRenderReference({
208 ...reference,
209 height: 2000,
210 heights: [
211 [520, 900],
212 [640, 450],
213 ],
214 })!;
215 expect(htmlRenderFrameHeight(responsive, 590)).toBe(900);
216 expect(htmlRenderFrameHeight(responsive, 640)).toBe(450);
217 });
218​
219 it("fits a page the client lays out taller than the server measured", () => {
220 // The agent passed contentHeight at the column width, so the page should never scroll.
221 const fitted = { ...measured, height: 1403 };
222 expect(htmlRenderFrameHeight(fitted, 728, 1415)).toBe(1415);
223 expect(htmlRenderFrameHeight(fitted, 1400)).toBe(1660);
224 expect(htmlRenderFrameHeight(fitted, 728, 5000)).toBe(HTML_RENDER_MAX_HEIGHT);
225 });
226​
227 it("keeps the agent's height when it asked for a scrolling frame or the page is unmeasured", () => {
228 const scrolling = { ...measured, height: 600 };
229 expect(htmlRenderFrameHeight(scrolling, 728, 1415)).toBe(600);
230 expect(htmlRenderFrameHeight(scrolling, 1400)).toBe(600);
231 expect(htmlRenderFrameHeight(reference, 728)).toBe(reference.height);
232 expect(htmlRenderFrameHeight(reference, 728, 900)).toBe(reference.height);
233 expect(htmlRenderFrameHeight(reference, 728, 300)).toBe(300);
234 });
235​
236 it("drops a malformed table and compares tables by value", () => {
237 expect(readHtmlRenderReference({ ...reference, heights: [[728, "x"]] })?.heights).toBe(
238 undefined,
239 );
240 const copy = readHtmlRenderReference(JSON.parse(JSON.stringify(measured)))!;
241 expect(htmlRenderReferencesEqual(measured, copy)).toBe(true);
242 expect(htmlRenderReferencesEqual(measured, { ...copy, heights: [[390, 1290]] })).toBe(false);
243 });
244});