OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
test(dashboard): end-to-end suite across the whole dashboard
Carl-Gerhard Lindesvärde4b2e50e458e
diff --git a/apps/start/e2e/auth-helpers.ts b/apps/start/e2e/auth-helpers.tsnew file mode 100644index 000000000..92c749208--- /dev/null+++ b/apps/start/e2e/auth-helpers.ts@@ -0,0 +1,234 @@+import { execFileSync } from 'node:child_process';+import { createHmac } from 'node:crypto';+import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';+import { dirname } from 'node:path';+import type { Locator, Page, Response } from '@playwright/test';++export const FRESH_CONTEXT = { cookies: [], origins: [] };+export const SHOTS_DIR = 'test-results/auth/shots';++const TOTP_PERIOD_SECONDS = 30;+const TOTP_DIGITS = 6;+// RFC 4226 dynamic truncation: the low nibble picks the offset, the top bit is dropped.+const TOTP_OFFSET_MODULUS = 16;+const TOTP_SIGN_BIT = 2 ** 31;+const BASE32_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';++// The API allows 3 `auth.signInEmail` calls per 30s for the whole local stack+// (every loopback client shares one bucket) and answers the 4th with a 5 minute+// block that doubles on each repeat. Spacing our own attempts keeps a run under+// it; the timestamp lives in a file because a failed test restarts the worker.+const SIGN_IN_SPACING_MS = 11_000;+const SIGN_IN_CLOCK_FILE = 'test-results/auth/.last-sign-in';+const TOO_MANY_REQUESTS = 429;++export async function waitForSignInSlot(): Promise<void> {+ const lastSignInAt = existsSync(SIGN_IN_CLOCK_FILE)+ ? Number(readFileSync(SIGN_IN_CLOCK_FILE, 'utf8'))+ : 0;+ const wait = lastSignInAt + SIGN_IN_SPACING_MS - Date.now();+ if (wait > 0) {+ await new Promise((resolve) => setTimeout(resolve, wait));+ }+ mkdirSync(dirname(SIGN_IN_CLOCK_FILE), { recursive: true });+ writeFileSync(SIGN_IN_CLOCK_FILE, String(Date.now()));+}++/** Submits the /login form (the page must already be on it) and returns the API's answer. */+export async function submitSignIn(+ page: Page,+ credentials: { email: string; password: string }+): Promise<Response> {+ await waitForSignInSlot();+ await page.getByLabel('Email').fill(credentials.email);+ await page.getByLabel('Password').fill(credentials.password);+ const answer = page.waitForResponse((response) =>+ response.url().includes('/trpc/auth.signInEmail')+ );+ await page.getByRole('button', { name: 'Sign in', exact: true }).click();+ const response = await answer;+ if (response.status() === TOO_MANY_REQUESTS) {+ throw new Error(+ 'auth.signInEmail is rate limited (shared bucket). Wait for the block to expire and rerun.'+ );+ }+ return response;+}++export function toast(page: Page, text: string | RegExp): Locator {+ return page.locator('[data-sonner-toast]').filter({ hasText: text });+}++/** New accounts get a fixed "Share Your Feedback" card that covers the bottom-right corner. */+export async function dismissFeedbackPrompt(page: Page): Promise<void> {+ const prompt = page.locator('div.fixed').filter({+ has: page.getByRole('heading', { name: 'Share Your Feedback' }),+ });+ if (await prompt.count()) {+ await prompt.getByRole('button').first().click();+ await prompt.waitFor({ state: 'hidden' });+ }+}++export async function hasHorizontalOverflow(page: Page): Promise<boolean> {+ return page.evaluate(+ () => document.documentElement.scrollWidth > window.innerWidth+ );+}++/** Noise a local dev stack produces that says nothing about the page under test. */+const IGNORED_ISSUES = [+ /api\.openpanel\.dev/,+ /hydrat/i,+ /favicon/,+ // The `response` listener reports the same failure with its URL.+ /Failed to load resource/,+ /\/@vite|\/@id\/|\/src\/|node_modules/,+ /cdn\.userjot\.com/,+ /requires a `DialogTitle`|aria-describedby/,+];++export interface IssueLog {+ all: string[];+ /** Entries that are not known dev noise and not listed in `expected`. */+ unexpected(expected?: RegExp[]): string[];+}++export function watchIssues(page: Page): IssueLog {+ const all: string[] = [];+ page.on('console', (message) => {+ if (message.type() === 'error') {+ all.push(`console: ${message.text().slice(0, 300)}`);+ }+ });+ page.on('pageerror', (error) => all.push(`pageerror: ${error.message}`));+ page.on('response', (response) => {+ if (response.status() >= 400) {+ all.push(+ `response ${response.status()} ${response.request().method()} ${response.url().slice(0, 200)}`+ );+ }+ });+ return {+ all,+ unexpected: (expected = []) =>+ all.filter(+ (entry) =>+ !(+ IGNORED_ISSUES.some((pattern) => pattern.test(entry)) ||+ expected.some((pattern) => pattern.test(entry))+ )+ ),+ };+}++function decodeBase32(secret: string): Buffer {+ let bits = '';+ for (const char of secret.replace(/=+$/, '').toUpperCase()) {+ bits += BASE32_ALPHABET.indexOf(char).toString(2).padStart(5, '0');+ }+ const bytes = bits.match(/.{8}/g) ?? [];+ return Buffer.from(bytes.map((byte) => Number.parseInt(byte, 2)));+}++/** RFC 6238 with the app's parameters: SHA-1, 30 second step, 6 digits. */+export function totpCode(secret: string, atMs = Date.now()): string {+ const counter = Buffer.alloc(8);+ counter.writeBigUInt64BE(+ BigInt(Math.floor(atMs / 1000 / TOTP_PERIOD_SECONDS))+ );+ const digest = createHmac('sha1', decodeBase32(secret))+ .update(counter)+ .digest();+ const offset = (digest.at(-1) ?? 0) % TOTP_OFFSET_MODULUS;+ const binary = digest.readUInt32BE(offset) % TOTP_SIGN_BIT;+ return String(binary % 10 ** TOTP_DIGITS).padStart(TOTP_DIGITS, '0');+}++/** A syntactically valid code the server must reject. */+export function wrongTotpCode(secret: string): string {+ const valid = new Set(+ [-60_000, -30_000, 0, 30_000, 60_000].map((drift) =>+ totpCode(secret, Date.now() + drift)+ )+ );+ let candidate = 123_456;+ while (valid.has(String(candidate))) {+ candidate += 1;+ }+ return String(candidate);+}++export function apiUrl(): string {+ return (+ process.env.API_URL ??+ (process.env.DASHBOARD_URL ?? '').replace('://', '://api.')+ );+}++/** Sends one event through the real ingest pipeline, as a customer's server would. */+export function trackEvent(+ client: { id: string; secret: string },+ name: string+): string {+ return execFileSync('curl', [+ '-sSk',+ '-o',+ '/dev/null',+ '-w',+ '%{http_code}',+ '-X',+ 'POST',+ `${apiUrl()}/track`,+ '-H',+ 'content-type: application/json',+ '-H',+ `openpanel-client-id: ${client.id}`,+ '-H',+ `openpanel-client-secret: ${client.secret}`,+ '-H',+ 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Chrome/128.0 Safari/537.36',+ '-H',+ 'x-client-ip: 81.2.69.142',+ '-d',+ JSON.stringify({ type: 'track', payload: { name, properties: {} } }),+ ]).toString();+}++const HYDRATION_TIMEOUT_MS = 45_000;++function isHydrated(page: Page): Promise<unknown> {+ return page.waitForFunction(+ () => {+ const element = document.querySelector('button, input, a');+ return (+ element !== null &&+ Object.keys(element).some((key) => key.startsWith('__reactProps'))+ );+ },+ undefined,+ { timeout: HYDRATION_TIMEOUT_MS }+ );+}++/**+ * Pages are server-rendered: a click that lands before React hydrates is lost+ * (or submits a form natively). React tags hydrated nodes with `__reactProps$…`.+ * A dev server shared by several suites sometimes drops a module request and+ * the page never hydrates; one reload recovers it.+ */+export async function waitForHydration(page: Page): Promise<void> {+ await page.waitForLoadState('networkidle');+ try {+ await isHydrated(page);+ } catch {+ await page.reload();+ await page.waitForLoadState('networkidle');+ await isHydrated(page);+ }+}++export async function gotoHydrated(page: Page, url: string): Promise<void> {+ await page.goto(url);+ await waitForHydration(page);+}diff --git a/apps/start/e2e/auth-login.spec.ts b/apps/start/e2e/auth-login.spec.tsnew file mode 100644index 000000000..2e16cf6ab--- /dev/null+++ b/apps/start/e2e/auth-login.spec.ts@@ -0,0 +1,219 @@+// Sign in, sign out and the signed-out redirects. Every test starts without the+// shared auth state and signs in by itself, so the shared session is never touched.++import {+ FRESH_CONTEXT,+ gotoHydrated,+ hasHorizontalOverflow,+ SHOTS_DIR,+ submitSignIn,+ toast,+ waitForHydration,+ watchIssues,+} from './auth-helpers';+import { expect, test } from './fixtures';++test.use({ storageState: FRESH_CONTEXT });+// The dev server hydrates slowly when several suites share it.+test.describe.configure({ timeout: 120_000 });++const MOBILE_VIEWPORT = { width: 390, height: 844 };+const SIGN_IN_REQUEST = '/trpc/auth.signInEmail';++test('signed-out visitors are sent to /login from every protected route', async ({+ page,+ seed,+}) => {+ const issues = watchIssues(page);+ const protectedPaths = [+ '/',+ '/account',+ `/${seed.organizationId}`,+ `/${seed.organizationId}/account`,+ `/${seed.organizationId}/account/two-factor`,+ `/${seed.organizationId}/${seed.projects[0]?.id}/events?foo=1`,+ ];+ for (const path of protectedPaths) {+ await page.goto(path);+ await expect(page, `from ${path}`).toHaveURL(/\/login$/);+ }+ await expect(page.getByRole('heading', { name: 'Sign in' })).toBeVisible();++ // The onboarding steps send a signed-out visitor to the sign-up page instead.+ await page.goto('/onboarding/project');+ await expect(page).toHaveURL(/\/onboarding$/);+ await page.goto(`/onboarding/${seed.projects[0]?.id}/connect`);+ await expect(page).toHaveURL(/\/onboarding$/);+ await page.goto(`/onboarding/${seed.projects[0]?.id}/verify`);+ await expect(page).toHaveURL(/\/onboarding$/);++ expect(issues.unexpected()).toEqual([]);+});++test('the login form validates before it calls the API', async ({ page }) => {+ const issues = watchIssues(page);+ let signInRequests = 0;+ page.on('request', (request) => {+ if (request.url().includes(SIGN_IN_REQUEST)) {+ signInRequests += 1;+ }+ });+ await gotoHydrated(page, '/login');++ await page.getByRole('button', { name: 'Sign in', exact: true }).click();+ await expect(page.getByText('Issues')).toHaveCount(2);+ await page.getByText('Issues').first().hover();+ await expect(page.getByRole('tooltip')).toContainText(+ 'Invalid email address'+ );++ await page.getByLabel('Email').fill('not-an-email');+ await page.getByLabel('Password').fill('short');+ await page.getByLabel('Password').press('Enter');+ await expect(page.getByText('Issues')).toHaveCount(2);++ await page.getByLabel('Email').fill('[email protected]');+ await page.getByLabel('Password').fill('long-enough-password');+ await page.getByLabel('Email').press('Tab');+ await expect(page.getByText('Issues')).toHaveCount(0);++ await expect(+ page.getByRole('link', { name: 'Create one today' })+ ).toHaveAttribute('href', '/onboarding');+ expect(signInRequests).toBe(0);+ await expect(page).toHaveURL(/\/login$/);+ expect(issues.unexpected()).toEqual([]);+});++test('a wrong password and an unknown email are both refused', async ({+ page,+ seed,+}) => {+ const issues = watchIssues(page);+ await gotoHydrated(page, '/login');++ const wrongPassword = await submitSignIn(page, {+ email: seed.user.email,+ password: 'definitely-not-the-password',+ });+ expect(wrongPassword.status()).toBe(401);+ await expect(toast(page, 'Incorrect email or password')).toBeVisible();+ await expect(page).toHaveURL(/\/login$/);+ await page.screenshot({ path: `${SHOTS_DIR}/login-wrong-password.png` });++ const unknownEmail = await submitSignIn(page, {+ email: `nobody-${Date.now()}@example.com`,+ password: 'definitely-not-the-password',+ });+ expect(unknownEmail.status()).toBeGreaterThanOrEqual(400);+ await expect(page).toHaveURL(/\/login$/);+ expect(+ (await page.context().cookies()).map((cookie) => cookie.name)+ ).not.toContain('session');+ expect(issues.unexpected([/auth\.signInEmail/])).toEqual([]);+});++test('sign in, land on the organization, then log out from the profile menu', async ({+ page,+ context,+ seed,+ request,+}) => {+ const issues = watchIssues(page);+ await gotoHydrated(page, '/login');+ await expect(page.getByText('Used last time')).toHaveCount(0);++ const response = await submitSignIn(page, seed.user);+ expect(response.status()).toBe(200);+ // One organization: `/` forwards straight to it.+ await expect(page).toHaveURL(new RegExp(`/${seed.organizationId}$`), {+ timeout: 20_000,+ });++ const cookies = await context.cookies();+ const session = cookies.find((cookie) => cookie.name === 'session');+ expect(session).toMatchObject({+ httpOnly: true,+ secure: true,+ sameSite: 'Lax',+ path: '/',+ });+ const THIRTY_DAYS_IN_SECONDS = 60 * 60 * 24 * 30;+ expect(session?.expires).toBeGreaterThan(+ Date.now() / 1000 + THIRTY_DAYS_IN_SECONDS - 120+ );+ expect(+ cookies.find((cookie) => cookie.name === 'last-auth-provider')?.value+ ).toBe('email');++ // The pages for signed-out visitors forward a signed-in one to the app.+ for (const path of ['/login', '/onboarding', '/reset-password?token=x']) {+ await page.goto(path);+ await expect(page, `from ${path}`).toHaveURL(+ new RegExp(`/${seed.organizationId}$`)+ );+ }++ await waitForHydration(page);+ await page.getByRole('button', { name: 'Profile' }).click();+ await expect(page.getByRole('menuitem', { name: 'Account' })).toBeVisible();+ await page.getByRole('menuitem', { name: 'Logout' }).click();+ await expect(page).toHaveURL(/\/login$/);++ // The provider hint survives the logout, the session does not.+ await expect(page.getByText('Used last time')).toBeVisible();+ await page.screenshot({ path: `${SHOTS_DIR}/login-used-last-time.png` });+ const afterLogout = (await context.cookies()).map((cookie) => cookie.name);+ expect(afterLogout).not.toContain('session');+ await page.goto(`/${seed.organizationId}`);+ await expect(page).toHaveURL(/\/login$/);++ // The server forgot the session too: replaying the old cookie is anonymous.+ const replay = await request.get(+ `${new URL(response.url()).origin}/trpc/auth.session`,+ { headers: { cookie: `session=${session?.value}` } }+ );+ expect((await replay.json()).result.data.json.userId).toBeNull();+ expect(issues.unexpected()).toEqual([]);+});++test('the login and sign-up pages fit a phone screen', async ({ page }) => {+ await page.setViewportSize(MOBILE_VIEWPORT);+ for (const path of ['/login', '/onboarding']) {+ await gotoHydrated(page, path);+ expect(await hasHorizontalOverflow(page), path).toBe(false);+ await page.screenshot({+ path: `${SHOTS_DIR}/mobile${path.replace('/', '-')}.png`,+ fullPage: true,+ });+ }+ await page.goto('/login');+ await expect(+ page.getByRole('button', { name: 'Sign in', exact: true })+ ).toBeInViewport();+});++// BUG "Sign-in before hydration puts the password in the URL": the forms are+// server-rendered without `method="post"`, so until React hydrates a submit is+// a native GET to /login?email=…&password=….+test('a submit before hydration never puts the password in the URL', async ({+ page,+ baseURL,+}) => {+ const dashboardHost = new URL(baseURL ?? '').hostname;+ // The document and its inline scripts arrive, the client bundle does not:+ // the state every visitor is in until hydration finishes.+ await page.route(+ (url) => url.hostname === dashboardHost,+ (route) =>+ route.request().resourceType() === 'script'+ ? route.abort()+ : route.continue()+ );+ await page.goto('/login');+ await page.getByLabel('Email').fill('[email protected]');+ await page.getByLabel('Password').fill('super-secret-password');+ await page.getByRole('button', { name: 'Sign in', exact: true }).click();+ await page.waitForLoadState('load');+ expect(page.url()).not.toContain('super-secret-password');+});diff --git a/apps/start/e2e/auth-onboarding.spec.ts b/apps/start/e2e/auth-onboarding.spec.tsnew file mode 100644index 000000000..fd04c5b8c--- /dev/null+++ b/apps/start/e2e/auth-onboarding.spec.ts@@ -0,0 +1,908 @@+// One brand-new user from sign-up to account deletion: onboarding (project,+// connect, verify), the account pages, two-factor and password reset.+//+// The tests run in order and hand each other the user through a state file+// rather than module variables: a failed test restarts the worker, and the+// tests that document a known bug are expected to fail without taking the rest+// of the journey down with them.++import { execFileSync } from 'node:child_process';+import { existsSync, readFileSync, writeFileSync } from 'node:fs';+import type { BrowserContext, Page } from '@playwright/test';+import {+ apiUrl,+ FRESH_CONTEXT,+ gotoHydrated,+ hasHorizontalOverflow,+ SHOTS_DIR,+ submitSignIn,+ toast,+ totpCode,+ trackEvent,+ waitForHydration,+ watchIssues,+ wrongTotpCode,+} from './auth-helpers';+import { expect, test } from './fixtures';++test.use({ storageState: FRESH_CONTEXT });+// The dev server hydrates slowly when several suites share it.+test.describe.configure({ timeout: 120_000 });++const STATE_FILE = 'test-results/auth/onboarding-state.json';+const RUN_ID = Date.now();+const MOBILE_VIEWPORT = { width: 390, height: 844 };+const DESKTOP_VIEWPORT = { width: 1280, height: 720 };+const RECOVERY_CODE_COUNT = 10;+const RECOVERY_CODE_PATTERN = /[A-Z2-9]{5}-[A-Z2-9]{5}/g;+const TOTP_SECRET_PATTERN = /[A-Z2-7]{32}/;+const FRAMEWORK_COUNT = 15;+const EVENT_ARRIVAL_TIMEOUT_MS = 45_000;++interface ProjectState {+ id: string;+ clientId: string;+ clientSecret: string;+}++interface JourneyState {+ email: string;+ password: string;+ cookies: Awaited<ReturnType<BrowserContext['cookies']>>;+ organizationId?: string;+ organizationName?: string;+ webProject?: ProjectState;+ serverProject?: ProjectState;+ totpSecret?: string;+ recoveryCodes?: string[];+ deleted?: boolean;+}++function readState(): JourneyState {+ if (!existsSync(STATE_FILE)) {+ throw new Error(+ 'The sign-up test did not create the user; see its failure.'+ );+ }+ return JSON.parse(readFileSync(STATE_FILE, 'utf8')) as JourneyState;+}++function writeState(patch: Partial<JourneyState>): JourneyState {+ const next = { ...(existsSync(STATE_FILE) ? readState() : {}), ...patch };+ writeFileSync(STATE_FILE, JSON.stringify(next, null, 2));+ return next as JourneyState;+}++/** Continue as the journey's user: its cookies, and the feedback card already dismissed. */+async function continueAsUser(+ context: BrowserContext,+ baseURL: string | undefined+): Promise<JourneyState> {+ const state = readState();+ await context.addCookies([+ ...state.cookies,+ {+ name: 'feedback-prompt-seen',+ value: new Date().toISOString(),+ url: baseURL ?? '',+ },+ ]);+ return state;+}++async function rememberCookies(context: BrowserContext): Promise<void> {+ writeState({ cookies: await context.cookies() });+}++function otpInput(scope: Page | ReturnType<Page['getByRole']>) {+ return scope.locator('input[data-input-otp]');+}++function resetTokenFor(email: string): string {+ const databaseUrl = (process.env.DATABASE_URL ?? '').split('?')[0] ?? '';+ return execFileSync('psql', [+ databaseUrl,+ '-Atc',+ `select rp.id from reset_password rp join accounts a on a.id = rp."accountId" join users u on u.id = a."userId" where u.email = '${email}'`,+ ])+ .toString()+ .trim();+}++function canReadDatabase(): boolean {+ if (!process.env.DATABASE_URL) {+ return false;+ }+ try {+ execFileSync('psql', ['--version']);+ return true;+ } catch {+ return false;+ }+}++test('sign-up validates every field before it calls the API', async ({+ page,+}) => {+ const issues = watchIssues(page);+ let signUpRequests = 0;+ page.on('request', (request) => {+ if (request.url().includes('/trpc/auth.signUpEmail')) {+ signUpRequests += 1;+ }+ });+ await gotoHydrated(page, '/onboarding');+ await expect(+ page.getByRole('heading', { name: 'Start tracking in minutes' })+ ).toBeVisible();+ await expect(page.getByRole('link', { name: 'Sign in' })).toHaveAttribute(+ 'href',+ '/login'+ );++ const submit = page.getByRole('button', { name: 'Create account' });+ await submit.click();+ await expect(page.getByText('Issues')).toHaveCount(5);++ await page.getByLabel('First name').fill('E2E');+ await page.getByLabel('Last name').fill('Auth');+ await page.getByLabel('Email').fill(`e2e-auth-${RUN_ID}@example.com`);+ await page.getByLabel('Password', { exact: true }).fill('short');+ await page.getByLabel('Confirm password').fill('short');+ await submit.click();+ await expect(page.getByText('Issues')).toHaveCount(2);++ await page.getByLabel('Password', { exact: true }).fill('long-enough-1');+ await page.getByLabel('Confirm password').fill('long-enough-2');+ await submit.click();+ await expect(page.getByText('Issues')).toHaveCount(1);+ await page.getByText('Issues').hover();+ await expect(page.getByRole('tooltip')).toContainText(+ 'Passwords do not match'+ );+ await page.screenshot({ path: `${SHOTS_DIR}/sign-up-validation.png` });++ expect(signUpRequests).toBe(0);+ expect(issues.unexpected()).toEqual([]);+});++test('sign up creates the account and opens the project step', async ({+ page,+ context,+}) => {+ const issues = watchIssues(page);+ const email = `e2e-auth-${RUN_ID}@example.com`;+ const password = `pw-${RUN_ID}-first`;+ await gotoHydrated(page, '/onboarding');+ await page.getByLabel('First name').fill('E2E');+ await page.getByLabel('Last name').fill('Auth');+ await page.getByLabel('Email').fill(email);+ await page.getByLabel('Password', { exact: true }).fill(password);+ await page.getByLabel('Confirm password').fill(password);+ await page.getByLabel('Confirm password').press('Enter');++ // No organization yet: `/` forwards to the project step.+ await expect(page).toHaveURL(/\/onboarding\/project$/, { timeout: 20_000 });+ const cookies = await context.cookies();+ expect(cookies.find((cookie) => cookie.name === 'session')).toMatchObject({+ httpOnly: true,+ secure: true,+ sameSite: 'Lax',+ });+ writeState({ email, password, cookies });+ expect(issues.unexpected()).toEqual([]);+});++test('signing up again with the same email is refused', async ({ page }) => {+ const state = readState();+ await gotoHydrated(page, '/onboarding');+ await page.getByLabel('First name').fill('E2E');+ await page.getByLabel('Last name').fill('Twin');+ await page.getByLabel('Email').fill(state.email);+ await page.getByLabel('Password', { exact: true }).fill('another-password');+ await page.getByLabel('Confirm password').fill('another-password');+ await page.getByRole('button', { name: 'Create account' }).click();+ await expect(toast(page, 'User already exists')).toBeVisible();+ await expect(page).toHaveURL(/\/onboarding$/);+});++test('the project step validates the workspace, the name, the type and the domain', async ({+ page,+ context,+ baseURL,+}) => {+ await continueAsUser(context, baseURL);+ const issues = watchIssues(page);+ let projectRequests = 0;+ page.on('request', (request) => {+ if (request.url().includes('/trpc/onboarding.project')) {+ projectRequests += 1;+ }+ });+ await gotoHydrated(page, '/onboarding/project');+ await expect(page.getByText('Create project')).toBeVisible();+ // Nothing to reuse yet.+ await expect(+ page.getByRole('button', { name: 'Use existing workspace' })+ ).toBeDisabled();++ const next = page.getByRole('button', { name: 'Next' });+ await next.click();+ await expect(+ page.getByText('At least one type must be selected')+ ).toBeVisible();+ await expect(page.getByText('Issues')).toHaveCount(2);++ await page.getByLabel('Workspace name').fill(`E2E auth ${RUN_ID}`);+ await page.getByLabel('Your first project name').fill('ab');+ await page.getByText('Website', { exact: true }).click();+ await next.click();+ // Too short a name, and a website needs its domain.+ await expect(page.getByText('Issues')).toHaveCount(2);+ await page.screenshot({+ path: `${SHOTS_DIR}/onboarding-project-invalid.png`,+ });++ // App and backend projects have no domain to ask for.+ await page.getByText('Website', { exact: true }).click();+ await expect(page.getByLabel('Domain')).toBeHidden();+ await page.getByText('App', { exact: true }).click();+ await next.click();+ await expect(page.getByText('Issues')).toHaveCount(1);++ expect(projectRequests).toBe(0);+ expect(issues.unexpected()).toEqual([]);+});++test('the timezone picker searches and keeps the choice', async ({+ page,+ context,+ baseURL,+}) => {+ await continueAsUser(context, baseURL);+ await gotoHydrated(page, '/onboarding/project');+ const timezone = page.getByRole('combobox').first();+ // Defaults to the browser's zone.+ await expect(timezone).toHaveText(/\w+\/\w+|UTC/);+ await timezone.click();+ await page.getByPlaceholder(/search/i).fill('Tokyo');+ await expect(page.getByRole('option')).toHaveCount(1);+ await page.getByRole('option', { name: 'Asia/Tokyo' }).click();+ await expect(timezone).toHaveText('Asia/Tokyo');++ await timezone.click();+ await page.getByPlaceholder(/search/i).fill('UTC');+ await page.getByRole('option', { name: 'UTC', exact: true }).click();+ await expect(timezone).toHaveText('UTC');++ await timezone.click();+ await page.getByPlaceholder(/search/i).fill('no-such-zone');+ await expect(page.getByRole('option')).toHaveCount(0);+});++test('a website project leads to the connect step with its one-time credentials', async ({+ page,+ context,+ baseURL,+}) => {+ await continueAsUser(context, baseURL);+ const issues = watchIssues(page);+ const organizationName = `E2E auth ${RUN_ID}`;+ await gotoHydrated(page, '/onboarding/project');+ await page.getByLabel('Workspace name').fill(organizationName);+ await page.getByRole('combobox').first().click();+ await page.getByPlaceholder(/search/i).fill('UTC');+ await page.getByRole('option', { name: 'UTC', exact: true }).click();+ await page+ .getByLabel('Your first project name')+ .fill(`E2E auth web ${RUN_ID}`);+ await page.getByText('Website', { exact: true }).click();++ // A bare host becomes an origin, and that origin is allowed by default.+ const domain = `e2e-auth-${RUN_ID}.example.com`;+ await page.getByLabel('Domain').fill(domain);+ await page.getByLabel('Domain').blur();+ await expect(page.getByLabel('Domain')).toHaveValue(`https://${domain}`);+ await page+ .getByText('will be allowed. Do you want to allow any other?')+ .click();+ await expect(+ page.getByText(`https://${domain}`, { exact: true }).last()+ ).toBeVisible();+ await page.screenshot({+ path: `${SHOTS_DIR}/onboarding-project-website.png`,+ });++ const created = page.waitForResponse((response) =>+ response.url().includes('/trpc/onboarding.project')+ );+ await page.getByRole('button', { name: 'Next' }).click();+ const client = (await (await created).json()).result.data.json;+ await expect(page).toHaveURL(+ new RegExp(`/onboarding/${client.projectId}/connect$`)+ );+ await expect(page.getByText('Client credentials')).toBeVisible();+ writeState({+ organizationId: client.organizationId,+ organizationName,+ webProject: {+ id: client.projectId,+ clientId: client.id,+ clientSecret: client.secret,+ },+ });++ await expect(page.getByText(client.id).first()).toBeVisible();+ await expect(page.getByText(client.secret)).toBeVisible();+ await expect(+ page.getByText(btoa(`${client.id}:${client.secret}`))+ ).toBeVisible();+ await expect(page.locator('code').first()).toContainText(+ `clientId: '${client.id}'`+ );+ await expect(+ page.locator('button').filter({ has: page.locator('.font-semibold') })+ ).toHaveCount(FRAMEWORK_COUNT);+ await page.screenshot({+ path: `${SHOTS_DIR}/onboarding-connect.png`,+ fullPage: true,+ });++ // Each framework opens its instructions next to the page.+ for (const framework of ['Next.js', 'Rest API', 'iOS (swift)']) {+ await page.getByRole('button', { name: framework, exact: true }).click();+ const sheet = page.getByRole('dialog');+ await expect(sheet.locator(`iframe[title="${framework}"]`)).toBeVisible();+ await sheet.getByRole('button', { name: 'Close' }).first().click();+ await expect(sheet).toBeHidden();+ }++ // The secret is stored hashed: after a reload in a new tab it cannot be shown again.+ await page.evaluate(() => sessionStorage.clear());+ await page.reload();+ await expect(page.getByText('Client credentials')).toBeVisible();+ await expect(page.getByText(client.secret)).toHaveCount(0);+ await expect(page.getByText('is only shown once')).toBeVisible();+ expect(issues.unexpected()).toEqual([]);+});++// BUG "`LinkButton href` renders a link to the current page": TanStack's+// `Link` builds its href from `to`, so `href` is dropped and `target="_blank"`+// links open the page you are already on.+test('"More details" in the framework instructions links to the docs', async ({+ page,+ context,+ baseURL,+}) => {+ const state = await continueAsUser(context, baseURL);+ await gotoHydrated(page, `/onboarding/${state.webProject?.id}/connect`);+ await page.getByRole('button', { name: 'Next.js', exact: true }).click();+ await expect(+ page.getByRole('dialog').getByRole('link', { name: 'More details' })+ ).toHaveAttribute('href', /^https:\/\/openpanel\.dev\/docs/);+});++test('the verify step turns green when the first event arrives', async ({+ page,+ context,+ baseURL,+}) => {+ const state = await continueAsUser(context, baseURL);+ const project = state.webProject;+ if (!project) {+ throw new Error('The website project was not created; see its failure.');+ }+ const issues = watchIssues(page);+ await gotoHydrated(page, `/onboarding/${project.id}/connect`);+ await page.getByRole('link', { name: 'Next' }).click();+ await expect(page).toHaveURL(new RegExp(`/onboarding/${project.id}/verify$`));+ await expect(page.getByText('Waiting for events')).toBeVisible();+ await expect(page.getByRole('link', { name: 'Skip for now' })).toBeVisible();++ await page.getByRole('button', { name: 'No events received?' }).click();+ await expect(page.getByText('Ensure client ID is correct')).toBeVisible();+ await expect(page.getByText(project.clientId).first()).toBeVisible();+ await page.getByRole('button', { name: 'Personal curl example' }).click();+ await expect(page.locator('code').last()).toContainText(+ `openpanel-client-id: ${project.clientId}`+ );+ await page.screenshot({+ path: `${SHOTS_DIR}/onboarding-verify-waiting.png`,+ fullPage: true,+ });++ // Back and forward keep the step.+ await page.getByRole('link', { name: 'Back' }).click();+ await expect(page).toHaveURL(/\/connect$/);+ await page.goBack();+ await expect(page).toHaveURL(/\/verify$/);+ await expect(page.getByText('Waiting for events')).toBeVisible();++ expect(+ trackEvent(+ { id: project.clientId, secret: project.clientSecret },+ 'e2e_auth_first_event'+ )+ ).toBe('200');+ await expect(page.getByText('Successfully connected')).toBeVisible({+ timeout: EVENT_ARRIVAL_TIMEOUT_MS,+ });+ await expect(page.getByText('e2e_auth_first_event')).toBeVisible();+ await expect(page.getByRole('link', { name: 'Skip for now' })).toHaveCount(0);+ await page.screenshot({+ path: `${SHOTS_DIR}/onboarding-verify-connected.png`,+ });++ await page.getByRole('link', { name: 'Your dashboard' }).click();+ await expect(page).toHaveURL(+ new RegExp(`/${state.organizationId}/${project.id}$`)+ );+ expect(issues.unexpected()).toEqual([]);+});++test('an app and backend project can be added to the existing workspace', async ({+ page,+ context,+ baseURL,+}) => {+ const state = await continueAsUser(context, baseURL);+ const issues = watchIssues(page);+ await gotoHydrated(page, '/onboarding/project');+ // With a workspace in hand, reusing it is the default.+ await expect(page.getByLabel('Workspace name')).toHaveCount(0);+ await page.getByRole('combobox').click();+ await page.getByRole('option', { name: state.organizationName }).click();+ await page+ .getByLabel('Your first project name')+ .fill(`E2E auth server ${RUN_ID}`);+ await page.getByText('App', { exact: true }).click();+ await page.getByText('Backend / API', { exact: true }).click();+ await expect(page.getByLabel('Domain')).toBeHidden();++ const created = page.waitForResponse((response) =>+ response.url().includes('/trpc/onboarding.project')+ );+ await page.getByRole('button', { name: 'Next' }).click();+ const client = (await (await created).json()).result.data.json;+ expect(client.organizationId).toBe(state.organizationId);+ await expect(page.getByText('Client credentials')).toBeVisible();+ await expect(page.getByText(client.secret)).toBeVisible();+ writeState({+ serverProject: {+ id: client.projectId,+ clientId: client.id,+ clientSecret: client.secret,+ },+ });++ await page.getByRole('link', { name: 'Next' }).click();+ await expect(page).toHaveURL(/\/verify$/);+ await page.getByRole('button', { name: 'Personal curl example' }).click();+ // A backend project gets a server-side example, with the real secret while it is known.+ const example = page.locator('code').last();+ await expect(example).toContainText('"name":"test_event"');+ await expect(example).toContainText(+ `openpanel-client-secret: ${client.secret}`+ );++ await page.getByRole('link', { name: 'Skip for now' }).click();+ await expect(page).toHaveURL(+ new RegExp(`/${state.organizationId}/${client.projectId}$`)+ );+ expect(issues.unexpected()).toEqual([]);+});++test('the onboarding steps fit a phone screen', async ({+ page,+ context,+ baseURL,+}) => {+ const state = await continueAsUser(context, baseURL);+ await page.setViewportSize(MOBILE_VIEWPORT);+ for (const [name, path] of [+ ['project', '/onboarding/project'],+ ['connect', `/onboarding/${state.webProject?.id}/connect`],+ ['verify', `/onboarding/${state.webProject?.id}/verify`],+ ['account', `/${state.organizationId}/account`],+ ['two-factor', `/${state.organizationId}/account/two-factor`],+ ] as const) {+ await gotoHydrated(page, path);+ expect(await hasHorizontalOverflow(page), path).toBe(false);+ await page.screenshot({+ path: `${SHOTS_DIR}/mobile-${name}.png`,+ fullPage: true,+ });+ }+});++test('the profile tab edits the name and keeps it after a reload', async ({+ page,+ context,+ baseURL,+}) => {+ const state = await continueAsUser(context, baseURL);+ const issues = watchIssues(page);+ // `/account` finds the organization by itself.+ await gotoHydrated(page, '/account');+ await expect(page).toHaveURL(new RegExp(`/${state.organizationId}/account$`));+ await expect(page.getByLabel('Email')).toHaveValue(state.email);+ await expect(page.getByLabel('Email')).toBeDisabled();+ const save = page.getByRole('button', { name: 'Save' });+ await expect(save).toBeDisabled();++ await page.getByLabel('First name').fill('Renamed');+ await page.getByLabel('Last name').fill('Tester');+ await expect(save).toBeEnabled();+ await page.getByLabel('Last name').press('Enter');+ await expect(toast(page, 'Profile updated')).toBeVisible();+ await expect(save).toBeDisabled();++ await page.reload();+ await expect(page.getByLabel('First name')).toHaveValue('Renamed');+ await expect(page.getByLabel('Last name')).toHaveValue('Tester');+ await page.screenshot({ path: `${SHOTS_DIR}/account-profile.png` });+ expect(issues.unexpected()).toEqual([]);+});++// BUG "Profile form shows a raw validation dump for an empty name": the form's+// own schema accepts '', the API does not, and the toast prints the Zod issues.+test('an empty first name is refused with a readable message', async ({+ page,+ context,+ baseURL,+}) => {+ const state = await continueAsUser(context, baseURL);+ await gotoHydrated(page, `/${state.organizationId}/account`);+ await page.getByLabel('First name').fill('');+ await page.getByRole('button', { name: 'Save' }).click();+ await expect(page.getByText('Issues').or(toast(page, /./))).toBeVisible();+ await expect(page.getByText('"code": "too_small"')).toHaveCount(0);+});++test('email preferences save per category and survive a reload', async ({+ page,+ context,+ baseURL,+}) => {+ const state = await continueAsUser(context, baseURL);+ const issues = watchIssues(page);+ await gotoHydrated(page, `/${state.organizationId}/account`);+ await page.getByRole('tab', { name: 'Email preferences' }).click();+ await expect(page).toHaveURL(/\/account\/email-preferences$/);++ const switches = page.getByRole('switch');+ await expect(switches.first()).toBeChecked();+ const categories = await switches.count();+ expect(categories).toBeGreaterThan(1);+ const save = page.getByRole('button', { name: 'Save' });+ await expect(save).toBeDisabled();++ await switches.first().click();+ await switches.last().click();+ await save.click();+ await expect(toast(page, 'Email preferences updated')).toBeVisible();++ await page.reload();+ await expect(switches.first()).not.toBeChecked();+ await expect(switches.last()).not.toBeChecked();+ await expect(switches.nth(1)).toBeChecked();+ await page.screenshot({ path: `${SHOTS_DIR}/account-email-preferences.png` });++ await switches.first().click();+ await switches.last().click();+ await save.click();+ await expect(toast(page, 'Email preferences updated')).toBeVisible();+ await page.reload();+ await expect(switches.first()).toBeChecked();+ expect(issues.unexpected()).toEqual([]);+});++// BUG "A wrong two-factor code throws the user out of the page": the API+// answers a wrong code with 401, and the dashboard treats every 401 as "session+// gone" and hard-navigates to /login.+test('a mistyped code while enabling two-factor keeps the setup open', async ({+ page,+ context,+ baseURL,+}) => {+ const state = await continueAsUser(context, baseURL);+ await gotoHydrated(page, `/${state.organizationId}/account/two-factor`);+ await page.getByRole('button', { name: 'Enable' }).click();+ const dialog = page.getByRole('dialog');+ await expect(dialog.getByAltText('Authenticator QR code')).toBeVisible();+ const secret =+ (await dialog.innerText()).match(TOTP_SECRET_PATTERN)?.[0] ?? '';+ await otpInput(dialog).fill(wrongTotpCode(secret));+ await dialog.getByRole('button', { name: 'Enable' }).click();+ await expect(toast(page, 'Invalid code')).toBeVisible();+ await expect(dialog.getByAltText('Authenticator QR code')).toBeVisible();+ await expect(page).toHaveURL(/\/account\/two-factor$/);+});++test('two-factor can be enabled and its recovery codes regenerated', async ({+ page,+ context,+ baseURL,+}) => {+ const state = await continueAsUser(context, baseURL);+ const issues = watchIssues(page);+ await gotoHydrated(page, `/${state.organizationId}/account`);+ await page.getByRole('tab', { name: 'Two-factor auth' }).click();+ await expect(page).toHaveURL(/\/account\/two-factor$/);+ await expect(+ page.getByText('Two-factor authentication is disabled.')+ ).toBeVisible();++ await page.getByRole('button', { name: 'Enable' }).click();+ const dialog = page.getByRole('dialog');+ await expect(dialog.getByAltText('Authenticator QR code')).toBeVisible();+ const enable = dialog.getByRole('button', { name: 'Enable' });+ await expect(enable).toBeDisabled();+ const secret = (await dialog.innerText()).match(TOTP_SECRET_PATTERN)?.[0];+ if (!secret) {+ throw new Error('The setup modal did not show a secret to type manually.');+ }+ await page.screenshot({ path: `${SHOTS_DIR}/two-factor-setup.png` });++ await otpInput(dialog).fill(totpCode(secret));+ await enable.click();+ await expect(dialog.getByText('Save your recovery codes')).toBeVisible();+ const firstCodes: string[] =+ (await dialog.innerText()).match(RECOVERY_CODE_PATTERN) ?? [];+ expect(new Set(firstCodes).size).toBe(RECOVERY_CODE_COUNT);+ await expect(dialog.getByRole('button', { name: 'Download' })).toBeVisible();+ await expect(dialog.getByRole('button', { name: 'Copy all' })).toBeVisible();+ await page.screenshot({ path: `${SHOTS_DIR}/two-factor-recovery-codes.png` });+ writeState({ totpSecret: secret, recoveryCodes: firstCodes });++ await dialog.getByRole('button', { name: "I've saved my codes" }).click();+ await expect(+ page.getByText('Two-factor authentication is enabled.')+ ).toBeVisible();+ await expect(+ page.getByText(`${RECOVERY_CODE_COUNT} recovery codes remaining`)+ ).toBeVisible();++ await page.getByRole('button', { name: 'Regenerate recovery codes' }).click();+ const regenerate = dialog.getByRole('button', { name: 'Regenerate' });+ await expect(regenerate).toBeDisabled();+ await otpInput(dialog).fill(totpCode(secret));+ await regenerate.click();+ await expect(dialog.getByText('New recovery codes')).toBeVisible();+ const newCodes =+ (await dialog.innerText()).match(RECOVERY_CODE_PATTERN) ?? [];+ expect(new Set(newCodes).size).toBe(RECOVERY_CODE_COUNT);+ expect(newCodes.some((code) => firstCodes.includes(code))).toBe(false);+ writeState({ recoveryCodes: newCodes });+ await dialog.getByRole('button', { name: 'Done' }).click();+ await expect(dialog).toBeHidden();+ await page.screenshot({ path: `${SHOTS_DIR}/two-factor-enabled.png` });+ expect(issues.unexpected()).toEqual([]);+});++// BUG "A wrong two-factor code throws the user out of the page", on /verify:+// the visitor lands back on /login without ever seeing "Invalid code".+test('a mistyped code on /verify says so and lets the visitor try again', async ({+ page,+}) => {+ const state = readState();+ test.skip(!state.totpSecret, 'two-factor was not enabled');+ await gotoHydrated(page, '/login');+ await submitSignIn(page, state);+ await expect(page).toHaveURL(/\/verify$/);+ await waitForHydration(page);+ await otpInput(page).fill(wrongTotpCode(state.totpSecret ?? ''));+ await expect(toast(page, 'Invalid code')).toBeVisible();+ await expect(page).toHaveURL(/\/verify$/);+});++test('signing in asks for the authenticator code', async ({+ page,+ context,+}) => {+ const state = readState();+ test.skip(!state.totpSecret, 'two-factor was not enabled');+ const issues = watchIssues(page);++ // Without a pending challenge there is nothing to verify.+ await gotoHydrated(page, '/verify');+ await expect(+ page.getByRole('heading', { name: 'Two-factor authentication' })+ ).toBeVisible();+ await expect(page.getByRole('button', { name: 'Verify' })).toBeDisabled();+ await expect(+ page.getByRole('link', { name: 'Sign in with a different account' })+ ).toHaveAttribute('href', '/login');++ await page.goto('/login');+ await waitForHydration(page);+ await submitSignIn(page, state);+ await expect(page).toHaveURL(/\/verify$/);+ // The password alone is not a session.+ expect((await context.cookies()).map((cookie) => cookie.name)).not.toContain(+ 'session'+ );+ await page.goto(`/${state.organizationId}`);+ await expect(page).toHaveURL(/\/login$/);++ await gotoHydrated(page, '/verify');+ await page.screenshot({ path: `${SHOTS_DIR}/two-factor-verify.png` });+ await page+ .getByRole('button', { name: 'Use a recovery code instead' })+ .click();+ await expect(page.getByPlaceholder('ABCDE-FGHIJ')).toBeVisible();+ await page+ .getByRole('button', { name: 'Use authenticator app instead' })+ .click();+ // Six digits submit by themselves.+ await otpInput(page).fill(totpCode(state.totpSecret ?? ''));+ await expect(page).toHaveURL(new RegExp(`/${state.organizationId}$`), {+ timeout: 20_000,+ });+ expect((await context.cookies()).map((cookie) => cookie.name)).toContain(+ 'session'+ );+ expect(issues.unexpected()).toEqual([]);+});++test('a recovery code signs in once and is then used up', async ({+ page,+ context,+}) => {+ const state = readState();+ const recoveryCode = state.recoveryCodes?.[0];+ test.skip(!recoveryCode, 'two-factor was not enabled');+ const issues = watchIssues(page);+ await gotoHydrated(page, '/login');+ await submitSignIn(page, state);+ await expect(page).toHaveURL(/\/verify$/);+ await waitForHydration(page);+ await page+ .getByRole('button', { name: 'Use a recovery code instead' })+ .click();+ // Typed in lower case: codes are case-insensitive.+ await page+ .getByPlaceholder('ABCDE-FGHIJ')+ .fill((recoveryCode ?? '').toLowerCase());+ await page.getByPlaceholder('ABCDE-FGHIJ').press('Enter');+ await expect(page).toHaveURL(new RegExp(`/${state.organizationId}$`), {+ timeout: 20_000,+ });+ await rememberCookies(context);++ await page.goto(`/${state.organizationId}/account/two-factor`);+ await expect(+ page.getByText(`${RECOVERY_CODE_COUNT - 1} recovery codes remaining`)+ ).toBeVisible();++ // The same code is refused the second time (asked through the API: see the 401 bug above).+ const reuse = await page.request.post(`${apiUrl()}/trpc/auth.totpDisable`, {+ data: { json: { code: recoveryCode } },+ });+ expect(reuse.status()).toBe(401);+ expect((await reuse.json()).error.json.message).toBe('Invalid code');+ expect(issues.unexpected()).toEqual([]);+});++test('two-factor can be disabled with an authenticator code', async ({+ page,+ context,+ baseURL,+}) => {+ const state = await continueAsUser(context, baseURL);+ test.skip(!state.totpSecret, 'two-factor was not enabled');+ const issues = watchIssues(page);+ await gotoHydrated(page, `/${state.organizationId}/account/two-factor`);+ await page.getByRole('button', { name: 'Disable' }).click();+ const dialog = page.getByRole('dialog');+ const disable = dialog.getByRole('button', { name: 'Disable' });+ await expect(disable).toBeDisabled();+ await dialog.getByRole('button', { name: 'Cancel' }).click();+ await expect(dialog).toBeHidden();++ await page.getByRole('button', { name: 'Disable' }).click();+ await dialog+ .getByPlaceholder('123456 or ABCDE-FGHIJ')+ .fill(totpCode(state.totpSecret ?? ''));+ await disable.click();+ await expect(toast(page, 'Two-factor authentication disabled')).toBeVisible();+ await expect(+ page.getByText('Two-factor authentication is disabled.')+ ).toBeVisible();+ writeState({ totpSecret: undefined, recoveryCodes: undefined });+ expect(issues.unexpected()).toEqual([]);+});++test('a reset link sets a new password and works only once', async ({+ page,+}) => {+ test.skip(+ !canReadDatabase(),+ 'needs psql and DATABASE_URL to read the token'+ );+ const state = readState();+ const issues = watchIssues(page);+ await gotoHydrated(page, '/login');+ await page.getByLabel('Email').fill(state.email);+ await page.getByRole('button', { name: 'Forgot password?' }).click();+ const dialog = page.getByRole('dialog');+ await expect(dialog.getByPlaceholder('Your email address')).toHaveValue(+ state.email+ );+ await dialog.getByRole('button', { name: 'Continue' }).click();+ await expect(+ toast(page, 'You should receive an email shortly!')+ ).toBeVisible();++ const token = resetTokenFor(state.email);+ expect(token).toMatch(/^pw_/);+ const newPassword = `pw-${RUN_ID}-second`;+ await gotoHydrated(page, `/reset-password?token=${token}`);+ await page.getByLabel('New password').fill(newPassword);+ await page.getByRole('button', { name: 'Reset password' }).click();+ await expect(toast(page, 'Password reset successfully')).toBeVisible();+ await expect(page).toHaveURL(/\/login$/);+ writeState({ password: newPassword });++ await waitForHydration(page);+ const response = await submitSignIn(page, {+ email: state.email,+ password: newPassword,+ });+ expect(response.status()).toBe(200);+ await expect(page).toHaveURL(new RegExp(`/${state.organizationId}$`), {+ timeout: 20_000,+ });++ const reuse = await page.request.post(`${apiUrl()}/trpc/auth.resetPassword`, {+ data: { json: { token, password: 'yet-another-password' } },+ });+ expect(reuse.status()).toBe(404);+ expect(issues.unexpected()).toEqual([]);+});++test('deleting the account needs the typed confirmation and ends the session', async ({+ page,+ context,+ baseURL,+ request,+}) => {+ const state = await continueAsUser(context, baseURL);+ const issues = watchIssues(page);+ const sessionCookie = state.cookies.find(+ (cookie) => cookie.name === 'session'+ );+ try {+ await page.setViewportSize(DESKTOP_VIEWPORT);+ await gotoHydrated(page, `/${state.organizationId}/account`);+ await page.getByRole('button', { name: 'Delete account' }).click();+ const dialog = page.getByRole('dialog');+ const confirm = dialog.getByRole('button', { name: 'Delete account' });+ await expect(confirm).toBeDisabled();+ await dialog.getByPlaceholder('DELETE').fill('delete');+ await expect(confirm).toBeDisabled();+ await dialog.getByRole('button', { name: 'Cancel' }).click();+ await expect(dialog).toBeHidden();++ await page.getByRole('button', { name: 'Delete account' }).click();+ await dialog.getByPlaceholder('DELETE').fill('DELETE');+ await page.screenshot({ path: `${SHOTS_DIR}/account-delete-confirm.png` });+ await confirm.click();+ await expect(page).toHaveURL(/\/login$/, { timeout: 20_000 });+ writeState({ deleted: true });+ } finally {+ if (!readState().deleted) {+ // Never leave the throwaway user behind when the modal is what broke.+ await request.post(`${apiUrl()}/trpc/user.delete`, {+ data: {},+ headers: { cookie: `session=${sessionCookie?.value}` },+ });+ }+ }++ const session = await request.get(`${apiUrl()}/trpc/auth.session`, {+ headers: { cookie: `session=${sessionCookie?.value}` },+ });+ expect((await session.json()).result.data.json.userId).toBeNull();+ expect(issues.unexpected()).toEqual([]);+});diff --git a/apps/start/e2e/auth-recovery.spec.ts b/apps/start/e2e/auth-recovery.spec.tsnew file mode 100644index 000000000..a5bffb60a--- /dev/null+++ b/apps/start/e2e/auth-recovery.spec.ts@@ -0,0 +1,149 @@+// Forgot password, /reset-password and /unsubscribe as a signed-out visitor.+// No email is delivered here; the reset that needs a real token is in+// auth-onboarding.spec.ts, which reads it from the database.++import {+ FRESH_CONTEXT,+ gotoHydrated,+ SHOTS_DIR,+ toast,+ watchIssues,+} from './auth-helpers';+import { expect, test } from './fixtures';++test.use({ storageState: FRESH_CONTEXT });+// The dev server hydrates slowly when several suites share it.+test.describe.configure({ timeout: 90_000 });++const RESET_REQUEST = '/trpc/auth.resetPassword';+const UNKNOWN_TOKEN = 'pw_e2e-does-not-exist';++test('the forgot password modal validates and answers the same for an unknown email', async ({+ page,+}) => {+ const issues = watchIssues(page);+ await gotoHydrated(page, '/login');+ const typedEmail = `nobody-${Date.now()}@example.com`;+ await page.getByLabel('Email').fill(typedEmail);++ await page.getByRole('button', { name: 'Forgot password?' }).click();+ const dialog = page.getByRole('dialog');+ await expect(+ dialog.getByRole('heading', { name: 'Request password reset' })+ ).toBeVisible();+ const email = dialog.getByPlaceholder('Your email address');+ // The address already typed in the login form is carried over.+ await expect(email).toHaveValue(typedEmail);++ await email.fill('not-an-email');+ await dialog.getByRole('button', { name: 'Continue' }).click();+ await expect(dialog.getByText('Issues')).toBeVisible();+ await page.screenshot({ path: `${SHOTS_DIR}/forgot-password-invalid.png` });++ await dialog.getByRole('button', { name: 'Cancel' }).click();+ await expect(dialog).toBeHidden();++ await page.getByRole('button', { name: 'Forgot password?' }).click();+ await email.fill(typedEmail);+ await email.press('Enter');+ // Never "no such user": the endpoint must not reveal who has an account.+ await expect(+ toast(page, 'You should receive an email shortly!')+ ).toBeVisible();+ await expect(dialog).toBeHidden();+ expect(issues.unexpected()).toEqual([]);+});++test('/reset-password without a token explains what is wrong', async ({+ page,+}) => {+ await page.goto('/reset-password');+ await expect(page.getByText('Missing reset password token')).toBeVisible();+ await expect(+ page.getByRole('button', { name: 'Reset password' })+ ).toHaveCount(0);+});++test('/reset-password refuses an unknown token and keeps the visitor on the page', async ({+ page,+}) => {+ const issues = watchIssues(page);+ await gotoHydrated(page, `/reset-password?token=${UNKNOWN_TOKEN}`);+ await expect(+ page.getByRole('heading', { name: 'Reset your password' })+ ).toBeVisible();+ await expect(page.getByRole('link', { name: 'Sign in' })).toHaveAttribute(+ 'href',+ '/login'+ );++ await page.getByLabel('New password').fill('a-perfectly-fine-password');+ const answer = page.waitForResponse((response) =>+ response.url().includes(RESET_REQUEST)+ );+ await page.getByLabel('New password').press('Enter');+ expect((await answer).status()).toBe(404);+ await expect(toast(page, 'Reset password not found')).toBeVisible();+ await expect(page).toHaveURL(/\/reset-password\?token=/);+ expect(issues.unexpected([/auth\.resetPassword/])).toEqual([]);+});++// BUG "Reset password form swallows validation errors": the input is rendered+// without the `error` prop, so a too-short password makes the button do nothing.+test('/reset-password tells the visitor when the new password is too short', async ({+ page,+}) => {+ let resetRequests = 0;+ page.on('request', (request) => {+ if (request.url().includes(RESET_REQUEST)) {+ resetRequests += 1;+ }+ });+ await gotoHydrated(page, `/reset-password?token=${UNKNOWN_TOKEN}`);+ await page.getByLabel('New password').fill('short');+ await page.getByRole('button', { name: 'Reset password' }).click();+ expect(resetRequests).toBe(0);+ await expect(page.getByText('Issues')).toBeVisible();+});++test('/unsubscribe with missing or malformed parameters shows a friendly page', async ({+ page,+}) => {+ for (const query of [+ '',+ '?email=a%40example.com',+ '?email=not-an-email&category=onboarding&token=x',+ ]) {+ await page.goto(`/unsubscribe${query}`);+ await expect(page.getByText('Link not valid'), query).toBeVisible();+ await expect(page.getByText('"code"')).toHaveCount(0);+ }+ await page.screenshot({ path: `${SHOTS_DIR}/unsubscribe-invalid.png` });+});++test('/unsubscribe with a forged token refuses and says so', async ({+ page,+}) => {+ const issues = watchIssues(page);+ await gotoHydrated(+ page,+ '/unsubscribe?email=a%40example.com&category=onboarding&token=forged'+ );+ await expect(+ page.getByText('Unsubscribe from Onboarding emails?')+ ).toBeVisible();+ await expect(page.getByText('[email protected]')).toBeVisible();++ const answer = page.waitForResponse((response) =>+ response.url().includes('/trpc/email.unsubscribe')+ );+ await page.getByRole('button', { name: 'Confirm Unsubscribe' }).click();+ expect((await answer).status()).toBe(400);+ await expect(page.getByText('Invalid unsubscribe link')).toBeVisible();+ await expect(page.getByText('Unsubscribed', { exact: true })).toHaveCount(0);+ expect(issues.unexpected([/email\.unsubscribe/])).toEqual([]);++ // Cancel leaves for the app, which sends a signed-out visitor to /login.+ await page.getByRole('link', { name: 'Cancel' }).click();+ await expect(page).toHaveURL(/\/login$/);+});diff --git a/apps/start/e2e/auth-session.spec.ts b/apps/start/e2e/auth-session.spec.tsnew file mode 100644index 000000000..fb0f044cb--- /dev/null+++ b/apps/start/e2e/auth-session.spec.ts@@ -0,0 +1,100 @@+// How the `session` cookie is read: a bad one, two of them, and the password+// that produced it.++import {+ apiUrl,+ FRESH_CONTEXT,+ gotoHydrated,+ SHOTS_DIR,+ submitSignIn,+ waitForSignInSlot,+} from './auth-helpers';+import { expect, test } from './fixtures';++test.use({ storageState: FRESH_CONTEXT });+// The dev server hydrates slowly when several suites share it.+test.describe.configure({ timeout: 90_000 });++const STALE_SESSION_TOKEN = 'stalestalestalestalestalestale12';++test('an invalid session cookie is treated as signed out, not as an error', async ({+ page,+ context,+ baseURL,+ seed,+ request,+}) => {+ await context.addCookies([+ { name: 'session', value: 'not-a-session', url: baseURL ?? '' },+ ]);+ await page.goto(`/${seed.organizationId}`);+ await expect(page).toHaveURL(/\/login$/);+ await expect(page.getByRole('heading', { name: 'Sign in' })).toBeVisible();++ const session = await request.get(`${apiUrl()}/trpc/auth.session`, {+ headers: { cookie: 'session=not-a-session' },+ });+ expect(session.status()).toBe(200);+ expect((await session.json()).result.data.json.userId).toBeNull();++ const guarded = await request.get(`${apiUrl()}/trpc/organization.list`, {+ headers: { cookie: 'session=not-a-session' },+ });+ expect(guarded.status()).toBe(401);+});++// BUG "A stale `session` cookie ahead of a valid one locks the user out": the+// API reads only the first `session` value of the Cookie header.+test('a stale session cookie next to a fresh one does not block signing in', async ({+ page,+ context,+ baseURL,+ seed,+}) => {+ // Host-only, as a build without a cookie domain set it. Sign-in writes its+ // cookie with `Domain=`, so the browser keeps both and sends the older first.+ await context.addCookies([+ { name: 'session', value: STALE_SESSION_TOKEN, url: baseURL ?? '' },+ ]);+ await gotoHydrated(page, '/login');+ const response = await submitSignIn(page, seed.user);+ expect(response.status()).toBe(200);+ await page.screenshot({ path: `${SHOTS_DIR}/session-two-cookies.png` });+ await expect(page).toHaveURL(new RegExp(`/${seed.organizationId}$`), {+ timeout: 15_000,+ });+});++// BUG "A password with surrounding whitespace can be set but never used":+// sign-up and reset hash the password as typed, sign-in trims it first.+test('a password with a trailing space signs in after signing up with it', async ({+ request,+}) => {+ const email = `e2e-auth-space-${Date.now()}@example.com`;+ const password = 'spaced-password-1 ';+ const signUp = await request.post(`${apiUrl()}/trpc/auth.signUpEmail`, {+ data: {+ json: {+ firstName: 'E2E',+ lastName: 'Auth',+ email,+ password,+ confirmPassword: password,+ },+ },+ });+ expect(signUp.status()).toBe(200);+ try {+ await waitForSignInSlot();+ const signIn = await request.post(`${apiUrl()}/trpc/auth.signInEmail`, {+ data: { json: { email, password } },+ });+ expect(signIn.status()).toBe(200);+ } finally {+ // `request` kept the sign-up's session cookie; the account removes itself.+ const cleanup = await request.post(`${apiUrl()}/trpc/user.delete`, {+ data: {},+ });+ expect(cleanup.status()).toBe(200);+ }+});diff --git a/apps/start/e2e/data-events.spec.ts b/apps/start/e2e/data-events.spec.tsnew file mode 100644index 000000000..9f27b0567--- /dev/null+++ b/apps/start/e2e/data-events.spec.ts@@ -0,0 +1,404 @@+import type { Page } from '@playwright/test';+import { expect as baseExpect, test } from './fixtures';++// The dev server shares a machine with other suites; default timeouts are too tight.+const expect = baseExpect.configure({ timeout: 30_000 });+test.describe.configure({ timeout: 240_000 });++const SHOTS = 'test-results/data/shots';+const SHOP = '/acme/acme-shop';+const NOISE = [+ /api\.openpanel\.dev/,+ /favicon/,+ /sentry/i,+ /\[vite\]/,+ /Failed to load resource/,+ // The sidebar's AI composer input (not this surface) differs in a `style` attribute.+ /A tree hydrated but some attributes/,+ /requires a `DialogTitle`/,+ /unique "key" prop/,+ // Asserted on its own in 'first visit hydrates without a mismatch'.+ /Hydration failed/,+];++function watch(page: Page) {+ const problems: string[] = [];+ page.on('console', (message) => {+ if (message.type() !== 'error') {+ return;+ }+ const text = message.text();+ if (!NOISE.some((pattern) => pattern.test(text))) {+ problems.push(`console: ${text.slice(0, 300)}`);+ }+ });+ page.on('pageerror', (error) => {+ if (!NOISE.some((pattern) => pattern.test(error.message))) {+ problems.push(`pageerror: ${error.message.slice(0, 300)}`);+ }+ });+ page.on('response', (response) => {+ const url = response.url();+ if (response.status() >= 400 && !NOISE.some((p) => p.test(url))) {+ problems.push(`http ${response.status()}: ${url.slice(0, 200)}`);+ }+ });+ return problems;+}++async function open(page: Page, path: string) {+ await page.context().addCookies([+ {+ name: 'feedback-prompt-seen',+ value: new Date().toISOString(),+ url: process.env.DASHBOARD_URL ?? 'http://localhost:3000',+ },+ ]);+ // The project switcher only gets its label once the client has hydrated.+ const hydrated = page.getByRole('combobox').first();+ for (const attempt of [1, 2, 3]) {+ await page.goto(path);+ await page.waitForLoadState('networkidle');+ try {+ await expect(hydrated).toContainText('Acme', { timeout: 45_000 });+ return;+ } catch (error) {+ if (attempt === 3) {+ throw error;+ }+ }+ }+}++const rows = (page: Page) => page.locator('[data-index]');++test.describe('events list', () => {+ test('renders rows without page errors', async ({ page }) => {+ const problems = watch(page);+ await open(page, `${SHOP}/events`);+ await expect(page).toHaveURL(/\/events\/events/);+ await expect(rows(page).first()).toBeVisible();+ expect(await rows(page).count()).toBeGreaterThan(10);+ await page.screenshot({ path: `${SHOTS}/events-list.png` });+ expect(problems).toEqual([]);+ });++ test('first visit hydrates without a mismatch', async ({ page }) => {+ const hydrationErrors: string[] = [];+ page.on('pageerror', (error) => {+ if (error.message.includes('Hydration failed')) {+ hydrationErrors.push(error.message.slice(0, 200));+ }+ });+ await open(page, `${SHOP}/events`);+ await expect(rows(page).first()).toBeVisible();+ expect(hydrationErrors).toEqual([]);+ });++ test('scrolling to the bottom loads the next page', async ({ page }) => {+ await open(page, `${SHOP}/events`);+ await expect(rows(page).first()).toBeVisible();+ const nextPage = page.waitForResponse(+ (response) =>+ response.url().includes('event.events') &&+ response.url().includes('cursor') &&+ response.status() === 200+ );+ await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));+ await nextPage;+ await expect+ .poll(() =>+ rows(page).evaluateAll((elements) =>+ Math.max(+ ...elements.map((el) => Number(el.getAttribute('data-index')))+ )+ )+ )+ .toBeGreaterThan(49);+ });++ test('event-name filter narrows the list and survives reload', async ({+ page,+ }) => {+ const problems = watch(page);+ await open(page, `${SHOP}/events`);+ await page.getByRole('button', { name: 'Filters' }).click();+ await page.screenshot({ path: `${SHOTS}/events-filters-sheet.png` });+ await page+ .getByRole('combobox')+ .filter({ hasText: 'Select event' })+ .click();+ await page+ .getByPlaceholder(/search/i)+ .last()+ .fill('purchase');+ await page+ .getByRole('option', { name: /purchase/ })+ .first()+ .click();+ await page.keyboard.press('Escape');+ await page.keyboard.press('Escape');+ await expect(page).toHaveURL(/events=purchase/);+ await page.waitForLoadState('networkidle');+ await expect(rows(page).first().locator('button[title]')).toBeVisible();+ await page.screenshot({ path: `${SHOTS}/events-filtered.png` });+ const names = await rows(page).locator('button[title]').allInnerTexts();+ expect(names.length).toBeGreaterThan(0);+ expect(new Set(names)).toEqual(new Set(['purchase']));++ await page.reload();+ await page.waitForLoadState('networkidle');+ await expect(rows(page).first().locator('button[title]')).toBeVisible();+ const afterReload = await rows(page)+ .locator('button[title]')+ .allInnerTexts();+ expect(new Set(afterReload)).toEqual(new Set(['purchase']));+ expect(problems).toEqual([]);+ });++ test('details modal shows properties and links to the profile', async ({+ page,+ }) => {+ const problems = watch(page);+ await open(page, `${SHOP}/events/events?events=purchase`);+ await rows(page).first().locator('button[title="purchase"]').click();+ const dialog = page.getByRole('dialog');+ await expect(dialog.getByText('Properties')).toBeVisible();+ await expect(dialog.getByText('Information')).toBeVisible();+ await expect(dialog.getByText('All events for purchase')).toBeVisible();+ await page.waitForLoadState('networkidle');+ await page.screenshot({ path: `${SHOTS}/event-details.png` });++ await dialog.getByRole('button', { name: 'Detailed' }).click();+ await expect(dialog.getByText('Session Id')).toBeVisible();+ await dialog.getByRole('button', { name: 'View JSON' }).click();+ await expect(+ dialog.getByRole('button', { name: 'View Table' })+ ).toBeVisible();+ await page.screenshot({ path: `${SHOTS}/event-details-detailed.png` });++ await dialog.locator('a[href*="/profiles/"]').click();+ await expect(page).toHaveURL(/\/profiles\/[^/]+/);+ await expect(page.getByRole('dialog')).toHaveCount(0);+ await expect(page.getByText('Profile Information')).toBeVisible();+ await page.waitForLoadState('networkidle');+ await page.goBack();+ await expect(page).toHaveURL(/events\?events=purchase/);+ expect(problems).toEqual([]);+ });++ test('clicking a property in the details modal filters by it', async ({+ page,+ }) => {+ await open(page, `${SHOP}/events/events?events=purchase`);+ await rows(page).first().locator('button[title]').click();+ const dialog = page.getByRole('dialog');+ await expect(dialog.getByText('Properties')).toBeVisible();+ await dialog.getByText('Payment', { exact: true }).click();+ await expect(page.getByRole('dialog')).toHaveCount(0);+ await expect(page).toHaveURL(/f=properties\.payment/);+ await page.waitForLoadState('networkidle');+ await page.screenshot({ path: `${SHOTS}/events-filter-by-property.png` });+ await expect(rows(page).first()).toBeVisible();+ });++ test('"Show all" in the details modal filters by the event name', async ({+ page,+ }) => {+ await open(page, `${SHOP}/events`);+ await rows(page).first().locator('button[title]').click();+ const dialog = page.getByRole('dialog');+ await dialog.getByRole('button', { name: 'Show all' }).click();+ await expect(page.getByRole('dialog')).toHaveCount(0);+ await expect(page).toHaveURL(/events=/);+ });++ test('the Session ID column can be switched on and links to the session', async ({+ page,+ }) => {+ await open(page, `${SHOP}/events`);+ await page.getByRole('combobox', { name: 'Toggle columns' }).click();+ await page.screenshot({ path: `${SHOTS}/events-view-options.png` });+ await page.getByRole('option', { name: 'Session ID' }).click();+ await page.keyboard.press('Escape');+ await expect(+ page.getByText('Session ID', { exact: true }).first()+ ).toBeVisible();+ const link = rows(page).first().locator('a[href*="/sessions/"]');+ await expect(link).toBeVisible();+ await link.click();+ await expect(page).toHaveURL(/\/sessions\/[^/]+/);+ await expect(page.getByText('Session not found')).toHaveCount(0);+ });++ test('date range limits the list', async ({ page }) => {+ await open(page, `${SHOP}/events`);+ await page.getByRole('button', { name: 'Date range' }).click();+ const dialog = page.getByRole('dialog');+ await dialog+ .getByRole('button', { name: /September 29/ })+ .first()+ .click();+ await dialog+ .getByRole('button', { name: /September 30/ })+ .first()+ .click();+ await page.screenshot({ path: `${SHOTS}/events-date-range.png` });+ await dialog.getByRole('button', { name: /^Select 29 sep/ }).click();+ await expect(page).toHaveURL(/startDate=2026-09-2\d.*endDate=/);+ await expect(+ page.getByRole('button', { name: 'Sep 29 - Sep 30' })+ ).toBeVisible();+ await page.waitForLoadState('networkidle');+ await page.screenshot({ path: `${SHOTS}/events-date-range-applied.png` });+ // Newest first: the last selected day (30 Sep) must be part of the range.+ await expect(rows(page).first().locator('button[title]')).toBeVisible();+ await expect(rows(page).first()).toContainText(/30 sep/i);+ await page.reload();+ await expect(+ page.getByRole('button', { name: 'Sep 29 - Sep 30' })+ ).toBeVisible();+ });++ test('editing an event toggles its conversion flag, icon and color', async ({+ page,+ }) => {+ const problems = watch(page);+ await open(page, `${SHOP}/events/events?events=add_to_cart`);+ const firstRow = rows(page).first();+ await expect(firstRow.locator('button[title="add to cart"]')).toBeVisible();+ await firstRow.locator('button').first().click();+ const dialog = page.getByRole('dialog');+ await expect(dialog.getByText('Edit: add_to_cart')).toBeVisible();+ await page.screenshot({ path: `${SHOTS}/event-edit.png` });+ const conversion = dialog.getByRole('checkbox');+ await expect(conversion).not.toBeChecked();+ await conversion.click();+ await dialog.getByPlaceholder('Search for an icon').fill('cart');+ await dialog.locator('.grid button').first().click();+ await expect(dialog.getByText('Pick a color')).toBeVisible();+ await dialog.locator('.grid button').nth(3).click();+ await page.screenshot({ path: `${SHOTS}/event-edit-color.png` });+ await dialog.getByRole('button', { name: 'Update event' }).click();+ await expect(page.getByText('Event updated')).toBeVisible();+ await expect(page.getByRole('dialog')).toHaveCount(0);++ await page.getByRole('tab', { name: 'Conversions' }).click();+ await expect(rows(page).first()).toBeVisible();+ await page.screenshot({ path: `${SHOTS}/event-edit-conversions.png` });+ await expect(+ rows(page).locator('button[title="add to cart"]').first()+ ).toBeVisible();++ // Restore: the seed does not mark add_to_cart as a conversion.+ await open(page, `${SHOP}/events/events?events=add_to_cart`);+ await expect(+ rows(page).first().locator('button[title="add to cart"]')+ ).toBeVisible();+ await rows(page).first().locator('button').first().click();+ await expect(dialog.getByText('Edit: add_to_cart')).toBeVisible();+ await expect(dialog.getByRole('checkbox')).toBeChecked();+ await dialog.getByRole('checkbox').click();+ await dialog.getByRole('button', { name: 'Update event' }).click();+ await expect(page.getByText('Event updated').first()).toBeVisible();+ await page.getByRole('tab', { name: 'Conversions' }).click();+ await expect(rows(page).first()).toBeVisible();+ await expect(rows(page).locator('button[title="add to cart"]')).toHaveCount(+ 0+ );+ expect(problems).toEqual([]);+ });++ test('the live indicator counts new events and refreshes on click', async ({+ page,+ seed,+ request,+ }) => {+ const shop = seed.projects.find((project) => project.id === 'acme-shop');+ if (!shop) {+ throw new Error('acme-shop is not seeded');+ }+ await open(page, `${SHOP}/events`);+ await expect(page.getByRole('button', { name: 'Listening' })).toBeVisible();+ const marker = `/e2e-data-${Date.now()}`;+ const response = await request.post(+ 'https://api.main.local.openpanel.cc/track',+ {+ headers: {+ 'openpanel-client-id': shop.clientId,+ 'openpanel-client-secret': shop.clientSecret,+ 'user-agent':+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Chrome/128.0 Safari/537.36',+ 'x-client-ip': '81.2.69.142',+ },+ data: {+ type: 'track',+ payload: { name: 'screen_view', properties: { __path: marker } },+ },+ }+ );+ expect(response.status()).toBeLessThan(300);+ const indicator = page.getByRole('button', { name: /new events/ });+ await expect(indicator).toBeVisible({ timeout: 60_000 });+ await page.screenshot({ path: `${SHOTS}/events-live.png` });+ await indicator.click();+ await expect(page.getByRole('button', { name: 'Listening' })).toBeVisible();+ await expect(rows(page).locator(`button[title="${marker}"]`)).toBeVisible();+ });++ test('conversions tab lists only conversion events', async ({ page }) => {+ const problems = watch(page);+ await open(page, `${SHOP}/events`);+ await page.getByRole('tab', { name: 'Conversions' }).click();+ await expect(page).toHaveURL(/\/events\/conversions/);+ await expect(rows(page).first()).toBeVisible();+ await page.waitForLoadState('networkidle');+ await page.screenshot({ path: `${SHOTS}/events-conversions.png` });+ const names = new Set(+ await rows(page).locator('button[title]').allInnerTexts()+ );+ for (const name of names) {+ expect(['purchase', 'account created']).toContain(name);+ }+ expect(problems).toEqual([]);+ });++ test('stats tab renders its four charts', async ({ page }) => {+ await open(page, `${SHOP}/events/stats`);+ for (const title of [+ 'Events per day',+ 'Share of events',+ 'Events by name',+ 'Events over time',+ ]) {+ await expect(page.getByText(title, { exact: true })).toBeVisible();+ }+ await expect(page.getByText('screen_view').first()).toBeVisible();+ await page.screenshot({+ path: `${SHOTS}/events-stats.png`,+ fullPage: true,+ });+ });++ test('mobile viewport has no horizontal page scroll', async ({ page }) => {+ await page.setViewportSize({ width: 390, height: 844 });+ await open(page, `${SHOP}/events`);+ await expect(rows(page).first()).toBeVisible();+ await page.screenshot({ path: `${SHOTS}/events-mobile.png` });+ const overflow = await page.evaluate(+ () => document.documentElement.scrollWidth - window.innerWidth+ );+ expect(overflow).toBeLessThanOrEqual(0);+ });++ test('pressing back right after opening a profile returns to the events list', async ({+ page,+ }) => {+ await open(page, `${SHOP}/events/events?events=purchase`);+ await rows(page).first().locator('a[href*="/profiles/"]').click();+ await expect(page).toHaveURL(/\/profiles\/[^/]+/);+ await page.goBack();+ await page.waitForLoadState('networkidle');+ await expect(page).toHaveURL(/events\?events=purchase/);+ });+});diff --git a/apps/start/e2e/data-groups-cohorts-references.spec.ts b/apps/start/e2e/data-groups-cohorts-references.spec.tsnew file mode 100644index 000000000..e1735d811--- /dev/null+++ b/apps/start/e2e/data-groups-cohorts-references.spec.ts@@ -0,0 +1,456 @@+import type { Page } from '@playwright/test';+import { expect as baseExpect, test } from './fixtures';++// The dev server shares a machine with other suites; default timeouts are too tight.+const expect = baseExpect.configure({ timeout: 30_000 });+test.describe.configure({ timeout: 300_000 });++const SHOTS = 'test-results/data/shots';+const SHOP = '/acme/acme-shop';+const SAAS = '/acme/acme-saas';+const STAMP = Date.now();+const NOISE = [+ /api\.openpanel\.dev/,+ /favicon/,+ /sentry/i,+ /\[vite\]/,+ /Failed to load resource/,+ // The sidebar's AI composer input (not this surface) differs in a `style` attribute.+ /A tree hydrated but some attributes/,+ /requires a `DialogTitle`/,+ /Missing `Description`/,+];++function watch(page: Page) {+ const problems: string[] = [];+ page.on('console', (message) => {+ if (message.type() !== 'error') {+ return;+ }+ const text = message.text();+ if (!NOISE.some((pattern) => pattern.test(text))) {+ problems.push(`console: ${text.slice(0, 400)}`);+ }+ });+ page.on('pageerror', (error) => {+ problems.push(`pageerror: ${error.message.slice(0, 400)}`);+ });+ page.on('response', (response) => {+ const url = response.url();+ if (response.status() >= 400 && !NOISE.some((p) => p.test(url))) {+ problems.push(`http ${response.status()}: ${url.slice(0, 200)}`);+ }+ });+ return problems;+}++async function hydrated(page: Page) {+ await page.waitForLoadState('networkidle');+ // The project switcher only gets its label once the client has hydrated.+ await expect(page.getByRole('combobox').first()).toContainText('Acme', {+ timeout: 45_000,+ });+}++async function open(page: Page, path: string) {+ await page.context().addCookies([+ {+ name: 'feedback-prompt-seen',+ value: new Date().toISOString(),+ url: process.env.DASHBOARD_URL ?? 'http://localhost:3000',+ },+ ]);+ for (const attempt of [1, 2, 3]) {+ await page.goto(path);+ try {+ await hydrated(page);+ return;+ } catch (error) {+ if (attempt === 3) {+ throw error;+ }+ }+ }+}++const shot = (page: Page, name: string, fullPage = false) =>+ page.screenshot({ path: `${SHOTS}/${name}.png`, fullPage });++test.describe('groups', () => {+ const groupId = `e2e-data-${STAMP}`;+ const groupName = `E2E data ${STAMP}`;++ test('add, open, edit and delete a group', async ({ page }) => {+ const problems = watch(page);+ await open(page, `${SAAS}/groups`);+ await page.getByRole('button', { name: 'Add group' }).click();+ const dialog = page.getByRole('dialog');+ await expect(dialog.getByText('Add group')).toBeVisible();++ // Validation: a dirty but incomplete form must say what is missing.+ await dialog.getByLabel('Name').fill(groupName);+ await dialog.getByRole('button', { name: 'Create' }).click();+ await shot(page, 'group-add-validation');+ await expect(dialog).toBeVisible();++ await dialog.getByLabel('ID').fill(groupId);+ await dialog.getByLabel('Type').fill('e2e-company');+ await dialog.getByRole('button', { name: 'Add', exact: true }).click();+ await dialog.getByPlaceholder('key').fill('plan');+ await dialog.getByPlaceholder('value').fill('enterprise');+ await shot(page, 'group-add');+ await dialog.getByLabel('Type').press('Enter');+ await expect(page.getByText('Group created.')).toBeVisible();+ await expect(page.getByRole('dialog')).toHaveCount(0);++ const row = page.locator('tbody tr', { hasText: groupName });+ await expect(row).toBeVisible();+ await shot(page, 'groups-list');++ await page.getByPlaceholder(/Search/).fill(groupName);+ await expect(page).toHaveURL(/search=/);+ await expect(row).toBeVisible();+ await expect(page.locator('tbody tr')).toHaveCount(1);++ await row.getByRole('link').first().click();+ await expect(page).toHaveURL(new RegExp(`/groups/${groupId}$`));+ await hydrated(page);+ await expect(page.getByText(groupName).first()).toBeVisible();+ await page.waitForTimeout(1500);+ await shot(page, 'group-detail', true);+ await expect(page.getByText('enterprise').first()).toBeVisible();++ await page.getByRole('tab', { name: 'Members' }).click();+ await expect(page).toHaveURL(/\/members/);+ await page.waitForLoadState('networkidle');+ await shot(page, 'group-members');+ await page.getByRole('tab', { name: 'Events' }).click();+ await expect(page).toHaveURL(/\/events/);+ await page.waitForLoadState('networkidle');+ await shot(page, 'group-events');+ await page.reload();+ await hydrated(page);+ await expect(page.getByText(groupName).first()).toBeVisible();+ await page.getByRole('tab', { name: 'Overview' }).click();++ await page.getByRole('button', { name: 'Edit' }).click();+ await expect(dialog.getByText('Edit group')).toBeVisible();+ await shot(page, 'group-edit');+ await dialog.getByLabel('Name').fill(`${groupName} edited`);+ await dialog.getByPlaceholder('value').fill('scale');+ await dialog.getByRole('button', { name: /Update|Save/ }).click();+ await expect(page.getByText('Group updated.')).toBeVisible();+ await expect(page.getByText(`${groupName} edited`).first()).toBeVisible();+ await expect(page.getByText('scale').first()).toBeVisible();+ await shot(page, 'group-edited', true);++ await page.getByRole('button', { name: 'Delete' }).click();+ await shot(page, 'group-delete-confirm');+ await page+ .getByRole('dialog')+ .getByRole('button', { name: /Yes|Delete|Confirm/ })+ .click();+ await expect(page).toHaveURL(/\/groups$/);+ await page.waitForLoadState('networkidle');+ await expect(page.getByText(`${groupName} edited`)).toHaveCount(0);+ expect(problems).toEqual([]);+ });++ test('mobile viewport has no horizontal page scroll', async ({ page }) => {+ await page.setViewportSize({ width: 390, height: 844 });+ await open(page, `${SAAS}/groups`);+ await shot(page, 'groups-mobile');+ const overflow = await page.evaluate(+ () => document.documentElement.scrollWidth - window.innerWidth+ );+ expect(overflow).toBeLessThanOrEqual(0);+ });+});++test.describe('references', () => {+ const title = `E2E data ${STAMP}`;++ test('add, edit and delete a reference; it marks the overview chart', async ({+ page,+ }) => {+ const problems = watch(page);+ await open(page, `${SHOP}/references`);+ await page.getByRole('button', { name: 'Create reference' }).last().click();+ const dialog = page.getByRole('dialog');+ await expect(dialog.getByText('Add reference')).toBeVisible();+ await dialog.getByLabel('Title').fill(title);+ await dialog.getByLabel('Description').fill('created by the data spec');+ await shot(page, 'reference-add');+ await dialog.getByLabel('Description').press('Enter');+ await expect(page.getByText('Success').first()).toBeVisible();+ const row = page.locator('tbody tr', { hasText: title });+ await expect(row).toBeVisible();+ await shot(page, 'references-list');++ await open(page, SHOP);+ await page.waitForTimeout(3000);+ await shot(page, 'reference-overview', true);+ const flag = page.locator('svg.lucide-flag').first();+ await expect(flag).toBeVisible();+ await flag.hover();+ await expect(page.getByText(title).first()).toBeVisible();+ await shot(page, 'reference-overview-hover');++ await open(page, `${SHOP}/references`);+ await row.getByRole('button').last().click();+ await page.getByRole('menuitem', { name: 'Edit' }).click();+ await expect(dialog.getByText('Edit reference')).toBeVisible();+ await dialog.getByLabel('Title').fill(`${title} edited`);+ await shot(page, 'reference-edit');+ await dialog.getByRole('button', { name: /Save|Update/ }).click();+ await expect(page.getByText('Reference updated.')).toBeVisible();+ const editedRow = page.locator('tbody tr', { hasText: `${title} edited` });+ await expect(editedRow).toBeVisible();++ await editedRow.getByRole('button').last().click();+ await page.getByRole('menuitem', { name: 'Delete' }).click();+ await shot(page, 'reference-delete-confirm');+ await page+ .getByRole('dialog')+ .getByRole('button', { name: /Yes|Delete|Confirm/ })+ .click();+ await expect(page.getByText('Reference deleted')).toBeVisible();+ await expect(editedRow).toHaveCount(0);+ expect(problems).toEqual([]);+ });++ test('mobile viewport has no horizontal page scroll', async ({ page }) => {+ await page.setViewportSize({ width: 390, height: 844 });+ await open(page, `${SHOP}/references`);+ await shot(page, 'references-mobile');+ const overflow = await page.evaluate(+ () => document.documentElement.scrollWidth - window.innerWidth+ );+ expect(overflow).toBeLessThanOrEqual(0);+ });+});++test.describe('cohorts', () => {+ const name = `E2E data ${STAMP} purchasers`;++ test('event-based cohort: create, count, tabs, edit, delete', async ({+ page,+ }) => {+ const problems = watch(page);+ await open(page, `${SHOP}/cohorts`);+ await page.getByRole('button', { name: /Create cohort|Cohort/ }).click();+ const dialog = page.getByRole('dialog');+ await expect(dialog.getByText('Create cohort')).toBeVisible();+ await shot(page, 'cohort-add-empty');++ await dialog.getByRole('button', { name: 'Add event criteria' }).click();+ await dialog.getByRole('button', { name: /Select event/ }).click();+ await page.getByPlaceholder('Search').last().fill('purchase');+ await page+ .getByRole('option', { name: /^purchase/ })+ .first()+ .click();+ await page.keyboard.press('Escape');+ await shot(page, 'cohort-add-event');++ // Submitting without a name must explain why nothing happened.+ await dialog.getByRole('button', { name: 'Create', exact: true }).click();+ await shot(page, 'cohort-add-no-name');+ await expect(dialog).toBeVisible();++ await dialog.getByLabel('Name').fill(name);+ await dialog.getByPlaceholder('Optional description').fill('bought once');+ await dialog.getByRole('button', { name: 'Create', exact: true }).click();+ await expect(page.getByText('Cohort created.')).toBeVisible();+ await expect(page.getByText(name)).toBeVisible();+ await shot(page, 'cohorts-list');++ await page.getByRole('link', { name: new RegExp(name) }).click();+ await expect(page).toHaveURL(/\/cohorts\/[0-9a-f-]+$/);+ await hydrated(page);+ await expect(page.getByRole('button', { name: 'Refresh' })).toBeVisible();+ await page.waitForLoadState('networkidle');+ await shot(page, 'cohort-detail', true);+ await page.getByRole('tab', { name: 'Members' }).click();+ await expect(page).toHaveURL(/\/members/);+ await page.waitForLoadState('networkidle');+ await page.waitForTimeout(1500);+ await shot(page, 'cohort-members', true);+ await page.getByRole('tab', { name: 'Events' }).click();+ await expect(page).toHaveURL(/\/events/);+ await page.waitForLoadState('networkidle');+ await page.waitForTimeout(1500);+ await shot(page, 'cohort-events', true);+ await page.reload();+ await hydrated(page);+ await page.getByRole('tab', { name: 'Overview' }).click();++ await page.getByRole('button', { name: 'Edit' }).click();+ await expect(dialog).toBeVisible();+ await shot(page, 'cohort-edit');+ await dialog.getByLabel('Name').fill(`${name} edited`);+ await dialog.getByRole('button', { name: /Save|Update/ }).click();+ await expect(page.getByText(/Cohort updated/)).toBeVisible();+ await expect(page.getByText(`${name} edited`).first()).toBeVisible();++ await page.getByRole('button', { name: 'Delete' }).click();+ await page+ .getByRole('dialog')+ .getByRole('button', { name: /Yes|Delete|Confirm/ })+ .click();+ await expect(page).toHaveURL(/\/cohorts$/);+ await page.waitForLoadState('networkidle');+ await expect(page.getByText(`${name} edited`)).toHaveCount(0);+ expect(problems).toEqual([]);+ });++ test('a cohort of purchasers gets computed and has members', async ({+ page,+ }) => {+ const computed = `E2E data ${STAMP} computed`;+ await open(page, `${SHOP}/cohorts`);+ await page.getByRole('button', { name: /Create cohort|Cohort/ }).click();+ const dialog = page.getByRole('dialog');+ await dialog.getByLabel('Name').fill(computed);+ await dialog.getByRole('button', { name: 'Add event criteria' }).click();+ await dialog.getByRole('button', { name: /Select event/ }).click();+ await page.getByPlaceholder('Search').last().fill('purchase');+ await page+ .getByRole('option', { name: /^purchase/ })+ .first()+ .click();+ await page.keyboard.press('Escape');+ await dialog.getByRole('button', { name: 'Create', exact: true }).click();+ await expect(page.getByText('Cohort created.')).toBeVisible();+ await page.getByRole('link', { name: new RegExp(computed) }).click();+ await expect(page).toHaveURL(/\/cohorts\/[0-9a-f-]+$/);+ await hydrated(page);++ let members = 0;+ try {+ // The seed has ~90 shoppers with a purchase; the compute job is async.+ await expect+ .poll(+ async () => {+ await page.reload();+ await hydrated(page);+ await page.getByRole('tab', { name: 'Members' }).click();+ await page.waitForLoadState('networkidle');+ members = await page.locator('tbody tr a').count();+ return members;+ },+ { timeout: 90_000, intervals: [10_000] }+ )+ .toBeGreaterThan(0);+ } finally {+ await shot(page, 'cohort-computed', true);+ await page.getByRole('button', { name: 'Delete' }).click();+ await page+ .getByRole('dialog')+ .getByRole('button', { name: /Yes|Delete|Confirm/ })+ .click();+ await expect(page).toHaveURL(/\/cohorts$/);+ }+ });++ test('the builder offers every criteria control and saves a combined definition', async ({+ page,+ }) => {+ const combined = `E2E data ${STAMP} combined`;+ const problems = watch(page);+ await open(page, `${SHOP}/cohorts`);+ await page.getByRole('button', { name: /Create cohort|Cohort/ }).click();+ const dialog = page.getByRole('dialog');+ await dialog.getByLabel('Name').fill(combined);+ await dialog.locator('#isStatic').click();++ const pick = async (trigger: string | RegExp, item: string) => {+ await dialog.getByRole('button', { name: trigger }).last().click();+ await page.getByRole('menuitem', { name: item, exact: true }).click();+ };+ const pickEvent = async (eventName: string) => {+ await dialog+ .getByRole('button', { name: /Select event/ })+ .last()+ .click();+ await page.getByPlaceholder('Search').last().fill(eventName);+ await page+ .getByRole('option', { name: new RegExp(`^${eventName}`) })+ .first()+ .click();+ await page.keyboard.press('Escape');+ };++ await dialog.getByRole('button', { name: 'Add event criteria' }).click();+ await pickEvent('purchase');+ await pick('At least', 'Exactly');+ await dialog.locator('input[type="number"]').last().fill('2');+ await pick('Last', 'Since');+ await dialog.locator('input[type="date"]').last().fill('2026-09-20');+ await shot(page, 'cohort-builder-since');++ await dialog.getByRole('button', { name: 'Add event criteria' }).click();+ await pickEvent('add_to_cart');+ await pick('At least', 'At most');+ await dialog.locator('input[type="number"]').last().fill('0');+ await pick('Last', 'Between');+ await expect(dialog.locator('input[type="date"]')).toHaveCount(3);+ await pick('Between', 'Last');+ await pick('30 days', '7 days');+ await pick('Any', 'All of these events');+ await expect(+ dialog.getByRole('button', { name: 'All', exact: true })+ ).toBeVisible();++ await dialog.getByRole('button', { name: 'Add filter' }).first().click();+ await shot(page, 'cohort-builder-filter-picker');+ await page.getByPlaceholder('Search').last().fill('payment');+ await page.getByText('payment', { exact: true }).first().click();+ await shot(page, 'cohort-builder-filter');+ await dialog.evaluate((el) => el.scrollTo(0, el.scrollHeight));+ await shot(page, 'cohort-builder-bottom');++ await dialog.getByRole('button', { name: 'Create', exact: true }).click();+ await expect(page.getByText('Cohort created.')).toBeVisible();+ const card = page+ .locator('div', {+ has: page.getByRole('link', { name: new RegExp(combined) }),+ })+ .last();+ await expect(card.getByText('Static')).toBeVisible();+ await shot(page, 'cohorts-list-combined');++ // Edit from the card: the saved definition comes back as entered.+ await card.getByRole('button').first().click();+ await page.getByRole('menuitem', { name: 'Edit' }).click();+ await expect(dialog.getByLabel('Name')).toHaveValue(combined);+ await shot(page, 'cohort-edit-combined');+ await expect(dialog.getByRole('button', { name: 'Exactly' })).toBeVisible();+ await expect(dialog.getByRole('button', { name: 'At most' })).toBeVisible();+ await expect(dialog.getByRole('button', { name: 'Since' })).toBeVisible();+ await expect(dialog.getByRole('button', { name: '7 days' })).toBeVisible();+ await expect(+ dialog.getByRole('button', { name: 'All', exact: true })+ ).toBeVisible();++ // Switch to a property-based definition.+ await dialog.getByRole('button', { name: 'Property-based' }).click();+ await dialog.getByRole('button', { name: 'Add property filter' }).click();+ await shot(page, 'cohort-builder-property-picker');+ await page.getByPlaceholder('Search').last().fill('country');+ await page.getByText('country', { exact: true }).first().click();+ await shot(page, 'cohort-builder-property');+ await dialog.getByRole('button', { name: /Save|Update/ }).click();+ await expect(page.getByText(/Cohort updated/)).toBeVisible();++ await card.getByRole('button').first().click();+ await page.getByRole('menuitem', { name: 'Delete' }).click();+ await page+ .getByRole('dialog')+ .getByRole('button', { name: /Yes|Delete|Confirm/ })+ .click();+ await expect(page.getByText('Cohort deleted.')).toBeVisible();+ await expect(page.getByText(combined)).toHaveCount(0);+ expect(problems).toEqual([]);+ });+});diff --git a/apps/start/e2e/data-sessions-profiles.spec.ts b/apps/start/e2e/data-sessions-profiles.spec.tsnew file mode 100644index 000000000..e90aeebde--- /dev/null+++ b/apps/start/e2e/data-sessions-profiles.spec.ts@@ -0,0 +1,395 @@+import type { Page } from '@playwright/test';+import { expect as baseExpect, test } from './fixtures';++// The dev server shares a machine with other suites; default timeouts are too tight.+const expect = baseExpect.configure({ timeout: 30_000 });+test.describe.configure({ timeout: 240_000 });++const SHOTS = 'test-results/data/shots';+const SHOP = '/acme/acme-shop';+const SAAS = '/acme/acme-saas';+const WEB = '/acme/acme-web';+const NOISE = [+ /api\.openpanel\.dev/,+ /favicon/,+ /sentry/i,+ /\[vite\]/,+ /Failed to load resource/,+ // The sidebar's AI composer input (not this surface) differs in a `style` attribute.+ /A tree hydrated but some attributes/,+ /requires a `DialogTitle`/,+];++function watch(page: Page) {+ const problems: string[] = [];+ page.on('console', (message) => {+ if (message.type() !== 'error') {+ return;+ }+ const text = message.text();+ if (!NOISE.some((pattern) => pattern.test(text))) {+ problems.push(`console: ${text.slice(0, 400)}`);+ }+ });+ page.on('pageerror', (error) => {+ problems.push(`pageerror: ${error.message.slice(0, 400)}`);+ });+ page.on('response', (response) => {+ const url = response.url();+ if (response.status() >= 400 && !NOISE.some((p) => p.test(url))) {+ problems.push(`http ${response.status()}: ${url.slice(0, 200)}`);+ }+ });+ return problems;+}++async function hydrated(page: Page) {+ await page.waitForLoadState('networkidle');+ // The project switcher only gets its label once the client has hydrated.+ await expect(page.getByRole('combobox').first()).toContainText('Acme', {+ timeout: 45_000,+ });+}++async function open(page: Page, path: string) {+ await page.context().addCookies([+ {+ name: 'feedback-prompt-seen',+ value: new Date().toISOString(),+ url: process.env.DASHBOARD_URL ?? 'http://localhost:3000',+ },+ ]);+ for (const attempt of [1, 2, 3]) {+ await page.goto(path);+ try {+ await hydrated(page);+ return;+ } catch (error) {+ if (attempt === 3) {+ throw error;+ }+ }+ }+}++const rows = (page: Page) => page.locator('[data-index]');+const tableRows = (page: Page) => page.locator('tbody tr');++test.describe('sessions', () => {+ test('list renders, scrolls to the next page and searches', async ({+ page,+ }) => {+ const problems = watch(page);+ await open(page, `${SHOP}/sessions`);+ await expect(rows(page).first()).toBeVisible();+ await page.screenshot({ path: `${SHOTS}/sessions-list.png` });++ await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));+ await expect+ .poll(() =>+ rows(page).evaluateAll((elements) =>+ Math.max(+ ...elements.map((el) => Number(el.getAttribute('data-index')))+ )+ )+ )+ .toBeGreaterThan(49);+ await page.evaluate(() => window.scrollTo(0, 0));++ await page.getByPlaceholder(/Search/).fill('/checkout');+ await expect(page).toHaveURL(/search=/);+ await page.waitForLoadState('networkidle');+ await expect(rows(page).first().locator('a').first()).toBeVisible();+ await page.screenshot({ path: `${SHOTS}/sessions-search.png` });+ const texts = await rows(page).allInnerTexts();+ for (const text of texts.slice(0, 10)) {+ expect(text).toContain('/checkout');+ }++ await page.getByPlaceholder(/Search/).fill('zzz-no-such-session');+ await expect(page.getByText('No sessions found')).toBeVisible();+ await page.screenshot({ path: `${SHOTS}/sessions-empty.png` });+ expect(problems).toEqual([]);+ });++ test('bounce filter narrows the list and survives reload', async ({+ page,+ }) => {+ const problems = watch(page);+ await open(page, `${SHOP}/sessions`);+ await page.getByRole('button', { name: 'Filters' }).click();+ await page.getByRole('button', { name: 'Add filter' }).click();+ await page.screenshot({ path: `${SHOTS}/sessions-filter-categories.png` });+ await page.getByText('Session metrics', { exact: true }).click();+ await page.getByText('Bounced', { exact: true }).click();+ await page.waitForTimeout(500);+ await page.screenshot({ path: `${SHOTS}/sessions-filter-bounced.png` });+ await page.getByRole('combobox').filter({ hasText: 'Yes / No' }).click();+ await page.screenshot({+ path: `${SHOTS}/sessions-filter-bounced-value.png`,+ });+ await page.getByRole('option', { name: 'Yes', exact: true }).click();+ await page.keyboard.press('Escape');+ await page.keyboard.press('Escape');+ await expect(page).toHaveURL(/f=session\.is_bounce/);+ await page.waitForLoadState('networkidle');+ await expect(rows(page).first()).toBeVisible();+ await page.screenshot({ path: `${SHOTS}/sessions-filtered.png` });+ const bounceCells = await rows(page).allInnerTexts();+ expect(bounceCells.length).toBeGreaterThan(0);+ for (const text of bounceCells.slice(0, 15)) {+ expect(text).toMatch(/\bYes\b/);+ }+ await page.reload();+ await hydrated(page);+ await expect(page).toHaveURL(/f=session\.is_bounce/);+ await expect(rows(page).first()).toContainText('Yes');+ expect(problems).toEqual([]);+ });++ test('hiding a column persists across reload', async ({ page }) => {+ await open(page, `${SHOP}/sessions`);+ await page.getByRole('combobox', { name: 'Toggle columns' }).click();+ const header = page+ .locator('.sticky')+ .getByText('Entry Page', { exact: true });+ await expect(header).toBeVisible();+ await page.getByRole('option', { name: 'Entry Page' }).click();+ await page.keyboard.press('Escape');+ await expect(header).toHaveCount(0);+ await page.screenshot({ path: `${SHOTS}/sessions-view.png` });+ await page.reload();+ await hydrated(page);+ await expect(rows(page).first().locator('a').first()).toBeVisible();+ await expect(header).toHaveCount(0);+ });++ test('detail page shows the timeline and links to the profile', async ({+ page,+ }) => {+ const problems = watch(page);+ await open(page, `${SAAS}/sessions`);+ await expect(rows(page).first()).toBeVisible();+ const firstRow = rows(page).first();+ await firstRow.locator('a[href*="/sessions/"]').first().click();+ await expect(page).toHaveURL(/\/sessions\/[^/]+$/);+ await expect(+ page.getByRole('heading', { name: /^Session: / })+ ).toBeVisible();+ await page.waitForLoadState('networkidle');+ await page.screenshot({+ path: `${SHOTS}/session-detail.png`,+ fullPage: true,+ });+ await expect(page.getByText('Session info')).toBeVisible();+ await expect(page.getByText('Visited pages')).toBeVisible();+ await expect(page.getByText('Event distribution')).toBeVisible();++ await page.reload();+ await hydrated(page);+ await expect(page.getByText('Session info')).toBeVisible();++ await page.getByRole('link', { name: /@/ }).click();+ await expect(page).toHaveURL(/\/profiles\/[^/]+$/);+ await page.goBack();+ await expect(page).toHaveURL(/\/sessions\/[^/]+$/);+ await expect(page.getByText('Session info')).toBeVisible();+ await page.goBack();+ await expect(page).toHaveURL(/\/sessions$/);+ await page.goForward();+ await expect(page).toHaveURL(/\/sessions\/[^/]+$/);+ expect(problems).toEqual([]);+ });++ test('browser back from a list reached through the sidebar returns to the previous page', async ({+ page,+ }) => {+ await open(page, `${SAAS}/sessions`);+ await page.getByRole('link', { name: 'Profiles', exact: true }).click();+ await expect(page).toHaveURL(/\/profiles\/identified/);+ await page.goBack();+ await expect(page).toHaveURL(/\/sessions$/);+ await page.getByRole('link', { name: 'Events', exact: true }).click();+ await expect(page).toHaveURL(/\/events\/events/);+ await page.goBack();+ await expect(page).toHaveURL(/\/sessions$/);+ });++ test('an unknown session id shows a not-found state inside the app', async ({+ page,+ }) => {+ await page.goto(`${SHOP}/sessions/does-not-exist`);+ await expect(page.getByText(/not found/i).first()).toBeVisible({+ timeout: 60_000,+ });+ await page.screenshot({ path: `${SHOTS}/session-not-found.png` });+ await expect(page.getByText('Something went wrong')).toHaveCount(0);+ await expect(page.getByRole('link', { name: 'Sessions' })).toBeVisible();+ });++ test('mobile viewport has no horizontal page scroll', async ({ page }) => {+ await page.setViewportSize({ width: 390, height: 844 });+ await open(page, `${SHOP}/sessions`);+ await expect(rows(page).first()).toBeVisible();+ await page.screenshot({ path: `${SHOTS}/sessions-mobile.png` });+ const overflow = await page.evaluate(+ () => document.documentElement.scrollWidth - window.innerWidth+ );+ expect(overflow).toBeLessThanOrEqual(0);+ });+});++test.describe('profiles', () => {+ test('identified tab lists, searches and paginates', async ({ page }) => {+ const problems = watch(page);+ await open(page, `${SAAS}/profiles`);+ await expect(page).toHaveURL(/\/profiles\/identified/);+ await expect(tableRows(page).first()).toBeVisible();+ await page.screenshot({+ path: `${SHOTS}/profiles-identified.png`,+ fullPage: true,+ });+ expect(await tableRows(page).count()).toBe(50);+ const firstName = await tableRows(page)+ .first()+ .locator('a')+ .first()+ .innerText();++ await page.locator('button:has(svg.lucide-chevron-right)').click();+ await expect(page).toHaveURL(/page=2/);+ await expect(tableRows(page).first().locator('a').first()).not.toHaveText(+ firstName+ );+ await page.reload();+ await hydrated(page);+ await expect(page).toHaveURL(/page=2/);+ await expect(tableRows(page).first()).toBeVisible();+ await expect(tableRows(page).first().locator('a').first()).not.toHaveText(+ firstName+ );++ const lastName = firstName.split(' ').pop() ?? firstName;+ await page.getByPlaceholder('Search profiles').fill(lastName);+ await page.waitForLoadState('networkidle');+ await page.screenshot({ path: `${SHOTS}/profiles-search.png` });+ await expect(tableRows(page).first()).toContainText(lastName);+ expect(problems).toEqual([]);+ });++ test('anonymous and power-user tabs list profiles', async ({ page }) => {+ const problems = watch(page);+ await open(page, `${WEB}/profiles`);+ await page.getByRole('tab', { name: 'Anonymous' }).click();+ await expect(page).toHaveURL(/\/profiles\/anonymous/);+ await expect(tableRows(page).first().locator('a').first()).toBeVisible();+ await page.waitForLoadState('networkidle');+ await page.screenshot({ path: `${SHOTS}/profiles-anonymous.png` });+ await page.getByRole('tab', { name: 'Power users' }).click();+ await expect(page).toHaveURL(/\/profiles\/power-users/);+ await expect(tableRows(page).first().locator('a').first()).toBeVisible();+ await page.waitForLoadState('networkidle');+ await page.screenshot({+ path: `${SHOTS}/profiles-power-users.png`,+ fullPage: true,+ });+ expect(problems).toEqual([]);+ });++ test('search on the power-users tab filters the list', async ({ page }) => {+ await open(page, `${SAAS}/profiles/power-users`);+ const names = tableRows(page).locator('a');+ await expect(names.first()).toBeVisible();+ const before = await tableRows(page).count();+ await page.getByPlaceholder('Search profiles').fill('zzz-no-such-profile');+ await expect(page).toHaveURL(/search=/);+ await page.waitForLoadState('networkidle');+ await page.waitForTimeout(1500);+ await page.screenshot({ path: `${SHOTS}/profiles-power-search.png` });+ expect(await names.count()).toBeLessThan(before);+ });++ test('detail page shows metrics, properties, activity and tabs', async ({+ page,+ }) => {+ const problems = watch(page);+ await open(page, `${SAAS}/profiles`);+ await tableRows(page).first().locator('a').first().click();+ await expect(page).toHaveURL(/\/profiles\/[^/]+$/);+ await hydrated(page);+ await page.waitForTimeout(1500);+ await page.screenshot({+ path: `${SHOTS}/profile-detail.png`,+ fullPage: true,+ });+ for (const widget of [+ 'Profile Information',+ 'Activity',+ 'Latest Events',+ 'Popular events',+ 'Most visited pages',+ 'Page views',+ 'Events per day',+ ]) {+ await expect(page.getByText(widget, { exact: true })).toBeVisible();+ }++ await page.getByRole('tab', { name: 'Events' }).click();+ await expect(page).toHaveURL(/\/profiles\/[^/]+\/events/);+ await expect(rows(page).first().locator('button[title]')).toBeVisible();+ await page.screenshot({ path: `${SHOTS}/profile-events.png` });++ await page.getByRole('tab', { name: 'Sessions' }).click();+ await expect(page).toHaveURL(/\/profiles\/[^/]+\/sessions/);+ await expect(rows(page).first().locator('a').first()).toBeVisible();+ await page.screenshot({ path: `${SHOTS}/profile-sessions.png` });++ await rows(page).first().locator('a[href*="/sessions/"]').first().click();+ await expect(page).toHaveURL(/\/sessions\/[^/]+$/);+ await expect(page.getByText('Session info')).toBeVisible();+ await page.goBack();+ await expect(page).toHaveURL(/\/profiles\/[^/]+\/sessions/);+ await page.reload();+ await hydrated(page);+ await expect(rows(page).first()).toBeVisible();+ expect(problems).toEqual([]);+ });++ test('an unknown profile id shows a not-found state', async ({ page }) => {+ await open(page, `${SHOP}/profiles/does-not-exist`);+ await page.screenshot({+ path: `${SHOTS}/profile-not-found.png`,+ fullPage: true,+ });+ await expect(page.getByText(/not found/i).first()).toBeVisible();+ });++ test("metrics agree with the profile's sessions", async ({ page }) => {+ await open(page, `${SAAS}/profiles`);+ await tableRows(page).first().locator('a').first().click();+ await expect(page).toHaveURL(/\/profiles\/[^/]+$/);+ await hydrated(page);+ const sessionsCard = page+ .locator('div', { hasText: /^Sessions/ })+ .filter({ hasText: 'Total' })+ .last();+ const shown = Number((await sessionsCard.innerText()).match(/\d+/)?.[0]);+ await page.getByRole('tab', { name: 'Sessions' }).click();+ const sessionLinks = page.locator('[data-index] a[href*="/sessions/"]');+ await expect(sessionLinks.first()).toBeVisible();+ await page.waitForLoadState('networkidle');+ const listed = await rows(page).count();+ expect(shown).toBe(listed);+ });++ test('mobile viewport has no horizontal page scroll', async ({ page }) => {+ await page.setViewportSize({ width: 390, height: 844 });+ await open(page, `${SAAS}/profiles`);+ await expect(tableRows(page).first()).toBeVisible();+ await page.screenshot({ path: `${SHOTS}/profiles-mobile.png` });+ const overflow = await page.evaluate(+ () => document.documentElement.scrollWidth - window.innerWidth+ );+ expect(overflow).toBeLessThanOrEqual(0);+ });+});diff --git a/apps/start/e2e/org-billing.spec.ts b/apps/start/e2e/org-billing.spec.tsnew file mode 100644index 000000000..926598ab1--- /dev/null+++ b/apps/start/e2e/org-billing.spec.ts@@ -0,0 +1,64 @@+import { expect, test } from './fixtures';+import {+ apiFailures,+ dismissFeedbackPrompt,+ expectNoCrashes,+ gotoHydrated,+ shot,+ watchProblems,+} from './org-helpers';++// The shared dev server takes 10-25s per navigation while other suites run.+const SLOW_DEV_SERVER_TIMEOUT_MS = 150_000;++test.setTimeout(SLOW_DEV_SERVER_TIMEOUT_MS);++// Plan rows start a real checkout when there is no current plan, so none is clicked.+test('billing renders plans, usage and the FAQ without errors', async ({+ page,+ seed,+}) => {+ const problems = watchProblems(page);+ await dismissFeedbackPrompt(page);+ await gotoHydrated(page, `/${seed.organizationId}/billing`);++ await expect(+ page.getByRole('heading', { name: 'Billing', level: 1 })+ ).toBeVisible();+ const yearlyPlan = page.getByRole('button', {+ name: /10K events per month \(yearly\)/,+ });+ await expect(yearlyPlan).toContainText('/yr');+ await expect(+ page.getByRole('button', { name: /events per month/ })+ ).toHaveCount(8);++ await page.getByRole('button', { name: 'Monthly' }).click();+ await expect(+ page.getByRole('button', { name: /^10K events per month/ })+ ).toContainText('/mo');+ await page.getByRole('button', { name: 'Yearly' }).click();+ await expect(yearlyPlan).toBeVisible();++ await expect(page.getByText('Usage', { exact: true })).toBeVisible();+ await expect(page.getByText('Limit', { exact: true })).toBeVisible();+ await expect(page.locator('body')).not.toContainText(/NaN|undefined/);++ const question = page.getByRole('button', {+ name: 'Does OpenPanel have a free tier?',+ });+ await question.click();+ await expect(page.getByText('30 days free trial')).toBeVisible();+ await question.click();+ await expect(page.getByText('30 days free trial')).toBeHidden();++ await shot(page, 'billing');+ expectNoCrashes(problems);+ expect(apiFailures(problems)).toEqual([]);++ await page.setViewportSize({ width: 390, height: 844 });+ const overflow = await page.evaluate(+ () => document.documentElement.scrollWidth - window.innerWidth+ );+ expect(overflow).toBeLessThanOrEqual(0);+});diff --git a/apps/start/e2e/org-create-project.spec.ts b/apps/start/e2e/org-create-project.spec.tsnew file mode 100644index 000000000..016d78e9b--- /dev/null+++ b/apps/start/e2e/org-create-project.spec.ts@@ -0,0 +1,218 @@+import type { Locator, Page } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ apiFailures,+ deleteProjectThroughSettings,+ dismissFeedbackPrompt,+ expectNoCrashes,+ gotoHydrated,+ openMenu,+ shot,+ trpcQuery,+ watchProblems,+} from './org-helpers';++// The shared dev server takes 10-25s per navigation while other suites run.+const SLOW_DEV_SERVER_TIMEOUT_MS = 240_000;+const HYDRATION_TIMEOUT_MS = 40_000;++test.setTimeout(SLOW_DEV_SERVER_TIMEOUT_MS);++interface CreatedProject {+ id: string;+ name: string;+ domain: string | null;+ cors: string[];+ types: string[];+ deleteAt: string | null;+}++const findProject = async (+ page: Page,+ organizationId: string,+ name: string+) => {+ const projects = await trpcQuery<CreatedProject[]>(page, 'project.list', {+ organizationId,+ });+ const project = projects.find((candidate) => candidate.name === name);+ if (!project) {+ throw new Error(`Project "${name}" was not created`);+ }+ return project;+};++const openFromActionMenu = async (page: Page): Promise<Locator> => {+ await openMenu(+ page,+ page.getByRole('button', { name: /Create a project|Invite a user/ })+ );+ await page.getByRole('menuitem', { name: 'Create a project' }).click();+ const dialog = page.getByRole('dialog');+ await expect(+ dialog.getByRole('heading', { name: 'Create project' })+ ).toBeVisible();+ return dialog;+};++const submit = (dialog: Locator) =>+ dialog.getByRole('button', { name: 'Create project' }).click();++test.beforeEach(async ({ page, seed }) => {+ await dismissFeedbackPrompt(page);+ await gotoHydrated(page, `/${seed.organizationId}`);+});++test('the form validates name, type and domain before creating anything', async ({+ page,+ seed,+}) => {+ const before = await trpcQuery<CreatedProject[]>(page, 'project.list', {+ organizationId: seed.organizationId,+ });+ const dialog = await openFromActionMenu(page);+ const typeError = dialog.getByText('At least one type must be selected');++ await submit(dialog);+ await expect(typeError).toHaveCount(3);+ await expect(dialog.getByText('Issues')).toHaveCount(1);++ await dialog.getByLabel('Project name').fill('ab');+ await dialog.getByText('Website', { exact: true }).click();+ await expect(typeError).toHaveCount(0);+ await expect(dialog.getByRole('switch', { name: /^App/ })).toBeDisabled();+ await submit(dialog);+ // Name too short and domain missing.+ await expect(dialog.getByText('Issues')).toHaveCount(2);++ await dialog.getByLabel('Project name').fill('E2E org never created');+ await dialog.getByLabel('Domain').fill('not a url');+ await submit(dialog);+ await expect(dialog.getByText('Issues')).toHaveCount(1);+ await shot(page, 'create-project-validation');++ await dialog.getByText('Website', { exact: true }).click();+ await expect(dialog.getByLabel('Domain')).toBeHidden();+ await expect(dialog.getByRole('switch', { name: /^App/ })).toBeEnabled();+ await dialog.getByText('App', { exact: true }).click();+ await expect(dialog.getByRole('switch', { name: /^Website/ })).toBeDisabled();++ await dialog.getByRole('button', { name: 'Close' }).click();+ await expect(dialog).toBeHidden();+ const after = await trpcQuery<CreatedProject[]>(page, 'project.list', {+ organizationId: seed.organizationId,+ });+ expect(after.length).toBe(before.length);+});++test('a website + backend project is created with its domains and leads into setup', async ({+ page,+ seed,+}) => {+ const problems = watchProblems(page);+ const name = `E2E org web ${Date.now()}`;+ const dialog = await openFromActionMenu(page);++ await dialog.getByLabel('Project name').fill(name);+ await dialog.getByText('Website', { exact: true }).click();+ const domain = dialog.getByLabel('Domain');+ await domain.fill('https://e2e-org.example.com');+ await domain.blur();+ // Leaving the domain field seeds the allowed domains with it.+ await expect(+ dialog.locator('[data-tag="https://e2e-org.example.com"]')+ ).toBeVisible();++ const tagInput = dialog.getByPlaceholder(/Accept events from these domains/);+ await tagInput.fill('second.example.com');+ await tagInput.press('Enter');+ await expect(+ dialog.locator('[data-tag="https://second.example.com"]')+ ).toBeVisible();+ // A duplicate is not added twice.+ await tagInput.fill('https://second.example.com');+ await tagInput.press('Enter');+ await expect(+ dialog.locator('[data-tag="https://second.example.com"]')+ ).toHaveCount(1);+ await tagInput.fill('');+ await tagInput.fill('removed.example.com');+ await tagInput.press('Enter');+ await dialog+ .locator('[data-tag="https://removed.example.com"]')+ .getByRole('button', { name: 'Remove tag' })+ .click();+ await expect(+ dialog.locator('[data-tag="https://removed.example.com"]')+ ).toHaveCount(0);++ await dialog.getByText('Backend / API', { exact: true }).click();+ // Keyboard submit from the name field.+ await dialog.getByLabel('Project name').press('Enter');++ await expect(dialog.getByText('Your project is created')).toBeVisible();+ await expect(page.getByText('Project created')).toBeVisible();+ await expect(dialog.getByRole('button', { name: /Client ID/ })).toContainText(+ /[0-9a-f-]{36}/+ );+ await expect(dialog.getByRole('button', { name: /Secret/ })).toContainText(+ /sec_[0-9a-f]{20}/+ );+ await shot(page, 'create-project-success');++ const project = await findProject(page, seed.organizationId, name);+ expect(project.domain).toBe('https://e2e-org.example.com');+ expect(project.cors).toEqual([+ 'https://e2e-org.example.com',+ 'https://second.example.com',+ ]);++ await dialog.getByRole('link', { name: 'Set up tracking' }).click();+ await expect(page).toHaveURL(new RegExp(`/onboarding/${project.id}/connect`));+ // The secret is read from sessionStorage after hydration.+ await expect(page.getByText(/sec_[0-9a-f]{20}/).first()).toBeVisible({+ timeout: HYDRATION_TIMEOUT_MS,+ });+ await page.getByRole('link', { name: 'Next' }).click();+ await expect(page).toHaveURL(new RegExp(`/onboarding/${project.id}/verify`));+ await expect(page.getByText('Waiting for events')).toBeVisible();+ await expect(+ page.getByRole('link', { name: 'Skip for now' })+ ).toHaveAttribute('href', `/${seed.organizationId}/${project.id}`);++ await deleteProjectThroughSettings(page, seed.organizationId, project.id);+ const deleted = await findProject(page, seed.organizationId, name);+ expect(deleted.deleteAt).not.toBeNull();+ expectNoCrashes(problems);+ expect(apiFailures(problems)).toEqual([]);+});++// BUG org-3: project.create hardcodes `types: []`, so the choice made in this+// modal is dropped (onboarding's first project keeps it).+test('the tracking types chosen in the modal are stored on the project', async ({+ page,+ seed,+}) => {+ const name = `E2E org app ${Date.now()}`;+ const dialog = page.getByRole('dialog');+ // Second entry point: the project selector.+ await openMenu(page, page.getByRole('combobox').first());+ await page.getByRole('menuitem', { name: 'Create new project' }).click();++ await dialog.getByLabel('Project name').fill(name);+ await dialog.getByText('App', { exact: true }).click();+ await dialog.getByText('Backend / API', { exact: true }).click();+ await submit(dialog);+ await expect(dialog.getByText('Your project is created')).toBeVisible();+ await dialog.getByRole('button', { name: 'Close' }).last().click();+ await expect(dialog).toBeHidden();+ // Projects without events sort after the seeded ones, where cards are lazy.+ await page.getByRole('textbox', { name: 'Search projects' }).click();+ await page.getByRole('textbox', { name: 'Search projects' }).fill(name);+ await expect(page.locator('.card', { hasText: name })).toBeVisible();++ const project = await findProject(page, seed.organizationId, name);+ await deleteProjectThroughSettings(page, seed.organizationId, project.id);+ expect(project.domain).toBeNull();+ expect([...project.types].sort()).toEqual(['app', 'backend']);+});diff --git a/apps/start/e2e/org-helpers.ts b/apps/start/e2e/org-helpers.tsnew file mode 100644index 000000000..c46d52a73--- /dev/null+++ b/apps/start/e2e/org-helpers.ts@@ -0,0 +1,192 @@+import type { Page, TestInfo } from '@playwright/test';++const HYDRATION_ATTEMPTS = 4;+const HYDRATION_TIMEOUT_MS = 25_000;++export const SHOTS_DIR = 'test-results/org/shots';+export const API_URL =+ process.env.API_URL ?? 'https://api.main.local.openpanel.cc';++const NOISE = [+ /favicon/,+ /sentry/i,+ /\[vite\]/,+ /hmr/i,+ /Download the React DevTools/,+ /userjot/i,+ /net::ERR_ABORTED/,+];++export interface Problems {+ consoleErrors: string[];+ pageErrors: string[];+ badResponses: string[];+}++export function watchProblems(page: Page): Problems {+ const problems: Problems = {+ consoleErrors: [],+ pageErrors: [],+ badResponses: [],+ };+ const isNoise = (text: string) => NOISE.some((pattern) => pattern.test(text));+ page.on('console', (message) => {+ if (message.type() === 'error' && !isNoise(message.text())) {+ problems.consoleErrors.push(message.text());+ }+ });+ page.on('pageerror', (error) => {+ problems.pageErrors.push(String(error));+ });+ page.on('response', (response) => {+ if (response.status() >= 400 && !isNoise(response.url())) {+ problems.badResponses.push(+ `${response.status()} ${response.request().method()} ${response.url()}`+ );+ }+ });+ return problems;+}++export async function attachProblems(testInfo: TestInfo, problems: Problems) {+ await testInfo.attach('problems', {+ body: JSON.stringify(problems, null, 2),+ contentType: 'application/json',+ });+}++export async function shot(page: Page, name: string) {+ await page.screenshot({ path: `${SHOTS_DIR}/${name}.png`, fullPage: true });+}++export async function trpcQuery<T>(+ page: Page,+ path: string,+ input?: unknown+): Promise<T> {+ const query =+ input === undefined+ ? ''+ : `?input=${encodeURIComponent(JSON.stringify({ json: input }))}`;+ const response = await page.request.get(`${API_URL}/trpc/${path}${query}`);+ const body = await response.json();+ if (!response.ok()) {+ throw new Error(+ `${path} ${response.status()}: ${JSON.stringify(body).slice(0, 300)}`+ );+ }+ return body.result.data.json as T;+}++export async function trpcMutation<T>(+ page: Page,+ path: string,+ input: unknown+): Promise<{ status: number; data?: T; error?: string }> {+ const response = await page.request.post(`${API_URL}/trpc/${path}`, {+ data: { json: input },+ });+ const body = await response.json();+ if (!response.ok()) {+ return { status: response.status(), error: body?.error?.json?.message };+ }+ return { status: response.status(), data: body.result.data.json as T };+}++/**+ * `networkidle` can be reached before React has hydrated the sidebar on the dev+ * server, so a first click on a menu trigger is sometimes swallowed.+ */+export async function openMenu(+ page: Page,+ trigger: import('@playwright/test').Locator+) {+ const menu = page.getByRole('menu');+ // A menu that is still animating out would satisfy the wait below.+ await menu.first().waitFor({ state: 'detached' });+ for (let attempt = 0; attempt < 10; attempt++) {+ await trigger.click();+ try {+ await menu.first().waitFor({ state: 'visible', timeout: 1500 });+ return menu.first();+ } catch {+ // Not hydrated yet; click again.+ if (await menu.first().isVisible()) {+ return menu.first();+ }+ }+ }+ throw new Error('Menu did not open');+}++/**+ * `networkidle` is not hydration: on the dev server the SSR markup is+ * interactive-looking well before React attaches, and input typed in that+ * window never reaches react-hook-form. React tags hydrated nodes with a+ * `__reactFiber$` key, which is the only client-side signal available.+ */+export async function gotoHydrated(page: Page, url: string) {+ const isHydrated = () => {+ const probe = document.querySelector('button, a');+ return (+ !!probe &&+ Object.keys(probe).some((key) => key.startsWith('__reactFiber$'))+ );+ };+ // A 502 on one module request (Vite re-optimizing under load) leaves the+ // page server-rendered forever; only a fresh load recovers.+ for (let attempt = 0; attempt < HYDRATION_ATTEMPTS; attempt++) {+ await page.goto(url);+ try {+ await page.waitForLoadState('networkidle', {+ timeout: HYDRATION_TIMEOUT_MS,+ });+ await page.waitForFunction(isHydrated, undefined, {+ timeout: HYDRATION_TIMEOUT_MS,+ });+ return;+ } catch {+ // Try again with a fresh document.+ }+ }+ throw new Error(`${url} did not hydrate`);+}++export async function dismissFeedbackPrompt(page: Page) {+ const { hostname } = new URL(+ process.env.DASHBOARD_URL ?? 'https://main.local.openpanel.cc'+ );+ await page.context().addCookies([+ {+ name: 'feedback-prompt-seen',+ value: new Date().toISOString(),+ domain: hostname,+ path: '/',+ },+ ]);+}++export function expectNoCrashes(problems: Problems) {+ if (problems.pageErrors.length > 0) {+ throw new Error(`Uncaught page errors: ${problems.pageErrors.join(' | ')}`);+ }+}++/** Dev-server 502s on module requests come from Vite re-optimizing under load. */+export function apiFailures(problems: Problems) {+ return problems.badResponses.filter(+ (line) => line.includes('/trpc/') && !line.startsWith('502')+ );+}++export async function deleteProjectThroughSettings(+ page: Page,+ organizationId: string,+ projectId: string+) {+ await gotoHydrated(page, `/${organizationId}/${projectId}/settings`);+ await page.getByRole('button', { name: 'Delete Project' }).click();+ const dialog = page.getByRole('dialog');+ await dialog.getByRole('button', { name: /^(Yes|Confirm|Delete)/ }).click();+ await page.getByText('Project is scheduled for deletion').waitFor();+}diff --git a/apps/start/e2e/org-members.spec.ts b/apps/start/e2e/org-members.spec.tsnew file mode 100644index 000000000..b2730de21--- /dev/null+++ b/apps/start/e2e/org-members.spec.ts@@ -0,0 +1,462 @@+import type { Browser, Locator, Page } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ API_URL,+ dismissFeedbackPrompt,+ expectNoCrashes,+ gotoHydrated,+ openMenu,+ shot,+ trpcMutation,+ trpcQuery,+ watchProblems,+} from './org-helpers';++// The shared dev server takes 10-25s per navigation while other suites run.+const SLOW_DEV_SERVER_TIMEOUT_MS = 420_000;+const GUEST_PASSWORD = 'E2e-org-Passw0rd!';+const HTTP_FORBIDDEN = 403;+const HTTP_OK = 200;+const ACTION_TIMEOUT_MS = 30_000;+const ROW_ACTION_TIMEOUT_MS = 3000;++test.setTimeout(SLOW_DEV_SERVER_TIMEOUT_MS);+test.use({ actionTimeout: ACTION_TIMEOUT_MS });++interface Invite {+ id: string;+ email: string;+ role: string;+ projectAccess: { projectId: string; level: string }[];+}++interface Member {+ id: string;+ userId: string;+ email: string;+ role: string;+ access: { projectId: string; level: string }[];+}++const THROWAWAY_EMAIL = /^e2e-org-\d+@example\.com$/;++const uniqueEmail = () => `e2e-org-${Date.now()}@example.com`;++const listInvites = (page: Page, organizationId: string) =>+ trpcQuery<Invite[]>(page, 'organization.invitations', { organizationId });++const listMembers = (page: Page, organizationId: string) =>+ trpcQuery<Member[]>(page, 'organization.members', { organizationId });++const openInviteSheet = async (page: Page): Promise<Locator> => {+ await page.getByRole('button', { name: 'Invite user' }).click();+ const sheet = page.getByRole('dialog');+ await expect(+ sheet.getByRole('heading', { name: 'Invite a user' })+ ).toBeVisible();+ return sheet;+};++const sendInvite = (sheet: Locator) =>+ sheet.getByRole('button', { name: 'Invite user' }).click();++const pickProjects = async (page: Page, scope: Locator, names: string[]) => {+ await scope+ .getByRole('button', { name: /Restrict access to projects/ })+ .click();+ for (const name of names) {+ await page.getByRole('option', { name }).click();+ }+ await page.keyboard.press('Escape');+};++// The row menu unmounts when the table re-renders after a refetch, so a click+// can land on a menu that is already gone; open it again until the click sticks.+const clickRowAction = async (page: Page, email: string, action: string) => {+ const trigger = page+ .getByRole('row', { name: new RegExp(email) })+ .getByRole('button');+ await expect(async () => {+ const menu = await openMenu(page, trigger);+ await menu+ .getByRole('menuitem', { name: action })+ .click({ timeout: ROW_ACTION_TIMEOUT_MS });+ }).toPass({ timeout: ACTION_TIMEOUT_MS * 2 });+};++const guestStatus = async (guest: Page, path: string, input: unknown) => {+ const query = encodeURIComponent(JSON.stringify({ json: input }));+ const response = await guest.request.get(+ `${API_URL}/trpc/${path}?input=${query}`+ );+ return response.status();+};++const signUpThroughInvite = async (+ browser: Browser,+ inviteId: string,+ email: string+) => {+ const context = await browser.newContext({+ ignoreHTTPSErrors: true,+ storageState: { cookies: [], origins: [] },+ });+ const guest = await context.newPage();+ await dismissFeedbackPrompt(guest);+ await gotoHydrated(guest, `/onboarding?inviteId=${inviteId}`);+ await expect(guest.getByText('Invitation to Acme')).toBeVisible();+ await guest.getByLabel('First name').fill('E2E');+ await guest.getByLabel('Last name').fill('OrgMember');+ await guest.getByLabel('Email').fill(email);+ await guest.getByLabel('Password', { exact: true }).fill(GUEST_PASSWORD);+ await guest.getByLabel('Confirm password').fill(GUEST_PASSWORD);+ await guest.getByRole('button', { name: 'Create account' }).click();+ await guest.waitForURL(/\/acme$/, { timeout: 60_000 });+ return { context, guest };+};++test.beforeEach(async ({ page }) => {+ await dismissFeedbackPrompt(page);+});++// A test that fails halfway must not leave invites or members on the seeded organization.+test.afterEach(async ({ page, seed }) => {+ const { organizationId } = seed;+ for (const invite of await listInvites(page, organizationId)) {+ if (THROWAWAY_EMAIL.test(invite.email)) {+ await trpcMutation(page, 'organization.revokeInvite', {+ inviteId: invite.id,+ });+ }+ }+ for (const member of await listMembers(page, organizationId)) {+ if (THROWAWAY_EMAIL.test(member.email)) {+ await trpcMutation(page, 'organization.removeMember', {+ organizationId,+ userId: member.userId,+ id: member.id,+ });+ }+ }+});++test('the members tab lists the admin and filters by email', async ({+ page,+ seed,+}) => {+ const problems = watchProblems(page);+ await gotoHydrated(page, `/${seed.organizationId}/members`);+ await expect(page).toHaveURL(/\/members\/members$/);+ const adminRow = page.getByRole('row', { name: new RegExp(seed.user.email) });+ await expect(adminRow).toContainText('Seed Admin');+ await expect(adminRow).toContainText('org:admin');+ await expect(adminRow).toContainText('All projects');++ const search = page.getByRole('textbox', { name: 'Search email' });+ await search.click();+ await search.fill('nobody-matches-this');+ await expect(adminRow).toHaveCount(0);+ await expect(page.getByText('No data')).toBeVisible();+ await search.fill('admin@');+ await expect(adminRow).toBeVisible();++ await page.getByRole('tab', { name: 'Invitations' }).click();+ await expect(page).toHaveURL(/\/members\/invitations$/);+ await expect(page.getByRole('button', { name: 'Invite user' })).toBeVisible();+ await page.goBack();+ await expect(page).toHaveURL(/\/members\/members/);+ await shot(page, 'members');+ expectNoCrashes(problems);+});++test('invite validation: bad email, existing member, duplicate', async ({+ page,+ seed,+}) => {+ const email = uniqueEmail();+ await gotoHydrated(page, `/${seed.organizationId}/members/invitations`);+ const sheet = await openInviteSheet(page);++ await sendInvite(sheet);+ await expect(sheet.getByText('Issues')).toBeVisible();+ await sheet.getByLabel('Email').fill('not-an-email');+ await sendInvite(sheet);+ await expect(sheet.getByText('Issues')).toBeVisible();++ await sheet.getByLabel('Email').fill(seed.user.email.toUpperCase());+ await sendInvite(sheet);+ await expect(+ page.getByText('User is already a member of the organization')+ ).toBeVisible();++ await sheet.getByLabel('Email').fill(email);+ await sendInvite(sheet);+ await expect(+ sheet.getByRole('heading', { name: 'User has been invited' })+ ).toBeVisible();+ await expect(sheet.getByText('We have sent an email')).toBeVisible();++ await sheet.getByRole('button', { name: 'Invite another user' }).click();+ await sheet.getByLabel('Email').fill(email.toUpperCase());+ await sendInvite(sheet);+ await expect(+ page.getByText('User is already invited to the organization')+ ).toBeVisible();+ await sheet.getByRole('button', { name: 'Close' }).first().click();++ await clickRowAction(page, email, 'Revoke invite');+ await expect(page.getByText(`Invite for ${email} revoked`)).toBeVisible();+ await expect(page.getByRole('row', { name: new RegExp(email) })).toHaveCount(+ 0+ );+ const invites = await listInvites(page, seed.organizationId);+ expect(invites.find((invite) => invite.email === email)).toBeUndefined();+});++test('an admin invite with project access shows in the list and can be copied and revoked', async ({+ page,+ seed,+ context,+}) => {+ const email = uniqueEmail();+ await gotoHydrated(page, `/${seed.organizationId}/members/invitations`);+ const sheet = await openInviteSheet(page);+ await sheet.getByLabel('Email').fill(email);+ await sheet.getByRole('radio', { name: 'Admin' }).click();+ await pickProjects(page, sheet, ['Acme Web', 'Acme Shop']);+ await sheet.getByRole('combobox').first().click();+ await page.getByRole('option', { name: 'Read-only' }).click();+ await shot(page, 'invite-sheet');+ await sendInvite(sheet);+ await expect(+ sheet.getByRole('heading', { name: 'User has been invited' })+ ).toBeVisible();+ await sheet.getByRole('button', { name: 'Close' }).first().click();++ const row = page.getByRole('row', { name: new RegExp(email) });+ await expect(row).toContainText('org:admin');+ await expect(row).toContainText('Acme Web (read-only)');+ await expect(row).toContainText('Acme Shop');+ await shot(page, 'invitations');++ const invites = await listInvites(page, seed.organizationId);+ const invite = invites.find((candidate) => candidate.email === email);+ expect(invite?.role).toBe('org:admin');+ expect(invite?.projectAccess).toEqual([+ { projectId: 'acme-web', level: 'read' },+ { projectId: 'acme-shop', level: 'write' },+ ]);++ await context.grantPermissions(['clipboard-read', 'clipboard-write']);+ await clickRowAction(page, email, 'Copy invite link');+ const copied = await page.evaluate(() => navigator.clipboard.readText());+ expect(copied).toMatch(new RegExp(`/onboarding\\?inviteId=${invite?.id}$`));++ await clickRowAction(page, email, 'Revoke invite');+ await expect(row).toHaveCount(0);++ // A revoked link no longer offers the organization.+ const guestContext = await page+ .context()+ .browser()+ ?.newContext({+ ignoreHTTPSErrors: true,+ storageState: { cookies: [], origins: [] },+ });+ if (!guestContext) {+ throw new Error('No browser');+ }+ const guest = await guestContext.newPage();+ await guest.goto(`/onboarding?inviteId=${invite?.id}`);+ await expect(+ guest.getByRole('button', { name: 'Create account' })+ ).toBeVisible();+ await expect(guest.getByText('Invitation to Acme')).toHaveCount(0);+ await guestContext.close();+});++// BUG org-8: the picker offers "Pick '<typed text>'" for anything that is not a project.+test('the project access picker only offers projects', async ({+ page,+ seed,+}) => {+ await gotoHydrated(page, `/${seed.organizationId}/members/invitations`);+ const sheet = await openInviteSheet(page);+ await sheet+ .getByRole('button', { name: /Restrict access to projects/ })+ .click();+ await expect(page.getByRole('option', { name: 'Acme Web' })).toBeVisible();+ await page.keyboard.type('no-such-project');+ await expect(page.getByRole('option', { name: /Pick/ })).toHaveCount(0);+});++test.describe('an invited member', () => {+ test('joins with restricted access, sees a restricted UI, and follows access edits', async ({+ page,+ seed,+ browser,+ }) => {+ const email = uniqueEmail();+ await gotoHydrated(page, `/${seed.organizationId}/members/invitations`);+ const sheet = await openInviteSheet(page);+ await sheet.getByLabel('Email').fill(email);+ await pickProjects(page, sheet, ['Acme Web', 'Acme Shop']);+ await sheet.getByRole('combobox').first().click();+ await page.getByRole('option', { name: 'Read-only' }).click();+ await sendInvite(sheet);+ await expect(+ sheet.getByRole('heading', { name: 'User has been invited' })+ ).toBeVisible();+ const invite = (await listInvites(page, seed.organizationId)).find(+ (candidate) => candidate.email === email+ );+ if (!invite) {+ throw new Error('Invite was not created');+ }++ const { context, guest } = await signUpThroughInvite(+ browser,+ invite.id,+ );+ try {+ // Accepting consumes the invite and creates the membership with the grants.+ expect(+ (await listInvites(page, seed.organizationId)).some(+ (candidate) => candidate.email === email+ )+ ).toBe(false);+ const member = (await listMembers(page, seed.organizationId)).find(+ (candidate) => candidate.email === email+ );+ expect(member?.role).toBe('org:member');+ expect(+ member?.access.map((grant) => [grant.projectId, grant.level])+ ).toEqual([+ ['acme-web', 'read'],+ ['acme-shop', 'write'],+ ]);++ // Restricted UI for a non-admin.+ await expect(guest.locator('.card')).toHaveCount(2);+ await expect(+ guest.getByRole('link', { name: 'Members', exact: true })+ ).toHaveCount(0);+ await expect(+ guest.getByRole('link', { name: 'Billing', exact: true })+ ).toHaveCount(0);+ await expect(+ guest.getByRole('button', { name: /Create a project|Invite a user/ })+ ).toHaveCount(0);+ await guest.screenshot({+ path: 'test-results/org/shots/member-projects.png',+ });+ for (const adminOnly of ['members', 'billing']) {+ await guest.goto(`/${seed.organizationId}/${adminOnly}`);+ await expect(guest).toHaveURL(new RegExp(`/${seed.organizationId}$`));+ }+ await guest.goto(`/${seed.organizationId}/acme-saas`);+ await expect(+ guest.getByText('You do not have access to this project')+ ).toBeVisible();+ expect(+ await guestStatus(guest, 'organization.members', {+ organizationId: seed.organizationId,+ })+ ).toBe(HTTP_FORBIDDEN);++ // The admin edits the member's access.+ await gotoHydrated(page, `/${seed.organizationId}/members`);+ const row = page.getByRole('row', { name: new RegExp(email) });+ await expect(row).toContainText('E2E OrgMember');+ await clickRowAction(page, email, 'Edit access');+ const dialog = page.getByRole('dialog');+ await expect(+ dialog.getByRole('heading', { name: 'Edit access for E2E OrgMember' })+ ).toBeVisible();+ await expect(dialog.getByRole('combobox').first()).toHaveText(+ 'Read-only'+ );+ await dialog+ .getByRole('button', { name: /Acme Web/ })+ .first()+ .click();+ await page.getByRole('option', { name: 'Acme Shop' }).click();+ await page.getByRole('option', { name: 'Acme App' }).click();+ await page.keyboard.press('Escape');+ await dialog.getByRole('button', { name: 'Save' }).click();+ await expect(page.getByText('Access updated')).toBeVisible();+ await expect(row).toContainText('acme-app');+ await expect(row).not.toContainText('acme-shop');++ await gotoHydrated(guest, `/${seed.organizationId}`);+ await expect(guest.locator('.card')).toHaveCount(2);+ await expect(+ guest.locator('.card', { hasText: 'Acme App' })+ ).toBeVisible();+ await expect(+ guest.locator('.card', { hasText: 'Acme Shop' })+ ).toHaveCount(0);++ // Cancel leaves access untouched.+ await clickRowAction(page, email, 'Edit access');+ await dialog.getByRole('button', { name: 'Cancel' }).click();+ await expect(dialog).toBeHidden();+ } finally {+ await gotoHydrated(page, `/${seed.organizationId}/members`);+ await clickRowAction(page, email, 'Remove member');+ await expect(+ page.getByText('E2E has been removed from the organization')+ ).toBeVisible();+ await expect(+ page.getByRole('row', { name: new RegExp(email) })+ ).toHaveCount(0);+ await context.close();+ }+ });++ // BUG org-0: removeMember and updateMemberAccess do not clear the 5-minute+ // access cache, so the removed user keeps reading project data.+ test('loses access as soon as an admin removes them', async ({+ page,+ seed,+ browser,+ }) => {+ const email = uniqueEmail();+ await gotoHydrated(page, `/${seed.organizationId}/members/invitations`);+ const sheet = await openInviteSheet(page);+ await sheet.getByLabel('Email').fill(email);+ await sendInvite(sheet);+ await expect(+ sheet.getByRole('heading', { name: 'User has been invited' })+ ).toBeVisible();+ const invite = (await listInvites(page, seed.organizationId)).find(+ (candidate) => candidate.email === email+ );+ if (!invite) {+ throw new Error('Invite was not created');+ }+ const { context, guest } = await signUpThroughInvite(+ browser,+ invite.id,+ );+ const readProject = () =>+ guestStatus(guest, 'chart.projectCard', { projectId: 'acme-web' });+ try {+ expect(await readProject()).toBe(HTTP_OK);+ } finally {+ await gotoHydrated(page, `/${seed.organizationId}/members`);+ await clickRowAction(page, email, 'Remove member');+ await expect(+ page.getByRole('row', { name: new RegExp(email) })+ ).toHaveCount(0);+ }+ expect.soft(await readProject()).toBe(HTTP_FORBIDDEN);+ await guest.goto(`/${seed.organizationId}`);+ await expect+ .soft(guest.getByRole('link', { name: 'Settings', exact: true }))+ .toHaveCount(0);+ await context.close();+ });+});diff --git a/apps/start/e2e/org-multi.spec.ts b/apps/start/e2e/org-multi.spec.tsnew file mode 100644index 000000000..d9fc54beb--- /dev/null+++ b/apps/start/e2e/org-multi.spec.ts@@ -0,0 +1,178 @@+import type { Page } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ API_URL,+ dismissFeedbackPrompt,+ expectNoCrashes,+ gotoHydrated,+ openMenu,+ watchProblems,+} from './org-helpers';++// The shared dev server takes 10-25s per navigation while other suites run.+const SLOW_DEV_SERVER_TIMEOUT_MS = 540_000;+const NAVIGATION_TIMEOUT_MS = 60_000;+const PASSWORD = 'E2e-org-Passw0rd!';++test.setTimeout(SLOW_DEV_SERVER_TIMEOUT_MS);+// A second organization on the seeded admin would turn `/` into a workspace+// picker for every other suite, and an organization cannot be left once+// joined. So this runs as its own user, which nothing else depends on.+test.use({ storageState: { cookies: [], origins: [] } });++interface Organization {+ id: string;+ name: string;+ deleteAt: string | null;+}++const listOrganizations = async (page: Page) => {+ const response = await page.request.get(`${API_URL}/trpc/organization.list`);+ const body = await response.json();+ return body.result.data.json as Organization[];+};++const createWorkspace = async (+ page: Page,+ workspace: string,+ project: string+) => {+ await gotoHydrated(page, '/onboarding/project');+ const createNew = page.getByRole('button', { name: 'Create new workspace' });+ if (await createNew.isVisible()) {+ await createNew.click();+ }+ await page.getByLabel('Workspace name').fill(workspace);+ await page.getByLabel('Your first project name').fill(project);+ await page.getByRole('button', { name: 'Backend / API' }).click();+ await page.getByRole('button', { name: 'Next' }).click();+ await page.waitForURL(/\/onboarding\/.+\/connect/, {+ timeout: NAVIGATION_TIMEOUT_MS,+ });+ const organization = (await listOrganizations(page)).find(+ (candidate) => candidate.name === workspace+ );+ if (!organization) {+ throw new Error(`Workspace "${workspace}" was not created`);+ }+ return organization;+};++const scheduleDeletion = async (page: Page, name: string) => {+ await page.getByRole('button', { name: 'Delete Organization' }).click();+ const dialog = page.getByRole('dialog');+ await dialog.getByRole('textbox').fill(name);+ await dialog.getByRole('button', { name: 'Delete organization' }).click();+ await expect(+ page.getByText('Organization scheduled for deletion').first()+ ).toBeVisible();+};++test('a user with two organizations can switch between them and delete one', async ({+ page,+}) => {+ const problems = watchProblems(page);+ const stamp = Date.now();+ const first = `E2E org A ${stamp}`;+ const second = `E2E org B ${stamp}`;+ await dismissFeedbackPrompt(page);++ await gotoHydrated(page, '/onboarding');+ await page.getByLabel('First name').fill('E2E');+ await page.getByLabel('Last name').fill('OrgOwner');+ await page.getByLabel('Email').fill(`e2e-org-owner-${stamp}@example.com`);+ await page.getByLabel('Password', { exact: true }).fill(PASSWORD);+ await page.getByLabel('Confirm password').fill(PASSWORD);+ await page.getByRole('button', { name: 'Create account' }).click();+ await page.waitForURL(/\/onboarding\/project/, {+ timeout: NAVIGATION_TIMEOUT_MS,+ });++ const firstOrganization = await createWorkspace(+ page,+ first,+ 'E2E org first project'+ );+ // With a single organization `/` goes straight to it.+ await page.goto('/');+ await expect(page).toHaveURL(new RegExp(`/${firstOrganization.id}$`));++ await gotoHydrated(page, `/${firstOrganization.id}`);+ await openMenu(page, page.getByRole('combobox').first());+ await page.getByRole('menuitem', { name: 'New organization' }).click();+ await expect(page).toHaveURL(/\/onboarding\/project/);+ const secondOrganization = await createWorkspace(+ page,+ second,+ 'E2E org second project'+ );++ // With two, `/` is a picker.+ await gotoHydrated(page, '/');+ await expect(+ page.getByRole('link', { name: new RegExp(first) })+ ).toHaveAttribute('href', `/${firstOrganization.id}`);+ await page.getByRole('link', { name: new RegExp(second) }).click();+ await expect(page).toHaveURL(new RegExp(`/${secondOrganization.id}$`));+ await expect(page.locator('.card')).toContainText('E2E org second project');++ const selector = page.getByRole('combobox').first();+ await expect(selector).toHaveText(second);+ await openMenu(page, selector);+ await page.getByRole('menuitem', { name: first, exact: true }).click();+ await expect(page).toHaveURL(new RegExp(`/${firstOrganization.id}$`));+ await expect(selector).toHaveText(first);+ await expect(page.locator('.card')).toContainText('E2E org first project');++ // The seeded organization is not theirs.+ const foreign = await page.goto('/acme');+ expect(foreign?.status()).toBe(404);+ await expect(page.getByText(/not found/i).first()).toBeVisible();+ const foreignApi = await page.request.get(+ `${API_URL}/trpc/project.list?input=${encodeURIComponent(+ JSON.stringify({ json: { organizationId: 'acme' } })+ )}`+ );+ expect(foreignApi.status()).toBe(403);++ // Delete the second organization: confirm modal, schedule, cancel, schedule.+ await gotoHydrated(page, `/${secondOrganization.id}/settings`);+ await page.getByRole('button', { name: 'Delete Organization' }).click();+ const dialog = page.getByRole('dialog');+ const confirm = dialog.getByRole('button', { name: 'Delete organization' });+ await expect(confirm).toBeDisabled();+ await dialog.getByRole('textbox').fill('something else');+ await expect(confirm).toBeDisabled();+ await dialog.getByRole('button', { name: 'Cancel' }).click();+ await expect(dialog).toBeHidden();+ expect(+ (await listOrganizations(page)).find(+ (org) => org.id === secondOrganization.id+ )?.deleteAt+ ).toBeNull();++ await scheduleDeletion(page, second);+ await expect(+ page.getByText('This organization will be deleted on')+ ).toBeVisible();+ await expect(+ page.getByRole('button', { name: 'Delete Organization' })+ ).toBeDisabled();+ await page.screenshot({+ path: 'test-results/org/shots/organization-scheduled-for-deletion.png',+ fullPage: true,+ });++ await page.getByRole('button', { name: 'Cancel deletion' }).click();+ await expect(page.getByText('Organization deletion cancelled')).toBeVisible();+ await expect(+ page.getByRole('button', { name: 'Delete Organization' })+ ).toBeEnabled();++ await scheduleDeletion(page, second);+ await gotoHydrated(page, `/${firstOrganization.id}/settings`);+ await scheduleDeletion(page, first);+ const remaining = await listOrganizations(page);+ expect(remaining.every((org) => org.deleteAt !== null)).toBe(true);+ expectNoCrashes(problems);+});diff --git a/apps/start/e2e/org-projects.spec.ts b/apps/start/e2e/org-projects.spec.tsnew file mode 100644index 000000000..f0624944f--- /dev/null+++ b/apps/start/e2e/org-projects.spec.ts@@ -0,0 +1,189 @@+import { expect, test } from './fixtures';+import {+ apiFailures,+ dismissFeedbackPrompt,+ expectNoCrashes,+ gotoHydrated,+ shot,+ trpcQuery,+ watchProblems,+} from './org-helpers';++const LAZY_FROM_INDEX = 6;+const MOBILE_VIEWPORT = { width: 390, height: 844 };+// The shared dev server takes 10-25s per navigation while other suites run.+const SLOW_DEV_SERVER_TIMEOUT_MS = 150_000;++test.setTimeout(SLOW_DEV_SERVER_TIMEOUT_MS);++interface ProjectListItem {+ id: string;+ name: string;+}++interface ProjectCardData {+ metrics: { months_3: number; month: number; day: number; revenue: number };+}++const compact = (value: number) =>+ new Intl.NumberFormat('en-US', { notation: 'compact' }).format(value);++test.beforeEach(async ({ page }) => {+ await dismissFeedbackPrompt(page);+});++test('project cards show the numbers the API returns', async ({+ page,+ seed,+}) => {+ const problems = watchProblems(page);+ await gotoHydrated(page, `/${seed.organizationId}`);++ for (const project of seed.projects) {+ const data = await trpcQuery<ProjectCardData>(page, 'chart.projectCard', {+ projectId: project.id,+ });+ const card = page.locator('.card', {+ has: page.locator(`a[href="/${seed.organizationId}/${project.id}"]`),+ });+ await expect(card).toContainText(project.name);+ await expect(card).toContainText(`3M${compact(data.metrics.months_3)}`);+ await expect(card).toContainText(`30D${compact(data.metrics.month)}`);+ // The 24h window moves while the test runs; the API call and the card+ // can land on either side of a session falling out of it.+ const dayText = (await card.innerText()).match(/24H\s*(\d+)/)?.[1];+ const shown = Number(dayText);+ expect(Math.abs(shown - data.metrics.day)).toBeLessThanOrEqual(3);+ await expect(card.locator('svg.recharts-surface')).toBeVisible();+ if (data.metrics.revenue > 0) {+ await expect(card).toContainText('Revenue');+ }+ }+ await shot(page, 'projects');+ expectNoCrashes(problems);+ expect(apiFailures(problems)).toEqual([]);+});++test('a card links to the project and its cog opens project settings', async ({+ page,+ seed,+}) => {+ await gotoHydrated(page, `/${seed.organizationId}`);+ const card = page.locator('.card', { hasText: 'Acme Shop' });+ await card.locator('a').nth(1).click();+ await expect(page).toHaveURL(/\/acme\/acme-shop\/settings/);++ await page.goBack();+ await card.locator('a').first().click();+ await expect(page).toHaveURL(/\/acme\/acme-shop$/);+});++// BUG org-1: LinkButton passes `href` to TanStack's <Link>, which renders the+// current location instead. Open-in-new-tab and copy-link go to /acme.+test('the cog on a card carries the project settings href', async ({+ page,+ seed,+}) => {+ await gotoHydrated(page, `/${seed.organizationId}`);+ const cog = page+ .locator('.card', { hasText: 'Acme Shop' })+ .locator('a')+ .nth(1);+ await expect(cog).toHaveAttribute(+ 'href',+ `/${seed.organizationId}/acme-shop/settings`+ );+});++test('search filters cards, lives in the URL and survives a reload', async ({+ page,+ seed,+}) => {+ await gotoHydrated(page, `/${seed.organizationId}`);+ const search = page.getByRole('textbox', { name: 'Search projects' });+ await search.click();+ await search.fill('acme sh');+ await expect(page.locator('.card')).toHaveCount(1);+ await expect(page.locator('.card')).toContainText('Acme Shop');+ await expect(page).toHaveURL(/search=acme(\+|%20)sh/);++ await page.reload();+ await expect(+ page.getByRole('textbox', { name: 'Search projects' })+ ).toHaveValue('acme sh');+ await expect(page.locator('.card')).toHaveCount(1);++ await gotoHydrated(+ page,+ `/${seed.organizationId}?search=zzz-no-such-project`+ );+ await expect(page.locator('.card')).toHaveCount(0);+ await expect(page.getByRole('heading', { name: 'Projects' })).toBeVisible();+ await page.getByRole('button', { name: 'Clear search' }).click();+ await expect(page.locator('.card').first()).toBeVisible();+ await expect(page).not.toHaveURL(/search=/);+});++test('cards below the fold load when scrolled to', async ({ page, seed }) => {+ const projects = await trpcQuery<ProjectListItem[]>(page, 'project.list', {+ organizationId: seed.organizationId,+ });+ test.skip(+ projects.length <= LAZY_FROM_INDEX,+ 'needs more than six projects to have a lazy card'+ );+ await page.setViewportSize({ width: 1280, height: 500 });+ await gotoHydrated(page, `/${seed.organizationId}`);+ const cards = page.locator('.card:has(a[href])');+ await expect(cards.first()).toBeVisible();+ expect(await cards.count()).toBeLessThan(projects.length);+ // Lazy cards mount when they enter the viewport, so walk down the page.+ await expect(async () => {+ await page.mouse.wheel(0, 400);+ expect(await cards.count()).toBe(projects.length);+ }).toPass({ timeout: 60_000 });+});++test('the projects page fits a phone and the sidebar opens', async ({+ page,+ seed,+}) => {+ await page.setViewportSize(MOBILE_VIEWPORT);+ await gotoHydrated(page, `/${seed.organizationId}`);+ await expect(page.locator('.card').first()).toContainText('3M');+ const overflow = await page.evaluate(+ () => document.documentElement.scrollWidth - window.innerWidth+ );+ expect(overflow).toBeLessThanOrEqual(0);++ const settingsLink = page.getByRole('link', {+ name: 'Settings',+ exact: true,+ });+ await expect(settingsLink).not.toBeInViewport();+ await page.locator('button:has(svg.lucide-menu)').click();+ await expect(settingsLink).toBeInViewport();+ await shot(page, 'projects-mobile-sidebar');+ await settingsLink.click();+ await expect(page).toHaveURL(/\/acme\/settings$/);+ await expect(settingsLink).not.toBeInViewport();+});++test('an organization the user is not a member of is refused', async ({+ page,+}) => {+ const response = await page.goto('/e2e-org-not-a-member');+ expect(response?.status()).toBe(404);+ await expect(page.getByText(/not found/i).first()).toBeVisible();+ await expect(page.getByRole('link', { name: 'Projects' })).toHaveCount(0);+ await shot(page, 'foreign-org');+});++// BUG org-2: same LinkButton `href` problem; the button points at the page it is on.+test('"Go to home" on the not-found page leaves the page', async ({ page }) => {+ await gotoHydrated(page, '/e2e-org-not-a-member');+ const home = page.getByRole('link', { name: 'Go to home' });+ await expect.soft(home).toHaveAttribute('href', '/');+ await home.click();+ await expect(page).not.toHaveURL(/e2e-org-not-a-member/);+});diff --git a/apps/start/e2e/org-settings.spec.ts b/apps/start/e2e/org-settings.spec.tsnew file mode 100644index 000000000..7c3a9ad39--- /dev/null+++ b/apps/start/e2e/org-settings.spec.ts@@ -0,0 +1,168 @@+import type { Page } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ dismissFeedbackPrompt,+ expectNoCrashes,+ gotoHydrated,+ shot,+ trpcQuery,+ watchProblems,+} from './org-helpers';++// The shared dev server takes 10-25s per navigation while other suites run.+const SLOW_DEV_SERVER_TIMEOUT_MS = 180_000;+const SEEDED_NAME = 'Acme';+const SEEDED_TIMEZONE = 'UTC';+// Same offset as UTC all year, so other suites' numbers do not move while this runs.+const OTHER_TIMEZONE = 'Africa/Abidjan';++test.setTimeout(SLOW_DEV_SERVER_TIMEOUT_MS);++interface Organization {+ name: string;+ timezone: string;+}++const getOrganization = (page: Page, organizationId: string) =>+ trpcQuery<Organization>(page, 'organization.get', { organizationId });++const timezonePicker = (page: Page) =>+ page.locator('form').getByRole('combobox');+const saveButton = (page: Page) => page.getByRole('button', { name: 'Save' });++const pickTimezone = async (page: Page, zone: string) => {+ await timezonePicker(page).click();+ await page.getByRole('option', { name: zone, exact: true }).click();+ await expect(timezonePicker(page)).toHaveText(zone);+};++test.beforeEach(async ({ page, seed }) => {+ await dismissFeedbackPrompt(page);+ await gotoHydrated(page, `/${seed.organizationId}/settings`);+});++test.afterEach(async ({ page, seed }) => {+ const organization = await getOrganization(page, seed.organizationId);+ expect(organization.name).toBe(SEEDED_NAME);+ expect(organization.timezone).toBe(SEEDED_TIMEZONE);+});++test('the name can be changed and restored', async ({ page, seed }) => {+ const problems = watchProblems(page);+ const name = page.getByLabel('Name');+ const renamed = `Acme E2E org ${Date.now()}`;+ await expect(name).toHaveValue(SEEDED_NAME);+ await expect(saveButton(page)).toBeDisabled();++ await name.fill(renamed);+ await expect(saveButton(page)).toBeEnabled();+ // Keyboard submit.+ await name.press('Enter');+ await expect(page.getByText('Organization updated')).toBeVisible();+ await expect(saveButton(page)).toBeDisabled();+ try {+ expect((await getOrganization(page, seed.organizationId)).name).toBe(+ renamed+ );+ await page.reload();+ await expect(page.getByLabel('Name')).toHaveValue(renamed);+ // The selector is empty until organization.list loads on the client.+ await expect(page.getByRole('combobox').first()).toHaveText(renamed, {+ timeout: 30_000,+ });+ } finally {+ await gotoHydrated(page, `/${seed.organizationId}/settings`);+ await page.getByLabel('Name').fill(SEEDED_NAME);+ await saveButton(page).click();+ await expect(page.getByLabel('Name')).toHaveValue(SEEDED_NAME);+ await expect(saveButton(page)).toBeDisabled();+ }+ expectNoCrashes(problems);+});++// BUG org-7: saving refetches organization.get only; the sidebar reads organization.list.+test('the sidebar shows the new name right after a rename', async ({+ page,+}) => {+ const renamed = `Acme E2E org ${Date.now()}`;+ await page.getByLabel('Name').fill(renamed);+ await saveButton(page).click();+ await expect(page.getByText('Organization updated')).toBeVisible();+ try {+ await expect.soft(page.getByRole('combobox').first()).toHaveText(renamed);+ } finally {+ await page.getByLabel('Name').fill(SEEDED_NAME);+ await saveButton(page).click();+ await expect(saveButton(page)).toBeDisabled();+ }+});++// BUG org-5: the form has no resolver, so the server's zod issue list is shown raw.+test('a too short name gets a readable validation message', async ({+ page,+ seed,+}) => {+ await page.getByLabel('Name').fill('A');+ await saveButton(page).click();+ const toast = page.locator('[data-sonner-toast]').first();+ const inlineError = page.getByText('Issues');+ await expect(toast.or(inlineError).first()).toBeVisible();+ await shot(page, 'settings-short-name');+ expect((await getOrganization(page, seed.organizationId)).name).toBe(+ SEEDED_NAME+ );+ if (await toast.isVisible()) {+ await expect(toast).not.toContainText('"code"');+ }+});++test('the timezone can be changed, survives a reload and is restored', async ({+ page,+ seed,+}) => {+ await expect(timezonePicker(page)).toHaveText(SEEDED_TIMEZONE);+ await pickTimezone(page, OTHER_TIMEZONE);+ await saveButton(page).click();+ await expect(page.getByText('Organization updated')).toBeVisible();+ try {+ expect((await getOrganization(page, seed.organizationId)).timezone).toBe(+ OTHER_TIMEZONE+ );+ await page.reload();+ await expect(timezonePicker(page)).toHaveText(OTHER_TIMEZONE);+ } finally {+ await gotoHydrated(page, `/${seed.organizationId}/settings`);+ await pickTimezone(page, SEEDED_TIMEZONE);+ await saveButton(page).click();+ await expect(saveButton(page)).toBeDisabled();+ }+});++// BUG org-6: 419 zones in a list that cannot be searched.+test('the timezone picker can be searched', async ({ page }) => {+ await timezonePicker(page).click();+ await expect(page.getByRole('option').first()).toBeVisible();+ await page.keyboard.type('stockholm');+ await expect(+ page.getByRole('option', { name: 'Europe/Stockholm' })+ ).toBeVisible();+});++test('deleting is blocked while the seeded subscription is active', async ({+ page,+}) => {+ await expect(page.getByText('Cancel your subscription first')).toBeVisible();+ await expect(+ page.getByRole('button', { name: 'Delete Organization' })+ ).toBeDisabled();+});++test('the settings page fits a phone', async ({ page, seed }) => {+ await page.setViewportSize({ width: 390, height: 844 });+ await gotoHydrated(page, `/${seed.organizationId}/settings`);+ await expect(page.getByLabel('Name')).toBeVisible();+ const overflow = await page.evaluate(+ () => document.documentElement.scrollWidth - window.innerWidth+ );+ expect(overflow).toBeLessThanOrEqual(0);+});diff --git a/apps/start/e2e/org-sidebar.spec.ts b/apps/start/e2e/org-sidebar.spec.tsnew file mode 100644index 000000000..fe2de939e--- /dev/null+++ b/apps/start/e2e/org-sidebar.spec.ts@@ -0,0 +1,161 @@+import { expect, test } from './fixtures';+import {+ dismissFeedbackPrompt,+ expectNoCrashes,+ gotoHydrated,+ openMenu,+ trpcQuery,+ watchProblems,+} from './org-helpers';++const SELECTOR_PROJECT_LIMIT = 10;+// The shared dev server takes 10-25s per navigation while other suites run.+const SLOW_DEV_SERVER_TIMEOUT_MS = 150_000;++test.setTimeout(SLOW_DEV_SERVER_TIMEOUT_MS);++test.beforeEach(async ({ page, seed }) => {+ await dismissFeedbackPrompt(page);+ await gotoHydrated(page, `/${seed.organizationId}`);+});++test('the action button rotates and opens both actions', async ({ page }) => {+ const trigger = page.getByRole('button', {+ name: /Create a project|Invite a user/,+ });+ await expect(trigger).toContainText('Create a project');+ await expect(trigger).toContainText('Invite a user', { timeout: 6000 });++ await openMenu(page, trigger);+ await page.getByRole('menuitem', { name: 'Create a project' }).click();+ await expect(+ page.getByRole('dialog').getByRole('heading', { name: 'Create project' })+ ).toBeVisible();+ await page.keyboard.press('Escape');+ await expect(page.getByRole('dialog')).toBeHidden();++ await openMenu(page, trigger);+ await page.getByRole('menuitem', { name: 'Invite a user' }).click();+ await expect(+ page.getByRole('dialog').getByRole('heading', { name: 'Invite a user' })+ ).toBeVisible();+ await page.keyboard.press('Escape');+ await expect(page.getByRole('dialog')).toBeHidden();+});++test('the selector lists projects and organizations and switches project', async ({+ page,+ seed,+}) => {+ const problems = watchProblems(page);+ const projects = await trpcQuery<{ id: string; name: string }[]>(+ page,+ 'project.list',+ { organizationId: seed.organizationId }+ );+ const selector = page.getByRole('combobox').first();+ await expect(selector).toHaveText('Acme');+ const menu = await openMenu(page, selector);++ for (const project of projects.slice(0, SELECTOR_PROJECT_LIMIT)) {+ await expect(+ menu.getByRole('menuitem', { name: project.name, exact: true })+ ).toBeVisible();+ }+ const allProjects = menu.getByRole('menuitem', { name: 'All projects' });+ await expect(allProjects).toHaveCount(+ projects.length > SELECTOR_PROJECT_LIMIT ? 1 : 0+ );+ await expect(+ menu.getByRole('menuitem', { name: 'Acme', exact: true })+ ).toBeVisible();+ await expect(+ menu.getByRole('menuitem', { name: 'New organization' })+ ).toHaveAttribute('href', '/onboarding/project');++ await menu.getByRole('menuitem', { name: 'Acme Web', exact: true }).click();+ await expect(page).toHaveURL(/\/acme\/acme-web$/);+ await expect(selector).toHaveText('Acme Web');++ await openMenu(page, selector);+ await page.getByRole('menuitem', { name: 'Acme SaaS', exact: true }).click();+ await expect(page).toHaveURL(/\/acme\/acme-saas$/);+ expectNoCrashes(problems);+});++test('"Create new project" in the selector opens the modal', async ({+ page,+}) => {+ await openMenu(page, page.getByRole('combobox').first());+ await page.getByRole('menuitem', { name: 'Create new project' }).click();+ await expect(+ page.getByRole('dialog').getByRole('heading', { name: 'Create project' })+ ).toBeVisible();+});++test('navigation links reach every organization page', async ({ page }) => {+ const problems = watchProblems(page);+ const pages = [+ {+ link: 'Settings',+ url: /\/acme\/settings$/,+ heading: 'Workspace settings',+ },+ { link: 'Billing', url: /\/acme\/billing$/, heading: 'Billing' },+ { link: 'Members', url: /\/acme\/members\/members$/, heading: 'Members' },+ { link: 'Projects', url: /\/acme$/, heading: 'Projects' },+ ];+ for (const target of pages) {+ const link = page.getByRole('link', { name: target.link, exact: true });+ await link.click();+ await expect(page).toHaveURL(target.url);+ await expect(+ page.getByRole('heading', { name: target.heading, level: 1 })+ ).toBeVisible();+ await expect(link).toHaveAttribute('data-status', 'active');+ }+ await page.goBack();+ await expect(page).toHaveURL(/\/acme\/members\/members$/);+ await page.goForward();+ await expect(page).toHaveURL(/\/acme$/);+ expectNoCrashes(problems);+});++test('footer has docs, feedback and the profile menu', async ({ page }) => {+ const docs = page.getByRole('link', { name: 'Docs' });+ await expect(docs).toHaveAttribute('href', 'https://openpanel.dev/docs');+ await expect(docs).toHaveAttribute('target', '_blank');+ await expect(+ page.getByRole('button', { name: 'Give feedback' })+ ).toBeEnabled();++ const menu = await openMenu(+ page,+ page.getByRole('button', { name: 'Profile' })+ );+ await expect(menu.getByRole('menuitem', { name: 'Logout' })).toBeVisible();+ await menu.getByRole('menuitem', { name: 'Account' }).click();+ await expect(page).toHaveURL(/\/acme\/account/);+});++test('the theme switch applies, persists and restores', async ({ page }) => {+ const html = page.locator('html');+ const pickTheme = async (name: string) => {+ await openMenu(page, page.getByRole('button', { name: 'Profile' }));+ await page.getByRole('menuitem', { name: /Theme/ }).click();+ await page.getByRole('menuitem', { name }).click();+ };++ await pickTheme('Dark');+ await expect(html).toHaveClass(/dark/);+ await page.reload();+ await expect(html).toHaveClass(/dark/);++ await gotoHydrated(page, page.url());+ await pickTheme('Light');+ await expect(html).toHaveClass(/light/);+ await expect(html).not.toHaveClass(/dark/);++ await pickTheme('System');+ await expect(html).toHaveClass(/system/);+});diff --git a/apps/start/e2e/overview-chart.spec.ts b/apps/start/e2e/overview-chart.spec.tsnew file mode 100644index 000000000..03231df7d--- /dev/null+++ b/apps/start/e2e/overview-chart.spec.ts@@ -0,0 +1,353 @@+import { expect as baseExpect, test } from './fixtures';+import {+ attachProblems,+ compactNumber,+ dashedTailStartIndex,+ dismissFeedbackPrompt,+ expectNoRealProblems,+ fetchStats,+ gotoOverview,+ hoverBucket,+ mainChart,+ metricCard,+ overviewReady,+ SLOW_TEST_TIMEOUT_MS,+ shot,+ trpcMutation,+ trpcQuery,+ watchPage,+} from './overview-helpers';++// The shared dev stack answers slowly while other suites run against it.+const expect = baseExpect.configure({ timeout: 30_000 });+test.use({ actionTimeout: 20_000 });++const PROJECT = 'acme-shop';+const HOURS_PER_DAY = 24;+const CHART_MARGIN_X = 20;+// Spike markers sit on the line's peak; for the seeded spike that is the top of the plot.+const SPIKE_MARKER_OFFSET_Y = 16;++test.beforeEach(async ({ page }) => {+ test.setTimeout(SLOW_TEST_TIMEOUT_MS);+ await page.setViewportSize({ width: 1440, height: 1000 });+});++const TOOLTIP_CASES = [+ { range: '7d', interval: 'day', step: 1 },+ { range: 'yesterday', interval: 'hour', step: 3 },+ { range: 'lastMonth', interval: 'day', step: 4 },+] as const;++for (const { range, interval, step } of TOOLTIP_CASES) {+ test(`chart tooltip shows overview.stats values for ${range}/${interval}`, async ({+ page,+ }, testInfo) => {+ const problems = watchPage(page);+ await gotoOverview(page, PROJECT, `?range=${range}&metric=1`);+ await dismissFeedbackPrompt(page);+ const stats = await fetchStats(page, PROJECT, { range, interval });+ const buckets = stats.series.length;++ // The last point sits on the chart's right edge, where the tooltip is not reachable.+ for (let index = 0; index < buckets - 1; index += step) {+ const row = stats.series[index];+ const text = await hoverBucket(page, index, buckets);+ const lines = text.split('\n');+ const valueLine = lines[lines.indexOf('Sessions') + 1];+ expect(valueLine, `${row?.date}: ${text}`).toBe(+ new Intl.NumberFormat('en-US').format(Number(row?.total_sessions))+ );+ const previous = Number(row?.prev_total_sessions ?? 0);+ if (previous > 0 && previous !== Number(row?.total_sessions)) {+ expect(text, `${row?.date} previous`).toContain(+ `(${new Intl.NumberFormat('en-US').format(previous)})`+ );+ }+ expect(text).not.toMatch(/NaN|undefined|Infinity/);+ }+ await shot(page, `chart-tooltip-${range}`, false);++ await attachProblems(testInfo, problems);+ expect(expectNoRealProblems(problems)).toEqual({+ pageErrors: [],+ responses: [],+ });+ });+}++test('previous-period totals on the cards match the API and long ranges skip them', async ({+ page,+}) => {+ await gotoOverview(page, PROJECT, '?range=7d');+ const stats = await fetchStats(page, PROJECT, {});+ const current = Number(stats.metrics.total_sessions);+ const previous = Number(stats.metrics.prev_total_sessions);+ const diff = (Math.abs(current - previous) / previous) * 100;+ await expect(metricCard(page, 'Sessions')).toContainText(+ `${diff.toFixed(1)}%`+ );++ const series = stats.series;+ expect(series.every((row) => row.prev_total_sessions !== undefined)).toBe(+ true+ );+ const previousSum = series.reduce(+ (total, row) => total + Number(row.prev_total_sessions ?? 0),+ 0+ );+ expect(previousSum).toBe(previous);+});++test('the incomplete tail is dashed for a running period and solid for a finished one', async ({+ page,+}) => {+ await gotoOverview(page, PROJECT, '?range=7d');+ const week = await fetchStats(page, PROJECT, {});+ expect(await dashedTailStartIndex(page, week.series.length)).toBe(+ week.series.length - 2+ );++ await page.goto(`/acme/${PROJECT}?range=yesterday`);+ await overviewReady(page);+ await expect(mainChart(page).locator('svg').first()).toBeVisible();+ expect(await dashedTailStartIndex(page, HOURS_PER_DAY)).toBeNull();++ await page.goto(`/acme/${PROJECT}?range=lastMonth`);+ await overviewReady(page);+ await expect(mainChart(page).locator('svg').first()).toBeVisible();+ expect(await dashedTailStartIndex(page, 30)).toBeNull();+});++test('custom range through the date picker and the "last days" input', async ({+ page,+}, testInfo) => {+ const problems = watchPage(page);+ await gotoOverview(page, PROJECT, '?range=7d');+ await dismissFeedbackPrompt(page);++ await page.getByRole('button', { name: 'Last 7 days', exact: true }).click();+ await page.getByRole('menuitem', { name: /^Custom range/ }).click();+ const picker = page.getByRole('dialog');+ await expect(picker.getByRole('button', { name: 'Cancel' })).toBeVisible();+ // Nothing is selected yet, so there is nothing to confirm.+ await expect(picker.getByRole('button', { name: /^Select / })).toHaveCount(0);+ await shot(page, 'chart-datepicker-empty', false);++ // The October grid repeats the last September days, hence `first()`.+ await picker+ .getByRole('button', { name: /September 22/ })+ .first()+ .click();+ await picker+ .getByRole('button', { name: /September 28/ })+ .first()+ .click();+ await shot(page, 'chart-datepicker-selected', false);+ await picker.getByRole('button', { name: /^Select / }).click();++ await expect(page).toHaveURL(/range=custom/);+ const search = new URL(page.url()).searchParams;+ expect(search.get('start')).toBe('2026-09-22 00:00:00');+ expect(search.get('end')).toBe('2026-09-28 23:59:59');+ await expect(+ page.getByRole('button', { name: 'Custom range', exact: true })+ ).toBeVisible();+ await expect(+ page.getByRole('button', { name: 'Day', exact: true })+ ).toBeVisible();++ const custom = await fetchStats(page, PROJECT, {+ range: 'custom',+ startDate: '2026-09-22 00:00:00',+ endDate: '2026-09-28 23:59:59',+ });+ expect(custom.series).toHaveLength(7);+ await expect(metricCard(page, 'Sessions')).toContainText(+ compactNumber(Number(custom.metrics.total_sessions))+ );+ await shot(page, 'chart-custom-range', false);++ // survives a reload+ await page.reload();+ await overviewReady(page);+ await expect(+ page.getByRole('button', { name: 'Custom range', exact: true })+ ).toBeVisible();+ await expect(metricCard(page, 'Sessions')).toContainText(+ compactNumber(Number(custom.metrics.total_sessions))+ );++ // "Last days" input+ await page.getByRole('button', { name: 'Custom range', exact: true }).click();+ const days = page.getByRole('spinbutton', {+ name: 'Number of days for custom date filter',+ });+ await days.fill('3');+ await days.press('Enter');+ await expect(page).toHaveURL(/start=/);+ await overviewReady(page);+ const lastDays = new URL(page.url()).searchParams;+ expect(lastDays.get('range')).toBe('custom');+ const stats = await fetchStats(page, PROJECT, {+ range: 'custom',+ startDate: lastDays.get('start'),+ endDate: lastDays.get('end'),+ });+ expect(stats.series).toHaveLength(3);+ await expect(metricCard(page, 'Sessions')).toContainText(+ compactNumber(Number(stats.metrics.total_sessions))+ );+ await shot(page, 'chart-last-3-days', false);++ // Out-of-range values are ignored.+ const urlBefore = page.url();+ await page.getByRole('button', { name: 'Custom range', exact: true }).click();+ await days.fill('0');+ await days.press('Enter');+ await days.fill('366');+ await days.press('Enter');+ expect(page.url()).toBe(urlBefore);+ await page.keyboard.press('Escape');++ await attachProblems(testInfo, problems);+ expect(expectNoRealProblems(problems)).toEqual({+ pageErrors: [],+ responses: [],+ });+});++test('referrer spike marker: tooltip annotation and click adds a referrer filter', async ({+ page,+}) => {+ await gotoOverview(page, PROJECT, '?range=7d');+ await dismissFeedbackPrompt(page);+ const spikes = await trpcQuery<+ Array<{+ anchorDate: string;+ spikes: Array<{ referrer_name: string; date: string }>;+ }>+ >(page, 'overview.getReferrerSpikes', {+ projectId: PROJECT,+ range: '7d',+ interval: 'day',+ filters: [],+ });+ expect(spikes.length).toBeGreaterThan(0);+ const spike = spikes[0]?.spikes[0];+ const stats = await fetchStats(page, PROJECT, {});+ const index = stats.series.findIndex((row) => row.date === spike?.date);+ expect(index).toBeGreaterThanOrEqual(0);++ const text = await hoverBucket(page, index, stats.series.length);+ expect(text).toContain(`Spike from ${spike?.referrer_name}`);+ await shot(page, 'chart-spike-tooltip', false);++ const box = await mainChart(page).boundingBox();+ const markerX =+ (box?.x ?? 0) ++ CHART_MARGIN_X ++ (((box?.width ?? 0) - CHART_MARGIN_X * 2) * index) /+ (stats.series.length - 1);+ await page.mouse.click(markerX, (box?.y ?? 0) + SPIKE_MARKER_OFFSET_Y);+ await expect(page).toHaveURL(+ new RegExp(`f=referrer_name(,|%2C)is(,|%2C)${spike?.referrer_name}`)+ );+ await expect(page.getByRole('button', { name: 'Remove filter' })).toHaveCount(+ 1+ );+ await shot(page, 'chart-spike-clicked', false);+});++test.describe('references', () => {+ // Pinned so the bucket a reference lands in does not depend on the machine.+ test.use({ timezoneId: 'UTC' });++ test('a reference is annotated on the bucket of its own day', async ({+ page,+ }) => {+ const title = `E2E overview ${Date.now()}`;+ await gotoOverview(page, PROJECT, '?range=7d');+ const reference = await trpcMutation<{ id: string }>(+ page,+ 'reference.create',+ {+ title,+ description: 'created by overview-chart.spec.ts',+ projectId: PROJECT,+ datetime: '2026-09-28T15:00:00.000Z',+ }+ );+ try {+ await page.reload();+ await overviewReady(page);+ await dismissFeedbackPrompt(page);+ const stats = await fetchStats(page, PROJECT, {});+ const index = stats.series.findIndex((row) =>+ String(row.date).startsWith('2026-09-28')+ );+ const nextDay = await hoverBucket(page, index + 1, stats.series.length);+ await shot(page, 'chart-reference-next-day', false);+ const ownDay = await hoverBucket(page, index, stats.series.length);+ await shot(page, 'chart-reference-own-day', false);+ expect(+ `${ownDay}${nextDay}`,+ 'the reference is on the chart at all'+ ).toContain(title);+ expect(ownDay, 'tooltip of Sep 28').toContain(title);+ expect(nextDay, 'tooltip of Sep 29').not.toContain(title);+ } finally {+ await trpcMutation(page, 'reference.delete', { id: reference.id });+ }+ });+});++test('live visitor histogram shows the last 30 minutes from overview.liveData', async ({+ page,+}) => {+ await gotoOverview(page, PROJECT, '?range=7d');+ const live = await trpcQuery<{+ totalSessions: number;+ minuteCounts: Array<{ time: string; sessionCount: number }>;+ }>(page, 'overview.liveData', { projectId: PROJECT });+ expect(live.minuteCounts).toHaveLength(30);+ const card = page.locator('div.card.grid').first().locator('> div').last();+ await expect(card).toContainText('Live · 30 min');+ await expect(card).toContainText('Last 30 min');+ await expect(card).not.toContainText(/NaN|undefined/);+});++test.describe('timezone', () => {+ for (const timezoneId of ['UTC', 'America/Los_Angeles']) {+ test.describe(timezoneId, () => {+ test.use({ timezoneId });++ test(`"today" is the org's UTC day and the dashed tail starts at the current hour (browser in ${timezoneId})`, async ({+ page,+ }) => {+ await gotoOverview(page, PROJECT, '?range=today');+ const stats = await fetchStats(page, PROJECT, {+ range: 'today',+ interval: 'hour',+ });+ const now = new Date();+ const utcDay = now.toISOString().slice(0, 10);+ expect(stats.series).toHaveLength(HOURS_PER_DAY);+ expect(stats.series[0]?.date).toBe(`${utcDay} 00:00:00`);+ expect(stats.series.at(-1)?.date).toBe(`${utcDay} 23:00:00`);+ const futureSessions = stats.series+ .slice(now.getUTCHours() + 1)+ .reduce((total, row) => total + Number(row.total_sessions), 0);+ expect(futureSessions).toBe(0);++ await expect(metricCard(page, 'Sessions')).toContainText(+ compactNumber(Number(stats.metrics.total_sessions))+ );+ await shot(page, `chart-today-${timezoneId.replace('/', '-')}`, false);+ const dashStart = await dashedTailStartIndex(page, HOURS_PER_DAY);+ // The tail starts two buckets before the running hour.+ expect(dashStart).toBe(Math.max(0, now.getUTCHours() - 2));+ });+ });+ }+});diff --git a/apps/start/e2e/overview-filters.spec.ts b/apps/start/e2e/overview-filters.spec.tsnew file mode 100644index 000000000..c7ba949dd--- /dev/null+++ b/apps/start/e2e/overview-filters.spec.ts@@ -0,0 +1,295 @@+import { expect as baseExpect, test } from './fixtures';+import {+ attachProblems,+ compactNumber,+ dismissFeedbackPrompt,+ expectNoRealProblems,+ fetchStats,+ filter,+ gotoOverview,+ metricCard,+ overviewReady,+ SLOW_TEST_TIMEOUT_MS,+ shot,+ trpcQuery,+ watchPage,+} from './overview-helpers';++const PROJECT = 'acme-shop';+const METRIC_KEYS = [+ 'unique_visitors',+ 'total_sessions',+ 'total_screen_views',+ 'views_per_session',+ 'bounce_rate',+ 'avg_session_duration',+ 'total_revenue',+];++// The shared dev stack answers slowly while other suites run against it.+const expect = baseExpect.configure({ timeout: 30_000 });+test.use({ actionTimeout: 20_000 });++test.beforeEach(async ({ page }) => {+ test.setTimeout(SLOW_TEST_TIMEOUT_MS);+ await page.setViewportSize({ width: 1440, height: 1000 });+});++function pill(page: import('@playwright/test').Page, label: string) {+ return page+ .locator('div.rounded-md.border', {+ has: page.getByRole('button', { name: 'Remove filter' }),+ })+ .filter({ hasText: label });+}++test('widget-row filters combine, survive reload and back/forward, and can be removed', async ({+ page,+}, testInfo) => {+ const problems = watchPage(page);+ await gotoOverview(page, PROJECT, '?range=7d');+ await dismissFeedbackPrompt(page);+ const unfiltered = await fetchStats(page, PROJECT, {});++ await page+ .getByRole('button', { name: 'Google', exact: true })+ .first()+ .click();+ await expect(page).toHaveURL(/f=referrer_name(,|%2C)is(,|%2C)Google/);+ const google = await fetchStats(page, PROJECT, {+ filters: [filter('referrer_name', ['Google'])],+ });+ await expect(metricCard(page, 'Sessions')).toContainText(+ compactNumber(google.metrics.total_sessions ?? 0)+ );+ await expect(pill(page, 'Google')).toBeVisible();++ await page+ .getByRole('button', { name: 'mobile', exact: true })+ .first()+ .click();+ const both = await fetchStats(page, PROJECT, {+ filters: [+ filter('referrer_name', ['Google']),+ filter('device', ['mobile']),+ ],+ });+ expect(both.metrics.total_sessions).toBeLessThan(+ google.metrics.total_sessions ?? 0+ );+ await expect(metricCard(page, 'Sessions')).toContainText(+ compactNumber(both.metrics.total_sessions ?? 0)+ );+ await expect(pill(page, 'mobile')).toBeVisible();+ await shot(page, 'filters-google-mobile', false);+ const searchWithBoth = new URL(page.url()).searchParams.toString();++ await page.reload();+ await overviewReady(page);+ expect(new URL(page.url()).searchParams.toString()).toBe(searchWithBoth);+ await expect(pill(page, 'Google')).toBeVisible();+ await expect(pill(page, 'mobile')).toBeVisible();+ await expect(metricCard(page, 'Sessions')).toContainText(+ compactNumber(both.metrics.total_sessions ?? 0)+ );++ // Entries pushed before the reload belong to the old document, so back and+ // forward are full page loads here.+ await page.goBack();+ await overviewReady(page);+ await expect(pill(page, 'mobile')).toHaveCount(0);+ await expect(pill(page, 'Google')).toBeVisible();+ await expect(metricCard(page, 'Sessions')).toContainText(+ compactNumber(google.metrics.total_sessions ?? 0)+ );+ await page.goForward();+ await overviewReady(page);+ await expect(pill(page, 'mobile')).toBeVisible();++ // operator: is -> is not+ await pill(page, 'Google')+ .getByRole('button', { name: 'Is', exact: true })+ .click();+ await shot(page, 'filters-operator-menu', false);+ await page+ .getByRole('menuitem')+ .filter({ hasText: /^Is not$/ })+ .click();+ await expect(page).toHaveURL(/referrer_name(,|%2C)isNot/);+ const notGoogleMobile = await fetchStats(page, PROJECT, {+ filters: [+ filter('referrer_name', ['Google'], 'isNot'),+ filter('device', ['mobile']),+ ],+ });+ await expect(metricCard(page, 'Sessions')).toContainText(+ compactNumber(notGoogleMobile.metrics.total_sessions ?? 0)+ );++ await pill(page, 'Google')+ .getByRole('button', { name: 'Remove filter' })+ .click();+ await pill(page, 'mobile')+ .getByRole('button', { name: 'Remove filter' })+ .click();+ await expect(page.getByRole('button', { name: 'Remove filter' })).toHaveCount(+ 0+ );+ await expect(metricCard(page, 'Sessions')).toContainText(+ compactNumber(unfiltered.metrics.total_sessions ?? 0)+ );++ await attachProblems(testInfo, problems);+ expect(expectNoRealProblems(problems)).toEqual({+ pageErrors: [],+ responses: [],+ });+});++test('page filter from the filter modal: totals match the API and the series is sane', async ({+ page,+}, testInfo) => {+ const problems = watchPage(page);+ await gotoOverview(page, PROJECT, '?range=7d');+ await dismissFeedbackPrompt(page);+ const unfiltered = await fetchStats(page, PROJECT, {});++ await page.getByRole('button', { name: 'Filters' }).click();+ const dialog = page.getByRole('dialog', { name: 'Filters' });+ await dialog.getByRole('button', { name: 'Add filter' }).click();+ await page+ .getByRole('menu', { name: 'Add filter' })+ .getByText('path', { exact: true })+ .click();+ await expect(page).toHaveURL(/f=path/);+ await shot(page, 'filters-modal-path-empty', false);++ // A filter without a value must not change the numbers.+ await page.waitForLoadState('networkidle');+ // The open sheet hides the page from the accessibility tree, so read the card by its text.+ await expect+ .soft(+ page+ .locator('div.card.grid')+ .first()+ .locator('button', { hasText: 'Unique Visitors' }),+ 'a path filter without a value must not change the totals'+ )+ .toContainText(compactNumber(unfiltered.metrics.unique_visitors ?? 0), {+ timeout: 10_000,+ });++ await dialog.getByRole('button', { name: /Select/ }).click();+ await shot(page, 'filters-modal-path-values', false);+ await page.getByRole('option', { name: '/cart', exact: true }).click();+ await page.keyboard.press('Escape');+ await expect(page).toHaveURL(/f=path(,|%2C)is(,|%2C)(%2F|%252F|\/)cart/);+ await shot(page, 'filters-modal-path-cart', false);+ await dialog.getByRole('button', { name: 'Close' }).click();++ const cart = await fetchStats(page, PROJECT, {+ filters: [filter('path', ['/cart'])],+ });+ await expect(metricCard(page, 'Sessions')).toContainText(+ compactNumber(cart.metrics.total_sessions ?? 0)+ );+ await expect(metricCard(page, 'Pageviews')).toContainText(+ compactNumber(cart.metrics.total_screen_views ?? 0)+ );+ for (const title of [+ 'Unique Visitors',+ 'Sessions',+ 'Pageviews',+ 'Pages per session',+ 'Bounce Rate',+ 'Session Duration',+ 'Revenue',+ ]) {+ await expect(metricCard(page, title)).not.toContainText(+ /NaN|undefined|null|N\/A/+ );+ }+ await shot(page, 'filters-path-cart', false);++ // API sanity for the page-filter query path+ for (const [range, interval, buckets] of [+ ['7d', 'day', 8],+ ['today', 'hour', 24],+ ['yesterday', 'hour', 24],+ ['30d', 'day', 31],+ ] as const) {+ const stats = await fetchStats(page, PROJECT, {+ range,+ interval,+ filters: [filter('path', ['/cart'])],+ });+ expect(stats.series, `${range} buckets`).toHaveLength(buckets);+ const sum = (key: string) =>+ stats.series.reduce((total, row) => total + Number(row[key] ?? 0), 0);+ expect(sum('total_sessions'), `${range} sessions`).toBe(+ stats.metrics.total_sessions+ );+ expect(sum('total_screen_views'), `${range} pageviews`).toBe(+ stats.metrics.total_screen_views+ );+ expect(sum('unique_visitors')).toBeGreaterThanOrEqual(+ stats.metrics.unique_visitors ?? 0+ );+ for (const value of Object.values(stats.metrics)) {+ expect(Number.isNaN(value)).toBe(false);+ }+ const rowsWithNull = stats.series.filter((row) =>+ METRIC_KEYS.some((key) => row[key] === null || row[key] === undefined)+ );+ expect+ .soft(+ rowsWithNull.map((row) => row.date),+ `${range}: buckets with a null metric`+ )+ .toEqual([]);+ }++ // /cart is viewed for a while in every session, so time on it cannot be zero.+ expect+ .soft(+ cart.metrics.avg_session_duration,+ 'session duration under a page filter'+ )+ .toBeGreaterThan(0);++ const topPages = await trpcQuery<Array<{ path: string; sessions: number }>>(+ page,+ 'overview.topPages',+ { projectId: PROJECT, range: '7d', filters: [], mode: 'page' }+ );+ expect(topPages.find((row) => row.path === '/cart')?.sessions).toBe(+ cart.metrics.total_sessions+ );++ await attachProblems(testInfo, problems);+ expect(expectNoRealProblems(problems)).toEqual({+ pageErrors: [],+ responses: [],+ });+});++test('a revenue filter (offered by "Add filter") does not break the widgets', async ({+ page,+}) => {+ const filters = [filter('revenue', ['0'], 'gt')];+ const stats = await fetchStats(page, PROJECT, { filters });+ expect(stats.metrics.total_sessions).toBeGreaterThan(0);+ // trpcQuery throws on a non-2xx answer.+ await trpcQuery(page, 'overview.topGeneric', {+ projectId: PROJECT,+ range: '7d',+ filters,+ column: 'device',+ });+ await trpcQuery(page, 'overview.topPages', {+ projectId: PROJECT,+ range: '7d',+ filters,+ mode: 'page',+ });+});diff --git a/apps/start/e2e/overview-helpers.ts b/apps/start/e2e/overview-helpers.tsnew file mode 100644index 000000000..c5c40e85c--- /dev/null+++ b/apps/start/e2e/overview-helpers.ts@@ -0,0 +1,261 @@+import type { Page, TestInfo } from '@playwright/test';++export const SHOTS_DIR = 'test-results/overview-shots';+export const API_URL = 'https://api.main.local.openpanel.cc';+const API_TIMEOUT_MS = 60_000;+export const PROJECT_IDS = [+ 'acme-web',+ 'acme-saas',+ 'acme-shop',+ 'acme-app',+] as const;++const NOISE_PATTERNS = [+ /\[vite\]/i,+ /favicon/i,+ /sentry/i,+ /Download the React DevTools/i,+ /node_modules\/\.vite/i,+ /tree hydrated but some attributes/i,+ /status of 401 \(\)/i,+ /status of 502 \(\)/i,+ /net::ERR_ABORTED/i,+];++export interface PageProblems {+ consoleErrors: string[];+ pageErrors: string[];+ badResponses: string[];+ all: () => string[];+}++/** Records console errors, uncaught exceptions and >= 400 responses. */+export function watchPage(page: Page): PageProblems {+ const problems: PageProblems = {+ consoleErrors: [],+ pageErrors: [],+ badResponses: [],+ all: () => [+ ...problems.consoleErrors.map((e) => `console: ${e}`),+ ...problems.pageErrors.map((e) => `pageerror: ${e}`),+ ...problems.badResponses.map((e) => `response: ${e}`),+ ],+ };+ const isNoise = (text: string) => NOISE_PATTERNS.some((p) => p.test(text));+ page.on('console', (message) => {+ if (message.type() === 'error' && !isNoise(message.text())) {+ problems.consoleErrors.push(message.text().slice(0, 500));+ }+ });+ page.on('pageerror', (error) => {+ problems.pageErrors.push(String(error).slice(0, 500));+ });+ page.on('response', (response) => {+ if (response.status() >= 400 && !isNoise(response.url())) {+ problems.badResponses.push(+ `${response.status()} ${response.url().slice(0, 300)}`+ );+ }+ });+ return problems;+}++export async function shot(page: Page, name: string, fullPage = true) {+ await page.screenshot({ path: `${SHOTS_DIR}/${name}.png`, fullPage });+}++export async function attachProblems(+ testInfo: TestInfo,+ problems: PageProblems+) {+ if (problems.all().length > 0) {+ await testInfo.attach('page-problems', {+ body: problems.all().join('\n'),+ contentType: 'text/plain',+ });+ }+}++/** Calls a tRPC query with the page's own session cookie. */+export async function trpcQuery<T>(+ page: Page,+ procedure: string,+ input: Record<string, unknown>+): Promise<T> {+ const response = await page.request.get(+ `${API_URL}/trpc/${procedure}?input=${encodeURIComponent(JSON.stringify({ json: input }))}`,+ { timeout: API_TIMEOUT_MS }+ );+ if (!response.ok()) {+ throw new Error(+ `${procedure} -> ${response.status()} ${(await response.text()).slice(0, 300)}`+ );+ }+ const body = await response.json();+ return body.result.data.json as T;+}++export async function gotoReady(page: Page, url: string) {+ await page.goto(url);+ await page.waitForLoadState('networkidle');+}++// The shared dev stack is slow while other suites run against it.+export const SLOW_TEST_TIMEOUT_MS = 300_000;++export function metricCard(page: Page, title: string) {+ return page+ .locator('div.card.grid')+ .first()+ .getByRole('button', { name: new RegExp(`^${title}`) });+}++const HYDRATION_ATTEMPT_TIMEOUT_MS = 30_000;+const HYDRATION_ATTEMPTS = 4;++/**+ * The overview has loaded once the first metric card shows a number. Under+ * load the dev server sometimes answers 502 for a module, which leaves the+ * server-rendered skeleton unhydrated forever; a reload recovers.+ */+export async function overviewReady(page: Page) {+ const firstValue = metricCard(page, 'Unique Visitors')+ .locator('span.font-mono')+ .first();+ for (let attempt = 1; attempt <= HYDRATION_ATTEMPTS; attempt++) {+ const loaded = await firstValue+ .waitFor({ timeout: HYDRATION_ATTEMPT_TIMEOUT_MS })+ .then(() => true)+ .catch(() => false);+ if (loaded) {+ break;+ }+ if (attempt === HYDRATION_ATTEMPTS) {+ throw new Error('overview never left its loading skeleton');+ }+ await page.reload();+ }+ await page.waitForLoadState('networkidle');+}++export async function gotoOverview(page: Page, projectId: string, query = '') {+ await page.goto(`/acme/${projectId}${query}`);+ await overviewReady(page);+}++export function mainChart(page: Page) {+ return page.locator('div.h-\\[190px\\]');+}++export async function dismissFeedbackPrompt(page: Page) {+ const prompt = page.getByText('Share Your Feedback');+ if (await prompt.isVisible().catch(() => false)) {+ await prompt.locator('..').getByRole('button').first().click();+ }+}++export interface OverviewStats {+ metrics: Record<string, number | null>;+ series: Record<string, number | string | null | undefined>[];+}++export interface OverviewFilter {+ id: string;+ name: string;+ operator: string;+ value: string[];+}++export function filter(+ name: string,+ value: string[],+ operator = 'is'+): OverviewFilter {+ return { id: name, name, operator, value };+}++export function fetchStats(+ page: Page,+ projectId: string,+ input: Record<string, unknown>+) {+ return trpcQuery<OverviewStats>(page, 'overview.stats', {+ projectId,+ range: '7d',+ interval: 'day',+ filters: [],+ ...input,+ });+}++export const compactNumber = (value: number) =>+ new Intl.NumberFormat('en-US', { notation: 'compact' }).format(value);++export function expectNoRealProblems(problems: PageProblems) {+ // The dashboard's own product analytics call (no key locally) and the+ // overloaded dev server's module 502s are not the app under test.+ const responses = problems.badResponses.filter(+ (r) => !(r.includes('api.openpanel.dev') || /^502 /.test(r))+ );+ const pageErrors = problems.pageErrors.filter(+ (e) => !e.includes('Failed to fetch dynamically imported module')+ );+ return { pageErrors, responses };+}++export async function trpcMutation<T>(+ page: Page,+ procedure: string,+ input: Record<string, unknown>+): Promise<T> {+ const response = await page.request.post(`${API_URL}/trpc/${procedure}`, {+ data: { json: input },+ timeout: API_TIMEOUT_MS,+ });+ if (!response.ok()) {+ throw new Error(+ `${procedure} -> ${response.status()} ${(await response.text()).slice(0, 300)}`+ );+ }+ const body = await response.json();+ return body.result.data.json as T;+}++const CHART_MARGIN_X = 20;++/** Hovers the main chart over the bucket at `index` and returns the tooltip text. */+export async function hoverBucket(page: Page, index: number, buckets: number) {+ const chart = mainChart(page);+ await chart.scrollIntoViewIfNeeded();+ const box = await chart.boundingBox();+ if (!box) {+ throw new Error('main chart is not visible');+ }+ const innerWidth = box.width - CHART_MARGIN_X * 2;+ const x = box.x + CHART_MARGIN_X + (innerWidth * index) / (buckets - 1);+ // Approach from the side so the chart sees a real mouse move.+ await page.mouse.move(x - 3, box.y + 70);+ await page.mouse.move(x, box.y + 80);+ const tooltip = page.locator('.pointer-events-none.absolute.z-50').last();+ await tooltip.waitFor();+ await page.waitForTimeout(250);+ return tooltip.innerText();+}++/** Index the dashed "incomplete" tail starts at, or null when the line is solid. */+export async function dashedTailStartIndex(page: Page, buckets: number) {+ const dashArrays = await mainChart(page)+ .locator('path[stroke-dasharray]')+ .evaluateAll((paths) =>+ paths.map((path) => path.getAttribute('stroke-dasharray') ?? '')+ );+ const hasDashedTail = dashArrays.includes('4,4');+ const solid = dashArrays.find((value) => /^[\d.]+ [\d.]+$/.test(value));+ if (!(hasDashedTail && solid)) {+ return null;+ }+ const [solidLength, rest] = solid.split(' ').map(Number);+ return Math.round(+ ((solidLength ?? 0) / ((solidLength ?? 0) + (rest ?? 0))) * (buckets - 1)+ );+}diff --git a/apps/start/e2e/overview-metrics.spec.ts b/apps/start/e2e/overview-metrics.spec.tsnew file mode 100644index 000000000..f5d6e3e62--- /dev/null+++ b/apps/start/e2e/overview-metrics.spec.ts@@ -0,0 +1,252 @@+import { expect as baseExpect, test } from './fixtures';+import {+ attachProblems,+ gotoOverview,+ mainChart,+ metricCard,+ overviewReady,+ PROJECT_IDS,+ SLOW_TEST_TIMEOUT_MS,+ shot,+ trpcQuery,+ watchPage,+} from './overview-helpers';++interface StatsSeriesRow {+ date: string;+ unique_visitors: number;+ total_sessions: number;+ total_screen_views: number;+ views_per_session: number;+ bounce_rate: number;+ avg_session_duration: number;+ total_revenue: number;+ prev_unique_visitors?: number | null;+ prev_total_sessions?: number | null;+}+interface Stats {+ metrics: Record<string, number | null>;+ series: StatsSeriesRow[];+}++const compact = (value: number) =>+ new Intl.NumberFormat('en-US', { notation: 'compact' }).format(value);++const METRIC_CARDS = [+ { index: 0, title: 'Unique Visitors', key: 'unique_visitors' },+ { index: 1, title: 'Sessions', key: 'total_sessions' },+ { index: 2, title: 'Pageviews', key: 'total_screen_views' },+ { index: 3, title: 'Pages per session', key: 'views_per_session' },+ { index: 4, title: 'Bounce Rate', key: 'bounce_rate' },+ { index: 5, title: 'Session Duration', key: 'avg_session_duration' },+ { index: 6, title: 'Revenue', key: 'total_revenue' },+] as const;++const RANGES = [+ {+ key: '30min',+ label: 'Last 30 min',+ interval: 'Minute',+ enabled: ['Minute', 'Hour', 'Day'],+ },+ {+ key: 'lastHour',+ label: 'Last hour',+ interval: 'Minute',+ enabled: ['Minute', 'Hour', 'Day'],+ },+ {+ key: 'last24h',+ label: 'Last 24 hours',+ interval: 'Hour',+ enabled: ['Hour', 'Day'],+ },+ { key: 'today', label: 'Today', interval: 'Hour', enabled: ['Hour', 'Day'] },+ {+ key: 'yesterday',+ label: 'Yesterday',+ interval: 'Hour',+ enabled: ['Hour', 'Day', 'Week', 'Month'],+ },+ {+ key: '7d',+ label: 'Last 7 days',+ interval: 'Day',+ enabled: ['Hour', 'Day', 'Month'],+ },+ {+ key: '30d',+ label: 'Last 30 days',+ interval: 'Day',+ enabled: ['Day', 'Week', 'Month'],+ },+ {+ key: '3m',+ label: 'Last 3 months',+ interval: 'Day',+ enabled: ['Day', 'Week', 'Month'],+ },+ {+ key: '6m',+ label: 'Last 6 months',+ interval: 'Week',+ enabled: ['Day', 'Week', 'Month'],+ },+ {+ key: '12m',+ label: 'Last 12 months',+ interval: 'Month',+ enabled: ['Day', 'Week', 'Month'],+ },+ {+ key: 'monthToDate',+ label: 'Month to Date',+ interval: 'Day',+ enabled: ['Day', 'Week', 'Month'],+ },+ {+ key: 'lastMonth',+ label: 'Last Month',+ interval: 'Day',+ enabled: ['Day', 'Week', 'Month'],+ },+ {+ key: 'yearToDate',+ label: 'Year to Date',+ interval: 'Month',+ enabled: ['Day', 'Week', 'Month'],+ },+ {+ key: 'lastYear',+ label: 'Last year',+ interval: 'Month',+ enabled: ['Day', 'Week', 'Month'],+ },+] as const;++const ALL_INTERVALS = ['Minute', 'Hour', 'Day', 'Week', 'Month'] as const;++// The shared dev stack answers slowly while other suites run against it.+const expect = baseExpect.configure({ timeout: 30_000 });+test.use({ actionTimeout: 20_000 });++test.beforeEach(async ({ page }) => {+ test.setTimeout(SLOW_TEST_TIMEOUT_MS);+ await page.setViewportSize({ width: 1440, height: 1000 });+});++for (const projectId of PROJECT_IDS) {+ test(`${projectId}: metric cards match overview.stats and switching the metric drives the chart`, async ({+ page,+ }, testInfo) => {+ const problems = watchPage(page);+ await gotoOverview(page, projectId, '?range=7d');+ const stats = await trpcQuery<Stats>(page, 'overview.stats', {+ projectId,+ range: '7d',+ interval: 'day',+ filters: [],+ });++ const card = (title: string) => metricCard(page, title);+ await expect(card('Unique Visitors')).toContainText(+ compact(stats.metrics.unique_visitors ?? 0)+ );+ await expect(card('Sessions')).toContainText(+ compact(stats.metrics.total_sessions ?? 0)+ );+ await expect(card('Pageviews')).toContainText(+ compact(stats.metrics.total_screen_views ?? 0)+ );+ await expect(card('Pages per session')).toContainText(+ compact(stats.metrics.views_per_session ?? 0)+ );+ await expect(card('Bounce Rate')).toContainText(+ new Intl.NumberFormat('en-US').format(stats.metrics.bounce_rate ?? 0)+ );+ for (const { title } of METRIC_CARDS) {+ await expect(card(title)).not.toContainText(/NaN|undefined|null|N\/A/);+ }++ for (const { index, title } of METRIC_CARDS) {+ await card(title).click();+ await expect(page).toHaveURL(+ index === 0 ? /^(?!.*metric=[1-9])/ : new RegExp(`metric=${index}`)+ );+ await expect(+ page.locator('div.card.p-4 >> div.text-muted-foreground', {+ hasText: new RegExp(`^${title}$`),+ })+ ).toBeVisible();+ await expect(mainChart(page).locator('svg').first()).toBeVisible();+ }+ await shot(page, `metrics-${projectId}-revenue`, false);++ // metric survives reload+ await page.reload();+ await overviewReady(page);+ await expect(page).toHaveURL(/metric=6/);+ await expect(+ page.locator('div.card.p-4 >> div.text-muted-foreground', {+ hasText: /^Revenue$/,+ })+ ).toBeVisible();++ await attachProblems(testInfo, problems);+ expect(problems.pageErrors).toEqual([]);+ expect(+ problems.badResponses.filter((r) => !r.includes('api.openpanel.dev'))+ ).toEqual([]);+ });+}++test('every range can be picked, sets its default interval and enables the right intervals', async ({+ page,+}, testInfo) => {+ const problems = watchPage(page);+ await gotoOverview(page, 'acme-shop', '?range=7d');++ for (const range of RANGES) {+ await page+ .getByRole('button', {+ name: /^(Last|Today|Yesterday|Month|Year|Custom)/,+ })+ .first()+ .click();+ await page+ .getByRole('menuitem', { name: new RegExp(`^${range.label}`) })+ .click();+ await expect(page).toHaveURL(new RegExp(`range=${range.key}(&|$)`));+ await overviewReady(page);+ await expect(+ page.getByRole('button', { name: range.label, exact: true })+ ).toBeVisible();+ const intervalButton = page.getByRole('button', {+ name: /^(Minute|Hour|Day|Week|Month)$/,+ });+ await expect(intervalButton).toHaveText(range.interval);+ await expect(metricCard(page, 'Unique Visitors')).toContainText(+ range.label+ );++ await intervalButton.click();+ for (const interval of ALL_INTERVALS) {+ const item = page.getByRole('menuitem', {+ name: new RegExp(`^${interval}`),+ });+ if ((range.enabled as readonly string[]).includes(interval)) {+ await expect(item, `${range.key}/${interval}`).toBeEnabled();+ } else {+ await expect(item, `${range.key}/${interval}`).toBeDisabled();+ }+ }+ await page.keyboard.press('Escape');+ await shot(page, `range-${range.key}`, false);+ }++ await attachProblems(testInfo, problems);+ expect(problems.pageErrors).toEqual([]);+ expect(+ problems.badResponses.filter((r) => !r.includes('api.openpanel.dev'))+ ).toEqual([]);+});diff --git a/apps/start/e2e/overview-share.spec.ts b/apps/start/e2e/overview-share.spec.tsnew file mode 100644index 000000000..f0594c11d--- /dev/null+++ b/apps/start/e2e/overview-share.spec.ts@@ -0,0 +1,358 @@+import type { Browser, Page } from '@playwright/test';+import { expect as baseExpect, test } from './fixtures';+import {+ API_URL,+ compactNumber,+ dismissFeedbackPrompt,+ fetchStats,+ gotoOverview,+ metricCard,+ overviewReady,+ type PageProblems,+ SLOW_TEST_TIMEOUT_MS,+ shot,+ trpcMutation,+ trpcQuery,+ watchPage,+} from './overview-helpers';++// The shared dev stack answers slowly while other suites run against it.+const expect = baseExpect.configure({ timeout: 30_000 });+test.use({ actionTimeout: 20_000 });++const PROJECT = 'acme-web';+const ORGANIZATION = 'acme';+const SHARE_PASSWORD = 'e2e-overview-secret';+const DIRECT_LOAD_TIMEOUT_MS = 60_000;++interface ShareSettings {+ id: string;+ public: boolean;+ hasPassword?: boolean;+}++function shareSettings(page: Page) {+ return trpcQuery<ShareSettings | null>(page, 'share.overviewSettings', {+ projectId: PROJECT,+ });+}++function makePrivate(page: Page) {+ return trpcMutation(page, 'share.createOverview', {+ organizationId: ORGANIZATION,+ projectId: PROJECT,+ public: false,+ password: null,+ });+}++async function anonymousPage(browser: Browser) {+ const context = await browser.newContext({+ ignoreHTTPSErrors: true,+ storageState: { cookies: [], origins: [] },+ viewport: { width: 1440, height: 1000 },+ });+ const page = await context.newPage();+ return { context, page, problems: watchPage(page) };+}++/**+ * Opens a share the way an in-app link does: a client-side route change from+ * an already loaded page. Opening the URL directly is covered by its own test.+ */+async function openShareClientSide(page: Page, path: string) {+ await page.goto('/login');+ await page.waitForLoadState('networkidle');+ await page.evaluate((target) => {+ window.history.pushState({}, '', target);+ window.dispatchEvent(new PopStateEvent('popstate'));+ }, path);+}++function realResponses(problems: PageProblems) {+ return problems.badResponses.filter((r) => !r.includes('api.openpanel.dev'));+}++async function shareOverviewThroughModal(page: Page, password?: string) {+ await page.getByRole('button', { name: 'Private', exact: true }).click();+ await page.getByRole('menuitem', { name: 'Make public' }).click();+ const modal = page.getByRole('dialog');+ await expect(modal.getByText('Overview public availability')).toBeVisible();+ if (password) {+ await modal+ .getByPlaceholder('Enter your password (optional)')+ .fill(password);+ }+ return modal;+}++test.beforeEach(async ({ page }) => {+ test.setTimeout(SLOW_TEST_TIMEOUT_MS * 2);+ await page.setViewportSize({ width: 1440, height: 1000 });+});++test.afterEach(async ({ page }) => {+ await makePrivate(page);+});++test('public share: create from the modal, open logged out, then make private', async ({+ page,+ browser,+}) => {+ await makePrivate(page);+ await gotoOverview(page, PROJECT, '?range=7d');+ await dismissFeedbackPrompt(page);++ const modal = await shareOverviewThroughModal(page);+ await shot(page, 'share-modal', false);+ await modal.getByRole('button', { name: 'Make it public' }).click();+ await expect(page.getByText('Your overview is now public')).toBeVisible();+ await expect(+ page.getByRole('button', { name: 'Public', exact: true })+ ).toBeVisible();+ const settings = await shareSettings(page);+ expect(settings?.public).toBe(true);+ expect(settings?.hasPassword).toBeFalsy();++ // the menu now offers View and Make private+ await page.getByRole('button', { name: 'Public', exact: true }).click();+ await expect(page.getByRole('menuitem', { name: 'View' })).toHaveAttribute(+ 'href',+ `/share/overview/${settings?.id}`+ );+ await expect(+ page.getByRole('menuitem', { name: 'Make private' })+ ).toBeVisible();+ await page.keyboard.press('Escape');++ const stats = await fetchStats(page, PROJECT, {});+ const visitor = await anonymousPage(browser);+ try {+ await openShareClientSide(+ visitor.page,+ `/share/overview/${settings?.id}?range=7d`+ );+ await overviewReady(visitor.page);+ await expect(metricCard(visitor.page, 'Sessions')).toContainText(+ compactNumber(Number(stats.metrics.total_sessions))+ );+ // No dashboard chrome for an anonymous viewer.+ await expect(+ visitor.page.getByRole('link', { name: 'Settings' })+ ).toHaveCount(0);+ await shot(visitor.page, 'share-public-anonymous');++ // range switch works for a visitor+ await visitor.page+ .getByRole('button', { name: 'Last 7 days', exact: true })+ .click();+ await visitor.page.getByRole('menuitem', { name: /^Last 30 days/ }).click();+ const month = await fetchStats(page, PROJECT, { range: '30d' });+ await expect(metricCard(visitor.page, 'Sessions')).toContainText(+ compactNumber(Number(month.metrics.total_sessions))+ );++ // widget row click filters+ await visitor.page+ .getByRole('button', { name: 'Google', exact: true })+ .first()+ .click();+ await expect(visitor.page).toHaveURL(/f=referrer_name/);+ await expect(+ visitor.page.getByRole('button', { name: 'Remove filter' })+ ).toHaveCount(1);+ await visitor.page.waitForLoadState('networkidle');+ await shot(visitor.page, 'share-public-filtered', false);++ expect+ .soft(+ realResponses(visitor.problems),+ 'failed requests on the public share'+ )+ .toEqual([]);+ // The client-side hop from /login can race that page's own hydration.+ expect+ .soft(+ visitor.problems.pageErrors.filter(+ (e) => !e.includes('Hydration failed')+ )+ )+ .toEqual([]);++ // details modal+ const sources = visitor.page+ .locator('div.card')+ .filter({ has: visitor.page.getByPlaceholder('Search refs') })+ .last();+ await sources.locator('button').filter({ hasNotText: /./ }).first().click();+ await visitor.page.waitForLoadState('networkidle');+ await visitor.page.waitForTimeout(3000);+ await shot(visitor.page, 'share-public-details-modal', false);+ expect+ .soft(+ visitor.page.url(),+ 'the details button keeps the visitor on the share'+ )+ .toContain('/share/overview/');+ await expect+ .soft(+ visitor.page.getByRole('dialog').locator('div.group\\/row').first(),+ 'details modal lists rows for a visitor'+ )+ .toBeVisible({ timeout: 5000 });+ expect+ .soft(+ realResponses(visitor.problems),+ 'failed requests after opening the details modal'+ )+ .toEqual([]);++ // make private from the menu+ await page.getByRole('button', { name: 'Public', exact: true }).click();+ await page.getByRole('menuitem', { name: 'Make private' }).click();+ await expect(+ page.getByRole('button', { name: 'Private', exact: true })+ ).toBeVisible();+ expect((await shareSettings(page))?.public).toBe(false);++ await visitor.page.goto(`/share/overview/${settings?.id}`, {+ timeout: DIRECT_LOAD_TIMEOUT_MS,+ });+ await expect(visitor.page.getByText('Share not found')).toBeVisible();+ await shot(visitor.page, 'share-private-anonymous', false);+ const denied = await visitor.page.request.get(+ `${API_URL}/trpc/overview.stats?input=${encodeURIComponent(+ JSON.stringify({+ json: {+ projectId: PROJECT,+ shareId: settings?.id,+ range: '7d',+ interval: 'day',+ filters: [],+ },+ })+ )}`+ );+ expect(denied.status()).toBeGreaterThanOrEqual(400);+ } finally {+ await visitor.context.close();+ }+});++test('password-protected share: locked without the password, opens with it', async ({+ page,+ browser,+}) => {+ await makePrivate(page);+ await gotoOverview(page, PROJECT, '?range=7d');+ await dismissFeedbackPrompt(page);++ const modal = await shareOverviewThroughModal(page, SHARE_PASSWORD);+ // keyboard submit+ await modal.getByPlaceholder('Enter your password (optional)').press('Enter');+ await expect(+ page.getByRole('button', { name: 'Public', exact: true })+ ).toBeVisible();+ const settings = await shareSettings(page);+ expect(settings?.public).toBe(true);+ expect(settings?.hasPassword).toBe(true);+ const stats = await fetchStats(page, PROJECT, {});++ const visitor = await anonymousPage(browser);+ try {+ // The data endpoints stay closed until the password is given.+ const locked = await visitor.page.request.get(+ `${API_URL}/trpc/overview.stats?input=${encodeURIComponent(+ JSON.stringify({+ json: {+ projectId: PROJECT,+ shareId: settings?.id,+ range: '7d',+ interval: 'day',+ filters: [],+ },+ })+ )}`+ );+ expect(locked.status()).toBeGreaterThanOrEqual(400);++ await openShareClientSide(+ visitor.page,+ `/share/overview/${settings?.id}?range=7d`+ );+ await expect(visitor.page.getByText(/is locked/)).toBeVisible();+ await expect(visitor.page.getByText('Unique Visitors')).toHaveCount(0);+ await shot(visitor.page, 'share-locked', false);++ await visitor.page.waitForLoadState('networkidle');+ await visitor.page+ .getByPlaceholder('Enter your password')+ .fill('wrong-password');+ await visitor.page.getByRole('button', { name: 'Get access' }).click();+ await expect(visitor.page.getByText('Incorrect password')).toBeVisible();+ await expect(visitor.page.getByText(/is locked/)).toBeVisible();++ const unlock = visitor.page.waitForResponse((response) =>+ response.url().includes('auth.signInShare')+ );+ await visitor.page+ .getByPlaceholder('Enter your password')+ .fill(SHARE_PASSWORD);+ await visitor.page.getByPlaceholder('Enter your password').press('Enter');+ expect((await unlock).status()).toBe(200);++ // The cookie from the unlock opens the data endpoints.+ const unlocked = await visitor.page.request.get(+ `${API_URL}/trpc/overview.stats?input=${encodeURIComponent(+ JSON.stringify({+ json: {+ projectId: PROJECT,+ shareId: settings?.id,+ range: '7d',+ interval: 'day',+ filters: [],+ },+ })+ )}`+ );+ expect(unlocked.status()).toBe(200);++ await openShareClientSide(+ visitor.page,+ `/share/overview/${settings?.id}?range=7d`+ );+ await overviewReady(visitor.page);+ await expect(visitor.page.getByText(/is locked/)).toHaveCount(0);+ await expect(metricCard(visitor.page, 'Sessions')).toContainText(+ compactNumber(Number(stats.metrics.total_sessions))+ );+ await shot(visitor.page, 'share-unlocked', false);+ } finally {+ await visitor.context.close();+ }+});++test('a public share link opens when loaded directly', async ({+ page,+ browser,+}) => {+ await page.goto('/login');+ await trpcMutation(page, 'share.createOverview', {+ organizationId: ORGANIZATION,+ projectId: PROJECT,+ public: true,+ password: null,+ });+ const settings = await shareSettings(page);+ const visitor = await anonymousPage(browser);+ try {+ await visitor.page.goto(`/share/overview/${settings?.id}?range=7d`, {+ timeout: DIRECT_LOAD_TIMEOUT_MS,+ });+ await overviewReady(visitor.page);+ await expect(visitor.page).toHaveTitle(/Acme Web/);+ await shot(visitor.page, 'share-direct-load', false);+ } finally {+ await visitor.context.close();+ }+});diff --git a/apps/start/e2e/overview-siblings.spec.ts b/apps/start/e2e/overview-siblings.spec.tsnew file mode 100644index 000000000..c4a6e95dd--- /dev/null+++ b/apps/start/e2e/overview-siblings.spec.ts@@ -0,0 +1,244 @@+import { expect as baseExpect, test } from './fixtures';+import {+ attachProblems,+ dismissFeedbackPrompt,+ expectNoRealProblems,+ gotoOverview,+ metricCard,+ PROJECT_IDS,+ SLOW_TEST_TIMEOUT_MS,+ shot,+ trpcQuery,+ watchPage,+} from './overview-helpers';++// The shared dev stack answers slowly while other suites run against it.+const expect = baseExpect.configure({ timeout: 30_000 });+test.use({ actionTimeout: 20_000 });++const MOBILE_VIEWPORT = { width: 390, height: 844 };++test.beforeEach(() => {+ test.setTimeout(SLOW_TEST_TIMEOUT_MS);+});++for (const projectId of PROJECT_IDS) {+ test(`${projectId}: realtime, pages and insights render without errors`, async ({+ page,+ }, testInfo) => {+ const problems = watchPage(page);+ await page.setViewportSize({ width: 1440, height: 1000 });++ await page.goto(`/acme/${projectId}/realtime`);+ for (const title of [+ 'Unique visitors last 30 min',+ 'Geo',+ 'Referrals',+ 'Paths',+ ]) {+ await expect(+ page.getByText(title, { exact: true }).locator('visible=true').first()+ ).toBeVisible();+ }+ await page.waitForLoadState('networkidle');+ await page.waitForTimeout(2000);+ await shot(page, `siblings-${projectId}-realtime`);+ await expect(page.locator('body')).not.toContainText(+ /NaN|undefined|Something went wrong/+ );++ await page.goto(`/acme/${projectId}/pages`);+ await expect(+ page.getByRole('heading', { name: 'Pages', level: 1 })+ ).toBeVisible();+ const pages = await trpcQuery<Array<{ path: string; origin: string }>>(+ page,+ 'overview.topPages',+ { projectId, range: '30d', filters: [], mode: 'page' }+ );+ await expect(+ page.getByText(String(pages[0]?.path), { exact: true }).first()+ ).toBeVisible();+ await page.waitForLoadState('networkidle');+ await shot(page, `siblings-${projectId}-pages`);+ await expect(page.locator('body')).not.toContainText(+ /NaN|undefined|Something went wrong/+ );++ await page.goto(`/acme/${projectId}/insights`);+ await expect(+ page.getByRole('heading', { name: 'Insights', level: 1 })+ ).toBeVisible();+ await expect(page.getByPlaceholder('Search insights...')).toBeVisible();+ await page.waitForLoadState('networkidle');+ await shot(page, `siblings-${projectId}-insights`);+ await expect(page.locator('body')).not.toContainText(+ /NaN|undefined|Something went wrong/+ );++ await attachProblems(testInfo, problems);+ expect(expectNoRealProblems(problems)).toEqual({+ pageErrors: [],+ responses: [],+ });+ });+}++test('pages: search, empty state and row click without Search Console', async ({+ page,+}, testInfo) => {+ const problems = watchPage(page);+ await page.setViewportSize({ width: 1440, height: 1000 });+ await page.goto('/acme/acme-shop/pages');+ await expect(+ page.getByRole('heading', { name: 'Pages', level: 1 })+ ).toBeVisible();+ await expect(page.getByText('/cart', { exact: true }).first()).toBeVisible();+ await page.waitForLoadState('networkidle');++ const search = page.getByPlaceholder('Search pages');+ await search.fill('checkout');+ await expect(+ page.getByText('/checkout', { exact: true }).first()+ ).toBeVisible();+ await expect(page.getByText('/cart', { exact: true })).toHaveCount(0);+ await search.fill('zzz-no-such-page');+ await expect(page.getByText('No pages').first()).toBeVisible();+ await shot(page, 'siblings-pages-empty', false);+ await search.fill('');+ await expect(page.getByText('/cart', { exact: true }).first()).toBeVisible();++ // The details panel only opens for projects with Search Console connected,+ // which the seeded projects are not: a row click must stay a no-op.+ await page.getByText('/cart', { exact: true }).first().click();+ await expect(page.getByRole('dialog')).toHaveCount(0);++ await attachProblems(testInfo, problems);+ expect(expectNoRealProblems(problems)).toEqual({+ pageErrors: [],+ responses: [],+ });+});++test('insights: filters keep their state in the URL and the empty state is shown', async ({+ page,+}) => {+ await page.setViewportSize({ width: 1440, height: 1000 });+ await page.goto('/acme/acme-shop/insights');+ await expect(+ page.getByRole('heading', { name: 'Insights', level: 1 })+ ).toBeVisible();+ await page.waitForLoadState('networkidle');+ await page.getByPlaceholder('Search insights...').fill('google');+ await expect(page).toHaveURL(/search=google/);+ for (const [trigger, option] of [+ ['All Windows', '7 Days'],+ ['All Severity', 'Severe'],+ ['All Directions', 'Increasing'],+ ] as const) {+ await page+ .getByRole('combobox')+ .filter({ hasText: new RegExp(trigger, 'i') })+ .click();+ await page.getByRole('option', { name: option, exact: true }).click();+ }+ await shot(page, 'siblings-insights-filtered', false);+ const url = page.url();+ await page.reload();+ await expect(page.getByPlaceholder('Search insights...')).toHaveValue(+ 'google'+ );+ expect(new URL(page.url()).search).toBe(new URL(url).search);+});++test.describe('390px mobile viewport', () => {+ test.use({ viewport: MOBILE_VIEWPORT, hasTouch: true, isMobile: true });++ test('overview fits the viewport and its controls work', async ({+ page,+ }, testInfo) => {+ const problems = watchPage(page);+ await gotoOverview(page, 'acme-shop', '?range=7d');+ await dismissFeedbackPrompt(page);+ await shot(page, 'mobile-overview-top', false);++ const overflow = await page.evaluate(+ () => document.documentElement.scrollWidth - window.innerWidth+ );+ expect(overflow, 'horizontal overflow in px').toBeLessThanOrEqual(0);+ for (const title of [+ 'Unique Visitors',+ 'Sessions',+ 'Pageviews',+ 'Pages per session',+ 'Bounce Rate',+ 'Session Duration',+ 'Revenue',+ ]) {+ const box = await metricCard(page, title).boundingBox();+ expect(box, title).not.toBeNull();+ expect(+ (box?.x ?? 0) + (box?.width ?? 0),+ `${title} right edge`+ ).toBeLessThanOrEqual(MOBILE_VIEWPORT.width);+ }++ await metricCard(page, 'Sessions').click();+ await expect(page).toHaveURL(/metric=1/);++ await page+ .getByRole('button', { name: 'Last 7 days', exact: true })+ .click();+ await page.getByRole('menuitem', { name: /^Last 30 days/ }).click();+ await expect(page).toHaveURL(/range=30d/);+ await shot(page, 'mobile-overview-30d', false);++ // Below `md` the Filters button is icon-only and has no accessible name.+ await page+ .locator('button:has(svg.lucide-funnel, svg.lucide-filter)')+ .first()+ .click();+ await expect(page.getByRole('dialog', { name: 'Filters' })).toBeVisible();+ await shot(page, 'mobile-overview-filters', false);+ await page+ .getByRole('dialog', { name: 'Filters' })+ .getByRole('button', { name: 'Close' })+ .click();++ await page+ .getByRole('button', { name: 'Google', exact: true })+ .first()+ .click();+ await expect(+ page.getByRole('button', { name: 'Remove filter' })+ ).toHaveCount(1);+ await shot(page, 'mobile-overview-filtered');++ const overflowAfter = await page.evaluate(+ () => document.documentElement.scrollWidth - window.innerWidth+ );+ expect(overflowAfter, 'horizontal overflow in px').toBeLessThanOrEqual(0);++ await attachProblems(testInfo, problems);+ expect(expectNoRealProblems(problems)).toEqual({+ pageErrors: [],+ responses: [],+ });+ });++ test('realtime and pages fit the viewport', async ({ page }) => {+ for (const path of ['realtime', 'pages']) {+ await page.goto(`/acme/acme-shop/${path}`);+ await page.waitForLoadState('networkidle');+ await page.waitForTimeout(2000);+ await shot(page, `mobile-${path}`);+ const overflow = await page.evaluate(+ () => document.documentElement.scrollWidth - window.innerWidth+ );+ expect(+ overflow,+ `${path}: horizontal overflow in px`+ ).toBeLessThanOrEqual(0);+ }+ });+});diff --git a/apps/start/e2e/overview-widgets.spec.ts b/apps/start/e2e/overview-widgets.spec.tsnew file mode 100644index 000000000..b1c1d8bb3--- /dev/null+++ b/apps/start/e2e/overview-widgets.spec.ts@@ -0,0 +1,516 @@+import type { Locator, Page } from '@playwright/test';+import { expect as baseExpect, test } from './fixtures';+import {+ attachProblems,+ compactNumber,+ dismissFeedbackPrompt,+ expectNoRealProblems,+ fetchStats,+ filter,+ gotoOverview,+ metricCard,+ PROJECT_IDS,+ SLOW_TEST_TIMEOUT_MS,+ shot,+ trpcQuery,+ watchPage,+} from './overview-helpers';++// The shared dev stack answers slowly while other suites run against it.+const expect = baseExpect.configure({ timeout: 30_000 });+test.use({ actionTimeout: 20_000 });++const SHOP = 'acme-shop';+const WIDGET_ROW_LIMIT = 15;++interface GenericRow {+ name: string | null;+ prefix?: string;+ sessions: number;+ pageviews: number;+ revenue?: number;+}+interface PageRow {+ origin: string;+ path: string;+ sessions: number;+ pageviews: number;+}++test.beforeEach(async ({ page }) => {+ test.setTimeout(SLOW_TEST_TIMEOUT_MS);+ await page.setViewportSize({ width: 1440, height: 1000 });+});++function widget(page: Page, searchPlaceholder: RegExp): Locator {+ return page+ .locator('div.card')+ .filter({ has: page.getByPlaceholder(searchPlaceholder) })+ .last();+}++function topGeneric(page: Page, projectId: string, column: string) {+ return trpcQuery<GenericRow[]>(page, 'overview.topGeneric', {+ projectId,+ range: '7d',+ filters: [],+ column,+ });+}++async function scrollThroughOverview(page: Page) {+ for (let step = 0; step < 8; step++) {+ await page.mouse.wheel(0, 900);+ await page.waitForTimeout(400);+ }+ await page.waitForLoadState('networkidle');+}++const GENERIC_TABS = [+ {+ search: /^Search (refs|urls|types|source|medium|campaign|term|content)$/,+ tabs: [+ ['Refs', 'referrer_name'],+ ['Urls', 'referrer'],+ ['Types', 'referrer_type'],+ ['Source', 'utm_source'],+ ['Medium', 'utm_medium'],+ ['Campaign', 'utm_campaign'],+ ['Term', 'utm_term'],+ ['Content', 'utm_content'],+ ],+ },+ {+ search:+ /^Search (devices|browser|browser version|os|os version|brands|models)$/,+ tabs: [+ ['Devices', 'device'],+ ['Browser', 'browser'],+ ['Browser Version', 'browser_version'],+ ['OS', 'os'],+ ['OS Version', 'os_version'],+ ['Brands', 'brand'],+ ['Models', 'model'],+ ],+ },+ {+ search: /^Search (countries|regions|cities)$/,+ tabs: [+ ['Countries', 'country'],+ ['Regions', 'region'],+ ['Cities', 'city'],+ ],+ },+] as const;++test('sources, devices and geo widgets: every tab lists the API rows and adds up to the session total', async ({+ page,+}, testInfo) => {+ const problems = watchPage(page);+ await gotoOverview(page, SHOP, '?range=7d');+ await dismissFeedbackPrompt(page);+ const stats = await fetchStats(page, SHOP, {});++ for (const { search, tabs } of GENERIC_TABS) {+ const card = widget(page, search);+ await card.scrollIntoViewIfNeeded();+ for (const [label, column] of tabs) {+ await card.getByRole('button', { name: label, exact: true }).click();+ const rows = await topGeneric(page, SHOP, column);+ const sessions = rows.reduce((total, row) => total + row.sessions, 0);+ expect(sessions, `${column} sessions add up`).toBe(+ stats.metrics.total_sessions+ );+ const pageviews = rows.reduce((total, row) => total + row.pageviews, 0);+ expect(pageviews, `${column} pageviews add up`).toBe(+ stats.metrics.total_screen_views+ );+ const revenue = rows.reduce(+ (total, row) => total + (row.revenue ?? 0),+ 0+ );+ expect(revenue, `${column} revenue adds up`).toBe(+ stats.metrics.total_revenue+ );++ const named = rows.find((row) => row.name);+ // Countries are listed by display name, not by the ISO code the API returns.+ if (named?.name && column !== 'country') {+ await expect(+ // Regions, cities and versions are prefixed with their parent ("US > California").+ card+ .getByRole('button')+ .filter({ hasText: named.name.replace(/https?:\/\/(www\.)?/, '') })+ .first(),+ `${column} first named row`+ ).toBeVisible();+ await expect(card, `${column} sessions of ${named.name}`).toContainText(+ compactNumber(named.sessions)+ );+ }+ await expect(card).not.toContainText(/NaN|undefined|\bnull\b/);+ expect(+ await card.getByRole('button').filter({ hasText: /./ }).count()+ ).toBeGreaterThan(0);+ }+ await shot(page, `widgets-${tabs[0][1]}-last-tab`, false);+ await card.getByRole('button', { name: tabs[0][0], exact: true }).click();+ }++ await attachProblems(testInfo, problems);+ expect(expectNoRealProblems(problems)).toEqual({+ pageErrors: [],+ responses: [],+ });+});++test('clicking a UTM source row filters the overview to that source', async ({+ page,+}) => {+ await gotoOverview(page, SHOP, '?range=7d&sources=utm_source');+ await dismissFeedbackPrompt(page);+ const rows = await topGeneric(page, SHOP, 'utm_source');+ const source = rows.find((row) => row.name);+ expect(source?.name).toBeTruthy();+ const card = widget(page, /^Search source$/);+ await card+ .getByRole('button', { name: String(source?.name), exact: true })+ .click();+ await expect(page.getByRole('button', { name: 'Remove filter' })).toHaveCount(+ 1+ );+ await page.waitForLoadState('networkidle');+ await shot(page, 'widgets-utm-filter', false);+ await expect(metricCard(page, 'Sessions')).toContainText(+ compactNumber(Number(source?.sessions))+ );+});++test('pages widget: tabs, search, sorting, domain toggle and the details modal', async ({+ page,+}, testInfo) => {+ const problems = watchPage(page);+ await gotoOverview(page, SHOP, '?range=7d');+ await dismissFeedbackPrompt(page);+ const card = widget(page, /^Search (pages|entries|exits)$/);+ const pages = await trpcQuery<PageRow[]>(page, 'overview.topPages', {+ projectId: SHOP,+ range: '7d',+ filters: [],+ mode: 'page',+ });+ const top = pages[0];+ await expect(+ card.getByRole('button', { name: String(top?.path), exact: true })+ ).toBeVisible();+ expect(pages.length).toBeGreaterThan(WIDGET_ROW_LIMIT);++ // search+ await card.getByPlaceholder('Search pages').fill('checkout');+ await expect(+ card.getByRole('button', { name: '/checkout', exact: true })+ ).toBeVisible();+ await expect(+ card.getByRole('button', { name: '/cart', exact: true })+ ).toHaveCount(0);+ await card.getByPlaceholder('Search pages').fill('zzz-no-such-page');+ await shot(page, 'widgets-pages-search-empty', false);+ await card.getByPlaceholder('Search pages').fill('');++ // sort ascending by views: desc -> asc+ await card.getByRole('button', { name: 'Views' }).click();+ await card.getByRole('button', { name: 'Views' }).click();+ const leastViewed = [...pages].sort((a, b) => a.pageviews - b.pageviews)[0];+ await shot(page, 'widgets-pages-sorted-asc', false);+ await expect(card).toContainText(String(leastViewed?.path));+ await card.getByRole('button', { name: 'Views' }).click();++ // domain toggle+ await card.getByRole('button', { name: 'Show domain' }).click();+ await expect(page).toHaveURL(/d=true/);+ await expect(card).toContainText('shop.acme.test');+ await card.getByRole('button', { name: 'Hide domain' }).click();++ // entries / exits+ for (const [label, mode, column] of [+ ['Entries', 'entry', 'Entries'],+ ['Exits', 'exit', 'Exits'],+ ] as const) {+ await card.getByRole('button', { name: label, exact: true }).click();+ await expect(page).toHaveURL(new RegExp(`pages=${mode}`));+ const rows = await trpcQuery<PageRow[]>(page, 'overview.topPages', {+ projectId: SHOP,+ range: '7d',+ filters: [],+ mode,+ });+ await expect(card).toContainText(column);+ await expect(+ card.getByRole('button', { name: String(rows[0]?.path), exact: true })+ ).toBeVisible();+ await expect(card).not.toContainText(/NaN|undefined/);+ await shot(page, `widgets-pages-${mode}`, false);+ }++ // The details button on the Exits tab should list exit pages.+ const exits = await trpcQuery<PageRow[]>(page, 'overview.topPages', {+ projectId: SHOP,+ range: '7d',+ filters: [],+ mode: 'exit',+ });+ await card.locator('button').filter({ hasNotText: /./ }).first().click();+ const modal = page.getByRole('dialog');+ await expect(modal.getByRole('heading')).toBeVisible();+ await shot(page, 'widgets-pages-modal-from-exits', false);+ await expect+ .soft(modal.getByRole('heading'), 'details modal opened from the Exits tab')+ .toHaveText(/Exit/);+ const firstRow = modal.locator('div.group\\/row').first();+ await expect+ .soft(firstRow, 'first modal row is the top exit page')+ .toContainText(compactNumber(Number(exits[0]?.sessions)));++ // search + virtualised list inside the modal+ await modal.getByPlaceholder('Search pages...').fill('/product/');+ await expect(+ modal.getByRole('button', { name: '/cart', exact: true })+ ).toHaveCount(0);+ await expect(modal.locator('div.group\\/row').first()).toContainText(+ '/product/'+ );+ await modal.getByPlaceholder('Search pages...').fill('zzz-no-such-page');+ await expect(modal).toContainText('No results found');+ await modal.getByPlaceholder('Search pages...').fill('');+ await modal.locator('div.overflow-y-auto').evaluate((element) => {+ element.scrollTop = element.scrollHeight;+ });+ await expect(+ modal.getByRole('button', { name: String(pages.at(-1)?.path), exact: true })+ ).toBeVisible();++ // clicking a row filters by page+ await modal.locator('div.overflow-y-auto').evaluate((element) => {+ element.scrollTop = 0;+ });+ await modal.getByRole('button', { name: '/cart', exact: true }).click();+ await expect(page).toHaveURL(/f=path(,|%2C)is(,|%2C)(%2F|%252F|\/)cart/);+ await page.keyboard.press('Escape');+ await expect(page.getByRole('button', { name: 'Remove filter' })).toHaveCount(+ 2+ );++ await attachProblems(testInfo, problems);+ expect(expectNoRealProblems(problems)).toEqual({+ pageErrors: [],+ responses: [],+ });+});++test('generic details modal: search, rows from the API, click to filter; chart view toggle', async ({+ page,+}, testInfo) => {+ const problems = watchPage(page);+ await gotoOverview(page, SHOP, '?range=7d&geo=city');+ await dismissFeedbackPrompt(page);+ const cities = await topGeneric(page, SHOP, 'city');+ expect(cities.length).toBeGreaterThan(WIDGET_ROW_LIMIT);++ const geo = widget(page, /^Search cities$/);+ await geo.scrollIntoViewIfNeeded();+ await geo.locator('button').filter({ hasNotText: /./ }).first().click();+ const modal = page.getByRole('dialog');+ await expect(modal.getByRole('heading')).toHaveText('Top Cities');+ await expect(modal.locator('div.group\\/row').first()).toContainText(+ String(cities[0]?.name)+ );+ await shot(page, 'widgets-city-modal', false);+ await modal.locator('div.overflow-y-auto').evaluate((element) => {+ element.scrollTop = element.scrollHeight;+ });+ await expect(modal.locator('div.group\\/row').last()).toContainText(+ cities.at(-1)?.name || 'Not set'+ );+ const searchTerm = String(+ cities.find((row, index) => index >= 3 && row.name)?.name+ );+ await modal.getByRole('searchbox').fill(searchTerm.toLowerCase());+ await expect(modal.locator('div.group\\/row')).toHaveCount(+ cities.filter(+ (row) =>+ row.name?.toLowerCase().includes(searchTerm.toLowerCase()) ||+ row.prefix?.toLowerCase().includes(searchTerm.toLowerCase())+ ).length+ );+ await modal.locator('div.group\\/row').first().getByRole('button').click();+ await expect(page).toHaveURL(/f=city/);+ await page.keyboard.press('Escape');+ const filtered = await fetchStats(page, SHOP, {+ filters: [filter('city', [searchTerm])],+ });+ await expect(metricCard(page, 'Sessions')).toContainText(+ compactNumber(Number(filtered.metrics.total_sessions))+ );+ await page.getByRole('button', { name: 'Remove filter' }).click();++ // chart view+ const sources = widget(page, /^Search refs$/);+ await sources.scrollIntoViewIfNeeded();+ await sources.getByRole('button', { name: 'Switch to chart view' }).click();+ await expect(page).toHaveURL(/view=chart/);+ await expect(+ sources.locator('svg.recharts-surface, svg').first()+ ).toBeVisible();+ await page.waitForLoadState('networkidle');+ await shot(page, 'widgets-chart-view', false);+ await expect(sources).not.toContainText(/NaN|undefined/);+ await sources.getByRole('button', { name: 'Switch to table view' }).click();+ await expect(+ sources.getByRole('button', { name: 'Google', exact: true })+ ).toBeVisible();++ await attachProblems(testInfo, problems);+ expect(expectNoRealProblems(problems)).toEqual({+ pageErrors: [],+ responses: [],+ });+});++test('events widget tabs, geo map, weekly trends and user journey render', async ({+ page,+}, testInfo) => {+ const problems = watchPage(page);+ await gotoOverview(page, SHOP, '?range=7d');+ await dismissFeedbackPrompt(page);+ const events = await trpcQuery<Array<{ name: string; count: number }>>(+ page,+ 'overview.topEvents',+ {+ projectId: SHOP,+ range: '7d',+ filters: [],+ excludeEvents: ['session_start', 'session_end', 'screen_view'],+ }+ );+ const card = widget(page, /^Search (events|conversions|link out)$/);+ await card.scrollIntoViewIfNeeded();+ await expect(+ card.getByRole('button', { name: String(events[0]?.name), exact: true })+ ).toBeVisible();+ await expect(card).toContainText(compactNumber(Number(events[0]?.count)));++ await card.getByRole('button', { name: 'Conversions', exact: true }).click();+ await expect(+ card.getByRole('button', { name: 'purchase', exact: true })+ ).toBeVisible();+ await shot(page, 'widgets-conversions', false);+ await card.getByRole('button', { name: 'Link out', exact: true }).click();+ await page.waitForLoadState('networkidle');+ await shot(page, 'widgets-link-out', false);+ await expect(card).not.toContainText(/NaN|undefined/);+ await card.getByRole('button', { name: 'Events', exact: true }).click();++ await page.getByText('Map', { exact: true }).scrollIntoViewIfNeeded();+ await expect(page.locator('figure svg path').first()).toBeAttached();+ await scrollThroughOverview(page);++ for (const tab of [+ 'Sessions',+ 'Pageviews',+ 'Bounce Rate',+ 'Pages / Session',+ 'Session Duration',+ 'Unique Visitors',+ ]) {+ await page.getByRole('button', { name: tab, exact: true }).last().click();+ }+ await expect(page.getByText('User Journey')).toBeVisible();+ for (const steps of ['3 Steps', '5 Steps']) {+ await page.getByRole('button', { name: steps, exact: true }).click();+ await page.waitForLoadState('networkidle');+ await expect(+ page.locator('svg').filter({ hasText: '/category/' }).first()+ ).toBeVisible();+ }+ await shot(page, 'widgets-journey', false);++ await attachProblems(testInfo, problems);+ expect(expectNoRealProblems(problems)).toEqual({+ pageErrors: [],+ responses: [],+ });+});++for (const projectId of PROJECT_IDS) {+ test(`${projectId}: every widget renders without errors and breakdowns add up`, async ({+ page,+ }, testInfo) => {+ const problems = watchPage(page);+ await gotoOverview(page, projectId, '?range=7d');+ await dismissFeedbackPrompt(page);+ const stats = await fetchStats(page, projectId, {});++ for (const column of [+ 'referrer_name',+ 'referrer_type',+ 'utm_source',+ 'country',+ 'city',+ 'device',+ 'browser',+ 'os',+ 'brand',+ 'model',+ ]) {+ const rows = await topGeneric(page, projectId, column);+ expect(+ rows.reduce((total, row) => total + row.sessions, 0),+ `${column} sessions`+ ).toBe(stats.metrics.total_sessions);+ expect(+ rows.reduce((total, row) => total + (row.revenue ?? 0), 0),+ `${column} revenue`+ ).toBe(stats.metrics.total_revenue);+ }+ for (const mode of ['entry', 'exit']) {+ const rows = await trpcQuery<PageRow[]>(page, 'overview.topPages', {+ projectId,+ range: '7d',+ filters: [],+ mode,+ });+ expect(+ rows.reduce((total, row) => total + row.sessions, 0),+ `${mode} pages sessions`+ ).toBe(stats.metrics.total_sessions);+ }+ const pages = await trpcQuery<PageRow[]>(page, 'overview.topPages', {+ projectId,+ range: '7d',+ filters: [],+ mode: 'page',+ });+ expect(pages.reduce((total, row) => total + row.pageviews, 0)).toBe(+ stats.metrics.total_screen_views+ );++ const hasRevenue = Number(stats.metrics.total_revenue) > 0;+ const sources = widget(page, /^Search refs$/);+ await expect(sources.getByRole('button', { name: 'Revenue' })).toHaveCount(+ hasRevenue ? 1 : 0+ );++ await scrollThroughOverview(page);+ await page.waitForTimeout(1500);+ await shot(page, `widgets-${projectId}-bottom`, false);+ await expect(page.locator('main, body').first()).not.toContainText(+ /NaN|undefined|Something went wrong/+ );++ await attachProblems(testInfo, problems);+ expect(expectNoRealProblems(problems)).toEqual({+ pageErrors: [],+ responses: [],+ });+ });+}diff --git a/apps/start/e2e/project-create.spec.ts b/apps/start/e2e/project-create.spec.tsnew file mode 100644index 000000000..95bc1444d--- /dev/null+++ b/apps/start/e2e/project-create.spec.ts@@ -0,0 +1,25 @@+import { expect, test } from './fixtures';++test('a project can be created from the organization page', async ({+ page,+ seed,+}) => {+ const projectName = `E2E project ${Date.now()}`;+ await page.goto(`/${seed.organizationId}`);+ await page.waitForLoadState('networkidle');+ // The sidebar action button cycles its label; the menu behind it is stable.+ await page.getByRole('button', { name: /Create a project/ }).click();+ await page.getByRole('menuitem', { name: 'Create a project' }).click();++ const dialog = page.getByRole('dialog');+ await dialog.getByLabel('Project name').fill(projectName);+ await dialog.getByText('Backend / API', { exact: true }).click();+ await dialog.getByRole('button', { name: 'Create project' }).click();++ await expect(dialog.getByText('Your project is created')).toBeVisible();+ await dialog+ .getByRole('button', { name: 'Close', exact: true })+ .first()+ .click();+ await expect(page.getByText(projectName, { exact: true })).toBeVisible();+});diff --git a/apps/start/e2e/reports-builder.spec.ts b/apps/start/e2e/reports-builder.spec.tsnew file mode 100644index 000000000..9c5748fbb--- /dev/null+++ b/apps/start/e2e/reports-builder.spec.ts@@ -0,0 +1,299 @@+import { expect, test } from './fixtures';+import {+ addEvent,+ closeSidebar,+ createDashboard,+ deleteDashboard,+ eventSeries,+ hideFeedbackPrompt,+ openSidebar,+ pickChartType,+ reportInput,+ shot,+ trackProblems,+ trpcQuery,+ uniqueName,+ waitForChart,+} from './reports-helpers';++const SHOP = 'acme-shop';+const CHECKOUT_STEPS = [+ 'product_viewed',+ 'add_to_cart',+ 'checkout_started',+ 'shipping_info_added',+ 'payment_info_added',+ 'purchase',+];++interface ChartSeries {+ names: string[];+ metrics: { sum: number };+}++test.use({ actionTimeout: 20_000 });++test.beforeEach(async ({ page }) => {+ await hideFeedbackPrompt(page);+});++test('linear report: build, compare with the API, save, reload, edit, update', async ({+ page,+}) => {+ test.setTimeout(300_000);+ const problems = trackProblems(page);+ await page.goto(`/acme/${SHOP}/dashboards`);+ const dashboard = await createDashboard(page, SHOP, 'builder');+ try {+ await page.goto(`/acme/${SHOP}/reports?dashboardId=${dashboard.id}`);+ await page.waitForLoadState('networkidle');+ await expect(+ page.getByText('Pick at least one event to start visualising')+ ).toBeVisible({ timeout: 30_000 });+ await addEvent(page, 'purchase');+ await addEvent(page, 'add_to_cart');+ const sheet = await openSidebar(page);+ await sheet.getByRole('button', { name: 'Select breakdown' }).click();+ await page.getByRole('menuitem', { name: 'Event properties' }).click();+ await page.getByPlaceholder('Search').last().fill('device');+ await page+ .locator('[data-radix-popper-content-wrapper]')+ .getByText('device', { exact: true })+ .first()+ .click();+ await closeSidebar(page);+ await waitForChart(page);+ await shot(page, 'builder-01-linear');++ const api = await trpcQuery<{ series: ChartSeries[] }>(+ page,+ 'chart.chart',+ reportInput(SHOP, {+ series: eventSeries(['purchase', 'add_to_cart']),+ breakdowns: [{ id: 'b', name: 'device' }],+ })+ );+ const tableText = (+ await page.locator('#report-editor').innerText()+ ).replace(/\s+/g, ' ');+ for (const serie of api.series) {+ // Row cells read: serie, breakdown, unique, sum.+ const row = new RegExp(+ `${serie.names[0].replace(/[()]/g, '\\$&')} ${serie.names[1]} \\d+ ${serie.metrics.sum} `+ );+ expect(+ tableText,+ `${serie.names.join(' / ')} shows sum ${serie.metrics.sum}`+ ).toMatch(row);+ }++ await page.getByRole('button', { name: 'Save', exact: true }).click();+ await expect(+ page.getByRole('heading', { name: 'Create report' })+ ).toBeVisible();+ const name = uniqueName('linear');+ await page.getByPlaceholder('Name').fill(name);+ await shot(page, 'builder-02-save-modal');+ await page+ .getByRole('button', { name: 'Save', exact: true })+ .last()+ .click();+ await expect(page).toHaveURL(/\/reports\/[0-9a-f-]{36}/, {+ timeout: 30_000,+ });+ const reportId = page+ .url()+ .match(/reports\/([0-9a-f-]{36})/)?.[1] as string;++ await page.reload();+ await waitForChart(page);+ await expect(page.getByRole('button', { name })).toBeVisible();+ await expect(page.getByRole('button', { name: 'Update' })).toBeDisabled();++ await pickChartType(page, 'Linear', 'Bar');+ await waitForChart(page);+ await shot(page, 'builder-03-bar');+ await expect(page.getByRole('button', { name: 'Update' })).toBeEnabled({+ timeout: 30_000,+ });+ await page.getByRole('button', { name: 'Update' }).click();+ await expect(page.getByText('Report updated.')).toBeVisible();+ const saved = await trpcQuery<{+ chartType: string;+ series: unknown[];+ breakdowns: { name: string }[];+ }>(page, 'report.get', { reportId });+ expect(saved.chartType).toBe('bar');+ expect(saved.series).toHaveLength(2);+ expect(saved.breakdowns.map((breakdown) => breakdown.name)).toEqual([+ 'device',+ ]);+ expect(problems.pageErrors).toEqual([]);+ expect(problems.responses).toEqual([]);+ } finally {+ await deleteDashboard(page, dashboard.id);+ }+});++test('every chart type renders the built report without errors', async ({+ page,+}) => {+ test.setTimeout(420_000);+ const problems = trackProblems(page);+ await page.goto(`/acme/${SHOP}/reports`);+ await page.waitForLoadState('networkidle');+ await addEvent(page, 'purchase');+ await addEvent(page, 'add_to_cart');+ await closeSidebar(page);+ await waitForChart(page);+ const order = [+ 'Linear',+ 'Bar',+ 'Histogram',+ 'Pie',+ 'Metric',+ 'Area',+ 'Map',+ 'Funnel',+ 'Conversion',+ ];+ for (let index = 1; index < order.length; index++) {+ await pickChartType(page, order[index - 1], order[index]);+ await waitForChart(page);+ await shot(page, `builder-10-${order[index].toLowerCase()}`);+ await expect(page.locator('#report-editor')).not.toContainText(+ /NaN|undefined|Something went wrong/+ );+ }+ expect(problems.pageErrors).toEqual([]);+ expect(problems.responses).toEqual([]);+});++test('checkout funnel matches the API and never grows between steps', async ({+ page,+}) => {+ test.setTimeout(300_000);+ const problems = trackProblems(page);+ await page.goto(`/acme/${SHOP}/reports`);+ await page.waitForLoadState('networkidle');+ await openSidebar(page);+ await closeSidebar(page);+ await pickChartType(page, 'Linear', 'Funnel');+ for (const step of CHECKOUT_STEPS) {+ await addEvent(page, step);+ }+ await closeSidebar(page);+ await waitForChart(page);+ await shot(page, 'builder-20-funnel');+ const api = await trpcQuery<{ current: { steps: { count: number }[] }[] }>(+ page,+ 'chart.funnel',+ reportInput(SHOP, {+ chartType: 'funnel',+ series: eventSeries(CHECKOUT_STEPS),+ })+ );+ const counts = api.current[0].steps.map((step) => step.count);+ expect(counts).toHaveLength(CHECKOUT_STEPS.length);+ for (let index = 1; index < counts.length; index++) {+ expect(counts[index]).toBeLessThanOrEqual(counts[index - 1]);+ }+ const funnelText = (await page.locator('#report-editor').innerText()).replace(+ /\s+/g,+ ' '+ );+ for (const [index, step] of CHECKOUT_STEPS.entries()) {+ expect(funnelText, `${step} shows ${counts[index]}`).toContain(+ `${step} ${counts[index]} `+ );+ }+ expect(problems.pageErrors).toEqual([]);+ expect(problems.responses).toEqual([]);+});++test('retention on acme-saas shows the cohort sizes the API returns', async ({+ page,+}) => {+ test.setTimeout(300_000);+ const problems = trackProblems(page);+ await page.goto('/acme/acme-saas/reports');+ await page.waitForLoadState('networkidle');+ await openSidebar(page);+ await closeSidebar(page);+ await pickChartType(page, 'Linear', 'Retention');+ await addEvent(page, 'session_start');+ await addEvent(page, 'session_start');+ await closeSidebar(page);+ await waitForChart(page);+ await shot(page, 'builder-30-retention');+ const api = await trpcQuery<{ cohort_interval: string; sum: number }[]>(+ page,+ 'chart.cohort',+ {+ projectId: 'acme-saas',+ firstEvent: ['session_start'],+ secondEvent: ['session_start'],+ criteria: 'on_or_after',+ interval: 'day',+ range: '30d',+ }+ );+ expect(api.length).toBeGreaterThan(5);+ const editor = page.locator('#report-editor');+ for (const cohort of api.slice(1, 6)) {+ const row = editor+ .locator('tr')+ .filter({ hasText: cohort.cohort_interval })+ .first();+ await expect(row).toContainText(String(cohort.sum));+ }+ expect(problems.pageErrors).toEqual([]);+ expect(problems.responses).toEqual([]);+});++test('sankey "between" mode shows the path from start to end event', async ({+ page,+}) => {+ const result = await trpcQuery<{ nodes: unknown[] }>(+ page,+ 'chart.sankey',+ reportInput(SHOP, {+ chartType: 'sankey',+ series: eventSeries(['checkout_started', 'purchase']),+ options: { type: 'sankey', mode: 'between', steps: 10, exclude: [] },+ })+ );+ expect(result.nodes.length).toBeGreaterThan(0);+});++test('session and profile-field filters narrow the result', async ({+ page,+}) => {+ const total = async (extra: Record<string, unknown>) => {+ const result = await trpcQuery<{ series: ChartSeries[] }>(+ page,+ 'chart.chart',+ reportInput(SHOP, { series: eventSeries(['screen_view']), ...extra })+ );+ return result.series.reduce((sum, serie) => sum + serie.metrics.sum, 0);+ };+ const unfiltered = await total({});+ const bounced = await total({+ globalFilters: [+ { id: 'g', name: 'session.is_bounce', operator: 'is', value: ['true'] },+ ],+ });+ const impossibleEmail = await total({+ globalFilters: [+ {+ id: 'g',+ name: 'profile.email',+ operator: 'is',+ value: ['[email protected]'],+ type: 'string',+ },+ ],+ });+ expect.soft(bounced).toBeLessThan(unfiltered);+ expect.soft(impossibleEmail).toBe(0);+});diff --git a/apps/start/e2e/reports-dashboards.spec.ts b/apps/start/e2e/reports-dashboards.spec.tsnew file mode 100644index 000000000..f4ba9a751--- /dev/null+++ b/apps/start/e2e/reports-dashboards.spec.ts@@ -0,0 +1,242 @@+import { expect, test } from './fixtures';+import {+ createDashboard,+ createReport,+ deleteDashboard,+ eventSeries,+ hideFeedbackPrompt,+ reportInput,+ shot,+ trackProblems,+ trpcQuery,+ uniqueName,+} from './reports-helpers';++const PROJECT = 'acme-app';+const LIST_URL = `/acme/${PROJECT}/dashboards`;++test.beforeEach(async ({ page }) => {+ await hideFeedbackPrompt(page);+});++test('create, rename and delete a dashboard from the list', async ({+ page,+}) => {+ test.setTimeout(120_000);+ const problems = trackProblems(page);+ const name = uniqueName('crud');+ await page.goto(LIST_URL);+ await page.waitForLoadState('networkidle');+ await shot(page, 'dash-01-list');++ await expect(async () => {+ await page+ .getByRole('button', { name: /Create dashboard|^Dashboard$/ })+ .first()+ .click({ timeout: 3000 });+ await expect(page.getByText('Add dashboard')).toBeVisible({+ timeout: 3000,+ });+ }).toPass({ timeout: 45_000 });+ await expect(+ page.getByRole('button', { name: 'Create', exact: true })+ ).toBeDisabled();+ await page.getByPlaceholder('Name of the dashboard').fill(name);+ await page.keyboard.press('Enter');+ await expect(page).toHaveURL(/\/dashboards\/[^/]+$/, { timeout: 20_000 });+ await expect(page.getByRole('heading', { name })).toBeVisible();+ await expect(page.getByText('No reports')).toBeVisible();+ await shot(page, 'dash-02-empty-dashboard');+ const dashboardId = page.url().split('/').pop() as string;++ await page.goto(LIST_URL);+ const card = page.locator('.card', { hasText: name });+ await expect(card).toBeVisible();+ await expect(async () => {+ await card.locator('button[aria-haspopup="menu"]').click({ timeout: 3000 });+ await page.getByRole('menuitem', { name: 'Edit' }).click({ timeout: 3000 });+ }).toPass({ timeout: 45_000 });+ const renamed = `${name} renamed`;+ await page.getByRole('textbox').last().fill(renamed);+ await page.keyboard.press('Enter');+ await expect(page.locator('.card', { hasText: renamed })).toBeVisible();++ await page+ .locator('.card', { hasText: renamed })+ .locator('button[aria-haspopup="menu"]')+ .click();+ await page.getByRole('menuitem', { name: 'Delete' }).click();+ await shot(page, 'dash-03-confirm-delete');+ await page+ .getByRole('button', { name: /^(Yes|Confirm|Delete|Continue|OK)/i })+ .last()+ .click();+ await expect(page.locator('.card', { hasText: renamed })).toHaveCount(0);+ const remaining = await trpcQuery<{ id: string }[]>(page, 'dashboard.list', {+ projectId: PROJECT,+ });+ expect(remaining.map((item) => item.id)).not.toContain(dashboardId);+ expect(problems.pageErrors).toEqual([]);+ expect(problems.responses).toEqual([]);+});++test('dashboard page: search, range, duplicate, move, delete, reload', async ({+ page,+}) => {+ test.setTimeout(240_000);+ const problems = trackProblems(page);+ await page.goto(LIST_URL);+ const source = await createDashboard(page, PROJECT, 'source');+ const target = await createDashboard(page, PROJECT, 'target');+ try {+ await createReport(+ page,+ source.id,+ 'E2E reports opens',+ reportInput(PROJECT, { series: eventSeries(['app_opened']) })+ );+ await createReport(+ page,+ source.id,+ 'E2E reports signups metric',+ reportInput(PROJECT, {+ chartType: 'metric',+ metric: 'sum',+ series: eventSeries(['signup_completed']),+ })+ );++ await page.goto(`${LIST_URL}/${source.id}`);+ await page.waitForLoadState('networkidle');+ const cards = page.locator('.react-grid-item, .card.flex.h-full');+ await expect(page.getByText('E2E reports opens')).toBeVisible({+ timeout: 30_000,+ });+ await expect(page.getByText('E2E reports signups metric')).toBeVisible();+ await page.waitForTimeout(3000);+ await shot(page, 'dash-10-two-reports');++ const search = page.getByPlaceholder('Search reports...');+ await search.fill('zzz-nothing');+ await expect(page.getByText('No matching reports')).toBeVisible();+ await search.fill('metric');+ await expect(page.getByText('E2E reports opens')).toHaveCount(0);+ await expect(page.getByText('E2E reports signups metric')).toBeVisible();+ await search.fill('');++ await page+ .getByRole('button', { name: /Last 7 days|Last 30 days|Today/ })+ .first()+ .click();+ await page.getByRole('menuitem', { name: 'Last 30 days' }).click();+ await expect(page).toHaveURL(/range=30d/);+ await page.reload();+ await expect(+ page.getByRole('button', { name: 'Last 30 days' })+ ).toBeVisible({ timeout: 30_000 });+ await expect(page.getByText('E2E reports opens')).toBeVisible({+ timeout: 30_000,+ });++ const opensCard = page+ .locator('.card', { hasText: 'E2E reports opens' })+ .last();+ const menuTrigger = (text: string) =>+ page+ .locator('.card', { hasText: text })+ .last()+ .locator('button[aria-haspopup="menu"]');+ await expect(async () => {+ await menuTrigger('E2E reports opens').click({ timeout: 3000 });+ await expect(+ page.getByRole('menuitem', { name: 'Duplicate' })+ ).toBeVisible({ timeout: 2000 });+ }).toPass({ timeout: 30_000 });+ await page.getByRole('menuitem', { name: 'Duplicate' }).click();+ await expect(page.getByText('Report duplicated')).toBeVisible();+ await expect+ .poll(+ async () =>+ (+ await trpcQuery<unknown[]>(page, 'report.list', {+ dashboardId: source.id,+ projectId: PROJECT,+ })+ ).length+ )+ .toBe(3);+ await page.waitForTimeout(1500);+ await shot(page, 'dash-11-duplicated');++ await menuTrigger('E2E reports signups metric').click();+ await page.getByRole('menuitem', { name: 'Move to dashboard' }).click();+ await expect(page.getByText('Move report')).toBeVisible();+ await shot(page, 'dash-12-move-modal');+ await expect(+ page.getByRole('button', { name: 'Move', exact: true })+ ).toBeDisabled();+ await page.getByRole('button', { name: target.name }).click();+ await page.getByRole('button', { name: 'Move', exact: true }).click();+ await expect(page.getByText('Report moved')).toBeVisible();+ await expect(page.getByText('E2E reports signups metric')).toHaveCount(0);+ const moved = await trpcQuery<{ name: string }[]>(page, 'report.list', {+ dashboardId: target.id,+ projectId: PROJECT,+ });+ expect(moved.map((report) => report.name)).toEqual([+ 'E2E reports signups metric',+ ]);++ await menuTrigger('E2E reports opens').click();+ await page.getByRole('menuitem', { name: 'Delete' }).click();+ await expect(page.getByText('Report deleted')).toBeVisible();+ await expect+ .poll(+ async () =>+ (+ await trpcQuery<unknown[]>(page, 'report.list', {+ dashboardId: source.id,+ projectId: PROJECT,+ })+ ).length+ )+ .toBe(1);+ await expect(opensCard).toBeVisible();+ await expect(cards.first()).toBeVisible();+ expect(problems.pageErrors).toEqual([]);+ expect(problems.responses).toEqual([]);+ } finally {+ await deleteDashboard(page, source.id).catch(() => undefined);+ await deleteDashboard(page, target.id).catch(() => undefined);+ }+});++test('a dashboard id that does not exist shows a not-found state', async ({+ page,+}) => {+ await page.goto(`${LIST_URL}/does-not-exist-e2e`);+ await page.waitForLoadState('networkidle');+ await page.waitForTimeout(2000);+ await shot(page, 'dash-20-missing');+ await expect(page.locator('body')).toContainText(+ /not found|does not exist|404/i+ );+});++test('dashboards list fits a 390px viewport', async ({ page }) => {+ await page.setViewportSize({ width: 390, height: 844 });+ await page.goto(LIST_URL);+ const dashboard = await createDashboard(page, PROJECT, 'mobile');+ try {+ await page.goto(`${LIST_URL}/${dashboard.id}`);+ await page.waitForLoadState('networkidle');+ await page.waitForTimeout(1500);+ await shot(page, 'dash-30-mobile');+ const overflow = await page.evaluate(+ () => document.documentElement.scrollWidth - window.innerWidth+ );+ expect(overflow).toBeLessThanOrEqual(1);+ } finally {+ await deleteDashboard(page, dashboard.id);+ }+});diff --git a/apps/start/e2e/reports-errors.spec.ts b/apps/start/e2e/reports-errors.spec.tsnew file mode 100644index 000000000..e6d64a5bb--- /dev/null+++ b/apps/start/e2e/reports-errors.spec.ts@@ -0,0 +1,142 @@+import { expect, test } from './fixtures';+import {+ addEvent,+ closeSidebar,+ hideFeedbackPrompt,+ openSidebar,+ pickChartType,+ shot,+ trackProblems,+ waitForChart,+} from './reports-helpers';++const SHOP = 'acme-shop';++test.use({ actionTimeout: 20_000 });++test.beforeEach(async ({ page }) => {+ await hideFeedbackPrompt(page);+});++test('a report id that does not exist shows a not-found state', async ({+ page,+}) => {+ test.setTimeout(120_000);+ await page.goto(`/acme/${SHOP}/reports/00000000-0000-4000-8000-000000000000`);+ await page.waitForLoadState('networkidle');+ await page.waitForTimeout(3000);+ await shot(page, 'err-01-missing-report');+ await expect(page.locator('body')).toContainText(+ /not found|does not exist|404/i,+ { timeout: 60_000 }+ );+});++test('an impossible filter shows the empty state, an invalid regex a readable error', async ({+ page,+}) => {+ test.setTimeout(240_000);+ const problems = trackProblems(page);+ await page.goto(`/acme/${SHOP}/reports`);+ await page.waitForLoadState('networkidle');+ await addEvent(page, 'purchase');+ const sheet = await openSidebar(page);+ await sheet+ .getByRole('button', { name: 'Add filter', exact: true })+ .first()+ .click();+ await page.getByRole('menuitem', { name: 'Event properties' }).click();+ await page.getByPlaceholder('Search').last().fill('payment');+ await page+ .locator('[data-radix-popper-content-wrapper]')+ .getByText('payment', { exact: true })+ .click();+ await sheet.getByRole('button', { name: 'Is', exact: true }).click();+ await page.getByRole('menuitem', { name: 'Contains', exact: true }).click();+ const valueInput = sheet.locator('input').last();+ await valueInput.fill('no-such-payment-method');+ await valueInput.press('Enter');+ await closeSidebar(page);+ await waitForChart(page);+ await shot(page, 'err-10-impossible-filter');+ await expect(page.locator('#report-editor')).toContainText('No data');++ await openSidebar(page);+ await sheet.getByRole('button', { name: 'Contains', exact: true }).click();+ await page.getByRole('menuitem', { name: 'Regex', exact: true }).click();+ await sheet.locator('input').last().fill('((');+ await sheet.locator('input').last().press('Enter');+ await closeSidebar(page);+ await waitForChart(page);+ await page.waitForTimeout(8000);+ await shot(page, 'err-11-invalid-regex');+ await expect+ .soft(page.locator('#report-editor'))+ .not.toContainText(/re2|OptimizedRegularExpression/);+ expect+ .soft(+ problems.responses.filter((line) => line.startsWith('500')),+ 'an invalid regex is a validation error, not a 500'+ )+ .toEqual([]);+});++test('conversion with a single event explains what is missing instead of failing', async ({+ page,+}) => {+ test.setTimeout(180_000);+ const problems = trackProblems(page);+ await page.goto(`/acme/${SHOP}/reports`);+ await page.waitForLoadState('networkidle');+ await addEvent(page, 'purchase');+ await closeSidebar(page);+ await pickChartType(page, 'Linear', 'Conversion');+ await waitForChart(page);+ await page.waitForTimeout(8000);+ await shot(page, 'err-20-conversion-one-event');+ expect(problems.responses.filter((line) => line.startsWith('500'))).toEqual(+ []+ );+});++test('leaving the builder and pressing back returns to the builder', async ({+ page,+}) => {+ test.setTimeout(180_000);+ await page.goto(`/acme/${SHOP}/reports`);+ await page.waitForLoadState('networkidle');+ await addEvent(page, 'purchase');+ await closeSidebar(page);+ await waitForChart(page);+ await page.getByRole('link', { name: 'Dashboards' }).click();+ await expect(page).toHaveURL(/\/dashboards$/);+ await page.goBack();+ await expect(page).toHaveURL(/\/reports$/);+ await page.waitForTimeout(5000);+ await shot(page, 'err-30-after-back');+ await expect(+ page,+ 'going back keeps the user on the report builder'+ ).toHaveURL(/\/reports$/);+ await expect(page.getByRole('button', { name: 'Pick events' })).toBeVisible({+ timeout: 30_000,+ });+});++test('report builder fits a 390px viewport', async ({ page }) => {+ test.setTimeout(180_000);+ await page.setViewportSize({ width: 390, height: 844 });+ await page.goto(`/acme/${SHOP}/reports`);+ await page.waitForLoadState('networkidle');+ await page.waitForTimeout(3000);+ await shot(page, 'err-39-mobile-builder');+ await addEvent(page, 'purchase');+ await shot(page, 'err-40-mobile-sidebar');+ await closeSidebar(page);+ await waitForChart(page);+ await shot(page, 'err-41-mobile-chart');+ const overflow = await page.evaluate(+ () => document.documentElement.scrollWidth - window.innerWidth+ );+ expect(overflow).toBeLessThanOrEqual(1);+});diff --git a/apps/start/e2e/reports-helpers.ts b/apps/start/e2e/reports-helpers.tsnew file mode 100644index 000000000..371fc6f6f--- /dev/null+++ b/apps/start/e2e/reports-helpers.ts@@ -0,0 +1,234 @@+import type { Page } from '@playwright/test';++export const API_URL = 'https://api.main.local.openpanel.cc';+export const ORGANIZATION_ID = 'acme';+export const SHOTS_DIR = 'test-results/reports/shots';++const IGNORED_CONSOLE = [+ /vite/i,+ /favicon/i,+ /sentry/i,+ /Download the React DevTools/i,+ /\[HMR\]/i,+ /status of 401/i,+ /hydrat/i,+];++export interface PageProblems {+ console: string[];+ pageErrors: string[];+ responses: string[];+}++export function trackProblems(page: Page): PageProblems {+ const problems: PageProblems = { console: [], pageErrors: [], responses: [] };+ page.on('console', (message) => {+ if (message.type() !== 'error') {+ return;+ }+ const text = message.text();+ if (IGNORED_CONSOLE.some((pattern) => pattern.test(text))) {+ return;+ }+ problems.console.push(text.slice(0, 500));+ });+ page.on('pageerror', (error) => {+ problems.pageErrors.push(String(error).slice(0, 500));+ });+ page.on('response', (response) => {+ if (+ response.status() >= 400 &&+ !/favicon|sentry|api\.openpanel\.dev/.test(response.url())+ ) {+ problems.responses.push(+ `${response.status()} ${response.request().method()} ${response.url().slice(0, 300)}`+ );+ }+ });+ return problems;+}++export async function trpcQuery<T = unknown>(+ page: Page,+ procedure: string,+ input: unknown+): Promise<T> {+ const response = await page.request.get(+ `${API_URL}/trpc/${procedure}?input=${encodeURIComponent(JSON.stringify({ json: input }))}`+ );+ const body = await response.json();+ if (!response.ok()) {+ throw new Error(+ `${procedure} ${response.status()}: ${JSON.stringify(body).slice(0, 400)}`+ );+ }+ return body.result.data.json as T;+}++export async function trpcMutation<T = unknown>(+ page: Page,+ procedure: string,+ input: unknown+): Promise<T> {+ const response = await page.request.post(`${API_URL}/trpc/${procedure}`, {+ data: { json: input },+ });+ const body = await response.json();+ if (!response.ok()) {+ throw new Error(+ `${procedure} ${response.status()}: ${JSON.stringify(body).slice(0, 400)}`+ );+ }+ return body.result.data.json as T;+}++export function uniqueName(label: string): string {+ return `E2E reports ${label} ${Date.now()}`;+}++export async function createDashboard(+ page: Page,+ projectId: string,+ label: string+): Promise<{ id: string; name: string }> {+ return trpcMutation(page, 'dashboard.create', {+ name: uniqueName(label),+ projectId,+ });+}++export async function deleteDashboard(page: Page, id: string): Promise<void> {+ await trpcMutation(page, 'dashboard.delete', { id, forceDelete: true });+}++export async function shot(page: Page, name: string): Promise<void> {+ await page.screenshot({ path: `${SHOTS_DIR}/${name}.png`, fullPage: true });+}++export async function hideFeedbackPrompt(page: Page): Promise<void> {+ await page.context().addCookies([+ {+ name: 'feedback-prompt-seen',+ value: encodeURIComponent(JSON.stringify(new Date().toISOString())),+ domain: 'main.local.openpanel.cc',+ path: '/',+ },+ ]);+}++interface SeriesEvent {+ id: string;+ type: 'event';+ name: string;+ segment: string;+ filters: unknown[];+ property?: string;+}++export function eventSeries(+ names: string[],+ overrides: Partial<SeriesEvent> = {}+): SeriesEvent[] {+ return names.map((name, index) => ({+ id: String.fromCharCode(65 + index),+ type: 'event',+ name,+ segment: 'event',+ filters: [],+ ...overrides,+ }));+}++export function reportInput(+ projectId: string,+ overrides: Record<string, unknown>+): Record<string, unknown> {+ return {+ projectId,+ chartType: 'linear',+ interval: 'day',+ range: '30d',+ series: [],+ breakdowns: [],+ previous: false,+ metric: 'sum',+ lineType: 'monotone',+ ...overrides,+ };+}++export async function createReport(+ page: Page,+ dashboardId: string,+ name: string,+ report: Record<string, unknown>+): Promise<{ id: string; name: string }> {+ const { projectId: _projectId, ...rest } = report;+ return trpcMutation(page, 'report.create', {+ dashboardId,+ report: { ...rest, name },+ });+}++/** The editor shows a "Stay calm" placeholder while the chart query runs. */+export async function waitForChart(page: Page): Promise<void> {+ const editor = page.locator('#report-editor');+ await editor.waitFor({ timeout: 45_000 });+ await page+ .waitForFunction(+ () => {+ const element = document.querySelector('#report-editor');+ const text = element?.textContent ?? '';+ return (+ text.length > 0 &&+ !text.includes('Stay calm') &&+ !element?.querySelector('.animate-pulse')+ );+ },+ undefined,+ { timeout: 45_000 }+ )+ .catch(() => undefined);+ await page.waitForTimeout(700);+}++export async function openSidebar(page: Page) {+ const sheet = page.locator('[role="dialog"][aria-describedby]');+ for (let attempt = 0; attempt < 10; attempt++) {+ if (await sheet.isVisible()) {+ return sheet;+ }+ await page+ .getByRole('button', { name: 'Pick events' })+ .click({ timeout: 3000 })+ .catch(() => undefined);+ await sheet.waitFor({ timeout: 5000 }).catch(() => undefined);+ }+ throw new Error('The report sidebar never opened');+}++export async function addEvent(page: Page, name: string): Promise<void> {+ const sheet = await openSidebar(page);+ await sheet.getByRole('combobox').filter({ hasText: 'Select event' }).click();+ await page.getByPlaceholder('Search event...').fill(name);+ await page+ .getByRole('option')+ .filter({ hasText: new RegExp(`^${name}(\\d|\\s|$)`) })+ .first()+ .click({ timeout: 15_000 });+}++export async function pickChartType(+ page: Page,+ current: string,+ next: string+): Promise<void> {+ await page.getByRole('button', { name: current, exact: true }).click();+ await page.getByRole('menuitem', { name: next }).click();+}++export async function closeSidebar(page: Page): Promise<void> {+ const sheet = page.locator('[role="dialog"][aria-describedby]');+ await sheet.getByRole('button', { name: 'Done' }).click();+ await sheet.waitFor({ state: 'hidden' });+}diff --git a/apps/start/e2e/reports-share.spec.ts b/apps/start/e2e/reports-share.spec.tsnew file mode 100644index 000000000..bccca8fa1--- /dev/null+++ b/apps/start/e2e/reports-share.spec.ts@@ -0,0 +1,275 @@+import type { Browser } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ createDashboard,+ createReport,+ deleteDashboard,+ eventSeries,+ hideFeedbackPrompt,+ reportInput,+ shot,+ trackProblems,+ trpcMutation,+ trpcQuery,+} from './reports-helpers';++const SHOP = 'acme-shop';+const DASHBOARD_URL = 'https://main.local.openpanel.cc';+const PASSWORD = 'e2e-share-secret';++interface ShareSettings {+ id: string;+ public: boolean;+ hasPassword: boolean;+}++async function anonymousPage(browser: Browser) {+ const context = await browser.newContext({+ ignoreHTTPSErrors: true,+ baseURL: DASHBOARD_URL,+ });+ context.setDefaultNavigationTimeout(45_000);+ return { context, page: await context.newPage() };+}++test.use({ actionTimeout: 15_000, navigationTimeout: 45_000 });++test.beforeEach(async ({ page }) => {+ await hideFeedbackPrompt(page);+});++test('share a dashboard publicly, with a password, then make it private', async ({+ page,+ browser,+}) => {+ test.setTimeout(300_000);+ await page.goto(`/acme/${SHOP}/dashboards`);+ const dashboard = await createDashboard(page, SHOP, 'share');+ try {+ await createReport(+ page,+ dashboard.id,+ 'E2E reports shared purchases',+ reportInput(SHOP, { series: eventSeries(['purchase']) })+ );+ await page.goto(`/acme/${SHOP}/dashboards/${dashboard.id}`);+ await page.waitForLoadState('networkidle');+ const openShareModal = async () => {+ await expect(async () => {+ if (+ !(await page+ .getByRole('menuitem', { name: 'Share dashboard' })+ .isVisible())+ ) {+ await page+ .locator('button:has(svg.lucide-ellipsis)')+ .first()+ .click({ timeout: 3000 });+ }+ await page+ .getByRole('menuitem', { name: 'Share dashboard' })+ .click({ timeout: 3000 });+ await expect(+ page.getByText('Dashboard public availability')+ ).toBeVisible({ timeout: 3000 });+ }).toPass({ timeout: 45_000 });+ };+ const settings = () =>+ trpcQuery<ShareSettings | null>(page, 'share.dashboardSettings', {+ projectId: SHOP,+ dashboardId: dashboard.id,+ });++ await openShareModal();+ await shot(page, 'share-01-modal');+ await page.getByRole('button', { name: 'Make it public' }).click();+ await expect+ .poll(async () => (await settings())?.public, { timeout: 20_000 })+ .toBe(true);+ const share = (await settings()) as ShareSettings;+ expect(share.public).toBe(true);+ expect(share.hasPassword).toBe(false);++ const anonymous = await anonymousPage(browser);+ const anonymousProblems = trackProblems(anonymous.page);+ await anonymous.page.goto(`/share/dashboard/${share.id}`);+ await expect(+ anonymous.page.getByText('E2E reports shared purchases')+ ).toBeVisible({ timeout: 45_000 });+ await anonymous.page.waitForTimeout(3000);+ await shot(anonymous.page, 'share-02-public-dashboard');+ expect(anonymousProblems.responses).toEqual([]);++ await openShareModal();+ await expect(page.getByText('Currently shared')).toBeVisible();+ await page+ .getByPlaceholder('Enter your password (optional)')+ .fill(PASSWORD);+ await page.getByRole('button', { name: 'Update' }).click();+ await expect+ .poll(async () => (await settings())?.hasPassword, { timeout: 20_000 })+ .toBe(true);+ await page.waitForTimeout(1500);++ const locked = await anonymousPage(browser);+ await locked.page.goto(`/share/dashboard/${share.id}`);+ await expect(locked.page.locator('input[type="password"]')).toBeVisible({+ timeout: 45_000,+ });+ await expect(+ locked.page.getByText('E2E reports shared purchases')+ ).toHaveCount(0);+ await shot(locked.page, 'share-03-password-gate');+ await locked.page.locator('input[type="password"]').fill('wrong-password');+ await locked.page.keyboard.press('Enter');+ await locked.page.waitForTimeout(2000);+ await expect(+ locked.page.getByText('E2E reports shared purchases')+ ).toHaveCount(0);+ await locked.page.locator('input[type="password"]').fill(PASSWORD);+ await locked.page.keyboard.press('Enter');+ await expect(+ locked.page.getByText('E2E reports shared purchases')+ ).toBeVisible({ timeout: 45_000 });+ await locked.context.close();++ // Reopening the modal and pressing Update without retyping must keep the password.+ await openShareModal();+ await page.waitForTimeout(1000);+ await shot(page, 'share-04-modal-with-password');+ await page.getByRole('button', { name: 'Update' }).click();+ await page.waitForTimeout(4000);+ expect+ .soft(+ ((await settings()) as ShareSettings).hasPassword,+ 'Update without retyping keeps the password'+ )+ .toBe(true);++ await trpcMutation(page, 'share.createDashboard', {+ organizationId: 'acme',+ projectId: SHOP,+ dashboardId: dashboard.id,+ public: false,+ password: null,+ });+ expect(((await settings()) as ShareSettings).public).toBe(false);+ await anonymous.page.goto(`/share/dashboard/${share.id}`);+ await expect(anonymous.page.getByText('Share not found')).toBeVisible({+ timeout: 45_000,+ });+ await shot(anonymous.page, 'share-05-disabled');+ await anonymous.context.close();+ } finally {+ await deleteDashboard(page, dashboard.id);+ }+});++test('share reports of several chart types and open them logged out', async ({+ page,+ browser,+}) => {+ test.setTimeout(300_000);+ await page.goto(`/acme/${SHOP}/dashboards`);+ const dashboard = await createDashboard(page, SHOP, 'share-report');+ try {+ const linear = await createReport(+ page,+ dashboard.id,+ 'E2E reports shared linear',+ reportInput(SHOP, { series: eventSeries(['purchase']) })+ );+ const sankey = await createReport(+ page,+ dashboard.id,+ 'E2E reports shared sankey',+ reportInput(SHOP, {+ chartType: 'sankey',+ series: eventSeries(['product_viewed']),+ options: { type: 'sankey', mode: 'after', steps: 5, exclude: [] },+ })+ );++ await page.goto(`/acme/${SHOP}/reports/${linear.id}`);+ await page.waitForLoadState('networkidle');+ await expect(async () => {+ await page+ .getByRole('button', { name: 'Share', exact: true })+ .click({ timeout: 3000 });+ await expect(+ page.getByRole('button', { name: 'Make it public' })+ ).toBeVisible({ timeout: 3000 });+ }).toPass({ timeout: 45_000 });+ await shot(page, 'share-10-report-modal');+ await page.getByRole('button', { name: 'Make it public' }).click();+ await expect+ .poll(+ async () =>+ (+ await trpcQuery<ShareSettings | null>(+ page,+ 'share.reportSettings',+ { projectId: SHOP, reportId: linear.id }+ )+ )?.public,+ { timeout: 20_000 }+ )+ .toBe(true);+ const linearShare = await trpcQuery<ShareSettings>(+ page,+ 'share.reportSettings',+ { projectId: SHOP, reportId: linear.id }+ );+ const sankeyShare = await trpcMutation<ShareSettings>(+ page,+ 'share.createReport',+ {+ organizationId: 'acme',+ projectId: SHOP,+ reportId: sankey.id,+ public: true,+ password: null,+ }+ );++ const anonymous = await anonymousPage(browser);+ const problems = trackProblems(anonymous.page);+ await anonymous.page.goto(`/share/report/${linearShare.id}`);+ await expect(+ anonymous.page.getByText('E2E reports shared linear')+ ).toBeVisible({ timeout: 45_000 });+ await expect(+ anonymous.page.locator('svg.recharts-surface').first()+ ).toBeVisible({ timeout: 45_000 });+ await shot(anonymous.page, 'share-11-public-report');+ expect(problems.responses).toEqual([]);++ await anonymous.page.goto(`/share/report/${sankeyShare.id}`);+ await expect(+ anonymous.page.getByText('E2E reports shared sankey')+ ).toBeVisible({ timeout: 45_000 });+ await anonymous.page.waitForTimeout(5000);+ await shot(anonymous.page, 'share-12-public-sankey');+ expect+ .soft(+ problems.responses,+ 'a shared sankey report loads for a logged-out viewer'+ )+ .toEqual([]);++ await trpcMutation(page, 'share.createReport', {+ organizationId: 'acme',+ projectId: SHOP,+ reportId: linear.id,+ public: false,+ password: null,+ });+ await anonymous.page.goto(`/share/report/${linearShare.id}`);+ await expect(anonymous.page.getByText('Share not found')).toBeVisible({+ timeout: 45_000,+ });+ await anonymous.context.close();+ } finally {+ await deleteDashboard(page, dashboard.id);+ }+});diff --git a/apps/start/e2e/settings-access.spec.ts b/apps/start/e2e/settings-access.spec.tsnew file mode 100644index 000000000..417ed24f3--- /dev/null+++ b/apps/start/e2e/settings-access.spec.ts@@ -0,0 +1,181 @@+import type { APIRequestContext } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ anonymousApi,+ createThrowawayProject,+ LOGGED_OUT,+ SLOW_ASSERT,+ SLOW_TEST_TIMEOUT_MS,+ scheduleProjectDeletion,+ seededApi,+ type ThrowawayProject,+ trpcMutation,+ trpcQuery,+} from './settings-helpers';++const OUTSIDER_PASSWORD = 'Openpanel-e2e-12345!';+const PROJECT_READS = [+ 'project.getProjectWithClients',+ 'client.list',+ 'notification.list',+ 'notification.rules',+ 'integration.list',+ 'import.list',+];++let admin: APIRequestContext;+let outsider: APIRequestContext;+let anonymous: APIRequestContext;+let project: ThrowawayProject;+let organizationId: string;++function writesAgainst(target: ThrowawayProject): [string, unknown][] {+ return [+ ['project.update', { id: target.id, name: 'E2E settings taken over' }],+ ['project.delete', { projectId: target.id }],+ ['project.cancelDeletion', { projectId: target.id }],+ [+ 'client.create',+ { name: 'x', projectId: target.id, organizationId, type: 'root' },+ ],+ ['client.update', { id: target.clientId, name: 'taken over' }],+ ['client.remove', { id: target.clientId }],+ [+ 'notification.createOrUpdateRule',+ {+ name: 'x',+ projectId: target.id,+ integrations: [],+ sendToApp: false,+ sendToEmail: false,+ config: { type: 'events', events: [] },+ },+ ],+ [+ 'integration.createOrUpdate',+ {+ name: 'x',+ projectId: target.id,+ config: { type: 'discord', url: 'https://example.com/hook' },+ },+ ],+ [+ 'widget.toggle',+ { projectId: target.id, organizationId, type: 'counter', enabled: true },+ ],+ [+ 'import.create',+ {+ projectId: target.id,+ provider: 'umami',+ config: {+ provider: 'umami',+ type: 'file',+ fileUrl: 'https://example.com/export.csv',+ projectMapper: [],+ },+ },+ ],+ ];+}++test.describe.configure({ mode: 'serial' });+test.setTimeout(SLOW_TEST_TIMEOUT_MS);++test.beforeAll(async ({ seed }) => {+ organizationId = seed.organizationId;+ admin = await seededApi();+ anonymous = await anonymousApi();+ outsider = await anonymousApi();+ project = await createThrowawayProject(admin, organizationId, 'access');++ // A real account that is not a member of the seeded organization.+ const signedUp = await trpcMutation(outsider, 'auth.signUpEmail', {+ firstName: 'E2E',+ lastName: 'Outsider',+ email: `e2e-settings-outsider-${Date.now()}@openpanel.local`,+ password: OUTSIDER_PASSWORD,+ confirmPassword: OUTSIDER_PASSWORD,+ });+ expect(signedUp.status, signedUp.errorMessage).toBe(200);+});++test.afterAll(async () => {+ await scheduleProjectDeletion(admin, project.id);+ await Promise.all([admin.dispose(), outsider.dispose(), anonymous.dispose()]);+});++test('project settings are not readable without a session', async () => {+ for (const procedure of PROJECT_READS) {+ const result = await trpcQuery(anonymous, procedure, {+ projectId: project.id,+ });+ expect(result.status, procedure).toBe(401);+ expect(result.data, procedure).toBeUndefined();+ }+});++test('a signed-in user outside the organization can read nothing of the project', async () => {+ for (const procedure of [...PROJECT_READS]) {+ const result = await trpcQuery(outsider, procedure, {+ projectId: project.id,+ });+ expect(result.status, procedure).toBe(403);+ expect(result.data, procedure).toBeUndefined();+ }+ const widget = await trpcQuery(outsider, 'widget.get', {+ projectId: project.id,+ type: 'realtime',+ });+ expect(widget.status).toBe(403);+ const projects = await trpcQuery(outsider, 'project.list', {+ organizationId,+ });+ expect(projects.status).toBe(403);+});++test('a signed-in user outside the organization can change nothing of the project', async () => {+ for (const [procedure, input] of writesAgainst(project)) {+ const result = await trpcMutation(outsider, procedure, input);+ expect(result.status, procedure).toBe(403);+ }++ const untouched = await trpcQuery<{+ name: string;+ deleteAt: string | null;+ clients: { id: string; name: string }[];+ }>(admin, 'project.getProjectWithClients', { projectId: project.id });+ expect(untouched.data).toMatchObject({ name: project.name, deleteAt: null });+ expect(untouched.data?.clients).toHaveLength(1);+ expect(untouched.data?.clients[0]).toMatchObject({ id: project.clientId });+});++test('client secrets are never returned after creation, even to an admin', async () => {+ const clients = await trpcQuery<Record<string, unknown>[]>(+ admin,+ 'client.list',+ { projectId: project.id }+ );+ expect(clients.data).toHaveLength(1);+ expect(clients.data?.[0]).not.toHaveProperty('secret');+ expect(JSON.stringify(clients.data)).not.toContain(project.clientSecret);++ const withClients = await trpcQuery(admin, 'project.getProjectWithClients', {+ projectId: project.id,+ });+ expect(JSON.stringify(withClients.data)).not.toContain(project.clientSecret);+ expect(JSON.stringify(withClients.data)).not.toContain('"secret"');+});++test('the settings pages send a logged-out visitor to the login page', async ({+ browser,+}) => {+ const loggedOut = await browser.newContext(LOGGED_OUT);+ const page = await loggedOut.newPage();+ for (const path of ['settings/clients', 'notifications', 'integrations']) {+ await page.goto(`/${organizationId}/${project.id}/${path}`);+ await expect(page).toHaveURL(/\/login/, SLOW_ASSERT);+ await expect(page.getByText(project.clientId)).toHaveCount(0);+ }+ await loggedOut.close();+});diff --git a/apps/start/e2e/settings-clients.spec.ts b/apps/start/e2e/settings-clients.spec.tsnew file mode 100644index 000000000..97abbf16b--- /dev/null+++ b/apps/start/e2e/settings-clients.spec.ts@@ -0,0 +1,256 @@+import type { APIRequestContext, Page } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ apiUrl,+ createThrowawayProject,+ gotoHydrated,+ recordPageIssues,+ SLOW_ASSERT,+ SLOW_TEST_TIMEOUT_MS,+ scheduleProjectDeletion,+ screenshot,+ seededApi,+ sendTrackEvent,+ type ThrowawayProject,+ trpcQuery,+} from './settings-helpers';++const CLIENT_TYPES = ['write', 'read', 'root'] as const;+type ClientType = (typeof CLIENT_TYPES)[number];+const TYPE_LABELS: Record<ClientType, string> = {+ write: 'Write (for ingestion)',+ read: 'Read (access export API)',+ root: 'Root (access export API)',+};+const INGEST_POLL = { timeout: 120_000, intervals: [3000] };++interface Credentials {+ id: string;+ secret: string;+ mcpToken: string | null;+}++let api: APIRequestContext;+let project: ThrowawayProject;+const created = {} as Record<ClientType, Credentials>;++const clientName = (type: ClientType) => `E2E settings ${type} client`;+const createClientButton = (page: Page) =>+ page.getByRole('button', { name: 'Create client' });++function listTools(token: string) {+ return api.post(`${apiUrl()}/mcp?token=${token}`, {+ headers: { accept: 'application/json, text/event-stream' },+ data: { jsonrpc: '2.0', id: 1, method: 'tools/list' },+ });+}++test.describe.configure({ mode: 'serial' });+test.setTimeout(SLOW_TEST_TIMEOUT_MS);++test.beforeAll(async ({ seed }) => {+ api = await seededApi();+ project = await createThrowawayProject(api, seed.organizationId, 'clients');+});++test.afterAll(async () => {+ await scheduleProjectDeletion(api, project.id);+ await api.dispose();+});++test('the create client modal requires a name', async ({ page, seed }) => {+ const issues = recordPageIssues(page);+ await gotoHydrated(+ page,+ `/${seed.organizationId}/${project.id}/settings/clients`,+ createClientButton+ );+ await expect(page.getByText('First client')).toBeVisible(SLOW_ASSERT);++ await createClientButton(page).click();+ const dialog = page.getByRole('dialog');+ await dialog.getByRole('button', { name: 'Create' }).click();+ await expect(dialog.getByPlaceholder('Eg. My App Client')).toHaveClass(+ /border-destructive/+ );+ await expect(dialog.getByText('Your client is created')).toBeHidden();+ await screenshot(page, 'clients-create-empty-name');++ await dialog.getByRole('button', { name: 'Cancel' }).click();+ await expect(dialog).toBeHidden();+ issues.expectNone();+});++for (const type of CLIENT_TYPES) {+ test(`a ${type} client is created and its secret is shown once`, async ({+ page,+ context,+ seed,+ }) => {+ await context.grantPermissions(['clipboard-read', 'clipboard-write']);+ const issues = recordPageIssues(page);+ await gotoHydrated(+ page,+ `/${seed.organizationId}/${project.id}/settings/clients`,+ createClientButton+ );++ await createClientButton(page).click();+ const dialog = page.getByRole('dialog');+ await dialog.getByPlaceholder('Eg. My App Client').fill(clientName(type));+ if (type !== 'write') {+ await dialog.getByRole('combobox').click();+ await page.getByRole('option', { name: TYPE_LABELS[type] }).click();+ }+ await dialog.getByRole('button', { name: 'Create' }).click();+ await expect(dialog.getByText('Your client is created')).toBeVisible(+ SLOW_ASSERT+ );+ await screenshot(page, `clients-created-${type}`);++ await dialog.getByRole('button', { name: 'Copy all' }).click();+ const copied = await page.evaluate(() => navigator.clipboard.readText());+ const id = copied.match(/^CLIENT_ID=(.+)$/m)?.[1] ?? '';+ const secret = copied.match(/^CLIENT_SECRET=(.+)$/m)?.[1] ?? '';+ const mcpToken = copied.match(/^MCP_TOKEN=(.+)$/m)?.[1] ?? null;+ expect(id).toMatch(/^[0-9a-f-]{36}$/);+ expect(secret).toMatch(/^sec_[0-9a-f]+$/);+ await expect(dialog.getByText(id)).toBeVisible();+ await expect(dialog.getByText(secret)).toBeVisible();++ if (type === 'write') {+ expect(mcpToken).toBeNull();+ await expect(dialog.getByText('MCP Token')).toBeHidden();+ } else {+ expect(mcpToken).toBe(Buffer.from(`${id}:${secret}`).toString('base64'));+ await expect(dialog.getByText('MCP Token')).toBeVisible();+ }+ created[type] = { id, secret, mcpToken };++ await dialog.getByRole('button', { name: 'Close' }).last().click();+ await expect(dialog).toBeHidden();+ const row = page.getByRole('row', { name: clientName(type) });+ await expect(row).toContainText(id);+ await expect(page.getByText(secret)).toHaveCount(0);++ const listed = await trpcQuery<Record<string, unknown>[]>(+ api,+ 'client.list',+ { projectId: project.id }+ );+ const listedClient = listed.data?.find((client) => client.id === id);+ expect(listedClient?.type).toBe(type);+ expect(JSON.stringify(listed.data)).not.toContain(secret);+ expect(listedClient).not.toHaveProperty('secret');+ issues.expectNone();+ });+}++test('an event sent with the new write client shows on the events page', async ({+ page,+ seed,+}) => {+ const eventName = `e2e_settings_client_${Date.now()}`;+ const accepted = await sendTrackEvent(api, created.write, eventName, {+ __path: '/pricing',+ });+ expect(accepted.status()).toBe(200);++ const wrongSecret = await sendTrackEvent(+ api,+ { id: created.write.id, secret: 'sec_wrong' },+ eventName+ );+ expect(wrongSecret.status()).toBe(401);++ await expect(async () => {+ await page.goto(`/${seed.organizationId}/${project.id}/events`);+ // The events table shows names with underscores as spaces.+ await expect(+ page.getByText(eventName.replaceAll('_', ' ')).first()+ ).toBeVisible({ timeout: 15_000 });+ }).toPass(INGEST_POLL);+ await screenshot(page, 'clients-event-on-events-page');+});++test('read and root clients answer MCP tools/list, a write client does not', async () => {+ for (const type of ['read', 'root'] as const) {+ const response = await listTools(created[type].mcpToken ?? '');+ expect(response.status(), `${type} client`).toBe(200);+ const body = await response.json();+ expect(body.result.tools.length).toBeGreaterThan(0);+ }++ const writeToken = Buffer.from(+ `${created.write.id}:${created.write.secret}`+ ).toString('base64');+ const rejected = await listTools(writeToken);+ expect(rejected.status()).toBe(401);+});++test('a client can be renamed from the row menu', async ({ page, seed }) => {+ const issues = recordPageIssues(page);+ const renamed = 'E2E settings renamed client';+ await gotoHydrated(+ page,+ `/${seed.organizationId}/${project.id}/settings/clients`,+ createClientButton+ );++ const row = page.getByRole('row', { name: clientName('read') });+ await row.getByRole('button').last().click();+ await page.getByRole('menuitem', { name: 'Edit' }).click();+ const dialog = page.getByRole('dialog');+ const save = dialog.getByRole('button', { name: 'Save' });+ await expect(save).toBeDisabled();++ await dialog.getByLabel('Name').fill(renamed);+ await save.click();+ await expect(dialog).toBeHidden(SLOW_ASSERT);+ await expect(page.getByRole('row', { name: renamed })).toContainText(+ created.read.id+ );++ await page.reload();+ await expect(page.getByRole('row', { name: renamed })).toBeVisible(+ SLOW_ASSERT+ );+ issues.expectNone();+});++test('a revoked client disappears and is rejected by /track', async ({+ page,+ seed,+}) => {+ const issues = recordPageIssues(page);+ await gotoHydrated(+ page,+ `/${seed.organizationId}/${project.id}/settings/clients`,+ createClientButton+ );++ const row = page.getByRole('row', { name: clientName('write') });+ await row.getByRole('button').last().click();+ await page.getByRole('menuitem', { name: 'Revoke' }).click();+ const dialog = page.getByRole('dialog');+ await expect(dialog.getByText('Revoke client')).toBeVisible();+ await dialog.getByRole('button', { name: 'Cancel' }).click();+ await expect(row).toBeVisible();++ await row.getByRole('button').last().click();+ await page.getByRole('menuitem', { name: 'Revoke' }).click();+ await dialog.getByRole('button', { name: 'Yes' }).click();+ await expect(row).toBeHidden(SLOW_ASSERT);+ await screenshot(page, 'clients-after-revoke');++ await expect+ .poll(+ async () =>+ (+ await sendTrackEvent(api, created.write, 'e2e_settings_revoked')+ ).status(),+ { timeout: 30_000 }+ )+ .toBe(401);+ issues.expectNone();+});diff --git a/apps/start/e2e/settings-helpers.ts b/apps/start/e2e/settings-helpers.tsnew file mode 100644index 000000000..a2e5d1b31--- /dev/null+++ b/apps/start/e2e/settings-helpers.ts@@ -0,0 +1,262 @@+// Shared by the settings-*.spec.ts files: a throwaway project per spec file,+// tRPC as the seeded user, and the console/network recorder every spec attaches.++import { readFileSync } from 'node:fs';+import { resolve } from 'node:path';+import {+ type APIRequestContext,+ type BrowserContext,+ expect,+ type Locator,+ type Page,+ request,+} from '@playwright/test';++const AUTH_STATE_FILE = resolve(import.meta.dirname, '.auth', 'user.json');+const SEED_FILE = resolve(import.meta.dirname, '..', '..', '..', '.seed.json');+const SCREENSHOT_DIR = 'test-results/settings/shots';++/** The dev server is shared with other suites; a cold route can take a while. */+export const SLOW_TEST_TIMEOUT_MS = 240_000;+export const SLOW_ASSERT = { timeout: 45_000 };++export const BROWSER_USER_AGENT =+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Chrome/128.0 Safari/537.36';++export function apiUrl(): string {+ const manifest = JSON.parse(readFileSync(SEED_FILE, 'utf8')) as {+ apiUrl: string;+ };+ return process.env.API_URL ?? manifest.apiUrl;+}++export function seededApi(): Promise<APIRequestContext> {+ return request.newContext({+ storageState: AUTH_STATE_FILE,+ ignoreHTTPSErrors: true,+ });+}++/** Playwright applies the project's storage state to every new context unless told otherwise. */+export const LOGGED_OUT = {+ ignoreHTTPSErrors: true,+ storageState: { cookies: [], origins: [] },+};++export function anonymousApi(): Promise<APIRequestContext> {+ return request.newContext(LOGGED_OUT);+}++interface TrpcResult<T> {+ status: number;+ data: T | undefined;+ errorMessage: string | undefined;+}++interface TrpcEnvelope<T> {+ result?: { data?: { json?: T } };+ error?: { json?: { message?: string } };+}++async function unwrap<T>(+ response: Awaited<ReturnType<APIRequestContext['get']>>+): Promise<TrpcResult<T>> {+ // A non-JSON body (a proxy error page) is reported as a bare status.+ const body = (await response+ .json()+ .catch(() => null)) as TrpcEnvelope<T> | null;+ return {+ status: response.status(),+ data: body?.result?.data?.json,+ errorMessage: body?.error?.json?.message,+ };+}++export async function trpcQuery<T = unknown>(+ api: APIRequestContext,+ procedure: string,+ input: unknown+): Promise<TrpcResult<T>> {+ const query = encodeURIComponent(JSON.stringify({ json: input }));+ return unwrap<T>(+ await api.get(`${apiUrl()}/trpc/${procedure}?input=${query}`)+ );+}++export async function trpcMutation<T = unknown>(+ api: APIRequestContext,+ procedure: string,+ input: unknown+): Promise<TrpcResult<T>> {+ return unwrap<T>(+ await api.post(`${apiUrl()}/trpc/${procedure}`, { data: { json: input } })+ );+}++export interface ThrowawayProject {+ id: string;+ name: string;+ clientId: string;+ clientSecret: string;+}++export async function createThrowawayProject(+ api: APIRequestContext,+ organizationId: string,+ label: string+): Promise<ThrowawayProject> {+ const name = `E2E settings ${label} ${Date.now()}`;+ const created = await trpcMutation<{+ id: string;+ client: { id: string; secret: string };+ }>(api, 'project.create', {+ organizationId,+ project: name,+ domain: null,+ cors: [],+ website: false,+ app: false,+ backend: true,+ });+ if (!created.data) {+ throw new Error(+ `project.create failed (${created.status}): ${created.errorMessage}`+ );+ }+ return {+ id: created.data.id,+ name,+ clientId: created.data.client.id,+ clientSecret: created.data.client.secret,+ };+}++/** Projects are soft-deleted: this schedules the deletion the hourly cron performs. */+export async function scheduleProjectDeletion(+ api: APIRequestContext,+ projectId: string+): Promise<void> {+ await trpcMutation(api, 'project.delete', { projectId });+}++export function sendTrackEvent(+ api: APIRequestContext,+ client: { id: string; secret: string },+ name: string,+ properties: Record<string, unknown> = {}+) {+ return api.post(`${apiUrl()}/track`, {+ headers: {+ 'openpanel-client-id': client.id,+ 'openpanel-client-secret': client.secret,+ 'user-agent': BROWSER_USER_AGENT,+ 'x-client-ip': '81.2.69.142',+ },+ data: { type: 'track', payload: { name, properties } },+ });+}++/** The feedback card is fixed over the bottom-right corner and swallows clicks there. */+export async function hideFeedbackPrompt(+ context: BrowserContext,+ baseURL: string | undefined+): Promise<void> {+ if (!baseURL) {+ return;+ }+ await context.addCookies([+ {+ name: 'feedback-prompt-seen',+ value: encodeURIComponent(new Date().toISOString()),+ url: baseURL,+ },+ ]);+}++const IGNORED_NOISE = [+ /api\.openpanel\.dev/, // the dashboard's own analytics, unauthenticated locally+ /status of 401/, // console echo of the line above+ /hydrated but some attributes/, // reported once in the sweep, not per test+ /@vite|\/@id\/|\/@fs\/|\/src\/.*\.(tsx?|css)(\?|$)|node_modules\/\.vite/, // dev-server module noise+ /status of 502/, // console echo of a dev-server module 502+ /favicon/,+ /sentry/i,+ /cdn\.brandfetch\.io|googleusercontent|vecteezy/, // third-party logos+];++export interface PageIssues {+ list: string[];+ expectNone: () => void;+}++export function recordPageIssues(page: Page): PageIssues {+ const list: string[] = [];+ const add = (issue: string) => {+ if (!IGNORED_NOISE.some((pattern) => pattern.test(issue))) {+ list.push(issue);+ }+ };+ page.on('console', (message) => {+ if (message.type() === 'error') {+ add(`console: ${message.text().slice(0, 400)}`);+ }+ });+ page.on('pageerror', (error) => add(`pageerror: ${error.message}`));+ page.on('response', (response) => {+ if (response.status() >= 400) {+ add(+ `${response.status()} ${response.request().method()} ${response.url()}`+ );+ }+ });+ return {+ list,+ expectNone: () =>+ expect(list, 'console errors / failed requests').toEqual([]),+ };+}++const HYDRATION_ATTEMPTS = 3;+const HYDRATION_TIMEOUT_MS = 25_000;++function isHydrated(locator: Locator): Promise<boolean> {+ return locator+ .first()+ .evaluate((element) =>+ Object.keys(element).some((key) => key.startsWith('__reactProps'))+ )+ .catch(() => false);+}++/**+ * SSR paints the page long before React attaches handlers; a click or fill in+ * between is silently lost. React marks hydrated DOM nodes with `__reactProps$…`.+ * The shared dev server sometimes fails to serve the client entry, which leaves+ * the page inert until it is loaded again.+ */+export async function gotoHydrated(+ page: Page,+ path: string,+ marker: (page: Page) => Locator+): Promise<void> {+ for (let attempt = 1; attempt <= HYDRATION_ATTEMPTS; attempt++) {+ await page.goto(path);+ await page.waitForLoadState('networkidle');+ const hydrated = await expect+ .poll(() => isHydrated(marker(page)), { timeout: HYDRATION_TIMEOUT_MS })+ .toBe(true)+ .then(() => true)+ .catch(() => false);+ if (hydrated) {+ return;+ }+ }+ throw new Error(`${path} did not hydrate after ${HYDRATION_ATTEMPTS} loads`);+}++export async function screenshot(page: Page, name: string): Promise<void> {+ await page.screenshot({+ path: `${SCREENSHOT_DIR}/${name}.png`,+ fullPage: true,+ });+}diff --git a/apps/start/e2e/settings-notifications.spec.ts b/apps/start/e2e/settings-notifications.spec.tsnew file mode 100644index 000000000..8e9172ae5--- /dev/null+++ b/apps/start/e2e/settings-notifications.spec.ts@@ -0,0 +1,471 @@+import { createServer, type Server } from 'node:http';+import type { AddressInfo } from 'node:net';+import type { APIRequestContext, Page } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ createThrowawayProject,+ gotoHydrated,+ hideFeedbackPrompt,+ recordPageIssues,+ SLOW_ASSERT,+ SLOW_TEST_TIMEOUT_MS,+ scheduleProjectDeletion,+ screenshot,+ seededApi,+ sendTrackEvent,+ type ThrowawayProject,+ trpcMutation,+ trpcQuery,+} from './settings-helpers';++interface Rule {+ id: string;+ name: string;+ sendToApp: boolean;+ template: string | null;+ config: { type: string; events: { name: string }[] };+ integrations: { id: string; name: string }[];+}++interface Integration {+ id: string;+ name: string;+ config: { type: string; url?: string; headers?: Record<string, string> };+}++const TRIGGER_EVENT = 'e2e_rule_trigger';+const RULE_NAME = 'E2E settings event rule';+const TEMPLATE =+ 'E2E {{name}} on {{path}} plan={{properties.plan}} rule={{rule_name}} / $RULE_NAME / $EVENT_NAME';+const RENDERED_TITLE = `E2E ${TRIGGER_EVENT} on /pricing plan=pro rule=${RULE_NAME} / ${RULE_NAME} / ${TRIGGER_EVENT}`;+const TEMPLATE_VARIABLES = [+ '{{name}}',+ '{{rule_name}}',+ '{{properties.your.property}}',+ '{{profile.firstName}}',+ 'profileId',+ 'createdAt',+ 'country',+ 'city',+ 'os',+ 'osVersion',+ 'browser',+ 'browserVersion',+ 'device',+ 'brand',+ 'model',+ 'path',+ 'origin',+ 'referrer',+ 'referrerName',+ 'referrerType',+];+const PROVIDERS = [+ { name: 'Slack', requiredFields: 1 },+ { name: 'Discord', requiredFields: 2 },+ { name: 'Webhook', requiredFields: 2 },+ { name: 'S3 Export', requiredFields: 3 },+ { name: 'GCS Export', requiredFields: 2 },+];+const DELIVERY_POLL = { timeout: 90_000, intervals: [3000] };++let api: APIRequestContext;+let project: ThrowawayProject;+let organizationId: string;++const projectPath = (path: string) =>+ `/${organizationId}/${project.id}/${path}`;+const addRuleButton = (page: Page) =>+ page.getByRole('button', { name: 'Add Rule' }).first();+const client = () => ({ id: project.clientId, secret: project.clientSecret });++async function fetchRules(): Promise<Rule[]> {+ const rules = await trpcQuery<Rule[]>(api, 'notification.rules', {+ projectId: project.id,+ });+ return rules.data ?? [];+}++async function pickEvent(page: Page, index: number, eventName: string) {+ const dialog = page.getByRole('dialog');+ await dialog+ .getByRole('combobox')+ .nth(index + 1)+ .click();+ await page.getByPlaceholder('Search event...').fill(eventName);+ await page.getByRole('option', { name: eventName }).first().click();+}++async function pickIntegration(page: Page, name: string) {+ await page+ .getByRole('dialog')+ .getByRole('button', { name: /Pick integrations|Website|E2E/ })+ .last()+ .click();+ await page.getByRole('option', { name }).click();+ await page.keyboard.press('Escape');+}++test.describe.configure({ mode: 'serial' });+test.setTimeout(SLOW_TEST_TIMEOUT_MS);++test.beforeAll(async ({ seed }) => {+ organizationId = seed.organizationId;+ api = await seededApi();+ project = await createThrowawayProject(api, organizationId, 'notifications');+ // The rule modal only offers event names the project has already received.+ const seeded = await sendTrackEvent(api, client(), TRIGGER_EVENT, {+ __path: '/pricing',+ });+ expect(seeded.status()).toBe(200);+ await expect+ .poll(async () => {+ const names = await trpcQuery<{ name: string }[]>(api, 'chart.events', {+ projectId: project.id,+ });+ return JSON.stringify(names.data ?? names.errorMessage);+ }, DELIVERY_POLL)+ .toContain(TRIGGER_EVENT);+});++test.afterAll(async () => {+ await scheduleProjectDeletion(api, project.id);+ await api.dispose();+});++test.beforeEach(async ({ context, baseURL }) => {+ await hideFeedbackPrompt(context, baseURL);+});++test('both notification tabs render their empty state', async ({ page }) => {+ const issues = recordPageIssues(page);+ await gotoHydrated(page, projectPath('notifications'), (target) =>+ target.getByRole('tab', { name: 'Rules' })+ );+ await expect(page).toHaveURL(/notifications\/notifications$/);+ await expect(page.getByText('No data')).toBeVisible(SLOW_ASSERT);++ await page.getByRole('tab', { name: 'Rules' }).click();+ await expect(page).toHaveURL(/notifications\/rules$/);+ await expect(page.getByText('No rules yet')).toBeVisible(SLOW_ASSERT);+ await screenshot(page, 'rules-empty');++ await page.goBack();+ await expect(page).toHaveURL(/notifications\/notifications$/);+ issues.expectNone();+});++test('the rule modal validates, lists every template variable and creates an event rule', async ({+ page,+}) => {+ const issues = recordPageIssues(page);+ await gotoHydrated(page, projectPath('notifications/rules'), addRuleButton);+ await addRuleButton(page).click();+ const dialog = page.getByRole('dialog');+ await expect(dialog.getByText('Create rule')).toBeVisible();++ await dialog.getByRole('button', { name: 'Create' }).click();+ await expect(dialog.getByText('Issues', { exact: true })).toBeVisible();++ await dialog.getByLabel('Rule name').fill(RULE_NAME);+ await dialog.getByRole('button', { name: 'Create' }).click();+ await expect(page.getByText('At least one event is required')).toBeVisible();+ expect(await fetchRules()).toEqual([]);++ await dialog.getByRole('combobox').first().click();+ await expect(page.getByRole('option')).toHaveText(['Events', 'Funnel']);+ await page.keyboard.press('Escape');++ await dialog.getByText('Template').locator('svg').hover();+ const tooltip = page.getByRole('tooltip');+ for (const variable of TEMPLATE_VARIABLES) {+ await expect(+ tooltip.getByText(variable, { exact: true }).first()+ ).toBeVisible();+ }+ await screenshot(page, 'rules-template-variables');+ // The tooltip covers the event picker until the pointer acts elsewhere.+ await dialog.getByText('Create rule').click();+ await page.mouse.move(5, 5, { steps: 5 });+ await expect(tooltip).toBeHidden();++ await pickEvent(page, 0, TRIGGER_EVENT);+ await dialog.getByPlaceholder(/You received a new/).fill(TEMPLATE);+ await pickIntegration(page, 'Website');+ await screenshot(page, 'rules-modal-filled');+ await dialog.getByRole('button', { name: 'Create' }).click();+ await expect(page.getByText('Notification rule created')).toBeVisible(+ SLOW_ASSERT+ );+ await expect(dialog).toBeHidden();++ const card = page.locator('.card').filter({ hasText: RULE_NAME });+ await expect(card).toContainText('Get notified when');+ await expect(card).toContainText(TRIGGER_EVENT);+ await expect(card).toContainText('Website');+ const [rule] = await fetchRules();+ expect(rule).toMatchObject({+ name: RULE_NAME,+ sendToApp: true,+ template: TEMPLATE,+ config: { type: 'events', events: [{ name: TRIGGER_EVENT }] },+ });+ // Opening the event picker logs a React list-key warning; reported separately.+ expect(+ issues.list.filter((issue) => !issue.includes('unique "key"'))+ ).toEqual([]);+});++test('a matching event produces an in-app notification with the template filled in', async ({+ page,+}) => {+ const issues = recordPageIssues(page);+ await gotoHydrated(+ page,+ projectPath('notifications/notifications'),+ (target) => target.getByRole('tab', { name: 'Rules' })+ );+ // The dashboard listens on a websocket and toasts new notifications.+ await page.waitForTimeout(2000);++ const sent = await sendTrackEvent(api, client(), TRIGGER_EVENT, {+ __path: '/pricing',+ plan: 'pro',+ });+ expect(sent.status()).toBe(200);+ const unrelated = await sendTrackEvent(api, client(), 'e2e_unrelated_event');+ expect(unrelated.status()).toBe(200);++ await expect(page.getByText(RENDERED_TITLE).first()).toBeVisible(+ DELIVERY_POLL+ );+ await screenshot(page, 'notification-toast');++ await expect(async () => {+ await page.reload();+ await expect(+ page.getByRole('row', { name: new RegExp(RULE_NAME) }).first()+ ).toBeVisible({ timeout: 10_000 });+ }).toPass(DELIVERY_POLL);+ const row = page.getByRole('row').filter({ hasText: RENDERED_TITLE });+ await expect(row).toHaveCount(1);+ await expect(row).toContainText('Chrome');+ await screenshot(page, 'notifications-list');++ await page.getByPlaceholder('Search').fill('no such notification');+ await expect(row).toHaveCount(0);+ await page.getByPlaceholder('Search').fill('plan=pro');+ await expect(row).toHaveCount(1);+ // The table logs a React setState-in-render warning; reported separately.+ expect(+ issues.list.filter((issue) => !issue.includes('Cannot update a component'))+ ).toEqual([]);+});++test('a rule can be edited into a funnel and keeps its integration', async ({+ page,+}) => {+ const renamed = 'E2E settings funnel rule';+ await gotoHydrated(page, projectPath('notifications/rules'), addRuleButton);+ const card = page.locator('.card').filter({ hasText: RULE_NAME });+ await card.getByRole('button', { name: 'Edit' }).click();+ const dialog = page.getByRole('dialog');+ await expect(dialog.getByText('Edit rule')).toBeVisible();+ await expect(dialog.getByLabel('Rule name')).toHaveValue(RULE_NAME);+ await expect(dialog.getByPlaceholder(/You received a new/)).toHaveValue(+ TEMPLATE+ );++ await dialog.getByLabel('Rule name').fill(renamed);+ await dialog.getByRole('combobox').first().click();+ await page.getByRole('option', { name: 'Funnel' }).click();+ await dialog.getByRole('button', { name: 'Add event' }).click();+ await pickEvent(page, 1, 'session_start');+ await screenshot(page, 'rules-modal-funnel');+ await dialog.getByRole('button', { name: 'Update' }).click();+ await expect(page.getByText('Notification rule updated')).toBeVisible(+ SLOW_ASSERT+ );++ const updated = page.locator('.card').filter({ hasText: renamed });+ await expect(updated).toContainText(+ 'Get notified when a session has completed this funnel'+ );+ await expect(updated).toContainText('Website');+ const [rule] = await fetchRules();+ expect(rule).toMatchObject({+ name: renamed,+ sendToApp: true,+ config: {+ type: 'funnel',+ events: [{ name: TRIGGER_EVENT }, { name: 'session_start' }],+ },+ });+});++test('a rule is deleted after confirmation', async ({ page }) => {+ const issues = recordPageIssues(page);+ await gotoHydrated(page, projectPath('notifications/rules'), addRuleButton);+ const card = page.locator('.card').filter({ hasText: 'E2E settings' });+ await card.getByRole('button', { name: 'Delete' }).click();+ const dialog = page.getByRole('dialog');+ await dialog.getByRole('button', { name: 'Cancel' }).click();+ await expect(card).toBeVisible();++ await card.getByRole('button', { name: 'Delete' }).click();+ await dialog.getByRole('button', { name: 'Yes' }).click();+ await expect(page.getByText('Rule deleted')).toBeVisible(SLOW_ASSERT);+ await expect(page.getByText('No rules yet')).toBeVisible(SLOW_ASSERT);+ expect(await fetchRules()).toEqual([]);+ // The funnel card renders its events without keys; reported separately.+ expect(+ issues.list.filter((issue) => !issue.includes('unique "key"'))+ ).toEqual([]);+});++test('every available integration opens a form that validates', async ({+ page,+}) => {+ const issues = recordPageIssues(page);+ const connectButtons = (target: Page) =>+ target.getByRole('button', { name: 'Connect' });+ await gotoHydrated(page, projectPath('integrations'), (target) =>+ target.getByRole('tab', { name: 'Available' })+ );+ await expect(page).toHaveURL(/integrations\/installed$/);+ await expect(page.getByText('No integrations yet')).toBeVisible(SLOW_ASSERT);++ await page.getByRole('tab', { name: 'Available' }).click();+ await expect(connectButtons(page)).toHaveCount(PROVIDERS.length);+ for (const [index, provider] of PROVIDERS.entries()) {+ await connectButtons(page).nth(index).click();+ const dialog = page.getByRole('dialog');+ await expect(+ dialog.getByText(provider.name, { exact: true }).first()+ ).toBeVisible();+ await dialog.getByRole('button', { name: 'Create' }).click();+ await expect(dialog.getByText('Issues', { exact: true })).toHaveCount(+ provider.requiredFields+ );+ await screenshot(page, `integration-validation-${provider.name}`);+ await dialog.getByRole('button', { name: 'Close' }).click();+ await expect(dialog).toBeHidden();+ }++ const integrations = await trpcQuery<Integration[]>(api, 'integration.list', {+ projectId: project.id,+ });+ expect(integrations.data?.map((integration) => integration.id)).toEqual([+ 'app',+ 'email',+ ]);+ issues.expectNone();+});++test.describe('webhook integration', () => {+ let listener: Server;+ let listenerUrl: string;+ const received: string[] = [];++ test.beforeAll(async () => {+ listener = createServer((request, response) => {+ let body = '';+ request.on('data', (chunk) => {+ body += chunk;+ });+ request.on('end', () => {+ received.push(body);+ response.writeHead(200).end('ok');+ });+ });+ await new Promise<void>((resolve) =>+ listener.listen(0, '127.0.0.1', resolve)+ );+ const { port } = listener.address() as AddressInfo;+ listenerUrl = `http://127.0.0.1:${port}/hook`;+ });++ test.afterAll(() => {+ listener.close();+ });++ test('a webhook is created, hides its header value, is edited and deleted', async ({+ page,+ }) => {+ const issues = recordPageIssues(page);+ const name = 'E2E settings webhook';+ await gotoHydrated(page, projectPath('integrations/available'), (target) =>+ target.getByRole('button', { name: 'Connect' }).first()+ );+ await page.getByRole('button', { name: 'Connect' }).nth(2).click();+ const dialog = page.getByRole('dialog');++ await dialog.getByLabel('Name').fill(name);+ await dialog.getByLabel('URL').fill('not a url');+ await dialog.getByRole('button', { name: 'Create' }).click();+ await expect(dialog.getByText('Issues', { exact: true })).toHaveCount(1);++ await dialog.getByLabel('URL').fill(listenerUrl);+ await dialog.getByRole('button', { name: 'Add Header' }).click();+ await dialog.getByPlaceholder('Header Name').fill('x-e2e-auth');+ await dialog.getByPlaceholder('Header Value').fill('super-secret-value');+ await dialog.getByRole('button', { name: 'Create' }).click();+ await expect(page.getByText('Integration created')).toBeVisible(+ SLOW_ASSERT+ );+ await expect(page).toHaveURL(/integrations\/installed$/);++ const card = page.locator('.card').filter({ hasText: name });+ await expect(card).toContainText('Connected');+ await screenshot(page, 'integrations-installed');+ const listed = await trpcQuery<Integration[]>(api, 'integration.list', {+ projectId: project.id,+ });+ const webhook = listed.data?.find(+ (integration) => integration.name === name+ );+ expect(webhook?.config).toMatchObject({+ type: 'webhook',+ url: listenerUrl,+ });+ expect(JSON.stringify(listed.data)).not.toContain('super-secret-value');++ await card.getByRole('button', { name: 'Edit' }).click();+ await expect(dialog.getByLabel('Name')).toHaveValue(name);+ await expect(dialog.getByLabel('URL')).toHaveValue(listenerUrl);+ await expect(dialog.getByPlaceholder('Header Name')).toHaveValue(+ 'x-e2e-auth'+ );+ await expect(dialog.getByPlaceholder('Unchanged')).toHaveValue('');+ await dialog.getByLabel('Name').fill(`${name} edited`);+ await dialog.getByRole('button', { name: 'Update' }).click();+ await expect(dialog).toBeHidden(SLOW_ASSERT);+ const edited = page.locator('.card').filter({ hasText: `${name} edited` });+ await expect(edited).toBeVisible(SLOW_ASSERT);++ // Loopback targets are refused by the worker, so a rule pointing here+ // must not reach the listener.+ const rule = await trpcMutation(api, 'notification.createOrUpdateRule', {+ name: 'E2E settings webhook rule',+ projectId: project.id,+ integrations: [webhook?.id],+ sendToApp: false,+ sendToEmail: false,+ config: {+ type: 'events',+ events: [{ name: TRIGGER_EVENT, segment: 'event', filters: [] }],+ },+ });+ expect(rule.status).toBe(200);+ await sendTrackEvent(api, client(), TRIGGER_EVENT, { __path: '/pricing' });+ await page.waitForTimeout(10_000);+ expect(received).toEqual([]);++ await edited.getByRole('button', { name: 'Delete' }).click();+ await dialog.getByRole('button', { name: 'Yes' }).click();+ await expect(page.getByText('No integrations yet')).toBeVisible(+ SLOW_ASSERT+ );+ issues.expectNone();+ });+});diff --git a/apps/start/e2e/settings-project.spec.ts b/apps/start/e2e/settings-project.spec.tsnew file mode 100644index 000000000..d6d1e0c7b--- /dev/null+++ b/apps/start/e2e/settings-project.spec.ts@@ -0,0 +1,763 @@+import type { APIRequestContext, Page } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ apiUrl,+ createThrowawayProject,+ gotoHydrated,+ hideFeedbackPrompt,+ LOGGED_OUT,+ recordPageIssues,+ SLOW_ASSERT,+ SLOW_TEST_TIMEOUT_MS,+ scheduleProjectDeletion,+ screenshot,+ seededApi,+ sendTrackEvent,+ type ThrowawayProject,+ trpcMutation,+ trpcQuery,+} from './settings-helpers';++interface ProjectDetails {+ name: string;+ domain: string | null;+ cors: string[];+ crossDomain: boolean;+ allowUnsafeRevenueTracking: boolean;+ filters: { type: string; ip?: string; profileId?: string }[];+ deleteAt: string | null;+}++const DOMAIN = 'https://e2e-settings.example.com';+const FRAMEWORK_COUNT = 15;+/** WCAG AA for large text. */+const MIN_READABLE_CONTRAST = 3;+const AI_CLIENTS = [+ 'Claude Desktop',+ 'Claude Code (CLI)',+ 'Cursor',+ 'Windsurf',+ 'VS Code (Copilot)',+ 'Raycast',+];++let api: APIRequestContext;+let project: ThrowawayProject;+let organizationId: string;++const saveButton = (page: Page) => page.getByRole('button', { name: 'Save' });+const settingsPath = (tab: string) =>+ `/${organizationId}/${project.id}/settings/${tab}`;++async function fetchProject(): Promise<ProjectDetails> {+ const result = await trpcQuery<ProjectDetails>(+ api,+ 'project.getProjectWithClients',+ { projectId: project.id }+ );+ if (!result.data) {+ throw new Error(`project not readable: ${result.errorMessage}`);+ }+ return result.data;+}++function resetDetails() {+ return trpcMutation(api, 'project.update', {+ id: project.id,+ name: project.name,+ domain: null,+ cors: [],+ crossDomain: false,+ allowUnsafeRevenueTracking: false,+ });+}++async function addTag(page: Page, placeholder: RegExp, value: string) {+ const input = page.getByPlaceholder(placeholder);+ await input.fill(value);+ await input.press('Enter');+}++test.setTimeout(SLOW_TEST_TIMEOUT_MS);++test.beforeAll(async ({ seed }) => {+ organizationId = seed.organizationId;+ api = await seededApi();+ project = await createThrowawayProject(api, organizationId, 'project');+});++test.afterAll(async () => {+ await scheduleProjectDeletion(api, project.id);+ await api.dispose();+});++test.beforeEach(async ({ context, baseURL }) => {+ await hideFeedbackPrompt(context, baseURL);+});++test.describe('details', () => {+ test.beforeEach(async () => {+ await resetDetails();+ });++ test('name, domain, allowed domains and toggles persist after a reload', async ({+ page,+ }) => {+ const issues = recordPageIssues(page);+ const renamed = `${project.name} renamed`;+ await gotoHydrated(page, settingsPath('details'), saveButton);++ await page.getByLabel('Name').fill(renamed);+ await page.getByRole('switch').click();+ await page.getByPlaceholder('https://example.com').fill(DOMAIN);+ await addTag(page, /Add a domain/, 'allowed.example.com');+ await addTag(page, /Add a domain/, '*');+ await expect(page.getByText('https://allowed.example.com')).toBeVisible();+ await expect(page.getByText('Allow all domains')).toBeVisible();+ await page.getByText('Enable cross domain support').click();+ await page.getByText('Allow "unsafe" revenue tracking').click();+ await saveButton(page).click();+ await expect(page.getByText('Project updated')).toBeVisible(SLOW_ASSERT);++ expect(await fetchProject()).toMatchObject({+ name: renamed,+ domain: DOMAIN,+ cors: ['https://allowed.example.com', '*'],+ crossDomain: true,+ allowUnsafeRevenueTracking: true,+ });++ await page.reload();+ await gotoHydrated(page, settingsPath('details'), saveButton);+ await expect(page.getByLabel('Name')).toHaveValue(renamed);+ await expect(page.getByPlaceholder('https://example.com')).toHaveValue(+ DOMAIN+ );+ await expect(page.getByText('https://allowed.example.com')).toBeVisible();+ await expect(page.getByText('Allow all domains')).toBeVisible();+ await expect(page.getByRole('checkbox').nth(0)).toBeChecked();+ await expect(page.getByRole('checkbox').nth(1)).toBeChecked();+ await expect(page.getByRole('combobox').first()).toContainText(renamed);+ await screenshot(page, 'details-persisted');++ await page.getByRole('switch').click();+ await saveButton(page).click();+ await expect(page.getByText('Project updated')).toBeVisible(SLOW_ASSERT);+ expect(await fetchProject()).toMatchObject({ domain: null, cors: [] });+ issues.expectNone();+ });++ test('a domain without allowed domains, or an invalid domain, is not saved', async ({+ page,+ }) => {+ const updates: string[] = [];+ page.on('request', (request) => {+ if (request.url().includes('project.update')) {+ updates.push(request.url());+ }+ });+ await gotoHydrated(page, settingsPath('details'), saveButton);++ await page.getByRole('switch').click();+ await saveButton(page).click();+ await expect(page.getByPlaceholder('https://example.com')).toHaveClass(+ /border-destructive/+ );+ await expect(page.getByText('Issues')).toBeVisible();+ await screenshot(page, 'details-domain-required');++ await page.getByPlaceholder('https://example.com').fill('not a url');+ await addTag(page, /Add a domain/, 'allowed.example.com');+ await saveButton(page).click();+ await expect(page.getByPlaceholder('https://example.com')).toHaveClass(+ /border-destructive/+ );+ expect(updates).toEqual([]);+ expect((await fetchProject()).domain).toBeNull();+ });++ test('an empty name is rejected with a visible error', async ({ page }) => {+ await gotoHydrated(page, settingsPath('details'), saveButton);++ await page.getByLabel('Name').fill('');+ await saveButton(page).click();+ await screenshot(page, 'details-empty-name');+ expect((await fetchProject()).name).toBe(project.name);+ // Nothing is saved, but nothing tells the user why either.+ await expect(+ page.getByText('Issues').or(page.locator('input.border-destructive'))+ ).toBeVisible({ timeout: 5000 });+ });++ test('the same allowed domain cannot be added twice', async ({ page }) => {+ const issues = recordPageIssues(page);+ await gotoHydrated(page, settingsPath('details'), saveButton);++ await page.getByRole('switch').click();+ await page.getByPlaceholder('https://example.com').fill(DOMAIN);+ await addTag(page, /Add a domain/, 'allowed.example.com');+ await addTag(page, /Add a domain/, 'allowed.example.com');+ await addTag(page, /Add a domain/, '*');+ await addTag(page, /Add a domain/, '*');+ // Leaving the field commits whatever is still typed in it.+ await page.getByLabel('Name').click();+ await screenshot(page, 'details-duplicate-allowed-domains');++ await expect(+ page.locator('[data-tag="https://allowed.example.com"]')+ ).toHaveCount(1);+ await expect(page.locator('[data-tag="*"]')).toHaveCount(1);+ issues.expectNone();+ });+});++test.describe('events', () => {+ test('excluded IPs and profile ids persist after a reload', async ({+ page,+ }) => {+ const issues = recordPageIssues(page);+ await gotoHydrated(page, settingsPath('events'), saveButton);++ await addTag(page, /Exclude IP addresses/, '203.0.113.7');+ await addTag(page, /Exclude Profile IDs/, 'e2e-blocked-profile');+ await page.getByRole('button', { name: 'Add event rule' }).click();+ await expect(page.getByText('Select event name...')).toBeVisible();+ await expect(+ page.getByRole('button', { name: 'Add property filter' })+ ).toBeVisible();+ await screenshot(page, 'events-filters-filled');+ await saveButton(page).click();+ await expect(page.getByText('Project filters updated')).toBeVisible(+ SLOW_ASSERT+ );++ expect((await fetchProject()).filters).toEqual([+ { type: 'ip', ip: '203.0.113.7' },+ { type: 'profile_id', profileId: 'e2e-blocked-profile' },+ ]);++ await gotoHydrated(page, settingsPath('events'), saveButton);+ await expect(page.getByText('203.0.113.7')).toBeVisible();+ await expect(page.getByText('e2e-blocked-profile')).toBeVisible();+ issues.expectNone();+ });+});++test.describe('tracking script', () => {+ test('the snippet carries the selected client id and every framework opens its instructions', async ({+ page,+ }) => {+ const issues = recordPageIssues(page);+ const second = await trpcMutation<{ id: string }>(api, 'client.create', {+ name: 'E2E settings second client',+ projectId: project.id,+ organizationId,+ type: 'write',+ });+ const secondId = second.data?.id ?? '';+ const copyButton = (target: Page) =>+ target.getByRole('button', { name: 'Copy', exact: true });+ await gotoHydrated(page, settingsPath('tracking'), copyButton);++ const snippet = page.locator('pre').first();+ await expect(snippet).toContainText(+ `clientId: '${project.clientId}'`,+ SLOW_ASSERT+ );+ await expect(snippet).toContainText('https://openpanel.dev/op1.js');++ await page+ .getByRole('combobox')+ .filter({ hasText: 'First client' })+ .click();+ await page+ .getByRole('option', { name: 'E2E settings second client' })+ .click();+ await expect(snippet).toContainText(`clientId: '${secondId}'`);+ await screenshot(page, 'tracking-second-client');++ const frameworks = page.locator('button:has(.font-semibold)');+ await expect(frameworks).toHaveCount(FRAMEWORK_COUNT);+ const names = await frameworks.allInnerTexts();+ for (const name of names) {+ await page.getByRole('button', { name, exact: true }).click();+ const dialog = page.getByRole('dialog');+ await expect(dialog.locator('iframe')).toHaveAttribute(+ 'src',+ /^https:\/\//+ );+ await expect(+ dialog.getByRole('link', { name: 'More details' })+ ).toBeVisible();+ await dialog.getByRole('button', { name: 'Close' }).first().click();+ await expect(dialog).toBeHidden();+ }+ // Both have a test of their own below.+ const coveredElsewhere = /DialogTitle|Framing/;+ expect(+ issues.list.filter((issue) => !coveredElsewhere.test(issue))+ ).toEqual([]);+ });++ test('the instructions sheet has an accessible title', async ({ page }) => {+ const issues = recordPageIssues(page);+ const copyButton = (target: Page) =>+ target.getByRole('button', { name: 'Copy', exact: true });+ await gotoHydrated(page, settingsPath('tracking'), copyButton);++ await page.getByRole('button', { name: 'React', exact: true }).click();+ await expect(page.getByRole('dialog').locator('iframe')).toBeVisible();+ await expect(+ page.getByRole('dialog', { name: 'Instructions for React' })+ ).toBeVisible({ timeout: 5000 });+ issues.expectNone();+ });++ test('every framework page is allowed to load inside the instructions sheet', async ({+ page,+ }) => {+ const blockedFrames: string[] = [];+ page.on('console', (message) => {+ if (/Framing|frame-ancestors|X-Frame-Options/.test(message.text())) {+ blockedFrames.push(message.text());+ }+ });+ const copyButton = (target: Page) =>+ target.getByRole('button', { name: 'Copy', exact: true });+ await gotoHydrated(page, settingsPath('tracking'), copyButton);++ // The only framework whose instructions live outside openpanel.dev.+ await page.getByRole('button', { name: 'Laravel', exact: true }).click();+ const frame = page.getByRole('dialog').locator('iframe');+ await expect(frame).toBeVisible();+ await page.waitForLoadState('networkidle');+ await screenshot(page, 'tracking-instructions-laravel');+ expect(blockedFrames).toEqual([]);+ });+});++test.describe('mcp', () => {+ test('the endpoint and client snippets point at the API, and the endpoint answers tools/list', async ({+ page,+ seed,+ }) => {+ const issues = recordPageIssues(page);+ const endpoint = `${apiUrl()}/mcp`;+ const createButton = (target: Page) =>+ target.getByRole('button', { name: 'Create MCP client' });+ await gotoHydrated(page, settingsPath('mcp'), createButton);++ await expect(page.getByText(endpoint, { exact: true })).toBeVisible();+ for (const name of AI_CLIENTS) {+ await page.getByRole('button', { name, exact: true }).click();+ const panel = page.getByRole('region', { name });+ await expect(panel.locator('pre')).toContainText(endpoint);+ await expect(panel.locator('pre')).toContainText('BASE64_TOKEN');+ }+ await screenshot(page, 'mcp');++ const token = Buffer.from(+ `${seed.rootClient.id}:${seed.rootClient.secret}`+ ).toString('base64');+ const viaHeader = await api.post(endpoint, {+ headers: {+ accept: 'application/json, text/event-stream',+ authorization: `Bearer ${token}`,+ },+ data: { jsonrpc: '2.0', id: 1, method: 'tools/list' },+ });+ expect(viaHeader.status()).toBe(200);+ expect((await viaHeader.json()).result.tools.length).toBeGreaterThan(0);++ const noToken = await api.post(endpoint, {+ headers: { accept: 'application/json, text/event-stream' },+ data: { jsonrpc: '2.0', id: 1, method: 'tools/list' },+ });+ expect(noToken.status()).toBe(401);+ issues.expectNone();+ });++ test('"Create MCP client" offers a client type that can use MCP', async ({+ page,+ }) => {+ const createButton = (target: Page) =>+ target.getByRole('button', { name: 'Create MCP client' });+ await gotoHydrated(page, settingsPath('mcp'), createButton);++ await createButton(page).click();+ const dialog = page.getByRole('dialog');+ await screenshot(page, 'mcp-create-client');+ // The page says only read and root clients can authenticate with MCP.+ await expect(dialog.getByRole('combobox')).not.toContainText('Write');+ });+});++test.describe('widgets', () => {+ test('widgets can be enabled, configured and are public while enabled', async ({+ page,+ browser,+ }) => {+ const issues = recordPageIssues(page);+ await sendTrackEvent(+ api,+ { id: project.clientId, secret: project.clientSecret },+ 'screen_view',+ { __path: '/pricing', __referrer: 'https://google.com' }+ );+ const firstSwitch = (target: Page) => target.getByRole('switch').first();+ await gotoHydrated(page, settingsPath('widgets'), firstSwitch);+ await expect(page.getByRole('switch')).toHaveCount(2);+ await expect(page.getByText('Widget URL')).toHaveCount(0);++ await page.getByRole('switch').first().click();+ await expect(page.getByText('Widget enabled')).toBeVisible(SLOW_ASSERT);+ const realtime = await trpcQuery<{ id: string }>(api, 'widget.get', {+ projectId: project.id,+ type: 'realtime',+ });+ const realtimePath = `/widget/realtime?shareId=${realtime.data?.id}`;+ await expect(page.locator('pre').first()).toContainText(realtimePath);++ await page.getByLabel('Show Paths').click();+ await expect(page.getByText('Widget options updated')).toBeVisible(+ SLOW_ASSERT+ );+ await gotoHydrated(page, settingsPath('widgets'), firstSwitch);+ await expect(page.getByLabel('Show Paths')).toBeChecked();+ await expect(page.getByLabel('Show Referrers')).toBeChecked();++ await page.getByRole('switch').last().click();+ await expect(page.getByText('Widget enabled')).toBeVisible(SLOW_ASSERT);+ const counter = await trpcQuery<{ id: string }>(api, 'widget.get', {+ projectId: project.id,+ type: 'counter',+ });+ const counterPath = `/widget/counter?shareId=${counter.data?.id}`;+ const badgePath = `/widget/badge?shareId=${counter.data?.id}`;+ await expect(+ page.getByText('Analytics Badge', { exact: true })+ ).toBeVisible();+ await expect(page.locator('pre').nth(1)).toContainText(counterPath);+ await expect(page.locator('pre').nth(2)).toContainText(badgePath);+ await screenshot(page, 'widgets-enabled');++ const loggedOut = await browser.newContext(LOGGED_OUT);+ const visitor = await loggedOut.newPage();+ await visitor.goto(realtimePath);+ await expect(visitor.getByText('USERS IN LAST 30 MINUTES')).toBeVisible(+ SLOW_ASSERT+ );+ await expect(visitor.getByText('/pricing')).toBeVisible(SLOW_ASSERT);+ await expect(visitor.getByText('Google')).toBeVisible();+ await visitor.screenshot({+ path: 'test-results/settings/shots/widget-public-realtime.png',+ });++ await visitor.goto(counterPath);+ await expect(visitor.getByText('unique visitors').first()).toBeVisible(+ SLOW_ASSERT+ );+ await visitor.goto(badgePath);+ await expect(visitor.getByText('ANALYTICS FROM')).toBeVisible(SLOW_ASSERT);+ await visitor.goto('/widget/counter');+ await expect(+ visitor.getByText('Missing or invalid widget link.')+ ).toBeVisible(SLOW_ASSERT);++ await page.getByRole('switch').first().click();+ await expect(page.getByText('Widget disabled')).toBeVisible(SLOW_ASSERT);+ await visitor.goto(realtimePath);+ await expect(visitor.getByText('Widget not found')).toBeVisible(+ SLOW_ASSERT+ );+ await loggedOut.close();+ issues.expectNone();+ });++ test('the badge is readable with the embed code the settings page hands out', async ({+ browser,+ }) => {+ await trpcMutation(api, 'widget.toggle', {+ projectId: project.id,+ organizationId,+ type: 'counter',+ enabled: true,+ });+ const counter = await trpcQuery<{ id: string }>(api, 'widget.get', {+ projectId: project.id,+ type: 'counter',+ });+ const loggedOut = await browser.newContext(LOGGED_OUT);+ const visitor = await loggedOut.newPage();+ await visitor.goto(`/widget/badge?shareId=${counter.data?.id}`);+ const label = visitor.getByText('OpenPanel', { exact: true });+ await expect(label).toBeVisible(SLOW_ASSERT);+ await visitor.screenshot({+ path: 'test-results/settings/shots/widget-public-badge.png',+ });++ const contrast = await label.evaluate((element) => {+ const transparent = /^rgba\(0, 0, 0, 0\)$|^transparent$/;+ let background = 'transparent';+ for (+ let node: Element | null = element;+ node && transparent.test(background);+ node = node.parentElement+ ) {+ background = getComputedStyle(node).backgroundColor;+ }+ const canvas = document.createElement('canvas');+ canvas.width = 1;+ canvas.height = 1;+ const context = canvas.getContext('2d');+ const luminance = (color: string) => {+ if (!context) {+ return 0;+ }+ // Painting over white resolves any CSS color syntax, and a+ // transparent chain to the browser's default white canvas.+ context.fillStyle = '#fff';+ context.fillRect(0, 0, 1, 1);+ context.fillStyle = color;+ context.fillRect(0, 0, 1, 1);+ const [red = 0, green = 0, blue = 0] = context.getImageData(+ 0,+ 0,+ 1,+ 1+ ).data;+ const channel = (value: number) => {+ const unit = value / 255;+ return unit <= 0.039_28+ ? unit / 12.92+ : ((unit + 0.055) / 1.055) ** 2.4;+ };+ return (+ 0.2126 * channel(red) ++ 0.7152 * channel(green) ++ 0.0722 * channel(blue)+ );+ };+ const text = luminance(getComputedStyle(element).color);+ const surface = luminance(background);+ return (+ (Math.max(text, surface) + 0.05) / (Math.min(text, surface) + 0.05)+ );+ });+ await loggedOut.close();+ // The default link sets no `color`, so the white label sits on the page.+ expect(contrast).toBeGreaterThan(MIN_READABLE_CONTRAST);+ });+});++test.describe('imports', () => {+ const importButtons = (page: Page) =>+ page.getByRole('button', { name: 'Import Data' });++ const PROVIDERS = ['Umami', 'Mixpanel', 'Amplitude'] as const;++ async function openImport(page: Page, provider: (typeof PROVIDERS)[number]) {+ await importButtons(page).nth(PROVIDERS.indexOf(provider)).click();+ const dialog = page.getByRole('dialog');+ await expect(dialog.getByText(`Import from ${provider}`)).toBeVisible();+ return dialog;+ }++ test('every provider modal validates before starting an import', async ({+ page,+ }) => {+ const issues = recordPageIssues(page);+ const created: string[] = [];+ page.on('request', (request) => {+ if (request.url().includes('import.create')) {+ created.push(request.url());+ }+ });+ await gotoHydrated(page, settingsPath('imports'), importButtons);+ await expect(importButtons(page)).toHaveCount(3);+ await expect(page.getByText('No imports yet')).toBeVisible(SLOW_ASSERT);++ const umami = await openImport(page, 'Umami');+ await umami.getByRole('button', { name: 'Start Import' }).click();+ await expect(umami.getByText('Issues')).toHaveCount(1);+ await umami.getByLabel('File URL').fill('ftp://example.com/export.csv');+ await umami.getByRole('button', { name: 'Start Import' }).click();+ await expect(umami.getByText('Issues')).toHaveCount(1);+ await umami.getByRole('button', { name: 'Cancel' }).click();++ const mixpanel = await openImport(page, 'Mixpanel');+ await mixpanel.getByRole('button', { name: 'Start Import' }).click();+ await expect(mixpanel.getByText('Issues')).toHaveCount(3);+ await screenshot(page, 'imports-mixpanel-validation');+ await mixpanel.getByRole('button', { name: 'Cancel' }).click();++ const amplitude = await openImport(page, 'Amplitude');+ await amplitude.getByRole('button', { name: 'Start Import' }).click();+ await expect(amplitude.getByText('Issues')).toHaveCount(2);+ await amplitude.getByRole('button', { name: 'Cancel' }).click();++ expect(created).toEqual([]);+ issues.expectNone();+ });++ test('a missing date range is reported in the Mixpanel modal', async ({+ page,+ }) => {+ await gotoHydrated(page, settingsPath('imports'), importButtons);+ const mixpanel = await openImport(page, 'Mixpanel');+ await mixpanel.getByLabel('Service Account').fill('e2e.service-account');+ await mixpanel.getByLabel('Service Secret').fill('not-a-real-secret');+ await mixpanel.getByLabel('Project ID').fill('123456');+ await mixpanel.getByRole('button', { name: 'Start Import' }).click();+ await screenshot(page, 'imports-mixpanel-no-date-range');+ // The submit is blocked by the empty range, so the modal has to say so.+ await expect(mixpanel.getByText('Issues')).toBeVisible({ timeout: 5000 });+ });++ test('an incomplete project mapping is reported in the Umami modal', async ({+ page,+ }) => {+ await gotoHydrated(page, settingsPath('imports'), importButtons);+ const umami = await openImport(page, 'Umami');+ await umami.getByLabel('File URL').fill('https://example.com/export.csv');+ await umami.getByRole('button', { name: 'Add Mapping' }).click();+ await umami.getByRole('button', { name: 'Start Import' }).click();+ await screenshot(page, 'imports-umami-empty-mapping');+ await expect(umami).toBeVisible();+ await expect(+ umami.getByText('Issues').or(umami.locator('.border-destructive'))+ ).toBeVisible({ timeout: 5000 });+ });++ test('a file import that cannot be fetched fails, can be retried and deleted', async ({+ page,+ }) => {+ const issues = recordPageIssues(page);+ await gotoHydrated(page, settingsPath('imports'), importButtons);+ const umami = await openImport(page, 'Umami');+ // Loopback is refused by the worker, so this never leaves the machine.+ await umami.getByLabel('File URL').fill('http://127.0.0.1:9/umami.csv');+ await umami.getByRole('button', { name: 'Start Import' }).click();+ await expect(page.getByText('Import started')).toBeVisible(SLOW_ASSERT);+ await expect(umami).toBeHidden();++ const row = page.getByRole('row', { name: /umami/i });+ await expect(row.getByText('failed', { exact: true })).toBeVisible(+ SLOW_ASSERT+ );+ await screenshot(page, 'imports-failed');+ await row.getByRole('button', { name: 'Retry' }).click();+ await expect(page.getByText('Import retried')).toBeVisible(SLOW_ASSERT);+ await expect(row.getByText('failed', { exact: true })).toBeVisible(+ SLOW_ASSERT+ );++ await row.getByRole('button', { name: 'Delete' }).click();+ await expect(page.getByText('Import deleted')).toBeVisible(SLOW_ASSERT);+ await expect(page.getByText('No imports yet')).toBeVisible(SLOW_ASSERT);+ issues.expectNone();+ });+});++test.describe('danger zone', () => {+ test('a project is scheduled for deletion, can be restored, and is flagged or gone on the projects page', async ({+ page,+ }) => {+ const issues = recordPageIssues(page);+ const doomed = await createThrowawayProject(api, organizationId, 'danger');+ const readDeleteAt = async () =>+ (+ await trpcQuery<ProjectDetails>(api, 'project.getProjectWithClients', {+ projectId: doomed.id,+ })+ ).data?.deleteAt ?? null;+ const deleteButton = (target: Page) =>+ target.getByRole('button', { name: 'Delete Project' });+ await gotoHydrated(+ page,+ `/${organizationId}/${doomed.id}/settings/details`,+ deleteButton+ );++ await deleteButton(page).click();+ const dialog = page.getByRole('dialog');+ await expect(+ dialog.getByText('Are you sure you want to delete this project?')+ ).toBeVisible();+ await dialog.getByRole('button', { name: 'Cancel' }).click();+ expect(await readDeleteAt()).toBeNull();++ await deleteButton(page).click();+ await dialog.getByRole('button', { name: 'Yes' }).click();+ await expect(page.getByText('Project scheduled for deletion')).toBeVisible(+ SLOW_ASSERT+ );+ await expect(deleteButton(page)).toBeDisabled();+ await screenshot(page, 'danger-scheduled');+ const deleteAt = await readDeleteAt();+ const hoursUntilDeletion =+ (new Date(deleteAt ?? 0).getTime() - Date.now()) / 3_600_000;+ expect(hoursUntilDeletion).toBeGreaterThan(23);+ expect(hoursUntilDeletion).toBeLessThan(25);++ await page.getByRole('button', { name: 'Cancel deletion' }).click();+ await expect(page.getByText('Project deletion cancelled')).toBeVisible(+ SLOW_ASSERT+ );+ await expect(page.getByText('Project scheduled for deletion')).toBeHidden();+ await expect(deleteButton(page)).toBeEnabled();+ expect(await readDeleteAt()).toBeNull();++ await deleteButton(page).click();+ await dialog.getByRole('button', { name: 'Yes' }).click();+ await expect(page.getByText('Project scheduled for deletion')).toBeVisible(+ SLOW_ASSERT+ );+ issues.expectNone();++ const search = (target: Page) => target.getByPlaceholder('Search');+ await gotoHydrated(page, `/${organizationId}`, search);+ await search(page).fill(doomed.name);+ const card = page.locator('a').filter({ hasText: doomed.name });+ const isStillListed = await card+ .first()+ .waitFor({ timeout: 60_000 })+ .then(() => true)+ .catch(() => false);+ await screenshot(page, 'danger-projects-page');+ if (isStillListed) {+ // Listed for another 24 hours: it has to say it is on its way out.+ await expect(card.first()).toContainText(/delet/i, { timeout: 5000 });+ }+ });+});++test.describe('mobile viewport', () => {+ const MOBILE = { width: 390, height: 844 };+ const PAGES = [+ 'settings/details',+ 'settings/events',+ 'settings/clients',+ 'settings/tracking',+ 'settings/mcp',+ 'settings/widgets',+ 'settings/imports',+ 'notifications/rules',+ 'integrations/available',+ ];++ for (const path of PAGES) {+ test(`${path} fits a 390px wide screen`, async ({ page }) => {+ await page.setViewportSize(MOBILE);+ await page.goto(`/${organizationId}/${project.id}/${path}`);+ await page.waitForLoadState('networkidle');+ await expect(page.getByRole('tab').first()).toBeVisible(SLOW_ASSERT);+ await screenshot(page, `mobile-${path.replace('/', '-')}`);++ const overflow = await page.evaluate(+ () => document.documentElement.scrollWidth - window.innerWidth+ );+ expect(overflow, 'horizontal page overflow in px').toBeLessThanOrEqual(0);+ });+ }+});diff --git a/apps/start/e2e/sweep-a11y.spec.ts b/apps/start/e2e/sweep-a11y.spec.tsnew file mode 100644index 000000000..b14ff2102--- /dev/null+++ b/apps/start/e2e/sweep-a11y.spec.ts@@ -0,0 +1,275 @@+import type { Page } from '@playwright/test';+import { expect, test } from './fixtures';+import { settle, writeJson } from './sweep-helpers';++// A click that cannot land should fail the step, not sit until the sweep-sized test timeout.+test.use({ actionTimeout: 20_000 });++const BASE = '/acme/acme-shop';+const MAIN_PAGES = [+ '/acme',+ '/acme/settings',+ BASE,+ `${BASE}/pages`,+ `${BASE}/events`,+ `${BASE}/sessions`,+ `${BASE}/profiles`,+ `${BASE}/settings/details`,+ `${BASE}/settings/clients`,+ `${BASE}/references`,+ `${BASE}/notifications/rules`,+];+const TAB_PRESSES = 45;+const MODAL_TAB_PRESSES = 25;+const MAX_MENU_ITEMS = 14;++/** Visible form controls and buttons/links that expose no accessible name. */+function findUnnamedControls(page: Page) {+ return page.evaluate(() => {+ const isVisible = (element: Element) => {+ const rect = element.getBoundingClientRect();+ const style = getComputedStyle(element);+ return rect.width > 0 && rect.height > 0 && style.visibility !== 'hidden';+ };+ const labelledBy = (element: Element) =>+ (element.getAttribute('aria-labelledby') ?? '')+ .split(' ')+ .map((id) => document.getElementById(id)?.textContent ?? '')+ .join('')+ .trim();+ const describe = (element: Element) =>+ element.outerHTML.replace(/\s+/g, ' ').slice(0, 160);++ const unlabeledFields: string[] = [];+ for (const field of document.querySelectorAll<HTMLInputElement>(+ 'input:not([type=hidden]), select, textarea, [role=combobox], [role=switch], [role=checkbox]'+ )) {+ if (!isVisible(field)) {+ continue;+ }+ const hasLabel =+ !!field.getAttribute('aria-label') ||+ !!labelledBy(field) ||+ !!field.closest('label') ||+ (!!field.id &&+ !!document.querySelector(`label[for="${CSS.escape(field.id)}"]`)) ||+ !!field.getAttribute('title') ||+ !!(field.textContent ?? '').trim();+ if (!hasLabel) {+ unlabeledFields.push(+ `${field.getAttribute('placeholder') ? '(placeholder only) ' : ''}${describe(field)}`+ );+ }+ }++ const unnamedButtons: string[] = [];+ for (const control of document.querySelectorAll(+ 'button, a[href], [role=button]'+ )) {+ if (!isVisible(control)) {+ continue;+ }+ const name =+ control.getAttribute('aria-label') ||+ labelledBy(control) ||+ control.getAttribute('title') ||+ (control.textContent ?? '').trim() ||+ control.querySelector('img[alt]')?.getAttribute('alt') ||+ control.querySelector('svg title')?.textContent;+ if (!name) {+ unnamedButtons.push(describe(control));+ }+ }+ return { unlabeledFields, unnamedButtons };+ });+}++const describeFocus = (page: Page) =>+ page.evaluate(() => {+ const element = document.activeElement;+ if (!element || element === document.body) {+ return 'body';+ }+ const name =+ element.getAttribute('aria-label') ||+ (element.textContent ?? '').trim().slice(0, 30) ||+ element.getAttribute('placeholder') ||+ element.getAttribute('name') ||+ '';+ const region = element.closest('div.fixed.w-72')+ ? 'sidebar'+ : element.closest('[role=dialog]')+ ? 'dialog'+ : 'main';+ const style = getComputedStyle(element);+ const hasIndicator =+ style.outlineStyle !== 'none' || style.boxShadow !== 'none';+ return `${region}:${element.tagName.toLowerCase()}[${name}]${hasIndicator ? '' : ' (no focus ring)'}`;+ });++test.describe('keyboard and accessibility basics', () => {+ test.setTimeout(15 * 60 * 1000);++ test('main pages: controls have names and Tab reaches the main content', async ({+ page,+ }) => {+ const report: Record<string, unknown>[] = [];+ for (const url of MAIN_PAGES) {+ await page.goto(url);+ await settle(page);+ const unnamed = await findUnnamedControls(page);+ const tabStops: string[] = [];+ for (let press = 0; press < TAB_PRESSES; press++) {+ await page.keyboard.press('Tab');+ tabStops.push(await describeFocus(page));+ }+ report.push({+ url,+ ...unnamed,+ firstMainStop:+ tabStops.findIndex((stop) => stop.startsWith('main:')) + 1,+ tabStops,+ });+ }+ writeJson('a11y-pages.json', report);+ for (const entry of report) {+ expect(+ entry.firstMainStop,+ `${entry.url}: Tab never reaches the main content`+ ).toBeGreaterThan(0);+ expect(+ (entry.tabStops as string[]).filter((stop) => stop.includes('[]')),+ `${entry.url}: focusable controls without an accessible name`+ ).toEqual([]);+ }+ });++ test('project selector works with the keyboard', async ({ page }) => {+ await page.goto(BASE);+ await settle(page);+ const selector = page.locator('div.fixed.w-72').getByRole('combobox');+ await selector.focus();+ await page.keyboard.press('Enter');+ const menu = page.getByRole('menu');+ await expect(menu).toBeVisible();+ await page.keyboard.press('ArrowDown');+ const focusedItem = await describeFocus(page);+ await page.keyboard.press('Escape');+ await expect(menu).toBeHidden();+ await expect(selector).toBeFocused();++ await page.keyboard.press('Enter');+ await expect(menu).toBeVisible();+ const visited: string[] = [];+ for (let press = 0; press < MAX_MENU_ITEMS; press++) {+ await page.keyboard.press('ArrowDown');+ const item = await describeFocus(page);+ visited.push(item);+ if (item.includes('[Acme Web]')) {+ break;+ }+ }+ await page.keyboard.press('Enter');+ writeJson('a11y-project-selector.json', { focusedItem, visited });+ await expect(page).toHaveURL(/\/acme\/acme-web/);+ });++ test('create-project modal traps focus, closes on Escape and returns focus', async ({+ page,+ }) => {+ await page.goto('/acme');+ await settle(page);+ const selector = page.locator('div.fixed.w-72').getByRole('combobox');+ await selector.click();+ await page.getByRole('menuitem', { name: 'Create new project' }).click();+ const dialog = page.getByRole('dialog');+ await expect(dialog).toBeVisible();+ await page.screenshot({+ path: 'test-results/sweep/shots/a11y/create-project-modal.png',+ });++ const unnamed = await findUnnamedControls(page);+ const stops: string[] = [];+ for (let press = 0; press < MODAL_TAB_PRESSES; press++) {+ await page.keyboard.press('Tab');+ stops.push(await describeFocus(page));+ }+ await page.keyboard.press('Escape');+ await expect(dialog).toBeHidden();+ const focusAfterClose = await describeFocus(page);+ writeJson('a11y-modal.json', { unnamed, stops, focusAfterClose });++ expect(stops.filter((stop) => !stop.startsWith('dialog:'))).toEqual([]);+ });++ test('overview pickers open, move and close with the keyboard', async ({+ page,+ }) => {+ await page.goto(BASE);+ await settle(page);+ const outcomes: Record<string, unknown> = {};+ for (const name of [+ /Last 7 days|Last 30 days|Today/,+ /^Day$|^Hour$/,+ /Filters/,+ ]) {+ const trigger = page.getByRole('button', { name }).first();+ await trigger.focus();+ await page.keyboard.press('Enter');+ await page.waitForTimeout(600);+ const opened = await page+ .locator(+ '[role=menu], [role=dialog], [role=listbox], [data-radix-popper-content-wrapper]'+ )+ .count();+ await page.keyboard.press('ArrowDown');+ const focusInside = await describeFocus(page);+ await page.screenshot({+ path: `test-results/sweep/shots/a11y/overview-picker-${String(name).replace(/\W+/g, '-')}.png`,+ });+ await page.keyboard.press('Escape');+ await page.waitForTimeout(600);+ const stillOpen = await page+ .locator(+ '[role=menu], [role=dialog], [role=listbox], [data-radix-popper-content-wrapper]'+ )+ .count();+ outcomes[String(name)] = {+ opened,+ focusInside,+ stillOpen,+ focusAfterClose: await describeFocus(page),+ };+ }+ writeJson('a11y-overview-pickers.json', outcomes);+ for (const [name, outcome] of Object.entries(outcomes)) {+ const { opened, stillOpen } = outcome as {+ opened: number;+ stillOpen: number;+ };+ expect(opened, `${name} opens with Enter`).toBeGreaterThan(0);+ expect(stillOpen, `${name} closes with Escape`).toBe(0);+ }+ });++ test('login form fields are labelled', async ({ browser, baseURL }) => {+ const context = await browser.newContext({+ ignoreHTTPSErrors: true,+ baseURL,+ storageState: { cookies: [], origins: [] },+ });+ const page = await context.newPage();+ await page.goto('/login');+ await settle(page);+ await page.screenshot({ path: 'test-results/sweep/shots/a11y/login.png' });+ const unnamed = await findUnnamedControls(page);+ const stops: string[] = [];+ for (let press = 0; press < 12; press++) {+ await page.keyboard.press('Tab');+ stops.push(await describeFocus(page));+ }+ writeJson('a11y-login.json', { unnamed, stops });+ await context.close();+ expect(unnamed.unlabeledFields).toEqual([]);+ });+});diff --git a/apps/start/e2e/sweep-client-nav.spec.ts b/apps/start/e2e/sweep-client-nav.spec.tsnew file mode 100644index 000000000..28987326a--- /dev/null+++ b/apps/start/e2e/sweep-client-nav.spec.ts@@ -0,0 +1,245 @@+import type { Page } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ attachRecorder,+ inspectPage,+ ORGANIZATION_ID,+ type Recorder,+ settle,+ type Visit,+ writeJson,+} from './sweep-helpers';++// A click that cannot land should fail the step, not sit until the sweep-sized test timeout.+test.use({ actionTimeout: 20_000 });++const NAV_TIMEOUT_MS = 15 * 60 * 1000;+const NAVIGATION_START_MS = 700;+const START_PROJECT = 'acme-shop';+const OTHER_PROJECT = { id: 'acme-web', name: 'Acme Web' };++const SIDEBAR_LINKS: { label: string; path: string }[] = [+ { label: 'Dashboards', path: '/dashboards' },+ { label: 'Insights', path: '/insights' },+ { label: 'Pages', path: '/pages' },+ { label: 'SEO', path: '/seo' },+ { label: 'Realtime', path: '/realtime' },+ { label: 'Events', path: '/events' },+ { label: 'Sessions', path: '/sessions' },+ { label: 'Profiles', path: '/profiles' },+ { label: 'Groups', path: '/groups' },+ { label: 'Cohorts', path: '/cohorts' },+ { label: 'Settings', path: '/settings' },+ { label: 'References', path: '/references' },+ { label: 'Notifications', path: '/notifications' },+ { label: 'Integrations', path: '/integrations' },+ { label: 'Overview', path: '' },+];++declare global {+ interface Window {+ sweepMarker?: boolean;+ }+}++const sidebar = (page: Page) => page.locator('div.fixed.w-72');++async function record(+ page: Page,+ recorder: Recorder,+ route: string,+ project: string,+ startedAt: number+): Promise<Visit> {+ // Queries start a tick after the click; settling at once would inspect the previous page.+ await page.waitForTimeout(NAVIGATION_START_MS);+ const wentIdle = await settle(page);+ const url = new URL(page.url());+ return {+ route,+ project,+ url: url.pathname + url.search,+ wentIdle,+ loadMs: Date.now() - startedAt,+ state: await inspectPage(page),+ ...recorder.snapshot(),+ };+}++test.describe('route sweep (client-side navigation)', () => {+ test.setTimeout(NAV_TIMEOUT_MS);++ test('sidebar links, tabs and detail rows navigate without a reload or crash', async ({+ page,+ }) => {+ const recorder = attachRecorder(page);+ const base = `/${ORGANIZATION_ID}/${START_PROJECT}`;+ await page.goto(base);+ await settle(page);+ await page.evaluate(() => {+ window.sweepMarker = true;+ });++ const results: Visit[] = [];+ for (const link of SIDEBAR_LINKS) {+ recorder.reset();+ const startedAt = Date.now();+ await sidebar(page)+ .getByRole('link', { name: link.label, exact: true })+ .click();+ await expect(page).toHaveURL(+ new RegExp(`${base}${link.path}(/[a-z-]*)?(\\?.*)?$`)+ );+ results.push(+ await record(+ page,+ recorder,+ `sidebar:${link.label}`,+ START_PROJECT,+ startedAt+ )+ );++ const tabs = page.getByRole('tab');+ const tabCount = await tabs.count();+ for (let index = 0; index < tabCount; index++) {+ const tab = tabs.nth(index);+ const tabName = (await tab.innerText()).trim();+ recorder.reset();+ const tabStartedAt = Date.now();+ await tab.click();+ results.push(+ await record(+ page,+ recorder,+ `sidebar:${link.label} > tab:${tabName}`,+ START_PROJECT,+ tabStartedAt+ )+ );+ }+ }++ for (const kind of ['sessions', 'profiles']) {+ recorder.reset();+ await sidebar(page)+ .getByRole('link', {+ name: kind === 'sessions' ? 'Sessions' : 'Profiles',+ exact: true,+ })+ .click();+ const firstRow = page.locator(`a[href^="${base}/${kind}/"]`).first();+ await expect(firstRow).toBeVisible();+ const startedAt = Date.now();+ await firstRow.click();+ await expect(page).toHaveURL(new RegExp(`${base}/${kind}/.+`));+ results.push(+ await record(+ page,+ recorder,+ `row:${kind} detail`,+ START_PROJECT,+ startedAt+ )+ );+ recorder.reset();+ const backStartedAt = Date.now();+ await page.goBack();+ results.push(+ await record(+ page,+ recorder,+ `back from ${kind} detail`,+ START_PROJECT,+ backStartedAt+ )+ );+ }++ const survivedWithoutReload = await page.evaluate(+ () => window.sweepMarker === true+ );+ writeJson('client-nav.json', { survivedWithoutReload, results });++ expect(survivedWithoutReload).toBe(true);+ expect(+ results.filter(+ (result) => result.pageErrors?.length || result.state?.isBlank+ )+ ).toEqual([]);+ });++ test('switching project in the selector swaps every query to the new project', async ({+ page,+ }) => {+ const recorder = attachRecorder(page);+ const base = `/${ORGANIZATION_ID}/${START_PROJECT}`;+ await page.goto(`${base}/sessions`);+ await settle(page);++ const requestedProjects: string[] = [];+ page.on('request', (request) => {+ const url = request.url();+ if (!url.includes('/trpc/')) {+ return;+ }+ const decoded = decodeURIComponent(url) + (request.postData() ?? '');+ for (const match of decoded.matchAll(/"projectId":"([^"]+)"/g)) {+ requestedProjects.push(match[1]);+ }+ });++ recorder.reset();+ await sidebar(page).getByRole('combobox').click();+ await page.getByRole('menuitem', { name: OTHER_PROJECT.name }).click();+ await expect(page).toHaveURL(+ new RegExp(`/${ORGANIZATION_ID}/${OTHER_PROJECT.id}`)+ );+ const idle = await settle(page);+ const state = await inspectPage(page);+ await page.screenshot({+ path: 'test-results/sweep/shots/client-nav/after-project-switch.png',+ fullPage: true,+ });++ writeJson('client-nav-project-switch.json', {+ url: page.url(),+ idle,+ state,+ requestedProjects: [...new Set(requestedProjects)],+ ...recorder.snapshot(),+ });++ await expect(sidebar(page).getByRole('combobox')).toContainText(+ OTHER_PROJECT.name+ );+ expect(requestedProjects).toContain(OTHER_PROJECT.id);+ expect(recorder.pageErrors).toEqual([]);++ // Stale data check: sessions list after the switch must link to the new project only.+ await sidebar(page)+ .getByRole('link', { name: 'Sessions', exact: true })+ .click();+ await expect(page.locator('a[href*="/sessions/"]').first()).toBeVisible();+ const hrefs = await page+ .locator('a[href*="/sessions/"]')+ .evaluateAll((links) =>+ links.map((link) => link.getAttribute('href') ?? '')+ );+ expect(hrefs.length).toBeGreaterThan(0);+ expect(+ hrefs.filter((href) => !href.includes(`/${OTHER_PROJECT.id}/`))+ ).toEqual([]);++ await sidebar(page)+ .getByRole('link', { name: 'Back to workspace', exact: true })+ .click();+ await expect(page).toHaveURL(new RegExp(`/${ORGANIZATION_ID}/?$`));+ await page.goBack();+ await expect(page).toHaveURL(+ new RegExp(`/${ORGANIZATION_ID}/${OTHER_PROJECT.id}/sessions`)+ );+ await page.goForward();+ await expect(page).toHaveURL(new RegExp(`/${ORGANIZATION_ID}/?$`));+ });+});diff --git a/apps/start/e2e/sweep-failures.spec.ts b/apps/start/e2e/sweep-failures.spec.tsnew file mode 100644index 000000000..d52c4e789--- /dev/null+++ b/apps/start/e2e/sweep-failures.spec.ts@@ -0,0 +1,241 @@+import { resolve } from 'node:path';+import type { Page, Route } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ attachRecorder,+ countSkeletons,+ inspectPage,+ OUTPUT_DIR,+ settle,+ writeJson,+} from './sweep-helpers';++// A click that cannot land should fail the step, not sit until the sweep-sized test timeout.+test.use({ actionTimeout: 20_000 });++const BASE = '/acme/acme-shop';+const SHOTS = resolve(OUTPUT_DIR, 'shots', 'failures');+// Three retries with exponential backoff (1s + 2s + 4s) before a query settles as failed.+const RETRIES_EXHAUSTED_MS = 12_000;+const HANG_OBSERVATION_MS = 15_000;++const sidebar = (page: Page) => page.locator('div.fixed.w-72');+const sidebarLink = (page: Page, name: string) =>+ sidebar(page).getByRole('link', { name, exact: true });++const failWith500 = (route: Route) =>+ route.fulfill({+ status: 500,+ contentType: 'application/json',+ body: JSON.stringify({+ error: {+ json: {+ message: 'E2E injected failure',+ code: -32_603,+ data: { code: 'INTERNAL_SERVER_ERROR', httpStatus: 500 },+ },+ },+ }),+ });++async function snapshot(page: Page, name: string) {+ await page.screenshot({ path: resolve(SHOTS, `${name}.png`) });+ const state = await inspectPage(page);+ return {+ name,+ url: page.url(),+ skeletons: await countSkeletons(page),+ textLength: state.textLength,+ suspicious: state.suspicious,+ text: await page.evaluate(() =>+ (document.querySelector('main')?.innerText ?? document.body.innerText)+ .replace(/\s+/g, ' ')+ .slice(0, 500)+ ),+ };+}++test.describe('failed and slow api calls', () => {+ test.setTimeout(8 * 60 * 1000);++ test('overview.stats answering 500 shows an error and recovers', async ({+ page,+ }) => {+ const recorder = attachRecorder(page);+ await page.goto(`${BASE}/sessions`);+ await settle(page);++ let calls = 0;+ await page.route('**/trpc/overview.stats*', (route) => {+ calls++;+ return failWith500(route);+ });+ await sidebarLink(page, 'Overview').click();+ await expect(page).toHaveURL(new RegExp(`${BASE}/?$`));+ await page.waitForTimeout(RETRIES_EXHAUSTED_MS);+ const failed = await snapshot(page, 'overview-stats-500');++ await page.unroute('**/trpc/overview.stats*');+ await sidebarLink(page, 'Sessions').click();+ await settle(page);+ await sidebarLink(page, 'Overview').click();+ await settle(page);+ const recovered = await snapshot(page, 'overview-stats-recovered');++ writeJson('failure-overview-stats-500.json', {+ calls,+ failed,+ recovered,+ ...recorder.snapshot(),+ });++ expect(recorder.pageErrors).toEqual([]);+ await expect(sidebarLink(page, 'Overview')).toBeVisible();+ expect(+ failed.skeletons,+ 'stat cards must leave the loading state once retries are exhausted'+ ).toBe(0);+ expect(+ failed.text,+ 'a failed stats call must not be presented as zero visitors'+ ).not.toMatch(/UNIQUE VISITORS 0 /i);+ await expect(+ page.getByRole('button', { name: /^Unique Visitors\s*[\d.,]+/ })+ ).toBeVisible();+ });++ test('overview.stats never answering leaves the rest of the page usable', async ({+ page,+ }) => {+ const recorder = attachRecorder(page);+ await page.goto(`${BASE}/sessions`);+ await settle(page);++ await page.route(+ '**/trpc/overview.stats*',+ () => new Promise(() => undefined)+ );+ await sidebarLink(page, 'Overview').click();+ await page.waitForTimeout(HANG_OBSERVATION_MS);+ const hanging = await snapshot(page, 'overview-stats-hanging');++ await sidebarLink(page, 'Sessions').click();+ await expect(page).toHaveURL(/\/sessions/);+ await settle(page);+ const afterLeaving = await snapshot(page, 'overview-stats-hanging-left');+ writeJson('failure-overview-stats-hang.json', {+ hanging,+ afterLeaving,+ ...recorder.snapshot(),+ });++ expect(recorder.pageErrors).toEqual([]);+ expect(hanging.textLength).toBeGreaterThan(200);+ await expect(+ page.locator(`a[href^="${BASE}/sessions/"]`).first()+ ).toBeVisible();+ });++ test('a failing session list shows an error inside the app shell', async ({+ page,+ }) => {+ const recorder = attachRecorder(page);+ await page.goto(BASE);+ await settle(page);++ await page.route('**/trpc/session.list*', failWith500);+ await sidebarLink(page, 'Sessions').click();+ await page.waitForTimeout(RETRIES_EXHAUSTED_MS);+ const failed = await snapshot(page, 'session-list-500');+ writeJson('failure-session-list.json', { failed, ...recorder.snapshot() });++ await expect(+ sidebarLink(page, 'Sessions'),+ 'the sidebar survives a failed list call'+ ).toBeVisible();+ expect(failed.text).not.toMatch(/Cannot read properties/);+ expect(failed.text).toMatch(/error|failed|wrong|try again/i);++ await page.unroute('**/trpc/session.list*');+ await sidebarLink(page, 'Overview').click();+ await sidebarLink(page, 'Sessions').click();+ await expect(+ page.locator(`a[href^="${BASE}/sessions/"]`).first()+ ).toBeVisible();+ });++ test('a profile list that times out shows an error instead of an empty list', async ({+ page,+ }) => {+ const recorder = attachRecorder(page);+ await page.goto(BASE);+ await settle(page);++ await page.route('**/trpc/profile.list*', (route) =>+ route.abort('timedout')+ );+ await sidebarLink(page, 'Profiles').click();+ await page.waitForTimeout(RETRIES_EXHAUSTED_MS);+ const failed = await snapshot(page, 'profile-list-timeout');+ writeJson('failure-profile-list.json', { failed, ...recorder.snapshot() });++ await expect(sidebarLink(page, 'Profiles')).toBeVisible();+ expect(recorder.pageErrors).toEqual([]);+ expect(+ failed.skeletons,+ 'profile list still loading after the call failed'+ ).toBe(0);+ expect(failed.text).toMatch(/error|failed|wrong|try again/i);++ await page.unroute('**/trpc/profile.list*');+ await sidebarLink(page, 'Overview').click();+ await sidebarLink(page, 'Profiles').click();+ await expect(+ page.locator(`a[href^="${BASE}/profiles/"]`).first()+ ).toBeVisible();+ });++ test('a failing mutation reports the error and keeps the form usable', async ({+ page,+ }) => {+ const recorder = attachRecorder(page);+ await page.goto(`${BASE}/settings/details`);+ await settle(page);++ // Every tRPC POST is answered locally, so nothing reaches the API and the seeded project is untouched.+ let intercepted = 0;+ await page.route('**/trpc/**', (route) => {+ if (route.request().method() !== 'POST') {+ return route.continue();+ }+ if (route.request().url().includes('auth.extendSession')) {+ return route.continue();+ }+ intercepted++;+ return failWith500(route);+ });++ const name = page.getByLabel('Name', { exact: true });+ const original = await name.inputValue();+ await name.fill(`${original} x`);+ await page.getByRole('button', { name: /save/i }).first().click();+ await page.waitForTimeout(2500);+ const afterSave = await snapshot(page, 'project-update-500');+ const toasts = await page.locator('[data-sonner-toast]').allInnerTexts();+ writeJson('failure-mutation.json', {+ intercepted,+ original,+ toasts,+ afterSave,+ ...recorder.snapshot(),+ });++ expect(intercepted).toBeGreaterThan(0);+ expect(recorder.pageErrors).toEqual([]);+ expect(toasts.join(' ')).toMatch(/E2E injected failure|error|wrong/i);+ await expect(name).toBeEditable();+ await expect(+ page.getByRole('button', { name: /save/i }).first()+ ).toBeEnabled();+ });+});diff --git a/apps/start/e2e/sweep-helpers.ts b/apps/start/e2e/sweep-helpers.tsnew file mode 100644index 000000000..71528d4bb--- /dev/null+++ b/apps/start/e2e/sweep-helpers.ts@@ -0,0 +1,680 @@+import { mkdirSync, writeFileSync } from 'node:fs';+import { dirname, resolve } from 'node:path';+import type { APIRequestContext, Page, Response } from '@playwright/test';++export const ORGANIZATION_ID = 'acme';+export const API_URL =+ process.env.API_URL ?? 'https://api.main.local.openpanel.cc';+export const OUTPUT_DIR = resolve(+ import.meta.dirname,+ '..',+ 'test-results',+ 'sweep'+);+export const SLOW_LOAD_MS = 5000;+const NETWORK_IDLE_TIMEOUT_MS = 12_000;+const BLANK_PAGE_MAX_CHARS = 20;+const SNIPPET_RADIUS = 40;++type IdKind =+ | 'profile'+ | 'session'+ | 'group'+ | 'cohort'+ | 'dashboard'+ | 'report';++export type ProjectIds = Record<IdKind, string | null>;++export interface RouteSpec {+ /** Route file pattern, used as the row label. */+ route: string;+ /** Path below `/$organizationId/$projectId`, or an absolute path when `scope` is not project. */+ path: (ids: ProjectIds) => string | null;+ scope: 'project' | 'organization' | 'global';+}++const fixed = (path: string) => () => path;+const withId = (kind: IdKind, suffix = '') => {+ const plural = `${kind}s`;+ return (ids: ProjectIds) =>+ ids[kind] ? `/${plural}/${encodeURIComponent(ids[kind])}${suffix}` : null;+};++export const PROJECT_ROUTES: RouteSpec[] = [+ { route: '$projectId/ (overview)', path: fixed(''), scope: 'project' },+ { route: 'pages', path: fixed('/pages'), scope: 'project' },+ { route: 'realtime', path: fixed('/realtime'), scope: 'project' },+ { route: 'insights', path: fixed('/insights'), scope: 'project' },+ { route: 'seo', path: fixed('/seo'), scope: 'project' },+ { route: 'references', path: fixed('/references'), scope: 'project' },+ { route: 'dashboards', path: fixed('/dashboards'), scope: 'project' },+ {+ route: 'dashboards/$dashboardId',+ path: withId('dashboard'),+ scope: 'project',+ },+ { route: 'reports (new)', path: fixed('/reports'), scope: 'project' },+ { route: 'reports/$reportId', path: withId('report'), scope: 'project' },+ { route: 'events', path: fixed('/events'), scope: 'project' },+ { route: 'events/events', path: fixed('/events/events'), scope: 'project' },+ {+ route: 'events/conversions',+ path: fixed('/events/conversions'),+ scope: 'project',+ },+ { route: 'events/stats', path: fixed('/events/stats'), scope: 'project' },+ { route: 'sessions', path: fixed('/sessions'), scope: 'project' },+ { route: 'sessions/$sessionId', path: withId('session'), scope: 'project' },+ { route: 'profiles', path: fixed('/profiles'), scope: 'project' },+ {+ route: 'profiles/identified',+ path: fixed('/profiles/identified'),+ scope: 'project',+ },+ {+ route: 'profiles/anonymous',+ path: fixed('/profiles/anonymous'),+ scope: 'project',+ },+ {+ route: 'profiles/power-users',+ path: fixed('/profiles/power-users'),+ scope: 'project',+ },+ { route: 'profiles/$profileId', path: withId('profile'), scope: 'project' },+ {+ route: 'profiles/$profileId/events',+ path: withId('profile', '/events'),+ scope: 'project',+ },+ {+ route: 'profiles/$profileId/sessions',+ path: withId('profile', '/sessions'),+ scope: 'project',+ },+ { route: 'groups', path: fixed('/groups'), scope: 'project' },+ { route: 'groups/$groupId', path: withId('group'), scope: 'project' },+ {+ route: 'groups/$groupId/events',+ path: withId('group', '/events'),+ scope: 'project',+ },+ {+ route: 'groups/$groupId/members',+ path: withId('group', '/members'),+ scope: 'project',+ },+ { route: 'cohorts', path: fixed('/cohorts'), scope: 'project' },+ { route: 'cohorts/$cohortId', path: withId('cohort'), scope: 'project' },+ {+ route: 'cohorts/$cohortId/events',+ path: withId('cohort', '/events'),+ scope: 'project',+ },+ {+ route: 'cohorts/$cohortId/members',+ path: withId('cohort', '/members'),+ scope: 'project',+ },+ { route: 'integrations', path: fixed('/integrations'), scope: 'project' },+ {+ route: 'integrations/available',+ path: fixed('/integrations/available'),+ scope: 'project',+ },+ {+ route: 'integrations/installed',+ path: fixed('/integrations/installed'),+ scope: 'project',+ },+ { route: 'notifications', path: fixed('/notifications'), scope: 'project' },+ {+ route: 'notifications/notifications',+ path: fixed('/notifications/notifications'),+ scope: 'project',+ },+ {+ route: 'notifications/rules',+ path: fixed('/notifications/rules'),+ scope: 'project',+ },+ { route: 'settings', path: fixed('/settings'), scope: 'project' },+ {+ route: 'settings/details',+ path: fixed('/settings/details'),+ scope: 'project',+ },+ {+ route: 'settings/events',+ path: fixed('/settings/events'),+ scope: 'project',+ },+ {+ route: 'settings/clients',+ path: fixed('/settings/clients'),+ scope: 'project',+ },+ {+ route: 'settings/tracking',+ path: fixed('/settings/tracking'),+ scope: 'project',+ },+ {+ route: 'settings/widgets',+ path: fixed('/settings/widgets'),+ scope: 'project',+ },+ {+ route: 'settings/imports',+ path: fixed('/settings/imports'),+ scope: 'project',+ },+ { route: 'settings/mcp', path: fixed('/settings/mcp'), scope: 'project' },+ { route: 'settings/gsc', path: fixed('/settings/gsc'), scope: 'project' },+];++export const ORGANIZATION_ROUTES: RouteSpec[] = [+ { route: '/ (index)', path: fixed('/'), scope: 'global' },+ { route: '$organizationId/', path: fixed(''), scope: 'organization' },+ {+ route: '$organizationId/settings',+ path: fixed('/settings'),+ scope: 'organization',+ },+ {+ route: '$organizationId/billing',+ path: fixed('/billing'),+ scope: 'organization',+ },+ {+ route: '$organizationId/members',+ path: fixed('/members'),+ scope: 'organization',+ },+ {+ route: '$organizationId/members/members',+ path: fixed('/members/members'),+ scope: 'organization',+ },+ {+ route: '$organizationId/members/invitations',+ path: fixed('/members/invitations'),+ scope: 'organization',+ },+ {+ route: '$organizationId/account',+ path: fixed('/account'),+ scope: 'organization',+ },+ {+ route: '$organizationId/account/email-preferences',+ path: fixed('/account/email-preferences'),+ scope: 'organization',+ },+ {+ route: '$organizationId/account/two-factor',+ path: fixed('/account/two-factor'),+ scope: 'organization',+ },+ { route: '/account', path: fixed('/account'), scope: 'global' },+ { route: '/login (signed in)', path: fixed('/login'), scope: 'global' },+ {+ route: '/reset-password (no token)',+ path: fixed('/reset-password'),+ scope: 'global',+ },+ { route: '/verify', path: fixed('/verify'), scope: 'global' },+ { route: '/onboarding', path: fixed('/onboarding'), scope: 'global' },+ {+ route: '/onboarding/project',+ path: fixed('/onboarding/project'),+ scope: 'global',+ },+ {+ route: '/onboarding/$projectId/connect',+ path: fixed('/onboarding/acme-shop/connect'),+ scope: 'global',+ },+ {+ route: '/onboarding/$projectId/verify',+ path: fixed('/onboarding/acme-shop/verify'),+ scope: 'global',+ },+ {+ route: '/unsubscribe (no token)',+ path: fixed('/unsubscribe'),+ scope: 'global',+ },+];++export function resolveUrl(+ spec: RouteSpec,+ projectId: string,+ ids: ProjectIds+): string | null {+ const path = spec.path(ids);+ if (path === null) {+ return null;+ }+ if (spec.scope === 'project') {+ return `/${ORGANIZATION_ID}/${projectId}${path}`;+ }+ if (spec.scope === 'organization') {+ return `/${ORGANIZATION_ID}${path}`;+ }+ return path;+}++export async function trpcQuery<T>(+ request: APIRequestContext,+ procedure: string,+ input: unknown+): Promise<T | null> {+ const response = await request.get(`${API_URL}/trpc/${procedure}`, {+ params: { input: JSON.stringify({ json: input }) },+ ignoreHTTPSErrors: true,+ });+ if (!response.ok()) {+ return null;+ }+ const body = await response.json();+ return (body?.result?.data?.json ?? null) as T | null;+}++export async function resolveProjectIds(+ request: APIRequestContext,+ projectId: string+): Promise<ProjectIds> {+ const [profiles, sessions, groups, cohorts, dashboards] = await Promise.all([+ trpcQuery<{ data: { id: string }[] }>(request, 'profile.list', {+ projectId,+ take: 1,+ }),+ trpcQuery<{ items: { id: string }[] }>(request, 'session.list', {+ projectId,+ take: 1,+ }),+ trpcQuery<{ data: { id: string }[] }>(request, 'group.list', {+ projectId,+ take: 1,+ }),+ trpcQuery<{ id: string }[]>(request, 'cohort.list', { projectId }),+ trpcQuery<{ id: string }[]>(request, 'dashboard.list', { projectId }),+ ]);+ const dashboard = dashboards?.[0]?.id ?? null;+ const reports = dashboard+ ? await trpcQuery<{ id: string }[]>(request, 'report.list', {+ projectId,+ dashboardId: dashboard,+ })+ : null;+ return {+ profile: profiles?.data?.[0]?.id ?? null,+ session: sessions?.items?.[0]?.id ?? null,+ group: groups?.data?.[0]?.id ?? null,+ cohort: cohorts?.[0]?.id ?? null,+ dashboard,+ report: reports?.[0]?.id ?? null,+ };+}++export interface BadResponse {+ status: number;+ procedure: string;+ message: string;+}++export interface Recorder {+ consoleErrors: string[];+ consoleWarnings: string[];+ pageErrors: string[];+ badResponses: BadResponse[];+ failedRequests: string[];+ /** Every 401 and every document navigation of the whole run; never reset, to explain surprise redirects. */+ authTrail: string[];+ reset: () => void;+ snapshot: () => Omit<Recorder, 'reset' | 'snapshot' | 'authTrail'>;+}++const NOISE = [+ /\[vite\]/i,+ /favicon/i,+ /sentry/i,+ /Download the React DevTools/i,+ /hot[- ]update/i,+ /api\.openpanel\.dev\/track/,+];++const isNoise = (text: string) => NOISE.some((pattern) => pattern.test(text));++async function describeBadResponse(response: Response): Promise<BadResponse> {+ const url = new URL(response.url());+ const procedure = url.pathname.includes('/trpc/')+ ? url.pathname.split('/trpc/')[1]+ : `${response.request().method()} ${url.host}${url.pathname}`;+ let message = '';+ try {+ const text = await response.text();+ const parsed = JSON.parse(text);+ const first = Array.isArray(parsed) ? parsed.find((p) => p.error) : parsed;+ message =+ first?.error?.json?.message ??+ first?.error?.message ??+ first?.message ??+ text.slice(0, 200);+ } catch {+ message = '(no readable body)';+ }+ return {+ status: response.status(),+ procedure,+ message: String(message).slice(0, 300),+ };+}++export function attachRecorder(page: Page): Recorder {+ const recorder: Recorder = {+ consoleErrors: [],+ consoleWarnings: [],+ pageErrors: [],+ badResponses: [],+ failedRequests: [],+ authTrail: [],+ reset() {+ recorder.consoleErrors = [];+ recorder.consoleWarnings = [];+ recorder.pageErrors = [];+ recorder.badResponses = [];+ recorder.failedRequests = [];+ },+ snapshot() {+ return {+ consoleErrors: [...recorder.consoleErrors],+ consoleWarnings: [...recorder.consoleWarnings],+ pageErrors: [...recorder.pageErrors],+ badResponses: [...recorder.badResponses],+ failedRequests: [...recorder.failedRequests],+ };+ },+ };+ page.on('console', (message) => {+ const text = message.text();+ if (isNoise(`${text} ${message.location().url}`)) {+ return;+ }+ if (message.type() === 'error') {+ recorder.consoleErrors.push(text.slice(0, 6000));+ }+ if (message.type() === 'warning') {+ recorder.consoleWarnings.push(text.slice(0, 600));+ }+ });+ page.on('pageerror', (error) => {+ recorder.pageErrors.push(error.message.slice(0, 8000));+ });+ page.on('response', (response) => {+ const isDocument = response.request().resourceType() === 'document';+ if (response.status() === 401 || isDocument) {+ recorder.authTrail.push(+ `${new Date().toISOString()} ${response.status()} ${response.url().slice(0, 160)} (page at ${page.url()})`+ );+ }+ if (response.status() < 400 || isNoise(response.url())) {+ return;+ }+ describeBadResponse(response).then((bad) =>+ recorder.badResponses.push(bad)+ );+ });+ page.on('requestfailed', (request) => {+ const failure = request.failure()?.errorText ?? '';+ const isAborted = failure.includes('ERR_ABORTED');+ if (isAborted || isNoise(request.url())) {+ return;+ }+ recorder.failedRequests.push(`${request.url()} ${failure}`.slice(0, 300));+ });+ return recorder;+}++const SUSPICIOUS_TEXT: [string, RegExp][] = [+ ['undefined', /\bundefined\b/],+ ['NaN', /\bNaN\b/],+ ['null', /\bnull\b/],+ ['Invalid Date', /Invalid Date/i],+ ['No match', /No match/],+ ['[object Object]', /\[object Object\]/],+ ['Something went wrong', /Something went wrong/i],+ ['Not found', /\bnot found\b/i],+ ['error boundary', /unexpected error|an error occurred|try again/i],+];++export interface PageState {+ suspicious: { label: string; snippet: string }[];+ isBlank: boolean;+ textLength: number;+ horizontalOverflowPx: number;+ title: string;+}++export async function inspectPage(page: Page): Promise<PageState> {+ const { text, overflow, title } = await page.evaluate(() => ({+ text: document.body?.innerText ?? '',+ overflow:+ document.documentElement.scrollWidth -+ document.documentElement.clientWidth,+ title: document.title,+ }));+ const suspicious: PageState['suspicious'] = [];+ for (const [label, pattern] of SUSPICIOUS_TEXT) {+ const match = pattern.exec(text);+ if (!match) {+ continue;+ }+ const start = Math.max(0, match.index - SNIPPET_RADIUS);+ const end = match.index + match[0].length + SNIPPET_RADIUS;+ suspicious.push({+ label,+ snippet: text.slice(start, end).replace(/\s+/g, ' '),+ });+ }+ return {+ suspicious,+ isBlank: text.trim().length < BLANK_PAGE_MAX_CHARS,+ textLength: text.trim().length,+ horizontalOverflowPx: overflow,+ title,+ };+}++const HYDRATION_TIMEOUT_MS = 45_000;+const CLIENT_NAVIGATION_START_MS = 700;+const SKELETON_TIMEOUT_MS = 15_000;+const SKELETON_SELECTOR = '.animate-pulse';++/** The dev server ships ~1200 unbundled modules, so React attaches many seconds after `load`. */+export async function waitForHydration(page: Page): Promise<boolean> {+ try {+ await page.waitForFunction(+ () =>+ Array.from(document.querySelectorAll('body div, body a, body button'))+ .slice(0, 40)+ .some((element) =>+ Object.keys(element).some((key) => key.startsWith('__react'))+ ),+ undefined,+ { timeout: HYDRATION_TIMEOUT_MS }+ );+ return true;+ } catch {+ return false;+ }+}++/** Waits for the page to settle; returns false when the network never went idle. */+export async function settle(page: Page): Promise<boolean> {+ await waitForHydration(page);+ let wentIdle = true;+ try {+ await page.waitForLoadState('networkidle', {+ timeout: NETWORK_IDLE_TIMEOUT_MS,+ });+ } catch {+ wentIdle = false;+ }+ await page+ .waitForFunction(+ (selector) => document.querySelectorAll(selector).length === 0,+ SKELETON_SELECTOR,+ { timeout: SKELETON_TIMEOUT_MS }+ )+ .catch(() => undefined);+ return wentIdle;+}++export const countSkeletons = (page: Page) =>+ page.locator(SKELETON_SELECTOR).count();++/** Client-side navigation: TanStack Router patches `history.pushState`, so this runs loaders without a document load. */+export async function clientNavigate(page: Page, url: string) {+ await page.evaluate((target) => {+ window.history.pushState({}, '', target);+ }, url);+}++export interface Visit {+ route: string;+ project: string;+ url: string | null;+ finalUrl?: string;+ status?: number | null;+ loadMs?: number;+ wentIdle?: boolean;+ state?: PageState;+ consoleErrors?: string[];+ consoleWarnings?: string[];+ pageErrors?: string[];+ badResponses?: BadResponse[];+ failedRequests?: string[];+ screenshot?: string;+ navigationError?: string;+ hydrateMs?: number;+ hydrated?: boolean;+ attempts?: number;+ skeletonsLeft?: number;+}++const MAX_LOAD_ATTEMPTS = 3;++/**+ * The dev server answers 502 for some of its ~1200 modules when the machine is+ * saturated; the client entry then never runs. That is not an app defect, so a+ * visit that never hydrated is loaded again before it is recorded.+ */+export async function visit(+ page: Page,+ recorder: Recorder,+ label: { route: string; project: string },+ url: string,+ screenshotPath?: string+): Promise<Visit> {+ const result: Visit = { ...label, url };+ for (let attempt = 1; attempt <= MAX_LOAD_ATTEMPTS; attempt++) {+ recorder.reset();+ const startedAt = Date.now();+ delete result.navigationError;+ result.attempts = attempt;+ try {+ const response = await page.goto(url, { waitUntil: 'load' });+ result.status = response?.status() ?? null;+ result.hydrated = await waitForHydration(page);+ result.hydrateMs = Date.now() - startedAt;+ if (!result.hydrated && attempt < MAX_LOAD_ATTEMPTS) {+ continue;+ }+ result.wentIdle = await settle(page);+ result.skeletonsLeft = await countSkeletons(page);+ result.loadMs = Date.now() - startedAt;+ result.finalUrl =+ new URL(page.url()).pathname + new URL(page.url()).search;+ result.state = await inspectPage(page);+ if (screenshotPath) {+ mkdirSync(dirname(screenshotPath), { recursive: true });+ await page.screenshot({ path: screenshotPath, fullPage: true });+ result.screenshot = screenshotPath;+ }+ break;+ } catch (error) {+ result.navigationError = String(error).slice(0, 500);+ }+ }+ Object.assign(result, recorder.snapshot());+ logProgress(result);+ return result;+}++/** Same record as `visit`, reached by client-side navigation from an already hydrated page. */+export async function visitClientSide(+ page: Page,+ recorder: Recorder,+ label: { route: string; project: string },+ url: string,+ screenshotPath?: string+): Promise<Visit> {+ recorder.reset();+ const startedAt = Date.now();+ const result: Visit = { ...label, url };+ try {+ await clientNavigate(page, url);+ // Loaders and queries start a tick after the history change; without this+ // pause `networkidle` resolves against the previous page.+ await page.waitForTimeout(CLIENT_NAVIGATION_START_MS);+ result.wentIdle = await settle(page);+ result.skeletonsLeft = await countSkeletons(page);+ result.loadMs = Date.now() - startedAt;+ result.finalUrl = new URL(page.url()).pathname + new URL(page.url()).search;+ result.state = await inspectPage(page);+ if (screenshotPath) {+ mkdirSync(dirname(screenshotPath), { recursive: true });+ await page.screenshot({ path: screenshotPath, fullPage: true });+ result.screenshot = screenshotPath;+ }+ } catch (error) {+ result.navigationError = String(error).slice(0, 500);+ }+ Object.assign(result, recorder.snapshot());+ logProgress(result);+ return result;+}++/** One line per visit on stdout, so a long sweep shows where it is. */+export function logProgress(result: Visit) {+ process.stdout.write(+ `${result.project} ${result.route} -> ${result.finalUrl ?? result.navigationError} ${result.loadMs ?? '?'}ms skeletons=${result.skeletonsLeft ?? '?'}\n`+ );+}++export function isClean(result: Visit): boolean {+ return (+ !result.navigationError &&+ (result.status ?? 0) < 400 &&+ !result.pageErrors?.length &&+ !result.consoleErrors?.length &&+ !result.badResponses?.length &&+ !result.state?.isBlank &&+ !result.state?.suspicious.length+ );+}++export function writeJson(name: string, data: unknown) {+ const file = resolve(OUTPUT_DIR, name);+ mkdirSync(dirname(file), { recursive: true });+ writeFileSync(file, JSON.stringify(data, null, 2));+}++export const slug = (route: string) =>+ route+ .replace(/[^a-zA-Z0-9]+/g, '-')+ .replace(/^-|-$/g, '')+ .toLowerCase() || 'root';diff --git a/apps/start/e2e/sweep-not-found.spec.ts b/apps/start/e2e/sweep-not-found.spec.tsnew file mode 100644index 000000000..a2b99db57--- /dev/null+++ b/apps/start/e2e/sweep-not-found.spec.ts@@ -0,0 +1,187 @@+import { resolve } from 'node:path';+import { expect, test } from './fixtures';+import {+ attachRecorder,+ OUTPUT_DIR,+ settle,+ slug,+ type Visit,+ visit,+ writeJson,+} from './sweep-helpers';++// A click that cannot land should fail the step, not sit until the sweep-sized test timeout.+test.use({ actionTimeout: 20_000 });++const UNKNOWN_ID = 'does-not-exist-e2e';+const PROJECT = '/acme/acme-shop';+const SPINNER_SELECTOR = '.animate-spin, .animate-pulse, [aria-busy="true"]';++const CASES: { name: string; url: string }[] = [+ { name: 'unknown organization', url: `/${UNKNOWN_ID}` },+ { name: 'unknown organization settings', url: `/${UNKNOWN_ID}/settings` },+ { name: 'unknown project', url: `/acme/${UNKNOWN_ID}` },+ { name: 'unknown project sessions', url: `/acme/${UNKNOWN_ID}/sessions` },+ { name: 'project of unknown organization', url: `/${UNKNOWN_ID}/acme-shop` },+ { name: 'unknown project page', url: `${PROJECT}/${UNKNOWN_ID}` },+ { name: 'unknown deep path', url: `/a/b/c/d/e/${UNKNOWN_ID}` },+ { name: 'unknown session', url: `${PROJECT}/sessions/${UNKNOWN_ID}` },+ { name: 'unknown profile', url: `${PROJECT}/profiles/${UNKNOWN_ID}` },+ {+ name: 'unknown profile events',+ url: `${PROJECT}/profiles/${UNKNOWN_ID}/events`,+ },+ { name: 'unknown dashboard', url: `${PROJECT}/dashboards/${UNKNOWN_ID}` },+ { name: 'unknown report', url: `${PROJECT}/reports/${UNKNOWN_ID}` },+ { name: 'unknown cohort', url: `${PROJECT}/cohorts/${UNKNOWN_ID}` },+ { name: 'unknown group', url: `${PROJECT}/groups/${UNKNOWN_ID}` },+ { name: 'unknown settings tab', url: `${PROJECT}/settings/${UNKNOWN_ID}` },+ {+ name: 'malformed search params',+ url: `${PROJECT}/sessions?filters=%7Bbroken&cursor=abc`,+ },+ {+ name: 'malformed overview range',+ url: `${PROJECT}?range=nonsense&startDate=not-a-date&endDate=x&interval=zzz`,+ },+ { name: 'double slash', url: '/acme//acme-shop' },+ { name: 'unknown overview share', url: `/share/overview/${UNKNOWN_ID}` },+ { name: 'unknown dashboard share', url: `/share/dashboard/${UNKNOWN_ID}` },+ { name: 'unknown report share', url: `/share/report/${UNKNOWN_ID}` },+ {+ name: 'widget counter unknown share',+ url: `/widget/counter?shareId=${UNKNOWN_ID}`,+ },+ {+ name: 'widget realtime unknown share',+ url: `/widget/realtime?shareId=${UNKNOWN_ID}`,+ },+ {+ name: 'widget badge unknown share',+ url: `/widget/badge?shareId=${UNKNOWN_ID}`,+ },+ { name: 'widget realtime without share id', url: '/widget/realtime' },+ {+ name: 'onboarding unknown project',+ url: `/onboarding/${UNKNOWN_ID}/connect`,+ },+ { name: 'verify without params', url: '/verify' },+];++test.describe('not-found and access handling', () => {+ test.setTimeout(10 * 60 * 1000);++ test('unknown ids and malformed urls end in a not-found, error page or redirect', async ({+ page,+ }) => {+ const recorder = attachRecorder(page);+ const results: (Visit & {+ name: string;+ spinners: number;+ text: string;+ })[] = [];+ for (const item of CASES) {+ const result = await visit(+ page,+ recorder,+ { route: item.name, project: '-' },+ item.url,+ resolve(OUTPUT_DIR, 'shots', 'not-found', `${slug(item.name)}.png`)+ );+ const spinners = await page+ .locator(SPINNER_SELECTOR)+ .count()+ .catch(() => -1);+ const text = await page+ .evaluate(() =>+ document.body.innerText.replace(/\s+/g, ' ').slice(0, 300)+ )+ .catch(() => '');+ results.push({ ...result, name: item.name, spinners, text });+ }+ writeJson('not-found.json', results);++ const broken = results+ .filter(+ (result) =>+ result.navigationError ||+ (result.status ?? 0) >= 500 ||+ result.pageErrors?.length ||+ result.state?.isBlank+ )+ .map(({ name, url, status, pageErrors, navigationError }) => ({+ name,+ url,+ status,+ pageErrors,+ navigationError,+ }));+ expect(broken).toEqual([]);+ });++ test('the escape buttons on not-found and error pages lead home', async ({+ page,+ }) => {+ const outcomes: Record<string, { href: string | null; landedOn: string }> =+ {};+ const cases = [+ { url: `/${UNKNOWN_ID}`, button: 'Go to home' },+ { url: `${PROJECT}/sessions/${UNKNOWN_ID}`, button: 'Go back to home' },+ { url: `/acme/${UNKNOWN_ID}`, button: 'Go back to home' },+ ];+ for (const item of cases) {+ await page.goto(item.url);+ await settle(page);+ const link = page.getByRole('link', { name: item.button });+ await expect(link).toBeVisible();+ const href = await link.getAttribute('href');+ await link.click();+ await page.waitForTimeout(2500);+ outcomes[item.url] = { href, landedOn: new URL(page.url()).pathname };+ }+ writeJson('not-found-escape-buttons.json', outcomes);+ for (const [url, outcome] of Object.entries(outcomes)) {+ expect(outcome.landedOn, `"home" button on ${url}`).toBe('/acme');+ }+ });++ test('a malformed percent-encoded path gets an http answer, not a dropped connection', async ({+ page,+ }) => {+ const response = await page+ .goto(`${PROJECT}/profiles/%E0%A4%A`)+ .catch((error: Error) => error);+ const outcome =+ response instanceof Error+ ? response.message.split('\n')[0]+ : `status ${response?.status()}`;+ writeJson('not-found-malformed-path.json', { outcome });+ expect(outcome).toMatch(/^status (400|404)$/);+ });++ test('a signed-out visitor is sent to the login page', async ({+ browser,+ }) => {+ const context = await browser.newContext({+ ignoreHTTPSErrors: true,+ storageState: { cookies: [], origins: [] },+ });+ const page = await context.newPage();+ const outcomes: Record<string, string> = {};+ for (const url of [+ '/acme/acme-shop',+ '/acme',+ '/acme/acme-shop/sessions',+ '/',+ ]) {+ await page.goto(url);+ await page.waitForLoadState('networkidle').catch(() => undefined);+ outcomes[url] = new URL(page.url()).pathname;+ }+ writeJson('not-found-signed-out.json', outcomes);+ await context.close();+ for (const [url, finalPath] of Object.entries(outcomes)) {+ expect(finalPath, `signed-out visit to ${url}`).toMatch(/^\/login/);+ }+ });+});diff --git a/apps/start/e2e/sweep-routes.spec.ts b/apps/start/e2e/sweep-routes.spec.tsnew file mode 100644index 000000000..ae193f22b--- /dev/null+++ b/apps/start/e2e/sweep-routes.spec.ts@@ -0,0 +1,120 @@+import { resolve } from 'node:path';+import { expect, test } from './fixtures';+import {+ attachRecorder,+ ORGANIZATION_ROUTES,+ OUTPUT_DIR,+ PROJECT_ROUTES,+ resolveProjectIds,+ resolveUrl,+ slug,+ type Visit,+ visit,+ visitClientSide,+ writeJson,+} from './sweep-helpers';++const SCREENSHOT_PROJECT = 'acme-shop';+const SWEEP_TIMEOUT_MS = 40 * 60 * 1000;++const EMPTY_IDS = {+ profile: null,+ session: null,+ group: null,+ cohort: null,+ dashboard: null,+ report: null,+};++function crashes(results: Visit[]) {+ return results+ .filter(+ (result) =>+ result.navigationError ||+ (result.status ?? 0) >= 500 ||+ result.pageErrors?.length ||+ result.state?.isBlank+ )+ .map((result) => ({+ url: result.url,+ status: result.status,+ pageErrors: result.pageErrors,+ navigationError: result.navigationError,+ blank: result.state?.isBlank,+ }));+}++test.describe('route sweep (direct load)', () => {+ test.setTimeout(SWEEP_TIMEOUT_MS);++ test('organization and global routes load without crashing', async ({+ page,+ }) => {+ const recorder = attachRecorder(page);+ const results: Visit[] = [];+ for (const spec of ORGANIZATION_ROUTES) {+ const url = resolveUrl(spec, SCREENSHOT_PROJECT, EMPTY_IDS);+ if (!url) {+ continue;+ }+ results.push(+ await visit(+ page,+ recorder,+ { route: spec.route, project: '-' },+ url,+ resolve(+ OUTPUT_DIR,+ 'shots',+ 'desktop-light',+ `org-${slug(spec.route)}.png`+ )+ )+ );+ }+ writeJson('routes-organization.json', results);+ expect(crashes(results)).toEqual([]);+ });++ for (const projectId of ['acme-shop', 'acme-web', 'acme-saas', 'acme-app']) {+ test(`project routes load without crashing: ${projectId}`, async ({+ page,+ }) => {+ const recorder = attachRecorder(page);+ const ids = await resolveProjectIds(page.request, projectId);+ const results: Visit[] = [];+ for (const spec of PROJECT_ROUTES) {+ const url = resolveUrl(spec, projectId, ids);+ if (!url) {+ results.push({ route: spec.route, project: projectId, url: null });+ continue;+ }+ const screenshot =+ projectId === SCREENSHOT_PROJECT+ ? resolve(+ OUTPUT_DIR,+ 'shots',+ 'desktop-light',+ `${slug(spec.route)}.png`+ )+ : undefined;+ // A document load costs ~1200 dev-server module requests, so only the+ // screenshot project and each project's first route pay for one.+ const isDocumentLoad =+ projectId === SCREENSHOT_PROJECT || results.length === 0;+ const label = { route: spec.route, project: projectId };+ results.push(+ isDocumentLoad+ ? await visit(page, recorder, label, url, screenshot)+ : await visitClientSide(page, recorder, label, url)+ );+ }+ writeJson(`routes-${projectId}.json`, {+ ids,+ authTrail: recorder.authTrail,+ results,+ });+ expect(crashes(results)).toEqual([]);+ });+ }+});diff --git a/apps/start/e2e/sweep-ssr.spec.ts b/apps/start/e2e/sweep-ssr.spec.tsnew file mode 100644index 000000000..9afad5935--- /dev/null+++ b/apps/start/e2e/sweep-ssr.spec.ts@@ -0,0 +1,104 @@+import { resolve } from 'node:path';+import { expect, test } from './fixtures';+import {+ attachRecorder,+ countSkeletons,+ OUTPUT_DIR,+ settle,+ slug,+ writeJson,+} from './sweep-helpers';++const AUTH_STATE_FILE = 'e2e/.auth/user.json';+const PROJECT = '/acme/acme-shop';+const MAIN_PAGES = [+ '/acme',+ '/acme/settings',+ PROJECT,+ `${PROJECT}/pages`,+ `${PROJECT}/events`,+ `${PROJECT}/sessions`,+ `${PROJECT}/profiles`,+ `${PROJECT}/realtime`,+ `${PROJECT}/dashboards`,+ `${PROJECT}/settings/details`,+ `${PROJECT}/settings/clients`,+ `${PROJECT}/events/stats`,+ `${PROJECT}/profiles/eff0e0780de4d2acfc1756f492f206b0`,+ `${PROJECT}/sessions/mTLtFVNQBmFLDSb4Fdt3vg`,+];++const textLines = (text: string) =>+ new Set(+ text+ .split('\n')+ .map((line) => line.trim())+ .filter(Boolean)+ );++test('server-rendered html matches what hydration expects on the main pages', async ({+ browser,+ page,+ baseURL,+}) => {+ test.setTimeout(20 * 60 * 1000);+ const serverOnly = await browser.newContext({+ ignoreHTTPSErrors: true,+ baseURL,+ storageState: AUTH_STATE_FILE,+ javaScriptEnabled: false,+ });+ const serverPage = await serverOnly.newPage();+ const recorder = attachRecorder(page);++ const report: Record<string, unknown>[] = [];+ for (const url of MAIN_PAGES) {+ const response = await serverPage.goto(url, { waitUntil: 'load' });+ const serverText = await serverPage.evaluate(() => document.body.innerText);+ const serverSkeletons = await serverPage.locator('.animate-pulse').count();+ await serverPage.screenshot({+ path: resolve(OUTPUT_DIR, 'shots', 'ssr', `${slug(url)}-server.png`),+ fullPage: true,+ });++ recorder.reset();+ await page.goto(url, { waitUntil: 'load' });+ await settle(page);+ const clientText = await page.evaluate(() => document.body.innerText);+ await page.screenshot({+ path: resolve(OUTPUT_DIR, 'shots', 'ssr', `${slug(url)}-hydrated.png`),+ fullPage: true,+ });++ const serverLines = textLines(serverText);+ const clientLines = textLines(clientText);+ report.push({+ url,+ status: response?.status(),+ serverTextLength: serverText.trim().length,+ clientTextLength: clientText.trim().length,+ serverSkeletons,+ clientSkeletons: await countSkeletons(page),+ onlyInServerHtml: [...serverLines]+ .filter((line) => !clientLines.has(line))+ .slice(0, 40),+ onlyAfterHydration: [...clientLines]+ .filter((line) => !serverLines.has(line))+ .slice(0, 40),+ hydrationErrors: [+ ...recorder.consoleErrors,+ ...recorder.pageErrors,+ ].filter((text) => /hydrat|did not match|didn't match/i.test(text)),+ pageErrors: recorder.pageErrors,+ });+ }+ await serverOnly.close();+ writeJson('ssr.json', report);++ expect(+ report+ .filter((entry) => (entry.hydrationErrors as string[]).length > 0)+ .map((entry) => entry.url),+ 'pages with a React hydration mismatch'+ ).toEqual([]);+});diff --git a/apps/start/e2e/sweep-viewports.spec.ts b/apps/start/e2e/sweep-viewports.spec.tsnew file mode 100644index 000000000..16e8605ab--- /dev/null+++ b/apps/start/e2e/sweep-viewports.spec.ts@@ -0,0 +1,190 @@+import { resolve } from 'node:path';+import type { Page } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ attachRecorder,+ ORGANIZATION_ROUTES,+ OUTPUT_DIR,+ PROJECT_ROUTES,+ resolveProjectIds,+ resolveUrl,+ slug,+ type Visit,+ visit,+ visitClientSide,+ writeJson,+} from './sweep-helpers';++// A click that cannot land should fail the step, not sit until the sweep-sized test timeout.+test.use({ actionTimeout: 20_000 });++const PROJECT = 'acme-shop';+const AUTH_STATE_FILE = 'e2e/.auth/user.json';+const THEME_COOKIE = 'ui-theme';+const MAX_REPORTED_OVERFLOWS = 6;++const VARIANTS = [+ { name: 'mobile-light', width: 390, height: 844, theme: 'light' },+ { name: 'tablet-light', width: 768, height: 1024, theme: 'light' },+ { name: 'desktop-dark', width: 1280, height: 720, theme: 'dark' },+ { name: 'mobile-dark', width: 390, height: 844, theme: 'dark' },+] as const;++/** Elements that stick out of the viewport horizontally and are not inside a scroll container. */+function findOverflowingElements(page: Page, limit: number) {+ return page.evaluate((max) => {+ const viewportWidth = document.documentElement.clientWidth;+ const isInsideScroller = (element: Element) => {+ let parent = element.parentElement;+ while (parent && parent !== document.body) {+ const overflowX = getComputedStyle(parent).overflowX;+ if (+ overflowX === 'auto' ||+ overflowX === 'scroll' ||+ overflowX === 'hidden'+ ) {+ return true;+ }+ parent = parent.parentElement;+ }+ return false;+ };+ const found: string[] = [];+ for (const element of document.querySelectorAll('body *')) {+ const rect = element.getBoundingClientRect();+ const isVisible = rect.width > 0 && rect.height > 0;+ const sticksOut =+ rect.right > viewportWidth + 2 && rect.left < viewportWidth;+ if (!(isVisible && sticksOut) || isInsideScroller(element)) {+ continue;+ }+ const position = getComputedStyle(element).position;+ if (position === 'fixed') {+ continue;+ }+ const text = (element.textContent ?? '').trim().slice(0, 40);+ found.push(+ `<${element.tagName.toLowerCase()} class="${String(element.className).slice(0, 80)}"> right=${Math.round(rect.right)} "${text}"`+ );+ if (found.length >= max) {+ break;+ }+ }+ return found;+ }, limit);+}++for (const variant of VARIANTS) {+ test(`acme-shop routes render at ${variant.name} without a crash or page-level horizontal scroll`, async ({+ browser,+ baseURL,+ }) => {+ test.setTimeout(15 * 60 * 1000);+ const context = await browser.newContext({+ ignoreHTTPSErrors: true,+ baseURL,+ storageState: AUTH_STATE_FILE,+ viewport: { width: variant.width, height: variant.height },+ colorScheme: variant.theme,+ });+ await context.addCookies([+ {+ name: THEME_COOKIE,+ value: variant.theme,+ domain: new URL(baseURL ?? '').hostname,+ path: '/',+ },+ ]);+ const page = await context.newPage();+ const recorder = attachRecorder(page);+ const ids = await resolveProjectIds(page.request, PROJECT);+ const shotDir = resolve(OUTPUT_DIR, 'shots', variant.name);++ const results: (Visit & { overflowing: string[]; htmlClass: string })[] =+ [];+ const specs = [+ ...PROJECT_ROUTES,+ ...ORGANIZATION_ROUTES.filter((spec) => spec.scope === 'organization'),+ ];+ for (const spec of specs) {+ const url = resolveUrl(spec, PROJECT, ids);+ if (!url) {+ continue;+ }+ const prefix = spec.scope === 'organization' ? 'org-' : '';+ // One document load per variant; the rest navigate client-side, which+ // keeps the viewport and theme and skips the dev server's module storm.+ const open = results.length === 0 ? visit : visitClientSide;+ const result = await open(+ page,+ recorder,+ { route: spec.route, project: PROJECT },+ url,+ resolve(shotDir, `${prefix}${slug(spec.route)}.png`)+ );+ results.push({+ ...result,+ overflowing: await findOverflowingElements(+ page,+ MAX_REPORTED_OVERFLOWS+ ),+ htmlClass: await page.evaluate(+ () => document.documentElement.className+ ),+ });+ }++ const isMobile = variant.width < 1024;+ let mobileNavigationWorks: boolean | null = null;+ if (isMobile) {+ await visitClientSide(+ page,+ recorder,+ { route: 'overview', project: PROJECT },+ `/acme/${PROJECT}`+ );+ const sidebar = page.locator('div.fixed.w-72');+ const sessionsLink = sidebar.getByRole('link', {+ name: 'Sessions',+ exact: true,+ });+ await expect(sessionsLink).not.toBeInViewport();+ await sidebar.locator('div.absolute.-right-12 button').click();+ await expect(sessionsLink).toBeInViewport();+ await page.waitForTimeout(400);+ await page.screenshot({+ path: resolve(shotDir, '_mobile-navigation-open.png'),+ });+ await sessionsLink.click();+ await expect(page).toHaveURL(/\/sessions/);+ await expect(sessionsLink).not.toBeInViewport();+ mobileNavigationWorks = true;+ }++ writeJson(`viewport-${variant.name}.json`, {+ mobileNavigationWorks,+ results,+ });+ await context.close();++ expect(+ results.every((result) =>+ result.htmlClass.split(' ').includes(variant.theme)+ ),+ 'the theme cookie is applied on every page'+ ).toBe(true);+ expect(+ results+ .filter((result) => result.pageErrors?.length || result.state?.isBlank)+ .map((result) => result.url)+ ).toEqual([]);+ expect(+ results+ .filter((result) => (result.state?.horizontalOverflowPx ?? 0) > 2)+ .map(+ (result) => `${result.url} (+${result.state?.horizontalOverflowPx}px)`+ ),+ 'pages that scroll horizontally as a whole'+ ).toEqual([]);+ });+}diff --git a/apps/start/e2e/verify-a-cohorts.spec.ts b/apps/start/e2e/verify-a-cohorts.spec.tsnew file mode 100644index 000000000..ccb458885--- /dev/null+++ b/apps/start/e2e/verify-a-cohorts.spec.ts@@ -0,0 +1,356 @@+import { readFileSync } from 'node:fs';+import {+ type APIRequestContext,+ type Page,+ request as playwrightRequest,+} from '@playwright/test';+import { expect, test } from './fixtures';+import {+ API_URL,+ clickhouseNumber,+ openHydrated,+ SHOTS_DIR,+ trpcMutation,+ trpcQuery,+ watchProblems,+} from './verify-a-helpers';++const PROJECT = 'acme-saas';+const BASE = `/acme/${PROJECT}`;+const AUTH_STATE = 'e2e/.auth/user.json';+const COMPUTE_TIMEOUT_MS = 90_000;+const RECOMPUTE_VISIBLE_MS = 45_000;+const LAST_30_DAYS = { type: 'relative', value: '30d' } as const;++// What the summary views the cohort queries read are built from: events of+// identified profiles only.+const IDENTIFIED_EVENTS = `FROM events WHERE project_id = '${PROJECT}' AND profile_id != device_id AND created_at >= toDate(now() - INTERVAL 30 DAY)`;++interface Filter {+ name: string;+ operator: string;+ value: string[];+}++const eventCriteria = (name: string, filters: Filter[] = []) => ({+ name,+ filters,+ timeframe: LAST_30_DAYS,+});++const eventDefinition = (+ operator: 'and' | 'or',+ ...events: ReturnType<typeof eventCriteria>[]+) => ({ type: 'event', criteria: { operator, events } });++const propertyIs = (name: string, value: string): Filter => ({+ name: `properties.${name}`,+ operator: 'is',+ value: [value],+});++interface CohortCase {+ definition: unknown;+ /** Counts the same members straight from `events`. */+ expected: string;+ isStatic?: boolean;+}++const CASES = {+ didEvent: {+ definition: eventDefinition('or', eventCriteria('report_created')),+ expected: `SELECT uniqExact(profile_id) ${IDENTIFIED_EVENTS} AND name = 'report_created'`,+ },+ oneProperty: {+ definition: eventDefinition(+ 'or',+ eventCriteria('feature_used', [propertyIs('feature', 'cohorts')])+ ),+ expected: `SELECT uniqExact(profile_id) ${IDENTIFIED_EVENTS} AND name = 'feature_used' AND properties['feature'] = 'cohorts'`,+ },+ twoProperties: {+ definition: eventDefinition(+ 'or',+ eventCriteria('report_viewed', [+ propertyIs('report_type', 'funnel'),+ propertyIs('range', '7d'),+ ])+ ),+ expected: `SELECT uniqExact(profile_id) ${IDENTIFIED_EVENTS} AND name = 'report_viewed' AND properties['report_type'] = 'funnel' AND properties['range'] = '7d'`,+ },+ startsWith: {+ definition: eventDefinition(+ 'or',+ eventCriteria('feature_used', [+ { name: 'properties.feature', operator: 'startsWith', value: ['fun'] },+ ])+ ),+ expected: `SELECT uniqExact(profile_id) ${IDENTIFIED_EVENTS} AND name = 'feature_used' AND properties['feature'] LIKE 'fun%'`,+ },+ eventColumn: {+ definition: eventDefinition(+ 'or',+ eventCriteria('screen_view', [+ { name: 'path', operator: 'is', value: ['/settings/billing'] },+ ])+ ),+ expected: `SELECT uniqExact(profile_id) ${IDENTIFIED_EVENTS} AND name = 'screen_view' AND path = '/settings/billing'`,+ },+ anyOfTwoEvents: {+ definition: eventDefinition(+ 'or',+ eventCriteria('checkout_started'),+ eventCriteria('pricing_viewed')+ ),+ expected: `SELECT uniqExact(profile_id) ${IDENTIFIED_EVENTS} AND name IN ('checkout_started', 'pricing_viewed')`,+ },+ profileProperty: {+ definition: {+ type: 'property',+ criteria: {+ operator: 'and',+ properties: [+ {+ name: 'profile.properties.company_size',+ operator: 'is',+ value: ['11-50'],+ },+ ],+ },+ },+ expected: `SELECT count() FROM profiles FINAL WHERE project_id = '${PROJECT}' AND properties['company_size'] = '11-50'`,+ },+ frozen: {+ definition: eventDefinition('or', eventCriteria('subscription_started')),+ expected: `SELECT uniqExact(profile_id) ${IDENTIFIED_EVENTS} AND name = 'subscription_started'`,+ isStatic: true,+ },+} satisfies Record<string, CohortCase>;++type CaseName = keyof typeof CASES;++interface Cohort {+ id: string;+ name: string;+ profileCount: number;+ lastComputedAt: string | null;+}++let api: APIRequestContext;+const cohorts = {} as Record<CaseName, Cohort>;++const getCohort = (id: string) => trpcQuery<Cohort>(api, 'cohort.get', { id });++/** The number on the overview's "Members" tile (the tab of the same name carries no number). */+const shownMemberCount = async (page: Page) => {+ const text = await page.locator('body').innerText();+ const match = /members\s*\n\s*([\d,]+)\s*\n/i.exec(text);+ return match ? Number(match[1].replace(/,/g, '')) : null;+};++test.beforeAll(async () => {+ api = await playwrightRequest.newContext({+ ignoreHTTPSErrors: true,+ baseURL: API_URL,+ storageState: JSON.parse(readFileSync(AUTH_STATE, 'utf8')),+ });+ const stamp = Date.now();+ for (const [name, cohortCase] of Object.entries(CASES) as [+ CaseName,+ CohortCase,+ ][]) {+ cohorts[name] = await trpcMutation<Cohort>(api, 'cohort.create', {+ name: `E2E verify-a ${stamp} ${name}`,+ projectId: PROJECT,+ definition: cohortCase.definition,+ isStatic: cohortCase.isStatic ?? false,+ });+ }+ for (const name of Object.keys(CASES) as CaseName[]) {+ await expect+ .poll(async () => (await getCohort(cohorts[name].id)).lastComputedAt, {+ timeout: COMPUTE_TIMEOUT_MS,+ message: `${name} was never computed`,+ })+ .not.toBeNull();+ }+});++test.afterAll(async () => {+ for (const cohort of Object.values(cohorts)) {+ await api+ .post(`${API_URL}/trpc/cohort.delete`, {+ data: { json: { id: cohort.id } },+ })+ .catch(() => undefined);+ }+ await api.dispose();+});++test('member counts match a direct count of the events', async () => {+ for (const [name, cohortCase] of Object.entries(CASES) as [+ CaseName,+ CohortCase,+ ][]) {+ const expected = await clickhouseNumber(cohortCase.expected);+ const computed = await getCohort(cohorts[name].id);+ const stored = await clickhouseNumber(+ `SELECT count() FROM cohort_members FINAL WHERE project_id = '${PROJECT}' AND cohort_id = '${cohorts[name].id}'`+ );+ expect.soft(stored, `${name}: stored members`).toBe(computed.profileCount);+ expect.soft(computed.profileCount, `${name}: member count`).toBe(expected);+ }+});++test('detail page: overview, members, events, download and refresh', async ({+ page,+}) => {+ const problems = watchProblems(page);+ const cohort = cohorts.oneProperty;+ const expected = await clickhouseNumber(CASES.oneProperty.expected);+ await openHydrated(page, `${BASE}/cohorts/${cohort.id}`);+ await expect.poll(() => shownMemberCount(page)).toBe(expected);+ await expect(page.getByText('Popular events')).toBeVisible();+ await page.screenshot({+ path: `${SHOTS_DIR}/cohort-overview.png`,+ fullPage: true,+ });++ await page.getByRole('tab', { name: 'Members' }).click();+ await expect(page).toHaveURL(/\/members$/);+ await expect(page.locator('tbody tr')).toHaveCount(expected);++ await page.getByRole('tab', { name: 'Events' }).click();+ await expect(page).toHaveURL(/\/events$/);+ await expect(page.locator('[data-index]').first()).toBeVisible();++ const download = page.waitForEvent('download');+ await page.getByRole('button', { name: 'Download' }).click();+ const file = await download;+ const csv = readFileSync(await file.path(), 'utf8')+ .trim()+ .split('\n');+ expect(csv[0]).toBe('profile_id');+ expect(csv.length - 1).toBe(expected);++ await page.getByRole('button', { name: 'Refresh' }).click();+ await expect(page.getByText('Cohort refresh queued.')).toBeVisible();+ expect(problems).toEqual([]);+});++test('a frozen cohort is marked static and cannot be refreshed', async ({+ page,+}) => {+ const expected = await clickhouseNumber(CASES.frozen.expected);+ await openHydrated(page, `${BASE}/cohorts/${cohorts.frozen.id}`);+ await expect(page.getByText('Static', { exact: true })).toBeVisible();+ await expect.poll(() => shownMemberCount(page)).toBe(expected);+ await expect(page.getByRole('button', { name: 'Refresh' })).toHaveCount(0);+ await expect(page.getByRole('button', { name: 'Download' })).toBeVisible();+});++test('editing the criteria recomputes and the page shows the new count', async ({+ page,+}) => {+ const cohort = cohorts.anyOfTwoEvents;+ const both = await clickhouseNumber(+ `SELECT count() FROM (SELECT profile_id ${IDENTIFIED_EVENTS} AND name = 'checkout_started' INTERSECT SELECT profile_id ${IDENTIFIED_EVENTS} AND name = 'pricing_viewed')`+ );+ await openHydrated(page, `${BASE}/cohorts/${cohort.id}`);+ await page.getByRole('button', { name: 'Edit' }).click();+ const dialog = page.getByRole('dialog');+ await dialog.getByRole('button', { name: 'Any', exact: true }).click();+ await page.getByRole('menuitem').filter({ hasText: 'All' }).click();+ await dialog.getByRole('button', { name: 'Save changes' }).click();+ await expect(page.getByText('Cohort updated.')).toBeVisible();++ await expect+ .poll(async () => (await getCohort(cohort.id)).profileCount, {+ timeout: COMPUTE_TIMEOUT_MS,+ })+ .toBe(both);+ await expect+ .poll(() => shownMemberCount(page), {+ timeout: RECOMPUTE_VISIBLE_MS,+ message: 'the open page picks up the recomputed count without a reload',+ })+ .toBe(both);+});++test('profiles list: in cohort and not in cohort', async ({ page }) => {+ const cohort = cohorts.oneProperty;+ const members = await clickhouseNumber(CASES.oneProperty.expected);+ const identified = await clickhouseNumber(+ `SELECT count() FROM profiles FINAL WHERE project_id = '${PROJECT}' AND is_external`+ );+ const counts: number[] = [];+ page.on('response', async (response) => {+ if (response.url().includes('/trpc/profile.list')) {+ const body = await response.json().catch(() => null);+ counts.push(body?.result?.data?.json?.meta?.count);+ }+ });++ await openHydrated(page, `${BASE}/profiles`);+ await page+ .getByRole('button', { name: /Filters/ })+ .first()+ .click();+ await page.getByRole('button', { name: 'Add filter' }).click();+ await page.getByRole('menuitem', { name: 'Cohorts' }).click();+ await page.getByText('Select cohorts...').click();+ await page.getByPlaceholder('Search').last().fill(cohort.name);+ await page.getByText(cohort.name).last().click();+ await expect.poll(() => counts.at(-1)).toBe(members);+ await expect(page).toHaveURL(new RegExp(`inCohort,,${cohort.id}`));+ await page.keyboard.press('Escape');++ await page+ .getByRole('button', { name: 'In cohort', exact: true })+ .last()+ .click();+ await page.getByRole('menuitem', { name: 'Not in cohort' }).click();+ await expect.poll(() => counts.at(-1)).toBe(identified - members);+ await page.screenshot({ path: `${SHOTS_DIR}/profiles-not-in-cohort.png` });+});++test('overview: a cohort filter narrows the metrics to the cohort members', async ({+ page,+}) => {+ const cohort = cohorts.oneProperty;+ const memberVisitors = await clickhouseNumber(+ `SELECT uniqExact(profile_id) FROM sessions WHERE project_id = '${PROJECT}' AND created_at >= now() - INTERVAL 31 DAY AND profile_id IN (SELECT profile_id FROM cohort_members FINAL WHERE project_id = '${PROJECT}' AND cohort_id = '${cohort.id}')`+ );+ const stats = page.waitForResponse(+ (response) =>+ response.url().includes('/trpc/overview.stats') &&+ decodeURIComponent(response.url()).includes(cohort.id)+ );+ await openHydrated(page, `${BASE}?range=30d&f=cohort,inCohort,,${cohort.id}`);+ const body = await (await stats).json();+ await page.screenshot({+ path: `${SHOTS_DIR}/overview-cohort-filter.png`,+ fullPage: true,+ });+ expect(body.result.data.json.metrics.unique_visitors).toBe(memberVisitors);+});++test('deleting a cohort removes it from the list and drops its members', async ({+ page,+}) => {+ const cohort = cohorts.didEvent;+ await openHydrated(page, `${BASE}/cohorts/${cohort.id}`);+ await page.getByRole('button', { name: 'Delete' }).click();+ const confirm = page.getByRole('dialog');+ await expect(confirm).toContainText(cohort.name);+ await confirm.getByRole('button', { name: /Yes|Confirm|Delete/ }).click();+ await expect(page).toHaveURL(/\/cohorts$/);+ await expect(page.getByText(cohort.name)).toHaveCount(0);+ await expect+ .poll(() =>+ clickhouseNumber(+ `SELECT count() FROM cohort_members FINAL WHERE project_id = '${PROJECT}' AND cohort_id = '${cohort.id}'`+ )+ )+ .toBe(0);+});diff --git a/apps/start/e2e/verify-a-events-sessions.spec.ts b/apps/start/e2e/verify-a-events-sessions.spec.tsnew file mode 100644index 000000000..5104e5fd5--- /dev/null+++ b/apps/start/e2e/verify-a-events-sessions.spec.ts@@ -0,0 +1,71 @@+import { expect, test } from './fixtures';+import {+ clickhouseText,+ openHydrated,+ SHOTS_DIR,+ watchProblems,+} from './verify-a-helpers';++const SHOP = '/acme/acme-shop';+const EVENT_LIST_PAGE_SIZE = 50;+const FILTER_QUERY =+ 'events=add_to_cart&startDate=2026-09-25T00:00:00.000Z&endDate=2026-09-30T00:00:00.000Z';++test('switching between the Events, Conversions and Stats tabs keeps the active filters', async ({+ page,+}) => {+ const problems = watchProblems(page);+ await openHydrated(page, `${SHOP}/events/events?${FILTER_QUERY}`);+ await expect(page).toHaveURL(/events=add_to_cart/);++ for (const tab of ['Conversions', 'Stats', 'Events']) {+ await page.getByRole('tab', { name: tab }).click();+ await expect(page).toHaveURL(new RegExp(`/events/${tab.toLowerCase()}`));+ const search = new URL(page.url()).searchParams;+ expect+ .soft(search.get('events'), `event-name filter after opening ${tab}`)+ .toBe('add_to_cart');+ expect+ .soft(search.get('startDate'), `date range after opening ${tab}`)+ .not.toBeNull();+ }+ expect(problems).toEqual([]);+});++test('a session with more events than one page shows every event', async ({+ page,+}) => {+ const largest = await clickhouseText(+ `SELECT session_id, count() AS events FROM events WHERE project_id = 'acme-shop' AND session_id != '' GROUP BY session_id HAVING events > ${EVENT_LIST_PAGE_SIZE} ORDER BY events DESC LIMIT 1`+ );+ test.skip(+ largest === '',+ `no acme-shop session has more than ${EVENT_LIST_PAGE_SIZE} events; send some through /track first`+ );+ const [sessionId, eventCount] = largest.split('\t');++ await openHydrated(page, `${SHOP}/sessions/${sessionId}`);+ await expect(page.getByText('Session info')).toBeVisible();+ const eventsWidget = page+ .locator('div')+ .filter({ has: page.getByText('Events', { exact: true }) })+ .filter({ has: page.locator('.divide-y') })+ .last();+ const timelineRows = eventsWidget.locator('.divide-y > div');+ await expect(timelineRows.first()).toBeVisible();+ await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));+ await page.screenshot({+ path: `${SHOTS_DIR}/session-over-one-page.png`,+ fullPage: true,+ });++ const hasPagination =+ (await page+ .getByRole('button', { name: /load more|show more|next/i })+ .count()) > 0;+ const shown = await timelineRows.count();+ expect(+ shown === Number(eventCount) || hasPagination,+ `timeline shows ${shown} of ${eventCount} events and offers no pagination`+ ).toBe(true);+});diff --git a/apps/start/e2e/verify-a-helpers.ts b/apps/start/e2e/verify-a-helpers.tsnew file mode 100644index 000000000..b4dc92c67--- /dev/null+++ b/apps/start/e2e/verify-a-helpers.ts@@ -0,0 +1,120 @@+import type { APIRequestContext, Page } from '@playwright/test';++export const API_URL =+ process.env.API_URL ?? 'https://api.main.local.openpanel.cc';+export const SHOTS_DIR = 'test-results/verify-a/shots';++const DEFAULT_CLICKHOUSE_URL = 'http://localhost:23123/openpanel';+const HYDRATION_TIMEOUT_MS = 45_000;++/** Read-only ClickHouse query over HTTP; returns the first cell as a number. */+export async function clickhouseNumber(query: string): Promise<number> {+ const configured = new URL(+ process.env.CLICKHOUSE_URL ?? DEFAULT_CLICKHOUSE_URL+ );+ const database = configured.pathname.replace('/', '') || 'default';+ const response = await fetch(+ `${configured.origin}/?database=${database}&readonly=1`,+ { method: 'POST', body: `${query} FORMAT TSV` }+ );+ const text = await response.text();+ if (!response.ok) {+ throw new Error(`ClickHouse ${response.status}: ${text.slice(0, 300)}`);+ }+ return Number(text.trim().split('\t')[0]);+}++export async function clickhouseText(query: string): Promise<string> {+ const configured = new URL(+ process.env.CLICKHOUSE_URL ?? DEFAULT_CLICKHOUSE_URL+ );+ const database = configured.pathname.replace('/', '') || 'default';+ const response = await fetch(+ `${configured.origin}/?database=${database}&readonly=1`,+ { method: 'POST', body: `${query} FORMAT TSV` }+ );+ return (await response.text()).trim();+}++export async function trpcQuery<T>(+ request: APIRequestContext,+ procedure: string,+ input?: unknown+): Promise<T> {+ const query =+ input === undefined+ ? ''+ : `?input=${encodeURIComponent(JSON.stringify({ json: input }))}`;+ const response = await request.get(`${API_URL}/trpc/${procedure}${query}`);+ const body = await response.json();+ if (!response.ok()) {+ throw new Error(+ `${procedure} ${response.status()}: ${JSON.stringify(body).slice(0, 300)}`+ );+ }+ return body.result.data.json as T;+}++export async function trpcMutation<T>(+ request: APIRequestContext,+ procedure: string,+ input?: unknown+): Promise<T> {+ const response = await request.post(`${API_URL}/trpc/${procedure}`, {+ data: { json: input ?? null },+ });+ const body = await response.json();+ if (!response.ok()) {+ throw new Error(+ `${procedure} ${response.status()}: ${JSON.stringify(body).slice(0, 300)}`+ );+ }+ return body.result.data.json as T;+}++/** The feedback card covers the bottom-right corner for accounts that have not dismissed it. */+export async function hideFeedbackPrompt(page: Page): Promise<void> {+ await page.context().addCookies([+ {+ name: 'feedback-prompt-seen',+ value: new Date().toISOString(),+ url: process.env.DASHBOARD_URL ?? 'http://localhost:3000',+ },+ ]);+}++/** `networkidle` is reached before React attaches on the dev server; a click before that is lost. */+export async function openHydrated(page: Page, path: string): Promise<void> {+ await hideFeedbackPrompt(page);+ await page.goto(path);+ await page.waitForLoadState('networkidle');+ await page.waitForFunction(+ () => {+ const probe = document.querySelector('button, a');+ return (+ probe !== null &&+ Object.keys(probe).some((key) => key.startsWith('__reactFiber$'))+ );+ },+ undefined,+ { timeout: HYDRATION_TIMEOUT_MS }+ );+}++/** Uncaught errors and failed API calls; module 502s and analytics noise are not the page's fault. */+export function watchProblems(page: Page): string[] {+ const problems: string[] = [];+ page.on('pageerror', (error) => {+ if (!/hydrat/i.test(error.message)) {+ problems.push(`pageerror: ${error.message.slice(0, 300)}`);+ }+ });+ page.on('response', (response) => {+ if (response.status() >= 400 && response.url().includes('/trpc/')) {+ problems.push(+ `${response.status()} ${decodeURIComponent(response.url()).slice(0, 240)}`+ );+ }+ });+ return problems;+}diff --git a/apps/start/e2e/verify-a-links.spec.ts b/apps/start/e2e/verify-a-links.spec.tsnew file mode 100644index 000000000..1ba9c1bb2--- /dev/null+++ b/apps/start/e2e/verify-a-links.spec.ts@@ -0,0 +1,43 @@+import { expect, test } from './fixtures';+import { openHydrated, SHOTS_DIR } from './verify-a-helpers';++const GSC_DOCS_URL =+ 'https://openpanel.dev/docs/self-hosting/google-search-console';+const SUPPORTER_URL = /^https:\/\/buy\.polar\.sh\//;+// seroval encodes `false` as {"t":2,"s":3} and `true` as {"t":2,"s":2}; the+// first boolean in the env payload is `isSelfHosted`.+const SEROVAL_FALSE = '{"t":2,"s":3}';+const SEROVAL_TRUE = '{"t":2,"s":2}';++test('the Search Console setup guide button links to the docs', async ({+ page,+}) => {+ // The button only renders when the instance has no Google OAuth client.+ await page.route('**/trpc/auth.providers**', async (route) => {+ const response = await route.fetch();+ const body = (await response.text()).replace('"gsc":true', '"gsc":false');+ await route.fulfill({ response, body });+ });+ await openHydrated(page, '/acme/acme-shop/settings/gsc');+ const link = page.getByRole('link', { name: 'Read the setup guide' });+ await expect(link).toBeVisible();+ await page.screenshot({ path: `${SHOTS_DIR}/gsc-setup-guide.png` });+ await expect(link).toHaveAttribute('href', GSC_DOCS_URL);+});++test('the self-hosted supporter button links to the checkout page', async ({+ page,+}) => {+ await page.route('**/_serverFn/**getServerEnvs**', async (route) => {+ const response = await route.fetch();+ const body = (await response.text()).replace(SEROVAL_FALSE, SEROVAL_TRUE);+ await route.fulfill({ response, body });+ });+ await page.context().clearCookies({ name: 'supporter-prompt-closed' });+ await page.goto('/acme');+ await page.waitForLoadState('networkidle');+ const link = page.getByRole('link', { name: 'Become a Supporter' });+ await expect(link).toBeVisible({ timeout: 30_000 });+ await page.screenshot({ path: `${SHOTS_DIR}/supporter-prompt.png` });+ await expect(link).toHaveAttribute('href', SUPPORTER_URL);+});diff --git a/apps/start/e2e/verify-a-members-menu.spec.ts b/apps/start/e2e/verify-a-members-menu.spec.tsnew file mode 100644index 000000000..15885e8dd--- /dev/null+++ b/apps/start/e2e/verify-a-members-menu.spec.ts@@ -0,0 +1,33 @@+import { expect, test } from './fixtures';+import { openHydrated } from './verify-a-helpers';++const IDLE_MS = 2000;++interface RouterWithQueryClient {+ options: {+ context: { queryClient: { invalidateQueries: () => Promise<void> } };+ };+}++test('an open member row menu stays open when the members table refetches', async ({+ page,+ seed,+}) => {+ await openHydrated(page, `/${seed.organizationId}/members/members`);+ const row = page.getByRole('row', { name: new RegExp(seed.user.email) });+ await row.getByRole('button').click();+ const menu = page.getByRole('menu');+ await expect(menu).toBeVisible();+ await page.waitForTimeout(IDLE_MS);+ await expect(menu, 'menu stays open while nothing happens').toBeVisible();++ // What an invite, a removed member or a window refocus does to the table.+ await page.evaluate(() =>+ (+ window as { __TSR_ROUTER__?: RouterWithQueryClient }+ ).__TSR_ROUTER__?.options.context.queryClient.invalidateQueries()+ );+ await page.waitForTimeout(IDLE_MS);+ await expect(menu, 'menu after the table refetched').toBeVisible();+ await page.keyboard.press('Escape');+});diff --git a/apps/start/e2e/verify-a-org.spec.ts b/apps/start/e2e/verify-a-org.spec.tsnew file mode 100644index 000000000..5a2820096--- /dev/null+++ b/apps/start/e2e/verify-a-org.spec.ts@@ -0,0 +1,108 @@+import { request as playwrightRequest } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ API_URL,+ openHydrated,+ SHOTS_DIR,+ trpcMutation,+ trpcQuery,+} from './verify-a-helpers';++const PASSWORD = 'Verify-a-pass-1!';+const VISIBLE_CARDS_BUDGET_MS = 10_000;++interface Project {+ id: string;+ deleteAt: string | null;+}++test('cancelling an organization deletion keeps a project that was already scheduled for deletion', async () => {+ const stamp = Date.now();+ const owner = await playwrightRequest.newContext({+ ignoreHTTPSErrors: true,+ baseURL: API_URL,+ });+ await trpcMutation(owner, 'auth.signUpEmail', {+ firstName: 'E2E',+ lastName: 'VerifyA',+ email: `e2e-verify-a-${stamp}@example.com`,+ password: PASSWORD,+ confirmPassword: PASSWORD,+ });+ const project = {+ domain: null,+ cors: [],+ website: false,+ app: false,+ backend: true,+ };+ await trpcMutation(owner, 'onboarding.project', {+ ...project,+ organization: `E2E verify-a ${stamp}`,+ project: `E2E verify-a keep ${stamp}`,+ timezone: 'UTC',+ });+ const [organization] = await trpcQuery<{ id: string }[]>(+ owner,+ 'organization.list'+ );+ const organizationId = organization.id;+ const doomed = await trpcMutation<Project>(owner, 'project.create', {+ ...project,+ organizationId,+ project: `E2E verify-a doomed ${stamp}`,+ });+ const listProjects = () =>+ trpcQuery<Project[]>(owner, 'project.list', { organizationId });+ const deleteAtOf = async (projectId: string) =>+ (await listProjects()).find((item) => item.id === projectId)?.deleteAt ??+ null;++ try {+ await trpcMutation(owner, 'project.delete', { projectId: doomed.id });+ expect(await deleteAtOf(doomed.id)).not.toBeNull();++ await trpcMutation(owner, 'organization.delete', { organizationId });+ await trpcMutation(owner, 'organization.cancelDeletion', {+ organizationId,+ });++ const projects = await listProjects();+ const kept = projects.find((item) => item.id !== doomed.id);+ expect(kept?.deleteAt, 'the other project is restored').toBeNull();+ expect(+ await deleteAtOf(doomed.id),+ 'the project deleted on its own is still scheduled for deletion'+ ).not.toBeNull();+ } finally {+ await trpcMutation(owner, 'organization.delete', { organizationId });+ await trpcMutation(owner, 'user.delete');+ await owner.dispose();+ }+});++test('the projects page fills every visible card and loads the rest on scroll', async ({+ page,+ seed,+}) => {+ await openHydrated(page, `/${seed.organizationId}`);+ const visibleSkeletonCards = () =>+ page.evaluate(+ () =>+ Array.from(document.querySelectorAll('.card')).filter((card) => {+ const box = card.getBoundingClientRect();+ const isVisible = box.bottom > 0 && box.top < window.innerHeight;+ return isVisible && card.querySelector('.animate-pulse') !== null;+ }).length+ );+ await expect+ .poll(visibleSkeletonCards, { timeout: VISIBLE_CARDS_BUDGET_MS })+ .toBe(0);+ await page.screenshot({ path: `${SHOTS_DIR}/projects-top.png` });++ await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));+ await expect+ .poll(visibleSkeletonCards, { timeout: VISIBLE_CARDS_BUDGET_MS })+ .toBe(0);+ await page.screenshot({ path: `${SHOTS_DIR}/projects-bottom.png` });+});diff --git a/apps/start/e2e/verify-a-redirect.spec.ts b/apps/start/e2e/verify-a-redirect.spec.tsnew file mode 100644index 000000000..5750f1ab5--- /dev/null+++ b/apps/start/e2e/verify-a-redirect.spec.ts@@ -0,0 +1,87 @@+import type { Page } from '@playwright/test';+import { expect, test } from './fixtures';++const SAAS = '/acme/acme-saas';+const SETTLE_MS = 5000;+const SLOW_SERVER_FN_MS = 1500;+const CLICK_GAP_MS = 700;++function pathTrail(page: Page) {+ const trail: string[] = [];+ page.on('framenavigated', (frame) => {+ if (frame === page.mainFrame()) {+ trail.push(new URL(frame.url()).pathname);+ }+ });+ return trail;+}++async function openHydrated(page: Page, path: string) {+ await page.goto(path);+ await page.waitForLoadState('networkidle');+ // The project switcher only gets its label once the client has hydrated.+ await expect(page.getByRole('combobox').first()).toContainText('Acme', {+ timeout: 45_000,+ });+}++test.describe('overlapping navigations never bounce a signed-in user through /login', () => {+ test('Back pressed twice in a row returns two pages back', async ({+ page,+ }) => {+ const trail = pathTrail(page);+ await openHydrated(page, `${SAAS}/events`);+ await page.getByRole('link', { name: 'Groups', exact: true }).click();+ await expect(page).toHaveURL(/\/groups$/);+ await page.getByRole('link', { name: 'Cohorts', exact: true }).click();+ await expect(page).toHaveURL(/\/cohorts$/);+ await page.goBack();+ await page.goBack();+ await page.waitForTimeout(SETTLE_MS);+ expect(trail).not.toContain('/login');+ await expect(page).toHaveURL(/\/acme\/acme-saas\/events/);+ });++ test('session detail -> reload -> profile -> Back -> Back returns to the sessions list', async ({+ page,+ }) => {+ const trail = pathTrail(page);+ await openHydrated(page, `${SAAS}/sessions`);+ await page+ .locator('[data-index]')+ .first()+ .locator('a[href*="/sessions/"]')+ .first()+ .click();+ await expect(page.getByText('Session info')).toBeVisible();+ await page.reload();+ await page.waitForLoadState('networkidle');+ await expect(page.getByText('Session info')).toBeVisible();+ await page.getByRole('link', { name: /@/ }).click();+ await expect(page).toHaveURL(/\/profiles\/[^/]+$/);+ await page.goBack();+ await page.goBack();+ await page.waitForTimeout(SETTLE_MS);+ expect(trail).not.toContain('/login');+ await expect(page).toHaveURL(/\/acme\/acme-saas\/sessions$/);+ });++ test('sidebar navigation on a slow connection stays in the project', async ({+ page,+ }) => {+ const trail = pathTrail(page);+ await openHydrated(page, `${SAAS}/sessions`);+ // The root route awaits this server function on every navigation.+ await page.route('**/_serverFn/**getCookiesFn**', async (route) => {+ await new Promise((resolve) => setTimeout(resolve, SLOW_SERVER_FN_MS));+ await route.continue();+ });+ for (const name of ['Events', 'Groups', 'Cohorts', 'Profiles', 'Pages']) {+ await page.getByRole('link', { name, exact: true }).click();+ await page.waitForTimeout(CLICK_GAP_MS);+ }+ await page.waitForTimeout(SETTLE_MS + SLOW_SERVER_FN_MS);+ expect(trail).not.toContain('/login');+ await expect(page).toHaveURL(/\/acme\/acme-saas\/pages$/);+ });+});diff --git a/apps/start/e2e/verify-b-dashboard-range.spec.ts b/apps/start/e2e/verify-b-dashboard-range.spec.tsnew file mode 100644index 000000000..47be9a66a--- /dev/null+++ b/apps/start/e2e/verify-b-dashboard-range.spec.ts@@ -0,0 +1,107 @@+import type { Page } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ eventSeries,+ hideFeedbackPrompt,+ reportInput,+ trpcMutation,+} from './reports-helpers';+import { shot } from './verify-b-helpers';++const SHOP = 'acme-shop';+const REPORT_NAME = 'E2E verify-b saved 30d week';++interface ChartCall {+ range: string;+ interval: string;+}++function recordChartCalls(page: Page) {+ const calls: ChartCall[] = [];+ page.on('request', (request) => {+ if (!request.url().includes('/trpc/chart.chart')) {+ return;+ }+ const input = JSON.parse(+ new URL(request.url()).searchParams.get('input') ?? '{}'+ ).json;+ calls.push({ range: input.range, interval: input.interval });+ });+ return calls;+}++async function rangeCookie(page: Page) {+ const cookies = await page.context().cookies();+ return cookies.find((cookie) => cookie.name === 'range')?.value;+}++test.use({ actionTimeout: 15_000 });++test('a dashboard card uses the range and interval saved on its report', async ({+ page,+}) => {+ test.setTimeout(180_000);+ await hideFeedbackPrompt(page);+ await page.goto('/login');+ const dashboard = await trpcMutation<{ id: string }>(+ page,+ 'dashboard.create',+ {+ name: `E2E verify-b range ${Date.now()}`,+ projectId: SHOP,+ }+ );+ try {+ const { projectId: _projectId, ...report } = reportInput(SHOP, {+ series: eventSeries(['purchase']),+ range: '30d',+ interval: 'week',+ });+ await trpcMutation(page, 'report.create', {+ dashboardId: dashboard.id,+ report: { ...report, name: REPORT_NAME },+ });+ expect(await rangeCookie(page), 'no range cookie yet').toBeUndefined();++ const calls = recordChartCalls(page);+ await page.goto(`/acme/${SHOP}/dashboards/${dashboard.id}`);+ await expect(page.getByText(REPORT_NAME)).toBeVisible({ timeout: 30_000 });+ await expect+ .poll(() => calls.length, { timeout: 30_000 })+ .toBeGreaterThan(0);+ await page.waitForLoadState('networkidle');+ await shot(page, 'item8-dashboard-fresh', false);+ const fresh = { ...calls[0] };++ // Choosing a range on the overview page changes what the dashboard opens with.+ await page.goto(`/acme/${SHOP}`);+ await page.waitForLoadState('networkidle');+ await page+ .getByRole('button', { name: 'Last 7 days', exact: true })+ .click();+ await page.getByRole('menuitem', { name: /^Last 6 months/ }).click();+ await expect.poll(() => rangeCookie(page)).toContain('6m');+ calls.length = 0;+ await page.goto(`/acme/${SHOP}/dashboards/${dashboard.id}`);+ await expect(page.getByText(REPORT_NAME)).toBeVisible({ timeout: 30_000 });+ await expect+ .poll(() => calls.length, { timeout: 30_000 })+ .toBeGreaterThan(0);+ await page.waitForLoadState('networkidle');+ await shot(page, 'item8-dashboard-after-overview-6m', false);+ const afterOverview = { ...calls[0] };++ expect.soft(fresh, 'fresh visit, no cookie').toEqual({+ range: '30d',+ interval: 'week',+ });+ expect+ .soft(afterOverview, 'after picking 6 months on the overview page')+ .toEqual({ range: '30d', interval: 'week' });+ } finally {+ await trpcMutation(page, 'dashboard.delete', {+ id: dashboard.id,+ forceDelete: true,+ });+ }+});diff --git a/apps/start/e2e/verify-b-funnels.spec.ts b/apps/start/e2e/verify-b-funnels.spec.tsnew file mode 100644index 000000000..fd9bbb2e7--- /dev/null+++ b/apps/start/e2e/verify-b-funnels.spec.ts@@ -0,0 +1,186 @@+import type { Page } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ eventSeries,+ hideFeedbackPrompt,+ reportInput,+ trpcMutation,+ trpcQuery,+ waitForChart,+} from './reports-helpers';+import { shot } from './verify-b-helpers';++interface FunnelSeries {+ breakdowns: string[];+ steps: { count: number; event: { name: string } }[];+}++const SIGNUP = [+ 'screen_view',+ 'plan_selected',+ 'signup_started',+ 'signup_completed',+];+const CHECKOUT = [+ 'product_viewed',+ 'add_to_cart',+ 'checkout_started',+ 'shipping_info_added',+ 'payment_info_added',+ 'purchase',+];++function funnelInput(+ projectId: string,+ steps: string[],+ overrides: Record<string, unknown>+) {+ return reportInput(projectId, {+ chartType: 'funnel',+ series: eventSeries(steps),+ ...overrides,+ });+}++/** Saves a funnel report, opens it, and returns the rendered text with the API result. */+async function openFunnel(+ page: Page,+ projectId: string,+ name: string,+ input: Record<string, unknown>+) {+ const dashboard = await trpcMutation<{ id: string }>(+ page,+ 'dashboard.create',+ {+ name: `E2E verify-b funnel ${Date.now()}`,+ projectId,+ }+ );+ const { projectId: _projectId, ...report } = input;+ const created = await trpcMutation<{ id: string }>(page, 'report.create', {+ dashboardId: dashboard.id,+ report: { ...report, name },+ });+ const api = await trpcQuery<{ current: FunnelSeries[] }>(+ page,+ 'chart.funnel',+ input+ );+ await page.goto(`/acme/${projectId}/reports/${created.id}`);+ await page.waitForLoadState('networkidle');+ await waitForChart(page);+ const text = (await page.locator('#report-editor').innerText()).replace(+ /\s+/g,+ ' '+ );+ return { dashboardId: dashboard.id, api: api.current, text };+}++test.use({ actionTimeout: 15_000 });++test.beforeEach(async ({ page }) => {+ test.setTimeout(180_000);+ await hideFeedbackPrompt(page);+ await page.setViewportSize({ width: 1440, height: 1200 });+ await page.goto('/login');+});++test('acme-web signup funnel grouped by profile reaches signup_completed', async ({+ page,+}) => {+ const session = await trpcQuery<{ current: FunnelSeries[] }>(+ page,+ 'chart.funnel',+ funnelInput('acme-web', SIGNUP, {+ options: { type: 'funnel', funnelGroup: 'session_id' },+ })+ );+ const opened = await openFunnel(+ page,+ 'acme-web',+ 'E2E verify-b signup by profile',+ funnelInput('acme-web', SIGNUP, {+ options: { type: 'funnel', funnelGroup: 'profile_id' },+ })+ );+ try {+ await shot(page, 'item10-signup-profile-group');+ const counts = opened.api[0]?.steps.map((step) => step.count) ?? [];+ test.info().annotations.push({+ type: 'counts',+ description: `profile: ${counts.join(' -> ')}; session: ${session.current[0]?.steps.map((s) => s.count).join(' -> ')}`,+ });+ // The page shows what the API returned.+ for (const [index, step] of SIGNUP.entries()) {+ expect(opened.text).toContain(+ `${step} ${(counts[index] ?? 0).toLocaleString('en-US')} `+ );+ }+ // 48 sessions complete the signup; the people behind them did too.+ expect(session.current[0]?.steps.at(-1)?.count).toBeGreaterThan(0);+ expect(counts.at(-1), 'profiles completing signup').toBeGreaterThan(0);+ } finally {+ await trpcMutation(page, 'dashboard.delete', {+ id: opened.dashboardId,+ forceDelete: true,+ });+ }+});++test('acme-shop checkout funnel breaks down by payment method', async ({+ page,+}) => {+ const opened = await openFunnel(+ page,+ 'acme-shop',+ 'E2E verify-b checkout by payment',+ funnelInput('acme-shop', CHECKOUT, {+ breakdowns: [{ id: 'b', name: 'properties.payment' }],+ })+ );+ try {+ await shot(page, 'item10-checkout-by-payment');+ const labels = opened.api.map((serie) => serie.breakdowns.join('/'));+ test.info().annotations.push({+ type: 'breakdowns',+ description: labels.join(', '),+ });+ expect(opened.text).toContain('Not set');+ // purchase events carry payment = card / paypal / …+ expect(+ labels.filter((label) => label !== 'Not set').length+ ).toBeGreaterThan(0);+ } finally {+ await trpcMutation(page, 'dashboard.delete', {+ id: opened.dashboardId,+ forceDelete: true,+ });+ }+});++test('acme-shop checkout funnel breaks down by category (carried by step 1)', async ({+ page,+}) => {+ const opened = await openFunnel(+ page,+ 'acme-shop',+ 'E2E verify-b checkout by category',+ funnelInput('acme-shop', CHECKOUT, {+ breakdowns: [{ id: 'b', name: 'properties.category' }],+ })+ );+ try {+ await shot(page, 'item10-checkout-by-category');+ const labels = opened.api.map((serie) => serie.breakdowns.join('/'));+ expect(labels.length).toBeGreaterThan(3);+ for (const label of labels.slice(0, 3)) {+ expect(opened.text).toContain(label);+ }+ } finally {+ await trpcMutation(page, 'dashboard.delete', {+ id: opened.dashboardId,+ forceDelete: true,+ });+ }+});diff --git a/apps/start/e2e/verify-b-helpers.ts b/apps/start/e2e/verify-b-helpers.tsnew file mode 100644index 000000000..1e0d9cef1--- /dev/null+++ b/apps/start/e2e/verify-b-helpers.ts@@ -0,0 +1,66 @@+import type { Browser, Page } from '@playwright/test';++export const SHOTS_DIR = 'test-results/verify-b/shots';+export const API_URL = 'https://api.main.local.openpanel.cc';+export const DASHBOARD_URL = 'https://main.local.openpanel.cc';++export function shot(page: Page, name: string, fullPage = true) {+ return page.screenshot({ path: `${SHOTS_DIR}/${name}.png`, fullPage });+}++export interface Traffic {+ bad: string[];+ consoleErrors: string[];+ pageErrors: string[];+}++/** Records >= 400 API responses, console errors and uncaught exceptions. */+export function watch(page: Page): Traffic {+ const traffic: Traffic = { bad: [], consoleErrors: [], pageErrors: [] };+ page.on('response', (response) => {+ if (+ response.status() >= 400 &&+ !/favicon|sentry|api\.openpanel\.dev/.test(response.url())+ ) {+ const url = new URL(response.url());+ traffic.bad.push(`${response.status()} ${url.pathname}`);+ }+ });+ page.on('console', (message) => {+ if (message.type() === 'error') {+ traffic.consoleErrors.push(message.text().slice(0, 400));+ }+ });+ page.on('pageerror', (error) => {+ traffic.pageErrors.push(String(error).slice(0, 400));+ });+ return traffic;+}++export async function anonymousPage(browser: Browser) {+ const context = await browser.newContext({+ ignoreHTTPSErrors: true,+ baseURL: DASHBOARD_URL,+ storageState: { cookies: [], origins: [] },+ viewport: { width: 1440, height: 1000 },+ });+ const page = await context.newPage();+ return { context, page, traffic: watch(page) };+}++/**+ * Opens a share by a client-side route change from an already hydrated page;+ * a direct document load of an enabled share does not answer on the dev stack.+ */+export async function openShareClientSide(page: Page, path: string) {+ await page.goto('/login');+ await page.waitForLoadState('networkidle');+ await page.evaluate((target) => {+ window.history.pushState({}, '', target);+ window.dispatchEvent(new PopStateEvent('popstate'));+ }, path);+}++export function trpcUrl(procedure: string, input: unknown) {+ return `${API_URL}/trpc/${procedure}?input=${encodeURIComponent(JSON.stringify({ json: input }))}`;+}diff --git a/apps/start/e2e/verify-b-hydration.spec.ts b/apps/start/e2e/verify-b-hydration.spec.tsnew file mode 100644index 000000000..349a8984d--- /dev/null+++ b/apps/start/e2e/verify-b-hydration.spec.ts@@ -0,0 +1,53 @@+import type { Page } from '@playwright/test';+import { expect, test } from './fixtures';++const ROUTE = '/acme/acme-web/dashboards';+const PREAMBLE =+ /<script type="module" async="">import \{ injectIntoGlobalHook \}[\s\S]*?<\/script>/;++function hydrationWarnings(page: Page) {+ const warnings: string[] = [];+ page.on('console', (message) => {+ if (message.text().includes('hydrated but some attributes')) {+ warnings.push(message.text().slice(0, 80));+ }+ });+ return warnings;+}++test('a document load hydrates without an attribute mismatch', async ({+ page,+}) => {+ const warnings = hydrationWarnings(page);+ await page.goto(ROUTE);+ await page.waitForLoadState('networkidle');+ await page.waitForTimeout(1500);+ expect(warnings).toEqual([]);+});++// The mismatch is the dev server's React Refresh preamble, which <Scripts />+// renders into <body> on the server only: the next <script> in the root+// document hydrates against it. With the preamble out of <body> it is gone.+test('the mismatch disappears when the React Refresh preamble is not in <body>', async ({+ page,+}) => {+ const warnings = hydrationWarnings(page);+ let preambleInBody = false;+ await page.route(`**${ROUTE}`, async (route) => {+ const response = await route.fetch();+ const html = await response.text();+ const preamble = html.match(PREAMBLE)?.[0];+ preambleInBody = Boolean(preamble);+ await route.fulfill({+ response,+ body: preamble+ ? html.replace(preamble, '').replace('</head>', `${preamble}</head>`)+ : html,+ });+ });+ await page.goto(ROUTE);+ await page.waitForLoadState('networkidle');+ await page.waitForTimeout(1500);+ test.skip(!preambleInBody, 'not a dev server: no preamble to move');+ expect(warnings).toEqual([]);+});diff --git a/apps/start/e2e/verify-b-interactions.spec.ts b/apps/start/e2e/verify-b-interactions.spec.tsnew file mode 100644index 000000000..40b37f4ab--- /dev/null+++ b/apps/start/e2e/verify-b-interactions.spec.ts@@ -0,0 +1,363 @@+import type { Locator, Page } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ eventSeries,+ hideFeedbackPrompt,+ reportInput,+ trpcMutation,+ trpcQuery,+ waitForChart,+} from './reports-helpers';+import { shot, watch } from './verify-b-helpers';++const SHOP = 'acme-shop';++async function createDashboard(page: Page, label: string) {+ return trpcMutation<{ id: string }>(page, 'dashboard.create', {+ name: `E2E verify-b ${label} ${Date.now()}`,+ projectId: SHOP,+ });+}++async function createReport(+ page: Page,+ dashboardId: string,+ name: string,+ overrides: Record<string, unknown>+) {+ const { projectId: _projectId, ...report } = reportInput(SHOP, overrides);+ return trpcMutation<{ id: string }>(page, 'report.create', {+ dashboardId,+ report: { ...report, name },+ });+}++function deleteDashboard(page: Page, id: string) {+ return trpcMutation(page, 'dashboard.delete', { id, forceDelete: true });+}++/** The report table is a virtualised div grid: one checkbox per series row. */+function tableRows(editor: Locator) {+ return editor.getByRole('checkbox').evaluateAll((boxes) =>+ boxes.map((box) => {+ let row: HTMLElement | null = box as HTMLElement;+ while (row && row.innerText.split('\n').filter(Boolean).length < 5) {+ row = row.parentElement;+ }+ const cells = (row?.innerText ?? '').split('\n').filter(Boolean);+ return {+ checked: box.getAttribute('data-state') === 'checked',+ serie: cells[0] ?? '',+ device: cells[1] ?? '',+ sum: Number(cells[3]),+ };+ })+ );+}++const drawnLines = (editor: Locator) =>+ editor.locator('path.recharts-line-curve').count();++test.use({ actionTimeout: 15_000 });++test.beforeEach(async ({ page }) => {+ test.setTimeout(240_000);+ await hideFeedbackPrompt(page);+ await page.setViewportSize({ width: 1440, height: 1100 });+ await page.goto('/login');+});++test('report page: view users from a chart point, series toggles, sorting, search', async ({+ page,+}) => {+ const traffic = watch(page);+ const dashboard = await createDashboard(page, 'interactions');+ try {+ const series = eventSeries(['purchase', 'add_to_cart']);+ const report = await createReport(+ page,+ dashboard.id,+ 'E2E verify-b interactions',+ {+ series,+ breakdowns: [{ id: 'b', name: 'device' }],+ }+ );+ await page.goto(`/acme/${SHOP}/reports/${report.id}`);+ await page.waitForLoadState('networkidle');+ await waitForChart(page);+ const editor = page.locator('#report-editor');++ // --- view users from a chart point+ const surface = editor.locator('svg.recharts-surface').first();+ const box = await surface.boundingBox();+ if (!box) {+ throw new Error('chart not drawn');+ }+ const profilesAnswer = page.waitForResponse((response) =>+ response.url().includes('/trpc/chart.getProfiles')+ );+ await page.mouse.click(box.x + box.width * 0.9, box.y + box.height * 0.5);+ await page.getByRole('menuitem', { name: 'View Users' }).click();+ const profilesResponse = await profilesAnswer;+ expect(profilesResponse.status()).toBe(200);+ const profiles = (await profilesResponse.json()).result.data.json as {+ id: string;+ }[];+ const dialog = page.getByRole('dialog');+ await expect(dialog.getByText('View Users')).toBeVisible();+ await expect(+ dialog.getByText(/Users who performed actions on/)+ ).toBeVisible();+ expect(profiles.length).toBeGreaterThan(0);+ await expect(dialog.locator('a[href*="/profiles/"]').first()).toBeVisible();+ await shot(page, 'item13-view-users', false);+ const firstHref = await dialog+ .locator('a[href*="/profiles/"]')+ .first()+ .getAttribute('href');+ expect(firstHref).toContain(`/acme/${SHOP}/profiles/`);+ await page.keyboard.press('Escape');+ await expect(dialog).toHaveCount(0);++ // --- series toggles (the chart legend itself is not interactive)+ const before = await tableRows(editor);+ const checkedBefore = before.filter((row) => row.checked).length;+ // Each visible series draws one curve; the chart adds one helper curve.+ await expect.poll(() => drawnLines(editor)).toBe(checkedBefore + 1);+ await editor.getByRole('checkbox').first().click();+ await expect.poll(() => drawnLines(editor)).toBe(checkedBefore);+ await editor.getByRole('checkbox').first().click();+ await expect.poll(() => drawnLines(editor)).toBe(checkedBefore + 1);+ await editor.getByRole('button', { name: 'Unselect All' }).click();+ await expect.poll(() => drawnLines(editor)).toBe(0);+ await shot(page, 'item13-unselect-all');+ expect((await tableRows(editor)).filter((row) => row.checked)).toEqual([]);+ for (const index of [0, 1]) {+ await editor.getByRole('checkbox').nth(index).click();+ }+ await expect.poll(() => drawnLines(editor)).toBe(3);++ // --- sorting on Sum, in the default flat view+ const sumHeader = editor.getByRole('button', { name: /^Sum/i }).first();+ const sortedSums: number[][] = [];+ for (let click = 0; click < 2; click++) {+ await sumHeader.click();+ await page.waitForTimeout(400);+ sortedSums.push((await tableRows(editor)).map((row) => row.sum));+ }+ await shot(page, 'item13-sorted');+ const ascending = [...(sortedSums[0] ?? [])].sort((a, b) => a - b);+ const descending = [...ascending].reverse();+ const orders = sortedSums.map((sums) => JSON.stringify(sums));+ expect(orders).toContain(JSON.stringify(ascending));+ expect(orders).toContain(JSON.stringify(descending));++ // --- sums match the API+ const api = await trpcQuery<{+ series: { names: string[]; metrics: { sum: number } }[];+ }>(page, 'chart.chart', {+ ...reportInput(SHOP, {+ series,+ breakdowns: [{ id: 'b', name: 'device' }],+ }),+ });+ expect([...(sortedSums[0] ?? [])].sort((a, b) => a - b)).toEqual(+ api.series.map((serie) => serie.metrics.sum).sort((a, b) => a - b)+ );++ // --- grouped view folds the rows under their series+ await editor.getByRole('button', { name: 'Flat', exact: true }).click();+ await expect(+ editor.getByRole('button', { name: 'Grouped', exact: true })+ ).toBeVisible();+ await shot(page, 'item13-grouped');+ await editor.getByRole('button', { name: 'Grouped', exact: true }).click();++ // --- search narrows the rows+ await editor.getByPlaceholder('Search...').fill('tablet');+ await expect+ .poll(async () => (await tableRows(editor)).map((row) => row.device))+ .toEqual(['tablet', 'tablet']);++ expect(traffic.bad).toEqual([]);+ expect(traffic.pageErrors).toEqual([]);+ } finally {+ await deleteDashboard(page, dashboard.id);+ }+});++test('dashboard grid: drag, resize, persist, reset layout', async ({+ page,+}) => {+ const traffic = watch(page);+ const dashboard = await createDashboard(page, 'grid');+ try {+ for (const name of ['E2E verify-b grid one', 'E2E verify-b grid two']) {+ await createReport(page, dashboard.id, name, {+ series: eventSeries(['purchase']),+ });+ }+ await page.goto(`/acme/${SHOP}/dashboards/${dashboard.id}`);+ await page.waitForLoadState('networkidle');+ const card = (name: string) =>+ page.locator('.react-grid-item').filter({ hasText: name });+ const one = card('E2E verify-b grid one');+ const two = card('E2E verify-b grid two');+ await expect(one).toBeVisible({ timeout: 30_000 });+ const boxes = async () => ({+ one: await one.boundingBox(),+ two: await two.boundingBox(),+ });+ const initial = await boxes();+ await shot(page, 'item13-grid-initial');++ // --- resize card one from its bottom-right handle+ const layoutSaved = () =>+ page.waitForResponse((response) =>+ response.url().includes('/trpc/report.updateLayout')+ );+ const handle = one.locator('.react-resizable-handle').last();+ const handleBox = await handle.boundingBox();+ if (!(handleBox && initial.one && initial.two)) {+ throw new Error('grid not laid out');+ }+ let saved = layoutSaved();+ await page.mouse.move(+ handleBox.x + handleBox.width / 2,+ handleBox.y + handleBox.height / 2+ );+ await page.mouse.down();+ await page.mouse.move(handleBox.x + 40, handleBox.y + 160, { steps: 12 });+ await page.mouse.up();+ expect((await saved).status()).toBe(200);+ await page.waitForTimeout(800);+ const resized = await boxes();+ expect(resized.one?.height ?? 0).toBeGreaterThan(initial.one.height + 80);++ // --- drag card two by its handle to the left column+ const grip = two.locator('.drag-handle').first();+ const gripBox = await grip.boundingBox();+ if (!gripBox) {+ throw new Error('no drag handle');+ }+ saved = layoutSaved();+ await page.mouse.move(+ gripBox.x + gripBox.width / 2,+ gripBox.y + gripBox.height / 2+ );+ await page.mouse.down();+ await page.mouse.move(gripBox.x - 300, gripBox.y + 500, { steps: 20 });+ await page.mouse.up();+ expect((await saved).status()).toBe(200);+ await page.waitForTimeout(800);+ const dragged = await boxes();+ await shot(page, 'item13-grid-changed');+ expect(+ Math.abs((dragged.two?.x ?? 0) - initial.two.x) ++ Math.abs((dragged.two?.y ?? 0) - initial.two.y)+ ).toBeGreaterThan(50);++ // --- the layout survives a reload+ await page.reload();+ await expect(one).toBeVisible({ timeout: 30_000 });+ await page.waitForLoadState('networkidle');+ const reloaded = await boxes();+ expect(Math.round(reloaded.one?.height ?? 0)).toBe(+ Math.round(dragged.one?.height ?? 0)+ );+ expect(Math.round(reloaded.two?.x ?? 0)).toBe(+ Math.round(dragged.two?.x ?? 0)+ );+ expect(Math.round(reloaded.two?.y ?? 0)).toBe(+ Math.round(dragged.two?.y ?? 0)+ );++ // --- reset layout+ await page.locator('button:has(svg.lucide-ellipsis)').first().click();+ await page.getByRole('menuitem', { name: 'Reset layout' }).click();+ await expect(+ page.getByText('Are you sure you want to reset the layout')+ ).toBeVisible();+ await shot(page, 'item13-grid-reset-confirm', false);+ await page+ .getByRole('button', { name: /^(Yes|Confirm|Reset|OK)/i })+ .click();+ await expect(page.getByText('Layout reset to default')).toBeVisible();+ await page.waitForTimeout(1000);+ const reset = await boxes();+ await shot(page, 'item13-grid-reset');+ expect(Math.round(reset.one?.height ?? 0)).toBe(+ Math.round(initial.one.height)+ );+ expect(Math.round(reset.two?.x ?? 0)).toBe(Math.round(initial.two.x));+ expect(Math.round(reset.two?.y ?? 0)).toBe(Math.round(initial.two.y));++ expect(traffic.bad).toEqual([]);+ expect(traffic.pageErrors).toEqual([]);+ } finally {+ await deleteDashboard(page, dashboard.id);+ }+});++// The only CSV export in the dashboard is the cohort members download;+// report tables have none.+test('cohort members download as CSV', async ({ page }) => {+ const SAAS = 'acme-saas';+ const cohort = await trpcMutation<{ id: string; name: string }>(+ page,+ 'cohort.create',+ {+ name: `E2E verify-b csv ${Date.now()}`,+ projectId: SAAS,+ isStatic: false,+ definition: {+ type: 'property',+ criteria: {+ operator: 'and',+ properties: [+ { name: 'profile.properties.plan', operator: 'is', value: ['pro'] },+ ],+ },+ },+ }+ );+ try {+ await expect+ .poll(+ async () =>+ (+ await trpcQuery<{ profileCount: number | null }>(+ page,+ 'cohort.get',+ {+ id: cohort.id,+ }+ )+ ).profileCount ?? 0,+ { timeout: 90_000, intervals: [3000] }+ )+ .toBeGreaterThan(0);+ const expected = await trpcQuery<{ profileIds: string[] }>(+ page,+ 'cohort.exportProfiles',+ { cohortId: cohort.id }+ );++ await page.goto(`/acme/${SAAS}/cohorts/${cohort.id}`);+ await page.waitForLoadState('networkidle');+ const download = page.waitForEvent('download');+ await page.getByRole('button', { name: 'Download' }).click();+ const file = await download;+ expect(file.suggestedFilename()).toBe(`${cohort.name}-members.csv`);+ const path = 'test-results/verify-b/cohort-members.csv';+ await file.saveAs(path);+ const { readFileSync } = await import('node:fs');+ const lines = readFileSync(path, 'utf8').trim().split('\n');+ expect(lines[0]).toBe('profile_id');+ expect(lines.slice(1).sort()).toEqual([...expected.profileIds].sort());+ expect(lines.length - 1).toBeGreaterThan(0);+ } finally {+ await trpcMutation(page, 'cohort.delete', { id: cohort.id });+ }+});diff --git a/apps/start/e2e/verify-b-profile-filters.spec.ts b/apps/start/e2e/verify-b-profile-filters.spec.tsnew file mode 100644index 000000000..c3b496e65--- /dev/null+++ b/apps/start/e2e/verify-b-profile-filters.spec.ts@@ -0,0 +1,152 @@+import type { Page } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ closeSidebar,+ hideFeedbackPrompt,+ openSidebar,+ pickChartType,+ waitForChart,+} from './reports-helpers';+import { shot, watch } from './verify-b-helpers';++const SAAS = 'acme-saas';+const CHART_ERROR = 'There was an error loading this chart.';++test.use({ actionTimeout: 20_000 });++/**+ * Adds `profile.properties.plan is pro` through the sidebar, as a global+ * filter or on the first event.+ */+async function addProfilePlanFilter(page: Page, scope: 'global' | 'event') {+ const sheet = await openSidebar(page);+ const addFilter = sheet.getByRole('button', { name: 'Add filter' });+ await (scope === 'global' ? addFilter.last() : addFilter.first()).click();+ await page.getByRole('menuitem', { name: 'Profile properties' }).click();+ await page+ .getByRole('menu')+ .getByPlaceholder('Search', { exact: true })+ .fill('plan');+ await page+ .locator('[data-radix-popper-content-wrapper]')+ .getByText('plan', { exact: true })+ .first()+ .click();+ await sheet.getByText('Select...').click();+ await page.getByRole('option').filter({ hasText: /^pro/ }).first().click();+ await page.keyboard.press('Escape');+ await closeSidebar(page);+}++/** `addEvent` from the reports helpers matches two comboboxes on a sankey. */+async function addEventExact(page: Page, name: string) {+ const sheet = await openSidebar(page);+ await sheet+ .getByRole('combobox')+ .filter({ hasText: /^Select event$/ })+ .click();+ await page.getByPlaceholder('Search event...').fill(name);+ await page+ .getByRole('option')+ .filter({ hasText: new RegExp(`^${name}(\\d|\\s|$)`) })+ .first()+ .click({ timeout: 15_000 });+}++async function buildReport(+ page: Page,+ chartType: string,+ events: readonly string[]+) {+ await hideFeedbackPrompt(page);+ await page.goto(`/acme/${SAAS}/reports`);+ await page.waitForLoadState('networkidle');+ await openSidebar(page);+ await closeSidebar(page);+ if (chartType !== 'Linear') {+ await pickChartType(page, 'Linear', chartType);+ }+ for (const event of events) {+ await addEventExact(page, event);+ }+ await closeSidebar(page);+ await waitForChart(page);+}++const CASES = [+ {+ chartType: 'Linear',+ events: ['signup_completed'],+ procedure: 'chart.chart',+ scope: 'global',+ },+ {+ chartType: 'Funnel',+ events: ['signup_completed', 'project_created'],+ procedure: 'chart.funnel',+ scope: 'global',+ },+ {+ chartType: 'Retention',+ events: ['signup_completed', 'session_start'],+ procedure: 'chart.cohort',+ scope: 'global',+ },+ {+ chartType: 'Conversion',+ events: ['signup_completed', 'project_created'],+ procedure: 'chart.conversion',+ scope: 'global',+ },+ {+ chartType: 'Sankey',+ events: ['signup_completed'],+ procedure: 'chart.sankey',+ scope: 'event',+ },+ // The sankey chart drops global filters from its request altogether.+ {+ chartType: 'Sankey',+ events: ['signup_completed'],+ procedure: 'chart.sankey',+ scope: 'global',+ },+] as const;++for (const { chartType, events, procedure, scope } of CASES) {+ test(`${chartType} report applies a ${scope} profile-property filter`, async ({+ page,+ }) => {+ test.setTimeout(240_000);+ watch(page);+ await buildReport(page, chartType, events);+ const editor = page.locator('#report-editor');+ await expect(editor).not.toContainText(CHART_ERROR);++ const filtered = page+ .waitForResponse(+ (response) => {+ const url = decodeURIComponent(response.url());+ return (+ url.includes(`/trpc/${procedure}`) &&+ url.includes('profile.properties.plan') &&+ url.includes('"pro"')+ );+ },+ { timeout: 20_000 }+ )+ .catch(() => null);+ await addProfilePlanFilter(page, scope);+ const response = await filtered;+ await page.waitForTimeout(1500);+ await shot(+ page,+ `item1-${chartType.toLowerCase()}-${scope}-profile-filter`+ );+ expect(response, 'the chart query carries the filter').not.toBeNull();+ expect(response?.status(), (await response?.text())?.slice(0, 200)).toBe(+ 200+ );+ await expect(editor).not.toContainText(CHART_ERROR);+ });+}diff --git a/apps/start/e2e/verify-b-range-edges.spec.ts b/apps/start/e2e/verify-b-range-edges.spec.tsnew file mode 100644index 000000000..6265ff15e--- /dev/null+++ b/apps/start/e2e/verify-b-range-edges.spec.ts@@ -0,0 +1,78 @@+import type { Page } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ addEvent,+ closeSidebar,+ hideFeedbackPrompt,+ openSidebar,+ waitForChart,+} from './reports-helpers';+import { shot } from './verify-b-helpers';++interface Bucket {+ date: string;+ count: number;+}++/** Picks a range in the builder and returns the buckets the chart then draws. */+async function bucketsAfter(page: Page, action: () => Promise<void>) {+ const answered = page.waitForResponse(+ (response) =>+ response.url().includes('/trpc/chart.chart') && response.status() === 200+ );+ await action();+ const body = await (await answered).json();+ await waitForChart(page);+ const input = JSON.parse(+ new URL((await answered).url()).searchParams.get('input') ?? '{}'+ ).json;+ const buckets = (body.result.data.json.series[0]?.data ?? []) as Bucket[];+ return { buckets, interval: input.interval as string, range: input.range };+}++test.use({ actionTimeout: 20_000 });++test('report builder ranges: bucket counts at the edges', async ({ page }) => {+ test.setTimeout(240_000);+ await hideFeedbackPrompt(page);+ // acme-shop has live events from today, so "Today" returns a series.+ await page.goto('/acme/acme-shop/reports');+ await page.waitForLoadState('networkidle');+ await openSidebar(page);+ await closeSidebar(page);+ const pickRange = (current: string, next: RegExp) => async () => {+ await page.getByRole('button', { name: current, exact: true }).click();+ await page.getByRole('menuitem', { name: next }).click();+ };+ const month = await bucketsAfter(page, async () => {+ await addEvent(page, 'screen_view');+ await closeSidebar(page);+ });+ const week = await bucketsAfter(+ page,+ pickRange('Last 30 days', /^Last 7 days/)+ );+ await shot(page, 'item11-7d');+ const year = await bucketsAfter(+ page,+ pickRange('Last 7 days', /^Last 12 months/)+ );+ await shot(page, 'item11-12m');+ const today = await bucketsAfter(page, pickRange('Last 12 months', /^Today/));+ await shot(page, 'item11-today');++ const facts = {+ '7d': `${week.buckets.length} ${week.interval} buckets, ${week.buckets[0]?.date} .. ${week.buckets.at(-1)?.date}`,+ '30d': `${month.buckets.length} ${month.interval} buckets, ${month.buckets[0]?.date} .. ${month.buckets.at(-1)?.date}`,+ '12m': `${year.buckets.length} ${year.interval} buckets, ${year.buckets[0]?.date} .. ${year.buckets.at(-1)?.date}`,+ today: `${today.buckets.length} ${today.interval} buckets, ${today.buckets[0]?.date} .. ${today.buckets.at(-1)?.date}`,+ };+ for (const [range, description] of Object.entries(facts)) {+ test.info().annotations.push({ type: range, description });+ }+ console.log(JSON.stringify(facts, null, 1));++ // Whether N days means N or N+1 buckets is a product decision; a day has 24 hours either way.+ expect(today.interval).toBe('hour');+ expect(today.buckets.length, 'hourly buckets for Today').toBe(24);+});diff --git a/apps/start/e2e/verify-b-realtime.spec.ts b/apps/start/e2e/verify-b-realtime.spec.tsnew file mode 100644index 000000000..bdf74f6bf--- /dev/null+++ b/apps/start/e2e/verify-b-realtime.spec.ts@@ -0,0 +1,49 @@+import { expect, test } from './fixtures';++const MARKER = {+ country: 'SE',+ city: 'Stockholm',+ long: 18.07,+ lat: 59.33,+ count: 3,+};++const VIEWPORTS = {+ desktop: { width: 1280, height: 720 },+ mobile: { width: 390, height: 844 },+};++// The seed has no events in the realtime window and local ingest resolves no+// geo, so a visitor with coordinates is supplied at the network boundary.+for (const [name, viewport] of Object.entries(VIEWPORTS)) {+ test(`realtime map with a located visitor logs no NaN transform (${name})`, async ({+ page,+ }) => {+ await page.setViewportSize(viewport);+ const nanErrors: string[] = [];+ page.on('console', (message) => {+ if (message.type() === 'error' && message.text().includes('NaN')) {+ nanErrors.push(message.text().slice(0, 120));+ }+ });+ await page.route('**/trpc/realtime.coordinates**', async (route) => {+ const response = await route.fetch();+ const body = await response.json();+ body.result.data.json = [MARKER];+ await route.fulfill({ response, json: body });+ });+ await page.goto('/acme/acme-web/realtime');+ await page.waitForLoadState('networkidle');+ await page.waitForTimeout(2000);+ await page.screenshot({+ path: `test-results/verify-b/shots/item6-realtime-${name}.png`,+ });+ const transforms = await page+ .locator('.rsm-zoomable-group')+ .evaluateAll((groups) => groups.map((g) => g.getAttribute('transform')));+ expect(nanErrors).toEqual([]);+ for (const transform of transforms) {+ expect(transform).not.toContain('NaN');+ }+ });+}diff --git a/apps/start/e2e/verify-b-share.spec.ts b/apps/start/e2e/verify-b-share.spec.tsnew file mode 100644index 000000000..44c34f774--- /dev/null+++ b/apps/start/e2e/verify-b-share.spec.ts@@ -0,0 +1,640 @@+import type { Page } from '@playwright/test';+import { expect, test } from './fixtures';+import {+ eventSeries,+ hideFeedbackPrompt,+ reportInput,+ trpcMutation,+ trpcQuery,+} from './reports-helpers';+import {+ anonymousPage,+ openShareClientSide,+ shot,+ trpcUrl,+} from './verify-b-helpers';++const SHOP = 'acme-shop';+const ORGANIZATION = 'acme';+const PASSWORD = 'e2e-verify-b-secret';+const SIGN_IN_SHARE_WINDOW_MS = 35_000;+const PASSWORD_PLACEHOLDER = 'Enter your password (optional)';++interface ShareSettings {+ id: string;+ public: boolean;+ hasPassword: boolean;+}++interface PublicShare {+ id: string;+ requiresPassword: boolean;+}++const RETENTION_SERIES = ['A', 'B'].map((id) => ({+ id,+ type: 'event',+ name: 'session_start',+ segment: 'event',+ filters: [+ { id: 'n', name: 'name', operator: 'is', value: ['session_start'] },+ ],+}));++const REPORTS: Record<string, Record<string, unknown>> = {+ linear: { series: eventSeries(['purchase']) },+ area: { series: eventSeries(['purchase']) },+ histogram: { series: eventSeries(['purchase']) },+ bar: {+ series: eventSeries(['purchase']),+ breakdowns: [{ id: 'b', name: 'device' }],+ },+ pie: {+ series: eventSeries(['purchase']),+ breakdowns: [{ id: 'b', name: 'device' }],+ },+ metric: { series: eventSeries(['purchase']) },+ map: {+ series: eventSeries(['purchase']),+ breakdowns: [{ id: 'b', name: 'country' }],+ },+ funnel: {+ series: eventSeries(['product_viewed', 'add_to_cart', 'purchase']),+ },+ conversion: { series: eventSeries(['product_viewed', 'purchase']) },+ retention: {+ series: RETENTION_SERIES,+ options: { type: 'retention', criteria: 'on_or_after' },+ },+ sankey: {+ series: eventSeries(['product_viewed']),+ options: { type: 'sankey', mode: 'after', steps: 5, exclude: [] },+ },+};++const reportName = (chartType: string) => `E2E verify-b ${chartType}`;++async function createDashboardWith(page: Page, chartTypes: string[]) {+ const dashboard = await trpcMutation<{ id: string }>(+ page,+ 'dashboard.create',+ {+ name: `E2E verify-b share ${Date.now()}`,+ projectId: SHOP,+ }+ );+ const reportIds: Record<string, string> = {};+ for (const chartType of chartTypes) {+ const { projectId: _projectId, ...report } = reportInput(SHOP, {+ chartType,+ ...REPORTS[chartType],+ });+ const created = await trpcMutation<{ id: string }>(page, 'report.create', {+ dashboardId: dashboard.id,+ report: { ...report, name: reportName(chartType) },+ });+ reportIds[chartType] = created.id;+ }+ return { id: dashboard.id, reportIds };+}++function deleteDashboard(page: Page, id: string) {+ return trpcMutation(page, 'dashboard.delete', { id, forceDelete: true });+}++function shareDashboard(+ page: Page,+ dashboardId: string,+ share: { public: boolean; password: string | null }+) {+ return trpcMutation<ShareSettings>(page, 'share.createDashboard', {+ organizationId: ORGANIZATION,+ projectId: SHOP,+ dashboardId,+ ...share,+ });+}++function shareReport(+ page: Page,+ reportId: string,+ share: { public: boolean; password: string | null }+) {+ return trpcMutation<ShareSettings>(page, 'share.createReport', {+ organizationId: ORGANIZATION,+ projectId: SHOP,+ reportId,+ ...share,+ });+}++const dashboardSettings = (page: Page, dashboardId: string) =>+ trpcQuery<ShareSettings | null>(page, 'share.dashboardSettings', {+ projectId: SHOP,+ dashboardId,+ });++const reportSettings = (page: Page, reportId: string) =>+ trpcQuery<ShareSettings | null>(page, 'share.reportSettings', {+ projectId: SHOP,+ reportId,+ });++async function openDashboardShareModal(page: Page) {+ await expect(async () => {+ const item = page.getByRole('menuitem', { name: 'Share dashboard' });+ if (!(await item.isVisible())) {+ await page+ .locator('button:has(svg.lucide-ellipsis)')+ .first()+ .click({ timeout: 3000 });+ }+ await item.click({ timeout: 3000 });+ await expect(page.getByText('Dashboard public availability')).toBeVisible({+ timeout: 3000,+ });+ }).toPass({ timeout: 45_000 });+}++async function openReportShareModal(page: Page) {+ await expect(async () => {+ await page+ .getByRole('button', { name: 'Share', exact: true })+ .click({ timeout: 3000 });+ await expect(page.getByPlaceholder(PASSWORD_PLACEHOLDER)).toBeVisible({+ timeout: 3000,+ });+ }).toPass({ timeout: 45_000 });+}++/** Every chart card on a shared dashboard loads lazily, once scrolled to. */+async function scrollThroughCards(page: Page, chartTypes: string[]) {+ for (const chartType of chartTypes) {+ await page+ .getByText(reportName(chartType), { exact: true })+ .scrollIntoViewIfNeeded();+ await page.mouse.wheel(0, 250);+ await page.waitForTimeout(700);+ }+ await page.waitForLoadState('networkidle');+}++function card(page: Page, chartType: string) {+ return page+ .locator('.react-grid-item')+ .filter({ hasText: reportName(chartType) });+}++test.use({ actionTimeout: 15_000 });++test.beforeEach(async ({ page }) => {+ test.setTimeout(300_000);+ await hideFeedbackPrompt(page);+ await page.goto('/login');+});++test('a shared dashboard renders every chart type for an anonymous visitor', async ({+ page,+ browser,+}) => {+ const chartTypes = Object.keys(REPORTS).filter((type) => type !== 'sankey');+ const dashboard = await createDashboardWith(page, chartTypes);+ const visitor = await anonymousPage(browser);+ try {+ const share = await shareDashboard(page, dashboard.id, {+ public: true,+ password: null,+ });+ await openShareClientSide(visitor.page, `/share/dashboard/${share.id}`);+ await expect(+ visitor.page.getByText(reportName('linear'), { exact: true })+ ).toBeVisible({ timeout: 45_000 });+ await scrollThroughCards(visitor.page, chartTypes);+ await shot(visitor.page, 'item4-shared-dashboard-all-types');++ expect(visitor.page.url()).toContain(`/share/dashboard/${share.id}`);+ expect(visitor.traffic.bad).toEqual([]);+ for (const chartType of chartTypes) {+ const chartCard = card(visitor.page, chartType);+ await expect(chartCard, chartType).not.toContainText(/error loading/i);+ await expect(chartCard, chartType).not.toContainText('Stay calm');+ await expect(chartCard, chartType).not.toContainText('Select 2 events');+ }+ await expect(+ card(visitor.page, 'linear').locator('svg.recharts-surface')+ ).toBeVisible();+ await expect(card(visitor.page, 'funnel')).toContainText('product_viewed');+ await expect(card(visitor.page, 'retention')).toContainText(+ 'Weighted Average'+ );+ } finally {+ await visitor.context.close();+ await deleteDashboard(page, dashboard.id);+ }+});++test('range and interval controls on a shared dashboard drive the chart queries', async ({+ page,+ browser,+}) => {+ const dashboard = await createDashboardWith(page, ['linear']);+ const visitor = await anonymousPage(browser);+ try {+ const share = await shareDashboard(page, dashboard.id, {+ public: true,+ password: null,+ });+ const chartRequests: { range: string; interval: string; status: number }[] =+ [];+ visitor.page.on('response', (response) => {+ if (!response.url().includes('/trpc/chart.chart')) {+ return;+ }+ const input = JSON.parse(+ new URL(response.url()).searchParams.get('input') ?? '{}'+ ).json;+ chartRequests.push({+ range: input.range,+ interval: input.interval,+ status: response.status(),+ });+ });+ await openShareClientSide(visitor.page, `/share/dashboard/${share.id}`);+ await expect(+ card(visitor.page, 'linear').locator('svg.recharts-surface')+ ).toBeVisible({ timeout: 45_000 });++ await visitor.page+ .getByRole('button', { name: 'Last 7 days', exact: true })+ .click();+ await visitor.page.getByRole('menuitem', { name: /^Last 30 days/ }).click();+ await expect+ .poll(() =>+ chartRequests.some((r) => r.range === '30d' && r.status === 200)+ )+ .toBe(true);+ await expect(visitor.page).toHaveURL(/range=30d/);++ await visitor.page+ .getByRole('button', { name: 'Day', exact: true })+ .click();+ await visitor.page.getByRole('menuitem', { name: 'Week' }).click();+ await expect+ .poll(() =>+ chartRequests.some(+ (r) => r.range === '30d' && r.interval === 'week' && r.status === 200+ )+ )+ .toBe(true);+ await visitor.page.waitForLoadState('networkidle');+ await shot(visitor.page, 'item4-shared-dashboard-30d-week');+ expect(visitor.traffic.bad).toEqual([]);+ } finally {+ await visitor.context.close();+ await deleteDashboard(page, dashboard.id);+ }+});++test('a shared sankey report loads for an anonymous visitor', async ({+ page,+ browser,+}) => {+ const dashboard = await createDashboardWith(page, ['sankey']);+ const visitor = await anonymousPage(browser);+ try {+ const share = await shareReport(+ page,+ dashboard.reportIds.sankey as string,+ {+ public: true,+ password: null,+ }+ );+ const api = await visitor.page.request.get(+ trpcUrl('chart.sankey', {+ ...reportInput(SHOP, { chartType: 'sankey', ...REPORTS.sankey }),+ shareId: share.id,+ id: dashboard.reportIds.sankey,+ })+ );+ expect+ .soft(api.status(), 'chart.sankey with a shareId and no session')+ .toBe(200);++ await openShareClientSide(visitor.page, `/share/report/${share.id}`);+ await expect(+ visitor.page.getByText(reportName('sankey'), { exact: true })+ ).toBeVisible({ timeout: 45_000 });+ await visitor.page.waitForLoadState('networkidle');+ await visitor.page.waitForTimeout(3000);+ await shot(visitor.page, 'item2-shared-sankey-report');+ expect.soft(visitor.traffic.bad).toEqual([]);+ expect(visitor.page.url(), 'the visitor stays on the share').toContain(+ `/share/report/${share.id}`+ );+ } finally {+ await visitor.context.close();+ await deleteDashboard(page, dashboard.id);+ }+});++test('a sankey card on a shared dashboard loads for an anonymous visitor', async ({+ page,+ browser,+}) => {+ const dashboard = await createDashboardWith(page, ['linear', 'sankey']);+ const visitor = await anonymousPage(browser);+ try {+ const share = await shareDashboard(page, dashboard.id, {+ public: true,+ password: null,+ });+ await openShareClientSide(visitor.page, `/share/dashboard/${share.id}`);+ await expect(+ visitor.page.getByText(reportName('sankey'), { exact: true })+ ).toBeVisible({ timeout: 45_000 });+ await scrollThroughCards(visitor.page, ['sankey']);+ await visitor.page.waitForTimeout(3000);+ await shot(visitor.page, 'item2-shared-dashboard-sankey');+ expect.soft(visitor.traffic.bad).toEqual([]);+ expect(visitor.page.url(), 'the visitor stays on the share').toContain(+ `/share/dashboard/${share.id}`+ );+ } finally {+ await visitor.context.close();+ await deleteDashboard(page, dashboard.id);+ }+});++test('dashboard share modal: Update keeps the existing password', async ({+ page,+ browser,+}) => {+ const dashboard = await createDashboardWith(page, ['linear']);+ const visitor = await anonymousPage(browser);+ try {+ await page.goto(`/acme/${SHOP}/dashboards/${dashboard.id}`);+ await page.waitForLoadState('networkidle');+ await openDashboardShareModal(page);+ await page.getByPlaceholder(PASSWORD_PLACEHOLDER).fill(PASSWORD);+ await page.getByRole('button', { name: 'Make it public' }).click();+ await expect+ .poll(+ async () => (await dashboardSettings(page, dashboard.id))?.hasPassword+ )+ .toBe(true);+ const share = (await dashboardSettings(+ page,+ dashboard.id+ )) as ShareSettings;+ await expect(page.getByText('Dashboard public availability')).toHaveCount(+ 0+ );++ await openDashboardShareModal(page);+ await expect(page.getByText('Currently shared')).toBeVisible();+ await page.waitForTimeout(1000);+ await shot(page, 'item3-dashboard-modal-reopened', false);+ const update = page.waitForResponse((response) =>+ response.url().includes('share.createDashboard')+ );+ await page.getByRole('button', { name: 'Update' }).click();+ expect((await update).status()).toBe(200);+ await expect(page.getByText('Dashboard public availability')).toHaveCount(+ 0+ );++ const after = (await dashboardSettings(+ page,+ dashboard.id+ )) as ShareSettings;+ expect.soft(after.hasPassword, 'share still has a password').toBe(true);+ const anonymous = await visitor.page.request.get(+ trpcUrl('share.dashboard', { shareId: share.id })+ );+ const body = (await anonymous.json()).result.data.json as PublicShare;+ expect+ .soft(body.requiresPassword, 'anonymous visit asks for it')+ .toBe(true);+ await openShareClientSide(visitor.page, `/share/dashboard/${share.id}`);+ await visitor.page.waitForLoadState('networkidle');+ await shot(visitor.page, 'item3-dashboard-anonymous-after-update', false);+ await expect(visitor.page.getByText('Dashboard is locked')).toBeVisible();+ } finally {+ await visitor.context.close();+ await deleteDashboard(page, dashboard.id);+ }+});++test('report share modal: Update keeps the existing password', async ({+ page,+ browser,+}) => {+ const dashboard = await createDashboardWith(page, ['linear']);+ const reportId = dashboard.reportIds.linear as string;+ const visitor = await anonymousPage(browser);+ try {+ await page.goto(`/acme/${SHOP}/reports/${reportId}`);+ await page.waitForLoadState('networkidle');+ await openReportShareModal(page);+ await page.getByPlaceholder(PASSWORD_PLACEHOLDER).fill(PASSWORD);+ await page.getByRole('button', { name: 'Make it public' }).click();+ await expect+ .poll(async () => (await reportSettings(page, reportId))?.hasPassword)+ .toBe(true);+ const share = (await reportSettings(page, reportId)) as ShareSettings;+ await expect(page.getByPlaceholder(PASSWORD_PLACEHOLDER)).toHaveCount(0);++ await openReportShareModal(page);+ await expect(page.getByText('Currently shared')).toBeVisible();+ await page.waitForTimeout(1000);+ await shot(page, 'item3-report-modal-reopened', false);+ const update = page.waitForResponse((response) =>+ response.url().includes('share.createReport')+ );+ await page.getByRole('button', { name: 'Update' }).click();+ expect((await update).status()).toBe(200);+ await expect(page.getByPlaceholder(PASSWORD_PLACEHOLDER)).toHaveCount(0);++ const after = (await reportSettings(page, reportId)) as ShareSettings;+ expect.soft(after.hasPassword, 'share still has a password').toBe(true);+ await openShareClientSide(visitor.page, `/share/report/${share.id}`);+ await visitor.page.waitForLoadState('networkidle');+ await shot(visitor.page, 'item3-report-anonymous-after-update', false);+ await expect(visitor.page.getByText('Report is locked')).toBeVisible();+ } finally {+ await visitor.context.close();+ await deleteDashboard(page, dashboard.id);+ }+});++for (const kind of ['dashboard', 'report'] as const) {+ test(`shared ${kind}: password gate, then disabling the share`, async ({+ page,+ browser,+ }) => {+ // `auth.signInShare` allows three attempts per 30 s for the whole machine.+ await page.waitForTimeout(SIGN_IN_SHARE_WINDOW_MS);+ const dashboard = await createDashboardWith(page, ['linear']);+ const reportId = dashboard.reportIds.linear as string;+ const setShare = (share: { public: boolean; password: string | null }) =>+ kind === 'dashboard'+ ? shareDashboard(page, dashboard.id, share)+ : shareReport(page, reportId, share);+ const visitor = await anonymousPage(browser);+ try {+ const share = await setShare({ public: true, password: PASSWORD });+ const path = `/share/${kind}/${share.id}`;+ const chartUrl = trpcUrl('chart.chart', {+ ...reportInput(SHOP, REPORTS.linear as Record<string, unknown>),+ shareId: share.id,+ id: reportId,+ });+ expect(+ (await visitor.page.request.get(chartUrl)).status(),+ 'chart data is closed before the password'+ ).toBeGreaterThanOrEqual(400);++ await openShareClientSide(visitor.page, path);+ await expect(visitor.page.getByText(/is locked/)).toBeVisible({+ timeout: 45_000,+ });+ await expect(+ visitor.page.getByText(reportName('linear'), { exact: true })+ ).toHaveCount(0);+ await visitor.page.waitForLoadState('networkidle');+ await visitor.page.getByPlaceholder('Enter your password').fill('wrong');+ await visitor.page.getByRole('button', { name: 'Get access' }).click();+ await expect(visitor.page.getByText('Incorrect password')).toBeVisible();+ await expect(visitor.page.getByText(/is locked/)).toBeVisible();+ await shot(visitor.page, `item4-${kind}-wrong-password`, false);++ const unlock = visitor.page.waitForResponse((response) =>+ response.url().includes('auth.signInShare')+ );+ await visitor.page.getByPlaceholder('Enter your password').fill(PASSWORD);+ await visitor.page.getByPlaceholder('Enter your password').press('Enter');+ expect((await unlock).status()).toBe(200);+ expect((await visitor.page.request.get(chartUrl)).status()).toBe(200);++ // The unlock reloads the document; re-enter client-side with the cookie.+ await openShareClientSide(visitor.page, path);+ await expect(+ visitor.page.getByText(reportName('linear'), { exact: true })+ ).toBeVisible({ timeout: 45_000 });+ await expect(+ visitor.page.locator('svg.recharts-surface').first()+ ).toBeVisible({ timeout: 45_000 });+ await shot(visitor.page, `item4-${kind}-unlocked`, false);++ await setShare({ public: false, password: null });+ expect(+ (await visitor.page.request.get(chartUrl)).status(),+ 'chart data is closed once the share is disabled'+ ).toBeGreaterThanOrEqual(400);+ await openShareClientSide(visitor.page, path);+ await expect(visitor.page.getByText('Share not found')).toBeVisible({+ timeout: 45_000,+ });+ await shot(visitor.page, `item4-${kind}-disabled`, false);+ } finally {+ await visitor.context.close();+ await deleteDashboard(page, dashboard.id);+ }+ });+}++test('clicking a chart point on a shared report keeps the visitor on the share', async ({+ page,+ browser,+}) => {+ const dashboard = await createDashboardWith(page, ['linear']);+ const visitor = await anonymousPage(browser);+ try {+ const share = await shareReport(+ page,+ dashboard.reportIds.linear as string,+ {+ public: true,+ password: null,+ }+ );+ await openShareClientSide(visitor.page, `/share/report/${share.id}`);+ const surface = visitor.page.locator('svg.recharts-surface').first();+ await expect(surface).toBeVisible({ timeout: 45_000 });+ await visitor.page.waitForLoadState('networkidle');+ const box = await surface.boundingBox();+ if (!box) {+ throw new Error('chart not drawn');+ }+ await visitor.page.mouse.click(+ box.x + box.width * 0.9,+ box.y + box.height * 0.5+ );+ await visitor.page.waitForTimeout(800);+ const items = await visitor.page.getByRole('menuitem').allInnerTexts();+ test.info().annotations.push({+ type: 'menu items offered to an anonymous visitor',+ description: items.join(', ') || '(none)',+ });+ await shot(visitor.page, 'item4-shared-report-point-menu', false);+ if (items.includes('View Users')) {+ await visitor.page.getByRole('menuitem', { name: 'View Users' }).click();+ await visitor.page.waitForLoadState('networkidle');+ await visitor.page.waitForTimeout(2500);+ await shot(visitor.page, 'item4-shared-report-view-users', false);+ }+ expect.soft(visitor.traffic.bad).toEqual([]);+ expect(visitor.page.url(), 'the visitor stays on the share').toContain(+ `/share/report/${share.id}`+ );+ } finally {+ await visitor.context.close();+ await deleteDashboard(page, dashboard.id);+ }+});++// The overview share modal has the same "placeholder means null" submit as the+// dashboard and report modals, but a public overview offers no way back into+// the modal, so its password cannot be dropped by an "Update".+test('overview share: a password set in the modal stays until the share is made private', async ({+ page,+}) => {+ const APP = 'acme-app';+ const settings = () =>+ trpcQuery<ShareSettings | null>(page, 'share.overviewSettings', {+ projectId: APP,+ });+ const makePrivate = () =>+ trpcMutation(page, 'share.createOverview', {+ organizationId: ORGANIZATION,+ projectId: APP,+ public: false,+ password: null,+ });+ expect((await settings())?.public ?? false, 'starts private').toBe(false);+ try {+ await page.goto(`/acme/${APP}?range=7d`);+ await page.waitForLoadState('networkidle');+ await page.getByRole('button', { name: 'Private', exact: true }).click();+ await page.getByRole('menuitem', { name: 'Make public' }).click();+ const modal = page.getByRole('dialog');+ await modal.getByPlaceholder(PASSWORD_PLACEHOLDER).fill(PASSWORD);+ await modal.getByRole('button', { name: 'Make it public' }).click();+ await expect(+ page.getByRole('button', { name: 'Public', exact: true })+ ).toBeVisible();+ expect((await settings())?.hasPassword).toBe(true);++ await page.reload();+ await page.waitForLoadState('networkidle');+ await page.getByRole('button', { name: 'Public', exact: true }).click();+ const items = await page.getByRole('menuitem').allInnerTexts();+ await shot(page, 'item3-overview-public-menu', false);+ expect(items.map((item) => item.trim())).toEqual(['View', 'Make private']);+ await page.keyboard.press('Escape');+ expect((await settings())?.hasPassword).toBe(true);+ } finally {+ await makePrivate();+ }+});