BlueGit

projects-timfalken-com allianties

Files

allianties / test / i18n.test.js

i18n.test.js

i18n.test.js 2.3 KiB Download Raw
1'use strict';
2const { describe, it } = require('node:test');
3const assert = require('node:assert/strict');
4const catalog = require('../server/lib/i18n.json');
5const I18n = require('../src/i18n.js');
6
7const LOCALES = ['en', 'nl', 'de', 'fr', 'ja', 'zh', 'ko', 'pl', 'ru', 'es'];
8
9describe('i18n catalog', () =>
10{
11 it('lists every supported locale', () =>
12 {
13 assert.deepEqual(catalog.localeIds, LOCALES);
14 for (const loc of LOCALES)
15 { assert.ok(catalog.locales[loc], loc); }
16 assert.equal(I18n.LOCALES.length, 10);
17 assert.equal(I18n.DEFAULT, 'en');
18 });
19
20 it('has the same non-empty keys in every language', () =>
21 {
22 const enKeys = Object.keys(catalog.locales.en).sort();
23 assert.ok(enKeys.length > 80, 'enough strings: ' + enKeys.length);
24 for (const loc of LOCALES)
25 {
26 const keys = Object.keys(catalog.locales[loc]).sort();
27 assert.deepEqual(keys, enKeys, loc + ' keys differ from English');
28 for (const k of keys)
29 {
30 const v = catalog.locales[loc][k];
31 assert.equal(typeof v, 'string', loc + ' ' + k);
32 assert.ok(v.length > 0, 'empty ' + loc + ' ' + k);
33 }
34 }
35 });
36
37 it('keeps English placeholders in every translation', () =>
38 {
39 const ph = /\{(\w+)\}/g;
40 const en = catalog.locales.en;
41 for (const loc of LOCALES)
42 {
43 if (loc === 'en') { continue; }
44 for (const [k, ev] of Object.entries(en))
45 {
46 if (k.endsWith('Html')) { continue; }
47 const enPh = new Set((ev.match(ph) || []).sort());
48 const locPh = new Set((catalog.locales[loc][k].match(ph) || []).sort());
49 assert.deepEqual([...locPh].sort(), [...enPh].sort(), k + ' in ' + loc);
50 }
51 }
52 });
53
54 it('never translates the game name Allianties', () =>
55 {
56 for (const loc of LOCALES)
57 {
58 assert.equal(catalog.locales[loc]['app.name'], 'Allianties', loc);
59 assert.match(catalog.locales[loc]['auth.backToGame'], /Allianties/);
60 }
61 });
62
63 it('maps every known server error code', () =>
64 {
65 const codes = [
66 'login','csrf','not_host','min_players','not_in_game','dead','no_ap',
67 'occupied','impassable','out_of_range','invalid_move','game_started',
68 'game_finished','lobby_full','unknown_code','cannot_vote','bad_vote',
69 'bad_give','unknown_action','ceasefire','bad_grid','generic'
70 ];
71 for (const c of codes)
72 {
73 assert.equal(catalog.errors[c], 'error.' + c, c);
74 assert.ok(catalog.locales.en['error.' + c], 'en missing error.' + c);
75 }
76 });
77});