feat: plugins only see their own host settings in the browser

This commit is contained in:
LukeGus committed 2026-09-29 13:38:11 -05:00
1 parent 50d43e29fb
commit fb9c168b6b
17 files changed
+488 -52

No files matched your search

@@ -5,11 +5,19 @@ import type {
TermixApp,
} from "@termix/plugin-sdk/frontend";
export function macAddressOf(
host: Pick<PluginHostRecord, "pluginSettings"> | null | undefined,
): string | null {
const macAddress = host?.pluginSettings?.["wake-on-lan"]?.macAddress;
return typeof macAddress === "string" && macAddress.trim()
? macAddress.trim()
: null;
}
export function hasMacAddress(
host: Pick<PluginHostRecord, "pluginSettings">,
): boolean {
const macAddress = host.pluginSettings?.["wake-on-lan"]?.macAddress;
return typeof macAddress === "string" && macAddress.trim().length > 0;
return macAddressOf(host) !== null;
}
function errorMessage(error: unknown, fallback: string): string {
+5 -1
View File
@@ -1,6 +1,6 @@
import { Zap } from "lucide-react";
import type { TermixApp } from "@termix/plugin-sdk/frontend";
import { hasMacAddress, wakeHost } from "./host-action.js";
import { hasMacAddress, macAddressOf, wakeHost } from "./host-action.js";
export function activate(app: TermixApp): void {
app.registerHostAction({
@@ -12,4 +12,8 @@ export function activate(app: TermixApp): void {
when: (host) => hasMacAddress(host),
run: (host) => wakeHost(app.api, app.t, String(host.id)),
});
// A saved host's MAC address, for plugins that send their own wake packet.
app.registerAction("wakeOnLan.macAddress", ((hostId: string | number) =>
macAddressOf(app.getHost(hostId))) as never);
}
@@ -1,7 +1,11 @@
import { describe, it, expect, vi, afterEach } from "vitest";
import { toast } from "sonner";
import type { PluginApiClient } from "@termix/plugin-sdk/frontend";
import { hasMacAddress, wakeHost } from "../../src/frontend/host-action.js";
import {
hasMacAddress,
macAddressOf,
wakeHost,
} from "../../src/frontend/host-action.js";
vi.mock("sonner", () => ({
toast: { success: vi.fn(), error: vi.fn() },
@@ -24,6 +28,26 @@ function stubApi(overrides: Partial<PluginApiClient> = {}): PluginApiClient {
const t = (key: string) => key;
describe("macAddressOf", () => {
it("returns the trimmed MAC address a host keeps", () => {
expect(
macAddressOf({
pluginSettings: {
"wake-on-lan": { macAddress: " aa:bb:cc:dd:ee:ff " },
},
}),
).toBe("aa:bb:cc:dd:ee:ff");
});
it("is null without a host or an address", () => {
expect(macAddressOf(undefined)).toBeNull();
expect(macAddressOf({ pluginSettings: {} })).toBeNull();
expect(
macAddressOf({ pluginSettings: { "wake-on-lan": { macAddress: " " } } }),
).toBeNull();
});
});
describe("hasMacAddress", () => {
it("is true when the host's wake-on-lan settings carry a MAC address", () => {
expect(