mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 00:58:28 +00:00
refactor(test): share cron isolated agent fixtures
This commit is contained in:
@@ -1,106 +1,22 @@
|
|||||||
|
import "./isolated-agent.mocks.js";
|
||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import path from "node:path";
|
|
||||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
||||||
import type { CliDeps } from "../cli/deps.js";
|
import type { CliDeps } from "../cli/deps.js";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
|
||||||
import type { CronJob } from "./types.js";
|
|
||||||
import { telegramOutbound } from "../channels/plugins/outbound/telegram.js";
|
|
||||||
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
|
||||||
import { createOutboundTestPlugin, createTestRegistry } from "../test-utils/channel-plugins.js";
|
|
||||||
|
|
||||||
vi.mock("../agents/pi-embedded.js", () => ({
|
|
||||||
abortEmbeddedPiRun: vi.fn().mockReturnValue(false),
|
|
||||||
runEmbeddedPiAgent: vi.fn(),
|
|
||||||
resolveEmbeddedSessionLane: (key: string) => `session:${key.trim() || "main"}`,
|
|
||||||
}));
|
|
||||||
vi.mock("../agents/model-catalog.js", () => ({
|
|
||||||
loadModelCatalog: vi.fn(),
|
|
||||||
}));
|
|
||||||
vi.mock("../agents/subagent-announce.js", () => ({
|
|
||||||
runSubagentAnnounceFlow: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
import { loadModelCatalog } from "../agents/model-catalog.js";
|
import { loadModelCatalog } from "../agents/model-catalog.js";
|
||||||
import { runEmbeddedPiAgent } from "../agents/pi-embedded.js";
|
import { runEmbeddedPiAgent } from "../agents/pi-embedded.js";
|
||||||
import { runSubagentAnnounceFlow } from "../agents/subagent-announce.js";
|
import { runSubagentAnnounceFlow } from "../agents/subagent-announce.js";
|
||||||
|
import { telegramOutbound } from "../channels/plugins/outbound/telegram.js";
|
||||||
|
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
||||||
|
import { createOutboundTestPlugin, createTestRegistry } from "../test-utils/channel-plugins.js";
|
||||||
import { runCronIsolatedAgentTurn } from "./isolated-agent.js";
|
import { runCronIsolatedAgentTurn } from "./isolated-agent.js";
|
||||||
|
import {
|
||||||
let fixtureRoot = "";
|
makeCfg,
|
||||||
let fixtureCount = 0;
|
makeJob,
|
||||||
|
withTempCronHome,
|
||||||
async function withTempHome<T>(fn: (home: string) => Promise<T>): Promise<T> {
|
writeSessionStore,
|
||||||
const home = path.join(fixtureRoot, `home-${fixtureCount++}`);
|
} from "./isolated-agent.test-harness.js";
|
||||||
await fs.mkdir(path.join(home, ".openclaw", "agents", "main", "sessions"), { recursive: true });
|
|
||||||
return await fn(home);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function writeSessionStore(home: string) {
|
|
||||||
const dir = path.join(home, ".openclaw", "sessions");
|
|
||||||
await fs.mkdir(dir, { recursive: true });
|
|
||||||
const storePath = path.join(dir, "sessions.json");
|
|
||||||
await fs.writeFile(
|
|
||||||
storePath,
|
|
||||||
JSON.stringify(
|
|
||||||
{
|
|
||||||
"agent:main:main": {
|
|
||||||
sessionId: "main-session",
|
|
||||||
updatedAt: Date.now(),
|
|
||||||
lastProvider: "telegram",
|
|
||||||
lastChannel: "telegram",
|
|
||||||
lastTo: "123",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
null,
|
|
||||||
2,
|
|
||||||
),
|
|
||||||
"utf-8",
|
|
||||||
);
|
|
||||||
return storePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeCfg(
|
|
||||||
home: string,
|
|
||||||
storePath: string,
|
|
||||||
overrides: Partial<OpenClawConfig> = {},
|
|
||||||
): OpenClawConfig {
|
|
||||||
const base: OpenClawConfig = {
|
|
||||||
agents: {
|
|
||||||
defaults: {
|
|
||||||
model: "anthropic/claude-opus-4-5",
|
|
||||||
workspace: path.join(home, "openclaw"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
session: { store: storePath, mainKey: "main" },
|
|
||||||
} as OpenClawConfig;
|
|
||||||
return { ...base, ...overrides };
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeJob(payload: CronJob["payload"]): CronJob {
|
|
||||||
const now = Date.now();
|
|
||||||
return {
|
|
||||||
id: "job-1",
|
|
||||||
name: "job-1",
|
|
||||||
enabled: true,
|
|
||||||
createdAtMs: now,
|
|
||||||
updatedAtMs: now,
|
|
||||||
schedule: { kind: "every", everyMs: 60_000 },
|
|
||||||
sessionTarget: "isolated",
|
|
||||||
wakeMode: "now",
|
|
||||||
payload,
|
|
||||||
state: {},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("runCronIsolatedAgentTurn", () => {
|
describe("runCronIsolatedAgentTurn", () => {
|
||||||
beforeAll(async () => {
|
|
||||||
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-cron-fixtures-"));
|
|
||||||
});
|
|
||||||
|
|
||||||
afterAll(async () => {
|
|
||||||
await fs.rm(fixtureRoot, { recursive: true, force: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.stubEnv("OPENCLAW_TEST_FAST", "1");
|
vi.stubEnv("OPENCLAW_TEST_FAST", "1");
|
||||||
vi.mocked(runEmbeddedPiAgent).mockReset();
|
vi.mocked(runEmbeddedPiAgent).mockReset();
|
||||||
@@ -118,8 +34,12 @@ describe("runCronIsolatedAgentTurn", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("handles media heartbeat delivery and announce cleanup modes", async () => {
|
it("handles media heartbeat delivery and announce cleanup modes", async () => {
|
||||||
await withTempHome(async (home) => {
|
await withTempCronHome(async (home) => {
|
||||||
const storePath = await writeSessionStore(home);
|
const storePath = await writeSessionStore(home, {
|
||||||
|
lastProvider: "telegram",
|
||||||
|
lastChannel: "telegram",
|
||||||
|
lastTo: "123",
|
||||||
|
});
|
||||||
const deps: CliDeps = {
|
const deps: CliDeps = {
|
||||||
sendMessageWhatsApp: vi.fn(),
|
sendMessageWhatsApp: vi.fn(),
|
||||||
sendMessageTelegram: vi.fn().mockResolvedValue({
|
sendMessageTelegram: vi.fn().mockResolvedValue({
|
||||||
|
|||||||
15
src/cron/isolated-agent.mocks.ts
Normal file
15
src/cron/isolated-agent.mocks.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { vi } from "vitest";
|
||||||
|
|
||||||
|
vi.mock("../agents/pi-embedded.js", () => ({
|
||||||
|
abortEmbeddedPiRun: vi.fn().mockReturnValue(false),
|
||||||
|
runEmbeddedPiAgent: vi.fn(),
|
||||||
|
resolveEmbeddedSessionLane: (key: string) => `session:${key.trim() || "main"}`,
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../agents/model-catalog.js", () => ({
|
||||||
|
loadModelCatalog: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../agents/subagent-announce.js", () => ({
|
||||||
|
runSubagentAnnounceFlow: vi.fn(),
|
||||||
|
}));
|
||||||
@@ -1,89 +1,66 @@
|
|||||||
|
import "./isolated-agent.mocks.js";
|
||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import type { CliDeps } from "../cli/deps.js";
|
import type { CliDeps } from "../cli/deps.js";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
|
||||||
import type { CronJob } from "./types.js";
|
|
||||||
import { withTempHome as withTempHomeBase } from "../../test/helpers/temp-home.js";
|
|
||||||
import { telegramOutbound } from "../channels/plugins/outbound/telegram.js";
|
|
||||||
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
|
||||||
import { createOutboundTestPlugin, createTestRegistry } from "../test-utils/channel-plugins.js";
|
|
||||||
|
|
||||||
vi.mock("../agents/pi-embedded.js", () => ({
|
|
||||||
abortEmbeddedPiRun: vi.fn().mockReturnValue(false),
|
|
||||||
runEmbeddedPiAgent: vi.fn(),
|
|
||||||
resolveEmbeddedSessionLane: (key: string) => `session:${key.trim() || "main"}`,
|
|
||||||
}));
|
|
||||||
vi.mock("../agents/model-catalog.js", () => ({
|
|
||||||
loadModelCatalog: vi.fn(),
|
|
||||||
}));
|
|
||||||
vi.mock("../agents/subagent-announce.js", () => ({
|
|
||||||
runSubagentAnnounceFlow: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
import { loadModelCatalog } from "../agents/model-catalog.js";
|
import { loadModelCatalog } from "../agents/model-catalog.js";
|
||||||
import { runEmbeddedPiAgent } from "../agents/pi-embedded.js";
|
import { runEmbeddedPiAgent } from "../agents/pi-embedded.js";
|
||||||
import { runSubagentAnnounceFlow } from "../agents/subagent-announce.js";
|
import { runSubagentAnnounceFlow } from "../agents/subagent-announce.js";
|
||||||
|
import { telegramOutbound } from "../channels/plugins/outbound/telegram.js";
|
||||||
|
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
||||||
|
import { createOutboundTestPlugin, createTestRegistry } from "../test-utils/channel-plugins.js";
|
||||||
import { runCronIsolatedAgentTurn } from "./isolated-agent.js";
|
import { runCronIsolatedAgentTurn } from "./isolated-agent.js";
|
||||||
|
import {
|
||||||
|
makeCfg,
|
||||||
|
makeJob,
|
||||||
|
withTempCronHome,
|
||||||
|
writeSessionStore,
|
||||||
|
} from "./isolated-agent.test-harness.js";
|
||||||
|
|
||||||
async function withTempHome<T>(fn: (home: string) => Promise<T>): Promise<T> {
|
async function expectBestEffortTelegramNotDelivered(
|
||||||
return withTempHomeBase(fn, { prefix: "openclaw-cron-" });
|
payload: Record<string, unknown>,
|
||||||
}
|
): Promise<void> {
|
||||||
|
await withTempCronHome(async (home) => {
|
||||||
|
const storePath = await writeSessionStore(home, { lastProvider: "webchat", lastTo: "" });
|
||||||
|
const deps: CliDeps = {
|
||||||
|
sendMessageWhatsApp: vi.fn(),
|
||||||
|
sendMessageTelegram: vi.fn().mockRejectedValue(new Error("boom")),
|
||||||
|
sendMessageDiscord: vi.fn(),
|
||||||
|
sendMessageSignal: vi.fn(),
|
||||||
|
sendMessageIMessage: vi.fn(),
|
||||||
|
};
|
||||||
|
vi.mocked(runEmbeddedPiAgent).mockResolvedValue({
|
||||||
|
payloads: [payload],
|
||||||
|
meta: {
|
||||||
|
durationMs: 5,
|
||||||
|
agentMeta: { sessionId: "s", provider: "p", model: "m" },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
async function writeSessionStore(home: string) {
|
const res = await runCronIsolatedAgentTurn({
|
||||||
const dir = path.join(home, ".openclaw", "sessions");
|
cfg: makeCfg(home, storePath, {
|
||||||
await fs.mkdir(dir, { recursive: true });
|
channels: { telegram: { botToken: "t-1" } },
|
||||||
const storePath = path.join(dir, "sessions.json");
|
}),
|
||||||
await fs.writeFile(
|
deps,
|
||||||
storePath,
|
job: {
|
||||||
JSON.stringify(
|
...makeJob({ kind: "agentTurn", message: "do it" }),
|
||||||
{
|
delivery: {
|
||||||
"agent:main:main": {
|
mode: "announce",
|
||||||
sessionId: "main-session",
|
channel: "telegram",
|
||||||
updatedAt: Date.now(),
|
to: "123",
|
||||||
lastProvider: "webchat",
|
bestEffort: true,
|
||||||
lastTo: "",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
null,
|
message: "do it",
|
||||||
2,
|
sessionKey: "cron:job-1",
|
||||||
),
|
lane: "cron",
|
||||||
"utf-8",
|
});
|
||||||
);
|
|
||||||
return storePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeCfg(
|
expect(res.status).toBe("ok");
|
||||||
home: string,
|
expect(res.delivered).toBe(false);
|
||||||
storePath: string,
|
expect(runSubagentAnnounceFlow).not.toHaveBeenCalled();
|
||||||
overrides: Partial<OpenClawConfig> = {},
|
expect(deps.sendMessageTelegram).toHaveBeenCalledTimes(1);
|
||||||
): OpenClawConfig {
|
});
|
||||||
const base: OpenClawConfig = {
|
|
||||||
agents: {
|
|
||||||
defaults: {
|
|
||||||
model: "anthropic/claude-opus-4-5",
|
|
||||||
workspace: path.join(home, "openclaw"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
session: { store: storePath, mainKey: "main" },
|
|
||||||
} as OpenClawConfig;
|
|
||||||
return { ...base, ...overrides };
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeJob(payload: CronJob["payload"]): CronJob {
|
|
||||||
const now = Date.now();
|
|
||||||
return {
|
|
||||||
id: "job-1",
|
|
||||||
name: "job-1",
|
|
||||||
enabled: true,
|
|
||||||
createdAtMs: now,
|
|
||||||
updatedAtMs: now,
|
|
||||||
schedule: { kind: "every", everyMs: 60_000 },
|
|
||||||
sessionTarget: "isolated",
|
|
||||||
wakeMode: "now",
|
|
||||||
payload,
|
|
||||||
state: {},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("runCronIsolatedAgentTurn", () => {
|
describe("runCronIsolatedAgentTurn", () => {
|
||||||
@@ -103,8 +80,8 @@ describe("runCronIsolatedAgentTurn", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("delivers directly when delivery has an explicit target", async () => {
|
it("delivers directly when delivery has an explicit target", async () => {
|
||||||
await withTempHome(async (home) => {
|
await withTempCronHome(async (home) => {
|
||||||
const storePath = await writeSessionStore(home);
|
const storePath = await writeSessionStore(home, { lastProvider: "webchat", lastTo: "" });
|
||||||
const deps: CliDeps = {
|
const deps: CliDeps = {
|
||||||
sendMessageWhatsApp: vi.fn(),
|
sendMessageWhatsApp: vi.fn(),
|
||||||
sendMessageTelegram: vi.fn(),
|
sendMessageTelegram: vi.fn(),
|
||||||
@@ -145,8 +122,8 @@ describe("runCronIsolatedAgentTurn", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("delivers the final payload text when delivery has an explicit target", async () => {
|
it("delivers the final payload text when delivery has an explicit target", async () => {
|
||||||
await withTempHome(async (home) => {
|
await withTempCronHome(async (home) => {
|
||||||
const storePath = await writeSessionStore(home);
|
const storePath = await writeSessionStore(home, { lastProvider: "webchat", lastTo: "" });
|
||||||
const deps: CliDeps = {
|
const deps: CliDeps = {
|
||||||
sendMessageWhatsApp: vi.fn(),
|
sendMessageWhatsApp: vi.fn(),
|
||||||
sendMessageTelegram: vi.fn(),
|
sendMessageTelegram: vi.fn(),
|
||||||
@@ -187,8 +164,8 @@ describe("runCronIsolatedAgentTurn", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("passes resolved threadId into shared subagent announce flow", async () => {
|
it("passes resolved threadId into shared subagent announce flow", async () => {
|
||||||
await withTempHome(async (home) => {
|
await withTempCronHome(async (home) => {
|
||||||
const storePath = await writeSessionStore(home);
|
const storePath = await writeSessionStore(home, { lastProvider: "webchat", lastTo: "" });
|
||||||
await fs.writeFile(
|
await fs.writeFile(
|
||||||
storePath,
|
storePath,
|
||||||
JSON.stringify(
|
JSON.stringify(
|
||||||
@@ -247,8 +224,8 @@ describe("runCronIsolatedAgentTurn", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("skips announce when messaging tool already sent to target", async () => {
|
it("skips announce when messaging tool already sent to target", async () => {
|
||||||
await withTempHome(async (home) => {
|
await withTempCronHome(async (home) => {
|
||||||
const storePath = await writeSessionStore(home);
|
const storePath = await writeSessionStore(home, { lastProvider: "webchat", lastTo: "" });
|
||||||
const deps: CliDeps = {
|
const deps: CliDeps = {
|
||||||
sendMessageWhatsApp: vi.fn(),
|
sendMessageWhatsApp: vi.fn(),
|
||||||
sendMessageTelegram: vi.fn(),
|
sendMessageTelegram: vi.fn(),
|
||||||
@@ -288,52 +265,15 @@ describe("runCronIsolatedAgentTurn", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("reports not-delivered when best-effort structured outbound sends all fail", async () => {
|
it("reports not-delivered when best-effort structured outbound sends all fail", async () => {
|
||||||
await withTempHome(async (home) => {
|
await expectBestEffortTelegramNotDelivered({
|
||||||
const storePath = await writeSessionStore(home);
|
text: "caption",
|
||||||
const deps: CliDeps = {
|
mediaUrl: "https://example.com/img.png",
|
||||||
sendMessageWhatsApp: vi.fn(),
|
|
||||||
sendMessageTelegram: vi.fn().mockRejectedValue(new Error("boom")),
|
|
||||||
sendMessageDiscord: vi.fn(),
|
|
||||||
sendMessageSignal: vi.fn(),
|
|
||||||
sendMessageIMessage: vi.fn(),
|
|
||||||
};
|
|
||||||
vi.mocked(runEmbeddedPiAgent).mockResolvedValue({
|
|
||||||
payloads: [{ text: "caption", mediaUrl: "https://example.com/img.png" }],
|
|
||||||
meta: {
|
|
||||||
durationMs: 5,
|
|
||||||
agentMeta: { sessionId: "s", provider: "p", model: "m" },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const res = await runCronIsolatedAgentTurn({
|
|
||||||
cfg: makeCfg(home, storePath, {
|
|
||||||
channels: { telegram: { botToken: "t-1" } },
|
|
||||||
}),
|
|
||||||
deps,
|
|
||||||
job: {
|
|
||||||
...makeJob({ kind: "agentTurn", message: "do it" }),
|
|
||||||
delivery: {
|
|
||||||
mode: "announce",
|
|
||||||
channel: "telegram",
|
|
||||||
to: "123",
|
|
||||||
bestEffort: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
message: "do it",
|
|
||||||
sessionKey: "cron:job-1",
|
|
||||||
lane: "cron",
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.status).toBe("ok");
|
|
||||||
expect(res.delivered).toBe(false);
|
|
||||||
expect(runSubagentAnnounceFlow).not.toHaveBeenCalled();
|
|
||||||
expect(deps.sendMessageTelegram).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("skips announce for heartbeat-only output", async () => {
|
it("skips announce for heartbeat-only output", async () => {
|
||||||
await withTempHome(async (home) => {
|
await withTempCronHome(async (home) => {
|
||||||
const storePath = await writeSessionStore(home);
|
const storePath = await writeSessionStore(home, { lastProvider: "webchat", lastTo: "" });
|
||||||
const deps: CliDeps = {
|
const deps: CliDeps = {
|
||||||
sendMessageWhatsApp: vi.fn(),
|
sendMessageWhatsApp: vi.fn(),
|
||||||
sendMessageTelegram: vi.fn(),
|
sendMessageTelegram: vi.fn(),
|
||||||
@@ -370,8 +310,8 @@ describe("runCronIsolatedAgentTurn", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("fails when direct delivery fails and best-effort is disabled", async () => {
|
it("fails when direct delivery fails and best-effort is disabled", async () => {
|
||||||
await withTempHome(async (home) => {
|
await withTempCronHome(async (home) => {
|
||||||
const storePath = await writeSessionStore(home);
|
const storePath = await writeSessionStore(home, { lastProvider: "webchat", lastTo: "" });
|
||||||
const deps: CliDeps = {
|
const deps: CliDeps = {
|
||||||
sendMessageWhatsApp: vi.fn(),
|
sendMessageWhatsApp: vi.fn(),
|
||||||
sendMessageTelegram: vi.fn().mockRejectedValue(new Error("boom")),
|
sendMessageTelegram: vi.fn().mockRejectedValue(new Error("boom")),
|
||||||
@@ -408,45 +348,6 @@ describe("runCronIsolatedAgentTurn", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("ignores direct delivery failures when best-effort is enabled", async () => {
|
it("ignores direct delivery failures when best-effort is enabled", async () => {
|
||||||
await withTempHome(async (home) => {
|
await expectBestEffortTelegramNotDelivered({ text: "hello from cron" });
|
||||||
const storePath = await writeSessionStore(home);
|
|
||||||
const deps: CliDeps = {
|
|
||||||
sendMessageWhatsApp: vi.fn(),
|
|
||||||
sendMessageTelegram: vi.fn().mockRejectedValue(new Error("boom")),
|
|
||||||
sendMessageDiscord: vi.fn(),
|
|
||||||
sendMessageSignal: vi.fn(),
|
|
||||||
sendMessageIMessage: vi.fn(),
|
|
||||||
};
|
|
||||||
vi.mocked(runEmbeddedPiAgent).mockResolvedValue({
|
|
||||||
payloads: [{ text: "hello from cron" }],
|
|
||||||
meta: {
|
|
||||||
durationMs: 5,
|
|
||||||
agentMeta: { sessionId: "s", provider: "p", model: "m" },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const res = await runCronIsolatedAgentTurn({
|
|
||||||
cfg: makeCfg(home, storePath, {
|
|
||||||
channels: { telegram: { botToken: "t-1" } },
|
|
||||||
}),
|
|
||||||
deps,
|
|
||||||
job: {
|
|
||||||
...makeJob({ kind: "agentTurn", message: "do it" }),
|
|
||||||
delivery: {
|
|
||||||
mode: "announce",
|
|
||||||
channel: "telegram",
|
|
||||||
to: "123",
|
|
||||||
bestEffort: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
message: "do it",
|
|
||||||
sessionKey: "cron:job-1",
|
|
||||||
lane: "cron",
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.status).toBe("ok");
|
|
||||||
expect(res.delivered).toBe(false);
|
|
||||||
expect(runSubagentAnnounceFlow).not.toHaveBeenCalled();
|
|
||||||
expect(deps.sendMessageTelegram).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
67
src/cron/isolated-agent.test-harness.ts
Normal file
67
src/cron/isolated-agent.test-harness.ts
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
import fs from "node:fs/promises";
|
||||||
|
import path from "node:path";
|
||||||
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
|
import type { CronJob } from "./types.js";
|
||||||
|
import { withTempHome as withTempHomeBase } from "../../test/helpers/temp-home.js";
|
||||||
|
|
||||||
|
export async function withTempCronHome<T>(fn: (home: string) => Promise<T>): Promise<T> {
|
||||||
|
return withTempHomeBase(fn, { prefix: "openclaw-cron-" });
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function writeSessionStore(
|
||||||
|
home: string,
|
||||||
|
session: { lastProvider: string; lastTo: string; lastChannel?: string },
|
||||||
|
): Promise<string> {
|
||||||
|
const dir = path.join(home, ".openclaw", "sessions");
|
||||||
|
await fs.mkdir(dir, { recursive: true });
|
||||||
|
const storePath = path.join(dir, "sessions.json");
|
||||||
|
await fs.writeFile(
|
||||||
|
storePath,
|
||||||
|
JSON.stringify(
|
||||||
|
{
|
||||||
|
"agent:main:main": {
|
||||||
|
sessionId: "main-session",
|
||||||
|
updatedAt: Date.now(),
|
||||||
|
...session,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
2,
|
||||||
|
),
|
||||||
|
"utf-8",
|
||||||
|
);
|
||||||
|
return storePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function makeCfg(
|
||||||
|
home: string,
|
||||||
|
storePath: string,
|
||||||
|
overrides: Partial<OpenClawConfig> = {},
|
||||||
|
): OpenClawConfig {
|
||||||
|
const base: OpenClawConfig = {
|
||||||
|
agents: {
|
||||||
|
defaults: {
|
||||||
|
model: "anthropic/claude-opus-4-5",
|
||||||
|
workspace: path.join(home, "openclaw"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
session: { store: storePath, mainKey: "main" },
|
||||||
|
} as OpenClawConfig;
|
||||||
|
return { ...base, ...overrides };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function makeJob(payload: CronJob["payload"]): CronJob {
|
||||||
|
const now = Date.now();
|
||||||
|
return {
|
||||||
|
id: "job-1",
|
||||||
|
name: "job-1",
|
||||||
|
enabled: true,
|
||||||
|
createdAtMs: now,
|
||||||
|
updatedAtMs: now,
|
||||||
|
schedule: { kind: "every", everyMs: 60_000 },
|
||||||
|
sessionTarget: "isolated",
|
||||||
|
wakeMode: "now",
|
||||||
|
payload,
|
||||||
|
state: {},
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user