Spaces:
Running

File size: 1,897 Bytes
c0e51f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { afterEach, describe, expect, it, vi } from "vitest";
import { listOpenAiCompatibleModels } from "./openaiModels";

const fetchMock = vi.fn();
vi.stubGlobal("fetch", fetchMock);

afterEach(() => {
  fetchMock.mockReset();
  vi.restoreAllMocks();
});

describe("listOpenAiCompatibleModels", () => {
  it("skips entries without a usable id", async () => {
    fetchMock.mockResolvedValue(
      new Response(
        JSON.stringify({
          data: [{ id: "a" }, { object: "model" }, { id: "" }, null, { id: 7 }],
        }),
      ),
    );

    await expect(listOpenAiCompatibleModels("http://x/v1/")).resolves.toEqual([
      { id: "a" },
    ]);
    expect(fetchMock.mock.calls[0][0]).toBe("http://x/v1/models");
  });

  it("aborts with the caller's signal", async () => {
    fetchMock.mockResolvedValue(new Response(JSON.stringify({ data: [] })));
    const controller = new AbortController();

    await listOpenAiCompatibleModels("http://x", undefined, controller.signal);
    const signal: AbortSignal = fetchMock.mock.calls[0][1].signal;
    expect(signal.aborted).toBe(false);
    controller.abort();
    expect(signal.aborted).toBe(true);
  });

  it("gives up on a server that never answers after 10 seconds", async () => {
    const timeout = new AbortController();
    const spy = vi
      .spyOn(AbortSignal, "timeout")
      .mockReturnValue(timeout.signal);
    fetchMock.mockImplementation(
      (_url: string, init: RequestInit) =>
        new Promise((_resolve, reject) =>
          init.signal?.addEventListener("abort", () =>
            reject(init.signal?.reason),
          ),
        ),
    );

    const listing = listOpenAiCompatibleModels("http://black-hole");
    expect(spy).toHaveBeenCalledWith(10_000);
    timeout.abort(new DOMException("Timed out", "TimeoutError"));
    await expect(listing).rejects.toMatchObject({ name: "TimeoutError" });
  });
});