Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/deploy-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ jobs:
# init (fail-open), so PR forks / unconfigured envs still build cleanly.
VITE_NEWRELIC_LICENSE_KEY: ${{ secrets.VITE_NEWRELIC_LICENSE_KEY }}
VITE_NEWRELIC_APP_ID: ${{ secrets.VITE_NEWRELIC_APP_ID }}
# ──────────────────────────────────────────────────────────────
# SCHEDULED-MAINTENANCE TOGGLE (src/components/MaintenanceNotice.tsx).
#
# '1' → the published Pages build renders the customer-facing
# maintenance banner (every route) + a one-time dismissible
# modal on /app* + /login*. This is ON because the prod
# cluster (api.instanode.dev) is intentionally paused, so the
# SPA loads but every API call fails — the banner explains the
# downtime is scheduled and the data is safe.
#
# Gated to PUBLISH events only (push to main / manual dispatch) so a
# PR's `build` check produces a byte-identical, banner-OFF artifact —
# it never publishes (the deploy job below is PR-gated) and runs no
# tests, so this has zero effect on the required CI checks. The
# separate build-and-test / playwright / coverage / lighthouse jobs
# never set this var, so they build with the notice OFF.
#
# ▶ TO TURN THE BANNER OFF ON RESUME: set this to '0' (or delete the
# line) and re-run this deploy — or revert the PR that added it.
# The component renders null when the flag is unset/'0'.
VITE_MAINTENANCE_MODE: ${{ github.event_name != 'pull_request' && '1' || '0' }}
# NOTE: do NOT `cp dist/index.html dist/404.html` here. prerender.mjs
# already writes dist/404.html as the bare SPA shell so every /app/*
# bookmark / shared link / magic-link callback rehydrates the React
Expand Down
13 changes: 12 additions & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ export default defineConfig({
// self-skip anyway, but keeping them out of the default config means the
// per-PR gate never boots a browser for a spec that always skips here, and
// the two suites stay cleanly separated.
testIgnore: ['live-*.spec.ts'],
//
// auth-contract.spec.ts is ALSO excluded here: it makes UNCONDITIONAL real
// fetches to PROD (api.instanode.dev) to assert the AUTH-004 CORS envelope,
// so it does NOT belong in this mocked, same-origin (VITE_NO_PROXY=1) gate —
// it has its own dedicated config (playwright.auth-contract.config.ts) and
// workflow (auth-contract-e2e.yml) that run it against prod. Leaving it in
// the default glob made the required `playwright` job depend on prod being
// reachable: when the prod cluster is down/paused the fetch times out and
// the mocked gate fails for a reason that has nothing to do with the PR.
// The prod-targeting assertion still runs (and is allowed to go red while
// prod is down) in its own non-required workflow.
testIgnore: ['live-*.spec.ts', 'auth-contract.spec.ts'],
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
Expand Down
14 changes: 14 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import { BrowserRouter, Navigate, Route, Routes, useLocation, useParams } from '
// sibling of <AppRoutes>. Renders null — no markup contribution.
import { RouteTracker } from './components/RouteTracker'

// MaintenanceNotice — customer-facing scheduled-maintenance notice. Sits
// inside <BrowserRouter> (it reads window.location to decide whether the
// dismissible modal applies on /app* + /login*) and OUTSIDE <AppRoutes> so
// the sticky banner persists across every route change. The whole thing is
// gated behind the build-time VITE_MAINTENANCE_MODE flag: when unset/'0' it
// renders null and contributes zero markup (the default in CI + locally). It
// is only '1' on the published GitHub Pages build (deploy-pages.yml).
import { MaintenanceNotice } from './components/MaintenanceNotice'

// Homepage — eagerly imported. It's the cold-load path and the most-visited
// public surface, so it stays in the main entry chunk.
import { MarketingPage } from './pages/MarketingPage'
Expand Down Expand Up @@ -430,6 +439,11 @@ export function App() {
during a lazy-chunk fetch (an unmount would skip the
setPageViewName for that navigation). */}
<RouteTracker />
{/* MaintenanceNotice renders the sticky banner on every route (and a
one-time modal on /app* + /login*) when VITE_MAINTENANCE_MODE='1';
otherwise it returns null and adds no markup. Mounted above
AppRoutes so the banner sits at the top of the document flow. */}
<MaintenanceNotice />
<AppRoutes />
</BrowserRouter>
)
Expand Down
216 changes: 216 additions & 0 deletions src/components/MaintenanceNotice.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/* MaintenanceNotice.test.tsx — full coverage for the scheduled-maintenance
* notice. The component is flag-gated (VITE_MAINTENANCE_MODE), so the tests
* drive the ON branch via the `enabled` prop and the per-route modal logic
* via the `pathname` prop — no need to stub import.meta.env or mount a
* router. The env-read helper (isMaintenanceEnabled) is covered separately.
*
* Coverage targets (every line of the new component):
* - enabled=false → renders nothing (the default everywhere except Pages)
* - enabled=true → sticky banner with the headline + body copy
* - modal shows on /app + /app/* + /login* and NOT on marketing routes
* - dismiss via button, overlay click, and Escape key — all close it and
* persist the dismissal in sessionStorage
* - a persisted dismissal keeps the modal closed on remount
* - isMaintenanceEnabled() reads the env flag
* - modalAppliesTo() route predicate
* - sessionStorage-throws fail-open paths
*/

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { render, screen, fireEvent, cleanup } from '@testing-library/react'
import {
MaintenanceNotice,
isMaintenanceEnabled,
modalAppliesTo,
MAINTENANCE_HEADLINE,
MAINTENANCE_BODY,
} from './MaintenanceNotice'

beforeEach(() => {
window.sessionStorage.clear()
})

afterEach(() => {
cleanup()
vi.restoreAllMocks()
})

describe('MaintenanceNotice — flag gating', () => {
it('renders nothing when disabled', () => {
const { container } = render(<MaintenanceNotice enabled={false} pathname="/app" />)
expect(container.firstChild).toBeNull()
expect(screen.queryByTestId('maintenance-banner')).toBeNull()
expect(screen.queryByTestId('maintenance-modal')).toBeNull()
})

it('renders nothing when called with no props and the env flag is off (default test env)', () => {
// In the unit-test env VITE_MAINTENANCE_MODE is unset, so the default
// (enabled omitted → isMaintenanceEnabled()) is false. This covers the
// env-read default-argument branch.
const { container } = render(<MaintenanceNotice />)
expect(container.firstChild).toBeNull()
})
})

describe('MaintenanceNotice — sticky banner', () => {
it('shows the banner with headline + body when enabled, on any route', () => {
render(<MaintenanceNotice enabled pathname="/" />)
const banner = screen.getByTestId('maintenance-banner')
expect(banner).toBeTruthy()
expect(banner.getAttribute('role')).toBe('status')
expect(banner.textContent).toContain(MAINTENANCE_HEADLINE)
expect(banner.textContent).toContain('Your data is safe')
})

it('does NOT show the modal on a marketing route', () => {
render(<MaintenanceNotice enabled pathname="/pricing" />)
expect(screen.getByTestId('maintenance-banner')).toBeTruthy()
expect(screen.queryByTestId('maintenance-modal')).toBeNull()
})

it('resolves the current path from window.location when pathname is omitted', () => {
// No `pathname` prop → resolvePathname() reads window.location.pathname.
// jsdom defaults to "/", a marketing path, so the banner shows but the
// modal does not. This covers the resolvePathname window branch.
render(<MaintenanceNotice enabled />)
expect(screen.getByTestId('maintenance-banner')).toBeTruthy()
expect(screen.queryByTestId('maintenance-modal')).toBeNull()
})
})

describe('MaintenanceNotice — modal on /app + /login', () => {
it('shows the modal on /app', () => {
render(<MaintenanceNotice enabled pathname="/app" />)
const modal = screen.getByTestId('maintenance-modal')
expect(modal).toBeTruthy()
expect(modal.getAttribute('aria-modal')).toBe('true')
expect(modal.textContent).toContain(MAINTENANCE_HEADLINE)
})

it('shows the modal on a nested /app/* route', () => {
render(<MaintenanceNotice enabled pathname="/app/resources" />)
expect(screen.getByTestId('maintenance-modal')).toBeTruthy()
})

it('shows the modal on /login', () => {
render(<MaintenanceNotice enabled pathname="/login?next=%2Fapp" />)
expect(screen.getByTestId('maintenance-modal')).toBeTruthy()
})

it('closes the modal and persists the dismissal when the button is clicked', () => {
render(<MaintenanceNotice enabled pathname="/app" />)
expect(screen.getByTestId('maintenance-modal')).toBeTruthy()
fireEvent.click(screen.getByTestId('maintenance-modal-dismiss'))
expect(screen.queryByTestId('maintenance-modal')).toBeNull()
expect(window.sessionStorage.getItem('instanode.maintenanceModalDismissed')).toBe('1')
// Banner persists after dismiss.
expect(screen.getByTestId('maintenance-banner')).toBeTruthy()
})

it('closes the modal on overlay (backdrop) click', () => {
render(<MaintenanceNotice enabled pathname="/app" />)
const overlay = screen.getByTestId('maintenance-modal')
fireEvent.click(overlay)
expect(screen.queryByTestId('maintenance-modal')).toBeNull()
})

it('does NOT close when clicking inside the dialog card (event target is a child)', () => {
render(<MaintenanceNotice enabled pathname="/app" />)
// Clicking the headline inside the card must not bubble-dismiss.
fireEvent.click(screen.getByText(MAINTENANCE_HEADLINE, { selector: 'h2' }))
expect(screen.getByTestId('maintenance-modal')).toBeTruthy()
})

it('closes the modal on Escape and ignores other keys', () => {
render(<MaintenanceNotice enabled pathname="/app" />)
// A non-Escape key is a no-op (covers the `if (e.key === 'Escape')` false branch).
fireEvent.keyDown(document, { key: 'a' })
expect(screen.getByTestId('maintenance-modal')).toBeTruthy()
fireEvent.keyDown(document, { key: 'Escape' })
expect(screen.queryByTestId('maintenance-modal')).toBeNull()
expect(window.sessionStorage.getItem('instanode.maintenanceModalDismissed')).toBe('1')
})

it('keeps the modal closed on a fresh mount once dismissed this session', () => {
window.sessionStorage.setItem('instanode.maintenanceModalDismissed', '1')
render(<MaintenanceNotice enabled pathname="/app" />)
expect(screen.queryByTestId('maintenance-modal')).toBeNull()
// Banner still shows.
expect(screen.getByTestId('maintenance-banner')).toBeTruthy()
})
})

describe('MaintenanceNotice — sessionStorage failure is non-fatal', () => {
// jsdom's sessionStorage is an exotic Storage object whose methods can't be
// replaced via vi.spyOn (the spy is silently ignored). To exercise the
// fail-open catch branches we swap the whole window.sessionStorage for a
// throwing stub via Object.defineProperty, then restore it after.
function withThrowingSessionStorage(stub: Partial<Storage>, fn: () => void) {
const original = Object.getOwnPropertyDescriptor(window, 'sessionStorage')
Object.defineProperty(window, 'sessionStorage', {
value: stub,
configurable: true,
writable: true,
})
try {
fn()
} finally {
if (original) Object.defineProperty(window, 'sessionStorage', original)
}
}

it('shows the modal when sessionStorage.getItem throws (fail-open on read)', () => {
withThrowingSessionStorage(
{
getItem() {
throw new Error('blocked')
},
setItem() {},
},
() => {
render(<MaintenanceNotice enabled pathname="/app" />)
expect(screen.getByTestId('maintenance-modal')).toBeTruthy()
},
)
})

it('still closes the modal when sessionStorage.setItem throws (fail-open on write)', () => {
withThrowingSessionStorage(
{
getItem() {
return null
},
setItem() {
throw new Error('blocked')
},
},
() => {
render(<MaintenanceNotice enabled pathname="/app" />)
fireEvent.click(screen.getByTestId('maintenance-modal-dismiss'))
expect(screen.queryByTestId('maintenance-modal')).toBeNull()
},
)
})
})

describe('MaintenanceNotice — helpers', () => {
it('isMaintenanceEnabled reflects the (off) env flag in the test env', () => {
expect(isMaintenanceEnabled()).toBe(false)
})

it('modalAppliesTo matches only /app* and /login*', () => {
expect(modalAppliesTo('/app')).toBe(true)
expect(modalAppliesTo('/app/billing')).toBe(true)
expect(modalAppliesTo('/login')).toBe(true)
expect(modalAppliesTo('/login/callback')).toBe(true)
expect(modalAppliesTo('/')).toBe(false)
expect(modalAppliesTo('/pricing')).toBe(false)
expect(modalAppliesTo('/docs')).toBe(false)
})

it('exposes stable copy constants used by both surfaces', () => {
expect(MAINTENANCE_HEADLINE).toBe('Scheduled maintenance')
expect(MAINTENANCE_BODY).toContain('temporarily unavailable')
expect(MAINTENANCE_BODY).toContain('back shortly')
})
})
Loading
Loading