|
| 1 | +import { expect, test } from "vite-plus/test"; |
| 2 | +import type { Client } from "bailian-cli-core"; |
| 3 | +import { PipelineError } from "../src/pipeline/errors.ts"; |
| 4 | +import type { PipelineEnv } from "../src/pipeline/bl-config.ts"; |
| 5 | +import { speechRecognize } from "../src/pipeline/steps/bl-api.ts"; |
| 6 | +import type { StepContext } from "../src/pipeline/types.ts"; |
| 7 | + |
| 8 | +type CapturedRequest = { |
| 9 | + path?: string; |
| 10 | + method?: string; |
| 11 | + headers?: Record<string, string>; |
| 12 | + body?: Record<string, unknown>; |
| 13 | + async?: boolean; |
| 14 | +}; |
| 15 | + |
| 16 | +function makeEnv(requestJsonImpl?: (opts: CapturedRequest) => Promise<unknown>): { |
| 17 | + env: PipelineEnv; |
| 18 | + captured: CapturedRequest[]; |
| 19 | +} { |
| 20 | + const captured: CapturedRequest[] = []; |
| 21 | + const client = { |
| 22 | + uploadFile: async (source: string) => source, |
| 23 | + requestJson: async (opts: CapturedRequest) => { |
| 24 | + captured.push(opts); |
| 25 | + if (requestJsonImpl) return requestJsonImpl(opts); |
| 26 | + return { output: { text: "ok" } }; |
| 27 | + }, |
| 28 | + } as unknown as Client; |
| 29 | + |
| 30 | + return { |
| 31 | + env: { |
| 32 | + client, |
| 33 | + settings: { quiet: true, output: "json" } as PipelineEnv["settings"], |
| 34 | + }, |
| 35 | + captured, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +function makeCtx(): StepContext { |
| 40 | + return { dryRun: false, signal: new AbortController().signal }; |
| 41 | +} |
| 42 | + |
| 43 | +test("pipeline speechRecognize routes input-audio flash to sync multimodal endpoint", async () => { |
| 44 | + const { env, captured } = makeEnv(); |
| 45 | + const result = (await speechRecognize( |
| 46 | + env, |
| 47 | + { |
| 48 | + url: "https://example.com/a.wav", |
| 49 | + model: "qwen-audio-3.0-asr-flash", |
| 50 | + language: "en", |
| 51 | + "vocabulary-id": "vocab-1", |
| 52 | + }, |
| 53 | + makeCtx(), |
| 54 | + )) as { mode?: string; text?: string }; |
| 55 | + |
| 56 | + expect(result.mode).toBe("sync"); |
| 57 | + expect(result.text).toBe("ok"); |
| 58 | + expect(captured).toHaveLength(1); |
| 59 | + expect(captured[0]?.path).toBe("/api/v1/services/aigc/multimodal-generation/generation"); |
| 60 | + expect(captured[0]?.headers?.["X-DashScope-SSE"]).toBe("disable"); |
| 61 | + expect(captured[0]?.body).toMatchObject({ |
| 62 | + model: "qwen-audio-3.0-asr-flash", |
| 63 | + parameters: { |
| 64 | + format: "wav", |
| 65 | + language_hints: ["en"], |
| 66 | + vocabulary_id: "vocab-1", |
| 67 | + }, |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +test("pipeline speechRecognize maps qwen3-filetrans language to parameters.language", async () => { |
| 72 | + const { env, captured } = makeEnv(async (opts) => { |
| 73 | + if (opts.async || opts.method === "POST") { |
| 74 | + return { output: { task_id: "task-1", task_status: "PENDING" } }; |
| 75 | + } |
| 76 | + return { |
| 77 | + output: { task_id: "task-1", task_status: "SUCCEEDED", results: [] }, |
| 78 | + request_id: "r1", |
| 79 | + }; |
| 80 | + }); |
| 81 | + |
| 82 | + await speechRecognize( |
| 83 | + env, |
| 84 | + { |
| 85 | + url: "https://example.com/a.wav", |
| 86 | + model: "qwen3-asr-flash-filetrans", |
| 87 | + language: "zh", |
| 88 | + "poll-interval": 0, |
| 89 | + }, |
| 90 | + makeCtx(), |
| 91 | + ); |
| 92 | + |
| 93 | + expect(captured[0]?.path).toBe("/api/v1/services/audio/asr/transcription"); |
| 94 | + expect(captured[0]?.async).toBe(true); |
| 95 | + expect(captured[0]?.body).toMatchObject({ |
| 96 | + model: "qwen3-asr-flash-filetrans", |
| 97 | + input: { file_url: "https://example.com/a.wav" }, |
| 98 | + parameters: { language: "zh" }, |
| 99 | + }); |
| 100 | + expect( |
| 101 | + (captured[0]?.body?.parameters as Record<string, unknown> | undefined)?.language_hints, |
| 102 | + ).toBeUndefined(); |
| 103 | +}); |
| 104 | + |
| 105 | +test("pipeline speechRecognize rejects realtime models before requesting", async () => { |
| 106 | + const { env, captured } = makeEnv(); |
| 107 | + await expect( |
| 108 | + speechRecognize( |
| 109 | + env, |
| 110 | + { url: "https://example.com/a.wav", model: "qwen3-asr-flash-realtime" }, |
| 111 | + makeCtx(), |
| 112 | + ), |
| 113 | + ).rejects.toBeInstanceOf(PipelineError); |
| 114 | + expect(captured).toHaveLength(0); |
| 115 | +}); |
| 116 | + |
| 117 | +test("pipeline speechRecognize rejects multiple urls for sync flash", async () => { |
| 118 | + const { env, captured } = makeEnv(); |
| 119 | + await expect( |
| 120 | + speechRecognize( |
| 121 | + env, |
| 122 | + { |
| 123 | + url: ["https://example.com/a.wav", "https://example.com/b.wav"], |
| 124 | + model: "fun-asr-flash-2026-06-15", |
| 125 | + }, |
| 126 | + makeCtx(), |
| 127 | + ), |
| 128 | + ).rejects.toBeInstanceOf(PipelineError); |
| 129 | + expect(captured).toHaveLength(0); |
| 130 | +}); |
0 commit comments