Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✅ test(editor-runtime): 按 @lobehub/editor 7.x 更新用例,并纳入 CI (#22643)

Mocha4b0d06a8cc24
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 3ae6f519b96..1829b0e6729 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -45,7 +45,7 @@ jobs:
runs-on: ubuntu-latest
name: Test Packages
env:
- PACKAGES: '@lobechat/file-loaders @lobechat/prompts @lobechat/model-runtime @lobechat/web-crawler @lobechat/electron-server-ipc @lobechat/utils @lobechat/context-engine @lobechat/agent-runtime @lobechat/conversation-flow @lobechat/ssrf-safe-fetch @lobechat/memory-user-memory @lobechat/types @lobechat/trpc @lobechat/app-config @lobechat/locales @lobechat/env @lobechat/builtin-tool-lobe-agent @lobechat/builtin-tool-message model-bank @lobechat/agent-gateway-client @lobechat/agent-manager-runtime @lobechat/device-gateway-client @lobechat/device-identity @lobechat/eval-dataset-parser @lobechat/eval-rubric @lobechat/fetch-sse @lobechat/heterogeneous-agents @lobechat/openapi'
+ PACKAGES: '@lobechat/file-loaders @lobechat/prompts @lobechat/model-runtime @lobechat/web-crawler @lobechat/electron-server-ipc @lobechat/utils @lobechat/context-engine @lobechat/agent-runtime @lobechat/conversation-flow @lobechat/editor-runtime @lobechat/ssrf-safe-fetch @lobechat/memory-user-memory @lobechat/types @lobechat/trpc @lobechat/app-config @lobechat/locales @lobechat/env @lobechat/builtin-tool-lobe-agent @lobechat/builtin-tool-message model-bank @lobechat/agent-gateway-client @lobechat/agent-manager-runtime @lobechat/device-gateway-client @lobechat/device-identity @lobechat/eval-dataset-parser @lobechat/eval-rubric @lobechat/fetch-sse @lobechat/heterogeneous-agents @lobechat/openapi'
steps:
- name: Checkout
diff --git a/packages/editor-runtime/package.json b/packages/editor-runtime/package.json
index d6e9fa7ef17..cc26b731c22 100644
--- a/packages/editor-runtime/package.json
+++ b/packages/editor-runtime/package.json
@@ -6,6 +6,11 @@
".": "./src/index.ts"
},
"main": "./src/index.ts",
+ "scripts": {
+ "test": "vitest",
+ "test:coverage": "vitest --coverage --silent='passed-only'",
+ "test:update": "vitest -u"
+ },
"dependencies": {
"@lobechat/prompts": "workspace:*"
},
diff --git a/packages/editor-runtime/src/__tests__/EditorRuntime.real.test.ts b/packages/editor-runtime/src/__tests__/EditorRuntime.real.test.ts
index aacff3865da..34d0ae2055d 100644
--- a/packages/editor-runtime/src/__tests__/EditorRuntime.real.test.ts
+++ b/packages/editor-runtime/src/__tests__/EditorRuntime.real.test.ts
@@ -148,13 +148,13 @@ describe('EditorRuntime - Real Cases', () => {
const result = await runtime.modifyNodes({
operations: [
- { action: 'remove', id: 'wps3' },
- { action: 'remove', id: 'w936' },
- { action: 'remove', id: 'vse9' },
- { action: 'remove', id: 'sp45' },
- { action: 'remove', id: 's8f8' },
- { action: 'remove', id: 'rrqb' },
- { action: 'remove', id: 'plu1' },
+ { action: 'remove', id: '73' },
+ { action: 'remove', id: '70' },
+ { action: 'remove', id: '67' },
+ { action: 'remove', id: '47' },
+ { action: 'remove', id: '44' },
+ { action: 'remove', id: '41' },
+ { action: 'remove', id: '27' },
],
});
await moment();
@@ -169,13 +169,13 @@ describe('EditorRuntime - Real Cases', () => {
const xmlAfter = editor.getDocument('litexml') as unknown as string;
// Verify the removed IDs are no longer present
- expect(xmlAfter).not.toContain('id="wps3"');
- expect(xmlAfter).not.toContain('id="w936"');
- expect(xmlAfter).not.toContain('id="vse9"');
- expect(xmlAfter).not.toContain('id="sp45"');
- expect(xmlAfter).not.toContain('id="s8f8"');
- expect(xmlAfter).not.toContain('id="rrqb"');
- expect(xmlAfter).not.toContain('id="plu1"');
+ expect(xmlAfter).not.toContain('id="73"');
+ expect(xmlAfter).not.toContain('id="70"');
+ expect(xmlAfter).not.toContain('id="67"');
+ expect(xmlAfter).not.toContain('id="47"');
+ expect(xmlAfter).not.toContain('id="44"');
+ expect(xmlAfter).not.toContain('id="41"');
+ expect(xmlAfter).not.toContain('id="27"');
expect(xmlAfter).toMatchSnapshot();
});
@@ -366,19 +366,19 @@ describe('EditorRuntime - Real Cases', () => {
// First operation: remove 13 paragraphs
const removeResult = await runtime.modifyNodes({
operations: [
- { action: 'remove', id: 'x4qr' },
- { action: 'remove', id: 'xfvd' },
- { action: 'remove', id: 'xqzz' },
- { action: 'remove', id: 'zrby' },
- { action: 'remove', id: '02gk' },
- { action: 'remove', id: '0dl6' },
- { action: 'remove', id: '0ops' },
- { action: 'remove', id: '1rnx' },
- { action: 'remove', id: '22sj' },
- { action: 'remove', id: '2dx5' },
- { action: 'remove', id: '3gva' },
- { action: 'remove', id: '3rzw' },
- { action: 'remove', id: '434i' },
+ { action: 'remove', id: '17545' },
+ { action: 'remove', id: '17547' },
+ { action: 'remove', id: '17549' },
+ { action: 'remove', id: '17562' },
+ { action: 'remove', id: '17564' },
+ { action: 'remove', id: '17566' },
+ { action: 'remove', id: '17568' },
+ { action: 'remove', id: '17575' },
+ { action: 'remove', id: '17577' },
+ { action: 'remove', id: '17579' },
+ { action: 'remove', id: '17586' },
+ { action: 'remove', id: '17588' },
+ { action: 'remove', id: '17590' },
],
});
@@ -394,12 +394,12 @@ describe('EditorRuntime - Real Cases', () => {
const removed = editor.getDocument('litexml') as unknown as string;
expect(removed).toMatchSnapshot('remove 13 paragraphs');
- // Second operation: insert a list after wtm5
+ // Second operation: insert a list after 17543
const insertResult = await runtime.modifyNodes({
operations: [
{
action: 'insert',
- afterId: 'wtm5',
+ afterId: '17543',
litexml:
'<ul><li>西湖风景区:杭州的灵魂,世界文化遗产</li><li>灵隐寺:杭州最著名的佛教寺庙</li><li>西溪国家湿地公园:中国第一个国家湿地公园</li><li>宋城:以宋代文化为主题的大型主题公园</li></ul>',
},
diff --git a/packages/editor-runtime/src/__tests__/EditorRuntime.test.ts b/packages/editor-runtime/src/__tests__/EditorRuntime.test.ts
index 65d747384d6..50ed6df7369 100644
--- a/packages/editor-runtime/src/__tests__/EditorRuntime.test.ts
+++ b/packages/editor-runtime/src/__tests__/EditorRuntime.test.ts
@@ -36,9 +36,9 @@ describe('EditorRuntime', () => {
expect(result.nodeCount).toBeGreaterThanOrEqual(0);
expect(result.extractedTitle).toBeUndefined();
- // Verify editor state - full text match (editor adds trailing space)
+ // Verify editor state - full text match
const editorMarkdown = editor.getDocument('markdown') as unknown as string;
- expect(editorMarkdown).toBe('Hello world\n\nThis is a paragraph. \n\n');
+ expect(editorMarkdown).toBe('Hello world\n\nThis is a paragraph.\n');
// Verify XML structure
const editorXml = editor.getDocument('litexml') as unknown as string;
@@ -55,9 +55,9 @@ describe('EditorRuntime', () => {
expect(result.extractedTitle).toBe('My Document Title');
expect(mockTitleSetter).toHaveBeenCalledWith('My Document Title');
- // Verify editor state - only content without title (editor adds trailing space)
+ // Verify editor state - only content without title
const editorMarkdown = editor.getDocument('markdown') as unknown as string;
- expect(editorMarkdown).toBe('This is the content. \n\n');
+ expect(editorMarkdown).toBe('This is the content.\n');
});
it('should handle markdown with multiple headings', async () => {
@@ -69,10 +69,10 @@ describe('EditorRuntime', () => {
// Verify title extraction (only first h1)
expect(result.extractedTitle).toBe('Main Title');
- // Verify editor state - content after title extraction (editor adds trailing space)
+ // Verify editor state - content after title extraction
const editorMarkdown = editor.getDocument('markdown') as unknown as string;
expect(editorMarkdown).toBe(
- '## Section 1\n\nContent here. \n\n## Section 2\n\nMore content. \n\n',
+ '## Section 1\n\nContent here.\n\n## Section 2\n\nMore content.\n',
);
});
@@ -265,8 +265,11 @@ describe('EditorRuntime', () => {
expect(result.successCount).toBe(1);
expect(result.results[0].action).toBe('remove');
- const markdownAfter = editor.getDocument('litexml') as unknown as string;
- expect(markdownAfter).toMatchSnapshot();
+ // Node ids are random per document, so check the id and snapshot the text.
+ expect(editor.getDocument('litexml') as unknown as string).not.toContain(
+ `id="${paragraphId}"`,
+ );
+ expect(editor.getDocument('markdown') as unknown as string).toMatchSnapshot();
});
it('should remove multiple nodes', async () => {
diff --git a/packages/editor-runtime/src/__tests__/__snapshots__/EditorRuntime.real.test.ts.snap b/packages/editor-runtime/src/__tests__/__snapshots__/EditorRuntime.real.test.ts.snap
index 9c6a25e5310..c978afc2400 100644
--- a/packages/editor-runtime/src/__tests__/__snapshots__/EditorRuntime.real.test.ts.snap
+++ b/packages/editor-runtime/src/__tests__/__snapshots__/EditorRuntime.real.test.ts.snap
@@ -38,47 +38,47 @@ exports[`EditorRuntime - Real Cases > modifyNodes - batch modify all paragraphs
exports[`EditorRuntime - Real Cases > modifyNodes - batch remove paragraphs > should remove 7 paragraphs in a single call 1`] = `
"<?xml version="1.0" encoding="UTF-8"?>
<root>
- <p id="lwap">
- <span id="m1v0">杭州,一座被诗意浸润的千年古都,静静地依偎在钱塘江畔,宛如一幅徐徐展开的水墨长卷。这里不仅是浙江省的政治、经济、文化中心,更是中国七大古都之一,承载着2200余年的历史记忆。从南宋临安的繁华盛景到今日数字经济的创新高地,杭州始终以"人间天堂"的美誉,向世界展示着东方文明的独特魅力。</span>
+ <p id="3">
+ <span id="4">杭州,一座被诗意浸润的千年古都,静静地依偎在钱塘江畔,宛如一幅徐徐展开的水墨长卷。这里不仅是浙江省的政治、经济、文化中心,更是中国七大古都之一,承载着2200余年的历史记忆。从南宋临安的繁华盛景到今日数字经济的创新高地,杭州始终以"人间天堂"的美誉,向世界展示着东方文明的独特魅力。</span>
</p>
- <h2 id="m7fb">
- <span id="mczm">地理与气候</span>
+ <h2 id="5">
+ <span id="6">地理与气候</span>
</h2>
- <p id="mijx">
- <span id="mo48">杭州地处钱塘江下游,京杭大运河南端,东临杭州湾,西接天目山。全市总面积16850平方公里,常住人口超过1200万。杭州属于亚热带季风气候,四季分明,雨量充沛,年平均气温17.8℃,气候宜人。</span>
+ <p id="7">
+ <span id="8">杭州地处钱塘江下游,京杭大运河南端,东临杭州湾,西接天目山。全市总面积16850平方公里,常住人口超过1200万。杭州属于亚热带季风气候,四季分明,雨量充沛,年平均气温17.8℃,气候宜人。</span>
</p>
- <h2 id="mtoj">
- <span id="mz8u">历史文化</span>
+ <h2 id="9">
+ <span id="10">历史文化</span>
</h2>
- <p id="n4t5">
- <span id="nadg">杭州是吴越文化和南宋文化的发源地之一。公元1138年,南宋定都临安(今杭州),使其成为当时世界上最繁华的城市之一。杭州拥有丰富的历史文化遗产,包括西湖文化景观、京杭大运河、良渚古城遗址等世界文化遗产。</span>
+ <p id="11">
+ <span id="12">杭州是吴越文化和南宋文化的发源地之一。公元1138年,南宋定都临安(今杭州),使其成为当时世界上最繁华的城市之一。杭州拥有丰富的历史文化遗产,包括西湖文化景观、京杭大运河、良渚古城遗址等世界文化遗产。</span>
</p>
- <p id="vbpc">
- <span id="vh9n">南宋时期(1127-1279年)是杭州历史上的黄金时代。宋室南渡后定都临安(今杭州),使其成为当时世界上人口最多、经济最繁荣的城市之一。马可·波罗在游记中称杭州为"世界上最美丽华贵之天城"。南宋时期杭州的工商业、文化艺术、科学技术都达到了空前的高度:</span>
+ <p id="64">
+ <span id="65">南宋时期(1127-1279年)是杭州历史上的黄金时代。宋室南渡后定都临安(今杭州),使其成为当时世界上人口最多、经济最繁荣的城市之一。马可·波罗在游记中称杭州为"世界上最美丽华贵之天城"。南宋时期杭州的工商业、文化艺术、科学技术都达到了空前的高度:</span>
</p>
- <ul id="t5t2">
- <li id="tbdd">
- <span id="tgxo" bold="true">经济繁荣</span>
- <span id="tmhz">:丝绸、瓷器、茶叶贸易发达,出现了世界上最早的纸币"交子"</span>
+ <ul id="50">
+ <li id="51">
+ <span id="52" bold="true">经济繁荣</span>
+ <span id="53">:丝绸、瓷器、茶叶贸易发达,出现了世界上最早的纸币"交子"</span>
</li>
- <li id="ts2a">
- <span id="txml" bold="true">文化鼎盛</span>
- <span id="u36w">:宋词达到艺术高峰,苏轼、柳永、李清照等文人雅士云集</span>
+ <li id="54">
+ <span id="55" bold="true">文化鼎盛</span>
+ <span id="56">:宋词达到艺术高峰,苏轼、柳永、李清照等文人雅士云集</span>
</li>
- <li id="u8r7">
- <span id="uebi" bold="true">科技创新</span>
- <span id="ujvt">:活字印刷术、指南针、火药等重大发明得到广泛应用</span>
+ <li id="57">
+ <span id="58" bold="true">科技创新</span>
+ <span id="59">:活字印刷术、指南针、火药等重大发明得到广泛应用</span>
</li>
- <li id="upg4">
- <span id="uv0f" bold="true">城市建设</span>
- <span id="v0kq">:形成了"前朝后市"的格局,御街、清河坊等商业区繁华异常</span>
+ <li id="60">
+ <span id="61" bold="true">城市建设</span>
+ <span id="62">:形成了"前朝后市"的格局,御街、清河坊等商业区繁华异常</span>
</li>
</ul>
- <h2 id="nfxr">
- <span id="nli2">西湖风光</span>
+ <h2 id="13">
+ <span id="14">西湖风光</span>
</h2>
- <p id="nr2d">
- <span id="nwmo">西湖是杭州的灵魂,也是中国最著名的风景名胜之一。西湖十景(如苏堤春晓、断桥残雪、雷峰夕照等)闻名遐迩。西湖不仅自然风光秀丽,更承载着深厚的文化内涵,历代文人墨客在此留下了无数诗词歌赋。</span>
+ <p id="15">
+ <span id="16">西湖是杭州的灵魂,也是中国最著名的风景名胜之一。西湖十景(如苏堤春晓、断桥残雪、雷峰夕照等)闻名遐迩。西湖不仅自然风光秀丽,更承载着深厚的文化内涵,历代文人墨客在此留下了无数诗词歌赋。</span>
</p>
</root>"
`;
@@ -86,14 +86,14 @@ exports[`EditorRuntime - Real Cases > modifyNodes - batch remove paragraphs > sh
exports[`EditorRuntime - Real Cases > modifyNodes - remove then add > should remove 13 paragraphs then insert a list > remove 13 paragraphs 1`] = `
"<?xml version="1.0" encoding="UTF-8"?>
<root>
- <p id="wihj">
- <span id="wo1u">杭州是中国著名的旅游城市,素有"人间天堂"的美誉。这里既有美丽的自然风光,又有深厚的历史文化底蕴。</span>
+ <p id="17541">
+ <span id="17542">杭州是中国著名的旅游城市,素有"人间天堂"的美誉。这里既有美丽的自然风光,又有深厚的历史文化底蕴。</span>
</p>
- <h2 id="wtm5">
- <span id="wz6g">必游景点</span>
+ <h2 id="17543">
+ <span id="17544">必游景点</span>
</h2>
- <h2 id="562n">
- <span id="5bmy">美食推荐</span>
+ <h2 id="17597">
+ <span id="17598">美食推荐</span>
</h2>
</root>"
`;
diff --git a/packages/editor-runtime/src/__tests__/__snapshots__/EditorRuntime.test.ts.snap b/packages/editor-runtime/src/__tests__/__snapshots__/EditorRuntime.test.ts.snap
index 88bd5a4e484..e48377b09a5 100644
--- a/packages/editor-runtime/src/__tests__/__snapshots__/EditorRuntime.test.ts.snap
+++ b/packages/editor-runtime/src/__tests__/__snapshots__/EditorRuntime.test.ts.snap
@@ -5,10 +5,9 @@ exports[`EditorRuntime > modifyNodes > error handling > should normalize single
Single op
-First paragraph.
-
-Second paragraph.
+First paragraph.
+Second paragraph.
"
`;
@@ -21,10 +20,9 @@ Insert 2
Insert 3
-First paragraph.
-
-Second paragraph.
+First paragraph.
+Second paragraph.
"
`;
@@ -33,10 +31,9 @@ exports[`EditorRuntime > modifyNodes > insert > should insert single node after
New inserted paragraph
-First paragraph.
-
-Second paragraph.
+First paragraph.
+Second paragraph.
"
`;
@@ -45,10 +42,9 @@ exports[`EditorRuntime > modifyNodes > mixed operations > should handle insert,
New content
-First paragraph.
-
-Second paragraph.
+First paragraph.
+Second paragraph.
"
`;
@@ -57,8 +53,7 @@ exports[`EditorRuntime > modifyNodes > modify > should modify existing node cont
Modified content here
-Second paragraph.
-
+Second paragraph.
"
`;
@@ -68,66 +63,52 @@ exports[`EditorRuntime > modifyNodes > modify > should modify multiple nodes at
Modified first
Modified second
-
"
`;
exports[`EditorRuntime > modifyNodes > remove > should remove existing node 1`] = `
-"<?xml version="1.0" encoding="UTF-8"?>
-<root>
- <h1 id="ll63">
- <span id="lqqe">Title</span>
- </h1>
- <p id="m7fb">
- <span id="mczm">Second paragraph.</span>
- </p>
-</root>"
+"# Title
+
+Second paragraph.
+"
`;
exports[`EditorRuntime > modifyNodes > remove > should remove multiple nodes 1`] = `
"# Title
-
"
`;
exports[`EditorRuntime > replaceText > should replace all occurrences by default 1`] = `
-"Hi world. This is a test. Hi again. Testing the world.
-
+"Hi world. This is a test. Hi again. Testing the world.
"
`;
exports[`EditorRuntime > replaceText > should support regex first occurrence only 1`] = `
-"X world. This is a test. Hello again. Testing the world.
-
+"X world. This is a test. Hello again. Testing the world.
"
`;
exports[`EditorRuntime > replaceText > should support regex patterns with optional groups 1`] = `
-"Hello world. This is a demo. Hello again. Testing the world.
-
+"Hello world. This is a demo. Hello again. Testing the world.
"
`;
exports[`EditorRuntime > replaceText > should support regex with alternation 1`] = `
-"X X. This is a test. X again. Testing the X.
-
+"X X. This is a test. X again. Testing the X.
"
`;
exports[`EditorRuntime > replaceText > should support regex with character classes 1`] = `
-"Guest and Guest are online.
-
+"Guest and Guest are online.
"
`;
exports[`EditorRuntime > replaceText > should support regex with quantifiers 1`] = `
-"Hi world! Hi again!
-
+"Hi world! Hi again!
"
`;
exports[`EditorRuntime > replaceText > should support regex with word boundaries 1`] = `
-"Hello universe. This is a test. Hello again. Testing the universe.
-
+"Hello universe. This is a test. Hello again. Testing the universe.
"
`;