|
| 1 | +import { mkdir, mkdtemp, rm, writeFile } from 'fs/promises' |
| 2 | +import { tmpdir } from 'os' |
| 3 | +import path from 'path' |
| 4 | +import { describe, expect, test } from 'vitest' |
| 5 | +import { ALL_LOCALES, ROOT_LOCALE, type DocsLocale } from './locales' |
| 6 | +import { scanBuiltLocales } from './validate-locales-build' |
| 7 | + |
| 8 | +function localeHref(locale: DocsLocale, publicPath: string): string { |
| 9 | + return `${publicPath}${locale.path ? `${locale.path}/` : ''}` |
| 10 | +} |
| 11 | + |
| 12 | +function rootHtml( |
| 13 | + publicPath: string, |
| 14 | + options: { omittedLocale?: DocsLocale; revision?: string; wrongLabelLocale?: DocsLocale } = {}, |
| 15 | +): string { |
| 16 | + const links = ALL_LOCALES.filter((locale) => locale !== ROOT_LOCALE) |
| 17 | + .filter((locale) => locale !== options.omittedLocale) |
| 18 | + .map( |
| 19 | + (locale) => |
| 20 | + `<a href="${localeHref(locale, publicPath)}">${locale === options.wrongLabelLocale ? '' : locale.label}</a>`, |
| 21 | + ) |
| 22 | + .join('') |
| 23 | + |
| 24 | + const revision = options.revision |
| 25 | + ? `<head><meta name="iroha-docs-revision" content="${options.revision}"></head>` |
| 26 | + : '' |
| 27 | + return `<html lang="en" dir="ltr">${revision}<body><div class="VPNavBarTranslations"><button aria-label="Change language"></button>${links}</div></body></html>` |
| 28 | +} |
| 29 | + |
| 30 | +async function writeLocaleFixture( |
| 31 | + root: string, |
| 32 | + publicPath: string, |
| 33 | + options: { omittedLocale?: DocsLocale; revision?: string; wrongLabelLocale?: DocsLocale } = {}, |
| 34 | +): Promise<void> { |
| 35 | + await Promise.all( |
| 36 | + ALL_LOCALES.map(async (locale) => { |
| 37 | + const directory = path.join(root, locale.path) |
| 38 | + await mkdir(directory, { recursive: true }) |
| 39 | + const html = |
| 40 | + locale === ROOT_LOCALE |
| 41 | + ? rootHtml(publicPath, options) |
| 42 | + : `<html lang="${locale.lang}" dir="${locale.direction}"><body></body></html>` |
| 43 | + await writeFile(path.join(directory, 'index.html'), html) |
| 44 | + }), |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +async function withFixture( |
| 49 | + publicPath: string, |
| 50 | + callback: (root: string) => Promise<void>, |
| 51 | + options: { omittedLocale?: DocsLocale; revision?: string; wrongLabelLocale?: DocsLocale } = {}, |
| 52 | +): Promise<void> { |
| 53 | + const root = await mkdtemp(path.join(tmpdir(), 'iroha-docs-built-locales-')) |
| 54 | + try { |
| 55 | + await writeLocaleFixture(root, publicPath, options) |
| 56 | + await callback(root) |
| 57 | + } finally { |
| 58 | + await rm(root, { recursive: true, force: true }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +describe('built locale validation', () => { |
| 63 | + test.each([ |
| 64 | + { option: undefined, builtPath: '/' }, |
| 65 | + { option: '/iroha-docs/', builtPath: '/iroha-docs/' }, |
| 66 | + { option: '/iroha-docs', builtPath: '/iroha-docs/' }, |
| 67 | + { option: 'iroha-docs', builtPath: '/iroha-docs/' }, |
| 68 | + ])('accepts a complete language selector for public path $option', async ({ option, builtPath }) => { |
| 69 | + await withFixture(builtPath, async (root) => { |
| 70 | + expect(await scanBuiltLocales({ root, publicPath: option })).toEqual([]) |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + test('reports a missing language selector', async () => { |
| 75 | + await withFixture('/', async (root) => { |
| 76 | + await writeFile(path.join(root, 'index.html'), '<html lang="en" dir="ltr"><body></body></html>') |
| 77 | + expect(await scanBuiltLocales({ root })).toContain('English locale index does not render the language selector') |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + test('accepts the expected deployment revision', async () => { |
| 82 | + await withFixture( |
| 83 | + '/', |
| 84 | + async (root) => { |
| 85 | + expect(await scanBuiltLocales({ root, revision: 'abc123' })).toEqual([]) |
| 86 | + }, |
| 87 | + { revision: 'abc123' }, |
| 88 | + ) |
| 89 | + }) |
| 90 | + |
| 91 | + test('reports a stale deployment revision', async () => { |
| 92 | + await withFixture( |
| 93 | + '/', |
| 94 | + async (root) => { |
| 95 | + expect(await scanBuiltLocales({ root, revision: 'new-sha' })).toContain( |
| 96 | + 'built revision is old-sha; expected new-sha', |
| 97 | + ) |
| 98 | + }, |
| 99 | + { revision: 'old-sha' }, |
| 100 | + ) |
| 101 | + }) |
| 102 | + |
| 103 | + test('reports a missing locale link', async () => { |
| 104 | + const japanese = ALL_LOCALES.find((locale) => locale.key === 'ja')! |
| 105 | + await withFixture( |
| 106 | + '/', |
| 107 | + async (root) => { |
| 108 | + expect(await scanBuiltLocales({ root })).toContain('language selector is missing 日本語: /ja/') |
| 109 | + }, |
| 110 | + { omittedLocale: japanese }, |
| 111 | + ) |
| 112 | + }) |
| 113 | + |
| 114 | + test('reports an empty language label', async () => { |
| 115 | + const french = ALL_LOCALES.find((locale) => locale.key === 'fr')! |
| 116 | + await withFixture( |
| 117 | + '/', |
| 118 | + async (root) => { |
| 119 | + expect(await scanBuiltLocales({ root })).toContain( |
| 120 | + 'language selector link /fr/ is labelled (empty); expected Français', |
| 121 | + ) |
| 122 | + }, |
| 123 | + { wrongLabelLocale: french }, |
| 124 | + ) |
| 125 | + }) |
| 126 | + |
| 127 | + test('reports a missing accessible button label', async () => { |
| 128 | + await withFixture('/', async (root) => { |
| 129 | + const html = rootHtml('/').replace(' aria-label="Change language"', '') |
| 130 | + await writeFile(path.join(root, 'index.html'), html) |
| 131 | + expect(await scanBuiltLocales({ root })).toContain( |
| 132 | + 'language selector is missing a non-empty accessible button label', |
| 133 | + ) |
| 134 | + }) |
| 135 | + }) |
| 136 | + |
| 137 | + test('reports a missing locale root once', async () => { |
| 138 | + const japanese = ALL_LOCALES.find((locale) => locale.key === 'ja')! |
| 139 | + await withFixture('/', async (root) => { |
| 140 | + await rm(path.join(root, japanese.path, 'index.html')) |
| 141 | + const issues = await scanBuiltLocales({ root }) |
| 142 | + expect(issues.filter((issue) => issue.includes('missing 日本語 locale index'))).toHaveLength(1) |
| 143 | + }) |
| 144 | + }) |
| 145 | + |
| 146 | + test('reports incorrect language metadata', async () => { |
| 147 | + const japanese = ALL_LOCALES.find((locale) => locale.key === 'ja')! |
| 148 | + await withFixture('/', async (root) => { |
| 149 | + await writeFile(path.join(root, japanese.path, 'index.html'), '<html lang="en" dir="ltr"></html>') |
| 150 | + expect(await scanBuiltLocales({ root })).toContain('日本語 locale index has lang=en; expected ja') |
| 151 | + }) |
| 152 | + }) |
| 153 | + |
| 154 | + test('reports a missing html element', async () => { |
| 155 | + const japanese = ALL_LOCALES.find((locale) => locale.key === 'ja')! |
| 156 | + await withFixture('/', async (root) => { |
| 157 | + await writeFile(path.join(root, japanese.path, 'index.html'), '<p>broken</p>') |
| 158 | + expect(await scanBuiltLocales({ root })).toContain('日本語 locale index has no html element') |
| 159 | + }) |
| 160 | + }) |
| 161 | + |
| 162 | + test('reports incorrect right-to-left metadata', async () => { |
| 163 | + const arabic = ALL_LOCALES.find((locale) => locale.key === 'ar')! |
| 164 | + await withFixture('/', async (root) => { |
| 165 | + await writeFile(path.join(root, arabic.path, 'index.html'), '<html lang="ar" dir="ltr"></html>') |
| 166 | + expect(await scanBuiltLocales({ root })).toContain('العربية locale index has dir=ltr; expected rtl') |
| 167 | + }) |
| 168 | + }) |
| 169 | +}) |
0 commit comments