Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 htmlRenderFileName,
5 htmlRenderFrameHeight,
6 injectHtmlRenderBootstrap,
7 readHtmlRenderImageRequest,
8 readHtmlRenderLinkRequest,
9} from "./htmlRender.ts";
10import { htmlRenderFromToolItem } from "./toolOutput.ts";
11
12const reference = { attachmentId: "thread-abc-123.html", title: "Chart", height: 420 };
13
14describe("htmlRenderFileName", () => {
15 it("never ends on half of an emoji at the length limit", () => {
16 const fits = "a".repeat(118);
17 const crosses = "a".repeat(119);
18 expect(htmlRenderFileName(`${fits}🚀 report`)).toBe(`${fits}🚀.html`);
19 expect(htmlRenderFileName(`${crosses}🚀 report`)).toBe(`${crosses}.html`);
20 });
21});
22
23describe("htmlRenderFrameHeight", () => {
24 const responsive = {
25 attachmentId: "a",
26 title: "Report",
27 height: 600,
28 heights: [
29 [728, 350],
30 [1000, 700],
31 [1144, 250],
32 ],
33 } as const;
34
35 it("uses the last measured height at and above the widest sampled width", () => {
36 expect(
37 [1143, 1144, 1145, 1300].map((width) => htmlRenderFrameHeight(responsive, width)),
38 ).toEqual([700, 250, 250, 250]);
39 });
40});
41
42describe("injectHtmlRenderBootstrap", () => {
43 it("puts the theme ahead of the page's own head content", () => {
44 const html =
45 "<!doctype html><html><head><style>:root{--background:red}</style></head><body>x</body></html>";
46 const injected = injectHtmlRenderBootstrap(html);
47 const themeAt = injected.indexOf('<style id="t3-theme">');
48 expect(themeAt).toBeGreaterThan(injected.indexOf("<head>"));
49 expect(themeAt).toBeLessThan(injected.indexOf(":root{--background:red}"));
50 expect(injected).toContain('<meta charset="utf-8">');
51 expect(injected).toContain('name="viewport"');
52 });
53
54 it.each([
55 "<html><head data-condition='x > 0'>",
56 '<html><head data-condition="x > 0" data-label=\'it "works"\'>',
57 "<html data-condition='x > 0'>",
58 '<html data-condition="x > 0" data-label="it \'works\'">',
59 ])("keeps the bootstrap after the complete opening tag: %s", (opening) => {
60 const prefix = `<!doctype html>${opening}`;
61 const head = opening.startsWith("<html><head") ? "" : "<head>";
62 const content = `${head ? "" : "<title>Report</title></head>"}<body><p>Report</p></body></html>`;
63 const injected = injectHtmlRenderBootstrap(prefix + content);
64 expect(injected.startsWith(`${prefix}${head}<meta charset="utf-8">`)).toBe(true);
65 expect(injected.indexOf('<style id="t3-theme">')).toBeGreaterThan(prefix.length);
66 expect(injected).toContain("<script>");
67 expect(injected.endsWith(content)).toBe(true);
68 });
69
70 it("wraps fragments without a head and keeps existing meta tags", () => {
71 const fragment =
72 '<meta charset="utf-8"><meta data-if="a<b" name="viewport" content="width=device-width"><p>hi</p>';
73 const injected = injectHtmlRenderBootstrap(fragment);
74 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
75 expect(injected.match(/charset/g)).toHaveLength(1);
76 expect(injected.match(/name="viewport"/g)).toHaveLength(1);
77 expect(injected.endsWith("<p>hi</p>")).toBe(true);
78 });
79
80 it.each([
81 '<meta data-name="viewport" content="card">',
82 '<meta name="viewport-fit" content="cover">',
83 "<meta name=viewport-fit content=cover>",
84 ])("injects a default viewport when metadata is only viewport-like: %s", (fragment) => {
85 const injected = injectHtmlRenderBootstrap(fragment);
86 expect(injected).toContain(
87 '<meta name="viewport" content="width=device-width, initial-scale=1">',
88 );
89 expect(injected.endsWith(fragment)).toBe(true);
90 });
91
92 it.each([
93 "<meta name='viewport' content='width=600'>",
94 "<meta name=viewport />",
95 '<meta data-x="1"name="viewport" content="width=600">',
96 '<meta data-if="a>b" name="viewport" content="width=600">',
97 ])("preserves a genuine viewport declaration: %s", (fragment) => {
98 const injected = injectHtmlRenderBootstrap(fragment);
99 expect(injected).not.toContain(
100 '<meta name="viewport" content="width=device-width, initial-scale=1">',
101 );
102 expect(injected.endsWith(fragment)).toBe(true);
103 });
104
105 it.each(["textarea", "iframe", "plaintext"])("keeps the bootstrap outside %s content", (tag) => {
106 const fragment = `<${tag}><head><meta name="viewport"></head></${tag}>`;
107 const injected = injectHtmlRenderBootstrap(fragment);
108 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
109 expect(injected.indexOf('<style id="t3-theme">')).toBeLessThan(injected.indexOf(`<${tag}>`));
110 expect(injected).toContain('<meta name="viewport" content="width=device-width');
111 expect(injected.endsWith(fragment)).toBe(true);
112 });
113
114 it.each([
115 '<template><head><meta name="viewport"></head></template>',
116 '<template><template>inner</template><head><meta name="viewport"></head></template>',
117 '<template data-if="a<b"><head><meta name="viewport"></head></template>',
118 ])("keeps the bootstrap outside inert template content: %s", (fragment) => {
119 const injected = injectHtmlRenderBootstrap(fragment);
120 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
121 expect(injected.indexOf('<style id="t3-theme">')).toBeLessThan(injected.indexOf("<template"));
122 expect(injected).toContain('<meta name="viewport" content="width=device-width');
123 expect(injected.endsWith(fragment)).toBe(true);
124 });
125
126 it.each([
127 "<template ",
128 "<template '",
129 "<meta ",
130 "<meta name ",
131 '<meta "',
132 "<head ",
133 "<head '",
134 '<html a="b" ',
135 ])("scans a run of unclosed %s tags in one pass", (tag) => {
136 const fragment = tag.repeat(2_000_000 / tag.length);
137 const injected = injectHtmlRenderBootstrap(fragment);
138 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
139 expect(injected.endsWith(fragment)).toBe(true);
140 });
141
142 it("ignores tags written inside comments and scripts", () => {
143 const html =
144 '<!-- copy <head> and <meta name="viewport"> here --><html><head>' +
145 "<script>const tag = '<meta name=\"viewport\">';</script></head><body>x</body></html>";
146 const injected = injectHtmlRenderBootstrap(html);
147 expect(injected.indexOf('<style id="t3-theme">')).toBeGreaterThan(
148 injected.indexOf("<html><head>"),
149 );
150 expect(injected).toContain('<meta name="viewport" content="width=device-width');
151 });
152});
153
154describe("readHtmlRenderLinkRequest", () => {
155 it("accepts only http(s) URLs in an MCP Apps ui/open-link request", () => {
156 const link = (url: unknown, modifiers = {}) => ({
157 jsonrpc: "2.0",
158 id: 1,
159 method: "ui/open-link",
160 params: { url, ...modifiers },
161 });
162 expect(readHtmlRenderLinkRequest(link("https://example.com/a"))).toEqual({
163 id: 1,
164 url: "https://example.com/a",
165 metaKey: false,
166 ctrlKey: false,
167 });
168 expect(
169 readHtmlRenderLinkRequest(link("https://example.com/a", { metaKey: true, ctrlKey: "yes" })),
170 ).toEqual({ id: 1, url: "https://example.com/a", metaKey: true, ctrlKey: false });
171 expect(readHtmlRenderLinkRequest(link("javascript:alert(1)"))).toBeUndefined();
172 expect(readHtmlRenderLinkRequest(link("file:///etc/passwd"))).toBeUndefined();
173 expect(
174 readHtmlRenderLinkRequest({
175 jsonrpc: "2.0",
176 method: "ui/open-link",
177 params: { url: "https://example.com" },
178 }),
179 ).toBeUndefined();
180 expect(
181 readHtmlRenderLinkRequest({ type: "t3-html-render-link", url: "https://example.com" }),
182 ).toBeUndefined();
183 });
184});
185
186describe("readHtmlRenderImageRequest", () => {
187 it("accepts http(s) and inline image URLs in an open-image notification", () => {
188 const image = (params: unknown) => ({
189 jsonrpc: "2.0",
190 method: "t3/notifications/open-image",
191 params,
192 });
193 expect(
194 readHtmlRenderImageRequest(image({ src: "data:image/png;base64,AA", name: " Chart " })),
195 ).toEqual({ images: [{ src: "data:image/png;base64,AA", name: "Chart" }], index: 0 });
196 expect(readHtmlRenderImageRequest(image({ src: "https://example.com/a.png" }))).toEqual({
197 images: [{ src: "https://example.com/a.png", name: "Image" }],
198 index: 0,
199 });
200 const boundaryImage = readHtmlRenderImageRequest(
201 image({ src: "https://example.com/a.png", name: "a".repeat(199) + "😀" }),
202 );
203 expect(boundaryImage?.images[0]?.name.isWellFormed()).toBe(true);
204 expect(boundaryImage?.images[0]?.name).toBe("a".repeat(199));
205 expect(readHtmlRenderImageRequest(image({ src: "javascript:alert(1)" }))).toBeUndefined();
206 expect(readHtmlRenderImageRequest(image({ src: "data:text/html,<p>" }))).toBeUndefined();
207 expect(readHtmlRenderImageRequest(image({ src: "blob:null/1" }))).toBeUndefined();
208 });
209
210 it("keeps the page's other images, dropping unsafe ones, around the clicked image", () => {
211 const b = { src: "https://example.com/b.png", name: "B" };
212 const params = {
213 ...b,
214 images: [{ src: "https://example.com/a.png", name: "A" }, { src: "blob:null/1" }, b, null],
215 index: 2,
216 };
217 const request = (overrides: object) =>
218 readHtmlRenderImageRequest({
219 jsonrpc: "2.0",
220 method: "t3/notifications/open-image",
221 params: { ...params, ...overrides },
222 });
223 expect(request({})).toEqual({
224 images: [{ src: "https://example.com/a.png", name: "A" }, b],
225 index: 1,
226 });
227 // An index that does not point at the clicked image falls back to that image alone.
228 expect(request({ index: 0 })).toEqual({ images: [b], index: 0 });
229 expect(request({ index: -1 })).toEqual({ images: [b], index: 0 });
230 });
231});
232
233describe("htmlRenderFromToolItem", () => {
234 const result = { htmlRender: reference, message: "Rendered above your reply." };
235
236 it("reads the reference from each provider's result envelope", () => {
237 for (const [toolName, output] of [
238 ["mcp__t3-code__html_render", [{ type: "text", text: JSON.stringify(result) }]],
239 ["t3-code.html_render", { structuredContent: result, content: [] }],
240 ["t3-code-thread_1_html_render", JSON.stringify(result)],
241 ["html_render", result],
242 // OpenCode 2 Code Mode reports a script's nested html_render call as one `execute` item.
243 ["execute", JSON.stringify(result)],
244 // Antigravity runs the `acp-mcp-call` terminal fallback and wraps its stdout.
245 [
246 "t3-code.html_render",
247 {
248 output: `Created At: 2026-11-19T02:54:02Z\nCompleted At: 2026-11-19T02:54:02Z\nCommand exited with code 0.\n\nCommand output:\n${JSON.stringify({ content: [], structuredContent: result, isError: false })}\r\n`,
249 exit_code: 0,
250 },
251 ],
252 ] as const) {
253 expect(htmlRenderFromToolItem({ toolName, output })).toEqual(reference);
254 }
255 });
256});