test: share lifecycle config guard helpers

This commit is contained in:
Peter Steinberger 2026-03-14 00:07:52 +00:00
parent 6e7e82e5e7
commit 68a507ab31
1 changed files with 35 additions and 74 deletions

View File

@ -27,6 +27,28 @@ vi.mock("../../runtime.js", () => ({
defaultRuntime, defaultRuntime,
})); }));
function setConfigSnapshot(params: {
exists: boolean;
valid: boolean;
issues?: Array<{ path: string; message: string }>;
}) {
readConfigFileSnapshotMock.mockResolvedValue({
exists: params.exists,
valid: params.valid,
config: {},
issues: params.issues ?? [],
});
}
function createServiceRunArgs() {
return {
serviceNoun: "Gateway",
service,
renderStartHints: () => [],
opts: { json: true },
};
}
describe("runServiceRestart config pre-flight (#35862)", () => { describe("runServiceRestart config pre-flight (#35862)", () => {
let runServiceRestart: typeof import("./lifecycle-core.js").runServiceRestart; let runServiceRestart: typeof import("./lifecycle-core.js").runServiceRestart;
@ -37,12 +59,7 @@ describe("runServiceRestart config pre-flight (#35862)", () => {
beforeEach(() => { beforeEach(() => {
resetLifecycleRuntimeLogs(); resetLifecycleRuntimeLogs();
readConfigFileSnapshotMock.mockReset(); readConfigFileSnapshotMock.mockReset();
readConfigFileSnapshotMock.mockResolvedValue({ setConfigSnapshot({ exists: true, valid: true });
exists: true,
valid: true,
config: {},
issues: [],
});
loadConfig.mockReset(); loadConfig.mockReset();
loadConfig.mockReturnValue({}); loadConfig.mockReturnValue({});
resetLifecycleServiceMocks(); resetLifecycleServiceMocks();
@ -50,58 +67,30 @@ describe("runServiceRestart config pre-flight (#35862)", () => {
}); });
it("aborts restart when config is invalid", async () => { it("aborts restart when config is invalid", async () => {
readConfigFileSnapshotMock.mockResolvedValue({ setConfigSnapshot({
exists: true, exists: true,
valid: false, valid: false,
config: {},
issues: [{ path: "agents.defaults.pdfModel", message: "Unrecognized key" }], issues: [{ path: "agents.defaults.pdfModel", message: "Unrecognized key" }],
}); });
await expect( await expect(runServiceRestart(createServiceRunArgs())).rejects.toThrow("__exit__:1");
runServiceRestart({
serviceNoun: "Gateway",
service,
renderStartHints: () => [],
opts: { json: true },
}),
).rejects.toThrow("__exit__:1");
expect(service.restart).not.toHaveBeenCalled(); expect(service.restart).not.toHaveBeenCalled();
}); });
it("proceeds with restart when config is valid", async () => { it("proceeds with restart when config is valid", async () => {
readConfigFileSnapshotMock.mockResolvedValue({ setConfigSnapshot({ exists: true, valid: true });
exists: true,
valid: true,
config: {},
issues: [],
});
const result = await runServiceRestart({ const result = await runServiceRestart(createServiceRunArgs());
serviceNoun: "Gateway",
service,
renderStartHints: () => [],
opts: { json: true },
});
expect(result).toBe(true); expect(result).toBe(true);
expect(service.restart).toHaveBeenCalledTimes(1); expect(service.restart).toHaveBeenCalledTimes(1);
}); });
it("proceeds with restart when config file does not exist", async () => { it("proceeds with restart when config file does not exist", async () => {
readConfigFileSnapshotMock.mockResolvedValue({ setConfigSnapshot({ exists: false, valid: true });
exists: false,
valid: true,
config: {},
issues: [],
});
const result = await runServiceRestart({ const result = await runServiceRestart(createServiceRunArgs());
serviceNoun: "Gateway",
service,
renderStartHints: () => [],
opts: { json: true },
});
expect(result).toBe(true); expect(result).toBe(true);
expect(service.restart).toHaveBeenCalledTimes(1); expect(service.restart).toHaveBeenCalledTimes(1);
@ -110,12 +99,7 @@ describe("runServiceRestart config pre-flight (#35862)", () => {
it("proceeds with restart when snapshot read throws", async () => { it("proceeds with restart when snapshot read throws", async () => {
readConfigFileSnapshotMock.mockRejectedValue(new Error("read failed")); readConfigFileSnapshotMock.mockRejectedValue(new Error("read failed"));
const result = await runServiceRestart({ const result = await runServiceRestart(createServiceRunArgs());
serviceNoun: "Gateway",
service,
renderStartHints: () => [],
opts: { json: true },
});
expect(result).toBe(true); expect(result).toBe(true);
expect(service.restart).toHaveBeenCalledTimes(1); expect(service.restart).toHaveBeenCalledTimes(1);
@ -132,49 +116,26 @@ describe("runServiceStart config pre-flight (#35862)", () => {
beforeEach(() => { beforeEach(() => {
resetLifecycleRuntimeLogs(); resetLifecycleRuntimeLogs();
readConfigFileSnapshotMock.mockReset(); readConfigFileSnapshotMock.mockReset();
readConfigFileSnapshotMock.mockResolvedValue({ setConfigSnapshot({ exists: true, valid: true });
exists: true,
valid: true,
config: {},
issues: [],
});
resetLifecycleServiceMocks(); resetLifecycleServiceMocks();
}); });
it("aborts start when config is invalid", async () => { it("aborts start when config is invalid", async () => {
readConfigFileSnapshotMock.mockResolvedValue({ setConfigSnapshot({
exists: true, exists: true,
valid: false, valid: false,
config: {},
issues: [{ path: "agents.defaults.pdfModel", message: "Unrecognized key" }], issues: [{ path: "agents.defaults.pdfModel", message: "Unrecognized key" }],
}); });
await expect( await expect(runServiceStart(createServiceRunArgs())).rejects.toThrow("__exit__:1");
runServiceStart({
serviceNoun: "Gateway",
service,
renderStartHints: () => [],
opts: { json: true },
}),
).rejects.toThrow("__exit__:1");
expect(service.restart).not.toHaveBeenCalled(); expect(service.restart).not.toHaveBeenCalled();
}); });
it("proceeds with start when config is valid", async () => { it("proceeds with start when config is valid", async () => {
readConfigFileSnapshotMock.mockResolvedValue({ setConfigSnapshot({ exists: true, valid: true });
exists: true,
valid: true,
config: {},
issues: [],
});
await runServiceStart({ await runServiceStart(createServiceRunArgs());
serviceNoun: "Gateway",
service,
renderStartHints: () => [],
opts: { json: true },
});
expect(service.restart).toHaveBeenCalledTimes(1); expect(service.restart).toHaveBeenCalledTimes(1);
}); });