Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 htmlRenderFileName,
5 injectHtmlRenderBootstrap,
6 readHtmlRenderImageRequest,
7 readHtmlRenderLinkRequest,
8} from "./htmlRender.ts";
9import { htmlRenderFromToolItem } from "./toolOutput.ts";
10
11const reference = { attachmentId: "thread-abc-123.html", title: "Chart", height: 420 };
12
13describe("htmlRenderFileName", () => {
14 it("never ends on half of an emoji at the length limit", () => {
15 const fits = "a".repeat(118);
16 const crosses = "a".repeat(119);
17 expect(htmlRenderFileName(`${fits}🚀 report`)).toBe(`${fits}🚀.html`);
18 expect(htmlRenderFileName(`${crosses}🚀 report`)).toBe(`${crosses}.html`);
19 });
20});
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.each([
35 "<html><head data-condition='x > 0'>",
36 '<html><head data-condition="x > 0" data-label=\'it "works"\'>',
37 "<html data-condition='x > 0'>",
38 '<html data-condition="x > 0" data-label="it \'works\'">',
39 ])("keeps the bootstrap after the complete opening tag: %s", (opening) => {
40 const prefix = `<!doctype html>${opening}`;
41 const head = opening.startsWith("<html><head") ? "" : "<head>";
42 const content = `${head ? "" : "<title>Report</title></head>"}<body><p>Report</p></body></html>`;
43 const injected = injectHtmlRenderBootstrap(prefix + content);
44 expect(injected.startsWith(`${prefix}${head}<meta charset="utf-8">`)).toBe(true);
45 expect(injected.indexOf('<style id="t3-theme">')).toBeGreaterThan(prefix.length);
46 expect(injected).toContain("<script>");
47 expect(injected.endsWith(content)).toBe(true);
48 });
49
50 it("wraps fragments without a head and keeps existing meta tags", () => {
51 const fragment =
52 '<meta charset="utf-8"><meta data-if="a<b" name="viewport" content="width=device-width"><p>hi</p>';
53 const injected = injectHtmlRenderBootstrap(fragment);
54 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
55 expect(injected.match(/charset/g)).toHaveLength(1);
56 expect(injected.match(/name="viewport"/g)).toHaveLength(1);
57 expect(injected.endsWith("<p>hi</p>")).toBe(true);
58 });
59
60 it.each(["textarea", "iframe", "plaintext"])("keeps the bootstrap outside %s content", (tag) => {
61 const fragment = `<${tag}><head><meta name="viewport"></head></${tag}>`;
62 const injected = injectHtmlRenderBootstrap(fragment);
63 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
64 expect(injected.indexOf('<style id="t3-theme">')).toBeLessThan(injected.indexOf(`<${tag}>`));
65 expect(injected).toContain('<meta name="viewport" content="width=device-width');
66 expect(injected.endsWith(fragment)).toBe(true);
67 });
68
69 it.each([
70 '<template><head><meta name="viewport"></head></template>',
71 '<template><template>inner</template><head><meta name="viewport"></head></template>',
72 '<template data-if="a<b"><head><meta name="viewport"></head></template>',
73 ])("keeps the bootstrap outside inert template content: %s", (fragment) => {
74 const injected = injectHtmlRenderBootstrap(fragment);
75 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
76 expect(injected.indexOf('<style id="t3-theme">')).toBeLessThan(injected.indexOf("<template"));
77 expect(injected).toContain('<meta name="viewport" content="width=device-width');
78 expect(injected.endsWith(fragment)).toBe(true);
79 });
80
81 it.each([
82 "<template ",
83 "<template '",
84 "<meta ",
85 "<meta name ",
86 '<meta "',
87 "<head ",
88 "<head '",
89 '<html a="b" ',
90 ])("scans a run of unclosed %s tags in one pass", (tag) => {
91 const fragment = tag.repeat(2_000_000 / tag.length);
92 const injected = injectHtmlRenderBootstrap(fragment);
93 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
94 expect(injected.endsWith(fragment)).toBe(true);
95 });
96
97 it("ignores tags written inside comments and scripts", () => {
98 const html =
99 '<!-- copy <head> and <meta name="viewport"> here --><html><head>' +
100 "<script>const tag = '<meta name=\"viewport\">';</script></head><body>x</body></html>";
101 const injected = injectHtmlRenderBootstrap(html);
102 expect(injected.indexOf('<style id="t3-theme">')).toBeGreaterThan(
103 injected.indexOf("<html><head>"),
104 );
105 expect(injected).toContain('<meta name="viewport" content="width=device-width');
106 });
107});
108
109describe("readHtmlRenderLinkRequest", () => {
110 it("accepts only http(s) URLs in an MCP Apps ui/open-link request", () => {
111 const link = (url: unknown, modifiers = {}) => ({
112 jsonrpc: "2.0",
113 id: 1,
114 method: "ui/open-link",
115 params: { url, ...modifiers },
116 });
117 expect(readHtmlRenderLinkRequest(link("https://example.com/a"))).toEqual({
118 id: 1,
119 url: "https://example.com/a",
120 metaKey: false,
121 ctrlKey: false,
122 });
123 expect(
124 readHtmlRenderLinkRequest(link("https://example.com/a", { metaKey: true, ctrlKey: "yes" })),
125 ).toEqual({ id: 1, url: "https://example.com/a", metaKey: true, ctrlKey: false });
126 expect(readHtmlRenderLinkRequest(link("javascript:alert(1)"))).toBeUndefined();
127 expect(readHtmlRenderLinkRequest(link("file:///etc/passwd"))).toBeUndefined();
128 expect(
129 readHtmlRenderLinkRequest({
130 jsonrpc: "2.0",
131 method: "ui/open-link",
132 params: { url: "https://example.com" },
133 }),
134 ).toBeUndefined();
135 expect(
136 readHtmlRenderLinkRequest({ type: "t3-html-render-link", url: "https://example.com" }),
137 ).toBeUndefined();
138 });
139});
140
141describe("readHtmlRenderImageRequest", () => {
142 it("accepts http(s) and inline image URLs in an open-image notification", () => {
143 const image = (params: unknown) => ({
144 jsonrpc: "2.0",
145 method: "t3/notifications/open-image",
146 params,
147 });
148 expect(
149 readHtmlRenderImageRequest(image({ src: "data:image/png;base64,AA", name: " Chart " })),
150 ).toEqual({ images: [{ src: "data:image/png;base64,AA", name: "Chart" }], index: 0 });
151 expect(readHtmlRenderImageRequest(image({ src: "https://example.com/a.png" }))).toEqual({
152 images: [{ src: "https://example.com/a.png", name: "Image" }],
153 index: 0,
154 });
155 const boundaryImage = readHtmlRenderImageRequest(
156 image({ src: "https://example.com/a.png", name: "a".repeat(199) + "😀" }),
157 );
158 expect(boundaryImage?.images[0]?.name.isWellFormed()).toBe(true);
159 expect(boundaryImage?.images[0]?.name).toBe("a".repeat(199));
160 expect(readHtmlRenderImageRequest(image({ src: "javascript:alert(1)" }))).toBeUndefined();
161 expect(readHtmlRenderImageRequest(image({ src: "data:text/html,<p>" }))).toBeUndefined();
162 expect(readHtmlRenderImageRequest(image({ src: "blob:null/1" }))).toBeUndefined();
163 });
164
165 it("keeps the page's other images, dropping unsafe ones, around the clicked image", () => {
166 const b = { src: "https://example.com/b.png", name: "B" };
167 const params = {
168 ...b,
169 images: [{ src: "https://example.com/a.png", name: "A" }, { src: "blob:null/1" }, b, null],
170 index: 2,
171 };
172 const request = (overrides: object) =>
173 readHtmlRenderImageRequest({
174 jsonrpc: "2.0",
175 method: "t3/notifications/open-image",
176 params: { ...params, ...overrides },
177 });
178 expect(request({})).toEqual({
179 images: [{ src: "https://example.com/a.png", name: "A" }, b],
180 index: 1,
181 });
182 // An index that does not point at the clicked image falls back to that image alone.
183 expect(request({ index: 0 })).toEqual({ images: [b], index: 0 });
184 expect(request({ index: -1 })).toEqual({ images: [b], index: 0 });
185 });
186});
187
188describe("htmlRenderFromToolItem", () => {
189 const result = { htmlRender: reference, message: "Rendered above your reply." };
190
191 it("reads the reference from each provider's result envelope", () => {
192 for (const [toolName, output] of [
193 ["mcp__t3-code__html_render", [{ type: "text", text: JSON.stringify(result) }]],
194 ["t3-code.html_render", { structuredContent: result, content: [] }],
195 ["t3-code-thread_1_html_render", JSON.stringify(result)],
196 ["html_render", result],
197 // OpenCode 2 Code Mode reports a script's nested html_render call as one `execute` item.
198 ["execute", JSON.stringify(result)],
199 // Antigravity runs the `acp-mcp-call` terminal fallback and wraps its stdout.
200 [
201 "t3-code.html_render",
202 {
203 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`,
204 exit_code: 0,
205 },
206 ],
207 ] as const) {
208 expect(htmlRenderFromToolItem({ toolName, output })).toEqual(reference);
209 }
210 });
211});