Files
oc-ls-stats/test-backoff.ts
T

154 lines
4.5 KiB
TypeScript

import { test } from "node:test"
import assert from "node:assert/strict"
import { backoffDelayMs, resolveServerUrls } from "./src/stats.ts"
test("first failure retries after the base delay", () => {
assert.equal(backoffDelayMs(1), 1000)
})
test("second failure doubles the delay", () => {
assert.equal(backoffDelayMs(2), 2000)
})
test("delay grows exponentially", () => {
assert.equal(backoffDelayMs(3), 4000)
assert.equal(backoffDelayMs(4), 8000)
})
test("delay is capped at the maximum", () => {
assert.equal(backoffDelayMs(5), 10000)
assert.equal(backoffDelayMs(10), 10000)
assert.equal(backoffDelayMs(20), 10000)
})
test("uses provided base and max", () => {
assert.equal(backoffDelayMs(1, 200, 5000), 200)
assert.equal(backoffDelayMs(5, 200, 5000), 3200)
assert.equal(backoffDelayMs(20, 200, 5000), 5000)
})
const llamaProviders = [
{
id: "llamacpp",
name: "llama.cpp",
source: "config",
env: [],
options: { baseURL: "http://localhost:9090/v1" },
models: {},
},
]
const visionProviders = [
{
id: "vision",
name: "vision",
source: "config",
env: [],
options: { baseURL: "http://headless.local:8080/v1" },
models: {},
},
]
const llamaConfig = {
provider: {
llama: { options: { baseURL: "http://localhost:9090/v1" } },
},
}
test("explicit server wins over provider list", () => {
assert.deepEqual(
resolveServerUrls({ server: "http://headless.local:8080" }, llamaProviders, {}),
["http://headless.local:8080"],
)
})
test("explicit server strips /v1 path", () => {
assert.deepEqual(
resolveServerUrls({ server: "http://headless.local:8080/v1" }, llamaProviders, {}),
["http://headless.local:8080"],
)
})
test("non-string server falls back to provider list", () => {
assert.deepEqual(resolveServerUrls({ server: 42 }, llamaProviders, {}), [
"http://localhost:9090",
])
})
test("empty server falls back to provider list", () => {
assert.deepEqual(resolveServerUrls({ server: "" }, llamaProviders, {}), [
"http://localhost:9090",
])
})
test("scheme-less server falls back to provider list", () => {
assert.deepEqual(resolveServerUrls({ server: "localhost:8080" }, llamaProviders, {}), [
"http://localhost:9090",
])
})
test("whitespace-only server falls back to provider list", () => {
assert.deepEqual(resolveServerUrls({ server: " " }, llamaProviders, {}), [
"http://localhost:9090",
])
})
test("null server falls back to provider list", () => {
assert.deepEqual(resolveServerUrls({ server: null }, llamaProviders, {}), [
"http://localhost:9090",
])
})
test("no options uses provider list", () => {
assert.deepEqual(resolveServerUrls(undefined, llamaProviders, {}), [
"http://localhost:9090",
])
})
test("provider name match detects llama servers", () => {
const byName = [{ ...visionProviders[0], name: "My llama server" }]
assert.deepEqual(resolveServerUrls(undefined, byName, {}), [
"http://headless.local:8080",
])
})
test("ollama providers are excluded", () => {
const ollama = [
{ id: "ollama", name: "Ollama", source: "config", env: [], options: { baseURL: "http://localhost:11434" }, models: {} },
]
assert.deepEqual(resolveServerUrls(undefined, ollama, {}), ["http://localhost:8080"])
})
test("non-string baseURL is ignored", () => {
const bad = [{ id: "llamacpp", name: "llama.cpp", options: { baseURL: 123 } }]
assert.deepEqual(resolveServerUrls(undefined, bad, {}), ["http://localhost:8080"])
})
test("missing provider list falls back to legacy config parse", () => {
assert.deepEqual(resolveServerUrls(undefined, undefined, llamaConfig), [
"http://localhost:9090",
])
})
test("empty provider list falls back to legacy config parse", () => {
assert.deepEqual(resolveServerUrls(undefined, [], llamaConfig), [
"http://localhost:9090",
])
})
test("nothing anywhere falls back to localhost", () => {
assert.deepEqual(resolveServerUrls(undefined, visionProviders, {}), [
"http://localhost:8080",
])
})
test("provider base_url fallback is detected", () => {
const snake = [{ id: "llamacpp", name: "llama.cpp", options: { base_url: "http://localhost:9090/v1" } }]
assert.deepEqual(resolveServerUrls(undefined, snake, {}), ["http://localhost:9090"])
})
test("ollama in id excludes even when name mentions llama", () => {
const mixed = [{ id: "ollama-bridge", name: "My llama server", options: { baseURL: "http://localhost:11434" } }]
assert.deepEqual(resolveServerUrls(undefined, mixed, {}), ["http://localhost:8080"])
})