Some checks failed
CI / verify (push) Has been cancelled
- 未加载媒体元数据时默认显示零时长 - 使用音频文件真实媒体时长更新播放器 - 避免使用不准确的接口时长覆盖播放器显示 - 补充音频时长回归测试 Made-with: Proma
78 lines
2.6 KiB
TypeScript
78 lines
2.6 KiB
TypeScript
// @vitest-environment happy-dom
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { useGlobalAudioPlayer } from '@/composables/useGlobalAudioPlayer'
|
|
|
|
describe('全局讲解播放器语言切换', () => {
|
|
const player = useGlobalAudioPlayer()
|
|
|
|
beforeEach(() => {
|
|
player.close()
|
|
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)
|
|
})
|
|
|
|
afterEach(() => {
|
|
player.close()
|
|
vi.restoreAllMocks()
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
it('媒体元数据加载前忽略接口时长,加载后使用音频真实时长', () => {
|
|
const audio = document.createElement('audio')
|
|
vi.spyOn(window, 'Audio').mockImplementation(() => audio)
|
|
|
|
player.prepare({
|
|
id: 'duration-audio',
|
|
name: '时长测试',
|
|
audioUrl: '/museum-assets/audio/duration.mp3',
|
|
duration: 28,
|
|
language: 'zh-CN'
|
|
})
|
|
|
|
expect(player.duration.value).toBe(0)
|
|
|
|
Object.defineProperty(audio, 'duration', { configurable: true, value: 54.974938 })
|
|
audio.dispatchEvent(new Event('loadedmetadata'))
|
|
|
|
expect(player.duration.value).toBeCloseTo(54.974938)
|
|
})
|
|
|
|
it('切换到粤语时只从当前统一详情的音轨数组选择,不产生额外 HTTP 请求', async () => {
|
|
const request = vi.fn()
|
|
vi.stubGlobal('uni', { request, showToast: vi.fn() })
|
|
await player.play({
|
|
id: 'mandarin-audio',
|
|
name: '测试讲解',
|
|
audioUrl: '/museum-assets/audio/mandarin.mp3',
|
|
language: 'zh-CN'
|
|
}, {
|
|
source: {
|
|
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 switching = player.switchLanguage('yue-HK')
|
|
|
|
await expect(switching).resolves.toBe(true)
|
|
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'
|
|
}))
|
|
})
|
|
})
|