Files
OmniRoute/tests/unit/alibaba-provider-regions.test.ts

366 lines
13 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { REGISTRY } from "../../open-sse/config/providers/index.ts";
import { DefaultExecutor } from "../../open-sse/executors/default.ts";
import { connectionBelongsToProviderPage } from "../../src/app/(dashboard)/dashboard/providers/providerPageUtils.ts";
import { validateProviderApiKey } from "../../src/lib/providers/validation.ts";
import { isFlatRateProvider } from "../../src/lib/usage/flatRateProviders.ts";
import {
ALIBABA_PROVIDER_ENDPOINTS,
getDefaultAlibabaProviderRegion,
isAlibabaRegionalProvider,
normalizeAlibabaProviderRegion,
resolveAlibabaProviderBaseUrl,
resolveAlibabaProviderMediaBaseUrl,
resolveAlibabaProviderModelsUrl,
} from "../../src/shared/constants/alibabaProviderRegions.ts";
import { APIKEY_PROVIDERS } from "../../src/shared/constants/providers.ts";
test("Alibaba-family endpoint matrix keeps product and region boundaries distinct", () => {
assert.deepEqual(ALIBABA_PROVIDER_ENDPOINTS, {
alibaba: {
"global-sg": "https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
"china-beijing": "https://dashscope.aliyuncs.com/compatible-mode/v1",
},
"bailian-coding-plan": {
"global-sg": "https://token-plan.ap-southeast-1.maas.aliyuncs.com/apps/anthropic/v1",
"china-beijing": "https://token-plan.cn-beijing.maas.aliyuncs.com/apps/anthropic/v1",
},
"qwen-cloud": {
"global-sg": "https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
"china-beijing": "https://dashscope.aliyuncs.com/compatible-mode/v1",
},
"qwen-cloud-token-plan": {
"global-sg": "https://token-plan.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
"china-beijing": "https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
},
});
});
test("regional resolver defaults legacy alibaba-cn to Beijing and new families to Singapore", () => {
assert.equal(getDefaultAlibabaProviderRegion("alibaba-cn"), "china-beijing");
assert.equal(getDefaultAlibabaProviderRegion("alibaba"), "global-sg");
assert.equal(getDefaultAlibabaProviderRegion("bailian-coding-plan"), "global-sg");
assert.equal(getDefaultAlibabaProviderRegion("qwen-cloud"), "global-sg");
assert.equal(getDefaultAlibabaProviderRegion("qwen-cloud-token-plan"), "global-sg");
assert.equal(
resolveAlibabaProviderBaseUrl("alibaba-cn"),
ALIBABA_PROVIDER_ENDPOINTS.alibaba["china-beijing"]
);
assert.equal(
resolveAlibabaProviderBaseUrl("qwen-cloud", { region: "china-beijing" }),
ALIBABA_PROVIDER_ENDPOINTS["qwen-cloud"]["china-beijing"]
);
});
test("regional resolver normalizes old region names and preserves genuine custom endpoints", () => {
assert.equal(normalizeAlibabaProviderRegion("international"), "global-sg");
assert.equal(normalizeAlibabaProviderRegion("cn"), "china-beijing");
assert.equal(
resolveAlibabaProviderBaseUrl("bailian-coding-plan", {
region: "china",
baseUrl: "https://coding-intl.dashscope.aliyuncs.com/apps/anthropic/v1/messages",
}),
ALIBABA_PROVIDER_ENDPOINTS["bailian-coding-plan"]["china-beijing"],
"a saved preset must not pin the old region"
);
assert.equal(
resolveAlibabaProviderBaseUrl("qwen-cloud-token-plan", {
region: "china",
baseUrl: "https://token-plan.example.internal/compatible-mode/v1",
}),
"https://token-plan.example.internal/compatible-mode/v1",
"an operator-supplied endpoint must remain authoritative"
);
});
test("DefaultExecutor applies the regional endpoint to normal requests", () => {
const alibaba = new DefaultExecutor("alibaba");
assert.equal(
alibaba.buildUrl("qwen3.7-plus", true, 0, {
providerSpecificData: { region: "china-beijing" },
}),
"https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"
);
const codingPlan = new DefaultExecutor("bailian-coding-plan");
assert.equal(
codingPlan.buildUrl("qwen3.7-plus", true, 0, {
providerSpecificData: { region: "china-beijing" },
}),
"https://token-plan.cn-beijing.maas.aliyuncs.com/apps/anthropic/v1/messages"
);
const qwenCloud = new DefaultExecutor("qwen-cloud");
assert.equal(
qwenCloud.buildUrl("qwen-plus", true, 0, {
providerSpecificData: { region: "china-beijing" },
}),
"https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"
);
const tokenPlan = new DefaultExecutor("qwen-cloud-token-plan");
assert.equal(
tokenPlan.buildUrl("qwen3.7-max", true, 0, {
providerSpecificData: { region: "global-sg" },
}),
"https://token-plan.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions"
);
});
test("provider validation probes the selected Coding Plan region", async () => {
const originalFetch = globalThis.fetch;
const urls: string[] = [];
globalThis.fetch = async (input) => {
urls.push(String(input));
return new Response(JSON.stringify({ error: "probe accepted" }), {
status: 400,
headers: { "content-type": "application/json" },
});
};
try {
const result = await validateProviderApiKey({
provider: "bailian-coding-plan",
apiKey: "sk-sp-test",
providerSpecificData: {
region: "china-beijing",
baseUrl: "https://coding-intl.dashscope.aliyuncs.com/apps/anthropic/v1",
},
});
assert.equal(result.valid, true);
// The stored URL is a RETIRED preset, so it must not pin the connection: the
// china-beijing selector still wins and routes to the Token Plan CN host.
assert.deepEqual(urls, [
"https://token-plan.cn-beijing.maas.aliyuncs.com/apps/anthropic/v1/messages",
]);
} finally {
globalThis.fetch = originalFetch;
}
});
test("provider validation probes the selected Qwen Cloud pay-as-you-go region", async () => {
const originalFetch = globalThis.fetch;
const urls: string[] = [];
globalThis.fetch = async (input) => {
urls.push(String(input));
return new Response(JSON.stringify({ error: "probe accepted" }), {
status: 400,
headers: { "content-type": "application/json" },
});
};
try {
const result = await validateProviderApiKey({
provider: "qwen-cloud",
apiKey: "sk-sp-test",
providerSpecificData: { region: "china-beijing" },
});
assert.equal(result.valid, true);
assert.deepEqual(urls, [
"https://dashscope.aliyuncs.com/compatible-mode/v1/models",
"https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",
]);
} finally {
globalThis.fetch = originalFetch;
}
});
test("provider validation probes the selected Qwen Cloud Token Plan region", async () => {
const originalFetch = globalThis.fetch;
const urls: string[] = [];
globalThis.fetch = async (input) => {
urls.push(String(input));
return new Response(JSON.stringify({ error: "probe accepted" }), {
status: 400,
headers: { "content-type": "application/json" },
});
};
try {
const result = await validateProviderApiKey({
provider: "qwen-cloud-token-plan",
apiKey: "sk-sp-test",
providerSpecificData: { region: "china-beijing" },
});
assert.equal(result.valid, true);
assert.deepEqual(urls, [
"https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions",
]);
} finally {
globalThis.fetch = originalFetch;
}
});
test("Qwen Cloud is a first-class metered API-key provider", () => {
assert.equal(APIKEY_PROVIDERS["qwen-cloud"]?.name, "Qwen Cloud");
assert.equal(REGISTRY["qwen-cloud"]?.format, "openai");
assert.equal(REGISTRY["qwen-cloud"]?.passthroughModels, true);
assert.equal(
REGISTRY["qwen-cloud"]?.modelsUrl,
"https://dashscope-intl.aliyuncs.com/compatible-mode/v1/models"
);
assert.equal(isFlatRateProvider("qwen-cloud"), false);
assert.deepEqual(
REGISTRY["qwen-cloud"].models.map((model) => model.id),
[
"qwen3.8-max",
"qwen3.7-max-2026-06-08",
"qwen3.7-plus",
"qwen3.6-plus",
"qwen3.6-27b",
"qwen3.6-35b-a3b",
"qwen3.5-plus-2026-04-20",
"qwen3.5-122b-a10b",
"qwen3.5-397b-a17b",
"glm-5.2",
"glm-5.2-fast-preview",
"deepseek-v4-pro",
"deepseek-v4-flash",
"kimi-k2.7-code",
]
);
});
test("Alibaba Model Studio exposes the curated modern text catalog", () => {
const expectedModels = [
"qwen3.8-max",
"qwen3.7-max",
"qwen3.7-plus",
"qwen3.6-plus",
"qwen3.6-27b",
"qwen3.6-35b-a3b",
"qwen3.5-plus",
"qwen3.5-122b-a10b",
"qwen3.5-397b-a17b",
"glm-5.2",
"glm-5.2-fast-preview",
"deepseek-v4-pro",
"deepseek-v4-flash",
"kimi-k2.7-code",
];
assert.deepEqual(
REGISTRY.alibaba.models.map((model) => model.id),
expectedModels
);
assert.deepEqual(
REGISTRY["alibaba-cn"].models.map((model) => model.id),
expectedModels
);
});
test("Qwen Cloud model discovery follows the selected region", () => {
assert.equal(
resolveAlibabaProviderModelsUrl("qwen-cloud", { region: "global-sg" }),
"https://dashscope-intl.aliyuncs.com/compatible-mode/v1/models"
);
assert.equal(
resolveAlibabaProviderModelsUrl("qwen-cloud", { region: "china-beijing" }),
"https://dashscope.aliyuncs.com/compatible-mode/v1/models"
);
assert.equal(
resolveAlibabaProviderModelsUrl("qwen-cloud", {
baseUrl: "https://proxy.example.com/v1/chat/completions",
}),
"https://proxy.example.com/v1/models"
);
});
test("Qwen Cloud Token Plan remains a flat-rate provider with chat models only", () => {
assert.equal(APIKEY_PROVIDERS["qwen-cloud-token-plan"]?.name, "Qwen Cloud Token Plan");
assert.equal(REGISTRY["qwen-cloud-token-plan"]?.format, "openai");
assert.equal(isFlatRateProvider("qwen-cloud-token-plan"), true);
const modelIds = REGISTRY["qwen-cloud-token-plan"].models.map((model) => model.id);
assert.deepEqual(modelIds, [
"qwen3.8-max",
"qwen3.7-max",
"qwen3.7-plus",
"qwen3.6-flash",
"glm-5.2",
"deepseek-v4-pro",
"deepseek-v4-flash-0731",
]);
const qwen38 = REGISTRY["qwen-cloud-token-plan"].models[0];
assert.equal(qwen38.supportsReasoning, true);
assert.equal(qwen38.supportsVision, true);
assert.equal(qwen38.contextLength, 1_000_000);
assert.equal(qwen38.maxOutputTokens, 131_072);
});
test("dashboard folds legacy China connections into the unified Alibaba card", () => {
assert.equal(APIKEY_PROVIDERS["alibaba-cn"]?.hiddenFromDashboard, true);
assert.equal(connectionBelongsToProviderPage("alibaba", "alibaba"), true);
assert.equal(connectionBelongsToProviderPage("alibaba-cn", "alibaba"), true);
assert.equal(connectionBelongsToProviderPage("qwen-cloud", "alibaba"), false);
for (const providerId of [
"alibaba",
"alibaba-cn",
"bailian-coding-plan",
"qwen-cloud",
"qwen-cloud-token-plan",
]) {
assert.equal(isAlibabaRegionalProvider(providerId), true);
}
});
/**
* Regression guard for CodeQL `js/polynomial-redos` (alerts #765/#766).
*
* The trailing-slash trim used to be `/\/+$/`. That regex has no left anchor, so
* the engine retries the match at every start offset and each attempt walks the
* whole run of slashes before failing `$` — O(n^2) on a connection baseUrl made
* of many slashes. Measured on the vulnerable version: 10k slashes = 102ms,
* 30k = 968ms, 60k = 4028ms (clean quadratic). The linear strip runs in <1ms.
*
* `providerSpecificData.baseUrl` is operator-supplied config, so this input
* reaches the trim through isFamilyPresetUrl() -> normalizeEndpoint() and
* through both resolve*Url() helpers.
*/
test("trailing-slash normalization is linear on pathological slash runs (ReDoS guard)", () => {
const pathological = { baseUrl: `${"/".repeat(60_000)}x` };
const budgetMs = 500;
const started = performance.now();
resolveAlibabaProviderModelsUrl("alibaba", pathological);
resolveAlibabaProviderMediaBaseUrl("alibaba", pathological);
resolveAlibabaProviderBaseUrl("alibaba", pathological);
const elapsed = performance.now() - started;
assert.ok(
elapsed < budgetMs,
`trailing-slash trim must stay linear; took ${elapsed.toFixed(0)}ms (budget ${budgetMs}ms). ` +
"A quadratic trim (/\\/+$/) takes seconds on this input."
);
});
test("trailing-slash normalization keeps its exact behavior", () => {
// Every trailing slash is removed (not a bounded subset), and only trailing ones.
const withSlashes = { baseUrl: "https://example.test/v1////" };
assert.equal(
resolveAlibabaProviderModelsUrl("alibaba", withSlashes),
"https://example.test/v1/models"
);
// Interior slashes are preserved.
const interior = { baseUrl: "https://example.test//deep//path/" };
assert.equal(
resolveAlibabaProviderBaseUrl("alibaba", interior),
"https://example.test//deep//path/"
);
assert.equal(
resolveAlibabaProviderModelsUrl("alibaba", interior),
"https://example.test//deep//path/models"
);
// A string that is nothing but slashes collapses to empty.
assert.equal(resolveAlibabaProviderModelsUrl("alibaba", { baseUrl: "////" }), "");
});