Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
177 lines
5.3 KiB
TypeScript
177 lines
5.3 KiB
TypeScript
import { VideoRecorder } from './videoRecorder';
|
|
import { createDeferredPromise } from '../../tests/mocks/utils/createDeferredMockFn';
|
|
|
|
describe('VideoRecorder', () => {
|
|
let mockStream: MediaStream;
|
|
let mockVideoTrack: MediaStreamTrack;
|
|
let mockAudioTrack: MediaStreamTrack;
|
|
let mockVideoElement: HTMLVideoElement;
|
|
let getUserMediaMock: jest.Mock;
|
|
|
|
const createMockStream = (videoTrack?: MediaStreamTrack, audioTrack?: MediaStreamTrack): MediaStream => {
|
|
return {
|
|
getVideoTracks: jest.fn(() => [videoTrack || ({ stop: jest.fn() } as unknown as MediaStreamTrack)]),
|
|
getAudioTracks: jest.fn(() => [audioTrack || ({ stop: jest.fn() } as unknown as MediaStreamTrack)]),
|
|
} as unknown as MediaStream;
|
|
};
|
|
|
|
beforeEach(() => {
|
|
jest.useFakeTimers();
|
|
|
|
mockVideoTrack = {
|
|
stop: jest.fn(),
|
|
} as unknown as MediaStreamTrack;
|
|
|
|
mockAudioTrack = {
|
|
stop: jest.fn(),
|
|
} as unknown as MediaStreamTrack;
|
|
|
|
mockStream = createMockStream(mockVideoTrack, mockAudioTrack);
|
|
|
|
mockVideoElement = document.createElement('video');
|
|
mockVideoElement.load = jest.fn();
|
|
mockVideoElement.play = jest.fn().mockResolvedValue(undefined);
|
|
mockVideoElement.pause = jest.fn();
|
|
|
|
getUserMediaMock = jest.fn();
|
|
|
|
Object.defineProperty(global.navigator, 'mediaDevices', {
|
|
writable: true,
|
|
value: {
|
|
getUserMedia: getUserMediaMock,
|
|
},
|
|
});
|
|
|
|
global.MediaRecorder = {
|
|
isTypeSupported: jest.fn((type: string) => type === 'video/webm'),
|
|
} as any;
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
jest.useRealTimers();
|
|
});
|
|
|
|
describe('Asynchronous start and stop handling', () => {
|
|
it('should stop camera tracks when stop is called before getUserMedia resolves', async () => {
|
|
const streamDeferred = createDeferredPromise<MediaStream>();
|
|
|
|
getUserMediaMock.mockReturnValue(streamDeferred.promise);
|
|
|
|
const callback = jest.fn();
|
|
VideoRecorder.start(mockVideoElement, callback);
|
|
VideoRecorder.stop();
|
|
|
|
streamDeferred.resolve(mockStream);
|
|
await jest.runAllTimersAsync();
|
|
|
|
expect(mockVideoTrack.stop).toHaveBeenCalled();
|
|
expect(mockAudioTrack.stop).toHaveBeenCalled();
|
|
expect(callback).not.toHaveBeenCalledWith(true);
|
|
});
|
|
|
|
it('should not initialize camera when stopped early', async () => {
|
|
const streamDeferred = createDeferredPromise<MediaStream>();
|
|
|
|
getUserMediaMock.mockReturnValue(streamDeferred.promise);
|
|
|
|
VideoRecorder.start(mockVideoElement, jest.fn());
|
|
VideoRecorder.stop();
|
|
|
|
streamDeferred.resolve(mockStream);
|
|
await jest.runAllTimersAsync();
|
|
|
|
expect(VideoRecorder.cameraStarted).toBe(false);
|
|
});
|
|
|
|
it('should handle multiple start/stop cycles', async () => {
|
|
const stream1 = createMockStream();
|
|
const stream2 = createMockStream(mockVideoTrack, mockAudioTrack);
|
|
|
|
getUserMediaMock.mockReturnValueOnce(Promise.resolve(stream1));
|
|
|
|
VideoRecorder.start(mockVideoElement, jest.fn());
|
|
VideoRecorder.stop();
|
|
|
|
const stream2Deferred = createDeferredPromise<MediaStream>();
|
|
getUserMediaMock.mockReturnValueOnce(stream2Deferred.promise);
|
|
|
|
const cb = jest.fn();
|
|
VideoRecorder.start(mockVideoElement, cb);
|
|
|
|
stream2Deferred.resolve(stream2);
|
|
await jest.runAllTimersAsync();
|
|
|
|
expect(cb).toHaveBeenCalledWith(true);
|
|
expect(VideoRecorder.cameraStarted).toBe(true);
|
|
});
|
|
|
|
it('should invalidate pending callbacks from previous start when new start is called', async () => {
|
|
const firstStream = createMockStream();
|
|
const secondStream = createMockStream(mockVideoTrack, mockAudioTrack);
|
|
|
|
const firstDeferred = createDeferredPromise<MediaStream>();
|
|
const secondDeferred = createDeferredPromise<MediaStream>();
|
|
|
|
getUserMediaMock.mockReturnValueOnce(firstDeferred.promise).mockReturnValueOnce(secondDeferred.promise);
|
|
|
|
const cb1 = jest.fn();
|
|
const cb2 = jest.fn();
|
|
|
|
VideoRecorder.start(mockVideoElement, cb1);
|
|
VideoRecorder.start(mockVideoElement, cb2);
|
|
|
|
secondDeferred.resolve(secondStream);
|
|
await jest.runAllTimersAsync();
|
|
firstDeferred.resolve(firstStream);
|
|
await jest.runAllTimersAsync();
|
|
|
|
expect(firstStream.getVideoTracks).toHaveBeenCalled();
|
|
expect(firstStream.getAudioTracks).toHaveBeenCalled();
|
|
expect(cb2).toHaveBeenCalledWith(true);
|
|
expect(cb1).not.toHaveBeenCalledWith(true);
|
|
});
|
|
});
|
|
|
|
describe('Normal operation', () => {
|
|
it('should initialize camera', async () => {
|
|
getUserMediaMock.mockResolvedValue(mockStream);
|
|
|
|
const cb = jest.fn();
|
|
VideoRecorder.start(mockVideoElement, cb);
|
|
|
|
await jest.runAllTimersAsync();
|
|
|
|
expect(cb).toHaveBeenCalledWith(true);
|
|
expect(VideoRecorder.cameraStarted).toBe(true);
|
|
});
|
|
|
|
it('should stop camera tracks', () => {
|
|
(VideoRecorder as any).stream = mockStream;
|
|
(VideoRecorder as any).started = true;
|
|
(VideoRecorder as any)._cameraStarted = true;
|
|
|
|
VideoRecorder.stop();
|
|
|
|
expect(mockVideoTrack.stop).toHaveBeenCalled();
|
|
expect(mockAudioTrack.stop).toHaveBeenCalled();
|
|
expect(VideoRecorder.cameraStarted).toBe(false);
|
|
});
|
|
|
|
it('should return supported mime types', () => {
|
|
expect(VideoRecorder.getSupportedMimeTypes()).toBe('video/webm; codecs=vp8,opus');
|
|
});
|
|
|
|
it('should handle permission errors', async () => {
|
|
getUserMediaMock.mockRejectedValue(new Error('Permission denied'));
|
|
|
|
const cb = jest.fn();
|
|
VideoRecorder.start(mockVideoElement, cb);
|
|
|
|
await jest.runAllTimersAsync();
|
|
|
|
expect(cb).toHaveBeenCalledWith(false);
|
|
});
|
|
});
|
|
});
|