mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-14 02:42:24 +03:00
Landed with the design call resolved per the owner's pick — **option 1**: the synced store is now endpoint-agnostic (persistDiscoveredModels and managedModelImport no longer drop non-chat models at write time), and chat selectability moved to read time (auto-pool expansion in autoStrategy applies filterChatSelectableModels; the models-route projection already had its chatOnly filter). Your discovery test now passes end-to-end (3/3): /api/show capabilities persist per connection and image/embedding requests route through the advertising host. Reconciliation notes: conflicted areas merged onto the current tip (adobe discovery import, requestedModel preflight signature, resolvedProvider fast-path coexists with the synced-route override — explicit resolution wins); carried base-red drains (#10055 memoization, #11071 test variants) dropped as already-landed; the managed-model-import exclusion test was propagated to the new contract (image/video models persist; the read filter still hides them from chat pickers — pinned by a new assertion). Full battery: 205/206 focused (the one red is a confirmed periodic-timer timing flake on the loaded devbox — 20/20 isolated), autoCombo vitest 30/30, combo suites 46/46, gates + typecheck clean. Thank you @yourspraveen — the capability probe + routing design was right; it just needed the store contract opened up. Fixes #11087.
120 lines
4.1 KiB
TypeScript
120 lines
4.1 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
const { validateProviderApiKey } = await import("../../src/lib/providers/validation.ts");
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
|
|
test.afterEach(() => {
|
|
globalThis.fetch = originalFetch;
|
|
});
|
|
|
|
const imageOnlyProviders = {
|
|
"fal-ai": {
|
|
url: "https://api.fal.ai/v1/models?limit=1",
|
|
header: "Authorization",
|
|
value: "Key fal-ai-key",
|
|
},
|
|
"stability-ai": {
|
|
url: "https://api.stability.ai/v1/user/account",
|
|
header: "Authorization",
|
|
value: "Bearer stability-ai-key",
|
|
},
|
|
"black-forest-labs": {
|
|
url: "https://api.bfl.ai/v1/credits",
|
|
header: "x-key",
|
|
value: "black-forest-labs-key",
|
|
},
|
|
recraft: {
|
|
url: "https://external.api.recraft.ai/v1/users/me",
|
|
header: "Authorization",
|
|
value: "Bearer recraft-key",
|
|
},
|
|
topaz: {
|
|
url: "https://api.topazlabs.com/account/v1/credits/balance",
|
|
header: "X-API-Key",
|
|
value: "topaz-key",
|
|
},
|
|
magnific: {
|
|
url: "https://api.magnific.com/v1/ai/mystic",
|
|
header: "x-magnific-api-key",
|
|
value: "magnific-key",
|
|
},
|
|
};
|
|
|
|
const expectedValidationError = (status: number) =>
|
|
status === 429 ? "Validation rate limited (429)" : `Validation failed: ${status}`;
|
|
|
|
for (const [provider, config] of Object.entries(imageOnlyProviders)) {
|
|
test(`${provider} API key validator returns valid on 200`, async () => {
|
|
let fetchCalled = false;
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
fetchCalled = true;
|
|
assert.equal(String(url), config.url);
|
|
assert.equal((init.headers as Record<string, string>)[config.header], config.value);
|
|
return new Response(JSON.stringify({ ok: true }), { status: 200 });
|
|
};
|
|
|
|
const result = await validateProviderApiKey({ provider, apiKey: `${provider}-key` });
|
|
|
|
assert.equal(result.valid, true, `${provider} should validate a 200 response`);
|
|
assert.equal(result.error, null, `${provider} should not return an error for 200`);
|
|
assert.equal(fetchCalled, true, `${provider} should call its validation endpoint`);
|
|
});
|
|
}
|
|
|
|
for (const provider of Object.keys(imageOnlyProviders)) {
|
|
for (const status of [401, 403]) {
|
|
test(`${provider} API key validator returns invalid on ${status}`, async () => {
|
|
let fetchCalled = false;
|
|
globalThis.fetch = async () => {
|
|
fetchCalled = true;
|
|
return new Response(JSON.stringify({ error: "unauthorized" }), { status });
|
|
};
|
|
|
|
const result = await validateProviderApiKey({ provider, apiKey: `${provider}-key` });
|
|
|
|
assert.equal(result.valid, false, `${provider} should reject ${status}`);
|
|
assert.equal(result.error, "Invalid API key", `${provider} should surface auth failure`);
|
|
assert.equal(fetchCalled, true, `${provider} should call its validation endpoint`);
|
|
});
|
|
}
|
|
|
|
for (const status of [400, 404, 429]) {
|
|
test(`${provider} API key validator returns validation failed on ${status}`, async () => {
|
|
let fetchCalled = false;
|
|
globalThis.fetch = async () => {
|
|
fetchCalled = true;
|
|
return new Response(JSON.stringify({ error: "validation failed" }), { status });
|
|
};
|
|
|
|
const result = await validateProviderApiKey({ provider, apiKey: `${provider}-key` });
|
|
|
|
assert.equal(result.valid, false, `${provider} should reject ${status}`);
|
|
assert.equal(
|
|
result.error,
|
|
expectedValidationError(status),
|
|
`${provider} should surface validation failure`
|
|
);
|
|
assert.equal(fetchCalled, true, `${provider} should call its validation endpoint`);
|
|
});
|
|
}
|
|
}
|
|
|
|
test("freepik alias validates through the Magnific Mystic endpoint", async () => {
|
|
let fetchCalled = false;
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
fetchCalled = true;
|
|
assert.equal(String(url), "https://api.magnific.com/v1/ai/mystic");
|
|
assert.equal((init.headers as Record<string, string>)["x-magnific-api-key"], "legacy-key");
|
|
return new Response(JSON.stringify({ data: [] }), { status: 200 });
|
|
};
|
|
|
|
const result = await validateProviderApiKey({ provider: "freepik", apiKey: "legacy-key" });
|
|
|
|
assert.equal(result.valid, true);
|
|
assert.equal(result.error, null);
|
|
assert.notEqual(result.unsupported, true);
|
|
assert.equal(fetchCalled, true);
|
|
});
|