refactor(test): dedupe channel and monitor action suites

This commit is contained in:
Peter Steinberger
2026-02-18 04:48:51 +00:00
parent 31f83c86b2
commit a69e7682c1
11 changed files with 506 additions and 789 deletions

View File

@@ -50,31 +50,34 @@ describe("heartbeat transcript pruning", () => {
});
}
it("prunes transcript when heartbeat returns HEARTBEAT_OK", async () => {
async function runTranscriptScenario(params: {
sessionId: string;
reply: {
text: string;
usage: {
inputTokens: number;
outputTokens: number;
cacheReadTokens: number;
cacheWriteTokens: number;
};
};
expectPruned: boolean;
}) {
await withTempTelegramHeartbeatSandbox(async ({ tmpDir, storePath, replySpy }) => {
const sessionKey = resolveMainSessionKey(undefined);
const sessionId = "test-session-prune";
const transcriptPath = path.join(tmpDir, `${sessionId}.jsonl`);
// Create a transcript with some existing content
const originalContent = await createTranscriptWithContent(transcriptPath, sessionId);
const transcriptPath = path.join(tmpDir, `${params.sessionId}.jsonl`);
const originalContent = await createTranscriptWithContent(transcriptPath, params.sessionId);
const originalSize = (await fs.stat(transcriptPath)).size;
// Seed session store
await seedSessionStore(storePath, sessionKey, {
sessionId,
sessionId: params.sessionId,
lastChannel: "telegram",
lastProvider: "telegram",
lastTo: "user123",
});
// Mock reply to return HEARTBEAT_OK (which triggers pruning)
replySpy.mockResolvedValueOnce({
text: "HEARTBEAT_OK",
usage: { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0 },
});
replySpy.mockResolvedValueOnce(params.reply);
// Run heartbeat
const cfg = {
version: 1,
model: "test-model",
@@ -90,57 +93,36 @@ describe("heartbeat transcript pruning", () => {
deps: { sendTelegram: vi.fn() },
});
// Verify transcript was truncated back to original size
const finalContent = await fs.readFile(transcriptPath, "utf-8");
expect(finalContent).toBe(originalContent);
const finalSize = (await fs.stat(transcriptPath)).size;
expect(finalSize).toBe(originalSize);
if (params.expectPruned) {
const finalContent = await fs.readFile(transcriptPath, "utf-8");
expect(finalContent).toBe(originalContent);
expect(finalSize).toBe(originalSize);
return;
}
expect(finalSize).toBeGreaterThanOrEqual(originalSize);
});
}
it("prunes transcript when heartbeat returns HEARTBEAT_OK", async () => {
await runTranscriptScenario({
sessionId: "test-session-prune",
reply: {
text: "HEARTBEAT_OK",
usage: { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0 },
},
expectPruned: true,
});
});
it("does not prune transcript when heartbeat returns meaningful content", async () => {
await withTempTelegramHeartbeatSandbox(async ({ tmpDir, storePath, replySpy }) => {
const sessionKey = resolveMainSessionKey(undefined);
const sessionId = "test-session-no-prune";
const transcriptPath = path.join(tmpDir, `${sessionId}.jsonl`);
// Create a transcript with some existing content
await createTranscriptWithContent(transcriptPath, sessionId);
const originalSize = (await fs.stat(transcriptPath)).size;
// Seed session store
await seedSessionStore(storePath, sessionKey, {
sessionId,
lastChannel: "telegram",
lastProvider: "telegram",
lastTo: "user123",
});
// Mock reply to return meaningful content (should NOT trigger pruning)
replySpy.mockResolvedValueOnce({
await runTranscriptScenario({
sessionId: "test-session-no-prune",
reply: {
text: "Alert: Something needs your attention!",
usage: { inputTokens: 10, outputTokens: 20, cacheReadTokens: 0, cacheWriteTokens: 0 },
});
// Run heartbeat
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() },
});
// Verify transcript was NOT truncated (it may have grown with new entries)
const finalSize = (await fs.stat(transcriptPath)).size;
expect(finalSize).toBeGreaterThanOrEqual(originalSize);
},
expectPruned: false,
});
});
});