Files
urutau/scripts/check-ui-i18n.mjs
T
Lucas RayanandClaude Opus 5.5 b4a2c2c9ec
Release / Plan (reuse the published image?) (push) Successful in 4s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 1m29s
CI / Tests (Linux, race detector) (push) Failing after 1m48s
CI / Dependency audit (push) Successful in 21s
Release / ci (push) Failing after 1m50s
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Skipped
Release / Gitea release (push) Skipped
feat: Urutau 1.0.0, lightweight threat intelligence and web analytics
Always-on Go service (stdlib only) that follows the Traefik JSON access
log, the sshd log and the fail2ban log of a host, classifies attacks,
scores sources, keeps bounded aggregates with snapshots, and serves an
embedded dashboard with live updates, offline geolocation (DB-IP Lite),
privacy-friendly traffic analytics, an exportable blocklist and
token-authenticated federation between instances.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 16:13:34 -03:00

84 lines
3.7 KiB
JavaScript
Executable File

#!/usr/bin/env node
// Verifies the dashboard translations: both dictionaries (en, pt-BR) must have
// exactly the same keys, every key used by the HTML and the JS must exist, and
// the HTML must not carry visible text outside a data-i18n element.
//
// Usage: node scripts/check-ui-i18n.mjs
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import vm from 'node:vm';
const root = join(fileURLToPath(new URL('.', import.meta.url)), '..');
const web = join(root, 'internal/server/web');
const read = (f) => readFileSync(join(web, f), 'utf8');
const sandbox = { window: {} };
vm.runInNewContext(read('i18n.js'), sandbox);
const dict = sandbox.window.URUTAU_I18N;
const langs = Object.keys(dict);
const errors = [];
// duplicate keys silently collapse in an object literal: look at the source
for (const lang of langs) {
const block = read('i18n.js').split(new RegExp(`\\n\\s*['"]?${lang}['"]?:\\s*\\{`))[1] ?? '';
const seen = new Set();
for (const m of block.split(/\n\s*\},?\s*\n/)[0].matchAll(/^\s*'([^']+)':/gm)) {
if (seen.has(m[1])) errors.push(`duplicate key in ${lang}: ${m[1]}`);
seen.add(m[1]);
}
}
const all = new Set(langs.flatMap((l) => Object.keys(dict[l])));
for (const lang of langs) {
for (const k of all) if (!(k in dict[lang])) errors.push(`missing in ${lang}: ${k}`);
for (const [k, v] of Object.entries(dict[lang])) if (typeof v !== 'string' || !v.trim()) errors.push(`empty in ${lang}: ${k}`);
}
// placeholders must match across languages
for (const k of all) {
const ph = langs.map((l) => (dict[l][k]?.match(/\{n\}/g) ?? []).length);
if (new Set(ph).size > 1) errors.push(`placeholder mismatch: ${k}`);
}
// keys used
const used = new Set();
for (const f of ['index.html', 'login.html']) {
for (const m of read(f).matchAll(/data-i18n(?:-ph|-aria|-title)?="([^"]+)"/g)) used.add(m[1]);
}
for (const f of ['app.js', 'login.js']) {
for (const m of read(f).matchAll(/\bt\(\s*'([^']+)'/g)) used.add(m[1]);
for (const m of read(f).matchAll(/\[\s*'((?:host|kpi)\.[a-z]+)'\s*,/g)) used.add(m[1]);
}
// keys built at runtime
const families = {
'cat.': ['ok', 'notfound', 'scanner', 'exploit', 'bruteforce', 'ssh-fail', 'ssh-probe', 'ssh-ok', 'ban'],
'level.': ['all', 'hostile', 'suspicious', 'low'],
'device.': ['desktop', 'mobile', 'tablet', 'bot', 'tool', 'unknown'],
'state.': ['following', 'waiting', 'backfill', 'error', 'disabled'],
};
for (const [p, names] of Object.entries(families)) for (const n of names) used.add(p + n);
for (const k of used) if (!k.endsWith('.') && !all.has(k)) errors.push(`used but not defined: ${k}`);
for (const k of all) if (!used.has(k) && !k.startsWith('ago.') && !['live.on', 'live.off', 'feed.unban', 'yes', 'never', 'blocklist.copied', 'blocklist.failed', 'pager.count'].includes(k)) {
errors.push(`defined but unused: ${k}`);
}
// no visible text outside data-i18n in the HTML
for (const f of ['index.html', 'login.html']) {
const html = read(f).replace(/<script[\s\S]*?<\/script>/g, '').replace(/<!--[\s\S]*?-->/g, '').replace(/<title>[\s\S]*?<\/title>/g, '');
for (const m of html.matchAll(/<([a-z0-9]+)([^>]*)>([^<]+)</gi)) {
const text = m[3].replace(/&[a-z#0-9]+;/g, '').trim();
if (!/[A-Za-zÀ-ÿ]{2,}/.test(text)) continue;
if (/data-i18n="/.test(m[2])) continue;
if (['h1', 'button'].includes(m[1]) && /^(Urutau|EN|PT)$/.test(text)) continue;
if (m[1] === 'span' && /id="(ver|live-text)"/.test(m[2])) continue;
errors.push(`${f}: hard-coded text <${m[1]}>${text}`);
}
}
if (errors.length) {
console.error('UI i18n problems:\n ' + errors.join('\n '));
process.exit(1);
}
console.log(`ui i18n ok: ${all.size} keys in ${langs.join(', ')}`);