Files
monkey-ai-web/test/unit/website-settings-rich-text-length.test.js
zhanjunhao 2aa9d73325 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

72 lines
2.4 KiB
JavaScript

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(
'<p>中&nbsp;&amp;&#x1f600; </p><img src="https://example.com/a.png">',
),
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: `<p>${'中'.repeat(20000)}</p>`,
};
assert.equal(articleErrors(article, emptyInsights()).body, undefined);
article.body = `<p>${'中'.repeat(20001)}</p>`;
assert.equal(
articleErrors(article, emptyInsights()).body,
'文章正文不能超过 20000 字',
);
});