mirror of
https://github.com/langgenius/dify.git
synced 2026-04-12 22:17:09 +08:00
Signed-off-by: edvatar <88481784+toroleapinc@users.noreply.github.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: majiayu000 <1835304752@qq.com> Co-authored-by: Poojan <poojan@infocusp.com> Co-authored-by: sahil-infocusp <73810410+sahil-infocusp@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Pandaaaa906 <ye.pandaaaa906@gmail.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: heyszt <270985384@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Ijas <ijas.ahmd.ap@gmail.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: 木之本澪 <kinomotomiovo@gmail.com> Co-authored-by: KinomotoMio <200703522+KinomotoMio@users.noreply.github.com> Co-authored-by: 不做了睡大觉 <64798754+stakeswky@users.noreply.github.com> Co-authored-by: User <user@example.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: edvatar <88481784+toroleapinc@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Leilei <138381132+Inlei@users.noreply.github.com> Co-authored-by: HaKu <104669497+haku-ink@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: wangxiaolei <fatelei@gmail.com> Co-authored-by: Varun Chawla <34209028+veeceey@users.noreply.github.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: tda <95275462+tda1017@users.noreply.github.com> Co-authored-by: root <root@DESKTOP-KQLO90N> Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai> Co-authored-by: Niels Kaspers <153818647+nielskaspers@users.noreply.github.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: Tyson Cung <45380903+tysoncung@users.noreply.github.com> Co-authored-by: Stephen Zhou <hi@hyoban.cc> Co-authored-by: FFXN <31929997+FFXN@users.noreply.github.com> Co-authored-by: slegarraga <64795732+slegarraga@users.noreply.github.com> Co-authored-by: 99 <wh2099@pm.me> Co-authored-by: Br1an <932039080@qq.com> Co-authored-by: L1nSn0w <l1nsn0w@qq.com> Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai> Co-authored-by: akkoaya <151345394+akkoaya@users.noreply.github.com> Co-authored-by: 盐粒 Yanli <yanli@dify.ai> Co-authored-by: lif <1835304752@qq.com> Co-authored-by: weiguang li <codingpunk@gmail.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: HanWenbo <124024253+hwb96@users.noreply.github.com> Co-authored-by: Coding On Star <447357187@qq.com> Co-authored-by: CodingOnStar <hanxujiang@dify.com> Co-authored-by: Stable Genius <stablegenius043@gmail.com> Co-authored-by: Stable Genius <259448942+stablegenius49@users.noreply.github.com> Co-authored-by: ふるい <46769295+Echo0ff@users.noreply.github.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
149 lines
5.0 KiB
TypeScript
149 lines
5.0 KiB
TypeScript
import { AudioPlayerManager } from '../audio.player.manager'
|
|
|
|
type AudioCallback = ((event: string) => void) | null
|
|
type AudioPlayerCtorArgs = [
|
|
string,
|
|
boolean,
|
|
string | undefined,
|
|
string | null | undefined,
|
|
string | undefined,
|
|
AudioCallback,
|
|
]
|
|
|
|
type MockAudioPlayerInstance = {
|
|
setCallback: ReturnType<typeof vi.fn>
|
|
pauseAudio: ReturnType<typeof vi.fn>
|
|
resetMsgId: ReturnType<typeof vi.fn>
|
|
cacheBuffers: Array<ArrayBuffer>
|
|
sourceBuffer: {
|
|
abort: ReturnType<typeof vi.fn>
|
|
} | undefined
|
|
}
|
|
|
|
const mockState = vi.hoisted(() => ({
|
|
instances: [] as MockAudioPlayerInstance[],
|
|
}))
|
|
|
|
const mockAudioPlayerConstructor = vi.hoisted(() => vi.fn())
|
|
|
|
const MockAudioPlayer = vi.hoisted(() => {
|
|
return class MockAudioPlayerClass {
|
|
setCallback = vi.fn()
|
|
pauseAudio = vi.fn()
|
|
resetMsgId = vi.fn()
|
|
cacheBuffers = [new ArrayBuffer(1)]
|
|
sourceBuffer = { abort: vi.fn() }
|
|
|
|
constructor(...args: AudioPlayerCtorArgs) {
|
|
mockAudioPlayerConstructor(...args)
|
|
mockState.instances.push(this as unknown as MockAudioPlayerInstance)
|
|
}
|
|
}
|
|
})
|
|
|
|
vi.mock('@/app/components/base/audio-btn/audio', () => ({
|
|
default: MockAudioPlayer,
|
|
}))
|
|
|
|
describe('AudioPlayerManager', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
mockState.instances = []
|
|
Reflect.set(AudioPlayerManager, 'instance', undefined)
|
|
})
|
|
|
|
describe('getInstance', () => {
|
|
it('should return the same singleton instance across calls', () => {
|
|
const first = AudioPlayerManager.getInstance()
|
|
const second = AudioPlayerManager.getInstance()
|
|
|
|
expect(first).toBe(second)
|
|
})
|
|
})
|
|
|
|
describe('getAudioPlayer', () => {
|
|
it('should create a new audio player when no existing player is cached', () => {
|
|
const manager = AudioPlayerManager.getInstance()
|
|
const callback = vi.fn()
|
|
|
|
const result = manager.getAudioPlayer('/text-to-audio', false, 'msg-1', 'hello', 'en-US', callback)
|
|
|
|
expect(mockAudioPlayerConstructor).toHaveBeenCalledTimes(1)
|
|
expect(mockAudioPlayerConstructor).toHaveBeenCalledWith(
|
|
'/text-to-audio',
|
|
false,
|
|
'msg-1',
|
|
'hello',
|
|
'en-US',
|
|
callback,
|
|
)
|
|
expect(result).toBe(mockState.instances[0])
|
|
})
|
|
|
|
it('should reuse existing player and update callback when msg id is unchanged', () => {
|
|
const manager = AudioPlayerManager.getInstance()
|
|
const firstCallback = vi.fn()
|
|
const secondCallback = vi.fn()
|
|
|
|
const first = manager.getAudioPlayer('/text-to-audio', false, 'msg-1', 'hello', 'en-US', firstCallback)
|
|
const second = manager.getAudioPlayer('/ignored', true, 'msg-1', 'ignored', 'fr-FR', secondCallback)
|
|
|
|
expect(mockAudioPlayerConstructor).toHaveBeenCalledTimes(1)
|
|
expect(first).toBe(second)
|
|
expect(mockState.instances[0].setCallback).toHaveBeenCalledTimes(1)
|
|
expect(mockState.instances[0].setCallback).toHaveBeenCalledWith(secondCallback)
|
|
})
|
|
|
|
it('should cleanup existing player and create a new one when msg id changes', () => {
|
|
const manager = AudioPlayerManager.getInstance()
|
|
const callback = vi.fn()
|
|
manager.getAudioPlayer('/text-to-audio', false, 'msg-1', 'hello', 'en-US', callback)
|
|
const previous = mockState.instances[0]
|
|
|
|
const next = manager.getAudioPlayer('/apps/1/text-to-audio', false, 'msg-2', 'world', 'en-US', callback)
|
|
|
|
expect(previous.pauseAudio).toHaveBeenCalledTimes(1)
|
|
expect(previous.cacheBuffers).toEqual([])
|
|
expect(previous.sourceBuffer?.abort).toHaveBeenCalledTimes(1)
|
|
expect(mockAudioPlayerConstructor).toHaveBeenCalledTimes(2)
|
|
expect(next).toBe(mockState.instances[1])
|
|
})
|
|
|
|
it('should swallow cleanup errors and still create a new player', () => {
|
|
const manager = AudioPlayerManager.getInstance()
|
|
const callback = vi.fn()
|
|
manager.getAudioPlayer('/text-to-audio', false, 'msg-1', 'hello', 'en-US', callback)
|
|
const previous = mockState.instances[0]
|
|
previous.pauseAudio.mockImplementation(() => {
|
|
throw new Error('cleanup failure')
|
|
})
|
|
|
|
expect(() => {
|
|
manager.getAudioPlayer('/apps/1/text-to-audio', false, 'msg-2', 'world', 'en-US', callback)
|
|
}).not.toThrow()
|
|
|
|
expect(previous.pauseAudio).toHaveBeenCalledTimes(1)
|
|
expect(mockAudioPlayerConstructor).toHaveBeenCalledTimes(2)
|
|
})
|
|
})
|
|
|
|
describe('resetMsgId', () => {
|
|
it('should forward reset message id to the cached audio player when present', () => {
|
|
const manager = AudioPlayerManager.getInstance()
|
|
const callback = vi.fn()
|
|
manager.getAudioPlayer('/text-to-audio', false, 'msg-1', 'hello', 'en-US', callback)
|
|
|
|
manager.resetMsgId('msg-updated')
|
|
|
|
expect(mockState.instances[0].resetMsgId).toHaveBeenCalledTimes(1)
|
|
expect(mockState.instances[0].resetMsgId).toHaveBeenCalledWith('msg-updated')
|
|
})
|
|
|
|
it('should not throw when resetting message id without an audio player', () => {
|
|
const manager = AudioPlayerManager.getInstance()
|
|
|
|
expect(() => manager.resetMsgId('msg-updated')).not.toThrow()
|
|
})
|
|
})
|
|
})
|