Files
wekan/tests/cardsLoading.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

71 lines
3.1 KiB
JavaScript

'use strict';
// Plain-Node unit test (no Meteor) for the CARDS_LOADING / lazy card-loading
// helpers. Run: node tests/cardsLoading.test.cjs
//
// resolveCardsLoadingMode: the CARDS_LOADING env var / Admin Panel setting maps
// to exactly 'lazy' (only for "lazy") or 'all' (everything else) — a wrong value
// must NEVER silently enable the experimental lazy mode.
// parseCardsLoadingEnv: env is authoritative only when explicitly all|lazy.
// windowCountId: a falsy swimlaneId (undefined/null/'') MUST collapse to one id,
// because listBody subscribes with undefined while listHeader reads with '' —
// they must reference the same reactive count doc.
const assert = require('assert');
const {
resolveCardsLoadingMode,
parseCardsLoadingEnv,
windowCountId,
} = require('../models/lib/cardsLoading');
let passed = 0;
function test(name, fn) {
fn();
passed += 1;
console.log(' ok -', name);
}
// --- resolveCardsLoadingMode: POSITIVE (enables lazy) -----------------------
test('"lazy" -> lazy', () => assert.strictEqual(resolveCardsLoadingMode('lazy'), 'lazy'));
test('case/space-insensitive " LAZY " -> lazy', () =>
assert.strictEqual(resolveCardsLoadingMode(' LAZY '), 'lazy'));
// --- resolveCardsLoadingMode: NEGATIVE (defaults to all, never lazy) --------
test('"all" -> all', () => assert.strictEqual(resolveCardsLoadingMode('all'), 'all'));
test('empty string -> all', () => assert.strictEqual(resolveCardsLoadingMode(''), 'all'));
test('undefined -> all', () => assert.strictEqual(resolveCardsLoadingMode(undefined), 'all'));
test('null -> all', () => assert.strictEqual(resolveCardsLoadingMode(null), 'all'));
test('garbage never enables lazy', () => {
for (const v of ['true', '1', 'yes', 'Lazyload', 'l', 'lazyy', 'ALL', 42, {}]) {
assert.strictEqual(resolveCardsLoadingMode(v), 'all', `value ${JSON.stringify(v)}`);
}
});
// --- parseCardsLoadingEnv: only explicit all|lazy count ---------------------
test('parseCardsLoadingEnv("lazy") -> lazy', () =>
assert.strictEqual(parseCardsLoadingEnv('lazy'), 'lazy'));
test('parseCardsLoadingEnv("all") -> all', () =>
assert.strictEqual(parseCardsLoadingEnv('all'), 'all'));
test('parseCardsLoadingEnv unset/garbage -> undefined', () => {
for (const v of ['', undefined, null, 'true', 'nope']) {
assert.strictEqual(parseCardsLoadingEnv(v), undefined, `value ${JSON.stringify(v)}`);
}
});
// --- windowCountId ----------------------------------------------------------
test('undefined / null / "" swimlane collapse to the SAME id', () => {
const a = windowCountId('list1', undefined);
assert.strictEqual(a, windowCountId('list1', null));
assert.strictEqual(a, windowCountId('list1', ''));
assert.strictEqual(a, 'list1::');
});
test('a real swimlane id produces a distinct id', () => {
assert.strictEqual(windowCountId('list1', 'sw1'), 'list1::sw1');
assert.notStrictEqual(windowCountId('list1', 'sw1'), windowCountId('list1', undefined));
});
test('different lists never collide', () => {
assert.notStrictEqual(windowCountId('l1', 's1'), windowCountId('l2', 's1'));
});
console.log(`\ncardsLoading: ${passed} tests passed`);