Spaces:
Sleeping
Sleeping
File size: 8,202 Bytes
476094d | 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import {
ApiEndpointPool,
isEndpointStructurallyEligible,
normalizeEndpointType,
} from "./api-endpoint-pool.mjs";
async function makeTempPoolDir() {
return fs.mkdtemp(path.join(os.tmpdir(), "api-pool-test-"));
}
test("normalizeEndpointType accepts codex and claude aliases", () => {
assert.equal(normalizeEndpointType("codex"), "codex");
assert.equal(normalizeEndpointType("claude"), "claude-code");
assert.equal(normalizeEndpointType("claude_code"), "claude-code");
});
test("isEndpointStructurallyEligible rejects invalid or disabled items", () => {
const valid = {
type: "codex",
baseUrl: "https://example.com/v1",
apiKey: "sk-1",
disabled: false,
};
assert.equal(isEndpointStructurallyEligible(valid, "codex"), true);
assert.equal(isEndpointStructurallyEligible({ ...valid, disabled: true }, "codex"), false);
assert.equal(isEndpointStructurallyEligible({ ...valid, apiKey: "" }, "codex"), false);
assert.equal(isEndpointStructurallyEligible({ ...valid, type: "claude-code" }, "codex"), false);
});
test("ApiEndpointPool loads eligible items for the selected provider", async () => {
const dir = await makeTempPoolDir();
await fs.writeFile(
path.join(dir, "good.json"),
JSON.stringify([
{
name: "main",
type: "codex",
baseUrl: "https://example.com/v1",
apiKey: "sk-good",
},
{
name: "backup",
type: "codex",
baseUrl: "https://backup.example.com/v1",
apiKey: "sk-backup",
},
]),
);
await fs.writeFile(
path.join(dir, "wrong-provider.json"),
JSON.stringify([
{
name: "cc",
type: "claude-code",
baseUrl: "https://claude.example.com",
apiKey: "sk-cc",
},
]),
);
const pool = new ApiEndpointPool({
poolDir: dir,
provider: "codex",
fetchFn: async () => new Response('{"data":[]}', { status: 200 }),
});
await pool.load();
assert.equal(pool.listEndpoints().length, 2);
assert.equal(pool.listEndpoints()[0].name, "main");
assert.equal(pool.listEndpoints()[1].name, "backup");
});
test("ApiEndpointPool supports legacy single-object files and array files together", async () => {
const dir = await makeTempPoolDir();
await fs.writeFile(
path.join(dir, "single.json"),
JSON.stringify({
name: "single",
type: "codex",
baseUrl: "https://single.example.com/v1",
apiKey: "sk-single",
}),
);
await fs.writeFile(
path.join(dir, "many.json"),
JSON.stringify([
{
name: "many-1",
type: "codex",
baseUrl: "https://many1.example.com/v1",
apiKey: "sk-many-1",
},
{
name: "many-2",
type: "codex",
baseUrl: "https://many2.example.com/v1",
apiKey: "sk-many-2",
},
]),
);
const pool = new ApiEndpointPool({
poolDir: dir,
provider: "codex",
fetchFn: async () => new Response('{"data":[]}', { status: 200 }),
});
await pool.load();
assert.equal(pool.listEndpoints().length, 3);
assert.deepEqual(
pool.listEndpoints().map((item) => item.name),
["many-1", "many-2", "single"],
);
});
test("ApiEndpointPool prioritizes pool.json over other json files in the same directory", async () => {
const dir = await makeTempPoolDir();
await fs.writeFile(
path.join(dir, "pool.json"),
JSON.stringify([
{
name: "preferred",
type: "codex",
baseUrl: "https://preferred.example.com/v1",
apiKey: "sk-preferred",
},
]),
);
await fs.writeFile(
path.join(dir, "other.json"),
JSON.stringify({
name: "ignored",
type: "codex",
baseUrl: "https://ignored.example.com/v1",
apiKey: "sk-ignored",
}),
);
const pool = new ApiEndpointPool({
poolDir: dir,
provider: "codex",
fetchFn: async () => new Response('{"data":[]}', { status: 200 }),
});
await pool.load();
assert.equal(pool.listEndpoints().length, 1);
assert.equal(pool.listEndpoints()[0].name, "preferred");
});
test("ApiEndpointPool rotates after failure and respects cooldown", async () => {
const dir = await makeTempPoolDir();
await fs.writeFile(
path.join(dir, "a.json"),
JSON.stringify([
{
name: "a",
type: "codex",
baseUrl: "https://a.example.com/v1",
apiKey: "sk-a",
},
{
name: "b",
type: "codex",
baseUrl: "https://b.example.com/v1",
apiKey: "sk-b",
},
]),
);
const pool = new ApiEndpointPool({
poolDir: dir,
provider: "codex",
fetchFn: async (url, options) => {
const auth = options?.headers?.authorization || "";
if (String(url).includes("a.example.com") && auth.includes("sk-a")) {
return new Response("forbidden", { status: 403 });
}
return new Response('{"data":[{"id":"gpt-5.4"}]}', { status: 200 });
},
});
await pool.load();
const initial = await pool.getInitialEndpoint();
assert.ok(initial);
assert.equal(initial.name, "b");
const failed = pool.listEndpoints().find((item) => item.name === "a");
assert.ok(pool.isCoolingDown(failed));
});
test("ApiEndpointPool returns null when all endpoints are cooling down", async () => {
const dir = await makeTempPoolDir();
await fs.writeFile(
path.join(dir, "only.json"),
JSON.stringify([
{
name: "only",
type: "codex",
baseUrl: "https://only.example.com/v1",
apiKey: "sk-only",
},
]),
);
const pool = new ApiEndpointPool({
poolDir: dir,
provider: "codex",
fetchFn: async () => new Response("rate limit", { status: 429 }),
});
await pool.load();
const initial = await pool.getInitialEndpoint();
assert.equal(initial, null);
assert.equal(pool.listEndpoints().length, 1);
assert.ok(pool.isCoolingDown(pool.listEndpoints()[0]));
});
test("ApiEndpointPool probes codex entries with the configured responses model", async () => {
const dir = await makeTempPoolDir();
await fs.writeFile(
path.join(dir, "pool.json"),
JSON.stringify([
{
name: "codex-main",
type: "codex",
baseUrl: "https://compat.example.com",
apiKey: "sk-compat",
model: "grok-4.1-fast",
},
]),
);
let seenUrl = "";
let seenMethod = "";
let seenBody = "";
const pool = new ApiEndpointPool({
poolDir: dir,
provider: "codex",
fetchFn: async (url, options) => {
seenUrl = String(url);
seenMethod = String(options?.method || "");
seenBody = String(options?.body || "");
return new Response('{"id":"resp_123","model":"grok-4.1-fast","output":[]}', { status: 200 });
},
});
await pool.load();
const endpoint = await pool.getInitialEndpoint();
assert.ok(endpoint);
assert.equal(seenMethod, "POST");
assert.match(seenUrl, /\/v1\/responses$/);
assert.match(seenBody, /"model":"grok-4\.1-fast"/);
});
test("ApiEndpointPool probes claude-code entries with the configured messages model", async () => {
const dir = await makeTempPoolDir();
await fs.writeFile(
path.join(dir, "pool.json"),
JSON.stringify([
{
name: "claude-main",
type: "claude-code",
baseUrl: "https://compat.example.com",
apiKey: "sk-compat",
model: "gpt-5.3-codex",
},
]),
);
let seenUrl = "";
let seenMethod = "";
let seenBody = "";
const pool = new ApiEndpointPool({
poolDir: dir,
provider: "claude-code",
fetchFn: async (url, options) => {
seenUrl = String(url);
seenMethod = String(options?.method || "");
seenBody = String(options?.body || "");
return new Response('{"id":"msg_123","content":[{"type":"text","text":"OK"}]}', { status: 200 });
},
});
await pool.load();
const endpoint = await pool.getInitialEndpoint();
assert.ok(endpoint);
assert.equal(seenMethod, "POST");
assert.match(seenUrl, /\/v1\/messages$/);
assert.match(seenBody, /"model":"gpt-5\.3-codex"/);
});
|