// @vitest-environment happy-dom
/* The app shell, rendered once, end to end.
*
* This exists because a missing hook import in App.jsx — `useState` used, never imported — shipped
* to production and the only symptom was a background: React threw while rendering the shell, so
* neither the app nor the launch screen ever mounted, and every test in the suite passed because
* none of them rendered App. A page that only ever needs to render is still a page worth rendering
* in a test: the whole class of "the bundle parses but the tree throws" lives here. */
import React, { act } from 'react'
import { createRoot } from 'react-dom/client'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
// No network in a smoke test: the shell's boot() pulls config and state, and every one of those
// goes through lib/api.js. Everything else in that module stays real (Login reads IS_APPLE, BIO,
// passkeyLogin… from it), which is what `importOriginal` is for — a hand-listed mock would leave
// the screens importing exports that do not exist, and the error boundary would hide it.
vi.mock('./lib/api.js', async importOriginal => ({
...(await importOriginal()),
api: vi.fn(async () => ({}))
}))
vi.mock('./lib/back.js', () => ({ initBackButton: async () => () => {} }))
vi.mock('./lib/push.js', () => ({ syncPushSubscription: async () => {} }))
const { default: App } = await import('./App.jsx')
const { useStore } = await import('./store/useStore.js')
let host, root
beforeEach(() => {
globalThis.IS_REACT_ACT_ENVIRONMENT = true
localStorage.clear()
// The store is a module singleton, so it carries state from one test into the next: `ready` is
// left true by whichever test booted first, and a test about a boot that never finishes has to
// start from a boot that has not finished. `user` goes too, or "signed in" means nothing here.
useStore.setState({ ready: false, user: null })
host = document.createElement('div')
document.body.appendChild(host)
root = createRoot(host)
})
afterEach(() => { act(() => root.unmount()); host.remove() })
describe('the app shell', () => {
it('mounts without throwing, and shows the launch screen first', async () => {
await act(async () => { root.render() })
// The splash: the shell renders it before anything else, which is what a launch screen is.
expect(host.querySelector('.boot-splash')).toBeTruthy()
expect(host.querySelector('img.boot-photo')?.getAttribute('src')).toBe('splash.webp')
// Only the photograph: the mark that used to sit on it is gone by request.
expect(host.querySelector('.boot-splash').querySelectorAll('img').length).toBe(1)
})
it('stays long enough to be seen, then gets out of the way', async () => {
await act(async () => { root.render() })
// Past a warm boot, which finishes in milliseconds — and past the 500ms a flicker would have
// ended at. The floor is the whole point: this screen has to be readable, not a flash.
await act(async () => { await new Promise(r => setTimeout(r, 600)) })
expect(host.querySelector('.boot-splash')).toBeTruthy()
// Once the floor is up and boot has finished, the app takes over.
await act(async () => { await new Promise(r => setTimeout(r, 700)) })
expect(host.querySelector('.boot-splash')).toBeNull()
expect(host.textContent.trim().length).toBeGreaterThan(0)
})
it('keeps local data behind a boot that never finishes, but not forever', async () => {
// A signed-in device on a network that answers nothing. The splash holds while the app is
// loading — that is what a launch screen does — and the ceiling is what stops it becoming a
// screen you cannot get past: the workouts are already in local storage.
useStore.setState({ user: { id: 'u1', name: 'Test' }, ready: false })
const api = (await import('./lib/api.js')).api
api.mockImplementation(() => new Promise(() => {}))
// Fake timers from BEFORE the render: the component's two timeouts are scheduled on mount, and
// installing them afterwards leaves those on the real clock, so advancing the fake one moves
// nothing. (Learned by watching this test fail with the splash still up.)
vi.useFakeTimers()
await act(async () => { root.render() })
await act(async () => { vi.advanceTimersByTime(1300) })
expect(host.querySelector('.boot-splash')).toBeTruthy() // still loading, still showing it
// ...and the ceiling releases it, six seconds in. Driving that with fake timers is why this
// test takes milliseconds instead of six seconds.
await act(async () => { vi.advanceTimersByTime(6000) })
expect(host.querySelector('.boot-splash')).toBeNull()
vi.useRealTimers()
})
})