import { act, fireEvent, screen, waitFor } from '@testing-library/react' import { beforeEach, describe, expect, it, vi } from 'vitest' import StaticMirrorPage from '@/pages/StaticMirrorPage' import { staticMirrorKeys } from '@/lib/queryKeys' import { renderWithProviders } from '@/test/render' const api = vi.hoisted (() => ({ apiGet: vi.fn (), apiPost: vi.fn (), startAPIDownload: vi.fn () })) vi.mock ('@/lib/api', async original => ({ ...await original (), ...api })) const definition = { resourceLimit: 100, nameLimit: 2000, maxBytes: 8388608, maxSeconds: 60, concurrency: 1, cooldownSeconds: 60, expirySeconds: 600, exclusions: ['historical_state', 'unversioned_resources', 'images', 'videos'] } const ready = { status: 'ready', dataAt: '2026-11-16T00:00:00.000000Z', processed: 2, total: 2 } beforeEach (() => { vi.clearAllMocks () api.apiGet.mockImplementation (async (path: string) => path === '/static_mirror' ? definition : ready) api.apiPost.mockResolvedValue ({ job: 'opaque-job' }) api.startAPIDownload.mockReturnValue (vi.fn ()) }) const startGeneration = async () => { const button = screen.getByRole ('button', { name: 'スナップショット生成' }) await waitFor (() => expect (button).toBeEnabled ()) fireEvent.click (button) } describe ('Static Mirror generation', () => { it ('starts a current snapshot and downloads through the common API', async () => { const view = renderWithProviders (, { route: '/ja/static-mirror' }) await startGeneration () expect (api.apiPost).toHaveBeenCalledWith ('/static_mirror/jobs', { }, { locale: 'ja' }) fireEvent.click (await screen.findByRole ('button', { name: 'ZIP ダウンロード' })) expect (screen.getByText (ready.dataAt)).toBeInTheDocument () expect (screen.getByText ('言語別名称').parentElement).toHaveTextContent ('2000') expect (api.startAPIDownload).toHaveBeenCalledWith ( '/static_mirror', 'opaque-job', expect.any (Function)) const release = api.startAPIDownload.mock.results[0].value view.unmount () expect (release).toHaveBeenCalled () }) it ('shows generation progress and offers the ZIP only after readiness', async () => { let state = { ...ready, status: 'running', processed: 1 } api.apiGet.mockImplementation (async (path: string) => path === '/static_mirror' ? definition : state) const view = renderWithProviders () await startGeneration () expect (await screen.findByText ('生成中')).toBeInTheDocument () expect (screen.queryByRole ('button', { name: 'ZIP ダウンロード' })).toBeNull () expect (screen.getByRole ('button', { name: 'スナップショット生成' })).toBeDisabled () state = ready await act (async () => { await view.queryClient.invalidateQueries ({ queryKey: staticMirrorKeys.job ('opaque-job') }) }) expect (await screen.findByRole ('button', { name: 'ZIP ダウンロード' })).toBeEnabled () }) it ('keeps failure reasons visible without offering a ZIP', async () => { api.apiGet.mockImplementation (async (path: string) => path === '/static_mirror' ? definition : { ...ready, status: 'failed', error: 'too_large' }) renderWithProviders () await startGeneration () expect (await screen.findByRole ('alert')).toHaveTextContent ('too_large') expect (screen.queryByRole ('button', { name: 'ZIP ダウンロード' })).toBeNull () }) it ('shows intake limits and revoked downloads using existing error copy', async () => { api.apiPost.mockRejectedValueOnce ({ isAxiosError: true, response: { status: 429 } }) renderWithProviders () await startGeneration () expect (await screen.findByRole ('alert')).toHaveTextContent ('HTTP 429') api.apiGet.mockImplementation (async (path: string) => { if (path === '/static_mirror') return definition throw { isAxiosError: true, response: { status: 404 } } }) await startGeneration () await waitFor (() => expect (screen.getByRole ('alert')).toHaveTextContent ('有効期限')) expect (screen.getByRole ('button', { name: 'スナップショット生成' })).toBeEnabled () }) it ('preserves Japanese fallback language metadata for untranslated UI', async () => { renderWithProviders (, { route: '/ain-Kana/static-mirror' }) await startGeneration () expect (api.apiPost).toHaveBeenCalledWith ( '/static_mirror/jobs', { }, { locale: 'ain-Kana' }) expect (screen.getByRole ('heading', { name: 'Static Mirror 生成機能' }) .querySelector ('[lang="ja"]')).toBeInTheDocument () }) })