Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✅ test(model-runtime): cover Straico and OpenCode provider configuration (#19948)

AmAzing-2f034f9881a4
diff --git a/packages/model-runtime/src/providers/opencodeCodingPlan/index.test.ts b/packages/model-runtime/src/providers/opencodeCodingPlan/index.test.ts
index 74128ee216f..ddcb0ee1303 100644
--- a/packages/model-runtime/src/providers/opencodeCodingPlan/index.test.ts
+++ b/packages/model-runtime/src/providers/opencodeCodingPlan/index.test.ts
@@ -1,10 +1,12 @@
// @vitest-environment node
import { ModelProvider } from 'model-bank';
import OpenAI from 'openai';
+import { Stream } from 'openai/core/streaming';
import type { Mock } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-import { LobeOpenCodeCodingPlanAI, sanitizeJsonSchema } from './index';
+import * as debugStreamModule from '../../utils/debugStream';
+import { LobeOpenCodeCodingPlanAI, params, sanitizeJsonSchema } from './index';
// The router pulls the cloud model-bank config for deepseek route resolution,
// which transitively imports server-only modules (e.g. redis-client). Stub it
@@ -31,6 +33,93 @@ describe('LobeOpenCodeCodingPlanAI', () => {
expect(instance).toBeInstanceOf(LobeOpenCodeCodingPlanAI);
});
});
+
+ describe('provider configuration', () => {
+ beforeEach(() => {
+ loadModelsMock.mockResolvedValue([]);
+ vi.spyOn(console, 'error').mockImplementation(() => {});
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ vi.unstubAllEnvs();
+ });
+
+ it.each([
+ [undefined, defaultBaseURL, 'https://opencode.ai/zen/go'],
+ [
+ 'https://proxy.example.com/go/v1',
+ 'https://proxy.example.com/go/v1',
+ 'https://proxy.example.com/go',
+ ],
+ [
+ 'https://proxy.example.com/v1/gateway',
+ 'https://proxy.example.com/v1/gateway',
+ 'https://proxy.example.com/v1/gateway',
+ ],
+ ])(
+ 'configures all route endpoints with baseURL=%s',
+ async (baseURL, openaiURL, anthropicURL) => {
+ const routers = await params.routers({ apiKey: 'test', baseURL });
+
+ expect(routers).toEqual([
+ expect.objectContaining({
+ apiType: 'anthropic',
+ options: { apiKey: 'test', baseURL: anthropicURL },
+ }),
+ expect.objectContaining({
+ apiType: 'deepseek',
+ options: { apiKey: 'test', baseURL: openaiURL, sdkType: 'openai' },
+ }),
+ expect.objectContaining({
+ apiType: 'openai',
+ options: { apiKey: 'test', baseURL: openaiURL },
+ }),
+ ]);
+ },
+ );
+
+ it('identifies OpenCode Coding Plan and its default endpoint in API errors', async () => {
+ vi.spyOn(OpenAI.Chat.Completions.prototype, 'create').mockRejectedValue(
+ new OpenAI.APIError(401, { message: 'Unauthorized' }, 'Unauthorized', new Headers()),
+ );
+ const instance = new LobeOpenCodeCodingPlanAI({ apiKey: 'test' });
+
+ await expect(
+ instance.chat({ messages: [{ content: 'Hello', role: 'user' }], model: 'glm-5' }),
+ ).rejects.toMatchObject({
+ endpoint: defaultBaseURL,
+ errorType: 'InvalidProviderAPIKey',
+ provider,
+ });
+ });
+
+ it.each([undefined, '0', '1'])(
+ 'enables streaming debug output only for DEBUG_OPENCODE_GO_CHAT_COMPLETION=1 (%s)',
+ async (value) => {
+ vi.stubEnv('DEBUG_OPENCODE_GO_CHAT_COMPLETION', value);
+ const debugStream = vi.spyOn(debugStreamModule, 'debugStream').mockResolvedValue();
+ vi.spyOn(debugStreamModule, 'debugPayload').mockImplementation(() => {});
+ vi.spyOn(OpenAI.Chat.Completions.prototype, 'create').mockResolvedValue(
+ Stream.fromReadableStream<OpenAI.ChatCompletionChunk>(
+ new ReadableStream({ start: (controller) => controller.close() }),
+ new AbortController(),
+ ),
+ );
+ const instance = new LobeOpenCodeCodingPlanAI({ apiKey: 'test' });
+
+ const response = await instance.chat({
+ messages: [{ content: 'Hello', role: 'user' }],
+ model: 'glm-5',
+ stream: true,
+ });
+ await response.text();
+
+ expect(params.debug.chatCompletion()).toBe(value === '1');
+ expect(debugStream).toHaveBeenCalledTimes(value === '1' ? 1 : 0);
+ },
+ );
+ });
});
describe('sanitizeJsonSchema', () => {
diff --git a/packages/model-runtime/src/providers/straico/index.test.ts b/packages/model-runtime/src/providers/straico/index.test.ts
index ed3b5e293a9..345bf3c888a 100644
--- a/packages/model-runtime/src/providers/straico/index.test.ts
+++ b/packages/model-runtime/src/providers/straico/index.test.ts
@@ -1,6 +1,9 @@
// @vitest-environment node
+import { ModelProvider } from 'model-bank';
+import OpenAI from 'openai';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import * as debugStreamModule from '../../utils/debugStream';
import { LobeStraicoAI } from './index';
const loadModelsMock = vi.hoisted(() => vi.fn().mockResolvedValue([]));
@@ -18,9 +21,81 @@ describe('LobeStraicoAI', () => {
});
afterEach(() => {
+ vi.restoreAllMocks();
+ vi.unstubAllEnvs();
vi.unstubAllGlobals();
});
+ describe('provider configuration', () => {
+ it.each([
+ [undefined, 'https://api.straico.com/v0'],
+ ['https://proxy.example.com/straico/v0', 'https://proxy.example.com/straico/v0'],
+ ])('uses the expected endpoint with baseURL=%s', (baseURL, expectedURL) => {
+ const instance = new LobeStraicoAI({ apiKey: 'test', baseURL });
+
+ expect(instance.baseURL).toBe(expectedURL);
+ expect(instance.client.baseURL).toBe(expectedURL);
+ });
+
+ it('identifies Straico and its endpoint in API errors', async () => {
+ const instance = new LobeStraicoAI({ apiKey: 'test' });
+ vi.spyOn(instance.client.chat.completions, 'create').mockRejectedValue(
+ new OpenAI.APIError(401, { message: 'Unauthorized' }, 'Unauthorized', new Headers()),
+ );
+
+ await expect(
+ instance.chat({
+ messages: [{ content: 'Hello', role: 'user' }],
+ model: 'openai/gpt-4o-mini',
+ }),
+ ).rejects.toMatchObject({
+ endpoint: 'https://api.straico.com/v0',
+ errorType: 'InvalidProviderAPIKey',
+ provider: ModelProvider.Straico,
+ });
+ });
+
+ it.each([undefined, '0', '1'])(
+ 'enables non-streaming debug output only for DEBUG_STRAICO_CHAT_COMPLETION=1 (%s)',
+ async (value) => {
+ vi.stubEnv('DEBUG_STRAICO_CHAT_COMPLETION', value);
+ const debugResponse = vi
+ .spyOn(debugStreamModule, 'debugResponse')
+ .mockImplementation(() => {});
+ vi.spyOn(debugStreamModule, 'debugPayload').mockImplementation(() => {});
+ const instance = new LobeStraicoAI({ apiKey: 'test' });
+ const completion: OpenAI.ChatCompletion = {
+ choices: [
+ {
+ finish_reason: 'stop',
+ index: 0,
+ logprobs: null,
+ message: { content: 'Hi', refusal: null, role: 'assistant' },
+ },
+ ],
+ created: 0,
+ id: 'test-completion',
+ model: 'openai/gpt-4o-mini',
+ object: 'chat.completion',
+ };
+ const create = vi
+ .spyOn(instance.client.chat.completions, 'create')
+ .mockResolvedValue(completion);
+
+ const response = await instance.chat({
+ messages: [{ content: 'Hello', role: 'user' }],
+ model: 'openai/gpt-4o-mini',
+ stream: true,
+ });
+ await response.text();
+
+ expect(create.mock.calls[0][0].stream).toBe(false);
+ expect(debugResponse).toHaveBeenCalledTimes(value === '1' ? 1 : 0);
+ if (value === '1') expect(debugResponse).toHaveBeenCalledWith(completion);
+ },
+ );
+ });
+
describe('models', () => {
it('should throw a regular Error when the API request fails', async () => {
mockFetch.mockResolvedValueOnce(new Response('Unauthorized', { status: 401 }));