mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-01 04:50:28 +00:00
13d0f24423
* Closes #36942 * Fixes #19265 Replaces the SSE-based push channel (`/user/events`) with a WebSocket endpoint (`/-/ws`). ### What changes - **New `/-/ws` endpoint** (authenticated). One WebSocket per origin, shared across tabs via a single `SharedWorker`. - **Pubsub broker** (`services/pubsub`) for fan-out by topic, behind a `Broker` interface. `MemoryBroker` is the default (single process); a Redis backend is available for multi-process setups, configured via `[websocket].PUBSUB_TYPE` / `PUBSUB_CONN_STR`. The internal Gitea queue was not usable here because it has FIFO/single-consumer semantics. - **Push-only event production.** Events are emitted by write-triggered notifiers — `NotificationCountChange`, `PublishStopwatchesForUser`, and the logout publisher — wired into the existing `notify.Notifier` interface. No server-side pollers. - **Typed pub/sub on the client.** `web_src/js/modules/worker.ts` is a singleton transport; features subscribe per event type via `onUserEvent('notification-count', cb)` instead of branching on `event.data.type`. - **Wire contract** (`UserEventType` union) is shared between the worker and consumers via `web_src/js/types.ts`, kept in sync with `services/websocket/events.go`. - **Client-side periodic polling fallback** kicks in only when the WebSocket cannot be established (e.g. proxy blocks WS, browser lacks module-SharedWorker support). ### What's removed - `modules/eventsource` (SSE manager, run loop, messenger). - `/user/events` route and `tests/integration/eventsource_test.go`. - All server-side polling for stopwatches and notification counts. ### Stopwatch multi-tab fix The navbar stopwatch icon was previously rendered conditionally on `{{if $activeStopwatch}}`, so tabs loaded before the timer started had no DOM element to update. The icon and popup are now always rendered (toggled with `tw-hidden`), and the start/stop/cancel handlers POST silently so all open tabs reflect the change in real time. ### Deployment note WebSocket needs the upgrade headers to pass through a reverse proxy, e.g. for nginx: ```nginx proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; ``` Without them the WebSocket cannot be established, and after 3 consecutive failed opens the shared worker signals `push-unavailable`: the notification count and stopwatch fall back to periodic polling on the existing `[ui.notification]` timeouts. Real-time push is lost, the features keep working. The reverse-proxy docs need the same note (see the `docs-update-needed` label). --------- Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Epid <rexmrj@gmail.com>
139 lines
6.0 KiB
TypeScript
139 lines
6.0 KiB
TypeScript
import {test, expect} from '@playwright/test';
|
|
import {loginUser, baseUrl, apiUserHeaders, apiCreateUser, apiCreateRepo, apiCreateIssue, apiStartStopwatch, apiCancelStopwatch, apiCloseIssue, randomString} from './utils.ts';
|
|
|
|
// The /-/ws WebSocket pipeline is push-only: every event is fired by the server
|
|
// immediately on the DB write. These tests exercise that each event type
|
|
// (notification-count, stopwatches, logout) reaches a connected tab.
|
|
test.describe('events', () => {
|
|
test('notification count increases on new notification', async ({page, request}) => {
|
|
const owner = `ev-notif-owner-${randomString(8)}`;
|
|
const commenter = `ev-notif-commenter-${randomString(8)}`;
|
|
const repoName = `ev-notif-${randomString(8)}`;
|
|
|
|
await Promise.all([apiCreateUser(request, owner), apiCreateUser(request, commenter)]);
|
|
|
|
await Promise.all([
|
|
apiCreateRepo(request, {name: repoName, autoInit: false, headers: apiUserHeaders(owner)}),
|
|
loginUser(page, owner),
|
|
]);
|
|
await page.goto('/');
|
|
const badge = page.locator('a.not-mobile .notification_count');
|
|
await expect(badge).toBeHidden();
|
|
|
|
await expect(page.locator('html[data-user-events-connected]')).toBeAttached();
|
|
|
|
await apiCreateIssue(request, {owner, repo: repoName, title: 'events-notif', headers: apiUserHeaders(commenter)});
|
|
await expect(badge).toBeVisible();
|
|
});
|
|
|
|
test('stopwatch appears and hides via real-time push', async ({page, request}) => {
|
|
const name = `ev-sw-push-${randomString(8)}`;
|
|
const headers = apiUserHeaders(name);
|
|
|
|
await apiCreateUser(request, name);
|
|
await Promise.all([
|
|
loginUser(page, name),
|
|
(async () => {
|
|
await apiCreateRepo(request, {name, headers});
|
|
await apiCreateIssue(request, {owner: name, repo: name, title: 'events stopwatch push test', headers});
|
|
})(),
|
|
]);
|
|
// Page loads before the stopwatch starts — the icon is hidden in the rendered HTML
|
|
await page.goto('/');
|
|
const stopwatch = page.locator('.active-stopwatch.not-mobile');
|
|
// Element must exist in the DOM (just hidden); otherwise the push has nothing to reveal.
|
|
await expect(stopwatch).toHaveCount(1);
|
|
await expect(stopwatch).toBeHidden();
|
|
|
|
await expect(page.locator('html[data-user-events-connected]')).toBeAttached();
|
|
|
|
// Drive both directions from outside this tab; each push must reach it
|
|
await apiStartStopwatch(request, name, name, 1, {headers});
|
|
await expect(stopwatch).toBeVisible();
|
|
|
|
await apiCancelStopwatch(request, name, name, 1, {headers});
|
|
await expect(stopwatch).toBeHidden();
|
|
});
|
|
|
|
// Closing an issue stops the timer away from any stopwatch route handler.
|
|
test('stopwatch renders when already active and hides when the issue is closed', async ({page, request}) => {
|
|
const name = `ev-sw-close-${randomString(8)}`;
|
|
const headers = apiUserHeaders(name);
|
|
|
|
await apiCreateUser(request, name);
|
|
await Promise.all([
|
|
loginUser(page, name),
|
|
(async () => {
|
|
await apiCreateRepo(request, {name, autoInit: false, headers});
|
|
await apiCreateIssue(request, {owner: name, repo: name, title: 'events stopwatch close test', headers});
|
|
await apiStartStopwatch(request, name, name, 1, {headers});
|
|
})(),
|
|
]);
|
|
await page.goto('/');
|
|
const stopwatch = page.locator('.active-stopwatch.not-mobile');
|
|
await expect(stopwatch).toBeVisible();
|
|
await expect(page.locator('html[data-user-events-connected]')).toBeAttached();
|
|
|
|
await apiCloseIssue(request, name, name, 1, {headers});
|
|
await expect(stopwatch).toBeHidden();
|
|
});
|
|
|
|
// Repro for https://github.com/go-gitea/gitea/pull/36965#issuecomment-4321282667:
|
|
// clicking the sidebar "Start timer" button reportedly produced a blank page.
|
|
// Drives the actual UI button (not the API) so the link-action → JSONRedirect("")
|
|
// → fetchActionDoRedirect("") path is exercised end-to-end.
|
|
test('sidebar start timer button starts stopwatch without blanking the page', async ({page, request}) => {
|
|
const name = `ev-sw-ui-${randomString(8)}`;
|
|
const headers = apiUserHeaders(name);
|
|
|
|
await apiCreateUser(request, name);
|
|
await Promise.all([
|
|
loginUser(page, name),
|
|
(async () => {
|
|
await apiCreateRepo(request, {name, headers});
|
|
await apiCreateIssue(request, {owner: name, repo: name, title: 'sidebar start timer test', headers});
|
|
})(),
|
|
]);
|
|
await page.goto(`/${name}/${name}/issues/1`);
|
|
|
|
await page.getByRole('button', {name: 'Start timer'}).click();
|
|
|
|
// After the click the page reloads; the sidebar should now show the Stop/Discard
|
|
// controls and the navbar stopwatch icon should appear. If the page blanked,
|
|
// neither of these would be present.
|
|
await expect(page.getByRole('button', {name: 'Stop timer'})).toBeVisible();
|
|
await expect(page.getByRole('button', {name: 'Discard timer'})).toBeVisible();
|
|
await expect(page.locator('.active-stopwatch.not-mobile')).toBeVisible();
|
|
});
|
|
|
|
test('logout propagation', async ({browser, request}) => {
|
|
const name = `ev-logout-${randomString(8)}`;
|
|
|
|
await apiCreateUser(request, name);
|
|
|
|
// Use a single context so both pages share the same session and SharedWorker
|
|
const context = await browser.newContext({baseURL: baseUrl()});
|
|
const page1 = await context.newPage();
|
|
const page2 = await context.newPage();
|
|
|
|
await loginUser(page1, name);
|
|
|
|
// Navigate page2 so it connects to the shared event stream
|
|
await page2.goto('/');
|
|
|
|
// Verify page2 is logged in
|
|
await expect(page2.getByRole('link', {name: 'Sign In'})).toBeHidden();
|
|
// Wait until page2's event stream is connected, otherwise the logout event
|
|
// can race the connection and be silently dropped.
|
|
await expect(page2.locator('html[data-user-events-connected]')).toBeAttached();
|
|
|
|
// Logout from page1 — this sends a logout event to all tabs
|
|
await page1.goto('/user/logout');
|
|
|
|
// page2 should be redirected via the logout event
|
|
await expect(page2.getByRole('link', {name: 'Sign In'})).toBeVisible();
|
|
|
|
await context.close();
|
|
});
|
|
});
|