import assert from 'node:assert/strict'; import test from 'node:test'; import Delta from 'quill-delta'; import { ARTICLE_BODY_LIMIT, countArticleHtml, countRichText, limitRichTextChange, } from '../../src/components/common/ui/richTextLength.js'; import { articleErrors, emptyInsights, newArticle, } from '../../src/pages/WebsiteSettings/insightsModel.js'; const bounded = (old, change) => old.compose(new Delta(limitRichTextChange(old, change, ARTICLE_BODY_LIMIT))); const text = (delta) => delta.ops .map((op) => (typeof op.insert === 'string' ? op.insert : '')) .join(''); test('counts visible Unicode characters and entities, excluding whitespace, markup and images', () => { assert.equal(countRichText('中 A😀\n\t'), 3); assert.equal( countArticleHtml( '
中 &😀
',
),
3,
);
});
test('typing and long paste respect the limit without deleting text after the caret', () => {
const old = new Delta().insert('甲'.repeat(19998) + '尾\n');
const next = bounded(old, new Delta().retain(19998).insert('新增文字'));
assert.equal(text(next), '甲'.repeat(19998) + '新尾\n');
assert.equal(countRichText(text(next)), 20000);
assert.deepEqual(bounded(next, new Delta().retain(20000).insert('多')), next);
});
test('selection replacement, deletion, formatting, emoji and images remain usable at the limit', () => {
const old = new Delta().insert('甲'.repeat(20000) + '\n');
const next = bounded(
old,
new Delta()
.retain(10)
.delete(2)
.insert('😀乙丙', { bold: true })
.insert({ image: 'https://example.com/a.png' }),
);
assert.equal(countRichText(text(next)), 20000);
assert.ok(text(next).includes('😀乙'));
assert.ok(next.ops.some((op) => op.insert.image));
assert.ok(next.ops.some((op) => op.attributes?.bold));
assert.equal(countRichText(text(bounded(old, new Delta().delete(1)))), 19999);
});
test('article validation accepts 20000 characters and blocks oversized existing drafts', () => {
const article = {
...newArticle(),
title: '标题',
summary: '摘要',
body: `${'中'.repeat(20000)}
`, }; assert.equal(articleErrors(article, emptyInsights()).body, undefined); article.body = `${'中'.repeat(20001)}
`; assert.equal( articleErrors(article, emptyInsights()).body, '文章正文不能超过 20000 字', ); });