mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 11:18:37 +00:00
refactor(tests): dedupe cron delivered status assertions
This commit is contained in:
@@ -82,98 +82,96 @@ async function runSingleJobAndReadState(params: {
|
|||||||
return { job, updated: jobs.find((entry) => entry.id === job.id) };
|
return { job, updated: jobs.find((entry) => entry.id === job.id) };
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("CronService persists delivered status", () => {
|
function expectSuccessfulCronRun(
|
||||||
it("persists lastDelivered=true when isolated job reports delivered", async () => {
|
updated: { state: { lastStatus?: string; lastRunStatus?: string } } | undefined,
|
||||||
const store = await makeStorePath();
|
) {
|
||||||
const { cron, finished } = createIsolatedCronWithFinishedBarrier({
|
expect(updated?.state.lastStatus).toBe("ok");
|
||||||
storePath: store.storePath,
|
expect(updated?.state.lastRunStatus).toBe("ok");
|
||||||
delivered: true,
|
}
|
||||||
});
|
|
||||||
|
|
||||||
await cron.start();
|
function expectDeliveryNotRequested(
|
||||||
|
updated:
|
||||||
|
| {
|
||||||
|
state: {
|
||||||
|
lastDelivered?: boolean;
|
||||||
|
lastDeliveryStatus?: string;
|
||||||
|
lastDeliveryError?: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
| undefined,
|
||||||
|
) {
|
||||||
|
expectSuccessfulCronRun(updated);
|
||||||
|
expect(updated?.state.lastDelivered).toBeUndefined();
|
||||||
|
expect(updated?.state.lastDeliveryStatus).toBe("not-requested");
|
||||||
|
expect(updated?.state.lastDeliveryError).toBeUndefined();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runIsolatedJobAndReadState(params: {
|
||||||
|
job: CronAddInput;
|
||||||
|
delivered?: boolean;
|
||||||
|
onFinished?: (evt: { jobId: string; delivered?: boolean; deliveryStatus?: string }) => void;
|
||||||
|
}) {
|
||||||
|
const store = await makeStorePath();
|
||||||
|
const { cron, finished } = createIsolatedCronWithFinishedBarrier({
|
||||||
|
storePath: store.storePath,
|
||||||
|
...(params.delivered !== undefined ? { delivered: params.delivered } : {}),
|
||||||
|
...(params.onFinished ? { onFinished: params.onFinished } : {}),
|
||||||
|
});
|
||||||
|
|
||||||
|
await cron.start();
|
||||||
|
try {
|
||||||
const { updated } = await runSingleJobAndReadState({
|
const { updated } = await runSingleJobAndReadState({
|
||||||
cron,
|
cron,
|
||||||
finished,
|
finished,
|
||||||
job: buildIsolatedAgentTurnJob("delivered-true"),
|
job: params.job,
|
||||||
});
|
});
|
||||||
|
return updated;
|
||||||
|
} finally {
|
||||||
|
cron.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
expect(updated?.state.lastStatus).toBe("ok");
|
describe("CronService persists delivered status", () => {
|
||||||
expect(updated?.state.lastRunStatus).toBe("ok");
|
it("persists lastDelivered=true when isolated job reports delivered", async () => {
|
||||||
|
const updated = await runIsolatedJobAndReadState({
|
||||||
|
job: buildIsolatedAgentTurnJob("delivered-true"),
|
||||||
|
delivered: true,
|
||||||
|
});
|
||||||
|
expectSuccessfulCronRun(updated);
|
||||||
expect(updated?.state.lastDelivered).toBe(true);
|
expect(updated?.state.lastDelivered).toBe(true);
|
||||||
expect(updated?.state.lastDeliveryStatus).toBe("delivered");
|
expect(updated?.state.lastDeliveryStatus).toBe("delivered");
|
||||||
expect(updated?.state.lastDeliveryError).toBeUndefined();
|
expect(updated?.state.lastDeliveryError).toBeUndefined();
|
||||||
|
|
||||||
cron.stop();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("persists lastDelivered=false when isolated job explicitly reports not delivered", async () => {
|
it("persists lastDelivered=false when isolated job explicitly reports not delivered", async () => {
|
||||||
const store = await makeStorePath();
|
const updated = await runIsolatedJobAndReadState({
|
||||||
const { cron, finished } = createIsolatedCronWithFinishedBarrier({
|
job: buildIsolatedAgentTurnJob("delivered-false"),
|
||||||
storePath: store.storePath,
|
|
||||||
delivered: false,
|
delivered: false,
|
||||||
});
|
});
|
||||||
|
expectSuccessfulCronRun(updated);
|
||||||
await cron.start();
|
|
||||||
const { updated } = await runSingleJobAndReadState({
|
|
||||||
cron,
|
|
||||||
finished,
|
|
||||||
job: buildIsolatedAgentTurnJob("delivered-false"),
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(updated?.state.lastStatus).toBe("ok");
|
|
||||||
expect(updated?.state.lastRunStatus).toBe("ok");
|
|
||||||
expect(updated?.state.lastDelivered).toBe(false);
|
expect(updated?.state.lastDelivered).toBe(false);
|
||||||
expect(updated?.state.lastDeliveryStatus).toBe("not-delivered");
|
expect(updated?.state.lastDeliveryStatus).toBe("not-delivered");
|
||||||
expect(updated?.state.lastDeliveryError).toBeUndefined();
|
expect(updated?.state.lastDeliveryError).toBeUndefined();
|
||||||
|
|
||||||
cron.stop();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("persists not-requested delivery state when delivery is not configured", async () => {
|
it("persists not-requested delivery state when delivery is not configured", async () => {
|
||||||
const store = await makeStorePath();
|
const updated = await runIsolatedJobAndReadState({
|
||||||
const { cron, finished } = createIsolatedCronWithFinishedBarrier({
|
|
||||||
storePath: store.storePath,
|
|
||||||
});
|
|
||||||
|
|
||||||
await cron.start();
|
|
||||||
const { updated } = await runSingleJobAndReadState({
|
|
||||||
cron,
|
|
||||||
finished,
|
|
||||||
job: buildIsolatedAgentTurnJob("no-delivery"),
|
job: buildIsolatedAgentTurnJob("no-delivery"),
|
||||||
});
|
});
|
||||||
|
expectDeliveryNotRequested(updated);
|
||||||
expect(updated?.state.lastStatus).toBe("ok");
|
|
||||||
expect(updated?.state.lastRunStatus).toBe("ok");
|
|
||||||
expect(updated?.state.lastDelivered).toBeUndefined();
|
|
||||||
expect(updated?.state.lastDeliveryStatus).toBe("not-requested");
|
|
||||||
expect(updated?.state.lastDeliveryError).toBeUndefined();
|
|
||||||
|
|
||||||
cron.stop();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("persists unknown delivery state when delivery is requested but the runner omits delivered", async () => {
|
it("persists unknown delivery state when delivery is requested but the runner omits delivered", async () => {
|
||||||
const store = await makeStorePath();
|
const updated = await runIsolatedJobAndReadState({
|
||||||
const { cron, finished } = createIsolatedCronWithFinishedBarrier({
|
|
||||||
storePath: store.storePath,
|
|
||||||
});
|
|
||||||
|
|
||||||
await cron.start();
|
|
||||||
const { updated } = await runSingleJobAndReadState({
|
|
||||||
cron,
|
|
||||||
finished,
|
|
||||||
job: {
|
job: {
|
||||||
...buildIsolatedAgentTurnJob("delivery-unknown"),
|
...buildIsolatedAgentTurnJob("delivery-unknown"),
|
||||||
delivery: { mode: "announce", channel: "telegram", to: "123" },
|
delivery: { mode: "announce", channel: "telegram", to: "123" },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
expectSuccessfulCronRun(updated);
|
||||||
expect(updated?.state.lastStatus).toBe("ok");
|
|
||||||
expect(updated?.state.lastRunStatus).toBe("ok");
|
|
||||||
expect(updated?.state.lastDelivered).toBeUndefined();
|
expect(updated?.state.lastDelivered).toBeUndefined();
|
||||||
expect(updated?.state.lastDeliveryStatus).toBe("unknown");
|
expect(updated?.state.lastDeliveryStatus).toBe("unknown");
|
||||||
expect(updated?.state.lastDeliveryError).toBeUndefined();
|
expect(updated?.state.lastDeliveryError).toBeUndefined();
|
||||||
|
|
||||||
cron.stop();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not set lastDelivered for main session jobs", async () => {
|
it("does not set lastDelivered for main session jobs", async () => {
|
||||||
@@ -190,36 +188,24 @@ describe("CronService persists delivered status", () => {
|
|||||||
job: buildMainSessionSystemEventJob("main-session"),
|
job: buildMainSessionSystemEventJob("main-session"),
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(updated?.state.lastStatus).toBe("ok");
|
expectDeliveryNotRequested(updated);
|
||||||
expect(updated?.state.lastRunStatus).toBe("ok");
|
|
||||||
expect(updated?.state.lastDelivered).toBeUndefined();
|
|
||||||
expect(updated?.state.lastDeliveryStatus).toBe("not-requested");
|
|
||||||
expect(enqueueSystemEvent).toHaveBeenCalled();
|
expect(enqueueSystemEvent).toHaveBeenCalled();
|
||||||
|
|
||||||
cron.stop();
|
cron.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("emits delivered in the finished event", async () => {
|
it("emits delivered in the finished event", async () => {
|
||||||
const store = await makeStorePath();
|
|
||||||
let capturedEvent: { jobId: string; delivered?: boolean; deliveryStatus?: string } | undefined;
|
let capturedEvent: { jobId: string; delivered?: boolean; deliveryStatus?: string } | undefined;
|
||||||
const { cron, finished } = createIsolatedCronWithFinishedBarrier({
|
await runIsolatedJobAndReadState({
|
||||||
storePath: store.storePath,
|
job: buildIsolatedAgentTurnJob("event-test"),
|
||||||
delivered: true,
|
delivered: true,
|
||||||
onFinished: (evt) => {
|
onFinished: (evt) => {
|
||||||
capturedEvent = evt;
|
capturedEvent = evt;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
await cron.start();
|
|
||||||
await runSingleJobAndReadState({
|
|
||||||
cron,
|
|
||||||
finished,
|
|
||||||
job: buildIsolatedAgentTurnJob("event-test"),
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(capturedEvent).toBeDefined();
|
expect(capturedEvent).toBeDefined();
|
||||||
expect(capturedEvent?.delivered).toBe(true);
|
expect(capturedEvent?.delivered).toBe(true);
|
||||||
expect(capturedEvent?.deliveryStatus).toBe("delivered");
|
expect(capturedEvent?.deliveryStatus).toBe("delivered");
|
||||||
cron.stop();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user