AI: address issue #483

このコミットが含まれているのは:
Codex Night Worker
2026-11-16 13:11:57 +09:00
コミット cb1f947a36
18個のファイルの変更、1352行の追加、0行の削除
+3
ファイルの表示
@@ -83,6 +83,7 @@ const MaterialSyncSuppressionsPage = lazyPage (
() => import ('@/pages/materials/MaterialSyncSuppressionsPage'))
const CountsResourcePage = lazyPage (() => import ('@/pages/counts/CountsResourcePage'))
const MorePage = lazyPage (() => import ('@/pages/MorePage'))
const StaticMirrorPage = lazyPage (() => import ('@/pages/StaticMirrorPage'))
const GekanatorUnavailablePage = lazyPage (() => import ('@/pages/GekanatorUnavailablePage'))
const NicoTagListPage = lazyPage (() => import ('@/pages/tags/NicoTagListPage'))
const TOSPage = lazyPage (() => import ('@/pages/TOSPage'))
@@ -126,6 +127,8 @@ export const pageRoutes = (
user: User | null,
setUser: Dispatch<SetStateAction<User | null>>,
): PageRoute[] => [
{ path: 'static-mirror', element: <StaticMirrorPage/>,
handle: { preload: StaticMirrorPage.preload, robots: 'noindex' } },
{ path: '', element: <LocaleRedirect to="/posts"/>,
handle: { redirectTo: '/posts' } },
{ path: 'posts', element: <PostListPage/>,
+4
ファイルの表示
@@ -7,6 +7,10 @@ import type {
MaterialFilter,
WikiTagReference } from '@/types'
export const staticMirrorKeys = {
definition: ['static_mirror', 'definition'] as const,
job: (id: string | null) => ['static_mirror', 'jobs', id] as const }
export const localesKeys = {
root: ['locales'] as const,
index: ['locales', 'index'] as const }
+59
ファイルの表示
@@ -0,0 +1,59 @@
import { readFileSync } from 'node:fs'
import { URL as NodeURL } from 'node:url'
import { fireEvent, waitFor } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const script = readFileSync (new NodeURL (
'../../../backend/app/views/static_mirror/archive.js', import.meta.url), 'utf8')
const archiveWindow = window as typeof window & { archiveConfig?: { origin: string | null } }
beforeEach (() => {
const element = document.createElement ('script')
element.src = 'https://mirror.test/subdirectory/archive/assets/archive.js'
vi.spyOn (document, 'currentScript', 'get').mockReturnValue (element)
delete archiveWindow.archiveConfig
document.body.innerHTML = `
<nav><a href="ja/posts/index.html">Post</a>
<a data-original="/ja/posts/1" href="https://source.test/ja/posts/1">Source</a></nav>
<form id="archive-search"><input name="q" value="alias"></form>
<ul id="search-results"></ul>`
})
afterEach (() => {
delete archiveWindow.archiveConfig
document.body.replaceChildren ()
vi.unstubAllGlobals ()
})
describe ('Portable static mirror assets', () => {
it ('searches local JSON by aliases using safe text and relative links', async () => {
const fetcher = vi.fn ().mockResolvedValue ({ ok: true, json: async () => [
{ path: 'ja/tags/1/index.html', title: '<img src=x onerror=alert(1)>', names: ['alias'] },
{ path: 'https://outside.test/', title: 'alias' },
{ path: '../outside.html', title: 'alias' }] })
vi.stubGlobal ('fetch', fetcher)
new Function (script) ()
fireEvent.submit (document.querySelector ('form')!)
await waitFor (() => expect (document.querySelectorAll ('#search-results a')).toHaveLength (1))
const link = document.querySelector ('#search-results a')!
expect (link).toHaveAttribute ('href', 'ja/tags/1/index.html')
expect (link.textContent).toBe ('<img src=x onerror=alert(1)>')
expect (document.querySelector ('img')).toBeNull ()
expect (fetcher).toHaveBeenCalledTimes (1)
expect (fetcher.mock.calls[0][0].href)
.toBe ('https://mirror.test/subdirectory/archive/search-index.json')
})
it ('retains baked links without config and changes only source links with valid config', () => {
new Function (script) ()
const source = document.querySelector ('[data-original]')!
expect (source).toHaveAttribute ('href', 'https://source.test/ja/posts/1')
archiveWindow.archiveConfig = { origin: 'javascript:alert(1)' }
new Function (script) ()
expect (source).toHaveAttribute ('href', 'https://source.test/ja/posts/1')
archiveWindow.archiveConfig = { origin: 'https://replacement.test/base/' }
new Function (script) ()
expect (source).toHaveAttribute ('href', 'https://replacement.test/base/ja/posts/1')
expect (document.querySelector ('a:not([data-original])'))
.toHaveAttribute ('href', 'ja/posts/index.html')
})
})
+19
ファイルの表示
@@ -0,0 +1,19 @@
{
"_untranslated": ["*"],
"title": "Static Mirror 生成機能",
"generation": "スナップショット生成",
"dataAt": "データ基準時点",
"currentOnly": "今回は現在状態のみの対応",
"developmentOnly": "開発環境限定",
"limits": "出力件数の制限",
"concurrency": "同時実行数",
"exclusions": "未対応機能",
"protection": "データ保護上の制約",
"protectionDetails": "公開抑止、除票、非公開情報、認証情報、監査専用情報を ZIP に含めない。",
"externalAssets": "外部画像や動画への依存",
"externalMedia": "外部画像や動画",
"externalOmissions": "ローカルへ取り込めていない資産",
"download": "ZIP ダウンロード",
"zip": "ZIP",
"failure": "失敗時の理由"
}
+18
ファイルの表示
@@ -0,0 +1,18 @@
{
"title": "Static Mirror 生成機能",
"generation": "スナップショット生成",
"dataAt": "データ基準時点",
"currentOnly": "今回は現在状態のみの対応",
"developmentOnly": "開発環境限定",
"limits": "出力件数の制限",
"concurrency": "同時実行数",
"exclusions": "未対応機能",
"protection": "データ保護上の制約",
"protectionDetails": "公開抑止、除票、非公開情報、認証情報、監査専用情報を ZIP に含めない。",
"externalAssets": "外部画像や動画への依存",
"externalMedia": "外部画像や動画",
"externalOmissions": "ローカルへ取り込めていない資産",
"download": "ZIP ダウンロード",
"zip": "ZIP",
"failure": "失敗時の理由"
}
+5
ファイルの表示
@@ -16,12 +16,17 @@ import type { User } from '@/types'
const MorePage: FC = () => {
const location = useLocation ()
const { message } = useUITranslation ('shared')
const { label: archiveLabel } = useUITranslation ('archive')
const menu = menuOutline (
{ tag: null, wikiId: null, user: { } as User, pathName: location.pathname })
return (
<MainArea className="md:flex">
<PageHead title={message ('labels.menu')}/>
{import.meta.env.DEV && (
<PrefetchLink to="/static-mirror">
{archiveLabel ('title')}
</PrefetchLink>)}
{[...Array (Math.ceil (menu.length / 4)).keys ()].map (i => (
<div key={i} className="flex-1 mx-16">
+96
ファイルの表示
@@ -0,0 +1,96 @@
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<typeof import ('@/lib/api')> (), ...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 (<StaticMirrorPage/>, { 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 (<StaticMirrorPage/>)
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 (<StaticMirrorPage/>)
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 (<StaticMirrorPage/>)
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 (<StaticMirrorPage/>, { 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 ()
})
})
+140
ファイルの表示
@@ -0,0 +1,140 @@
import { useMutation, useQuery } from '@tanstack/react-query'
import { useEffect, useRef, useState } from 'react'
import PageHead from '@/components/common/PageHead'
import PageTitle from '@/components/common/PageTitle'
import MainArea from '@/components/layout/MainArea'
import { Button } from '@/components/ui/button'
import { apiGet, apiPost, isApiError, startAPIDownload } from '@/lib/api'
import { staticMirrorKeys } from '@/lib/queryKeys'
import { useLocale } from '@/lib/useLocale'
import { useUITranslation } from '@/lib/useUITranslation'
type Definition = {
resourceLimit: number
nameLimit: number
maxBytes: number
maxSeconds: number
concurrency: number
cooldownSeconds: number
expirySeconds: number
exclusions: string[] }
type Job = {
status: 'queued' | 'running' | 'ready' | 'failed'
dataAt: string | null
processed: number
total: number | null
error?: string }
const StaticMirrorPage = () => {
const locale = useLocale ()
const { label, message } = useUITranslation ('archive')
const [job, setJob] = useState<string | null> (null)
const [downloadFailed, setDownloadFailed] = useState (false)
const downloadRef = useRef<(() => void) | null> (null)
useEffect (() => () => downloadRef.current?.(), [])
const definition = useQuery ({
queryKey: staticMirrorKeys.definition,
queryFn: () => apiGet<Definition> ('/static_mirror'), retry: false })
const { mutate: start, isPending: pending, error: creationError } = useMutation ({
mutationFn: () => apiPost<{ job: string }> ('/static_mirror/jobs', { }, { locale }),
onSuccess: result => {
downloadRef.current?.()
setDownloadFailed (false)
setJob (result.job)
} })
const status = useQuery ({
queryKey: staticMirrorKeys.job (job),
queryFn: () => apiGet<Job> (`/static_mirror/jobs/${encodeURIComponent (job!)}`),
enabled: job != null,
retry: false,
refetchInterval: query =>
(query.state.error != null
|| ['ready', 'failed'].includes (query.state.data?.status ?? ''))
? false : 1000 })
const unavailable = isApiError (status.error) && status.error.response?.status === 404
const active = job != null && !unavailable
&& !['ready', 'failed'].includes (status.data?.status ?? '')
const error = creationError ?? status.error ?? definition.error
const errorStatus = isApiError (error) ? error.response?.status : null
const metadata = definition.data
const percentage =
(status.data?.total == null || status.data.total === 0)
? 100
: Math.round (100 * status.data.processed / status.data.total)
const download = () => {
if (job == null) return
downloadRef.current?.()
setDownloadFailed (false)
downloadRef.current = startAPIDownload (
'/static_mirror', job, () => setDownloadFailed (true))
}
return (
<MainArea className="space-y-4">
<PageHead title={message ('title')}/>
<PageTitle>{label ('title')}</PageTitle>
<p>{label ('developmentOnly')} / {label ('currentOnly')}</p>
{metadata != null && (
<dl>
<dt>{label ('limits')}</dt>
<dd>
{label ('counts:resources.posts')} / {label ('counts:resources.tags')}:
{' '}{metadata.resourceLimit}
</dd>
<dd>{label ('counts:columns.names')}: {metadata.nameLimit}</dd>
<dd>{metadata.maxBytes} B / {metadata.maxSeconds} s</dd>
<dt>{label ('concurrency')}</dt>
<dd>{metadata.concurrency} / {metadata.cooldownSeconds} s</dd>
<dd>{label ('zip')}: {metadata.expirySeconds} s</dd>
<dt>{label ('exclusions')}</dt>
<dd>
{['counts:resources.tag_descriptions', 'navigation:help',
'counts:resources.materials', 'counts:resources.nico_tags',
'counts:attributes.parent_post_ids', 'counts:attributes.parent_tag_ids',
'counts:columns.sections', 'navigation:history', 'navigation:edit']
.map (key => <span key={key}>{label (key)}{' / '}</span>)}
</dd>
<dt>{label ('externalAssets')}</dt>
<dd>{label ('externalOmissions')}: {label ('externalMedia')}</dd>
<dt>{label ('protection')}</dt>
<dd>{label ('protectionDetails')}</dd>
</dl>)}
<Button onClick={() => start ()}
disabled={metadata == null || pending || active}>
{label ('generation')}
</Button>
{status.data != null && (
<div role="status">
<p>{label ('dataAt')}: <time>{status.data.dataAt ?? '—'}</time></p>
<p>
{status.data.status === 'ready'
? label ('download')
: label (`counts:exportProgress.${status.data.status}`)}
</p>
{status.data.total != null && (
<p>{label ('counts:exportProgress.measured', {
processed: status.data.processed, total: status.data.total,
percent: percentage })}</p>)}
{status.data.status === 'ready' && (
<Button onClick={download}>{label ('download')}</Button>)}
{status.data.error != null && (
<p role="alert">{label ('failure')}: <code>{status.data.error}</code></p>)}
</div>)}
{error != null && (
<p role="alert">
{errorStatus === 404
? label ('counts:downloadErrors.unavailable')
: (errorStatus != null
? label ('counts:errors.http', { status: errorStatus })
: label ('counts:errors.network'))}
</p>)}
{status.error != null && (
<Button onClick={() => status.refetch ()}>{label ('shared:actions.retry')}</Button>)}
{downloadFailed && <p role="alert">{label ('counts:downloadErrors.failed')}</p>}
</MainArea>)
}
export default StaticMirrorPage