-
-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathstreaming.node.spec.ts
More file actions
181 lines (153 loc) · 4.66 KB
/
Copy pathstreaming.node.spec.ts
File metadata and controls
181 lines (153 loc) · 4.66 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
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import "dotenv/config";
import { Essay } from "./essays";
import { StreamConfig } from "../src/Typesense/Configuration";
import { client, collection, model } from "./setup";
import { EventEmitter } from "events";
class MockReadableWithError extends EventEmitter {
pipe() {
return this;
}
on(event, handler) {
super.on(event, handler);
return this;
}
}
const axiosMock = vi.hoisted(() => {
const mockFn = vi.fn();
return {
mockFn,
request: vi.fn(),
isAxiosError: vi.fn(),
create: vi.fn(),
};
});
vi.mock("axios", () => {
return {
default: axiosMock.mockFn,
__esModule: true,
};
});
const runIntegrationTests = process.env.RUN_INTEGRATION_TESTS === "true";
describe.skipIf(!runIntegrationTests)("Streaming responses in Node.js", () => {
beforeEach(async () => {
vi.clearAllMocks();
const realAxios = (await vi.importActual("axios")) as { default: any };
axiosMock.mockFn.mockImplementation((config) => {
return realAxios.default(config);
});
});
afterEach(() => {
vi.resetAllMocks();
});
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 response = await client
.collections<Essay>(collection.name)
.documents()
.search({
q: "What is the maker schedule?",
query_by: "embedding",
conversation: true,
conversation_stream: true,
conversation_model_id: model.id,
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 response = await client.multiSearch.perform<[Essay]>(
{
searches: [
{
collection: collection.name,
include_fields: "title",
},
],
},
{
conversation_stream: true,
conversation: true,
conversation_model_id: model.id,
query_by: "embedding",
q: "What are the advantages and disadvantages of a startup being located in Silicon Valley?",
streamConfig: {
onChunk,
onComplete,
onError,
},
},
);
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,
};
const mockStream = new MockReadableWithError();
axiosMock.mockFn.mockResolvedValueOnce({
status: 200,
data: mockStream,
});
const requestPromise = client
.collections<Essay>(collection.name)
.documents()
.search({
q: "What is the maker schedule?",
query_by: "embedding",
conversation: true,
conversation_stream: true,
conversation_model_id: model.id,
include_fields: "title",
streamConfig,
});
// Add a small delay to ensure search processing has started
await new Promise((resolve) => setTimeout(resolve, 50));
mockStream.emit(
"data",
Buffer.from(
'data: {"conversation_id":"123","message":"This is test data"}\n\n',
),
);
await new Promise((resolve) => setTimeout(resolve, 50));
mockStream.emit("error", new Error("Stream error during processing"));
try {
await requestPromise;
// 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();
}
});
});