Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
feat(web-app): run the Cowork thread migration before the app renders
Akarshan Biswasc70fd01c7c96
diff --git a/web-app/src/providers/ServiceHubProvider.tsx b/web-app/src/providers/ServiceHubProvider.tsxindex afdddefe0..3a927adea 100644--- a/web-app/src/providers/ServiceHubProvider.tsx+++ b/web-app/src/providers/ServiceHubProvider.tsx@@ -3,6 +3,7 @@ import { initializeServiceHub } from '@/services'import { initializeServiceHubStore } from '@/hooks/useServiceHub'import { hydrateBackendStores } from '@/lib/hydrateStores'import { migrateLocalStorageToBackend } from '@/lib/migrateLocalStorageSettings'+import { migrateCoworkSessionsOnStartup } from '@/lib/migrateCoworkSessions'interface ServiceHubProviderProps {children: React.ReactNode@@ -22,6 +23,9 @@ export function ServiceHubProvider({ children }: ServiceHubProviderProps) {// Settings stores use async backend storage; hydrate before rendering// children so no component reads pre-hydration defaults.await hydrateBackendStores()+ // One-time copy of Cowork sessions into threads. It must finish before+ // children render because they list threads on mount. It never throws.+ await migrateCoworkSessionsOnStartup()setIsReady(true)}).catch((error) => {diff --git a/web-app/src/providers/__tests__/ServiceHubProvider.test.tsx b/web-app/src/providers/__tests__/ServiceHubProvider.test.tsxnew file mode 100644index 000000000..166274cfe--- /dev/null+++ b/web-app/src/providers/__tests__/ServiceHubProvider.test.tsx@@ -0,0 +1,67 @@+import { render, screen, waitFor } from '@testing-library/react'+import { describe, it, expect, vi, beforeEach } from 'vitest'++const h = vi.hoisted(() => ({+ order: [] as string[],+ initializeServiceHub: vi.fn(),+ migrateLocalStorageToBackend: vi.fn(),+ hydrateBackendStores: vi.fn(),+ migrateCoworkSessionsOnStartup: vi.fn(),+}))++vi.mock('@/services', () => ({+ initializeServiceHub: h.initializeServiceHub,+}))+vi.mock('@/hooks/useServiceHub', () => ({+ initializeServiceHubStore: vi.fn(),+}))+vi.mock('@/lib/migrateLocalStorageSettings', () => ({+ migrateLocalStorageToBackend: h.migrateLocalStorageToBackend,+}))+vi.mock('@/lib/hydrateStores', () => ({+ hydrateBackendStores: h.hydrateBackendStores,+}))+vi.mock('@/lib/migrateCoworkSessions', () => ({+ migrateCoworkSessionsOnStartup: h.migrateCoworkSessionsOnStartup,+}))++import { ServiceHubProvider } from '../ServiceHubProvider'++describe('ServiceHubProvider', () => {+ beforeEach(() => {+ h.order.length = 0+ h.initializeServiceHub.mockReset().mockResolvedValue({})+ h.migrateLocalStorageToBackend.mockReset().mockImplementation(async () => {+ h.order.push('settings-migration')+ })+ h.hydrateBackendStores.mockReset().mockImplementation(async () => {+ h.order.push('hydrate')+ })+ h.migrateCoworkSessionsOnStartup.mockReset().mockImplementation(async () => {+ h.order.push('cowork-migration')+ return null+ })+ })++ it('migrates Cowork sessions after the stores hydrate and before children render', async () => {+ render(+ <ServiceHubProvider>+ <div data-testid="child" />+ </ServiceHubProvider>+ )+ expect(screen.queryByTestId('child')).toBeNull()+ await waitFor(() => expect(screen.getByTestId('child')).toBeTruthy())+ // Children fetch threads on mount, so the migrated threads must exist by then.+ expect(h.order).toEqual(['settings-migration', 'hydrate', 'cowork-migration'])+ })++ it('still renders the app when the Cowork migration rejects', async () => {+ h.migrateCoworkSessionsOnStartup.mockRejectedValue(new Error('boom'))+ render(+ <ServiceHubProvider>+ <div data-testid="child" />+ </ServiceHubProvider>+ )+ await waitFor(() => expect(screen.getByTestId('child')).toBeTruthy())+ })+})