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 = ` ` }) 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: '', 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 ('') 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') }) it.each (['tag name', 'alias']) ('finds linked posts by %s', async query => { const input = document.querySelector ('input')! input.value = query vi.stubGlobal ('fetch', vi.fn ().mockResolvedValue ({ ok: true, json: async () => [ { path: 'ja/posts/1/index.html', title: 'Post title', names: ['Tag Name', 'Alias'] }, { path: 'ja/posts/2/index.html', title: 'Unrelated post', names: ['Other tag'] }, { path: 'ja/tags/1/index.html', title: 'Tag Name', names: ['Tag Name', 'Alias'] }] })) new Function (script) () fireEvent.submit (document.querySelector ('form')!) await waitFor (() => expect (document.querySelectorAll ('#search-results a')).toHaveLength (2)) const links = [...document.querySelectorAll ('#search-results a')] expect (links.map (link => link.getAttribute ('href'))) .toEqual (['ja/posts/1/index.html', 'ja/tags/1/index.html']) expect (links[0].textContent).toBe ('Post title') }) })