diff --git a/apps/web/e2e/QUARANTINE.md b/apps/web/e2e/QUARANTINE.md index f664b83cba..912b03feba 100644 --- a/apps/web/e2e/QUARANTINE.md +++ b/apps/web/e2e/QUARANTINE.md @@ -590,3 +590,32 @@ no such part beyond the wrong notification). visitors see "All systems operational". Fix: derive it from the anonymous projection (`getStatusPageSnapshot(ANONYMOUS_ACTOR, …).topLevel`), or relabel the banner. owner: unassigned, review by: 2026-12-03. +- `status/public-page.spec.ts` › STA-P-15 uptime follows a status that has no + history row. `createStatusComponent` + (`apps/web/src/lib/server/domains/status/status.components.ts:176`) stores a + non-operational initial status (REST and the server fn accept one) without a + `status_component_events` row or a `status.component_changed` event, and the + uptime derivation starts from operational when a service has no event + (`status.public.ts:291`), so a service created "Major outage" shows a 100% + operational bar. Fix: write the initial event (and the webhook event) when the + create status is not operational. owner: unassigned, review by: 2026-12-03. +- `status/subscribe.spec.ts` › STA-S-09 a component subscription with no or + unknown services is refused. `subscribeStatusFn` + (`apps/web/src/lib/server/functions/status-subscriptions.ts:25`) takes + `scope: 'components'` with an empty or non-existent `componentIds` and stores + an active subscription that can never match an incident (it still counts as + a subscriber and takes a plan slot). The portal UI prevents both. Fix: refuse + an empty list and ids that are not live, visible services. owner: + unassigned, review by: 2026-12-03. +- `status/emails.spec.ts` › STA-S-23 nothing is emailed for an incident or + window created already over. Same cause as STA-A-30 + (`status.service.ts:242`, `:317`): subscribers get "New incident reported … + Resolved". owner: unassigned, review by: 2026-12-03. +- `status/emails.spec.ts` › STA-S-30 an incident cannot be pinned to the + "maintenance" impact. The REST validator + (`apps/web/src/routes/api/v1/status/-validation.ts:33`) and the server fn + accept `impact: 'maintenance'` on `kind: 'incident'`, and the incident email + (`packages/email/src/templates/status-incident-published.tsx:29`) has no + colour or label for it, so its impact bar reads " · Investigating". Fix: + refuse it for incidents (or give the template a Maintenance label). owner: + unassigned, review by: 2026-12-03. diff --git a/apps/web/e2e/product/status.ts b/apps/web/e2e/product/status.ts index b5ff998be4..b78224bc50 100644 --- a/apps/web/e2e/product/status.ts +++ b/apps/web/e2e/product/status.ts @@ -152,7 +152,23 @@ export async function createIncident( api: APIRequestContext, input: IncidentInput ): Promise { - return must(api, 'createStatusIncidentFn', incidentPayload(input)) + const incident = await must(api, 'createStatusIncidentFn', incidentPayload(input)) + made.add(incident.id) + return incident +} + +/** Incidents and windows made by createIncident in this worker since the last cleanup. */ +const made = new Set() + +/** + * Delete what this test's helpers created. The feed keeps every open + * incident's newest entry within its 50-item cap, so open incidents left + * behind would crowd later tests' items out of it. Call in afterEach. + */ +export async function deleteMadeIncidents(api: APIRequestContext): Promise { + const ids = [...made] + made.clear() + for (const id of ids) await callServerFn(api, 'deleteStatusIncidentFn', { id }) } /** The same call, without failing: for refusals. */ @@ -388,10 +404,13 @@ export interface PortalUser extends MintedCustomer { export async function mintPortalUser( playwright: PlaywrightWorkerArgs['playwright'], marker: string, - label = 'sub' + label = 'sub', + opts: { anonymous?: boolean } = {} ): Promise { const email = `${marker}-${label}-${Math.random().toString(36).slice(2, 7)}@customers.example` - const customer = await mintCustomer({ email, name: `Sub ${label} ${marker.slice(-6)}` }) + const customer = opts.anonymous + ? await mintCustomer({ anonymous: true }) + : await mintCustomer({ email, name: `Sub ${label} ${marker.slice(-6)}` }) const env = supportEnv() const token = await mintSessionToken(customer.userUuid) const signature = createHmac('sha256', env.app.SECRET_KEY).update(token).digest('base64') diff --git a/apps/web/e2e/tests/status/admin-incidents.spec.ts b/apps/web/e2e/tests/status/admin-incidents.spec.ts index 5e5bd82335..78ddd13a60 100644 --- a/apps/web/e2e/tests/status/admin-incidents.spec.ts +++ b/apps/web/e2e/tests/status/admin-incidents.spec.ts @@ -36,6 +36,7 @@ import { serviceRow, setServiceStatus, tryCreateIncident, + deleteMadeIncidents, } from '../../product/status' // Browser journeys share the machine with the app under load: allow a minute. @@ -47,6 +48,7 @@ test.beforeEach(async ({ playwright }) => { admin = await personApi(playwright, 'admin') }) test.afterEach(async () => { + await deleteMadeIncidents(admin) await admin.dispose() }) diff --git a/apps/web/e2e/tests/status/admin-maintenance.spec.ts b/apps/web/e2e/tests/status/admin-maintenance.spec.ts index 694accbdee..76054d0508 100644 --- a/apps/web/e2e/tests/status/admin-maintenance.spec.ts +++ b/apps/web/e2e/tests/status/admin-maintenance.spec.ts @@ -34,6 +34,7 @@ import { serviceRow, toggleService, tryCreateIncident, + deleteMadeIncidents, } from '../../product/status' test.describe.configure({ timeout: 60_000 }) @@ -44,6 +45,7 @@ test.beforeEach(async ({ playwright }) => { admin = await personApi(playwright, 'admin') }) test.afterEach(async () => { + await deleteMadeIncidents(admin) await admin.dispose() }) diff --git a/apps/web/e2e/tests/status/admin-services.spec.ts b/apps/web/e2e/tests/status/admin-services.spec.ts index 3d8f3f7036..4ad09fbb4d 100644 --- a/apps/web/e2e/tests/status/admin-services.spec.ts +++ b/apps/web/e2e/tests/status/admin-services.spec.ts @@ -27,6 +27,7 @@ import { serviceEvents, serviceRow, setServiceStatus, + deleteMadeIncidents, } from '../../product/status' test.describe.configure({ timeout: 60_000 }) @@ -37,6 +38,7 @@ test.beforeEach(async ({ playwright }) => { admin = await personApi(playwright, 'admin') }) test.afterEach(async () => { + await deleteMadeIncidents(admin) await admin.dispose() }) diff --git a/apps/web/e2e/tests/status/admin-templates.spec.ts b/apps/web/e2e/tests/status/admin-templates.spec.ts index f4debeeef5..50f3538a22 100644 --- a/apps/web/e2e/tests/status/admin-templates.spec.ts +++ b/apps/web/e2e/tests/status/admin-templates.spec.ts @@ -20,6 +20,7 @@ import { must, openAdmin, searchList, + deleteMadeIncidents, } from '../../product/status' test.describe.configure({ timeout: 60_000 }) @@ -30,6 +31,7 @@ test.beforeEach(async ({ playwright }) => { admin = await personApi(playwright, 'admin') }) test.afterEach(async () => { + await deleteMadeIncidents(admin) await admin.dispose() }) diff --git a/apps/web/e2e/tests/status/emails.spec.ts b/apps/web/e2e/tests/status/emails.spec.ts new file mode 100644 index 0000000000..a0db1edc75 --- /dev/null +++ b/apps/web/e2e/tests/status/emails.spec.ts @@ -0,0 +1,548 @@ +/** + * Status publish emails and bells: who gets them, what they say, and the ways + * to stop them. + * + * Journeys: STA-S-10, STA-S-11, STA-S-12, STA-S-13, STA-S-14, STA-S-15, + * STA-S-16, STA-S-19, STA-S-20, STA-S-21, STA-S-22, STA-S-23, STA-S-24, + * STA-S-26, STA-S-29, STA-S-30. + * + * A publish emails every matching subscriber in the workspace, so each test + * subscribes its own fresh people, finds mail by recipient and a marker in the + * subject, and unsubscribes them at the end. "Gets nothing" is always proved + * against a later mail or bell the same person (or a sibling) must get. + */ +import type { APIRequestContext } from '@playwright/test' +import { test, expect } from '../../support/fixtures' +import { callServerFn, personApi } from '../../support/server-fns' +import { pollFor, supportDb } from '../../support/db' +import { + getMailHeaders, + linksIn, + mailTo, + waitForEmailLog, + waitForMail, +} from '../../support/mailpit' +import { waitForEventHandled } from '../../support/bells' +import { blockPrincipal } from '../../support/email-journeys' +import { runSweep } from '../../support/sweeps' +import { + createIncident, + createSegment, + createService, + deleteMadeIncidents, + domainEvents, + incidentRow, + mintPortalUser, + must, + openAdmin, + postUpdate, + statusBells, + subscribeAs, + unsubscribeAs, + type PortalUser, +} from '../../product/status' + +test.describe.configure({ timeout: 60_000 }) + +let admin: APIRequestContext +let users: PortalUser[] = [] + +test.beforeEach(async ({ playwright }) => { + admin = await personApi(playwright, 'admin') + users = [] +}) +test.afterEach(async () => { + for (const u of users) { + await unsubscribeAs(u) + await u.api.dispose() + } + await deleteMadeIncidents(admin) + await admin.dispose() +}) + +async function subscriber( + playwright: Parameters[0], + marker: string, + label: string, + scope: 'page' | 'components' = 'page', + componentIds: string[] = [] +): Promise { + const u = await mintPortalUser(playwright, marker, label) + users.push(u) + await subscribeAs(u, scope, componentIds) + return u +} + +const mailsWith = async (to: string, subject: string) => + (await mailTo(to)).filter((m) => m.Subject.includes(subject)) + +const text = (m: { Text: string }) => m.Text.replace(/\s+/g, ' ') + +/** Publish a notifying incident and wait until its fan-out is done. */ +async function publish( + title: string, + services: [string, Parameters[1]['services'][0][1]][] +) { + const inc = await createIncident(admin, { title, services, notifySubscribers: true }) + await waitForEventHandled(inc.id, { type: 'status.incident_created' }) + return inc +} + +test( + 'STA-S-10 an incident publish emails page-wide subscribers', + { tag: '@smoke' }, + async ({ playwright, marker }) => { + const a = await createService(admin, `${marker} Web`) + const b = await createService(admin, `${marker} Db`) + const sub = await subscriber(playwright, marker, 'page') + const inc = await createIncident(admin, { + title: `${marker} outage`, + body: `${marker} line one\nline two`, + services: [ + [a.id, 'degraded_performance'], + [b.id, 'major_outage'], + ], + notifySubscribers: true, + }) + const mail = await waitForMail({ to: sub.email, subject: `Incident: ${marker} outage` }) + expect(mail.Subject).toBe(`Incident: ${marker} outage`) + const body = text(mail) + expect(body).toMatch(/New incident reported/i) + expect(body).toContain('just posted an update to its status page.') + expect(body).toContain('Critical impact · Investigating') + expect(body).toContain(`${marker} line one`) + expect(body).toContain('Affected services') + expect(body).toContain(`${marker} Web: Degraded performance`) + expect(body).toContain(`${marker} Db: Major outage`) + expect(body).toContain("You received this email because you're subscribed to status updates.") + const links = linksIn(mail) + expect(links.some((l) => l.endsWith(`/status/${inc.id}`))).toBe(true) + expect(links.some((l) => /\/unsubscribe\?token=[0-9a-f-]{36}/.test(l))).toBe(true) + const headers = await getMailHeaders(mail.ID) + expect(headers['List-Unsubscribe']?.[0]).toMatch(/unsubscribe\?token=/) + await waitForEmailLog(sub.email, { email_type: 'StatusIncidentPublishedEmail' }) + } +) + +test( + 'STA-S-11 an incident publish rings a bell for subscribers only', + { tag: '@core' }, + async ({ playwright, marker }) => { + const svc = await createService(admin, `${marker} Bell`) + const sub = await subscriber(playwright, marker, 'bell') + const other = await mintPortalUser(playwright, marker, 'none') + users.push(other) + const inc = await publish(`${marker} bell incident`, [[svc.id, 'major_outage']]) + const bells = await pollFor('the bell', async () => { + const rows = await statusBells(sub.principalUuid, inc.id) + return rows.length ? rows : null + }) + expect(bells).toEqual([ + expect.objectContaining({ + type: 'status_incident', + title: `New incident: ${marker} bell incident`, + body: 'Investigating', + }), + ]) + const [meta] = await supportDb()<{ metadata: Record }[]>` + SELECT metadata FROM in_app_notifications + WHERE principal_id = ${sub.principalUuid} AND metadata::text LIKE ${`%${inc.id}%`}` + expect(meta.metadata).toMatchObject({ + incidentId: inc.id, + incidentTitle: `${marker} bell incident`, + kind: 'incident', + impact: 'critical', + }) + // The fan-out is done (waitForEventHandled): a non-subscriber has none. + expect(await statusBells(other.principalUuid, inc.id)).toEqual([]) + } +) + +test( + 'STA-S-12 a scheduled window emails and rings subscribers', + { tag: '@core' }, + async ({ playwright, marker }) => { + const svc = await createService(admin, `${marker} Maint`) + const sub = await subscriber(playwright, marker, 'maint') + const day = new Date(Date.now() + 2 * 86_400_000) + const start = new Date( + Date.UTC(day.getUTCFullYear(), day.getUTCMonth(), day.getUTCDate(), 2, 0) + ) + const end = new Date(start.getTime() + 2 * 3_600_000) + const w = await createIncident(admin, { + kind: 'maintenance', + title: `${marker} upgrade`, + body: `${marker} planned work`, + services: [[svc.id, 'under_maintenance']], + scheduledStartAt: start, + scheduledEndAt: end, + notifySubscribers: true, + }) + const mail = await waitForMail({ + to: sub.email, + subject: `Scheduled maintenance: ${marker} upgrade`, + }) + const body = text(mail) + expect(body).toContain('has scheduled maintenance that may affect its services.') + expect(body).toContain(`${marker} planned work`) + expect(body).toContain('Maintenance window') + const dateWords = start.toLocaleDateString('en-US', { + month: 'long', + day: 'numeric', + year: 'numeric', + timeZone: 'UTC', + }) + expect(body).toContain(`${dateWords} at 02:00 UTC`) + expect(body).toContain('04:00 UTC') + expect(body).toContain(`${marker} Maint`) + expect(linksIn(mail).some((l) => l.endsWith(`/status/${w.id}`))).toBe(true) + const bells = await pollFor('the bell', async () => { + const rows = await statusBells(sub.principalUuid, w.id) + return rows.length ? rows : null + }) + expect(bells).toEqual([ + expect.objectContaining({ + title: `Scheduled maintenance: ${marker} upgrade`, + body: 'Scheduled', + }), + ]) + } +) + +test( + 'STA-S-13 updates, resolve, start and complete never email or ring', + { tag: '@core' }, + async ({ playwright, marker }) => { + const svc = await createService(admin, `${marker} Quiet`) + const sub = await subscriber(playwright, marker, 'quiet') + const inc = await publish(`${marker} quiet incident`, [[svc.id, 'major_outage']]) + await waitForMail({ to: sub.email, subject: `${marker} quiet incident` }) + await postUpdate(admin, inc.id, 'identified', 'found') + await postUpdate(admin, inc.id, 'resolved', 'fixed') + await postUpdate(admin, inc.id, 'investigating', 'back') + const w = await createIncident(admin, { + kind: 'maintenance', + title: `${marker} quiet window`, + services: [[svc.id, 'under_maintenance']], + scheduledStartAt: new Date(Date.now() + 3_600_000), + scheduledEndAt: new Date(Date.now() + 7_200_000), + notifySubscribers: true, + }) + await waitForMail({ to: sub.email, subject: `${marker} quiet window` }) + await must(admin, 'startStatusMaintenanceNowFn', { id: w.id }) + await postUpdate(admin, w.id, 'completed', 'done') + for (const id of [inc.id, w.id]) { + for (const type of [ + 'status.incident_updated', + 'status.maintenance_started', + 'status.maintenance_completed', + ]) { + if ((await domainEvents(id)).some((e) => e.type === type)) + await waitForEventHandled(id, { type }) + } + } + // A later publish the subscriber does get bounds the wait. + await publish(`${marker} sentinel`, [[svc.id, 'degraded_performance']]) + await waitForMail({ to: sub.email, subject: `${marker} sentinel` }) + expect(await mailsWith(sub.email, `${marker} quiet incident`)).toHaveLength(1) + expect(await mailsWith(sub.email, `${marker} quiet window`)).toHaveLength(1) + expect(await statusBells(sub.principalUuid, inc.id)).toHaveLength(1) + expect(await statusBells(sub.principalUuid, w.id)).toHaveLength(1) + } +) + +test( + 'STA-S-14 publishing with "Email subscribers" off sends nothing', + { tag: '@core' }, + async ({ asPerson, playwright, marker }) => { + const svc = await createService(admin, `${marker} Off`) + const sub = await subscriber(playwright, marker, 'off') + const inc = await createIncident(admin, { + title: `${marker} silent`, + services: [[svc.id, 'major_outage']], + notifySubscribers: false, + }) + const row = await incidentRow(inc.id) + expect(row.notify_subscribers).toBe(false) + expect(row.notified_at).not.toBeNull() + await waitForEventHandled(inc.id, { type: 'status.incident_created' }) + const created = (await domainEvents(inc.id)).find((e) => e.type === 'status.incident_created') + expect(created?.payload).toMatchObject({ incident: { notifySubscribers: false } }) + await publish(`${marker} sentinel`, [[svc.id, 'degraded_performance']]) + await waitForMail({ to: sub.email, subject: `${marker} sentinel` }) + expect(await mailsWith(sub.email, `${marker} silent`)).toEqual([]) + expect(await statusBells(sub.principalUuid, inc.id)).toEqual([]) + const admInc = await must<{ notifiedSubscriberCount: number | null }>( + admin, + 'getStatusIncidentAdminFn', + { id: inc.id }, + { method: 'GET' } + ) + expect(admInc.notifiedSubscriberCount).toBeNull() + const ui = await asPerson('admin') + await openAdmin(ui, `/admin/status?view=open&incident=${inc.id}`) + await expect( + ui.getByText( + 'Subscribers were not emailed: "Email subscribers" was off when this was published. Updates appear on the status page and in-app.' + ) + ).toBeVisible() + } +) + +test( + 'STA-S-15 component-scoped subscribers hear only about their services', + { tag: '@core' }, + async ({ playwright, marker }) => { + const a = await createService(admin, `${marker} A`) + const b = await createService(admin, `${marker} B`) + const follower = await subscriber(playwright, marker, 'follow', 'components', [a.id]) + const onlyB = await publish(`${marker} only B`, [[b.id, 'major_outage']]) + await publish(`${marker} A and B`, [ + [a.id, 'degraded_performance'], + [b.id, 'major_outage'], + ]) + const mail = await waitForMail({ to: follower.email, subject: `${marker} A and B` }) + expect(text(mail)).toContain(`${marker} A: Degraded performance`) + expect(text(mail)).toContain(`${marker} B: Major outage`) + expect(await mailsWith(follower.email, `${marker} only B`)).toEqual([]) + expect(await statusBells(follower.principalUuid, onlyB.id)).toEqual([]) + } +) + +test( + 'STA-S-16 recipients see only what the page shows them', + { tag: '@core' }, + async ({ playwright, marker }) => { + const outsider = await subscriber(playwright, marker, 'outsider') + const member = await subscriber(playwright, marker, 'member') + const segment = await createSegment(marker, [member.principalUuid]) + const g = await createService(admin, `${marker} G`, { segmentIds: [segment.id] }) + const p = await createService(admin, `${marker} P`) + await publish(`${marker} mixed`, [ + [g.id, 'major_outage'], + [p.id, 'degraded_performance'], + ]) + const outMail = text(await waitForMail({ to: outsider.email, subject: `${marker} mixed` })) + expect(outMail).toContain('Minor impact') + expect(outMail).toContain(`${marker} P: Degraded performance`) + expect(outMail).not.toContain(`${marker} G`) + const memMail = text(await waitForMail({ to: member.email, subject: `${marker} mixed` })) + expect(memMail).toContain('Critical impact') + expect(memMail).toContain(`${marker} G: Major outage`) + + const gOnly = await publish(`${marker} G only`, [[g.id, 'major_outage']]) + await waitForMail({ to: member.email, subject: `${marker} G only` }) + expect(await mailsWith(outsider.email, `${marker} G only`)).toEqual([]) + expect(await statusBells(outsider.principalUuid, gOnly.id)).toEqual([]) + } +) + +test( + 'STA-S-19 STA-S-20 blocked and unsubscribed people get nothing', + { tag: '@core' }, + async ({ playwright, marker }) => { + const svc = await createService(admin, `${marker} Stop`) + const blocked = await subscriber(playwright, marker, 'blocked') + const gone = await subscriber(playwright, marker, 'gone') + const kept = await subscriber(playwright, marker, 'kept') + await blockPrincipal(blocked.principalUuid) + await unsubscribeAs(gone) + const inc = await publish(`${marker} after stop`, [[svc.id, 'major_outage']]) + await waitForMail({ to: kept.email, subject: `${marker} after stop` }) + for (const u of [blocked, gone]) { + expect(await mailsWith(u.email, `${marker} after stop`), u.email).toEqual([]) + } + expect(await statusBells(gone.principalUuid, inc.id)).toEqual([]) + } +) + +test( + 'STA-S-21 unsubscribe from the email link', + { tag: '@core' }, + async ({ playwright, page, marker }) => { + const svc = await createService(admin, `${marker} Link`) + const sub = await subscriber(playwright, marker, 'link') + await publish(`${marker} with link`, [[svc.id, 'degraded_performance']]) + const mail = await waitForMail({ to: sub.email, subject: `${marker} with link` }) + const link = linksIn(mail).find((l) => l.includes('/unsubscribe?token='))! + const url = new URL(link) + await page.goto(url.pathname + url.search) + await expect(page.getByText('Unsubscribe from status updates?')).toBeVisible() + await expect(page.getByText('You will stop getting status page emails.')).toBeVisible() + // The GET changed nothing. + const [before] = + await supportDb()`SELECT unsubscribed_at FROM status_subscriptions WHERE principal_id = ${sub.principalUuid}` + expect(before.unsubscribed_at).toBeNull() + await page.waitForLoadState('networkidle') + await page.getByRole('button', { name: 'Unsubscribe' }).click() + await expect( + page.getByText('You will not get more status page emails. You can subscribe again any time.') + ).toBeVisible() + const [after] = + await supportDb()`SELECT unsubscribed_at FROM status_subscriptions WHERE principal_id = ${sub.principalUuid}` + expect(after.unsubscribed_at).not.toBeNull() + await page.goto(url.pathname + url.search) + await expect(page.getByText('This link has expired')).toBeVisible() + } +) + +test( + 'STA-S-22 one-click unsubscribe', + { tag: '@nightly' }, + async ({ playwright, request, marker }) => { + const svc = await createService(admin, `${marker} One click`) + const sub = await subscriber(playwright, marker, 'oneclick') + await publish(`${marker} one click`, [[svc.id, 'degraded_performance']]) + const mail = await waitForMail({ to: sub.email, subject: `${marker} one click` }) + const url = new URL(linksIn(mail).find((l) => l.includes('/unsubscribe?token='))!) + const path = url.pathname + url.search + const wrong = await request.post(path, { form: { other: 'x' } }) + expect(wrong.status()).toBe(400) + expect(await wrong.text()).toBe('Expected List-Unsubscribe=One-Click') + const ok = await request.post(path, { form: { 'List-Unsubscribe': 'One-Click' } }) + expect(ok.status()).toBe(200) + expect(await ok.text()).toBe('OK') + expect(ok.headers()['content-type']).toMatch(/text\/plain/) + expect(ok.headers()['cache-control']).toMatch(/no-store/) + const [row] = + await supportDb()`SELECT unsubscribed_at FROM status_subscriptions WHERE principal_id = ${sub.principalUuid}` + expect(row.unsubscribed_at).not.toBeNull() + const unknown = await request.post('/unsubscribe?token=00000000-0000-4000-8000-000000000000', { + form: { 'List-Unsubscribe': 'One-Click' }, + }) + expect(unknown.status()).toBe(200) + } +) + +test( + 'STA-S-23 nothing is emailed for an incident or window created already over', + { + tag: ['@nightly', '@quarantine'], + annotation: { + type: 'quarantine', + description: + 'createIncident (status.service.ts:242, :317) defaults notifySubscribers to true for a non-backfill row created resolved or completed, so subscribers are emailed about something already over; see QUARANTINE.md (STA-A-30)', + }, + }, + async ({ playwright, marker }) => { + const svc = await createService(admin, `${marker} Over`) + const sub = await subscriber(playwright, marker, 'over') + await createIncident(admin, { + title: `${marker} already resolved`, + status: 'resolved', + services: [[svc.id, 'major_outage']], + notifySubscribers: true, + }) + await publish(`${marker} sentinel`, [[svc.id, 'degraded_performance']]) + await waitForMail({ to: sub.email, subject: `${marker} sentinel` }) + expect(await mailsWith(sub.email, `${marker} already resolved`)).toEqual([]) + } +) + +test( + 'STA-S-24 backfilled incidents never notify, even after the notify sweep', + { tag: '@core' }, + async ({ playwright, request, marker }) => { + const svc = await createService(admin, `${marker} Past`) + const sub = await subscriber(playwright, marker, 'past') + const past = await createIncident(admin, { + title: `${marker} backfilled`, + status: 'resolved', + services: [[svc.id, 'major_outage']], + backfill: { + startedAt: new Date(Date.now() - 86_400_000), + resolvedAt: new Date(Date.now() - 82_800_000), + }, + }) + await supportDb()`UPDATE status_incidents SET created_at = now() - interval '10 minutes' + WHERE id = ${(await incidentRow(past.id)).id}` + await runSweep(request, 'status-notify') + await publish(`${marker} sentinel`, [[svc.id, 'degraded_performance']]) + await waitForMail({ to: sub.email, subject: `${marker} sentinel` }) + expect(await mailsWith(sub.email, `${marker} backfilled`)).toEqual([]) + expect(await statusBells(sub.principalUuid, past.id)).toEqual([]) + expect(await domainEvents(past.id)).toEqual([]) + expect((await incidentRow(past.id)).notified_at).toBeNull() + } +) + +test( + 'STA-S-26 the notify reconcile announces an unclaimed row once', + { tag: '@nightly' }, + async ({ playwright, request, marker }) => { + const svc = await createService(admin, `${marker} Missed`) + const sub = await subscriber(playwright, marker, 'missed') + const inc = await createIncident(admin, { + title: `${marker} missed`, + services: [[svc.id, 'degraded_performance']], + notifySubscribers: false, + }) + // As an older version would have left it: unclaimed, asking for the email, 10 minutes old. + const dbId = (await incidentRow(inc.id)).id + await supportDb()`DELETE FROM events WHERE entity_id = ${inc.id}` + await supportDb()`UPDATE status_incidents + SET notified_at = NULL, notify_subscribers = true, created_at = now() - interval '10 minutes' + WHERE id = ${dbId}` + await runSweep(request, 'status-notify') + expect((await incidentRow(inc.id)).notified_at).not.toBeNull() + await waitForMail({ to: sub.email, subject: `${marker} missed` }) + await runSweep(request, 'status-notify') + await waitForEventHandled(inc.id, { type: 'status.incident_created' }) + expect( + (await domainEvents(inc.id)).filter((e) => e.type === 'status.incident_created') + ).toHaveLength(1) + await publish(`${marker} sentinel`, [[svc.id, 'degraded_performance']]) + await waitForMail({ to: sub.email, subject: `${marker} sentinel` }) + expect(await mailsWith(sub.email, `${marker} missed`)).toHaveLength(1) + } +) + +test( + 'STA-S-29 the editor\'s "emailed ~N" count follows the pool', + { tag: '@nightly' }, + async ({ playwright, marker }) => { + const svc = await createService(admin, `${marker} Count`) + await subscriber(playwright, marker, 'c1', 'components', [svc.id]) + await subscriber(playwright, marker, 'c2', 'components', [svc.id]) + const inc = await publish(`${marker} counted`, [[svc.id, 'degraded_performance']]) + const read = async () => + ( + await must<{ notifiedSubscriberCount: number | null }>( + admin, + 'getStatusIncidentAdminFn', + { id: inc.id }, + { method: 'GET' } + ) + ).notifiedSubscriberCount + const first = await read() + expect(first).toBeGreaterThanOrEqual(2) + await subscriber(playwright, marker, 'c3', 'components', [svc.id]) + expect(await read()).toBeGreaterThanOrEqual((first ?? 0) + 1) + } +) + +test( + 'STA-S-30 an incident cannot be pinned to the "maintenance" impact', + { + tag: ['@nightly', '@quarantine'], + annotation: { + type: 'quarantine', + description: + 'Incident impact "maintenance" is accepted (REST -validation.ts:33 and the server fn), and the incident email template has no label or colour for it (status-incident-published.tsx:29); see QUARANTINE.md', + }, + }, + async ({ marker }) => { + const svc = await createService(admin, `${marker} Pin`) + const res = await callServerFn(admin, 'createStatusIncidentFn', { + kind: 'incident', + title: marker, + status: 'investigating', + body: 'x', + impact: 'maintenance', + impactOverride: true, + notifySubscribers: false, + affectedComponents: [{ componentId: svc.id, componentStatus: 'major_outage' }], + }) + expect(res.ok).toBe(false) + } +) diff --git a/apps/web/e2e/tests/status/feed.spec.ts b/apps/web/e2e/tests/status/feed.spec.ts new file mode 100644 index 0000000000..334f57e802 --- /dev/null +++ b/apps/web/e2e/tests/status/feed.spec.ts @@ -0,0 +1,159 @@ +/** + * The public status RSS feed (/status/feed). + * + * Journeys: STA-P-20, STA-P-21. The cap, escaping and section-host journeys + * (STA-P-22, STA-P-24, STA-P-25) are proved by src/routes/status/__tests__/feed.test.ts. + * + * The feed is workspace-wide and capped at 50 items: each test reads it right + * after writing and finds its own items by marker. + */ +import type { APIRequestContext } from '@playwright/test' +import { test, expect } from '../../support/fixtures' +import { personApi } from '../../support/server-fns' +import { supportEnv } from '../../support/env' +import { + createIncident, + createService, + must, + postUpdate, + scheduleWindow, + deleteMadeIncidents, +} from '../../product/status' + +let admin: APIRequestContext +let reader: APIRequestContext + +test.beforeEach(async ({ playwright }) => { + admin = await personApi(playwright, 'admin') + reader = await playwright.request.newContext({ baseURL: supportEnv().baseUrl }) +}) +test.afterEach(async () => { + await deleteMadeIncidents(admin) + await admin.dispose() + await reader.dispose() +}) + +interface FeedItem { + title: string + link: string + guid: string + description: string +} + +function decode(text: string): string { + return text + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/"/g, '"') + .replace(/'/g, "'") + .replace(/&/g, '&') +} + +async function readFeed(): Promise<{ + xml: string + items: FeedItem[] + headers: Record +}> { + const res = await reader.get('/status/feed') + expect(res.status()).toBe(200) + const xml = await res.text() + const items = [...xml.matchAll(/([\s\S]*?)<\/item>/g)].map(([, body]) => { + const pick = (tag: string) => + decode(new RegExp(`<${tag}[^>]*>([\\s\\S]*?)`).exec(body)?.[1] ?? '') + return { + title: pick('title'), + link: pick('link'), + guid: pick('guid'), + description: pick('description'), + } + }) + return { xml, items, headers: res.headers() } +} + +test('STA-P-20 the RSS feed lists every update', { tag: '@core' }, async ({ marker }) => { + const a = await createService(admin, `${marker} Alpha`) + const b = await createService(admin, `${marker} Beta`) + const inc = await createIncident(admin, { + title: `${marker} feed incident`, + services: [ + [a.id, 'major_outage'], + [b.id, 'degraded_performance'], + ], + body: `${marker} first update`, + }) + await postUpdate(admin, inc.id, 'identified', `${marker} second update`) + const w = await scheduleWindow(admin, { + title: `${marker} feed window`, + services: [[a.id, 'under_maintenance']], + body: `${marker} window plan`, + }) + + const { xml, items, headers } = await readFeed() + expect(headers['content-type']).toBe('application/rss+xml; charset=utf-8') + expect(headers['cache-control']).toBe('private, max-age=300') + expect(headers['vary']).toMatch(/Cookie/) + expect(xml).toMatch(/\s*.+ Status<\/title>/) + expect(xml).toMatch(/<description>Incidents and maintenance for .+<\/description>/) + expect(xml).toMatch( + /<atom:link[^>]*href="[^"]*\/status\/feed"[^>]*rel="self"|<atom:link[^>]*rel="self"[^>]*href="[^"]*\/status\/feed"/ + ) + expect(xml).toContain('<language>en-us</language>') + + const mine = items.filter((i) => i.title.includes(marker)) + expect(mine.map((i) => i.title)).toEqual([ + `Maintenance: ${marker} feed window (Scheduled)`, + `${marker} feed incident (Identified)`, + `${marker} feed incident (Investigating)`, + ]) + const [windowItem, second, first] = mine + expect(first.link).toMatch(new RegExp(`/status/${inc.id}$`)) + expect(first.guid).toMatch(new RegExp(`^.*/status/${inc.id}#status_update_`)) + expect(second.guid).not.toBe(first.guid) + expect(first.description).toContain(`${marker} first update`) + // The services' order is not fixed (the links are read without an ORDER BY). + const affected = / · Affected: (.+)$/.exec(first.description)?.[1].split(', ') ?? [] + expect(affected.sort()).toEqual([`${marker} Alpha`, `${marker} Beta`]) + expect(windowItem.link).toMatch(new RegExp(`/status/${w.id}$`)) + expect(windowItem.description).toMatch(/Window: .+ to .+ UTC/) + expect(xml).toMatch(/<guid isPermaLink="false">/) +}) + +test( + 'STA-P-21 the feed announces a details edit made after the latest update', + { tag: '@nightly' }, + async ({ marker }) => { + const svc = await createService(admin, `${marker} Edited`) + const inc = await createIncident(admin, { + title: `${marker} before`, + services: [[svc.id, 'degraded_performance']], + }) + // An impact-only change adds nothing. + await must(admin, 'updateStatusIncidentFn', { + id: inc.id, + impact: 'major', + impactOverride: true, + }) + let mine = (await readFeed()).items.filter((i) => i.link.endsWith(inc.id)) + expect(mine.filter((i) => i.guid.includes('#edited-'))).toEqual([]) + + await must(admin, 'updateStatusIncidentFn', { id: inc.id, title: `${marker} renamed once` }) + mine = (await readFeed()).items.filter((i) => i.link.endsWith(inc.id)) + const edits = mine.filter((i) => i.guid.includes('#edited-')) + expect(edits).toHaveLength(1) + expect(edits[0].title).toBe(`${marker} renamed once (Investigating)`) + expect(edits[0].description).toMatch(/^Details updated/) + + await new Promise((r) => setTimeout(r, 20)) + await must(admin, 'updateStatusIncidentFn', { id: inc.id, title: `${marker} renamed twice` }) + mine = (await readFeed()).items.filter((i) => i.link.endsWith(inc.id)) + const latest = mine.filter((i) => i.guid.includes('#edited-')) + expect(latest).toHaveLength(1) + expect(latest[0].title).toBe(`${marker} renamed twice (Investigating)`) + expect(latest[0].guid).not.toBe(edits[0].guid) + + // An update posted after the edit supersedes it. + await postUpdate(admin, inc.id, 'identified', 'found it') + mine = (await readFeed()).items.filter((i) => i.link.endsWith(inc.id)) + expect(mine.filter((i) => i.guid.includes('#edited-'))).toEqual([]) + } +) diff --git a/apps/web/e2e/tests/status/public-page.spec.ts b/apps/web/e2e/tests/status/public-page.spec.ts new file mode 100644 index 0000000000..f060a65c8b --- /dev/null +++ b/apps/web/e2e/tests/status/public-page.spec.ts @@ -0,0 +1,462 @@ +/** + * The public status page and incident pages, as visitors see them. + * + * Journeys: STA-P-01, STA-P-02, STA-P-03, STA-P-04, STA-P-08, STA-P-10, + * STA-P-11, STA-P-12, STA-P-13, STA-P-14, STA-P-15, STA-P-19, STA-P-28. + * + * The hero, the "No incidents" line and the feed cap are workspace-wide, so + * these assert only on this test's own services and incidents. + */ +import type { APIRequestContext, Page } from '@playwright/test' +import { toUuid } from '@quackback/ids' +import { test, expect } from '../../support/fixtures' +import { callServerFn, personApi } from '../../support/server-fns' +import { supportDb } from '../../support/db' +import { supportEnv } from '../../support/env' +import { + createIncident, + createSegment, + createService, + mintPortalUser, + must, + postUpdate, + publicServiceIds, + publicSnapshot, + recentIds, + scheduleWindow, + type PublicSnapshot, + deleteMadeIncidents, +} from '../../product/status' + +test.describe.configure({ timeout: 60_000 }) + +let admin: APIRequestContext +let anon: APIRequestContext + +test.beforeEach(async ({ playwright }) => { + admin = await personApi(playwright, 'admin') + anon = await playwright.request.newContext({ baseURL: supportEnv().baseUrl }) +}) +test.afterEach(async () => { + await deleteMadeIncidents(admin) + await admin.dispose() + await anon.dispose() +}) + +/** A service row on the public page: its name and status label. */ +function serviceRowOn(page: Page, name: string) { + return page.locator('div.justify-between', { has: page.getByText(name, { exact: true }) }) +} + +const uuidOf = (typeId: string) => toUuid(typeId as `status_component_${string}`) + +test( + 'STA-P-01 an anonymous visitor opens the published status page', + { tag: '@core' }, + async ({ page, marker }) => { + const group = await must<{ id: string }>(admin, 'createStatusGroupFn', { + name: `${marker} Group`, + collapsed: true, + }) + await createService(admin, `${marker} In group`, { groupId: group.id }) + const loose = await createService(admin, `${marker} Loose`) + await must(admin, 'setStatusComponentStatusFn', { + id: loose.id, + status: 'degraded_performance', + }) + + const res = await page.goto('/status') + expect(res?.status()).toBe(200) + await expect(page).toHaveTitle(/^Status - /) + expect(await page.locator('link[rel="canonical"]').getAttribute('href')).toMatch(/\/status$/) + await expect(page.getByRole('heading', { name: 'Status', exact: true })).toBeVisible() + await expect(page.getByRole('button', { name: 'Subscribe' }).first()).toBeVisible() + await expect(page.getByRole('link', { name: /RSS Feed/ })).toBeVisible() + await expect(page.getByText('Current status by service')).toBeVisible() + await expect(serviceRowOn(page, `${marker} Loose`)).toContainText('Degraded performance') + // The collapsed group is closed: its service is not listed until opened. + const groupHeader = page.getByRole('button', { name: new RegExp(`${marker} Group`) }) + await expect(groupHeader).toBeVisible() + await expect(page.getByText(`${marker} In group`, { exact: true })).toBeHidden() + // A click before hydration is lost: retry until the group opens. + await page.waitForLoadState('networkidle') + await expect(async () => { + await groupHeader.click() + await expect(serviceRowOn(page, `${marker} In group`)).toContainText('Operational', { + timeout: 2_000, + }) + }).toPass({ timeout: 15_000 }) + } +) + +test('STA-P-02 an active incident shows as a card', { tag: '@core' }, async ({ page, marker }) => { + const svc = await createService(admin, `${marker} Card`) + const inc = await createIncident(admin, { + title: `${marker} card incident`, + services: [[svc.id, 'major_outage']], + body: `${marker} latest words`, + }) + await page.goto('/status') + const card = page.getByRole('link', { name: new RegExp(`${marker} card incident`) }) + await expect(card).toBeVisible() + await expect(card).toContainText('Investigating') + await expect(card).toContainText('Critical impact') + await expect(card).toContainText(`${marker} Card`) + await expect(card).toContainText(`${marker} latest words`) + await expect(card).toContainText('Last update') + await expect(serviceRowOn(page, `${marker} Card`)).toContainText('Major outage') + expect((await publicSnapshot(anon)).activeIncidents.map((i) => i.id)).toContain(inc.id) +}) + +test('STA-P-03 the incident detail page', { tag: '@core' }, async ({ page, marker }) => { + const svc = await createService(admin, `${marker} Detail`) + const inc = await createIncident(admin, { + title: `${marker} detail incident`, + services: [[svc.id, 'partial_outage']], + body: `${marker} first`, + }) + await postUpdate(admin, inc.id, 'identified', `${marker} second`) + const res = await page.goto(`/status/${inc.id}`) + expect(res?.headers()['cache-control']).toBe('public, s-maxage=60, stale-while-revalidate=600') + await expect(page).toHaveTitle(new RegExp(`^${marker} detail incident - .* Status$`)) + await expect( + page.getByRole('heading', { level: 1, name: `${marker} detail incident` }) + ).toBeVisible() + await expect(page.getByRole('link', { name: 'Status' }).first()).toBeVisible() + await expect(page.getByText('Major impact').first()).toBeVisible() + await expect(page.getByText(/^Started /)).toBeVisible() + await expect(page.getByText('Affected:')).toBeVisible() + await expect(page.getByText('Updates', { exact: true })).toBeVisible() + // The server sends the timeline oldest first; the page shows the newest on top. + const first = page.getByText(`${marker} first`) + const second = page.getByText(`${marker} second`) + await expect(first).toBeVisible() + const [y1, y2] = [await first.boundingBox(), await second.boundingBox()] + expect(y2!.y).toBeLessThan(y1!.y) +}) + +test( + 'STA-P-04 the incident page for a missing, deleted, gated or malformed id', + { tag: '@core' }, + async ({ page, marker }) => { + const svc = await createService(admin, `${marker} Del`) + const deleted = await createIncident(admin, { + title: marker, + services: [[svc.id, 'degraded_performance']], + }) + await must(admin, 'deleteStatusIncidentFn', { id: deleted.id }) + const segment = await createSegment(marker) + const gatedSvc = await createService(admin, `${marker} Gated`, { segmentIds: [segment.id] }) + const gated = await createIncident(admin, { + title: `${marker} gated`, + services: [[gatedSvc.id, 'major_outage']], + }) + + for (const id of [ + deleted.id, + 'status_incident_01m7v6aewge02vtv3kd36ggmcw', + 'garbage', + gated.id, + ]) { + await page.goto(`/status/${id}`) + await expect(page.getByRole('heading', { name: 'Incident not found' }), id).toBeVisible() + await expect( + page.getByText("This incident may have been removed, or you don't have access to view it.") + ).toBeVisible() + await expect(page.getByRole('link', { name: 'Status' }).first()).toBeVisible() + } + for (const id of [gated.id, deleted.id]) { + const res = await callServerFn(anon, 'getStatusIncidentPublicFn', { id }, { method: 'GET' }) + expect(res.code, id).toBe('STATUS_INCIDENT_NOT_FOUND') + } + } +) + +test( + 'STA-P-08 segment-gated services and incidents per viewer', + { tag: '@core' }, + async ({ playwright, marker }) => { + const member = await mintPortalUser(playwright, marker, 'member') + const outsider = await mintPortalUser(playwright, marker, 'outsider') + try { + const segment = await createSegment(marker, [member.principalUuid]) + const g = await createService(admin, `${marker} G`, { segmentIds: [segment.id] }) + const p = await createService(admin, `${marker} P`) + const onlyG = await createIncident(admin, { + title: `${marker} only G`, + services: [[g.id, 'major_outage']], + }) + const both = await createIncident(admin, { + title: `${marker} both`, + services: [ + [g.id, 'major_outage'], + [p.id, 'degraded_performance'], + ], + }) + const view = async (api: APIRequestContext) => { + const snap: PublicSnapshot = await publicSnapshot(api) + const active = snap.activeIncidents as unknown as { + id: string + impact: string + affectedComponents: { id: string }[] + }[] + return { snap, active, both: active.find((i) => i.id === both.id) } + } + for (const api of [anon, outsider.api]) { + const { snap, active, both: b } = await view(api) + expect(publicServiceIds(snap)).not.toContain(g.id) + expect(active.map((i) => i.id)).not.toContain(onlyG.id) + expect(b?.impact).toBe('minor') + expect(b?.affectedComponents.map((c) => c.id)).toEqual([p.id]) + const detail = await callServerFn( + api, + 'getStatusIncidentPublicFn', + { id: onlyG.id }, + { method: 'GET' } + ) + expect(detail.code).toBe('STATUS_INCIDENT_NOT_FOUND') + } + for (const api of [member.api, admin]) { + const { snap, active, both: b } = await view(api) + expect(publicServiceIds(snap)).toContain(g.id) + expect(active.map((i) => i.id)).toContain(onlyG.id) + expect(b?.impact).toBe('critical') + expect(b?.affectedComponents.map((c) => c.id).sort()).toEqual([g.id, p.id].sort()) + } + } finally { + await member.api.dispose() + await outsider.api.dispose() + } + } +) + +test( + 'STA-P-10 STA-P-11 scheduled maintenance, then in progress', + { tag: '@core' }, + async ({ page, marker }) => { + const svc = await createService(admin, `${marker} Maint`) + const w = await scheduleWindow(admin, { + title: `${marker} window`, + services: [[svc.id, 'under_maintenance']], + body: `${marker} plan`, + startMin: 600, + endMin: 660, + }) + await page.goto('/status') + await expect(page.getByText('Scheduled maintenance', { exact: true })).toBeVisible() + const row = page + .locator('div', { has: page.getByRole('link', { name: `${marker} window` }) }) + .filter({ hasText: `${marker} plan` }) + .last() + await expect(page.getByRole('link', { name: `${marker} window` })).toBeVisible() + await expect(row).toContainText('Scheduled') + await expect(row).toContainText(`${marker} plan`) + await expect(serviceRowOn(page, `${marker} Maint`)).toContainText('Operational') + + await must(admin, 'startStatusMaintenanceNowFn', { id: w.id }) + await page.goto('/status') + const card = page.getByRole('link', { name: new RegExp(`${marker} window`) }) + await expect(card).toContainText('In progress') + await expect(card).toContainText('Maintenance') + await expect(serviceRowOn(page, `${marker} Maint`)).toContainText('Under maintenance') + const snap = await publicSnapshot(anon) + expect(snap.upcomingMaintenance.map((x) => x.id)).not.toContain(w.id) + await page.goto(`/status/${w.id}`) + await expect(page.getByText(/^Scheduled window: /)).toBeVisible() + } +) + +test('STA-P-12 recent incidents grouped by UTC day', { tag: '@nightly' }, async ({ marker }) => { + const svc = await createService(admin, `${marker} Recent`) + const today = await createIncident(admin, { + title: `${marker} today`, + services: [[svc.id, 'degraded_performance']], + }) + await postUpdate(admin, today.id, 'resolved', 'done') + const older = await createIncident(admin, { + title: `${marker} older`, + services: [[svc.id, 'degraded_performance']], + }) + await postUpdate(admin, older.id, 'resolved', 'done') + const threeDaysAgo = new Date(Date.now() - 3 * 86_400_000) + await supportDb()`UPDATE status_incidents SET started_at = ${threeDaysAgo} WHERE id = ${uuidOf(older.id)}` + const w = await scheduleWindow(admin, { + title: `${marker} window`, + services: [[svc.id, 'under_maintenance']], + }) + await postUpdate(admin, w.id, 'in_progress', 'go') + await postUpdate(admin, w.id, 'completed', 'done') + + const snap = await publicSnapshot(anon) + const dayOf = (id: string) => + snap.recentIncidents.find((d) => d.incidents.some((i) => i.id === id))?.date + const isoDay = (d: Date) => d.toISOString().slice(0, 10) + expect(dayOf(today.id)?.slice(0, 10)).toBe(isoDay(new Date())) + expect(dayOf(older.id)?.slice(0, 10)).toBe(isoDay(threeDaysAgo)) + const dates = snap.recentIncidents.map((d) => d.date) + expect([...dates].sort().reverse()).toEqual(dates) + expect(recentIds(snap)).not.toContain(w.id) + const again = await publicSnapshot(anon) + expect((again as unknown as { recentWindow: { start: string } }).recentWindow.start).toBe( + (snap as unknown as { recentWindow: { start: string } }).recentWindow.start + ) +}) + +test( + 'STA-P-13 incident history pages past the recent window', + { tag: '@nightly' }, + async ({ page, marker }) => { + const svc = await createService(admin, `${marker} History`) + const old = await createIncident(admin, { + title: `${marker} twenty days ago`, + status: 'resolved', + services: [[svc.id, 'major_outage']], + backfill: { + startedAt: new Date(Date.now() - 20 * 86_400_000), + resolvedAt: new Date(Date.now() - 20 * 86_400_000 + 3_600_000), + }, + }) + const w = await scheduleWindow(admin, { + title: `${marker} done window`, + services: [[svc.id, 'under_maintenance']], + }) + await postUpdate(admin, w.id, 'in_progress', 'go') + await postUpdate(admin, w.id, 'completed', 'done') + + const snap = await publicSnapshot(anon) + const before = (snap as unknown as { recentWindow: { start: string } }).recentWindow.start + const seen: string[] = [] + let cursor: string | undefined + for (let i = 0; i < 50; i++) { + const res = await must<{ + items: { id: string }[] + nextCursor: string | null + hasMore: boolean + }>(anon, 'listStatusHistoryFn', { before, ...(cursor ? { cursor } : {}) }, { method: 'GET' }) + seen.push(...res.items.map((x) => x.id)) + if (seen.includes(old.id) && seen.includes(w.id)) break + if (!res.hasMore || !res.nextCursor) break + cursor = res.nextCursor + } + expect(seen).toContain(old.id) + expect(seen).toContain(w.id) + await page.goto('/status') + await expect(page.getByRole('button', { name: 'Incident history →' })).toBeVisible() + } +) + +test('STA-P-14 uptime bars', { tag: '@core' }, async ({ page, marker }) => { + const s = await createService(admin, `${marker} S`) + const m = await createService(admin, `${marker} M`) + const h = await createService(admin, `${marker} H`, { showUptime: false }) + const now = new Date() + const yesterday = new Date( + Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() - 1) + ) + const at = (hours: number) => new Date(yesterday.getTime() + hours * 3_600_000) + const sql = supportDb() + for (const svc of [s, m, h]) { + await sql`UPDATE status_components SET created_at = ${new Date(yesterday.getTime() - 86_400_000)} + WHERE id = ${uuidOf(svc.id)}` + } + const event = (id: string, status: string, when: Date) => + sql`INSERT INTO status_component_events (id, component_id, status, source, created_at) + VALUES (gen_random_uuid(), ${uuidOf(id)}, ${status}, 'manual', ${when})` + await event(s.id, 'major_outage', at(0)) + await event(s.id, 'operational', at(6)) + await event(m.id, 'under_maintenance', at(0)) + await event(m.id, 'operational', at(24)) + + const payload = await must<{ + uptime: { + componentId: string + days: { date: string; worstStatus: string; uptimePct: number }[] + }[] + }>(anon, 'getStatusPageFn', undefined, { method: 'GET' }) + const dayOf = (id: string) => + payload.uptime + .find((u) => u.componentId === id) + ?.days.find((d) => d.date === yesterday.toISOString().slice(0, 10)) + expect(dayOf(s.id)).toMatchObject({ worstStatus: 'major_outage', uptimePct: 75 }) + expect(dayOf(m.id)?.uptimePct).toBe(100) + expect(payload.uptime.map((u) => u.componentId)).not.toContain(h.id) + const twoDaysBack = payload.uptime + .find((u) => u.componentId === s.id) + ?.days.find( + (d) => d.date === new Date(yesterday.getTime() - 5 * 86_400_000).toISOString().slice(0, 10) + ) + expect(twoDaysBack).toMatchObject({ worstStatus: 'operational', uptimePct: 100 }) + + await page.goto('/status') + await expect(page.getByLabel(`${marker} S uptime, last 90 days`)).toBeVisible() + await expect(page.getByLabel(new RegExp(`: Major outage, 75%$`)).first()).toBeAttached() + await expect(page.getByLabel(`${marker} H uptime, last 90 days`)).toHaveCount(0) +}) + +test( + 'STA-P-15 uptime follows a status that has no history row', + { + tag: ['@nightly', '@quarantine'], + annotation: { + type: 'quarantine', + description: + 'createStatusComponent writes no status_component_events row for a non-operational initial status (status.components.ts:176), and uptime starts from operational when no event exists; see QUARANTINE.md', + }, + }, + async ({ marker }) => { + const svc = await createService(admin, `${marker} Born down`, { status: 'major_outage' }) + const payload = await must<{ + uptime: { componentId: string; days: { worstStatus: string; uptimePct: number }[] }[] + }>(anon, 'getStatusPageFn', undefined, { method: 'GET' }) + const today = payload.uptime.find((u) => u.componentId === svc.id)?.days.at(-1) + expect(today?.worstStatus).toBe('major_outage') + } +) + +test( + 'STA-P-19 public status documents are cached for anonymous visitors only', + { tag: '@nightly' }, + async ({ playwright, marker }) => { + const svc = await createService(admin, `${marker} Cache`) + const inc = await createIncident(admin, { + title: marker, + services: [[svc.id, 'degraded_performance']], + }) + const header = 'public, s-maxage=60, stale-while-revalidate=600' + for (const path of ['/status', `/status/${inc.id}`]) { + const res = await anon.get(path) + expect(res.headers()['cache-control'], path).toBe(header) + } + const user = await mintPortalUser(playwright, marker) + try { + for (const path of ['/status', `/status/${inc.id}`]) { + const res = await user.api.get(path) + expect(res.status()).toBe(200) + expect(res.headers()['cache-control'] ?? '', path).not.toBe(header) + } + } finally { + await user.api.dispose() + } + } +) + +test( + 'STA-P-28 teammates see the whole page on the portal', + { tag: '@nightly' }, + async ({ asPerson, marker }) => { + const segment = await createSegment(marker) + const g = await createService(admin, `${marker} Team only`, { segmentIds: [segment.id] }) + const inc = await createIncident(admin, { + title: `${marker} team incident`, + services: [[g.id, 'major_outage']], + }) + const ui = await asPerson('contributor') + await ui.goto('/status') + await expect(serviceRowOn(ui, `${marker} Team only`)).toContainText('Major outage') + await ui.goto(`/status/${inc.id}`) + await expect( + ui.getByRole('heading', { level: 1, name: `${marker} team incident` }) + ).toBeVisible() + await expect(ui.getByText('Critical impact').first()).toBeVisible() + } +) diff --git a/apps/web/e2e/tests/status/subscribe.spec.ts b/apps/web/e2e/tests/status/subscribe.spec.ts new file mode 100644 index 0000000000..447b3620e1 --- /dev/null +++ b/apps/web/e2e/tests/status/subscribe.spec.ts @@ -0,0 +1,272 @@ +/** + * Self-serve status subscriptions from the public page. + * + * Journeys: STA-S-01, STA-S-02, STA-S-03, STA-S-04, STA-S-05, STA-S-06, + * STA-S-07, STA-S-09. + * + * One subscription row per person: every test mints its own portal users and + * unsubscribes them at the end (a page-wide subscriber gets every other + * test's publish email). + */ +import type { APIRequestContext } from '@playwright/test' +import { test, expect } from '../../support/fixtures' +import { callServerFn, personApi } from '../../support/server-fns' +import { pollFor } from '../../support/db' +import { supportEnv } from '../../support/env' +import { portalPage } from '../../support/widget-portal' +import { + createService, + deleteMadeIncidents, + messageOf, + mintPortalUser, + must, + subscribeAs, + subscriptionRow, + unsubscribeAs, + type PortalUser, +} from '../../product/status' + +test.describe.configure({ timeout: 60_000 }) + +let admin: APIRequestContext +let users: PortalUser[] = [] + +test.beforeEach(async ({ playwright }) => { + admin = await personApi(playwright, 'admin') + users = [] +}) +test.afterEach(async () => { + for (const u of users) { + await unsubscribeAs(u) + await u.api.dispose() + } + await deleteMadeIncidents(admin) + await admin.dispose() +}) + +async function user( + playwright: Parameters<typeof mintPortalUser>[0], + marker: string, + label = 'sub' +) { + const u = await mintPortalUser(playwright, marker, label) + users.push(u) + return u +} + +test( + 'STA-S-01 a signed-in user subscribes to all updates', + { tag: '@core' }, + async ({ browser, playwright, marker }) => { + const u = await user(playwright, marker) + const page = await portalPage(browser, u) + await page.goto('/status') + await page.waitForLoadState('networkidle') + await page.getByRole('button', { name: 'Subscribe', exact: true }).first().click() + const dialog = page.getByRole('dialog', { name: 'Subscribe to updates' }) + await expect( + dialog.getByText("We'll email you when incidents and maintenance are posted.") + ).toBeVisible() + await expect(dialog.getByRole('radio', { name: /All updates/ })).toBeChecked() + await dialog.getByRole('button', { name: 'Subscribe', exact: true }).click() + await expect(page.getByText('Subscribed to status updates')).toBeVisible() + await expect(page.getByRole('button', { name: 'Subscribed' })).toBeVisible() + expect(await subscriptionRow(u.principalUuid)).toMatchObject({ + scope: 'page', + component_ids: [], + source: 'self_serve', + unsubscribed_at: null, + }) + const mine = await must<{ subscribed: boolean }>( + u.api, + 'getMyStatusSubscriptionFn', + undefined, + { method: 'GET' } + ) + expect(mine.subscribed).toBe(true) + await page.context().close() + } +) + +test( + 'STA-S-02 subscribe to specific services', + { tag: '@core' }, + async ({ browser, playwright, marker }) => { + const a = await createService(admin, `${marker} Pick A`) + const b = await createService(admin, `${marker} Pick B`) + const u = await user(playwright, marker) + const page = await portalPage(browser, u) + await page.goto('/status') + await page.waitForLoadState('networkidle') + await page.getByRole('button', { name: 'Subscribe', exact: true }).first().click() + const dialog = page.getByRole('dialog', { name: 'Subscribe to updates' }) + await dialog.getByRole('radio', { name: /Specific services/ }).click() + const submit = dialog.getByRole('button', { name: 'Subscribe', exact: true }) + await expect(submit).toBeDisabled() + await dialog.getByRole('checkbox', { name: a.name }).check() + await dialog.getByRole('checkbox', { name: b.name }).check() + await submit.click() + await expect(page.getByText('Subscribed to status updates')).toBeVisible() + const row = await subscriptionRow(u.principalUuid) + expect(row?.scope).toBe('components') + expect([...(row?.component_ids ?? [])].sort()).toEqual([a.id, b.id].sort()) + await page.context().close() + } +) + +test( + 'STA-S-03 STA-S-04 an anonymous visitor is sent through sign-in and subscribed on return', + { tag: '@core' }, + async ({ browser, page, playwright, marker }) => { + await page.goto('/status') + await page.waitForLoadState('networkidle') + await page.getByRole('button', { name: 'Subscribe', exact: true }).first().click() + const dialog = page.getByRole('dialog', { name: 'Subscribe to updates' }) + await expect( + dialog.getByText(/Email updates need a free portal account\. Or follow the/) + ).toBeVisible() + await expect(dialog.getByRole('link', { name: 'RSS feed' })).toHaveAttribute( + 'href', + /\/status\/feed$/ + ) + await dialog.getByRole('button', { name: 'Subscribe', exact: true }).click() + await expect(page.getByText('Sign in to get status updates by email.')).toBeVisible() + const saved = await page.evaluate(() => + localStorage.getItem('quackback:pending-status-subscription') + ) + expect(JSON.parse(saved ?? '{}')).toMatchObject({ scope: 'page' }) + + // A sign-in that left the page comes back to ?subscribe=resume: the saved + // choice completes and the marker leaves the URL. + const u = await user(playwright, marker) + const signedIn = await portalPage(browser, u) + await signedIn.goto('/status') + await signedIn.evaluate( + (value) => localStorage.setItem('quackback:pending-status-subscription', value), + JSON.stringify({ scope: 'page', componentIds: [], savedAt: Date.now() }) + ) + await signedIn.bringToFront() + await signedIn.goto('/status?subscribe=resume') + // The session is read on the client first, so the resume can take a while. + await expect(signedIn.getByText('Subscribed to status updates')).toBeVisible({ + timeout: 20_000, + }) + await expect(signedIn).not.toHaveURL(/subscribe=resume/) + await pollFor('the subscription', async () => + (await subscriptionRow(u.principalUuid))?.unsubscribed_at === null ? true : null + ) + + // A choice older than 30 minutes is dropped. + const late = await user(playwright, marker, 'late') + const latePage = await portalPage(browser, late) + await latePage.goto('/status') + await latePage.evaluate( + (value) => localStorage.setItem('quackback:pending-status-subscription', value), + JSON.stringify({ scope: 'page', componentIds: [], savedAt: Date.now() - 31 * 60_000 }) + ) + await latePage.bringToFront() + await latePage.goto('/status?subscribe=resume') + await expect( + latePage.getByRole('button', { name: 'Subscribe', exact: true }).first() + ).toBeVisible() + await latePage.waitForLoadState('networkidle') + expect(await subscriptionRow(late.principalUuid)).toBeNull() + await signedIn.context().close() + await latePage.context().close() + } +) + +test( + 'STA-S-05 anonymous sessions cannot subscribe', + { tag: '@core' }, + async ({ playwright, marker }) => { + const anonUser = await mintPortalUser(playwright, marker, 'anon', { anonymous: true }) + const anonApi = anonUser.api + const nobody = await playwright.request.newContext({ baseURL: supportEnv().baseUrl }) + try { + const anon = await callServerFn(anonApi, 'subscribeStatusFn', { scope: 'page' }) + expect(anon.ok).toBe(false) + expect(anon.code).toBe('ANONYMOUS_NOT_ALLOWED') + expect(messageOf(anon)).toBe('Anonymous interaction is not enabled') + const none = await callServerFn(nobody, 'subscribeStatusFn', { scope: 'page' }) + expect(none.ok).toBe(false) + // The portal button opens sign-in on this message (not masked in production). + expect(messageOf(none)).toBe('Authentication required') + expect(await subscriptionRow(anonUser.principalUuid)).toBeNull() + } finally { + await anonApi.dispose() + await nobody.dispose() + } + } +) + +test( + 'STA-S-06 unsubscribe from the page', + { tag: '@core' }, + async ({ browser, playwright, marker }) => { + const u = await user(playwright, marker) + await subscribeAs(u) + const page = await portalPage(browser, u) + await page.goto('/status') + await page.waitForLoadState('networkidle') + await page.getByRole('button', { name: 'Subscribed' }).click() + const confirm = page.getByRole('alertdialog').or(page.getByRole('dialog')) + await expect(confirm.getByText('Unsubscribe from status updates?')).toBeVisible() + await expect( + confirm.getByText("You'll stop getting emails about incidents and maintenance.") + ).toBeVisible() + await expect(confirm.getByRole('button', { name: 'Stay subscribed' })).toBeVisible() + await confirm.getByRole('button', { name: 'Unsubscribe', exact: true }).click() + await expect(page.getByText('Unsubscribed from status updates')).toBeVisible() + await expect(page.getByRole('button', { name: 'Subscribe', exact: true }).first()).toBeVisible() + const row = await subscriptionRow(u.principalUuid) + expect(row).not.toBeNull() + expect(row?.unsubscribed_at).not.toBeNull() + await page.context().close() + } +) + +test( + 'STA-S-07 re-subscribe after opting out', + { tag: '@nightly' }, + async ({ playwright, marker }) => { + const svc = await createService(admin, `${marker} Resub`) + const u = await user(playwright, marker) + await must(admin, 'addStatusSubscriberFn', { email: u.email }) + await must(admin, 'removeStatusSubscriberFn', { principalId: u.principalId }) + await subscribeAs(u, 'components', [svc.id]) + const row = await subscriptionRow(u.principalUuid) + expect(row).toMatchObject({ + scope: 'components', + component_ids: [svc.id], + unsubscribed_at: null, + }) + } +) + +test( + 'STA-S-09 a component subscription with no or unknown services is refused', + { + tag: ['@nightly', '@quarantine'], + annotation: { + type: 'quarantine', + description: + 'subscribeStatusFn (functions/status-subscriptions.ts:25) does not validate scope components against componentIds, so an empty or bogus list is stored as an active subscription; see QUARANTINE.md', + }, + }, + async ({ playwright, marker }) => { + const empty = await user(playwright, marker, 'empty') + const res = await callServerFn(empty.api, 'subscribeStatusFn', { + scope: 'components', + componentIds: [], + }) + expect(res.ok).toBe(false) + const bogus = await user(playwright, marker, 'bogus') + const res2 = await callServerFn(bogus.api, 'subscribeStatusFn', { + scope: 'components', + componentIds: ['status_component_01m7v6aewge02vtv3kd36ggmcw'], + }) + expect(res2.ok).toBe(false) + expect(await subscriptionRow(empty.principalUuid)).toBeNull() + } +)