mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-30 06:14:23 +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) };
|
||||
}
|
||||
|
||||
describe("CronService persists delivered status", () => {
|
||||
it("persists lastDelivered=true when isolated job reports delivered", async () => {
|
||||
const store = await makeStorePath();
|
||||
const { cron, finished } = createIsolatedCronWithFinishedBarrier({
|
||||
storePath: store.storePath,
|
||||
delivered: true,
|
||||
});
|
||||
function expectSuccessfulCronRun(
|
||||
updated: { state: { lastStatus?: string; lastRunStatus?: string } } | undefined,
|
||||
) {
|
||||
expect(updated?.state.lastStatus).toBe("ok");
|
||||
expect(updated?.state.lastRunStatus).toBe("ok");
|
||||
}
|
||||
|
||||
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({
|
||||
cron,
|
||||
finished,
|
||||
job: buildIsolatedAgentTurnJob("delivered-true"),
|
||||
job: params.job,
|
||||
});
|
||||
return updated;
|
||||
} finally {
|
||||
cron.stop();
|
||||
}
|
||||
}
|
||||
|
||||
expect(updated?.state.lastStatus).toBe("ok");
|
||||
expect(updated?.state.lastRunStatus).toBe("ok");
|
||||
describe("CronService persists delivered status", () => {
|
||||
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.lastDeliveryStatus).toBe("delivered");
|
||||
expect(updated?.state.lastDeliveryError).toBeUndefined();
|
||||
|
||||
cron.stop();
|
||||
});
|
||||
|
||||
it("persists lastDelivered=false when isolated job explicitly reports not delivered", async () => {
|
||||
const store = await makeStorePath();
|
||||
const { cron, finished } = createIsolatedCronWithFinishedBarrier({
|
||||
storePath: store.storePath,
|
||||
const updated = await runIsolatedJobAndReadState({
|
||||
job: buildIsolatedAgentTurnJob("delivered-false"),
|
||||
delivered: false,
|
||||
});
|
||||
|
||||
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");
|
||||
expectSuccessfulCronRun(updated);
|
||||
expect(updated?.state.lastDelivered).toBe(false);
|
||||
expect(updated?.state.lastDeliveryStatus).toBe("not-delivered");
|
||||
expect(updated?.state.lastDeliveryError).toBeUndefined();
|
||||
|
||||
cron.stop();
|
||||
});
|
||||
|
||||
it("persists not-requested delivery state when delivery is not configured", async () => {
|
||||
const store = await makeStorePath();
|
||||
const { cron, finished } = createIsolatedCronWithFinishedBarrier({
|
||||
storePath: store.storePath,
|
||||
});
|
||||
|
||||
await cron.start();
|
||||
const { updated } = await runSingleJobAndReadState({
|
||||
cron,
|
||||
finished,
|
||||
const updated = await runIsolatedJobAndReadState({
|
||||
job: buildIsolatedAgentTurnJob("no-delivery"),
|
||||
});
|
||||
|
||||
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();
|
||||
expectDeliveryNotRequested(updated);
|
||||
});
|
||||
|
||||
it("persists unknown delivery state when delivery is requested but the runner omits delivered", async () => {
|
||||
const store = await makeStorePath();
|
||||
const { cron, finished } = createIsolatedCronWithFinishedBarrier({
|
||||
storePath: store.storePath,
|
||||
});
|
||||
|
||||
await cron.start();
|
||||
const { updated } = await runSingleJobAndReadState({
|
||||
cron,
|
||||
finished,
|
||||
const updated = await runIsolatedJobAndReadState({
|
||||
job: {
|
||||
...buildIsolatedAgentTurnJob("delivery-unknown"),
|
||||
delivery: { mode: "announce", channel: "telegram", to: "123" },
|
||||
},
|
||||
});
|
||||
|
||||
expect(updated?.state.lastStatus).toBe("ok");
|
||||
expect(updated?.state.lastRunStatus).toBe("ok");
|
||||
expectSuccessfulCronRun(updated);
|
||||
expect(updated?.state.lastDelivered).toBeUndefined();
|
||||
expect(updated?.state.lastDeliveryStatus).toBe("unknown");
|
||||
expect(updated?.state.lastDeliveryError).toBeUndefined();
|
||||
|
||||
cron.stop();
|
||||
});
|
||||
|
||||
it("does not set lastDelivered for main session jobs", async () => {
|
||||
@@ -190,36 +188,24 @@ describe("CronService persists delivered status", () => {
|
||||
job: buildMainSessionSystemEventJob("main-session"),
|
||||
});
|
||||
|
||||
expect(updated?.state.lastStatus).toBe("ok");
|
||||
expect(updated?.state.lastRunStatus).toBe("ok");
|
||||
expect(updated?.state.lastDelivered).toBeUndefined();
|
||||
expect(updated?.state.lastDeliveryStatus).toBe("not-requested");
|
||||
expectDeliveryNotRequested(updated);
|
||||
expect(enqueueSystemEvent).toHaveBeenCalled();
|
||||
|
||||
cron.stop();
|
||||
});
|
||||
|
||||
it("emits delivered in the finished event", async () => {
|
||||
const store = await makeStorePath();
|
||||
let capturedEvent: { jobId: string; delivered?: boolean; deliveryStatus?: string } | undefined;
|
||||
const { cron, finished } = createIsolatedCronWithFinishedBarrier({
|
||||
storePath: store.storePath,
|
||||
await runIsolatedJobAndReadState({
|
||||
job: buildIsolatedAgentTurnJob("event-test"),
|
||||
delivered: true,
|
||||
onFinished: (evt) => {
|
||||
capturedEvent = evt;
|
||||
},
|
||||
});
|
||||
|
||||
await cron.start();
|
||||
await runSingleJobAndReadState({
|
||||
cron,
|
||||
finished,
|
||||
job: buildIsolatedAgentTurnJob("event-test"),
|
||||
});
|
||||
|
||||
expect(capturedEvent).toBeDefined();
|
||||
expect(capturedEvent?.delivered).toBe(true);
|
||||
expect(capturedEvent?.deliveryStatus).toBe("delivered");
|
||||
cron.stop();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user