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

htmlRender.test.ts

134 lines5.6 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("wraps fragments without a head and keeps existing meta tags", () => {
25 const fragment =
26 '<meta charset="utf-8"><meta name="viewport" content="width=device-width"><p>hi</p>';
27 const injected = injectHtmlRenderBootstrap(fragment);
28 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
29 expect(injected.match(/charset/g)).toHaveLength(1);
30 expect(injected.match(/name="viewport"/g)).toHaveLength(1);
31 expect(injected.endsWith("<p>hi</p>")).toBe(true);
32 });
33​
34 it.each(["textarea", "iframe", "plaintext"])("keeps the bootstrap outside %s content", (tag) => {
35 const fragment = `<${tag}><head><meta name="viewport"></head></${tag}>`;
36 const injected = injectHtmlRenderBootstrap(fragment);
37 expect(injected.startsWith("<!doctype html><head>")).toBe(true);
38 expect(injected.indexOf('<style id="t3-theme">')).toBeLessThan(injected.indexOf(`<${tag}>`));
39 expect(injected).toContain('<meta name="viewport" content="width=device-width');
40 expect(injected.endsWith(fragment)).toBe(true);
41 });
42​
43 it.each([
44 '<template><head><meta name="viewport"></head></template>',
45 '<template><template>inner</template><head><meta name="viewport"></head></template>',
46 ])("keeps the bootstrap outside inert template content: %s", (fragment) => {
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("<template>"));
50 expect(injected).toContain('<meta name="viewport" content="width=device-width');
51 expect(injected.endsWith(fragment)).toBe(true);
52 });
53​
54 it("ignores tags written inside comments and scripts", () => {
55 const html =
56 '<!-- copy <head> and <meta name="viewport"> here --><html><head>' +
57 "<script>const tag = '<meta name=\"viewport\">';</script></head><body>x</body></html>";
58 const injected = injectHtmlRenderBootstrap(html);
59 expect(injected.indexOf('<style id="t3-theme">')).toBeGreaterThan(
60 injected.indexOf("<html><head>"),
61 );
62 expect(injected).toContain('<meta name="viewport" content="width=device-width');
63 });
64});
65​
66describe("readHtmlRenderLinkRequest", () => {
67 it("accepts only http(s) URLs in an MCP Apps ui/open-link request", () => {
68 const link = (url: unknown, modifiers = {}) => ({
69 jsonrpc: "2.0",
70 id: 1,
71 method: "ui/open-link",
72 params: { url, ...modifiers },
73 });
74 expect(readHtmlRenderLinkRequest(link("https://example.com/a"))).toEqual({
75 id: 1,
76 url: "https://example.com/a",
77 metaKey: false,
78 ctrlKey: false,
79 });
80 expect(
81 readHtmlRenderLinkRequest(link("https://example.com/a", { metaKey: true, ctrlKey: "yes" })),
82 ).toEqual({ id: 1, url: "https://example.com/a", metaKey: true, ctrlKey: false });
83 expect(readHtmlRenderLinkRequest(link("javascript:alert(1)"))).toBeUndefined();
84 expect(readHtmlRenderLinkRequest(link("file:///etc/passwd"))).toBeUndefined();
85 expect(
86 readHtmlRenderLinkRequest({
87 jsonrpc: "2.0",
88 method: "ui/open-link",
89 params: { url: "https://example.com" },
90 }),
91 ).toBeUndefined();
92 expect(
93 readHtmlRenderLinkRequest({ type: "t3-html-render-link", url: "https://example.com" }),
94 ).toBeUndefined();
95 });
96});
97​
98describe("readHtmlRenderImageRequest", () => {
99 it("accepts http(s) and inline image URLs in an open-image notification", () => {
100 const image = (params: unknown) => ({
101 jsonrpc: "2.0",
102 method: "t3/notifications/open-image",
103 params,
104 });
105 expect(
106 readHtmlRenderImageRequest(image({ src: "data:image/png;base64,AA", name: " Chart " })),
107 ).toEqual({
108 src: "data:image/png;base64,AA",
109 name: "Chart",
110 });
111 expect(readHtmlRenderImageRequest(image({ src: "https://example.com/a.png" }))).toEqual({
112 src: "https://example.com/a.png",
113 name: "Image",
114 });
115 expect(readHtmlRenderImageRequest(image({ src: "javascript:alert(1)" }))).toBeUndefined();
116 expect(readHtmlRenderImageRequest(image({ src: "data:text/html,<p>" }))).toBeUndefined();
117 expect(readHtmlRenderImageRequest(image({ src: "blob:null/1" }))).toBeUndefined();
118 });
119});
120​
121describe("htmlRenderFromToolItem", () => {
122 const result = { htmlRender: reference, message: "Rendered above your reply." };
123​
124 it("reads the reference from each provider's result envelope", () => {
125 for (const [toolName, output] of [
126 ["mcp__t3-code__html_render", [{ type: "text", text: JSON.stringify(result) }]],
127 ["t3-code.html_render", { structuredContent: result, content: [] }],
128 ["t3-code-thread_1_html_render", JSON.stringify(result)],
129 ["html_render", result],
130 ] as const) {
131 expect(htmlRenderFromToolItem({ toolName, output })).toEqual(reference);
132 }
133 });
134});