import assert from "node:assert/strict"; import test from "node:test"; import { createRenderer, createSSRApp, nextTick } from "vue"; import { renderToString } from "@vue/server-renderer"; import type { SkinPluginDocument } from "../../../src/shared/skin-plugin.js"; import { 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, permissions: string[] = [], actions: Record = {}, state?: Record): 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 = { 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("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 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 callbacks reject unsupported events, malformed IDs, and password inputs", () => { const schema5 = (root: Record) => 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 callback data only for a trusted user event", async () => { interface HostNode { type: string; props: Record; text: string; children: HostNode[]; parent?: HostNode; } const createHostNode = (type: string, text = ""): HostNode => ({ type, props: {}, text, children: [] }); const renderer = createRenderer({ 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 emitted: unknown[] = []; const host = createHostNode("root"); const app = renderer.createApp(SkinPluginOutlet, { skinId: "community.test", skinVersion: "1.0.0", document, extensionOutput: JSON.stringify({ schemaVersion: 5, components: [{ id: "server-rail", name: "Server rail", page: "voice", accessibleName: "Saved voice servers", state: { expanded: false }, root: { tag: "button", attributes: { type: "button" }, events: { click: "select-server" }, children: [{ text: "Home" }] }, }], }), page: "voice", data: {}, assets: {}, actions: {}, "onExtension-event": (payload: unknown) => emitted.push(payload), }); app.mount(host); await nextTick(); const findButton = (node: HostNode): HostNode | undefined => node.type === "button" ? node : node.children.map(findButton).find(Boolean); const button = findButton(host); const describe = (node: HostNode): string => `${node.type}[${node.text}](${node.children.map(describe).join(",")})`; assert.ok(button, describe(host)); const onClick = button.props.onClick as (event: Event) => void; onClick({ isTrusted: false, target: null } as unknown as Event); assert.equal(emitted.length, 0, "script-created events must not reach extension logic"); onClick({ isTrusted: true, target: null } as unknown as Event); assert.equal(emitted.length, 1); const payload = emitted[0] as { skinId: string; componentId: string; input: { state: Record; event: Record } }; assert.equal(payload.skinId, "community.test"); assert.equal(payload.componentId, "server-rail"); assert.deepEqual({ ...payload.input.state }, { expanded: false }); assert.deepEqual(payload.input.event, { componentId: "server-rail", handlerId: "select-server", eventName: "click" }); app.unmount(); }); 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: { mystery: "value" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID"); expectOutputError(generatedOutput("runtime-ui", { tag: "div", attributes: { "data-ws-part": "spoofed" } }), "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("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, /]*data-ws-generated-element="kook-server-rail"/); assert.match(html, /Server/); }); 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"/); });