Files
wekan/tests/commentThreadGrouping.test.cjs
T
Lauri Ojansivu ab1f60d806 Cap comment replies at one level of threading, grouped under their parent.
A comment's Reply link (added earlier for #5907) let a reply be posted
against ANY comment, including another reply, so clicking Reply enough
times nested comments arbitrarily deep. For #3011, a reply now attaches
to a reply's own parent instead of the reply itself - resolveParentId()
(models/cardComments.js) makes that decision once, is unit tested, and
is applied both when the client opens the composer (so the "In reply
to ..." banner already names the right comment) and again in a
CardComments.before.insert hook server-side, so the cap holds no
matter how a comment is inserted.

Replies also now render directly under their top-level parent instead
of interleaved by date with unrelated comments: groupCommentsByThread()
(imports/lib/commentThreading.js) reorders the already-sorted flat list
the comments template already used, with no schema or query change.

The "Replying to <author>" indicator on the composer reuses the
existing, already fully translated comment-in-reply-to string plus the
parent comment's author name, rather than adding a new i18n key that
would need translating across every locale file for a small wording
difference.

Added tests/commentReplyFlattening.test.cjs (the one-level cap, both
the client and server wiring) and tests/commentThreadGrouping.test.cjs
(grouping, including the regression cases: a flat list of only
top-level comments is unchanged, and a comment whose parent was
deleted still renders as top-level rather than being dropped).

Thanks to FuXXz and xet7 !

Closes #3011,
2026-09-10 20:28:46 +03:00

107 lines
3.5 KiB
JavaScript

'use strict';
(async () => {
// Issue #3011: replies render directly under their parent within the same
// flat comments list, instead of interleaved by date with unrelated
// top-level comments. groupCommentsByThread() (imports/lib/commentThreading.js)
// is the pure reordering step the `comments` Blaze template's getComments()
// helper applies (client/components/activities/comments.js); it has no
// Meteor imports, so it is imported and run directly here.
//
// Run: node tests/commentThreadGrouping.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { groupCommentsByThread } = await import(
'../imports/lib/commentThreading.js'
);
const repoRoot = path.resolve(__dirname, '..');
const clientComments = fs.readFileSync(
path.join(repoRoot, 'client/components/activities/comments.js'),
'utf8',
);
let passed = 0;
function test(name, fn) {
fn();
passed += 1;
console.log(' ok -', name);
}
// --------------------------------------------------------------- POSITIVE
test('a reply is moved to sit directly after its top-level parent', () => {
// Newest-first order, as the real query sorts (createdAt: -1) - the reply
// to "top-older" ends up newest here, so without grouping it would sit
// ahead of "top-older" rather than under it.
const input = [
{ _id: 'reply-to-older', parentId: 'top-older' },
{ _id: 'top-newer', parentId: '' },
{ _id: 'top-older', parentId: '' },
];
const grouped = groupCommentsByThread(input);
assert.deepStrictEqual(
grouped.map(c => c._id),
['top-newer', 'top-older', 'reply-to-older'],
);
});
test('multiple replies to the same parent all group under it, in list order', () => {
const input = [
{ _id: 'r2', parentId: 'top' },
{ _id: 'r1', parentId: 'top' },
{ _id: 'top', parentId: '' },
];
const grouped = groupCommentsByThread(input);
assert.deepStrictEqual(
grouped.map(c => c._id),
['top', 'r2', 'r1'],
);
});
test('a reply whose parent is no longer in the list falls back to top-level', () => {
const input = [{ _id: 'orphan', parentId: 'deleted-parent' }];
const grouped = groupCommentsByThread(input);
assert.deepStrictEqual(grouped.map(c => c._id), ['orphan']);
});
test('a non-array input returns an empty list rather than throwing', () => {
assert.deepStrictEqual(groupCommentsByThread(undefined), []);
assert.deepStrictEqual(groupCommentsByThread(null), []);
});
// -------------------------------------------------------- NEGATIVE/REGRESSION
test('a plain flat list of only top-level comments is left in the same order', () => {
const input = [
{ _id: 'c3', parentId: '' },
{ _id: 'c2', parentId: '' },
{ _id: 'c1', parentId: '' },
];
const grouped = groupCommentsByThread(input);
assert.deepStrictEqual(grouped.map(c => c._id), ['c3', 'c2', 'c1']);
assert.strictEqual(grouped.length, input.length);
});
test('comments missing parentId altogether (legacy docs) are unaffected', () => {
const input = [{ _id: 'legacy1' }, { _id: 'legacy2' }];
const grouped = groupCommentsByThread(input);
assert.deepStrictEqual(grouped.map(c => c._id), ['legacy1', 'legacy2']);
});
// ----------------------------------------------------- wiring (regression)
test('the comments template groups through groupCommentsByThread', () => {
assert.ok(
/getComments\(\) \{[\s\S]*?groupCommentsByThread\(data\.comments\(\)\)/.test(
clientComments,
),
);
});
console.log(`\ncommentThreadGrouping: ${passed} passed`);
})();