Files
wekan/tests/errorMessageI18n.test.cjs
T
Lauri Ojansivu 0fff9d4828 Board view fixes, and failed calls reported in the reader's language.
- Calendar, Calendar Mode and Multi Board Calendar showed archived cards
  once the Archive sidebar had loaded them; every calendar selector leaves
  them out now.
- Timeline offered "Restore to this state" to every viewer, and a refused
  restore vanished, since nothing awaited it. The button is shown to those
  who may change the card, and a refusal is reported.
- Table: a swimlane header row spanned a fixed 11 columns, one short with
  all four date columns and too wide on a phone; it spans the columns the
  table has. Its add-card error was an English sentence.
- Map showed the raw "upload-failed" for a failed upload; it is translated.
- client/lib/errorMessage.js turns a failed call into a message in the
  reader's language: the translation when the error code is a translation
  key, otherwise the translated "Something went wrong" with the server's
  reason after it. 25 alert() calls with their own English fallback use it.

tests/errorMessageI18n.test.cjs runs the helper and fails on an alert()
that falls back to English again; calendarFilter tests the archived cards
for all four selectors.

Thanks to xet7 !
2026-10-17 01:01:34 +03:00

91 lines
4.1 KiB
JavaScript

'use strict';
// A failed call is reported in the reader's language. client/lib/errorMessage.js
// translates a Meteor.Error whose code is a translation key, and otherwise shows
// the translated "Something went wrong" with the server's reason after it.
// Before it, 25 alert() calls fell back to their own English sentence, the Map
// view showed the raw "upload-failed", and the Table view's add-card form an
// English "Could not add the card.".
//
// The negative sweep fails on any alert() under client/ that falls back to an
// English literal again.
//
// Run: node tests/errorMessageI18n.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const vm = require('vm');
const ROOT = path.join(__dirname, '..');
const read = rel => fs.readFileSync(path.join(ROOT, rel), 'utf8');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
function loadErrorMessage(translations) {
const src = read('client/lib/errorMessage.js')
.replace(/^import \{ TAPi18n \} from '\/imports\/i18n';$/m, '')
.replace(/^export function/m, 'function');
const context = { TAPi18n: { __: key => translations[key] || key } };
vm.runInNewContext(`${src}\nthis.errorMessage = errorMessage;`, context);
return context.errorMessage;
}
const errorMessage = loadErrorMessage({
'error-undefined': 'Jokin meni vikaan',
'error-notAllowed': 'Roolisi ei salli tätä',
});
console.log('errorMessageI18n:');
test('an error code that is a translation key is shown translated', () => {
assert.strictEqual(errorMessage({ error: 'error-notAllowed', reason: 'Not allowed' }), 'Roolisi ei salli tätä');
});
test('anything else is the translated generic message, with the reason for the admin', () => {
assert.strictEqual(errorMessage({ error: 'not-found', reason: 'List not found' }), 'Jokin meni vikaan (List not found)');
assert.strictEqual(errorMessage(new Error('boom')), 'Jokin meni vikaan (boom)');
assert.strictEqual(errorMessage(undefined), 'Jokin meni vikaan');
});
test('the Map, Table and Timeline views report failures translated', () => {
const mapJs = read('client/components/boards/mapView.js');
assert.ok(!/err\.reason \|\| err\.message \|\| 'upload-failed'/.test(mapJs), 'no raw reason or key in the map upload');
assert.match(read('client/components/boards/mapView.jade'), /p\.warning \{\{_ error\}\}/);
const tableJs = read('client/components/boards/tableView.js');
assert.ok(!tableJs.includes('Could not add the card.'));
assert.match(tableJs, /alert\(TAPi18n\.__\(key\)\)/);
const timelineJs = read('client/components/boards/timelineView.js');
assert.match(timelineJs, /restoreCardFromTimeline\(cardContext\.cardId, cardContext\.fields\)\.catch\(/,
'a refused restore is reported, not dropped');
assert.match(timelineJs, /canRestoreCard\(cardId\) \{[\s\S]*?Utils\.canModifyCard\(card\)/);
assert.match(read('client/components/boards/timelineView.jade'), /if canRestoreCard this\.cardId\s*\n\s*button\.timeline-card-restore/,
'only somebody who may change the card is offered the restore');
});
test('a Table swimlane header row spans the columns the table has', () => {
assert.match(read('client/components/boards/tableView.jade'), /th\(colspan=columnCount scope="rowgroup"\)/);
const js = read('client/components/boards/tableView.js');
assert.match(js, /columnCount\(\) \{[\s\S]*?if \(Utils\.isMiniScreen\(\)\) return 4;[\s\S]*?return 7 \+ dates;/);
});
test('no alert() under client/ falls back to an English sentence (negative)', () => {
const found = [];
const walk = dir => {
for (const e of fs.readdirSync(path.join(ROOT, dir), { withFileTypes: true })) {
const rel = path.join(dir, e.name);
if (e.isDirectory()) walk(rel);
else if (rel.endsWith('.js')) {
read(rel).split('\n').forEach((line, i) => {
if (/alert\([^)]*\|\|\s*['"][A-Z][a-z]/.test(line)) found.push(`${rel}:${i + 1}`);
});
}
}
};
walk('client');
assert.deepStrictEqual(found, [], 'use errorMessage(error) from client/lib/errorMessage.js');
});
console.log(`\nerrorMessageI18n: ${passed} tests passed`);