File size: 15,935 Bytes
f0634fb | 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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | import { describe, expect, it, vi } from 'vitest';
import {
applyOpenPlatformConfig,
capabilitiesForModel,
fetchOpenPlatformModels,
filterModelsByPrefix,
getOpenPlatformById,
isOpenPlatformId,
OPEN_PLATFORMS,
OpenPlatformApiError,
removeOpenPlatformConfig,
type ManagedKimiConfigShape,
} from '../src/open-platform';
function makeModelsResponse(): Response {
return new Response(
JSON.stringify({
data: [
{
id: 'kimi-k2-0712-preview',
context_length: 256000,
supports_reasoning: true,
supports_image_in: true,
supports_video_in: true,
display_name: 'Kimi K2 0712 Preview',
},
{
id: 'kimi-k2-lite',
context_length: 128000,
supports_reasoning: false,
supports_image_in: false,
supports_video_in: false,
supports_tool_use: false,
},
{
id: 'non-kimi-model',
context_length: 1000,
supports_reasoning: false,
},
],
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
);
}
describe('OPEN_PLATFORMS', () => {
it('contains moonshot.cn and moonshot.ai', () => {
expect(getOpenPlatformById('moonshot-cn')).toMatchObject({
name: 'Kimi Platform (API key · platform.kimi.com)',
baseUrl: 'https://api.moonshot.cn/v1',
consoleUrl: 'https://platform.kimi.com',
allowedPrefixes: ['kimi-k'],
});
expect(getOpenPlatformById('moonshot-ai')).toMatchObject({
name: 'Kimi Platform (API key · platform.kimi.ai)',
baseUrl: 'https://api.moonshot.ai/v1',
consoleUrl: 'https://platform.kimi.ai',
allowedPrefixes: ['kimi-k'],
});
expect(getOpenPlatformById('unknown')).toBeUndefined();
});
it('isOpenPlatformId works', () => {
expect(isOpenPlatformId('moonshot-cn')).toBe(true);
expect(isOpenPlatformId('moonshot-ai')).toBe(true);
expect(isOpenPlatformId('kimi-code')).toBe(false);
});
});
describe('fetchOpenPlatformModels', () => {
it('lists and parses models from the platform endpoint', async () => {
const fetchMock = vi.fn(async () => makeModelsResponse());
const platform = getOpenPlatformById('moonshot-cn')!;
const models = await fetchOpenPlatformModels(platform, 'sk-test', fetchMock as unknown as typeof fetch);
expect(models).toHaveLength(3);
expect(models[0]).toMatchObject({
id: 'kimi-k2-0712-preview',
contextLength: 256000,
supportsReasoning: true,
supportsImageIn: true,
supportsVideoIn: true,
displayName: 'Kimi K2 0712 Preview',
});
expect(models[1]?.supportsToolUse).toBe(false);
expect(models[2]?.id).toBe('non-kimi-model');
expect(fetchMock).toHaveBeenCalledWith(
'https://api.moonshot.cn/v1/models',
expect.objectContaining({
headers: expect.objectContaining({
Authorization: 'Bearer sk-test',
Accept: 'application/json',
}),
}),
);
});
it('surfaces API error messages and status on HTTP error', async () => {
const fetchMock = vi.fn(
async () =>
new Response(JSON.stringify({ error: { message: 'invalid API key' } }), { status: 401 }),
);
const platform = getOpenPlatformById('moonshot-cn')!;
const error = await fetchOpenPlatformModels(
platform,
'sk-bad',
fetchMock as unknown as typeof fetch,
).catch((caught: unknown) => caught);
expect(error).toBeInstanceOf(OpenPlatformApiError);
expect((error as OpenPlatformApiError).status).toBe(401);
expect((error as Error).message).toBe('invalid API key');
});
it('throws on unexpected response shape', async () => {
const fetchMock = vi.fn(async () => new Response(JSON.stringify({}), { status: 200 }));
const platform = getOpenPlatformById('moonshot-cn')!;
await expect(
fetchOpenPlatformModels(platform, 'sk-test', fetchMock as unknown as typeof fetch),
).rejects.toThrow(/Unexpected models response/);
});
});
describe('filterModelsByPrefix', () => {
it('filters by allowedPrefixes when present', () => {
const platform = getOpenPlatformById('moonshot-cn')!;
const models = [
{ id: 'kimi-k2-0712-preview', contextLength: 256000, supportsReasoning: true, supportsImageIn: true, supportsVideoIn: true },
{ id: 'gpt-4', contextLength: 1000, supportsReasoning: false, supportsImageIn: false, supportsVideoIn: false },
];
const filtered = filterModelsByPrefix(models as unknown as import('../src/managed-kimi-code').ManagedKimiCodeModelInfo[], platform);
expect(filtered).toHaveLength(1);
expect(filtered[0]?.id).toBe('kimi-k2-0712-preview');
});
it('returns all models when allowedPrefixes is absent', () => {
const platform: import('../src/open-platform').OpenPlatformDefinition = {
id: 'custom',
name: 'Custom',
baseUrl: 'https://example.com/v1',
};
const models = [
{ id: 'model-a', contextLength: 1000, supportsReasoning: false, supportsImageIn: false, supportsVideoIn: false },
{ id: 'model-b', contextLength: 2000, supportsReasoning: false, supportsImageIn: false, supportsVideoIn: false },
];
const filtered = filterModelsByPrefix(models as unknown as import('../src/managed-kimi-code').ManagedKimiCodeModelInfo[], platform);
expect(filtered).toHaveLength(2);
});
});
describe('fetchOpenPlatformModels supports_thinking_type', () => {
it('parses supports_thinking_type from the models endpoint', async () => {
const fetchMock = vi.fn(
async () =>
new Response(
JSON.stringify({
data: [
{
id: 'kimi-k2-deep',
context_length: 256000,
supports_reasoning: true,
supports_thinking_type: 'only',
},
{
id: 'kimi-k2-lite',
context_length: 128000,
supports_reasoning: false,
},
],
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
),
);
const platform = getOpenPlatformById('moonshot-cn')!;
const models = await fetchOpenPlatformModels(platform, 'sk-test', fetchMock as unknown as typeof fetch);
expect(models[0]?.supportsThinkingType).toBe('only');
expect(models[1]?.supportsThinkingType).toBeUndefined();
});
});
describe('capabilitiesForModel', () => {
it("locks thinking on for 'only' models", () => {
const model = {
id: 'deep',
contextLength: 1000,
supportsReasoning: true,
supportsImageIn: false,
supportsVideoIn: false,
supportsToolUse: false,
supportsThinkingType: 'only' as const,
};
expect(capabilitiesForModel(model)).toEqual(['thinking', 'always_thinking']);
});
it("lets 'no' override the legacy supports_reasoning boolean", () => {
const model = {
id: 'plain',
contextLength: 1000,
supportsReasoning: true,
supportsImageIn: false,
supportsVideoIn: false,
supportsToolUse: false,
supportsThinkingType: 'no' as const,
};
expect(capabilitiesForModel(model)).toBeUndefined();
});
it("emits a plain toggleable thinking capability for 'both'", () => {
const model = {
id: 'toggle',
contextLength: 1000,
supportsReasoning: false,
supportsImageIn: false,
supportsVideoIn: false,
supportsToolUse: false,
supportsThinkingType: 'both' as const,
};
expect(capabilitiesForModel(model)).toEqual(['thinking']);
});
it('returns undefined for a model with no capabilities', () => {
const model = {
id: 'plain',
contextLength: 1000,
supportsReasoning: false,
supportsImageIn: false,
supportsVideoIn: false,
supportsToolUse: false,
};
expect(capabilitiesForModel(model)).toBeUndefined();
});
it('returns all caps for a full-featured model', () => {
const model = {
id: 'full',
contextLength: 1000,
supportsReasoning: true,
supportsImageIn: true,
supportsVideoIn: true,
supportsToolUse: true,
};
expect(capabilitiesForModel(model)).toEqual(['thinking', 'image_in', 'video_in', 'tool_use']);
});
});
describe('applyOpenPlatformConfig', () => {
it('writes provider, models, and defaults', () => {
const config: ManagedKimiConfigShape = {
providers: {},
};
const platform = getOpenPlatformById('moonshot-cn')!;
const models = [
{ id: 'kimi-k2-0712-preview', contextLength: 256000, supportsReasoning: true, supportsImageIn: true, supportsVideoIn: true, displayName: 'Kimi K2' },
{ id: 'kimi-k2-lite', contextLength: 128000, supportsReasoning: false, supportsImageIn: false, supportsVideoIn: false },
];
const result = applyOpenPlatformConfig(config, {
platform,
models,
selectedModel: models[0]!,
thinking: true,
apiKey: 'sk-test',
});
expect(result).toEqual({
defaultModel: 'moonshot-cn/kimi-k2-0712-preview',
defaultThinking: true,
});
expect(config.providers['moonshot-cn']).toMatchObject({
type: 'kimi',
baseUrl: 'https://api.moonshot.cn/v1',
apiKey: 'sk-test',
});
expect(config.models?.['moonshot-cn/kimi-k2-0712-preview']).toMatchObject({
provider: 'moonshot-cn',
model: 'kimi-k2-0712-preview',
maxContextSize: 256000,
capabilities: ['thinking', 'image_in', 'video_in', 'tool_use'],
displayName: 'Kimi K2',
});
expect(config.defaultModel).toBe('moonshot-cn/kimi-k2-0712-preview');
expect(config.thinking?.enabled).toBe(true);
expect(config.services).toBeUndefined();
});
it('clears stale models for the same provider', () => {
const config: ManagedKimiConfigShape = {
providers: {
'moonshot-cn': { type: 'kimi', baseUrl: 'https://api.moonshot.cn/v1', apiKey: 'sk-old' },
},
models: {
'moonshot-cn/stale': { provider: 'moonshot-cn', model: 'stale', maxContextSize: 1000 },
'other/model': { provider: 'other', model: 'other-model', maxContextSize: 1000 },
},
};
const platform = getOpenPlatformById('moonshot-cn')!;
const models = [
{ id: 'kimi-k2-0712-preview', contextLength: 256000, supportsReasoning: true, supportsImageIn: true, supportsVideoIn: true },
];
applyOpenPlatformConfig(config, {
platform,
models,
selectedModel: models[0]!,
thinking: false,
apiKey: 'sk-new',
});
expect(config.models?.['moonshot-cn/stale']).toBeUndefined();
expect(config.models?.['other/model']).toBeDefined();
});
it('preserves hand-edited fields that upstream does not declare', () => {
const config: ManagedKimiConfigShape = {
providers: {
'moonshot-cn': { type: 'kimi', baseUrl: 'https://api.moonshot.cn/v1', apiKey: 'sk-old' },
},
models: {
'moonshot-cn/kimi-k2-0712-preview': {
provider: 'moonshot-cn',
model: 'kimi-k2-0712-preview',
maxContextSize: 256000,
maxOutputSize: 8192,
supportEfforts: ['low', 'high'],
} as Record<string, unknown>,
},
};
const platform = getOpenPlatformById('moonshot-cn')!;
const models = [
{
id: 'kimi-k2-0712-preview',
contextLength: 256000,
supportsReasoning: true,
supportsImageIn: true,
supportsVideoIn: true,
},
];
applyOpenPlatformConfig(config, {
platform,
models,
selectedModel: models[0]!,
thinking: false,
apiKey: 'sk-new',
});
const alias = config.models?.['moonshot-cn/kimi-k2-0712-preview'];
expect(alias?.['maxOutputSize']).toBe(8192);
expect(alias?.['supportEfforts']).toBeUndefined();
});
it('preserves open-platform overrides during refresh', () => {
const config: ManagedKimiConfigShape = {
providers: {
'moonshot-cn': { type: 'kimi', baseUrl: 'https://api.moonshot.cn/v1', apiKey: 'sk-old' },
},
models: {
'moonshot-cn/kimi-k2-0712-preview': {
provider: 'moonshot-cn',
model: 'kimi-k2-0712-preview',
maxContextSize: 256000,
overrides: { supportEfforts: ['low'] },
} as Record<string, unknown>,
},
};
const platform = getOpenPlatformById('moonshot-cn')!;
const models = [
{
id: 'kimi-k2-0712-preview',
contextLength: 256000,
supportsReasoning: true,
supportsImageIn: false,
supportsVideoIn: false,
supportEfforts: ['low', 'high'],
},
];
applyOpenPlatformConfig(config, {
platform,
models,
selectedModel: models[0]!,
thinking: false,
apiKey: 'sk-new',
});
const alias = config.models?.['moonshot-cn/kimi-k2-0712-preview'];
expect(alias?.['supportEfforts']).toEqual(['low', 'high']);
expect(alias?.['overrides']).toEqual({ supportEfforts: ['low'] });
});
it('writes a concrete effort into config.thinking when provided', () => {
const config: ManagedKimiConfigShape = { providers: {} };
const platform = getOpenPlatformById('moonshot-cn')!;
const models = [
{
id: 'kimi-k2-0712-preview',
contextLength: 256000,
supportsReasoning: true,
supportsImageIn: false,
supportsVideoIn: false,
},
];
applyOpenPlatformConfig(config, {
platform,
models,
selectedModel: models[0]!,
thinking: true,
effort: 'high',
apiKey: 'sk-test',
});
expect(config.thinking).toEqual({ enabled: true, effort: 'high' });
});
it('omits effort for a boolean on (no concrete effort)', () => {
const config: ManagedKimiConfigShape = { providers: {} };
const platform = getOpenPlatformById('moonshot-cn')!;
const models = [
{
id: 'kimi-k2-0712-preview',
contextLength: 256000,
supportsReasoning: true,
supportsImageIn: false,
supportsVideoIn: false,
},
];
applyOpenPlatformConfig(config, {
platform,
models,
selectedModel: models[0]!,
thinking: true,
apiKey: 'sk-test',
});
expect(config.thinking).toEqual({ enabled: true });
expect(config.thinking?.effort).toBeUndefined();
});
});
describe('removeOpenPlatformConfig', () => {
it('removes provider, its models, and defaultModel when matched', () => {
const config: ManagedKimiConfigShape = {
providers: {
'moonshot-cn': { type: 'kimi', baseUrl: 'https://api.moonshot.cn/v1', apiKey: 'sk-test' },
'other': { type: 'kimi', baseUrl: 'https://other.test/v1', apiKey: 'sk-other' },
},
models: {
'moonshot-cn/kimi-k2': { provider: 'moonshot-cn', model: 'kimi-k2', maxContextSize: 256000 },
'other/model': { provider: 'other', model: 'other-model', maxContextSize: 1000 },
},
defaultModel: 'moonshot-cn/kimi-k2',
};
removeOpenPlatformConfig(config, 'moonshot-cn');
expect(config.providers['moonshot-cn']).toBeUndefined();
expect(config.providers['other']).toBeDefined();
expect(config.models?.['moonshot-cn/kimi-k2']).toBeUndefined();
expect(config.models?.['other/model']).toBeDefined();
expect(config.defaultModel).toBeUndefined();
});
it('leaves defaultModel intact when it belongs to another provider', () => {
const config: ManagedKimiConfigShape = {
providers: {
'moonshot-cn': { type: 'kimi', baseUrl: 'https://api.moonshot.cn/v1', apiKey: 'sk-test' },
},
models: {
'moonshot-cn/kimi-k2': { provider: 'moonshot-cn', model: 'kimi-k2', maxContextSize: 256000 },
},
defaultModel: 'other/model',
};
removeOpenPlatformConfig(config, 'moonshot-cn');
expect(config.defaultModel).toBe('other/model');
});
});
|