Files
wekan/tests/recoveryReportQuery.test.cjs
T
Lauri Ojansivu 63c77a27f4 Automatic recovery/remediation for the SQLite text data + Admin Panel Recovery report
Completes the #6492 data-safety system on the WeKan side (FerretDB detection/repair/cap
landed in the fork):

- Decision brain: models/lib/recoveryPlan.js decideRecovery() picks the least-invasive
  recovery when the database is KNOWN corrupt (latest good backup -> previous backup ->
  re-migrate from MongoDB -> manual). It never acts on a healthy/unknown database and
  never restores a known-bad backup.
- Startup restore/re-migrate: every FerretDB launch path (snap, bundled release, Docker)
  can restore the text data BEFORE FerretDB opens the files, on request
  (WEKAN_FORCE_RESTORE env or a RESTORE_REQUESTED marker). It copies a known-good backup
  INTO the live database (dropping stale WAL side-files); backups are never deleted and
  the main wekan.sqlite is only overwritten, never removed. remigrate is delegated to
  migration-control. Attachments/avatars on the filesystem are untouched.
- Recovery events: the scripts append JSON lines to recovery-events.jsonl; the server
  (server/recovery.js) imports them into the recoveryEvents collection on startup, plus
  an admin-only recordRecoveryEvent method.
- Admin Panel / Problems / Recovery: a new admin-only report
  (server/publications/recoveryReport.js, wired in adminReports.js/.jade + i18n) lists
  the remediation history newest-first, readies up front and fetches directly (no hang).
- Docs: docs/Features/Admin-Panel/Problems/Recovery.md.

Tests (wired into npm run test:unit:node), verified by mirror since there is no Node
runtime here to execute them:
  - recoveryPlan.test.cjs (decision logic, positive + negatives)
  - recoveryEventsJsonl.test.cjs (parser: skips junk, normalizes severity, never throws)
  - recoveryReportQuery.test.cjs (search selector escapes regex metacharacters)
  - recoveryReportWiring.test.cjs (report wired; publication/count/method admin-gated)
  - ferretdbTextDataBackup.test.cjs extended: restore copies from a backup, is
    request-gated, and NEVER deletes the live text data or a backup copy

Thanks to bluetopaz1204, mueschel and xet7 !

Fixes #6492,
2026-07-21 10:06:40 +03:00

37 lines
1.4 KiB
JavaScript

'use strict';
// Unit tests for the #6492 recovery report search selector (models/lib/recoveryReportQuery.js).
//
// Run: node tests/recoveryReportQuery.test.cjs
const assert = require('assert');
const { recoveryReportQuery, searchRegex } = require('../models/lib/recoveryReportQuery');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
console.log('recoveryReportQuery:');
test('empty term matches everything (no filter)', () => {
assert.deepStrictEqual(recoveryReportQuery(''), {});
assert.deepStrictEqual(recoveryReportQuery(null), {});
assert.deepStrictEqual(recoveryReportQuery(undefined), {});
});
test('a term searches type / detail / db (case-insensitive)', () => {
const q = recoveryReportQuery('backup');
assert.ok(Array.isArray(q.$or) && q.$or.length === 3, 'must be an $or over three fields');
const fields = q.$or.map(c => Object.keys(c)[0]).sort();
assert.deepStrictEqual(fields, ['db', 'detail', 'type']);
assert.ok(q.$or.every(c => Object.values(c)[0] instanceof RegExp), 'each is a regex');
assert.ok(q.$or[0].type.test('BACKUP-created'), 'case-insensitive');
});
test('NEGATIVE — regex metacharacters are escaped (matched literally)', () => {
const re = searchRegex('a.*b');
assert.ok(re.test('a.*b'), 'the literal string matches');
assert.ok(!re.test('axxb'), 'the metacharacters must NOT act as a wildcard');
});
console.log(`\n${passed} tests passed`);