-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
VIDCS-2175: Should mitigate against publisher failures via permissions, hardware, or network failures by automatically retrying. (no ghost viewers, if retrying doesn’t work we fail)) #38
Open
cpettet
wants to merge
10
commits into
develop
Choose a base branch
from
cpettet/vidcs-2175-mitigate-publisher-failures
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+354
−32
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2977b19
copy-pasta from my old branch; rebasing was dozens of commits, and wa…
cpettet 92e714d
spying on reload
cpettet 0ec83a6
just typing some type of types for typescript on my keyboard
cpettet 3f3be12
too clever
cpettet 9d3088e
Merge branch 'develop' into cpettet/vidcs-2175-mitigate-publisher-fai…
cpettet c38211b
another test
cpettet 6abf93e
Merge branch 'cpettet/vidcs-2175-mitigate-publisher-failures' of gith…
cpettet 567f4a4
cleanup for usePublisher
cpettet 7f9f037
that was almost a fail
cpettet e3b44d9
last bit of cleanup?
cpettet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
...end/src/Context/PreviewPublisherProvider/usePreviewPublisher/usePreviewPublisher.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { act, cleanup, renderHook } from '@testing-library/react'; | ||
import { afterAll, afterEach, beforeEach, describe, expect, it, Mock, vi } from 'vitest'; | ||
import { initPublisher, Publisher } from '@vonage/client-sdk-video'; | ||
import EventEmitter from 'events'; | ||
import usePreviewPublisher from './usePreviewPublisher'; | ||
import { UserContextType } from '../../user'; | ||
import useUserContext from '../../../hooks/useUserContext'; | ||
import usePermissions, { PermissionsHookType } from '../../../hooks/usePermissions'; | ||
import useDevices from '../../../hooks/useDevices'; | ||
import { AllMediaDevices } from '../../../types'; | ||
import { | ||
allMediaDevices, | ||
defaultAudioDevice, | ||
defaultVideoDevice, | ||
} from '../../../utils/mockData/device'; | ||
import { DEVICE_ACCESS_STATUS } from '../../../utils/constants'; | ||
|
||
vi.mock('@vonage/client-sdk-video'); | ||
vi.mock('../../../hooks/useUserContext.tsx'); | ||
vi.mock('../../../hooks/usePermissions.tsx'); | ||
vi.mock('../../../hooks/useDevices.tsx'); | ||
|
||
const mockUseUserContext = useUserContext as Mock<[], UserContextType>; | ||
const mockUsePermissions = usePermissions as Mock<[], PermissionsHookType>; | ||
const mockUseDevices = useDevices as Mock< | ||
[], | ||
{ allMediaDevices: AllMediaDevices; getAllMediaDevices: () => void } | ||
>; | ||
|
||
const defaultSettings = { | ||
publishAudio: false, | ||
publishVideo: false, | ||
name: '', | ||
blur: false, | ||
noiseSuppression: true, | ||
}; | ||
const mockUserContextWithDefaultSettings = { | ||
user: { | ||
defaultSettings, | ||
}, | ||
} as UserContextType; | ||
|
||
describe('usePreviewPublisher', () => { | ||
const mockPublisher = Object.assign(new EventEmitter(), { | ||
getAudioSource: () => defaultAudioDevice, | ||
getVideoSource: () => defaultVideoDevice, | ||
}) as unknown as Publisher; | ||
const mockedInitPublisher = vi.fn(); | ||
const consoleErrorSpy = vi.spyOn(console, 'error'); | ||
const mockSetAccessStatus = vi.fn(); | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
|
||
mockUseUserContext.mockImplementation(() => mockUserContextWithDefaultSettings); | ||
(initPublisher as Mock).mockImplementation(mockedInitPublisher); | ||
mockUseDevices.mockReturnValue({ | ||
getAllMediaDevices: vi.fn(), | ||
allMediaDevices, | ||
}); | ||
mockUsePermissions.mockReturnValue({ | ||
accessStatus: DEVICE_ACCESS_STATUS.PENDING, | ||
setAccessStatus: mockSetAccessStatus, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
describe('initLocalPublisher', () => { | ||
it('should call initPublisher', () => { | ||
mockedInitPublisher.mockReturnValue(mockPublisher); | ||
(initPublisher as Mock).mockImplementation(mockedInitPublisher); | ||
const { result } = renderHook(() => usePreviewPublisher()); | ||
result.current.initLocalPublisher(); | ||
|
||
expect(mockedInitPublisher).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should log access denied errors', () => { | ||
(initPublisher as Mock).mockImplementation((_, _args, callback) => { | ||
const error = new Error( | ||
"It hit me pretty hard, how there's no kind of sad in this world that will stop it turning." | ||
); | ||
error.name = 'OT_USER_MEDIA_ACCESS_DENIED'; | ||
callback(error); | ||
}); | ||
|
||
const { result } = renderHook(() => usePreviewPublisher()); | ||
act(() => { | ||
result.current.initLocalPublisher(); | ||
}); | ||
expect(consoleErrorSpy).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('on accessDenied', () => { | ||
const nativePermissions = global.navigator.permissions; | ||
const mockQuery = vi.fn(); | ||
let mockedPermissionStatus: { onchange: null | (() => void); status: string }; | ||
|
||
beforeEach(() => { | ||
mockedPermissionStatus = { | ||
onchange: null, | ||
status: 'prompt', | ||
}; | ||
mockQuery.mockResolvedValue(mockedPermissionStatus); | ||
|
||
Object.defineProperty(global.navigator, 'permissions', { | ||
writable: true, | ||
value: { | ||
query: mockQuery, | ||
}, | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
Object.defineProperty(global.navigator, 'permissions', { | ||
writable: true, | ||
value: nativePermissions, | ||
}); | ||
}); | ||
|
||
it('calls setAccessStatus', async () => { | ||
mockedInitPublisher.mockReturnValue(mockPublisher); | ||
(initPublisher as Mock).mockImplementation(mockedInitPublisher); | ||
|
||
const { result } = renderHook(() => usePreviewPublisher()); | ||
|
||
act(() => { | ||
result.current.initLocalPublisher(); | ||
}); | ||
expect(result.current.accessStatus).toBe(DEVICE_ACCESS_STATUS.PENDING); | ||
|
||
act(() => { | ||
// @ts-expect-error We simulate user denying microphone permissions in a browser. | ||
mockPublisher.emit('accessDenied', { | ||
message: 'microphone permission denied during the call', | ||
}); | ||
}); | ||
|
||
expect(mockSetAccessStatus).toBeCalledWith(DEVICE_ACCESS_STATUS.REJECTED); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why TS didn't catch this, but this can be
null
when we see a publishing error. Maybe it's because we specifyerr: unknown
and then use theinstanceof
operator?vonage-video-react-app/frontend/src/Context/PreviewPublisherProvider/usePreviewPublisher/usePreviewPublisher.tsx
Lines 195 to 203 in a0c2287