feat(skin): add approved full-page surface composition

This commit is contained in:
illusia committed 2026-10-10 15:53:48 +08:00
1 parent eabc78f36a
commit d505815f28
19 files changed
+1261 -99

No files matched your search

+49
View File
@@ -331,6 +331,37 @@ test("schema version 3 supports arbitrary public layouts and validated declarati
await assert.rejects(importSkinPack(makeSkinV3(missingAsset, "[data-ws-part=app] { color: red; }")), (error: unknown) => error instanceof SkinPackError && error.code === "SKIN_PLUGIN_ASSET_INVALID");
});
test("schema version 3 accepts a schema v2 full-page surface with host widgets", async () => {
const components = {
schemaVersion: 2,
components: [{
id: "custom-voice-surface",
name: "Custom voice surface",
page: "voice",
mode: "surface",
accessibleName: "Custom voice workspace",
permissions: ["ui.surface.replace"],
actions: {},
root: {
tag: "main",
className: "custom-workspace",
children: [
{ widget: "voice.channel-panel" },
{ widget: "voice.member-cards" },
{ widget: "voice.chat-panel" },
{ widget: "voice.audio-controls" },
],
},
}],
};
const skin = await importSkinPack(makeSkinV3(components, '[data-ws-part="app"] { --skin-surface: #123456; }'));
assert.equal(skin.pluginData?.schemaVersion, 2);
assert.equal(skin.pluginData?.components[0].mode, "surface");
assert.deepEqual(skin.pluginData?.components[0].root.children?.map((node) => node.widget), [
"voice.channel-panel", "voice.member-cards", "voice.chat-panel", "voice.audio-controls",
]);
});
test("the distributable KAAK v3 package validates end to end", async () => {
const archive = await readFile(new URL("../../../docs/examples/kaak-voice.wskin", import.meta.url));
const file = new File([archive], "kaak-voice.wskin", { type: "application/octet-stream" });
@@ -341,6 +372,24 @@ test("the distributable KAAK v3 package validates end to end", async () => {
assert.ok(skin.css.includes('data-ws-plugin-part="kaak-room-sidebar.channel-row"'));
});
test("the full voice surface example packages home, voice, and host-owned controls", async () => {
const archive = await readFile(new URL("../../../docs/examples/open-voice-surface.wskin", import.meta.url));
const file = new File([archive], "open-voice-surface.wskin", { type: "application/octet-stream" });
const skin = await importSkinPack(file);
assert.equal(skin.id, "community.open-voice-surface");
assert.equal(skin.pluginData?.schemaVersion, 2);
assert.deepEqual(skin.pluginData?.components.map((component) => [component.page, component.mode]), [
["home", "surface"], ["voice", "surface"],
]);
const voiceSurface = skin.pluginData?.components.find((component) => component.page === "voice");
assert.ok(voiceSurface?.permissions.includes("ui.surface.replace"));
assert.ok(voiceSurface?.permissions.includes("servers.quickList.read"));
assert.ok(voiceSurface?.permissions.includes("servers.quickList.switch"));
assert.ok(voiceSurface?.permissions.includes("session.channels.read"));
assert.ok(voiceSurface?.permissions.includes("session.members.read"));
assert.ok(skin.css.includes("@media (max-width: 800px)"));
});
function makeSkinV2(layout: unknown, manifestChanges: Record<string, unknown> = {}): File {
const packageFiles: Record<string, Uint8Array> = {
"manifest.json": strToU8(JSON.stringify({
+58
View File
@@ -0,0 +1,58 @@
import type { QuickServer } from "./quick-servers.js";
import { splitTeamSpeakTarget } from "./teamspeak-target.js";
export interface SkinPluginQuickServer {
id: string;
label: string;
current: boolean;
kind: "favorite" | "recent";
favorite: boolean;
}
function randomOpaqueId(): string {
return globalThis.crypto?.randomUUID?.() ?? `quick-server-${Math.random().toString(36).slice(2)}`;
}
function containsConnectionTarget(label: string, address: string): boolean {
const candidates = new Set([address.trim()]);
try { candidates.add(splitTeamSpeakTarget(address).address.trim()); } catch { /* Persisted targets are validated on write. */ }
const normalizedLabel = label.toLocaleLowerCase();
return [...candidates].some((candidate) => candidate.length >= 3 && normalizedLabel.includes(candidate.toLocaleLowerCase()));
}
/** Projects local favorite/recent entries without exposing their connection targets. */
export function createSkinPluginQuickServerProjection(createId: () => string = randomOpaqueId) {
const opaqueIdByServerId = new Map<string, string>();
const targetByOpaqueId = new Map<string, QuickServer>();
return {
project(servers: readonly QuickServer[], isCurrent: (server: QuickServer) => boolean): SkinPluginQuickServer[] {
const activeServerIds = new Set(servers.map((server) => server.id));
for (const id of opaqueIdByServerId.keys()) if (!activeServerIds.has(id)) opaqueIdByServerId.delete(id);
targetByOpaqueId.clear();
return servers.map((server, index) => {
let opaqueId = opaqueIdByServerId.get(server.id);
if (!opaqueId) {
opaqueId = createId();
opaqueIdByServerId.set(server.id, opaqueId);
}
targetByOpaqueId.set(opaqueId, server);
const label = server.label.trim();
const safeLabel = !label || containsConnectionTarget(label, server.address)
? `${server.isFavorite ? "Favorite" : "Recent"} ${index + 1}`
: label;
return {
id: opaqueId,
label: safeLabel,
current: isCurrent(server),
kind: server.isFavorite ? "favorite" : "recent",
favorite: server.isFavorite,
};
});
},
resolve(opaqueId: string): QuickServer | undefined {
return targetByOpaqueId.get(opaqueId);
},
};
}
@@ -0,0 +1,74 @@
import assert from "node:assert/strict";
import test from "node:test";
import { parseSkinPluginDocument, type SkinPluginDocument } from "../../../src/shared/skin-plugin.js";
import {
approveSkinPluginComponents,
getMissingSkinPluginApprovals,
isSkinPluginComponentApproved,
revokeSkinPluginApprovals,
} from "./skin-plugin-approval.js";
import { createSkinPluginQuickServerProjection } from "./skin-plugin-context.js";
function surfaceDocument(): SkinPluginDocument {
return {
schemaVersion: 2,
components: [{
id: "custom-surface",
name: "Custom surface",
page: "voice",
mode: "surface",
accessibleName: "Custom voice page",
permissions: ["ui.surface.replace"],
actions: {},
root: { tag: "main", children: [{ tag: "h1", children: [{ text: "Custom client" }] }] },
}],
};
}
test("page replacement stays disabled until local approval and returns to the host after revocation", () => {
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 {
const document = parseSkinPluginDocument(surfaceDocument());
const component = document.components[0];
assert.deepEqual(getMissingSkinPluginApprovals("test.skin", "1.0.0", document), [component]);
assert.equal(isSkinPluginComponentApproved("test.skin", "1.0.0", component), false);
approveSkinPluginComponents("test.skin", "1.0.0", [component]);
assert.deepEqual(getMissingSkinPluginApprovals("test.skin", "1.0.0", document), []);
assert.equal(isSkinPluginComponentApproved("test.skin", "1.0.0", component), true);
revokeSkinPluginApprovals("test.skin");
assert.equal(isSkinPluginComponentApproved("test.skin", "1.0.0", component), false);
} finally {
if (previousStorage) Object.defineProperty(globalThis, "localStorage", previousStorage);
else Reflect.deleteProperty(globalThis, "localStorage");
}
});
test("skin quick-server data redacts connection targets and resolves only host-generated opaque IDs", () => {
const servers = [
{ id: "192.0.2.10:9987", address: "192.0.2.10:9987", label: "192.0.2.10:9987", isFavorite: true },
{ id: "voice.example:9987", address: "voice.example:9987", label: "Friends (voice.example:9987)", isFavorite: false },
{ id: "voice2.example:9987", address: "voice2.example:9987", label: "Friends", isFavorite: true },
];
let sequence = 0;
const projection = createSkinPluginQuickServerProjection(() => `opaque-${++sequence}`);
const rows = projection.project(servers, (server) => server.id === "voice2.example:9987");
assert.deepEqual(rows.map(({ label, kind, favorite, current }) => [label, kind, favorite, current]), [
["Favorite 1", "favorite", true, false],
["Recent 2", "recent", false, false],
["Friends", "favorite", true, true],
]);
assert.equal(JSON.stringify(rows).includes("192.0.2.10"), false);
assert.equal(JSON.stringify(rows).includes("voice.example"), false);
assert.equal(projection.resolve("opaque-1")?.address, "192.0.2.10:9987");
assert.equal(projection.resolve("192.0.2.10:9987"), undefined);
});