-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.test.ts
More file actions
299 lines (280 loc) · 9.11 KB
/
runtime.test.ts
File metadata and controls
299 lines (280 loc) · 9.11 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@actions/core", () => ({
debug: vi.fn(),
info: vi.fn(),
setSecret: vi.fn(),
warning: vi.fn(),
}));
vi.mock("@spiceai/spice", () => ({
SpiceClient: vi.fn(),
}));
import type { SdkLike } from "../src/runtime.js";
import { RuntimeClient } from "../src/runtime.js";
function makeSdk(overrides: Partial<SdkLike> = {}): SdkLike {
return {
isSpiceReady: vi.fn().mockResolvedValue(true),
sqlJson: vi.fn().mockResolvedValue({ row_count: 0, data: [] }),
nsql: vi.fn().mockResolvedValue({ row_count: 0, data: [], sql: "SELECT 1" }),
...overrides,
};
}
function makeClock() {
let now = 0;
return {
now: () => now,
sleep: async (ms: number) => {
now += ms;
},
advance(ms: number) {
now += ms;
},
};
}
describe("RuntimeClient", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("returns immediately when warmup is 0", async () => {
const sdk = makeSdk();
const rt = new RuntimeClient({
apiKey: "k",
baseUrl: "https://data.spiceai.io",
warmupSeconds: 0,
timeoutSeconds: 5,
sdkFactory: () => sdk,
});
await rt.waitForReady();
expect(sdk.isSpiceReady).not.toHaveBeenCalled();
});
it("polls isSpiceReady until ready", async () => {
const isReady = vi
.fn()
.mockResolvedValueOnce(false)
.mockResolvedValueOnce(false)
.mockResolvedValue(true);
const sdk = makeSdk({ isSpiceReady: isReady });
const clock = makeClock();
const rt = new RuntimeClient({
apiKey: "k",
baseUrl: "https://data.spiceai.io",
warmupSeconds: 30,
timeoutSeconds: 5,
sdkFactory: () => sdk,
clock,
});
await rt.waitForReady();
expect(isReady).toHaveBeenCalledTimes(3);
});
it("throws when warmup deadline elapses", async () => {
const sdk = makeSdk({ isSpiceReady: vi.fn().mockResolvedValue(false) });
const clock = makeClock();
const rt = new RuntimeClient({
apiKey: "k",
baseUrl: "https://data.spiceai.io",
warmupSeconds: 5,
timeoutSeconds: 5,
sdkFactory: () => sdk,
clock,
});
await expect(rt.waitForReady()).rejects.toThrow(/Runtime not ready/);
});
it("probeSql succeeds and returns row count detail", async () => {
const sqlJson = vi.fn().mockResolvedValue({ row_count: 3, execution_time_ms: 17 });
const sdk = makeSdk({ sqlJson });
const rt = new RuntimeClient({
apiKey: "k",
baseUrl: "https://data.spiceai.io",
warmupSeconds: 0,
timeoutSeconds: 5,
sdkFactory: () => sdk,
});
const result = await rt.probeSql("SELECT 1");
expect(result.ok).toBe(true);
expect(result.detail).toContain("3 row(s)");
expect(sqlJson).toHaveBeenCalledWith("SELECT 1");
});
it("probeSql captures errors", async () => {
const sdk = makeSdk({ sqlJson: vi.fn().mockRejectedValue(new Error("boom")) });
const rt = new RuntimeClient({
apiKey: "k",
baseUrl: "https://data.spiceai.io",
warmupSeconds: 0,
timeoutSeconds: 5,
sdkFactory: () => sdk,
});
const result = await rt.probeSql("SELECT 1");
expect(result.ok).toBe(false);
expect(result.error).toContain("boom");
});
it("probeNsql passes model option through", async () => {
const nsql = vi.fn().mockResolvedValue({ row_count: 1, sql: "SELECT now()" });
const sdk = makeSdk({ nsql });
const rt = new RuntimeClient({
apiKey: "k",
baseUrl: "https://data.spiceai.io",
warmupSeconds: 0,
timeoutSeconds: 5,
sdkFactory: () => sdk,
});
const result = await rt.probeNsql("rows please", "nql");
expect(nsql).toHaveBeenCalledWith("rows please", { model: "nql" });
expect(result.ok).toBe(true);
expect(result.detail).toContain("SELECT now()");
});
it("probeChat issues a POST to /v1/chat/completions with bearer auth", async () => {
const fetchImpl = vi.fn().mockResolvedValue(
new Response(
JSON.stringify({
choices: [{ message: { content: "hello world" } }],
usage: { total_tokens: 7 },
}),
{ status: 200, headers: { "content-type": "application/json" } },
),
);
const rt = new RuntimeClient({
apiKey: "k",
baseUrl: "https://data.spiceai.io",
warmupSeconds: 0,
timeoutSeconds: 5,
sdkFactory: () => makeSdk(),
fetchImpl,
});
const result = await rt.probeChat({ messages: [{ role: "user", content: "hi" }] });
expect(result.ok).toBe(true);
expect(result.detail).toContain("7 tokens");
expect(result.detail).toContain("hello world");
const [url, init] = fetchImpl.mock.calls[0]!;
expect(url).toBe("https://data.spiceai.io/v1/chat/completions");
expect((init.headers as Record<string, string>).Authorization).toBe("Bearer k");
});
it("getDatasets requests /v1/datasets?status=true with x-api-key", async () => {
const fetchImpl = vi.fn().mockResolvedValue(
new Response(JSON.stringify([{ name: "a", status: "Ready" }]), {
status: 200,
headers: { "content-type": "application/json" },
}),
);
const rt = new RuntimeClient({
apiKey: "k",
baseUrl: "https://us-west-2-prod-aws-data.spiceai.io",
warmupSeconds: 0,
timeoutSeconds: 5,
sdkFactory: () => makeSdk(),
fetchImpl,
});
const result = await rt.getDatasets();
expect(result).toEqual([{ name: "a", status: "Ready" }]);
const [url, init] = fetchImpl.mock.calls[0]!;
expect(url).toBe("https://us-west-2-prod-aws-data.spiceai.io/v1/datasets?status=true");
expect(init.method).toBe("GET");
expect((init.headers as Record<string, string>)["x-api-key"]).toBe("k");
});
it("waitForDatasetsReady returns once all datasets are ready", async () => {
const fetchImpl = vi
.fn()
.mockResolvedValueOnce(
new Response(
JSON.stringify([
{ name: "a", status: "Initializing" },
{ name: "b", status: "Ready" },
]),
{ status: 200, headers: { "content-type": "application/json" } },
),
)
.mockResolvedValue(
new Response(
JSON.stringify([
{ name: "a", status: "Ready" },
{ name: "b", status: "Ready" },
]),
{ status: 200, headers: { "content-type": "application/json" } },
),
);
const clock = makeClock();
const rt = new RuntimeClient({
apiKey: "k",
baseUrl: "https://x.example",
warmupSeconds: 0,
timeoutSeconds: 5,
sdkFactory: () => makeSdk(),
fetchImpl,
clock,
});
const datasets = await rt.waitForDatasetsReady(60);
expect(datasets.every((d) => d.status === "Ready")).toBe(true);
expect(fetchImpl).toHaveBeenCalledTimes(2);
});
it("waitForDatasetsReady throws DatasetReadinessError on error state", async () => {
const fetchImpl = vi
.fn()
.mockResolvedValue(
new Response(
JSON.stringify([{ name: "a", status: "Error", error_message: "auth failed" }]),
{ status: 200, headers: { "content-type": "application/json" } },
),
);
const rt = new RuntimeClient({
apiKey: "k",
baseUrl: "https://x.example",
warmupSeconds: 0,
timeoutSeconds: 5,
sdkFactory: () => makeSdk(),
fetchImpl,
});
await expect(rt.waitForDatasetsReady(60)).rejects.toMatchObject({
name: "DatasetReadinessError",
message: expect.stringContaining("auth failed"),
});
});
it("waitForDatasetsReady throws on timeout while still initializing", async () => {
const fetchImpl = vi.fn().mockResolvedValue(
new Response(JSON.stringify([{ name: "a", status: "Initializing" }]), {
status: 200,
headers: { "content-type": "application/json" },
}),
);
const clock = makeClock();
const rt = new RuntimeClient({
apiKey: "k",
baseUrl: "https://x.example",
warmupSeconds: 0,
timeoutSeconds: 5,
sdkFactory: () => makeSdk(),
fetchImpl,
clock,
});
await expect(rt.waitForDatasetsReady(5)).rejects.toThrow(/did not finish loading/);
});
it("waitForDatasetsReady is a no-op when timeout is 0", async () => {
const fetchImpl = vi.fn();
const rt = new RuntimeClient({
apiKey: "k",
baseUrl: "https://x.example",
warmupSeconds: 0,
timeoutSeconds: 5,
sdkFactory: () => makeSdk(),
fetchImpl,
});
expect(await rt.waitForDatasetsReady(0)).toEqual([]);
expect(fetchImpl).not.toHaveBeenCalled();
});
it("probeSearch reports failures with body context", async () => {
const fetchImpl = vi
.fn()
.mockResolvedValue(
new Response("upstream timeout", { status: 504, statusText: "Gateway Timeout" }),
);
const rt = new RuntimeClient({
apiKey: "k",
baseUrl: "https://data.spiceai.io",
warmupSeconds: 0,
timeoutSeconds: 5,
sdkFactory: () => makeSdk(),
fetchImpl,
});
const result = await rt.probeSearch({ datasets: ["x"], text: "y" });
expect(result.ok).toBe(false);
expect(result.error).toContain("504");
expect(result.error).toContain("upstream timeout");
});
});