import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises' import os from 'node:os' import path from 'node:path' import { describe, expect, it } from 'vitest' import { checkDeployment } from '../docsDeployment.js' const PRODUCTION = 'https://ecosystem.vuejs.press' const MIRROR_HOSTNAME = 'https://vuepress.github.io' interface Site { base: string hostname: string indexable: boolean } // the production site, served by its own origin at `/` const PRODUCTION_SITE: Site = { base: '/', hostname: PRODUCTION, indexable: true, } // the GitHub Pages mirror, served from a sub path of the organization site const MIRROR_SITE: Site = { base: '/ecosystem/', hostname: MIRROR_HOSTNAME, indexable: false, } const siteUrl = (site: Site): string => `${site.hostname}${site.base}` const page = (site: Site, pagePath: string): string => [ '', ``, ``, ``, ``, ``, ...(site.indexable ? [] : ['']), '', ].join('') const MANIFEST = JSON.stringify({ start_url: './', icons: ['192', '512'].map((size) => ({ src: `images/icons/android-chrome-${size}x${size}.png`, })), }) const sitemapXml = (site: Site): string => [ '', '', `${siteUrl(site)}`, `${siteUrl(site)}zh/`, '', ].join('') const robotsTxt = (site: Site): string => site.indexable ? `User-agent:*\nDisallow:\n\nSitemap: ${siteUrl(site)}sitemap.xml\n` : 'User-agent:*\nDisallow:\n' const createFiles = (site: Site): Record => ({ 'images/icons/android-chrome-192x192.png': '', 'images/icons/android-chrome-512x512.png': '', 'images/icons/favicon-16x16.png': '', 'index.html': page(site, '/'), 'manifest.webmanifest': MANIFEST, 'robots.txt': robotsTxt(site), ...(site.indexable ? { 'sitemap.xml': sitemapXml(site) } : {}), 'zh/index.html': page(site, '/zh/'), }) // write a minimal docs build and return its directory const createDist = async (files: Record): Promise => { const dist = await mkdtemp(path.join(os.tmpdir(), 'docs-deployment-')) await Promise.all( Object.entries(files).map(async ([name, content]) => { const file = path.join(dist, name) await mkdir(path.dirname(file), { recursive: true }) await writeFile(file, content) }), ) return dist } // run a check against a build that is removed afterwards const withDist = async ( files: Record, run: (dist: string) => Promise, ): Promise => { const dist = await createDist(files) try { await run(dist) } finally { await rm(dist, { force: true, recursive: true }) } } // replace a string in one file of a build const replaceIn = ( files: Record, file: string, from: string, to: string, ): Record => ({ ...files, [file]: files[file].replaceAll(from, to), }) const check = (dist: string, site: Site): Promise => checkDeployment({ base: site.base, canonicalHostname: PRODUCTION, dist, hostname: site.hostname, indexable: site.indexable, }) const canonicalLink = (pagePath: string): string => `` describe('docs deployment', () => { it('accepts the production build', async () => { await withDist(createFiles(PRODUCTION_SITE), async (dist) => { await expect(check(dist, PRODUCTION_SITE)).resolves.toStrictEqual([]) }) }) it('accepts the build of the sub path mirror', async () => { await withDist(createFiles(MIRROR_SITE), async (dist) => { await expect(check(dist, MIRROR_SITE)).resolves.toStrictEqual([]) }) }) it('reports a head file outside of the base', async () => { const files = replaceIn( createFiles(MIRROR_SITE), 'index.html', '/ecosystem/images/icons/favicon-16x16.png', '/images/icons/favicon-16x16.png', ) await withDist(files, async (dist) => { await expect(check(dist, MIRROR_SITE)).resolves.toContainEqual( expect.stringContaining( '"/images/icons/favicon-16x16.png" is not served under the base', ), ) }) }) it('reports a head file missing from the build', async () => { await withDist(createFiles(MIRROR_SITE), async (dist) => { await rm(path.join(dist, 'images/icons/favicon-16x16.png'), { force: true, }) await expect(check(dist, MIRROR_SITE)).resolves.toContainEqual( expect.stringContaining('misses its file in the build'), ) }) }) it('reports a page without canonical URL', async () => { const files = replaceIn( createFiles(MIRROR_SITE), 'index.html', canonicalLink('/'), '', ) await withDist(files, async (dist) => { await expect(check(dist, MIRROR_SITE)).resolves.toContainEqual( expect.stringContaining('misses its canonical URL'), ) }) }) it('reports a canonical URL of another page', async () => { const files = replaceIn( createFiles(MIRROR_SITE), 'zh/index.html', canonicalLink('/zh/'), canonicalLink('/'), ) await withDist(files, async (dist) => { await expect(check(dist, MIRROR_SITE)).resolves.toContainEqual( expect.stringContaining(`is not the canonical URL "${PRODUCTION}/zh/"`), ) }) }) it('reports an absolute URL of another origin', async () => { const files = replaceIn( createFiles(MIRROR_SITE), 'index.html', ``, '', ) await withDist(files, async (dist) => { await expect(check(dist, MIRROR_SITE)).resolves.toContainEqual( expect.stringContaining(`is not under ${MIRROR_HOSTNAME}/ecosystem/`), ) }) }) it('reports a doubled base path', async () => { const files = replaceIn( createFiles(MIRROR_SITE), 'index.html', ``, ``, ) await withDist(files, async (dist) => { await expect(check(dist, MIRROR_SITE)).resolves.toContainEqual( expect.stringContaining('repeats the base path'), ) }) }) it('reports a sitemap page missing from the build', async () => { const site = PRODUCTION_SITE const files = { ...createFiles(site), 'sitemap.xml': `${siteUrl(site)}missing.html`, } await withDist(files, async (dist) => { await expect(check(dist, site)).resolves.toContainEqual( expect.stringContaining('misses its page in the build'), ) }) }) it('reports a robots.txt that misses the sitemap of the build', async () => { const files = { ...createFiles(MIRROR_SITE), 'sitemap.xml': sitemapXml(MIRROR_SITE), } await withDist(files, async (dist) => { await expect(check(dist, MIRROR_SITE)).resolves.toContainEqual( expect.stringContaining('sitemap.xml" is missing'), ) }) }) it('reports a robots.txt that advertises a missing sitemap', async () => { const files = replaceIn( createFiles(MIRROR_SITE), 'robots.txt', 'Disallow:\n', `Disallow:\n\nSitemap: ${siteUrl(MIRROR_SITE)}sitemap.xml\n`, ) await withDist(files, async (dist) => { await expect(check(dist, MIRROR_SITE)).resolves.toContainEqual( expect.stringContaining('advertises a sitemap the build does not have'), ) }) }) it('reports a mirror page that may be indexed', async () => { const files = replaceIn( createFiles(MIRROR_SITE), 'zh/index.html', '', '', ) await withDist(files, async (dist) => { await expect(check(dist, MIRROR_SITE)).resolves.toContainEqual( expect.stringContaining('is not marked "noindex"'), ) }) }) it('reports a production page that is not indexable', async () => { const files = replaceIn( createFiles(PRODUCTION_SITE), 'index.html', '', '', ) await withDist(files, async (dist) => { await expect(check(dist, PRODUCTION_SITE)).resolves.toContainEqual( expect.stringContaining('is marked "noindex" although it is indexable'), ) }) }) it('reports a manifest that hard codes the root of its origin', async () => { const files = { ...createFiles(MIRROR_SITE), 'manifest.webmanifest': JSON.stringify({ start_url: '/index.html', icons: [{ src: '/images/icons/android-chrome-192x192.png' }], }), } await withDist(files, async (dist) => { await expect(check(dist, MIRROR_SITE)).resolves.toContainEqual( expect.stringContaining( `"/index.html" resolves to "${MIRROR_HOSTNAME}/index.html"`, ), ) }) }) it('reports a missing build', async () => { await expect( checkDeployment({ base: MIRROR_SITE.base, canonicalHostname: PRODUCTION, dist: path.join(os.tmpdir(), 'docs-deployment-missing'), hostname: MIRROR_SITE.hostname, indexable: MIRROR_SITE.indexable, }), ).resolves.toContainEqual(expect.stringContaining('build the docs first')) }) })