Files
vue-journal-layer/tests/i18n.test.ts
Jakub Zych a4dca42d77 test(16-05): Journal layer util tests
- node --test over routes, client (fake fetcher, include_empty=1),
  pagination, htmlGate (Go FormatHTML parity cases), sitemap, seo and
  pl/en i18n parity
- package script test and a Testing line in the README
2026-10-09 20:03:46 +02:00

57 lines
1.9 KiB
TypeScript

// pl and en strings have the same keys and the same {name} placeholders,
// so a string added to one locale fails until the other has it too.
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
const LOCALES = join(dirname(fileURLToPath(import.meta.url)), '..', 'i18n', 'locales')
type Messages = { [key: string]: string | Messages }
function load(code: string): Messages {
return JSON.parse(readFileSync(join(LOCALES, `${code}.json`), 'utf8')) as Messages
}
// Every leaf string of the message tree, keyed by its dotted path.
function leaves(tree: Messages, prefix = ''): Array<[string, string]> {
const out: Array<[string, string]> = []
for (const [key, value] of Object.entries(tree)) {
const path = prefix ? `${prefix}.${key}` : key
if (typeof value === 'string') out.push([path, value])
else out.push(...leaves(value, path))
}
return out
}
function placeholders(value: string): string[] {
return [...value.matchAll(/\{([A-Za-z_][A-Za-z0-9_]*)\}/g)].map((m) => m[1]!).sort()
}
const pl = new Map(leaves(load('pl')))
const en = new Map(leaves(load('en')))
test('every string lives under the journal namespace', () => {
for (const key of [...pl.keys(), ...en.keys()]) assert.ok(key.startsWith('journal.'), key)
})
test('pl and en have identical leaf key sets', () => {
assert.deepEqual([...pl.keys()].sort(), [...en.keys()].sort())
assert.ok(pl.size > 0)
})
test('no leaf is empty', () => {
for (const [key, value] of [...pl, ...en]) assert.ok(value.trim() !== '', key)
})
test('pl and en use the same placeholders per key', () => {
for (const [key, value] of pl) {
assert.deepEqual(placeholders(value), placeholders(en.get(key) ?? ''), key)
}
})
test('the pagination title carries {page}', () => {
assert.deepEqual(placeholders(pl.get('journal.pagination.pageTitle') ?? ''), ['page'])
})