test(heartbeat): dedupe sandbox/session helpers and collapse ack cases

This commit is contained in:
Peter Steinberger
2026-02-22 09:20:19 +00:00
parent b4cdffc7a4
commit 4520fdda69
3 changed files with 108 additions and 120 deletions

View File

@@ -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 () => {