mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-18 11:17:27 +00:00
test(heartbeat): dedupe sandbox/session helpers and collapse ack cases
This commit is contained in:
@@ -1,17 +1,20 @@
|
||||
import fs from "node:fs/promises";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import { resolveMainSessionKey } from "../config/sessions.js";
|
||||
import { runHeartbeatOnce, type HeartbeatDeps } from "./heartbeat-runner.js";
|
||||
import { installHeartbeatRunnerTestRuntime } from "./heartbeat-runner.test-harness.js";
|
||||
import { seedSessionStore, withTempHeartbeatSandbox } from "./heartbeat-runner.test-utils.js";
|
||||
import {
|
||||
seedMainSessionStore,
|
||||
withTempHeartbeatSandbox,
|
||||
withTempTelegramHeartbeatSandbox,
|
||||
} from "./heartbeat-runner.test-utils.js";
|
||||
|
||||
// Avoid pulling optional runtime deps during isolated runs.
|
||||
vi.mock("jiti", () => ({ createJiti: () => () => ({}) }));
|
||||
|
||||
installHeartbeatRunnerTestRuntime();
|
||||
|
||||
describe("resolveHeartbeatIntervalMs", () => {
|
||||
describe("runHeartbeatOnce ack handling", () => {
|
||||
function createHeartbeatConfig(params: {
|
||||
tmpDir: string;
|
||||
storePath: string;
|
||||
@@ -32,22 +35,6 @@ describe("resolveHeartbeatIntervalMs", () => {
|
||||
};
|
||||
}
|
||||
|
||||
async function seedMainSession(
|
||||
storePath: string,
|
||||
cfg: OpenClawConfig,
|
||||
session: {
|
||||
sessionId?: string;
|
||||
updatedAt?: number;
|
||||
lastChannel: string;
|
||||
lastProvider: string;
|
||||
lastTo: string;
|
||||
},
|
||||
) {
|
||||
const sessionKey = resolveMainSessionKey(cfg);
|
||||
await seedSessionStore(storePath, sessionKey, session);
|
||||
return sessionKey;
|
||||
}
|
||||
|
||||
function makeWhatsAppDeps(
|
||||
params: {
|
||||
sendWhatsApp?: ReturnType<typeof vi.fn>;
|
||||
@@ -84,16 +71,6 @@ describe("resolveHeartbeatIntervalMs", () => {
|
||||
} satisfies HeartbeatDeps;
|
||||
}
|
||||
|
||||
async function withTempTelegramHeartbeatSandbox<T>(
|
||||
fn: (ctx: {
|
||||
tmpDir: string;
|
||||
storePath: string;
|
||||
replySpy: ReturnType<typeof vi.spyOn>;
|
||||
}) => Promise<T>,
|
||||
) {
|
||||
return withTempHeartbeatSandbox(fn, { unsetEnvVars: ["TELEGRAM_BOT_TOKEN"] });
|
||||
}
|
||||
|
||||
function createMessageSendSpy(extra: Record<string, unknown> = {}) {
|
||||
return vi.fn().mockResolvedValue({
|
||||
messageId: "m1",
|
||||
@@ -125,7 +102,7 @@ describe("resolveHeartbeatIntervalMs", () => {
|
||||
...(params.messages ? { messages: params.messages } : {}),
|
||||
});
|
||||
|
||||
await seedMainSession(params.storePath, cfg, {
|
||||
await seedMainSessionStore(params.storePath, cfg, {
|
||||
lastChannel: "telegram",
|
||||
lastProvider: "telegram",
|
||||
lastTo: "12345",
|
||||
@@ -170,7 +147,7 @@ describe("resolveHeartbeatIntervalMs", () => {
|
||||
visibility?: Record<string, unknown>;
|
||||
}): Promise<OpenClawConfig> {
|
||||
const cfg = createWhatsAppHeartbeatConfig(params);
|
||||
await seedMainSession(params.storePath, cfg, {
|
||||
await seedMainSessionStore(params.storePath, cfg, {
|
||||
lastChannel: "whatsapp",
|
||||
lastProvider: "whatsapp",
|
||||
lastTo: "+1555",
|
||||
@@ -186,7 +163,7 @@ describe("resolveHeartbeatIntervalMs", () => {
|
||||
heartbeat: { ackMaxChars: 0 },
|
||||
});
|
||||
|
||||
await seedMainSession(storePath, cfg, {
|
||||
await seedMainSessionStore(storePath, cfg, {
|
||||
lastChannel: "whatsapp",
|
||||
lastProvider: "whatsapp",
|
||||
lastTo: "+1555",
|
||||
@@ -212,7 +189,7 @@ describe("resolveHeartbeatIntervalMs", () => {
|
||||
visibility: { showOk: true },
|
||||
});
|
||||
|
||||
await seedMainSession(storePath, cfg, {
|
||||
await seedMainSessionStore(storePath, cfg, {
|
||||
lastChannel: "whatsapp",
|
||||
lastProvider: "whatsapp",
|
||||
lastTo: "+1555",
|
||||
@@ -231,49 +208,39 @@ describe("resolveHeartbeatIntervalMs", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("does not deliver HEARTBEAT_OK to telegram when showOk is false", async () => {
|
||||
it.each([
|
||||
{
|
||||
title: "does not deliver HEARTBEAT_OK to telegram when showOk is false",
|
||||
replyText: "HEARTBEAT_OK",
|
||||
expectedCalls: 0,
|
||||
},
|
||||
{
|
||||
title: "strips responsePrefix before HEARTBEAT_OK detection and suppresses short ack text",
|
||||
replyText: "[openclaw] HEARTBEAT_OK all good",
|
||||
messages: { responsePrefix: "[openclaw]" },
|
||||
expectedCalls: 0,
|
||||
},
|
||||
{
|
||||
title: "does not strip alphanumeric responsePrefix from larger words",
|
||||
replyText: "History check complete",
|
||||
messages: { responsePrefix: "Hi" },
|
||||
expectedCalls: 1,
|
||||
expectedText: "History check complete",
|
||||
},
|
||||
])("$title", async ({ replyText, messages, expectedCalls, expectedText }) => {
|
||||
await withTempTelegramHeartbeatSandbox(async ({ tmpDir, storePath, replySpy }) => {
|
||||
const sendTelegram = await runTelegramHeartbeatWithDefaults({
|
||||
tmpDir,
|
||||
storePath,
|
||||
replySpy,
|
||||
replyText: "HEARTBEAT_OK",
|
||||
replyText,
|
||||
messages,
|
||||
});
|
||||
|
||||
expect(sendTelegram).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it("strips responsePrefix before HEARTBEAT_OK detection and suppresses short ack text", async () => {
|
||||
await withTempTelegramHeartbeatSandbox(async ({ tmpDir, storePath, replySpy }) => {
|
||||
const sendTelegram = await runTelegramHeartbeatWithDefaults({
|
||||
tmpDir,
|
||||
storePath,
|
||||
replySpy,
|
||||
replyText: "[openclaw] HEARTBEAT_OK all good",
|
||||
messages: { responsePrefix: "[openclaw]" },
|
||||
});
|
||||
|
||||
expect(sendTelegram).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it("does not strip alphanumeric responsePrefix from larger words", async () => {
|
||||
await withTempTelegramHeartbeatSandbox(async ({ tmpDir, storePath, replySpy }) => {
|
||||
const sendTelegram = await runTelegramHeartbeatWithDefaults({
|
||||
tmpDir,
|
||||
storePath,
|
||||
replySpy,
|
||||
replyText: "History check complete",
|
||||
messages: { responsePrefix: "Hi" },
|
||||
});
|
||||
|
||||
expect(sendTelegram).toHaveBeenCalledTimes(1);
|
||||
expect(sendTelegram).toHaveBeenCalledWith(
|
||||
"12345",
|
||||
"History check complete",
|
||||
expect.any(Object),
|
||||
);
|
||||
expect(sendTelegram).toHaveBeenCalledTimes(expectedCalls);
|
||||
if (expectedText) {
|
||||
expect(sendTelegram).toHaveBeenCalledWith("12345", expectedText, expect.any(Object));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -285,7 +252,7 @@ describe("resolveHeartbeatIntervalMs", () => {
|
||||
visibility: { showOk: false, showAlerts: false, useIndicator: false },
|
||||
});
|
||||
|
||||
await seedMainSession(storePath, cfg, {
|
||||
await seedMainSessionStore(storePath, cfg, {
|
||||
lastChannel: "whatsapp",
|
||||
lastProvider: "whatsapp",
|
||||
lastTo: "+1555",
|
||||
@@ -332,7 +299,7 @@ describe("resolveHeartbeatIntervalMs", () => {
|
||||
storePath,
|
||||
});
|
||||
|
||||
const sessionKey = await seedMainSession(storePath, cfg, {
|
||||
const sessionKey = await seedMainSessionStore(storePath, cfg, {
|
||||
updatedAt: originalUpdatedAt,
|
||||
lastChannel: "whatsapp",
|
||||
lastProvider: "whatsapp",
|
||||
@@ -402,7 +369,7 @@ describe("resolveHeartbeatIntervalMs", () => {
|
||||
heartbeat: params.heartbeat,
|
||||
channels: { telegram: params.telegram },
|
||||
});
|
||||
await seedMainSession(storePath, cfg, {
|
||||
await seedMainSessionStore(storePath, cfg, {
|
||||
lastChannel: "telegram",
|
||||
lastProvider: "telegram",
|
||||
lastTo: "123456",
|
||||
|
||||
@@ -3,6 +3,8 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { vi } from "vitest";
|
||||
import * as replyModule from "../auto-reply/reply.js";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import { resolveMainSessionKey } from "../config/sessions.js";
|
||||
|
||||
export type HeartbeatSessionSeed = {
|
||||
sessionId?: string;
|
||||
@@ -33,6 +35,16 @@ export async function seedSessionStore(
|
||||
);
|
||||
}
|
||||
|
||||
export async function seedMainSessionStore(
|
||||
storePath: string,
|
||||
cfg: OpenClawConfig,
|
||||
session: HeartbeatSessionSeed,
|
||||
): Promise<string> {
|
||||
const sessionKey = resolveMainSessionKey(cfg);
|
||||
await seedSessionStore(storePath, sessionKey, session);
|
||||
return sessionKey;
|
||||
}
|
||||
|
||||
export async function withTempHeartbeatSandbox<T>(
|
||||
fn: (ctx: {
|
||||
tmpDir: string;
|
||||
@@ -67,3 +79,19 @@ export async function withTempHeartbeatSandbox<T>(
|
||||
await fs.rm(tmpDir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
export async function withTempTelegramHeartbeatSandbox<T>(
|
||||
fn: (ctx: {
|
||||
tmpDir: string;
|
||||
storePath: string;
|
||||
replySpy: ReturnType<typeof vi.spyOn>;
|
||||
}) => Promise<T>,
|
||||
options?: {
|
||||
prefix?: string;
|
||||
},
|
||||
): Promise<T> {
|
||||
return withTempHeartbeatSandbox(fn, {
|
||||
prefix: options?.prefix,
|
||||
unsetEnvVars: ["TELEGRAM_BOT_TOKEN"],
|
||||
});
|
||||
}
|
||||
|
||||
@@ -9,7 +9,10 @@ import { setActivePluginRegistry } from "../plugins/runtime.js";
|
||||
import { createPluginRuntime } from "../plugins/runtime/index.js";
|
||||
import { createTestRegistry } from "../test-utils/channel-plugins.js";
|
||||
import { runHeartbeatOnce } from "./heartbeat-runner.js";
|
||||
import { seedSessionStore, withTempHeartbeatSandbox } from "./heartbeat-runner.test-utils.js";
|
||||
import {
|
||||
seedSessionStore,
|
||||
withTempTelegramHeartbeatSandbox,
|
||||
} from "./heartbeat-runner.test-utils.js";
|
||||
|
||||
// Avoid pulling optional runtime deps during isolated runs.
|
||||
vi.mock("jiti", () => ({ createJiti: () => () => ({}) }));
|
||||
@@ -37,19 +40,6 @@ describe("heartbeat transcript pruning", () => {
|
||||
return existingContent;
|
||||
}
|
||||
|
||||
async function withTempTelegramHeartbeatSandbox<T>(
|
||||
fn: (ctx: {
|
||||
tmpDir: string;
|
||||
storePath: string;
|
||||
replySpy: ReturnType<typeof vi.spyOn>;
|
||||
}) => Promise<T>,
|
||||
) {
|
||||
return withTempHeartbeatSandbox(fn, {
|
||||
prefix: "openclaw-hb-prune-",
|
||||
unsetEnvVars: ["TELEGRAM_BOT_TOKEN"],
|
||||
});
|
||||
}
|
||||
|
||||
async function runTranscriptScenario(params: {
|
||||
sessionId: string;
|
||||
reply: {
|
||||
@@ -63,45 +53,48 @@ describe("heartbeat transcript pruning", () => {
|
||||
};
|
||||
expectPruned: boolean;
|
||||
}) {
|
||||
await withTempTelegramHeartbeatSandbox(async ({ tmpDir, storePath, replySpy }) => {
|
||||
const sessionKey = resolveMainSessionKey(undefined);
|
||||
const transcriptPath = path.join(tmpDir, `${params.sessionId}.jsonl`);
|
||||
const originalContent = await createTranscriptWithContent(transcriptPath, params.sessionId);
|
||||
const originalSize = (await fs.stat(transcriptPath)).size;
|
||||
await withTempTelegramHeartbeatSandbox(
|
||||
async ({ tmpDir, storePath, replySpy }) => {
|
||||
const sessionKey = resolveMainSessionKey(undefined);
|
||||
const transcriptPath = path.join(tmpDir, `${params.sessionId}.jsonl`);
|
||||
const originalContent = await createTranscriptWithContent(transcriptPath, params.sessionId);
|
||||
const originalSize = (await fs.stat(transcriptPath)).size;
|
||||
|
||||
await seedSessionStore(storePath, sessionKey, {
|
||||
sessionId: params.sessionId,
|
||||
lastChannel: "telegram",
|
||||
lastProvider: "telegram",
|
||||
lastTo: "user123",
|
||||
});
|
||||
await seedSessionStore(storePath, sessionKey, {
|
||||
sessionId: params.sessionId,
|
||||
lastChannel: "telegram",
|
||||
lastProvider: "telegram",
|
||||
lastTo: "user123",
|
||||
});
|
||||
|
||||
replySpy.mockResolvedValueOnce(params.reply);
|
||||
replySpy.mockResolvedValueOnce(params.reply);
|
||||
|
||||
const cfg = {
|
||||
version: 1,
|
||||
model: "test-model",
|
||||
agent: { workspace: tmpDir },
|
||||
sessionStore: storePath,
|
||||
channels: { telegram: {} },
|
||||
} as unknown as OpenClawConfig;
|
||||
const cfg = {
|
||||
version: 1,
|
||||
model: "test-model",
|
||||
agent: { workspace: tmpDir },
|
||||
sessionStore: storePath,
|
||||
channels: { telegram: {} },
|
||||
} as unknown as OpenClawConfig;
|
||||
|
||||
await runHeartbeatOnce({
|
||||
agentId: undefined,
|
||||
reason: "test",
|
||||
cfg,
|
||||
deps: { sendTelegram: vi.fn() },
|
||||
});
|
||||
await runHeartbeatOnce({
|
||||
agentId: undefined,
|
||||
reason: "test",
|
||||
cfg,
|
||||
deps: { sendTelegram: vi.fn() },
|
||||
});
|
||||
|
||||
const finalSize = (await fs.stat(transcriptPath)).size;
|
||||
if (params.expectPruned) {
|
||||
const finalContent = await fs.readFile(transcriptPath, "utf-8");
|
||||
expect(finalContent).toBe(originalContent);
|
||||
expect(finalSize).toBe(originalSize);
|
||||
return;
|
||||
}
|
||||
expect(finalSize).toBeGreaterThanOrEqual(originalSize);
|
||||
});
|
||||
const finalSize = (await fs.stat(transcriptPath)).size;
|
||||
if (params.expectPruned) {
|
||||
const finalContent = await fs.readFile(transcriptPath, "utf-8");
|
||||
expect(finalContent).toBe(originalContent);
|
||||
expect(finalSize).toBe(originalSize);
|
||||
return;
|
||||
}
|
||||
expect(finalSize).toBeGreaterThanOrEqual(originalSize);
|
||||
},
|
||||
{ prefix: "openclaw-hb-prune-" },
|
||||
);
|
||||
}
|
||||
|
||||
it("prunes transcript when heartbeat returns HEARTBEAT_OK", async () => {
|
||||
|
||||
Reference in New Issue
Block a user