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

94 lines
2.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 llamaConfig = {
provider: {
llama: { options: { baseURL: "http://localhost:9090/v1" } },
},
}
test("explicit server wins over provider detection", () => {
assert.deepEqual(
resolveServerUrls({ server: "http://headless.local:8080" }, llamaConfig),
["http://headless.local:8080"],
)
})
test("explicit server strips /v1 path", () => {
assert.deepEqual(
resolveServerUrls({ server: "http://headless.local:8080/v1" }, {}),
["http://headless.local:8080"],
)
})
test("non-string server falls back to detection", () => {
assert.deepEqual(resolveServerUrls({ server: 42 }, llamaConfig), [
"http://localhost:9090",
])
})
test("empty server falls back to detection", () => {
assert.deepEqual(resolveServerUrls({ server: "" }, llamaConfig), [
"http://localhost:9090",
])
})
test("no options uses detected providers", () => {
assert.deepEqual(resolveServerUrls(undefined, llamaConfig), [
"http://localhost:9090",
])
})
test("no options and no matching providers falls back to localhost", () => {
assert.deepEqual(
resolveServerUrls(undefined, {
provider: { vision: { options: { baseURL: "http://headless.local:8080/v1" } } },
}),
["http://localhost:8080"],
)
})
test("scheme-less server falls back to detection", () => {
assert.deepEqual(resolveServerUrls({ server: "localhost:8080" }, llamaConfig), [
"http://localhost:9090",
])
})
test("whitespace-only server falls back to detection", () => {
assert.deepEqual(resolveServerUrls({ server: " " }, llamaConfig), [
"http://localhost:9090",
])
})
test("null server falls back to detection", () => {
assert.deepEqual(resolveServerUrls({ server: null }, llamaConfig), [
"http://localhost:9090",
])
})