openclaw/src/auto-reply/reply/history.test.ts

109 lines
3.3 KiB
TypeScript
Raw Normal View History

2026-01-10 18:53:33 +01:00
import { describe, expect, it } from "vitest";
import {
appendHistoryEntry,
buildHistoryContext,
buildHistoryContextFromEntries,
buildHistoryContextFromMap,
buildPendingHistoryContextFromMap,
2026-01-10 18:58:21 +01:00
HISTORY_CONTEXT_MARKER,
2026-01-10 18:53:33 +01:00
} from "./history.js";
2026-01-10 18:58:21 +01:00
import { CURRENT_MESSAGE_MARKER } from "./mentions.js";
2026-01-10 18:53:33 +01:00
describe("history helpers", () => {
it("returns current message when history is empty", () => {
const result = buildHistoryContext({
historyText: " ",
currentMessage: "hello",
});
expect(result).toBe("hello");
});
it("wraps history entries and excludes current by default", () => {
const result = buildHistoryContextFromEntries({
entries: [
{ sender: "A", body: "one" },
{ sender: "B", body: "two" },
],
currentMessage: "current",
formatEntry: (entry) => `${entry.sender}: ${entry.body}`,
});
expect(result).toContain(HISTORY_CONTEXT_MARKER);
expect(result).toContain("A: one");
expect(result).not.toContain("B: two");
expect(result).toContain(CURRENT_MESSAGE_MARKER);
expect(result).toContain("current");
});
it("trims history to configured limit", () => {
const historyMap = new Map<string, { sender: string; body: string }[]>();
appendHistoryEntry({
historyMap,
2026-01-17 09:01:36 +00:00
historyKey: "group",
2026-01-10 18:53:33 +01:00
limit: 2,
entry: { sender: "A", body: "one" },
});
appendHistoryEntry({
historyMap,
2026-01-17 09:01:36 +00:00
historyKey: "group",
2026-01-10 18:53:33 +01:00
limit: 2,
entry: { sender: "B", body: "two" },
});
appendHistoryEntry({
historyMap,
2026-01-17 09:01:36 +00:00
historyKey: "group",
2026-01-10 18:53:33 +01:00
limit: 2,
entry: { sender: "C", body: "three" },
});
2026-01-17 09:01:36 +00:00
expect(historyMap.get("group")?.map((entry) => entry.body)).toEqual(["two", "three"]);
2026-01-10 18:53:33 +01:00
});
it("builds context from map and appends entry", () => {
const historyMap = new Map<string, { sender: string; body: string }[]>();
2026-01-17 09:01:36 +00:00
historyMap.set("group", [
{ sender: "A", body: "one" },
{ sender: "B", body: "two" },
]);
const result = buildHistoryContextFromMap({
historyMap,
2026-01-17 09:01:36 +00:00
historyKey: "group",
limit: 3,
entry: { sender: "C", body: "three" },
currentMessage: "current",
formatEntry: (entry) => `${entry.sender}: ${entry.body}`,
});
2026-01-17 09:01:36 +00:00
expect(historyMap.get("group")?.map((entry) => entry.body)).toEqual(["one", "two", "three"]);
expect(result).toContain(HISTORY_CONTEXT_MARKER);
expect(result).toContain("A: one");
expect(result).toContain("B: two");
expect(result).not.toContain("C: three");
});
it("builds context from pending map without appending", () => {
const historyMap = new Map<string, { sender: string; body: string }[]>();
2026-01-17 09:01:36 +00:00
historyMap.set("group", [
{ sender: "A", body: "one" },
{ sender: "B", body: "two" },
]);
const result = buildPendingHistoryContextFromMap({
historyMap,
2026-01-17 09:01:36 +00:00
historyKey: "group",
limit: 3,
currentMessage: "current",
formatEntry: (entry) => `${entry.sender}: ${entry.body}`,
});
2026-01-17 09:01:36 +00:00
expect(historyMap.get("group")?.map((entry) => entry.body)).toEqual(["one", "two"]);
expect(result).toContain(HISTORY_CONTEXT_MARKER);
expect(result).toContain("A: one");
expect(result).toContain("B: two");
expect(result).toContain(CURRENT_MESSAGE_MARKER);
expect(result).toContain("current");
});
2026-01-10 18:53:33 +01:00
});