import { afterEach, describe, expect, it, vi } from 'vitest' import { Context } from '@deepseek-ai/cordis' import LlmRuntime from '@deepseek-ai/dsh-llm' import * as LlmPiAi from '@deepseek-ai/dsh-llm-pi-ai' import type { Config, PiAiProviderProfile } from '@deepseek-ai/dsh-llm-pi-ai' import { bundleProviders, chatCompletionEvents, collectStream, mockSseServer, responseEvents, } from './support.ts' const contexts: Context[] = [] afterEach(async () => { vi.unstubAllEnvs() await Promise.all(contexts.splice(0).map(ctx => ctx.fiber.dispose())) }) async function harness(providers: Record): Promise { const ctx = new Context() contexts.push(ctx) await ctx.plugin(LlmRuntime) await ctx.plugin(LlmPiAi, { providers }) return ctx } function localProfile(profile: PiAiProviderProfile, baseURL: string): PiAiProviderProfile { return { ...profile, baseURL } } describe('DSH cloud model provider bundle', () => { it('declares one installable bundle layer whose profiles pass the published adapter schema', async () => { const manifest = (await import('../package.json', { with: { type: 'json' } })).default expect(manifest.dsh.bundle.patch).toBe('./cordis.patch.yml') const providers = bundleProviders() expect(Object.keys(providers)).toEqual(['ant-maas-responses', 'ant-maas-chat', 'nvidia']) expect(() => LlmPiAi.Config({ providers })).not.toThrow() const ctx = await harness(providers) expect(ctx.llm.listProviders().map(provider => provider.id)).toEqual([ 'ant-maas-responses', 'ant-maas-chat', 'nvidia', ]) await expect(ctx.llm.resolveModelInfo('ant-maas-responses', 'lingdt-3.0-flash')) .resolves.toMatchObject({ provider: 'ant-maas-responses', id: 'lingdt-3.0-flash' }) await expect(ctx.llm.resolveModelInfo('ant-maas-chat', 'qwen3.6-plus')) .resolves.toMatchObject({ provider: 'ant-maas-chat', id: 'qwen3.6-plus' }) }) it.each([ ['ant-maas-chat', 'qwen3.6-plus'], ['nvidia', 'nvidia/nemotron-3-super-120b-a12b'], ] as const)('streams %s Chat Completions SSE through the production DSH adapter', async (provider, model) => { const server = await mockSseServer(chatCompletionEvents) try { const source = bundleProviders()[provider] if (source === undefined) throw new Error(`missing bundle provider ${provider}`) const env = source.apiKeyEnv if (env === undefined) throw new Error(`provider ${provider} has no credential reference`) vi.stubEnv(env, 'test-secret') const ctx = await harness({ [provider]: localProfile(source, `${server.url}/v1`) }) const result = await collectStream(ctx, provider, model) expect(result.text).toBe('hello') expect(result.finish).toBe('stop') expect(result.chunks.map(chunk => chunk.type)).toEqual([ 'block-start', 'text-delta', 'block-end', 'usage', 'finish', ]) expect(server.paths).toEqual(['/v1/chat/completions']) expect(server.requests[0]).toMatchObject({ model, stream: true }) expect(server.headers[0]?.authorization).toBe('Bearer test-secret') } finally { await server.close() } }) it('streams the MaaS Responses route through the production DSH adapter', async () => { const server = await mockSseServer(responseEvents) try { const source = bundleProviders()['ant-maas-responses'] if (source === undefined) throw new Error('missing MaaS Responses profile') vi.stubEnv('MAAS_API_KEY', 'test-secret') const providers: Config['providers'] = { 'ant-maas-responses': localProfile(source, `${server.url}/v1`), } const ctx = await harness(providers ?? {}) const result = await collectStream(ctx, 'ant-maas-responses', 'lingdt-3.0-flash') expect(result.text).toBe('hello') expect(result.finish).toBe('stop') expect(result.chunks.map(chunk => chunk.type)).toEqual([ 'block-start', 'text-delta', 'block-end', 'usage', 'finish', ]) expect(server.paths).toEqual(['/v1/responses']) expect(server.requests[0]).toMatchObject({ model: 'lingdt-3.0-flash', stream: true }) expect(server.headers[0]?.authorization).toBe('Bearer test-secret') } finally { await server.close() } }) })