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

htmlRender.test.ts

201 lines8.7 KB

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