File size: 12,228 Bytes
e4daa3b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
# Spec 37.4: App Integration
**Status**: READY FOR IMPLEMENTATION
**Phase**: 4 of 5
**Depends On**: Spec 37.3 (Interactive Components)
**Goal**: Wire all components together into a working application
---
## Deliverables
By the end of this phase, you will have:
1. Complete `App.tsx` with full user flow
2. Integration tests for the complete workflow
3. Error handling for all states
4. Working end-to-end flow (with mocked API)
---
## Step 1: App Integration Tests
Create `src/App.test.tsx`:
```typescript
import { describe, it, expect, vi } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { server } from './mocks/server'
import { errorHandlers } from './mocks/handlers'
import App from './App'
describe('App Integration', () => {
describe('Initial Render', () => {
it('renders main heading', () => {
render(<App />)
expect(
screen.getByRole('heading', { name: /stroke lesion segmentation/i })
).toBeInTheDocument()
})
it('renders case selector', async () => {
render(<App />)
await waitFor(() => {
expect(screen.getByRole('combobox')).toBeInTheDocument()
})
})
it('renders run button', () => {
render(<App />)
expect(
screen.getByRole('button', { name: /run segmentation/i })
).toBeInTheDocument()
})
it('shows placeholder viewer message', () => {
render(<App />)
expect(
screen.getByText(/select a case and run segmentation/i)
).toBeInTheDocument()
})
})
describe('Run Button State', () => {
it('disables run button when no case selected', async () => {
render(<App />)
await waitFor(() => {
expect(screen.getByRole('combobox')).toBeInTheDocument()
})
expect(
screen.getByRole('button', { name: /run segmentation/i })
).toBeDisabled()
})
it('enables run button when case selected', async () => {
const user = userEvent.setup()
render(<App />)
await waitFor(() => {
expect(screen.getByRole('combobox')).toBeInTheDocument()
})
await user.selectOptions(screen.getByRole('combobox'), 'sub-stroke0001')
expect(
screen.getByRole('button', { name: /run segmentation/i })
).toBeEnabled()
})
})
describe('Segmentation Flow', () => {
it('shows processing state when running', async () => {
const user = userEvent.setup()
render(<App />)
await waitFor(() => {
expect(screen.getByRole('combobox')).toBeInTheDocument()
})
await user.selectOptions(screen.getByRole('combobox'), 'sub-stroke0001')
await user.click(screen.getByRole('button', { name: /run segmentation/i }))
expect(screen.getByText(/processing/i)).toBeInTheDocument()
})
it('displays metrics after successful segmentation', async () => {
const user = userEvent.setup()
render(<App />)
await waitFor(() => {
expect(screen.getByRole('combobox')).toBeInTheDocument()
})
await user.selectOptions(screen.getByRole('combobox'), 'sub-stroke0001')
await user.click(screen.getByRole('button', { name: /run segmentation/i }))
await waitFor(() => {
expect(screen.getByText('0.847')).toBeInTheDocument()
})
expect(screen.getByText('15.32 mL')).toBeInTheDocument()
expect(screen.getByText(/12\.5s/)).toBeInTheDocument()
})
it('displays viewer after successful segmentation', async () => {
const user = userEvent.setup()
render(<App />)
await waitFor(() => {
expect(screen.getByRole('combobox')).toBeInTheDocument()
})
await user.selectOptions(screen.getByRole('combobox'), 'sub-stroke0001')
await user.click(screen.getByRole('button', { name: /run segmentation/i }))
await waitFor(() => {
expect(document.querySelector('canvas')).toBeInTheDocument()
})
})
it('hides placeholder after successful segmentation', async () => {
const user = userEvent.setup()
render(<App />)
await waitFor(() => {
expect(screen.getByRole('combobox')).toBeInTheDocument()
})
await user.selectOptions(screen.getByRole('combobox'), 'sub-stroke0001')
await user.click(screen.getByRole('button', { name: /run segmentation/i }))
await waitFor(() => {
expect(screen.getByText('0.847')).toBeInTheDocument()
})
expect(
screen.queryByText(/select a case and run segmentation/i)
).not.toBeInTheDocument()
})
})
describe('Error Handling', () => {
it('shows error when segmentation fails', async () => {
server.use(errorHandlers.segmentServerError)
const user = userEvent.setup()
render(<App />)
await waitFor(() => {
expect(screen.getByRole('combobox')).toBeInTheDocument()
})
await user.selectOptions(screen.getByRole('combobox'), 'sub-stroke0001')
await user.click(screen.getByRole('button', { name: /run segmentation/i }))
await waitFor(() => {
expect(screen.getByRole('alert')).toBeInTheDocument()
})
expect(screen.getByText(/segmentation failed/i)).toBeInTheDocument()
})
it('allows retry after error', async () => {
server.use(errorHandlers.segmentServerError)
const user = userEvent.setup()
render(<App />)
await waitFor(() => {
expect(screen.getByRole('combobox')).toBeInTheDocument()
})
await user.selectOptions(screen.getByRole('combobox'), 'sub-stroke0001')
await user.click(screen.getByRole('button', { name: /run segmentation/i }))
await waitFor(() => {
expect(screen.getByRole('alert')).toBeInTheDocument()
})
// Reset to success handler
server.resetHandlers()
// Retry
await user.click(screen.getByRole('button', { name: /run segmentation/i }))
await waitFor(() => {
expect(screen.getByText('0.847')).toBeInTheDocument()
})
expect(screen.queryByRole('alert')).not.toBeInTheDocument()
})
})
describe('Multiple Runs', () => {
it('allows running segmentation on different cases', async () => {
const user = userEvent.setup()
render(<App />)
await waitFor(() => {
expect(screen.getByRole('combobox')).toBeInTheDocument()
})
// First case
await user.selectOptions(screen.getByRole('combobox'), 'sub-stroke0001')
await user.click(screen.getByRole('button', { name: /run segmentation/i }))
await waitFor(() => {
expect(screen.getByText('sub-stroke0001')).toBeInTheDocument()
})
// Second case
await user.selectOptions(screen.getByRole('combobox'), 'sub-stroke0002')
await user.click(screen.getByRole('button', { name: /run segmentation/i }))
await waitFor(() => {
expect(screen.getByText('sub-stroke0002')).toBeInTheDocument()
})
})
})
})
```
---
## Step 2: App Implementation
Replace `src/App.tsx`:
```typescript
import { useState } from 'react'
import { Layout } from './components/Layout'
import { CaseSelector } from './components/CaseSelector'
import { NiiVueViewer } from './components/NiiVueViewer'
import { MetricsPanel } from './components/MetricsPanel'
import { useSegmentation } from './hooks/useSegmentation'
export default function App() {
const [selectedCase, setSelectedCase] = useState<string | null>(null)
const { result, isLoading, error, runSegmentation } = useSegmentation()
const handleRunSegmentation = async () => {
if (selectedCase) {
await runSegmentation(selectedCase)
}
}
return (
<Layout>
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
{/* Left Panel: Controls */}
<div className="space-y-4">
<CaseSelector
selectedCase={selectedCase}
onSelectCase={setSelectedCase}
/>
<button
onClick={handleRunSegmentation}
disabled={!selectedCase || isLoading}
className="w-full bg-blue-600 hover:bg-blue-700 disabled:bg-gray-600
disabled:cursor-not-allowed text-white font-medium
py-3 px-4 rounded-lg transition-colors"
>
{isLoading ? 'Processing...' : 'Run Segmentation'}
</button>
{error && (
<div role="alert" className="bg-red-900/50 text-red-300 p-3 rounded-lg">
{error}
</div>
)}
{result && <MetricsPanel metrics={result.metrics} />}
</div>
{/* Right Panel: Viewer */}
<div className="lg:col-span-2">
{result ? (
<NiiVueViewer
backgroundUrl={result.dwiUrl}
overlayUrl={result.predictionUrl}
/>
) : (
<div className="bg-gray-900 rounded-lg h-[500px] flex items-center justify-center">
<p className="text-gray-400">
Select a case and run segmentation to view results
</p>
</div>
)}
</div>
</div>
</Layout>
)
}
```
---
## Step 3: Run Tests
```bash
npm test
# Expected: ~45+ tests passing
```
---
## Step 4: Visual Verification
```bash
npm run dev
# Open http://localhost:5173
```
**Manual Test Checklist:**
1. [ ] Page loads with header
2. [ ] Case selector shows "Loading cases..."
3. [ ] Case selector populates with 3 cases
4. [ ] Run button is disabled initially
5. [ ] Selecting a case enables run button
6. [ ] Clicking run shows "Processing..."
7. [ ] After completion, metrics panel appears
8. [ ] After completion, viewer shows (with demo image)
9. [ ] Selecting different case and running updates results
---
## Step 5: Test Custom Render Utility (Optional Enhancement)
Create `src/test/test-utils.tsx`:
```typescript
import type { ReactElement, ReactNode } from 'react'
import { render, RenderOptions } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
// Wrapper for any providers (Router, Theme, etc.)
function AllTheProviders({ children }: { children: ReactNode }) {
return <>{children}</>
}
function customRender(
ui: ReactElement,
options?: Omit<RenderOptions, 'wrapper'>
) {
return {
user: userEvent.setup(),
...render(ui, { wrapper: AllTheProviders, ...options }),
}
}
// Re-export everything
export * from '@testing-library/react'
export { customRender as render }
```
Update tests to use custom render (optional):
```typescript
import { render, screen, waitFor } from '../test/test-utils'
// Now `render` returns `{ user, ...result }`
```
---
## Verification Checklist
```bash
# All tests pass
npm test
# Expected: ~45+ tests passing
# Build succeeds
npm run build
# Expected: dist/ folder created
# No TypeScript errors
npx tsc --noEmit
# Expected: No errors
```
- [ ] All integration tests pass
- [ ] Full flow works in browser
- [ ] Error states display correctly
- [ ] Loading states display correctly
- [ ] Results update on new runs
---
## File Structure After This Phase
```
frontend/src/
βββ components/
β βββ __tests__/
β β βββ Layout.test.tsx
β β βββ MetricsPanel.test.tsx
β β βββ CaseSelector.test.tsx
β β βββ NiiVueViewer.test.tsx
β βββ Layout.tsx
β βββ MetricsPanel.tsx
β βββ CaseSelector.tsx
β βββ NiiVueViewer.tsx
β βββ index.ts
βββ api/
β βββ __tests__/
β β βββ client.test.ts
β βββ client.ts
β βββ index.ts
βββ hooks/
β βββ __tests__/
β β βββ useSegmentation.test.tsx
β βββ useSegmentation.ts
β βββ index.ts
βββ types/
β βββ index.ts
βββ test/
β βββ setup.ts
β βββ fixtures.ts
β βββ test-utils.tsx
βββ mocks/
β βββ handlers.ts
β βββ server.ts
βββ App.tsx
βββ App.test.tsx
βββ main.tsx
βββ index.css
```
---
## Next Phase
Once verification passes, proceed to **Spec 37.5: E2E Tests & CI/CD**
|