-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathuseChat.attachments.test.ts
More file actions
244 lines (225 loc) · 6.75 KB
/
useChat.attachments.test.ts
File metadata and controls
244 lines (225 loc) · 6.75 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { act, renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { useAgentStore } from "@/features/agents/stores/agentStore";
import { useChatStore } from "../../stores/chatStore";
import { useChatSessionStore } from "../../stores/chatSessionStore";
const mockAcpSendMessage = vi.fn();
const mockAcpCancelSession = vi.fn();
const mockAcpPrepareSession = vi.fn();
const mockAcpSetModel = vi.fn();
vi.mock("@/shared/api/acp", () => ({
acpSendMessage: (...args: unknown[]) => mockAcpSendMessage(...args),
acpCancelSession: (...args: unknown[]) => mockAcpCancelSession(...args),
acpPrepareSession: (...args: unknown[]) => mockAcpPrepareSession(...args),
acpSetModel: (...args: unknown[]) => mockAcpSetModel(...args),
}));
import { useChat } from "../useChat";
describe("useChat attachments", () => {
beforeEach(() => {
vi.clearAllMocks();
useChatStore.setState({
messagesBySession: {},
sessionStateById: {},
activeSessionId: null,
isConnected: true,
});
useChatSessionStore.setState({
sessions: [],
activeSessionId: null,
isLoading: false,
contextPanelOpenBySession: {},
activeWorkspaceBySession: {},
modelsBySession: {},
modelCacheByProvider: {},
});
useAgentStore.setState({
personas: [],
personasLoading: false,
agents: [],
agentsLoading: false,
activeAgentId: null,
isLoading: false,
personaEditorOpen: false,
editingPersona: null,
});
mockAcpCancelSession.mockResolvedValue(true);
mockAcpPrepareSession.mockResolvedValue(undefined);
mockAcpSetModel.mockResolvedValue(undefined);
});
it("stores non-image attachments in metadata and prepends path references to the prompt", async () => {
const { result } = renderHook(() => useChat("session-1"));
const attachments = [
{
id: "file-1",
kind: "file" as const,
name: "report.pdf",
path: "/tmp/report.pdf",
mimeType: "application/pdf",
},
{
id: "dir-1",
kind: "directory" as const,
name: "screenshots",
path: "/tmp/screenshots",
},
];
await act(async () => {
await result.current.sendMessage(
"Please review these",
undefined,
attachments,
);
});
const message = useChatStore.getState().messagesBySession["session-1"][0];
expect(message.metadata?.attachments).toEqual([
{
type: "file",
name: "report.pdf",
path: "/tmp/report.pdf",
mimeType: "application/pdf",
},
{
type: "directory",
name: "screenshots",
path: "/tmp/screenshots",
},
]);
expect(mockAcpSendMessage).toHaveBeenCalledWith(
"session-1",
"Attached items:\n- [file] /tmp/report.pdf\n- [directory] /tmp/screenshots\nPlease review these",
{
systemPrompt: undefined,
personaId: undefined,
personaName: undefined,
images: undefined,
},
);
});
it("keeps image attachments in ACP images while preserving path metadata", async () => {
const { result } = renderHook(() => useChat("session-1"));
const attachments = [
{
id: "image-1",
kind: "image" as const,
name: "diagram.png",
path: "/tmp/diagram.png",
mimeType: "image/png",
base64: "abc123",
previewUrl: "tauri://localhost/tmp/diagram.png",
},
];
await act(async () => {
await result.current.sendMessage("", undefined, attachments);
});
const message = useChatStore.getState().messagesBySession["session-1"][0];
expect(message.metadata?.attachments).toEqual([
{
type: "file",
name: "diagram.png",
path: "/tmp/diagram.png",
mimeType: "image/png",
},
]);
expect(message.content).toEqual([
{ type: "text", text: "" },
{
type: "image",
source: {
type: "base64",
mediaType: "image/png",
data: "abc123",
},
},
]);
expect(mockAcpSendMessage).toHaveBeenCalledWith(
"session-1",
"Attached items:\n- [image] diagram.png (image attached)\n ",
{
systemPrompt: undefined,
personaId: undefined,
personaName: undefined,
images: [["abc123", "image/png"]],
},
);
});
it("includes image attachments in the prompt summary for mixed sends", async () => {
const { result } = renderHook(() => useChat("session-1"));
const attachments = [
{
id: "file-1",
kind: "file" as const,
name: "mobile-confirmation.html",
path: "/tmp/mobile-confirmation.html",
mimeType: "text/html",
},
{
id: "dir-1",
kind: "directory" as const,
name: "neighborhood block",
path: "/tmp/neighborhood block",
},
{
id: "image-1",
kind: "image" as const,
name: "Screenshot 2026-04-09 at 1.25.32 PM.png",
path: "/tmp/Screenshot.png",
mimeType: "image/png",
base64: "abc123",
previewUrl: "tauri://localhost/tmp/Screenshot.png",
},
];
await act(async () => {
await result.current.sendMessage(
"can you see the attachments i attached?",
undefined,
attachments,
);
});
expect(mockAcpSendMessage).toHaveBeenCalledWith(
"session-1",
"Attached items:\n- [file] /tmp/mobile-confirmation.html\n- [directory] /tmp/neighborhood block\n- [image] Screenshot 2026-04-09 at 1.25.32 PM.png (image attached)\ncan you see the attachments i attached?",
{
systemPrompt: undefined,
personaId: undefined,
personaName: undefined,
images: [["abc123", "image/png"]],
},
);
});
it("preserves pathless browser file attachments in sent message metadata", async () => {
const { result } = renderHook(() => useChat("session-1"));
const attachments = [
{
id: "file-1",
kind: "file" as const,
name: "report.pdf",
mimeType: "application/pdf",
},
];
await act(async () => {
await result.current.sendMessage(
"Please review this",
undefined,
attachments,
);
});
const message = useChatStore.getState().messagesBySession["session-1"][0];
expect(message.metadata?.attachments).toEqual([
{
type: "file",
name: "report.pdf",
mimeType: "application/pdf",
},
]);
expect(mockAcpSendMessage).toHaveBeenCalledWith(
"session-1",
"Attached items:\n- [file] report.pdf\nPlease review this",
{
systemPrompt: undefined,
personaId: undefined,
personaName: undefined,
images: undefined,
},
);
});
});