mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-20 15:19:16 +00:00
feat: rebuild plugin UI with host rendering and standard settings (#4053)
* feat: rebuild plugin pages with host rendering and native settings * fix(plugins): contain host-view errors, isolate editing, and migrate fixtures * fix(plugins): migrate remaining permission fixture hosts
This commit is contained in:
227 files changed
+8298
-4172
No files matched your search
@@ -1,42 +1,56 @@
|
||||
"use strict";
|
||||
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const vm = require("node:vm");
|
||||
const { pathToFileURL } = require("node:url");
|
||||
const test = require("node:test");
|
||||
const { JSDOM } = require("jsdom");
|
||||
|
||||
test("Starter panel renders the actual command result as text and reports denial", async () => {
|
||||
const root = path.resolve(__dirname, "../../examples/plugins/starter-netcatty");
|
||||
const plugin = (await import(pathToFileURL(path.join(root, "dist/index.js")).href)).default;
|
||||
let command;
|
||||
let receive;
|
||||
let click;
|
||||
const elements = new Map(["greet", "result", "title", "description"].map((id) => [id, {
|
||||
textContent: "", disabled: false, addEventListener(_event, callback) { click = callback; },
|
||||
}]));
|
||||
const greeting = '<img src=x onerror="alert(1)"> synthetic hello';
|
||||
await plugin.activate({
|
||||
subscriptions: { add() {} },
|
||||
commands: { registerCommand(id, handler) { assert.equal(id, "com.netcatty.starter.greet"); command = handler; } },
|
||||
settings: { get: async () => greeting },
|
||||
views: { postMessage: (_id, message) => receive?.(message) },
|
||||
});
|
||||
let deny = false;
|
||||
vm.runInNewContext(fs.readFileSync(path.join(root, "panel.js"), "utf8"), {
|
||||
document: { getElementById: (id) => elements.get(id), documentElement: { style: {} } },
|
||||
window: { netcattyView: {
|
||||
onDidChangeEnvironment: (listener) => listener({ locale: "en", theme: "light" }),
|
||||
onDidReceiveMessage: (listener) => { receive = listener; },
|
||||
executeCommand: async (id) => { assert.equal(id, "com.netcatty.starter.greet"); if (deny) throw new Error("Denied"); return command(); },
|
||||
} },
|
||||
});
|
||||
await click();
|
||||
assert.equal(elements.get("result").textContent, greeting);
|
||||
assert.equal(elements.get("greet").disabled, false);
|
||||
deny = true;
|
||||
await click();
|
||||
assert.match(elements.get("result").textContent, /Check permission approval/);
|
||||
assert.equal(elements.get("greet").disabled, false);
|
||||
const dom = new JSDOM("<!doctype html><body></body>");
|
||||
const saved = new Map();
|
||||
for (const key of ["document", "MutationObserver", "HTMLElement", "Element", "Node"]) {
|
||||
saved.set(key, Object.getOwnPropertyDescriptor(globalThis, key));
|
||||
Object.defineProperty(globalThis, key, { configurable: true, writable: true, value: dom.window[key] });
|
||||
}
|
||||
const subscriptions = [];
|
||||
try {
|
||||
const root = path.resolve(__dirname, "../../examples/plugins/starter-netcatty");
|
||||
const plugin = (await import(pathToFileURL(path.join(root, "dist/host.js")).href)).default;
|
||||
let command; let deny = false;
|
||||
const listeners = new Set();
|
||||
const greeting = '<img src=x onerror="alert(1)"> synthetic hello';
|
||||
await plugin.activate({
|
||||
subscriptions: { add(disposable) { subscriptions.push(disposable); } },
|
||||
commands: {
|
||||
registerCommand(id, handler) { assert.equal(id, "com.netcatty.starter.greet"); command = handler; return { dispose() {} }; },
|
||||
async executeCommand(id) { assert.equal(id, "com.netcatty.starter.greet"); if (deny) throw new Error("Denied"); return command(); },
|
||||
},
|
||||
settings: { get: async () => greeting },
|
||||
environment: { locale: "en", theme: "light", onDidChange: () => ({ dispose() {} }) },
|
||||
views: {
|
||||
postMessage: (_id, message) => { for (const receive of listeners) receive(message); },
|
||||
onDidReceiveMessage(_id, receive) { listeners.add(receive); return { dispose() { listeners.delete(receive); } }; },
|
||||
},
|
||||
});
|
||||
for (const receive of listeners) receive({ kind: "host-ui.open", instanceId: "test", stateScope: "test" });
|
||||
const button = dom.window.document.getElementById("greet");
|
||||
const result = dom.window.document.getElementById("result");
|
||||
assert.ok(button, "the bundled host entry mounts the starter view");
|
||||
const click = async () => {
|
||||
button.click();
|
||||
for (let i = 0; button.disabled && i < 100; i++) await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
assert.equal(button.disabled, false);
|
||||
};
|
||||
await click();
|
||||
assert.equal(result.textContent, greeting);
|
||||
assert.equal(result.querySelector("img"), null);
|
||||
deny = true;
|
||||
await click();
|
||||
assert.match(result.textContent, /Check permission approval/);
|
||||
} finally {
|
||||
for (const subscription of subscriptions.reverse()) subscription.dispose();
|
||||
dom.window.close();
|
||||
for (const [key, value] of saved) { if (value) Object.defineProperty(globalThis, key, value); else Reflect.deleteProperty(globalThis, key); }
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user