import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import test from "node:test"; import vm from "node:vm"; import ts from "typescript"; import { ref } from "vue"; for (const authorized of [true, false]) { test(`card loads the shared SDK through host authentication: ${authorized}`, async () => { let mount; const requests = []; const elements = []; const sdk = readFileSync( new URL("../../astrbot/dashboard/plugin_page_bridge.js", import.meta.url), "utf8", ); const context = vm.createContext({ exports: {}, axios: { create: () => ({}) }, localStorage: { getItem: (key) => (key === "token" ? "test-dashboard-token" : null), }, window: { location: new URL("https://dashboard.test/"), addEventListener() {}, async fetch(url, init) { requests.push({ url, headers: init?.headers }); if ( !authorized || init?.headers?.get("Authorization") !== "Bearer test-dashboard-token" ) { return new Response("Unauthorized", { status: 401 }); } return url.endsWith("bridge-sdk.js") ? new Response(sdk) : Response.json({ data: { html: "
Reading list", base_url: "/scoped-view-assets/", data: { books: [{ title: "Example" }] }, revision: 1, }, }); }, }, DOMParser: class { parseFromString() { return { querySelectorAll: () => [], createElement: (tag) => ({ tag }), head: { prepend: (...nodes) => elements.push(...nodes) }, documentElement: { get outerHTML() { return JSON.stringify(elements); }, }, }; } }, URL, Request, Headers, crypto: { randomUUID: () => "test-session" }, ref, defineProps: () => ({ part: { id: "test-view", name: "reading" } }), inject() {}, watch() {}, onMounted: (callback) => (mount = callback), onBeforeUnmount() {}, useI18n: () => ({ locale: ref("zh-CN") }), useModuleI18n: () => ({ tm: (key) => key }), useTheme: () => ({ global: { current: ref({ dark: false, colors: {} }) }, }), viewActionKey: Symbol(), }); for (const path of [ "../src/api/http.ts", "../src/utils/pluginI18n.js", "../src/components/chat/message_list_comps/PluginViewCard.vue", ]) { let source = readFileSync(new URL(path, import.meta.url), "utf8"); if (path.endsWith(".vue")) { source = source.match( /