Spaces:
Running

File size: 3,444 Bytes
2d91c13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { afterEach, describe, expect, it, vi } from "vitest";
import { getInternalApiConfig } from "./internalApiConfig.ts";

afterEach(() => {
  vi.unstubAllEnvs();
  vi.restoreAllMocks();
});

function stubEnvironment(values: Record<string, string | undefined>) {
  for (const variable of [
    "INTERNAL_OPENAI_COMPATIBLE_API_BASE_URL",
    "INTERNAL_OPENAI_COMPATIBLE_API_KEY",
    "INTERNAL_OPENAI_COMPATIBLE_API_MODEL",
    "INTERNAL_OPENAI_COMPATIBLE_API_NAME",
    "INTERNAL_OPENAI_COMPATIBLE_API_CONTEXT_LENGTH",
    "INTERNAL_OPENAI_COMPATIBLE_API_REASONING_EFFORT",
    "MODEL_DEFAULT_MAX_TOKENS",
    "MODEL_REQUEST_TIMEOUT_MS",
  ]) {
    vi.stubEnv(variable, values[variable]);
  }
}

describe("getInternalApiConfig", () => {
  it("returns the defaults when nothing is set", () => {
    stubEnvironment({});

    expect(getInternalApiConfig()).toEqual({
      baseUrl: undefined,
      apiKey: undefined,
      models: [],
      name: "Internal API",
      contextLength: 131072,
      reasoningEffort: undefined,
      maxOutputTokens: 8192,
      requestTimeoutMs: 30000,
    });
  });

  it("reads every variable that is set", () => {
    stubEnvironment({
      INTERNAL_OPENAI_COMPATIBLE_API_BASE_URL: "https://llm.example/v1",
      INTERNAL_OPENAI_COMPATIBLE_API_KEY: "test-key",
      INTERNAL_OPENAI_COMPATIBLE_API_MODEL: " model-a , model-b ,",
      INTERNAL_OPENAI_COMPATIBLE_API_NAME: "Company LLM",
      INTERNAL_OPENAI_COMPATIBLE_API_CONTEXT_LENGTH: "32768",
      INTERNAL_OPENAI_COMPATIBLE_API_REASONING_EFFORT: " low ",
      MODEL_DEFAULT_MAX_TOKENS: "4096",
      MODEL_REQUEST_TIMEOUT_MS: "60000",
    });

    expect(getInternalApiConfig()).toEqual({
      baseUrl: "https://llm.example/v1",
      apiKey: "test-key",
      models: ["model-a", "model-b"],
      name: "Company LLM",
      contextLength: 32768,
      reasoningEffort: "low",
      maxOutputTokens: 4096,
      requestTimeoutMs: 60000,
    });
  });

  // `parseInt` read "8k" as 8, so a typo cut every answer to 8 tokens.
  it.each(["8k", "abc", "0", "-1", "1.5", "0x2000", "1e4"])(
    "replaces %j with the default and logs it",
    (value) => {
      stubEnvironment({ MODEL_DEFAULT_MAX_TOKENS: value });
      const consoleError = vi
        .spyOn(console, "error")
        .mockImplementation(() => {});

      expect(getInternalApiConfig().maxOutputTokens).toBe(8192);
      expect(consoleError).toHaveBeenCalledWith(
        `MODEL_DEFAULT_MAX_TOKENS must be a whole number from 1 to ${Number.MAX_SAFE_INTEGER} ("${value}"); using 8192.`,
      );
    },
  );

  // Node fires a longer timer after 1 ms, which would abort every attempt.
  it("rejects a timeout longer than a timer can wait", () => {
    stubEnvironment({ MODEL_REQUEST_TIMEOUT_MS: "2147483648" });
    vi.spyOn(console, "error").mockImplementation(() => {});

    expect(getInternalApiConfig().requestTimeoutMs).toBe(30000);
  });

  it("accepts the longest timeout a timer can wait", () => {
    stubEnvironment({ MODEL_REQUEST_TIMEOUT_MS: "2147483647" });

    expect(getInternalApiConfig().requestTimeoutMs).toBe(2147483647);
  });

  it("logs a bad value once, not on every read", () => {
    stubEnvironment({ MODEL_REQUEST_TIMEOUT_MS: "30s" });
    const consoleError = vi
      .spyOn(console, "error")
      .mockImplementation(() => {});

    getInternalApiConfig();
    getInternalApiConfig();

    expect(consoleError).toHaveBeenCalledTimes(1);
  });
});