Files
wekan/client/lib/lazyCards.js
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

57 lines
2.7 KiB
JavaScript

import { Mongo } from 'meteor/mongo';
import { ReactiveCache } from '/imports/reactiveCache';
import { Filter } from '/client/lib/filter';
import { listCardsSelector } from '/models/lib/swimlaneFilter';
const { windowCountId: windowCountIdImpl, resolveCardsLoadingMode } = require('/models/lib/cardsLoading');
// Client-only mirror of the server `boardListCardCounts` docs published by the
// 'boardListCardCount' publication (one doc per list/swimlane window, holding
// the total card count so the board knows whether more cards remain to scroll
// in). Only used in lazy card-loading mode.
export const BoardListCardCounts = new Mongo.Collection('boardListCardCounts');
// Effective card-loading mode. Prefers the admin Setting doc (reactive, so an
// Admin Panel change is picked up), falls back to the public env value, then to
// 'all'. See server/cards-loading.js and Admin Panel / Features.
export function cardsLoadingMode() {
const setting = ReactiveCache.getCurrentSetting();
const fromDoc = setting && setting.cardsLoading;
const fromEnv =
Meteor.settings && Meteor.settings.public && Meteor.settings.public.cardsLoading;
return resolveCardsLoadingMode(fromDoc || fromEnv);
}
export function isLazyCards() {
return cardsLoadingMode() === 'lazy';
}
// Stable id for one (list, swimlane) window's count doc (shared, unit-tested).
export function windowCountId(listId, swimlaneId) {
return windowCountIdImpl(listId, swimlaneId);
}
// Accurate total card count for `list.cards(swimlaneId)` in lazy mode, where
// minimongo holds only the loaded window. Builds the SAME selector
// list.cards(swimlaneId) uses (listCardsSelector + the board Filter), subscribes
// (deduped) to the server-side count for it, and returns that count. Returns null
// when not in lazy mode or the count has not arrived yet, so callers can fall
// back to the loaded-window length. Because the selector is identical to the one
// list.cards() runs against minimongo, the server count is exactly the accurate
// version of whatever list.cards(swimlaneId).length would report.
export function lazyListCardCount(list, swimlaneId) {
if (!isLazyCards() || !list || !list.boardId) return null;
const selector = listCardsSelector(
list._id,
swimlaneId,
list.orphanedCardsSwimlaneIds ? list.orphanedCardsSwimlaneIds(swimlaneId) : undefined,
);
const mongoSelector = Filter.mongoSelector(selector);
const id = windowCountId(list._id, swimlaneId);
const inst = typeof Template !== 'undefined' && Template.instance ? Template.instance() : null;
if (inst && inst.subscribe) {
inst.subscribe('boardListCardCount', id, list.boardId, mongoSelector);
}
const doc = BoardListCardCounts.findOne(id);
return doc ? doc.count : null;
}