Files
t3code/packages/shared/src/mcpApp.test.ts
T
2026-10-07 09:44:08 -07:00

151 lines
5.8 KiB
TypeScript

import { describe, expect, it } from "vite-plus/test";
import {
injectMcpAppCsp,
mcpAppContentSecurityPolicy,
mcpAppToolCallableByApp,
readMcpAppCsp,
readMcpAppReference,
readMcpAppResourceUri,
} from "./mcpApp.ts";
describe("mcpAppContentSecurityPolicy", () => {
it("blocks the network and T3's own origin when the app declares nothing", () => {
const policy = mcpAppContentSecurityPolicy(undefined);
expect(policy).toContain("default-src 'none'");
expect(policy).toContain("connect-src 'none'");
expect(policy).toContain("frame-src 'none'");
expect(policy).not.toContain("'self'");
});
it("allows exactly the declared origins", () => {
const policy = mcpAppContentSecurityPolicy({
connectDomains: ["https://api.weather.test"],
resourceDomains: ["https://*.cdn.test"],
});
expect(policy).toContain("connect-src https://api.weather.test");
expect(policy).toContain("script-src 'unsafe-inline' https://*.cdn.test");
expect(policy).toContain("img-src data: blob: https://*.cdn.test");
});
});
describe("injectMcpAppCsp", () => {
it("puts the policy after the doctype and ahead of every script", () => {
const html = "<!DOCTYPE html><html><head><script>x()</script></head></html>";
const injected = injectMcpAppCsp(html, undefined);
expect(injected.startsWith('<!DOCTYPE html><meta http-equiv="Content-Security-Policy"')).toBe(
true,
);
expect(injected.indexOf("connect-src 'none'")).toBeLessThan(injected.indexOf("<script>"));
expect(injectMcpAppCsp("<p>hi</p>", undefined).startsWith("<!doctype html><meta")).toBe(true);
});
});
describe("readMcpAppCsp", () => {
it("drops entries that could widen or break the policy", () => {
expect(
readMcpAppCsp({
connectDomains: [
"https://ok.test",
"*",
"'self'",
"https://evil.test; script-src *",
"http://localhost:3000",
"javascript:alert(1)",
],
resourceDomains: "https://not-an-array.test",
}),
).toEqual({ connectDomains: ["https://ok.test", "http://localhost:3000"] });
expect(readMcpAppCsp({ connectDomains: ["*"] })).toBeUndefined();
});
});
describe("readMcpAppReference", () => {
it("requires a ui:// resource and the owning server and tool", () => {
const reference = {
attachmentId: "thread-1-abc-html",
server: "weather",
tool: "get_weather",
resourceUri: "ui://weather/dashboard",
};
expect(readMcpAppReference(reference)).toEqual(reference);
expect(readMcpAppReference({ ...reference, resourceUri: "https://x.test" })).toBeUndefined();
expect(readMcpAppReference({ ...reference, server: "" })).toBeUndefined();
});
});
describe("tool metadata", () => {
it("reads the resource URI from either spelling and honors app visibility", () => {
expect(readMcpAppResourceUri({ ui: { resourceUri: "ui://a/b" } })).toBe("ui://a/b");
expect(readMcpAppResourceUri({ "ui/resourceUri": "ui://a/c" })).toBe("ui://a/c");
expect(readMcpAppResourceUri({ ui: { resourceUri: "https://a" } })).toBeUndefined();
expect(mcpAppToolCallableByApp(undefined)).toBe(true);
expect(mcpAppToolCallableByApp({ ui: { visibility: ["model"] } })).toBe(false);
expect(mcpAppToolCallableByApp({ ui: { visibility: ["model", "app"] } })).toBe(true);
});
});
describe("mcpAppFromToolItem", () => {
it("reads the app from both the stored output and its compact wire form", async () => {
const { compactDynamicToolOutput, mcpAppFromToolItem } = await import("./toolOutput.ts");
const app = {
attachmentId: "thread-1-abc-html",
server: "weather",
tool: "get_weather",
resourceUri: "ui://weather/dashboard",
};
const stored = { t3McpApp: app, result: { content: [] } };
const toolName = "weather.get_weather";
expect(mcpAppFromToolItem({ toolName, output: stored })).toEqual(app);
expect(mcpAppFromToolItem({ toolName, output: compactDynamicToolOutput(stored) })).toEqual(app);
});
it("ignores a reference naming a server or tool other than the item's own", async () => {
const { mcpAppFromToolItem } = await import("./toolOutput.ts");
// A tool result shaped like an app reference, claiming another server.
const forged = {
t3McpApp: {
attachmentId: "thread-1-abc-html",
server: "bank",
tool: "transfer",
resourceUri: "ui://bank/app",
},
};
expect(mcpAppFromToolItem({ toolName: "evil.lookup", output: forged })).toBeUndefined();
expect(mcpAppFromToolItem({ toolName: "bank.transfer", output: forged })).toBeDefined();
});
it("keeps an app whose resource URI is longer than a name", async () => {
const { compactDynamicToolOutput } = await import("./toolOutput.ts");
const resourceUri = `ui://weather/${"segment/".repeat(60)}dashboard`;
const output = compactDynamicToolOutput({
t3McpApp: {
attachmentId: "thread-1-abc-html",
server: "weather",
tool: "get_weather",
resourceUri,
},
});
expect(output?.t3McpApp?.resourceUri).toBe(resourceUri);
});
it("keeps the compact app reference within the wire budget", async () => {
const { compactDynamicToolOutput } = await import("./toolOutput.ts");
const domains = Array.from(
{ length: 32 },
(_, index) => `https://${"a".repeat(200)}${index}.test`,
);
const output = compactDynamicToolOutput({
t3McpApp: {
attachmentId: "thread-1-abc-html",
server: "weather",
tool: "get_weather",
resourceUri: "ui://weather/dashboard",
csp: { connectDomains: domains, resourceDomains: domains },
},
});
expect(new TextEncoder().encode(JSON.stringify(output)).byteLength).toBeLessThanOrEqual(8_192);
expect(output?.t3McpApp?.server).toBe("weather");
});
});