test: cover typing and history helpers
This commit is contained in:
@@ -5,7 +5,9 @@ import {
|
|||||||
buildHistoryContextFromEntries,
|
buildHistoryContextFromEntries,
|
||||||
buildHistoryContextFromMap,
|
buildHistoryContextFromMap,
|
||||||
buildPendingHistoryContextFromMap,
|
buildPendingHistoryContextFromMap,
|
||||||
|
clearHistoryEntriesIfEnabled,
|
||||||
HISTORY_CONTEXT_MARKER,
|
HISTORY_CONTEXT_MARKER,
|
||||||
|
recordPendingHistoryEntryIfEnabled,
|
||||||
} from "./history.js";
|
} from "./history.js";
|
||||||
import { CURRENT_MESSAGE_MARKER } from "./mentions.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_MESSAGE_MARKER);
|
||||||
expect(result).toContain("current");
|
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([]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
42
src/channels/typing.test.ts
Normal file
42
src/channels/typing.test.ts
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user