Files
kandev/apps/web/components/task/session-context-summary.test.ts
2026-08-03 10:10:14 +01:00

95 lines
2.7 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { applySummarizeSessionResult } from "./session-context-summary";
describe("applySummarizeSessionResult", () => {
it("sanitizes unsafe characters before writing the prompt", () => {
const promptRef = { current: { value: "" } as HTMLTextAreaElement };
const setContextValue = vi.fn();
const setHasPrompt = vi.fn();
const toast = vi.fn();
applySummarizeSessionResult({
result: { summary: "line1\r\n<unsafe>\nline2" },
promptRef,
setContextValue,
setHasPrompt,
toast,
});
expect(promptRef.current.value).toBe("line1\n unsafe \nline2");
expect(setHasPrompt).toHaveBeenCalledWith(true);
expect(toast).not.toHaveBeenCalled();
});
it("treats an empty summary string as a successful summary", () => {
const promptRef = { current: { value: "existing" } as HTMLTextAreaElement };
const setContextValue = vi.fn();
const setHasPrompt = vi.fn();
const toast = vi.fn();
applySummarizeSessionResult({
result: { summary: "" },
promptRef,
setContextValue,
setHasPrompt,
toast,
});
expect(promptRef.current.value).toBe("");
expect(setHasPrompt).toHaveBeenCalledWith(true);
expect(toast).not.toHaveBeenCalled();
});
it("writes successful summaries through a shared composer handle", () => {
let value = "existing";
const promptRef = {
current: {
getValue: () => value,
setValue: (next: string) => {
value = next;
},
getAttachments: () => [],
},
};
const setContextValue = vi.fn();
const setHasPrompt = vi.fn();
const toast = vi.fn();
applySummarizeSessionResult({
result: { summary: "summary text" },
promptRef,
setContextValue,
setHasPrompt,
toast,
});
expect(value).toBe("summary text");
expect(setHasPrompt).toHaveBeenCalledWith(true);
expect(toast).not.toHaveBeenCalled();
});
it("resets context and prompt state when summary is null", () => {
const promptRef = { current: { value: "stale prompt" } as HTMLTextAreaElement };
const setContextValue = vi.fn();
const setHasPrompt = vi.fn();
const toast = vi.fn();
applySummarizeSessionResult({
result: { summary: null, error: "connection refused" },
promptRef,
setContextValue,
setHasPrompt,
toast,
});
expect(setContextValue).toHaveBeenCalledWith("blank");
expect(promptRef.current.value).toBe("");
expect(setHasPrompt).toHaveBeenCalledWith(false);
expect(toast).toHaveBeenCalledWith({
title: "Summarize failed",
description: "connection refused",
variant: "error",
});
});
});