openpetswithchatandmcp/apps/desktop/tests/plugin-sdk-runtime-helpers.test.ts
2026-06-19 01:59:44 +00:00

32 lines
2.1 KiB
TypeScript

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { countActiveBubbles, nextScheduleDelayMs, parseDaily, safeError, withTimeout } from "../src/plugin-sdk-runtime-helpers.js";
const desktopRoot = process.env.FAMILIAROS_DESKTOP_ROOT ?? resolve(dirname(fileURLToPath(import.meta.url)), "..");
const pluginSdkApiBuilderSource = readFileSync(resolve(desktopRoot, "src/plugin-sdk-api-builder.ts"), "utf8");
const pluginSdkRuntimeHelpersSource = readFileSync(resolve(desktopRoot, "src/plugin-sdk-runtime-helpers.ts"), "utf8");
assert.match(pluginSdkApiBuilderSource, /from "\.\/plugin-sdk-runtime-helpers\.js"/, "Plugin SDK API builder must import the extracted runtime helper seam.");
assert.match(pluginSdkRuntimeHelpersSource, /export function nextScheduleDelayMs/, "Plugin SDK runtime helpers must export schedule delay calculation.");
assert.match(pluginSdkRuntimeHelpersSource, /export function withTimeout/, "Plugin SDK runtime helpers must export timeout wrapping.");
assert.deepEqual(parseDaily("09:15"), { time: "09:15" });
assert.deepEqual(parseDaily({ time: "18:45", days: [1, 3, 5] }), { time: "18:45", days: [1, 3, 5] });
assert.throws(() => parseDaily("25:00"), /Daily schedule time must be HH:mm/);
assert.throws(() => parseDaily({ time: "12:30", days: [7] }), /Daily schedule days must be weekdays 0-6/);
assert.equal(nextScheduleDelayMs({ type: "once", delayMs: 0 }), 1);
assert.equal(nextScheduleDelayMs({ type: "every", intervalMs: 25 }), 25);
assert.equal(nextScheduleDelayMs({ type: "at", timestamp: Date.now() - 5_000 }), 1_000);
assert.equal(safeError(new Error("boom")), "boom");
assert.equal(safeError("boom"), "Plugin SDK callback failed.");
assert.equal(countActiveBubbles({ bubbles: new Map([["one", {}], ["two", {}]]) } as never), 2);
await assert.rejects(() => withTimeout(new Promise(() => undefined), 5), /Plugin command timed out\./);
await assert.doesNotReject(() => withTimeout(Promise.resolve("ok"), 50));
console.error("Plugin SDK runtime helper validation passed.");