test: cover typing and history helpers

This commit is contained in:
Peter Steinberger
2026-01-23 23:22:08 +00:00
parent aeb6b2ffad
commit c9a7c77b24
2 changed files with 86 additions and 0 deletions

View File

@@ -5,7 +5,9 @@ import {
buildHistoryContextFromEntries,
buildHistoryContextFromMap,
buildPendingHistoryContextFromMap,
clearHistoryEntriesIfEnabled,
HISTORY_CONTEXT_MARKER,
recordPendingHistoryEntryIfEnabled,
} from "./history.js";
import { CURRENT_MESSAGE_MARKER } from "./mentions.js";
@@ -105,4 +107,46 @@ describe("history helpers", () => {
expect(result).toContain(CURRENT_MESSAGE_MARKER);
expect(result).toContain("current");
});
it("records pending entries only when enabled", () => {
const historyMap = new Map<string, { sender: string; body: string }[]>();
recordPendingHistoryEntryIfEnabled({
historyMap,
historyKey: "group",
limit: 0,
entry: { sender: "A", body: "one" },
});
expect(historyMap.get("group")).toEqual(undefined);
recordPendingHistoryEntryIfEnabled({
historyMap,
historyKey: "group",
limit: 2,
entry: null,
});
expect(historyMap.get("group")).toEqual(undefined);
recordPendingHistoryEntryIfEnabled({
historyMap,
historyKey: "group",
limit: 2,
entry: { sender: "B", body: "two" },
});
expect(historyMap.get("group")?.map((entry) => entry.body)).toEqual(["two"]);
});
it("clears history entries only when enabled", () => {
const historyMap = new Map<string, { sender: string; body: string }[]>();
historyMap.set("group", [
{ sender: "A", body: "one" },
{ sender: "B", body: "two" },
]);
clearHistoryEntriesIfEnabled({ historyMap, historyKey: "group", limit: 0 });
expect(historyMap.get("group")?.map((entry) => entry.body)).toEqual(["one", "two"]);
clearHistoryEntriesIfEnabled({ historyMap, historyKey: "group", limit: 2 });
expect(historyMap.get("group")).toEqual([]);
});
});

View File

@@ -0,0 +1,42 @@
import { describe, expect, it, vi } from "vitest";
import { createTypingCallbacks } from "./typing.js";
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
describe("createTypingCallbacks", () => {
it("invokes start on reply start", async () => {
const start = vi.fn().mockResolvedValue(undefined);
const onStartError = vi.fn();
const callbacks = createTypingCallbacks({ start, onStartError });
await callbacks.onReplyStart();
expect(start).toHaveBeenCalledTimes(1);
expect(onStartError).not.toHaveBeenCalled();
});
it("reports start errors", async () => {
const start = vi.fn().mockRejectedValue(new Error("fail"));
const onStartError = vi.fn();
const callbacks = createTypingCallbacks({ start, onStartError });
await callbacks.onReplyStart();
expect(onStartError).toHaveBeenCalledTimes(1);
});
it("invokes stop on idle and reports stop errors", async () => {
const start = vi.fn().mockResolvedValue(undefined);
const stop = vi.fn().mockRejectedValue(new Error("stop"));
const onStartError = vi.fn();
const onStopError = vi.fn();
const callbacks = createTypingCallbacks({ start, stop, onStartError, onStopError });
callbacks.onIdle?.();
await flush();
expect(stop).toHaveBeenCalledTimes(1);
expect(onStopError).toHaveBeenCalledTimes(1);
});
});