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

htmlRender.test.ts

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