Files
wekan/tests/rtl.test.js
T
Lauri Ojansivu b46eecf58b Fix the 20 node suites the && chain had been hiding.
The new runner ran all 260 suites for the first time: 20 failed. Four were real
defects in the app, the rest were guards pinning a spelling or a design that had
since changed - each of them written when it was true, and none of them run
since, because npm stopped the chain at the first failure.

The app fixes:

  * models/lib/themeAccents.js: accentOf('constructor') returned Object's
    constructor - a FUNCTION - because a plain lookup answers from
    Object.prototype. A board colour named after any prototype member would have
    written a function into a stylesheet. Only the map's own keys are themes now.
  * RTL: the dependency overlay and its connect handle each set a physical
    left/right AFTER the logical property, so the physical one won and both were
    pinned to the LTR side in an Arabic or Hebrew layout. The skip link, the
    avatar's account badge, the stats value column and the theme-category label
    were physical too. All logical now (inset-inline-*, text-align: start/end).

The guards, each corrected to what the code actually does now, with the reason:

  * addBoardTileHeight, publicationArgumentGuard: a fixed-size slice
    (jade + 3000, source + 1400) that a grown comment pushed the subject out of.
    Both now take the whole block they are about.
  * adminCheckboxStyle: the Grey Icons feature is gone; asserts its REMOVAL.
  * boardsVisibilitySettings: the separator count was pinned at 3; the site-theme
    picker group made it 4. Asserts the shape - one Save per section, none for
    the picker that applies immediately.
  * changelogFormat: "# Upcoming WeKan ® release" is a legitimate heading.
  * dbConformanceWiring: the log directory honours WEKAN_LOGDIR now.
  * domainTablePage: the default page is the app-wide 10, not 25.
  * ferretdbOplogGating: the exec runs under the CPU launcher.
  * leftMenu: peopleMenu takes the user (multitenancy option D).
  * loginEmailPanesMoved: the board-member restriction is two checkboxes in
    Organizations and Teams now; asserted there.
  * mobileAllBoardsFit: the bell and the avatar are two rules with variants.
  * narrowWindowLayout: the hamburger is the last button in the flow, not pinned.
  * rulesApiTriggerNormalize: #6491 made a missing matching field mean "any
    value", so the #2674 "can never match" case now matches - and removeMember
    iterates assignees, not members.
  * securityLog: the new-problems selector moved to models/lib/eventLogProblems.
  * tablePage: the Cards report renders the page the SERVER named.
  * tenantWiring: the site colour is read in two places; the autorun is the one.
  * themeAccents: see above - the guard was right, the code was not.
  * translationPane: the "no hand-written table" regex matched the +tablePage
    include it also requires.
  * uiDensity: admin table cells wrap in one td+th rule now.
  * rtl: the dir attribute comes from a helper, `!important` is not part of a
    value, and the scanner read CSS COMMENTS as declarations - the comment
    explaining why `left: 50%` needs no RTL variant was reported as a violation.

Thanks to xet7 !
2026-07-28 04:28:36 +03:00

223 lines
8.7 KiB
JavaScript

