mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 01:33:29 +00:00
refactor: register gateway service adapters
This commit is contained in:
40
src/daemon/service.test.ts
Normal file
40
src/daemon/service.test.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { afterEach, describe, expect, it } from "vitest";
|
||||||
|
import { resolveGatewayService } from "./service.js";
|
||||||
|
|
||||||
|
const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
|
||||||
|
|
||||||
|
function setPlatform(value: NodeJS.Platform | "aix") {
|
||||||
|
if (!originalPlatformDescriptor) {
|
||||||
|
throw new Error("missing process.platform descriptor");
|
||||||
|
}
|
||||||
|
Object.defineProperty(process, "platform", {
|
||||||
|
configurable: true,
|
||||||
|
enumerable: originalPlatformDescriptor.enumerable ?? false,
|
||||||
|
value,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
if (!originalPlatformDescriptor) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Object.defineProperty(process, "platform", originalPlatformDescriptor);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("resolveGatewayService", () => {
|
||||||
|
it.each([
|
||||||
|
{ platform: "darwin" as const, label: "LaunchAgent", loadedText: "loaded" },
|
||||||
|
{ platform: "linux" as const, label: "systemd", loadedText: "enabled" },
|
||||||
|
{ platform: "win32" as const, label: "Scheduled Task", loadedText: "registered" },
|
||||||
|
])("returns the registered adapter for $platform", ({ platform, label, loadedText }) => {
|
||||||
|
setPlatform(platform);
|
||||||
|
const service = resolveGatewayService();
|
||||||
|
expect(service.label).toBe(label);
|
||||||
|
expect(service.loadedText).toBe(loadedText);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws for unsupported platforms", () => {
|
||||||
|
setPlatform("aix");
|
||||||
|
expect(() => resolveGatewayService()).toThrow("Gateway service install not supported on aix");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -64,9 +64,8 @@ export type GatewayService = {
|
|||||||
readRuntime: (env: GatewayServiceEnv) => Promise<GatewayServiceRuntime>;
|
readRuntime: (env: GatewayServiceEnv) => Promise<GatewayServiceRuntime>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function resolveGatewayService(): GatewayService {
|
const GATEWAY_SERVICE_REGISTRY = {
|
||||||
if (process.platform === "darwin") {
|
darwin: {
|
||||||
return {
|
|
||||||
label: "LaunchAgent",
|
label: "LaunchAgent",
|
||||||
loadedText: "loaded",
|
loadedText: "loaded",
|
||||||
notLoadedText: "not loaded",
|
notLoadedText: "not loaded",
|
||||||
@@ -77,11 +76,8 @@ export function resolveGatewayService(): GatewayService {
|
|||||||
isLoaded: isLaunchAgentLoaded,
|
isLoaded: isLaunchAgentLoaded,
|
||||||
readCommand: readLaunchAgentProgramArguments,
|
readCommand: readLaunchAgentProgramArguments,
|
||||||
readRuntime: readLaunchAgentRuntime,
|
readRuntime: readLaunchAgentRuntime,
|
||||||
};
|
},
|
||||||
}
|
linux: {
|
||||||
|
|
||||||
if (process.platform === "linux") {
|
|
||||||
return {
|
|
||||||
label: "systemd",
|
label: "systemd",
|
||||||
loadedText: "enabled",
|
loadedText: "enabled",
|
||||||
notLoadedText: "disabled",
|
notLoadedText: "disabled",
|
||||||
@@ -92,11 +88,8 @@ export function resolveGatewayService(): GatewayService {
|
|||||||
isLoaded: isSystemdServiceEnabled,
|
isLoaded: isSystemdServiceEnabled,
|
||||||
readCommand: readSystemdServiceExecStart,
|
readCommand: readSystemdServiceExecStart,
|
||||||
readRuntime: readSystemdServiceRuntime,
|
readRuntime: readSystemdServiceRuntime,
|
||||||
};
|
},
|
||||||
}
|
win32: {
|
||||||
|
|
||||||
if (process.platform === "win32") {
|
|
||||||
return {
|
|
||||||
label: "Scheduled Task",
|
label: "Scheduled Task",
|
||||||
loadedText: "registered",
|
loadedText: "registered",
|
||||||
notLoadedText: "missing",
|
notLoadedText: "missing",
|
||||||
@@ -107,8 +100,13 @@ export function resolveGatewayService(): GatewayService {
|
|||||||
isLoaded: isScheduledTaskInstalled,
|
isLoaded: isScheduledTaskInstalled,
|
||||||
readCommand: readScheduledTaskCommand,
|
readCommand: readScheduledTaskCommand,
|
||||||
readRuntime: readScheduledTaskRuntime,
|
readRuntime: readScheduledTaskRuntime,
|
||||||
};
|
},
|
||||||
}
|
} satisfies Partial<Record<NodeJS.Platform, GatewayService>>;
|
||||||
|
|
||||||
|
export function resolveGatewayService(): GatewayService {
|
||||||
|
const service = GATEWAY_SERVICE_REGISTRY[process.platform];
|
||||||
|
if (service) {
|
||||||
|
return service;
|
||||||
|
}
|
||||||
throw new Error(`Gateway service install not supported on ${process.platform}`);
|
throw new Error(`Gateway service install not supported on ${process.platform}`);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user