|
1 | 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 2 |
|
3 | 3 | vi.mock("@actions/core", () => ({ |
| 4 | + debug: vi.fn(), |
4 | 5 | info: vi.fn(), |
5 | 6 | setSecret: vi.fn(), |
6 | 7 | warning: vi.fn(), |
@@ -160,6 +161,171 @@ describe("RuntimeClient", () => { |
160 | 161 | expect((init.headers as Record<string, string>).Authorization).toBe("Bearer k"); |
161 | 162 | }); |
162 | 163 |
|
| 164 | + it("getDatasets requests /v1/datasets?status=true with x-api-key", async () => { |
| 165 | + const fetchImpl = vi.fn().mockResolvedValue( |
| 166 | + new Response(JSON.stringify([{ name: "a", status: "Ready" }]), { |
| 167 | + status: 200, |
| 168 | + headers: { "content-type": "application/json" }, |
| 169 | + }), |
| 170 | + ); |
| 171 | + const rt = new RuntimeClient({ |
| 172 | + apiKey: "k", |
| 173 | + baseUrl: "https://us-west-2-prod-aws-data.spiceai.io", |
| 174 | + warmupSeconds: 0, |
| 175 | + timeoutSeconds: 5, |
| 176 | + sdkFactory: () => makeSdk(), |
| 177 | + fetchImpl, |
| 178 | + }); |
| 179 | + const result = await rt.getDatasets(); |
| 180 | + expect(result).toEqual([{ name: "a", status: "Ready" }]); |
| 181 | + const [url, init] = fetchImpl.mock.calls[0]!; |
| 182 | + expect(url).toBe("https://us-west-2-prod-aws-data.spiceai.io/v1/datasets?status=true"); |
| 183 | + expect(init.method).toBe("GET"); |
| 184 | + expect((init.headers as Record<string, string>)["x-api-key"]).toBe("k"); |
| 185 | + }); |
| 186 | + |
| 187 | + it("waitForDatasetsReady returns once all datasets are ready", async () => { |
| 188 | + const fetchImpl = vi |
| 189 | + .fn() |
| 190 | + .mockResolvedValueOnce( |
| 191 | + new Response( |
| 192 | + JSON.stringify([ |
| 193 | + { name: "a", status: "Initializing" }, |
| 194 | + { name: "b", status: "Ready" }, |
| 195 | + ]), |
| 196 | + { status: 200, headers: { "content-type": "application/json" } }, |
| 197 | + ), |
| 198 | + ) |
| 199 | + .mockResolvedValue( |
| 200 | + new Response( |
| 201 | + JSON.stringify([ |
| 202 | + { name: "a", status: "Ready" }, |
| 203 | + { name: "b", status: "Ready" }, |
| 204 | + ]), |
| 205 | + { status: 200, headers: { "content-type": "application/json" } }, |
| 206 | + ), |
| 207 | + ); |
| 208 | + const clock = makeClock(); |
| 209 | + const rt = new RuntimeClient({ |
| 210 | + apiKey: "k", |
| 211 | + baseUrl: "https://x.example", |
| 212 | + warmupSeconds: 0, |
| 213 | + timeoutSeconds: 5, |
| 214 | + sdkFactory: () => makeSdk(), |
| 215 | + fetchImpl, |
| 216 | + clock, |
| 217 | + }); |
| 218 | + const datasets = await rt.waitForDatasetsReady(60); |
| 219 | + expect(datasets.every((d) => d.status === "Ready")).toBe(true); |
| 220 | + expect(fetchImpl).toHaveBeenCalledTimes(2); |
| 221 | + }); |
| 222 | + |
| 223 | + it("waitForDatasetsReady throws DatasetReadinessError on error state", async () => { |
| 224 | + const fetchImpl = vi |
| 225 | + .fn() |
| 226 | + .mockResolvedValue( |
| 227 | + new Response( |
| 228 | + JSON.stringify([{ name: "a", status: "Error", error_message: "auth failed" }]), |
| 229 | + { status: 200, headers: { "content-type": "application/json" } }, |
| 230 | + ), |
| 231 | + ); |
| 232 | + const rt = new RuntimeClient({ |
| 233 | + apiKey: "k", |
| 234 | + baseUrl: "https://x.example", |
| 235 | + warmupSeconds: 0, |
| 236 | + timeoutSeconds: 5, |
| 237 | + sdkFactory: () => makeSdk(), |
| 238 | + fetchImpl, |
| 239 | + }); |
| 240 | + await expect(rt.waitForDatasetsReady(60)).rejects.toMatchObject({ |
| 241 | + name: "DatasetReadinessError", |
| 242 | + message: expect.stringContaining("auth failed"), |
| 243 | + }); |
| 244 | + }); |
| 245 | + |
| 246 | + it("waitForDatasetsReady throws on timeout while still initializing", async () => { |
| 247 | + const fetchImpl = vi.fn().mockResolvedValue( |
| 248 | + new Response(JSON.stringify([{ name: "a", status: "Initializing" }]), { |
| 249 | + status: 200, |
| 250 | + headers: { "content-type": "application/json" }, |
| 251 | + }), |
| 252 | + ); |
| 253 | + const clock = makeClock(); |
| 254 | + const rt = new RuntimeClient({ |
| 255 | + apiKey: "k", |
| 256 | + baseUrl: "https://x.example", |
| 257 | + warmupSeconds: 0, |
| 258 | + timeoutSeconds: 5, |
| 259 | + sdkFactory: () => makeSdk(), |
| 260 | + fetchImpl, |
| 261 | + clock, |
| 262 | + }); |
| 263 | + await expect(rt.waitForDatasetsReady(5)).rejects.toThrow(/did not finish loading/); |
| 264 | + }); |
| 265 | + |
| 266 | + it("waitForDatasetsReady accepts disabled and refreshing as terminal-ok", async () => { |
| 267 | + const fetchImpl = vi.fn().mockResolvedValue( |
| 268 | + new Response( |
| 269 | + JSON.stringify([ |
| 270 | + { name: "a", status: "Ready" }, |
| 271 | + { name: "b", status: "Disabled" }, |
| 272 | + { name: "c", status: "Refreshing" }, |
| 273 | + ]), |
| 274 | + { status: 200, headers: { "content-type": "application/json" } }, |
| 275 | + ), |
| 276 | + ); |
| 277 | + const rt = new RuntimeClient({ |
| 278 | + apiKey: "k", |
| 279 | + baseUrl: "https://x.example", |
| 280 | + warmupSeconds: 0, |
| 281 | + timeoutSeconds: 5, |
| 282 | + sdkFactory: () => makeSdk(), |
| 283 | + fetchImpl, |
| 284 | + }); |
| 285 | + const datasets = await rt.waitForDatasetsReady(60); |
| 286 | + expect(datasets.map((d) => d.name)).toEqual(["a", "b", "c"]); |
| 287 | + }); |
| 288 | + |
| 289 | + it("waitForDatasetsReady treats shuttingdown and unknown statuses as still-pending (does not return early)", async () => { |
| 290 | + const fetchImpl = vi.fn().mockResolvedValue( |
| 291 | + new Response( |
| 292 | + JSON.stringify([ |
| 293 | + { name: "a", status: "Ready" }, |
| 294 | + { name: "b", status: "ShuttingDown" }, |
| 295 | + { name: "c", status: "QuantumFlux" }, |
| 296 | + ]), |
| 297 | + { status: 200, headers: { "content-type": "application/json" } }, |
| 298 | + ), |
| 299 | + ); |
| 300 | + const clock = makeClock(); |
| 301 | + const rt = new RuntimeClient({ |
| 302 | + apiKey: "k", |
| 303 | + baseUrl: "https://x.example", |
| 304 | + warmupSeconds: 0, |
| 305 | + timeoutSeconds: 5, |
| 306 | + sdkFactory: () => makeSdk(), |
| 307 | + fetchImpl, |
| 308 | + clock, |
| 309 | + }); |
| 310 | + await expect(rt.waitForDatasetsReady(5)).rejects.toThrow( |
| 311 | + /did not finish loading.*ShuttingDown.*QuantumFlux/s, |
| 312 | + ); |
| 313 | + }); |
| 314 | + |
| 315 | + it("waitForDatasetsReady is a no-op when timeout is 0", async () => { |
| 316 | + const fetchImpl = vi.fn(); |
| 317 | + const rt = new RuntimeClient({ |
| 318 | + apiKey: "k", |
| 319 | + baseUrl: "https://x.example", |
| 320 | + warmupSeconds: 0, |
| 321 | + timeoutSeconds: 5, |
| 322 | + sdkFactory: () => makeSdk(), |
| 323 | + fetchImpl, |
| 324 | + }); |
| 325 | + expect(await rt.waitForDatasetsReady(0)).toEqual([]); |
| 326 | + expect(fetchImpl).not.toHaveBeenCalled(); |
| 327 | + }); |
| 328 | + |
163 | 329 | it("probeSearch reports failures with body context", async () => { |
164 | 330 | const fetchImpl = vi |
165 | 331 | .fn() |
|
0 commit comments