From d3a4234f4a33e8d0c26b95b9c8aeb7c529c15c7f Mon Sep 17 00:00:00 2001 From: fkwp Date: Fri, 6 Feb 2026 11:54:22 +0100 Subject: [PATCH 01/17] setup webhooks for SFUs --- backend/dev_livekit-othersite.yaml | 4 ++++ backend/dev_livekit.yaml | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/backend/dev_livekit-othersite.yaml b/backend/dev_livekit-othersite.yaml index 0ae98c24..53fc9ce9 100644 --- a/backend/dev_livekit-othersite.yaml +++ b/backend/dev_livekit-othersite.yaml @@ -18,3 +18,7 @@ keys: devkey: secret room: auto_create: false +webhook: + api_key: devkey + urls: + - https://matrix-rtc.othersite.m.localhost/livekit/jwt/sfu_webhook diff --git a/backend/dev_livekit.yaml b/backend/dev_livekit.yaml index 157e4d04..6cef4241 100644 --- a/backend/dev_livekit.yaml +++ b/backend/dev_livekit.yaml @@ -18,3 +18,7 @@ keys: devkey: secret room: auto_create: false +webhook: + api_key: devkey + urls: + - https://matrix-rtc.m.localhost/livekit/jwt/sfu_webhook From 7b1eb03e2a8ebd4ffa81338ee9ad7a6628205b5e Mon Sep 17 00:00:00 2001 From: fkwp Date: Fri, 6 Feb 2026 11:54:39 +0100 Subject: [PATCH 02/17] add ca certs to SFU containers such that webhooks are accepted --- dev-backend-docker-compose.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/dev-backend-docker-compose.yml b/dev-backend-docker-compose.yml index 28682a33..31694c01 100644 --- a/dev-backend-docker-compose.yml +++ b/dev-backend-docker-compose.yml @@ -62,7 +62,10 @@ services: - 7882:7882/tcp - 50100-50200:50100-50200/udp volumes: + - ./backend/dev_tls_m.localhost.crt:/local_cert.pem:Z - ./backend/dev_livekit.yaml:/etc/livekit.yaml:Z + environment: + - SSL_CERT_FILE=/local_cert.pem networks: - ecbackend @@ -82,7 +85,10 @@ services: - 17882:17882/tcp - 50300-50400:50300-50400/udp volumes: + - ./backend/dev_tls_m.localhost.crt:/local_cert.pem:Z - ./backend/dev_livekit-othersite.yaml:/etc/livekit.yaml:Z + environment: + - SSL_CERT_FILE=/local_cert.pem networks: - ecbackend @@ -164,6 +170,8 @@ services: - "8448:8448" extra_hosts: - "host.docker.internal:host-gateway" + - "auth-server:127.0.0.1" + - "auth-server-1:127.0.0.1" depends_on: - synapse networks: From 9e2eef09d413cd4a6f5f6d34e6f6f9a9cd9ec5c7 Mon Sep 17 00:00:00 2001 From: fkwp Date: Wed, 22 Apr 2026 15:18:50 +0200 Subject: [PATCH 03/17] Add support for a grace period for /sync (aka homeserver disconnected) interruptions --- config/config.sample.json | 1 + src/config/ConfigOptions.ts | 8 ++++++++ src/state/CallViewModel/CallViewModel.ts | 2 ++ .../localMember/HomeserverConnected.test.ts | 19 ++++++++++--------- .../localMember/HomeserverConnected.ts | 14 ++++++++++++-- 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/config/config.sample.json b/config/config.sample.json index 126d7626..5fc63667 100644 --- a/config/config.sample.json +++ b/config/config.sample.json @@ -12,6 +12,7 @@ "feature_use_device_session_member_events": true }, "ssla": "https://static.element.io/legal/element-software-and-services-license-agreement-uk-1.pdf", + "sync_disconnect_grace_period_ms": 60000, "matrix_rtc_session": { "wait_for_key_rotation_ms": 3000, "membership_event_expiry_ms": 180000000, diff --git a/src/config/ConfigOptions.ts b/src/config/ConfigOptions.ts index 1b120546..d5839fb3 100644 --- a/src/config/ConfigOptions.ts +++ b/src/config/ConfigOptions.ts @@ -97,6 +97,13 @@ export interface ConfigOptions { enable_video?: boolean; }; + /** + * Grace period in milliseconds to wait before reporting the sync loop as disconnected. + * This allows brief sync interruptions without triggering a reconnection message. + * Default is 60000ms (60 seconds). Set to 0 to disable the grace period. + */ + sync_disconnect_grace_period_ms?: number; + /** * These are low level options that are used to configure the MatrixRTC session. * Take care when changing these options. @@ -168,5 +175,6 @@ export const DEFAULT_CONFIG: ResolvedConfigOptions = { features: { feature_use_device_session_member_events: true, }, + sync_disconnect_grace_period_ms: 60000, ssla: "https://static.element.io/legal/element-software-and-services-license-agreement-uk-1.pdf", }; diff --git a/src/state/CallViewModel/CallViewModel.ts b/src/state/CallViewModel/CallViewModel.ts index e298bcfd..8677f2d0 100644 --- a/src/state/CallViewModel/CallViewModel.ts +++ b/src/state/CallViewModel/CallViewModel.ts @@ -83,6 +83,7 @@ import { E2eeType } from "../../e2ee/e2eeType"; import { MatrixKeyProvider } from "../../e2ee/matrixKeyProvider"; import { type MuteStates } from "../MuteStates"; import { getUrlParams, HeaderStyle } from "../../UrlParams"; +import { Config } from "../../config/Config"; import { type ProcessorState } from "../../livekit/TrackProcessorContext"; import { ElementWidgetActions, widget } from "../../widget"; import { @@ -536,6 +537,7 @@ export function createCallViewModel$( scope, client, matrixRTCSession, + Config.get().sync_disconnect_grace_period_ms, ), muteStates, joinMatrixRTC: (transport: LivekitTransportConfig) => { diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts index 87ca35d0..e8861641 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts @@ -96,19 +96,20 @@ describe("createHomeserverConnected$", () => { // LLM generated test cases. They are a bit overkill but I improved the mocking so it is // easy enough to read them so I think they can stay. + // Note: gracePeriodMs is set to 0 to avoid debouncing delays in tests it("is false when sync state is not Syncing", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); expect(hsConnected.combined$.value).toBe(false); }); it("remains false while membership status is not Connected even if sync is Syncing", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); client.setSyncState(SyncState.Syncing); expect(hsConnected.combined$.value).toBe(false); // membership still disconnected }); it("is false when membership status transitions to Connected but ProbablyLeft is true", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); // Make sync loop OK client.setSyncState(SyncState.Syncing); // Indicate probable leave before connection @@ -118,7 +119,7 @@ describe("createHomeserverConnected$", () => { }); it("becomes true only when all three conditions are satisfied", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); // 1. Sync loop connected client.setSyncState(SyncState.Syncing); expect(hsConnected.combined$.value).toBe(false); // not yet membership connected @@ -128,7 +129,7 @@ describe("createHomeserverConnected$", () => { }); it("drops back to false when sync loop leaves Syncing", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); // Reach connected state client.setSyncState(SyncState.Syncing); session.setMembershipStatus(Status.Connected); @@ -140,7 +141,7 @@ describe("createHomeserverConnected$", () => { }); it("drops back to false when membership status becomes disconnected", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); client.setSyncState(SyncState.Syncing); session.setMembershipStatus(Status.Connected); expect(hsConnected.combined$.value).toBe(true); @@ -150,7 +151,7 @@ describe("createHomeserverConnected$", () => { }); it("drops to false when ProbablyLeft is emitted after being true", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); client.setSyncState(SyncState.Syncing); session.setMembershipStatus(Status.Connected); expect(hsConnected.combined$.value).toBe(true); @@ -160,7 +161,7 @@ describe("createHomeserverConnected$", () => { }); it("recovers to true if ProbablyLeft becomes false again while other conditions remain true", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); client.setSyncState(SyncState.Syncing); session.setMembershipStatus(Status.Connected); expect(hsConnected.combined$.value).toBe(true); @@ -174,7 +175,7 @@ describe("createHomeserverConnected$", () => { }); it("composite sequence reflects each individual failure reason", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); // Initially false (sync error + disconnected + not probably left) expect(hsConnected.combined$.value).toBe(false); diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.ts b/src/state/CallViewModel/localMember/HomeserverConnected.ts index c8bcd021..84da6780 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.ts @@ -12,9 +12,11 @@ import { type MatrixRTCSession, } from "matrix-js-sdk/lib/matrixrtc"; import { ClientEvent, type MatrixClient, SyncState } from "matrix-js-sdk"; -import { fromEvent, startWith, map, tap, type Observable } from "rxjs"; +import { fromEvent, startWith, map, tap, type Observable, debounceTime } from "rxjs"; import { logger as rootLogger } from "matrix-js-sdk/lib/logger"; +import { Config } from "../../../config/Config"; + import { type ObservableScope } from "../../ObservableScope"; import { type Behavior } from "../../Behavior"; import { and$ } from "../../../utils/observable"; @@ -35,21 +37,29 @@ export interface HomeserverConnected { * for the purposes of a MatrixRTC session. * * Becomes FALSE if ANY sub-condition is fulfilled: - * 1. Sync loop is not in SyncState.Syncing + * 1. Sync loop is not in SyncState.Syncing (after grace period) * 2. membershipStatus !== Status.Connected * 3. probablyLeft === true + * + * @param gracePeriodMs - Grace period in milliseconds to wait before reporting sync disconnect. + * If not provided, uses the config value (default 60000ms). */ export function createHomeserverConnected$( scope: ObservableScope, client: NodeStyleEventEmitter & Pick, matrixRTCSession: NodeStyleEventEmitter & Pick, + gracePeriodMs?: number, ): HomeserverConnected { + // Get grace period from parameter or config (default 60000ms) + const graceMs = gracePeriodMs ?? Config.get().sync_disconnect_grace_period_ms ?? 60000; + const syncing$ = ( fromEvent(client, ClientEvent.Sync) as Observable<[SyncState]> ).pipe( startWith([client.getSyncState()]), map(([state]) => state === SyncState.Syncing), + debounceTime(graceMs), ); const rtsSession$ = scope.behavior( From 9fccd49d7ddbf776b68bb72980ba0bb2b2026801 Mon Sep 17 00:00:00 2001 From: fkwp Date: Wed, 22 Apr 2026 21:08:31 +0200 Subject: [PATCH 04/17] Improve the handling of the grace period for the home server connection to support both immediate and delayed emissions. --- .../localMember/HomeserverConnected.ts | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.ts b/src/state/CallViewModel/localMember/HomeserverConnected.ts index 84da6780..cad987c8 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.ts @@ -12,11 +12,20 @@ import { type MatrixRTCSession, } from "matrix-js-sdk/lib/matrixrtc"; import { ClientEvent, type MatrixClient, SyncState } from "matrix-js-sdk"; -import { fromEvent, startWith, map, tap, type Observable, debounceTime } from "rxjs"; +import { + fromEvent, + startWith, + map, + tap, + type Observable, + distinctUntilChanged, + switchMap, + of, + delay, +} from "rxjs"; import { logger as rootLogger } from "matrix-js-sdk/lib/logger"; import { Config } from "../../../config/Config"; - import { type ObservableScope } from "../../ObservableScope"; import { type Behavior } from "../../Behavior"; import { and$ } from "../../../utils/observable"; @@ -41,6 +50,9 @@ export interface HomeserverConnected { * 2. membershipStatus !== Status.Connected * 3. probablyLeft === true * + * @param scope - The observable scope for lifecycle management. + * @param client - The Matrix client to monitor sync state. + * @param matrixRTCSession - The RTC session to monitor membership. * @param gracePeriodMs - Grace period in milliseconds to wait before reporting sync disconnect. * If not provided, uses the config value (default 60000ms). */ @@ -52,14 +64,24 @@ export function createHomeserverConnected$( gracePeriodMs?: number, ): HomeserverConnected { // Get grace period from parameter or config (default 60000ms) - const graceMs = gracePeriodMs ?? Config.get().sync_disconnect_grace_period_ms ?? 60000; + const graceMs = + gracePeriodMs ?? Config.get().sync_disconnect_grace_period_ms ?? 60000; const syncing$ = ( fromEvent(client, ClientEvent.Sync) as Observable<[SyncState]> ).pipe( startWith([client.getSyncState()]), map(([state]) => state === SyncState.Syncing), - debounceTime(graceMs), + distinctUntilChanged(), + switchMap((isSyncing) => +{ + if (isSyncing || graceMs <= 0) { + return of(isSyncing); // Sofortige Emission (Synchron) + } + return of(false).pipe(delay(graceMs)); // Verzögertes false + } ), + startWith(client.getSyncState() === SyncState.Syncing), + distinctUntilChanged(), ); const rtsSession$ = scope.behavior( From 032ec662e92b0cbc1f3ab89af5a47334af1119d3 Mon Sep 17 00:00:00 2001 From: fkwp Date: Wed, 22 Apr 2026 21:08:49 +0200 Subject: [PATCH 05/17] bump lk-jwt-service --- dev-backend-docker-compose.yml | 350 ++++++++++++++++----------------- 1 file changed, 175 insertions(+), 175 deletions(-) diff --git a/dev-backend-docker-compose.yml b/dev-backend-docker-compose.yml index 36fc7f44..0a42dad5 100644 --- a/dev-backend-docker-compose.yml +++ b/dev-backend-docker-compose.yml @@ -1,175 +1,175 @@ -networks: - ecbackend: - -services: - auth-service: - image: ghcr.io/element-hq/lk-jwt-service:0.4.4 - pull_policy: always - hostname: auth-server - environment: - - LIVEKIT_JWT_PORT=6080 - - LIVEKIT_URL=wss://matrix-rtc.m.localhost/livekit/sfu - - LIVEKIT_KEY=devkey - - LIVEKIT_SECRET=secret - # If the configured homeserver runs on localhost, it'll probably be using - # a self-signed certificate - - LIVEKIT_INSECURE_SKIP_VERIFY_TLS=YES_I_KNOW_WHAT_I_AM_DOING - - LIVEKIT_FULL_ACCESS_HOMESERVERS=* - deploy: - restart_policy: - condition: on-failure - ports: - # HOST_PORT:CONTAINER_PORT - - 6080:6080 - networks: - - ecbackend - - auth-service-1: - image: ghcr.io/element-hq/lk-jwt-service:0.4.4 - pull_policy: always - hostname: auth-server-1 - environment: - - LIVEKIT_JWT_PORT=16080 - - LIVEKIT_URL=wss://matrix-rtc.othersite.m.localhost/livekit/sfu - - LIVEKIT_KEY=devkey - - LIVEKIT_SECRET=secret - # If the configured homeserver runs on localhost, it'll probably be using - # a self-signed certificate - - LIVEKIT_INSECURE_SKIP_VERIFY_TLS=YES_I_KNOW_WHAT_I_AM_DOING - - LIVEKIT_FULL_ACCESS_HOMESERVERS=* - deploy: - restart_policy: - condition: on-failure - ports: - # HOST_PORT:CONTAINER_PORT - - 16080:16080 - networks: - - ecbackend - - livekit: - image: livekit/livekit-server:v1.10.1 - pull_policy: always - hostname: livekit-sfu - command: --dev --config /etc/livekit.yaml - restart: unless-stopped - # The SFU seems to work far more reliably when we let it share the host - # network rather than opening specific ports (but why?? we're not missing - # any…) - ports: - # HOST_PORT:CONTAINER_PORT - - 7880:7880/tcp - - 7881:7881/tcp - - 7882:7882/tcp - - 50100-50200:50100-50200/udp - volumes: - - ./backend/dev_livekit.yaml:/etc/livekit.yaml:Z - networks: - - ecbackend - - livekit-1: - image: livekit/livekit-server:v1.10.1 - pull_policy: always - hostname: livekit-sfu-1 - command: --dev --config /etc/livekit.yaml - restart: unless-stopped - # The SFU seems to work far more reliably when we let it share the host - # network rather than opening specific ports (but why?? we're not missing - # any…) - ports: - # HOST_PORT:CONTAINER_PORT - - 17880:17880/tcp - - 17881:17881/tcp - - 17882:17882/tcp - - 50300-50400:50300-50400/udp - volumes: - - ./backend/dev_livekit-othersite.yaml:/etc/livekit.yaml:Z - networks: - - ecbackend - - synapse: - hostname: homeserver - image: ghcr.io/element-hq/synapse:latest - pull_policy: always - environment: - - SYNAPSE_CONFIG_PATH=/data/cfg/homeserver.yaml - # Needed for rootless podman-compose such that the uid/gid mapping does - # fit local user uid. If the container runs as root (uid 0) it is fine as - # it actually maps to your non-root user on the host (e.g. 1000). - # Otherwise uid mapping will not match your non-root user. - - UID=0 - - GID=0 - volumes: - - ./backend/synapse_tmp:/data:Z - - ./backend/dev_homeserver.yaml:/data/cfg/homeserver.yaml:Z - networks: - - ecbackend - - synapse-1: - hostname: homeserver-1 - image: ghcr.io/element-hq/synapse:latest - pull_policy: always - environment: - - SYNAPSE_CONFIG_PATH=/data/cfg/homeserver.yaml - # Needed for rootless podman-compose such that the uid/gid mapping does - # fit local user uid. If the container runs as root (uid 0) it is fine as - # it actually maps to your non-root user on the host (e.g. 1000). - # Otherwise uid mapping will not match your non-root user. - - UID=0 - - GID=0 - volumes: - - ./backend/synapse_tmp_othersite:/data:Z - - ./backend/dev_homeserver-othersite.yaml:/data/cfg/homeserver.yaml:Z - networks: - - ecbackend - - element-web: - image: ghcr.io/element-hq/element-web:develop - pull_policy: always - volumes: - - ./backend/ew.test.config.json:/app/config.json:Z - environment: - ELEMENT_WEB_PORT: 8081 - ports: - - "8081:8081" - networks: - - ecbackend - - element-web-1: - image: ghcr.io/element-hq/element-web:develop - pull_policy: always - volumes: - - ./backend/ew.test.othersite.config.json:/app/config.json:Z - environment: - ELEMENT_WEB_PORT: 18081 - ports: - # HOST_PORT:CONTAINER_PORT - - "18081:18081" - networks: - - ecbackend - - nginx: - # see backend/dev_tls_setup for how to generate the tls certs - hostname: synapse.m.localhost - image: nginx:latest - pull_policy: always - volumes: - - ./backend/dev_nginx.conf:/etc/nginx/conf.d/default.conf:Z - - ./backend/dev_tls_m.localhost.key:/root/ssl/key.pem:Z - - ./backend/dev_tls_m.localhost.crt:/root/ssl/cert.pem:Z - ports: - # HOST_PORT:CONTAINER_PORT - - "443:443" - - "8008:80" - - "4443:443" - - "8448:8448" - extra_hosts: - - "host.docker.internal:host-gateway" - depends_on: - - synapse - networks: - ecbackend: - aliases: - - synapse.m.localhost - - synapse.othersite.m.localhost - - matrix-rtc.m.localhost - - matrix-rtc.othersite.m.localhost +networks: + ecbackend: + +services: + auth-service: + image: ghcr.io/element-hq/lk-jwt-service:pr_171 + pull_policy: always + hostname: auth-server + environment: + - LIVEKIT_JWT_PORT=6080 + - LIVEKIT_URL=wss://matrix-rtc.m.localhost/livekit/sfu + - LIVEKIT_KEY=devkey + - LIVEKIT_SECRET=secret + # If the configured homeserver runs on localhost, it'll probably be using + # a self-signed certificate + - LIVEKIT_INSECURE_SKIP_VERIFY_TLS=YES_I_KNOW_WHAT_I_AM_DOING + - LIVEKIT_FULL_ACCESS_HOMESERVERS=* + deploy: + restart_policy: + condition: on-failure + ports: + # HOST_PORT:CONTAINER_PORT + - 6080:6080 + networks: + - ecbackend + + auth-service-1: + image: ghcr.io/element-hq/lk-jwt-service:pr_171 + pull_policy: always + hostname: auth-server-1 + environment: + - LIVEKIT_JWT_PORT=16080 + - LIVEKIT_URL=wss://matrix-rtc.othersite.m.localhost/livekit/sfu + - LIVEKIT_KEY=devkey + - LIVEKIT_SECRET=secret + # If the configured homeserver runs on localhost, it'll probably be using + # a self-signed certificate + - LIVEKIT_INSECURE_SKIP_VERIFY_TLS=YES_I_KNOW_WHAT_I_AM_DOING + - LIVEKIT_FULL_ACCESS_HOMESERVERS=* + deploy: + restart_policy: + condition: on-failure + ports: + # HOST_PORT:CONTAINER_PORT + - 16080:16080 + networks: + - ecbackend + + livekit: + image: livekit/livekit-server:v1.10.1 + pull_policy: always + hostname: livekit-sfu + command: --dev --config /etc/livekit.yaml + restart: unless-stopped + # The SFU seems to work far more reliably when we let it share the host + # network rather than opening specific ports (but why?? we're not missing + # any…) + ports: + # HOST_PORT:CONTAINER_PORT + - 7880:7880/tcp + - 7881:7881/tcp + - 7882:7882/tcp + - 50100-50200:50100-50200/udp + volumes: + - ./backend/dev_livekit.yaml:/etc/livekit.yaml:Z + networks: + - ecbackend + + livekit-1: + image: livekit/livekit-server:v1.10.1 + pull_policy: always + hostname: livekit-sfu-1 + command: --dev --config /etc/livekit.yaml + restart: unless-stopped + # The SFU seems to work far more reliably when we let it share the host + # network rather than opening specific ports (but why?? we're not missing + # any…) + ports: + # HOST_PORT:CONTAINER_PORT + - 17880:17880/tcp + - 17881:17881/tcp + - 17882:17882/tcp + - 50300-50400:50300-50400/udp + volumes: + - ./backend/dev_livekit-othersite.yaml:/etc/livekit.yaml:Z + networks: + - ecbackend + + synapse: + hostname: homeserver + image: ghcr.io/element-hq/synapse:latest + pull_policy: always + environment: + - SYNAPSE_CONFIG_PATH=/data/cfg/homeserver.yaml + # Needed for rootless podman-compose such that the uid/gid mapping does + # fit local user uid. If the container runs as root (uid 0) it is fine as + # it actually maps to your non-root user on the host (e.g. 1000). + # Otherwise uid mapping will not match your non-root user. + - UID=0 + - GID=0 + volumes: + - ./backend/synapse_tmp:/data:Z + - ./backend/dev_homeserver.yaml:/data/cfg/homeserver.yaml:Z + networks: + - ecbackend + + synapse-1: + hostname: homeserver-1 + image: ghcr.io/element-hq/synapse:latest + pull_policy: always + environment: + - SYNAPSE_CONFIG_PATH=/data/cfg/homeserver.yaml + # Needed for rootless podman-compose such that the uid/gid mapping does + # fit local user uid. If the container runs as root (uid 0) it is fine as + # it actually maps to your non-root user on the host (e.g. 1000). + # Otherwise uid mapping will not match your non-root user. + - UID=0 + - GID=0 + volumes: + - ./backend/synapse_tmp_othersite:/data:Z + - ./backend/dev_homeserver-othersite.yaml:/data/cfg/homeserver.yaml:Z + networks: + - ecbackend + + element-web: + image: ghcr.io/element-hq/element-web:develop + pull_policy: always + volumes: + - ./backend/ew.test.config.json:/app/config.json:Z + environment: + ELEMENT_WEB_PORT: 8081 + ports: + - "8081:8081" + networks: + - ecbackend + + element-web-1: + image: ghcr.io/element-hq/element-web:develop + pull_policy: always + volumes: + - ./backend/ew.test.othersite.config.json:/app/config.json:Z + environment: + ELEMENT_WEB_PORT: 18081 + ports: + # HOST_PORT:CONTAINER_PORT + - "18081:18081" + networks: + - ecbackend + + nginx: + # see backend/dev_tls_setup for how to generate the tls certs + hostname: synapse.m.localhost + image: nginx:latest + pull_policy: always + volumes: + - ./backend/dev_nginx.conf:/etc/nginx/conf.d/default.conf:Z + - ./backend/dev_tls_m.localhost.key:/root/ssl/key.pem:Z + - ./backend/dev_tls_m.localhost.crt:/root/ssl/cert.pem:Z + ports: + # HOST_PORT:CONTAINER_PORT + - "443:443" + - "8008:80" + - "4443:443" + - "8448:8448" + extra_hosts: + - "host.docker.internal:host-gateway" + depends_on: + - synapse + networks: + ecbackend: + aliases: + - synapse.m.localhost + - synapse.othersite.m.localhost + - matrix-rtc.m.localhost + - matrix-rtc.othersite.m.localhost From 283c606b9d35ad6c04c6c22442e922cb556f6140 Mon Sep 17 00:00:00 2001 From: fkwp Date: Wed, 22 Apr 2026 21:46:25 +0200 Subject: [PATCH 06/17] Add tests for grace period handling in createHomeserverConnected$ --- .../localMember/HomeserverConnected.test.ts | 87 ++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts index e8861641..5b759cd1 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts @@ -6,7 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial Please see LICENSE in the repository root for full details. */ -import { describe, it, expect, beforeEach, afterEach } from "vitest"; +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; import { EventEmitter } from "events"; import { ClientEvent, SyncState } from "matrix-js-sdk"; import { MembershipManagerEvent, Status } from "matrix-js-sdk/lib/matrixrtc"; @@ -201,3 +201,88 @@ describe("createHomeserverConnected$", () => { expect(hsConnected.combined$.value).toBe(false); }); }); + +describe("createHomeserverConnected$ - Grace Period", () => { + let scope: ObservableScope; + let client: MockMatrixClient; + let session: MockMatrixRTCSession; + const GRACE_PERIOD = 5000; + + beforeEach(() => { + vi.useFakeTimers(); + scope = new ObservableScope(); + // Initialize with values that satisfy the "Connected" condition + client = new MockMatrixClient(SyncState.Syncing); + session = new MockMatrixRTCSession({ + membershipStatus: Status.Connected, + probablyLeft: false, + }); + }); + + afterEach(() => { + scope.end(); + vi.useRealTimers(); + }); + + it("respects gracePeriodMs: stays true during grace period and flips false after", () => { + const hsConnected = createHomeserverConnected$(scope, client, session, GRACE_PERIOD); + + session.setMembershipStatus(Status.Connected); + session.setProbablyLeft(false); + + // Initial state: Everything is connected + expect(hsConnected.combined$.value).toBe(true); + + // 1. Sync loses connection -> should remain TRUE due to grace period + client.setSyncState(SyncState.Error); + expect(hsConnected.combined$.value).toBe(true); + + // 2. Fast forward time (just before expiration) + vi.advanceTimersByTime(GRACE_PERIOD - 1); + expect(hsConnected.combined$.value).toBe(true); + + // 3. Fast forward time (expiration) + vi.advanceTimersByTime(1); + expect(hsConnected.combined$.value).toBe(false); + }); + + it("recovers immediately if sync returns during grace period", () => { + const hsConnected = createHomeserverConnected$(scope, client, session, GRACE_PERIOD); + + session.setMembershipStatus(Status.Connected); + session.setProbablyLeft(false); + + // Initial state: Connected + expect(hsConnected.combined$.value).toBe(true); + + // 1. Sync error occurs + client.setSyncState(SyncState.Error); + vi.advanceTimersByTime(GRACE_PERIOD / 2); + expect(hsConnected.combined$.value).toBe(true); + + // 2. Sync recovers BEFORE the grace period expires + client.setSyncState(SyncState.Syncing); + expect(hsConnected.combined$.value).toBe(true); + + // 3. Fast forward the remaining time -> should stay TRUE + vi.advanceTimersByTime(GRACE_PERIOD); + expect(hsConnected.combined$.value).toBe(true); + }); + + it("flips to true IMMEDIATELY even if a grace period was pending", () => { + const hsConnected = createHomeserverConnected$(scope, client, session, GRACE_PERIOD); + + session.setMembershipStatus(Status.Connected); + session.setProbablyLeft(false); + + // 1. Initial error: wait until it flips to false + client.setSyncState(SyncState.Error); + expect(hsConnected.combined$.value).toBe(true); + vi.advanceTimersByTime(GRACE_PERIOD + 1); + expect(hsConnected.combined$.value).toBe(false); + + // 2. Back to Syncing -> Must be TRUE immediately (synchronously) + client.setSyncState(SyncState.Syncing); + expect(hsConnected.combined$.value).toBe(true); + }); +}); \ No newline at end of file From c1f821ca0f1fe002deea4f7efc23e0e09da739fb Mon Sep 17 00:00:00 2001 From: fkwp Date: Thu, 23 Apr 2026 15:33:00 +0200 Subject: [PATCH 07/17] Update default sync disconnect grace period to 10000ms in configuration and related functions --- src/config/ConfigOptions.ts | 4 ++-- src/state/CallViewModel/CallViewModel.ts | 2 -- src/state/CallViewModel/localMember/HomeserverConnected.ts | 6 +++--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/config/ConfigOptions.ts b/src/config/ConfigOptions.ts index d5839fb3..1781dc97 100644 --- a/src/config/ConfigOptions.ts +++ b/src/config/ConfigOptions.ts @@ -100,7 +100,7 @@ export interface ConfigOptions { /** * Grace period in milliseconds to wait before reporting the sync loop as disconnected. * This allows brief sync interruptions without triggering a reconnection message. - * Default is 60000ms (60 seconds). Set to 0 to disable the grace period. + * Default is 10000ms (10 seconds). Set to 0 to disable the grace period. */ sync_disconnect_grace_period_ms?: number; @@ -175,6 +175,6 @@ export const DEFAULT_CONFIG: ResolvedConfigOptions = { features: { feature_use_device_session_member_events: true, }, - sync_disconnect_grace_period_ms: 60000, + sync_disconnect_grace_period_ms: 10000, ssla: "https://static.element.io/legal/element-software-and-services-license-agreement-uk-1.pdf", }; diff --git a/src/state/CallViewModel/CallViewModel.ts b/src/state/CallViewModel/CallViewModel.ts index 8677f2d0..e298bcfd 100644 --- a/src/state/CallViewModel/CallViewModel.ts +++ b/src/state/CallViewModel/CallViewModel.ts @@ -83,7 +83,6 @@ import { E2eeType } from "../../e2ee/e2eeType"; import { MatrixKeyProvider } from "../../e2ee/matrixKeyProvider"; import { type MuteStates } from "../MuteStates"; import { getUrlParams, HeaderStyle } from "../../UrlParams"; -import { Config } from "../../config/Config"; import { type ProcessorState } from "../../livekit/TrackProcessorContext"; import { ElementWidgetActions, widget } from "../../widget"; import { @@ -537,7 +536,6 @@ export function createCallViewModel$( scope, client, matrixRTCSession, - Config.get().sync_disconnect_grace_period_ms, ), muteStates, joinMatrixRTC: (transport: LivekitTransportConfig) => { diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.ts b/src/state/CallViewModel/localMember/HomeserverConnected.ts index cad987c8..d66d8657 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.ts @@ -54,7 +54,7 @@ export interface HomeserverConnected { * @param client - The Matrix client to monitor sync state. * @param matrixRTCSession - The RTC session to monitor membership. * @param gracePeriodMs - Grace period in milliseconds to wait before reporting sync disconnect. - * If not provided, uses the config value (default 60000ms). + * If not provided, uses the config value (default 10000ms). */ export function createHomeserverConnected$( scope: ObservableScope, @@ -63,9 +63,9 @@ export function createHomeserverConnected$( Pick, gracePeriodMs?: number, ): HomeserverConnected { - // Get grace period from parameter or config (default 60000ms) + // Get grace period from parameter or config (default 10000ms) const graceMs = - gracePeriodMs ?? Config.get().sync_disconnect_grace_period_ms ?? 60000; + gracePeriodMs ?? Config.get().sync_disconnect_grace_period_ms ?? 10000; const syncing$ = ( fromEvent(client, ClientEvent.Sync) as Observable<[SyncState]> From 0abc32b80ee5055fc4106f2baf9d48025116ede1 Mon Sep 17 00:00:00 2001 From: fkwp Date: Thu, 23 Apr 2026 15:47:40 +0200 Subject: [PATCH 08/17] prettier --- .../localMember/HomeserverConnected.test.ts | 23 +++++++++++++++---- .../localMember/HomeserverConnected.ts | 13 +++++------ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts index 5b759cd1..96feeeb1 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts @@ -225,7 +225,12 @@ describe("createHomeserverConnected$ - Grace Period", () => { }); it("respects gracePeriodMs: stays true during grace period and flips false after", () => { - const hsConnected = createHomeserverConnected$(scope, client, session, GRACE_PERIOD); + const hsConnected = createHomeserverConnected$( + scope, + client, + session, + GRACE_PERIOD, + ); session.setMembershipStatus(Status.Connected); session.setProbablyLeft(false); @@ -247,7 +252,12 @@ describe("createHomeserverConnected$ - Grace Period", () => { }); it("recovers immediately if sync returns during grace period", () => { - const hsConnected = createHomeserverConnected$(scope, client, session, GRACE_PERIOD); + const hsConnected = createHomeserverConnected$( + scope, + client, + session, + GRACE_PERIOD, + ); session.setMembershipStatus(Status.Connected); session.setProbablyLeft(false); @@ -270,7 +280,12 @@ describe("createHomeserverConnected$ - Grace Period", () => { }); it("flips to true IMMEDIATELY even if a grace period was pending", () => { - const hsConnected = createHomeserverConnected$(scope, client, session, GRACE_PERIOD); + const hsConnected = createHomeserverConnected$( + scope, + client, + session, + GRACE_PERIOD, + ); session.setMembershipStatus(Status.Connected); session.setProbablyLeft(false); @@ -285,4 +300,4 @@ describe("createHomeserverConnected$ - Grace Period", () => { client.setSyncState(SyncState.Syncing); expect(hsConnected.combined$.value).toBe(true); }); -}); \ No newline at end of file +}); diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.ts b/src/state/CallViewModel/localMember/HomeserverConnected.ts index d66d8657..ff25a250 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.ts @@ -73,13 +73,12 @@ export function createHomeserverConnected$( startWith([client.getSyncState()]), map(([state]) => state === SyncState.Syncing), distinctUntilChanged(), - switchMap((isSyncing) => -{ - if (isSyncing || graceMs <= 0) { - return of(isSyncing); // Sofortige Emission (Synchron) - } - return of(false).pipe(delay(graceMs)); // Verzögertes false - } ), + switchMap((isSyncing) => { + if (isSyncing || graceMs <= 0) { + return of(isSyncing); + } + return of(false).pipe(delay(graceMs)); + }), startWith(client.getSyncState() === SyncState.Syncing), distinctUntilChanged(), ); From 24c2845958a4fbf9f91b57c2c9118fa376b6f539 Mon Sep 17 00:00:00 2001 From: fkwp Date: Thu, 30 Apr 2026 20:50:45 +0200 Subject: [PATCH 09/17] move starWith to corresponding branch in switchMap --- src/state/CallViewModel/localMember/HomeserverConnected.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.ts b/src/state/CallViewModel/localMember/HomeserverConnected.ts index ff25a250..06d660be 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.ts @@ -77,9 +77,8 @@ export function createHomeserverConnected$( if (isSyncing || graceMs <= 0) { return of(isSyncing); } - return of(false).pipe(delay(graceMs)); + return of(false).pipe(delay(graceMs), startWith(true)); }), - startWith(client.getSyncState() === SyncState.Syncing), distinctUntilChanged(), ); From 42aae307f907065e8fe88bf3648affcb7e779f5d Mon Sep 17 00:00:00 2001 From: fkwp Date: Thu, 30 Apr 2026 20:53:18 +0200 Subject: [PATCH 10/17] use latest stable version of jwt service --- dev-backend-docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev-backend-docker-compose.yml b/dev-backend-docker-compose.yml index 95953766..702aef39 100644 --- a/dev-backend-docker-compose.yml +++ b/dev-backend-docker-compose.yml @@ -3,7 +3,7 @@ networks: services: auth-service: - image: ghcr.io/element-hq/lk-jwt-service:pr_171 + image: ghcr.io/element-hq/lk-jwt-service:0.4.4 pull_policy: always hostname: auth-server environment: @@ -25,7 +25,7 @@ services: - ecbackend auth-service-1: - image: ghcr.io/element-hq/lk-jwt-service:pr_171 + image: ghcr.io/element-hq/lk-jwt-service:0.4.4 pull_policy: always hostname: auth-server-1 environment: From 2e4ca9b761c3191dc65b98f30d60abf9ec40d94a Mon Sep 17 00:00:00 2001 From: fkwp Date: Thu, 30 Apr 2026 20:54:46 +0200 Subject: [PATCH 11/17] use 10 Seconds of grace period in config.sample.json --- config/config.sample.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.sample.json b/config/config.sample.json index 5fc63667..bb0e4dfd 100644 --- a/config/config.sample.json +++ b/config/config.sample.json @@ -12,7 +12,7 @@ "feature_use_device_session_member_events": true }, "ssla": "https://static.element.io/legal/element-software-and-services-license-agreement-uk-1.pdf", - "sync_disconnect_grace_period_ms": 60000, + "sync_disconnect_grace_period_ms": 10000, "matrix_rtc_session": { "wait_for_key_rotation_ms": 3000, "membership_event_expiry_ms": 180000000, From 22f16174efebac62f917ab74589a86f47dea6d83 Mon Sep 17 00:00:00 2001 From: fkwp Date: Mon, 4 May 2026 09:16:55 +0200 Subject: [PATCH 12/17] addressing review comments --- config/config.sample.json | 1 - src/config/ConfigOptions.ts | 1 + src/state/CallViewModel/localMember/HomeserverConnected.ts | 3 +-- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/config/config.sample.json b/config/config.sample.json index bb0e4dfd..126d7626 100644 --- a/config/config.sample.json +++ b/config/config.sample.json @@ -12,7 +12,6 @@ "feature_use_device_session_member_events": true }, "ssla": "https://static.element.io/legal/element-software-and-services-license-agreement-uk-1.pdf", - "sync_disconnect_grace_period_ms": 10000, "matrix_rtc_session": { "wait_for_key_rotation_ms": 3000, "membership_event_expiry_ms": 180000000, diff --git a/src/config/ConfigOptions.ts b/src/config/ConfigOptions.ts index 1781dc97..93403a96 100644 --- a/src/config/ConfigOptions.ts +++ b/src/config/ConfigOptions.ts @@ -162,6 +162,7 @@ export interface ResolvedConfigOptions extends ConfigOptions { server_name: string; }; }; + sync_disconnect_grace_period_ms: number; ssla: string; } diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.ts b/src/state/CallViewModel/localMember/HomeserverConnected.ts index 06d660be..445a4d2a 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.ts @@ -64,8 +64,7 @@ export function createHomeserverConnected$( gracePeriodMs?: number, ): HomeserverConnected { // Get grace period from parameter or config (default 10000ms) - const graceMs = - gracePeriodMs ?? Config.get().sync_disconnect_grace_period_ms ?? 10000; + const graceMs = gracePeriodMs ?? Config.get().sync_disconnect_grace_period_ms; const syncing$ = ( fromEvent(client, ClientEvent.Sync) as Observable<[SyncState]> From 88b5db6675a296e5e7a00de451d51b7022d69dc0 Mon Sep 17 00:00:00 2001 From: fkwp Date: Mon, 4 May 2026 10:41:48 +0200 Subject: [PATCH 13/17] fix rtsSession initial state --- .../localMember/HomeserverConnected.test.ts | 9 --------- .../CallViewModel/localMember/HomeserverConnected.ts | 2 +- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts index 96feeeb1..413c5745 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts @@ -232,9 +232,6 @@ describe("createHomeserverConnected$ - Grace Period", () => { GRACE_PERIOD, ); - session.setMembershipStatus(Status.Connected); - session.setProbablyLeft(false); - // Initial state: Everything is connected expect(hsConnected.combined$.value).toBe(true); @@ -259,9 +256,6 @@ describe("createHomeserverConnected$ - Grace Period", () => { GRACE_PERIOD, ); - session.setMembershipStatus(Status.Connected); - session.setProbablyLeft(false); - // Initial state: Connected expect(hsConnected.combined$.value).toBe(true); @@ -287,9 +281,6 @@ describe("createHomeserverConnected$ - Grace Period", () => { GRACE_PERIOD, ); - session.setMembershipStatus(Status.Connected); - session.setProbablyLeft(false); - // 1. Initial error: wait until it flips to false client.setSyncState(SyncState.Error); expect(hsConnected.combined$.value).toBe(true); diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.ts b/src/state/CallViewModel/localMember/HomeserverConnected.ts index 445a4d2a..65cc24c6 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.ts @@ -85,7 +85,7 @@ export function createHomeserverConnected$( fromEvent(matrixRTCSession, MembershipManagerEvent.StatusChanged).pipe( map(() => matrixRTCSession.membershipStatus ?? Status.Unknown), ), - Status.Unknown, + matrixRTCSession.membershipStatus ?? Status.Unknown, ); const membershipConnected$ = rtsSession$.pipe( From a081b40fb23087ac7d7d57f71e23d414fb17b000 Mon Sep 17 00:00:00 2001 From: fkwp Date: Mon, 4 May 2026 13:24:19 +0200 Subject: [PATCH 14/17] add marble test --- .../localMember/HomeserverConnected.test.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts index 413c5745..56e1eef9 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts @@ -14,6 +14,8 @@ import { MembershipManagerEvent, Status } from "matrix-js-sdk/lib/matrixrtc"; import { ObservableScope } from "../../ObservableScope"; import { createHomeserverConnected$ } from "./HomeserverConnected"; +import { TestScheduler } from "rxjs/testing"; + /** * Minimal stub of a Matrix client sufficient for our tests: ``` @@ -291,4 +293,42 @@ describe("createHomeserverConnected$ - Grace Period", () => { client.setSyncState(SyncState.Syncing); expect(hsConnected.combined$.value).toBe(true); }); + + it('marble: sync "s----e" -> HomeserverConnected "t---------f"', () => { + const ts = new TestScheduler((a, b) => expect(a).toEqual(b)); + + ts.run(({ cold, expectObservable }) => { + const GRACE = 5; + const scope = new ObservableScope(); + + // Setup Mocks + const client = new MockMatrixClient(SyncState.Syncing); + const session = new MockMatrixRTCSession({ + membershipStatus: Status.Connected, + probablyLeft: false, + }); + + const hs = createHomeserverConnected$(scope, client, session, GRACE); + + // Marble-Input: s (Syncing) at 0ms, e (Error) at 5ms + const syncValues = { s: SyncState.Syncing, e: SyncState.Error }; + const driver$ = cold("s----e", syncValues); + + // Feed Mock-Client with marble values + driver$.subscribe((state) => { + client.setSyncState(state); + }); + + const values = { t: true, f: false }; + + // t (0ms: Syncing + Connected = true) + // (5ms: Error occurs, Grace period starts, still true) + // f (10ms: 5ms + 5ms Grace period ends, should flip to false) + expectObservable(hs.combined$).toBe("t---------f", values); + + ts.flush(); + scope.end(); + }); + }); + }); From 69757b06cee10e830ad9057dcad59c0e98485731 Mon Sep 17 00:00:00 2001 From: fkwp Date: Mon, 4 May 2026 13:27:57 +0200 Subject: [PATCH 15/17] prettier --- .../CallViewModel/localMember/HomeserverConnected.test.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts index 56e1eef9..edace7d3 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts @@ -296,7 +296,7 @@ describe("createHomeserverConnected$ - Grace Period", () => { it('marble: sync "s----e" -> HomeserverConnected "t---------f"', () => { const ts = new TestScheduler((a, b) => expect(a).toEqual(b)); - + ts.run(({ cold, expectObservable }) => { const GRACE = 5; const scope = new ObservableScope(); @@ -313,14 +313,14 @@ describe("createHomeserverConnected$ - Grace Period", () => { // Marble-Input: s (Syncing) at 0ms, e (Error) at 5ms const syncValues = { s: SyncState.Syncing, e: SyncState.Error }; const driver$ = cold("s----e", syncValues); - + // Feed Mock-Client with marble values driver$.subscribe((state) => { client.setSyncState(state); }); const values = { t: true, f: false }; - + // t (0ms: Syncing + Connected = true) // (5ms: Error occurs, Grace period starts, still true) // f (10ms: 5ms + 5ms Grace period ends, should flip to false) @@ -330,5 +330,4 @@ describe("createHomeserverConnected$ - Grace Period", () => { scope.end(); }); }); - }); From 0dc8fab2562af889767c09eac28fc3d9097981a8 Mon Sep 17 00:00:00 2001 From: fkwp Date: Mon, 4 May 2026 13:36:26 +0200 Subject: [PATCH 16/17] linting --- .../CallViewModel/localMember/HomeserverConnected.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts index edace7d3..d32b8e4e 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts @@ -10,12 +10,11 @@ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; import { EventEmitter } from "events"; import { ClientEvent, SyncState } from "matrix-js-sdk"; import { MembershipManagerEvent, Status } from "matrix-js-sdk/lib/matrixrtc"; +import { TestScheduler } from "rxjs/testing"; import { ObservableScope } from "../../ObservableScope"; import { createHomeserverConnected$ } from "./HomeserverConnected"; -import { TestScheduler } from "rxjs/testing"; - /** * Minimal stub of a Matrix client sufficient for our tests: ``` From 7916360b520f1a3752bbbb42cfb8011574d66381 Mon Sep 17 00:00:00 2001 From: Robin Date: Mon, 4 May 2026 17:54:35 +0200 Subject: [PATCH 17/17] Convert all grace period tests to marble tests --- .../localMember/HomeserverConnected.test.ts | 169 ++++++------------ 1 file changed, 50 insertions(+), 119 deletions(-) diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts index d32b8e4e..3de6a7d5 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts @@ -6,14 +6,14 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial Please see LICENSE in the repository root for full details. */ -import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; +import { describe, it, expect, beforeEach, afterEach } from "vitest"; import { EventEmitter } from "events"; import { ClientEvent, SyncState } from "matrix-js-sdk"; import { MembershipManagerEvent, Status } from "matrix-js-sdk/lib/matrixrtc"; -import { TestScheduler } from "rxjs/testing"; import { ObservableScope } from "../../ObservableScope"; import { createHomeserverConnected$ } from "./HomeserverConnected"; +import { testScope, withTestScheduler } from "../../../utils/test"; /** * Minimal stub of a Matrix client sufficient for our tests: @@ -204,129 +204,60 @@ describe("createHomeserverConnected$", () => { }); describe("createHomeserverConnected$ - Grace Period", () => { - let scope: ObservableScope; - let client: MockMatrixClient; - let session: MockMatrixRTCSession; - const GRACE_PERIOD = 5000; + const GRACE_PERIOD = 5; - beforeEach(() => { - vi.useFakeTimers(); - scope = new ObservableScope(); - // Initialize with values that satisfy the "Connected" condition - client = new MockMatrixClient(SyncState.Syncing); - session = new MockMatrixRTCSession({ - membershipStatus: Status.Connected, - probablyLeft: false, - }); - }); - - afterEach(() => { - scope.end(); - vi.useRealTimers(); - }); - - it("respects gracePeriodMs: stays true during grace period and flips false after", () => { - const hsConnected = createHomeserverConnected$( - scope, - client, - session, - GRACE_PERIOD, - ); - - // Initial state: Everything is connected - expect(hsConnected.combined$.value).toBe(true); - - // 1. Sync loses connection -> should remain TRUE due to grace period - client.setSyncState(SyncState.Error); - expect(hsConnected.combined$.value).toBe(true); - - // 2. Fast forward time (just before expiration) - vi.advanceTimersByTime(GRACE_PERIOD - 1); - expect(hsConnected.combined$.value).toBe(true); - - // 3. Fast forward time (expiration) - vi.advanceTimersByTime(1); - expect(hsConnected.combined$.value).toBe(false); - }); - - it("recovers immediately if sync returns during grace period", () => { - const hsConnected = createHomeserverConnected$( - scope, - client, - session, - GRACE_PERIOD, - ); - - // Initial state: Connected - expect(hsConnected.combined$.value).toBe(true); - - // 1. Sync error occurs - client.setSyncState(SyncState.Error); - vi.advanceTimersByTime(GRACE_PERIOD / 2); - expect(hsConnected.combined$.value).toBe(true); - - // 2. Sync recovers BEFORE the grace period expires - client.setSyncState(SyncState.Syncing); - expect(hsConnected.combined$.value).toBe(true); - - // 3. Fast forward the remaining time -> should stay TRUE - vi.advanceTimersByTime(GRACE_PERIOD); - expect(hsConnected.combined$.value).toBe(true); - }); - - it("flips to true IMMEDIATELY even if a grace period was pending", () => { - const hsConnected = createHomeserverConnected$( - scope, - client, - session, - GRACE_PERIOD, - ); - - // 1. Initial error: wait until it flips to false - client.setSyncState(SyncState.Error); - expect(hsConnected.combined$.value).toBe(true); - vi.advanceTimersByTime(GRACE_PERIOD + 1); - expect(hsConnected.combined$.value).toBe(false); - - // 2. Back to Syncing -> Must be TRUE immediately (synchronously) - client.setSyncState(SyncState.Syncing); - expect(hsConnected.combined$.value).toBe(true); - }); - - it('marble: sync "s----e" -> HomeserverConnected "t---------f"', () => { - const ts = new TestScheduler((a, b) => expect(a).toEqual(b)); - - ts.run(({ cold, expectObservable }) => { - const GRACE = 5; - const scope = new ObservableScope(); - - // Setup Mocks - const client = new MockMatrixClient(SyncState.Syncing); + function marbleTest( + syncStateMarbles: string, + expectedConnectedMarbles: string, + ): void { + withTestScheduler(({ behavior, schedule, expectObservable }) => { + const syncState$ = behavior(syncStateMarbles, { + s: SyncState.Syncing, + e: SyncState.Error, + }); + const client = new MockMatrixClient(syncState$.value); + schedule(syncStateMarbles, { + s: () => client.setSyncState(SyncState.Syncing), + e: () => client.setSyncState(SyncState.Error), + }); const session = new MockMatrixRTCSession({ membershipStatus: Status.Connected, probablyLeft: false, }); - - const hs = createHomeserverConnected$(scope, client, session, GRACE); - - // Marble-Input: s (Syncing) at 0ms, e (Error) at 5ms - const syncValues = { s: SyncState.Syncing, e: SyncState.Error }; - const driver$ = cold("s----e", syncValues); - - // Feed Mock-Client with marble values - driver$.subscribe((state) => { - client.setSyncState(state); + const hsConnected = createHomeserverConnected$( + testScope(), + client, + session, + GRACE_PERIOD, + ); + expectObservable(hsConnected.combined$).toBe(expectedConnectedMarbles, { + y: true, + n: false, }); - - const values = { t: true, f: false }; - - // t (0ms: Syncing + Connected = true) - // (5ms: Error occurs, Grace period starts, still true) - // f (10ms: 5ms + 5ms Grace period ends, should flip to false) - expectObservable(hs.combined$).toBe("t---------f", values); - - ts.flush(); - scope.end(); }); + } + + it("respects gracePeriodMs: stays true during grace period and flips false after", () => { + // - Initial state: Everything is connected + // - Sync error occurs -> should remain connected due to grace period + // - After grace period, not connected + marbleTest("se", "y-----n"); + // If the sync error takes longer to occur, it should take equally long for + // the connection state to change + marbleTest("s--e", "y-------n"); + }); + + it("recovers immediately if sync returns during grace period", () => { + // - Initial state: Connected + // - Sync error occurs + // - Sync recovers BEFORE the grace period expires + // - Connection state remains constant + marbleTest("se--s", "y"); + }); + + it("flips to true IMMEDIATELY even if a grace period was pending", () => { + // - Initial error: connection eventually flips to false + // - Back to Syncing -> Must be connected immediately (synchronously) + marbleTest("e-----s", "y----ny"); }); });