diff --git a/apps/web/e2e/QUARANTINE.md b/apps/web/e2e/QUARANTINE.md index ba1c9c1013..f664b83cba 100644 --- a/apps/web/e2e/QUARANTINE.md +++ b/apps/web/e2e/QUARANTINE.md @@ -534,3 +534,59 @@ proved by a separate test that is not quarantined where one exists. `list_suggestions`, which the server does not register, against 43 served (`apps/web/src/components/admin/settings/mcp/mcp-setup-guide.tsx` `MCP_TOOLS`). owner: unassigned, review by: 2026-12-03. + +### Product suite: status page + +Each test asserts the right behaviour. Where the rest of a journey works, a +separate test that is not quarantined proves it (STA-A-30 and STA-S-23 have +no such part beyond the wrong notification). + +- `status/admin-incidents.spec.ts` › STA-A-30 an incident created already + resolved announces nothing. `createIncident` sets + `notifySubscribers = !backfilled && (input.notifySubscribers ?? true)` + (`apps/web/src/lib/server/domains/status/status.service.ts:242`) without + looking at the status, so a non-backfill incident created `resolved` (or a + window created `completed`) writes `status.incident_created` and emails + subscribers "New incident reported" for something already over. Fix: treat a + terminal create status like a backfill for notification (no event, no + email). owner: unassigned, review by: 2026-12-03. +- `status/admin-incidents.spec.ts` › STA-A-33 editor deep link to a malformed + id says not found. `getStatusIncidentAdminFn` passes the raw id to + `findStatusIncidentById` + (`apps/web/src/lib/server/domains/status/status.service.ts:866`), whose + TypeID conversion throws, so the fn errors ("Something went wrong on our + side") and the editor shows "Couldn't load this incident" with Try again. + Fix: return null for an id that fails `isTypeId(id, 'status_incident')`, as + the public path does. owner: unassigned, review by: 2026-12-03. +- `status/admin-maintenance.spec.ts` › STA-A-46 a maintenance window with no + dates is refused. `createIncident` + (`apps/web/src/lib/server/domains/status/status.service.ts:183`) and the + REST create accept `kind: 'maintenance'` with no `scheduledStartAt` or + `scheduledEndAt`: no job is ever enqueued, so the window stays "scheduled" + (and in Upcoming maintenance) forever, and its email reads " to + ". The schedule dialog requires both bounds for exactly this reason. + Fix: refuse a maintenance create without both bounds (VALIDATION_ERROR). + owner: unassigned, review by: 2026-12-03. +- `status/admin-templates.spec.ts` › STA-A-49 a blank template name is + refused with the Name field named. The template name goes through + `validateTitle` (`apps/web/src/lib/server/domains/status/status.service.ts:1161` + and `:1191`), so a blank name is refused with "Title is required" (and a long + one with "Title must not exceed 200 characters") while the field is "Template + name". Fix: a `validateName`-style check with "Name is required". owner: + unassigned, review by: 2026-12-03. +- `status/admin-subscribers.spec.ts` › STA-A-53 every row of a large import is + listed across pages. A CSV import inserts every row in one transaction, so + they share one `created_at`; `listStatusSubscriptions` orders by `created_at` + only and pages with `created_at < cursor` + (`apps/web/src/lib/server/domains/status/status.subscription.ts:210`, `:236`), + so page 2 skips the rest of the import (35 imported, 30 listed; the export + has all 35). Fix: order by `(created_at, id)` and page on the pair. owner: + unassigned, review by: 2026-12-03. +- `status/admin-overview.serial.spec.ts` › STA-A-57 the overview banner shows + what visitors see. `getStatusOverviewAdminFn` derives `topLevelStatus` from + every service (`apps/web/src/lib/server/functions/status.ts:624`), segment + gated and plan hidden ones included, so with only a gated service down the + "Visitors currently see" banner says "Major system outage" while anonymous + 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. diff --git a/apps/web/e2e/product/setup.product.ts b/apps/web/e2e/product/setup.product.ts index b6991db814..ddbead833c 100644 --- a/apps/web/e2e/product/setup.product.ts +++ b/apps/web/e2e/product/setup.product.ts @@ -52,5 +52,17 @@ setup('prepare the product suite', async ({ request }) => { // The app is up and is the production build the suite expects. const ready = await request.get('/api/health/ready') expect(ready.status()).toBe(200) + // A request that read the settings row before the writes above can put the + // old row back in the shared cache after the bust. Bust until the app serves + // the published status page to a signed-out visitor. + await expect + .poll( + async () => { + await bustSettingsCache() + return (await request.get('/status')).status() + }, + { timeout: 30_000 } + ) + .toBe(200) await closeSupportDb() }) diff --git a/apps/web/e2e/product/status.ts b/apps/web/e2e/product/status.ts new file mode 100644 index 0000000000..b5ff998be4 --- /dev/null +++ b/apps/web/e2e/product/status.ts @@ -0,0 +1,575 @@ +/** + * Helpers for the status page specs (e2e/tests/status/). + * + * Service status, the hero headline and the overview tiles are shared by every + * test, so each test makes its own services (named with its marker) and reads + * back only its own rows. Incidents and windows made here default to + * `notifySubscribers: false`: a publish email goes to every subscriber in the + * workspace, so only the notification specs turn it on. + * + * const api = await personApi(playwright, 'admin') + * const svc = await createService(api, `${marker} API`) + * const inc = await createIncident(api, { title: marker, services: [[svc.id, 'major_outage']] }) + * expect((await serviceRow(svc.id)).status).toBe('major_outage') + */ +import { createHmac } from 'node:crypto' +import { + expect, + type APIRequestContext, + type Locator, + type Page, + type PlaywrightWorkerArgs, +} from '@playwright/test' +import { fromUuid, generateId, toUuid } from '@quackback/ids' +import { pollFor, supportDb } from '../support/db' +import { supportEnv } from '../support/env' +import { callServerFn, type ServerFnResult } from '../support/server-fns' +import { mintCustomer, mintSessionToken, type MintedCustomer } from '../support/widget-portal' + +export type ServiceStatus = + 'operational' | 'degraded_performance' | 'partial_outage' | 'major_outage' | 'under_maintenance' + +export type Lifecycle = + | 'investigating' + | 'identified' + | 'monitoring' + | 'resolved' + | 'scheduled' + | 'in_progress' + | 'verifying' + | 'completed' + +/** Wire shape of an admin incident (serializeIncident in functions/status.ts). */ +export interface AdminIncident { + id: string + kind: 'incident' | 'maintenance' + title: string + status: Lifecycle + impact: string + impactOverride: boolean + scheduledStartAt: string | null + scheduledEndAt: string | null + autoStart: boolean + autoComplete: boolean + startedAt: string + resolvedAt: string | null + notifiedAt: string | null + backfilled: boolean + notifySubscribers: boolean + updates: { id: string; status: Lifecycle; body: string; createdAt: string }[] + components?: { componentId: string; componentStatus: ServiceStatus }[] + [k: string]: unknown +} + +export interface Service { + id: string + name: string + status: ServiceStatus + [k: string]: unknown +} + +// --------------------------------------------------------------------------- +// Server functions +// --------------------------------------------------------------------------- + +/** Call a status server fn and fail the test if it threw. */ +export async function must( + api: APIRequestContext, + name: string, + data?: unknown, + opts: { method?: 'GET' | 'POST' } = {} +): Promise { + const res = await callServerFn(api, name, data, opts) + expect(res.ok, `${name} failed: ${res.raw.slice(0, 400)}`).toBe(true) + return res.data +} + +/** Call a status server fn and expect it to refuse; returns the result. */ +export async function refused( + api: APIRequestContext, + name: string, + data?: unknown, + opts: { method?: 'GET' | 'POST' } = {} +): Promise { + const res = await callServerFn(api, name, data, opts) + expect(res.ok, `${name} should have been refused: ${res.raw.slice(0, 300)}`).toBe(false) + return res +} + +export async function createService( + api: APIRequestContext, + name: string, + extra: { + status?: ServiceStatus + groupId?: string | null + segmentIds?: string[] + showUptime?: boolean + description?: string | null + } = {} +): Promise { + return must(api, 'createStatusComponentFn', { name, ...extra }) +} + +export async function setServiceStatus(api: APIRequestContext, id: string, status: ServiceStatus) { + await must(api, 'setStatusComponentStatusFn', { id, status }) +} + +export interface IncidentInput { + title: string + services: [string, ServiceStatus][] + kind?: 'incident' | 'maintenance' + status?: Lifecycle + body?: string + impact?: string + impactOverride?: boolean + scheduledStartAt?: Date | string | null + scheduledEndAt?: Date | string | null + autoStart?: boolean + autoComplete?: boolean + backfill?: { startedAt: Date | string; resolvedAt: Date | string } + notifySubscribers?: boolean + templateId?: string +} + +function incidentPayload(input: IncidentInput): Record { + const kind = input.kind ?? 'incident' + const { services, ...rest } = input + return { + kind, + status: input.status ?? (kind === 'maintenance' ? 'scheduled' : 'investigating'), + body: input.body ?? `First update for ${input.title}`, + notifySubscribers: input.notifySubscribers ?? false, + ...rest, + affectedComponents: services.map(([componentId, componentStatus]) => ({ + componentId, + componentStatus, + })), + } +} + +/** Create an incident (or a window, with kind 'maintenance') through the server fn. */ +export async function createIncident( + api: APIRequestContext, + input: IncidentInput +): Promise { + return must(api, 'createStatusIncidentFn', incidentPayload(input)) +} + +/** The same call, without failing: for refusals. */ +export async function tryCreateIncident(api: APIRequestContext, input: IncidentInput) { + return callServerFn(api, 'createStatusIncidentFn', incidentPayload(input)) +} + +/** A maintenance window starting and ending `startMin` / `endMin` minutes from now. */ +export async function scheduleWindow( + api: APIRequestContext, + input: Omit & { startMin?: number; endMin?: number } +): Promise { + const { startMin = 60, endMin = 120, ...rest } = input + return createIncident(api, { + kind: 'maintenance', + scheduledStartAt: minutesFromNow(startMin), + scheduledEndAt: minutesFromNow(endMin), + ...rest, + }) +} + +export async function postUpdate( + api: APIRequestContext, + id: string, + status: Lifecycle, + body: string, + extra: { skipRestore?: boolean; templateId?: string } = {} +): Promise { + return must(api, 'postStatusIncidentUpdateFn', { id, status, body, ...extra }) +} + +export async function getIncidentAdmin(api: APIRequestContext, id: string) { + return must(api, 'getStatusIncidentAdminFn', { id }, { method: 'GET' }) +} + +export function minutesFromNow(minutes: number): Date { + return new Date(Date.now() + minutes * 60_000) +} + +// --------------------------------------------------------------------------- +// Rows the product promises +// --------------------------------------------------------------------------- + +const uuid = (typeId: string) => toUuid(typeId as `status_component_${string}`) + +export interface ServiceRowDb { + id: string + name: string + status: ServiceStatus + group_id: string | null + position: number + show_uptime: boolean + segment_ids: string[] + deleted_at: Date | null + description: string | null +} + +export async function serviceRow(id: string): Promise { + const [row] = await supportDb()` + SELECT id, name, status, group_id, position, show_uptime, segment_ids, deleted_at, description + FROM status_components WHERE id = ${uuid(id)}` + if (!row) throw new Error(`no status component ${id}`) + return row +} + +export interface IncidentRowDb { + id: string + kind: string + title: string + status: Lifecycle + impact: string + impact_override: boolean + scheduled_start_at: Date | null + scheduled_end_at: Date | null + auto_start: boolean + auto_complete: boolean + started_at: Date + resolved_at: Date | null + backfilled: boolean + notified_at: Date | null + notify_subscribers: boolean + details_edited_at: Date | null + created_by: string | null + deleted_at: Date | null + created_at: Date + updated_at: Date +} + +export async function incidentRow(id: string): Promise { + const [row] = await supportDb()` + SELECT * FROM status_incidents WHERE id = ${uuid(id)}` + if (!row) throw new Error(`no status incident ${id}`) + return row +} + +/** The incident's timeline, oldest first. */ +export async function incidentUpdates(id: string) { + return supportDb()< + { status: Lifecycle; body: string; created_at: Date; template_id: string | null }[] + >`SELECT status, body, created_at, template_id FROM status_incident_updates + WHERE incident_id = ${uuid(id)} ORDER BY created_at, id` +} + +/** The incident's affected-service links. */ +export async function incidentLinks(id: string) { + return supportDb()<{ component_id: string; component_status: ServiceStatus }[]>` + SELECT component_id, component_status FROM status_incident_components + WHERE incident_id = ${uuid(id)}` +} + +/** A service's status history (the uptime source), oldest first. */ +export async function serviceEvents(id: string) { + return supportDb()< + { status: ServiceStatus; source: string; incident_id: string | null; created_at: Date }[] + >`SELECT status, source, incident_id, created_at FROM status_component_events + WHERE component_id = ${uuid(id)} ORDER BY created_at, id` +} + +/** Domain events written for an incident or service (TypeID), oldest first. */ +export async function domainEvents(entityId: string) { + return supportDb()<{ type: string; payload: Record; actor_type: string }[]>` + SELECT type, payload, actor_type FROM events WHERE entity_id = ${entityId} ORDER BY id` +} + +export async function domainEventTypes(entityId: string): Promise { + return (await domainEvents(entityId)).map((e) => e.type) +} + +/** + * Move a window's planned bounds (the handlers compare them with the wall + * clock). A window still scheduled is dated at its planned start, so that + * moves with it, as it would had the window been planned that way. + */ +export async function backdateWindow( + id: string, + bounds: { start?: Date; end?: Date } +): Promise { + const sql = supportDb() + if (bounds.start) { + await sql`UPDATE status_incidents SET scheduled_start_at = ${bounds.start}, + started_at = CASE WHEN status = 'scheduled' THEN ${bounds.start} ELSE started_at END + WHERE id = ${uuid(id)}` + } + if (bounds.end) { + await sql`UPDATE status_incidents SET scheduled_end_at = ${bounds.end} + WHERE id = ${uuid(id)}` + } +} + +/** + * Pending scheduler jobs for a window's current schedule (`start` / + * `complete`). Jobs keyed by an older schedule (a backdated or moved window) + * are left out: their handlers re-check the row and do nothing. + */ +export async function maintenanceJobs(id: string) { + const row = await incidentRow(id) + const hash = `${row.scheduled_start_at?.getTime() ?? 'none'}-${row.scheduled_end_at?.getTime() ?? 'none'}` + const rows = await supportDb()<{ job_id: string; run_at: Date }[]>` + SELECT dedupe_key AS job_id, run_at FROM job_queue + WHERE dedupe_key LIKE ${`status-maintenance-%--${id}--${hash}`} AND status = 'pending' + ORDER BY dedupe_key` + return rows.map((r) => { + const m = /^status-maintenance-(start|complete)--/.exec(r.job_id) + return { kind: m?.[1] ?? '?', hash, runAt: r.run_at } + }) +} + +/** Run the scheduler's safety net (every overdue window in the workspace). */ +export async function runMaintenanceSweep(request: APIRequestContext): Promise { + const res = await request.post('/api/e2e/sweep', { data: { job: 'status-maintenance' } }) + expect(res.status(), await res.text()).toBe(200) +} + +/** Choose a date and time in a DateTimePicker opened by `trigger`. */ +export async function pickDateTime(page: Page, trigger: Locator, when: Date): Promise { + await trigger.click() + const iso = [ + when.getFullYear(), + String(when.getMonth() + 1).padStart(2, '0'), + String(when.getDate()).padStart(2, '0'), + ].join('-') + const day = page.locator(`[data-day="${iso}"]:not([data-outside]) button`) + if ((await day.count()) === 0) { + await page.getByRole('button', { name: /next month/i }).click() + } + await day.click() + const hh = String(when.getHours()).padStart(2, '0') + const mm = String(when.getMinutes()).padStart(2, '0') + await page.locator('input[type="time"]').fill(`${hh}:${mm}`) + await page.keyboard.press('Escape') + await expect(page.locator('[data-day]').first()).toBeHidden() +} + +export async function pollIncident( + id: string, + what: string, + check: (row: IncidentRowDb) => boolean, + timeoutMs = 10_000 +): Promise { + return pollFor( + `${what} (${id})`, + async () => { + const row = await incidentRow(id) + return check(row) ? row : null + }, + { timeoutMs } + ) +} + +/** A manual segment only this test uses, with the given members (principal uuids). */ +export async function createSegment(marker: string, memberUuids: string[] = []) { + const id = toUuid(generateId('segment')) + const slug = `${marker}-${Math.random().toString(36).slice(2, 7)}`.toLowerCase() + await supportDb()`INSERT INTO segments (id, name, slug, type) + VALUES (${id}, ${`Segment ${slug}`}, ${slug}, 'manual')` + for (const principalId of memberUuids) { + await supportDb()`INSERT INTO user_segments (principal_id, segment_id) VALUES (${principalId}, ${id})` + } + return { id: fromUuid('segment', id), uuid: id } +} + +// --------------------------------------------------------------------------- +// Subscribers +// --------------------------------------------------------------------------- + +export interface PortalUser extends MintedCustomer { + /** A request context signed in as them (for callServerFn). */ + api: APIRequestContext + cookieValue: string +} + +/** A fresh portal user with a signed-in request context. One subscription row per person. */ +export async function mintPortalUser( + playwright: PlaywrightWorkerArgs['playwright'], + marker: string, + label = 'sub' +): 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 env = supportEnv() + const token = await mintSessionToken(customer.userUuid) + const signature = createHmac('sha256', env.app.SECRET_KEY).update(token).digest('base64') + const cookieValue = encodeURIComponent(`${token}.${signature}`) + const domain = new URL(env.baseUrl).hostname + const api = await playwright.request.newContext({ + baseURL: env.baseUrl, + storageState: { + cookies: [ + { + name: 'better-auth.session_token', + value: cookieValue, + domain, + path: '/', + expires: Math.floor(Date.now() / 1000) + 86_400, + httpOnly: true, + secure: false, + sameSite: 'Lax', + }, + ], + origins: [], + }, + }) + return { ...customer, api, cookieValue } +} + +/** Subscribe a portal user through the portal's own server fn. */ +export async function subscribeAs( + user: PortalUser, + scope: 'page' | 'components' = 'page', + componentIds: string[] = [] +): Promise { + await must(user.api, 'subscribeStatusFn', { scope, componentIds }) +} + +/** Unsubscribe (so a page-wide subscriber stops receiving other tests' mail). */ +export async function unsubscribeAs(user: PortalUser): Promise { + await callServerFn(user.api, 'unsubscribeStatusFn', undefined) +} + +export async function subscriptionRow(principalUuid: string) { + const [row] = await supportDb()< + { + scope: string + component_ids: string[] + source: string + unsubscribed_at: Date | null + }[] + >`SELECT scope, component_ids, source, unsubscribed_at FROM status_subscriptions + WHERE principal_id = ${principalUuid}` + return row ?? null +} + +/** Status bells for a person about an incident. */ +export async function statusBells(principalUuid: string, incidentId: string) { + return supportDb()<{ type: string; title: string; body: string | null }[]>` + SELECT type, title, body FROM in_app_notifications + WHERE principal_id = ${principalUuid} AND metadata::text LIKE ${`%${incidentId}%`}` +} + +// --------------------------------------------------------------------------- +// Admin UI +// --------------------------------------------------------------------------- + +type Scope = Page | Locator + +const pageOf = (scope: Scope): Page => ('page' in scope ? scope.page() : scope) + +/** The affected-services row for a service in a composer or the editor sidebar. */ +export function serviceChoice(scope: Scope, name: string): Locator { + const page = pageOf(scope) + return scope + .locator('div', { has: page.getByText(name, { exact: true }) }) + .filter({ has: page.getByRole('checkbox') }) + .last() +} + +/** + * Tick or untick a service in the affected-services list, from the keyboard: + * in the editor sidebar the modal's footer can sit over a row, and a pointer + * click then lands on the footer. + */ +export async function toggleService(scope: Scope, name: string): Promise { + const box = serviceChoice(scope, name).getByRole('checkbox') + const before = await box.getAttribute('aria-checked') + await box.focus() + await box.press('Space') + await expect(box).not.toHaveAttribute('aria-checked', before ?? 'false') +} + +/** Open an admin page and wait for it to hydrate (clicks before that are lost). */ +export async function openAdmin(page: Page, url: string): Promise { + await page.goto(url) + await page.waitForLoadState('networkidle') +} + +/** Pick a status in a row's status menu (StatusSelect). */ +export async function pickStatus(page: Page, row: Locator, label: string): Promise { + await row.getByRole('button').first().click() + await page.getByRole('button', { name: label, exact: true }).click() +} + +/** + * Type into a list's search box once the page has hydrated (a fill before + * that is wiped), then wait for `expected` to show. + */ +export async function searchList( + page: Page, + text: string, + expected: Locator, + placeholder = 'Search incidents…' +): Promise { + const box = page.getByPlaceholder(placeholder) + await page.waitForLoadState('networkidle') + await expect(async () => { + await box.fill('') + await box.fill(text) + await expect(expected).toBeVisible({ timeout: 2_000 }) + }).toPass({ timeout: 15_000 }) +} + +/** The message a refused server fn call carries (typed or plain error). */ +export function messageOf(res: ServerFnResult): string { + const found: string[] = [] + const walk = (node: unknown): void => { + if (!node || typeof node !== 'object') return + const n = node as { p?: { k?: string[]; v?: unknown[] }; s?: unknown } + if (n.p?.k && n.p.v) { + const i = n.p.k.indexOf('message') + const v = i === -1 ? undefined : (n.p.v[i] as { s?: unknown } | undefined) + if (v && typeof v.s === 'string') found.push(v.s) + } + if (n.s && typeof n.s === 'object' && 'message' in n.s) { + const m = (n.s as { message?: { s?: unknown } }).message + if (m && typeof m.s === 'string') found.push(m.s) + } + for (const value of Object.values(n)) { + if (Array.isArray(value)) value.forEach(walk) + else walk(value) + } + } + try { + walk(JSON.parse(res.raw)) + } catch { + return res.raw + } + // seroval writes strings as JS literals: undo its backslash escapes. + return (found[0] ?? res.raw).replace(/\\(.)/g, '$1') +} + +export interface PublicService { + id: string + name: string + status: ServiceStatus + showUptime: boolean +} + +export interface PublicSnapshot { + activeIncidents: { id: string; title: string }[] + upcomingMaintenance: { id: string; title: string }[] + recentIncidents: { date: string; incidents: { id: string; title: string }[] }[] + ungroupedComponents: PublicService[] + groups: { id: string; name: string; collapsed: boolean; components: PublicService[] }[] + [k: string]: unknown +} + +/** getStatusPageFn as whoever `api` is; the snapshot part. */ +export async function publicSnapshot(api: APIRequestContext): Promise { + const page = await must<{ snapshot: PublicSnapshot }>(api, 'getStatusPageFn', undefined, { + method: 'GET', + }) + return page.snapshot +} + +export const recentIds = (s: PublicSnapshot) => + s.recentIncidents.flatMap((d) => d.incidents.map((i) => i.id)) + +export const publicServiceIds = (s: PublicSnapshot) => [ + ...s.ungroupedComponents.map((c) => c.id), + ...s.groups.flatMap((g) => g.components.map((c) => c.id)), +] diff --git a/apps/web/e2e/support/sweeps.ts b/apps/web/e2e/support/sweeps.ts index e8318beb1b..b415305f0e 100644 --- a/apps/web/e2e/support/sweeps.ts +++ b/apps/web/e2e/support/sweeps.ts @@ -7,7 +7,8 @@ * * Sweeps with an "as of" clock: sla-breach-sweep, snooze-sweep, workflow-sweep, * workflow-wait (promotes delayed workflow jobs due by then). Wall clock only: - * email-log-retention, workflow-retention, spam-retention, anon-sweep. + * email-log-retention, workflow-retention, spam-retention, anon-sweep, + * status-maintenance, status-notify. */ import { expect, type APIRequestContext } from '@playwright/test' @@ -20,6 +21,8 @@ export type SweepName = | 'workflow-retention' | 'spam-retention' | 'anon-sweep' + | 'status-maintenance' + | 'status-notify' export interface SweepRun { ok: true diff --git a/apps/web/e2e/tests/status/admin-access.spec.ts b/apps/web/e2e/tests/status/admin-access.spec.ts new file mode 100644 index 0000000000..c73e046242 --- /dev/null +++ b/apps/web/e2e/tests/status/admin-access.spec.ts @@ -0,0 +1,234 @@ +/** + * Status admin: who can open what, and the server's refusals behind it. + * + * Journeys: STA-A-01, STA-A-02, STA-A-03, STA-A-05. + * + * Seeded people: `admin` holds status_page.publish and status_page.manage; + * `contributor` holds publish only; `noAccess` holds neither. A manage-only + * teammate is made per test with its own custom role. + */ +import type { APIRequestContext } from '@playwright/test' +import { test, expect } from '../../support/fixtures' +import { callServerFn, personApi } from '../../support/server-fns' +import { newTeammate } from '../../support/api-client' +import { mintSession } from '../../support/teammates' +import { supportDb } from '../../support/db' +import { supportEnv } from '../../support/env' +import { + createIncident, + createService, + incidentRow, + must, + openAdmin, + postUpdate, + scheduleWindow, + serviceRow, +} from '../../product/status' + +test.describe.configure({ timeout: 60_000 }) + +let admin: APIRequestContext + +test.beforeEach(async ({ playwright }) => { + admin = await personApi(playwright, 'admin') +}) +test.afterEach(async () => { + await admin.dispose() +}) + +const DENIED = (perm: string) => + new RegExp(`Access denied: Requires permission '${perm.replace('.', '\\.')}'`) + +/** A fresh teammate holding exactly `permissions`, signed in. */ +async function teammateWith( + playwright: Parameters[0], + marker: string, + permissions: string[] +) { + const t = newTeammate(`${marker} status`, permissions) + const storageState = await mintSession(t.userId) + const api = await playwright.request.newContext({ + baseURL: supportEnv().baseUrl, + storageState, + }) + return { ...t, storageState, api } +} + +test( + 'STA-A-01 the status admin opens on the right default view per role', + { tag: '@core' }, + async ({ asPerson, browser, playwright, marker }) => { + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/status') + await expect(ui.getByRole('button', { name: 'Overview', exact: true })).toBeVisible() + await expect(ui.getByText('Visitors currently see')).toBeVisible() + + const contributor = await asPerson('contributor') + await openAdmin(contributor, '/admin/status') + await expect(contributor.getByText('Visitors currently see')).toBeVisible() + await expect(contributor.getByRole('button', { name: /^Services/ })).toHaveCount(0) + await expect(contributor.getByRole('link', { name: 'Status' })).toBeVisible() + + // Manage only: lands on Services; the nav still shows Status. + const manager = await teammateWith(playwright, marker, ['status_page.manage']) + const context = await browser.newContext({ storageState: manager.storageState }) + try { + const page = await context.newPage() + await openAdmin(page, '/admin/status') + await expect(page.getByPlaceholder('Search services…')).toBeVisible() + await expect(page.getByRole('button', { name: 'Overview', exact: true })).toHaveCount(0) + await expect(page.getByRole('link', { name: 'Status' })).toBeVisible() + } finally { + await context.close() + await manager.api.dispose() + } + } +) + +test( + 'STA-A-02 a teammate with no status permission is shut out', + { tag: '@core' }, + async ({ asPerson, playwright, marker }) => { + const ui = await asPerson('noAccess') + await openAdmin(ui, '/admin/status') + await expect(ui.getByText("You don't have access to the status page")).toBeVisible() + await expect( + ui.getByText( + 'The status page needs the status_page.publish permission to run incidents, or status_page.manage to manage the page. Ask a workspace admin to add one to your role.' + ) + ).toBeVisible() + + const svc = await createService(admin, `${marker} Locked`) + const api = await personApi(playwright, 'noAccess') + try { + const overview = await callServerFn(api, 'getStatusOverviewAdminFn', undefined, { + method: 'GET', + }) + expect(overview.ok).toBe(false) + expect(overview.raw).toMatch(DENIED('status_page.publish')) + const create = await callServerFn(api, 'createStatusIncidentFn', { + kind: 'incident', + title: marker, + status: 'investigating', + body: 'x', + affectedComponents: [{ componentId: svc.id, componentStatus: 'major_outage' }], + }) + expect(create.ok).toBe(false) + expect(create.raw).toMatch(DENIED('status_page.publish')) + const list = await callServerFn(api, 'listStatusComponentsAdminFn', undefined, { + method: 'GET', + }) + expect(list.ok).toBe(false) + expect(list.raw).toMatch(DENIED('status_page.manage')) + } finally { + await api.dispose() + } + // Nothing was written. + const rows = await supportDb()`SELECT 1 FROM status_incidents WHERE title = ${marker}` + expect(rows).toHaveLength(0) + expect((await serviceRow(svc.id)).status).toBe('operational') + } +) + +test( + 'STA-A-03 a publish-only teammate runs incidents but cannot reshape the page', + { tag: '@core' }, + async ({ asPerson, playwright, marker }) => { + const svc = await createService(admin, `${marker} Contrib`) + const api = await personApi(playwright, 'contributor') + try { + // Every incident action works, delete included. + const inc = await createIncident(api, { title: marker, services: [[svc.id, 'major_outage']] }) + await must(api, 'updateStatusIncidentFn', { id: inc.id, title: `${marker} renamed` }) + await postUpdate(api, inc.id, 'identified', 'found it') + await must(api, 'listStatusIncidentTemplatesFn', undefined, { method: 'GET' }) + const w = await scheduleWindow(api, { + title: `${marker} window`, + services: [[svc.id, 'under_maintenance']], + }) + await must(api, 'startStatusMaintenanceNowFn', { id: w.id }) + const choices = await must<{ + ungrouped: { id: string }[] + groups: { components: { id: string }[] }[] + }>(api, 'listStatusComponentChoicesFn', undefined, { method: 'GET' }) + expect( + [...choices.ungrouped, ...choices.groups.flatMap((g) => g.components)].map((c) => c.id) + ).toContain(svc.id) + await must(api, 'deleteStatusIncidentFn', { id: inc.id }) + expect((await incidentRow(inc.id)).deleted_at).not.toBeNull() + await must(api, 'deleteStatusIncidentFn', { id: w.id }) + + // Every manage-only fn is refused. + const manageOnly: [string, unknown, 'GET' | 'POST'][] = [ + ['createStatusComponentFn', { name: `${marker} nope` }, 'POST'], + ['setStatusComponentStatusFn', { id: svc.id, status: 'major_outage' }, 'POST'], + ['createStatusGroupFn', { name: `${marker} nope` }, 'POST'], + ['createStatusIncidentTemplateFn', { name: marker, title: marker, body: 'x' }, 'POST'], + [ + 'updateStatusIncidentTemplateFn', + { id: 'status_tmpl_01m7v6aewge02vtv3kd36ggmcw', name: 'x' }, + 'POST', + ], + [ + 'deleteStatusIncidentTemplateFn', + { id: 'status_tmpl_01m7v6aewge02vtv3kd36ggmcw' }, + 'POST', + ], + ['addStatusSubscriberFn', { email: `${marker}@customers.example` }, 'POST'], + ['importStatusSubscribersFn', { emails: [`${marker}@customers.example`] }, 'POST'], + ['exportStatusSubscribersAdminFn', undefined, 'GET'], + ['updateStatusSettingsFn', { pageDescription: marker }, 'POST'], + ['clearStatusHistoryFn', undefined, 'POST'], + ['getStatusUptimeAdminFn', { componentIds: [svc.id] }, 'GET'], + ] + for (const [name, data, method] of manageOnly) { + const res = await callServerFn(api, name, data, { method }) + expect(res.ok, name).toBe(false) + expect(res.raw, name).toMatch(DENIED('status_page.manage')) + } + expect((await serviceRow(svc.id)).status).toBe('operational') + const groups = + await supportDb()`SELECT 1 FROM status_component_groups WHERE name = ${`${marker} nope`}` + expect(groups).toHaveLength(0) + } finally { + await api.dispose() + } + + const ui = await asPerson('contributor') + for (const view of ['components', 'subscribers']) { + await openAdmin(ui, `/admin/status?view=${view}`) + await expect(ui.getByText("You don't have access to this")).toBeVisible() + await expect( + ui.getByText( + 'Managing services and subscribers needs the status_page.manage permission. Ask a workspace admin to add it to your role.' + ) + ).toBeVisible() + } + } +) + +test( + 'STA-A-05 the status settings page needs status_page.manage', + { tag: '@nightly' }, + async ({ asPerson, playwright }) => { + const contributor = await asPerson('contributor') + await contributor.goto('/admin/settings/status') + await expect(contributor).not.toHaveURL(/\/admin\/settings\/status/) + await expect(contributor).toHaveURL(/not_team_member/) + const api = await personApi(playwright, 'contributor') + try { + const res = await callServerFn(api, 'getStatusSettingsFn', undefined, { method: 'GET' }) + expect(res.ok).toBe(false) + expect(res.raw).toMatch(DENIED('status_page.manage')) + } finally { + await api.dispose() + } + + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/settings/status') + for (const title of ['Page', 'Visibility', 'Notifications', 'Danger zone']) { + await expect(ui.getByText(title, { exact: true }).first(), title).toBeVisible() + } + await expect(ui.getByText(/Sent from/).first()).toBeVisible() + } +) diff --git a/apps/web/e2e/tests/status/admin-incidents.spec.ts b/apps/web/e2e/tests/status/admin-incidents.spec.ts new file mode 100644 index 0000000000..5e5bd82335 --- /dev/null +++ b/apps/web/e2e/tests/status/admin-incidents.spec.ts @@ -0,0 +1,824 @@ +/** + * Status admin: incidents, from report to resolve, reopen, backfill and delete. + * + * Journeys: STA-A-10, STA-A-17, STA-A-18, STA-A-19, STA-A-20, STA-A-21, + * STA-A-22, STA-A-23, STA-A-24, STA-A-25, STA-A-26, STA-A-27, STA-A-28, + * STA-A-29, STA-A-30, STA-A-31, STA-A-32, STA-A-33. + * + * Every test makes its own services (named by marker) and reads back only its + * own rows: service status is workspace-wide. + */ +import type { APIRequestContext } from '@playwright/test' +import { toUuid } from '@quackback/ids' +import { test, expect } from '../../support/fixtures' +import { personApi, callServerFn } from '../../support/server-fns' +import { pollFor } from '../../support/db' +import { + createIncident, + createService, + domainEventTypes, + domainEvents, + getIncidentAdmin, + messageOf, + openAdmin, + pickStatus, + searchList, + serviceChoice, + toggleService, + publicSnapshot, + recentIds, + incidentLinks, + incidentRow, + incidentUpdates, + must, + postUpdate, + serviceEvents, + serviceRow, + setServiceStatus, + tryCreateIncident, +} from '../../product/status' + +// Browser journeys share the machine with the app under load: allow a minute. +test.describe.configure({ timeout: 60_000 }) + +let admin: APIRequestContext + +test.beforeEach(async ({ playwright }) => { + admin = await personApi(playwright, 'admin') +}) +test.afterEach(async () => { + await admin.dispose() +}) + +const statusOf = async (id: string) => (await serviceRow(id)).status + +test.describe('report and lifecycle', () => { + test( + 'STA-A-17 report an incident in the composer and see it live', + { tag: '@smoke' }, + async ({ asPerson, people, page, marker }) => { + const a = await createService(admin, `${marker} Web`) + const b = await createService(admin, `${marker} Database`) + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/status?view=open') + await ui.getByRole('button', { name: 'Report incident' }).first().click() + const dialog = ui.getByRole('dialog', { name: 'Report an incident' }) + await dialog.getByLabel('Title').fill(`${marker} outage`) + await dialog.getByLabel("What's happening?").fill(`${marker} we are looking into it`) + await toggleService(dialog, a.name) + await toggleService(dialog, b.name) + await pickStatus(ui, serviceChoice(dialog, b.name), 'Major outage') + await expect(dialog.getByText(/Impact:\s*Critical/)).toBeVisible() + // "Email subscribers" is on by default; turned off here so the publish + // does not mail every other test's subscribers. + const notify = dialog.getByRole('checkbox', { name: /Email subscribers/ }) + await expect(notify).toBeChecked() + await expect( + dialog.getByText('One email at publish. Updates and the resolve never email.') + ).toBeVisible() + await notify.click() + await dialog.getByRole('button', { name: 'Publish incident' }).click() + + await expect(dialog).toBeHidden() + await expect(ui).toHaveURL(/view=open/) + await expect(ui).toHaveURL(/incident=status_incident_/) + const id = new URL(ui.url()).searchParams.get('incident')! + + const row = await incidentRow(id) + expect(row).toMatchObject({ + kind: 'incident', + title: `${marker} outage`, + status: 'investigating', + impact: 'critical', + impact_override: false, + resolved_at: null, + backfilled: false, + created_by: toUuid(people.people.admin.principalId as `principal_${string}`), + }) + expect(Date.now() - row.started_at.getTime()).toBeLessThan(60_000) + expect(row.notified_at).not.toBeNull() + expect(await incidentUpdates(id)).toEqual([ + expect.objectContaining({ + status: 'investigating', + body: `${marker} we are looking into it`, + }), + ]) + expect(await incidentLinks(id)).toHaveLength(2) + expect(await statusOf(a.id)).toBe('degraded_performance') + expect(await statusOf(b.id)).toBe('major_outage') + for (const [svc, status] of [ + [a, 'degraded_performance'], + [b, 'major_outage'], + ] as const) { + expect(await serviceEvents(svc.id)).toEqual([ + expect.objectContaining({ status, source: 'incident', incident_id: toUuid(id as never) }), + ]) + } + expect(await domainEventTypes(id)).toEqual(['status.incident_created']) + + // Publicly the incident is live. + await page.goto('/status') + await expect(page.getByText(`${marker} outage`).first()).toBeVisible() + } + ) + + test( + 'STA-A-21 resolving from the stepper restores the affected services', + { tag: '@smoke' }, + async ({ asPerson, page, marker }) => { + const svc = await createService(admin, `${marker} Checkout`) + const inc = await createIncident(admin, { + title: `${marker} checkout down`, + services: [[svc.id, 'major_outage']], + }) + const ui = await asPerson('admin') + await openAdmin(ui, `/admin/status?view=open&incident=${inc.id}`) + await ui.getByRole('radio', { name: /Resolved/ }).click() + await expect(ui.getByRole('button', { name: 'Post update & resolve' })).toBeVisible() + const restore = ui.getByRole('checkbox', { name: 'Restore affected services to operational' }) + await expect(restore).toBeChecked() + await ui + .getByPlaceholder("What's the latest? This appears on the public status page.") + .fill(`${marker} fixed`) + await ui.getByRole('button', { name: 'Post update & resolve' }).click() + await expect(ui.getByText('Update posted').first()).toBeVisible({ timeout: 15_000 }) + + const row = await pollFor('the resolve', async () => { + const r = await incidentRow(inc.id) + return r.status === 'resolved' ? r : null + }) + expect(row.resolved_at).not.toBeNull() + expect(await statusOf(svc.id)).toBe('operational') + expect((await serviceEvents(svc.id)).map((e) => [e.status, e.source])).toEqual([ + ['major_outage', 'incident'], + ['operational', 'incident'], + ]) + const events = await domainEvents(inc.id) + expect(events.map((e) => e.type)).toEqual([ + 'status.incident_created', + 'status.incident_updated', + ]) + expect(events[1].payload).toMatchObject({ status: 'resolved' }) + + // Gone from the Open list; publicly listed as past, not active. + await ui.keyboard.press('Escape') + await openAdmin(ui, '/admin/status?view=open') + await searchList(ui, marker, ui.getByText('No incidents match your search')) + const snapshot = await publicSnapshot(admin) + expect(snapshot.activeIncidents.map((i) => i.id)).not.toContain(inc.id) + expect(recentIds(snapshot)).toContain(inc.id) + await page.goto(`/status/${inc.id}`) + await expect(page.getByText(`${marker} fixed`)).toBeVisible() + } + ) + + test( + 'STA-A-20 posting through the stepper advances the incident', + { tag: '@core' }, + async ({ asPerson, marker }) => { + const svc = await createService(admin, `${marker} Search`) + const inc = await createIncident(admin, { + title: `${marker} slow search`, + services: [[svc.id, 'partial_outage']], + }) + const ui = await asPerson('admin') + await openAdmin(ui, `/admin/status?view=open&incident=${inc.id}`) + const composer = ui.getByPlaceholder( + "What's the latest? This appears on the public status page." + ) + + await ui.getByRole('radio', { name: /Identified/ }).click() + await composer.fill(`${marker} found it`) + await ui.getByRole('button', { name: 'Post update & mark as Identified' }).click() + await expect(ui.getByText('Update posted').first()).toBeVisible({ timeout: 15_000 }) + await expect(composer).toHaveValue('') + // The stepper moves on to the next stage. + await expect(ui.getByRole('radio', { name: /Monitoring/ })).toHaveAttribute( + 'aria-checked', + 'true' + ) + await composer.fill(`${marker} watching`) + await ui.getByRole('button', { name: 'Post update & mark as Monitoring' }).click() + + await pollFor('two updates', async () => + (await incidentUpdates(inc.id)).length === 3 ? true : null + ) + expect((await incidentUpdates(inc.id)).map((u) => [u.status, u.body])).toEqual([ + ['investigating', `First update for ${marker} slow search`], + ['identified', `${marker} found it`], + ['monitoring', `${marker} watching`], + ]) + const row = await incidentRow(inc.id) + expect(row.status).toBe('monitoring') + expect(row.updated_at.getTime()).toBeGreaterThan(row.created_at.getTime()) + const updated = (await domainEvents(inc.id)).filter( + (e) => e.type === 'status.incident_updated' + ) + expect(updated.map((e) => e.payload)).toEqual([ + expect.objectContaining({ + incidentId: inc.id, + kind: 'incident', + status: 'identified', + body: `${marker} found it`, + }), + expect.objectContaining({ + incidentId: inc.id, + kind: 'incident', + status: 'monitoring', + body: `${marker} watching`, + }), + ]) + // The hold did not change, so the service did not move. + expect(await statusOf(svc.id)).toBe('partial_outage') + expect(await serviceEvents(svc.id)).toHaveLength(1) + } + ) + + test( + 'STA-A-10 a hand-set status on a held service stays until the resolve recomputes it', + { tag: '@core' }, + async ({ marker }) => { + const svc = await createService(admin, `${marker} Held`) + const inc = await createIncident(admin, { + title: marker, + services: [[svc.id, 'major_outage']], + }) + await setServiceStatus(admin, svc.id, 'operational') + await postUpdate(admin, inc.id, 'identified', 'still open') + expect(await statusOf(svc.id)).toBe('operational') + + // Another open incident holds it at degraded; the resolve takes that. + const other = await createIncident(admin, { + title: `${marker} other`, + services: [[svc.id, 'degraded_performance']], + }) + await setServiceStatus(admin, svc.id, 'operational') + await postUpdate(admin, inc.id, 'resolved', 'done') + expect(await statusOf(svc.id)).toBe('degraded_performance') + await postUpdate(admin, other.id, 'resolved', 'done too') + expect(await statusOf(svc.id)).toBe('operational') + } + ) + + test( + 'STA-A-22 resolving without restore keeps the service status', + { tag: '@core' }, + async ({ marker }) => { + const svc = await createService(admin, `${marker} Partial`) + const inc = await createIncident(admin, { + title: marker, + services: [[svc.id, 'partial_outage']], + }) + await postUpdate(admin, inc.id, 'resolved', 'mostly back', { skipRestore: true }) + expect((await incidentRow(inc.id)).status).toBe('resolved') + expect(await statusOf(svc.id)).toBe('partial_outage') + expect(await serviceEvents(svc.id)).toHaveLength(1) + + // A later recompute on the service sets it from the open incidents. + const next = await createIncident(admin, { + title: `${marker} next`, + services: [[svc.id, 'degraded_performance']], + }) + await postUpdate(admin, next.id, 'resolved', 'all good') + expect(await statusOf(svc.id)).toBe('operational') + } + ) + + test( + 'STA-A-23 two open incidents on one service: the worst wins and the rest survive a resolve', + { tag: '@core' }, + async ({ marker }) => { + const svc = await createService(admin, `${marker} Shared`) + const one = await createIncident(admin, { + title: `${marker} 1`, + services: [[svc.id, 'degraded_performance']], + }) + const two = await createIncident(admin, { + title: `${marker} 2`, + services: [[svc.id, 'major_outage']], + }) + expect(await statusOf(svc.id)).toBe('major_outage') + await postUpdate(admin, two.id, 'resolved', 'two fixed') + expect(await statusOf(svc.id)).toBe('degraded_performance') + await postUpdate(admin, one.id, 'resolved', 'one fixed') + expect(await statusOf(svc.id)).toBe('operational') + expect((await serviceEvents(svc.id)).map((e) => e.status)).toEqual([ + 'degraded_performance', + 'major_outage', + 'degraded_performance', + 'operational', + ]) + } + ) + + test( + 'STA-A-24 reopening a resolved incident holds its services again', + { tag: '@core' }, + async ({ marker }) => { + const svc = await createService(admin, `${marker} Reopen`) + const inc = await createIncident(admin, { + title: marker, + services: [[svc.id, 'major_outage']], + }) + await postUpdate(admin, inc.id, 'resolved', 'fixed') + const firstResolve = (await incidentRow(inc.id)).resolved_at! + expect(await statusOf(svc.id)).toBe('operational') + + await postUpdate(admin, inc.id, 'investigating', 'it is back') + const reopened = await incidentRow(inc.id) + expect(reopened.status).toBe('investigating') + expect(reopened.resolved_at).toBeNull() + expect(await statusOf(svc.id)).toBe('major_outage') + const active = await publicSnapshot(admin) + expect(active.activeIncidents.map((i) => i.id)).toContain(inc.id) + const open = await must<{ items: { id: string }[] }>( + admin, + 'listStatusIncidentsAdminFn', + { kind: 'incident', state: 'active', search: marker }, + { method: 'GET' } + ) + expect(open.items.map((i) => i.id)).toEqual([inc.id]) + expect(await domainEventTypes(inc.id)).toEqual([ + 'status.incident_created', + 'status.incident_updated', + 'status.incident_updated', + ]) + + await postUpdate(admin, inc.id, 'resolved', 'fixed again') + const second = (await incidentRow(inc.id)).resolved_at! + expect(second.getTime()).toBeGreaterThan(firstResolve.getTime()) + } + ) + + test( + 'STA-A-25 a note on a resolved incident changes nothing else', + { tag: '@nightly' }, + async ({ marker }) => { + const svc = await createService(admin, `${marker} Note`) + const inc = await createIncident(admin, { + title: marker, + services: [[svc.id, 'major_outage']], + }) + await postUpdate(admin, inc.id, 'resolved', 'fixed') + const resolvedAt = (await incidentRow(inc.id)).resolved_at! + await setServiceStatus(admin, svc.id, 'partial_outage') + await postUpdate(admin, inc.id, 'resolved', 'postscript') + expect((await incidentRow(inc.id)).resolved_at).toEqual(resolvedAt) + expect(await incidentUpdates(inc.id)).toHaveLength(3) + expect(await statusOf(svc.id)).toBe('partial_outage') + expect( + (await domainEventTypes(inc.id)).filter((t) => t === 'status.incident_updated') + ).toHaveLength(2) + } + ) +}) + +test.describe('refusals and impact', () => { + test('STA-A-18 report-incident refusals write nothing', { tag: '@core' }, async ({ marker }) => { + const svc = await createService(admin, `${marker} Refuse`) + const base = { + title: marker, + services: [[svc.id, 'major_outage']] as [string, 'major_outage'][], + } + + // Zod: empty title, empty body, no services. + for (const bad of [ + { ...base, title: '' }, + { ...base, body: '' }, + { ...base, services: [] }, + ]) { + const res = await tryCreateIncident(admin, bad) + expect(res.ok, JSON.stringify(bad)).toBe(false) + } + const blankTitle = await tryCreateIncident(admin, { ...base, title: ' ' }) + expect(blankTitle.code).toBe('VALIDATION_ERROR') + expect(blankTitle.raw).toContain('Title is required') + const blankBody = await tryCreateIncident(admin, { ...base, body: ' ' }) + expect(blankBody.raw).toContain('Update body is required') + const wrongStatus = await tryCreateIncident(admin, { ...base, status: 'scheduled' }) + expect(wrongStatus.code).toBe('VALIDATION_ERROR') + expect(messageOf(wrongStatus)).toBe( + '"scheduled" is not an incident status. Use one of: investigating, identified, monitoring, resolved.' + ) + const gone = await createService(admin, `${marker} Gone`) + await must(admin, 'deleteStatusComponentFn', { id: gone.id }) + const deleted = await tryCreateIncident(admin, { + ...base, + services: [ + [svc.id, 'major_outage'], + [gone.id, 'major_outage'], + ], + }) + expect(deleted.raw).toContain( + 'An affected service has been deleted. Choose the services again.' + ) + const order = await tryCreateIncident(admin, { + ...base, + kind: 'maintenance', + status: 'scheduled', + scheduledStartAt: new Date(Date.now() + 2 * 3600_000), + scheduledEndAt: new Date(Date.now() + 3600_000), + }) + expect(order.raw).toContain('The scheduled end must be after the scheduled start') + + // Nothing was written: no incident by that title and the service never moved. + const list = await must<{ items: unknown[] }>( + admin, + 'listStatusIncidentsAdminFn', + { state: 'all', search: marker }, + { method: 'GET' } + ) + expect(list.items).toEqual([]) + expect(await statusOf(svc.id)).toBe('operational') + expect(await serviceEvents(svc.id)).toEqual([]) + }) + + test( + 'STA-A-19 impact is derived from the worst status unless pinned', + { tag: '@core' }, + async ({ marker }) => { + const svc = await createService(admin, `${marker} Impact`) + const other = await createService(admin, `${marker} Impact 2`) + for (const [status, impact] of [ + ['degraded_performance', 'minor'], + ['partial_outage', 'major'], + ['major_outage', 'critical'], + ['under_maintenance', 'none'], + ] as const) { + const inc = await createIncident(admin, { + title: `${marker} ${status}`, + services: [[svc.id, status]], + }) + expect(inc.impact, status).toBe(impact) + expect(inc.impactOverride).toBe(false) + } + // An unpinned incident re-derives on a services edit. + const unpinned = await createIncident(admin, { + title: `${marker} unpinned`, + services: [[other.id, 'degraded_performance']], + }) + const rederived = await must<{ impact: string }>(admin, 'updateStatusIncidentFn', { + id: unpinned.id, + affectedComponents: [{ componentId: other.id, componentStatus: 'major_outage' }], + }) + expect(rederived.impact).toBe('critical') + // A pinned impact stays when services change. + const pinned = await createIncident(admin, { + title: `${marker} pinned`, + services: [[other.id, 'major_outage']], + impact: 'minor', + impactOverride: true, + }) + expect(pinned.impact).toBe('minor') + const edited = await must<{ impact: string; impactOverride: boolean }>( + admin, + 'updateStatusIncidentFn', + { + id: pinned.id, + affectedComponents: [{ componentId: other.id, componentStatus: 'partial_outage' }], + } + ) + expect(edited).toMatchObject({ impact: 'minor', impactOverride: true }) + expect((await incidentRow(pinned.id)).impact).toBe('minor') + } + ) +}) + +test.describe('editing', () => { + test( + 'STA-A-26 sidebar edits autosave and apply live', + { tag: '@core' }, + async ({ asPerson, page, marker }) => { + const a = await createService(admin, `${marker} Edit A`) + const b = await createService(admin, `${marker} Edit B`) + const inc = await createIncident(admin, { + title: `${marker} before`, + services: [[a.id, 'major_outage']], + }) + expect((await incidentRow(inc.id)).details_edited_at).toBeNull() + + const ui = await asPerson('admin') + await openAdmin(ui, `/admin/status?view=open&incident=${inc.id}`) + await expect(ui.getByText('Details save automatically.')).toBeVisible() + const titleBox = ui + .locator('div', { has: ui.locator('p', { hasText: /^Title$/ }) }) + .last() + .locator('input') + await titleBox.fill(`${marker} after`) + await expect(ui.getByText('Saved', { exact: true })).toBeVisible() + await pollFor('the title to save', async () => + (await incidentRow(inc.id)).title === `${marker} after` ? true : null + ) + const stamped = (await incidentRow(inc.id)).details_edited_at + expect(stamped).not.toBeNull() + + // Add B (live on an open incident) and remove A (recomputed). + const sidebar = ui + .locator('div', { has: ui.locator('p', { hasText: /^Affected services$/ }) }) + .last() + await toggleService(sidebar, b.name) + await pollFor('B to be held', async () => + (await statusOf(b.id)) === 'degraded_performance' ? true : null + ) + await toggleService(sidebar, a.name) + await pollFor('A to be released', async () => + (await statusOf(a.id)) === 'operational' ? true : null + ) + + // An impact-only change does not stamp details_edited_at. + const before = (await incidentRow(inc.id)).details_edited_at! + await must(admin, 'updateStatusIncidentFn', { + id: inc.id, + impact: 'minor', + impactOverride: true, + }) + expect((await incidentRow(inc.id)).details_edited_at).toEqual(before) + // Re-sending the same values does not stamp it either. + await must(admin, 'updateStatusIncidentFn', { id: inc.id, title: `${marker} after` }) + expect((await incidentRow(inc.id)).details_edited_at).toEqual(before) + + // No webhook event for edits; the public title follows. + expect(await domainEventTypes(inc.id)).toEqual(['status.incident_created']) + await page.goto(`/status/${inc.id}`) + await expect(page.getByRole('heading', { name: `${marker} after` })).toBeVisible() + } + ) + + test( + 'STA-A-27 editing services on a resolved incident leaves live statuses alone', + { tag: '@nightly' }, + async ({ marker }) => { + const a = await createService(admin, `${marker} Res A`) + const s = await createService(admin, `${marker} Res S`) + const inc = await createIncident(admin, { title: marker, services: [[a.id, 'major_outage']] }) + await postUpdate(admin, inc.id, 'resolved', 'done') + await must(admin, 'updateStatusIncidentFn', { + id: inc.id, + affectedComponents: [ + { componentId: a.id, componentStatus: 'major_outage' }, + { componentId: s.id, componentStatus: 'major_outage' }, + ], + }) + expect((await incidentLinks(inc.id)).map((l) => l.component_id)).toContain( + toUuid(s.id as never) + ) + expect(await statusOf(s.id)).toBe('operational') + expect(await serviceEvents(s.id)).toEqual([]) + } + ) +}) + +test.describe('backfill and already-resolved', () => { + test('STA-A-28 log a past incident', { tag: '@core' }, async ({ asPerson, marker }) => { + const svc = await createService(admin, `${marker} Past`) + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/status?view=open') + await ui.getByRole('button', { name: 'Report incident' }).first().click() + await ui.getByRole('button', { name: 'Log a past incident instead' }).click() + const dialog = ui.getByRole('dialog', { name: 'Log a past incident' }) + await expect( + dialog.getByText( + 'Adds a resolved incident to your incident history. It does not change uptime or service status, and subscribers are not emailed.' + ) + ).toBeVisible() + await dialog.getByLabel('Title').fill(`${marker} last week`) + await dialog.getByLabel("What's happening?").fill('It happened') + await toggleService(dialog, svc.name) + // No dates yet: the button stays disabled. + await expect(dialog.getByRole('button', { name: 'Log past incident' })).toBeDisabled() + await dialog.getByRole('button', { name: 'Cancel' }).click() + + // The dates go through the server fn (the date-time picker is a calendar widget). + const startedAt = new Date(Date.now() - 3 * 86_400_000) + const resolvedAt = new Date(Date.now() - 3 * 86_400_000 + 3_600_000) + const inc = await createIncident(admin, { + title: `${marker} last week`, + status: 'resolved', + services: [[svc.id, 'major_outage']], + backfill: { startedAt, resolvedAt }, + }) + const row = await incidentRow(inc.id) + expect(row).toMatchObject({ + status: 'resolved', + backfilled: true, + notified_at: null, + notify_subscribers: false, + }) + expect(row.started_at.toISOString()).toBe(startedAt.toISOString()) + expect(row.resolved_at!.toISOString()).toBe(resolvedAt.toISOString()) + expect((await incidentUpdates(inc.id))[0].created_at.toISOString()).toBe( + resolvedAt.toISOString() + ) + expect(await statusOf(svc.id)).toBe('operational') + expect(await serviceEvents(svc.id)).toEqual([]) + expect(await domainEventTypes(inc.id)).toEqual([]) + + await openAdmin(ui, `/admin/status?view=all&incident=${inc.id}`) + await expect(ui.getByText('Backfilled incident: subscribers were never emailed.')).toBeVisible() + expect(recentIds(await publicSnapshot(admin))).toContain(inc.id) + }) + + test('STA-A-29 backfill refusals', { tag: '@core' }, async ({ marker }) => { + const svc = await createService(admin, `${marker} Backfill`) + const base = { + title: marker, + services: [[svc.id, 'major_outage']] as [string, 'major_outage'][], + } + const t0 = new Date(Date.now() - 86_400_000) + const outOfOrder = await tryCreateIncident(admin, { + ...base, + status: 'resolved', + backfill: { startedAt: t0, resolvedAt: new Date(t0.getTime() - 60_000) }, + }) + expect(outOfOrder.raw).toContain('A past incident must be resolved after it started') + const live = await tryCreateIncident(admin, { + ...base, + status: 'investigating', + backfill: { startedAt: t0, resolvedAt: new Date(t0.getTime() + 60_000) }, + }) + expect(messageOf(live)).toBe( + 'A past incident is logged as resolved. Use the "resolved" status.' + ) + + const past = await createIncident(admin, { + ...base, + status: 'resolved', + backfill: { startedAt: t0, resolvedAt: new Date(t0.getTime() + 60_000) }, + }) + const reopen = await callServerFn(admin, 'postStatusIncidentUpdateFn', { + id: past.id, + status: 'investigating', + body: 'reopen', + }) + expect(reopen.ok).toBe(false) + expect(reopen.raw).toContain( + "A logged past incident can't be reopened. Report a new incident instead." + ) + expect((await incidentRow(past.id)).status).toBe('resolved') + }) + + test( + 'STA-A-30 an incident created already resolved announces nothing', + { + tag: ['@nightly', '@quarantine'], + annotation: { + type: 'quarantine', + description: + 'createIncident (status.service.ts:242) defaults notifySubscribers to true for a non-backfill row created already resolved, so it announces status.incident_created for something already over; see QUARANTINE.md', + }, + }, + async ({ marker }) => { + const svc = await createService(admin, `${marker} Over`) + const inc = await createIncident(admin, { + title: marker, + status: 'resolved', + services: [[svc.id, 'major_outage']], + notifySubscribers: true, + }) + const row = await incidentRow(inc.id) + expect(row.resolved_at).not.toBeNull() + expect(await statusOf(svc.id)).toBe('operational') + expect(await serviceEvents(svc.id)).toEqual([]) + expect(await domainEventTypes(inc.id)).not.toContain('status.incident_created') + } + ) +}) + +test.describe('delete and lists', () => { + test( + 'STA-A-31 delete an incident from the list', + { tag: '@core' }, + async ({ asPerson, page, marker }) => { + const svc = await createService(admin, `${marker} Del`) + const keep = await createIncident(admin, { + title: `${marker} keep`, + services: [[svc.id, 'degraded_performance']], + }) + const inc = await createIncident(admin, { + title: `${marker} delete me`, + services: [[svc.id, 'major_outage']], + }) + expect(await statusOf(svc.id)).toBe('major_outage') + + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/status?view=open') + await searchList( + ui, + `${marker} delete`, + ui.getByRole('heading', { name: `${marker} delete me` }) + ) + const row = ui.locator('div.group', { hasText: `${marker} delete me` }) + await row.hover() + await row.getByRole('button').click() + await ui.getByRole('menuitem', { name: 'Delete' }).click() + const confirm = ui.getByRole('alertdialog').or(ui.getByRole('dialog')) + await expect(confirm.getByText('Delete incident?')).toBeVisible() + await expect( + confirm.getByText( + 'It will be removed from your status page and incident history. Any services it affects go back to the status set by your other open incidents.' + ) + ).toBeVisible() + await confirm.getByRole('button', { name: 'Delete' }).click() + await pollFor('the delete', async () => + (await incidentRow(inc.id)).deleted_at ? true : null + ) + + // Its service falls back to the other open incident. + expect(await statusOf(svc.id)).toBe('degraded_performance') + expect(await domainEventTypes(inc.id)).toEqual(['status.incident_created']) + await page.goto(`/status/${inc.id}`) + await expect(page.getByText('Incident not found')).toBeVisible() + const again = await callServerFn(admin, 'deleteStatusIncidentFn', { id: inc.id }) + expect(again.ok).toBe(false) + expect(again.code).toBe('STATUS_INCIDENT_NOT_FOUND') + expect((await incidentRow(keep.id)).deleted_at).toBeNull() + } + ) + + test( + 'STA-A-32 incident lists: tabs, search and sort', + { tag: '@nightly' }, + async ({ asPerson, marker }) => { + const svc = await createService(admin, `${marker} List`) + // Critical first, so newest-first puts minor on top and the Impact sort flips it. + await createIncident(admin, { + title: `${marker} critical one`, + services: [[svc.id, 'major_outage']], + }) + await createIncident(admin, { + title: `${marker} minor one`, + services: [[svc.id, 'degraded_performance']], + }) + const resolved = await createIncident(admin, { + title: `${marker} resolved one`, + services: [[svc.id, 'partial_outage']], + }) + await postUpdate(admin, resolved.id, 'resolved', `${marker}-needle-in-body`) + + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/status?view=open') + await searchList(ui, marker, ui.getByRole('heading', { name: `${marker} minor one` })) + await expect(ui.getByRole('heading', { name: `${marker} critical one` })).toBeVisible() + await expect(ui.getByRole('heading', { name: `${marker} resolved one` })).toHaveCount(0) + const titles = ui.locator('h3', { hasText: marker }) + await expect(titles).toHaveText([`${marker} minor one`, `${marker} critical one`]) + await ui.getByRole('button', { name: /Newest/ }).click() + await ui.getByRole('menuitemradio', { name: 'Impact' }).click() + await expect(titles).toHaveText([`${marker} critical one`, `${marker} minor one`]) + + // Search matches an update body, case-insensitively, on All. + await openAdmin(ui, '/admin/status?view=all') + await searchList( + ui, + `${marker}-NEEDLE`.toUpperCase(), + ui.getByRole('heading', { name: `${marker} resolved one` }) + ) + await expect(titles).toHaveCount(1) + await ui.getByPlaceholder('Search incidents…').fill(`${marker}-nothing-here`) + await expect(ui.getByText('No incidents match your search')).toBeVisible() + } + ) + + test( + 'STA-A-33 editor deep link to a deleted incident says not found', + { tag: '@nightly' }, + async ({ asPerson, marker }) => { + const svc = await createService(admin, `${marker} Missing`) + const inc = await createIncident(admin, { + title: marker, + services: [[svc.id, 'degraded_performance']], + }) + await must(admin, 'deleteStatusIncidentFn', { id: inc.id }) + expect(await getIncidentAdmin(admin, inc.id)).toBeNull() + const ui = await asPerson('admin') + await openAdmin(ui, `/admin/status?incident=${inc.id}`) + await expect(ui.getByRole('heading', { name: 'Incident not found' })).toBeVisible() + await expect(ui.getByText('It may have been deleted, or the link is wrong.')).toBeVisible() + } + ) + + test( + 'STA-A-33 editor deep link to a malformed id says not found', + { + tag: ['@nightly', '@quarantine'], + annotation: { + type: 'quarantine', + description: + 'findStatusIncidentById (status.service.ts:866) throws on a malformed id instead of returning null, so the editor shows "Couldn\'t load this incident"; see QUARANTINE.md', + }, + }, + async ({ asPerson }) => { + const res = await callServerFn( + admin, + 'getStatusIncidentAdminFn', + { id: 'not-an-id' }, + { method: 'GET' } + ) + expect(res.ok, res.raw.slice(0, 300)).toBe(true) + expect(res.data).toBeNull() + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/status?incident=not-an-id') + await expect(ui.getByRole('heading', { name: 'Incident not found' })).toBeVisible() + } + ) +}) diff --git a/apps/web/e2e/tests/status/admin-maintenance.spec.ts b/apps/web/e2e/tests/status/admin-maintenance.spec.ts new file mode 100644 index 0000000000..694accbdee --- /dev/null +++ b/apps/web/e2e/tests/status/admin-maintenance.spec.ts @@ -0,0 +1,486 @@ +/** + * Status admin: scheduled maintenance, from scheduling through the scheduler's + * start and complete, start now, reschedule, move back and delete. + * + * Journeys: STA-A-34, STA-A-35, STA-A-36, STA-A-37, STA-A-38, STA-A-39, + * STA-A-40, STA-A-42, STA-A-43, STA-A-44, STA-A-45, STA-A-46. + * + * The scheduler's handlers compare the window with the wall clock, so a test + * backdates its own window's bounds and runs the `status-maintenance` sweep + * (every overdue window in the workspace); it asserts only on its own rows. + */ +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 { + backdateWindow, + createService, + domainEvents, + domainEventTypes, + incidentRow, + incidentUpdates, + maintenanceJobs, + messageOf, + minutesFromNow, + must, + openAdmin, + pickDateTime, + postUpdate, + publicSnapshot, + runMaintenanceSweep, + scheduleWindow, + serviceEvents, + serviceRow, + toggleService, + tryCreateIncident, +} from '../../product/status' + +test.describe.configure({ timeout: 60_000 }) + +let admin: APIRequestContext + +test.beforeEach(async ({ playwright }) => { + admin = await personApi(playwright, 'admin') +}) +test.afterEach(async () => { + await admin.dispose() +}) + +const statusOf = async (id: string) => (await serviceRow(id)).status +const STARTED_TEXT = 'This scheduled maintenance is now in progress.' +const COMPLETED_TEXT = 'This scheduled maintenance is complete.' + +test( + 'STA-A-34 schedule a maintenance window from the dialog', + { tag: '@smoke' }, + async ({ asPerson, marker }) => { + const svc = await createService(admin, `${marker} Database`) + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/status?view=maintenance') + await ui.getByRole('button', { name: 'Schedule maintenance' }).first().click() + const dialog = ui.getByRole('dialog', { name: 'Schedule maintenance' }) + await expect( + dialog.getByText('Appears on your status page and emails subscribers once, when scheduled.') + ).toBeVisible() + await dialog.getByLabel('Title').fill(`${marker} upgrade`) + await dialog.getByLabel("What's planned?").fill('Upgrading the primary') + await toggleService(dialog, svc.name) + const tomorrow = new Date(Date.now() + 86_400_000) + const start = new Date(tomorrow.getFullYear(), tomorrow.getMonth(), tomorrow.getDate(), 10, 0) + const end = new Date(tomorrow.getFullYear(), tomorrow.getMonth(), tomorrow.getDate(), 12, 0) + const picker = (label: string) => + dialog + .locator('div.space-y-2', { has: ui.getByText(label, { exact: true }) }) + .getByRole('button') + const pickers = { nth: (i: number) => picker(i === 0 ? 'Scheduled start' : 'Scheduled end') } + // An end before the start keeps the button disabled. + await pickDateTime(ui, pickers.nth(0), end) + await pickDateTime(ui, pickers.nth(1), start) + await expect(dialog.getByText('The end must be after the start.')).toBeVisible() + const submit = dialog.getByRole('button', { name: 'Schedule maintenance' }) + await expect(submit).toBeDisabled() + await pickDateTime(ui, pickers.nth(0), start) + await pickDateTime(ui, pickers.nth(1), end) + await expect(dialog.getByText('The end must be after the start.')).toBeHidden() + // Auto-start and auto-complete are on by default. + for (const sw of await dialog.getByRole('switch').all()) await expect(sw).toBeChecked() + // Keep the publish email off: other tests' subscribers would get it. + await dialog.getByRole('checkbox', { name: /Email subscribers/ }).click() + await submit.click() + + await expect(dialog).toBeHidden() + await expect(ui).toHaveURL(/view=maintenance/) + await expect(ui).toHaveURL(/incident=status_incident_/) + const id = new URL(ui.url()).searchParams.get('incident')! + const row = await incidentRow(id) + expect(row).toMatchObject({ + kind: 'maintenance', + status: 'scheduled', + impact: 'maintenance', + auto_start: true, + auto_complete: true, + }) + expect(row.scheduled_start_at!.getTime()).toBe(start.getTime()) + expect(row.scheduled_end_at!.getTime()).toBe(end.getTime()) + expect(row.started_at.getTime()).toBe(start.getTime()) + expect(await statusOf(svc.id)).toBe('operational') + expect(await domainEventTypes(id)).toEqual(['status.maintenance_scheduled']) + const jobs = await maintenanceJobs(id) + expect(jobs.map((j) => j.kind)).toEqual(['complete', 'start']) + expect( + Math.abs(jobs.find((j) => j.kind === 'start')!.runAt.getTime() - start.getTime()) + ).toBeLessThan(1_000) + expect( + Math.abs(jobs.find((j) => j.kind === 'complete')!.runAt.getTime() - end.getTime()) + ).toBeLessThan(1_000) + const snap = await publicSnapshot(admin) + expect(snap.upcomingMaintenance.map((w) => w.id)).toContain(id) + } +) + +test( + 'STA-A-35 STA-A-36 the scheduler starts a window at its start and completes it at its end', + { tag: '@core' }, + async ({ request, marker }) => { + const svc = await createService(admin, `${marker} Sched`) + const w = await scheduleWindow(admin, { + title: `${marker} window`, + services: [[svc.id, 'under_maintenance']], + }) + // Its start passes (only the start: both at once would start and complete it in one pass). + const realStart = new Date(Date.now() - 1_000) + await backdateWindow(w.id, { start: realStart }) + await runMaintenanceSweep(request) + const started = await incidentRow(w.id) + expect(started.status).toBe('in_progress') + expect(Math.abs(started.started_at.getTime() - Date.now())).toBeLessThan(30_000) + expect(await statusOf(svc.id)).toBe('under_maintenance') + expect((await serviceEvents(svc.id)).map((e) => [e.status, e.source])).toEqual([ + ['under_maintenance', 'maintenance'], + ]) + const timeline = await incidentUpdates(w.id) + expect(timeline.map((u) => [u.status, u.body])).toEqual([ + ['scheduled', `First update for ${marker} window`], + ['in_progress', STARTED_TEXT], + ]) + const startEvent = (await domainEvents(w.id)).find( + (e) => e.type === 'status.maintenance_started' + ) + expect(startEvent?.actor_type).toBe('service') + expect(startEvent?.payload).toMatchObject({ + incidentId: w.id, + title: `${marker} window`, + componentIds: [svc.id], + }) + expect((await publicSnapshot(admin)).activeIncidents.map((i) => i.id)).toContain(w.id) + + // Its end passes. + await backdateWindow(w.id, { end: new Date(Date.now() - 1_000) }) + await runMaintenanceSweep(request) + const done = await incidentRow(w.id) + expect(done.status).toBe('completed') + expect(done.resolved_at).not.toBeNull() + expect(await statusOf(svc.id)).toBe('operational') + expect((await incidentUpdates(w.id)).at(-1)?.body).toBe(COMPLETED_TEXT) + expect(await domainEventTypes(w.id)).toEqual([ + 'status.maintenance_scheduled', + 'status.maintenance_started', + 'status.maintenance_completed', + ]) + const snap = await publicSnapshot(admin) + expect(snap.activeIncidents.map((i) => i.id)).not.toContain(w.id) + expect(snap.upcomingMaintenance.map((i) => i.id)).not.toContain(w.id) + // Recent incidents list incidents only; the window stays readable as history. + const pub = await must<{ status: string }>( + admin, + 'getStatusIncidentPublicFn', + { id: w.id }, + { method: 'GET' } + ) + expect(pub.status).toBe('completed') + } +) + +test( + 'STA-A-37 auto-start off: the window waits, then auto-complete finishes it', + { tag: '@nightly' }, + async ({ request, marker }) => { + const svc = await createService(admin, `${marker} Manual`) + const w = await scheduleWindow(admin, { + title: marker, + services: [[svc.id, 'under_maintenance']], + autoStart: false, + }) + expect((await maintenanceJobs(w.id)).map((j) => j.kind)).toEqual(['complete']) + await backdateWindow(w.id, { start: new Date(Date.now() - 60_000) }) + await runMaintenanceSweep(request) + expect((await incidentRow(w.id)).status).toBe('scheduled') + expect((await publicSnapshot(admin)).upcomingMaintenance.map((i) => i.id)).toContain(w.id) + + await backdateWindow(w.id, { end: new Date(Date.now() - 1_000) }) + await runMaintenanceSweep(request) + const done = await incidentRow(w.id) + expect(done.status).toBe('completed') + expect(done.started_at.getTime()).toBeLessThanOrEqual(done.resolved_at!.getTime()) + expect(await serviceEvents(svc.id)).toEqual([]) + expect(await domainEventTypes(w.id)).toContain('status.maintenance_completed') + expect(await domainEventTypes(w.id)).not.toContain('status.maintenance_started') + } +) + +test('STA-A-38 start now from the overview', { tag: '@core' }, async ({ asPerson, marker }) => { + const svc = await createService(admin, `${marker} Now`) + const w = await scheduleWindow(admin, { + title: `${marker} soon`, + services: [[svc.id, 'under_maintenance']], + startMin: 3, + endMin: 90, + }) + const plannedEnd = (await incidentRow(w.id)).scheduled_end_at! + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/status?view=overview') + // The Upcoming maintenance card row: the window's title and its Start now. + const row = ui + .locator('div', { has: ui.getByRole('button', { name: `${marker} soon` }) }) + .filter({ has: ui.getByRole('button', { name: 'Start now' }) }) + .last() + await row.getByRole('button', { name: 'Start now' }).click() + const confirm = ui.getByRole('alertdialog').or(ui.getByRole('dialog')) + await expect(confirm.getByText('Start maintenance now?')).toBeVisible() + await expect( + confirm.getByText( + 'The window begins immediately: affected services switch to Under maintenance on the public page and an update is posted to the timeline. Subscribers are not emailed.' + ) + ).toBeVisible() + await confirm.getByRole('button', { name: 'Start now' }).click() + await expect(ui.getByText('Maintenance started').first()).toBeVisible({ timeout: 15_000 }) + + const row2 = await pollFor('the start', async () => { + const r = await incidentRow(w.id) + return r.status === 'in_progress' ? r : null + }) + expect(Math.abs(row2.started_at.getTime() - Date.now())).toBeLessThan(60_000) + expect(row2.scheduled_end_at).toEqual(plannedEnd) + expect(await statusOf(svc.id)).toBe('under_maintenance') + expect((await incidentUpdates(w.id)).at(-1)?.body).toBe(STARTED_TEXT) + const jobs = await maintenanceJobs(w.id) + expect(jobs.map((j) => j.kind)).toEqual(['complete']) + expect(Math.abs(jobs[0].runAt.getTime() - plannedEnd.getTime())).toBeLessThan(1_000) + expect(await domainEventTypes(w.id)).toEqual([ + 'status.maintenance_scheduled', + 'status.maintenance_started', + ]) + + // Again, on an incident, and on an unknown id. + const again = await callServerFn(admin, 'startStatusMaintenanceNowFn', { id: w.id }) + expect(again.code).toBe('STATUS_MAINTENANCE_NOT_STARTABLE') + expect(messageOf(again)).toBe('Only a scheduled maintenance window can be started now') + const missing = await callServerFn(admin, 'startStatusMaintenanceNowFn', { + id: 'status_incident_01m7v6aewge02vtv3kd36ggmcw', + }) + expect(missing.code).toBe('STATUS_INCIDENT_NOT_FOUND') + expect(messageOf(missing)).toBe('Maintenance not found') +}) + +test( + 'STA-A-39 start a window from the stepper with your own words', + { tag: '@core' }, + async ({ request, marker }) => { + const svc = await createService(admin, `${marker} Stepper`) + const w = await scheduleWindow(admin, { + title: marker, + services: [[svc.id, 'under_maintenance']], + }) + await postUpdate(admin, w.id, 'in_progress', `${marker} starting early`) + const row = await incidentRow(w.id) + expect(row.status).toBe('in_progress') + const timeline = await incidentUpdates(w.id) + expect(timeline.filter((u) => u.status === 'in_progress').map((u) => u.body)).toEqual([ + `${marker} starting early`, + ]) + expect(timeline.at(-1)!.created_at.getTime()).toBe(row.started_at.getTime()) + expect(await statusOf(svc.id)).toBe('under_maintenance') + expect((await maintenanceJobs(w.id)).map((j) => j.kind)).toEqual(['complete']) + expect(await domainEventTypes(w.id)).toEqual([ + 'status.maintenance_scheduled', + 'status.incident_updated', + 'status.maintenance_started', + ]) + + // A window the scheduler already started takes a plain update. + const other = await scheduleWindow(admin, { + title: `${marker} other`, + services: [[svc.id, 'under_maintenance']], + }) + await backdateWindow(other.id, { start: new Date(Date.now() - 1_000) }) + await runMaintenanceSweep(request) + expect((await incidentRow(other.id)).status).toBe('in_progress') + await postUpdate(admin, other.id, 'in_progress', 'still going') + expect( + (await domainEventTypes(other.id)).filter((t) => t === 'status.maintenance_started') + ).toHaveLength(1) + } +) + +test('STA-A-40 reschedule a window', { tag: '@core' }, async ({ marker }) => { + const svc = await createService(admin, `${marker} Move`) + const w = await scheduleWindow(admin, { + title: marker, + services: [[svc.id, 'under_maintenance']], + }) + const oldJobs = await maintenanceJobs(w.id) + expect(oldJobs).toHaveLength(2) + const newStart = minutesFromNow(240) + const newEnd = minutesFromNow(300) + await must(admin, 'updateStatusIncidentFn', { + id: w.id, + scheduledStartAt: newStart, + scheduledEndAt: newEnd, + }) + const row = await incidentRow(w.id) + expect(row.started_at.getTime()).toBe(newStart.getTime()) + expect(row.details_edited_at).not.toBeNull() + const jobs = await maintenanceJobs(w.id) + expect(jobs.map((j) => j.kind)).toEqual(['complete', 'start']) + expect(jobs[0].hash).not.toBe(oldJobs[0].hash) + // The delay is computed from the clock, so allow a few ms. + expect( + Math.abs(jobs.find((j) => j.kind === 'start')!.runAt.getTime() - newStart.getTime()) + ).toBeLessThan(1_000) + + const bad = await callServerFn(admin, 'updateStatusIncidentFn', { + id: w.id, + scheduledStartAt: newEnd, + scheduledEndAt: newStart, + }) + expect(messageOf(bad)).toBe('The scheduled end must be after the scheduled start') + expect((await incidentRow(w.id)).scheduled_start_at!.getTime()).toBe(newStart.getTime()) +}) + +test( + 'STA-A-42 move a running window back, or reopen a completed one', + { tag: '@nightly' }, + async ({ request, marker }) => { + const svc = await createService(admin, `${marker} Back`) + const w = await scheduleWindow(admin, { + title: marker, + services: [[svc.id, 'under_maintenance']], + }) + const planned = (await incidentRow(w.id)).scheduled_start_at! + const pastStart = new Date(Date.now() - 60_000) + await backdateWindow(w.id, { start: pastStart }) + await runMaintenanceSweep(request) + expect(await statusOf(svc.id)).toBe('under_maintenance') + + const back = await postUpdate(admin, w.id, 'scheduled', 'not yet') + expect(back.autoStart).toBe(false) + const row = await incidentRow(w.id) + expect(row.status).toBe('scheduled') + expect(row.started_at.getTime()).toBe(pastStart.getTime()) + expect(planned).not.toEqual(pastStart) + expect(await statusOf(svc.id)).toBe('operational') + // Only the end, still ahead, is armed. + expect((await maintenanceJobs(w.id)).map((j) => j.kind)).toEqual(['complete']) + + // Complete it, let its end pass, then reopen it. + await postUpdate(admin, w.id, 'completed', 'done') + await backdateWindow(w.id, { end: new Date(Date.now() - 1_000) }) + const reopened = await postUpdate(admin, w.id, 'in_progress', 'back on') + expect(reopened.autoComplete).toBe(false) + const again = await incidentRow(w.id) + expect(again.resolved_at).toBeNull() + expect(await statusOf(svc.id)).toBe('under_maintenance') + } +) + +test( + 'STA-A-43 complete a window by hand before its end', + { tag: '@core' }, + async ({ request, marker }) => { + const svc = await createService(admin, `${marker} Early`) + const w = await scheduleWindow(admin, { + title: marker, + services: [[svc.id, 'under_maintenance']], + }) + await postUpdate(admin, w.id, 'in_progress', 'starting') + await postUpdate(admin, w.id, 'completed', 'finished early') + const row = await incidentRow(w.id) + expect(row.status).toBe('completed') + expect(row.resolved_at).not.toBeNull() + expect(await statusOf(svc.id)).toBe('operational') + expect((await domainEventTypes(w.id)).slice(-2)).toEqual([ + 'status.incident_updated', + 'status.maintenance_completed', + ]) + // Its auto-complete, when the end passes, does nothing more. + await backdateWindow(w.id, { end: new Date(Date.now() - 1_000) }) + await runMaintenanceSweep(request) + expect( + (await domainEventTypes(w.id)).filter((x) => x === 'status.maintenance_completed') + ).toHaveLength(1) + } +) + +test( + 'STA-A-44 the sweep catches a window whose start and end passed while down', + { tag: '@nightly' }, + async ({ request, marker }) => { + const svc = await createService(admin, `${marker} Missed`) + const w = await scheduleWindow(admin, { + title: marker, + services: [[svc.id, 'under_maintenance']], + }) + // Both bounds passed with nothing listening: no jobs left. + await backdateWindow(w.id, { + start: new Date(Date.now() - 2 * 60_000), + end: new Date(Date.now() - 60_000), + }) + await Promise.all([runMaintenanceSweep(request), runMaintenanceSweep(request)]) + expect((await incidentRow(w.id)).status).toBe('completed') + expect((await incidentUpdates(w.id)).map((u) => u.body).slice(1)).toEqual([ + STARTED_TEXT, + COMPLETED_TEXT, + ]) + expect(await domainEventTypes(w.id)).toEqual([ + 'status.maintenance_scheduled', + 'status.maintenance_started', + 'status.maintenance_completed', + ]) + expect(await statusOf(svc.id)).toBe('operational') + } +) + +test('STA-A-45 delete a scheduled or running window', { tag: '@core' }, async ({ marker }) => { + const svc = await createService(admin, `${marker} Gone`) + const scheduled = await scheduleWindow(admin, { + title: `${marker} scheduled`, + services: [[svc.id, 'under_maintenance']], + }) + expect(await maintenanceJobs(scheduled.id)).toHaveLength(2) + await must(admin, 'deleteStatusIncidentFn', { id: scheduled.id }) + expect(await maintenanceJobs(scheduled.id)).toEqual([]) + expect(await serviceEvents(svc.id)).toEqual([]) + + const running = await scheduleWindow(admin, { + title: `${marker} running`, + services: [[svc.id, 'under_maintenance']], + }) + await postUpdate(admin, running.id, 'in_progress', 'go') + expect(await statusOf(svc.id)).toBe('under_maintenance') + await must(admin, 'deleteStatusIncidentFn', { id: running.id }) + expect(await statusOf(svc.id)).toBe('operational') + expect(await maintenanceJobs(running.id)).toEqual([]) + const snap = await publicSnapshot(admin) + const ids = [ + ...snap.activeIncidents, + ...snap.upcomingMaintenance, + ...snap.recentIncidents.flatMap((d) => d.incidents), + ].map((i) => i.id) + expect(ids).not.toContain(scheduled.id) + expect(ids).not.toContain(running.id) + expect((await incidentRow(running.id)).deleted_at).not.toBeNull() +}) + +test( + 'STA-A-46 a maintenance window with no dates is refused', + { + tag: ['@nightly', '@quarantine'], + annotation: { + type: 'quarantine', + description: + 'createIncident (status.service.ts:183) accepts kind maintenance without scheduledStartAt/scheduledEndAt; the window never starts and its email reads "to"; see QUARANTINE.md', + }, + }, + async ({ marker }) => { + const svc = await createService(admin, `${marker} NoDates`) + const res = await tryCreateIncident(admin, { + kind: 'maintenance', + title: marker, + services: [[svc.id, 'under_maintenance']], + }) + // A window accepted with no dates would sit in Upcoming forever: remove it. + if (res.ok) await must(admin, 'deleteStatusIncidentFn', { id: res.data.id }) + expect(res.ok).toBe(false) + } +) diff --git a/apps/web/e2e/tests/status/admin-overview.serial.spec.ts b/apps/web/e2e/tests/status/admin-overview.serial.spec.ts new file mode 100644 index 0000000000..1be1410560 --- /dev/null +++ b/apps/web/e2e/tests/status/admin-overview.serial.spec.ts @@ -0,0 +1,62 @@ +/** + * Status admin overview against the whole workspace's service state: runs in + * the product-serial project, with no other test running. + * + * Journeys: STA-A-57. + * + * Every live service is set operational for the test (other specs leave theirs + * in all sorts of states) and put back afterwards. + */ +import { test, expect } from '../../support/fixtures' +import { personApi } from '../../support/server-fns' +import { supportDb } from '../../support/db' +import { supportEnv } from '../../support/env' +import { createSegment, createService, must, publicSnapshot } from '../../product/status' + +test( + 'STA-A-57 the overview banner shows what visitors see, not gated services', + { + tag: ['@nightly', '@quarantine'], + annotation: { + type: 'quarantine', + description: + 'getStatusOverviewAdminFn derives topLevelStatus from every service, segment-gated and plan-hidden ones included (functions/status.ts:624), so "Visitors currently see" can say a status no visitor sees; see QUARANTINE.md', + }, + }, + async ({ playwright, marker }) => { + const sql = supportDb() + const saved = await sql<{ id: string; status: string }[]>` + SELECT id, status FROM status_components WHERE deleted_at IS NULL` + const admin = await personApi(playwright, 'admin') + const anon = await playwright.request.newContext({ baseURL: supportEnv().baseUrl }) + try { + await sql`UPDATE status_components SET status = 'operational' WHERE deleted_at IS NULL` + const segment = await createSegment(marker) + const gated = await createService(admin, `${marker} Gated`, { + segmentIds: [segment.id], + status: 'major_outage', + }) + expect(gated.status).toBe('major_outage') + + const visitors = await publicSnapshot(anon) + expect((visitors as unknown as { topLevel: { status: string } }).topLevel.status).toBe( + 'operational' + ) + const overview = await must<{ topLevelStatus: string }>( + admin, + 'getStatusOverviewAdminFn', + undefined, + { method: 'GET' } + ) + expect(overview.topLevelStatus).toBe('operational') + } finally { + for (const row of saved) { + await sql`UPDATE status_components SET status = ${row.status} WHERE id = ${row.id}` + } + await sql`UPDATE status_components SET deleted_at = now() + WHERE name = ${`${marker} Gated`}` + await admin.dispose() + await anon.dispose() + } + } +) diff --git a/apps/web/e2e/tests/status/admin-overview.spec.ts b/apps/web/e2e/tests/status/admin-overview.spec.ts new file mode 100644 index 0000000000..8ef18d8f6b --- /dev/null +++ b/apps/web/e2e/tests/status/admin-overview.spec.ts @@ -0,0 +1,101 @@ +/** + * Status admin: the Overview. + * + * Journeys: STA-A-56. + * + * The banner and tiles are workspace-wide, so this asserts only that its own + * incident and windows show in the right cards, and tile deltas. + */ +import { test, expect } from '../../support/fixtures' +import type { APIRequestContext } from '@playwright/test' +import { callServerFn, personApi } from '../../support/server-fns' +import { + createIncident, + createService, + must, + openAdmin, + postUpdate, + scheduleWindow, +} from '../../product/status' + +test.describe.configure({ timeout: 60_000 }) + +interface Overview { + activeIncidents: { id: string }[] + liveMaintenance: { id: string }[] + upcomingMaintenance: { id: string }[] + incidentsLast30d: number + [k: string]: unknown +} + +/** + * The overview, read again if it failed: listStatusIncidents loads each listed + * incident by id and throws when another test deletes one in between (a race + * reported with this suite, status.service.ts:936). + */ +async function overview(api: APIRequestContext): Promise { + let result: Overview | null = null + await expect(async () => { + const res = await callServerFn(api, 'getStatusOverviewAdminFn', undefined, { + method: 'GET', + }) + expect(res.ok, res.raw.slice(0, 300)).toBe(true) + result = res.data + }).toPass({ timeout: 10_000 }) + return result! +} + +test( + 'STA-A-56 the overview reflects the live state', + { tag: '@nightly' }, + async ({ asPerson, playwright, marker }) => { + const admin = await personApi(playwright, 'admin') + try { + const before = await overview(admin) + const svc = await createService(admin, `${marker} Overview`) + const inc = await createIncident(admin, { + title: `${marker} open one`, + services: [[svc.id, 'partial_outage']], + }) + const running = await scheduleWindow(admin, { + title: `${marker} running`, + services: [[svc.id, 'under_maintenance']], + }) + await postUpdate(admin, running.id, 'in_progress', 'started') + const upcoming = await scheduleWindow(admin, { + title: `${marker} upcoming`, + services: [[svc.id, 'under_maintenance']], + startMin: 2, + endMin: 30, + }) + + const after = await overview(admin) + expect(after.incidentsLast30d).toBeGreaterThanOrEqual(before.incidentsLast30d + 1) + expect(after.activeIncidents.map((i) => i.id)).toContain(inc.id) + expect(after.liveMaintenance.map((i) => i.id)).toContain(running.id) + expect(after.upcomingMaintenance.map((i) => i.id)).toContain(upcoming.id) + + const ui = await asPerson('contributor') + await openAdmin(ui, '/admin/status?view=overview') + await expect(ui.getByText('Visitors currently see')).toBeVisible() + for (const [card, title] of [ + ['Open incidents', `${marker} open one`], + ['Maintenance in progress', `${marker} running`], + ['Upcoming maintenance', `${marker} upcoming`], + ] as const) { + await expect(ui.getByRole('heading', { name: card }), card).toBeVisible() + await expect(ui.getByText(title).first(), title).toBeVisible() + } + const upcomingRow = ui + .locator('div', { has: ui.getByRole('button', { name: `${marker} upcoming` }) }) + .filter({ has: ui.getByRole('button', { name: 'Start now' }) }) + .last() + await expect(upcomingRow.getByRole('button', { name: 'Start now' })).toBeVisible() + await expect(ui.getByText(/Incidents, 30 days/)).toBeVisible() + // Clean up: the upcoming window would otherwise start in two minutes. + await must(admin, 'deleteStatusIncidentFn', { id: upcoming.id }) + } finally { + 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 new file mode 100644 index 0000000000..3d8f3f7036 --- /dev/null +++ b/apps/web/e2e/tests/status/admin-services.spec.ts @@ -0,0 +1,292 @@ +/** + * Status admin: services and groups. + * + * Journeys: STA-A-06, STA-A-07, STA-A-08, STA-A-09, STA-A-11, STA-A-12, + * STA-A-13, STA-A-14. + * + * Service lists are workspace-wide: each test finds its own services and + * groups by marker and compares only their relative order. + */ +import type { APIRequestContext } from '@playwright/test' +import { toUuid } from '@quackback/ids' +import { test, expect } from '../../support/fixtures' +import { callServerFn, personApi } from '../../support/server-fns' +import { supportDb, pollFor } from '../../support/db' +import { + createIncident, + createService, + domainEvents, + getIncidentAdmin, + messageOf, + must, + openAdmin, + postUpdate, + publicServiceIds, + publicSnapshot, + searchList, + serviceEvents, + serviceRow, + setServiceStatus, +} from '../../product/status' + +test.describe.configure({ timeout: 60_000 }) + +let admin: APIRequestContext + +test.beforeEach(async ({ playwright }) => { + admin = await personApi(playwright, 'admin') +}) +test.afterEach(async () => { + await admin.dispose() +}) + +async function serviceByName(name: string) { + return pollFor(`service ${name}`, async () => { + const [row] = await supportDb()< + { + id: string + status: string + position: number + show_uptime: boolean + description: string | null + group_id: string | null + deleted_at: Date | null + }[] + >` + SELECT id, status, position, show_uptime, description, group_id, deleted_at + FROM status_components WHERE name = ${name}` + return row ?? null + }) +} + +test( + 'STA-A-06 create a service from the Services view', + { tag: '@core' }, + async ({ asPerson, marker }) => { + const before = await createService(admin, `${marker} Before`) + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/status?view=components') + await ui.getByRole('button', { name: 'New service' }).click() + const dialog = ui.getByRole('dialog', { name: 'New service' }) + await dialog.getByLabel('Name').fill(`${marker} API`) + await dialog.getByLabel('Description').fill('The public API') + await dialog.getByRole('button', { name: 'Create service' }).click() + await expect(dialog).toBeHidden() + + const row = await serviceByName(`${marker} API`) + expect(row).toMatchObject({ + status: 'operational', + description: 'The public API', + group_id: null, + show_uptime: true, + }) + expect(row.position).toBeGreaterThan((await serviceRow(before.id)).position) + expect( + await supportDb()`SELECT 1 FROM status_component_events WHERE component_id = ${row.id}` + ).toHaveLength(0) + await searchList( + ui, + `${marker} API`, + ui.getByText(`${marker} API`, { exact: true }), + 'Search services…' + ) + const snap = await publicSnapshot(admin) + expect(snap.ungroupedComponents.map((c) => c.name)).toContain(`${marker} API`) + } +) + +test( + 'STA-A-07 service and group name refusals write nothing', + { tag: '@nightly' }, + async ({ marker }) => { + for (const name of ['', 'x'.repeat(201)]) { + expect((await callServerFn(admin, 'createStatusComponentFn', { name })).ok).toBe(false) + expect((await callServerFn(admin, 'createStatusGroupFn', { name })).ok).toBe(false) + } + const blankSvc = await callServerFn(admin, 'createStatusComponentFn', { name: ' ' }) + expect(messageOf(blankSvc)).toBe('Component name is required') + const blankGroup = await callServerFn(admin, 'createStatusGroupFn', { name: ' ' }) + expect(messageOf(blankGroup)).toBe('Group name is required') + const svc = await createService(admin, `${marker} Named`) + const rename = await callServerFn(admin, 'updateStatusComponentFn', { id: svc.id, name: ' ' }) + expect(messageOf(rename)).toBe('Component name is required') + expect((await serviceRow(svc.id)).name).toBe(`${marker} Named`) + const blanks = await supportDb()`SELECT 1 FROM status_components WHERE btrim(name) = ''` + expect(blanks).toHaveLength(0) + } +) + +test('STA-A-08 edit a service', { tag: '@nightly' }, async ({ asPerson, marker }) => { + const svc = await createService(admin, `${marker} Old`) + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/status?view=components') + await searchList( + ui, + `${marker} Old`, + ui.getByText(`${marker} Old`, { exact: true }), + 'Search services…' + ) + const row = ui.locator('div.group', { hasText: `${marker} Old` }).last() + await row.hover() + await row.getByTitle('Service actions').click() + await ui.getByRole('menuitem', { name: 'Edit service' }).click() + const dialog = ui.getByRole('dialog', { name: 'Edit service' }) + await dialog.getByLabel('Name').fill(`${marker} New`) + await dialog.getByLabel('Description').fill('Renamed') + await dialog.getByRole('switch').click() + await dialog.getByRole('button', { name: 'Save changes' }).click() + await expect(dialog).toBeHidden() + + await pollFor('the edit', async () => + (await serviceRow(svc.id)).name === `${marker} New` ? true : null + ) + expect(await serviceRow(svc.id)).toMatchObject({ description: 'Renamed', show_uptime: false }) + expect(await serviceEvents(svc.id)).toEqual([]) + const snap = await publicSnapshot(admin) + const pub = snap.ungroupedComponents.find((c) => c.id === svc.id) + expect(pub).toMatchObject({ name: `${marker} New`, showUptime: false }) +}) + +test('STA-A-09 set a service status by hand', { tag: '@core' }, async ({ marker }) => { + const svc = await createService(admin, `${marker} Manual`) + await setServiceStatus(admin, svc.id, 'partial_outage') + expect((await serviceRow(svc.id)).status).toBe('partial_outage') + expect(await serviceEvents(svc.id)).toEqual([ + expect.objectContaining({ status: 'partial_outage', source: 'manual', incident_id: null }), + ]) + const events = await domainEvents(svc.id) + expect(events.map((e) => e.type)).toEqual(['status.component_changed']) + expect(events[0].actor_type).toBe('service') + expect(events[0].payload).toMatchObject({ + componentId: svc.id, + componentName: `${marker} Manual`, + previousStatus: 'operational', + status: 'partial_outage', + source: 'manual', + }) + // The same status again is a no-op. + await setServiceStatus(admin, svc.id, 'partial_outage') + expect(await serviceEvents(svc.id)).toHaveLength(1) + expect(await domainEvents(svc.id)).toHaveLength(1) +}) + +test( + 'STA-A-11 delete a service while an incident uses it', + { tag: '@core' }, + async ({ marker }) => { + const a = await createService(admin, `${marker} A`) + const b = await createService(admin, `${marker} B`) + const inc = await createIncident(admin, { + title: marker, + services: [ + [a.id, 'major_outage'], + [b.id, 'major_outage'], + ], + }) + await must(admin, 'deleteStatusComponentFn', { id: a.id }) + expect((await serviceRow(a.id)).deleted_at).not.toBeNull() + + const list = await must<{ + ungrouped: { id: string }[] + groups: { components: { id: string }[] }[] + }>(admin, 'listStatusComponentsAdminFn', undefined, { method: 'GET' }) + const listed = [...list.ungrouped, ...list.groups.flatMap((g) => g.components)].map((c) => c.id) + expect(listed).not.toContain(a.id) + expect(listed).toContain(b.id) + expect(publicServiceIds(await publicSnapshot(admin))).not.toContain(a.id) + const editor = await getIncidentAdmin(admin, inc.id) + expect(editor!.affectedComponents.map((c: { componentId: string }) => c.componentId)).toEqual([ + b.id, + ]) + + await postUpdate(admin, inc.id, 'resolved', 'fixed') + expect((await serviceRow(b.id)).status).toBe('operational') + const again = await callServerFn(admin, 'deleteStatusComponentFn', { id: a.id }) + expect(again.code).toBe('STATUS_COMPONENT_NOT_FOUND') + } +) + +test('STA-A-12 reorder services and groups', { tag: '@nightly' }, async ({ marker }) => { + const one = await createService(admin, `${marker} One`) + const two = await createService(admin, `${marker} Two`) + await must(admin, 'reorderStatusComponentsFn', { ids: [two.id, one.id] }) + expect((await serviceRow(two.id)).position).toBeLessThan((await serviceRow(one.id)).position) + const order = (await publicSnapshot(admin)).ungroupedComponents.map((c) => c.id) + expect(order.indexOf(two.id)).toBeLessThan(order.indexOf(one.id)) + + const g1 = await must<{ id: string }>(admin, 'createStatusGroupFn', { name: `${marker} G1` }) + const g2 = await must<{ id: string }>(admin, 'createStatusGroupFn', { name: `${marker} G2` }) + await must(admin, 'reorderStatusGroupsFn', { ids: [g2.id, g1.id] }) + const [p1] = + await supportDb()`SELECT position FROM status_component_groups WHERE id = ${toUuid(g1.id as never)}` + const [p2] = + await supportDb()`SELECT position FROM status_component_groups WHERE id = ${toUuid(g2.id as never)}` + expect(Number(p2.position)).toBeLessThan(Number(p1.position)) + + expect((await callServerFn(admin, 'reorderStatusComponentsFn', { ids: [] })).ok).toBe(false) + expect((await callServerFn(admin, 'reorderStatusGroupsFn', { ids: [] })).ok).toBe(false) +}) + +test( + 'STA-A-13 groups: create, rename, collapse', + { tag: '@nightly' }, + async ({ asPerson, marker }) => { + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/status?view=components') + await ui.getByRole('button', { name: 'New group' }).click() + const dialog = ui.getByRole('dialog', { name: 'New group' }) + await dialog.getByLabel('Name').fill(`${marker} Infra`) + await dialog.getByRole('button', { name: 'Create group' }).click() + await expect(dialog).toBeHidden() + const [group] = await pollFor('the group', async () => { + const rows = await supportDb()< + { id: string }[] + >`SELECT id FROM status_component_groups WHERE name = ${`${marker} Infra`}` + return rows.length ? rows : null + }) + const groupId = ( + await must<{ groups: { id: string; name: string }[] }>( + admin, + 'listStatusComponentsAdminFn', + undefined, + { method: 'GET' } + ) + ).groups.find((g) => g.name === `${marker} Infra`)!.id + expect(toUuid(groupId as never)).toBe(group.id) + + // Empty: not rendered publicly. + expect((await publicSnapshot(admin)).groups.map((g) => g.id)).not.toContain(groupId) + await must(admin, 'updateStatusGroupFn', { + id: groupId, + name: `${marker} Platform`, + collapsed: true, + }) + const svc = await createService(admin, `${marker} In group`, { groupId }) + const pub = (await publicSnapshot(admin)).groups.find((g) => g.id === groupId) + expect(pub).toMatchObject({ name: `${marker} Platform`, collapsed: true }) + expect(pub!.components.map((c) => c.id)).toEqual([svc.id]) + + const missing = await callServerFn(admin, 'updateStatusGroupFn', { + id: 'status_group_01m7v6aewge02vtv3kd36ggmcw', + name: 'x', + }) + expect(missing.code).toBe('STATUS_GROUP_NOT_FOUND') + } +) + +test( + 'STA-A-14 deleting a group leaves its services ungrouped', + { tag: '@nightly' }, + async ({ marker }) => { + const g = await must<{ id: string }>(admin, 'createStatusGroupFn', { name: `${marker} Doomed` }) + const svc = await createService(admin, `${marker} Orphan`, { groupId: g.id }) + await must(admin, 'deleteStatusGroupFn', { id: g.id }) + expect( + await supportDb()`SELECT 1 FROM status_component_groups WHERE id = ${toUuid(g.id as never)}` + ).toHaveLength(0) + expect((await serviceRow(svc.id)).group_id).toBeNull() + expect((await publicSnapshot(admin)).ungroupedComponents.map((c) => c.id)).toContain(svc.id) + const again = await callServerFn(admin, 'deleteStatusGroupFn', { id: g.id }) + expect(again.code).toBe('STATUS_GROUP_NOT_FOUND') + } +) diff --git a/apps/web/e2e/tests/status/admin-subscribers.spec.ts b/apps/web/e2e/tests/status/admin-subscribers.spec.ts new file mode 100644 index 0000000000..ccebbd5700 --- /dev/null +++ b/apps/web/e2e/tests/status/admin-subscribers.spec.ts @@ -0,0 +1,269 @@ +/** + * Status admin: the Subscribers view (list, add, import, remove, export). + * + * Journeys: STA-A-50, STA-A-51, STA-A-52, STA-A-53, STA-A-54, STA-A-55. + * + * Subscribers are page-wide, so each one would receive every other test's + * publish email: every subscriber made here is removed again at the end. + * People are minted per test (one subscription row per person). + */ +import { writeFileSync } from 'node:fs' +import { readFile } from 'node:fs/promises' +import type { APIRequestContext } from '@playwright/test' +import { test, expect } from '../../support/fixtures' +import { callServerFn, personApi } from '../../support/server-fns' +import { mintCustomer, type MintedCustomer } from '../../support/widget-portal' +import { pollFor } from '../../support/db' +import { must, openAdmin, searchList, subscriptionRow } from '../../product/status' + +test.describe.configure({ timeout: 60_000 }) + +let admin: APIRequestContext +let made: MintedCustomer[] = [] + +test.beforeEach(async ({ playwright }) => { + admin = await personApi(playwright, 'admin') + made = [] +}) +test.afterEach(async () => { + for (const p of made) { + await callServerFn(admin, 'removeStatusSubscriberFn', { principalId: p.principalId }) + } + await admin.dispose() +}) + +async function person(marker: string, label: string): Promise { + const p = await mintCustomer({ + email: `${marker}-${label}@customers.example`, + name: `${label} ${marker.slice(-8)}`, + }) + made.push(p) + return p +} + +interface SubListItem { + principalId: string + email: string | null + source: string + scope: string + unsubscribedAt: string | null +} + +async function listAll(search: string, limit = 30): Promise { + const out: SubListItem[] = [] + let cursor: string | undefined + for (let i = 0; i < 20; i++) { + const page = await must<{ items: SubListItem[]; nextCursor: string | null; hasMore: boolean }>( + admin, + 'listStatusSubscriptionsAdminFn', + { search, limit, ...(cursor ? { cursor } : {}) }, + { method: 'GET' } + ) + out.push(...page.items) + if (!page.hasMore || !page.nextCursor) break + cursor = page.nextCursor + } + return out +} + +test( + 'STA-A-50 the Subscribers view lists and searches subscribers', + { tag: '@nightly' }, + async ({ asPerson, marker }) => { + const ann = await person(marker, 'ann') + const bob = await person(marker, 'bob') + await must(admin, 'addStatusSubscriberFn', { email: ann.email }) + await must(admin, 'importStatusSubscribersFn', { emails: [bob.email] }) + await must(admin, 'removeStatusSubscriberFn', { principalId: bob.principalId }) + + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/status?view=subscribers') + for (const label of ['Total', 'Active', 'Unsubscribed']) { + await expect(ui.getByText(label, { exact: true }).first()).toBeVisible() + } + const annRow = ui.locator('div.group', { hasText: ann.email }) + await searchList(ui, marker, annRow, 'Search subscribers…') + await expect(annRow.getByText('Added by admin')).toBeVisible() + const bobRow = ui.locator('div.group', { hasText: bob.email }) + await expect(bobRow.getByText('CSV import')).toBeVisible() + await expect(bobRow.getByText(/Unsubscribed/)).toBeVisible() + // Newest first. + await expect(ui.locator('div.group', { hasText: marker })).toHaveCount(2) + await expect(ui.locator('div.group', { hasText: marker }).first()).toContainText(bob.email) + // Display-name search. + await searchList(ui, `ann ${marker.slice(-8)}`, annRow, 'Search subscribers…') + await expect(bobRow).toHaveCount(0) + await searchList( + ui, + `${marker}-nobody`, + ui.getByText('No subscribers match your search'), + 'Search subscribers…' + ) + } +) + +test('STA-A-51 add a subscriber by email', { tag: '@core' }, async ({ marker }) => { + const p = await person(marker, 'Add') + const res = await must<{ subscribed: boolean }>(admin, 'addStatusSubscriberFn', { + email: p.email.toUpperCase(), + }) + expect(res.subscribed).toBe(true) + expect(await subscriptionRow(p.principalUuid)).toMatchObject({ + scope: 'page', + component_ids: [], + source: 'admin', + unsubscribed_at: null, + }) + + const unknown = await callServerFn(admin, 'addStatusSubscriberFn', { + email: `${marker}-nobody@customers.example`, + }) + expect(unknown.code).toBe('STATUS_SUBSCRIBER_NOT_FOUND') + + // Someone who unsubscribed stays unsubscribed. + await must(admin, 'removeStatusSubscriberFn', { principalId: p.principalId }) + const optedOut = await must<{ subscribed: boolean }>(admin, 'addStatusSubscriberFn', { + email: p.email, + }) + expect(optedOut.subscribed).toBe(false) + expect((await subscriptionRow(p.principalUuid))?.unsubscribed_at).not.toBeNull() +}) + +test( + 'STA-A-52 import subscribers from a CSV file', + { tag: '@core' }, + async ({ asPerson, marker }, testInfo) => { + const a = await person(marker, 'csva') + const b = await person(marker, 'csvb') + const out = await person(marker, 'csvout') + await must(admin, 'addStatusSubscriberFn', { email: out.email }) + await must(admin, 'removeStatusSubscriberFn', { principalId: out.principalId }) + const file = testInfo.outputPath('subscribers.csv') + writeFileSync( + file, + [ + 'Name,Email', + `A,${a.email}`, + `A again,${a.email.toUpperCase()}`, + `B,${b.email}`, + `X,${marker}-nobody@customers.example`, + `O,${out.email}`, + ].join('\n') + ) + + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/status?view=subscribers') + await ui.getByRole('button', { name: 'New subscriber' }).click() + const dialog = ui.getByRole('dialog', { name: 'New subscriber' }) + await dialog.getByRole('tab', { name: 'CSV import' }).click() + await dialog.locator('input[type="file"]').setInputFiles(file) + await expect(dialog.getByText('(5 emails found)')).toBeVisible() + const submit = dialog.getByRole('button', { name: 'Subscribe 5 emails' }) + await expect(submit).toBeDisabled() + await dialog.getByRole('checkbox').click() + await submit.click() + await expect( + ui.getByText('Imported 2. Skipped 1 without a matching account. Skipped 1 who unsubscribed.') + ).toBeVisible() + for (const p of [a, b]) { + expect(await subscriptionRow(p.principalUuid)).toMatchObject({ + scope: 'page', + source: 'csv_import', + unsubscribed_at: null, + }) + } + expect((await subscriptionRow(out.principalUuid))?.unsubscribed_at).not.toBeNull() + + // A file with no emails. + const empty = testInfo.outputPath('empty.csv') + writeFileSync(empty, 'Email\n') + // The dialog closed after the import; open it again. + await ui.getByRole('button', { name: 'New subscriber' }).click() + await dialog.getByRole('tab', { name: 'CSV import' }).click() + await dialog.locator('input[type="file"]').setInputFiles(empty) + await expect( + dialog.getByText('No emails found. Make sure the file has an "Email" column.') + ).toBeVisible() + // More than 5000 is refused. + const tooMany = await callServerFn(admin, 'importStatusSubscribersFn', { + emails: Array.from({ length: 5001 }, (_, i) => `${marker}-${i}@customers.example`), + }) + expect(tooMany.ok).toBe(false) + } +) + +test( + 'STA-A-53 every row of a large import is listed across pages', + { + tag: ['@nightly', '@quarantine'], + annotation: { + type: 'quarantine', + description: + 'listStatusSubscriptions pages on created_at < cursor with no id tiebreak (status.subscription.ts:210, :236); one import shares one created_at, so page 2 drops the rest; see QUARANTINE.md', + }, + }, + async ({ marker }) => { + const people: MintedCustomer[] = [] + for (let i = 0; i < 35; i++) people.push(await person(marker, `bulk${i}`)) + const res = await must<{ imported: number }>(admin, 'importStatusSubscribersFn', { + emails: people.map((p) => p.email), + }) + expect(res.imported).toBe(35) + const listed = await listAll(marker) + const exported = await must<{ email: string | null }[]>( + admin, + 'exportStatusSubscribersAdminFn', + undefined, + { method: 'GET' } + ) + expect(exported.filter((r) => r.email?.startsWith(marker))).toHaveLength(35) + expect(new Set(listed.map((r) => r.principalId)).size).toBe(35) + } +) + +test('STA-A-54 remove a subscriber', { tag: '@nightly' }, async ({ asPerson, marker }) => { + const p = await person(marker, 'rm') + await must(admin, 'addStatusSubscriberFn', { email: p.email }) + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/status?view=subscribers') + const row = ui.locator('div.group', { hasText: p.email }) + await searchList(ui, marker, row, 'Search subscribers…') + await row.hover() + await row.getByRole('button', { name: `Remove ${p.name}` }).click() + const confirm = ui.getByRole('alertdialog').or(ui.getByRole('dialog')) + await expect(confirm.getByText('Remove subscriber?')).toBeVisible() + await confirm.getByRole('button', { name: 'Remove' }).click() + await expect(ui.getByText(`Removed ${p.name}.`)).toBeVisible() + const first = await pollFor( + 'the opt-out', + async () => (await subscriptionRow(p.principalUuid))?.unsubscribed_at ?? null + ) + + const again = await must<{ removed: boolean }>(admin, 'removeStatusSubscriberFn', { + principalId: p.principalId, + }) + expect(again.removed).toBe(false) + expect((await subscriptionRow(p.principalUuid))?.unsubscribed_at).toEqual(first) +}) + +test('STA-A-55 export subscribers to CSV', { tag: '@nightly' }, async ({ asPerson, marker }) => { + const live = await person(marker, 'exlive') + const gone = await person(marker, 'exgone') + await must(admin, 'addStatusSubscriberFn', { email: live.email }) + await must(admin, 'addStatusSubscriberFn', { email: gone.email }) + await must(admin, 'removeStatusSubscriberFn', { principalId: gone.principalId }) + + const ui = await asPerson('admin') + await openAdmin(ui, '/admin/status?view=subscribers') + const download = ui.waitForEvent('download') + await ui.getByRole('button', { name: 'Export CSV' }).click() + const file = await (await download).path() + const csv = await readFile(file, 'utf8') + const lines = csv.split(/\r?\n/) + const liveLine = lines.find((l) => l.includes(live.email)) + const goneLine = lines.find((l) => l.includes(gone.email)) + expect(liveLine).toBeTruthy() + expect(goneLine).toBeTruthy() + // Newest first: the later subscription comes first. + expect(lines.indexOf(goneLine!)).toBeLessThan(lines.indexOf(liveLine!)) +}) diff --git a/apps/web/e2e/tests/status/admin-templates.spec.ts b/apps/web/e2e/tests/status/admin-templates.spec.ts new file mode 100644 index 0000000000..f4debeeef5 --- /dev/null +++ b/apps/web/e2e/tests/status/admin-templates.spec.ts @@ -0,0 +1,198 @@ +/** + * Status admin: incident templates. + * + * Journeys: STA-A-47, STA-A-48, STA-A-49. + * + * The template list and the composers' template pickers are workspace-wide; + * each test picks its own template by marker. + */ +import type { APIRequestContext } from '@playwright/test' +import { toUuid } from '@quackback/ids' +import { test, expect } from '../../support/fixtures' +import { callServerFn, personApi } from '../../support/server-fns' +import { pollFor, supportDb } from '../../support/db' +import { + createIncident, + createService, + incidentRow, + incidentUpdates, + messageOf, + must, + openAdmin, + searchList, +} from '../../product/status' + +test.describe.configure({ timeout: 60_000 }) + +let admin: APIRequestContext + +test.beforeEach(async ({ playwright }) => { + admin = await personApi(playwright, 'admin') +}) +test.afterEach(async () => { + await admin.dispose() +}) + +interface Template { + id: string + name: string + title: string + body: string + impact: string + componentIds: string[] + usageCount: number +} + +async function templateNamed(name: string): Promise