Files
wekan/tests/backupPaths.test.cjs
T
xet7 d2ec3ee400 Unit + negative tests for newest features (lazy loading, streaming export, backup)
Extract the pure, security- and correctness-critical logic of the recent features
into Meteor-free models/lib/* modules (so production and tests share one
implementation) and add plain-Node tests with positive AND negative cases, wired
into test:unit:node:

- models/lib/mongoSelectorSafety.js hasWhere() — the windowed card publication
  refuses a client selector carrying $where (server-side JS). Tests: safe
  selectors pass; $where at top level / in $and / deep in arrays / in $elemMatch
  is rejected. (was inline in server/publications/cardsWindow.js)
- models/lib/cardsLoading.js resolveCardsLoadingMode / parseCardsLoadingEnv /
  windowCountId — CARDS_LOADING maps to 'lazy' only for exactly "lazy" (garbage
  never enables the experimental mode); a falsy swimlane id collapses to one
  count-doc id (listBody uses undefined, listHeader uses ''). (used by
  server/cards-loading.js, server/models/settings.js, client/lib/lazyCards.js)
- models/lib/base64Chunk.js encodeAligned/encodeFinal — streaming an attachment
  into the JSON export as base64 in any chunk sizes reconstructs exactly the
  whole-file base64; aligned pieces never carry padding. (used by
  Exporter.buildStream)
- models/lib/backupPaths.js filesRootFrom/scheduleText — WRITABLE_PATH -> files
  root (no double /files suffix, Windows-safe) and the synced-cron schedule text
  (daily/weekly/monthly + defaults, unknown frequency falls back to daily). (used
  by server/methods/backup.js)

41 new assertions across 4 files, all passing; whole test:unit:node suite green.
2026-07-11 00:14:49 +03:00

69 lines
2.5 KiB
JavaScript

'use strict';
// Plain-Node unit test (no Meteor) for the Admin Panel / Attachments / Backup
// path + schedule helpers. Run: node tests/backupPaths.test.cjs
const assert = require('assert');
const { filesRootFrom, scheduleText } = require('../models/lib/backupPaths');
let passed = 0;
function test(name, fn) {
fn();
passed += 1;
console.log(' ok -', name);
}
// --- filesRootFrom ----------------------------------------------------------
test('docker/dev base gets /files appended', () => {
assert.strictEqual(filesRootFrom('/data'), '/data/files');
assert.strictEqual(filesRootFrom('/srv/wekan'), '/srv/wekan/files');
});
test('snap base already ending in /files is left unchanged (no double suffix)', () => {
assert.strictEqual(filesRootFrom('/var/snap/wekan/common/files'), '/var/snap/wekan/common/files');
});
test('Windows base ending in \\files is left unchanged', () => {
assert.strictEqual(filesRootFrom('C:\\wekan\\files'), 'C:\\wekan\\files');
});
test('empty base does not crash', () => {
assert.strictEqual(filesRootFrom(''), 'files');
assert.strictEqual(filesRootFrom(undefined), 'files');
});
// --- scheduleText -----------------------------------------------------------
test('daily', () => {
assert.strictEqual(scheduleText({ frequency: 'daily', time: '04:00' }), 'every day at 04:00');
});
test('weekly uses the given day', () => {
assert.strictEqual(
scheduleText({ frequency: 'weekly', dayOfWeek: 'Monday', time: '01:00' }),
'on Monday at 01:00',
);
});
test('weekly defaults to Sunday', () => {
assert.strictEqual(scheduleText({ frequency: 'weekly', time: '02:30' }), 'on Sunday at 02:30');
});
test('monthly uses the given day-of-month', () => {
assert.strictEqual(
scheduleText({ frequency: 'monthly', dayOfMonth: 15, time: '03:00' }),
'on the 15 day of the month at 03:00',
);
});
test('monthly defaults to the 1st', () => {
assert.strictEqual(
scheduleText({ frequency: 'monthly', time: '03:00' }),
'on the 1 day of the month at 03:00',
);
});
test('time defaults to 04:00 when missing', () => {
assert.strictEqual(scheduleText({ frequency: 'daily' }), 'every day at 04:00');
});
// --- NEGATIVE: unknown / absent frequency falls back to the daily form ------
test('unknown frequency falls back to daily', () => {
assert.strictEqual(scheduleText({ frequency: 'off', time: '05:00' }), 'every day at 05:00');
assert.strictEqual(scheduleText({ time: '06:00' }), 'every day at 06:00');
assert.strictEqual(scheduleText({}), 'every day at 04:00');
});
console.log(`\nbackupPaths: ${passed} tests passed`);