/* @vitest-environment jsdom */ import { renderHook, waitFor } from '@testing-library/react'; import { beforeEach, expect, test, vi } from 'vitest'; import type { AuthUser } from '../../services/authService'; import { listRpgCreationWorks } from '../../services/rpg-creation/index'; import { listRpgEntryWorldGallery, listRpgEntryWorldLibrary, listRpgProfileBrowseHistory, listRpgProfileSaveArchives, } from '../../services/rpg-entry'; import { useRpgEntryBootstrap } from './useRpgEntryBootstrap'; vi.mock('../../services/rpg-entry', () => ({ listRpgEntryWorldGallery: vi.fn(), listRpgEntryWorldLibrary: vi.fn(), listRpgProfileBrowseHistory: vi.fn(), listRpgProfileSaveArchives: vi.fn(), resumeRpgProfileSaveArchive: vi.fn(), upsertRpgProfileBrowseHistory: vi.fn(), })); vi.mock('../../services/rpg-creation/index', () => ({ listRpgCreationWorks: vi.fn(), })); const mockListRpgEntryWorldGallery = vi.mocked(listRpgEntryWorldGallery); const mockListRpgEntryWorldLibrary = vi.mocked(listRpgEntryWorldLibrary); const mockListRpgCreationWorks = vi.mocked(listRpgCreationWorks); const mockListRpgProfileBrowseHistory = vi.mocked(listRpgProfileBrowseHistory); const mockListRpgProfileSaveArchives = vi.mocked(listRpgProfileSaveArchives); const testUser: AuthUser = { id: 'user-1', publicUserCode: '100001', displayName: 'Test User', avatarUrl: null, phoneNumberMasked: null, loginMethod: 'password', bindingStatus: 'active', wechatBound: false, }; beforeEach(() => { vi.clearAllMocks(); mockListRpgEntryWorldGallery.mockResolvedValue([]); mockListRpgEntryWorldLibrary.mockResolvedValue([]); mockListRpgCreationWorks.mockResolvedValue([]); mockListRpgProfileBrowseHistory.mockResolvedValue([]); mockListRpgProfileSaveArchives.mockResolvedValue([]); }); test('login refreshes platform data without showing page loading again', async () => { const getProfileDashboard = vi.fn(async () => null); let resolveInitialGallery: (value: []) => void = () => undefined; mockListRpgEntryWorldGallery.mockReturnValueOnce( new Promise<[]>((resolve) => { resolveInitialGallery = resolve; }), ); const { result, rerender } = renderHook( ({ user }: { user: AuthUser | null }) => useRpgEntryBootstrap({ user, canAccessProtectedData: Boolean(user), getProfileDashboard, handleContinueGame: vi.fn(), hasInitialAgentSession: false, }), { initialProps: { user: null as AuthUser | null } }, ); await waitFor(() => { expect(result.current.isLoadingPlatform).toBe(true); }); resolveInitialGallery([]); await waitFor(() => { expect(result.current.isLoadingPlatform).toBe(false); }); rerender({ user: testUser }); await waitFor(() => { expect(mockListRpgEntryWorldLibrary).toHaveBeenCalledTimes(1); }); expect(result.current.isLoadingPlatform).toBe(false); expect(result.current.isLoadingDashboard).toBe(false); });