适配H5讲解统一详情接口
Some checks failed
CI / verify (push) Has been cancelled

This commit is contained in:
lyf
2026-08-05 14:52:36 +08:00
parent c287ef3a2a
commit 2edb6bd9f2
25 changed files with 946 additions and 1148 deletions

View File

@@ -1,18 +1,6 @@
// @vitest-environment happy-dom
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const repositoryMocks = vi.hoisted(() => ({
getPlayInfo: vi.fn()
}))
vi.mock('@/repositories/AudioPlayInfoRepository', () => ({
audioPlayInfoRepository: {
getPlayInfo: repositoryMocks.getPlayInfo
},
audioReasonToText: () => '当前语言暂无语音讲解'
}))
import { useGlobalAudioPlayer } from '@/composables/useGlobalAudioPlayer'
describe('全局讲解播放器语言切换', () => {
@@ -20,10 +8,7 @@ describe('全局讲解播放器语言切换', () => {
beforeEach(() => {
player.close()
repositoryMocks.getPlayInfo.mockReset()
vi.stubGlobal('uni', {
showToast: vi.fn()
})
vi.stubGlobal('uni', { showToast: vi.fn() })
vi.spyOn(window.HTMLMediaElement.prototype, 'play').mockResolvedValue(undefined)
vi.spyOn(window.HTMLMediaElement.prototype, 'pause').mockImplementation(() => undefined)
vi.spyOn(window.HTMLMediaElement.prototype, 'load').mockImplementation(() => undefined)
@@ -35,18 +20,9 @@ describe('全局讲解播放器语言切换', () => {
vi.unstubAllGlobals()
})
it('切换到粤语时立即中断旧音频,并使用 yue-HK 播放地址', async () => {
repositoryMocks.getPlayInfo.mockResolvedValue({
playable: true,
targetType: 'STOP',
targetId: '1823450596800289',
lang: 'yue-HK',
audioId: 'cantonese-audio',
playUrl: '/museum-assets/audio/cantonese.mp3',
hasText: true,
fallback: false
})
it('切换到粤语时只从当前统一详情的音轨数组选择,不产生额外 HTTP 请求', async () => {
const request = vi.fn()
vi.stubGlobal('uni', { request, showToast: vi.fn() })
await player.play({
id: 'mandarin-audio',
name: '测试讲解',
@@ -54,28 +30,25 @@ describe('全局讲解播放器语言切换', () => {
language: 'zh-CN'
}, {
source: {
targetType: 'STOP',
targetId: '1823450596800289',
lang: 'zh-CN'
stopId: '1823450596800289',
lang: 'zh-CN',
voiceGender: 'male',
audioOptions: [
{ channelCode: 'standard.zh-CN.male', version: 'standard', languageCode: 'zh-CN', gender: 'male', playUrl: '/museum-assets/audio/mandarin.mp3' },
{ channelCode: 'standard.yue-HK.female', version: 'standard', languageCode: 'yue-HK', gender: 'female', playUrl: '/museum-assets/audio/cantonese.mp3' }
]
}
})
const pauseSpy = vi.mocked(window.HTMLMediaElement.prototype.pause)
pauseSpy.mockClear()
const switching = player.switchLanguage('yue-HK')
expect(pauseSpy).toHaveBeenCalledTimes(1)
expect(player.playing.value).toBe(false)
expect(player.currentTime.value).toBe(0)
expect(repositoryMocks.getPlayInfo).toHaveBeenCalledWith(expect.objectContaining({
targetType: 'STOP',
targetId: '1823450596800289',
lang: 'yue-HK',
refresh: true
}))
await expect(switching).resolves.toBe(true)
expect(player.currentSource.value?.lang).toBe('yue-HK')
expect(request).not.toHaveBeenCalled()
expect(player.currentSource.value).toEqual(expect.objectContaining({
stopId: '1823450596800289',
lang: 'yue-HK',
voiceGender: 'female'
}))
expect(player.currentAudio.value).toEqual(expect.objectContaining({
language: 'yue-HK',
audioUrl: '/museum-assets/audio/cantonese.mp3'