|
| 1 | +/* |
| 2 | +Copyright 2025 New Vector Ltd. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | +Please see LICENSE in the repository root for full details. |
| 6 | +*/ |
| 7 | + |
| 8 | +import { describe, expect, test, vi } from "vitest"; |
| 9 | +import { render, screen } from "@testing-library/react"; |
| 10 | +import { |
| 11 | + type FC, |
| 12 | + type ReactElement, |
| 13 | + type ReactNode, |
| 14 | + useCallback, |
| 15 | + useState, |
| 16 | +} from "react"; |
| 17 | +import { BrowserRouter } from "react-router-dom"; |
| 18 | +import userEvent from "@testing-library/user-event"; |
| 19 | + |
| 20 | +import { |
| 21 | + type CallErrorRecoveryAction, |
| 22 | + GroupCallErrorBoundary, |
| 23 | +} from "./GroupCallErrorBoundary.tsx"; |
| 24 | +import { |
| 25 | + ConnectionLostError, |
| 26 | + E2EENotSupportedError, |
| 27 | + type ElementCallError, |
| 28 | + InsufficientCapacityError, |
| 29 | + MatrixRTCFocusMissingError, |
| 30 | + UnknownCallError, |
| 31 | +} from "../utils/errors.ts"; |
| 32 | +import { mockConfig } from "../utils/test.ts"; |
| 33 | + |
| 34 | +test.each([ |
| 35 | + { |
| 36 | + error: new MatrixRTCFocusMissingError("example.com"), |
| 37 | + expectedTitle: "Call is not supported", |
| 38 | + }, |
| 39 | + { |
| 40 | + error: new ConnectionLostError(), |
| 41 | + expectedTitle: "Connection lost", |
| 42 | + expectedDescription: "You were disconnected from the call.", |
| 43 | + }, |
| 44 | + { |
| 45 | + error: new E2EENotSupportedError(), |
| 46 | + expectedTitle: "Incompatible browser", |
| 47 | + expectedDescription: |
| 48 | + "Your web browser does not support encrypted calls. Supported browsers include Chrome, Safari, and Firefox 117+.", |
| 49 | + }, |
| 50 | + { |
| 51 | + error: new InsufficientCapacityError(), |
| 52 | + expectedTitle: "Insufficient capacity", |
| 53 | + expectedDescription: |
| 54 | + "The server has reached its maximum capacity and you cannot join the call at this time. Try again later, or contact your server admin if the problem persists.", |
| 55 | + }, |
| 56 | +])( |
| 57 | + "should report correct error for $expectedTitle", |
| 58 | + async ({ error, expectedTitle, expectedDescription }) => { |
| 59 | + const TestComponent = (): ReactNode => { |
| 60 | + throw error; |
| 61 | + }; |
| 62 | + |
| 63 | + const onErrorMock = vi.fn(); |
| 64 | + const { asFragment } = render( |
| 65 | + <BrowserRouter> |
| 66 | + <GroupCallErrorBoundary |
| 67 | + onError={onErrorMock} |
| 68 | + recoveryActionHandler={vi.fn()} |
| 69 | + > |
| 70 | + <TestComponent /> |
| 71 | + </GroupCallErrorBoundary> |
| 72 | + </BrowserRouter>, |
| 73 | + ); |
| 74 | + |
| 75 | + await screen.findByText(expectedTitle); |
| 76 | + if (expectedDescription) { |
| 77 | + expect(screen.queryByText(expectedDescription)).toBeInTheDocument(); |
| 78 | + } |
| 79 | + expect(onErrorMock).toHaveBeenCalledWith(error); |
| 80 | + |
| 81 | + expect(asFragment()).toMatchSnapshot(); |
| 82 | + }, |
| 83 | +); |
| 84 | + |
| 85 | +test("should render the error page with link back to home", async () => { |
| 86 | + const error = new MatrixRTCFocusMissingError("example.com"); |
| 87 | + const TestComponent = (): ReactNode => { |
| 88 | + throw error; |
| 89 | + }; |
| 90 | + |
| 91 | + const onErrorMock = vi.fn(); |
| 92 | + const { asFragment } = render( |
| 93 | + <BrowserRouter> |
| 94 | + <GroupCallErrorBoundary |
| 95 | + onError={onErrorMock} |
| 96 | + recoveryActionHandler={vi.fn()} |
| 97 | + > |
| 98 | + <TestComponent /> |
| 99 | + </GroupCallErrorBoundary> |
| 100 | + </BrowserRouter>, |
| 101 | + ); |
| 102 | + |
| 103 | + await screen.findByText("Call is not supported"); |
| 104 | + expect(screen.getByText(/Domain: example\.com/i)).toBeInTheDocument(); |
| 105 | + expect( |
| 106 | + screen.getByText(/Error Code: MISSING_MATRIX_RTC_FOCUS/i), |
| 107 | + ).toBeInTheDocument(); |
| 108 | + |
| 109 | + await screen.findByRole("button", { name: "Return to home screen" }); |
| 110 | + |
| 111 | + expect(onErrorMock).toHaveBeenCalledOnce(); |
| 112 | + expect(onErrorMock).toHaveBeenCalledWith(error); |
| 113 | + |
| 114 | + expect(asFragment()).toMatchSnapshot(); |
| 115 | +}); |
| 116 | + |
| 117 | +test("ConnectionLostError: Action handling should reset error state", async () => { |
| 118 | + const user = userEvent.setup(); |
| 119 | + |
| 120 | + const TestComponent: FC<{ fail: boolean }> = ({ fail }): ReactNode => { |
| 121 | + if (fail) { |
| 122 | + throw new ConnectionLostError(); |
| 123 | + } |
| 124 | + return <div>HELLO</div>; |
| 125 | + }; |
| 126 | + |
| 127 | + const reconnectCallbackSpy = vi.fn(); |
| 128 | + |
| 129 | + const WrapComponent = (): ReactNode => { |
| 130 | + const [failState, setFailState] = useState(true); |
| 131 | + const reconnectCallback = useCallback( |
| 132 | + (action: CallErrorRecoveryAction) => { |
| 133 | + reconnectCallbackSpy(action); |
| 134 | + setFailState(false); |
| 135 | + }, |
| 136 | + [setFailState], |
| 137 | + ); |
| 138 | + |
| 139 | + return ( |
| 140 | + <BrowserRouter> |
| 141 | + <GroupCallErrorBoundary recoveryActionHandler={reconnectCallback}> |
| 142 | + <TestComponent fail={failState} /> |
| 143 | + </GroupCallErrorBoundary> |
| 144 | + </BrowserRouter> |
| 145 | + ); |
| 146 | + }; |
| 147 | + |
| 148 | + const { asFragment } = render(<WrapComponent />); |
| 149 | + |
| 150 | + // Should fail first |
| 151 | + await screen.findByText("Connection lost"); |
| 152 | + await screen.findByRole("button", { name: "Reconnect" }); |
| 153 | + await screen.findByRole("button", { name: "Return to home screen" }); |
| 154 | + |
| 155 | + expect(asFragment()).toMatchSnapshot(); |
| 156 | + |
| 157 | + await user.click(screen.getByRole("button", { name: "Reconnect" })); |
| 158 | + |
| 159 | + // reconnect should have reset the error, thus rendering should be ok |
| 160 | + await screen.findByText("HELLO"); |
| 161 | + |
| 162 | + expect(reconnectCallbackSpy).toHaveBeenCalledOnce(); |
| 163 | + expect(reconnectCallbackSpy).toHaveBeenCalledWith("reconnect"); |
| 164 | +}); |
| 165 | + |
| 166 | +describe("Rageshake button", () => { |
| 167 | + function setupTest(testError: ElementCallError): void { |
| 168 | + mockConfig({ |
| 169 | + rageshake: { |
| 170 | + submit_url: "https://rageshake.example.com.localhost", |
| 171 | + }, |
| 172 | + }); |
| 173 | + |
| 174 | + const TestComponent = (): ReactElement => { |
| 175 | + throw testError; |
| 176 | + }; |
| 177 | + |
| 178 | + render( |
| 179 | + <BrowserRouter> |
| 180 | + <GroupCallErrorBoundary |
| 181 | + onError={vi.fn()} |
| 182 | + recoveryActionHandler={vi.fn()} |
| 183 | + > |
| 184 | + <TestComponent /> |
| 185 | + </GroupCallErrorBoundary> |
| 186 | + </BrowserRouter>, |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + test("should show send rageshake button for unknown errors", () => { |
| 191 | + setupTest(new UnknownCallError(new Error("FOO"))); |
| 192 | + |
| 193 | + expect( |
| 194 | + screen.queryByRole("button", { name: "Send debug logs" }), |
| 195 | + ).toBeInTheDocument(); |
| 196 | + }); |
| 197 | + |
| 198 | + test("should not show send rageshake button for call errors", () => { |
| 199 | + setupTest(new E2EENotSupportedError()); |
| 200 | + |
| 201 | + expect( |
| 202 | + screen.queryByRole("button", { name: "Send debug logs" }), |
| 203 | + ).not.toBeInTheDocument(); |
| 204 | + }); |
| 205 | +}); |
0 commit comments