/**
* Test: right-to-left (RTL) language support.
*
* These are lightweight, dependency-free checks (Node `assert`, runnable both
* standalone via `node tests/rtl.test.js` and under Mocha) that lock in the RTL
* behaviour added for Arabic/Persian/Hebrew/etc.:
*
* 1. The set of languages flagged `rtl: true` in imports/i18n/languages.js is
* exactly the expected set (a guard against an accidental flag flip).
* 2. The direction helper logic (mirrors TAPi18n.getLanguageDirection) maps
* each language to 'rtl' / 'ltr' correctly.
* 3. The root <html> gets a reactive `dir`, and the client keeps it in sync.
* 4. Component CSS stays RTL-safe: it uses CSS logical properties
* (margin-inline-start, inset-inline-start, text-align:start, …) instead of
* physical left/right longhands, so every page mirrors when `dir` flips.
* The only allowed physical offsets are `left/right: 50%` centering tricks
* (paired with transform: translate, which already work in both directions).
*
* The visual/placement behaviour ("text is visible and on the correct side on
* every page") is covered by the browser test in
* tests/playwright/specs/18-rtl-layout.e2e.js.
*/
const assert = require('assert');
const fs = require('fs');
const path = require('path');
// Find the repo root by walking up from process.cwd()/PWD, NOT from the bare `__dirname`
// global: this file is also loaded by `meteor test`, where referencing `__dirname` makes
// Meteor inject a `const __dirname` that collides with the one its CommonJS module wrapper
// already provides, crashing the whole server bundle at boot with "Identifier '__dirname'
// has already been declared" (same fix as server/lib/tests/dependencies.openapi.tests.js).
function findRepoRoot(marker) {
const seeds = [process.cwd(), process.env.PWD, process.env.OLDPWD].filter(Boolean);
for (const seed of seeds) {
let dir = seed;
for (let i = 0; i < 12; i += 1) {
try { fs.accessSync(path.join(dir, marker)); return dir; } catch (e) { /* walk up */ }
const parent = path.dirname(dir);
if (parent === dir) break;
dir = parent;
}
}
return process.cwd();
}
const repoRoot = findRepoRoot('imports/i18n/languages.js');
const read = rel => fs.readFileSync(path.join(repoRoot, rel), 'utf8');
// --- Parse the rtl flags straight out of languages.js -----------------------
function parseRtlFlags() {
const src = read('imports/i18n/languages.js');
const flags = {};
// Each entry looks like: tag: "ar-EG", ... rtl: true,
const re = /tag:\s*"([^"]+)"[\s\S]*?rtl:\s*(true|false)/g;
let m;
while ((m = re.exec(src))) {
flags[m[1]] = m[2] === 'true';
}
return flags;
}
// Mirrors TAPi18n.getLanguageDirection(): rtl flag -> 'rtl' | 'ltr'.
const directionFor = isRtl => (isRtl ? 'rtl' : 'ltr');
// Recursively collect component .css files.
function cssFiles() {
const out = [];
const walk = dir => {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const p = path.join(dir, entry.name);
if (entry.isDirectory()) walk(p);
else if (entry.name.endsWith('.css')) out.push(p);
}
};
walk(path.join(repoRoot, 'client/components'));
return out;
}
// CSS comments, blanked (line structure kept, so line numbers still point at the
// right line). A comment is prose: it explains WHY a rule is written the way it
// is, and prose that says "`left: 50%` + a -50% shift centres the same way in
// both directions" is not a physical offset - it was reported as one.
const withoutComments = css =>
css.replace(/\/\*[\s\S]*?\*\//g, m => m.replace(/[^\n]/g, ' '));
const tests = [];
const test = (name, fn) => tests.push([name, fn]);
// Languages that must be written right-to-left.
const EXPECTED_RTL = [
'ar-DZ', 'ar-EG', 'ar', 'ary', // Arabic + variants
'fa-IR', 'fa', // Persian / Farsi
'he-IL', 'he', // Hebrew
'ug', // Uyghur
'uz-AR', // Uzbek (Arabic script)
'yi', // Yiddish
];
test('languages.js flags exactly the expected RTL languages', () => {
const flags = parseRtlFlags();
assert.ok(Object.keys(flags).length > 100, 'should parse the whole language table');
const actualRtl = Object.keys(flags).filter(tag => flags[tag]).sort();
assert.deepStrictEqual(
actualRtl,
[...EXPECTED_RTL].sort(),
'set of rtl:true languages changed unexpectedly',
);
});
test('direction helper maps rtl flag to the html dir value', () => {
const flags = parseRtlFlags();
// A few representatives from each script + a sanity LTR check.
assert.strictEqual(directionFor(flags['ar']), 'rtl');
assert.strictEqual(directionFor(flags['he']), 'rtl');
assert.strictEqual(directionFor(flags['fa-IR']), 'rtl');
assert.strictEqual(directionFor(flags['yi']), 'rtl');
assert.strictEqual(directionFor(flags['en']), 'ltr');
assert.strictEqual(directionFor(flags['zh-CN']), 'ltr');
// Unknown language defaults to ltr (languages[x] is undefined -> falsy).
assert.strictEqual(directionFor(flags['does-not-exist']), 'ltr');
});
test('tap.js exposes isRTL + getLanguageDirection', () => {
const src = read('imports/i18n/tap.js');
assert.ok(/isRTL\s*\(/.test(src), 'isRTL() missing from tap.js');
assert.ok(/getLanguageDirection\s*\(/.test(src), 'getLanguageDirection() missing from tap.js');
});
test('root <html> template carries a reactive dir attribute', () => {
// The attribute is fed by a template helper now - `dir="{{htmlDir}}"` with
// htmlDir() in layouts.js - rather than by a global TAPi18n path in the
// template. Both are reactive; only the helper can also give a fallback when
// the language has no direction yet.
const src = read('client/components/main/layouts.jade');
assert.ok(
/html\([^)]*dir="\{\{htmlDir\}\}"/.test(src),
'layouts.jade <html> is missing dir="{{htmlDir}}"',
);
const js = read('client/components/main/layouts.js');
assert.ok(
/htmlDir\(\) \{\s*\n\s*return TAPi18n\.getLanguageDirection\(\) \|\| 'ltr';/.test(js),
'htmlDir() must answer from the language direction, defaulting to ltr',
);
});
test('client keeps document dir in sync with the language', () => {
const src = read('client/lib/i18n.js');
assert.ok(
/document\.documentElement\.dir\s*=/.test(src),
'client/lib/i18n.js does not set document.documentElement.dir',
);
assert.ok(
/getLanguageDirection\s*\(/.test(src),
'client/lib/i18n.js should derive dir from getLanguageDirection()',
);
});
test('component CSS uses logical properties, not physical left/right longhands', () => {
const physical = /(^|[^-\w])(margin|padding|border)-(left|right)\b|text-align\s*:\s*(left|right)\b|float\s*:\s*(left|right)\b/;
const offenders = [];
for (const file of cssFiles()) {
const lines = withoutComments(read(path.relative(repoRoot, file))).split('\n');
lines.forEach((line, i) => {
if (physical.test(line)) {
offenders.push(`${path.relative(repoRoot, file)}:${i + 1}: ${line.trim()}`);
}
});
}
assert.deepStrictEqual(
offenders,
[],
`physical directional CSS found (use *-inline-start/-end, text-align:start/end, float:inline-start/end):\n${offenders.join('\n')}`,
);
});
test('bare left:/right: offsets are only the allowed 50% centering tricks', () => {
// Everything else was converted to inset-inline-start/-end. A leftover
// `left:`/`right:` is only acceptable as a 50% centering anchor (it pairs with
// transform: translate(-50%, …) and centers identically in LTR and RTL).
const offsetRe = /(^|[^-\w])(left|right)\s*:\s*([^;]+);/g;
const offenders = [];
for (const file of cssFiles()) {
const css = withoutComments(read(path.relative(repoRoot, file)));
let m;
while ((m = offsetRe.exec(css))) {
// `!important` is not part of the value for this purpose, and `auto` is the
// initial value: it only ever CANCELS a physical offset (the centred admin
// popups pair `left: 50%` with `right: auto`), so it cannot be a direction
// bug by itself.
const value = m[3].replace(/\s*!important\s*$/, '').trim();
if (value !== '50%' && value !== 'auto') {
offenders.push(`${path.relative(repoRoot, file)}: ${m[2]}: ${value}`);
}
}
}
assert.deepStrictEqual(
offenders,
[],
`unexpected physical left/right offsets (convert to inset-inline-start/-end):\n${offenders.join('\n')}`,
);
});
if (typeof describe === 'function') {
describe('RTL language support', () => {
tests.forEach(([name, fn]) => it(name, fn));
});
} else {
console.log('RTL language support tests\n');
let failed = 0;
tests.forEach(([name, fn]) => {
try {
fn();
console.log(`✓ ${name}`);
} catch (e) {
failed++;
console.log(`✗ ${name}\n ${e.message}`);
}
});
console.log(`\n${tests.length - failed}/${tests.length} passed`);
if (failed > 0) process.exitCode = 1;
}