-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseResponseActions.test.ts
More file actions
166 lines (136 loc) · 5.22 KB
/
useResponseActions.test.ts
File metadata and controls
166 lines (136 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// @vitest-environment jsdom
import { act, renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
const invokeMock = vi.fn();
vi.mock("@tauri-apps/api/core", () => ({
invoke: (command: string, payload?: Record<string, unknown>) =>
invokeMock(command, payload),
}));
import { useResponseActions } from "./useResponseActions";
type HookOptions = Parameters<typeof useResponseActions>[0];
const writeText = vi.fn().mockResolvedValue(undefined);
beforeEach(() => {
writeText.mockClear();
invokeMock.mockReset();
invokeMock.mockResolvedValue(true);
Object.defineProperty(navigator, "clipboard", {
value: { writeText },
configurable: true,
});
});
function makeOptions(overrides: Partial<HookOptions> = {}): HookOptions {
return {
response: "generated text",
originalResponse: "generated text",
isResponseEdited: false,
confidence: { mode: "answer" } as HookOptions["confidence"],
sources: [
{ chunk_id: "s1", title: "Source 1", snippet: "x", score: 1 },
] as unknown as HookOptions["sources"],
savedDraftId: null,
streamingText: "",
cancelGeneration: vi.fn(),
saveAsTemplate: vi.fn().mockResolvedValue("tpl-1"),
logEvent: vi.fn(),
setResponse: vi.fn(),
setOriginalResponse: vi.fn(),
setIsResponseEdited: vi.fn(),
setGenerating: vi.fn(),
setHandoffTouched: vi.fn(),
onShowSuccess: vi.fn(),
onShowError: vi.fn(),
...overrides,
};
}
describe("useResponseActions", () => {
it("copies the response directly when mode is answer and citations exist", async () => {
const options = makeOptions();
const { result } = renderHook(() => useResponseActions(options));
await act(async () => {
await result.current.handleCopyResponse();
});
expect(writeText).toHaveBeenCalledWith("generated text");
expect(invokeMock).not.toHaveBeenCalledWith(
"audit_response_copy_override",
expect.anything(),
);
expect(options.setHandoffTouched).toHaveBeenCalledWith(true);
expect(options.onShowSuccess).toHaveBeenCalledWith(
"Response copied to clipboard",
);
});
it("requires a reason when copy guard would otherwise block", async () => {
const promptSpy = vi
.spyOn(window, "prompt")
.mockReturnValue("ops needs this now");
const options = makeOptions({ sources: [] });
const { result } = renderHook(() => useResponseActions(options));
await act(async () => {
await result.current.handleCopyResponse();
});
expect(promptSpy).toHaveBeenCalled();
expect(invokeMock).toHaveBeenCalledWith("audit_response_copy_override", {
reason: "ops needs this now",
confidenceMode: "answer",
sourcesCount: 0,
});
expect(writeText).toHaveBeenCalled();
});
it("cancels and returns early when the user declines the override prompt", async () => {
vi.spyOn(window, "prompt").mockReturnValue("");
const options = makeOptions({ sources: [] });
const { result } = renderHook(() => useResponseActions(options));
await act(async () => {
await result.current.handleCopyResponse();
});
expect(writeText).not.toHaveBeenCalled();
expect(options.onShowError).toHaveBeenCalledWith(
"Copy cancelled (reason required).",
);
});
it("exports the response and marks handoff touched", async () => {
const options = makeOptions();
const { result } = renderHook(() => useResponseActions(options));
await act(async () => {
await result.current.handleExportResponse();
});
expect(invokeMock).toHaveBeenCalledWith("export_draft", {
responseText: "generated text",
format: "Markdown",
});
expect(options.setHandoffTouched).toHaveBeenCalledWith(true);
expect(options.onShowSuccess).toHaveBeenCalledWith(
"Response exported successfully",
);
});
it("keeps partial streaming text on cancel", async () => {
const options = makeOptions({ streamingText: "partial..." });
const { result } = renderHook(() => useResponseActions(options));
await act(async () => {
await result.current.handleCancel();
});
expect(options.cancelGeneration).toHaveBeenCalled();
expect(options.setGenerating).toHaveBeenCalledWith(false);
expect(options.setResponse).toHaveBeenCalledWith("partial...");
expect(options.setOriginalResponse).toHaveBeenCalledWith("partial...");
expect(options.setIsResponseEdited).toHaveBeenCalledWith(false);
});
it("flags edited when response changes from the original", () => {
const options = makeOptions({ originalResponse: "orig" });
const { result } = renderHook(() => useResponseActions(options));
act(() => {
result.current.handleResponseChange("new text");
});
expect(options.setResponse).toHaveBeenCalledWith("new text");
expect(options.setIsResponseEdited).toHaveBeenCalledWith(true);
});
it("opens the template modal and stores the rating when saveAsTemplate is called", () => {
const options = makeOptions();
const { result } = renderHook(() => useResponseActions(options));
act(() => {
result.current.handleSaveAsTemplate(4);
});
expect(result.current.showTemplateModal).toBe(true);
expect(result.current.templateModalRating).toBe(4);
});
});