-
Notifications
You must be signed in to change notification settings - Fork 0
feat(@helm/web): read-only kanban dashboard and item detail (Session 6) #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6d77651
feat(@helm/web): add type-safe API client and test infrastructure
lhpaul b7c924d
feat(@helm/web): add usePolling hook and Kanban board view
lhpaul 7943da1
feat(@helm/web): add ItemDetail view and React Router wiring
lhpaul 35c3382
test(@helm/web): add usePolling hook tests with vi.useFakeTimers
lhpaul aee8561
fix(@helm/web): address 8 CodeRabbit findings on PR #10
lhpaul 7ac157e
fix(@helm/web): address 3 CodeRabbit findings on PR #10 (round 2)
lhpaul 7e98ec8
fix(@helm/web): address 2 CodeRabbit findings on PR #10 (round 3)
lhpaul 2f5ef5e
fix(@helm/web): show inline warning on transient poll errors in ItemD…
lhpaul 8a04081
fix(@helm/web): guard missing route param in ItemDetail before polling
lhpaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,14 @@ | ||
| import { useEffect, useState } from 'react'; | ||
|
|
||
| type WsStatus = 'connecting' | 'connected' | 'disconnected'; | ||
| import { BrowserRouter, Route, Routes } from 'react-router-dom'; | ||
| import { Kanban } from './views/Kanban.js'; | ||
| import { ItemDetail } from './views/ItemDetail.js'; | ||
|
|
||
| export default function App() { | ||
| const [status, setStatus] = useState<WsStatus>('connecting'); | ||
|
|
||
| useEffect(() => { | ||
| const ws = new WebSocket('ws://localhost:5173/ws'); | ||
| ws.onopen = () => setStatus('connected'); | ||
| ws.onclose = () => setStatus('disconnected'); | ||
| ws.onerror = () => setStatus('disconnected'); | ||
| return () => ws.close(); | ||
| }, []); | ||
|
|
||
| return ( | ||
| <div className="flex min-h-screen flex-col items-center justify-center bg-gray-50"> | ||
| <h1 className="text-4xl font-bold tracking-tight text-gray-900">Helm</h1> | ||
| <p className="mt-4 font-mono text-sm text-gray-500">{status}</p> | ||
| </div> | ||
| <BrowserRouter> | ||
| <Routes> | ||
| <Route path="/" element={<Kanban />} /> | ||
| <Route path="/items/:id" element={<ItemDetail />} /> | ||
| </Routes> | ||
| </BrowserRouter> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import { renderHook, act } from '@testing-library/react'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { usePolling } from './usePolling.js'; | ||
| import type { ApiResult } from '../lib/api.js'; | ||
|
|
||
| function ok<T>(data: T): ApiResult<T> { | ||
| return { ok: true, data }; | ||
| } | ||
| function err(message: string): ApiResult<never> { | ||
| return { ok: false, error: { type: 'network', message } }; | ||
| } | ||
|
|
||
| describe('usePolling', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it('starts in loading state and populates data on first fetch', async () => { | ||
| const fetcher = vi.fn().mockResolvedValue(ok({ count: 1 })); | ||
|
|
||
| const { result } = renderHook(() => usePolling(fetcher, 5_000)); | ||
|
|
||
| expect(result.current.loading).toBe(true); | ||
| expect(result.current.data).toBeNull(); | ||
|
|
||
| await act(async () => { | ||
| await Promise.resolve(); | ||
| }); | ||
|
|
||
| expect(result.current.loading).toBe(false); | ||
| expect(result.current.data).toEqual({ count: 1 }); | ||
| expect(fetcher).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('re-fetches after each interval tick', async () => { | ||
| const fetcher = vi.fn().mockResolvedValue(ok({ count: 1 })); | ||
| renderHook(() => usePolling(fetcher, 1_000)); | ||
|
|
||
| await act(async () => { | ||
| await Promise.resolve(); | ||
| }); | ||
| expect(fetcher).toHaveBeenCalledTimes(1); | ||
|
|
||
| await act(async () => { | ||
| vi.advanceTimersByTime(1_000); | ||
| await Promise.resolve(); | ||
| }); | ||
| expect(fetcher).toHaveBeenCalledTimes(2); | ||
|
|
||
| await act(async () => { | ||
| vi.advanceTimersByTime(1_000); | ||
| await Promise.resolve(); | ||
| }); | ||
| expect(fetcher).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| it('clears the interval on unmount', async () => { | ||
| const fetcher = vi.fn().mockResolvedValue(ok({})); | ||
| const { unmount } = renderHook(() => usePolling(fetcher, 1_000)); | ||
|
|
||
| await act(async () => { | ||
| await Promise.resolve(); | ||
| }); | ||
| unmount(); | ||
|
|
||
| await act(async () => { | ||
| vi.advanceTimersByTime(5_000); | ||
| await Promise.resolve(); | ||
| }); | ||
|
|
||
| expect(fetcher).toHaveBeenCalledTimes(1); // only the initial fetch | ||
| }); | ||
|
|
||
| it('sets error and clears data on fetch failure', async () => { | ||
| const fetcher = vi.fn().mockResolvedValue(err('Failed to fetch')); | ||
| const { result } = renderHook(() => usePolling(fetcher, 5_000)); | ||
|
|
||
| await act(async () => { | ||
| await Promise.resolve(); | ||
| }); | ||
|
|
||
| expect(result.current.error).toBe('Failed to fetch'); | ||
| expect(result.current.data).toBeNull(); | ||
| expect(result.current.loading).toBe(false); | ||
| }); | ||
|
|
||
| it('clears error when a subsequent poll succeeds', async () => { | ||
| const fetcher = vi | ||
| .fn() | ||
| .mockResolvedValueOnce(err('Network error')) | ||
| .mockResolvedValue(ok({ value: 42 })); | ||
|
|
||
| const { result } = renderHook(() => usePolling(fetcher, 1_000)); | ||
|
|
||
| await act(async () => { | ||
| await Promise.resolve(); | ||
| }); | ||
| expect(result.current.error).toBe('Network error'); | ||
|
|
||
| await act(async () => { | ||
| vi.advanceTimersByTime(1_000); | ||
| await Promise.resolve(); | ||
| }); | ||
|
|
||
| expect(result.current.error).toBeNull(); | ||
| expect(result.current.data).toEqual({ value: 42 }); | ||
| }); | ||
|
|
||
| it('handles a rejected fetcher promise and sets error state', async () => { | ||
| const fetcher = vi.fn().mockRejectedValue(new Error('boom')); | ||
| const { result } = renderHook(() => usePolling(fetcher, 5_000)); | ||
|
|
||
| await act(async () => { | ||
| await Promise.resolve(); | ||
| }); | ||
|
|
||
| expect(result.current.loading).toBe(false); | ||
| expect(result.current.error).toBe('boom'); | ||
| expect(result.current.data).toBeNull(); | ||
| }); | ||
|
|
||
| it('fetches once and does not poll when intervalMs is null', async () => { | ||
| const fetcher = vi.fn().mockResolvedValue(ok({})); | ||
| renderHook(() => usePolling(fetcher, null)); | ||
|
|
||
| await act(async () => { | ||
| await Promise.resolve(); | ||
| }); | ||
| expect(fetcher).toHaveBeenCalledTimes(1); | ||
|
|
||
| await act(async () => { | ||
| vi.advanceTimersByTime(30_000); | ||
| await Promise.resolve(); | ||
| }); | ||
| expect(fetcher).toHaveBeenCalledTimes(1); // still just once | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { useEffect, useRef, useState } from 'react'; | ||
| import type { ApiResult } from '../lib/api.js'; | ||
|
|
||
| export type PollingState<T> = { | ||
| data: T | null; | ||
| error: string | null; | ||
| loading: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Fetches immediately, then polls at `intervalMs`. | ||
| * Pass `intervalMs = null` to fetch once and skip polling. | ||
| * The fetcher reference is kept stable via a ref so callers can pass | ||
| * inline arrow functions without causing the effect to re-run. | ||
| * | ||
| * Guards: | ||
| * - `inFlight`: skips a tick if the previous request is still pending, | ||
| * preventing overlapping requests and out-of-order state writes. | ||
| * - try-catch: maps thrown errors (not just { ok: false } returns) to the | ||
| * error state so unhandled rejections never bubble up. | ||
| */ | ||
| export function usePolling<T>( | ||
| fetcher: () => Promise<ApiResult<T>>, | ||
| intervalMs: number | null = 5_000, | ||
| ): PollingState<T> { | ||
| const [data, setData] = useState<T | null>(null); | ||
| const [error, setError] = useState<string | null>(null); | ||
| const [loading, setLoading] = useState(true); | ||
|
|
||
| const fetcherRef = useRef(fetcher); | ||
| fetcherRef.current = fetcher; | ||
|
|
||
| useEffect(() => { | ||
| let cancelled = false; | ||
| let inFlight = false; | ||
|
|
||
| const doFetch = async () => { | ||
| if (inFlight) return; | ||
| inFlight = true; | ||
| try { | ||
| const result = await fetcherRef.current(); | ||
| if (cancelled) return; | ||
| if (result.ok) { | ||
| setData(result.data); | ||
| setError(null); | ||
| } else { | ||
| setError(result.error.message); | ||
| } | ||
| } catch (err) { | ||
| if (cancelled) return; | ||
| setError(err instanceof Error ? err.message : 'Unexpected polling error'); | ||
| } finally { | ||
| inFlight = false; | ||
| setLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| void doFetch(); | ||
|
|
||
| if (intervalMs === null) | ||
| return () => { | ||
| cancelled = true; | ||
| }; | ||
|
|
||
| const id = setInterval(() => { | ||
| void doFetch(); | ||
| }, intervalMs); | ||
| return () => { | ||
| cancelled = true; | ||
| clearInterval(id); | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }, [intervalMs]); | ||
|
|
||
| return { data, error, loading }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
| import { api } from './api.js'; | ||
|
|
||
| const mockFetch = vi.fn(); | ||
| vi.stubGlobal('fetch', mockFetch); | ||
|
|
||
| function mockOk(body: unknown): Response { | ||
| return { | ||
| ok: true, | ||
| status: 200, | ||
| json: () => Promise.resolve(body), | ||
| } as unknown as Response; | ||
| } | ||
|
|
||
| function mockErr(status: number, body: unknown): Response { | ||
| return { | ||
| ok: false, | ||
| status, | ||
| statusText: `Error ${status}`, | ||
| json: () => Promise.resolve(body), | ||
| } as unknown as Response; | ||
| } | ||
|
|
||
| afterEach(() => vi.clearAllMocks()); | ||
|
|
||
| describe('api.getProduct', () => { | ||
| it('returns ok result on success', async () => { | ||
| const product = { helm_version: '0', product: { slug: 'helm', name: 'Helm' } }; | ||
| mockFetch.mockResolvedValueOnce(mockOk(product)); | ||
| const result = await api.getProduct(); | ||
| expect(result.ok).toBe(true); | ||
| if (result.ok) expect(result.data.product.slug).toBe('helm'); | ||
| }); | ||
|
|
||
| it('returns http error on non-200', async () => { | ||
| mockFetch.mockResolvedValueOnce(mockErr(404, { error: 'Not found' })); | ||
| const result = await api.getProduct(); | ||
| expect(result.ok).toBe(false); | ||
| if (!result.ok) { | ||
| expect(result.error.type).toBe('http'); | ||
| expect((result.error as { status: number }).status).toBe(404); | ||
| } | ||
| }); | ||
|
|
||
| it('returns network error on fetch throw', async () => { | ||
| mockFetch.mockRejectedValueOnce(new Error('Failed to fetch')); | ||
| const result = await api.getProduct(); | ||
| expect(result.ok).toBe(false); | ||
| if (!result.ok) { | ||
| expect(result.error.type).toBe('network'); | ||
| expect(result.error.message).toBe('Failed to fetch'); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe('api.listItems', () => { | ||
| it('returns array of items', async () => { | ||
| const items = [{ externalId: 'issue_1', currentStage: 'discovery' }]; | ||
| mockFetch.mockResolvedValueOnce(mockOk(items)); | ||
| const result = await api.listItems(); | ||
| expect(result.ok).toBe(true); | ||
| if (result.ok) expect(result.data).toHaveLength(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('api.getItem', () => { | ||
| it('URL-encodes special characters in externalId', async () => { | ||
| const externalId = 'issue/1 with space'; | ||
| mockFetch.mockResolvedValueOnce(mockOk({ externalId })); | ||
| await api.getItem(externalId); | ||
| const calledUrl = mockFetch.mock.calls[0]?.[0] as string; | ||
| expect(calledUrl).toContain(encodeURIComponent(externalId)); | ||
| }); | ||
|
|
||
| it('returns http error on 404', async () => { | ||
| mockFetch.mockResolvedValueOnce(mockErr(404, { error: 'Not found' })); | ||
| const result = await api.getItem('issue_999'); | ||
| expect(result.ok).toBe(false); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.