Files
supabase/apps/studio/lib/status-page/status-page.schema.test.ts
CharisandClaude Code b2cf3693dd feat(studio): /api/status-page endpoint backed by incident.io Widget API (#50931)
## Summary

* Adds `/api/status-page` (Next route + TanStack wrapper), backed by the
[incident.io](<http://incident.io>) Widget API, annotating each item
with `visible`, `show_banner`, and (for scheduled maintenances)
`banner_lead_days`.
* Deployment-mode visibility is driven by a new
`status_page:visibility_field_ids` custom-content key.
* Widget array parsing is fault-tolerant: a malformed item in one array
is dropped and logged rather than failing the whole response, so one bad
item can't hide a real ongoing incident.
* 429s from [incident.io](<http://incident.io>) are retried with
equal-jitter exponential backoff, respecting `Retry-After`, up to 2
retries.
* Nothing consumes this endpoint yet — it replaces no existing behavior
and changes nothing user-visible. Later PRs (this is PR 1 of a stack)
wire up consumers behind the `incidentIoStatusPage` ConfigCat flag.

Part of
[FE-4057](https://linear.app/supabase/issue/FE-4057/frontend-bannerbot-reconfigured)
— see Linear for full design context.

## Test plan

- [X] `pnpm --filter studio run typecheck`
- [X] `pnpm --filter studio run lint:ratchet`
- [X] `pnpm knip --workspace apps/studio`
- [X] `pnpm test:prettier`
- [X] `pnpm --filter studio exec vitest run status-page` — 44 tests
passing, including a regression test built from a real production
[incident.io](<http://incident.io>) payload that initially failed to
parse, and a compile-time type-safety regression test for the
array-parsing helper

Co-authored-by: Claude Code
[charis@supabase.io](<mailto:charis@supabase.io>)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **New Features**
* Added a status page that displays ongoing incidents and maintenance,
with visibility and banner settings based on linked incident details.
* Status page data is available through a new API endpoint, with caching
for successful responses and degraded results.
* **Bug Fixes**
* Status page data can still display when some linked incident details
are unavailable; affected results are marked as degraded.
* Improved handling of invalid widget entries so they don’t prevent
valid items from being processed.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Code <charis@supabase.io>
2026-09-28 11:36:29 -04:00

94 lines
3.2 KiB
TypeScript

import { describe, expect, expectTypeOf, test, vi } from 'vitest'
import { z } from 'zod'
import {
WidgetIncidentSchema,
WidgetInProgressMaintenanceSchema,
WidgetResponseSchema,
WidgetScheduledMaintenanceSchema,
} from './status-page.schema'
describe('WidgetResponseSchema: tolerant arrays (fail-open)', () => {
test('drops a malformed item from one array without failing the whole parse, leaving the valid item in that array and the other arrays intact', () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
const payload = {
page_title: 'Supabase',
page_url: 'https://statuspage.incident.io/supabase',
ongoing_incidents: [
{
id: 'incident-valid',
name: 'Valid incident',
url: 'https://status.example.com/incidents/incident-valid',
last_update_at: '2026-01-01T00:00:00Z',
status: 'identified',
current_worst_impact: 'partial_outage',
},
// Malformed: missing required `id`/`name`/`url`/`last_update_at`.
{ status: 'identified' },
],
in_progress_maintenances: [
{
id: 'maintenance-valid',
name: 'Valid maintenance',
url: 'https://status.example.com/maintenances/maintenance-valid',
last_update_at: '2026-01-01T00:00:00Z',
status: 'maintenance_in_progress',
started_at: '2026-01-01T00:00:00Z',
},
],
scheduled_maintenances: [],
}
const result = WidgetResponseSchema.safeParse(payload)
expect(result.success).toBe(true)
if (!result.success) return
// The other valid item in the same array survives...
expect(result.data.ongoing_incidents).toHaveLength(1)
expect(result.data.ongoing_incidents[0].id).toBe('incident-valid')
// ...and the other arrays are unaffected by the malformed item elsewhere.
expect(result.data.in_progress_maintenances).toHaveLength(1)
expect(result.data.in_progress_maintenances[0].id).toBe('maintenance-valid')
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining('ongoing_incidents'),
expect.any(Object)
)
warnSpy.mockRestore()
})
test('defaults a missing array key to an empty array', () => {
const result = WidgetResponseSchema.safeParse({
page_title: 'Supabase',
page_url: 'https://statuspage.incident.io/supabase',
})
expect(result.success).toBe(true)
if (!result.success) return
expect(result.data.ongoing_incidents).toEqual([])
expect(result.data.in_progress_maintenances).toEqual([])
expect(result.data.scheduled_maintenances).toEqual([])
})
test('tolerantArray keeps parsed item types concrete, not any', () => {
const result = WidgetResponseSchema.parse({
page_title: 'Supabase',
page_url: 'https://statuspage.incident.io/supabase',
})
expectTypeOf(result.ongoing_incidents).toEqualTypeOf<
Array<z.infer<typeof WidgetIncidentSchema>>
>()
expectTypeOf(result.in_progress_maintenances).toEqualTypeOf<
Array<z.infer<typeof WidgetInProgressMaintenanceSchema>>
>()
expectTypeOf(result.scheduled_maintenances).toEqualTypeOf<
Array<z.infer<typeof WidgetScheduledMaintenanceSchema>>
>()
})
})