mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-12 01:31:08 +00:00
test: share daemon cli service helpers
This commit is contained in:
@@ -29,6 +29,21 @@ let runServiceRestart: typeof import("./lifecycle-core.js").runServiceRestart;
|
||||
let runServiceStart: typeof import("./lifecycle-core.js").runServiceStart;
|
||||
let runServiceStop: typeof import("./lifecycle-core.js").runServiceStop;
|
||||
|
||||
function readJsonLog<T extends object>() {
|
||||
const jsonLine = runtimeLogs.find((line) => line.trim().startsWith("{"));
|
||||
return JSON.parse(jsonLine ?? "{}") as T;
|
||||
}
|
||||
|
||||
function createServiceRunArgs(checkTokenDrift?: boolean) {
|
||||
return {
|
||||
serviceNoun: "Gateway",
|
||||
service,
|
||||
renderStartHints: () => [],
|
||||
opts: { json: true as const },
|
||||
...(checkTokenDrift ? { checkTokenDrift } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
describe("runServiceRestart token drift", () => {
|
||||
beforeAll(async () => {
|
||||
({ runServiceRestart, runServiceStart, runServiceStop } = await import("./lifecycle-core.js"));
|
||||
@@ -53,17 +68,10 @@ describe("runServiceRestart token drift", () => {
|
||||
});
|
||||
|
||||
it("emits drift warning when enabled", async () => {
|
||||
await runServiceRestart({
|
||||
serviceNoun: "Gateway",
|
||||
service,
|
||||
renderStartHints: () => [],
|
||||
opts: { json: true },
|
||||
checkTokenDrift: true,
|
||||
});
|
||||
await runServiceRestart(createServiceRunArgs(true));
|
||||
|
||||
expect(loadConfig).toHaveBeenCalledTimes(1);
|
||||
const jsonLine = runtimeLogs.find((line) => line.trim().startsWith("{"));
|
||||
const payload = JSON.parse(jsonLine ?? "{}") as { warnings?: string[] };
|
||||
const payload = readJsonLog<{ warnings?: string[] }>();
|
||||
expect(payload.warnings).toEqual(
|
||||
expect.arrayContaining([expect.stringContaining("gateway install --force")]),
|
||||
);
|
||||
@@ -83,16 +91,9 @@ describe("runServiceRestart token drift", () => {
|
||||
});
|
||||
vi.stubEnv("OPENCLAW_GATEWAY_TOKEN", "env-token");
|
||||
|
||||
await runServiceRestart({
|
||||
serviceNoun: "Gateway",
|
||||
service,
|
||||
renderStartHints: () => [],
|
||||
opts: { json: true },
|
||||
checkTokenDrift: true,
|
||||
});
|
||||
await runServiceRestart(createServiceRunArgs(true));
|
||||
|
||||
const jsonLine = runtimeLogs.find((line) => line.trim().startsWith("{"));
|
||||
const payload = JSON.parse(jsonLine ?? "{}") as { warnings?: string[] };
|
||||
const payload = readJsonLog<{ warnings?: string[] }>();
|
||||
expect(payload.warnings).toEqual(
|
||||
expect.arrayContaining([expect.stringContaining("gateway install --force")]),
|
||||
);
|
||||
@@ -108,8 +109,7 @@ describe("runServiceRestart token drift", () => {
|
||||
|
||||
expect(loadConfig).not.toHaveBeenCalled();
|
||||
expect(service.readCommand).not.toHaveBeenCalled();
|
||||
const jsonLine = runtimeLogs.find((line) => line.trim().startsWith("{"));
|
||||
const payload = JSON.parse(jsonLine ?? "{}") as { warnings?: string[] };
|
||||
const payload = readJsonLog<{ warnings?: string[] }>();
|
||||
expect(payload.warnings).toBeUndefined();
|
||||
});
|
||||
|
||||
@@ -126,8 +126,7 @@ describe("runServiceRestart token drift", () => {
|
||||
}),
|
||||
});
|
||||
|
||||
const jsonLine = runtimeLogs.find((line) => line.trim().startsWith("{"));
|
||||
const payload = JSON.parse(jsonLine ?? "{}") as { result?: string; message?: string };
|
||||
const payload = readJsonLog<{ result?: string; message?: string }>();
|
||||
expect(payload.result).toBe("stopped");
|
||||
expect(payload.message).toContain("unmanaged process");
|
||||
expect(service.stop).not.toHaveBeenCalled();
|
||||
@@ -152,8 +151,7 @@ describe("runServiceRestart token drift", () => {
|
||||
expect(postRestartCheck).toHaveBeenCalledTimes(1);
|
||||
expect(service.restart).not.toHaveBeenCalled();
|
||||
expect(service.readCommand).not.toHaveBeenCalled();
|
||||
const jsonLine = runtimeLogs.find((line) => line.trim().startsWith("{"));
|
||||
const payload = JSON.parse(jsonLine ?? "{}") as { result?: string; message?: string };
|
||||
const payload = readJsonLog<{ result?: string; message?: string }>();
|
||||
expect(payload.result).toBe("restarted");
|
||||
expect(payload.message).toContain("unmanaged process");
|
||||
});
|
||||
@@ -172,8 +170,7 @@ describe("runServiceRestart token drift", () => {
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(postRestartCheck).not.toHaveBeenCalled();
|
||||
const jsonLine = runtimeLogs.find((line) => line.trim().startsWith("{"));
|
||||
const payload = JSON.parse(jsonLine ?? "{}") as { result?: string; message?: string };
|
||||
const payload = readJsonLog<{ result?: string; message?: string }>();
|
||||
expect(payload.result).toBe("scheduled");
|
||||
expect(payload.message).toBe("restart scheduled, gateway will restart momentarily");
|
||||
});
|
||||
@@ -189,8 +186,7 @@ describe("runServiceRestart token drift", () => {
|
||||
});
|
||||
|
||||
expect(service.isLoaded).toHaveBeenCalledTimes(1);
|
||||
const jsonLine = runtimeLogs.find((line) => line.trim().startsWith("{"));
|
||||
const payload = JSON.parse(jsonLine ?? "{}") as { result?: string; message?: string };
|
||||
const payload = readJsonLog<{ result?: string; message?: string }>();
|
||||
expect(payload.result).toBe("scheduled");
|
||||
expect(payload.message).toBe("restart scheduled, gateway will restart momentarily");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user