fix: Increase reliability of media auto actions (e.g. play) (#1934)

Refactors media player handling entirely to make the code more
consistent and less boilerplate. Add testing for media player actions.

- Closes #1921
- Closes #1916
This commit is contained in:
Dermot Duffy
2025-03-03 20:59:01 -08:00
committed by GitHub
parent d4650eae8a
commit 3b77b11196
58 changed files with 1972 additions and 1165 deletions
@@ -1,15 +1,15 @@
import { expect, it, vi } from 'vitest';
import { mock } from 'vitest-mock-extended';
import { MuteAction } from '../../../../src/card-controller/actions/actions/mute';
import { AdvancedCameraCardMediaPlayer } from '../../../../src/types';
import { MediaPlayerController } from '../../../../src/types';
import { createCardAPI, createMediaLoadedInfo } from '../../../test-utils';
it('should handle mute action', async () => {
const api = createCardAPI();
const player = mock<AdvancedCameraCardMediaPlayer>();
const mediaPlayerController = mock<MediaPlayerController>();
vi.mocked(api.getMediaLoadedInfoManager().get).mockReturnValue(
createMediaLoadedInfo({
player: player,
mediaPlayerController,
}),
);
const action = new MuteAction(
@@ -22,5 +22,5 @@ it('should handle mute action', async () => {
await action.execute(api);
expect(player.mute).toBeCalled();
expect(mediaPlayerController.mute).toBeCalled();
});
@@ -1,15 +1,15 @@
import { expect, it, vi } from 'vitest';
import { mock } from 'vitest-mock-extended';
import { PauseAction } from '../../../../src/card-controller/actions/actions/pause';
import { AdvancedCameraCardMediaPlayer } from '../../../../src/types';
import { MediaPlayerController } from '../../../../src/types';
import { createCardAPI, createMediaLoadedInfo } from '../../../test-utils';
it('should handle pause action', async () => {
const api = createCardAPI();
const player = mock<AdvancedCameraCardMediaPlayer>();
const mediaPlayerController = mock<MediaPlayerController>();
vi.mocked(api.getMediaLoadedInfoManager().get).mockReturnValue(
createMediaLoadedInfo({
player: player,
mediaPlayerController,
}),
);
const action = new PauseAction(
@@ -22,5 +22,5 @@ it('should handle pause action', async () => {
await action.execute(api);
expect(player.pause).toBeCalled();
expect(mediaPlayerController.pause).toBeCalled();
});
@@ -1,15 +1,15 @@
import { expect, it, vi } from 'vitest';
import { mock } from 'vitest-mock-extended';
import { PlayAction } from '../../../../src/card-controller/actions/actions/play';
import { AdvancedCameraCardMediaPlayer } from '../../../../src/types';
import { MediaPlayerController } from '../../../../src/types';
import { createCardAPI, createMediaLoadedInfo } from '../../../test-utils';
it('should handle play action', async () => {
const api = createCardAPI();
const player = mock<AdvancedCameraCardMediaPlayer>();
const mediaPlayerController = mock<MediaPlayerController>();
vi.mocked(api.getMediaLoadedInfoManager().get).mockReturnValue(
createMediaLoadedInfo({
player: player,
mediaPlayerController,
}),
);
const action = new PlayAction(
@@ -22,5 +22,5 @@ it('should handle play action', async () => {
await action.execute(api);
expect(player.play).toBeCalled();
expect(mediaPlayerController.play).toBeCalled();
});
@@ -1,15 +1,15 @@
import { expect, it, vi } from 'vitest';
import { mock } from 'vitest-mock-extended';
import { UnmuteAction } from '../../../../src/card-controller/actions/actions/unmute';
import { AdvancedCameraCardMediaPlayer } from '../../../../src/types';
import { MediaPlayerController } from '../../../../src/types';
import { createCardAPI, createMediaLoadedInfo } from '../../../test-utils';
it('should handle unmute action', async () => {
const api = createCardAPI();
const player = mock<AdvancedCameraCardMediaPlayer>();
const mediaPlayerController = mock<MediaPlayerController>();
vi.mocked(api.getMediaLoadedInfoManager().get).mockReturnValue(
createMediaLoadedInfo({
player: player,
mediaPlayerController,
}),
);
const action = new UnmuteAction(
@@ -22,5 +22,5 @@ it('should handle unmute action', async () => {
await action.execute(api);
expect(player.unmute).toBeCalled();
expect(mediaPlayerController.unmute).toBeCalled();
});