|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { afterEach, beforeEach, test } from 'node:test'; |
| 3 | + |
| 4 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
| 5 | +import { act, cleanup, render, waitFor } from '@testing-library/react'; |
| 6 | +import { createElement } from 'react'; |
| 7 | + |
| 8 | +import type { Project } from '../types/app'; |
| 9 | +import { resetAppShellStore } from '../stores/useAppShellStore'; |
| 10 | + |
| 11 | +import { useProjectsState } from './useProjectsState'; |
| 12 | + |
| 13 | +/* |
| 14 | + * The selected project survives a reload of `/`. |
| 15 | + * |
| 16 | + * `/session/:id` restores its context from the URL, but `/` used to come back |
| 17 | + * to "pick a project" every time. The last selection is remembered and |
| 18 | + * restored when the list has loaded - and only if the project is still in it. |
| 19 | + */ |
| 20 | + |
| 21 | +const project = (projectId: string, displayName: string): Project => ({ |
| 22 | + projectId, |
| 23 | + path: `/workspace/${projectId}`, |
| 24 | + fullPath: `/workspace/${projectId}`, |
| 25 | + displayName, |
| 26 | + origin: 'explicit', |
| 27 | + isStarred: false, |
| 28 | + sessions: [], |
| 29 | + sessionMeta: { hasMore: false, total: 0 }, |
| 30 | +}); |
| 31 | + |
| 32 | +// Two projects, so the "a lone project selects itself" rule stays out of the way. |
| 33 | +const twoProjects = [project('project-1', 'Project one'), project('project-2', 'Project two')]; |
| 34 | + |
| 35 | +type HookState = ReturnType<typeof useProjectsState>; |
| 36 | + |
| 37 | +/** One page load: a fresh query cache and shell store, the same localStorage. */ |
| 38 | +const mountApp = (route: { sessionId?: string | null } = {}) => { |
| 39 | + let state: HookState | null = null; |
| 40 | + const queryClient = new QueryClient({ defaultOptions: { queries: { refetchOnWindowFocus: false, retry: false } } }); |
| 41 | + const Harness = () => { |
| 42 | + state = useProjectsState({ |
| 43 | + sessionId: route.sessionId ?? null, |
| 44 | + navigate: (() => undefined) as never, |
| 45 | + subscribe: () => () => undefined, |
| 46 | + isMobile: false, |
| 47 | + activeSessions: new Map(), |
| 48 | + }); |
| 49 | + return null; |
| 50 | + }; |
| 51 | + const view = render(createElement(QueryClientProvider, { client: queryClient }, createElement(Harness))); |
| 52 | + return { |
| 53 | + getState: () => { |
| 54 | + assert.ok(state, 'hook state is available after rendering'); |
| 55 | + return state; |
| 56 | + }, |
| 57 | + reload: () => { |
| 58 | + view.unmount(); |
| 59 | + resetAppShellStore(); |
| 60 | + }, |
| 61 | + }; |
| 62 | +}; |
| 63 | + |
| 64 | +const serveProjects = (projects: Project[]) => { |
| 65 | + const originalFetch = globalThis.fetch; |
| 66 | + globalThis.fetch = async () => new Response(JSON.stringify(projects), { status: 200 }); |
| 67 | + return () => { globalThis.fetch = originalFetch; }; |
| 68 | +}; |
| 69 | + |
| 70 | +// Suites share one window here, so a remembered id from another file must not |
| 71 | +// leak into a "first visit". |
| 72 | +beforeEach(() => { |
| 73 | + localStorage.clear(); |
| 74 | + resetAppShellStore(); |
| 75 | +}); |
| 76 | + |
| 77 | +afterEach(() => { |
| 78 | + cleanup(); |
| 79 | + localStorage.clear(); |
| 80 | + resetAppShellStore(); |
| 81 | +}); |
| 82 | + |
| 83 | +test('the project selected before a reload is selected again after it', async () => { |
| 84 | + const restore = serveProjects(twoProjects); |
| 85 | + try { |
| 86 | + const first = mountApp(); |
| 87 | + await waitFor(() => assert.equal(first.getState().projects.length, 2)); |
| 88 | + assert.equal(first.getState().selectedProject, null, 'nothing is chosen for the user on a first visit'); |
| 89 | + |
| 90 | + act(() => { first.getState().handleProjectSelect(twoProjects[1]); }); |
| 91 | + assert.equal(first.getState().selectedProject?.projectId, 'project-2'); |
| 92 | + |
| 93 | + first.reload(); |
| 94 | + const second = mountApp(); |
| 95 | + await waitFor(() => assert.equal(second.getState().selectedProject?.projectId, 'project-2')); |
| 96 | + } finally { |
| 97 | + restore(); |
| 98 | + } |
| 99 | +}); |
| 100 | + |
| 101 | +test('a remembered project that no longer exists is ignored', async () => { |
| 102 | + localStorage.setItem('selectedProjectId', 'project-deleted'); |
| 103 | + const restore = serveProjects(twoProjects); |
| 104 | + try { |
| 105 | + const app = mountApp(); |
| 106 | + await waitFor(() => assert.equal(app.getState().projects.length, 2)); |
| 107 | + // Give the restore effect a turn; a stale id must not select anything. |
| 108 | + await act(async () => { await Promise.resolve(); }); |
| 109 | + assert.equal(app.getState().selectedProject, null); |
| 110 | + } finally { |
| 111 | + restore(); |
| 112 | + } |
| 113 | +}); |
| 114 | + |
| 115 | +test('deleting the selected project forgets it, so a reload does not bring it back', async () => { |
| 116 | + // Three, so that two remain and neither selects itself as a lone project. |
| 117 | + const restore = serveProjects([...twoProjects, project('project-3', 'Project three')]); |
| 118 | + try { |
| 119 | + const app = mountApp(); |
| 120 | + await waitFor(() => assert.equal(app.getState().projects.length, 3)); |
| 121 | + act(() => { app.getState().handleProjectSelect(twoProjects[0]); }); |
| 122 | + assert.equal(localStorage.getItem('selectedProjectId'), 'project-1'); |
| 123 | + |
| 124 | + act(() => { app.getState().handleProjectDelete('project-1'); }); |
| 125 | + await act(async () => { await Promise.resolve(); }); |
| 126 | + assert.equal(localStorage.getItem('selectedProjectId'), null); |
| 127 | + assert.equal(app.getState().selectedProject, null); |
| 128 | + } finally { |
| 129 | + restore(); |
| 130 | + } |
| 131 | +}); |
| 132 | + |
| 133 | +test('a session route restores from the URL and leaves the remembered project alone', async () => { |
| 134 | + localStorage.setItem('selectedProjectId', 'project-2'); |
| 135 | + const restore = serveProjects(twoProjects); |
| 136 | + try { |
| 137 | + const app = mountApp({ sessionId: 'session-elsewhere' }); |
| 138 | + await waitFor(() => assert.equal(app.getState().projects.length, 2)); |
| 139 | + await act(async () => { await Promise.resolve(); }); |
| 140 | + assert.equal(app.getState().selectedProject, null, 'the URL owns the context on /session/:id'); |
| 141 | + } finally { |
| 142 | + restore(); |
| 143 | + } |
| 144 | +}); |
0 commit comments