-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathstreaming.node.mock.spec.ts
More file actions
205 lines (178 loc) · 5.55 KB
/
streaming.node.mock.spec.ts
File metadata and controls
205 lines (178 loc) · 5.55 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
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { Readable } from "stream";
import MockAdapter from "axios-mock-adapter";
import axios from "axios";
import { client } from "./setup";
import { StreamConfig } from "../src/Typesense/Configuration";
import { Essay } from "./essays";
import { MultiSearchResultsStreamConfig } from "../src/Typesense/Types";
describe("Streaming responses with axios-mock-adapter", () => {
let mock: MockAdapter;
beforeEach(() => {
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.reset();
});
it("should handle streaming responses for search", async () => {
const onChunk = vi.fn();
const onComplete = vi.fn();
const onError = vi.fn();
const streamConfig: StreamConfig<Essay> = {
onChunk,
onComplete,
onError,
};
const chunks = [
'{"conversation_id":"123","message":"First chunk"}',
'{"conversation_id":"123","message":"Second chunk"}',
'{"conversation_id":"123","message":"Final chunk"}',
];
const metadata = {
found: 3,
hits: [
{ document: { title: "Test 1" } },
{ document: { title: "Test 2" } },
{ document: { title: "Test 3" } },
],
page: 1,
search_time_ms: 123,
};
mock.onAny().reply(() => {
const stream = new Readable({
read() {
chunks.forEach((chunk) => {
this.push(`data: ${chunk}\n\n`);
});
this.push(JSON.stringify(metadata));
this.push(null);
},
});
return [200, stream, { "content-type": "text/event-stream" }];
});
const response = await client
.collections<Essay>("test-collection")
.documents()
.search({
q: "What is the maker schedule?",
query_by: "embedding",
conversation: true,
conversation_stream: true,
conversation_model_id: "test-model",
include_fields: "title",
streamConfig,
});
expect(onChunk.mock.calls.length).toBeGreaterThan(1);
expect(onComplete).toHaveBeenCalledOnce();
expect(onError).not.toHaveBeenCalled();
expect(response).toBeDefined();
expect(response.hits?.length).toBeGreaterThan(0);
});
it("should handle streaming responses for multisearch", async () => {
const onChunk = vi.fn();
const onComplete = vi.fn();
const onError = vi.fn();
const streamConfig: MultiSearchResultsStreamConfig<[Essay]> = {
onChunk,
onComplete,
onError,
};
const chunks = [
'{"conversation_id":"123","message":"First chunk"}',
'{"conversation_id":"123","message":"Second chunk"}',
'{"conversation_id":"123","message":"Final chunk"}',
];
const metadata = {
results: [
{
found: 3,
hits: [
{ document: { title: "Test 1" } },
{ document: { title: "Test 2" } },
{ document: { title: "Test 3" } },
],
page: 1,
search_time_ms: 123,
},
],
};
mock.onAny().reply(() => {
const stream = new Readable({
read() {
chunks.forEach((chunk) => {
this.push(`data: ${chunk}\n\n`);
});
this.push(JSON.stringify(metadata));
this.push(null);
},
});
return [200, stream, { "content-type": "text/event-stream" }];
});
const response = await client.multiSearch.perform<[Essay]>(
{
searches: [
{
collection: "test-collection",
include_fields: "title",
},
],
},
{
conversation_stream: true,
conversation: true,
conversation_model_id: "test-model",
query_by: "embedding",
q: "What are the advantages and disadvantages of a startup being located in Silicon Valley?",
streamConfig,
},
);
expect(onChunk.mock.calls.length).toBeGreaterThan(1);
expect(onComplete).toHaveBeenCalledOnce();
expect(onError).not.toHaveBeenCalled();
expect(response).toBeDefined();
expect(response.results[0].hits?.length).toBeGreaterThan(0);
});
it("should invoke onError callback when an error occurs during stream processing", async () => {
const onChunk = vi.fn();
const onComplete = vi.fn();
const onError = vi.fn();
const streamConfig: StreamConfig<Essay> = {
onChunk,
onComplete,
onError,
};
mock.onAny().reply(() => {
const stream = new Readable({
read() {
this.push(
'data: {"conversation_id":"123","message":"Error chunk"}\n\n',
);
this.destroy(new Error("Stream error during processing"));
},
});
return [200, stream, { "content-type": "text/event-stream" }];
});
try {
await client.collections<Essay>("test-collection").documents().search({
q: "What is the maker schedule?",
query_by: "embedding",
conversation: true,
conversation_stream: true,
conversation_model_id: "test-model",
include_fields: "title",
streamConfig,
});
// If it doesn't fail, that's a problem
expect(true).toBe(false);
} catch (error) {
expect(onChunk).toHaveBeenCalled();
expect(onChunk.mock.calls[0][0]).toHaveProperty("conversation_id", "123");
expect(onError).toHaveBeenCalledTimes(1);
expect(onError.mock.calls[0][0]).toBeInstanceOf(Error);
expect(onError.mock.calls[0][0].message).toBe(
"Stream error during processing",
);
expect(onComplete).not.toHaveBeenCalled();
}
});
});