remove duplicated connecting state and update Test setup

This commit is contained in:
Timo K
2025-12-11 16:04:12 +01:00
parent 328cc7133a
commit 08306d663a
2 changed files with 35 additions and 27 deletions

View File

@@ -50,11 +50,6 @@ let fakeLivekitRoom: MockedObject<LivekitRoom>;
let localParticipantEventEmiter: EventEmitter; let localParticipantEventEmiter: EventEmitter;
let fakeLocalParticipant: MockedObject<LocalParticipant>; let fakeLocalParticipant: MockedObject<LocalParticipant>;
let fakeRoomEventEmiter: EventEmitter;
// let fakeMembershipsFocusMap$: BehaviorSubject<
// { membership: CallMembership; transport: LivekitTransport }[]
// >;
const livekitFocus: LivekitTransport = { const livekitFocus: LivekitTransport = {
livekit_alias: "!roomID:example.org", livekit_alias: "!roomID:example.org",
livekit_service_url: "https://matrix-rtc.example.org/livekit/jwt", livekit_service_url: "https://matrix-rtc.example.org/livekit/jwt",
@@ -91,22 +86,25 @@ function setupTest(): void {
localParticipantEventEmiter, localParticipantEventEmiter,
), ),
} as unknown as LocalParticipant); } as unknown as LocalParticipant);
fakeRoomEventEmiter = new EventEmitter();
const fakeRoomEventEmitter = new EventEmitter();
fakeLivekitRoom = vi.mocked<LivekitRoom>({ fakeLivekitRoom = vi.mocked<LivekitRoom>({
connect: vi.fn(), connect: vi.fn(),
disconnect: vi.fn(), disconnect: vi.fn(),
remoteParticipants: new Map(), remoteParticipants: new Map(),
localParticipant: fakeLocalParticipant, localParticipant: fakeLocalParticipant,
state: LivekitConnectionState.Disconnected, state: LivekitConnectionState.Disconnected,
on: fakeRoomEventEmiter.on.bind(fakeRoomEventEmiter), on: fakeRoomEventEmitter.on.bind(fakeRoomEventEmitter),
off: fakeRoomEventEmiter.off.bind(fakeRoomEventEmiter), off: fakeRoomEventEmitter.off.bind(fakeRoomEventEmitter),
addListener: fakeRoomEventEmiter.addListener.bind(fakeRoomEventEmiter), addListener: fakeRoomEventEmitter.addListener.bind(fakeRoomEventEmitter),
removeListener: removeListener:
fakeRoomEventEmiter.removeListener.bind(fakeRoomEventEmiter), fakeRoomEventEmitter.removeListener.bind(fakeRoomEventEmitter),
removeAllListeners: removeAllListeners:
fakeRoomEventEmiter.removeAllListeners.bind(fakeRoomEventEmiter), fakeRoomEventEmitter.removeAllListeners.bind(fakeRoomEventEmitter),
setE2EEEnabled: vi.fn().mockResolvedValue(undefined), setE2EEEnabled: vi.fn().mockResolvedValue(undefined),
emit: (eventName: string | symbol, ...args: unknown[]) => {
fakeRoomEventEmitter.emit(eventName, ...args);
},
} as unknown as LivekitRoom); } as unknown as LivekitRoom);
} }
@@ -129,7 +127,13 @@ function setupRemoteConnection(): Connection {
}); });
fakeLivekitRoom.connect.mockImplementation(async (): Promise<void> => { fakeLivekitRoom.connect.mockImplementation(async (): Promise<void> => {
const changeEv = RoomEvent.ConnectionStateChanged;
fakeLivekitRoom.state = LivekitConnectionState.Connecting;
fakeLivekitRoom.emit(changeEv, fakeLivekitRoom.state);
fakeLivekitRoom.state = LivekitConnectionState.Connected; fakeLivekitRoom.state = LivekitConnectionState.Connected;
fakeLivekitRoom.emit(changeEv, fakeLivekitRoom.state);
return Promise.resolve(); return Promise.resolve();
}); });
@@ -350,8 +354,10 @@ describe("Start connection states", () => {
expect(initialState).toEqual(ConnectionState.Initialized); expect(initialState).toEqual(ConnectionState.Initialized);
const fetchingState = capturedStates.shift(); const fetchingState = capturedStates.shift();
expect(fetchingState).toEqual(ConnectionState.FetchingConfig); expect(fetchingState).toEqual(ConnectionState.FetchingConfig);
const disconnectedState = capturedStates.shift();
expect(disconnectedState).toEqual(ConnectionState.LivekitDisconnected);
const connectingState = capturedStates.shift(); const connectingState = capturedStates.shift();
expect(connectingState).toEqual(ConnectionState.ConnectingToLkRoom); expect(connectingState).toEqual(ConnectionState.LivekitConnecting);
const connectedState = capturedStates.shift(); const connectedState = capturedStates.shift();
expect(connectedState).toEqual(ConnectionState.LivekitConnected); expect(connectedState).toEqual(ConnectionState.LivekitConnected);
}); });
@@ -419,7 +425,7 @@ describe("Publishing participants observations", () => {
); );
participants.forEach((p) => participants.forEach((p) =>
fakeRoomEventEmiter.emit(RoomEvent.ParticipantConnected, p), fakeLivekitRoom.emit(RoomEvent.ParticipantConnected, p),
); );
// At this point there should be no publishers // At this point there should be no publishers
@@ -432,7 +438,7 @@ describe("Publishing participants observations", () => {
fakeRemoteLivekitParticipant("@dan:example.org:DEV333", 2), fakeRemoteLivekitParticipant("@dan:example.org:DEV333", 2),
]; ];
participants.forEach((p) => participants.forEach((p) =>
fakeRoomEventEmiter.emit(RoomEvent.ParticipantConnected, p), fakeLivekitRoom.emit(RoomEvent.ParticipantConnected, p),
); );
// At this point there should be no publishers // At this point there should be no publishers
@@ -462,7 +468,7 @@ describe("Publishing participants observations", () => {
); );
for (const participant of participants) { for (const participant of participants) {
fakeRoomEventEmiter.emit(RoomEvent.ParticipantConnected, participant); fakeLivekitRoom.emit(RoomEvent.ParticipantConnected, participant);
} }
// At this point there should be no publishers // At this point there should be no publishers
@@ -471,7 +477,7 @@ describe("Publishing participants observations", () => {
participants = [fakeRemoteLivekitParticipant("@bob:example.org:DEV111", 1)]; participants = [fakeRemoteLivekitParticipant("@bob:example.org:DEV111", 1)];
for (const participant of participants) { for (const participant of participants) {
fakeRoomEventEmiter.emit(RoomEvent.ParticipantConnected, participant); fakeLivekitRoom.emit(RoomEvent.ParticipantConnected, participant);
} }
// We should have bob has a publisher now // We should have bob has a publisher now
@@ -488,7 +494,7 @@ describe("Publishing participants observations", () => {
(p) => p.identity !== "@bob:example.org:DEV111", (p) => p.identity !== "@bob:example.org:DEV111",
); );
fakeRoomEventEmiter.emit( fakeLivekitRoom.emit(
RoomEvent.ParticipantDisconnected, RoomEvent.ParticipantDisconnected,
fakeRemoteLivekitParticipant("@bob:example.org:DEV111"), fakeRemoteLivekitParticipant("@bob:example.org:DEV111"),
); );

View File

@@ -18,7 +18,7 @@ import {
RoomEvent, RoomEvent,
} from "livekit-client"; } from "livekit-client";
import { type LivekitTransport } from "matrix-js-sdk/lib/matrixrtc"; import { type LivekitTransport } from "matrix-js-sdk/lib/matrixrtc";
import { BehaviorSubject, map } from "rxjs"; import { BehaviorSubject, map, skip, skipWhile } from "rxjs";
import { type Logger } from "matrix-js-sdk/lib/logger"; import { type Logger } from "matrix-js-sdk/lib/logger";
import { import {
@@ -61,7 +61,6 @@ export enum ConnectionState {
/** `start` has been called on the connection. It aquires the jwt info to conenct to the LK Room */ /** `start` has been called on the connection. It aquires the jwt info to conenct to the LK Room */
FetchingConfig = "FetchingConfig", FetchingConfig = "FetchingConfig",
Stopped = "Stopped", Stopped = "Stopped",
ConnectingToLkRoom = "ConnectingToLkRoom",
/** The same as ConnectionState.Disconnected from `livekit-client` */ /** The same as ConnectionState.Disconnected from `livekit-client` */
LivekitDisconnected = "disconnected", LivekitDisconnected = "disconnected",
/** The same as ConnectionState.Connecting from `livekit-client` */ /** The same as ConnectionState.Connecting from `livekit-client` */
@@ -139,7 +138,17 @@ export class Connection {
// If we were stopped while fetching the config, don't proceed to connect // If we were stopped while fetching the config, don't proceed to connect
if (this.stopped) return; if (this.stopped) return;
this._state$.next(ConnectionState.ConnectingToLkRoom); // Setup observer once we are done with getSFUConfigWithOpenID
connectionStateObserver(this.livekitRoom)
.pipe(
this.scope.bind(),
map((s) => s as unknown as ConnectionState),
)
.subscribe((lkState) => {
// It is save to cast lkState to ConnectionState as they are fully overlapping.
this._state$.next(lkState);
});
try { try {
await this.livekitRoom.connect(url, jwt); await this.livekitRoom.connect(url, jwt);
} catch (e) { } catch (e) {
@@ -167,13 +176,6 @@ export class Connection {
} }
// If we were stopped while connecting, don't proceed to update state. // If we were stopped while connecting, don't proceed to update state.
if (this.stopped) return; if (this.stopped) return;
connectionStateObserver(this.livekitRoom)
.pipe(this.scope.bind())
.subscribe((lkState) => {
// It is save to cast lkState to ConnectionState as they are fully overlapping.
this._state$.next(lkState as unknown as ConnectionState);
});
} catch (error) { } catch (error) {
this.logger.debug(`Failed to connect to LiveKit room: ${error}`); this.logger.debug(`Failed to connect to LiveKit room: ${error}`);
this._state$.next( this._state$.next(