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

htmlRender.test.ts

294 lines11.8 KB

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