Files
WebSpeak-client-for-TeamSpeak/web/src/services/skin-extension-ui.test.ts
T

805 lines
35 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { createRenderer, createSSRApp, defineComponent, h, nextTick, ref } from "vue";
import { renderToString } from "@vue/server-renderer";
import { parseSkinPluginDocument, type SkinPluginDocument } from "../../../src/shared/skin-plugin.js";
import { approveSkinPluginComponents } from "./skin-plugin-approval.js";
import {
parseSkinExtensionUiActionRequest,
parseSkinExtensionUiOutput,
SKIN_EXTENSION_UI_OUTPUT_LIMIT_BYTES,
SkinExtensionUiOutputError,
} from "../../../src/shared/skin-extension-ui.js";
import SkinPluginOutlet from "../components/SkinPluginOutlet.js";
function outputDocument(componentId = "runtime-ui", root?: Record<string, unknown>, permissions: string[] = [], actions: Record<string, unknown> = {}, state?: Record<string, unknown>): string {
return JSON.stringify({
schemaVersion: 3,
components: [{
id: componentId,
name: "Runtime UI",
page: "voice",
accessibleName: "Runtime UI",
permissions,
actions,
...(state ? { state } : {}),
root: root ?? { tag: "section", children: [{ text: "Generated by Wasm" }] },
}],
});
}
function generatedOutput(componentId = "runtime-ui", root: Record<string, unknown> = { tag: "main", children: [{ text: "Generated by Wasm" }] }): string {
return JSON.stringify({
schemaVersion: 4,
components: [{ id: componentId, name: "Runtime UI", page: "voice", accessibleName: "Runtime UI", root }],
});
}
function expectOutputError(source: string, code: string): void {
assert.throws(() => parseSkinExtensionUiOutput(source), (error: unknown) =>
error instanceof SkinExtensionUiOutputError && error.code === code);
}
test("Wasm UI output uses the v3 schema and supports local-only interaction", () => {
const document = parseSkinExtensionUiOutput(outputDocument("runtime-ui", {
tag: "section",
children: [
{ tag: "button", attributes: { type: "button" }, events: { click: "toggle" }, children: [{ text: "Toggle" }] },
{ tag: "p", when: { path: "state.open", equals: true }, children: [{ text: "Expanded" }] },
],
}, [], { toggle: { type: "ui.toggleState", args: { key: "open" } } }, { open: false }));
assert.equal(document.schemaVersion, 3);
assert.equal(document.components[0].actions.toggle.type, "ui.toggleState");
});
test("scoped local state keeps each voice channel's chat visibility independent", async () => {
const skinId = "community.kaak-state-scope-test";
const skinVersion = "1.0.0";
const document = parseSkinPluginDocument({
schemaVersion: 3,
components: [{
id: "voice-workspace",
name: "Voice workspace",
page: "voice",
accessibleName: "Voice workspace",
permissions: ["session.status.read"],
stateScope: "session.status.channelId",
state: { chatopen: true, roommenu: true },
actions: { toggle: { type: "ui.toggleState", args: { key: "chatopen", resetKey: "roommenu" } } },
root: { tag: "main", children: [
{ tag: "button", attributes: { type: "button" }, events: { click: "toggle" }, children: [{ text: "Toggle chat" }] },
{ tag: "span", when: { path: "state.chatopen", equals: true }, children: [{ text: "Open" }] },
{ tag: "span", when: { path: "state.chatopen", equals: false }, children: [{ text: "Closed" }] },
{ tag: "span", when: { path: "state.roommenu", equals: true }, children: [{ text: "Menu open" }] },
] },
}],
});
const storage = new Map<string, string>();
const previousStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage");
Object.defineProperty(globalThis, "localStorage", {
configurable: true,
value: {
getItem: (key: string) => storage.get(key) ?? null,
setItem: (key: string, value: string) => { storage.set(key, value); },
},
});
try {
approveSkinPluginComponents(skinId, skinVersion, document.components);
interface HostNode { type: string; props: Record<string, unknown>; text: string; children: HostNode[]; parent?: HostNode }
const createHostNode = (type: string, text = ""): HostNode => ({ type, props: {}, text, children: [] });
const body = createHostNode("body");
const renderer = createRenderer<HostNode, HostNode>({
patchProp(node, key, _previous, next) { node.props[key] = next; },
insert(node, parent, anchor) {
node.parent = parent;
const oldIndex = parent.children.indexOf(node);
if (oldIndex >= 0) parent.children.splice(oldIndex, 1);
const anchorIndex = anchor ? parent.children.indexOf(anchor) : -1;
parent.children.splice(anchorIndex >= 0 ? anchorIndex : parent.children.length, 0, node);
},
remove(node) {
if (!node.parent) return;
const index = node.parent.children.indexOf(node);
if (index >= 0) node.parent.children.splice(index, 1);
node.parent = undefined;
},
createElement: (type) => createHostNode(type),
createText: (text) => createHostNode("#text", text),
createComment: (text) => createHostNode("#comment", text),
setText(node, text) { node.text = text; },
setElementText(node, text) { node.children = []; node.text = text; },
parentNode: (node) => node.parent ?? null,
querySelector: (selector) => selector === "body" ? body : null,
nextSibling(node) {
if (!node.parent) return null;
return node.parent.children[node.parent.children.indexOf(node) + 1] ?? null;
},
});
const channelId = ref("voice-a");
const outlet = defineComponent(() => () => h(SkinPluginOutlet, {
skinId,
skinVersion,
document,
page: "voice",
data: { session: { status: { channelId: channelId.value } } },
assets: {},
actions: {},
}));
const host = createHostNode("root");
const app = renderer.createApp(outlet);
const textContent = (node: HostNode): string => node.text + node.children.map(textContent).join("");
const findButton = (node: HostNode): HostNode | undefined => node.type === "button" ? node : node.children.map(findButton).find(Boolean);
const toggle = async () => {
const button = findButton(host);
assert.ok(button);
(button.props.onClick as (event: Event) => void)({ isTrusted: true, target: null } as unknown as Event);
await nextTick();
};
app.mount(host);
await nextTick();
assert.match(textContent(host), /Open/);
assert.match(textContent(host), /Menu open/);
await toggle();
assert.match(textContent(host), /Closed/);
assert.doesNotMatch(textContent(host), /Menu open/, "a page toggle can close a related transient menu");
channelId.value = "voice-b";
await nextTick();
assert.match(textContent(host), /Open/, "a new channel starts from the declared default");
await toggle();
channelId.value = "voice-a";
await nextTick();
assert.match(textContent(host), /Closed/, "returning to a channel restores its own visibility state");
app.unmount();
} finally {
if (previousStorage) Object.defineProperty(globalThis, "localStorage", previousStorage);
else Reflect.deleteProperty(globalThis, "localStorage");
}
});
test("Wasm UI output rejects malformed, oversized, legacy, and capability-bearing documents", () => {
expectOutputError("{", "SKIN_EXTENSION_UI_OUTPUT_JSON");
expectOutputError(" ".repeat(SKIN_EXTENSION_UI_OUTPUT_LIMIT_BYTES + 1), "SKIN_EXTENSION_UI_OUTPUT_SIZE");
expectOutputError(JSON.stringify({ schemaVersion: 2, components: [{
id: "runtime-ui", name: "Runtime UI", page: "voice", accessibleName: "Runtime UI", permissions: [], actions: {}, root: { tag: "section" },
}] }), "SKIN_EXTENSION_UI_SCHEMA_UNSUPPORTED");
expectOutputError(outputDocument("runtime-ui", undefined, ["session.status.read"]), "SKIN_EXTENSION_UI_CAPABILITY_UNSUPPORTED");
expectOutputError(outputDocument("runtime-ui", undefined, [], { disconnect: { type: "voice.disconnect", args: {} } }), "SKIN_EXTENSION_UI_OUTPUT_SCHEMA");
expectOutputError(outputDocument("runtime-ui", { widget: "voice.audio-controls" }), "SKIN_EXTENSION_UI_WIDGET_UNSUPPORTED");
expectOutputError(outputDocument("runtime-ui", { tag: "p", text: "{{session.status.channelName}}" }), "SKIN_EXTENSION_UI_OUTPUT_SCHEMA");
});
test("generated UI supports broad safe markup, ARIA/data attributes, and inert custom-element placeholders", () => {
const document = parseSkinExtensionUiOutput(generatedOutput("kook-rail", {
tag: "kook-server-rail",
attributes: { "aria-label": "Servers", "data-selected": "home", role: "navigation" },
children: [
{ tag: "a", attributes: { href: "#voice" }, children: [{ text: "Voice" }] },
{ tag: "button", attributes: { type: "button", "aria-pressed": true }, children: [{ text: "Home" }] },
],
}));
assert.equal(document.schemaVersion, 3);
assert.equal(document.components[0].root.tag, "kook-server-rail");
assert.equal(document.components[0].root.children?.[0].tag, "a");
assert.equal(document.components[0].root.children?.[1].tag, "button");
});
test("generated UI supports standard elements and attributes without a fixed presentation allowlist", () => {
const document = parseSkinExtensionUiOutput(generatedOutput("custom-layout", {
tag: "dialog",
attributes: { open: true, inert: true, "aria-roledescription": "voice panel", "data-layout-state": "expanded" },
children: [{ tag: "menu", attributes: { type: "toolbar", "aria-label": "Voice tools" }, children: [{ tag: "picture" }] }],
}));
assert.equal(document.components[0].root.tag, "dialog");
assert.equal(document.components[0].root.attributes?.inert, true);
assert.equal(document.components[0].root.children?.[0].tag, "menu");
assert.equal(document.components[0].root.children?.[0].children?.[0].tag, "picture");
});
test("generated SVG restores case-sensitive standard attribute names in host-rendered markup", async () => {
const html = await renderToString(createSSRApp(SkinPluginOutlet, {
skinId: "community.test",
skinVersion: "1.0.0",
document: { schemaVersion: 3, components: [] },
extensionOutput: generatedOutput("svg-ui", {
tag: "svg",
attributes: { viewbox: "0 0 24 24", preserveaspectratio: "xMidYMid meet" },
children: [{ tag: "linearGradient", attributes: { id: "paint", gradientunits: "userSpaceOnUse" } }],
}),
page: "voice",
data: {},
assets: {},
actions: {},
}));
assert.match(html, /viewBox="0 0 24 24"/);
assert.match(html, /preserveAspectRatio="xMidYMid meet"/);
assert.match(html, /gradientUnits="userSpaceOnUse"/);
});
test("host adds a trusted server-add control to custom server rails in every skin", async () => {
for (const part of ["server-rail", "quick-rail"]) {
const html = await renderToString(createSSRApp(SkinPluginOutlet, {
skinId: "community.server-add-test",
skinVersion: "1.0.0",
document: { schemaVersion: 3, components: [] },
extensionOutput: generatedOutput("voice-workspace", { tag: "nav", part, children: [] }),
page: "voice",
data: {},
assets: {},
actions: {},
serverAddLabel: "Add server",
}));
assert.match(html, /data-ws-plugin-part="voice-workspace\.server-add"/);
assert.match(html, /aria-label="Add server"/);
}
const homeHtml = await renderToString(createSSRApp(SkinPluginOutlet, {
skinId: "community.server-add-test",
skinVersion: "1.0.0",
document: { schemaVersion: 3, components: [] },
extensionOutput: generatedOutput("home-workspace", { tag: "nav", part: "server-rail", children: [] }),
page: "home",
data: {},
assets: {},
actions: {},
}));
assert.doesNotMatch(homeHtml, /\.server-add/);
const restrictedQuickServerHtml = await renderToString(createSSRApp(SkinPluginOutlet, {
skinId: "community.server-add-test",
skinVersion: "1.0.0",
document: { schemaVersion: 3, components: [] },
extensionOutput: generatedOutput("voice-workspace", {
tag: "nav",
part: "server-rail",
children: [{ tag: "button", part: "quick-server", attributes: { type: "button", disabled: true } }],
}),
page: "voice",
data: { server: { switchRestricted: true, switchTitle: "管理员不允许切换TS服务器" } },
assets: {},
actions: {},
}));
assert.match(restrictedQuickServerHtml, /aria-disabled="true"/);
assert.match(restrictedQuickServerHtml, /title="管理员不允许切换TS服务器"/);
assert.doesNotMatch(restrictedQuickServerHtml, /\sdisabled(?:="(?:true|disabled)?")?(?:\s|>)/);
type HostNode = { type: string; props: Record<string, unknown>; text: string; children: HostNode[]; parent?: HostNode };
const createHostNode = (type: string, text = ""): HostNode => ({ type, props: {}, text, children: [] });
const renderer = createRenderer<HostNode, HostNode>({
patchProp(node, key, _previous, next) { node.props[key] = next; },
insert(node, parent, anchor) {
node.parent = parent;
const oldIndex = parent.children.indexOf(node);
if (oldIndex >= 0) parent.children.splice(oldIndex, 1);
const anchorIndex = anchor ? parent.children.indexOf(anchor) : -1;
parent.children.splice(anchorIndex >= 0 ? anchorIndex : parent.children.length, 0, node);
},
remove(node) {
if (!node.parent) return;
const index = node.parent.children.indexOf(node);
if (index >= 0) node.parent.children.splice(index, 1);
node.parent = undefined;
},
createElement: (type) => createHostNode(type),
createText: (text) => createHostNode("#text", text),
createComment: (text) => createHostNode("#comment", text),
setText(node, text) { node.text = text; },
setElementText(node, text) { node.children = []; node.text = text; },
parentNode: (node) => node.parent ?? null,
nextSibling(node) {
if (!node.parent) return null;
return node.parent.children[node.parent.children.indexOf(node) + 1] ?? null;
},
});
let addCount = 0;
const app = renderer.createApp(SkinPluginOutlet, {
skinId: "community.server-add-test",
skinVersion: "1.0.0",
document: { schemaVersion: 3, components: [] },
extensionOutput: generatedOutput("voice-workspace", { tag: "nav", part: "server-rail", children: [] }),
page: "voice",
data: {},
assets: {},
actions: {},
onAddServer: () => { addCount += 1; },
});
const host = createHostNode("root");
app.mount(host);
await nextTick();
const findAddButton = (node: HostNode): HostNode | undefined => {
if (node.type === "button" && node.props["data-ws-plugin-part"] === "voice-workspace.server-add") return node;
return node.children.map(findAddButton).find(Boolean);
};
const addButton = findAddButton(host);
assert.ok(addButton);
(addButton.props.onClick as (event: Event) => void)({ isTrusted: false } as Event);
assert.equal(addCount, 0, "synthetic events cannot open the host dialog");
(addButton.props.onClick as (event: Event) => void)({ isTrusted: true } as Event);
assert.equal(addCount, 1, "a trusted click opens the existing host add-server flow");
app.unmount();
});
test("generated UI can exceed the former 16 KiB output ceiling while staying within the new package-sized limit", () => {
const source = JSON.stringify({
schemaVersion: 4,
components: [{
id: "expanded-ui",
name: "Expanded UI",
page: "voice",
accessibleName: "Expanded UI",
root: { tag: "main", children: Array.from({ length: 4 }, () => ({ text: "x".repeat(7_000) })) },
}],
});
assert.ok(new TextEncoder().encode(source).byteLength > 16 * 1024);
assert.ok(parseSkinExtensionUiOutput(source).components.length === 1);
});
test("generated screen-share host widget requires permission and renders as an interactive host control", async () => {
const source = generatedOutput("share-control", {
tag: "section",
children: [{ widget: "voice.screen-share-start", part: "start-share" }],
});
expectOutputError(source, "SKIN_EXTENSION_UI_PERMISSION_MISSING");
const permissions = ["voice.screenShare.control"] as const;
const document = parseSkinExtensionUiOutput(source, { permissions });
assert.equal(document.components[0].root.children?.[0].widget, "voice.screen-share-start");
const html = await renderToString(createSSRApp(SkinPluginOutlet, {
skinId: "community.test",
skinVersion: "1.0.0",
document: { schemaVersion: 3, components: [] },
extensionOutput: source,
extensionPermissions: permissions,
page: "voice",
data: {},
assets: {},
actions: {},
widgets: { "voice.screen-share-start": () => "Start screen share" },
}));
assert.match(html, /data-ws-plugin-widget="voice\.screen-share-start"/);
assert.match(html, /data-ws-part="skin\.voice\.share-control\.control-part-start-share"/);
assert.match(html, /Start screen share/);
});
test("generated UI schema 5 carries bounded state and named callbacks for a trusted host event loop", () => {
const source = JSON.stringify({
schemaVersion: 5,
components: [{
id: "server-rail",
name: "Server rail",
page: "voice",
accessibleName: "Saved voice servers",
state: { expanded: false, selected: "home" },
root: {
tag: "nav",
children: [{
tag: "button",
attributes: { type: "button", "aria-pressed": false },
events: { click: "select-server" },
children: [{ text: "Home" }],
}],
},
}],
});
const document = parseSkinExtensionUiOutput(source);
const component = document.components[0];
assert.equal(component.runtimeCallbacks, true);
assert.deepEqual({ ...component.state }, { expanded: false, selected: "home" });
assert.equal(component.root.children?.[0].events?.click, "select-server");
});
test("generated UI schema 6 validates one host action against plugin permissions", () => {
const source = JSON.stringify({
schemaVersion: 6,
components: [{
id: "voice-channels",
name: "Voice channels",
page: "voice",
accessibleName: "Voice channels",
state: {},
root: { tag: "button", events: { dblclick: "join" }, children: [{ text: "Join" }] },
}],
request: { type: "voice.joinChannel", args: { channelId: "channel-1" } },
});
const document = parseSkinExtensionUiOutput(source);
assert.equal(document.components[0].runtimeCallbacks, true);
const action = parseSkinExtensionUiActionRequest(source, ["session.channel.join"]);
assert.deepEqual(action && { type: action.type, args: { ...action.args } }, {
type: "voice.joinChannel",
args: { channelId: "channel-1" },
});
assert.throws(() => parseSkinExtensionUiActionRequest(source, []), (error: unknown) =>
error instanceof SkinExtensionUiOutputError && error.code === "SKIN_EXTENSION_UI_ACTION_INVALID");
expectOutputError(JSON.stringify({
schemaVersion: 6,
components: [{ id: "voice-channels", name: "Voice channels", page: "voice", accessibleName: "Voice channels", root: { tag: "main" } }],
request: { type: "voice.joinChannel", args: { channelId: "channel-1", address: "private.example" } },
}), "SKIN_EXTENSION_UI_ACTION_INVALID");
});
test("generated UI callbacks reject unsupported events, malformed IDs, and password inputs", () => {
const schema5 = (root: Record<string, unknown>) => JSON.stringify({
schemaVersion: 5,
components: [{ id: "runtime-ui", name: "Runtime UI", page: "voice", accessibleName: "Runtime UI", root }],
});
expectOutputError(schema5({ tag: "button", events: { animationstart: "run" } }), "SKIN_EXTENSION_UI_OUTPUT_SCHEMA");
expectOutputError(schema5({ tag: "button", events: { click: "../run" } }), "SKIN_EXTENSION_UI_OUTPUT_SCHEMA");
expectOutputError(schema5({ tag: "input", attributes: { type: "password" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "button", events: { click: "run" } }), "SKIN_EXTENSION_UI_OUTPUT_SCHEMA");
});
test("SkinPluginOutlet emits only trusted callbacks and gates user-entered values by permission", async () => {
interface HostNode {
type: string;
props: Record<string, unknown>;
text: string;
children: HostNode[];
parent?: HostNode;
}
const createHostNode = (type: string, text = ""): HostNode => ({ type, props: {}, text, children: [] });
const renderer = createRenderer<HostNode, HostNode>({
patchProp(node, key, _previous, next) { node.props[key] = next; },
insert(node, parent, anchor) {
node.parent = parent;
const oldIndex = parent.children.indexOf(node);
if (oldIndex >= 0) parent.children.splice(oldIndex, 1);
const anchorIndex = anchor ? parent.children.indexOf(anchor) : -1;
parent.children.splice(anchorIndex >= 0 ? anchorIndex : parent.children.length, 0, node);
},
remove(node) {
const parent = node.parent;
if (!parent) return;
const index = parent.children.indexOf(node);
if (index >= 0) parent.children.splice(index, 1);
node.parent = undefined;
},
createElement: (type) => createHostNode(type),
createText: (text) => createHostNode("#text", text),
createComment: (text) => createHostNode("#comment", text),
setText(node, text) { node.text = text; },
setElementText(node, text) { node.children = []; node.text = text; },
parentNode: (node) => node.parent ?? null,
nextSibling(node) {
if (!node.parent) return null;
const index = node.parent.children.indexOf(node);
return node.parent.children[index + 1] ?? null;
},
});
const document: SkinPluginDocument = {
schemaVersion: 3,
components: [{
id: "base", name: "Base", page: "voice", accessibleName: "Base", permissions: [], actions: {}, root: { tag: "p", children: [{ text: "Base" }] },
}],
};
const output = JSON.stringify({
schemaVersion: 5,
components: [{
id: "server-rail", name: "Server rail", page: "voice", accessibleName: "Saved voice servers",
state: { expanded: false },
root: { tag: "div", children: [
{ tag: "button", attributes: { type: "button" }, events: { click: "select-server" }, children: [{ text: "Home" }] },
{ tag: "input", attributes: { type: "text" }, events: { input: "search" } },
] },
}],
});
class MockInputElement {
type = "text";
value = "private search text";
checked = false;
}
const previousInputConstructor = Object.getOwnPropertyDescriptor(globalThis, "HTMLInputElement");
Object.defineProperty(globalThis, "HTMLInputElement", { configurable: true, value: MockInputElement });
const find = (node: HostNode, type: string): HostNode | undefined => node.type === type ? node : node.children.map((child) => find(child, type)).find(Boolean);
const runInput = async (permissions: string[]) => {
const emitted: unknown[] = [];
const host = createHostNode("root");
const app = renderer.createApp(SkinPluginOutlet, {
skinId: "community.test", skinVersion: "1.0.0", document, extensionOutput: output,
extensionPermissions: permissions, page: "voice", data: {}, assets: {}, actions: {},
"onExtension-event": (payload: unknown) => emitted.push(payload),
});
app.mount(host);
await nextTick();
const input = find(host, "input");
assert.ok(input);
(input.props.onInput as (event: Event) => void)({ isTrusted: true, target: new MockInputElement() } as unknown as Event);
app.unmount();
return emitted[0] as { input: { event: Record<string, unknown> } };
};
try {
const deniedHost = createHostNode("root");
const deniedEvents: unknown[] = [];
const deniedApp = renderer.createApp(SkinPluginOutlet, {
skinId: "community.test", skinVersion: "1.0.0", document, extensionOutput: output,
page: "voice", data: {}, assets: {}, actions: {},
"onExtension-event": (payload: unknown) => deniedEvents.push(payload),
});
deniedApp.mount(deniedHost);
await nextTick();
const button = find(deniedHost, "button");
const input = find(deniedHost, "input");
assert.ok(button && input);
const onClick = button.props.onClick as (event: Event) => void;
onClick({ isTrusted: false, target: null } as unknown as Event);
assert.equal(deniedEvents.length, 0, "script-created events must not reach extension logic");
onClick({ isTrusted: true, target: null } as unknown as Event);
const deniedValueEvent = input.props.onInput as (event: Event) => void;
deniedValueEvent({ isTrusted: true, target: new MockInputElement() } as unknown as Event);
const clickPayload = deniedEvents[0] as { skinId: string; componentId: string; input: { state: Record<string, unknown>; event: Record<string, unknown> } };
assert.equal(clickPayload.skinId, "community.test");
assert.equal(clickPayload.componentId, "server-rail");
assert.deepEqual({ ...clickPayload.input.state }, { expanded: false });
assert.deepEqual(clickPayload.input.event, { componentId: "server-rail", handlerId: "select-server", eventName: "click" });
const deniedValuePayload = deniedEvents[1] as { input: { event: Record<string, unknown> } };
assert.deepEqual(deniedValuePayload.input.event, { componentId: "server-rail", handlerId: "search", eventName: "input" });
deniedApp.unmount();
const approvedValuePayload = await runInput(["ui.input.read"]);
assert.deepEqual(approvedValuePayload.input.event, {
componentId: "server-rail", handlerId: "search", eventName: "input", value: "private search text",
});
} finally {
if (previousInputConstructor) Object.defineProperty(globalThis, "HTMLInputElement", previousInputConstructor);
else Reflect.deleteProperty(globalThis, "HTMLInputElement");
}
});
test("generated UI rejects executable elements, event handlers, and network/navigation sinks", () => {
expectOutputError(generatedOutput("runtime-ui", { tag: "script" }), "SKIN_EXTENSION_UI_ELEMENT_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "iframe" }), "SKIN_EXTENSION_UI_ELEMENT_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "button", attributes: { onclick: "run" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "img", attributes: { src: "https://example.invalid/a.png" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "a", attributes: { href: "https://example.invalid/" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "div", attributes: { style: "display:none" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "div", attributes: { "bad name": "value" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "div", attributes: { Onclick: "run" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "div", attributes: { "data-ws-part": "spoofed" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
});
test("generated UI cannot create browser top-layer popovers that cover host controls", () => {
expectOutputError(generatedOutput("runtime-ui", { tag: "div", attributes: { popover: "manual" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "button", attributes: { popovertarget: "host-editor" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "button", attributes: { popovertargetaction: "show" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
});
test("generated UI enforces component and per-node child limits", () => {
const tooManyComponents = JSON.stringify({
schemaVersion: 4,
components: Array.from({ length: 65 }, (_, index) => ({
id: `runtime-${index}`, name: "Runtime UI", page: "voice", accessibleName: "Runtime UI", root: { tag: "main" },
})),
});
const tooManyChildren = generatedOutput("runtime-ui", {
tag: "main",
children: Array.from({ length: 257 }, () => ({ text: "x" })),
});
expectOutputError(tooManyComponents, "SKIN_EXTENSION_UI_OUTPUT_SCHEMA");
expectOutputError(tooManyChildren, "SKIN_EXTENSION_UI_COMPLEXITY_LIMIT");
});
test("SkinPluginOutlet renders validated dynamic output and preserves package UI on collisions", async () => {
const document: SkinPluginDocument = {
schemaVersion: 3,
components: [{
id: "base-ui",
name: "Base UI",
page: "voice",
accessibleName: "Base UI",
permissions: [],
actions: {},
root: { tag: "p", children: [{ text: "Package UI" }] },
}],
};
const props = {
skinId: "community.test",
skinVersion: "1.0.0",
document,
page: "voice" as const,
data: {},
assets: {},
actions: {},
};
const html = await renderToString(createSSRApp(SkinPluginOutlet, {
...props,
extensionOutput: outputDocument("runtime-ui"),
}));
assert.match(html, /data-ws-plugin-component="base-ui"/);
assert.match(html, /data-ws-plugin-component="runtime-ui"/);
assert.match(html, /Generated by Wasm/);
const collisionHtml = await renderToString(createSSRApp(SkinPluginOutlet, {
...props,
extensionOutput: outputDocument("base-ui"),
}));
assert.match(collisionHtml, /data-ws-plugin-component="base-ui"/);
assert.doesNotMatch(collisionHtml, /Generated by Wasm/);
assert.match(collisionHtml, /Package UI/);
});
test("runtime plugin component layout parts keep plugin and component identities in the layout registry format", async () => {
const html = await renderToString(createSSRApp(SkinPluginOutlet, {
skinId: "community.test",
skinVersion: "1.0.0",
document: {
schemaVersion: 3,
components: [{
id: "base-ui",
name: "Base UI",
page: "voice",
accessibleName: "Base UI",
permissions: [],
actions: {},
root: { tag: "main", children: [{ text: "Base UI" }] },
}],
},
extensionOutput: generatedOutput("voice-ui", { tag: "button", part: "join", children: [{ text: "Join" }] }),
componentNamespace: "voice-surface",
page: "voice",
data: {},
assets: {},
actions: {},
}));
assert.match(html, /data-ws-part="skin\.voice\.runtime_plugin_voice-surface__voice-ui"/);
assert.match(html, /data-ws-part="skin\.voice\.runtime_plugin_voice-surface__voice-ui\.control-part-join"/);
});
test("SkinPluginOutlet renders generated custom elements as inert host nodes", async () => {
const document: SkinPluginDocument = {
schemaVersion: 3,
components: [{
id: "base-ui",
name: "Base UI",
page: "voice",
accessibleName: "Base UI",
permissions: [],
actions: {},
root: { tag: "p", children: [{ text: "Base" }] },
}],
};
const html = await renderToString(createSSRApp(SkinPluginOutlet, {
skinId: "community.test",
skinVersion: "1.0.0",
document,
extensionOutput: generatedOutput("kook-rail", { tag: "kook-server-rail", children: [{ text: "Server" }] }),
page: "voice",
data: {},
assets: {},
actions: {},
}));
assert.match(html, /<div[^>]*data-ws-generated-element="kook-server-rail"/);
assert.match(html, /Server/);
});
test("SkinPluginOutlet renders bounded members nested under their channel", async () => {
const document = {
schemaVersion: 3,
components: [{
id: "channel-members",
name: "Channel members",
page: "voice",
accessibleName: "Voice channels and members",
permissions: ["session.channels.read", "session.members.read"],
actions: {},
root: {
tag: "nav",
repeat: { path: "session.channels", as: "channel" },
children: [{
tag: "section",
children: [
{ tag: "h2", children: [{ text: "{{channel.name}}" }] },
{
tag: "ul",
children: [{
tag: "li",
repeat: { path: "channel.members", as: "member" },
children: [{ text: "{{member.name}}" }],
}],
},
],
}],
},
}],
} as unknown as SkinPluginDocument;
const storage = new Map<string, string>();
const previousStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage");
Object.defineProperty(globalThis, "localStorage", {
configurable: true,
value: {
getItem: (key: string) => storage.get(key) ?? null,
setItem: (key: string, value: string) => { storage.set(key, value); },
},
});
try {
approveSkinPluginComponents("community.test", "1.0.0", document.components);
const html = await renderToString(createSSRApp(SkinPluginOutlet, {
skinId: "community.test",
skinVersion: "1.0.0",
document,
page: "voice",
data: { session: { channels: Array.from({ length: 100 }, (_, channelIndex) => ({
id: `room-${channelIndex}`,
name: channelIndex === 0 ? "Room One" : `Room ${channelIndex}`,
members: Array.from({ length: 100 }, (_, memberIndex) => ({ id: `user-${channelIndex}-${memberIndex}`, name: `Member-${channelIndex}-${memberIndex}` })),
})) } },
assets: {},
actions: {},
}));
assert.match(html, /Room One/);
const renderedMembers = html.match(/Member-\d+-\d+/g) ?? [];
assert.ok(renderedMembers.length > 0);
assert.ok(renderedMembers.length < 10_000, "nested repeats must stop at the component render-node limit");
} finally {
if (previousStorage) Object.defineProperty(globalThis, "localStorage", previousStorage);
else Reflect.deleteProperty(globalThis, "localStorage");
}
});
test("skin-authored DOM nodes expose independent layout parts and retain touch bounds for controls", async () => {
const document: SkinPluginDocument = {
schemaVersion: 3,
components: [{
id: "voice-toolbar",
name: "Voice toolbar",
page: "voice",
accessibleName: "Voice toolbar",
permissions: [],
actions: {},
root: {
tag: "section",
children: [
{ tag: "button", part: "disconnect", attributes: { type: "button" }, children: [{ text: "Disconnect" }] },
{ text: "Voice tools", part: "caption" },
],
},
}],
};
const html = await renderToString(createSSRApp(SkinPluginOutlet, {
skinId: "community.test",
skinVersion: "1.0.0",
document,
page: "voice",
data: {},
assets: {},
actions: {},
}));
assert.match(html, /data-ws-part="skin\.voice\.voice-toolbar"/);
assert.match(html, /data-ws-part="skin\.voice\.voice-toolbar\.node-root"/);
assert.match(html, /data-ws-part="skin\.voice\.voice-toolbar\.control-part-disconnect"/);
assert.match(html, /data-ws-part="skin\.voice\.voice-toolbar\.part-caption"/);
});
test("skin nodes cannot impersonate WebSpeak page and layout hooks", async () => {
const document = parseSkinPluginDocument({
schemaVersion: 3,
components: [{
id: "hook-test",
name: "Hook test",
page: "voice",
accessibleName: "Hook test",
permissions: [],
actions: {},
root: {
tag: "section",
children: [{ tag: "div", attributes: { "data-ws-page": "admin", "data-ws-layout-hidden": true, "data-testid": "safe" } }],
},
}],
});
const html = await renderToString(createSSRApp(SkinPluginOutlet, {
skinId: "community.test",
skinVersion: "1.0.0",
document,
page: "voice",
data: {},
assets: {},
actions: {},
}));
assert.match(html, /data-testid="safe"/);
assert.doesNotMatch(html, /data-ws-page="admin"/);
assert.doesNotMatch(html, /data-ws-layout-hidden=/);
});