Files
gitea/tests/e2e/events.test.ts
silverwind f9819dbb74 test(e2e): deterministically wait for event stream in logout propagation test (#38535)
The test raced the server-side SSE channel registration: a logout
emitted before registration is silently dropped ([example
flake](https://github.com/go-gitea/gitea/actions/runs/29699349255/job/88225654080)).
The 500ms wait from https://github.com/go-gitea/gitea/pull/37403 only
made this unlikely.

The server now registers the channel before sending the initial response
bytes, so an open connection implies registration. The shared worker
forwards the built-in `open` event (replaying it to late-attaching ports
via `EventSource.readyState`), the page exposes it as a
`data-user-events-connected` attribute, and the test waits for that
attribute instead of a fixed timeout.
2026-07-20 08:44:53 +00:00

82 lines
3.3 KiB
TypeScript

import {test, expect} from '@playwright/test';
import {loginUser, baseUrl, apiUserHeaders, apiCreateUser, apiCreateRepo, apiCreateIssue, apiStartStopwatch, timeoutFactor, randomString} from './utils.ts';
// These tests rely on a short EVENT_SOURCE_UPDATE_TIME in the e2e server config.
test.describe('events', () => {
test('notification count', 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)]);
// Create repo and login in parallel — repo is needed for the issue, login for the event stream
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();
// Create issue as another user — this generates a notification delivered via server push
await apiCreateIssue(request, {owner, repo: repoName, title: 'events notification test', headers: apiUserHeaders(commenter)});
// Wait for the notification badge to appear via server event
await expect(badge).toBeVisible({timeout: 15000 * timeoutFactor});
});
test('stopwatch', async ({page, request}) => {
const name = `ev-sw-${randomString(8)}`;
const headers = apiUserHeaders(name);
await apiCreateUser(request, name);
// Login in parallel with repo+issue+stopwatch setup (all independent after user exists)
await Promise.all([
loginUser(page, name),
(async () => {
await apiCreateRepo(request, {name, autoInit: false, headers});
await apiCreateIssue(request, {owner: name, repo: name, title: 'events stopwatch test', headers});
await apiStartStopwatch(request, name, name, 1, {headers});
})(),
]);
await page.goto('/');
// Verify stopwatch is visible and links to the correct issue
const stopwatch = page.locator('.active-stopwatch.not-mobile');
await expect(stopwatch).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 the server has registered page2's event stream, 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();
});
});