import test from "node:test"; import assert from "node:assert/strict"; import { readFile, access } from "node:fs/promises"; import path from "node:path"; import { API_VERSION, describeFeature, hasFeature, missingFeatures, MissingFeatureError, pluginFeatures, requireFeature, requireFeatures, type PluginApi } from "../../../packages/plugin-sdk/src/index.ts"; import * as sdkIcons from "../../../packages/plugin-sdk/src/icons.ts"; import * as sdkTooltip from "../../../packages/plugin-sdk/src/tooltip.ts"; import * as hostIcons from "../../../src/lib/ui/actionIcons.ts"; import * as hostTooltip from "../../../src/lib/ui/tooltip.ts"; import { ExternalSession, type SessionServices } from "../../../src/lib/plugins/externalSession.ts"; import { ContributionRegistry } from "../../../src/lib/plugins/registry.ts"; import { ServiceHub } from "../../../src/lib/plugins/services.ts"; import { declarationImports } from "../../../scripts/buildSdk.mjs"; import type { ActionMenuItem as HostMenuItem } from "../../../src/lib/ui/actionMenu.ts"; import type { MenuOptions as HostMenuOptions } from "../../../src/lib/ui/managedActionMenu.ts"; import type { ActionMenuItem, ActionMenuOptions } from "../../../packages/plugin-sdk/src/index.ts"; // The host menu files keep their own menu types. `pnpm check` fails when those types and the SDK types differ. type Same = [A] extends [B] ? ([B] extends [A] ? true : false) : false; const sameMenuTypes: [Same, Same] = [true, true]; const base: SessionServices = { workspace: { list: async () => [], read: async () => "", write: async () => {} }, settings: { read: async () => null, write: async () => {} }, assets: { read: async () => new Uint8Array() }, }; async function hostApi(services: Partial, views = false) { let api!: PluginApi; const session = views ? new ExternalSession("org.test.sdk", { ...base, ...services }, () => {}, () => {}, new ContributionRegistry(), async () => {}, async () => "", () => {}, () => {}, () => {}) : new ExternalSession("org.test.sdk", { ...base, ...services }, () => {}, () => {}, new ContributionRegistry()); await session.start({ activate(value) { api = value; } }); return { api, session }; } test("feature checks find optional members, and missing members give an update message", () => { const read = async () => new Uint8Array([1]); const api = { workspace: { readBytes: read }, services: { get: () => null } } as unknown as PluginApi; assert.equal(hasFeature(api, "workspace.readBytes"), true); assert.equal(hasFeature(api, "services"), true); assert.equal(hasFeature(api, "workspace.subscribe"), false); assert.equal(hasFeature(api, "files"), false); assert.deepEqual(missingFeatures(api, ["files", "workspace.readBytes", "settings.subscribe"]), ["files", "settings.subscribe"]); assert.equal(requireFeature(api, "services"), (api as unknown as { services: unknown }).services); assert.throws(() => requireFeature(api, "files"), (error: unknown) => error instanceof MissingFeatureError && error.name === "MissingFeatureError" && error.features.length === 1 && error.features[0] === "files" && error.message === "This plugin needs the folder grant API (api.files). Update RuneStone to a version that has this feature."); assert.throws(() => requireFeatures(api, ["files", "backgrounds", "workspace.readBytes"]), { message: "This plugin needs the folder grant API (api.files) and the background API (api.backgrounds). Update RuneStone to a version that has these features.", }); // The terminal API (api.pty) left the SDK when Terminal became a native feature. assert.throws(() => hasFeature(api, "pty" as never), TypeError); assert.doesNotThrow(() => requireFeatures(api, ["services", "workspace.readBytes"])); assert.throws(() => hasFeature(api, "workspace.read" as never), TypeError); assert.equal(describeFeature("ui.showActionMenu"), "the shared action menu (api.ui.showActionMenu)"); assert.equal(API_VERSION, 1); }); test("a required function stays bound to its parent object", async () => { const workspace = { prefix: "ws:", async readOptional(this: { prefix: string }, file: string) { return this.prefix + file; } }; const api = { workspace } as unknown as PluginApi; const readOptional = requireFeature(api, "workspace.readOptional"); assert.equal(await readOptional("a.md"), "ws:a.md"); }); test("the current host gives every SDK feature when all services are present", async () => { const files = { chooseFolder: async () => null, pictureFolder: async () => ({ id: "g", path: "p", name: "n" }), grants: async () => [], revoke: async () => false, list: async () => [], readBytes: async () => new Uint8Array(), readSidecar: async () => null, writeSidecar: async () => {} }; const { api, session } = await hostApi({ files, backgrounds: true, serviceHub: new ServiceHub(), openFile: async () => {}, completionTransport: async () => {}, workspace: { ...base.workspace, listMarkdown: async () => [], readMany: async () => [], readBytes: async () => new Uint8Array(), readOptional: async () => null, create: async () => {}, subscribe: () => () => {} }, settings: { ...base.settings, subscribe: () => () => {} }, }, true); assert.deepEqual(missingFeatures(api, pluginFeatures), []); await session.stop(); }); test("the host without optional services lacks only the features that depend on them", async () => { const { api, session } = await hostApi({}); assert.deepEqual(missingFeatures(api, pluginFeatures).sort(), [ "backgrounds", "closeView", "completion.transport", "files", "openFile", "renameDocument", "replaceDocumentWithView", "replaceView", "settings.subscribe", "workspace.create", "workspace.listMarkdown", "workspace.readBytes", "workspace.readMany", "workspace.readOptional", "workspace.subscribe", ]); await session.stop(); }); test("the host uses the SDK icons and tooltip, so plugins get the same artwork and behavior", () => { assert.equal(hostIcons.actionIcons, sdkIcons.actionIcons); assert.equal(hostIcons.actionIcon, sdkIcons.actionIcon); assert.equal(hostTooltip.attachTooltip, sdkTooltip.attachTooltip); for (const [name, markup] of Object.entries(sdkIcons.actionIcons)) assert.match(markup, /^<(path|circle|rect|ellipse)\b/, name); }); test("the host menu types are the SDK menu types", () => { assert.deepEqual(sameMenuTypes, [true, true]); }); test("the SDK build gives declaration imports with .js paths", () => { assert.equal(declarationImports('export type * from "./api.ts";\nimport type { A } from \'../b.ts\';\ntype C = import("./c.ts").C;\nimport x from "node:fs";'), 'export type * from "./api.js";\nimport type { A } from \'../b.js\';\ntype C = import("./c.js").C;\nimport x from "node:fs";'); }); test("each SDK export has a source module, and the package is not for npm", async () => { const folder = path.resolve("packages/plugin-sdk"); const manifest = JSON.parse(await readFile(path.join(folder, "package.json"), "utf8")); assert.equal(manifest.name, "@runestone/plugin-sdk"); assert.equal(manifest.private, true); assert.equal(manifest.license, "Apache-2.0"); assert.equal(manifest.dependencies, undefined); assert.deepEqual(manifest.files, ["dist", "README.md", "LICENSE", "NOTICE"]); for (const file of ["README.md", "LICENSE", "NOTICE"]) await access(path.join(folder, file)); for (const [entry, target] of Object.entries(manifest.exports as Record)) { if (typeof target === "string") continue; assert.equal(target.types.replace(/\.d\.ts$/, ""), target.default.replace(/\.js$/, ""), entry); await access(path.join(folder, target.default.replace(/^\.\/dist\//, "src/").replace(/\.js$/, ".ts"))); } });