Compare commits
1 Commits
v1.0.180
...
85eef74bf7
| Author | SHA1 | Date | |
|---|---|---|---|
| 85eef74bf7 |
@@ -1,6 +1,14 @@
|
||||
name: Build Android APK
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main, master]
|
||||
paths:
|
||||
- 'toju-app/**'
|
||||
- 'package.json'
|
||||
- 'package-lock.json'
|
||||
- 'tools/build-android-apk.sh'
|
||||
- '.gitea/workflows/build-android-apk.yml'
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
@@ -61,39 +69,12 @@ jobs:
|
||||
NODE_ENV: development
|
||||
run: npm ci
|
||||
|
||||
- name: Resolve release version
|
||||
id: version
|
||||
run: node tools/resolve-release-version.js --write-output
|
||||
|
||||
- name: Ensure draft release exists
|
||||
id: release
|
||||
env:
|
||||
GITEA_RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
|
||||
run: >
|
||||
node tools/gitea-release.js ensure-draft
|
||||
--server-url "${{ github.server_url }}"
|
||||
--repository "${{ github.repository }}"
|
||||
--tag "${{ steps.version.outputs.release_tag }}"
|
||||
--target "${{ github.sha }}"
|
||||
--name "${{ steps.version.outputs.release_name }}"
|
||||
--body "Automated draft release from ${{ github.ref_name }} @ ${{ github.sha }}"
|
||||
--write-output
|
||||
|
||||
- name: Build debug APK
|
||||
run: bash tools/build-android-apk.sh
|
||||
|
||||
- name: Stage Android APK
|
||||
run: |
|
||||
mkdir -p dist-android
|
||||
cp toju-app/android/app/build/outputs/apk/debug/app-debug.apk \
|
||||
"dist-android/Toju-${{ steps.version.outputs.release_version }}-android-debug.apk"
|
||||
|
||||
- name: Upload Android APK to draft release
|
||||
env:
|
||||
GITEA_RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
|
||||
run: >
|
||||
node tools/gitea-release.js upload-built-assets
|
||||
--server-url "${{ github.server_url }}"
|
||||
--repository "${{ github.repository }}"
|
||||
--release-id "${{ steps.release.outputs.release_id }}"
|
||||
--dist-android dist-android
|
||||
- name: Upload APK artifact
|
||||
uses: https://github.com/actions/upload-artifact@v4
|
||||
with:
|
||||
name: metoyou-android-debug-apk
|
||||
path: toju-app/android/app/build/outputs/apk/debug/app-debug.apk
|
||||
if-no-files-found: error
|
||||
|
||||
@@ -110,87 +110,6 @@ jobs:
|
||||
--dist-electron dist-electron
|
||||
--dist-server dist-server
|
||||
|
||||
build-android:
|
||||
needs: prepare
|
||||
runs-on: ubuntu-latest
|
||||
container: node:22
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: https://github.com/actions/checkout@v4
|
||||
|
||||
- name: Restore npm cache
|
||||
uses: https://github.com/actions/cache@v4
|
||||
with:
|
||||
path: /root/.npm
|
||||
key: npm-android-${{ hashFiles('package-lock.json') }}
|
||||
restore-keys: npm-android-
|
||||
|
||||
- name: Restore Gradle cache
|
||||
uses: https://github.com/actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
/root/.gradle/caches
|
||||
/root/.gradle/wrapper
|
||||
key: gradle-android-${{ hashFiles('toju-app/android/**/*.gradle*', 'toju-app/android/gradle/wrapper/gradle-wrapper.properties') }}
|
||||
restore-keys: gradle-android-
|
||||
|
||||
- name: Install Android build toolchain
|
||||
run: |
|
||||
apt-get update
|
||||
apt-get install -y --no-install-recommends wget unzip ca-certificates gnupg
|
||||
|
||||
install -d /etc/apt/keyrings
|
||||
wget -qO - https://packages.adoptium.net/artifactory/api/gpg/key/public | gpg --dearmor -o /etc/apt/keyrings/adoptium.gpg
|
||||
echo "deb [signed-by=/etc/apt/keyrings/adoptium.gpg] https://packages.adoptium.net/artifactory/deb bookworm main" > /etc/apt/sources.list.d/adoptium.list
|
||||
apt-get update
|
||||
apt-get install -y --no-install-recommends temurin-21-jdk
|
||||
|
||||
export ANDROID_SDK_ROOT=/opt/android-sdk
|
||||
mkdir -p "$ANDROID_SDK_ROOT/cmdline-tools"
|
||||
cd /tmp
|
||||
wget -q https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip
|
||||
unzip -q commandlinetools-linux-11076708_latest.zip
|
||||
mv cmdline-tools "$ANDROID_SDK_ROOT/cmdline-tools/latest"
|
||||
export PATH="$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/platform-tools"
|
||||
|
||||
yes | sdkmanager --licenses >/dev/null
|
||||
sdkmanager "platform-tools" "platforms;android-36" "build-tools;35.0.0"
|
||||
|
||||
echo "ANDROID_SDK_ROOT=$ANDROID_SDK_ROOT" >> "$GITHUB_ENV"
|
||||
echo "ANDROID_HOME=$ANDROID_SDK_ROOT" >> "$GITHUB_ENV"
|
||||
echo "JAVA_HOME=/usr/lib/jvm/temurin-21-jdk-amd64" >> "$GITHUB_ENV"
|
||||
echo "PATH=$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/platform-tools" >> "$GITHUB_ENV"
|
||||
|
||||
- name: Install dependencies
|
||||
env:
|
||||
NODE_ENV: development
|
||||
run: npm ci
|
||||
|
||||
- name: Set CI release version
|
||||
run: >
|
||||
node tools/set-release-version.js
|
||||
--version "${{ needs.prepare.outputs.release_version }}"
|
||||
|
||||
- name: Build debug APK
|
||||
run: bash tools/build-android-apk.sh
|
||||
|
||||
- name: Stage Android APK
|
||||
run: |
|
||||
mkdir -p dist-android
|
||||
cp toju-app/android/app/build/outputs/apk/debug/app-debug.apk \
|
||||
"dist-android/Toju-${{ needs.prepare.outputs.release_version }}-android-debug.apk"
|
||||
|
||||
- name: Upload Android APK
|
||||
env:
|
||||
GITEA_RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
|
||||
run: >
|
||||
node tools/gitea-release.js upload-built-assets
|
||||
--server-url "${{ github.server_url }}"
|
||||
--repository "${{ github.repository }}"
|
||||
--release-id "${{ needs.prepare.outputs.release_id }}"
|
||||
--dist-android dist-android
|
||||
|
||||
build-windows:
|
||||
needs: prepare
|
||||
runs-on: windows
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -59,7 +59,6 @@ Thumbs.db
|
||||
.env
|
||||
.certs/
|
||||
/server/data/variables.json
|
||||
/server/data/metoyou.sqlite
|
||||
dist-server/*
|
||||
|
||||
doc/**
|
||||
|
||||
@@ -124,8 +124,7 @@ Behavioral changes to any of these qualify as a feature-doc update under the rul
|
||||
- `release-draft.yml` — queues release builds on push to `main` / `master`
|
||||
- `publish-draft-release.yml` — publishes draft releases
|
||||
- `deploy-web-apps.yml` — deploys the marketing site and Docusaurus docs
|
||||
- `build-android-apk.yml` — manual **workflow_dispatch** debug Capacitor Android APK build; uploads `Toju-<version>-android-debug.apk` to the draft release (same path as desktop assets)
|
||||
- `release-draft.yml` job `build-android` — builds and uploads the debug APK to each queued draft release alongside desktop/server archives
|
||||
- `build-android-apk.yml` — builds a debug Capacitor Android APK on push (mobile-related paths) or manual dispatch; uploads `app-debug.apk` as a workflow artifact
|
||||
- All checks must pass before merging a PR
|
||||
- Workflow status is visible in the Gitea PR view; use the web UI or `tea` CLI to inspect runs
|
||||
|
||||
|
||||
@@ -9,9 +9,7 @@ It must stay accurate as new features are introduced, renamed, merged, or remove
|
||||
## Feature list (alphabetical)
|
||||
|
||||
- [App i18n](features/app-i18n.md) — `@ngx-translate/core` localization for the product client; English-only catalog today, same stack as the marketing website.
|
||||
- [Authentication](features/authentication.md) — signaling-server session tokens, protected REST/WebSocket identity, and client bearer storage.
|
||||
- [Custom Emoji](features/custom-emoji.md) — peer-synced user-created emoji assets, chat reaction shortcuts, and composer emoji insertion.
|
||||
- [Message Integrity](features/message-integrity.md) — signed P2P message revision chains, inventory `headHash` convergence, and Ed25519 signing-key registration on the signaling server.
|
||||
- [Mobile Capacitor](features/mobile-capacitor.md) — Capacitor native shell, mobile infrastructure facades, and phone-specific call/chat/media integrations.
|
||||
- [Server Discovery](features/server-discovery.md) — featured/trending public-server REST endpoints (server) consumed by the `/dashboard` and `/servers` client pages.
|
||||
- [Signal Server Tag](features/signal-server-tag.md) — configurable signal-server display tag shown on profile cards for a user's registration server.
|
||||
|
||||
@@ -25,55 +25,6 @@ Durable rules for AI agents working on this project. Read this file at session s
|
||||
|
||||
## Lessons
|
||||
|
||||
### Server registration needs `ownerPublicKey: oderId || id`, and must not be fire-and-forget [server-directory] [rooms]
|
||||
|
||||
- **Trigger:** creating a server appeared to work (the creator landed in the room view) but the server didn't exist on the backend — invite-link creation and search both 404'd. `createRoom$` sent `ownerPublicKey: currentUser.oderId` with no fallback; on restored sessions `oderId` can be falsy (identify still works because it falls back to `id`), so `POST /api/servers` returned `400 Missing required fields`, and the `.subscribe()` swallowed the error while `createRoomSuccess` fired regardless.
|
||||
- **Rule:** resolve owner identity as `oderId || id` everywhere it's required (the server rejects an empty `ownerPublicKey`), and give `registerServer().subscribe()` an `error` handler so a failed registration is never silent.
|
||||
- **Why:** verified against the live server — authed POST with a truthy `ownerPublicKey` → 201; authed POST with an empty one → 400; the swallowed 400 is exactly what produces a "ghost" room the creator can enter but no one can find.
|
||||
- **Example:** `buildServerRegistrationPayload(room, currentUser, normalizedPassword)` in `toju-app/src/app/store/rooms/server-registration.rules.ts`, used by `RoomsEffects.createRoom$`.
|
||||
|
||||
### Identify must fall back to the legacy session token, not only the new credential store [realtime] [authentication]
|
||||
|
||||
- **Trigger:** the multi-signal-server auth refactor changed `resolveCredentialForSignalUrl` to read *only* `SignalServerCredentialStoreService`; sessions restored from disk (and logins where `user.homeSignalServerUrl` is unset) have an empty credential store, so `identify` was skipped on every signal server ("Skipping identify because no session token is available") and users appeared alone — no presence, no peers, sent messages visible only to themselves. E2E never caught it because every e2e flow does a *fresh* register/login that writes the credential store directly.
|
||||
- **Rule:** when resolving the identify credential for a signal URL, prefer the per-signal credential but fall back to the legacy `AuthTokenStoreService` token reconstructed with the current home user's `id`/`displayName`; never gate `identify` solely on the new credential store.
|
||||
- **Why:** `persistSessionToken` always writes the legacy `metoyou.authTokens` store on login, but the per-signal credential store is only populated on fresh login (with a `loginResponse`) or successful migration/provisioning — so on reload it can be empty while a valid session still exists.
|
||||
- **Example:** `resolveSignalIdentity(credential, legacyTokenEntry, homeUser)` in `signal-server-credential-resolution.rules.ts`, wired through `SignalServerAuthService.resolveCredentialForSignalUrl` (which now passes `this.authTokenStore.getTokenEntry(httpUrl)` and a `homeUser` carrying `id`). Test cross-user behavior via a *session-restore* path, not just fresh login.
|
||||
|
||||
### Keep the per-signal-URL identify credential resolvable from the store [realtime] [authentication]
|
||||
|
||||
- **Trigger:** after the multi-signal-server auth refactor, `SignalingManager.getLastIdentify` was switched to `getIdentifyCredentialsForSignalUrl`, which only read an in-memory cache populated *after* `identify()` ran; a freshly (re)connected socket then emitted `join_server` before any identify and users silently never appeared in the presence roster (almost all multi-user e2e tests timed out waiting for the peer's `room-user-card`).
|
||||
- **Rule:** `getIdentifyCredentialsForSignalUrl` must fall back to resolving the credential from the credential store so a new socket's `onopen` re-identifies before it re-joins; never restrict it to only the in-memory identify cache.
|
||||
- **Why:** the server drops `join_server`/`view_server` on any unauthenticated connection, so an identify-less join is lost with no error and recovery only happens on a later reconnect (often beyond the 20s test timeout).
|
||||
- **Example:** server log showed `join_server authed=false ... display=User` dropped, then `User identified: Alice` on a different connection but no `Alice joined server`; fixed in `signaling-transport-handler.ts` by resolving via `dependencies.resolveCredential(signalUrl)` when the cache is empty.
|
||||
|
||||
### Store clientInstanceId in sessionStorage not localStorage [realtime] [multi-device]
|
||||
|
||||
- **Trigger:** same user logged in on two tabs, browsers, or synced profiles sees alternating "Disconnected from signaling server" and no cross-device chat/voice sync.
|
||||
- **Rule:** persist `metoyou.clientInstanceId` in `sessionStorage` (one id per tab/window) and clear any legacy `localStorage` copy on first read.
|
||||
- **Why:** server identify evicts stale sockets with the same `(oderId, connectionScope, clientInstanceId)` tuple; a shared localStorage id makes each client kick the other in a reconnect loop.
|
||||
- **Example:** `ClientInstanceService.getClientInstanceId()` writes to `sessionStorage`; two tabs get different ids and stay connected simultaneously.
|
||||
|
||||
### Revalidate IndexedDB scope without reinitializing on every read [persistence] [performance]
|
||||
|
||||
- **Trigger:** `DatabaseService.ensureReady()` called `initialize()` before every delegated read/write to fix user-scope races.
|
||||
- **Rule:** cache the last validated `metoyou_currentUserId` and only re-run backend initialization when that scope changes or an in-flight initialize completes with a different scope.
|
||||
- **Why:** per-operation revalidation fans out across ban lookups, room loads, and message reads, causing channel/chat UI to stay blank until repeated server clicks eventually win the race.
|
||||
- **Example:** `ensureReady()` returns immediately when `isReady()` and `validatedUserScope` still match `getStoredCurrentUserId()`.
|
||||
|
||||
### Restore local user scope before protected writes [authentication] [persistence]
|
||||
|
||||
- **Trigger:** a logged-in in-memory user can create rooms or messages after `metoyou_currentUserId` was cleared by a late session-expired path.
|
||||
- **Rule:** before protected local persistence or server-directory actions, restore `metoyou_currentUserId` from the current user and avoid treating a live current user as unauthenticated.
|
||||
- **Why:** otherwise rooms/messages fall into the anonymous IndexedDB scope, and route checks redirect to login even though NgRx still has the authenticated user.
|
||||
- **Example:** `MessagesEffects.sendMessage$`, `RoomsEffects.createRoom$`, and server-directory create/join components call `setStoredCurrentUserId(currentUser.id)` before writing or joining.
|
||||
|
||||
### Persisted local user state still requires a session token [authentication] [signaling]
|
||||
|
||||
- **Trigger:** Users appear logged in from local storage but cannot see peers online or send chat after session-token auth shipped.
|
||||
- **Rule:** before connecting signaling or loading rooms for a persisted user, require a non-expired token in `metoyou.authTokens`; redirect to `/login` on `SESSION_EXPIRED`, `auth_required`, or `auth_error`.
|
||||
- **Why:** WebSocket `identify` is skipped without a token, so `join_server`, RTC relay, and presence never establish even though the profile exists locally.
|
||||
- **Example:** `hasValidPersistedSession()` in `auth-session.rules.ts` from `loadCurrentUser$`.
|
||||
|
||||
### Declare MODIFY_AUDIO_SETTINGS for Android WebRTC mic capture [mobile] [android]
|
||||
|
||||
- **Trigger:** Android users accept the microphone prompt but voice calls and channels still fail to join.
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
# ADR-0002: Session-Token Authentication on the Signaling Server
|
||||
|
||||
## Status
|
||||
Accepted
|
||||
|
||||
## Context
|
||||
The signaling server trusted client-supplied user IDs on REST mutations and WebSocket `identify`, allowing impersonation for kicks, bans, joins, plugin administration, and push dispatch. The product client already used bearer tokens for the Electron Local API, but the shared signaling server had no equivalent binding between HTTP/WebSocket actions and a logged-in user.
|
||||
|
||||
## Decision
|
||||
Issue opaque session tokens on login/register, persist them in server SQLite, require `Authorization: Bearer` on all mutating REST routes, and require `identify.token` on WebSocket connections before any other client message is accepted. Actor fields (`currentOwnerId`, `actorUserId`, `requesterUserId`) are derived from the token instead of request bodies.
|
||||
|
||||
## Rationale
|
||||
This closes identity spoofing without changing the P2P product model: discovery stays public, chat/media still relay over WebSocket, and DM WebRTC signaling remains available across servers. Bcrypt password hashing with transparent SHA-256 upgrade preserves existing accounts. A deprecation window for body-only auth was intentionally omitted so all clients must authenticate in one release, avoiding prolonged dual-trust behavior.
|
||||
@@ -1,16 +0,0 @@
|
||||
# ADR-0003: Multi-Client Sessions with Connection-Scoped Routing
|
||||
|
||||
## Status
|
||||
Accepted
|
||||
|
||||
## Context
|
||||
Users expect to stay logged in on multiple devices simultaneously (Discord-style). The signaling server already issued multiple session tokens per user, but WebSocket broadcasts deduplicated by `oderId`, which prevented a user's second device from receiving chat, typing, or voice-state updates from their first device. Voice had no per-device identity, so two clients could both attempt to transmit audio.
|
||||
|
||||
## Decision
|
||||
Introduce a stable per-install `clientInstanceId` on the product client. Route server broadcasts by **connection id** (exclude only the sender socket) while keeping presence `user_joined` / `user_left` identity-scoped. Track `voiceActive` per connection; relay RTC to the voice-active socket. Enforce single voice owner per user via `VoiceState.clientInstanceId` and `voice_client_takeover` handoff between connections.
|
||||
|
||||
## Consequences
|
||||
- **Positive:** Chat and presence sync across a user's devices; voice behaves like Discord (one transmitting client, passive viewers, explicit takeover).
|
||||
- **Positive:** Stale-tab hygiene uses `(oderId, connectionScope, clientInstanceId)` eviction without kicking other devices.
|
||||
- **Negative:** `findUserByOderId` semantics change — RTC now prefers voice-active connections; callers must not assume one socket per user.
|
||||
- **Negative:** Clients must include `clientInstanceId` on identify and voice payloads; older builds without it still work but cannot participate in multi-device voice exclusivity reliably.
|
||||
@@ -1,23 +0,0 @@
|
||||
# ADR-0003: Signed Message Revision Chains for P2P Chat Integrity
|
||||
|
||||
## Status
|
||||
Accepted
|
||||
|
||||
## Context
|
||||
P2P chat sync compared timestamps, reaction counts, and attachment counts only. A peer could rewrite history or apply edits out of order with no cryptographic check. The product has no central message store, so integrity must travel with sync traffic and local audit logs.
|
||||
|
||||
## Decision
|
||||
Adopt an append-only **revision chain** per message:
|
||||
|
||||
- Each mutation emits a `MessageRevision` (create, edit, delete, moderation, plugin) with `revision`, `prevRevisionHash`, and `headHash` (SHA-256 over canonical head state).
|
||||
- Inventories advertise `{ revision, headHash }` so peers detect gaps and hash mismatches.
|
||||
- Human-authored revisions are signed with per-user Ed25519 keys; public keys are registered on the signaling server for verification.
|
||||
- Legacy `chat-message` / `message-edited` / `message-deleted` events continue to broadcast alongside `message-revision` for one-release backward compatibility.
|
||||
|
||||
## Rationale
|
||||
Revision chains give deterministic merge (higher valid revision wins) without requiring a trusted relay. Signing binds edits to registered users while keeping chat payloads off the server. Dual emit avoids breaking peers that have not upgraded inventory or revision handlers yet.
|
||||
|
||||
## Consequences
|
||||
- New persistence columns and revision audit stores on browser IDB, Electron SQLite, and Capacitor schemas.
|
||||
- Plugin synthetic users may emit unsigned revisions until a plugin signing model exists.
|
||||
- Attachment byte integrity (SHA-256 on `file-announce`) remains a separate follow-up.
|
||||
@@ -1,91 +0,0 @@
|
||||
# Authentication
|
||||
|
||||
Session-token authentication for the signaling server and product client.
|
||||
|
||||
## Trust boundaries
|
||||
|
||||
| Surface | Identity proof | Notes |
|
||||
|---|---|---|
|
||||
| Signaling server REST (mutations) | `Authorization: Bearer <token>` | Actor user IDs in request bodies are ignored; server derives `authUserId` from the token |
|
||||
| Signaling server REST (discovery) | None | `GET /api/servers`, featured/trending/search remain public |
|
||||
| Signaling server WebSocket | `identify.token` | Connections must identify before any other message type |
|
||||
| Electron Local API | Separate in-memory bearer tokens | Proxies login to allowed signaling servers only |
|
||||
| Product client local DB | OS user account | SQLite and attachments are plaintext at rest |
|
||||
|
||||
## Login / register response
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "<uuid>",
|
||||
"username": "alice",
|
||||
"displayName": "Alice",
|
||||
"token": "<opaque-hex>",
|
||||
"expiresAt": 1710000000000
|
||||
}
|
||||
```
|
||||
|
||||
- Tokens are opaque 64-character hex strings stored in server SQLite (`session_tokens`).
|
||||
- Default TTL: 10 years (`SESSION_TOKEN_TTL_MS` env override supported on the signaling server).
|
||||
- Passwords are stored with bcrypt; legacy SHA-256 hashes are upgraded transparently on successful login.
|
||||
|
||||
## Protected REST routes
|
||||
|
||||
Require `Authorization: Bearer`:
|
||||
|
||||
- `PUT/POST/DELETE` under `/api/servers/*` (except public `GET`)
|
||||
- `PUT /api/requests/:id`
|
||||
- Plugin-support mutations under `/api/servers/:serverId/plugins/*`
|
||||
- `/api/users/device-tokens/*`
|
||||
- `POST /api/users/logout`
|
||||
|
||||
## WebSocket identify contract
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "identify",
|
||||
"token": "<session-token>",
|
||||
"oderId": "<user-id>",
|
||||
"displayName": "Alice",
|
||||
"connectionScope": "ws://host:3001",
|
||||
"clientInstanceId": "<per-install-uuid>"
|
||||
}
|
||||
```
|
||||
|
||||
- `oderId` must match the token's user id when provided.
|
||||
- `clientInstanceId` is a stable per-install UUID generated by the product client (`metoyou.clientInstanceId` in `localStorage`). The signaling server uses it to distinguish multiple WebSocket connections for the same user and to route voice ownership.
|
||||
- Server responds with `auth_error` or `auth_required` when authentication fails.
|
||||
|
||||
## Multi-device sessions
|
||||
|
||||
- Each login/register issues a **new** session token; prior tokens remain valid until they expire or the client calls `POST /api/users/logout` with that token.
|
||||
- The same user may keep multiple WebSocket connections open (different devices or browser profiles). Server broadcasts (chat, typing, voice state, status) exclude only the **sending connection**, so other connections for that identity still receive updates.
|
||||
- Voice/WebRTC is exclusive per user: only one `clientInstanceId` may own active voice at a time. Other connections show passive UI and can send `voice_client_takeover` to move voice to the local device.
|
||||
- Stale reconnect hygiene: when a client re-identifies with the same `(oderId, connectionScope, clientInstanceId)` tuple, the server closes the older socket for that tuple.
|
||||
|
||||
## Client storage
|
||||
|
||||
The product client stores tokens per signaling-server base URL in `localStorage` (`metoyou.authTokens`). An HTTP interceptor attaches the bearer token to `/api/*` requests targeting that server.
|
||||
|
||||
Per-server credentials (`metoyou.signalServerCredentials`) map each normalized signal-server URL to the authenticated user id, username, display name, session token, expiry, and whether the account was auto-provisioned. The home user profile in SQLite/NgRx remains the device-local identity (`homeSignalServerUrl`); foreign-server credentials are a side map used for REST and WebSocket identify on that URL.
|
||||
|
||||
A per-install **provision secret** enables silent account creation on newly added or encountered signal servers. It is generated on home register/login, stored in Electron `safeStorage` when available (sessionStorage fallback on web), and never persisted as the user's visible login password.
|
||||
|
||||
### Multi-signal-server auth flows
|
||||
|
||||
| Flow | Action | Effect |
|
||||
|---|---|---|
|
||||
| Home login/register | `authenticateUser` | Resets local state, stores home credential + provision secret |
|
||||
| Foreign login/register | `authorizeSignalServer` | Upserts credential for that URL only; home session unchanged |
|
||||
| Auto-provision | `SignalServerProvisionerService` | Registers or logs in on foreign server using provision secret; on username collision tries suffixed username (`alice-<homeUserIdPrefix>`) |
|
||||
| Foreign auth failure | `signalServerAuthFailed` | Clears that URL's credential and re-provisions when home token is still valid; global logout only when home server rejects auth |
|
||||
|
||||
Authorize UI: `/login?mode=authorize&serverId=…&returnUrl=…` (also supported on `/register`). Settings → Network shows per-endpoint `Authorized` / `Needs sign-in` badges.
|
||||
|
||||
Persisted local user state (`metoyou_currentUserId` + IndexedDB/SQLite profile) is **not** sufficient to use chat or presence. On startup, `loadCurrentUser$` requires a non-expired session token for the user's home signaling server (or any stored token as a fallback). Missing or rejected **home** tokens dispatch `SESSION_EXPIRED` and redirect to `/login`. Foreign-server `auth_required` / `auth_error` responses clear only that server's credential and attempt re-provision.
|
||||
|
||||
## Security considerations
|
||||
|
||||
- Rate limits: login/register (100 / 15 min), server join (30 / min).
|
||||
- CORS allowlist: optional `corsAllowlist` in `server/data/variables.json` or `CORS_ALLOWLIST` env (comma-separated). Empty allowlist keeps permissive CORS for local development.
|
||||
- Push-token routes require bearer auth and user-id match.
|
||||
- RTC relay: direct-message/direct-call types always relay; server-icon types require shared server membership; WebRTC offer/answer/ice remain open for cross-server DM WebRTC.
|
||||
@@ -1,60 +0,0 @@
|
||||
# Message Integrity
|
||||
|
||||
Signed, append-only **message revisions** give P2P chat a verifiable history without central message storage. The materialized `Message` row in local SQLite/IDB is a cache; peers converge via inventory snapshots and revision events.
|
||||
|
||||
## Responsibilities
|
||||
|
||||
- **Revision chain** — Every create, edit, delete, moderation, or plugin mutation appends a `MessageRevision` with monotonically increasing `revision`, `prevRevisionHash`, and `headHash`.
|
||||
- **Dual emit** — Outgoing mutations broadcast the legacy event (`chat-message`, `message-edited`, `message-deleted`) **and** `message-revision` so older peers keep working while integrity-aware peers prefer revisions.
|
||||
- **Inventory** — Sync inventories include `{ id, ts, rc, ac, revision, headHash }`. Peers re-fetch when remote revision is newer or the same revision has a different hash (tamper detection).
|
||||
- **Signing** — Human authors sign revisions with per-user Ed25519 keys. Public keys are registered on the signaling server; private keys stay in browser `localStorage`.
|
||||
|
||||
## Boundaries
|
||||
|
||||
| Layer | Owns |
|
||||
| --- | --- |
|
||||
| Product client (`toju-app`) | Revision construction, merge, verification, P2P broadcast, local persistence |
|
||||
| Signaling server (`server`) | `PUT /api/users/me/signing-key`, `GET /api/users/:id/signing-public-key` — key directory only, no message storage |
|
||||
| Electron / mobile persistence | `revision` + `headHash` on message rows; revision audit log (IDB store / SQLite meta) |
|
||||
|
||||
Plugin API messages may emit unsigned revisions (`plugin-edit` / `plugin-delete`) when the actor is a synthetic plugin user.
|
||||
|
||||
## Key types
|
||||
|
||||
- `Message.revision`, `Message.headHash` — materialized cache fields on the shared `Message` model.
|
||||
- `MessageRevision` — wire + persistence audit record (`message-revision.models.ts`).
|
||||
- `MessageRevisionType` — `create`, `author-edit`, `author-delete`, `moderate-edit`, `moderate-delete`, `plugin-edit`, `plugin-delete`.
|
||||
- `ChatEvent.type: 'message-revision'` — P2P envelope carrying a full `MessageRevision`.
|
||||
|
||||
## Merge rules
|
||||
|
||||
1. Valid signed revision with higher `revision` wins over legacy timestamp edits.
|
||||
2. Same `revision`, different `headHash` → treat as stale/tampered and re-fetch.
|
||||
3. Unsigned revisions (no `signature`) are accepted for backward compatibility when verification is skipped.
|
||||
4. Legacy peers without `revision`/`headHash` in inventory fall back to `ts` / `rc` / `ac` comparison.
|
||||
|
||||
## Client touchpoints
|
||||
|
||||
- Domain rules: `message-integrity.rules.ts`, `message-revision.builder.rules.ts`, `message-sync.rules.ts`
|
||||
- Services: `MessageRevisionService`, `MessageSigningService`
|
||||
- Store: `messages.effects.ts` (outgoing dual-emit), `messages-incoming.handlers.ts` (`handleMessageRevision`), `messages.helpers.ts` (inventory + merge)
|
||||
- Plugins: `plugin-client-api.service.ts` emits revisions for send/edit/delete
|
||||
|
||||
## Server API
|
||||
|
||||
| Method | Path | Auth | Body / response |
|
||||
| --- | --- | --- | --- |
|
||||
| `PUT` | `/api/users/me/signing-key` | Bearer | `{ publicKeyJwk }` — stores Ed25519 public JWK on the user row |
|
||||
| `GET` | `/api/users/:id/signing-public-key` | Public | `{ publicKeyJwk }` — used by peers to verify signatures |
|
||||
|
||||
Registration runs automatically after login/register via `AuthenticationService`.
|
||||
|
||||
## Degraded-mode behavior
|
||||
|
||||
- Outgoing revision signing is **best-effort**: if `Ed25519` signing fails, the client still broadcasts the legacy `chat-message` envelope (unsigned revision).
|
||||
- Incoming signed revisions are accepted without cryptographic verification when the sender's public key is not yet registered on the server, so chat is not blocked during key-registration races.
|
||||
|
||||
## Testing
|
||||
|
||||
- Unit: `message-integrity.rules.spec.ts`, `message-revision.builder.rules.spec.ts`, `message-revision-signing.rules.spec.ts`, `message-sync.rules.spec.ts`, `messages-incoming.handlers.spec.ts`
|
||||
- Outgoing revision wiring is covered indirectly through existing message effect tests; add focused specs when changing merge or signing behavior.
|
||||
@@ -48,11 +48,9 @@ Config: `toju-app/capacitor.config.ts` (`webDir: ../dist/client/browser`).
|
||||
|
||||
### CI (Gitea)
|
||||
|
||||
Release workflow `.gitea/workflows/release-draft.yml` builds a debug Android APK on every push to `main` / `master` (job `build-android`), stages it as `Toju-<version>-android-debug.apk`, and uploads it to the same draft Gitea release as the desktop `.exe` / `.deb` assets via `tools/gitea-release.js`.
|
||||
Workflow `.gitea/workflows/build-android-apk.yml` runs automatically on push to `main` / `master` when mobile client paths change (`toju-app/**`, root lockfile, or the workflow itself). Use **workflow_dispatch** to build on demand from any branch.
|
||||
|
||||
Manual-only workflow `.gitea/workflows/build-android-apk.yml` (**workflow_dispatch**) repeats the same build and release upload on demand from any branch.
|
||||
|
||||
Both jobs install JDK 21 and Android SDK platform 36 inside the `node:22` container and run `tools/build-android-apk.sh`. No signing keystore is configured — output is a **debug** APK suitable for sideloading and QA.
|
||||
The job installs JDK 21 and Android SDK platform 36 inside the `node:22` container, runs `tools/build-android-apk.sh`, and uploads `metoyou-android-debug-apk` (`app-debug.apk`) as a workflow artifact. No signing keystore is configured — output is a **debug** APK suitable for sideloading and QA.
|
||||
|
||||
Optional `google-services.json` is not injected in CI; push registration in artifact builds follows the same optional-Firebase behavior as local unsigned debug builds.
|
||||
|
||||
|
||||
@@ -48,8 +48,7 @@ export const test = base.extend<MultiClientFixture>({
|
||||
|
||||
const context = await browser.newContext({
|
||||
permissions: ['microphone', 'camera'],
|
||||
baseURL: 'http://localhost:4200',
|
||||
viewport: { width: 1440, height: 900 }
|
||||
baseURL: 'http://localhost:4200'
|
||||
});
|
||||
|
||||
await installTestServerEndpoint(context, testServer.port);
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
import { expect, type Page } from '@playwright/test';
|
||||
|
||||
export async function openTitleBarMenu(page: Page): Promise<void> {
|
||||
const menuButton = page.getByRole('button', { name: 'Menu' });
|
||||
|
||||
await expect(menuButton).toBeVisible({ timeout: 15_000 });
|
||||
await menuButton.click();
|
||||
await expect(page.locator('app-title-bar .absolute.right-0.top-full').first()).toBeVisible({ timeout: 10_000 });
|
||||
}
|
||||
|
||||
export async function openPluginStore(page: Page): Promise<void> {
|
||||
await openTitleBarMenu(page);
|
||||
await page.getByRole('button', { name: 'Plugin Store' }).click();
|
||||
await expect(page).toHaveURL(/\/plugin-store/, { timeout: 20_000 });
|
||||
}
|
||||
|
||||
export async function openSettingsFromMenu(page: Page): Promise<void> {
|
||||
await openTitleBarMenu(page);
|
||||
await page.getByRole('button', { name: 'Settings' }).click();
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
import { type APIRequestContext, type Page } from '@playwright/test';
|
||||
|
||||
export const AUTH_TOKENS_STORAGE_KEY = 'metoyou.authTokens';
|
||||
export const SIGNAL_SERVER_CREDENTIALS_STORAGE_KEY = 'metoyou.signalServerCredentials';
|
||||
|
||||
export interface AuthSession {
|
||||
id: string;
|
||||
username: string;
|
||||
displayName: string;
|
||||
token: string;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
export function authHeaders(token: string): Record<string, string> {
|
||||
return {
|
||||
Authorization: `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
}
|
||||
|
||||
export async function registerTestUser(
|
||||
request: APIRequestContext,
|
||||
baseUrl: string,
|
||||
username: string,
|
||||
password: string,
|
||||
displayName?: string
|
||||
): Promise<AuthSession> {
|
||||
const response = await request.post(`${baseUrl}/api/users/register`, {
|
||||
data: {
|
||||
username,
|
||||
password,
|
||||
displayName: displayName ?? username
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok()) {
|
||||
throw new Error(`Failed to register test user ${username}: ${response.status()} ${await response.text()}`);
|
||||
}
|
||||
|
||||
return await response.json() as AuthSession;
|
||||
}
|
||||
|
||||
export async function loginTestUser(
|
||||
request: APIRequestContext,
|
||||
baseUrl: string,
|
||||
username: string,
|
||||
password: string
|
||||
): Promise<AuthSession> {
|
||||
const response = await request.post(`${baseUrl}/api/users/login`, {
|
||||
data: { username, password }
|
||||
});
|
||||
|
||||
if (!response.ok()) {
|
||||
throw new Error(`Failed to login test user ${username}: ${response.status()} ${await response.text()}`);
|
||||
}
|
||||
|
||||
return await response.json() as AuthSession;
|
||||
}
|
||||
|
||||
export async function readSignalServerCredentialFromPage(
|
||||
page: Page,
|
||||
serverUrl: string
|
||||
): Promise<{ userId: string; token: string; username: string } | null> {
|
||||
return await page.evaluate(({ storageKey, url }) => {
|
||||
try {
|
||||
const store = JSON.parse(localStorage.getItem(storageKey) || '{}') as Record<string, {
|
||||
userId: string;
|
||||
token: string;
|
||||
username: string;
|
||||
expiresAt: number;
|
||||
}>;
|
||||
const normalizedUrl = url.trim().replace(/\/+$/, '');
|
||||
const entry = store[normalizedUrl];
|
||||
|
||||
if (!entry || entry.expiresAt <= Date.now()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
userId: entry.userId,
|
||||
token: entry.token,
|
||||
username: entry.username
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}, { storageKey: SIGNAL_SERVER_CREDENTIALS_STORAGE_KEY, url: serverUrl });
|
||||
}
|
||||
|
||||
export async function readAuthTokenFromPage(page: Page, serverUrl: string): Promise<string | null> {
|
||||
return await page.evaluate(({ storageKey, url }) => {
|
||||
try {
|
||||
const store = JSON.parse(localStorage.getItem(storageKey) || '{}') as Record<string, { token: string; expiresAt: number }>;
|
||||
const normalizedUrl = url.trim().replace(/\/+$/, '');
|
||||
const entry = store[normalizedUrl];
|
||||
|
||||
if (!entry || entry.expiresAt <= Date.now()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry.token;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}, { storageKey: AUTH_TOKENS_STORAGE_KEY, url: serverUrl });
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import { expect, type Page } from '@playwright/test';
|
||||
|
||||
/** Dashboard omnibox (desktop placeholder copy changed with i18n refresh). */
|
||||
export function dashboardSearchInput(page: Page) {
|
||||
return page.getByRole('textbox', { name: 'Search people, servers, and invites' });
|
||||
}
|
||||
|
||||
export async function expectDashboardReady(page: Page, timeout = 30_000): Promise<void> {
|
||||
await expect(page).toHaveURL(/\/dashboard/, { timeout });
|
||||
await expect(dashboardSearchInput(page)).toBeVisible({ timeout });
|
||||
}
|
||||
@@ -1,219 +0,0 @@
|
||||
import { expect, type Page } from '@playwright/test';
|
||||
import { type Client } from '../fixtures/multi-client';
|
||||
import { LoginPage } from '../pages/login.page';
|
||||
import { RegisterPage } from '../pages/register.page';
|
||||
import { ServerSearchPage } from '../pages/server-search.page';
|
||||
import { ChatRoomPage } from '../pages/chat-room.page';
|
||||
import { ChatMessagesPage } from '../pages/chat-messages.page';
|
||||
|
||||
export const MULTI_DEVICE_PASSWORD = 'TestPass123!';
|
||||
export const MULTI_DEVICE_VOICE_CHANNEL = 'General';
|
||||
|
||||
export interface MultiDeviceCredentials {
|
||||
username: string;
|
||||
displayName: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface MultiDeviceScenario {
|
||||
clientA: Client;
|
||||
clientB: Client;
|
||||
credentials: MultiDeviceCredentials;
|
||||
serverName: string;
|
||||
messagesA: ChatMessagesPage;
|
||||
messagesB: ChatMessagesPage;
|
||||
roomA: ChatRoomPage;
|
||||
roomB: ChatRoomPage;
|
||||
}
|
||||
|
||||
export function uniqueMultiDeviceName(prefix: string): string {
|
||||
return `${prefix}-${Date.now()}-${Math.floor(Math.random() * 10_000)}`;
|
||||
}
|
||||
|
||||
export async function createMultiDeviceScenario(
|
||||
createClient: () => Promise<Client>,
|
||||
options: { suffix?: string; serverDescription?: string } = {}
|
||||
): Promise<MultiDeviceScenario> {
|
||||
const suffix = options.suffix ?? uniqueMultiDeviceName('multi-device');
|
||||
const credentials: MultiDeviceCredentials = {
|
||||
username: `multi_${suffix}`,
|
||||
displayName: 'Multi Device User',
|
||||
password: MULTI_DEVICE_PASSWORD
|
||||
};
|
||||
const serverName = `Multi Device Server ${suffix}`;
|
||||
const clientA = await createClient();
|
||||
const clientB = await createClient();
|
||||
|
||||
await warmClientPage(clientA.page);
|
||||
await warmClientPage(clientB.page);
|
||||
|
||||
const registerPage = new RegisterPage(clientA.page);
|
||||
|
||||
await registerPage.goto();
|
||||
await registerPage.register(credentials.username, credentials.displayName, credentials.password);
|
||||
await expect(clientA.page).toHaveURL(/\/dashboard/, { timeout: 15_000 });
|
||||
|
||||
const searchA = new ServerSearchPage(clientA.page);
|
||||
|
||||
await searchA.createServer(serverName, {
|
||||
description: options.serverDescription ?? 'Multi-device session coverage'
|
||||
});
|
||||
|
||||
await expect(clientA.page).toHaveURL(/\/room\//, { timeout: 15_000 });
|
||||
await waitForCurrentRoomName(clientA.page, serverName);
|
||||
|
||||
const roomA = new ChatRoomPage(clientA.page);
|
||||
|
||||
await roomA.ensureVoiceChannelExists(MULTI_DEVICE_VOICE_CHANNEL);
|
||||
|
||||
await loginSecondDeviceIntoServer(clientB.page, credentials, serverName);
|
||||
await waitForCurrentRoomName(clientB.page, serverName);
|
||||
|
||||
const messagesA = new ChatMessagesPage(clientA.page);
|
||||
const messagesB = new ChatMessagesPage(clientB.page);
|
||||
const roomB = new ChatRoomPage(clientB.page);
|
||||
|
||||
await messagesA.waitForReady();
|
||||
await messagesB.waitForReady();
|
||||
|
||||
return {
|
||||
clientA,
|
||||
clientB,
|
||||
credentials,
|
||||
serverName,
|
||||
messagesA,
|
||||
messagesB,
|
||||
roomA,
|
||||
roomB
|
||||
};
|
||||
}
|
||||
|
||||
export async function loginSecondDeviceIntoServer(
|
||||
page: Page,
|
||||
credentials: MultiDeviceCredentials,
|
||||
serverName: string
|
||||
): Promise<void> {
|
||||
const loginPage = new LoginPage(page);
|
||||
|
||||
await loginPage.goto();
|
||||
await loginPage.login(credentials.username, credentials.password);
|
||||
await expect(page).toHaveURL(/\/dashboard/, { timeout: 15_000 });
|
||||
|
||||
const search = new ServerSearchPage(page);
|
||||
|
||||
await search.joinServerFromSearch(serverName);
|
||||
await expect(page).toHaveURL(/\/room\//, { timeout: 20_000 });
|
||||
await expect(page.locator('app-rooms-side-panel').first()).toBeVisible({ timeout: 20_000 });
|
||||
}
|
||||
|
||||
export async function expectCrossDeviceMessage(
|
||||
sender: ChatMessagesPage,
|
||||
receiver: ChatMessagesPage,
|
||||
message: string,
|
||||
timeout = 60_000
|
||||
): Promise<void> {
|
||||
await sender.sendMessage(message);
|
||||
|
||||
await expect.poll(async () => {
|
||||
return await receiver.getMessageItemByText(message).isVisible()
|
||||
.catch(() => false);
|
||||
}, { timeout }).toBe(true);
|
||||
}
|
||||
|
||||
async function warmClientPage(page: Page): Promise<void> {
|
||||
await page.goto('/dashboard', { waitUntil: 'domcontentloaded' });
|
||||
await page.waitForLoadState('networkidle').catch(() => undefined);
|
||||
}
|
||||
|
||||
async function waitForCurrentRoomName(page: Page, roomName: string, timeout = 20_000): Promise<void> {
|
||||
await page.waitForFunction(
|
||||
(expectedRoomName) => {
|
||||
interface RoomShape { name?: string }
|
||||
interface AngularDebugApi {
|
||||
getComponent: (element: Element) => Record<string, unknown>;
|
||||
}
|
||||
|
||||
const host = document.querySelector('app-rooms-side-panel');
|
||||
const debugApi = (window as { ng?: AngularDebugApi }).ng;
|
||||
|
||||
if (!host || !debugApi?.getComponent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const component = debugApi.getComponent(host);
|
||||
const currentRoom = (component['currentRoom'] as (() => RoomShape | null) | undefined)?.() ?? null;
|
||||
|
||||
return currentRoom?.name === expectedRoomName;
|
||||
},
|
||||
roomName,
|
||||
{ timeout }
|
||||
);
|
||||
}
|
||||
|
||||
export async function readClientInstanceId(page: Page): Promise<string | null> {
|
||||
return page.evaluate(() => {
|
||||
const sessionId = sessionStorage.getItem('metoyou.clientInstanceId')?.trim();
|
||||
|
||||
if (sessionId) {
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
return localStorage.getItem('metoyou.clientInstanceId')?.trim() ?? null;
|
||||
});
|
||||
}
|
||||
|
||||
export async function logoutFromMenu(page: Page): Promise<void> {
|
||||
const menuButton = page.getByRole('button', { name: 'Menu' });
|
||||
const logoutButton = page.getByRole('button', { name: 'Logout' });
|
||||
|
||||
await expect(menuButton).toBeVisible({ timeout: 10_000 });
|
||||
await menuButton.click();
|
||||
await expect(logoutButton).toBeVisible({ timeout: 10_000 });
|
||||
await logoutButton.click();
|
||||
await expect(page).toHaveURL(/\/login/, { timeout: 15_000 });
|
||||
}
|
||||
|
||||
export function channelsSidePanel(page: Page) {
|
||||
return page.locator('app-rooms-side-panel').first();
|
||||
}
|
||||
|
||||
export function membersSidePanel(page: Page) {
|
||||
return page.locator('app-rooms-side-panel').last();
|
||||
}
|
||||
|
||||
export function passiveVoiceChannelJoinBadge(page: Page, channelName = MULTI_DEVICE_VOICE_CHANNEL) {
|
||||
return page
|
||||
.locator(`button[data-channel-type="voice"][data-channel-name="${channelName}"]`)
|
||||
.getByText('Join', { exact: true });
|
||||
}
|
||||
|
||||
export async function expectPassiveVoiceOnDevice(
|
||||
page: Page,
|
||||
options: { timeout?: number; displayName?: string; channelName?: string } = {}
|
||||
): Promise<void> {
|
||||
const timeout = options.timeout ?? 45_000;
|
||||
const channelName = options.channelName ?? MULTI_DEVICE_VOICE_CHANNEL;
|
||||
const displayName = options.displayName;
|
||||
|
||||
await expect.poll(async () => {
|
||||
const membersLabel = await membersSidePanel(page)
|
||||
.getByText('In voice on another device', { exact: false })
|
||||
.isVisible()
|
||||
.catch(() => false);
|
||||
const joinBadge = await passiveVoiceChannelJoinBadge(page, channelName).isVisible()
|
||||
.catch(() => false);
|
||||
const grayedVoiceUser = displayName
|
||||
? await channelsSidePanel(page).locator('.opacity-50')
|
||||
.filter({ hasText: displayName })
|
||||
.first()
|
||||
.isVisible()
|
||||
.catch(() => false)
|
||||
: false;
|
||||
|
||||
return membersLabel || joinBadge || grayedVoiceUser;
|
||||
}, { timeout }).toBe(true);
|
||||
}
|
||||
|
||||
export async function expectActiveVoiceOnDevice(page: Page, timeout = 20_000): Promise<void> {
|
||||
await expect(page.locator('app-voice-controls, app-voice-workspace').first()).toBeVisible({ timeout });
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
import { expect, type Page } from '@playwright/test';
|
||||
|
||||
export const E2E_PLUGIN_SOURCE_URL = 'http://localhost:4200/plugins/e2e-plugin-source.json';
|
||||
export const E2E_PLUGIN_TITLE = 'E2E All API Plugin';
|
||||
|
||||
export async function addPluginSource(page: Page, sourceUrl = E2E_PLUGIN_SOURCE_URL): Promise<void> {
|
||||
const sourceInput = page.getByLabel('Plugin source manifest URL');
|
||||
|
||||
await expect(sourceInput).toBeVisible({ timeout: 15_000 });
|
||||
await sourceInput.click();
|
||||
await sourceInput.fill(sourceUrl);
|
||||
await expect(sourceInput).toHaveValue(sourceUrl, { timeout: 5_000 });
|
||||
|
||||
const addSourceButton = page.getByRole('button', { name: 'Add Source' });
|
||||
|
||||
await expect(addSourceButton).toBeEnabled({ timeout: 10_000 });
|
||||
await addSourceButton.click();
|
||||
await expect(page.getByRole('heading', { name: E2E_PLUGIN_TITLE })).toBeVisible({ timeout: 20_000 });
|
||||
}
|
||||
@@ -7,19 +7,16 @@
|
||||
*
|
||||
* Cleanup: the temp directory is removed when the process exits.
|
||||
*/
|
||||
const { existsSync, mkdtempSync, writeFileSync, mkdirSync, rmSync } = require('fs');
|
||||
const { mkdtempSync, writeFileSync, mkdirSync, rmSync } = require('fs');
|
||||
const { join } = require('path');
|
||||
const { tmpdir } = require('os');
|
||||
const { spawn } = require('child_process');
|
||||
|
||||
const TEST_PORT = process.env.TEST_SERVER_PORT || '3099';
|
||||
const SERVER_DIR = join(__dirname, '..', '..', 'server');
|
||||
const SERVER_DIST_ENTRY = join(SERVER_DIR, 'dist', 'index.js');
|
||||
const SERVER_SRC_ENTRY = join(SERVER_DIR, 'src', 'index.ts');
|
||||
const SERVER_ENTRY = join(SERVER_DIR, 'src', 'index.ts');
|
||||
const SERVER_TSCONFIG = join(SERVER_DIR, 'tsconfig.json');
|
||||
const TS_NODE_BIN = join(SERVER_DIR, 'node_modules', 'ts-node', 'dist', 'bin.js');
|
||||
const SERVER_ENTRY = existsSync(SERVER_DIST_ENTRY) ? SERVER_DIST_ENTRY : SERVER_SRC_ENTRY;
|
||||
const USE_COMPILED_SERVER = SERVER_ENTRY === SERVER_DIST_ENTRY;
|
||||
|
||||
// ── Create isolated temp data directory ──────────────────────────────
|
||||
const tmpDir = mkdtempSync(join(tmpdir(), 'metoyou-e2e-'));
|
||||
@@ -48,7 +45,7 @@ console.log(`[E2E Server] Starting on port ${TEST_PORT}...`);
|
||||
// and node_modules are found from the real server/ directory.
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
USE_COMPILED_SERVER ? [SERVER_ENTRY] : [TS_NODE_BIN, '--project', SERVER_TSCONFIG, SERVER_ENTRY],
|
||||
[TS_NODE_BIN, '--project', SERVER_TSCONFIG, SERVER_ENTRY],
|
||||
{
|
||||
cwd: tmpDir,
|
||||
env: {
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { type BrowserContext, type Page } from '@playwright/test';
|
||||
import { type Page } from '@playwright/test';
|
||||
|
||||
/**
|
||||
* Install RTCPeerConnection monkey-patch on a page BEFORE navigating.
|
||||
* Tracks all created peer connections and their remote tracks so tests
|
||||
* can inspect WebRTC state via `page.evaluate()`.
|
||||
*
|
||||
* Call on the browser context (preferred) or page before any `goto()`.
|
||||
* Call immediately after page creation, before any `goto()`.
|
||||
*/
|
||||
export async function installWebRTCTracking(target: BrowserContext | Page): Promise<void> {
|
||||
const addInitScript = 'addInitScript' in target && typeof target.addInitScript === 'function'
|
||||
? target.addInitScript.bind(target)
|
||||
: (target as Page).addInitScript.bind(target);
|
||||
|
||||
await addInitScript(() => {
|
||||
export async function installWebRTCTracking(page: Page): Promise<void> {
|
||||
await page.addInitScript(() => {
|
||||
const connections: RTCPeerConnection[] = [];
|
||||
const dataChannels: RTCDataChannel[] = [];
|
||||
const syntheticMediaResources: {
|
||||
@@ -201,7 +197,6 @@ export async function waitForPeerConnected(page: Page, timeout = 30_000): Promis
|
||||
() => (window as any).__rtcConnections?.some(
|
||||
(pc: RTCPeerConnection) => pc.connectionState === 'connected'
|
||||
) ?? false,
|
||||
undefined,
|
||||
{ timeout }
|
||||
);
|
||||
}
|
||||
@@ -616,7 +611,6 @@ export async function waitForAudioStatsPresent(page: Page, timeout = 15_000): Pr
|
||||
|
||||
return false;
|
||||
},
|
||||
undefined,
|
||||
{ timeout }
|
||||
);
|
||||
}
|
||||
@@ -824,7 +818,6 @@ export async function waitForVideoStatsPresent(page: Page, timeout = 15_000): Pr
|
||||
|
||||
return false;
|
||||
},
|
||||
undefined,
|
||||
{ timeout }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -34,22 +34,9 @@ export class ChatMessagesPage {
|
||||
}
|
||||
|
||||
async sendMessage(content: string): Promise<void> {
|
||||
let lastError: unknown;
|
||||
|
||||
for (let attempt = 1; attempt <= 3; attempt += 1) {
|
||||
try {
|
||||
await this.waitForReady();
|
||||
await this.composerInput.fill(content);
|
||||
await expect(this.composerInput).toHaveValue(content, { timeout: 5_000 });
|
||||
await expect(this.sendButton).toBeEnabled({ timeout: 5_000 });
|
||||
await this.sendButton.click();
|
||||
return;
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError instanceof Error ? lastError : new Error('Failed to send chat message');
|
||||
}
|
||||
|
||||
async typeDraft(content: string): Promise<void> {
|
||||
@@ -57,13 +44,6 @@ export class ChatMessagesPage {
|
||||
await this.composerInput.fill(content);
|
||||
}
|
||||
|
||||
/** Types into the composer in a way that emits input/typing events (not just fill). */
|
||||
async typeDraftWithTypingEvents(content: string): Promise<void> {
|
||||
await this.waitForReady();
|
||||
await this.composerInput.click();
|
||||
await this.composerInput.pressSequentially(content, { delay: 40 });
|
||||
}
|
||||
|
||||
async clearDraft(): Promise<void> {
|
||||
await this.waitForReady();
|
||||
await this.composerInput.fill('');
|
||||
|
||||
@@ -317,22 +317,13 @@ export class ChatRoomPage {
|
||||
throw new Error('Missing room, user, or endpoint when persisting channels');
|
||||
}
|
||||
|
||||
const authTokens = JSON.parse(localStorage.getItem('metoyou.authTokens') || '{}') as Record<string, { token: string; expiresAt: number }>;
|
||||
const normalizedApiUrl = apiBaseUrl.trim().replace(/\/+$/, '');
|
||||
const authEntry = authTokens[normalizedApiUrl];
|
||||
const authToken = authEntry && authEntry.expiresAt > Date.now() ? authEntry.token : null;
|
||||
|
||||
if (!authToken) {
|
||||
throw new Error('Missing session token for channel persistence');
|
||||
}
|
||||
|
||||
const response = await fetch(`${apiBaseUrl}/api/servers/${room.id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${authToken}`
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
currentOwnerId: currentUser.id,
|
||||
channels: nextChannels
|
||||
})
|
||||
});
|
||||
|
||||
@@ -10,14 +10,15 @@ export class LoginPage {
|
||||
readonly registerLink: Locator;
|
||||
|
||||
constructor(private page: Page) {
|
||||
this.form = page.locator('form').filter({ has: page.locator('#login-username') });
|
||||
this.form = page.locator('#login-username').locator('xpath=ancestor::div[contains(@class, "space-y-3")]')
|
||||
.first();
|
||||
|
||||
this.usernameInput = page.locator('#login-username');
|
||||
this.passwordInput = page.locator('#login-password');
|
||||
this.serverSelect = page.locator('#login-server');
|
||||
this.submitButton = this.form.getByRole('button', { name: 'Login' });
|
||||
this.errorText = page.locator('.text-destructive');
|
||||
this.registerLink = page.getByRole('button', { name: 'Register' });
|
||||
this.registerLink = this.form.getByRole('button', { name: 'Register' });
|
||||
}
|
||||
|
||||
async goto() {
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
import { spawn } from 'node:child_process';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const e2eDirectory = fileURLToPath(new URL('.', import.meta.url));
|
||||
const env = { ...process.env };
|
||||
const browsersPath = env.PLAYWRIGHT_BROWSERS_PATH;
|
||||
|
||||
if (browsersPath?.includes('/cursor-sandbox-cache/')) {
|
||||
delete env.PLAYWRIGHT_BROWSERS_PATH;
|
||||
}
|
||||
|
||||
const [command = 'test', ...args] = process.argv.slice(2);
|
||||
const executable = process.platform === 'win32' ? 'npx.cmd' : 'npx';
|
||||
const child = spawn(executable, ['playwright', command, ...args], {
|
||||
cwd: e2eDirectory,
|
||||
env,
|
||||
stdio: 'inherit'
|
||||
});
|
||||
|
||||
child.on('exit', (code, signal) => {
|
||||
if (signal) {
|
||||
process.kill(process.pid, signal);
|
||||
return;
|
||||
}
|
||||
|
||||
process.exit(code ?? 1);
|
||||
});
|
||||
@@ -1,153 +0,0 @@
|
||||
import { test, expect } from '../../fixtures/multi-client';
|
||||
import { LoginPage } from '../../pages/login.page';
|
||||
import { RegisterPage } from '../../pages/register.page';
|
||||
|
||||
interface TestUser {
|
||||
username: string;
|
||||
displayName: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
test.describe('Login returnUrl handling', () => {
|
||||
test.describe.configure({ timeout: 120_000 });
|
||||
|
||||
test('unwraps nested login returnUrl chains after successful login', async ({ createClient }) => {
|
||||
const client = await createClient();
|
||||
const { page } = client;
|
||||
const suffix = uniqueName('nested-return');
|
||||
const user: TestUser = {
|
||||
username: `user_${suffix}`,
|
||||
displayName: 'Return Url User',
|
||||
password: 'TestPass123!'
|
||||
};
|
||||
|
||||
await test.step('Create an account', async () => {
|
||||
const registerPage = new RegisterPage(page);
|
||||
|
||||
await registerPage.goto();
|
||||
await registerPage.register(user.username, user.displayName, user.password);
|
||||
await expect(page).toHaveURL(/\/dashboard/, { timeout: 15_000 });
|
||||
});
|
||||
|
||||
await test.step('Log out and open a deeply nested login returnUrl', async () => {
|
||||
await logout(page);
|
||||
|
||||
const nestedReturnUrl = '/login?returnUrl=%2Flogin%3FreturnUrl%3D%252Fservers';
|
||||
|
||||
await page.goto(`/login?returnUrl=${encodeURIComponent(nestedReturnUrl)}`, {
|
||||
waitUntil: 'domcontentloaded'
|
||||
});
|
||||
|
||||
await expect(page.locator('#login-username')).toBeVisible({ timeout: 15_000 });
|
||||
});
|
||||
|
||||
await test.step('Login lands on the original destination instead of looping on /login', async () => {
|
||||
const loginPage = new LoginPage(page);
|
||||
|
||||
await loginPage.login(user.username, user.password);
|
||||
await expect(page).toHaveURL(/\/servers/, { timeout: 15_000 });
|
||||
await expect(page).not.toHaveURL(/returnUrl=.*login/);
|
||||
});
|
||||
});
|
||||
|
||||
test('redirects unauthenticated /servers visits to login and returns there after login', async ({ createClient }) => {
|
||||
const client = await createClient();
|
||||
const { page } = client;
|
||||
const suffix = uniqueName('servers-return');
|
||||
const user: TestUser = {
|
||||
username: `user_${suffix}`,
|
||||
displayName: 'Servers Return User',
|
||||
password: 'TestPass123!'
|
||||
};
|
||||
|
||||
await test.step('Create an account and log out', async () => {
|
||||
const registerPage = new RegisterPage(page);
|
||||
|
||||
await registerPage.goto();
|
||||
await registerPage.register(user.username, user.displayName, user.password);
|
||||
await expect(page).toHaveURL(/\/dashboard/, { timeout: 15_000 });
|
||||
await logout(page);
|
||||
});
|
||||
|
||||
await test.step('Visiting /servers sends the user to a single-level login returnUrl', async () => {
|
||||
await page.goto('/servers', { waitUntil: 'domcontentloaded' });
|
||||
await expect(page).toHaveURL(/\/login/, { timeout: 15_000 });
|
||||
await expect(page).toHaveURL(/returnUrl=%2Fservers/);
|
||||
await expect(page).not.toHaveURL(/returnUrl=.*login/);
|
||||
});
|
||||
|
||||
await test.step('Logging in returns to /servers', async () => {
|
||||
const loginPage = new LoginPage(page);
|
||||
|
||||
await loginPage.login(user.username, user.password);
|
||||
await expect(page).toHaveURL(/\/servers/, { timeout: 15_000 });
|
||||
await expect(page.locator('app-server-browser')).toBeVisible({ timeout: 15_000 });
|
||||
});
|
||||
});
|
||||
|
||||
test('lets a returning user log back in after an expired session redirect', async ({ createClient }) => {
|
||||
const client = await createClient();
|
||||
const { page } = client;
|
||||
const suffix = uniqueName('expired-session');
|
||||
const user: TestUser = {
|
||||
username: `user_${suffix}`,
|
||||
displayName: 'Expired Session User',
|
||||
password: 'TestPass123!'
|
||||
};
|
||||
|
||||
await test.step('Create an account', async () => {
|
||||
const registerPage = new RegisterPage(page);
|
||||
|
||||
await registerPage.goto();
|
||||
await registerPage.register(user.username, user.displayName, user.password);
|
||||
await expect(page).toHaveURL(/\/dashboard/, { timeout: 15_000 });
|
||||
});
|
||||
|
||||
await test.step('Simulate an expired session while keeping the persisted user id', async () => {
|
||||
await page.evaluate(() => {
|
||||
const storageKey = 'metoyou.authTokens';
|
||||
const raw = localStorage.getItem(storageKey);
|
||||
|
||||
if (!raw) {
|
||||
return;
|
||||
}
|
||||
|
||||
const parsed = JSON.parse(raw) as Record<string, { token: string; expiresAt: number }>;
|
||||
const expiredStore = Object.fromEntries(
|
||||
Object.entries(parsed).map(([url, entry]) => [url, { ...entry, expiresAt: 0 }])
|
||||
);
|
||||
|
||||
localStorage.setItem(storageKey, JSON.stringify(expiredStore));
|
||||
});
|
||||
|
||||
await page.goto('/servers', { waitUntil: 'domcontentloaded' });
|
||||
await expect(page).toHaveURL(/\/login/, { timeout: 15_000 });
|
||||
await expect(page).toHaveURL(/returnUrl=%2Fservers/);
|
||||
await expect(page).not.toHaveURL(/returnUrl=.*login/);
|
||||
});
|
||||
|
||||
await test.step('The user can authenticate again and reach /servers', async () => {
|
||||
const loginPage = new LoginPage(page);
|
||||
|
||||
await loginPage.login(user.username, user.password);
|
||||
await expect(page).toHaveURL(/\/servers/, { timeout: 15_000 });
|
||||
await expect(page.locator('app-server-browser')).toBeVisible({ timeout: 15_000 });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
async function logout(page: import('@playwright/test').Page): Promise<void> {
|
||||
const menuButton = page.getByRole('button', { name: 'Menu' });
|
||||
const logoutButton = page.getByRole('button', { name: 'Logout' });
|
||||
|
||||
await expect(menuButton).toBeVisible({ timeout: 10_000 });
|
||||
await menuButton.click();
|
||||
await expect(logoutButton).toBeVisible({ timeout: 10_000 });
|
||||
await logoutButton.click();
|
||||
await expect(page).toHaveURL(/\/login/, { timeout: 15_000 });
|
||||
}
|
||||
|
||||
function uniqueName(prefix: string): string {
|
||||
return `${prefix}-${Date.now().toString(36)}-${Math.random().toString(36)
|
||||
.slice(2, 8)}`;
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
import { test, expect } from '../../fixtures/multi-client';
|
||||
import {
|
||||
MULTI_DEVICE_VOICE_CHANNEL,
|
||||
channelsSidePanel,
|
||||
createMultiDeviceScenario,
|
||||
expectCrossDeviceMessage,
|
||||
expectActiveVoiceOnDevice,
|
||||
expectPassiveVoiceOnDevice,
|
||||
logoutFromMenu,
|
||||
membersSidePanel,
|
||||
passiveVoiceChannelJoinBadge,
|
||||
readClientInstanceId,
|
||||
uniqueMultiDeviceName
|
||||
} from '../../helpers/multi-device-session';
|
||||
|
||||
test.describe('Multi-device session', () => {
|
||||
test.describe.configure({ timeout: 300_000, retries: 1 });
|
||||
|
||||
test('covers identity, chat sync, typing exclusion, and voice exclusivity', async ({ createClient }) => {
|
||||
const scenario = await createMultiDeviceScenario(createClient);
|
||||
const messageAtoB = `Cross-device A to B ${uniqueMultiDeviceName('msg')}`;
|
||||
const messageBtoA = `Cross-device B to A ${uniqueMultiDeviceName('msg')}`;
|
||||
const typingDraft = `Typing draft ${uniqueMultiDeviceName('draft')}`;
|
||||
|
||||
await test.step('assigns distinct clientInstanceId per browser context', async () => {
|
||||
const instanceA = await readClientInstanceId(scenario.clientA.page);
|
||||
const instanceB = await readClientInstanceId(scenario.clientB.page);
|
||||
|
||||
expect(instanceA).toBeTruthy();
|
||||
expect(instanceB).toBeTruthy();
|
||||
expect(instanceA).not.toEqual(instanceB);
|
||||
});
|
||||
|
||||
await test.step('syncs chat from device A to device B', async () => {
|
||||
await expectCrossDeviceMessage(scenario.messagesA, scenario.messagesB, messageAtoB);
|
||||
});
|
||||
|
||||
await test.step('syncs chat from device B to device A', async () => {
|
||||
await expectCrossDeviceMessage(scenario.messagesB, scenario.messagesA, messageBtoA);
|
||||
});
|
||||
|
||||
await test.step('does not show own typing indicator on the other device for the same user', async () => {
|
||||
await scenario.messagesA.typeDraftWithTypingEvents(typingDraft);
|
||||
|
||||
await expect(
|
||||
scenario.clientB.page.getByText(`${scenario.credentials.displayName} is typing`, { exact: false })
|
||||
).toHaveCount(0, { timeout: 5_000 });
|
||||
});
|
||||
|
||||
await test.step('shows passive in-voice UI on the second device when the first joins voice', async () => {
|
||||
await scenario.roomA.joinVoiceChannel(MULTI_DEVICE_VOICE_CHANNEL);
|
||||
await expectActiveVoiceOnDevice(scenario.clientA.page);
|
||||
|
||||
await expectPassiveVoiceOnDevice(scenario.clientB.page, {
|
||||
displayName: scenario.credentials.displayName
|
||||
});
|
||||
|
||||
await expect(
|
||||
membersSidePanel(scenario.clientB.page).getByText('In voice on another device', { exact: false })
|
||||
).toBeVisible({ timeout: 20_000 });
|
||||
|
||||
await expect(
|
||||
channelsSidePanel(scenario.clientB.page).locator('.opacity-50')
|
||||
.filter({
|
||||
hasText: scenario.credentials.displayName
|
||||
})
|
||||
.first()
|
||||
).toBeVisible({ timeout: 20_000 });
|
||||
});
|
||||
|
||||
await test.step('shows Join takeover affordance on passive device voice channel', async () => {
|
||||
await expect(passiveVoiceChannelJoinBadge(scenario.clientB.page)).toBeVisible({ timeout: 20_000 });
|
||||
});
|
||||
|
||||
await test.step('transfers voice ownership when the passive device takes over', async () => {
|
||||
await scenario.roomB.joinVoiceChannel(MULTI_DEVICE_VOICE_CHANNEL);
|
||||
await expectActiveVoiceOnDevice(scenario.clientB.page);
|
||||
|
||||
await expectPassiveVoiceOnDevice(scenario.clientA.page, {
|
||||
displayName: scenario.credentials.displayName
|
||||
});
|
||||
});
|
||||
|
||||
await test.step('keeps the second device logged in when the first device logs out', async () => {
|
||||
const message = `Still logged in ${uniqueMultiDeviceName('logout')}`;
|
||||
|
||||
await logoutFromMenu(scenario.clientA.page);
|
||||
|
||||
await scenario.messagesB.sendMessage(message);
|
||||
await expect(scenario.messagesB.getMessageItemByText(message)).toBeVisible({ timeout: 20_000 });
|
||||
await expect(scenario.clientB.page).toHaveURL(/\/room\//, { timeout: 10_000 });
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,111 +0,0 @@
|
||||
import { expect } from '@playwright/test';
|
||||
import { test } from '../../fixtures/multi-client';
|
||||
import { openSettingsFromMenu } from '../../helpers/app-menu';
|
||||
import { expectDashboardReady } from '../../helpers/dashboard';
|
||||
import { installTestServerEndpoints } from '../../helpers/seed-test-endpoint';
|
||||
import { startTestServer } from '../../helpers/test-server';
|
||||
import {
|
||||
readAuthTokenFromPage,
|
||||
readSignalServerCredentialFromPage,
|
||||
registerTestUser
|
||||
} from '../../helpers/auth-api';
|
||||
import { RegisterPage } from '../../pages/register.page';
|
||||
|
||||
const PRIMARY_ENDPOINT_ID = 'e2e-multi-auth-primary';
|
||||
const USER_PASSWORD = 'TestPass123!';
|
||||
|
||||
test.describe('Multi-signal-server authentication', () => {
|
||||
test.describe.configure({ timeout: 180_000 });
|
||||
|
||||
test('auto-provisions a foreign signal server when a new endpoint is added', async ({ createClient, request }) => {
|
||||
const primaryServer = await startTestServer();
|
||||
const secondaryServer = await startTestServer();
|
||||
|
||||
try {
|
||||
const client = await createClient();
|
||||
const suffix = `multi_auth_${Date.now()}`;
|
||||
const username = `user_${suffix}`;
|
||||
|
||||
await installTestServerEndpoints(client.context, [
|
||||
{
|
||||
id: PRIMARY_ENDPOINT_ID,
|
||||
name: 'E2E Primary Signal',
|
||||
url: primaryServer.url,
|
||||
isActive: true,
|
||||
status: 'online'
|
||||
}
|
||||
]);
|
||||
|
||||
await test.step('Register on the home signal server', async () => {
|
||||
const register = new RegisterPage(client.page);
|
||||
|
||||
await register.goto();
|
||||
await register.register(username, 'Multi Auth User', USER_PASSWORD);
|
||||
await expectDashboardReady(client.page);
|
||||
});
|
||||
|
||||
await test.step('Add a second signal server in network settings', async () => {
|
||||
await openSettingsFromMenu(client.page);
|
||||
await client.page.getByRole('button', { name: 'Network' }).click();
|
||||
|
||||
await client.page.getByPlaceholder('Server name').fill('E2E Secondary Signal');
|
||||
await client.page.getByPlaceholder('Server URL (e.g., http://localhost:3001)').fill(secondaryServer.url);
|
||||
await client.page.getByTestId('add-signal-server-button').click();
|
||||
|
||||
await expect(client.page.getByText(secondaryServer.url)).toBeVisible({ timeout: 15_000 });
|
||||
});
|
||||
|
||||
await test.step('Wait for auto-provisioned credentials on the secondary server', async () => {
|
||||
await expect.poll(async () =>
|
||||
await readSignalServerCredentialFromPage(client.page, secondaryServer.url),
|
||||
{ timeout: 30_000 }
|
||||
).not.toBeNull();
|
||||
|
||||
const homeToken = await readAuthTokenFromPage(client.page, primaryServer.url);
|
||||
const secondaryCredential = await readSignalServerCredentialFromPage(client.page, secondaryServer.url);
|
||||
|
||||
expect(homeToken).toBeTruthy();
|
||||
expect(secondaryCredential?.username).toBe(username);
|
||||
expect(secondaryCredential?.token).toBeTruthy();
|
||||
});
|
||||
|
||||
await test.step('Secondary credential can call authenticated APIs', async () => {
|
||||
const secondaryCredential = await readSignalServerCredentialFromPage(client.page, secondaryServer.url);
|
||||
|
||||
if (!secondaryCredential) {
|
||||
throw new Error('Expected secondary signal-server credential to be provisioned');
|
||||
}
|
||||
|
||||
const response = await request.post(`${secondaryServer.url}/api/servers`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${secondaryCredential.token}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
data: {
|
||||
name: `Secondary Provisioned Server ${suffix}`,
|
||||
description: 'Created with auto-provisioned credentials',
|
||||
ownerId: secondaryCredential.userId,
|
||||
ownerPublicKey: 'e2e-secondary-owner-key'
|
||||
}
|
||||
});
|
||||
|
||||
expect(response.ok(), `POST /api/servers failed: ${response.status()} ${await response.text()}`).toBe(true);
|
||||
});
|
||||
|
||||
await test.step('Home registration still works independently on the secondary server', async () => {
|
||||
const otherUser = await registerTestUser(
|
||||
request,
|
||||
secondaryServer.url,
|
||||
`other_${suffix}`,
|
||||
USER_PASSWORD,
|
||||
'Other User'
|
||||
);
|
||||
|
||||
expect(otherUser.username).toBe(`other_${suffix}`);
|
||||
});
|
||||
} finally {
|
||||
await primaryServer.stop();
|
||||
await secondaryServer.stop();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -48,13 +48,14 @@ test.describe('User session data isolation', () => {
|
||||
|
||||
await test.step('Alice registers and creates local chat history', async () => {
|
||||
await registerUser(client.page, alice);
|
||||
await createServerAndSendMessage(client.page, alice, aliceServerName, aliceMessage);
|
||||
await createServerAndSendMessage(client.page, aliceServerName, aliceMessage);
|
||||
});
|
||||
|
||||
await test.step('Alice sees the same saved room and message after a full restart', async () => {
|
||||
await restartPersistentClient(client, testServer.port);
|
||||
await openApp(client.page);
|
||||
await expectSavedRoomAndHistory(client.page, alice, aliceServerName, aliceMessage);
|
||||
await expect(client.page).not.toHaveURL(/\/login/, { timeout: 15_000 });
|
||||
await expectSavedRoomAndHistory(client.page, aliceServerName, aliceMessage);
|
||||
});
|
||||
} finally {
|
||||
await closePersistentClient(client);
|
||||
@@ -87,11 +88,11 @@ test.describe('User session data isolation', () => {
|
||||
|
||||
await test.step('Alice creates persisted local data and verifies it survives a restart', async () => {
|
||||
await registerUser(client.page, alice);
|
||||
await createServerAndSendMessage(client.page, alice, aliceServerName, aliceMessage);
|
||||
await createServerAndSendMessage(client.page, aliceServerName, aliceMessage);
|
||||
|
||||
await restartPersistentClient(client, testServer.port);
|
||||
await openApp(client.page);
|
||||
await expectSavedRoomAndHistory(client.page, alice, aliceServerName, aliceMessage);
|
||||
await expectSavedRoomAndHistory(client.page, aliceServerName, aliceMessage);
|
||||
});
|
||||
|
||||
await test.step('Bob starts from a blank slate in the same browser profile', async () => {
|
||||
@@ -101,11 +102,11 @@ test.describe('User session data isolation', () => {
|
||||
});
|
||||
|
||||
await test.step('Bob gets only his own saved room and history after a restart', async () => {
|
||||
await createServerAndSendMessage(client.page, bob, bobServerName, bobMessage);
|
||||
await createServerAndSendMessage(client.page, bobServerName, bobMessage);
|
||||
|
||||
await restartPersistentClient(client, testServer.port);
|
||||
await openApp(client.page);
|
||||
await expectSavedRoomAndHistory(client.page, bob, bobServerName, bobMessage);
|
||||
await expectSavedRoomAndHistory(client.page, bobServerName, bobMessage);
|
||||
await expectSavedRoomHidden(client.page, aliceServerName);
|
||||
});
|
||||
|
||||
@@ -116,7 +117,7 @@ test.describe('User session data isolation', () => {
|
||||
|
||||
await expectSavedRoomVisible(client.page, aliceServerName);
|
||||
await expectSavedRoomHidden(client.page, bobServerName);
|
||||
await expectSavedRoomAndHistory(client.page, alice, aliceServerName, aliceMessage);
|
||||
await expectSavedRoomAndHistory(client.page, aliceServerName, aliceMessage);
|
||||
});
|
||||
} finally {
|
||||
await closePersistentClient(client);
|
||||
@@ -193,58 +194,32 @@ async function logoutUser(page: Page): Promise<void> {
|
||||
await expect(loginPage.usernameInput).toBeVisible({ timeout: 10_000 });
|
||||
}
|
||||
|
||||
async function createServerAndSendMessage(page: Page, user: TestUser, serverName: string, messageText: string): Promise<void> {
|
||||
async function createServerAndSendMessage(page: Page, serverName: string, messageText: string): Promise<void> {
|
||||
const searchPage = new ServerSearchPage(page);
|
||||
const messagesPage = new ChatMessagesPage(page);
|
||||
|
||||
await loginIfNeeded(page, user);
|
||||
await ensureCurrentUserScope(page, user);
|
||||
await page.goto('/create-server', { waitUntil: 'domcontentloaded' });
|
||||
|
||||
if (await waitForLoginForm(page, 5_000)) {
|
||||
await loginUser(page, user);
|
||||
await page.goto('/create-server', { waitUntil: 'domcontentloaded' });
|
||||
}
|
||||
|
||||
await expect(searchPage.serverNameInput).toBeVisible({ timeout: 10_000 });
|
||||
await searchPage.serverNameInput.fill(serverName);
|
||||
await searchPage.serverDescriptionInput.fill(`User session isolation coverage for ${serverName}`);
|
||||
await searchPage.createSubmitButton.click();
|
||||
await searchPage.createServer(serverName, {
|
||||
description: `User session isolation coverage for ${serverName}`
|
||||
});
|
||||
|
||||
await expect(page).toHaveURL(/\/room\//, { timeout: 15_000 });
|
||||
|
||||
await messagesPage.sendMessage(messageText);
|
||||
await expect(messagesPage.getMessageItemByText(messageText)).toBeVisible({ timeout: 20_000 });
|
||||
await expectMessagePersistedInIndexedDb(page, messageText);
|
||||
}
|
||||
|
||||
async function expectSavedRoomAndHistory(page: Page, user: TestUser, roomName: string, messageText: string): Promise<void> {
|
||||
if (await waitForVisibleText(page, messageText, 5_000)) {
|
||||
return;
|
||||
}
|
||||
async function expectSavedRoomAndHistory(page: Page, roomName: string, messageText: string): Promise<void> {
|
||||
const railRoomButton = getRailSavedRoomButton(page, roomName);
|
||||
const messagesPage = new ChatMessagesPage(page);
|
||||
|
||||
if (await new LoginPage(page).usernameInput.isVisible().catch(() => false)) {
|
||||
await loginUser(page, user);
|
||||
}
|
||||
await expect(railRoomButton).toBeVisible({ timeout: 20_000 });
|
||||
await page.goto('/servers', { waitUntil: 'domcontentloaded' });
|
||||
const searchRoomButton = getSearchSavedRoomButton(page, roomName);
|
||||
|
||||
await expectMessagePersistedInIndexedDb(page, messageText);
|
||||
|
||||
const persistedRoomId = await getPersistedRoomIdForMessage(page, messageText);
|
||||
|
||||
if (persistedRoomId) {
|
||||
await openPersistedRoomById(page, user, persistedRoomId);
|
||||
await expect(page.getByText(messageText, { exact: false })).toBeVisible({ timeout: 20_000 });
|
||||
return;
|
||||
}
|
||||
|
||||
if (await openSavedRoomFromRail(page, roomName)) {
|
||||
await expect(page.getByText(messageText, { exact: false })).toBeVisible({ timeout: 20_000 });
|
||||
return;
|
||||
}
|
||||
|
||||
await joinServerFromSearchAfterLogin(page, user, roomName);
|
||||
await expect(searchRoomButton).toBeVisible({ timeout: 20_000 });
|
||||
await searchRoomButton.click();
|
||||
await expect(page).toHaveURL(/\/room\//, { timeout: 20_000 });
|
||||
await expect(page.getByText(messageText, { exact: false })).toBeVisible({ timeout: 20_000 });
|
||||
await expect(messagesPage.getMessageItemByText(messageText)).toBeVisible({ timeout: 20_000 });
|
||||
}
|
||||
|
||||
async function expectBlankSlate(page: Page, hiddenRoomNames: string[]): Promise<void> {
|
||||
@@ -257,17 +232,14 @@ async function expectBlankSlate(page: Page, hiddenRoomNames: string[]): Promise<
|
||||
}
|
||||
|
||||
async function expectSavedRoomVisible(page: Page, roomName: string): Promise<void> {
|
||||
if (await page.getByText(roomName, { exact: false }).first()
|
||||
.isVisible()
|
||||
.catch(() => false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
await expect(getRailSavedRoomButton(page, roomName)).toBeVisible({ timeout: 20_000 });
|
||||
await page.goto('/servers', { waitUntil: 'domcontentloaded' });
|
||||
await expect(getSearchSavedRoomButton(page, roomName)).toBeVisible({ timeout: 20_000 });
|
||||
}
|
||||
|
||||
async function expectSavedRoomHidden(page: Page, roomName: string): Promise<void> {
|
||||
await expect(getRailSavedRoomButton(page, roomName)).toHaveCount(0);
|
||||
|
||||
if (!page.url().includes('/servers')) {
|
||||
await page.goto('/servers', { waitUntil: 'domcontentloaded' });
|
||||
}
|
||||
@@ -275,227 +247,14 @@ async function expectSavedRoomHidden(page: Page, roomName: string): Promise<void
|
||||
await expect(getSearchSavedRoomButton(page, roomName)).toHaveCount(0);
|
||||
}
|
||||
|
||||
function getRailSavedRoomButton(page: Page, roomName: string) {
|
||||
return page.locator(`button[title="${roomName}"]`).first();
|
||||
}
|
||||
|
||||
function getSearchSavedRoomButton(page: Page, roomName: string) {
|
||||
return page.locator('app-server-browser').getByRole('button', { name: roomName, exact: true });
|
||||
}
|
||||
|
||||
async function openSavedRoomFromRail(page: Page, roomName: string): Promise<boolean> {
|
||||
try {
|
||||
await expect(page.locator('app-servers-rail')).toBeVisible({ timeout: 10_000 });
|
||||
const clicked = await page.locator('app-servers-rail button').evaluateAll((buttons, expectedName) => {
|
||||
const expectedPrefix = expectedName.slice(0, 24);
|
||||
const button = buttons.find((candidate) => {
|
||||
const title = (candidate as HTMLButtonElement).title;
|
||||
|
||||
return title === expectedName || title.startsWith(expectedPrefix);
|
||||
}) as HTMLButtonElement | undefined;
|
||||
|
||||
button?.click();
|
||||
return !!button;
|
||||
}, roomName);
|
||||
|
||||
if (!clicked) {
|
||||
return await openSavedRoomFromDashboard(page, roomName);
|
||||
}
|
||||
|
||||
await expect(page).toHaveURL(/\/room\//, { timeout: 20_000 });
|
||||
return true;
|
||||
} catch {
|
||||
return await openSavedRoomFromDashboard(page, roomName);
|
||||
}
|
||||
}
|
||||
|
||||
async function openSavedRoomFromDashboard(page: Page, roomName: string): Promise<boolean> {
|
||||
const roomNamePattern = new RegExp(escapeRegExp(roomName.slice(0, 24)));
|
||||
const roomButton = page.getByRole('button', { name: roomNamePattern }).first();
|
||||
|
||||
try {
|
||||
await expect(roomButton).toBeVisible({ timeout: 10_000 });
|
||||
await roomButton.click();
|
||||
await expect(page).toHaveURL(/\/room\//, { timeout: 20_000 });
|
||||
return true;
|
||||
} catch {
|
||||
return await joinVisibleServerFromDashboard(page, roomNamePattern);
|
||||
}
|
||||
}
|
||||
|
||||
async function joinVisibleServerFromDashboard(page: Page, roomNamePattern: RegExp): Promise<boolean> {
|
||||
const serverRow = page.locator('div', { hasText: roomNamePattern }).filter({
|
||||
has: page.getByRole('button', { name: 'Join' })
|
||||
})
|
||||
.last();
|
||||
const joinButton = serverRow.getByRole('button', { name: 'Join' });
|
||||
|
||||
try {
|
||||
await expect(joinButton).toBeVisible({ timeout: 10_000 });
|
||||
await joinButton.click();
|
||||
await expect(page).toHaveURL(/\/room\//, { timeout: 20_000 });
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function joinServerFromSearchAfterLogin(page: Page, user: TestUser, roomName: string): Promise<void> {
|
||||
const searchPage = new ServerSearchPage(page);
|
||||
|
||||
await loginIfNeeded(page, user);
|
||||
await searchPage.goto();
|
||||
|
||||
if (!await waitForServerSearch(page, 5_000)) {
|
||||
await loginUser(page, user);
|
||||
await searchPage.goto();
|
||||
}
|
||||
|
||||
await expect(searchPage.searchInput).toBeVisible({ timeout: 15_000 });
|
||||
await searchPage.searchInput.fill(roomName);
|
||||
|
||||
const serverCard = page.locator('div[title]', { hasText: roomName }).first();
|
||||
|
||||
await expect(serverCard).toBeVisible({ timeout: 15_000 });
|
||||
await serverCard.dblclick();
|
||||
}
|
||||
|
||||
async function loginIfNeeded(page: Page, user: TestUser): Promise<void> {
|
||||
const loginPage = new LoginPage(page);
|
||||
|
||||
if (page.url().includes('/login')) {
|
||||
await expect(loginPage.usernameInput).toBeVisible({ timeout: 15_000 });
|
||||
await loginUser(page, user);
|
||||
return;
|
||||
}
|
||||
|
||||
if (await loginPage.usernameInput.isVisible().catch(() => false)) {
|
||||
await loginUser(page, user);
|
||||
}
|
||||
}
|
||||
|
||||
async function ensureCurrentUserScope(page: Page, user: TestUser): Promise<void> {
|
||||
if (await hasCurrentUserScope(page)) {
|
||||
return;
|
||||
}
|
||||
|
||||
await loginUser(page, user);
|
||||
await expect.poll(() => hasCurrentUserScope(page), { timeout: 10_000 }).toBe(true);
|
||||
}
|
||||
|
||||
async function hasCurrentUserScope(page: Page): Promise<boolean> {
|
||||
return page.evaluate(() => !!localStorage.getItem('metoyou_currentUserId')?.trim());
|
||||
}
|
||||
|
||||
async function openPersistedRoomById(page: Page, user: TestUser, roomId: string): Promise<void> {
|
||||
for (let attempt = 1; attempt <= 3; attempt += 1) {
|
||||
await page.goto(`/room/${roomId}`, { waitUntil: 'domcontentloaded' });
|
||||
|
||||
if (await waitForLoginForm(page, 5_000)) {
|
||||
await loginUser(page, user);
|
||||
continue;
|
||||
}
|
||||
|
||||
await expect(page).toHaveURL(/\/room\//, { timeout: 20_000 });
|
||||
|
||||
if (!await waitForLoginForm(page, 2_000)) {
|
||||
return;
|
||||
}
|
||||
|
||||
await loginUser(page, user);
|
||||
}
|
||||
|
||||
await page.goto(`/room/${roomId}`, { waitUntil: 'domcontentloaded' });
|
||||
await expect(page).toHaveURL(/\/room\//, { timeout: 20_000 });
|
||||
}
|
||||
|
||||
async function waitForLoginForm(page: Page, timeout: number): Promise<boolean> {
|
||||
try {
|
||||
await expect(new LoginPage(page).usernameInput).toBeVisible({ timeout });
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForServerSearch(page: Page, timeout: number): Promise<boolean> {
|
||||
try {
|
||||
await expect(new ServerSearchPage(page).searchInput).toBeVisible({ timeout });
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForVisibleText(page: Page, text: string, timeout: number): Promise<boolean> {
|
||||
try {
|
||||
await expect(page.getByText(text, { exact: false })).toBeVisible({ timeout });
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function expectMessagePersistedInIndexedDb(page: Page, messageText: string): Promise<void> {
|
||||
await expect.poll(
|
||||
() => getPersistedRoomIdForMessage(page, messageText).then((roomId) => !!roomId),
|
||||
{ timeout: 10_000 }
|
||||
).toBe(true);
|
||||
}
|
||||
|
||||
async function getPersistedRoomIdForMessage(page: Page, messageText: string): Promise<string | null> {
|
||||
return page.evaluate(async (expectedContent) => {
|
||||
const currentUserId = localStorage.getItem('metoyou_currentUserId')?.trim();
|
||||
const preferredDatabaseName = `metoyou::${encodeURIComponent(currentUserId || 'anonymous')}`;
|
||||
const discoveredDatabaseNames = typeof indexedDB.databases === 'function'
|
||||
? (await indexedDB.databases())
|
||||
.map((database) => database.name)
|
||||
.filter((name): name is string => !!name && (name === 'metoyou' || name.startsWith('metoyou::')))
|
||||
: null;
|
||||
const databaseNames = discoveredDatabaseNames ?? [preferredDatabaseName];
|
||||
const remainingDatabaseNames = databaseNames.filter((name) => name !== preferredDatabaseName);
|
||||
const orderedDatabaseNames = databaseNames.includes(preferredDatabaseName)
|
||||
? [preferredDatabaseName].concat(remainingDatabaseNames)
|
||||
: remainingDatabaseNames;
|
||||
|
||||
for (const databaseName of orderedDatabaseNames) {
|
||||
const database = await new Promise<IDBDatabase>((resolve, reject) => {
|
||||
const request = indexedDB.open(databaseName);
|
||||
|
||||
request.onerror = () => reject(request.error);
|
||||
request.onsuccess = () => resolve(request.result);
|
||||
});
|
||||
|
||||
try {
|
||||
if (!database.objectStoreNames.contains('messages')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const transaction = database.transaction('messages', 'readonly');
|
||||
const request = transaction.objectStore('messages').getAll();
|
||||
const roomId = await new Promise<string | null>((resolve, reject) => {
|
||||
request.onerror = () => reject(request.error);
|
||||
|
||||
request.onsuccess = () => {
|
||||
const match = ((request.result as { content?: string; roomId?: string }[]) ?? [])
|
||||
.find((message) => message.content === expectedContent);
|
||||
|
||||
resolve(match?.roomId ?? null);
|
||||
};
|
||||
});
|
||||
|
||||
if (roomId) {
|
||||
return roomId;
|
||||
}
|
||||
} finally {
|
||||
database.close();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}, messageText);
|
||||
}
|
||||
|
||||
function escapeRegExp(value: string): string {
|
||||
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
}
|
||||
|
||||
async function retryTransientNavigation<T>(navigate: () => Promise<T>, attempts = 4): Promise<T> {
|
||||
let lastError: unknown;
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import {
|
||||
expect,
|
||||
type BrowserContext,
|
||||
type Locator,
|
||||
type Page
|
||||
} from '@playwright/test';
|
||||
@@ -36,7 +35,6 @@ test.describe('Chat notifications', () => {
|
||||
await clearDesktopNotifications(scenario.alice.page);
|
||||
await scenario.bobRoom.joinTextChannel(scenario.channelName);
|
||||
await scenario.bobMessages.sendMessage(message);
|
||||
await expectUnreadCounts(scenario.alice.page, scenario.serverName, scenario.channelName);
|
||||
});
|
||||
|
||||
await test.step('Alice receives a desktop notification with the channel preview', async () => {
|
||||
@@ -69,7 +67,8 @@ test.describe('Chat notifications', () => {
|
||||
});
|
||||
|
||||
await test.step('Alice still sees unread badges for the room and channel', async () => {
|
||||
await expectUnreadCounts(scenario.alice.page, scenario.serverName, scenario.channelName);
|
||||
await expect(getUnreadBadge(getSavedRoomButton(scenario.alice.page, scenario.serverName))).toHaveText('1', { timeout: 20_000 });
|
||||
await expect(getUnreadBadge(getTextChannelButton(scenario.alice.page, scenario.channelName))).toHaveText('1', { timeout: 20_000 });
|
||||
});
|
||||
|
||||
await test.step('Alice does not get a muted desktop popup', async () => {
|
||||
@@ -97,7 +96,7 @@ async function createNotificationScenario(createClient: () => Promise<Client>):
|
||||
const alice = await createClient();
|
||||
const bob = await createClient();
|
||||
|
||||
await installDesktopNotificationSpy(alice.context);
|
||||
await installDesktopNotificationSpy(alice.page);
|
||||
|
||||
await registerUser(alice.page, aliceCredentials.username, aliceCredentials.displayName, aliceCredentials.password);
|
||||
await registerUser(bob.page, bobCredentials.username, bobCredentials.displayName, bobCredentials.password);
|
||||
@@ -144,8 +143,8 @@ async function registerUser(page: Page, username: string, displayName: string, p
|
||||
await expect(page).toHaveURL(/\/dashboard/, { timeout: 15_000 });
|
||||
}
|
||||
|
||||
async function installDesktopNotificationSpy(context: BrowserContext): Promise<void> {
|
||||
await context.addInitScript(() => {
|
||||
async function installDesktopNotificationSpy(page: Page): Promise<void> {
|
||||
await page.addInitScript(() => {
|
||||
const notifications: DesktopNotificationRecord[] = [];
|
||||
|
||||
class MockNotification {
|
||||
@@ -251,11 +250,6 @@ function getUnreadBadge(container: Locator): Locator {
|
||||
return container.locator('span.rounded-full').first();
|
||||
}
|
||||
|
||||
async function expectUnreadCounts(page: Page, serverName: string, channelName: string): Promise<void> {
|
||||
await expect(getUnreadBadge(getSavedRoomButton(page, serverName))).toHaveText('1', { timeout: 45_000 });
|
||||
await expect(getUnreadBadge(getTextChannelButton(page, channelName))).toHaveText('1', { timeout: 45_000 });
|
||||
}
|
||||
|
||||
function uniqueName(prefix: string): string {
|
||||
return `${prefix}-${Date.now()}-${Math.random().toString(36)
|
||||
.slice(2, 8)}`;
|
||||
|
||||
@@ -367,10 +367,11 @@ async function launchPersistentSession(
|
||||
});
|
||||
|
||||
await installTestServerEndpoint(context, testServerPort);
|
||||
await installWebRTCTracking(context);
|
||||
|
||||
const page = context.pages()[0] ?? await context.newPage();
|
||||
|
||||
await installWebRTCTracking(page);
|
||||
|
||||
return { context, page };
|
||||
}
|
||||
|
||||
|
||||
@@ -196,10 +196,11 @@ async function launchPersistentSession(userDataDir: string, testServerPort: numb
|
||||
});
|
||||
|
||||
await installTestServerEndpoint(context, testServerPort);
|
||||
await installWebRTCTracking(context);
|
||||
|
||||
const page = context.pages()[0] ?? (await context.newPage());
|
||||
|
||||
await installWebRTCTracking(page);
|
||||
|
||||
return { context, page };
|
||||
}
|
||||
|
||||
|
||||
@@ -4,20 +4,13 @@ import {
|
||||
test,
|
||||
type Client
|
||||
} from '../../fixtures/multi-client';
|
||||
import { openPluginStore } from '../../helpers/app-menu';
|
||||
import {
|
||||
addPluginSource,
|
||||
E2E_PLUGIN_SOURCE_URL,
|
||||
E2E_PLUGIN_TITLE
|
||||
} from '../../helpers/plugin-store';
|
||||
import { installWebRTCTracking } from '../../helpers/webrtc-helpers';
|
||||
import { ChatMessagesPage } from '../../pages/chat-messages.page';
|
||||
import { ChatRoomPage } from '../../pages/chat-room.page';
|
||||
import { RegisterPage } from '../../pages/register.page';
|
||||
import { ServerSearchPage } from '../../pages/server-search.page';
|
||||
|
||||
const PLUGIN_SOURCE_URL = E2E_PLUGIN_SOURCE_URL;
|
||||
const PLUGIN_TITLE = E2E_PLUGIN_TITLE;
|
||||
const PLUGIN_SOURCE_URL = 'http://localhost:4200/plugins/e2e-plugin-source.json';
|
||||
const PLUGIN_TITLE = 'E2E All API Plugin';
|
||||
const EDITED_MESSAGE = 'Plugin API edited message';
|
||||
const ORIGINAL_MESSAGE = 'Plugin API original message';
|
||||
const DELETED_MESSAGE = 'Plugin API deleted message';
|
||||
@@ -94,9 +87,6 @@ async function createPluginApiScenario(createClient: () => Promise<Client>): Pro
|
||||
const alice = await createClient();
|
||||
const bob = await createClient();
|
||||
|
||||
await installWebRTCTracking(alice.page);
|
||||
await installWebRTCTracking(bob.page);
|
||||
|
||||
await registerUser(alice.page, `alice_${suffix}`, 'Alice');
|
||||
await registerUser(bob.page, `bob_${suffix}`, 'Bob');
|
||||
|
||||
@@ -108,10 +98,13 @@ async function createPluginApiScenario(createClient: () => Promise<Client>): Pro
|
||||
const aliceRoom = new ChatRoomPage(alice.page);
|
||||
|
||||
await aliceRoom.ensureVoiceChannelExists(VOICE_CHANNEL);
|
||||
await installGrantAndActivatePlugin(alice.page, true);
|
||||
await closeSettingsModal(alice.page);
|
||||
await expect(soundboardComposerButton(alice.page)).toBeVisible({ timeout: 20_000 });
|
||||
|
||||
const bobSearch = new ServerSearchPage(bob.page);
|
||||
|
||||
await bobSearch.joinServerFromSearch(serverName);
|
||||
await bobSearch.joinServerFromSearch(serverName, { acceptPluginDownloads: true });
|
||||
await expect(bob.page).toHaveURL(/\/room\//, { timeout: 30_000 });
|
||||
|
||||
const bobRoom = new ChatRoomPage(bob.page);
|
||||
@@ -120,9 +113,6 @@ async function createPluginApiScenario(createClient: () => Promise<Client>): Pro
|
||||
await bobRoom.joinVoiceChannel(VOICE_CHANNEL);
|
||||
await expect(aliceRoom.voiceControls).toBeVisible({ timeout: 30_000 });
|
||||
await expect(bobRoom.voiceControls).toBeVisible({ timeout: 30_000 });
|
||||
await installGrantAndActivatePlugin(alice.page, true);
|
||||
await closeSettingsModal(alice.page);
|
||||
await expect(soundboardComposerButton(alice.page)).toBeVisible({ timeout: 20_000 });
|
||||
|
||||
const aliceMessages = new ChatMessagesPage(alice.page);
|
||||
const bobMessages = new ChatMessagesPage(bob.page);
|
||||
@@ -151,11 +141,14 @@ async function registerUser(page: Page, username: string, displayName: string):
|
||||
}
|
||||
|
||||
async function installGrantAndActivatePlugin(page: Page, installFromStore: boolean): Promise<void> {
|
||||
await openPluginStore(page);
|
||||
await page.getByRole('button', { name: 'Plugin Store' }).click();
|
||||
await expect(page).toHaveURL(/\/plugin-store/, { timeout: 20_000 });
|
||||
await expect(page.getByTestId('plugin-store-page')).toBeVisible({ timeout: 20_000 });
|
||||
|
||||
if (installFromStore) {
|
||||
await addPluginSource(page, PLUGIN_SOURCE_URL);
|
||||
await page.getByLabel('Plugin source manifest URL').fill(PLUGIN_SOURCE_URL);
|
||||
await page.getByRole('button', { name: 'Add Source' }).click();
|
||||
await expect(page.getByRole('heading', { name: PLUGIN_TITLE })).toBeVisible({ timeout: 20_000 });
|
||||
await page.locator('article', { hasText: PLUGIN_TITLE }).getByRole('button', { exact: true, name: /^(Install|Install to Server)$/ })
|
||||
.click();
|
||||
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
import { expect, test } from '../../fixtures/multi-client';
|
||||
import { openPluginStore } from '../../helpers/app-menu';
|
||||
import { expectDashboardReady } from '../../helpers/dashboard';
|
||||
import { addPluginSource } from '../../helpers/plugin-store';
|
||||
import { RegisterPage } from '../../pages/register.page';
|
||||
import { ServerSearchPage } from '../../pages/server-search.page';
|
||||
|
||||
@@ -18,7 +15,7 @@ test.describe('Plugin manager UI', () => {
|
||||
await test.step('Register user and create server context', async () => {
|
||||
await register.goto();
|
||||
await register.register(`plugin_${suffix}`, 'Plugin Tester', 'TestPass123!');
|
||||
await expectDashboardReady(page);
|
||||
await expect(page.getByPlaceholder('Search people, servers, or paste an invite...')).toBeVisible({ timeout: 30_000 });
|
||||
await search.createServer(`Plugin API Server ${suffix}`, {
|
||||
description: 'Plugin manager UI E2E coverage'
|
||||
});
|
||||
@@ -26,13 +23,16 @@ test.describe('Plugin manager UI', () => {
|
||||
await expect(page).toHaveURL(/\/room\//, { timeout: 30_000 });
|
||||
});
|
||||
|
||||
await test.step('Open Plugin Store from the title-bar menu', async () => {
|
||||
await openPluginStore(page);
|
||||
await test.step('Open visible Plugin Store button', async () => {
|
||||
await page.getByRole('button', { name: 'Plugin Store' }).click();
|
||||
await expect(page).toHaveURL(/\/plugin-store/, { timeout: 10_000 });
|
||||
await expect(page.getByTestId('plugin-store-page')).toBeVisible({ timeout: 10_000 });
|
||||
});
|
||||
|
||||
await test.step('Install fixture plugin from source manifest', async () => {
|
||||
await addPluginSource(page);
|
||||
await page.getByLabel('Plugin source manifest URL').fill('http://localhost:4200/plugins/e2e-plugin-source.json');
|
||||
await page.getByRole('button', { name: 'Add Source' }).click();
|
||||
await expect(page.getByRole('heading', { name: 'E2E All API Plugin' })).toBeVisible({ timeout: 15_000 });
|
||||
const pluginCard = page.locator('article', { hasText: 'E2E All API Plugin' });
|
||||
|
||||
await pluginCard.getByRole('button', { name: 'Readme' }).click();
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
import type { APIRequestContext, APIResponse } from '@playwright/test';
|
||||
import WebSocket from 'ws';
|
||||
import { expect, test } from '../../fixtures/multi-client';
|
||||
import {
|
||||
authHeaders,
|
||||
registerTestUser,
|
||||
type AuthSession
|
||||
} from '../../helpers/auth-api';
|
||||
import {
|
||||
getPluginApiTestEvent,
|
||||
readPluginApiTestManifest,
|
||||
@@ -14,6 +9,8 @@ import {
|
||||
TEST_PLUGIN_RELAY_EVENT
|
||||
} from '../../helpers/plugin-api-test-fixture';
|
||||
|
||||
const OWNER_USER_ID = 'plugin-api-owner';
|
||||
|
||||
interface CreatedServerResponse {
|
||||
id: string;
|
||||
}
|
||||
@@ -57,25 +54,10 @@ interface TestSocket {
|
||||
test.describe('Plugin support API', () => {
|
||||
test('covers plugin requirement, event, data, and websocket APIs with the fixture plugin', async ({ request, testServer }) => {
|
||||
const manifest = await readPluginApiTestManifest();
|
||||
const owner = await registerTestUser(
|
||||
request,
|
||||
testServer.url,
|
||||
`plugin-owner-${Date.now()}`,
|
||||
'TestPass123!',
|
||||
'Plugin Owner'
|
||||
);
|
||||
const peer = await registerTestUser(
|
||||
request,
|
||||
testServer.url,
|
||||
`plugin-peer-${Date.now()}`,
|
||||
'TestPass123!',
|
||||
'Plugin Peer'
|
||||
);
|
||||
const server = await createServer(request, testServer.url, owner, `Plugin API ${Date.now()}`);
|
||||
const server = await createServer(request, testServer.url, `Plugin API ${Date.now()}`);
|
||||
const relayEvent = getPluginApiTestEvent(manifest, TEST_PLUGIN_RELAY_EVENT);
|
||||
const p2pEvent = getPluginApiTestEvent(manifest, TEST_PLUGIN_P2P_EVENT);
|
||||
const pluginsApi = `${testServer.url}/api/servers/${encodeURIComponent(server.id)}/plugins`;
|
||||
const ownerHeaders = authHeaders(owner.token);
|
||||
|
||||
await test.step('Initial snapshot is empty', async () => {
|
||||
const snapshot = await expectJson<PluginSnapshotResponse>(await request.get(pluginsApi));
|
||||
@@ -89,8 +71,8 @@ test.describe('Plugin support API', () => {
|
||||
|
||||
await test.step('Requirement API enforces server management permission', async () => {
|
||||
const response = await request.put(`${pluginsApi}/${TEST_PLUGIN_ID}/requirement`, {
|
||||
headers: authHeaders(peer.token),
|
||||
data: {
|
||||
actorUserId: 'not-the-owner',
|
||||
status: 'required'
|
||||
}
|
||||
});
|
||||
@@ -101,8 +83,8 @@ test.describe('Plugin support API', () => {
|
||||
|
||||
await test.step('Requirement and event definition APIs persist the test plugin contract', async () => {
|
||||
const requirement = await expectJson<PluginRequirementResponse>(await request.put(`${pluginsApi}/${TEST_PLUGIN_ID}/requirement`, {
|
||||
headers: ownerHeaders,
|
||||
data: {
|
||||
actorUserId: OWNER_USER_ID,
|
||||
reason: manifest.description,
|
||||
status: 'required',
|
||||
versionRange: `^${manifest.version}`
|
||||
@@ -116,8 +98,8 @@ test.describe('Plugin support API', () => {
|
||||
versionRange: `^${manifest.version}`
|
||||
}));
|
||||
|
||||
const relayDefinition = await upsertEventDefinition(request, pluginsApi, ownerHeaders, relayEvent);
|
||||
const p2pDefinition = await upsertEventDefinition(request, pluginsApi, ownerHeaders, p2pEvent);
|
||||
const relayDefinition = await upsertEventDefinition(request, pluginsApi, relayEvent);
|
||||
const p2pDefinition = await upsertEventDefinition(request, pluginsApi, p2pEvent);
|
||||
|
||||
expect(relayDefinition.eventDefinition).toEqual(expect.objectContaining({
|
||||
direction: 'serverRelay',
|
||||
@@ -141,8 +123,8 @@ test.describe('Plugin support API', () => {
|
||||
|
||||
await test.step('Plugin data API refuses arbitrary server persistence', async () => {
|
||||
const stored = await expectJson<{ errorCode: string }>(await request.put(`${pluginsApi}/${TEST_PLUGIN_ID}/data/settings`, {
|
||||
headers: ownerHeaders,
|
||||
data: {
|
||||
actorUserId: OWNER_USER_ID,
|
||||
schemaVersion: 1,
|
||||
scope: 'server',
|
||||
value: {
|
||||
@@ -158,15 +140,15 @@ test.describe('Plugin support API', () => {
|
||||
params: {
|
||||
key: 'settings',
|
||||
scope: 'server',
|
||||
userId: owner.id
|
||||
userId: OWNER_USER_ID
|
||||
}
|
||||
}), 410);
|
||||
|
||||
expect(listed.errorCode).toBe('PLUGIN_DATA_DISABLED');
|
||||
|
||||
const afterDelete = await expectJson<{ errorCode: string }>(await request.delete(`${pluginsApi}/${TEST_PLUGIN_ID}/data/settings`, {
|
||||
headers: ownerHeaders,
|
||||
data: {
|
||||
actorUserId: OWNER_USER_ID,
|
||||
scope: 'server'
|
||||
}
|
||||
}), 410);
|
||||
@@ -179,8 +161,8 @@ test.describe('Plugin support API', () => {
|
||||
const bob = await openTestSocket(testServer.url);
|
||||
|
||||
try {
|
||||
await identifySocket(alice, owner.token, 'Plugin Owner');
|
||||
await identifySocket(bob, peer.token, 'Plugin Peer');
|
||||
alice.send({ type: 'identify', oderId: OWNER_USER_ID, displayName: 'Plugin Owner' });
|
||||
bob.send({ type: 'identify', oderId: 'plugin-api-peer', displayName: 'Plugin Peer' });
|
||||
alice.send({ type: 'join_server', serverId: server.id });
|
||||
bob.send({ type: 'join_server', serverId: server.id });
|
||||
|
||||
@@ -211,7 +193,7 @@ test.describe('Plugin support API', () => {
|
||||
pluginId: TEST_PLUGIN_ID,
|
||||
serverId: server.id,
|
||||
sourcePluginUserId: 'fixture-plugin-user',
|
||||
sourceUserId: owner.id
|
||||
sourceUserId: OWNER_USER_ID
|
||||
}));
|
||||
|
||||
expect(relayedEvent['payload']).toEqual({ message: 'hello from fixture plugin' });
|
||||
@@ -255,15 +237,15 @@ test.describe('Plugin support API', () => {
|
||||
|
||||
await test.step('Delete APIs remove event definitions and requirements', async () => {
|
||||
await expectJson<{ ok: boolean }>(await request.delete(`${pluginsApi}/${TEST_PLUGIN_ID}/events/${TEST_PLUGIN_RELAY_EVENT}`, {
|
||||
headers: ownerHeaders
|
||||
data: { actorUserId: OWNER_USER_ID }
|
||||
}));
|
||||
|
||||
await expectJson<{ ok: boolean }>(await request.delete(`${pluginsApi}/${TEST_PLUGIN_ID}/events/${TEST_PLUGIN_P2P_EVENT}`, {
|
||||
headers: ownerHeaders
|
||||
data: { actorUserId: OWNER_USER_ID }
|
||||
}));
|
||||
|
||||
await expectJson<{ ok: boolean }>(await request.delete(`${pluginsApi}/${TEST_PLUGIN_ID}/requirement`, {
|
||||
headers: ownerHeaders
|
||||
data: { actorUserId: OWNER_USER_ID }
|
||||
}));
|
||||
|
||||
const snapshot = await expectJson<PluginSnapshotResponse>(await request.get(pluginsApi));
|
||||
@@ -277,11 +259,9 @@ test.describe('Plugin support API', () => {
|
||||
async function createServer(
|
||||
request: APIRequestContext,
|
||||
baseUrl: string,
|
||||
owner: AuthSession,
|
||||
serverName: string
|
||||
): Promise<CreatedServerResponse> {
|
||||
const response = await request.post(`${baseUrl}/api/servers`, {
|
||||
headers: authHeaders(owner.token),
|
||||
data: {
|
||||
channels: [
|
||||
{
|
||||
@@ -295,7 +275,7 @@ async function createServer(
|
||||
id: `plugin-api-${Date.now()}`,
|
||||
isPrivate: false,
|
||||
name: serverName,
|
||||
ownerId: owner.id,
|
||||
ownerId: OWNER_USER_ID,
|
||||
ownerPublicKey: 'plugin-api-owner-public-key',
|
||||
tags: ['plugins']
|
||||
}
|
||||
@@ -307,14 +287,13 @@ async function createServer(
|
||||
async function upsertEventDefinition(
|
||||
request: APIRequestContext,
|
||||
pluginsApi: string,
|
||||
headers: Record<string, string>,
|
||||
eventDefinition: ReturnType<typeof getPluginApiTestEvent>
|
||||
): Promise<PluginEventDefinitionResponse> {
|
||||
return await expectJson<PluginEventDefinitionResponse>(await request.put(
|
||||
`${pluginsApi}/${TEST_PLUGIN_ID}/events/${encodeURIComponent(eventDefinition.eventName)}`,
|
||||
{
|
||||
headers,
|
||||
data: {
|
||||
actorUserId: OWNER_USER_ID,
|
||||
direction: eventDefinition.direction,
|
||||
maxPayloadBytes: eventDefinition.maxPayloadBytes,
|
||||
schemaJson: '{"type":"object"}',
|
||||
@@ -330,20 +309,6 @@ async function expectJson<T>(response: APIResponse, status = 200): Promise<T> {
|
||||
return await response.json() as T;
|
||||
}
|
||||
|
||||
async function identifySocket(socket: TestSocket, token: string, displayName: string): Promise<void> {
|
||||
socket.send({ type: 'identify', token, displayName });
|
||||
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(resolve, 300);
|
||||
});
|
||||
|
||||
const authError = socket.messages.find((message) => message.type === 'auth_error');
|
||||
|
||||
if (authError) {
|
||||
throw new Error(`WebSocket identify failed: ${JSON.stringify(authError)}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function openTestSocket(baseUrl: string): Promise<TestSocket> {
|
||||
const socketUrl = baseUrl.replace(/^http/, 'ws');
|
||||
const socket = new WebSocket(socketUrl);
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { test, expect } from '../../fixtures/multi-client';
|
||||
import { expectDashboardReady } from '../../helpers/dashboard';
|
||||
import { RegisterPage } from '../../pages/register.page';
|
||||
import { ServerSearchPage } from '../../pages/server-search.page';
|
||||
import { ChatRoomPage } from '../../pages/chat-room.page';
|
||||
@@ -89,7 +88,7 @@ test.describe('Connectivity warning', () => {
|
||||
|
||||
await register.goto();
|
||||
await register.register(`alice_${suffix}`, 'Alice', 'TestPass123!');
|
||||
await expectDashboardReady(alice.page);
|
||||
await expect(alice.page.getByPlaceholder('Search people, servers, or paste an invite...')).toBeVisible({ timeout: 30_000 });
|
||||
});
|
||||
|
||||
await test.step('Register Bob', async () => {
|
||||
@@ -97,7 +96,7 @@ test.describe('Connectivity warning', () => {
|
||||
|
||||
await register.goto();
|
||||
await register.register(`bob_${suffix}`, 'Bob', 'TestPass123!');
|
||||
await expectDashboardReady(bob.page);
|
||||
await expect(bob.page.getByPlaceholder('Search people, servers, or paste an invite...')).toBeVisible({ timeout: 30_000 });
|
||||
});
|
||||
|
||||
await test.step('Register Charlie', async () => {
|
||||
@@ -105,7 +104,7 @@ test.describe('Connectivity warning', () => {
|
||||
|
||||
await register.goto();
|
||||
await register.register(`charlie_${suffix}`, 'Charlie', 'TestPass123!');
|
||||
await expectDashboardReady(charlie.page);
|
||||
await expect(charlie.page.getByPlaceholder('Search people, servers, or paste an invite...')).toBeVisible({ timeout: 30_000 });
|
||||
});
|
||||
|
||||
// ── Create server and have everyone join ──
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import { test, expect } from '../../fixtures/multi-client';
|
||||
import { openSettingsFromMenu } from '../../helpers/app-menu';
|
||||
import { expectDashboardReady } from '../../helpers/dashboard';
|
||||
import { RegisterPage } from '../../pages/register.page';
|
||||
|
||||
test.describe('ICE server settings', () => {
|
||||
@@ -11,8 +9,8 @@ test.describe('ICE server settings', () => {
|
||||
|
||||
await register.goto();
|
||||
await register.register(`user_${suffix}`, 'IceTestUser', 'TestPass123!');
|
||||
await expectDashboardReady(page);
|
||||
await openSettingsFromMenu(page);
|
||||
await expect(page.getByPlaceholder('Search people, servers, or paste an invite...')).toBeVisible({ timeout: 30_000 });
|
||||
await page.getByTitle('Settings').click();
|
||||
await expect(page.getByRole('button', { name: 'Network' })).toBeVisible({ timeout: 10_000 });
|
||||
await page.getByRole('button', { name: 'Network' }).click();
|
||||
await expect(page.getByTestId('ice-server-settings')).toBeVisible({ timeout: 10_000 });
|
||||
@@ -103,7 +101,7 @@ test.describe('ICE server settings', () => {
|
||||
await expect(page.getByText('stun:persist-test.example.com:3478')).toBeVisible({ timeout: 5_000 });
|
||||
|
||||
await page.reload({ waitUntil: 'domcontentloaded' });
|
||||
await openSettingsFromMenu(page);
|
||||
await page.getByTitle('Settings').click();
|
||||
await expect(page.getByRole('button', { name: 'Network' })).toBeVisible({ timeout: 10_000 });
|
||||
await page.getByRole('button', { name: 'Network' }).click();
|
||||
await expect(page.getByText('stun:persist-test.example.com:3478')).toBeVisible({ timeout: 10_000 });
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { test, expect } from '../../fixtures/multi-client';
|
||||
import { expectDashboardReady } from '../../helpers/dashboard';
|
||||
import { RegisterPage } from '../../pages/register.page';
|
||||
import { ServerSearchPage } from '../../pages/server-search.page';
|
||||
import { ChatRoomPage } from '../../pages/chat-room.page';
|
||||
@@ -90,7 +89,7 @@ test.describe('STUN/TURN fallback behaviour', () => {
|
||||
|
||||
await register.goto();
|
||||
await register.register(`alice_${suffix}`, 'Alice', 'TestPass123!');
|
||||
await expectDashboardReady(alice.page);
|
||||
await expect(alice.page.getByPlaceholder('Search people, servers, or paste an invite...')).toBeVisible({ timeout: 30_000 });
|
||||
});
|
||||
|
||||
await test.step('Register Bob', async () => {
|
||||
@@ -98,7 +97,7 @@ test.describe('STUN/TURN fallback behaviour', () => {
|
||||
|
||||
await register.goto();
|
||||
await register.register(`bob_${suffix}`, 'Bob', 'TestPass123!');
|
||||
await expectDashboardReady(bob.page);
|
||||
await expect(bob.page.getByPlaceholder('Search people, servers, or paste an invite...')).toBeVisible({ timeout: 30_000 });
|
||||
});
|
||||
|
||||
await test.step('Alice creates a server', async () => {
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
import {
|
||||
expect,
|
||||
type APIRequestContext,
|
||||
type Page
|
||||
} from '@playwright/test';
|
||||
import { expect, type Page } from '@playwright/test';
|
||||
import { test, type Client } from '../../fixtures/multi-client';
|
||||
import { installTestServerEndpoints, type SeededEndpointInput } from '../../helpers/seed-test-endpoint';
|
||||
import { startTestServer } from '../../helpers/test-server';
|
||||
@@ -15,11 +11,6 @@ import {
|
||||
waitForConnectedPeerCount,
|
||||
waitForPeerConnected
|
||||
} from '../../helpers/webrtc-helpers';
|
||||
import {
|
||||
authHeaders,
|
||||
readAuthTokenFromPage,
|
||||
registerTestUser
|
||||
} from '../../helpers/auth-api';
|
||||
import { RegisterPage } from '../../pages/register.page';
|
||||
import { ServerSearchPage } from '../../pages/server-search.page';
|
||||
import { ChatRoomPage } from '../../pages/chat-room.page';
|
||||
@@ -113,7 +104,6 @@ function endpointsForGroup(
|
||||
test.describe('Mixed signal-config voice', () => {
|
||||
test('8 users with different signal configs can voice, mute, deafen, and chat concurrently', async ({
|
||||
createClient,
|
||||
request,
|
||||
testServer
|
||||
}) => {
|
||||
test.setTimeout(720_000);
|
||||
@@ -150,32 +140,10 @@ test.describe('Mixed signal-config voice', () => {
|
||||
}
|
||||
});
|
||||
|
||||
let secondaryRoomId = '';
|
||||
|
||||
// ── Create rooms ────────────────────────────────────────────
|
||||
await test.step('Create voice room on primary and chat room on secondary', async () => {
|
||||
// Use a "both" user (client 0) to create both rooms
|
||||
const searchPage = new ServerSearchPage(clients[0].page);
|
||||
const secondarySession = await registerTestUser(
|
||||
request,
|
||||
secondaryServer.url,
|
||||
clients[0].user.username,
|
||||
clients[0].user.password,
|
||||
clients[0].user.displayName
|
||||
);
|
||||
|
||||
await clients[0].page.evaluate(({ serverUrl, token, expiresAt }) => {
|
||||
const storageKey = 'metoyou.authTokens';
|
||||
const store = JSON.parse(localStorage.getItem(storageKey) || '{}') as Record<string, { token: string; expiresAt: number }>;
|
||||
const normalizedUrl = serverUrl.trim().replace(/\/+$/, '');
|
||||
|
||||
store[normalizedUrl] = { token, expiresAt };
|
||||
localStorage.setItem(storageKey, JSON.stringify(store));
|
||||
}, {
|
||||
serverUrl: secondaryServer.url,
|
||||
token: secondarySession.token,
|
||||
expiresAt: secondarySession.expiresAt
|
||||
});
|
||||
|
||||
await searchPage.createServer(VOICE_ROOM_NAME, {
|
||||
description: 'Voice room on primary signal',
|
||||
@@ -184,14 +152,12 @@ test.describe('Mixed signal-config voice', () => {
|
||||
|
||||
await expect(clients[0].page).toHaveURL(/\/room\//, { timeout: 20_000 });
|
||||
|
||||
const secondaryRoom = await createServerViaApi(
|
||||
request,
|
||||
secondaryServer.url,
|
||||
secondarySession,
|
||||
SECONDARY_ROOM_NAME
|
||||
);
|
||||
await searchPage.createServer(SECONDARY_ROOM_NAME, {
|
||||
description: 'Chat room on secondary signal',
|
||||
sourceId: SECONDARY_SIGNAL_ID
|
||||
});
|
||||
|
||||
secondaryRoomId = secondaryRoom.id;
|
||||
await expect(clients[0].page).toHaveURL(/\/room\//, { timeout: 20_000 });
|
||||
});
|
||||
|
||||
// ── Create invite links ─────────────────────────────────────
|
||||
@@ -205,33 +171,26 @@ test.describe('Mixed signal-config voice', () => {
|
||||
// Navigate to voice room to get its ID
|
||||
await openSavedRoomByName(clients[0].page, VOICE_ROOM_NAME);
|
||||
const primaryRoomId = await getCurrentRoomId(clients[0].page);
|
||||
const userId = await getCurrentUserId(clients[0].page);
|
||||
|
||||
// Navigate to secondary room to get its ID
|
||||
await openSavedRoomByName(clients[0].page, SECONDARY_ROOM_NAME);
|
||||
const secondaryRoomId = await getCurrentRoomId(clients[0].page);
|
||||
// Create invite for primary room (voice) via API
|
||||
const primaryToken = await readAuthTokenFromPage(clients[0].page, testServer.url);
|
||||
|
||||
if (!primaryToken) {
|
||||
throw new Error('Missing session token for primary signal invite creation');
|
||||
}
|
||||
|
||||
const primaryInvite = await createInviteViaApi(
|
||||
testServer.url,
|
||||
primaryRoomId,
|
||||
primaryToken,
|
||||
userId,
|
||||
clients[0].user.displayName
|
||||
);
|
||||
|
||||
primaryRoomInviteUrl = `/invite/${primaryInvite.id}?server=${encodeURIComponent(testServer.url)}`;
|
||||
|
||||
// Create invite for secondary room (chat) via API
|
||||
const secondaryToken = await readAuthTokenFromPage(clients[0].page, secondaryServer.url);
|
||||
|
||||
if (!secondaryToken) {
|
||||
throw new Error('Missing session token for secondary signal invite creation');
|
||||
}
|
||||
|
||||
const secondaryInvite = await createInviteViaApi(
|
||||
secondaryServer.url,
|
||||
secondaryRoomId,
|
||||
secondaryToken,
|
||||
userId,
|
||||
clients[0].user.displayName
|
||||
);
|
||||
|
||||
@@ -504,55 +463,17 @@ function buildUsers(): TestUser[] {
|
||||
|
||||
// ── API helpers ──────────────────────────────────────────────────────
|
||||
|
||||
async function createServerViaApi(
|
||||
request: APIRequestContext,
|
||||
serverBaseUrl: string,
|
||||
owner: { id: string; token: string },
|
||||
serverName: string
|
||||
): Promise<{ id: string }> {
|
||||
const response = await request.post(`${serverBaseUrl}/api/servers`, {
|
||||
headers: authHeaders(owner.token),
|
||||
data: {
|
||||
channels: [
|
||||
{
|
||||
id: 'general-text',
|
||||
name: 'general',
|
||||
position: 0,
|
||||
type: 'text'
|
||||
}
|
||||
],
|
||||
description: `E2E room on ${serverBaseUrl}`,
|
||||
id: `mixed-signal-${Date.now()}-${Math.random()
|
||||
.toString(36)
|
||||
.slice(2, 8)}`,
|
||||
isPrivate: false,
|
||||
name: serverName,
|
||||
ownerId: owner.id,
|
||||
ownerPublicKey: 'mixed-signal-owner-public-key',
|
||||
tags: ['e2e']
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok()) {
|
||||
throw new Error(`Failed to create server via API: ${response.status()} ${await response.text()}`);
|
||||
}
|
||||
|
||||
return await response.json() as { id: string };
|
||||
}
|
||||
|
||||
async function createInviteViaApi(
|
||||
serverBaseUrl: string,
|
||||
roomId: string,
|
||||
authToken: string,
|
||||
userId: string,
|
||||
displayName: string
|
||||
): Promise<{ id: string }> {
|
||||
const response = await fetch(`${serverBaseUrl}/api/servers/${roomId}/invites`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${authToken}`
|
||||
},
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
requesterUserId: userId,
|
||||
requesterDisplayName: displayName
|
||||
})
|
||||
});
|
||||
@@ -589,6 +510,34 @@ async function getCurrentRoomId(page: Page): Promise<string> {
|
||||
});
|
||||
}
|
||||
|
||||
async function getCurrentUserId(page: Page): Promise<string> {
|
||||
return await page.evaluate(() => {
|
||||
interface AngularDebugApi {
|
||||
getComponent: (element: Element) => Record<string, unknown>;
|
||||
}
|
||||
|
||||
interface UserShape {
|
||||
id: string;
|
||||
}
|
||||
|
||||
const host = document.querySelector('app-rooms-side-panel');
|
||||
const debugApi = (window as { ng?: AngularDebugApi }).ng;
|
||||
|
||||
if (!host || !debugApi?.getComponent) {
|
||||
throw new Error('Angular debug API unavailable');
|
||||
}
|
||||
|
||||
const component = debugApi.getComponent(host);
|
||||
const user = (component['currentUser'] as (() => UserShape | null) | undefined)?.();
|
||||
|
||||
if (!user?.id) {
|
||||
throw new Error('Current user not found');
|
||||
}
|
||||
|
||||
return user.id;
|
||||
});
|
||||
}
|
||||
|
||||
// ── Navigation helpers ───────────────────────────────────────────────
|
||||
|
||||
async function installDeterministicVoiceSettings(page: Page): Promise<void> {
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
expect,
|
||||
it
|
||||
} from 'vitest';
|
||||
import { getLocalApiTokenTtlMs } from './auth-store';
|
||||
|
||||
const TEN_YEARS_MS = 10 * 365 * 24 * 60 * 60 * 1000;
|
||||
|
||||
describe('auth-store', () => {
|
||||
it('defaults local API tokens to a very long lifetime', () => {
|
||||
expect(getLocalApiTokenTtlMs()).toBe(TEN_YEARS_MS);
|
||||
});
|
||||
});
|
||||
@@ -10,13 +10,9 @@ export interface IssuedToken {
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
const DEFAULT_TOKEN_TTL_MS = 10 * 365 * 24 * 60 * 60 * 1000;
|
||||
const TOKEN_TTL_MS = 24 * 60 * 60 * 1000;
|
||||
const tokens = new Map<string, IssuedToken>();
|
||||
|
||||
export function getLocalApiTokenTtlMs(): number {
|
||||
return DEFAULT_TOKEN_TTL_MS;
|
||||
}
|
||||
|
||||
export function issueToken(params: {
|
||||
userId: string;
|
||||
username: string;
|
||||
@@ -28,7 +24,7 @@ export function issueToken(params: {
|
||||
const issued: IssuedToken = {
|
||||
token,
|
||||
issuedAt,
|
||||
expiresAt: issuedAt + getLocalApiTokenTtlMs(),
|
||||
expiresAt: issuedAt + TOKEN_TTL_MS,
|
||||
userId: params.userId,
|
||||
username: params.username,
|
||||
displayName: params.displayName,
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
import { safeStorage } from 'electron';
|
||||
import {
|
||||
mkdir,
|
||||
readFile,
|
||||
writeFile
|
||||
} from 'fs/promises';
|
||||
import path from 'path';
|
||||
import { app } from 'electron';
|
||||
|
||||
const STORAGE_DIR_NAME = 'provision-secrets';
|
||||
|
||||
function getStorageDir(): string {
|
||||
return path.join(app.getPath('userData'), STORAGE_DIR_NAME);
|
||||
}
|
||||
|
||||
function getSecretFilePath(homeUserId: string): string {
|
||||
return path.join(getStorageDir(), `${homeUserId}.bin`);
|
||||
}
|
||||
|
||||
async function ensureStorageDir(): Promise<void> {
|
||||
await mkdir(getStorageDir(), { recursive: true });
|
||||
}
|
||||
|
||||
export async function storeProvisionSecret(homeUserId: string, secret: string): Promise<boolean> {
|
||||
if (!homeUserId.trim() || !secret) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await ensureStorageDir();
|
||||
|
||||
if (!safeStorage.isEncryptionAvailable()) {
|
||||
await writeFile(getSecretFilePath(homeUserId), secret, 'utf8');
|
||||
return true;
|
||||
}
|
||||
|
||||
const encrypted = safeStorage.encryptString(secret);
|
||||
|
||||
await writeFile(getSecretFilePath(homeUserId), encrypted);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function getProvisionSecret(homeUserId: string): Promise<string | null> {
|
||||
if (!homeUserId.trim()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const filePath = getSecretFilePath(homeUserId);
|
||||
const payload = await readFile(filePath);
|
||||
|
||||
if (!safeStorage.isEncryptionAvailable()) {
|
||||
return payload.toString('utf8');
|
||||
}
|
||||
|
||||
return safeStorage.decryptString(payload);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -22,12 +22,6 @@ import {
|
||||
setupWindowControlHandlers
|
||||
} from '../ipc';
|
||||
import { startIdleMonitor, stopIdleMonitor } from '../idle/idle-monitor';
|
||||
import {
|
||||
attachRendererDiagnosticsHooks,
|
||||
ensurePerfDiagIpcRegistered,
|
||||
shutdownPerfDiagnostics,
|
||||
startPerfDiagnostics
|
||||
} from '../diagnostics';
|
||||
|
||||
function startLocalApiAfterWindowReady(): void {
|
||||
setImmediate(() => {
|
||||
@@ -38,8 +32,6 @@ function startLocalApiAfterWindowReady(): void {
|
||||
}
|
||||
|
||||
export function registerAppLifecycle(): void {
|
||||
ensurePerfDiagIpcRegistered();
|
||||
|
||||
app.whenReady().then(async () => {
|
||||
const dockIconPath = getDockIconPath();
|
||||
|
||||
@@ -53,15 +45,7 @@ export function registerAppLifecycle(): void {
|
||||
await migrateLegacyDesktopBranding();
|
||||
await synchronizeAutoStartSetting();
|
||||
initializeDesktopUpdater();
|
||||
startPerfDiagnostics();
|
||||
await createWindow();
|
||||
|
||||
const mainWindow = getMainWindow();
|
||||
|
||||
if (mainWindow) {
|
||||
attachRendererDiagnosticsHooks(mainWindow);
|
||||
}
|
||||
|
||||
startLocalApiAfterWindowReady();
|
||||
startIdleMonitor();
|
||||
|
||||
@@ -83,7 +67,6 @@ export function registerAppLifecycle(): void {
|
||||
|
||||
app.on('before-quit', async (event) => {
|
||||
prepareWindowForAppQuit();
|
||||
await shutdownPerfDiagnostics();
|
||||
|
||||
if (getDataSource()?.isInitialized) {
|
||||
event.preventDefault();
|
||||
|
||||
@@ -20,8 +20,6 @@ export async function handleSaveMessage(command: SaveMessageCommand, dataSource:
|
||||
content: message.content,
|
||||
timestamp: message.timestamp,
|
||||
editedAt: message.editedAt ?? null,
|
||||
revision: message.revision ?? 0,
|
||||
headHash: message.headHash ?? null,
|
||||
isDeleted: message.isDeleted ? 1 : 0,
|
||||
replyToId: message.replyToId ?? null,
|
||||
linkMetadata: message.linkMetadata ? JSON.stringify(message.linkMetadata) : null
|
||||
|
||||
@@ -36,8 +36,7 @@ export async function handleUpdateMessage(command: UpdateMessageCommand, dataSou
|
||||
const nullableFields = [
|
||||
'channelId',
|
||||
'editedAt',
|
||||
'replyToId',
|
||||
'headHash'
|
||||
'replyToId'
|
||||
] as const;
|
||||
|
||||
for (const field of nullableFields) {
|
||||
@@ -45,13 +44,8 @@ export async function handleUpdateMessage(command: UpdateMessageCommand, dataSou
|
||||
entity[field] = updates[field] ?? null;
|
||||
}
|
||||
|
||||
if (updates.revision !== undefined) {
|
||||
existing.revision = updates.revision;
|
||||
}
|
||||
|
||||
if (updates.isDeleted !== undefined) {
|
||||
if (updates.isDeleted !== undefined)
|
||||
existing.isDeleted = updates.isDeleted ? 1 : 0;
|
||||
}
|
||||
|
||||
if (updates.linkMetadata !== undefined)
|
||||
existing.linkMetadata = updates.linkMetadata ? JSON.stringify(updates.linkMetadata) : null;
|
||||
|
||||
@@ -34,8 +34,6 @@ export function rowToMessage(row: MessageEntity, reactions: ReactionPayload[] =
|
||||
content: isDeleted ? DELETED_MESSAGE_CONTENT : row.content,
|
||||
timestamp: row.timestamp,
|
||||
editedAt: row.editedAt ?? undefined,
|
||||
revision: row.revision ?? 0,
|
||||
headHash: row.headHash ?? undefined,
|
||||
reactions: isDeleted ? [] : reactions,
|
||||
isDeleted,
|
||||
replyToId: row.replyToId ?? undefined,
|
||||
|
||||
@@ -57,8 +57,6 @@ export interface MessagePayload {
|
||||
content: string;
|
||||
timestamp: number;
|
||||
editedAt?: number;
|
||||
revision?: number;
|
||||
headHash?: string;
|
||||
reactions?: ReactionPayload[];
|
||||
isDeleted?: boolean;
|
||||
replyToId?: string;
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
expect
|
||||
} from 'vitest';
|
||||
import { isPerfDiagEnabled } from './diagnostics.flags';
|
||||
|
||||
describe('isPerfDiagEnabled', () => {
|
||||
it('returns false when the flag is unset', () => {
|
||||
expect(isPerfDiagEnabled({}, false)).toBe(false);
|
||||
expect(isPerfDiagEnabled({}, true)).toBe(false);
|
||||
});
|
||||
|
||||
it('returns true in development when METOYOU_PERF_DIAG is truthy', () => {
|
||||
expect(isPerfDiagEnabled({ METOYOU_PERF_DIAG: '1' }, false)).toBe(true);
|
||||
expect(isPerfDiagEnabled({ METOYOU_PERF_DIAG: 'true' }, false)).toBe(true);
|
||||
expect(isPerfDiagEnabled({ METOYOU_PERF_DIAG: 'on' }, false)).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false in packaged builds unless force is set', () => {
|
||||
expect(isPerfDiagEnabled({ METOYOU_PERF_DIAG: '1' }, true)).toBe(false);
|
||||
expect(isPerfDiagEnabled({
|
||||
METOYOU_PERF_DIAG: '1',
|
||||
METOYOU_PERF_DIAG_FORCE: '1'
|
||||
}, true)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,29 +0,0 @@
|
||||
export const PERF_DIAG_ENV = 'METOYOU_PERF_DIAG';
|
||||
export const PERF_DIAG_FORCE_ENV = 'METOYOU_PERF_DIAG_FORCE';
|
||||
|
||||
const TRUTHY = new Set([
|
||||
'1',
|
||||
'true',
|
||||
'yes',
|
||||
'on'
|
||||
]);
|
||||
|
||||
function isTruthyFlag(value: string | undefined): boolean {
|
||||
return TRUTHY.has(String(value ?? '').trim()
|
||||
.toLowerCase());
|
||||
}
|
||||
|
||||
export function isPerfDiagEnabled(
|
||||
env: NodeJS.ProcessEnv,
|
||||
isPackaged: boolean
|
||||
): boolean {
|
||||
if (!isTruthyFlag(env[PERF_DIAG_ENV])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isPackaged && !isTruthyFlag(env[PERF_DIAG_FORCE_ENV])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1,214 +0,0 @@
|
||||
import {
|
||||
app,
|
||||
BrowserWindow,
|
||||
ipcMain
|
||||
} from 'electron';
|
||||
import { collectAppMetricsSnapshot } from '../app-metrics';
|
||||
import { sumWorkingSetKb } from './process-metrics.rules';
|
||||
import { isPerfDiagEnabled } from './diagnostics.flags';
|
||||
import type { PerfDiagEntry } from './diagnostics.models';
|
||||
import { PerfDiagWriter } from './diagnostics.writer';
|
||||
|
||||
const PROCESS_POLL_INTERVAL_MS = 5_000;
|
||||
|
||||
let activeWriter: PerfDiagWriter | null = null;
|
||||
let processPollTimer: NodeJS.Timeout | null = null;
|
||||
let diagnosticsEnabled = false;
|
||||
let ipcRegistered = false;
|
||||
|
||||
export function isPerfDiagActive(): boolean {
|
||||
return diagnosticsEnabled;
|
||||
}
|
||||
|
||||
export function ensurePerfDiagIpcRegistered(): void {
|
||||
if (ipcRegistered) {
|
||||
return;
|
||||
}
|
||||
|
||||
ipcRegistered = true;
|
||||
|
||||
ipcMain.handle('perf-diag-is-enabled', () => diagnosticsEnabled);
|
||||
|
||||
ipcMain.handle('perf-diag-report', (_event, entry: PerfDiagEntry) => {
|
||||
const writer = activeWriter;
|
||||
|
||||
if (!diagnosticsEnabled || !writer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
writer.append(normalizeRendererEntry(entry));
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function getActivePerfDiagWriter(): PerfDiagWriter | null {
|
||||
return activeWriter;
|
||||
}
|
||||
|
||||
export function startPerfDiagnostics(): PerfDiagWriter | null {
|
||||
ensurePerfDiagIpcRegistered();
|
||||
diagnosticsEnabled = isPerfDiagEnabled(process.env, app.isPackaged);
|
||||
|
||||
if (!diagnosticsEnabled) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const sessionId = `${Date.now().toString(36)}-${process.pid}`;
|
||||
const writer = new PerfDiagWriter({
|
||||
userDataPath: app.getPath('userData'),
|
||||
sessionId
|
||||
});
|
||||
|
||||
activeWriter = writer;
|
||||
registerProcessCrashHandlers(writer);
|
||||
startProcessMetricsPolling(writer);
|
||||
|
||||
writer.append({
|
||||
collectedAt: Date.now(),
|
||||
source: 'main',
|
||||
type: 'session',
|
||||
payload: {
|
||||
event: 'started',
|
||||
sessionId,
|
||||
filePath: writer.snapshotFilePath
|
||||
}
|
||||
});
|
||||
|
||||
return writer;
|
||||
}
|
||||
|
||||
export function attachRendererDiagnosticsHooks(window: BrowserWindow): void {
|
||||
const writer = activeWriter;
|
||||
|
||||
if (!writer) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.webContents.on('render-process-gone', (_event, details) => {
|
||||
writer.append({
|
||||
collectedAt: Date.now(),
|
||||
source: 'main',
|
||||
type: 'crash',
|
||||
payload: {
|
||||
reason: details.reason,
|
||||
exitCode: details.exitCode
|
||||
}
|
||||
});
|
||||
|
||||
void writer.flushSnapshot('render-process-gone');
|
||||
});
|
||||
|
||||
window.webContents.on('unresponsive', () => {
|
||||
writer.append({
|
||||
collectedAt: Date.now(),
|
||||
source: 'main',
|
||||
type: 'unresponsive',
|
||||
payload: {}
|
||||
});
|
||||
});
|
||||
|
||||
window.webContents.on('responsive', () => {
|
||||
writer.append({
|
||||
collectedAt: Date.now(),
|
||||
source: 'main',
|
||||
type: 'session',
|
||||
payload: { event: 'renderer-responsive' }
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function shutdownPerfDiagnostics(): Promise<void> {
|
||||
if (!activeWriter) {
|
||||
return;
|
||||
}
|
||||
|
||||
await activeWriter.flushSnapshot('shutdown');
|
||||
|
||||
if (processPollTimer) {
|
||||
clearInterval(processPollTimer);
|
||||
processPollTimer = null;
|
||||
}
|
||||
|
||||
activeWriter = null;
|
||||
diagnosticsEnabled = false;
|
||||
}
|
||||
|
||||
function registerProcessCrashHandlers(writer: PerfDiagWriter): void {
|
||||
app.on('child-process-gone', (_event, details) => {
|
||||
writer.append({
|
||||
collectedAt: Date.now(),
|
||||
source: 'main',
|
||||
type: 'crash',
|
||||
payload: {
|
||||
type: details.type,
|
||||
reason: details.reason,
|
||||
exitCode: details.exitCode,
|
||||
serviceName: details.serviceName ?? null,
|
||||
name: details.name ?? null
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
process.on('uncaughtException', (error) => {
|
||||
writer.append({
|
||||
collectedAt: Date.now(),
|
||||
source: 'main',
|
||||
type: 'crash',
|
||||
payload: {
|
||||
scope: 'main-uncaughtException',
|
||||
message: error.message
|
||||
}
|
||||
});
|
||||
|
||||
void writer.flushSnapshot('uncaughtException');
|
||||
});
|
||||
|
||||
process.on('unhandledRejection', (reason) => {
|
||||
writer.append({
|
||||
collectedAt: Date.now(),
|
||||
source: 'main',
|
||||
type: 'crash',
|
||||
payload: {
|
||||
scope: 'main-unhandledRejection',
|
||||
reason: String(reason)
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function startProcessMetricsPolling(writer: PerfDiagWriter): void {
|
||||
const sample = (): void => {
|
||||
try {
|
||||
const metrics = collectAppMetricsSnapshot();
|
||||
const totalKb = sumWorkingSetKb(metrics.processes);
|
||||
|
||||
writer.append({
|
||||
collectedAt: metrics.collectedAt,
|
||||
source: 'main',
|
||||
type: 'process',
|
||||
payload: {
|
||||
totalWorkingSetKb: totalKb,
|
||||
processes: metrics.processes
|
||||
}
|
||||
});
|
||||
} catch {
|
||||
// Collector failures must never affect the app.
|
||||
}
|
||||
};
|
||||
|
||||
sample();
|
||||
processPollTimer = setInterval(sample, PROCESS_POLL_INTERVAL_MS);
|
||||
}
|
||||
|
||||
function normalizeRendererEntry(entry: PerfDiagEntry): PerfDiagEntry {
|
||||
return {
|
||||
collectedAt: Number(entry.collectedAt) || Date.now(),
|
||||
source: 'renderer',
|
||||
type: entry.type,
|
||||
payload: entry.payload ?? {}
|
||||
};
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
export type PerfDiagSource = 'main' | 'renderer';
|
||||
|
||||
export type PerfDiagEntryType =
|
||||
| 'session'
|
||||
| 'process'
|
||||
| 'store'
|
||||
| 'components'
|
||||
| 'heap'
|
||||
| 'crash'
|
||||
| 'unresponsive';
|
||||
|
||||
export interface PerfDiagEntry {
|
||||
collectedAt: number;
|
||||
source: PerfDiagSource;
|
||||
type: PerfDiagEntryType;
|
||||
payload: Record<string, unknown>;
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
expect
|
||||
} from 'vitest';
|
||||
import {
|
||||
formatPerfDiagLine,
|
||||
pushRingBuffer,
|
||||
resolveDiagnosticsFilePath
|
||||
} from './diagnostics.rules';
|
||||
|
||||
describe('pushRingBuffer', () => {
|
||||
it('appends items until capacity is reached', () => {
|
||||
expect(pushRingBuffer([1, 2], 3, 4)).toEqual([
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]);
|
||||
});
|
||||
|
||||
it('drops the oldest items when capacity is exceeded', () => {
|
||||
expect(pushRingBuffer([
|
||||
1,
|
||||
2,
|
||||
3
|
||||
], 4, 3)).toEqual([
|
||||
2,
|
||||
3,
|
||||
4
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatPerfDiagLine', () => {
|
||||
it('serializes one JSON object per line', () => {
|
||||
const line = formatPerfDiagLine({
|
||||
collectedAt: 1_700_000_000_000,
|
||||
source: 'main',
|
||||
type: 'process',
|
||||
payload: { browserKb: 128 }
|
||||
});
|
||||
|
||||
expect(line).toBe('{"collectedAt":1700000000000,"source":"main","type":"process","payload":{"browserKb":128}}');
|
||||
expect(line.endsWith('\n')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('resolveDiagnosticsFilePath', () => {
|
||||
it('places session files under diagnostics/', () => {
|
||||
expect(resolveDiagnosticsFilePath('/tmp/user-data', 'session-1'))
|
||||
.toBe('/tmp/user-data/diagnostics/perf-session-1.jsonl');
|
||||
});
|
||||
});
|
||||
@@ -1,24 +0,0 @@
|
||||
import * as path from 'path';
|
||||
import type { PerfDiagEntry } from './diagnostics.models';
|
||||
|
||||
export function pushRingBuffer<T>(items: readonly T[], item: T, capacity: number): T[] {
|
||||
const next = [...items, item];
|
||||
|
||||
if (next.length <= capacity) {
|
||||
return next;
|
||||
}
|
||||
|
||||
return next.slice(next.length - capacity);
|
||||
}
|
||||
|
||||
export function formatPerfDiagLine(entry: PerfDiagEntry): string {
|
||||
return JSON.stringify(entry);
|
||||
}
|
||||
|
||||
export function resolveDiagnosticsFilePath(userDataPath: string, sessionId: string): string {
|
||||
return path.join(userDataPath, 'diagnostics', `perf-${sessionId}.jsonl`);
|
||||
}
|
||||
|
||||
export function resolveDiagnosticsDirectory(userDataPath: string): string {
|
||||
return path.join(userDataPath, 'diagnostics');
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
import * as fsp from 'fs/promises';
|
||||
import * as path from 'path';
|
||||
import type { PerfDiagEntry } from './diagnostics.models';
|
||||
import {
|
||||
formatPerfDiagLine,
|
||||
pushRingBuffer,
|
||||
resolveDiagnosticsFilePath
|
||||
} from './diagnostics.rules';
|
||||
|
||||
const DEFAULT_RING_CAPACITY = 120;
|
||||
const FLUSH_DEBOUNCE_MS = 250;
|
||||
|
||||
export interface PerfDiagWriterOptions {
|
||||
userDataPath: string;
|
||||
sessionId: string;
|
||||
ringCapacity?: number;
|
||||
}
|
||||
|
||||
export class PerfDiagWriter {
|
||||
private readonly filePath: string;
|
||||
private readonly ringCapacity: number;
|
||||
private readonly pendingLines: string[] = [];
|
||||
private ring: PerfDiagEntry[] = [];
|
||||
private flushTimer: NodeJS.Timeout | null = null;
|
||||
private flushInFlight: Promise<void> | null = null;
|
||||
private disabled = false;
|
||||
|
||||
constructor(options: PerfDiagWriterOptions) {
|
||||
this.filePath = resolveDiagnosticsFilePath(options.userDataPath, options.sessionId);
|
||||
this.ringCapacity = options.ringCapacity ?? DEFAULT_RING_CAPACITY;
|
||||
}
|
||||
|
||||
get snapshotFilePath(): string {
|
||||
return this.filePath;
|
||||
}
|
||||
|
||||
get bufferedEntries(): readonly PerfDiagEntry[] {
|
||||
return this.ring;
|
||||
}
|
||||
|
||||
append(entry: PerfDiagEntry): void {
|
||||
if (this.disabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this.ring = pushRingBuffer(this.ring, entry, this.ringCapacity);
|
||||
this.pendingLines.push(`${formatPerfDiagLine(entry)}\n`);
|
||||
this.scheduleFlush();
|
||||
} catch {
|
||||
this.disabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
async flush(): Promise<void> {
|
||||
if (this.disabled || this.pendingLines.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.flushInFlight) {
|
||||
await this.flushInFlight;
|
||||
return;
|
||||
}
|
||||
|
||||
const lines = this.pendingLines.splice(0, this.pendingLines.length);
|
||||
|
||||
this.flushInFlight = this.writeLines(lines)
|
||||
.catch(() => {
|
||||
this.disabled = true;
|
||||
})
|
||||
.finally(() => {
|
||||
this.flushInFlight = null;
|
||||
});
|
||||
|
||||
await this.flushInFlight;
|
||||
}
|
||||
|
||||
async flushSnapshot(label: string): Promise<void> {
|
||||
this.append({
|
||||
collectedAt: Date.now(),
|
||||
source: 'main',
|
||||
type: 'session',
|
||||
payload: {
|
||||
event: label,
|
||||
filePath: this.filePath,
|
||||
entries: this.ring
|
||||
}
|
||||
});
|
||||
|
||||
await this.flush();
|
||||
}
|
||||
|
||||
private scheduleFlush(): void {
|
||||
if (this.flushTimer) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.flushTimer = setTimeout(() => {
|
||||
this.flushTimer = null;
|
||||
void this.flush();
|
||||
}, FLUSH_DEBOUNCE_MS);
|
||||
}
|
||||
|
||||
private async writeLines(lines: string[]): Promise<void> {
|
||||
await fsp.mkdir(path.dirname(this.filePath), { recursive: true });
|
||||
await fsp.appendFile(this.filePath, lines.join(''), 'utf8');
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
export { isPerfDiagEnabled, PERF_DIAG_ENV, PERF_DIAG_FORCE_ENV } from './diagnostics.flags';
|
||||
export {
|
||||
attachRendererDiagnosticsHooks,
|
||||
ensurePerfDiagIpcRegistered,
|
||||
getActivePerfDiagWriter,
|
||||
isPerfDiagActive,
|
||||
shutdownPerfDiagnostics,
|
||||
startPerfDiagnostics
|
||||
} from './diagnostics.lifecycle';
|
||||
export type { PerfDiagEntry, PerfDiagEntryType, PerfDiagSource } from './diagnostics.models';
|
||||
export { PerfDiagWriter } from './diagnostics.writer';
|
||||
@@ -1,19 +0,0 @@
|
||||
export interface ProcessWorkingSetSnapshot {
|
||||
workingSetKb: number | null;
|
||||
}
|
||||
|
||||
export function sumWorkingSetKb(processes: readonly ProcessWorkingSetSnapshot[]): number | null {
|
||||
let total = 0;
|
||||
let hasAny = false;
|
||||
|
||||
for (const process of processes) {
|
||||
if (process.workingSetKb == null || process.workingSetKb < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
total += process.workingSetKb;
|
||||
hasAny = true;
|
||||
}
|
||||
|
||||
return hasAny ? total : null;
|
||||
}
|
||||
@@ -41,10 +41,4 @@ export class MessageEntity {
|
||||
|
||||
@Column('text', { nullable: true })
|
||||
linkMetadata!: string | null;
|
||||
|
||||
@Column('integer', { default: 0 })
|
||||
revision!: number;
|
||||
|
||||
@Column('text', { nullable: true })
|
||||
headHash!: string | null;
|
||||
}
|
||||
|
||||
@@ -7,25 +7,14 @@ import {
|
||||
afterEach
|
||||
} from 'vitest';
|
||||
|
||||
const {
|
||||
mockGetSystemIdleTime,
|
||||
mockSend,
|
||||
mockGetMainWindow
|
||||
} = vi.hoisted(() => {
|
||||
const send = vi.fn();
|
||||
const getSystemIdleTime = vi.fn(() => 0);
|
||||
const getMainWindow = vi.fn(() => ({
|
||||
// Mock Electron modules before importing the module under test
|
||||
const mockGetSystemIdleTime = vi.fn(() => 0);
|
||||
const mockSend = vi.fn();
|
||||
const mockGetMainWindow = vi.fn(() => ({
|
||||
isDestroyed: () => false,
|
||||
webContents: { send }
|
||||
webContents: { send: mockSend }
|
||||
}));
|
||||
|
||||
return {
|
||||
mockGetSystemIdleTime: getSystemIdleTime,
|
||||
mockSend: send,
|
||||
mockGetMainWindow: getMainWindow
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock('electron', () => ({
|
||||
powerMonitor: {
|
||||
getSystemIdleTime: mockGetSystemIdleTime
|
||||
|
||||
@@ -20,7 +20,6 @@ import {
|
||||
type DesktopSettings
|
||||
} from '../desktop-settings';
|
||||
import { applyLocalApiSettings, getLocalApiSnapshot } from '../api';
|
||||
import { getProvisionSecret, storeProvisionSecret } from '../api/provision-secret-store';
|
||||
import {
|
||||
activateLinuxScreenShareAudioRouting,
|
||||
deactivateLinuxScreenShareAudioRouting,
|
||||
@@ -62,12 +61,6 @@ import {
|
||||
import { listRunningProcessNames } from '../process-list';
|
||||
import { detectActiveGame } from '../game-detection';
|
||||
import { collectAppMetricsSnapshot } from '../app-metrics';
|
||||
import { clearAllTokens } from '../api/auth-store';
|
||||
import {
|
||||
assertPathUnderUserData,
|
||||
grantPluginReadRoot,
|
||||
resolveReadablePath
|
||||
} from '../path-jail';
|
||||
|
||||
const DEFAULT_MIME_TYPE = 'application/octet-stream';
|
||||
const MAX_ACTIVE_DESKTOP_NOTIFICATIONS = 20;
|
||||
@@ -79,19 +72,6 @@ const FILE_CLIPBOARD_FORMATS = [
|
||||
'public.file-url',
|
||||
'FileNameW'
|
||||
] as const;
|
||||
|
||||
async function resolveUserDataFilePath(filePath: string): Promise<string | null> {
|
||||
return await resolveReadablePath(filePath);
|
||||
}
|
||||
|
||||
async function resolveWritableUserDataFilePath(filePath: string): Promise<string | null> {
|
||||
try {
|
||||
return await assertPathUnderUserData(filePath);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const MIME_TYPES_BY_EXTENSION: Record<string, string> = {
|
||||
'.7z': 'application/x-7z-compressed',
|
||||
'.aac': 'audio/aac',
|
||||
@@ -385,14 +365,6 @@ export function setupSystemHandlers(): void {
|
||||
|
||||
ipcMain.handle('get-app-metrics', () => collectAppMetricsSnapshot());
|
||||
|
||||
ipcMain.handle('store-provision-secret', async (_event, homeUserId: string, secret: string) =>
|
||||
await storeProvisionSecret(homeUserId, secret)
|
||||
);
|
||||
|
||||
ipcMain.handle('get-provision-secret', async (_event, homeUserId: string) =>
|
||||
await getProvisionSecret(homeUserId)
|
||||
);
|
||||
|
||||
ipcMain.handle('get-app-data-path', () => app.getPath('userData'));
|
||||
ipcMain.handle('open-current-data-folder', async () => await openCurrentDataFolder());
|
||||
ipcMain.handle('export-user-data', async () => await exportUserData());
|
||||
@@ -524,10 +496,6 @@ export function setupSystemHandlers(): void {
|
||||
ipcMain.handle('set-desktop-settings', async (_event, patch: Partial<DesktopSettings>) => {
|
||||
const snapshot = updateDesktopSettings(patch);
|
||||
|
||||
if (Object.prototype.hasOwnProperty.call(patch, 'allowedSignalingServers')) {
|
||||
clearAllTokens();
|
||||
}
|
||||
|
||||
await synchronizeAutoStartSetting(snapshot.autoStart);
|
||||
updateCloseToTraySetting(snapshot.closeToTray);
|
||||
await handleDesktopSettingsChanged();
|
||||
@@ -597,12 +565,6 @@ export function setupSystemHandlers(): void {
|
||||
return false;
|
||||
}
|
||||
|
||||
const scopedDestination = await resolveWritableUserDataFilePath(destinationFilePath);
|
||||
|
||||
if (!scopedDestination) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const stats = await fsp.stat(sourceFilePath);
|
||||
|
||||
@@ -610,7 +572,7 @@ export function setupSystemHandlers(): void {
|
||||
return false;
|
||||
}
|
||||
|
||||
await fsp.copyFile(sourceFilePath, scopedDestination);
|
||||
await fsp.copyFile(sourceFilePath, destinationFilePath);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
@@ -618,14 +580,8 @@ export function setupSystemHandlers(): void {
|
||||
});
|
||||
|
||||
ipcMain.handle('file-exists', async (_event, filePath: string) => {
|
||||
const scopedPath = await resolveUserDataFilePath(filePath);
|
||||
|
||||
if (!scopedPath) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
await fsp.access(scopedPath, fs.constants.F_OK);
|
||||
await fsp.access(filePath, fs.constants.F_OK);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
@@ -633,40 +589,26 @@ export function setupSystemHandlers(): void {
|
||||
});
|
||||
|
||||
ipcMain.handle('get-file-url', async (_event, filePath: string) => {
|
||||
const scopedPath = await resolveUserDataFilePath(filePath);
|
||||
|
||||
if (!scopedPath) {
|
||||
if (typeof filePath !== 'string' || !filePath.trim()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
await fsp.access(scopedPath, fs.constants.F_OK);
|
||||
return pathToFileURL(scopedPath).toString();
|
||||
await fsp.access(filePath, fs.constants.F_OK);
|
||||
return pathToFileURL(filePath).toString();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('read-file', async (_event, filePath: string) => {
|
||||
const scopedPath = await resolveUserDataFilePath(filePath);
|
||||
|
||||
if (!scopedPath) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const data = await fsp.readFile(scopedPath);
|
||||
const data = await fsp.readFile(filePath);
|
||||
|
||||
return data.toString('base64');
|
||||
});
|
||||
|
||||
ipcMain.handle('read-file-chunk', async (_event, filePath: string, start: number, end: number) => {
|
||||
const scopedPath = await resolveUserDataFilePath(filePath);
|
||||
|
||||
if (!scopedPath) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const fileHandle = await fsp.open(scopedPath, 'r');
|
||||
const fileHandle = await fsp.open(filePath, 'r');
|
||||
|
||||
try {
|
||||
const safeStart = Math.max(0, Math.trunc(start));
|
||||
@@ -681,13 +623,7 @@ export function setupSystemHandlers(): void {
|
||||
});
|
||||
|
||||
ipcMain.handle('get-file-size', async (_event, filePath: string) => {
|
||||
const scopedPath = await resolveUserDataFilePath(filePath);
|
||||
|
||||
if (!scopedPath) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const stats = await fsp.stat(scopedPath);
|
||||
const stats = await fsp.stat(filePath);
|
||||
|
||||
return stats.size;
|
||||
});
|
||||
@@ -696,47 +632,23 @@ export function setupSystemHandlers(): void {
|
||||
return await readClipboardFiles();
|
||||
});
|
||||
|
||||
ipcMain.handle('grant-plugin-read-root', (_event, rootPath: string) => {
|
||||
grantPluginReadRoot(rootPath);
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
ipcMain.handle('write-file', async (_event, filePath: string, base64Data: string) => {
|
||||
const scopedPath = await resolveWritableUserDataFilePath(filePath);
|
||||
|
||||
if (!scopedPath) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const buffer = Buffer.from(base64Data, 'base64');
|
||||
|
||||
await fsp.writeFile(scopedPath, buffer);
|
||||
await fsp.writeFile(filePath, buffer);
|
||||
return true;
|
||||
});
|
||||
|
||||
ipcMain.handle('append-file', async (_event, filePath: string, base64Data: string) => {
|
||||
const scopedPath = await resolveWritableUserDataFilePath(filePath);
|
||||
|
||||
if (!scopedPath) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const buffer = Buffer.from(base64Data, 'base64');
|
||||
|
||||
await fsp.appendFile(scopedPath, buffer);
|
||||
await fsp.appendFile(filePath, buffer);
|
||||
return true;
|
||||
});
|
||||
|
||||
ipcMain.handle('delete-file', async (_event, filePath: string) => {
|
||||
const scopedPath = await resolveWritableUserDataFilePath(filePath);
|
||||
|
||||
if (!scopedPath) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
await fsp.unlink(scopedPath);
|
||||
await fsp.unlink(filePath);
|
||||
return true;
|
||||
} catch (error) {
|
||||
if ((error as { code?: string }).code === 'ENOENT') {
|
||||
@@ -771,14 +683,7 @@ export function setupSystemHandlers(): void {
|
||||
cancelled: false };
|
||||
}
|
||||
|
||||
const scopedSourcePath = await resolveUserDataFilePath(sourceFilePath);
|
||||
|
||||
if (!scopedSourcePath) {
|
||||
return { saved: false,
|
||||
cancelled: false };
|
||||
}
|
||||
|
||||
const stats = await fsp.stat(scopedSourcePath);
|
||||
const stats = await fsp.stat(sourceFilePath);
|
||||
|
||||
if (!stats.isFile()) {
|
||||
return { saved: false,
|
||||
@@ -786,7 +691,7 @@ export function setupSystemHandlers(): void {
|
||||
}
|
||||
|
||||
const result = await dialog.showSaveDialog({
|
||||
defaultPath: defaultFileName || path.basename(scopedSourcePath)
|
||||
defaultPath: defaultFileName || path.basename(sourceFilePath)
|
||||
});
|
||||
|
||||
if (result.canceled || !result.filePath) {
|
||||
@@ -794,7 +699,7 @@ export function setupSystemHandlers(): void {
|
||||
cancelled: true };
|
||||
}
|
||||
|
||||
await fsp.copyFile(scopedSourcePath, result.filePath);
|
||||
await fsp.copyFile(sourceFilePath, result.filePath);
|
||||
|
||||
return { saved: true,
|
||||
cancelled: false };
|
||||
@@ -806,22 +711,15 @@ export function setupSystemHandlers(): void {
|
||||
reason: 'missing-path' };
|
||||
}
|
||||
|
||||
const scopedPath = await resolveUserDataFilePath(filePath);
|
||||
|
||||
if (!scopedPath) {
|
||||
return { opened: false,
|
||||
reason: 'outside-app-data' };
|
||||
}
|
||||
|
||||
try {
|
||||
const stats = await fsp.stat(scopedPath);
|
||||
const stats = await fsp.stat(filePath);
|
||||
|
||||
if (!stats.isFile()) {
|
||||
return { opened: false,
|
||||
reason: 'not-a-file' };
|
||||
}
|
||||
|
||||
const error = await shell.openPath(scopedPath);
|
||||
const error = await shell.openPath(filePath);
|
||||
|
||||
return error
|
||||
? { opened: false,
|
||||
@@ -834,13 +732,7 @@ export function setupSystemHandlers(): void {
|
||||
});
|
||||
|
||||
ipcMain.handle('ensure-dir', async (_event, dirPath: string) => {
|
||||
const scopedPath = await resolveWritableUserDataFilePath(dirPath);
|
||||
|
||||
if (!scopedPath) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await fsp.mkdir(scopedPath, { recursive: true });
|
||||
await fsp.mkdir(dirPath, { recursive: true });
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class MessageIntegrity1000000000013 implements MigrationInterface {
|
||||
name = 'MessageIntegrity1000000000013';
|
||||
|
||||
async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query('ALTER TABLE "messages" ADD COLUMN "revision" integer NOT NULL DEFAULT 0');
|
||||
await queryRunner.query('ALTER TABLE "messages" ADD COLUMN "headHash" text');
|
||||
}
|
||||
|
||||
async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query('ALTER TABLE "messages" DROP COLUMN "headHash"');
|
||||
await queryRunner.query('ALTER TABLE "messages" DROP COLUMN "revision"');
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
expect,
|
||||
beforeEach,
|
||||
afterEach
|
||||
} from 'vitest';
|
||||
import * as fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
import {
|
||||
assertPathUnderRoot,
|
||||
clearGrantedPluginReadRoots,
|
||||
grantPluginReadRoot,
|
||||
resolveReadablePath
|
||||
} from './path-jail';
|
||||
|
||||
describe('path-jail', () => {
|
||||
let tempRoot = '';
|
||||
|
||||
beforeEach(() => {
|
||||
tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'metoyou-path-jail-'));
|
||||
fs.mkdirSync(path.join(tempRoot, 'server', 'room-1'), { recursive: true });
|
||||
fs.writeFileSync(path.join(tempRoot, 'server', 'room-1', 'file.txt'), 'ok');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
clearGrantedPluginReadRoots();
|
||||
fs.rmSync(tempRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('accepts paths inside allowed subdirectories', async () => {
|
||||
const allowedPath = path.join(tempRoot, 'server', 'room-1', 'file.txt');
|
||||
|
||||
await expect(assertPathUnderRoot(tempRoot, allowedPath, ['server'])).resolves.toBe(allowedPath);
|
||||
});
|
||||
|
||||
it('accepts cached plugin bundle paths under plugin-bundles', async () => {
|
||||
const bundleDir = path.join(tempRoot, 'plugin-bundles', 'example.plugin', '1.0.0');
|
||||
|
||||
fs.mkdirSync(bundleDir, { recursive: true });
|
||||
const bundlePath = path.join(bundleDir, 'main.js');
|
||||
|
||||
fs.writeFileSync(bundlePath, 'export default {}');
|
||||
|
||||
await expect(assertPathUnderRoot(tempRoot, bundlePath)).resolves.toBe(bundlePath);
|
||||
});
|
||||
|
||||
it('rejects paths outside the user data root', async () => {
|
||||
const outsidePath = path.join(os.tmpdir(), 'outside.txt');
|
||||
|
||||
await expect(assertPathUnderRoot(tempRoot, outsidePath, ['server'])).rejects.toThrow('outside allowed app-data paths');
|
||||
});
|
||||
|
||||
it('rejects paths outside allowed subdirectories', async () => {
|
||||
const pluginsPath = path.join(tempRoot, 'plugins', 'evil.txt');
|
||||
|
||||
await expect(assertPathUnderRoot(tempRoot, pluginsPath, ['server'])).rejects.toThrow('outside allowed app-data paths');
|
||||
});
|
||||
|
||||
it('allows user-granted plugin source roots outside app data', async () => {
|
||||
const externalRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'metoyou-plugin-source-'));
|
||||
const manifestPath = path.join(externalRoot, 'plugin-source.json');
|
||||
|
||||
fs.writeFileSync(manifestPath, '{}');
|
||||
|
||||
grantPluginReadRoot(externalRoot);
|
||||
|
||||
await expect(resolveReadablePath(manifestPath)).resolves.toBe(manifestPath);
|
||||
|
||||
fs.rmSync(externalRoot, { recursive: true, force: true });
|
||||
});
|
||||
});
|
||||
@@ -1,108 +0,0 @@
|
||||
import { app } from 'electron';
|
||||
import * as fsp from 'fs/promises';
|
||||
import * as path from 'path';
|
||||
|
||||
export const DEFAULT_USER_DATA_SUBDIRS = [
|
||||
'server',
|
||||
'direct-messages',
|
||||
'plugins',
|
||||
'plugin-bundles',
|
||||
'plugin-cache',
|
||||
'themes',
|
||||
'metoyou'
|
||||
] as const;
|
||||
|
||||
export function isPathInside(parentPath: string, candidatePath: string): boolean {
|
||||
const relativePath = path.relative(parentPath, candidatePath);
|
||||
|
||||
return relativePath === '' || (!relativePath.startsWith('..') && !path.isAbsolute(relativePath));
|
||||
}
|
||||
|
||||
async function realpathOrSelf(filePath: string): Promise<string> {
|
||||
try {
|
||||
return await fsp.realpath(filePath);
|
||||
} catch {
|
||||
return path.resolve(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeAllowedSubdirs(allowedSubdirs: readonly string[]): string[] {
|
||||
return allowedSubdirs.map((entry) => entry.replace(/\\/g, '/').replace(/^\/+|\/+$/g, '')).filter(Boolean);
|
||||
}
|
||||
|
||||
export async function assertPathUnderRoot(
|
||||
rootPath: string,
|
||||
candidatePath: string,
|
||||
allowedSubdirs: readonly string[] = DEFAULT_USER_DATA_SUBDIRS
|
||||
): Promise<string> {
|
||||
if (typeof candidatePath !== 'string' || !candidatePath.trim()) {
|
||||
throw new Error('Invalid file path');
|
||||
}
|
||||
|
||||
const [realRoot, realCandidate] = await Promise.all([realpathOrSelf(rootPath), realpathOrSelf(candidatePath)]);
|
||||
|
||||
if (!isPathInside(realRoot, realCandidate)) {
|
||||
throw new Error('Path is outside allowed app-data paths');
|
||||
}
|
||||
|
||||
const relativePath = path.relative(realRoot, realCandidate).replace(/\\/g, '/');
|
||||
const [topLevelSegment] = relativePath.split('/');
|
||||
|
||||
if (!topLevelSegment || !normalizeAllowedSubdirs(allowedSubdirs).includes(topLevelSegment)) {
|
||||
throw new Error('Path is outside allowed app-data paths');
|
||||
}
|
||||
|
||||
return realCandidate;
|
||||
}
|
||||
|
||||
export async function assertPathUnderUserData(
|
||||
candidatePath: string,
|
||||
allowedSubdirs: readonly string[] = DEFAULT_USER_DATA_SUBDIRS
|
||||
): Promise<string> {
|
||||
return assertPathUnderRoot(app.getPath('userData'), candidatePath, allowedSubdirs);
|
||||
}
|
||||
|
||||
const grantedPluginReadRoots = new Set<string>();
|
||||
|
||||
export function grantPluginReadRoot(rootPath: string): void {
|
||||
if (typeof rootPath !== 'string' || !rootPath.trim()) {
|
||||
return;
|
||||
}
|
||||
|
||||
grantedPluginReadRoots.add(path.resolve(rootPath));
|
||||
}
|
||||
|
||||
export function clearGrantedPluginReadRoots(): void {
|
||||
grantedPluginReadRoots.clear();
|
||||
}
|
||||
|
||||
async function assertPathUnderGrantedPluginRoot(candidatePath: string): Promise<string> {
|
||||
const realCandidate = await realpathOrSelf(candidatePath);
|
||||
|
||||
for (const rootPath of grantedPluginReadRoots) {
|
||||
const realRoot = await realpathOrSelf(rootPath);
|
||||
|
||||
if (isPathInside(realRoot, realCandidate)) {
|
||||
return realCandidate;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('Path is outside allowed app-data paths');
|
||||
}
|
||||
|
||||
/** Resolves readable paths under app data or user-granted plugin source roots. */
|
||||
export async function resolveReadablePath(candidatePath: string): Promise<string | null> {
|
||||
if (typeof candidatePath !== 'string' || !candidatePath.trim()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return await assertPathUnderUserData(candidatePath);
|
||||
} catch {
|
||||
try {
|
||||
return await assertPathUnderGrantedPluginRoot(candidatePath);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -252,13 +252,6 @@ export interface ElectronAPI {
|
||||
workingSetKb: number | null;
|
||||
}[];
|
||||
}>;
|
||||
isPerfDiagEnabled: () => Promise<boolean>;
|
||||
reportPerfDiagSample: (entry: {
|
||||
collectedAt: number;
|
||||
source: 'main' | 'renderer';
|
||||
type: string;
|
||||
payload: Record<string, unknown>;
|
||||
}) => Promise<boolean>;
|
||||
getAppDataPath: () => Promise<string>;
|
||||
openCurrentDataFolder: () => Promise<boolean>;
|
||||
exportUserData: () => Promise<ExportUserDataResult>;
|
||||
@@ -321,10 +314,9 @@ export interface ElectronAPI {
|
||||
relaunchApp: () => Promise<boolean>;
|
||||
onDeepLinkReceived: (listener: (url: string) => void) => () => void;
|
||||
readClipboardFiles: () => Promise<ClipboardFilePayload[]>;
|
||||
readFile: (filePath: string) => Promise<string | null>;
|
||||
readFileChunk: (filePath: string, start: number, end: number) => Promise<string | null>;
|
||||
getFileSize: (filePath: string) => Promise<number | null>;
|
||||
grantPluginReadRoot: (rootPath: string) => Promise<boolean>;
|
||||
readFile: (filePath: string) => Promise<string>;
|
||||
readFileChunk: (filePath: string, start: number, end: number) => Promise<string>;
|
||||
getFileSize: (filePath: string) => Promise<number>;
|
||||
writeFile: (filePath: string, data: string) => Promise<boolean>;
|
||||
appendFile: (filePath: string, data: string) => Promise<boolean>;
|
||||
saveFileAs: (defaultFileName: string, data: string) => Promise<{ saved: boolean; cancelled: boolean }>;
|
||||
@@ -346,9 +338,6 @@ export interface ElectronAPI {
|
||||
|
||||
command: <T = unknown>(command: Command) => Promise<T>;
|
||||
query: <T = unknown>(query: Query) => Promise<T>;
|
||||
|
||||
storeProvisionSecret: (homeUserId: string, secret: string) => Promise<boolean>;
|
||||
getProvisionSecret: (homeUserId: string) => Promise<string | null>;
|
||||
}
|
||||
|
||||
const electronAPI: ElectronAPI = {
|
||||
@@ -398,8 +387,6 @@ const electronAPI: ElectronAPI = {
|
||||
};
|
||||
},
|
||||
getAppMetrics: () => ipcRenderer.invoke('get-app-metrics'),
|
||||
isPerfDiagEnabled: () => ipcRenderer.invoke('perf-diag-is-enabled'),
|
||||
reportPerfDiagSample: (entry) => ipcRenderer.invoke('perf-diag-report', entry),
|
||||
getAppDataPath: () => ipcRenderer.invoke('get-app-data-path'),
|
||||
openCurrentDataFolder: () => ipcRenderer.invoke('open-current-data-folder'),
|
||||
exportUserData: () => ipcRenderer.invoke('export-user-data'),
|
||||
@@ -464,7 +451,6 @@ const electronAPI: ElectronAPI = {
|
||||
readFile: (filePath) => ipcRenderer.invoke('read-file', filePath),
|
||||
readFileChunk: (filePath, start, end) => ipcRenderer.invoke('read-file-chunk', filePath, start, end),
|
||||
getFileSize: (filePath) => ipcRenderer.invoke('get-file-size', filePath),
|
||||
grantPluginReadRoot: (rootPath) => ipcRenderer.invoke('grant-plugin-read-root', rootPath),
|
||||
writeFile: (filePath, data) => ipcRenderer.invoke('write-file', filePath, data),
|
||||
appendFile: (filePath, data) => ipcRenderer.invoke('append-file', filePath, data),
|
||||
saveFileAs: (defaultFileName, data) => ipcRenderer.invoke('save-file-as', defaultFileName, data),
|
||||
@@ -505,10 +491,7 @@ const electronAPI: ElectronAPI = {
|
||||
},
|
||||
|
||||
command: (command) => ipcRenderer.invoke('cqrs:command', command),
|
||||
query: (query) => ipcRenderer.invoke('cqrs:query', query),
|
||||
|
||||
storeProvisionSecret: (homeUserId, secret) => ipcRenderer.invoke('store-provision-secret', homeUserId, secret),
|
||||
getProvisionSecret: (homeUserId) => ipcRenderer.invoke('get-provision-secret', homeUserId)
|
||||
query: (query) => ipcRenderer.invoke('cqrs:query', query)
|
||||
};
|
||||
|
||||
contextBridge.exposeInMainWorld('electronAPI', electronAPI);
|
||||
|
||||
10
package.json
10
package.json
@@ -52,12 +52,10 @@
|
||||
"server:bundle:win": "node tools/package-server-executable.js --target node18-win-x64 --output metoyou-server-win-x64.exe",
|
||||
"sort:props": "node tools/sort-template-properties.js",
|
||||
"i18n:sync": "node tools/sync-app-i18n-catalog.mjs",
|
||||
"test:e2e": "node e2e/run-playwright.mjs test",
|
||||
"test:e2e:ui": "node e2e/run-playwright.mjs test --ui",
|
||||
"test:e2e:debug": "node e2e/run-playwright.mjs test --debug",
|
||||
"test:e2e:report": "node e2e/run-playwright.mjs show-report ../test-results/html-report",
|
||||
"perf:diag:view": "node tools/perf-diag-viewer.js",
|
||||
"perf:diag:tail": "node tools/perf-diag-viewer.js --tail",
|
||||
"test:e2e": "cd e2e && npx playwright test",
|
||||
"test:e2e:ui": "cd e2e && npx playwright test --ui",
|
||||
"test:e2e:debug": "cd e2e && npx playwright test --debug",
|
||||
"test:e2e:report": "cd e2e && npx playwright show-report ../test-results/html-report",
|
||||
"cap:sync": "cd toju-app && npx cap sync",
|
||||
"cap:open:android": "node tools/cap-open-android.js",
|
||||
"cap:open:ios": "cd toju-app && npx cap open ios",
|
||||
|
||||
@@ -21,9 +21,6 @@ Owns the shared, internet-reachable runtime: HTTP routes for server directory /
|
||||
| **Server directory** | The catalog of joinable chat servers, exposed by `src/routes/servers.ts` plus invite and join-request routes. | "guild list" |
|
||||
| **SSRF guard** | The outbound-fetch policy enforced by `src/routes/ssrf-guard.ts` — gates link-metadata and proxy routes that fetch user-supplied URLs. | "proxy filter" |
|
||||
| **Variables file** | `data/variables.json` — runtime config (klipy key, server host/protocol, release manifest URL, link-preview toggle) normalized on startup. | "config", ".env" (those are separate) |
|
||||
| **Session token** | Opaque bearer token issued on login/register, stored in `session_tokens`, required on mutating REST routes and WebSocket `identify`. Multiple valid tokens may exist per user (multi-device login). | "API key", "JWT" |
|
||||
| **Client instance id** | Opaque per-install string on WebSocket `identify` and `voice_state`; used to distinguish connections for the same `oderId` and to track which connection owns active voice. | "device id" |
|
||||
| **Voice-active connection** | WebSocket connection for a user with `voiceActive=true` after a connected `voice_state`; preferred target for RTC relay. | "voice owner socket" |
|
||||
|
||||
## Relationships
|
||||
|
||||
|
||||
BIN
server/data/metoyou.sqlite
Normal file
BIN
server/data/metoyou.sqlite
Normal file
Binary file not shown.
46
server/package-lock.json
generated
46
server/package-lock.json
generated
@@ -8,11 +8,9 @@
|
||||
"name": "metoyou-server",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"bcryptjs": "^3.0.2",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^17.3.1",
|
||||
"express": "^4.18.2",
|
||||
"express-rate-limit": "^8.2.1",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"sql.js": "^1.9.0",
|
||||
"typeorm": "^0.3.28",
|
||||
@@ -23,7 +21,6 @@
|
||||
"metoyou-server": "dist/index.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bcryptjs": "^2.4.6",
|
||||
"@types/cors": "^2.8.14",
|
||||
"@types/express": "^4.17.18",
|
||||
"@types/node": "^20.8.0",
|
||||
@@ -215,13 +212,6 @@
|
||||
"devOptional": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/bcryptjs": {
|
||||
"version": "2.4.6",
|
||||
"resolved": "https://registry.npmjs.org/@types/bcryptjs/-/bcryptjs-2.4.6.tgz",
|
||||
"integrity": "sha512-9xlo6R2qDs5uixm0bcIqCeMCE6HiQsIyel9KQySStiyqNl2tnj2mP3DX1Nf56MD6KMenNNlBBsy3LJ7gUEQPXQ==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/body-parser": {
|
||||
"version": "1.19.6",
|
||||
"resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.6.tgz",
|
||||
@@ -548,15 +538,6 @@
|
||||
],
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/bcryptjs": {
|
||||
"version": "3.0.3",
|
||||
"resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-3.0.3.tgz",
|
||||
"integrity": "sha512-GlF5wPWnSa/X5LKM1o0wz0suXIINz1iHRLvTS+sLyi7XPbe5ycmYI3DlZqVGZZtDgl4DmasFg7gOB3JYbphV5g==",
|
||||
"license": "BSD-3-Clause",
|
||||
"bin": {
|
||||
"bcrypt": "bin/bcrypt"
|
||||
}
|
||||
},
|
||||
"node_modules/binary-extensions": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.3.0.tgz",
|
||||
@@ -1077,24 +1058,6 @@
|
||||
"url": "https://opencollective.com/express"
|
||||
}
|
||||
},
|
||||
"node_modules/express-rate-limit": {
|
||||
"version": "8.5.2",
|
||||
"resolved": "https://registry.npmjs.org/express-rate-limit/-/express-rate-limit-8.5.2.tgz",
|
||||
"integrity": "sha512-5Kb34ipNX694DH48vN9irak1Qx30nb0PLYHXfJgw4YEjiC3ZEmZJhwOp+VfiCYwFzvFTdB9QkArYS5kXa2cx2A==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"ip-address": "^10.2.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 16"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/express-rate-limit"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"express": ">= 4.11"
|
||||
}
|
||||
},
|
||||
"node_modules/fill-range": {
|
||||
"version": "7.1.1",
|
||||
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz",
|
||||
@@ -1420,15 +1383,6 @@
|
||||
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
|
||||
"license": "ISC"
|
||||
},
|
||||
"node_modules/ip-address": {
|
||||
"version": "10.2.0",
|
||||
"resolved": "https://registry.npmjs.org/ip-address/-/ip-address-10.2.0.tgz",
|
||||
"integrity": "sha512-/+S6j4E9AHvW9SWMSEY9Xfy66O5PWvVEJ08O0y5JGyEKQpojb0K0GKpz/v5HJ/G0vi3D2sjGK78119oXZeE0qA==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 12"
|
||||
}
|
||||
},
|
||||
"node_modules/ipaddr.js": {
|
||||
"version": "1.9.1",
|
||||
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
|
||||
|
||||
@@ -11,9 +11,7 @@
|
||||
"dev": "ts-node-dev --respawn src/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"bcryptjs": "^3.0.2",
|
||||
"cors": "^2.8.5",
|
||||
"express-rate-limit": "^8.2.1",
|
||||
"dotenv": "^17.3.1",
|
||||
"express": "^4.18.2",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
@@ -23,7 +21,6 @@
|
||||
"ws": "^8.14.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bcryptjs": "^2.4.6",
|
||||
"@types/cors": "^2.8.14",
|
||||
"@types/express": "^4.17.18",
|
||||
"@types/node": "^20.8.0",
|
||||
|
||||
@@ -1,53 +1,13 @@
|
||||
import express from 'express';
|
||||
import cors from 'cors';
|
||||
import rateLimit from 'express-rate-limit';
|
||||
import { registerRoutes } from './routes';
|
||||
import { getCorsAllowlist } from './config/variables';
|
||||
|
||||
const authRateLimiter = rateLimit({
|
||||
windowMs: 15 * 60 * 1000,
|
||||
max: 100,
|
||||
standardHeaders: true,
|
||||
legacyHeaders: false,
|
||||
message: { error: 'Too many authentication attempts', errorCode: 'RATE_LIMITED' }
|
||||
});
|
||||
const joinRateLimiter = rateLimit({
|
||||
windowMs: 60 * 1000,
|
||||
max: 30,
|
||||
standardHeaders: true,
|
||||
legacyHeaders: false,
|
||||
message: { error: 'Too many join attempts', errorCode: 'RATE_LIMITED' }
|
||||
});
|
||||
|
||||
function buildCorsOptions() {
|
||||
const allowlist = getCorsAllowlist();
|
||||
|
||||
if (allowlist.length === 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return {
|
||||
origin(origin: string | undefined, callback: (error: Error | null, allow?: boolean) => void) {
|
||||
if (!origin || allowlist.includes(origin)) {
|
||||
callback(null, true);
|
||||
return;
|
||||
}
|
||||
|
||||
callback(new Error('CORS origin not allowed'));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function createApp(): express.Express {
|
||||
const app = express();
|
||||
|
||||
// Trust loopback proxies only - avoids express-rate-limit ERR_ERL_PERMISSIVE_TRUST_PROXY.
|
||||
app.set('trust proxy', 'loopback');
|
||||
app.use(cors(buildCorsOptions()));
|
||||
app.set('trust proxy', true);
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
app.use('/api/users/login', authRateLimiter);
|
||||
app.use('/api/users/register', authRateLimiter);
|
||||
app.use('/api/servers/:id/join', joinRateLimiter);
|
||||
|
||||
registerRoutes(app);
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@ export interface ServerVariablesConfig {
|
||||
serverProtocol: ServerHttpProtocol;
|
||||
serverHost: string;
|
||||
serverTag: string;
|
||||
corsAllowlist: string[];
|
||||
linkPreview: LinkPreviewConfig;
|
||||
openApiDocs: OpenApiDocsConfig;
|
||||
}
|
||||
@@ -114,17 +113,6 @@ function normalizeLinkPreviewConfig(value: unknown): LinkPreviewConfig {
|
||||
return { enabled, cacheTtlMinutes: cacheTtl, maxCacheSizeMb: maxSize };
|
||||
}
|
||||
|
||||
function normalizeCorsAllowlist(value: unknown): string[] {
|
||||
if (!Array.isArray(value)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return value
|
||||
.filter((entry): entry is string => typeof entry === 'string')
|
||||
.map((entry) => entry.trim())
|
||||
.filter((entry) => entry.length > 0);
|
||||
}
|
||||
|
||||
function normalizeOpenApiDocsConfig(value: unknown): OpenApiDocsConfig {
|
||||
const raw = (value && typeof value === 'object' && !Array.isArray(value))
|
||||
? value as Record<string, unknown>
|
||||
@@ -181,7 +169,6 @@ export function ensureVariablesConfig(): ServerVariablesConfig {
|
||||
serverProtocol: normalizeServerProtocol(remainingParsed.serverProtocol),
|
||||
serverHost: normalizeServerHost(remainingParsed.serverHost ?? legacyServerIpAddress),
|
||||
serverTag: normalizeServerTag(remainingParsed.serverTag),
|
||||
corsAllowlist: normalizeCorsAllowlist(remainingParsed.corsAllowlist),
|
||||
linkPreview: normalizeLinkPreviewConfig(remainingParsed.linkPreview),
|
||||
openApiDocs: normalizeOpenApiDocsConfig(remainingParsed.openApiDocs)
|
||||
};
|
||||
@@ -199,23 +186,11 @@ export function ensureVariablesConfig(): ServerVariablesConfig {
|
||||
serverProtocol: normalized.serverProtocol,
|
||||
serverHost: normalized.serverHost,
|
||||
serverTag: normalized.serverTag,
|
||||
corsAllowlist: normalized.corsAllowlist,
|
||||
linkPreview: normalized.linkPreview,
|
||||
openApiDocs: normalized.openApiDocs
|
||||
};
|
||||
}
|
||||
|
||||
export function getCorsAllowlist(): string[] {
|
||||
if (hasEnvironmentOverride(process.env.CORS_ALLOWLIST)) {
|
||||
return (process.env.CORS_ALLOWLIST ?? '')
|
||||
.split(',')
|
||||
.map((entry) => entry.trim())
|
||||
.filter((entry) => entry.length > 0);
|
||||
}
|
||||
|
||||
return getVariablesConfig().corsAllowlist;
|
||||
}
|
||||
|
||||
export function getVariablesConfig(): ServerVariablesConfig {
|
||||
return ensureVariablesConfig();
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import { DataSource } from 'typeorm';
|
||||
import { AuthUserEntity } from '../../../entities';
|
||||
|
||||
export async function handleUpdateUserPasswordHash(
|
||||
dataSource: DataSource,
|
||||
userId: string,
|
||||
passwordHash: string
|
||||
): Promise<void> {
|
||||
const repo = dataSource.getRepository(AuthUserEntity);
|
||||
|
||||
await repo.update({ id: userId }, { passwordHash });
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
import { DataSource } from 'typeorm';
|
||||
import { AuthUserEntity } from '../../../entities';
|
||||
|
||||
export async function handleUpdateUserSigningPublicKey(
|
||||
dataSource: DataSource,
|
||||
userId: string,
|
||||
signingPublicKey: string
|
||||
): Promise<void> {
|
||||
const repo = dataSource.getRepository(AuthUserEntity);
|
||||
|
||||
await repo.update({ id: userId }, { signingPublicKey });
|
||||
}
|
||||
@@ -20,8 +20,6 @@ import { handleGetTrendingServers } from './queries/handlers/getTrendingServers'
|
||||
import { handleGetServerById } from './queries/handlers/getServerById';
|
||||
import { handleGetJoinRequestById } from './queries/handlers/getJoinRequestById';
|
||||
import { handleGetPendingRequestsForServer } from './queries/handlers/getPendingRequestsForServer';
|
||||
import { handleUpdateUserPasswordHash } from './commands/handlers/updateUserPasswordHash';
|
||||
import { handleUpdateUserSigningPublicKey } from './commands/handlers/updateUserSigningPublicKey';
|
||||
|
||||
export const registerUser = (user: AuthUserPayload) =>
|
||||
handleRegisterUser({ type: CommandType.RegisterUser, payload: { user } }, getDataSource());
|
||||
@@ -64,9 +62,3 @@ export const getJoinRequestById = (requestId: string) =>
|
||||
|
||||
export const getPendingRequestsForServer = (serverId: string) =>
|
||||
handleGetPendingRequestsForServer({ type: QueryType.GetPendingRequestsForServer, payload: { serverId } }, getDataSource());
|
||||
|
||||
export const updateUserPasswordHash = (userId: string, passwordHash: string) =>
|
||||
handleUpdateUserPasswordHash(getDataSource(), userId, passwordHash);
|
||||
|
||||
export const updateUserSigningPublicKey = (userId: string, signingPublicKey: string) =>
|
||||
handleUpdateUserSigningPublicKey(getDataSource(), userId, signingPublicKey);
|
||||
|
||||
@@ -14,8 +14,7 @@ export function rowToAuthUser(row: AuthUserEntity): AuthUserPayload {
|
||||
username: row.username,
|
||||
passwordHash: row.passwordHash,
|
||||
displayName: row.displayName,
|
||||
createdAt: row.createdAt,
|
||||
signingPublicKey: row.signingPublicKey ?? null
|
||||
createdAt: row.createdAt
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@ export interface AuthUserPayload {
|
||||
passwordHash: string;
|
||||
displayName: string;
|
||||
createdAt: number;
|
||||
signingPublicKey?: string | null;
|
||||
}
|
||||
|
||||
export type ServerChannelType = 'text' | 'voice';
|
||||
|
||||
@@ -21,8 +21,7 @@ import {
|
||||
PluginDataEntity,
|
||||
ServerPluginSettingsEntity,
|
||||
PluginUserMetadataEntity,
|
||||
DeviceTokenEntity,
|
||||
SessionTokenEntity
|
||||
DeviceTokenEntity
|
||||
} from '../entities';
|
||||
import { serverMigrations } from '../migrations';
|
||||
import {
|
||||
@@ -273,8 +272,7 @@ export async function initDatabase(): Promise<void> {
|
||||
PluginDataEntity,
|
||||
ServerPluginSettingsEntity,
|
||||
PluginUserMetadataEntity,
|
||||
DeviceTokenEntity,
|
||||
SessionTokenEntity
|
||||
DeviceTokenEntity
|
||||
],
|
||||
migrations: serverMigrations,
|
||||
synchronize: process.env.DB_SYNCHRONIZE === 'true',
|
||||
|
||||
@@ -20,7 +20,4 @@ export class AuthUserEntity {
|
||||
|
||||
@Column('integer')
|
||||
createdAt!: number;
|
||||
|
||||
@Column('text', { nullable: true })
|
||||
signingPublicKey!: string | null;
|
||||
}
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
import {
|
||||
Entity,
|
||||
PrimaryColumn,
|
||||
Column,
|
||||
Index
|
||||
} from 'typeorm';
|
||||
|
||||
@Entity('session_tokens')
|
||||
export class SessionTokenEntity {
|
||||
@PrimaryColumn('text')
|
||||
token!: string;
|
||||
|
||||
@Index('idx_session_tokens_user_id')
|
||||
@Column('text')
|
||||
userId!: string;
|
||||
|
||||
@Column('integer')
|
||||
issuedAt!: number;
|
||||
|
||||
@Column('integer')
|
||||
expiresAt!: number;
|
||||
}
|
||||
@@ -18,4 +18,3 @@ export { PluginDataEntity } from './PluginDataEntity';
|
||||
export { ServerPluginSettingsEntity } from './ServerPluginSettingsEntity';
|
||||
export { PluginUserMetadataEntity } from './PluginUserMetadataEntity';
|
||||
export { DeviceTokenEntity } from './DeviceTokenEntity';
|
||||
export { SessionTokenEntity } from './SessionTokenEntity';
|
||||
|
||||
@@ -21,7 +21,6 @@ import {
|
||||
ServerHttpProtocol
|
||||
} from './config/variables';
|
||||
import { setupWebSocket } from './websocket';
|
||||
import { pruneExpiredSessionTokens } from './services/session-auth.service';
|
||||
|
||||
function formatHostForUrl(host: string): string {
|
||||
if (host.startsWith('[') || !host.includes(':')) {
|
||||
@@ -62,7 +61,6 @@ function buildServer(app: ReturnType<typeof createApp>, serverProtocol: ServerHt
|
||||
|
||||
let listeningServer: ReturnType<typeof buildServer> | null = null;
|
||||
let staleJoinRequestInterval: ReturnType<typeof setInterval> | null = null;
|
||||
let sessionTokenPruneInterval: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
async function bootstrap(): Promise<void> {
|
||||
const variablesConfig = ensureVariablesConfig();
|
||||
@@ -101,11 +99,6 @@ async function bootstrap(): Promise<void> {
|
||||
.catch(err => console.error('Failed to clean up stale join requests:', err));
|
||||
}, 60 * 1000);
|
||||
|
||||
sessionTokenPruneInterval = setInterval(() => {
|
||||
pruneExpiredSessionTokens()
|
||||
.catch(err => console.error('Failed to prune expired session tokens:', err));
|
||||
}, 60 * 1000);
|
||||
|
||||
const onListening = () => {
|
||||
const displayHost = formatHostForUrl(getDisplayHost(serverHost));
|
||||
const wsProto = serverProtocol === 'https' ? 'wss' : 'ws';
|
||||
@@ -144,11 +137,6 @@ async function gracefulShutdown(signal: string): Promise<void> {
|
||||
staleJoinRequestInterval = null;
|
||||
}
|
||||
|
||||
if (sessionTokenPruneInterval) {
|
||||
clearInterval(sessionTokenPruneInterval);
|
||||
sessionTokenPruneInterval = null;
|
||||
}
|
||||
|
||||
console.log(`\n[Shutdown] ${signal} received - closing database...`);
|
||||
|
||||
if (listeningServer?.listening) {
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
import '../types/express-augmentation';
|
||||
import {
|
||||
NextFunction,
|
||||
Request,
|
||||
Response
|
||||
} from 'express';
|
||||
import { consumeSessionToken } from '../services/session-auth.service';
|
||||
|
||||
function readBearerToken(req: Request): string | null {
|
||||
const header = req.header('authorization');
|
||||
|
||||
if (!header || !header.toLowerCase().startsWith('bearer ')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const token = header.slice(7).trim();
|
||||
|
||||
return token || null;
|
||||
}
|
||||
|
||||
export async function requireAuth(req: Request, res: Response, next: NextFunction): Promise<void> {
|
||||
const token = readBearerToken(req);
|
||||
|
||||
if (!token) {
|
||||
res.status(401).json({ error: 'Missing or invalid authorization token', errorCode: 'UNAUTHORIZED' });
|
||||
return;
|
||||
}
|
||||
|
||||
const session = await consumeSessionToken(token);
|
||||
|
||||
if (!session) {
|
||||
res.status(401).json({ error: 'Missing or invalid authorization token', errorCode: 'UNAUTHORIZED' });
|
||||
return;
|
||||
}
|
||||
|
||||
req.authToken = session.token;
|
||||
req.authUserId = session.user.id;
|
||||
req.authUser = session.user;
|
||||
next();
|
||||
}
|
||||
|
||||
export function getAuthenticatedUserId(req: Request): string {
|
||||
const userId = req.authUserId;
|
||||
|
||||
if (!userId) {
|
||||
throw new Error('Authenticated user id missing after requireAuth');
|
||||
}
|
||||
|
||||
return userId;
|
||||
}
|
||||
|
||||
export function rejectSpoofedUserId(
|
||||
req: Request,
|
||||
res: Response,
|
||||
bodyUserId: unknown,
|
||||
fieldName: string
|
||||
): bodyUserId is string {
|
||||
if (typeof bodyUserId !== 'string' || !bodyUserId.trim()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (bodyUserId !== req.authUserId) {
|
||||
res.status(400).json({
|
||||
error: `${fieldName} must match the authenticated user`,
|
||||
errorCode: 'USER_ID_MISMATCH'
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class SessionTokens1000000000011 implements MigrationInterface {
|
||||
name = 'SessionTokens1000000000011';
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
CREATE TABLE IF NOT EXISTS "session_tokens" (
|
||||
"token" TEXT PRIMARY KEY NOT NULL,
|
||||
"userId" TEXT NOT NULL,
|
||||
"issuedAt" INTEGER NOT NULL,
|
||||
"expiresAt" INTEGER NOT NULL
|
||||
)
|
||||
`);
|
||||
|
||||
await queryRunner.query(`CREATE INDEX IF NOT EXISTS "idx_session_tokens_user_id" ON "session_tokens" ("userId")`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`DROP TABLE IF EXISTS "session_tokens"`);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class SigningPublicKey1000000000012 implements MigrationInterface {
|
||||
name = 'SigningPublicKey1000000000012';
|
||||
|
||||
async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query('ALTER TABLE "users" ADD COLUMN "signingPublicKey" text');
|
||||
}
|
||||
|
||||
async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query('ALTER TABLE "users" DROP COLUMN "signingPublicKey"');
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,6 @@ import { PluginSupport1000000000007 } from './1000000000007-PluginSupport';
|
||||
import { ServerPluginInstallMetadata1000000000008 } from './1000000000008-ServerPluginInstallMetadata';
|
||||
import { ServerIcons1000000000009 } from './1000000000009-ServerIcons';
|
||||
import { DeviceTokens1000000000010 } from './1000000000010-DeviceTokens';
|
||||
import { SessionTokens1000000000011 } from './1000000000011-SessionTokens';
|
||||
import { SigningPublicKey1000000000012 } from './1000000000012-SigningPublicKey';
|
||||
|
||||
export const serverMigrations = [
|
||||
InitialSchema1000000000000,
|
||||
@@ -23,7 +21,5 @@ export const serverMigrations = [
|
||||
PluginSupport1000000000007,
|
||||
ServerPluginInstallMetadata1000000000008,
|
||||
ServerIcons1000000000009,
|
||||
DeviceTokens1000000000010,
|
||||
SessionTokens1000000000011,
|
||||
SigningPublicKey1000000000012
|
||||
DeviceTokens1000000000010
|
||||
];
|
||||
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
listDeviceTokensForUser,
|
||||
upsertDeviceToken
|
||||
} from '../services/push-dispatch.service';
|
||||
import { requireAuth } from '../middleware/require-auth';
|
||||
|
||||
export interface DeviceTokenRecord {
|
||||
userId: string;
|
||||
@@ -15,8 +14,6 @@ export interface DeviceTokenRecord {
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.use(requireAuth);
|
||||
|
||||
router.post('/', async (req, res) => {
|
||||
const { userId, platform, token } = req.body as Partial<DeviceTokenRecord>;
|
||||
|
||||
@@ -24,20 +21,12 @@ router.post('/', async (req, res) => {
|
||||
return res.status(400).json({ error: 'Missing or invalid userId/platform/token' });
|
||||
}
|
||||
|
||||
if (userId !== req.authUserId) {
|
||||
return res.status(403).json({ error: 'Not authorized', errorCode: 'NOT_AUTHORIZED' });
|
||||
}
|
||||
|
||||
await upsertDeviceToken({ userId, platform, token });
|
||||
|
||||
res.status(201).json({ ok: true });
|
||||
});
|
||||
|
||||
router.get('/:userId', async (req, res) => {
|
||||
if (req.params.userId !== req.authUserId) {
|
||||
return res.status(403).json({ error: 'Not authorized', errorCode: 'NOT_AUTHORIZED' });
|
||||
}
|
||||
|
||||
const records = await listDeviceTokensForUser(req.params.userId);
|
||||
|
||||
res.json({
|
||||
@@ -51,10 +40,6 @@ router.get('/:userId', async (req, res) => {
|
||||
});
|
||||
|
||||
router.post('/:userId/dispatch', async (req, res) => {
|
||||
if (req.params.userId !== req.authUserId) {
|
||||
return res.status(403).json({ error: 'Not authorized', errorCode: 'NOT_AUTHORIZED' });
|
||||
}
|
||||
|
||||
const { title, body, data } = req.body as {
|
||||
title?: string;
|
||||
body?: string;
|
||||
|
||||
@@ -7,29 +7,20 @@ import {
|
||||
} from '../cqrs';
|
||||
import { notifyUser } from '../websocket/broadcast';
|
||||
import { resolveServerPermission } from '../services/server-permissions.service';
|
||||
import { getAuthenticatedUserId, requireAuth } from '../middleware/require-auth';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.put('/:id', requireAuth, async (req, res) => {
|
||||
router.put('/:id', async (req, res) => {
|
||||
const { id } = req.params;
|
||||
const { ownerId, status } = req.body;
|
||||
const authenticatedUserId = getAuthenticatedUserId(req);
|
||||
const request = await getJoinRequestById(id);
|
||||
|
||||
if (!request)
|
||||
return res.status(404).json({ error: 'Request not found' });
|
||||
|
||||
if (ownerId && ownerId !== authenticatedUserId) {
|
||||
return res.status(400).json({
|
||||
error: 'ownerId must match the authenticated user',
|
||||
errorCode: 'USER_ID_MISMATCH'
|
||||
});
|
||||
}
|
||||
|
||||
const server = await getServerById(request.serverId);
|
||||
|
||||
if (!server || !resolveServerPermission(server, authenticatedUserId, 'manageServer'))
|
||||
if (!server || !ownerId || !resolveServerPermission(server, String(ownerId), 'manageServer'))
|
||||
return res.status(403).json({ error: 'Not authorized' });
|
||||
|
||||
await updateJoinRequestStatus(id, status as JoinRequestPayload['status']);
|
||||
|
||||
@@ -8,7 +8,6 @@ import {
|
||||
upsertPluginRequirement
|
||||
} from '../services/plugin-support.service';
|
||||
import { broadcastToServer } from '../websocket/broadcast';
|
||||
import { getAuthenticatedUserId, requireAuth } from '../middleware/require-auth';
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -22,28 +21,8 @@ function sendPluginSupportError(error: unknown, res: Response): void {
|
||||
res.status(500).json({ error: 'Internal server error', errorCode: 'INTERNAL_ERROR' });
|
||||
}
|
||||
|
||||
type AuthenticatedRequest = Parameters<typeof getAuthenticatedUserId>[0] & { body: { actorUserId?: unknown } };
|
||||
|
||||
function readOptionalActorUserId(req: AuthenticatedRequest, res: Response): string | null {
|
||||
let authenticatedUserId: string;
|
||||
|
||||
try {
|
||||
authenticatedUserId = getAuthenticatedUserId(req);
|
||||
} catch {
|
||||
res.status(401).json({ error: 'Missing or invalid authorization token', errorCode: 'UNAUTHORIZED' });
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof req.body.actorUserId === 'string' && req.body.actorUserId.trim() && req.body.actorUserId !== authenticatedUserId) {
|
||||
res.status(400).json({
|
||||
error: 'actorUserId must match the authenticated user',
|
||||
errorCode: 'USER_ID_MISMATCH'
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return authenticatedUserId;
|
||||
function readActorUserId(value: unknown): string {
|
||||
return typeof value === 'string' ? value.trim() : '';
|
||||
}
|
||||
|
||||
async function broadcastRequirementsSnapshot(serverId: string): Promise<void> {
|
||||
@@ -64,17 +43,12 @@ router.get('/:serverId/plugins', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/:serverId/plugins/:pluginId/requirement', requireAuth, async (req, res) => {
|
||||
router.put('/:serverId/plugins/:pluginId/requirement', async (req, res) => {
|
||||
const { serverId, pluginId } = req.params;
|
||||
const actorUserId = readOptionalActorUserId(req, res);
|
||||
|
||||
if (!actorUserId) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const requirement = await upsertPluginRequirement({
|
||||
actorUserId,
|
||||
actorUserId: readActorUserId(req.body.actorUserId),
|
||||
installUrl: req.body.installUrl,
|
||||
manifest: req.body.manifest,
|
||||
pluginId,
|
||||
@@ -92,17 +66,12 @@ router.put('/:serverId/plugins/:pluginId/requirement', requireAuth, async (req,
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/:serverId/plugins/:pluginId/requirement', requireAuth, async (req, res) => {
|
||||
router.delete('/:serverId/plugins/:pluginId/requirement', async (req, res) => {
|
||||
const { serverId, pluginId } = req.params;
|
||||
const actorUserId = readOptionalActorUserId(req, res);
|
||||
|
||||
if (!actorUserId) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await deletePluginRequirement({
|
||||
actorUserId,
|
||||
actorUserId: readActorUserId(req.body.actorUserId),
|
||||
pluginId,
|
||||
serverId
|
||||
});
|
||||
@@ -114,17 +83,12 @@ router.delete('/:serverId/plugins/:pluginId/requirement', requireAuth, async (re
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/:serverId/plugins/:pluginId/events/:eventName', requireAuth, async (req, res) => {
|
||||
router.put('/:serverId/plugins/:pluginId/events/:eventName', async (req, res) => {
|
||||
const { serverId, pluginId, eventName } = req.params;
|
||||
const actorUserId = readOptionalActorUserId(req, res);
|
||||
|
||||
if (!actorUserId) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const eventDefinition = await upsertPluginEventDefinition({
|
||||
actorUserId,
|
||||
actorUserId: readActorUserId(req.body.actorUserId),
|
||||
direction: req.body.direction,
|
||||
eventName,
|
||||
maxPayloadBytes: req.body.maxPayloadBytes,
|
||||
@@ -142,17 +106,12 @@ router.put('/:serverId/plugins/:pluginId/events/:eventName', requireAuth, async
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/:serverId/plugins/:pluginId/events/:eventName', requireAuth, async (req, res) => {
|
||||
router.delete('/:serverId/plugins/:pluginId/events/:eventName', async (req, res) => {
|
||||
const { serverId, pluginId, eventName } = req.params;
|
||||
const actorUserId = readOptionalActorUserId(req, res);
|
||||
|
||||
if (!actorUserId) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await deletePluginEventDefinition({
|
||||
actorUserId,
|
||||
actorUserId: readActorUserId(req.body.actorUserId),
|
||||
eventName,
|
||||
pluginId,
|
||||
serverId
|
||||
|
||||
@@ -35,11 +35,6 @@ import {
|
||||
canModerateServerMember,
|
||||
resolveServerPermission
|
||||
} from '../services/server-permissions.service';
|
||||
import {
|
||||
getAuthenticatedUserId,
|
||||
requireAuth,
|
||||
rejectSpoofedUserId
|
||||
} from '../middleware/require-auth';
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -190,7 +185,7 @@ router.get('/trending', async (req, res) => {
|
||||
res.json({ servers: enrichedResults, total: enrichedResults.length, limit });
|
||||
});
|
||||
|
||||
router.post('/', requireAuth, async (req, res) => {
|
||||
router.post('/', async (req, res) => {
|
||||
const {
|
||||
id: clientId,
|
||||
name,
|
||||
@@ -209,17 +204,12 @@ router.post('/', requireAuth, async (req, res) => {
|
||||
if (!name || !ownerId || !ownerPublicKey)
|
||||
return res.status(400).json({ error: 'Missing required fields' });
|
||||
|
||||
if (!rejectSpoofedUserId(req, res, ownerId, 'ownerId')) {
|
||||
return;
|
||||
}
|
||||
|
||||
const authenticatedOwnerId = getAuthenticatedUserId(req);
|
||||
const passwordHash = passwordHashForInput(password);
|
||||
const server: ServerPayload = {
|
||||
id: clientId || uuidv4(),
|
||||
name,
|
||||
description,
|
||||
ownerId: authenticatedOwnerId,
|
||||
ownerId,
|
||||
ownerPublicKey,
|
||||
hasPassword: !!passwordHash,
|
||||
passwordHash,
|
||||
@@ -235,12 +225,12 @@ router.post('/', requireAuth, async (req, res) => {
|
||||
};
|
||||
|
||||
await upsertServer(server);
|
||||
await ensureServerMembership(server.id, authenticatedOwnerId);
|
||||
await ensureServerMembership(server.id, ownerId);
|
||||
|
||||
res.status(201).json(await enrichServer(server, getRequestOrigin(req)));
|
||||
});
|
||||
|
||||
router.put('/:id', requireAuth, async (req, res) => {
|
||||
router.put('/:id', async (req, res) => {
|
||||
const { id } = req.params;
|
||||
const {
|
||||
currentOwnerId,
|
||||
@@ -252,16 +242,13 @@ router.put('/:id', requireAuth, async (req, res) => {
|
||||
...updates
|
||||
} = req.body;
|
||||
const existing = await getServerById(id);
|
||||
const authenticatedOwnerId = getAuthenticatedUserId(req);
|
||||
const authenticatedOwnerId = currentOwnerId ?? req.body.ownerId;
|
||||
|
||||
if (!existing)
|
||||
return res.status(404).json({ error: 'Server not found' });
|
||||
|
||||
if (currentOwnerId && currentOwnerId !== authenticatedOwnerId) {
|
||||
return res.status(400).json({
|
||||
error: 'currentOwnerId must match the authenticated user',
|
||||
errorCode: 'USER_ID_MISMATCH'
|
||||
});
|
||||
if (!authenticatedOwnerId) {
|
||||
return res.status(400).json({ error: 'Missing currentOwnerId' });
|
||||
}
|
||||
|
||||
if (!canManageServerUpdate(existing, authenticatedOwnerId, {
|
||||
@@ -289,22 +276,18 @@ router.put('/:id', requireAuth, async (req, res) => {
|
||||
res.json(await enrichServer(server, getRequestOrigin(req)));
|
||||
});
|
||||
|
||||
router.post('/:id/join', requireAuth, async (req, res) => {
|
||||
router.post('/:id/join', async (req, res) => {
|
||||
const { id: serverId } = req.params;
|
||||
const { userId, password, inviteId } = req.body;
|
||||
const authenticatedUserId = getAuthenticatedUserId(req);
|
||||
|
||||
if (userId && userId !== authenticatedUserId) {
|
||||
return res.status(400).json({
|
||||
error: 'userId must match the authenticated user',
|
||||
errorCode: 'USER_ID_MISMATCH'
|
||||
});
|
||||
if (!userId) {
|
||||
return res.status(400).json({ error: 'Missing userId', errorCode: 'MISSING_USER' });
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await joinServerWithAccess({
|
||||
serverId,
|
||||
userId: authenticatedUserId,
|
||||
userId: String(userId),
|
||||
password: typeof password === 'string' ? password : undefined,
|
||||
inviteId: typeof inviteId === 'string' ? inviteId : undefined
|
||||
});
|
||||
@@ -322,16 +305,12 @@ router.post('/:id/join', requireAuth, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/:id/invites', requireAuth, async (req, res) => {
|
||||
router.post('/:id/invites', async (req, res) => {
|
||||
const { id: serverId } = req.params;
|
||||
const { requesterUserId, requesterDisplayName } = req.body;
|
||||
const authenticatedUserId = getAuthenticatedUserId(req);
|
||||
|
||||
if (requesterUserId && requesterUserId !== authenticatedUserId) {
|
||||
return res.status(400).json({
|
||||
error: 'requesterUserId must match the authenticated user',
|
||||
errorCode: 'USER_ID_MISMATCH'
|
||||
});
|
||||
if (!requesterUserId) {
|
||||
return res.status(400).json({ error: 'Missing requesterUserId', errorCode: 'MISSING_USER' });
|
||||
}
|
||||
|
||||
const server = await getServerById(serverId);
|
||||
@@ -343,7 +322,7 @@ router.post('/:id/invites', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const invite = await createServerInvite(
|
||||
serverId,
|
||||
authenticatedUserId,
|
||||
String(requesterUserId),
|
||||
typeof requesterDisplayName === 'string' ? requesterDisplayName : undefined
|
||||
);
|
||||
|
||||
@@ -353,10 +332,9 @@ router.post('/:id/invites', requireAuth, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/:id/moderation/kick', requireAuth, async (req, res) => {
|
||||
router.post('/:id/moderation/kick', async (req, res) => {
|
||||
const { id: serverId } = req.params;
|
||||
const { actorUserId, targetUserId } = req.body;
|
||||
const authenticatedUserId = getAuthenticatedUserId(req);
|
||||
const server = await getServerById(serverId);
|
||||
|
||||
if (!server) {
|
||||
@@ -367,14 +345,7 @@ router.post('/:id/moderation/kick', requireAuth, async (req, res) => {
|
||||
return res.status(400).json({ error: 'Missing targetUserId', errorCode: 'MISSING_TARGET' });
|
||||
}
|
||||
|
||||
if (actorUserId && actorUserId !== authenticatedUserId) {
|
||||
return res.status(400).json({
|
||||
error: 'actorUserId must match the authenticated user',
|
||||
errorCode: 'USER_ID_MISMATCH'
|
||||
});
|
||||
}
|
||||
|
||||
if (!canModerateServerMember(server, authenticatedUserId, String(targetUserId), 'kickMembers')) {
|
||||
if (!canModerateServerMember(server, String(actorUserId || ''), String(targetUserId), 'kickMembers')) {
|
||||
return res.status(403).json({ error: 'Not authorized', errorCode: 'NOT_AUTHORIZED' });
|
||||
}
|
||||
|
||||
@@ -383,10 +354,9 @@ router.post('/:id/moderation/kick', requireAuth, async (req, res) => {
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
router.post('/:id/moderation/ban', requireAuth, async (req, res) => {
|
||||
router.post('/:id/moderation/ban', async (req, res) => {
|
||||
const { id: serverId } = req.params;
|
||||
const { actorUserId, targetUserId, banId, displayName, reason, expiresAt } = req.body;
|
||||
const authenticatedUserId = getAuthenticatedUserId(req);
|
||||
const server = await getServerById(serverId);
|
||||
|
||||
if (!server) {
|
||||
@@ -397,14 +367,7 @@ router.post('/:id/moderation/ban', requireAuth, async (req, res) => {
|
||||
return res.status(400).json({ error: 'Missing targetUserId', errorCode: 'MISSING_TARGET' });
|
||||
}
|
||||
|
||||
if (actorUserId && actorUserId !== authenticatedUserId) {
|
||||
return res.status(400).json({
|
||||
error: 'actorUserId must match the authenticated user',
|
||||
errorCode: 'USER_ID_MISMATCH'
|
||||
});
|
||||
}
|
||||
|
||||
if (!canModerateServerMember(server, authenticatedUserId, String(targetUserId), 'banMembers')) {
|
||||
if (!canModerateServerMember(server, String(actorUserId || ''), String(targetUserId), 'banMembers')) {
|
||||
return res.status(403).json({ error: 'Not authorized', errorCode: 'NOT_AUTHORIZED' });
|
||||
}
|
||||
|
||||
@@ -412,7 +375,7 @@ router.post('/:id/moderation/ban', requireAuth, async (req, res) => {
|
||||
serverId,
|
||||
userId: String(targetUserId),
|
||||
banId: typeof banId === 'string' ? banId : undefined,
|
||||
bannedBy: authenticatedUserId,
|
||||
bannedBy: String(actorUserId || ''),
|
||||
displayName: typeof displayName === 'string' ? displayName : undefined,
|
||||
reason: typeof reason === 'string' ? reason : undefined,
|
||||
expiresAt: typeof expiresAt === 'number' ? expiresAt : undefined
|
||||
@@ -421,24 +384,16 @@ router.post('/:id/moderation/ban', requireAuth, async (req, res) => {
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
router.post('/:id/moderation/unban', requireAuth, async (req, res) => {
|
||||
router.post('/:id/moderation/unban', async (req, res) => {
|
||||
const { id: serverId } = req.params;
|
||||
const { actorUserId, banId, targetUserId } = req.body;
|
||||
const authenticatedUserId = getAuthenticatedUserId(req);
|
||||
const server = await getServerById(serverId);
|
||||
|
||||
if (!server) {
|
||||
return res.status(404).json({ error: 'Server not found', errorCode: 'SERVER_NOT_FOUND' });
|
||||
}
|
||||
|
||||
if (actorUserId && actorUserId !== authenticatedUserId) {
|
||||
return res.status(400).json({
|
||||
error: 'actorUserId must match the authenticated user',
|
||||
errorCode: 'USER_ID_MISMATCH'
|
||||
});
|
||||
}
|
||||
|
||||
if (!resolveServerPermission(server, authenticatedUserId, 'manageBans')) {
|
||||
if (!resolveServerPermission(server, String(actorUserId || ''), 'manageBans')) {
|
||||
return res.status(403).json({ error: 'Not authorized', errorCode: 'NOT_AUTHORIZED' });
|
||||
}
|
||||
|
||||
@@ -451,29 +406,25 @@ router.post('/:id/moderation/unban', requireAuth, async (req, res) => {
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
router.post('/:id/leave', requireAuth, async (req, res) => {
|
||||
router.post('/:id/leave', async (req, res) => {
|
||||
const { id: serverId } = req.params;
|
||||
const { userId } = req.body;
|
||||
const authenticatedUserId = getAuthenticatedUserId(req);
|
||||
const server = await getServerById(serverId);
|
||||
|
||||
if (!server) {
|
||||
return res.status(404).json({ error: 'Server not found', errorCode: 'SERVER_NOT_FOUND' });
|
||||
}
|
||||
|
||||
if (userId && userId !== authenticatedUserId) {
|
||||
return res.status(400).json({
|
||||
error: 'userId must match the authenticated user',
|
||||
errorCode: 'USER_ID_MISMATCH'
|
||||
});
|
||||
if (!userId) {
|
||||
return res.status(400).json({ error: 'Missing userId', errorCode: 'MISSING_USER' });
|
||||
}
|
||||
|
||||
await leaveServerUser(serverId, authenticatedUserId);
|
||||
await leaveServerUser(serverId, String(userId));
|
||||
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
router.post('/:id/heartbeat', requireAuth, async (req, res) => {
|
||||
router.post('/:id/heartbeat', async (req, res) => {
|
||||
const { id } = req.params;
|
||||
const { currentUsers } = req.body;
|
||||
const existing = await getServerById(id);
|
||||
@@ -491,38 +442,30 @@ router.post('/:id/heartbeat', requireAuth, async (req, res) => {
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
router.delete('/:id', requireAuth, async (req, res) => {
|
||||
router.delete('/:id', async (req, res) => {
|
||||
const { id } = req.params;
|
||||
const { ownerId } = req.body;
|
||||
const authenticatedOwnerId = getAuthenticatedUserId(req);
|
||||
const existing = await getServerById(id);
|
||||
|
||||
if (!existing)
|
||||
return res.status(404).json({ error: 'Server not found' });
|
||||
|
||||
if (ownerId && ownerId !== authenticatedOwnerId) {
|
||||
return res.status(400).json({
|
||||
error: 'ownerId must match the authenticated user',
|
||||
errorCode: 'USER_ID_MISMATCH'
|
||||
});
|
||||
}
|
||||
|
||||
if (existing.ownerId !== authenticatedOwnerId)
|
||||
if (existing.ownerId !== ownerId)
|
||||
return res.status(403).json({ error: 'Not authorized' });
|
||||
|
||||
await deleteServer(id);
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
router.get('/:id/requests', requireAuth, async (req, res) => {
|
||||
router.get('/:id/requests', async (req, res) => {
|
||||
const { id: serverId } = req.params;
|
||||
const authenticatedUserId = getAuthenticatedUserId(req);
|
||||
const { ownerId } = req.query;
|
||||
const server = await getServerById(serverId);
|
||||
|
||||
if (!server)
|
||||
return res.status(404).json({ error: 'Server not found' });
|
||||
|
||||
if (!resolveServerPermission(server, authenticatedUserId, 'manageServer'))
|
||||
if (server.ownerId !== ownerId)
|
||||
return res.status(403).json({ error: 'Not authorized' });
|
||||
|
||||
const requests = await getPendingRequestsForServer(serverId);
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
expect
|
||||
} from 'vitest';
|
||||
import { isDuplicateUsernameError } from './user-registration.rules';
|
||||
|
||||
describe('user-registration.rules', () => {
|
||||
it('detects sqlite unique constraint failures on username', () => {
|
||||
expect(isDuplicateUsernameError({
|
||||
message: 'UNIQUE constraint failed: users.username'
|
||||
})).toBe(true);
|
||||
});
|
||||
|
||||
it('detects typeorm query failed errors with username constraint text', () => {
|
||||
expect(isDuplicateUsernameError({
|
||||
name: 'QueryFailedError',
|
||||
message: 'SQLITE_CONSTRAINT: UNIQUE constraint failed: users.username'
|
||||
})).toBe(true);
|
||||
});
|
||||
|
||||
it('ignores unrelated database errors', () => {
|
||||
expect(isDuplicateUsernameError({
|
||||
message: 'UNIQUE constraint failed: servers.id'
|
||||
})).toBe(false);
|
||||
|
||||
expect(isDuplicateUsernameError(new Error('connection lost'))).toBe(false);
|
||||
expect(isDuplicateUsernameError(null)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,11 +0,0 @@
|
||||
export function isDuplicateUsernameError(error: unknown): boolean {
|
||||
if (!error || typeof error !== 'object') {
|
||||
return false;
|
||||
}
|
||||
|
||||
const message = 'message' in error && typeof error.message === 'string'
|
||||
? error.message
|
||||
: '';
|
||||
|
||||
return message.includes('UNIQUE constraint failed: users.username');
|
||||
}
|
||||
@@ -1,31 +1,13 @@
|
||||
import crypto from 'crypto';
|
||||
import { Router } from 'express';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import {
|
||||
getUserById,
|
||||
getUserByUsername,
|
||||
registerUser,
|
||||
updateUserPasswordHash,
|
||||
updateUserSigningPublicKey
|
||||
} from '../cqrs';
|
||||
import { hashPasswordForStorage, verifyPassword } from '../services/password-auth.service';
|
||||
import { issueSessionToken, revokeSessionToken } from '../services/session-auth.service';
|
||||
import { getAuthenticatedUserId, requireAuth } from '../middleware/require-auth';
|
||||
import { isDuplicateUsernameError } from './user-registration.rules';
|
||||
import { getUserByUsername, registerUser } from '../cqrs';
|
||||
|
||||
const router = Router();
|
||||
|
||||
function buildAuthResponse(user: {
|
||||
id: string;
|
||||
username: string;
|
||||
displayName: string;
|
||||
}, token: string, expiresAt: number) {
|
||||
return {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
displayName: user.displayName,
|
||||
token,
|
||||
expiresAt
|
||||
};
|
||||
function hashPassword(pw: string): string {
|
||||
return crypto.createHash('sha256').update(pw)
|
||||
.digest('hex');
|
||||
}
|
||||
|
||||
router.post('/register', async (req, res) => {
|
||||
@@ -42,73 +24,23 @@ router.post('/register', async (req, res) => {
|
||||
const user = {
|
||||
id: uuidv4(),
|
||||
username,
|
||||
passwordHash: await hashPasswordForStorage(password),
|
||||
passwordHash: hashPassword(password),
|
||||
displayName: displayName || username,
|
||||
createdAt: Date.now()
|
||||
};
|
||||
|
||||
try {
|
||||
await registerUser(user);
|
||||
} catch (error) {
|
||||
if (isDuplicateUsernameError(error)) {
|
||||
return res.status(409).json({ error: 'Username taken' });
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
const session = await issueSessionToken(user.id);
|
||||
|
||||
res.status(201).json(buildAuthResponse(user, session.token, session.expiresAt));
|
||||
res.status(201).json({ id: user.id, username: user.username, displayName: user.displayName });
|
||||
});
|
||||
|
||||
router.post('/login', async (req, res) => {
|
||||
const { username, password } = req.body;
|
||||
const user = await getUserByUsername(username);
|
||||
|
||||
if (!user || !(await verifyPassword(password, user.passwordHash)))
|
||||
if (!user || user.passwordHash !== hashPassword(password))
|
||||
return res.status(401).json({ error: 'Invalid credentials' });
|
||||
|
||||
const upgradedHash = await hashPasswordForStorage(password, user.passwordHash);
|
||||
|
||||
if (upgradedHash !== user.passwordHash) {
|
||||
await updateUserPasswordHash(user.id, upgradedHash);
|
||||
}
|
||||
|
||||
const session = await issueSessionToken(user.id);
|
||||
|
||||
res.json(buildAuthResponse(user, session.token, session.expiresAt));
|
||||
});
|
||||
|
||||
router.put('/me/signing-key', requireAuth, async (req, res) => {
|
||||
const { publicKeyJwk } = req.body;
|
||||
const userId = getAuthenticatedUserId(req);
|
||||
|
||||
if (!publicKeyJwk || typeof publicKeyJwk !== 'object') {
|
||||
return res.status(400).json({ error: 'Missing publicKeyJwk', errorCode: 'INVALID_SIGNING_KEY' });
|
||||
}
|
||||
|
||||
await updateUserSigningPublicKey(userId, JSON.stringify(publicKeyJwk));
|
||||
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
router.get('/:id/signing-public-key', async (req, res) => {
|
||||
const user = await getUserById(req.params.id);
|
||||
|
||||
if (!user?.signingPublicKey) {
|
||||
return res.status(404).json({ error: 'Signing public key not found', errorCode: 'SIGNING_KEY_NOT_FOUND' });
|
||||
}
|
||||
|
||||
res.json({ publicKeyJwk: JSON.parse(user.signingPublicKey) });
|
||||
});
|
||||
|
||||
router.post('/logout', requireAuth, async (req, res) => {
|
||||
if (req.authToken) {
|
||||
await revokeSessionToken(req.authToken);
|
||||
}
|
||||
|
||||
res.json({ ok: true });
|
||||
res.json({ id: user.id, username: user.username, displayName: user.displayName });
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
expect
|
||||
} from 'vitest';
|
||||
import crypto from 'crypto';
|
||||
import {
|
||||
hashPasswordForStorage,
|
||||
isLegacySha256Hash,
|
||||
verifyPassword
|
||||
} from './password-auth.service';
|
||||
|
||||
describe('password-auth.service', () => {
|
||||
it('stores bcrypt hashes for new passwords', async () => {
|
||||
const hash = await hashPasswordForStorage('secret-pass');
|
||||
|
||||
expect(hash.startsWith('$2')).toBe(true);
|
||||
expect(isLegacySha256Hash(hash)).toBe(false);
|
||||
});
|
||||
|
||||
it('verifies bcrypt passwords', async () => {
|
||||
const hash = await hashPasswordForStorage('secret-pass');
|
||||
|
||||
await expect(verifyPassword('secret-pass', hash)).resolves.toBe(true);
|
||||
await expect(verifyPassword('wrong-pass', hash)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('verifies legacy sha256 passwords', async () => {
|
||||
const legacyHash = crypto.createHash('sha256').update('legacy-pass')
|
||||
.digest('hex');
|
||||
|
||||
expect(isLegacySha256Hash(legacyHash)).toBe(true);
|
||||
await expect(verifyPassword('legacy-pass', legacyHash)).resolves.toBe(true);
|
||||
await expect(verifyPassword('wrong-pass', legacyHash)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('rehashes legacy passwords to bcrypt after successful verification', async () => {
|
||||
const legacyHash = crypto.createHash('sha256').update('legacy-pass')
|
||||
.digest('hex');
|
||||
const upgraded = await hashPasswordForStorage('legacy-pass', legacyHash);
|
||||
|
||||
expect(upgraded.startsWith('$2')).toBe(true);
|
||||
await expect(verifyPassword('legacy-pass', upgraded)).resolves.toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,33 +0,0 @@
|
||||
import crypto from 'crypto';
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
const BCRYPT_ROUNDS = 12;
|
||||
|
||||
export function isLegacySha256Hash(passwordHash: string): boolean {
|
||||
return /^[a-f0-9]{64}$/i.test(passwordHash);
|
||||
}
|
||||
|
||||
export async function hashPasswordForStorage(
|
||||
password: string,
|
||||
existingHash?: string
|
||||
): Promise<string> {
|
||||
if (existingHash && isLegacySha256Hash(existingHash)) {
|
||||
const legacyMatches = crypto.createHash('sha256').update(password)
|
||||
.digest('hex') === existingHash;
|
||||
|
||||
if (legacyMatches) {
|
||||
return bcrypt.hash(password, BCRYPT_ROUNDS);
|
||||
}
|
||||
}
|
||||
|
||||
return bcrypt.hash(password, BCRYPT_ROUNDS);
|
||||
}
|
||||
|
||||
export async function verifyPassword(password: string, passwordHash: string): Promise<boolean> {
|
||||
if (isLegacySha256Hash(passwordHash)) {
|
||||
return crypto.createHash('sha256').update(password)
|
||||
.digest('hex') === passwordHash;
|
||||
}
|
||||
|
||||
return bcrypt.compare(password, passwordHash);
|
||||
}
|
||||
@@ -134,34 +134,6 @@ export async function countServerMemberships(serverId: string): Promise<number>
|
||||
return await getMembershipRepository().count({ where: { serverId } });
|
||||
}
|
||||
|
||||
export async function usersShareServerMembership(userA: string, userB: string): Promise<boolean> {
|
||||
if (userA === userB) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const repo = getMembershipRepository();
|
||||
const membershipsForA = await repo.find({
|
||||
where: { userId: userA },
|
||||
select: ['serverId']
|
||||
});
|
||||
|
||||
if (membershipsForA.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const membership of membershipsForA) {
|
||||
const shared = await repo.findOne({
|
||||
where: { serverId: membership.serverId, userId: userB }
|
||||
});
|
||||
|
||||
if (shared) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function ensureServerMembership(serverId: string, userId: string): Promise<ServerMembershipEntity> {
|
||||
const repo = getMembershipRepository();
|
||||
const now = Date.now();
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
import {
|
||||
afterEach,
|
||||
describe,
|
||||
expect,
|
||||
it
|
||||
} from 'vitest';
|
||||
import { getSessionTokenTtlMs } from './session-auth.service';
|
||||
|
||||
const TEN_YEARS_MS = 10 * 365 * 24 * 60 * 60 * 1000;
|
||||
|
||||
describe('session-auth.service', () => {
|
||||
const originalTtl = process.env.SESSION_TOKEN_TTL_MS;
|
||||
|
||||
afterEach(() => {
|
||||
if (originalTtl === undefined) {
|
||||
delete process.env.SESSION_TOKEN_TTL_MS;
|
||||
} else {
|
||||
process.env.SESSION_TOKEN_TTL_MS = originalTtl;
|
||||
}
|
||||
});
|
||||
|
||||
it('defaults session tokens to a very long lifetime', () => {
|
||||
delete process.env.SESSION_TOKEN_TTL_MS;
|
||||
|
||||
expect(getSessionTokenTtlMs()).toBe(TEN_YEARS_MS);
|
||||
});
|
||||
|
||||
it('honors SESSION_TOKEN_TTL_MS when configured', () => {
|
||||
process.env.SESSION_TOKEN_TTL_MS = '3600000';
|
||||
|
||||
expect(getSessionTokenTtlMs()).toBe(3_600_000);
|
||||
});
|
||||
|
||||
it('falls back to the default when SESSION_TOKEN_TTL_MS is invalid', () => {
|
||||
process.env.SESSION_TOKEN_TTL_MS = 'not-a-number';
|
||||
|
||||
expect(getSessionTokenTtlMs()).toBe(TEN_YEARS_MS);
|
||||
});
|
||||
});
|
||||
@@ -1,110 +0,0 @@
|
||||
import { randomBytes } from 'crypto';
|
||||
import { getDataSource } from '../db/database';
|
||||
import { SessionTokenEntity } from '../entities/SessionTokenEntity';
|
||||
import { getUserById } from '../cqrs';
|
||||
import type { AuthUserPayload } from '../cqrs/types';
|
||||
|
||||
const DEFAULT_TOKEN_TTL_MS = 10 * 365 * 24 * 60 * 60 * 1000;
|
||||
|
||||
export interface IssuedSessionToken {
|
||||
token: string;
|
||||
userId: string;
|
||||
issuedAt: number;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
export interface AuthenticatedSession {
|
||||
token: string;
|
||||
user: AuthUserPayload;
|
||||
issuedAt: number;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
function getTokenRepository() {
|
||||
return getDataSource().getRepository(SessionTokenEntity);
|
||||
}
|
||||
|
||||
export function getSessionTokenTtlMs(): number {
|
||||
const configured = Number(process.env.SESSION_TOKEN_TTL_MS);
|
||||
|
||||
return Number.isFinite(configured) && configured > 0
|
||||
? configured
|
||||
: DEFAULT_TOKEN_TTL_MS;
|
||||
}
|
||||
|
||||
export async function issueSessionToken(userId: string): Promise<IssuedSessionToken> {
|
||||
const token = randomBytes(32).toString('hex');
|
||||
const issuedAt = Date.now();
|
||||
const expiresAt = issuedAt + getSessionTokenTtlMs();
|
||||
const repo = getTokenRepository();
|
||||
|
||||
await repo.save(repo.create({
|
||||
token,
|
||||
userId,
|
||||
issuedAt,
|
||||
expiresAt
|
||||
}));
|
||||
|
||||
return { token, userId, issuedAt, expiresAt };
|
||||
}
|
||||
|
||||
export async function consumeSessionToken(token: string): Promise<AuthenticatedSession | null> {
|
||||
const normalized = token.trim();
|
||||
|
||||
if (!normalized) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const repo = getTokenRepository();
|
||||
const record = await repo.findOne({ where: { token: normalized } });
|
||||
|
||||
if (!record || record.expiresAt < Date.now()) {
|
||||
if (record) {
|
||||
await repo.delete({ token: normalized });
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
const user = await getUserById(record.userId);
|
||||
|
||||
if (!user) {
|
||||
await repo.delete({ token: normalized });
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
token: record.token,
|
||||
user,
|
||||
issuedAt: record.issuedAt,
|
||||
expiresAt: record.expiresAt
|
||||
};
|
||||
}
|
||||
|
||||
export async function revokeSessionToken(token: string): Promise<void> {
|
||||
const normalized = token.trim();
|
||||
|
||||
if (!normalized) {
|
||||
return;
|
||||
}
|
||||
|
||||
await getTokenRepository().delete({ token: normalized });
|
||||
}
|
||||
|
||||
export async function revokeAllSessionTokensForUser(userId: string): Promise<void> {
|
||||
await getTokenRepository().delete({ userId });
|
||||
}
|
||||
|
||||
export async function pruneExpiredSessionTokens(): Promise<void> {
|
||||
const repo = getTokenRepository();
|
||||
const now = Date.now();
|
||||
const expired = await repo.createQueryBuilder('token')
|
||||
.where('token.expiresAt < :now', { now })
|
||||
.getMany();
|
||||
|
||||
if (expired.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
await repo.remove(expired);
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import type { AuthUserPayload } from '../cqrs/types';
|
||||
|
||||
declare module 'express-serve-static-core' {
|
||||
interface Request {
|
||||
authUserId?: string;
|
||||
authUser?: AuthUserPayload;
|
||||
authToken?: string;
|
||||
}
|
||||
}
|
||||
|
||||
export {};
|
||||
@@ -1,106 +0,0 @@
|
||||
import {
|
||||
beforeEach,
|
||||
describe,
|
||||
expect,
|
||||
it
|
||||
} from 'vitest';
|
||||
import { WebSocket } from 'ws';
|
||||
import { connectedUsers } from './state';
|
||||
import { ConnectedUser } from './types';
|
||||
import {
|
||||
broadcastToServer,
|
||||
findUserByOderId,
|
||||
findVoiceActiveConnection
|
||||
} from './broadcast';
|
||||
|
||||
function createMockWs(): WebSocket & { sentMessages: string[] } {
|
||||
const sent: string[] = [];
|
||||
const ws = {
|
||||
readyState: WebSocket.OPEN,
|
||||
send: (data: string) => { sent.push(data); },
|
||||
close: () => {},
|
||||
sentMessages: sent
|
||||
} as unknown as WebSocket & { sentMessages: string[] };
|
||||
|
||||
return ws;
|
||||
}
|
||||
|
||||
function createConnectedUser(
|
||||
connectionId: string,
|
||||
oderId: string,
|
||||
overrides: Partial<ConnectedUser> = {}
|
||||
): ConnectedUser {
|
||||
const user: ConnectedUser = {
|
||||
oderId,
|
||||
ws: createMockWs(),
|
||||
authenticated: true,
|
||||
serverIds: new Set(['server-1']),
|
||||
displayName: 'Test User',
|
||||
lastPong: Date.now(),
|
||||
...overrides
|
||||
};
|
||||
|
||||
connectedUsers.set(connectionId, user);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
describe('broadcastToServer', () => {
|
||||
beforeEach(() => {
|
||||
connectedUsers.clear();
|
||||
});
|
||||
|
||||
it('delivers chat_message to every connection in the server except the sender connection', () => {
|
||||
createConnectedUser('conn-a1', 'user-1');
|
||||
const connA2 = createConnectedUser('conn-a2', 'user-1');
|
||||
const connB = createConnectedUser('conn-b', 'user-2');
|
||||
|
||||
broadcastToServer('server-1', { type: 'chat_message', text: 'hello' }, {
|
||||
excludeConnectionId: 'conn-a1'
|
||||
});
|
||||
|
||||
expect((connA2.ws as WebSocket & { sentMessages: string[] }).sentMessages).toHaveLength(1);
|
||||
expect((connB.ws as WebSocket & { sentMessages: string[] }).sentMessages).toHaveLength(1);
|
||||
expect(connectedUsers.get('conn-a1')?.ws).toBeDefined();
|
||||
expect((connectedUsers.get('conn-a1')!.ws as WebSocket & { sentMessages: string[] }).sentMessages).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('excludes every connection for an identity when excludeIdentityOderId is set', () => {
|
||||
const connA1 = createConnectedUser('conn-a1', 'user-1');
|
||||
const connA2 = createConnectedUser('conn-a2', 'user-1');
|
||||
const connB = createConnectedUser('conn-b', 'user-2');
|
||||
|
||||
broadcastToServer('server-1', { type: 'user_left', oderId: 'user-1' }, {
|
||||
excludeIdentityOderId: 'user-1'
|
||||
});
|
||||
|
||||
expect((connA1.ws as WebSocket & { sentMessages: string[] }).sentMessages).toHaveLength(0);
|
||||
expect((connA2.ws as WebSocket & { sentMessages: string[] }).sentMessages).toHaveLength(0);
|
||||
expect((connB.ws as WebSocket & { sentMessages: string[] }).sentMessages).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findVoiceActiveConnection', () => {
|
||||
beforeEach(() => {
|
||||
connectedUsers.clear();
|
||||
});
|
||||
|
||||
it('returns the connection marked voiceActive for the user', () => {
|
||||
createConnectedUser('conn-passive', 'user-1', { voiceActive: false });
|
||||
const active = createConnectedUser('conn-active', 'user-1', { voiceActive: true });
|
||||
|
||||
expect(findVoiceActiveConnection('user-1')).toBe(active);
|
||||
});
|
||||
|
||||
it('returns undefined when no voiceActive connection exists', () => {
|
||||
createConnectedUser('conn-1', 'user-1');
|
||||
|
||||
expect(findVoiceActiveConnection('user-1')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('findUserByOderId falls back to any open connection when no voiceActive connection exists', () => {
|
||||
const fallback = createConnectedUser('conn-1', 'user-1');
|
||||
|
||||
expect(findUserByOderId('user-1')).toBe(fallback);
|
||||
});
|
||||
});
|
||||
@@ -7,35 +7,19 @@ interface WsMessage {
|
||||
type: string;
|
||||
}
|
||||
|
||||
export interface BroadcastOptions {
|
||||
/** Skip only the sending WebSocket connection. */
|
||||
excludeConnectionId?: string;
|
||||
/** Skip every open connection for this identity (presence events). */
|
||||
excludeIdentityOderId?: string;
|
||||
}
|
||||
export function broadcastToServer(serverId: string, message: WsMessage, excludeOderId?: string): void {
|
||||
console.log(`Broadcasting to server ${serverId}, excluding ${excludeOderId}:`, message.type);
|
||||
|
||||
export function broadcastToServer(serverId: string, message: WsMessage, options?: BroadcastOptions): void {
|
||||
console.log(
|
||||
`Broadcasting to server ${serverId}, excluding connection ${options?.excludeConnectionId ?? 'none'} ` +
|
||||
`identity ${options?.excludeIdentityOderId ?? 'none'}:`,
|
||||
message.type
|
||||
);
|
||||
// Deduplicate by oderId so users with multiple connections (e.g. from
|
||||
// different signal URLs routing to the same server) receive the
|
||||
// broadcast only once.
|
||||
const sentToOderIds = new Set<string>();
|
||||
|
||||
connectedUsers.forEach((user, connectionId) => {
|
||||
if (
|
||||
!user.serverIds.has(serverId)
|
||||
|| connectionId === options?.excludeConnectionId
|
||||
|| (options?.excludeIdentityOderId && user.oderId === options.excludeIdentityOderId)
|
||||
|| user.ws.readyState !== WebSocket.OPEN
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
console.log(` -> Sending to ${user.displayName} (${user.oderId}) via ${connectionId}`);
|
||||
connectedUsers.forEach((user) => {
|
||||
if (user.serverIds.has(serverId) && user.oderId !== excludeOderId && !sentToOderIds.has(user.oderId)) {
|
||||
sentToOderIds.add(user.oderId);
|
||||
console.log(` -> Sending to ${user.displayName} (${user.oderId})`);
|
||||
user.ws.send(JSON.stringify(message));
|
||||
} catch (error) {
|
||||
console.warn(`Failed to broadcast ${message.type} to ${user.displayName ?? 'Unknown'} (${user.oderId})`, error);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -93,45 +77,7 @@ export function notifyUser(oderId: string, message: WsMessage): void {
|
||||
}
|
||||
}
|
||||
|
||||
export function notifyOtherConnectionsForOderId(
|
||||
oderId: string,
|
||||
message: WsMessage,
|
||||
excludeConnectionId?: string
|
||||
): void {
|
||||
connectedUsers.forEach((user, connectionId) => {
|
||||
if (
|
||||
connectionId === excludeConnectionId
|
||||
|| user.oderId !== oderId
|
||||
|| user.ws.readyState !== WebSocket.OPEN
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
user.ws.send(JSON.stringify(message));
|
||||
} catch (error) {
|
||||
console.warn(`Failed to notify ${user.displayName ?? 'Unknown'} (${user.oderId})`, error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function findUserByOderId(oderId: string) {
|
||||
return findVoiceActiveConnection(oderId) ?? findAnyConnectionForOderId(oderId);
|
||||
}
|
||||
|
||||
export function findVoiceActiveConnection(oderId: string): ConnectedUser | undefined {
|
||||
let voiceActiveMatch: ConnectedUser | undefined;
|
||||
|
||||
connectedUsers.forEach((user) => {
|
||||
if (user.oderId === oderId && user.voiceActive && user.ws.readyState === WebSocket.OPEN) {
|
||||
voiceActiveMatch = user;
|
||||
}
|
||||
});
|
||||
|
||||
return voiceActiveMatch;
|
||||
}
|
||||
|
||||
export function findAnyConnectionForOderId(oderId: string): ConnectedUser | undefined {
|
||||
let match: ConnectedUser | undefined;
|
||||
|
||||
connectedUsers.forEach((user) => {
|
||||
|
||||
@@ -1,120 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
expect,
|
||||
beforeEach,
|
||||
vi
|
||||
} from 'vitest';
|
||||
import { WebSocket } from 'ws';
|
||||
import { connectedUsers } from './state';
|
||||
import { ConnectedUser } from './types';
|
||||
import { handleWebSocketMessage } from './handler';
|
||||
|
||||
vi.mock('../services/server-access.service', () => ({
|
||||
authorizeWebSocketJoin: vi.fn(async () => ({ allowed: true as const })),
|
||||
findServerMembership: vi.fn(async () => ({ id: 'membership-1' })),
|
||||
usersShareServerMembership: vi.fn(async () => false)
|
||||
}));
|
||||
|
||||
vi.mock('../services/session-auth.service', () => ({
|
||||
consumeSessionToken: vi.fn(async (token: string) => {
|
||||
if (token !== 'valid-token') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
token,
|
||||
user: {
|
||||
id: 'user-1',
|
||||
username: 'alice',
|
||||
displayName: 'Alice',
|
||||
passwordHash: 'hash',
|
||||
createdAt: Date.now()
|
||||
},
|
||||
issuedAt: Date.now(),
|
||||
expiresAt: Date.now() + 60_000
|
||||
};
|
||||
})
|
||||
}));
|
||||
|
||||
function createMockWs(): WebSocket & { sentMessages: string[] } {
|
||||
const sent: string[] = [];
|
||||
const ws = {
|
||||
readyState: WebSocket.OPEN,
|
||||
send: (data: string) => { sent.push(data); },
|
||||
close: () => {},
|
||||
sentMessages: sent
|
||||
} as unknown as WebSocket & { sentMessages: string[] };
|
||||
|
||||
return ws;
|
||||
}
|
||||
|
||||
function createConnectedUser(connectionId: string): ConnectedUser {
|
||||
const ws = createMockWs();
|
||||
const user: ConnectedUser = {
|
||||
oderId: connectionId,
|
||||
ws,
|
||||
authenticated: false,
|
||||
serverIds: new Set(),
|
||||
displayName: 'Test User',
|
||||
lastPong: Date.now()
|
||||
};
|
||||
|
||||
connectedUsers.set(connectionId, user);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
describe('server websocket handler - authentication', () => {
|
||||
beforeEach(() => {
|
||||
connectedUsers.clear();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('rejects non-identify messages until the connection is authenticated', async () => {
|
||||
createConnectedUser('conn-1');
|
||||
|
||||
await handleWebSocketMessage('conn-1', { type: 'typing', serverId: 'server-1' });
|
||||
|
||||
const user = connectedUsers.get('conn-1');
|
||||
const sentMessages = (user?.ws as WebSocket & { sentMessages: string[] }).sentMessages;
|
||||
const response = JSON.parse(sentMessages[0]) as { type: string };
|
||||
|
||||
expect(response.type).toBe('auth_required');
|
||||
expect(user?.authenticated).toBe(false);
|
||||
});
|
||||
|
||||
it('rejects identify without a session token', async () => {
|
||||
createConnectedUser('conn-1');
|
||||
|
||||
await handleWebSocketMessage('conn-1', {
|
||||
type: 'identify',
|
||||
oderId: 'user-1',
|
||||
displayName: 'Alice'
|
||||
});
|
||||
|
||||
const user = connectedUsers.get('conn-1');
|
||||
const sentMessages = (user?.ws as WebSocket & { sentMessages: string[] }).sentMessages;
|
||||
const response = JSON.parse(sentMessages[0]) as { type: string; code: string };
|
||||
|
||||
expect(response.type).toBe('auth_error');
|
||||
expect(response.code).toBe('MISSING_TOKEN');
|
||||
expect(user?.authenticated).toBe(false);
|
||||
});
|
||||
|
||||
it('binds identify to the authenticated user id from the token', async () => {
|
||||
createConnectedUser('conn-1');
|
||||
|
||||
await handleWebSocketMessage('conn-1', {
|
||||
type: 'identify',
|
||||
token: 'valid-token',
|
||||
oderId: 'user-1',
|
||||
displayName: 'Alice'
|
||||
});
|
||||
|
||||
const user = connectedUsers.get('conn-1');
|
||||
|
||||
expect(user?.authenticated).toBe(true);
|
||||
expect(user?.oderId).toBe('user-1');
|
||||
});
|
||||
});
|
||||
@@ -1,223 +0,0 @@
|
||||
import {
|
||||
beforeEach,
|
||||
describe,
|
||||
expect,
|
||||
it,
|
||||
vi
|
||||
} from 'vitest';
|
||||
import { WebSocket } from 'ws';
|
||||
import { connectedUsers } from './state';
|
||||
import { ConnectedUser } from './types';
|
||||
import { handleWebSocketMessage } from './handler';
|
||||
|
||||
vi.mock('../services/server-access.service', () => ({
|
||||
authorizeWebSocketJoin: vi.fn(async () => ({ allowed: true as const })),
|
||||
findServerMembership: vi.fn(async () => ({ id: 'membership-1' })),
|
||||
usersShareServerMembership: vi.fn(async () => true)
|
||||
}));
|
||||
|
||||
vi.mock('../services/session-auth.service', () => ({
|
||||
consumeSessionToken: vi.fn(async (token: string) => {
|
||||
if (token !== 'test-token') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
token,
|
||||
user: {
|
||||
id: 'user-1',
|
||||
username: 'alice',
|
||||
displayName: 'Alice',
|
||||
passwordHash: 'hash',
|
||||
createdAt: Date.now()
|
||||
},
|
||||
issuedAt: Date.now(),
|
||||
expiresAt: Date.now() + 60_000
|
||||
};
|
||||
})
|
||||
}));
|
||||
|
||||
vi.mock('../services/plugin-support.service', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('../services/plugin-support.service')>();
|
||||
|
||||
return {
|
||||
...actual,
|
||||
getPluginRequirementsSnapshot: vi.fn(async () => ({
|
||||
requirements: [],
|
||||
eventDefinitions: []
|
||||
}))
|
||||
};
|
||||
});
|
||||
|
||||
function createMockWs(): WebSocket & { sentMessages: string[]; closeCalled: boolean } {
|
||||
const sent: string[] = [];
|
||||
const ws = {
|
||||
readyState: WebSocket.OPEN,
|
||||
send: (data: string) => { sent.push(data); },
|
||||
close: () => { ws.closeCalled = true; },
|
||||
terminate: () => { ws.closeCalled = true; },
|
||||
closeCalled: false,
|
||||
sentMessages: sent
|
||||
} as unknown as WebSocket & { sentMessages: string[]; closeCalled: boolean };
|
||||
|
||||
return ws;
|
||||
}
|
||||
|
||||
function createConnectedUser(
|
||||
connectionId: string,
|
||||
overrides: Partial<ConnectedUser> = {}
|
||||
): ConnectedUser {
|
||||
const ws = createMockWs();
|
||||
const user: ConnectedUser = {
|
||||
oderId: connectionId,
|
||||
ws,
|
||||
authenticated: false,
|
||||
serverIds: new Set(),
|
||||
displayName: 'Alice',
|
||||
lastPong: Date.now(),
|
||||
...overrides
|
||||
};
|
||||
|
||||
connectedUsers.set(connectionId, user);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
function getSentMessages(user: ConnectedUser): string[] {
|
||||
return (user.ws as WebSocket & { sentMessages: string[] }).sentMessages;
|
||||
}
|
||||
|
||||
describe('server websocket handler - multi-client sessions', () => {
|
||||
beforeEach(() => {
|
||||
connectedUsers.clear();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('relays voice_state to other connections for the same user', async () => {
|
||||
const sender = createConnectedUser('conn-a1', {
|
||||
authenticated: true,
|
||||
oderId: 'user-1',
|
||||
serverIds: new Set(['server-1']),
|
||||
clientInstanceId: 'device-a'
|
||||
});
|
||||
const passive = createConnectedUser('conn-a2', {
|
||||
authenticated: true,
|
||||
oderId: 'user-1',
|
||||
serverIds: new Set(['server-1']),
|
||||
clientInstanceId: 'device-b'
|
||||
});
|
||||
|
||||
getSentMessages(passive).length = 0;
|
||||
|
||||
await handleWebSocketMessage('conn-a1', {
|
||||
type: 'voice_state',
|
||||
serverId: 'server-1',
|
||||
voiceState: {
|
||||
isConnected: true,
|
||||
roomId: 'voice-1',
|
||||
serverId: 'server-1',
|
||||
clientInstanceId: 'device-a'
|
||||
}
|
||||
});
|
||||
|
||||
const messages = getSentMessages(passive).map((raw) => JSON.parse(raw) as { type: string });
|
||||
const voiceState = messages.find((message) => message.type === 'voice_state');
|
||||
|
||||
expect(voiceState).toBeDefined();
|
||||
expect(connectedUsers.get('conn-a1')?.voiceActive).toBe(true);
|
||||
expect(connectedUsers.get('conn-a2')?.voiceActive).toBeFalsy();
|
||||
});
|
||||
|
||||
it('forwards RTC offers to the voice-active connection for the target user', async () => {
|
||||
const sender = createConnectedUser('conn-sender', {
|
||||
authenticated: true,
|
||||
oderId: 'user-2',
|
||||
serverIds: new Set(['server-1'])
|
||||
});
|
||||
|
||||
createConnectedUser('conn-passive', {
|
||||
authenticated: true,
|
||||
oderId: 'user-1',
|
||||
serverIds: new Set(['server-1']),
|
||||
clientInstanceId: 'device-passive'
|
||||
});
|
||||
|
||||
const active = createConnectedUser('conn-active', {
|
||||
authenticated: true,
|
||||
oderId: 'user-1',
|
||||
serverIds: new Set(['server-1']),
|
||||
voiceActive: true,
|
||||
clientInstanceId: 'device-active'
|
||||
});
|
||||
|
||||
getSentMessages(active).length = 0;
|
||||
|
||||
await handleWebSocketMessage('conn-sender', {
|
||||
type: 'offer',
|
||||
targetUserId: 'user-1',
|
||||
serverId: 'server-1',
|
||||
payload: { sdp: { type: 'offer', sdp: 'v=0' } }
|
||||
});
|
||||
|
||||
const messages = getSentMessages(active).map((raw) => JSON.parse(raw) as { type: string });
|
||||
|
||||
expect(messages.some((message) => message.type === 'offer')).toBe(true);
|
||||
});
|
||||
|
||||
it('relays voice_client_takeover to other connections for the same user', async () => {
|
||||
createConnectedUser('conn-requester', {
|
||||
authenticated: true,
|
||||
oderId: 'user-1',
|
||||
serverIds: new Set(['server-1']),
|
||||
clientInstanceId: 'device-b'
|
||||
});
|
||||
|
||||
const active = createConnectedUser('conn-active', {
|
||||
authenticated: true,
|
||||
oderId: 'user-1',
|
||||
serverIds: new Set(['server-1']),
|
||||
voiceActive: true,
|
||||
clientInstanceId: 'device-a'
|
||||
});
|
||||
|
||||
getSentMessages(active).length = 0;
|
||||
|
||||
await handleWebSocketMessage('conn-requester', {
|
||||
type: 'voice_client_takeover',
|
||||
clientInstanceId: 'device-b'
|
||||
});
|
||||
|
||||
const messages = getSentMessages(active).map((raw) => JSON.parse(raw) as { type: string; clientInstanceId?: string });
|
||||
const takeover = messages.find((message) => message.type === 'voice_client_takeover');
|
||||
|
||||
expect(takeover?.clientInstanceId).toBe('device-b');
|
||||
});
|
||||
|
||||
it('evicts a stale connection with the same identity scope and client instance', async () => {
|
||||
const stale = createConnectedUser('conn-stale', {
|
||||
authenticated: true,
|
||||
oderId: 'user-1',
|
||||
connectionScope: 'ws://localhost:3001',
|
||||
clientInstanceId: 'device-a'
|
||||
});
|
||||
|
||||
createConnectedUser('conn-new', {
|
||||
authenticated: false,
|
||||
connectionScope: 'ws://localhost:3001',
|
||||
clientInstanceId: 'device-a'
|
||||
});
|
||||
|
||||
await handleWebSocketMessage('conn-new', {
|
||||
type: 'identify',
|
||||
token: 'test-token',
|
||||
oderId: 'user-1',
|
||||
displayName: 'Alice',
|
||||
connectionScope: 'ws://localhost:3001',
|
||||
clientInstanceId: 'device-a'
|
||||
});
|
||||
|
||||
expect(connectedUsers.has('conn-stale')).toBe(false);
|
||||
expect((stale.ws as WebSocket & { closeCalled: boolean }).closeCalled).toBe(true);
|
||||
expect(connectedUsers.get('conn-new')?.authenticated).toBe(true);
|
||||
});
|
||||
});
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user