fix(service): preview lifecycle actions without process mutations

This commit is contained in:
gitea committed 2026-10-20 17:53:48 +08:00
1 parent fac81b08e8
commit b232e934fc
4 files changed
+71 -48

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
import { expect, spyOn, test } from "bun:test";
import * as service from "../service.js";
import { parseArgs } from "../parser.js";
import { handleServiceCommand } from "./service.js";
test("service lifecycle previews never dispatch process or state mutations", async () => {
const dispatch = spyOn(service, "runServiceCommand").mockResolvedValue({ running: false } as any);
try {
for (const action of ["start", "stop", "restart", "run"]) {
const result = await handleServiceCommand(parseArgs(["service", action, "--dry-run", "--yes"]), { jsonMode: true } as any);
expect(JSON.parse(result.stdout).dryRun).toBe(true);
expect(JSON.parse(result.stdout).changes.action).toBe(action);
}
expect(dispatch).not.toHaveBeenCalled();
await handleServiceCommand(parseArgs(["service", "status"]), { jsonMode: true } as any);
expect(dispatch).toHaveBeenCalledTimes(1);
} finally { dispatch.mockRestore(); }
});
+4
View File
@@ -1,3 +1,4 @@
import { isDryRun, mutationPreview } from "../mutation.js";
import { homedir } from "node:os";
import { join } from "node:path";
import { VERSION } from "../../version.js";
@@ -42,6 +43,9 @@ export async function handleServiceCommand(
host,
};
if (["start", "stop", "restart", "run"].includes(action) && isDryRun(parsed, runtime)) {
return mutationPreview(parsed, runtime, "管理 hose-use 服务", action, { action, port, host, stateDir: config.stateDir });
}
const result = await runServiceCommand(action, config);
if (result === undefined) {