mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-10-31 15:45:23 +00:00
feat: convert wake-on-lan into a plugin (B20)
This commit is contained in:
1 parent
d4ec444558
commit
2ac7779eda
50 files changed
+17844
-199
No files matched your search
@@ -0,0 +1,33 @@
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import {
|
||||
renderWithApp,
|
||||
type RenderedPluginApp,
|
||||
} from "@termix/plugin-sdk/testing";
|
||||
import type { PluginManifest } from "@termix/plugin-sdk/manifest";
|
||||
import * as plugin from "../../src/frontend/index";
|
||||
import manifestJson from "../../manifest.json";
|
||||
import locales from "../../locales/en.json";
|
||||
|
||||
const manifest = manifestJson as unknown as PluginManifest;
|
||||
|
||||
let rendered: RenderedPluginApp | null = null;
|
||||
|
||||
afterEach(async () => {
|
||||
await rendered?.deactivate();
|
||||
rendered = null;
|
||||
});
|
||||
|
||||
describe(`${manifest.id} activate`, () => {
|
||||
it("registers the Wake host action", async () => {
|
||||
rendered = await renderWithApp(plugin, { manifest, locales });
|
||||
expect(rendered.registered.hostActions()).toEqual([
|
||||
expect.objectContaining({ id: "wake-on-lan" }),
|
||||
]);
|
||||
});
|
||||
|
||||
it("removes the host action on deactivate", async () => {
|
||||
const app = await renderWithApp(plugin, { manifest, locales });
|
||||
await app.deactivate();
|
||||
expect(app.registered.hostActions()).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,74 @@
|
||||
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";
|
||||
|
||||
vi.mock("sonner", () => ({
|
||||
toast: { success: vi.fn(), error: vi.fn() },
|
||||
}));
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
function stubApi(overrides: Partial<PluginApiClient> = {}): PluginApiClient {
|
||||
return {
|
||||
get: vi.fn(async () => ({ data: {} })),
|
||||
post: vi.fn(async () => ({ data: {} })),
|
||||
put: vi.fn(async () => ({ data: {} })),
|
||||
patch: vi.fn(async () => ({ data: {} })),
|
||||
delete: vi.fn(async () => ({ data: {} })),
|
||||
...overrides,
|
||||
} as PluginApiClient;
|
||||
}
|
||||
|
||||
const t = (key: string) => key;
|
||||
|
||||
describe("hasMacAddress", () => {
|
||||
it("is true when the host's wake-on-lan settings carry a MAC address", () => {
|
||||
expect(
|
||||
hasMacAddress({
|
||||
pluginSettings: { "wake-on-lan": { macAddress: "aa:bb:cc:dd:ee:ff" } },
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("is false with no plugin settings", () => {
|
||||
expect(hasMacAddress({})).toBe(false);
|
||||
});
|
||||
|
||||
it("is false with an empty MAC address", () => {
|
||||
expect(
|
||||
hasMacAddress({ pluginSettings: { "wake-on-lan": { macAddress: "" } } }),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("wakeHost", () => {
|
||||
it("posts to the host's wake route and toasts success", async () => {
|
||||
const api = stubApi();
|
||||
wakeHost(api, t, "1");
|
||||
await vi.waitFor(() =>
|
||||
expect(api.post).toHaveBeenCalledWith("/host/1/wake"),
|
||||
);
|
||||
await vi.waitFor(() => expect(toast.success).toHaveBeenCalled());
|
||||
});
|
||||
|
||||
it("toasts the server's error message on failure", async () => {
|
||||
const api = stubApi({
|
||||
post: vi
|
||||
.fn()
|
||||
.mockRejectedValue({ response: { data: { error: "no mac" } } }),
|
||||
});
|
||||
wakeHost(api, t, "1");
|
||||
await vi.waitFor(() => expect(toast.error).toHaveBeenCalledWith("no mac"));
|
||||
});
|
||||
|
||||
it("falls back to a generic message with no server error", async () => {
|
||||
const api = stubApi({ post: vi.fn().mockRejectedValue(new Error("x")) });
|
||||
wakeHost(api, t, "1");
|
||||
await vi.waitFor(() =>
|
||||
expect(toast.error).toHaveBeenCalledWith("hostAction.failed"),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user