Files
wekan/tests/calendarDateDisplay.test.cjs
T
Lauri Ojansivu 6d5f24ee27 Large board: read only the user fields each card and list needs.
Opening a card writes the user document (profile.cardLastViews, #3078). The
board view (Utils.storedBoardView and allowBoardView, which every list's card
loop reads) and the helpers every minicard runs read the WHOLE user document,
so that one write re-ran every list's card loop, and Blaze then re-evaluated
every minicard: 37,452 Tracker invalidations for one card open on a 40-card
board. Measured in the running app, it is now 390.

client/lib/currentUserWith.js reads the current user with only the named
fields; minimongo re-runs a field-limited query only when a projected field
changes. The board view, feature preview, unread comments, folds, drag
handles, label text, dependency layers, date format, week number, mobile mode
and the admin-only custom field check now use it. setCardLastViewed $sets one
entry instead of rewriting the whole map, which is also capped at 5,000.

Thanks to markusst1982 and xet7 !

Fixes #6745.
2026-10-07 03:08:48 +03:00

181 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const calendars = require('../imports/lib/calendarSystems');
const read = file => fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
(async () => {
const { formatDateByUserPreference } = await import('../imports/lib/dateUtils.js');
const { formatJalaliDate, gregorianToJalali } = await import('../imports/lib/jalaliDate.js');
let setting = {};
let profile = { calendar: 'gregorian', format: 'YYYY-MM-DD' };
let storage = { calendarSystem: 'jalali', dateFormat: 'DD-MM-YYYY' };
const currentUser = () => profile && ({
profile: { dateFormatOverride: profile.override !== false },
getCalendarSystem: () => profile.calendar,
getDateFormat: () => profile.format,
});
// #6745: dateDisplay.js reads only the date fields of the user
// (client/lib/currentUserWith.js); the stub records which ones.
const requestedFields = new Set();
const context = {
Utils: { getCurrentBoard: () => null },
ReactiveCache: { getCurrentSetting: () => setting, getCurrentUser: currentUser },
currentUserWith: fields => { fields.forEach(field => requestedFields.add(field)); return currentUser(); },
window: { localStorage: { getItem: key => storage[key] } },
TAPi18n: { getLanguage: () => 'en', __: key => key },
formatDateByUserPreference, formatJalaliDate, gregorianToJalali,
require: name => name.endsWith('dateFormatPolicy') ? require('../models/lib/dateFormatPolicy') : name.endsWith('calendarMonth') ? require('../imports/lib/calendarMonth') : calendars, Date, Intl,
};
const source = read('client/lib/dateDisplay.js');
vm.runInNewContext(source.replace(/^import .*;\n/gm, '').replace(/export function/g, 'function'), context);
const display = context.formatDateForDisplay;
const date = new Date(2026, 2, 21, 9, 5);
const timestamp = date.getTime();
for (const calendar of ['gregorian', 'jalali', 'buddhist']) {
for (const format of ['YYYY-MM-DD', 'DD-MM-YYYY', 'MM-DD-YYYY']) {
profile = { calendar, format };
const expected = display(date, false);
profile.format = `${format}-date-only`;
assert.equal(display(date, true), expected, 'date-only preserves the chosen calendar and order');
assert.equal(display(date, true, () => 'unexpected time'), expected, 'legacy format callbacks cannot restore time');
assert.equal(date.getTime(), timestamp, 'display preferences never change stored timestamps');
}
}
profile = { calendar: 'gregorian', format: 'YYYY-MM-DD' };
assert.deepEqual([...requestedFields].sort(),
['profile.calendarSystem', 'profile.dateFormat', 'profile.dateFormatOverride'],
'only the date fields, so a card open does not re-run every date badge');
assert.equal(display(date), '2026-03-21 09:05');
profile.override = false;
assert.equal(display(date, false, () => 'old Gregorian text'), 'old Gregorian text');
setting = { hideDateFormat: true, globalDateFormat: 'DD-MM-YYYY' };
assert.equal(display(date, false), '21-03-2026', 'admin overrides a saved user format');
assert.equal(display(date, false, () => 'old Gregorian text'), '21-03-2026', 'legacy display callbacks cannot bypass the override');
assert.equal(profile.format, 'YYYY-MM-DD', 'saved preference remains intact');
setting.hideDateFormat = false;
assert.equal(display(date, false), '2026-03-21', 'disabling restores the user preference');
profile.override = true;
profile.calendar = 'jalali';
assert.equal(display(date), '1405-01-01 09:05');
assert.equal(display(date, false, () => 'must not append Gregorian'), '1405-01-01');
profile.format = 'DD-MM-YYYY';
assert.equal(display(date, false), '01-01-1405');
assert.equal(date.getTime(), timestamp, 'display must not mutate stored dates');
for (const invalid of [null, undefined, '', 'invalid']) assert.equal(display(invalid), '');
assert.notEqual(display(new Date(5000, 0, 1), false, () => 'Gregorian must not appear'), 'Gregorian must not appear');
profile = null;
assert.equal(display(date, false), '1405-01-01', 'anonymous calendar is honored without a member override');
setting = { hideDateFormat: true, globalDateFormat: 'MM-DD-YYYY' };
assert.equal(display(date, false), '01-01-1405', 'global order also applies to anonymous calendars');
storage.calendarSystem = 'gregorian';
assert.equal(display(date, false), '03-21-2026', 'anonymous local preference is overridden');
setting.hideDateFormat = false;
context.window.localStorage.getItem = () => { throw new Error('storage blocked'); };
assert.equal(display(date, false), '2026-03-21');
profile = { calendar: 'gregorian', format: 'YYYY-MM-DD' };
assert.equal(Object.keys(context.calendarDateDisplayOptions()).length, 0);
profile.override = true;
profile.calendar = 'jalali';
const options = context.calendarDateDisplayOptions();
assert.equal(options.dayCellContent({ date }), '1');
assert.equal(options.dayHeaderContent({ date, view: { type: 'timeGridWeek' } }), '1405-01-01');
assert.equal(options.dayHeaderContent({ date, text: 'Sat', view: { type: 'dayGridMonth' } }), 'Sat');
assert.equal(options.titleFormat({ start: { year: 2026, month: 2, day: 21 } }), '1405-01-01');
assert.equal(options.titleFormat({ start: { year: 2026, month: 2, day: 21 },
end: { year: 2026, month: 2, day: 22 }, defaultSeparator: ' – ' }), '1405-01-01 – 1405-01-02');
assert.equal(options.listDaySideFormat(), '');
assert.equal(options.initialView, 'selectedCalendarMonth');
assert.equal(options.views.selectedCalendarMonth.type, 'dayGrid');
assert.equal(options.views.selectedCalendarMonth.duration, undefined,
'a fixed Gregorian duration must not override the chosen calendar range');
const range = options.views.selectedCalendarMonth.visibleRange(date);
assert.equal(display(range.start, false), '1405-01-01');
assert.equal(display(new Date(range.end.getTime() - 1), false), '1405-01-31');
assert.equal(options.views.selectedCalendarListMonth.type, 'list');
assert.equal(options.views.selectedCalendarListMonth.duration, undefined);
assert.equal(options.views.selectedCalendarListMonth.visibleRange(date).start.getTime(), range.start.getTime());
assert.equal(options.dayHeaderContent({ date, text: 'Sat', view: { type: 'selectedCalendarMonth' } }), 'Sat');
let moved;
const calendar = {
view: { type: 'selectedCalendarMonth' }, getDate: () => date,
gotoDate: value => { moved = value; },
changeView: value => { calendar.view.type = value; },
prev: () => { moved = 'native-prev'; }, next: () => { moved = 'native-next'; },
};
context.document = { getElementById: () => ({ _wekanCalendar: calendar }) };
options.customButtons.next.click();
assert.equal(display(moved, false), '1405-02-01');
options.customButtons.prev.click();
assert.equal(display(moved, false), '1404-12-29');
options.customButtons.listMonth.click();
assert.equal(calendar.view.type, 'selectedCalendarListMonth');
options.customButtons.next.click();
assert.equal(display(moved, false), '1405-02-01');
options.customButtons.dayGridMonth.click();
assert.equal(calendar.view.type, 'selectedCalendarMonth');
calendar.view.type = 'timeGridWeek';
options.customButtons.prev.click();
assert.equal(moved, 'native-prev', 'week navigation must retain native seven-day intervals');
options.customButtons.next.click();
assert.equal(moved, 'native-next');
const available = calendars.availableCalendarSystems();
assert.deepEqual(available.slice(0, 2).map(item => item.value), ['gregorian', 'jalali']);
for (const calendar of Intl.supportedValuesOf('calendar')) {
assert.ok(calendars.CALENDAR_SYSTEMS.some(item => item.intl === calendar), calendar);
}
for (const system of available.filter(item => !['gregorian', 'jalali'].includes(item.value))) {
profile.calendar = system.value;
const result = display(date, false);
const reference = calendars.formatNativeCalendarDate(date, system.intl, profile.format, false, 'en');
assert.equal(result, reference, system.value);
assert.ok(result, system.value);
}
profile.calendar = 'japanese';
assert.match(display(date, false), /Reiwa/);
profile.calendar = 'unknown';
assert.equal(display(date, false), '2026-03-21');
// Every previously missed display uses the shared policy. Input and storage
// utilities must not implicitly depend on a user's display calendar.
for (const file of ['cards/cardCustomFields.js', 'cards/checklists.js',
'notifications/notification.js', 'history/historyTable.js', 'cards/attachments.js',
'boards/boardsList.js', 'boards/timelineView.js', 'boards/originalPositionsView.js',
'boards/groupByAssigneeView.js', 'lists/listHeader.js', 'cards/labels.js', 'settings/adminProblems.js',
'settings/peopleBody.js', 'settings/attachments.js', 'gantt/gantt.js',
'gantt/frappeGantt.js', 'gantt/dhtmlxGantt.js']) {
assert.match(read('client/components/' + file), /formatDateForDisplay\(/, file);
}
assert.match(read('client/config/blazeHelpers.js'), /formatDateForDisplay\(date/);
for (const file of ['client/lib/datepicker.js', 'imports/lib/dateUtils.js']) {
assert.doesNotMatch(read(file), /dateDisplay|calendarSystems|formatJalaliDate/);
}
for (const file of ['client/components/cards/cardDate.js', 'client/components/cards/checklists.js',
'client/components/cards/cardCustomFields.js']) assert.match(read(file), /date.get\(\).toISOString\(\)/);
assert.match(read('models/users.js'), /allowedValues: CALENDAR_SYSTEM_IDS/);
assert.match(read('server/models/users.js'), /CALENDAR_SYSTEM_IDS.includes\(calendarSystem\)/);
assert.match(read('server/models/users.js'), /return await user.setCalendarSystem\(calendarSystem\)/);
assert.match(read('client/components/forms/dateFormatSettings.js'), /availableCalendarSystems\(\)/);
const english = JSON.parse(read('imports/i18n/data/en.i18n.json'));
for (const system of calendars.CALENDAR_SYSTEMS) assert.ok(english[system.labelKey]);
for (const file of fs.readdirSync(path.join(__dirname, '../imports/i18n/data')).filter(file => file.endsWith('.i18n.json'))) {
const locale = JSON.parse(read('imports/i18n/data/' + file));
assert.deepEqual(Object.keys(locale), Object.keys(english), file + ' key order');
for (const system of calendars.CALENDAR_SYSTEMS) {
assert.ok(locale[system.labelKey], file + system.labelKey);
const tokens = text => text.match(/__[A-Za-z0-9_]+__|%\d*\$?[sdif]/g) || [];
assert.deepEqual(tokens(locale[system.labelKey]), tokens(english[system.labelKey]));
}
}
console.log('calendarDateDisplay: all calendars, preferences, display coverage and Gregorian storage checks passed');
})().catch(error => { console.error(error); process.exitCode = 1; });