// Every error code the backend can return has a text in every language, using only // params the backend sends (local_backend/core/errors.py CODES). import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import { test } from 'node:test' const py = readFileSync(new URL('../../../local_backend/core/errors.py', import.meta.url), 'utf8') const start = py.indexOf('CODES: dict') const block = py.slice(start, py.indexOf('\n}\n', start)) const ENTRY = /^\s*"([\w.]+)":\s*\(\d+,\s*"((?:[^"\\]|\\.)*)"\)/gm const codes = Object.fromEntries([...block.matchAll(ENTRY)].map(m => [m[1], m[2]])) const flat = (o, p = '') => Object.entries(o).flatMap(([k, v]) => (typeof v === 'object' ? flat(v, `${p}${k}.`) : [[`${p}${k}`, v]])) const load = lang => Object.fromEntries(flat(JSON.parse(readFileSync(new URL(`../locales/${lang}/errors.json`, import.meta.url), 'utf8')))) test('the backend registry was read', () => { assert.ok(Object.keys(codes).length >= 40, String(Object.keys(codes).length)) assert.ok('order.not_found' in codes) }) for (const lang of ['en', 'el']) { test(`${lang}: every backend code has a text with backend params only`, () => { const texts = load(lang) const missing = Object.keys(codes).filter(c => !(c in texts)) assert.deepEqual(missing, []) const bad = Object.keys(codes).filter(c => { const params = new Set([...codes[c].matchAll(/\{(\w+)/g)].map(m => m[1])) return [...texts[c].matchAll(/\{\{\s*(\w+)/g)].some(m => !params.has(m[1])) }) assert.deepEqual(bad, [], 'placeholders the backend never sends') assert.ok(texts.generic && texts['network.offline']) }) }