Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
import { describe, it, expect } from 'vitest'
import { readdirSync, readFileSync } from 'node:fs'
import { join, resolve, basename } from 'node:path'
/**
* The custom `t` returns the key itself when a lookup fails, so a missing entry
* renders `tools:toolApproval.title` at the user rather than throwing or falling
* back. That is invisible in review and in every other test, which is how five
* approval-dialog keys and two toast keys shipped broken.
*
* This walks every statically-written `t('ns:key')` in the app and asserts it
* resolves in the `en` locale, which is also the fallback for every other one.
*/
const SRC = resolve(__dirname, '../..')
const LOCALES = join(SRC, 'locales/en')
const loadEnglish = (): Record<string, unknown> =>
Object.fromEntries(
readdirSync(LOCALES)
.filter((f) => f.endsWith('.json'))
.map((f) => [
basename(f, '.json'),
JSON.parse(readFileSync(join(LOCALES, f), 'utf-8')),
])
)
const sourceFiles = (dir: string): string[] =>
readdirSync(dir, { withFileTypes: true }).flatMap((e) => {
const full = join(dir, e.name)
if (e.isDirectory()) {
return e.name === '__tests__' || e.name === 'node_modules'
? []
: sourceFiles(full)
}
return /\.tsx?$/.test(e.name) && !/\.test\.tsx?$/.test(e.name) ? [full] : []
})
const lookup = (
bundle: Record<string, unknown>,
ns: string,
key: string
): unknown => {
let cur: unknown = bundle[ns]
for (const part of key.split('.')) {
if (typeof cur !== 'object' || cur === null || !(part in cur)) return undefined
cur = (cur as Record<string, unknown>)[part]
}
return cur
}
/**
* A count-aware key is stored as `_one`/`_other` so i18next can pick the form,
* never under its own name. Without this the only way to satisfy the scan is to
* drop the namespace prefix and fall out of it entirely.
*/
const resolves = (
bundle: Record<string, unknown>,
ns: string,
key: string
): boolean =>
typeof lookup(bundle, ns, key) === 'string' ||
(typeof lookup(bundle, ns, `${key}_one`) === 'string' &&
typeof lookup(bundle, ns, `${key}_other`) === 'string')
// Only literal keys; dynamically-built ones can't be checked statically. The
// word boundary keeps calls such as `gatedEvent('ns:x')` from reading as `t(`.
const KEY_CALL = /\bt\(\s*['"]([a-zA-Z0-9_-]+):([a-zA-Z0-9_.]+)['"]/g
describe('i18n keys', () => {
it('resolves every statically-referenced key in the en locale', () => {
const bundle = loadEnglish()
const missing: string[] = []
for (const file of sourceFiles(SRC)) {
const text = readFileSync(file, 'utf-8')
for (const [, ns, key] of text.matchAll(KEY_CALL)) {
if (!resolves(bundle, ns, key)) {
missing.push(`${ns}:${key} (${file.slice(SRC.length + 1)})`)
}
}
}
expect(missing).toEqual([])
})
it('finds the locale bundle it is auditing', () => {
const bundle = loadEnglish()
expect(Object.keys(bundle).length).toBeGreaterThan(5)
expect(resolves(bundle, 'tools', 'toolApproval.title')).toBe(true)
})
})