mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 09:37:41 +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,51 +64,49 @@ export type GatewayService = {
|
|||||||
readRuntime: (env: GatewayServiceEnv) => Promise<GatewayServiceRuntime>;
|
readRuntime: (env: GatewayServiceEnv) => Promise<GatewayServiceRuntime>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const GATEWAY_SERVICE_REGISTRY = {
|
||||||
|
darwin: {
|
||||||
|
label: "LaunchAgent",
|
||||||
|
loadedText: "loaded",
|
||||||
|
notLoadedText: "not loaded",
|
||||||
|
install: ignoreInstallResult(installLaunchAgent),
|
||||||
|
uninstall: uninstallLaunchAgent,
|
||||||
|
stop: stopLaunchAgent,
|
||||||
|
restart: restartLaunchAgent,
|
||||||
|
isLoaded: isLaunchAgentLoaded,
|
||||||
|
readCommand: readLaunchAgentProgramArguments,
|
||||||
|
readRuntime: readLaunchAgentRuntime,
|
||||||
|
},
|
||||||
|
linux: {
|
||||||
|
label: "systemd",
|
||||||
|
loadedText: "enabled",
|
||||||
|
notLoadedText: "disabled",
|
||||||
|
install: ignoreInstallResult(installSystemdService),
|
||||||
|
uninstall: uninstallSystemdService,
|
||||||
|
stop: stopSystemdService,
|
||||||
|
restart: restartSystemdService,
|
||||||
|
isLoaded: isSystemdServiceEnabled,
|
||||||
|
readCommand: readSystemdServiceExecStart,
|
||||||
|
readRuntime: readSystemdServiceRuntime,
|
||||||
|
},
|
||||||
|
win32: {
|
||||||
|
label: "Scheduled Task",
|
||||||
|
loadedText: "registered",
|
||||||
|
notLoadedText: "missing",
|
||||||
|
install: ignoreInstallResult(installScheduledTask),
|
||||||
|
uninstall: uninstallScheduledTask,
|
||||||
|
stop: stopScheduledTask,
|
||||||
|
restart: restartScheduledTask,
|
||||||
|
isLoaded: isScheduledTaskInstalled,
|
||||||
|
readCommand: readScheduledTaskCommand,
|
||||||
|
readRuntime: readScheduledTaskRuntime,
|
||||||
|
},
|
||||||
|
} satisfies Partial<Record<NodeJS.Platform, GatewayService>>;
|
||||||
|
|
||||||
export function resolveGatewayService(): GatewayService {
|
export function resolveGatewayService(): GatewayService {
|
||||||
if (process.platform === "darwin") {
|
const service = GATEWAY_SERVICE_REGISTRY[process.platform];
|
||||||
return {
|
if (service) {
|
||||||
label: "LaunchAgent",
|
return service;
|
||||||
loadedText: "loaded",
|
|
||||||
notLoadedText: "not loaded",
|
|
||||||
install: ignoreInstallResult(installLaunchAgent),
|
|
||||||
uninstall: uninstallLaunchAgent,
|
|
||||||
stop: stopLaunchAgent,
|
|
||||||
restart: restartLaunchAgent,
|
|
||||||
isLoaded: isLaunchAgentLoaded,
|
|
||||||
readCommand: readLaunchAgentProgramArguments,
|
|
||||||
readRuntime: readLaunchAgentRuntime,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.platform === "linux") {
|
|
||||||
return {
|
|
||||||
label: "systemd",
|
|
||||||
loadedText: "enabled",
|
|
||||||
notLoadedText: "disabled",
|
|
||||||
install: ignoreInstallResult(installSystemdService),
|
|
||||||
uninstall: uninstallSystemdService,
|
|
||||||
stop: stopSystemdService,
|
|
||||||
restart: restartSystemdService,
|
|
||||||
isLoaded: isSystemdServiceEnabled,
|
|
||||||
readCommand: readSystemdServiceExecStart,
|
|
||||||
readRuntime: readSystemdServiceRuntime,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.platform === "win32") {
|
|
||||||
return {
|
|
||||||
label: "Scheduled Task",
|
|
||||||
loadedText: "registered",
|
|
||||||
notLoadedText: "missing",
|
|
||||||
install: ignoreInstallResult(installScheduledTask),
|
|
||||||
uninstall: uninstallScheduledTask,
|
|
||||||
stop: stopScheduledTask,
|
|
||||||
restart: restartScheduledTask,
|
|
||||||
isLoaded: isScheduledTaskInstalled,
|
|
||||||
readCommand: readScheduledTaskCommand,
|
|
||||||
readRuntime: readScheduledTaskRuntime,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
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