mirror of
https://github.com/element-hq/element-call.git
synced 2026-11-23 00:18:50 +00:00
The package carried the standalone app's IndexedDB store worker: 11 MB with a copy of the Matrix SDK and its crypto wasm, referenced by an absolute path no host could serve, and never used by the component, which takes the host's client. The bundler emits the worker file as soon as it loads the `?worker` import, used or not, and the import sat in the Matrix utilities the component reaches for the room URLs. The client initialisation moves into a module of its own, with the worker import, which only the standalone login, registration and session restore paths import. ClientProvider takes the session loader through a `loadClient` prop instead of importing it itself, since even a dynamic import keeps a module in the package: the standalone app passes a lazy `initSPA` from App.tsx, the component passes nothing, and given neither a client nor a loader no one is signed in. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
135 lines
3.9 KiB
TypeScript
135 lines
3.9 KiB
TypeScript
/*
|
|
Copyright 2026 Element Creations Ltd.
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
Please see LICENSE in the repository root for full details.
|
|
*/
|
|
|
|
import { expect, test, vi } from "vitest";
|
|
import { render, waitFor } from "@testing-library/react";
|
|
import { BrowserRouter } from "react-router-dom";
|
|
import { type MatrixClient } from "matrix-js-sdk";
|
|
import { type FC } from "react";
|
|
|
|
import {
|
|
type ClientLoader,
|
|
ClientProvider,
|
|
useClientState,
|
|
} from "./ClientContext";
|
|
|
|
const mockClient = (userId = "@alice:example.org"): MatrixClient =>
|
|
({
|
|
on: vi.fn(),
|
|
removeListener: vi.fn(),
|
|
getUserId: () => userId,
|
|
getDeviceId: () => "AAAA",
|
|
stopClient: vi.fn(),
|
|
}) as Partial<MatrixClient> as MatrixClient;
|
|
|
|
/** Reports what the context says, so a test can assert on it. */
|
|
const ShowClientState: FC = () => {
|
|
const state = useClientState();
|
|
if (state === undefined) return <span>loading</span>;
|
|
if (state.state === "error") return <span>error</span>;
|
|
return (
|
|
<span>{state.authenticated?.client.getUserId() ?? "unauthenticated"}</span>
|
|
);
|
|
};
|
|
|
|
test("uses a client supplied by the host without waiting", () => {
|
|
const client = mockClient();
|
|
|
|
const { container } = render(
|
|
<BrowserRouter>
|
|
<ClientProvider client={client}>
|
|
<ShowClientState />
|
|
</ClientProvider>
|
|
</BrowserRouter>,
|
|
);
|
|
|
|
// Available on the very first render: a supplied client needs no session
|
|
// restoring, so there is no loading state to pass through.
|
|
expect(container.textContent).toBe("@alice:example.org");
|
|
});
|
|
|
|
test("does not claim exclusive use of storage when given a client", () => {
|
|
// The channel is created when the module loads, so spy on the prototype
|
|
// rather than trying to replace the global.
|
|
const postMessage = vi.spyOn(BroadcastChannel.prototype, "postMessage");
|
|
|
|
render(
|
|
<BrowserRouter>
|
|
<ClientProvider client={mockClient()}>
|
|
<ShowClientState />
|
|
</ClientProvider>
|
|
</BrowserRouter>,
|
|
);
|
|
|
|
// The broadcast shuts down other instances to protect Element Call's own
|
|
// stores. A host's client brings its own, so there is nothing to protect.
|
|
expect(postMessage).not.toHaveBeenCalled();
|
|
|
|
postMessage.mockRestore();
|
|
});
|
|
|
|
test("follows the client when the host swaps it", () => {
|
|
const first = mockClient();
|
|
const second = mockClient("@bob:example.org");
|
|
|
|
const { container, rerender } = render(
|
|
<BrowserRouter>
|
|
<ClientProvider client={first}>
|
|
<ShowClientState />
|
|
</ClientProvider>
|
|
</BrowserRouter>,
|
|
);
|
|
expect(container.textContent).toBe("@alice:example.org");
|
|
|
|
// A host that re-authenticates hands us a new client on a mounted component
|
|
rerender(
|
|
<BrowserRouter>
|
|
<ClientProvider client={second}>
|
|
<ShowClientState />
|
|
</ClientProvider>
|
|
</BrowserRouter>,
|
|
);
|
|
|
|
expect(container.textContent).toBe("@bob:example.org");
|
|
});
|
|
|
|
test("finds a client of its own when the host supplies none", async () => {
|
|
const client = mockClient();
|
|
const loadClient = vi
|
|
.fn<ClientLoader>()
|
|
.mockResolvedValue({ client, passwordlessUser: true });
|
|
|
|
const { container } = render(
|
|
<BrowserRouter>
|
|
<ClientProvider loadClient={loadClient}>
|
|
<ShowClientState />
|
|
</ClientProvider>
|
|
</BrowserRouter>,
|
|
);
|
|
|
|
// Nothing to show until a session has been restored or created
|
|
expect(container.textContent).toBe("loading");
|
|
await waitFor(() => expect(container.textContent).toBe("@alice:example.org"));
|
|
// Given the session store to restore from and to clear on a stale session
|
|
expect(loadClient).toHaveBeenCalledWith(
|
|
expect.any(Function),
|
|
expect.any(Function),
|
|
);
|
|
});
|
|
|
|
test("has no one signed in when given neither a client nor a way to load one", async () => {
|
|
const { container } = render(
|
|
<BrowserRouter>
|
|
<ClientProvider>
|
|
<ShowClientState />
|
|
</ClientProvider>
|
|
</BrowserRouter>,
|
|
);
|
|
|
|
await waitFor(() => expect(container.textContent).toBe("unauthenticated"));
|
|
});
|