import React from 'react'; import {render,screen,fireEvent,waitFor,cleanup} from '@testing-library/react'; import {vi,beforeEach,afterEach,it,expect} from 'vitest'; import PodcastInlinePlayer from '../PodcastInlinePlayer'; vi.mock('../SafeImage',()=>({default:({alt}:{alt:string})=>{alt}/})); vi.mock('../../lib/media',()=>({getMediaUrl:(s:string)=>s})); const podcast={audioUrl:'/test.mp3',title:'Clinical audio',description:'d'.repeat(140)}; beforeEach(()=>{vi.spyOn(HTMLMediaElement.prototype,'play').mockResolvedValue();vi.spyOn(HTMLMediaElement.prototype,'pause').mockImplementation(()=>{});}); afterEach(()=>{cleanup();vi.restoreAllMocks();}); function setup(){const v=render();const audio=v.container.querySelector('audio')!;Object.defineProperty(audio,'duration',{configurable:true,value:100});return {audio,...v};} it('plays, pauses and ends',()=>{const {audio}=setup();fireEvent.click(screen.getByTitle('پخش'));expect(audio.play).toHaveBeenCalled();Object.defineProperty(audio,'paused',{configurable:true,value:false});fireEvent.click(screen.getByTitle('توقف'));expect(audio.pause).toHaveBeenCalled();fireEvent.play(audio);fireEvent.ended(audio);expect(screen.getByTitle('پخش')).toBeInTheDocument();}); it('does not claim playback when play rejects',async()=>{vi.mocked(HTMLMediaElement.prototype.play).mockRejectedValueOnce(new Error('blocked'));vi.spyOn(console,'error').mockImplementation(()=>{});setup();fireEvent.click(screen.getByTitle('پخش'));await waitFor(()=>expect(screen.getByTitle('پخش')).toBeInTheDocument());}); it('seeks waveform and clamps skip boundaries',()=>{const {audio}=setup();fireEvent.loadedMetadata(audio);const wave=screen.getByTitle('برای رفتن به زمان مورد نظر کلیک کنید');vi.spyOn(wave,'getBoundingClientRect').mockReturnValue({left:10,width:200} as DOMRect);fireEvent.click(wave,{clientX:110});expect(audio.currentTime).toBe(50);audio.currentTime=98;fireEvent.click(screen.getByTitle('۱۵ ثانیه جلو'));expect(audio.currentTime).toBe(100);audio.currentTime=2;fireEvent.click(screen.getByTitle('۱۵ ثانیه عقب'));expect(audio.currentTime).toBe(0);}); it('ignores seeking before metadata and shows elapsed time',()=>{const {audio}=setup();fireEvent.click(screen.getByTitle('برای رفتن به زمان مورد نظر کلیک کنید'),{clientX:100});expect(audio.currentTime).toBe(0);audio.currentTime=65;fireEvent.timeUpdate(audio);expect(screen.getByText('01:05')).toBeInTheDocument();}); it('changes speed and dismisses rate menu outside',()=>{const {audio}=setup();fireEvent.click(screen.getByTitle('سرعت پخش'));fireEvent.click(screen.getByText('1.5x'));expect(audio.playbackRate).toBe(1.5);fireEvent.click(screen.getByTitle('سرعت پخش'));fireEvent.mouseDown(document.body);expect(screen.queryByText('2.5x')).not.toBeInTheDocument();}); it('controls volume and mute',()=>{const {audio}=setup();fireEvent.change(screen.getByRole('slider'),{target:{value:'0'}});expect(audio.muted).toBe(true);fireEvent.change(screen.getByRole('slider'),{target:{value:'0.6'}});expect(audio.volume).toBe(0.6);expect(audio.muted).toBe(false);fireEvent.click(screen.getByTitle('بی‌صدا'));expect(audio.muted).toBe(true);}); it('expands and collapses description',()=>{setup();fireEvent.click(screen.getByText('مشاهده متن کامل'));expect(screen.getByText(podcast.description)).toBeInTheDocument();fireEvent.click(screen.getByText('بستن متن'));expect(screen.queryByText(podcast.description)).not.toBeInTheDocument();}); it('uses clipboard if native sharing absent',async()=>{Object.defineProperty(navigator,'share',{configurable:true,value:undefined});const writeText=vi.fn().mockResolvedValue(undefined);Object.defineProperty(navigator,'clipboard',{configurable:true,value:{writeText}});setup();fireEvent.click(screen.getByTitle('اشتراک‌گذاری'));await waitFor(()=>expect(writeText).toHaveBeenCalledWith(window.location.href));});