mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-01 04:42:10 +03:00
* docs(changelog): record PR #1748 for next release * fix(models): apply blocked providers filter to non-chat catalog models (#1752) * chore(release): v3.7.5 — integrate ngrok tunnel and fix models filter (#1753, #1752) * chore(release): update changelog format for v3.7.5 * Speed up endpoint initial render * Address endpoint review feedback * Add endpoint loading model translations * fix: resolve build issues and implement memory UPSERT logic (#1763) * fix: resolve build issues for v3.7.5 and apply memory/translation fixes 1. antigravityHeaders.ts: restore ANTIGRAVITY_LOAD_CODE_ASSIST_* exports for oauth.ts compatibility 2. next.config.mjs: add @ngrok/ngrok to serverExternalPackages and webpack externals to handle native .node modules 3. Memory system: UPSERT logic to prevent duplicate entries with same apiKeyId + key 4. Chinese translations: complete CLI tools and memory dashboard localizations 5. Test fixes: unique keys for pagination tests to comply with unique constraint Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: address Gemini Code Assist review feedback 1. store.ts: add expires_at to UPDATE statement in UPSERT logic - Previously, expires_at was not being persisted to database on update - This caused state mismatch between returned Memory object and actual DB row 2. package-lock.json: revert react-markdown registry to official npmjs.org - Mirror-specific registry URL (npmmirror.com) should not be in lockfile Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> * fix(antigravity): normalize Gemini bridge payloads (#1769) * fix(antigravity): normalize Gemini bridge payloads Clamp Claude bridge output tokens, use Gemini-valid system roles and tool names, and serialize antigravity requests from a cloned body so Cloud Code payload shaping stays valid. * fix(cli): stop fallback after unsafe known paths Preserve known-path security checks by stopping command discovery when a configured CLI path is suspicious or non-executable, instead of falling through to PATH discovery. * test(memory): make query result assertion deterministic Avoid relying on database result ordering when checking filtered memory keys so the unit suite remains stable across runs. * fix(review): preserve safe cloning and CLI reasons Handle non-cloneable antigravity request bodies without throwing and preserve specific CLI known-path failure reasons instead of masking them as not_found. * fix(sse): propagate AbortSignal to pre-fetch semaphore and rate-limit awaits (#1771) When a combo target takes too long, the request-level deadline fires and calls abortController.abort() on the stream controller, but the abort signal never reaches pending awaits in acquireAccountSemaphore() or withRateLimit(). These awaits sit between stream controller creation and executor.execute(), causing requests to hang indefinitely past the 600s deadline. Pass streamController.signal to both functions so they can respond to abort events and terminate early when the request deadline expires. Signed-off-by: wucm667 <stevenwucongmin@gmail.com> * Fix model sync import handling (#1755) * Fix model sync import handling * Align model import storage semantics * Address model review feedback * fix(codex): stabilize copilot responses reasoning and tool replay (#1750) * chore(xiaomi): Update Xiaomi provider model list (#1759) * Move DB health to management API (#1757) * Move DB health to management API * Address DB health review feedback * fix(kiro): support organization IDC OAuth with regional endpoints and refresh (#1754) * fix(kiro): support organization IDC OAuth with regional endpoints and refresh * fix(kiro): refresh IDC tokens with stored region --------- Co-authored-by: ngocdb <ngocdb@ngocdb.local> * chore(workflows): add strict PR contributor credit policy - Add ABSOLUTE PROHIBITION section to review-prs.md - Add PR PROHIBITION rule to resolve-issues.md - Add contributor credit rule to AGENTS.md Review Focus - Based on audit finding: 37 PRs had code absorbed without merge credit * chore(release): acknowledge 29 community contributors with retroactive credit This commit formally recognizes 29 contributors whose code was manually integrated across releases v3.4.0 through v3.7.4 without proper GitHub merge credit. Their PRs were resolved locally due to merge conflicts but closed instead of merged, preventing them from appearing in the Contributors graph. We have updated our workflows to ensure this never happens again. Co-authored-by: Randi <55005611+rdself@users.noreply.github.com> Co-authored-by: Benson K B <4044180+benzntech@users.noreply.github.com> Co-authored-by: clousky2020 <33016567+clousky2020@users.noreply.github.com> Co-authored-by: Raxxoor <7317522+dhaern@users.noreply.github.com> Co-authored-by: Jason Landbridge <15127381+JasonLandbridge@users.noreply.github.com> Co-authored-by: slewis3600 <35925982+slewis3600@users.noreply.github.com> Co-authored-by: Markus Hartung <12826053+hartmark@users.noreply.github.com> Co-authored-by: Hernan Javier Ardila Sanchez <204746071+herjarsa@users.noreply.github.com> Co-authored-by: 3_1_3_u <5846351+andruwa13@users.noreply.github.com> Co-authored-by: Paijo <14921983+oyi77@users.noreply.github.com> Co-authored-by: i1hwan <35260883+i1hwan@users.noreply.github.com> Co-authored-by: xandr0s <1709302+xandr0s@users.noreply.github.com> Co-authored-by: backryun <24198422+backryun@users.noreply.github.com> Co-authored-by: Owen <36758131+kang-heewon@users.noreply.github.com> Co-authored-by: Ravi Tharuma <25951435+RaviTharuma@users.noreply.github.com> Co-authored-by: Chris <3751981+christopher-s@users.noreply.github.com> Co-authored-by: Wellington Fonseca <5421548+wlfonseca@users.noreply.github.com> Co-authored-by: Ethan Hunt <136065060+only4copilot@users.noreply.github.com> Co-authored-by: tombii <6607822+tombii@users.noreply.github.com> Co-authored-by: AndrewDragonIV <7906124+AndrewDragonIV@users.noreply.github.com> Co-authored-by: Danh Thanh <50534210+dt418@users.noreply.github.com> Co-authored-by: Will F <30637450+willbnu@users.noreply.github.com> Co-authored-by: defhouse <232128212+defhouse@users.noreply.github.com> Co-authored-by: Skydwest <186351198+mercs2910@users.noreply.github.com> Co-authored-by: zenobit <6384793+zen0bit@users.noreply.github.com> Co-authored-by: Ivan <16905671+razllivan@users.noreply.github.com> Co-authored-by: foxy1402 <45601526+foxy1402@users.noreply.github.com> Co-authored-by: Luan Dias <65574834+luandiasrj@users.noreply.github.com> Co-authored-by: Sergei Korolev <891832+knopki@users.noreply.github.com> Co-authored-by: dail45 <69967573+dail45@users.noreply.github.com> * fix(combo): include 429 in provider circuit breaker to stop infinite retry on exhausted quotas (#1767) Previously, PROVIDER_FAILURE_ERROR_CODES only included {408, 500, 502, 503, 504}, meaning 429 responses never counted toward the circuit breaker threshold. This caused exhausted accounts to be retried every 3-5 seconds indefinitely instead of being blocked by the provider breaker. Adding 429 ensures persistent rate limiting triggers the circuit breaker after the configured failure threshold, giving the provider time to recover. * fix(claude): respect client thinking/effort params to prevent forced quota drain (#1761) Previously, OmniRoute unconditionally injected thinking: {type: 'adaptive'} and output_config: {effort: 'high'} for Claude Opus 4.7 in Claude Code client requests. This caused Claude Max 5h quota to drain in ~15 minutes. Now checks the original client body: if thinking or output_config are explicitly set (even to null or a different value), the injection is skipped. Users can opt-out by sending thinking: null or output_config: {effort: 'low'}. * Add MseeP.ai badge to README.md (#1727) Integrated into release/v3.7.5 * chore(docs): update CHANGELOG for PR #1727 * fix(tests): update stream-utils assertion for responses api compliance * feat: Fix support for claude-cli using Gemini provider (#1779) Integrated into release/v3.7.5 * fix(codex): align client identity metadata (#1778) Integrated into release/v3.7.5 * fix(blackbox-web): correct cookie name and populate session/subscription fields (#1776) Integrated into release/v3.7.5 * Fix Codex /responses/compact passthrough (#1777) Integrated into release/v3.7.5 * test(reasoning-cache): isolate DB state using mkdtempSync to prevent 401 middleware errors * chore(release): v3.7.5 — integrate remaining PRs and finalize stability * chore(config): remove local patch artifacts and trim workspace config Delete temporary patch scripts and local OMC session files that should not ship with the repository. Also remove the Next.js config file and expand editor and TypeScript exclusions to ignore large local workspace directories and reduce unnecessary indexing. * fix(antigravity): cap Claude bridge output tokens (#1785) Integrated into release/v3.7.5 * fix(codex): stabilize Copilot responses replay state (#1791) Integrated into release/v3.7.5 * fix(chatgpt-web): restore validator + expand model catalog to ChatGPT Plus tier (#1792) Integrated into release/v3.7.5 * fix(antigravity): scrub internal OmniRoute headers (#1794) Integrated into release/v3.7.5 * fix(grok-web): fix Grok validator and cookie parsing (#1793) Integrated into release/v3.7.5 * chore(release): v3.7.5 — finalize changelog for LTS patch * feat(api-keys): add rename support in permissions modal Add an editable key name field at the top of the permissions modal, allowing users to rename API keys alongside existing permission settings. The backend already supported name updates via PATCH /api/keys/:id — this wires the UI to send the name field and refreshes the key list on success. Changes: - Add keyName state and text input to PermissionsModal - Update handleUpdatePermissions to validate and send name in PATCH body - Add integration test for rename via PATCH (valid, empty, too-long names) - Update E2E mock to handle PATCH requests * chore(release): finalize v3.7.5 LTS release with schema and db initialization fixes * test: fix json escaping in stream-utilities test * fix(build): restore next.config.mjs that was accidentally deleted * fix(sse): decrement pending requests on passthrough mode failure (#1798) Integrated into release/v3.7.5 * fix(grok-web): repair validator probe + accept full cookie blobs (#1793) Integrated into release/v3.7.5 * docs(i18n): sync documentation updates to 40 languages --------- Signed-off-by: wucm667 <stevenwucongmin@gmail.com> Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com> Co-authored-by: R.D. <rogerproself@gmail.com> Co-authored-by: clousky2020 <33016567+clousky2020@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: cloudy <37777261+uwuclxdy@users.noreply.github.com> Co-authored-by: wucm667 <109257021+wucm667@users.noreply.github.com> Co-authored-by: Randi <55005611+rdself@users.noreply.github.com> Co-authored-by: ivan-mezentsev <ivan@mezentsev.me> Co-authored-by: backryun <bakryun0718@proton.me> Co-authored-by: Dao Bao Ngoc <42265865+daongoc315@users.noreply.github.com> Co-authored-by: ngocdb <ngocdb@ngocdb.local> Co-authored-by: Benson K B <4044180+benzntech@users.noreply.github.com> Co-authored-by: Raxxoor <7317522+dhaern@users.noreply.github.com> Co-authored-by: Jason Landbridge <15127381+JasonLandbridge@users.noreply.github.com> Co-authored-by: slewis3600 <35925982+slewis3600@users.noreply.github.com> Co-authored-by: Markus Hartung <12826053+hartmark@users.noreply.github.com> Co-authored-by: Hernan Javier Ardila Sanchez <204746071+herjarsa@users.noreply.github.com> Co-authored-by: 3_1_3_u <5846351+andruwa13@users.noreply.github.com> Co-authored-by: Paijo <14921983+oyi77@users.noreply.github.com> Co-authored-by: i1hwan <35260883+i1hwan@users.noreply.github.com> Co-authored-by: xandr0s <1709302+xandr0s@users.noreply.github.com> Co-authored-by: backryun <24198422+backryun@users.noreply.github.com> Co-authored-by: Owen <36758131+kang-heewon@users.noreply.github.com> Co-authored-by: Ravi Tharuma <25951435+RaviTharuma@users.noreply.github.com> Co-authored-by: Chris <3751981+christopher-s@users.noreply.github.com> Co-authored-by: Wellington Fonseca <5421548+wlfonseca@users.noreply.github.com> Co-authored-by: Ethan Hunt <136065060+only4copilot@users.noreply.github.com> Co-authored-by: tombii <6607822+tombii@users.noreply.github.com> Co-authored-by: AndrewDragonIV <7906124+AndrewDragonIV@users.noreply.github.com> Co-authored-by: Danh Thanh <50534210+dt418@users.noreply.github.com> Co-authored-by: Will F <30637450+willbnu@users.noreply.github.com> Co-authored-by: defhouse <232128212+defhouse@users.noreply.github.com> Co-authored-by: Skydwest <186351198+mercs2910@users.noreply.github.com> Co-authored-by: zenobit <6384793+zen0bit@users.noreply.github.com> Co-authored-by: Ivan <16905671+razllivan@users.noreply.github.com> Co-authored-by: foxy1402 <45601526+foxy1402@users.noreply.github.com> Co-authored-by: Luan Dias <65574834+luandiasrj@users.noreply.github.com> Co-authored-by: Sergei Korolev <891832+knopki@users.noreply.github.com> Co-authored-by: dail45 <69967573+dail45@users.noreply.github.com> Co-authored-by: MseeP.ai <mseep@skydeck.ai> Co-authored-by: Markus Hartung <mail@hartmark.se> Co-authored-by: Raxxoor <manker_lol@hotmail.com> Co-authored-by: Jack <5443152+hijak@users.noreply.github.com> Co-authored-by: Sergey Morozov <tr0st@bk.ru> Co-authored-by: payne <baboialex95@gmail.com> Co-authored-by: Antigravity Assistant <bot@antigravity.local> Co-authored-by: Andrew Munsell <andrew@wizardapps.net>
973 lines
30 KiB
TypeScript
973 lines
30 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-model-sync-route-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
// FASE-01: API_KEY_SECRET is required for CRC operations (no hardcoded fallback)
|
|
if (!process.env.API_KEY_SECRET) {
|
|
process.env.API_KEY_SECRET = "test-model-sync-secret-" + Date.now();
|
|
}
|
|
|
|
const core = await import("../../src/lib/db/core.ts");
|
|
const localDb = await import("../../src/lib/localDb.ts");
|
|
const apiKeysDb = await import("../../src/lib/db/apiKeys.ts");
|
|
const providersDb = await import("../../src/lib/db/providers.ts");
|
|
const modelsDb = await import("../../src/lib/db/models.ts");
|
|
const callLogs = await import("../../src/lib/usage/callLogs.ts");
|
|
const modelSyncRoute = await import("../../src/app/api/providers/[id]/sync-models/route.ts");
|
|
const scheduler = await import("../../src/shared/services/modelSyncScheduler.ts");
|
|
const originalFetch = globalThis.fetch;
|
|
|
|
async function resetStorage() {
|
|
delete process.env.INITIAL_PASSWORD;
|
|
globalThis.fetch = originalFetch;
|
|
core.resetDbInstance();
|
|
apiKeysDb.resetApiKeyState();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
}
|
|
|
|
test.after(() => {
|
|
globalThis.fetch = originalFetch;
|
|
core.resetDbInstance();
|
|
apiKeysDb.resetApiKeyState();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
|
|
});
|
|
|
|
async function enableAuth() {
|
|
process.env.INITIAL_PASSWORD = "bootstrap-password";
|
|
await localDb.updateSettings({ requireLogin: true, password: "" });
|
|
}
|
|
|
|
test("model sync route skips success log when fetched models do not change stored models", async () => {
|
|
await resetStorage();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "openrouter",
|
|
authType: "apikey",
|
|
name: "MAIN",
|
|
displayName: "OpenRouter Main",
|
|
apiKey: "test-key",
|
|
});
|
|
|
|
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", connection.id, [
|
|
{
|
|
id: "custom-model-1",
|
|
name: "Custom Model 1",
|
|
source: "imported",
|
|
},
|
|
]);
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(
|
|
String(url),
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`
|
|
);
|
|
return Response.json({
|
|
models: [{ id: "custom-model-1", name: "Custom Model 1" }],
|
|
});
|
|
};
|
|
|
|
try {
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models`, {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
|
|
assert.equal(response.status, 200);
|
|
const body = (await response.json()) as any;
|
|
assert.equal(body.logged, false);
|
|
assert.deepEqual(body.modelChanges, { added: 0, removed: 0, updated: 0, total: 0 });
|
|
assert.deepEqual(body.models, []);
|
|
|
|
const logs = await callLogs.getCallLogs({ model: "model-sync", limit: 10 });
|
|
assert.equal(logs.length, 0);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("model sync route stores the real provider while keeping the account label", async () => {
|
|
await resetStorage();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "openrouter",
|
|
authType: "apikey",
|
|
name: "MAIN",
|
|
displayName: "OpenRouter Main",
|
|
apiKey: "test-key",
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(
|
|
String(url),
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`
|
|
);
|
|
return Response.json({
|
|
models: [{ id: "custom-model-2", name: "Custom Model 2" }],
|
|
});
|
|
};
|
|
|
|
try {
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models`, {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
|
|
assert.equal(response.status, 200);
|
|
const body = (await response.json()) as any;
|
|
assert.equal(body.logged, true);
|
|
assert.deepEqual(body.modelChanges, { added: 1, removed: 0, updated: 0, total: 1 });
|
|
assert.equal(body.provider, "openrouter");
|
|
|
|
const logs = await callLogs.getCallLogs({ model: "model-sync", limit: 10 });
|
|
assert.equal(logs.length, 1);
|
|
assert.equal(logs[0].provider, "openrouter");
|
|
assert.equal(logs[0].account, "MAIN");
|
|
assert.equal(logs[0].model, "model-sync");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("model sync route requires authentication for external requests when auth is enabled", async () => {
|
|
await resetStorage();
|
|
await enableAuth();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "openrouter",
|
|
authType: "apikey",
|
|
name: "Protected Connection",
|
|
apiKey: "test-key",
|
|
});
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models`, {
|
|
method: "POST",
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
const body = (await response.json()) as any;
|
|
|
|
assert.equal(response.status, 401);
|
|
assert.equal(body.error.message, "Authentication required");
|
|
assert.equal(body.error.type, "invalid_api_key");
|
|
});
|
|
|
|
test("model sync route returns 404 for unknown connections after internal auth passes", async () => {
|
|
await resetStorage();
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request("http://localhost/api/providers/missing/sync-models", {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: "missing" } }
|
|
);
|
|
|
|
assert.equal(response.status, 404);
|
|
assert.deepEqual(await response.json(), { error: "Connection not found" });
|
|
});
|
|
|
|
test("model sync route propagates upstream failures and records an error log entry", async () => {
|
|
await resetStorage();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "openrouter",
|
|
authType: "apikey",
|
|
name: "Error Branch",
|
|
apiKey: "test-key",
|
|
});
|
|
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(
|
|
String(url),
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`
|
|
);
|
|
return Response.json({ error: "Provider upstream unavailable" }, { status: 502 });
|
|
};
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models`, {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
const body = (await response.json()) as any;
|
|
const logs = await callLogs.getCallLogs({ model: "model-sync", limit: 10 });
|
|
|
|
assert.equal(response.status, 502);
|
|
assert.equal(body.error, "Provider upstream unavailable");
|
|
assert.equal(logs.length, 1);
|
|
assert.equal(logs[0].status, 502);
|
|
assert.equal(logs[0].provider, "openrouter");
|
|
assert.equal(logs[0].path, `/api/providers/${connection.id}/models`);
|
|
});
|
|
|
|
test("model sync route falls back to the upstream HTTP status when the models payload has no error field", async () => {
|
|
await resetStorage();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "openrouter",
|
|
authType: "apikey",
|
|
name: "Rate Limited Sync",
|
|
apiKey: "test-key",
|
|
});
|
|
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(
|
|
String(url),
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`
|
|
);
|
|
return Response.json({}, { status: 429 });
|
|
};
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models`, {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
const body = (await response.json()) as any;
|
|
const logs = await callLogs.getCallLogs({ model: "model-sync", limit: 10 });
|
|
|
|
assert.equal(response.status, 429);
|
|
assert.equal(body.error, "Failed to fetch models");
|
|
assert.equal(logs.length, 1);
|
|
assert.equal(logs[0].status, 429);
|
|
assert.equal(logs[0].error, "HTTP 429");
|
|
});
|
|
|
|
test("model sync route reports invalid JSON /models responses without losing upstream status", async () => {
|
|
await resetStorage();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "openrouter",
|
|
authType: "apikey",
|
|
name: "Invalid JSON Sync",
|
|
apiKey: "test-key",
|
|
});
|
|
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(
|
|
String(url),
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`
|
|
);
|
|
return new Response("<html>bad gateway</html>", {
|
|
status: 200,
|
|
headers: { "content-type": "text/html" },
|
|
});
|
|
};
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models`, {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
const body = (await response.json()) as any;
|
|
const logs = await callLogs.getCallLogs({ model: "model-sync", limit: 10 });
|
|
|
|
assert.equal(response.status, 502);
|
|
assert.equal(body.error, "Invalid JSON response from /models");
|
|
assert.equal(body.upstreamStatus, 200);
|
|
assert.equal(logs.length, 1);
|
|
assert.equal(logs[0].status, 200);
|
|
assert.equal(logs[0].error, "Invalid JSON response from /models");
|
|
});
|
|
|
|
test("model sync route preserves previously synced models when the upstream omits the models list", async () => {
|
|
await resetStorage();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "openrouter",
|
|
authType: "apikey",
|
|
name: "No Models Returned",
|
|
apiKey: "test-key",
|
|
});
|
|
|
|
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", connection.id, [
|
|
{
|
|
id: "persisted-model",
|
|
name: "Persisted Model",
|
|
source: "imported",
|
|
},
|
|
]);
|
|
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(
|
|
String(url),
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`
|
|
);
|
|
return Response.json({});
|
|
};
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models`, {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
const body = (await response.json()) as any;
|
|
const logs = await callLogs.getCallLogs({ model: "model-sync", limit: 10 });
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.equal(body.syncedModels, 1);
|
|
assert.equal(body.logged, false);
|
|
assert.deepEqual(body.modelChanges, { added: 0, removed: 0, updated: 0, total: 0 });
|
|
assert.deepEqual(body.models, []);
|
|
assert.deepEqual(await modelsDb.getSyncedAvailableModels("openrouter"), [
|
|
{
|
|
id: "persisted-model",
|
|
name: "Persisted Model",
|
|
source: "imported",
|
|
},
|
|
]);
|
|
assert.equal(logs.length, 0);
|
|
});
|
|
|
|
test("model sync route writes synced available models for Gemini connections", async () => {
|
|
await resetStorage();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "gemini",
|
|
authType: "apikey",
|
|
name: "Gemini Sync",
|
|
apiKey: "gm-key",
|
|
});
|
|
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(
|
|
String(url),
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`
|
|
);
|
|
return Response.json({
|
|
models: [
|
|
{
|
|
id: "gemini-custom-preview",
|
|
name: "Gemini Custom Preview",
|
|
supportedEndpoints: ["chat", "embeddings"],
|
|
inputTokenLimit: 32768,
|
|
outputTokenLimit: 8192,
|
|
description: "Custom Gemini preview model",
|
|
supportsThinking: true,
|
|
},
|
|
],
|
|
});
|
|
};
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models`, {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
const body = (await response.json()) as any;
|
|
const synced = await modelsDb.getSyncedAvailableModels("gemini");
|
|
const logs = await callLogs.getCallLogs({ model: "model-sync", limit: 10 });
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.equal(body.provider, "gemini");
|
|
assert.equal(body.syncedModels, 1);
|
|
assert.equal(body.logged, true);
|
|
assert.deepEqual(body.modelChanges, { added: 1, removed: 0, updated: 0, total: 1 });
|
|
assert.deepEqual(body.models, []);
|
|
assert.deepEqual(synced, [
|
|
{
|
|
id: "gemini-custom-preview",
|
|
name: "Gemini Custom Preview",
|
|
source: "imported",
|
|
supportedEndpoints: ["chat", "embeddings"],
|
|
inputTokenLimit: 32768,
|
|
outputTokenLimit: 8192,
|
|
description: "Custom Gemini preview model",
|
|
supportsThinking: true,
|
|
},
|
|
]);
|
|
assert.equal(logs.length, 1);
|
|
assert.equal(logs[0].status, 200);
|
|
});
|
|
|
|
test("model sync route writes synced available models for non-Gemini providers too", async () => {
|
|
await resetStorage();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "opencode-go",
|
|
authType: "apikey",
|
|
name: "OpenCode Go Sync",
|
|
apiKey: "opencode-go-key",
|
|
});
|
|
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(
|
|
String(url),
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`
|
|
);
|
|
return Response.json({
|
|
models: [
|
|
{
|
|
id: "glm-5.1",
|
|
name: "GLM 5.1",
|
|
supportedEndpoints: ["chat"],
|
|
inputTokenLimit: 262144,
|
|
},
|
|
],
|
|
});
|
|
};
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models`, {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
const body = (await response.json()) as any;
|
|
const synced = await modelsDb.getSyncedAvailableModels("opencode-go");
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.equal(body.provider, "opencode-go");
|
|
assert.equal(body.syncedModels, 1);
|
|
assert.deepEqual(synced, [
|
|
{
|
|
id: "glm-5.1",
|
|
name: "GLM 5.1",
|
|
source: "imported",
|
|
supportedEndpoints: ["chat"],
|
|
inputTokenLimit: 262144,
|
|
},
|
|
]);
|
|
});
|
|
|
|
test("model sync route import mode merges discovered models without deleting manual models", async () => {
|
|
await resetStorage();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "openrouter",
|
|
authType: "apikey",
|
|
name: "OpenRouter Import",
|
|
apiKey: "test-key",
|
|
});
|
|
|
|
await modelsDb.addCustomModel("openrouter", "manual-only", "Manual Only", "manual");
|
|
await modelsDb.addCustomModel("openrouter", "router-v4", "Manual Router V4", "manual");
|
|
await localDb.setModelAlias("manual-only", "openrouter/manual-only");
|
|
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(
|
|
String(url),
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`
|
|
);
|
|
return Response.json({
|
|
models: [{ id: "router-v4", name: "Router V4" }],
|
|
});
|
|
};
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models?mode=import`, {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
const body = (await response.json()) as any;
|
|
const aliases = await localDb.getModelAliases();
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.equal(body.mode, "merge");
|
|
assert.equal(body.importedCount, 1);
|
|
assert.equal(body.updatedCount, 0);
|
|
assert.equal(body.syncedAliases, 1);
|
|
assert.deepEqual(body.modelChanges, { added: 1, removed: 0, updated: 0, total: 1 });
|
|
assert.deepEqual(body.customModelChanges, { added: 0, removed: 1, updated: 0, total: 1 });
|
|
assert.deepEqual(
|
|
body.models.map((model) => ({ id: model.id, source: model.source })),
|
|
[{ id: "manual-only", source: "manual" }]
|
|
);
|
|
assert.deepEqual(
|
|
body.importedModels.map((model) => ({ id: model.id, source: model.source })),
|
|
[{ id: "router-v4", source: "imported" }]
|
|
);
|
|
assert.deepEqual(
|
|
(await modelsDb.getSyncedAvailableModels("openrouter")).map((model) => ({
|
|
id: model.id,
|
|
source: model.source,
|
|
})),
|
|
[{ id: "router-v4", source: "imported" }]
|
|
);
|
|
assert.equal(aliases["manual-only"], "openrouter/manual-only");
|
|
assert.equal(aliases["router-v4"], "openrouter/router-v4");
|
|
});
|
|
|
|
test("model sync route import mode ignores supported endpoint ordering changes", async () => {
|
|
await resetStorage();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "openrouter",
|
|
authType: "apikey",
|
|
name: "OpenRouter Import Stable",
|
|
apiKey: "test-key",
|
|
});
|
|
|
|
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", connection.id, [
|
|
{
|
|
id: "router-v4",
|
|
name: "Router V4",
|
|
source: "imported",
|
|
supportedEndpoints: ["chat", "embeddings"],
|
|
},
|
|
]);
|
|
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(
|
|
String(url),
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`
|
|
);
|
|
return Response.json({
|
|
models: [
|
|
{
|
|
id: "router-v4",
|
|
name: "Router V4",
|
|
supportedEndpoints: ["embeddings", "chat"],
|
|
},
|
|
],
|
|
});
|
|
};
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models?mode=import`, {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
const body = (await response.json()) as any;
|
|
const logs = await callLogs.getCallLogs({ model: "model-sync", limit: 10 });
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.equal(body.importedCount, 0);
|
|
assert.equal(body.updatedCount, 0);
|
|
assert.deepEqual(body.importedChanges, { added: 0, updated: 0, unchanged: 1, total: 0 });
|
|
assert.deepEqual(body.modelChanges, { added: 0, removed: 0, updated: 0, total: 0 });
|
|
assert.equal(body.logged, false);
|
|
assert.deepEqual(body.importedModels, []);
|
|
assert.deepEqual(
|
|
(await modelsDb.getSyncedAvailableModels("openrouter")).map((model) => ({
|
|
id: model.id,
|
|
supportedEndpoints: model.supportedEndpoints,
|
|
})),
|
|
[{ id: "router-v4", supportedEndpoints: ["chat", "embeddings"] }]
|
|
);
|
|
assert.deepEqual(body.models, []);
|
|
assert.equal(logs.length, 0);
|
|
});
|
|
|
|
test("model sync route import mode reports updates without counting them as new imports", async () => {
|
|
await resetStorage();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "openrouter",
|
|
authType: "apikey",
|
|
name: "OpenRouter Import Update",
|
|
apiKey: "test-key",
|
|
});
|
|
|
|
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", connection.id, [
|
|
{
|
|
id: "router-v4",
|
|
name: "Router V4",
|
|
source: "imported",
|
|
supportedEndpoints: ["chat"],
|
|
},
|
|
]);
|
|
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(
|
|
String(url),
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`
|
|
);
|
|
return Response.json({
|
|
models: [
|
|
{
|
|
id: "router-v4",
|
|
name: "Router V4 Updated",
|
|
supportedEndpoints: ["chat", "embeddings"],
|
|
},
|
|
],
|
|
});
|
|
};
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models?mode=import`, {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
const body = (await response.json()) as any;
|
|
const logs = await callLogs.getCallLogs({ model: "model-sync", limit: 10 });
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.equal(body.importedCount, 0);
|
|
assert.equal(body.updatedCount, 1);
|
|
assert.deepEqual(body.importedChanges, { added: 0, updated: 1, unchanged: 0, total: 1 });
|
|
assert.deepEqual(body.importedModels, []);
|
|
assert.deepEqual(
|
|
(await modelsDb.getSyncedAvailableModels("openrouter")).map((model) => ({
|
|
id: model.id,
|
|
name: model.name,
|
|
supportedEndpoints: model.supportedEndpoints,
|
|
})),
|
|
[
|
|
{
|
|
id: "router-v4",
|
|
name: "Router V4 Updated",
|
|
supportedEndpoints: ["chat", "embeddings"],
|
|
},
|
|
]
|
|
);
|
|
assert.deepEqual(body.models, []);
|
|
assert.equal(body.logged, true);
|
|
assert.equal(logs.length, 1);
|
|
});
|
|
|
|
test("model sync route records added, removed, and updated model diffs with fallback identifiers", async () => {
|
|
await resetStorage();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "openrouter",
|
|
authType: "oauth",
|
|
email: "sync@example.com",
|
|
accessToken: "sync-token",
|
|
});
|
|
|
|
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", connection.id, [
|
|
{
|
|
id: "persisted-model",
|
|
name: "Persisted Model",
|
|
source: "imported",
|
|
},
|
|
{
|
|
id: "removed-model",
|
|
name: "Removed Model",
|
|
source: "imported",
|
|
},
|
|
]);
|
|
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(
|
|
String(url),
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`
|
|
);
|
|
return Response.json({
|
|
models: [
|
|
{
|
|
id: "persisted-model",
|
|
name: "Persisted Model v2",
|
|
supportedEndpoints: ["chat", "embeddings"],
|
|
},
|
|
{
|
|
model: "fallback-model",
|
|
displayName: "Fallback Model",
|
|
description: "Fallback from model field",
|
|
},
|
|
],
|
|
});
|
|
};
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models`, {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
const body = (await response.json()) as any;
|
|
const logs = await callLogs.getCallLogs({ model: "model-sync", limit: 10 });
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.equal(body.syncedModels, 2);
|
|
assert.equal(body.logged, true);
|
|
assert.deepEqual(body.modelChanges, { added: 1, removed: 1, updated: 1, total: 3 });
|
|
assert.deepEqual(
|
|
(await modelsDb.getSyncedAvailableModels("openrouter")).map((model) => ({
|
|
id: model.id,
|
|
name: model.name,
|
|
supportedEndpoints: model.supportedEndpoints,
|
|
description: model.description,
|
|
})),
|
|
[
|
|
{
|
|
id: "persisted-model",
|
|
name: "Persisted Model v2",
|
|
supportedEndpoints: ["chat", "embeddings"],
|
|
description: undefined,
|
|
},
|
|
{
|
|
id: "fallback-model",
|
|
name: "Fallback Model",
|
|
supportedEndpoints: undefined,
|
|
description: "Fallback from model field",
|
|
},
|
|
]
|
|
);
|
|
assert.equal(logs.length, 1);
|
|
assert.equal(logs[0].status, 200);
|
|
assert.equal(logs[0].provider, "openrouter");
|
|
assert.ok(logs[0].account.includes("*"), `Expected masked email, got: ${logs[0].account}`);
|
|
});
|
|
|
|
test("model sync route forwards cookies, filters built-ins, and syncs aliases for internal requests", async () => {
|
|
await resetStorage();
|
|
await enableAuth();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "openrouter",
|
|
authType: "apikey",
|
|
name: "External Sync",
|
|
displayName: "OpenRouter External",
|
|
apiKey: "test-key",
|
|
});
|
|
|
|
await localDb.setModelAlias("stale-model", "openrouter/stale-model");
|
|
await localDb.setModelAlias("router-v2", "other-provider/router-v2");
|
|
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
assert.equal(
|
|
String(url),
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`
|
|
);
|
|
assert.equal(init.headers.cookie, "session=test-cookie");
|
|
assert.equal(
|
|
init.headers[scheduler.getModelSyncInternalAuthHeaderName()],
|
|
scheduler.buildModelSyncInternalHeaders()[scheduler.getModelSyncInternalAuthHeaderName()]
|
|
);
|
|
|
|
return Response.json({
|
|
models: [
|
|
{ id: "auto", name: "Auto (Best Available)" },
|
|
{ id: "router-v2", name: "Router V2" },
|
|
{ id: "router-v3", name: "Router V3" },
|
|
],
|
|
});
|
|
};
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models`, {
|
|
method: "POST",
|
|
headers: {
|
|
cookie: "session=test-cookie",
|
|
...scheduler.buildModelSyncInternalHeaders(),
|
|
},
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
const body = (await response.json()) as any;
|
|
const aliases = await localDb.getModelAliases();
|
|
const logs = await callLogs.getCallLogs({ model: "model-sync", limit: 10 });
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.equal(body.provider, "openrouter");
|
|
assert.equal(body.syncedModels, 3);
|
|
assert.equal(body.availableModelsCount, 3);
|
|
assert.equal(body.syncedAliases, 3);
|
|
assert.equal(body.logged, true);
|
|
assert.deepEqual(body.modelChanges, { added: 3, removed: 0, updated: 0, total: 3 });
|
|
assert.deepEqual(body.models, []);
|
|
assert.equal(aliases["stale-model"], undefined);
|
|
assert.equal(aliases["auto"], "openrouter/auto");
|
|
assert.equal(aliases["openrouter-router-v2"], "openrouter/router-v2");
|
|
assert.equal(aliases["router-v3"], "openrouter/router-v3");
|
|
assert.equal(logs.length, 1);
|
|
assert.equal(logs[0].status, 200);
|
|
assert.equal(logs[0].account, "External Sync");
|
|
});
|
|
|
|
test("model sync route reports synced managed models separately from preserved manual models", async () => {
|
|
await resetStorage();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "openrouter",
|
|
authType: "apikey",
|
|
name: "Mixed Sync",
|
|
apiKey: "test-key",
|
|
});
|
|
|
|
await modelsDb.addCustomModel("openrouter", "manual-only", "Manual Only", "manual");
|
|
await modelsDb.addCustomModel("openrouter", "router-v4", "Manual Router V4", "manual");
|
|
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(
|
|
String(url),
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`
|
|
);
|
|
return Response.json({
|
|
models: [{ id: "router-v4", name: "Router V4" }],
|
|
});
|
|
};
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models`, {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
const body = (await response.json()) as any;
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.equal(body.syncedModels, 1);
|
|
assert.equal(body.availableModelsCount, 2);
|
|
assert.equal(body.importedCount, 1);
|
|
assert.equal(body.updatedCount, 0);
|
|
assert.deepEqual(body.customModelChanges, { added: 0, removed: 1, updated: 0, total: 1 });
|
|
assert.deepEqual(
|
|
body.models.map((model) => ({ id: model.id, source: model.source })),
|
|
[{ id: "manual-only", source: "manual" }]
|
|
);
|
|
assert.deepEqual(
|
|
(await modelsDb.getSyncedAvailableModels("openrouter")).map((model) => ({
|
|
id: model.id,
|
|
source: model.source,
|
|
})),
|
|
[{ id: "router-v4", source: "imported" }]
|
|
);
|
|
});
|
|
|
|
test("model sync route uses provider-node prefixes when syncing compatible-provider aliases", async () => {
|
|
await resetStorage();
|
|
|
|
await providersDb.createProviderNode({
|
|
id: "anthropic-compatible-demo",
|
|
type: "anthropic-compatible",
|
|
name: "Anthropic Demo",
|
|
prefix: "cm",
|
|
baseUrl: "https://proxy.example.com",
|
|
chatPath: "/v1/messages",
|
|
modelsPath: "/v1/models",
|
|
});
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "anthropic-compatible-demo",
|
|
authType: "apikey",
|
|
name: "Compatible Sync",
|
|
apiKey: "compat-key",
|
|
providerSpecificData: {
|
|
baseUrl: "https://proxy.example.com",
|
|
chatPath: "/v1/messages",
|
|
modelsPath: "/v1/models",
|
|
},
|
|
});
|
|
|
|
await localDb.setModelAlias("sonnet-4-6", "some-other-provider/sonnet-4-6");
|
|
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(
|
|
String(url),
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`
|
|
);
|
|
return Response.json({
|
|
models: [{ id: "sonnet-4-6", name: "Sonnet 4.6" }],
|
|
});
|
|
};
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models`, {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
const body = (await response.json()) as any;
|
|
const aliases = await localDb.getModelAliases();
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.equal(body.provider, "anthropic-compatible-demo");
|
|
assert.equal(body.syncedAliases, 1);
|
|
assert.equal(aliases["cm-sonnet-4-6"], "anthropic-compatible-demo/sonnet-4-6");
|
|
});
|
|
|
|
test("model sync route falls back to in-process discovery when internal self-fetch throws", async () => {
|
|
await resetStorage();
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "openai-compatible-aio",
|
|
authType: "apikey",
|
|
name: "AIO Import",
|
|
apiKey: "test-key",
|
|
providerSpecificData: {
|
|
prefix: "aio",
|
|
apiType: "chat",
|
|
baseUrl: "https://api.bltcy.ai/v1",
|
|
nodeName: "aio",
|
|
autoSync: true,
|
|
},
|
|
});
|
|
|
|
const fetchCalls: string[] = [];
|
|
globalThis.fetch = async (url) => {
|
|
const urlString = String(url);
|
|
fetchCalls.push(urlString);
|
|
|
|
if (urlString === `http://localhost/api/providers/${connection.id}/models?refresh=true`) {
|
|
throw new Error("fetch failed");
|
|
}
|
|
|
|
assert.equal(urlString, "https://api.bltcy.ai/v1/models");
|
|
return Response.json({
|
|
data: [{ id: "aio-model", name: "AIO Model" }],
|
|
});
|
|
};
|
|
|
|
const response = await modelSyncRoute.POST(
|
|
new Request(`http://localhost/api/providers/${connection.id}/sync-models?mode=import`, {
|
|
method: "POST",
|
|
headers: scheduler.buildModelSyncInternalHeaders(),
|
|
}),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
const body = (await response.json()) as {
|
|
importedCount: number;
|
|
importedModels: Array<{ id: string; source: string }>;
|
|
};
|
|
const customModels = (await modelsDb.getCustomModels("openai-compatible-aio")) as Array<{
|
|
id: string;
|
|
source: string;
|
|
}>;
|
|
const availableModels = await modelsDb.getSyncedAvailableModels("openai-compatible-aio");
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.equal(body.importedCount, 1);
|
|
assert.deepEqual(
|
|
body.importedModels.map((model) => ({ id: model.id, source: model.source })),
|
|
[{ id: "aio-model", source: "imported" }]
|
|
);
|
|
assert.deepEqual(
|
|
customModels.map((model) => ({ id: model.id, source: model.source })),
|
|
[]
|
|
);
|
|
assert.deepEqual(
|
|
availableModels.map((model) => ({ id: model.id, source: model.source })),
|
|
[{ id: "aio-model", source: "imported" }]
|
|
);
|
|
assert.deepEqual(fetchCalls, [
|
|
`http://localhost/api/providers/${connection.id}/models?refresh=true`,
|
|
"https://api.bltcy.ai/v1/models",
|
|
]);
|
|
});
|