|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Cleanup should report a damaged history as a damaged history. |
| 4 | +// |
| 5 | +// Before this, deleteRevisions() went straight into pad.check(), which |
| 6 | +// replays the whole history and dies on `assert(timestamp != null)` -- |
| 7 | +// an assertion about a null timestamp, when what the operator needs to |
| 8 | +// hear is "revision 600 is missing, here is what to do about it". The |
| 9 | +// reporter on #8134 had to bisect their database by hand. |
| 10 | + |
| 11 | +const assert = require('assert').strict; |
| 12 | +const common = require('../common'); |
| 13 | +const padManager = require('../../../node/db/PadManager'); |
| 14 | +const db = require('../../../node/db/DB'); |
| 15 | +const settings = require('../../../node/utils/Settings'); |
| 16 | +const {deleteRevisions, deleteAllRevisions} = require('../../../node/utils/Cleanup'); |
| 17 | + |
| 18 | +describe(__filename, function () { |
| 19 | + let padId: string; |
| 20 | + let cleanupEnabledBackup: boolean; |
| 21 | + |
| 22 | + before(async function () { |
| 23 | + await common.init(); |
| 24 | + cleanupEnabledBackup = settings.cleanup.enabled; |
| 25 | + settings.cleanup.enabled = true; |
| 26 | + }); |
| 27 | + |
| 28 | + after(function () { settings.cleanup.enabled = cleanupEnabledBackup; }); |
| 29 | + |
| 30 | + beforeEach(async function () { |
| 31 | + padId = common.randomString(); |
| 32 | + assert(!await padManager.doesPadExist(padId)); |
| 33 | + }); |
| 34 | + |
| 35 | + const padWithHoles = async (holes: number[], revs = 12) => { |
| 36 | + const pad = await padManager.getPad(padId); |
| 37 | + for (let i = 0; i < revs; i++) await pad.appendText(`line ${i}\n`); |
| 38 | + for (const h of holes) await db.remove(`pad:${padId}:revs:${h}`, null); |
| 39 | + padManager.unloadPad(padId); |
| 40 | + return await padManager.getPad(padId); |
| 41 | + }; |
| 42 | + |
| 43 | + describe('Pad.findMissingRevisions()', function () { |
| 44 | + it('returns [] for a healthy pad', async function () { |
| 45 | + const pad = await padWithHoles([]); |
| 46 | + assert.deepEqual(await pad.findMissingRevisions(), []); |
| 47 | + }); |
| 48 | + |
| 49 | + it('finds a single gap', async function () { |
| 50 | + const pad = await padWithHoles([3]); |
| 51 | + assert.deepEqual(await pad.findMissingRevisions(), [3]); |
| 52 | + }); |
| 53 | + |
| 54 | + it('finds several gaps, in ascending order', async function () { |
| 55 | + const pad = await padWithHoles([7, 2, 5]); |
| 56 | + assert.deepEqual(await pad.findMissingRevisions(), [2, 5, 7]); |
| 57 | + }); |
| 58 | + |
| 59 | + it('honours the limit', async function () { |
| 60 | + const pad = await padWithHoles([2, 3, 4, 5, 6]); |
| 61 | + const found = await pad.findMissingRevisions(2); |
| 62 | + assert.equal(found.length, 2); |
| 63 | + assert.deepEqual(found, [2, 3]); |
| 64 | + }); |
| 65 | + |
| 66 | + it('does not report revisions beyond head', async function () { |
| 67 | + const pad = await padWithHoles([]); |
| 68 | + const head = pad.getHeadRevisionNumber(); |
| 69 | + await db.remove(`pad:${padId}:revs:${head + 5}`, null); // no-op |
| 70 | + assert.deepEqual(await pad.findMissingRevisions(), []); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('deleteRevisions() on a damaged pad', function () { |
| 75 | + it('names the missing revision instead of asserting', async function () { |
| 76 | + await padWithHoles([3]); |
| 77 | + padManager.unloadPad(padId); |
| 78 | + const err: any = await deleteRevisions(padId, 2).then(() => null, (e: any) => e); |
| 79 | + assert.ok(err != null, 'expected deleteRevisions to throw'); |
| 80 | + assert.match(err.message, /missing revision\(s\) 3\b/); |
| 81 | + assert.match(err.message, new RegExp(padId)); |
| 82 | + // Not a bare assertion failure any more. |
| 83 | + assert.ok(!/timestamp != null/.test(err.message), |
| 84 | + `still surfacing the raw assertion:\n${err.message}`); |
| 85 | + }); |
| 86 | + |
| 87 | + it('tells the operator their text is safe and how to recover', |
| 88 | + async function () { |
| 89 | + await padWithHoles([3]); |
| 90 | + padManager.unloadPad(padId); |
| 91 | + const err: any = |
| 92 | + await deleteRevisions(padId, 2).then(() => null, (e: any) => e); |
| 93 | + assert.match(err.message, /current text is unaffected/i); |
| 94 | + assert.match(err.message, /compactPad/); |
| 95 | + }); |
| 96 | + |
| 97 | + it('lists multiple gaps', async function () { |
| 98 | + await padWithHoles([3, 6]); |
| 99 | + padManager.unloadPad(padId); |
| 100 | + const err: any = await deleteRevisions(padId, 2).then(() => null, (e: any) => e); |
| 101 | + assert.match(err.message, /missing revision\(s\) 3, 6/); |
| 102 | + }); |
| 103 | + |
| 104 | + it('leaves the damaged pad untouched', async function () { |
| 105 | + // The whole point of failing before the destructive phase. |
| 106 | + const pad = await padWithHoles([3]); |
| 107 | + const headBefore = pad.getHeadRevisionNumber(); |
| 108 | + const textBefore = pad.atext.text; |
| 109 | + padManager.unloadPad(padId); |
| 110 | + |
| 111 | + await deleteRevisions(padId, 2).catch(() => {}); |
| 112 | + |
| 113 | + padManager.unloadPad(padId); |
| 114 | + const after = await padManager.getPad(padId); |
| 115 | + assert.equal(after.getHeadRevisionNumber(), headBefore); |
| 116 | + assert.equal(after.atext.text, textBefore); |
| 117 | + }); |
| 118 | + |
| 119 | + it('still cleans up a healthy pad', async function () { |
| 120 | + const pad = await padWithHoles([]); |
| 121 | + padManager.unloadPad(padId); |
| 122 | + assert.equal(await deleteRevisions(padId, 3), true); |
| 123 | + padManager.unloadPad(padId); |
| 124 | + await (await padManager.getPad(padId)).check(); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('full compaction is still allowed', function () { |
| 129 | + it('deleteAllRevisions works on a damaged pad', async function () { |
| 130 | + // This is the recovery path the error message points at, so it must |
| 131 | + // not be gated behind the same check. |
| 132 | + const pad = await padWithHoles([3]); |
| 133 | + const textBefore = pad.atext.text; |
| 134 | + padManager.unloadPad(padId); |
| 135 | + |
| 136 | + await deleteAllRevisions(padId); |
| 137 | + |
| 138 | + padManager.unloadPad(padId); |
| 139 | + const after = await padManager.getPad(padId); |
| 140 | + // Compared trimmed: on develop, copyPadWithoutHistory still appends a |
| 141 | + // newline per copy (issue #8139, fixed by #8140), and this spec is |
| 142 | + // deliberately independent of that one. What matters here is that the |
| 143 | + // recovery path runs at all on a pad the keep-count path refuses. |
| 144 | + assert.equal(after.atext.text.trimEnd(), textBefore.trimEnd(), |
| 145 | + 'author-written text preserved'); |
| 146 | + assert.deepEqual(await after.findMissingRevisions(), [], |
| 147 | + 'history rebuilt without gaps'); |
| 148 | + }); |
| 149 | + }); |
| 150 | +}); |
0 commit comments