Files
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

119 lines
4.2 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('../src/services/llmService', () => ({
callWithAudio: vi.fn(),
hasRuntimeConfig: vi.fn(),
}));
vi.mock('../src/services/promptRegistryService', () => ({
resolvePrompt: vi.fn(async () => ({ systemPrompt: 'stub-fallback-prompt' })),
}));
import { callWithAudio, hasRuntimeConfig } from '../src/services/llmService';
import { resolvePrompt } from '../src/services/promptRegistryService';
import {
isSpeechConfigured,
isVoiceTranscriptionConfigured,
transcribeAudioBase64,
} from '../src/services/speechService';
/** 豆包 ASR 成功响应 */
function asrSuccess(text = '西红柿五斤土豆三斤') {
return new Response(JSON.stringify({ result: { text } }), {
status: 200,
headers: { 'X-Api-Status-Code': '20000000' },
});
}
/** 豆包 ASR 拒绝:key 失效(生产实测就是这个码) */
function asrInvalidKey() {
return new Response(JSON.stringify({ header: { code: 45000010, message: 'Invalid X-Api-Key' } }), {
status: 401,
headers: { 'X-Api-Status-Code': '45000010' },
});
}
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
describe('speechService:ASR 优先 + Ark 多模态兜底', () => {
beforeEach(() => {
vi.clearAllMocks();
delete process.env.SPEECH_API_KEY;
delete process.env.SPEECH_APP_KEY;
delete process.env.SPEECH_ACCESS_KEY;
vi.mocked(hasRuntimeConfig).mockResolvedValue(true);
vi.mocked(callWithAudio).mockResolvedValue('西红柿五斤');
});
it('配了 ASR:走 ASR,不触发兜底', async () => {
process.env.SPEECH_API_KEY = 'test-asr-key';
const fetchMock = vi.fn(async () => asrSuccess());
vi.stubGlobal('fetch', fetchMock);
const result = await transcribeAudioBase64('QUJD', 'wav');
expect(result.text).toBe('西红柿五斤土豆三斤');
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(callWithAudio).not.toHaveBeenCalled();
});
it('ASR key 失效(401/45000010):回落 Ark 兜底', async () => {
process.env.SPEECH_API_KEY = 'stale-key';
vi.stubGlobal('fetch', vi.fn(async () => asrInvalidKey()));
vi.mocked(callWithAudio).mockResolvedValue('```\n阿珂五斤\n```');
const result = await transcribeAudioBase64('QUJD', 'wav', { branchCode: 'BR002' });
expect(result.text).toBe('阿珂五斤');
expect(resolvePrompt).toHaveBeenCalledWith('speech.transcribe.ark_fallback', {
branchCode: 'BR002',
});
expect(callWithAudio).toHaveBeenCalledWith(
'stub-fallback-prompt',
'QUJD',
'wav',
{ branchCode: 'BR002' }
);
expect(warnSpy).toHaveBeenCalled();
});
it('未配 ASR:直接用 Ark 兜底(开发环境当前形态)', async () => {
const fetchMock = vi.fn(async () => asrSuccess());
vi.stubGlobal('fetch', fetchMock);
const result = await transcribeAudioBase64('QUJD', 'mp3');
expect(result.text).toBe('西红柿五斤');
expect(fetchMock).not.toHaveBeenCalled();
});
it('ASR 失败且 Ark 不可用:抛原始 ASR 错误(保留状态码)', async () => {
process.env.SPEECH_API_KEY = 'stale-key';
vi.stubGlobal('fetch', vi.fn(async () => asrInvalidKey()));
vi.mocked(hasRuntimeConfig).mockResolvedValue(false);
await expect(transcribeAudioBase64('QUJD', 'wav')).rejects.toThrow(/45000010/);
});
it('两边都不可用:提示配置 SPEECH_API_KEY 或 ARK_API_KEY', async () => {
vi.stubGlobal('fetch', vi.fn(async () => asrSuccess()));
vi.mocked(hasRuntimeConfig).mockResolvedValue(false);
vi.mocked(callWithAudio).mockResolvedValue(null);
expect(isSpeechConfigured()).toBe(false);
await expect(transcribeAudioBase64('QUJD', 'wav')).rejects.toThrow(/SPEECH_API_KEY/);
});
it('可用性判定:ASR 或 Ark 任一可用即为 true', async () => {
vi.mocked(hasRuntimeConfig).mockResolvedValue(false);
await expect(isVoiceTranscriptionConfigured()).resolves.toBe(false);
vi.mocked(hasRuntimeConfig).mockResolvedValue(true);
await expect(isVoiceTranscriptionConfigured()).resolves.toBe(true);
process.env.SPEECH_API_KEY = 'test-asr-key';
vi.mocked(hasRuntimeConfig).mockResolvedValue(false);
await expect(isVoiceTranscriptionConfigured()).resolves.toBe(true);
});
});