|
| 1 | +import { clearLiveCatalogCacheForTests } from "openclaw/plugin-sdk/provider-catalog-shared"; |
1 | 2 | import type { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime"; |
2 | | -import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
3 | 4 | import { discoverLlamaServer } from "./discovery.js"; |
4 | 5 |
|
5 | 6 | type LlamaServerFetchGuard = typeof fetchWithSsrFGuard; |
@@ -40,6 +41,10 @@ function json(value: unknown, status = 200): Response { |
40 | 41 | } |
41 | 42 |
|
42 | 43 | describe("llama-server discovery", () => { |
| 44 | + beforeEach(() => { |
| 45 | + clearLiveCatalogCacheForTests(); |
| 46 | + }); |
| 47 | + |
43 | 48 | it("discovers a single model and reads runtime properties", async () => { |
44 | 49 | const { guard, requests } = createFetchGuard({ |
45 | 50 | "http://localhost:8080/health": json({ status: "ok" }), |
@@ -201,6 +206,60 @@ describe("llama-server discovery", () => { |
201 | 206 | expect(requests.filter((url) => url.includes("/props?"))).toHaveLength(8); |
202 | 207 | }); |
203 | 208 |
|
| 209 | + it("keeps router properties associated with their model when probes finish out of order", async () => { |
| 210 | + const rows = [0, 1, 2].map((index) => ({ |
| 211 | + id: `model-${index}`, |
| 212 | + object: "model", |
| 213 | + status: { value: "loaded" }, |
| 214 | + })); |
| 215 | + const routes: Record<string, RouteValue> = { |
| 216 | + "http://localhost:8080/health": json({ status: "ok" }), |
| 217 | + "http://localhost:8080/models": json({ data: rows }), |
| 218 | + }; |
| 219 | + for (const [index, row] of rows.entries()) { |
| 220 | + routes[`http://localhost:8080/props?model=${row.id}&autoload=false`] = async () => { |
| 221 | + await new Promise((resolve) => { |
| 222 | + setTimeout(resolve, (2 - index) * 5); |
| 223 | + }); |
| 224 | + return json({ default_generation_settings: { n_ctx: 8_000 + index } }); |
| 225 | + }; |
| 226 | + } |
| 227 | + const { guard } = createFetchGuard(routes); |
| 228 | + |
| 229 | + const result = await discoverTest({ |
| 230 | + baseUrl: "http://localhost:8080", |
| 231 | + fetchGuard: guard, |
| 232 | + }); |
| 233 | + |
| 234 | + expect( |
| 235 | + result.kind === "success" ? result.models.map((model) => model.config.contextWindow) : [], |
| 236 | + ).toEqual([8_000, 8_001, 8_002]); |
| 237 | + }); |
| 238 | + |
| 239 | + it("caps router property probes at 200 models", async () => { |
| 240 | + const rows = Array.from({ length: 205 }, (_, index) => ({ |
| 241 | + id: `model-${index}`, |
| 242 | + object: "model", |
| 243 | + status: { value: "loaded" }, |
| 244 | + })); |
| 245 | + const routes: Record<string, RouteValue> = { |
| 246 | + "http://localhost:8080/health": json({ status: "ok" }), |
| 247 | + "http://localhost:8080/models": json({ data: rows }), |
| 248 | + }; |
| 249 | + for (const row of rows.slice(0, 200)) { |
| 250 | + routes[`http://localhost:8080/props?model=${row.id}&autoload=false`] = json({}); |
| 251 | + } |
| 252 | + const { guard, requests } = createFetchGuard(routes); |
| 253 | + |
| 254 | + const result = await discoverTest({ |
| 255 | + baseUrl: "http://localhost:8080", |
| 256 | + fetchGuard: guard, |
| 257 | + }); |
| 258 | + |
| 259 | + expect(result.kind === "success" ? result.models : []).toHaveLength(205); |
| 260 | + expect(requests.filter((url) => url.includes("/props?"))).toHaveLength(200); |
| 261 | + }); |
| 262 | + |
204 | 263 | it("falls back to /v1/models for an older compatible server", async () => { |
205 | 264 | const { guard } = createFetchGuard({ |
206 | 265 | "http://localhost:8080/health": json({ status: "ok" }), |
@@ -313,6 +372,131 @@ describe("llama-server discovery", () => { |
313 | 372 | expect(guard).toHaveBeenCalledTimes(2); |
314 | 373 | }); |
315 | 374 |
|
| 375 | + it("coalesces concurrent discovery for the same uncredentialed endpoint", async () => { |
| 376 | + let releaseHealth: ((response: Response) => void) | undefined; |
| 377 | + const { guard } = createFetchGuard({ |
| 378 | + "http://localhost:8080/health": () => |
| 379 | + new Promise<Response>((resolve) => { |
| 380 | + releaseHealth = resolve; |
| 381 | + }), |
| 382 | + "http://localhost:8080/models": () => json({ data: [] }), |
| 383 | + }); |
| 384 | + |
| 385 | + const first = discoverTest({ |
| 386 | + baseUrl: "http://localhost:8080", |
| 387 | + apiKey: "custom-local", |
| 388 | + fetchGuard: guard, |
| 389 | + cacheTtlMs: 30_000, |
| 390 | + }); |
| 391 | + const second = discoverTest({ |
| 392 | + baseUrl: "http://localhost:8080", |
| 393 | + apiKey: "custom-local", |
| 394 | + fetchGuard: guard, |
| 395 | + cacheTtlMs: 30_000, |
| 396 | + }); |
| 397 | + |
| 398 | + expect(releaseHealth).toBeDefined(); |
| 399 | + expect(guard).toHaveBeenCalledTimes(1); |
| 400 | + releaseHealth?.(json({ status: "ok" })); |
| 401 | + await expect(Promise.all([first, second])).resolves.toEqual([ |
| 402 | + expect.objectContaining({ kind: "success" }), |
| 403 | + expect.objectContaining({ kind: "success" }), |
| 404 | + ]); |
| 405 | + expect(guard).toHaveBeenCalledTimes(2); |
| 406 | + }); |
| 407 | + |
| 408 | + it("reloads cached discovery after its configured TTL", async () => { |
| 409 | + vi.useFakeTimers(); |
| 410 | + try { |
| 411 | + vi.setSystemTime(1_000); |
| 412 | + const { guard } = createFetchGuard({ |
| 413 | + "http://localhost:8080/health": () => json({ status: "ok" }), |
| 414 | + "http://localhost:8080/models": () => json({ data: [] }), |
| 415 | + }); |
| 416 | + |
| 417 | + await discoverTest({ |
| 418 | + baseUrl: "http://localhost:8080", |
| 419 | + apiKey: "custom-local", |
| 420 | + fetchGuard: guard, |
| 421 | + cacheTtlMs: 100, |
| 422 | + }); |
| 423 | + vi.setSystemTime(1_101); |
| 424 | + await discoverTest({ |
| 425 | + baseUrl: "http://localhost:8080", |
| 426 | + apiKey: "custom-local", |
| 427 | + fetchGuard: guard, |
| 428 | + cacheTtlMs: 100, |
| 429 | + }); |
| 430 | + |
| 431 | + expect(guard).toHaveBeenCalledTimes(4); |
| 432 | + } finally { |
| 433 | + vi.useRealTimers(); |
| 434 | + } |
| 435 | + }); |
| 436 | + |
| 437 | + it("does not retain unsuccessful discovery results", async () => { |
| 438 | + const { guard } = createFetchGuard({ |
| 439 | + "http://localhost:8080/health": () => json({ error: "unauthorized" }, 401), |
| 440 | + }); |
| 441 | + |
| 442 | + for (let index = 0; index < 2; index += 1) { |
| 443 | + await discoverTest({ |
| 444 | + baseUrl: "http://localhost:8080", |
| 445 | + apiKey: "custom-local", |
| 446 | + fetchGuard: guard, |
| 447 | + cacheTtlMs: 30_000, |
| 448 | + }); |
| 449 | + } |
| 450 | + |
| 451 | + expect(guard).toHaveBeenCalledTimes(2); |
| 452 | + }); |
| 453 | + |
| 454 | + it("bypasses an existing endpoint cache when a real API key is supplied", async () => { |
| 455 | + const { guard } = createFetchGuard({ |
| 456 | + "http://localhost:8080/health": () => json({ status: "ok" }), |
| 457 | + "http://localhost:8080/models": () => json({ data: [] }), |
| 458 | + }); |
| 459 | + |
| 460 | + await discoverTest({ |
| 461 | + baseUrl: "http://localhost:8080", |
| 462 | + apiKey: "custom-local", |
| 463 | + fetchGuard: guard, |
| 464 | + cacheTtlMs: 30_000, |
| 465 | + }); |
| 466 | + await discoverTest({ |
| 467 | + baseUrl: "http://localhost:8080", |
| 468 | + apiKey: "real-secret", |
| 469 | + fetchGuard: guard, |
| 470 | + cacheTtlMs: 30_000, |
| 471 | + }); |
| 472 | + |
| 473 | + expect(guard).toHaveBeenCalledTimes(4); |
| 474 | + }); |
| 475 | + |
| 476 | + it("keeps uncredentialed discovery caches isolated by endpoint", async () => { |
| 477 | + const { guard } = createFetchGuard({ |
| 478 | + "http://localhost:8080/health": json({ status: "ok" }), |
| 479 | + "http://localhost:8080/models": json({ data: [] }), |
| 480 | + "http://localhost:8081/health": json({ status: "ok" }), |
| 481 | + "http://localhost:8081/models": json({ data: [] }), |
| 482 | + }); |
| 483 | + |
| 484 | + await discoverTest({ |
| 485 | + baseUrl: "http://localhost:8080", |
| 486 | + apiKey: "custom-local", |
| 487 | + fetchGuard: guard, |
| 488 | + cacheTtlMs: 30_000, |
| 489 | + }); |
| 490 | + await discoverTest({ |
| 491 | + baseUrl: "http://localhost:8081", |
| 492 | + apiKey: "custom-local", |
| 493 | + fetchGuard: guard, |
| 494 | + cacheTtlMs: 30_000, |
| 495 | + }); |
| 496 | + |
| 497 | + expect(guard).toHaveBeenCalledTimes(4); |
| 498 | + }); |
| 499 | + |
316 | 500 | it("bounds cached endpoint discovery", async () => { |
317 | 501 | const guard = vi.fn(async (params: Parameters<LlamaServerFetchGuard>[0]) => ({ |
318 | 502 | response: params.url.endsWith("/health") ? json({ status: "ok" }) : json({ data: [] }), |
|
0 commit comments