fix: unit test and integration test

This commit is contained in:
Lucas.Xu 2025-04-09 09:32:10 +08:00
parent dba8235440
commit 5b6af79dbd
4 changed files with 12 additions and 4 deletions

View file

@ -72,8 +72,11 @@ extension AiWriterNodeExtension on EditorState {
} }
} }
// use \n\n as line break to improve the ai response
// using \n will cause the ai response treat the text as a single line
final markdown = await customDocumentToMarkdown( final markdown = await customDocumentToMarkdown(
Document.blank()..insert([0], slicedNodes), Document.blank()..insert([0], slicedNodes),
lineBreak: '\n\n',
); );
// trim the last \n if it exists // trim the last \n if it exists

View file

@ -424,6 +424,7 @@ class MarkdownTextRobot {
firstMarkdownDelta != null) { firstMarkdownDelta != null) {
final startIndex = selection.startIndex; final startIndex = selection.startIndex;
final length = delta.length - startIndex; final length = delta.length - startIndex;
transaction transaction
..deleteText(firstNode, startIndex, length) ..deleteText(firstNode, startIndex, length)
..insertTextDelta(firstNode, startIndex, firstMarkdownDelta); ..insertTextDelta(firstNode, startIndex, firstMarkdownDelta);
@ -439,7 +440,9 @@ class MarkdownTextRobot {
lastMarkdownNode != null && lastMarkdownNode != null &&
lastMarkdownDelta != null) { lastMarkdownDelta != null) {
final endIndex = selection.endIndex; final endIndex = selection.endIndex;
transaction.deleteText(lastNode, 0, endIndex); transaction.deleteText(lastNode, 0, endIndex);
// if the last node is same as the first node, it means we have replaced the // if the last node is same as the first node, it means we have replaced the
// selected text in the first node. // selected text in the first node.
if (lastMarkdownNode.id != firstMarkdownNode?.id) { if (lastMarkdownNode.id != firstMarkdownNode?.id) {
@ -457,8 +460,9 @@ class MarkdownTextRobot {
} }
// step 4 // step 4
if (nodes.length > 2) { final length = nodes.length - 2;
final middleNodes = nodes.skip(1).take(nodes.length - 2).toList(); if (length > 0) {
final middleNodes = nodes.skip(1).take(length).toList();
transaction.deleteNodes(middleNodes); transaction.deleteNodes(middleNodes);
} }

View file

@ -27,6 +27,7 @@ Future<String> customDocumentToMarkdown(
Document document, { Document document, {
String path = '', String path = '',
AsyncValueSetter<Archive>? onArchive, AsyncValueSetter<Archive>? onArchive,
String lineBreak = '',
}) async { }) async {
final List<Future<ArchiveFile>> fileFutures = []; final List<Future<ArchiveFile>> fileFutures = [];
@ -41,7 +42,7 @@ Future<String> customDocumentToMarkdown(
try { try {
markdown = documentToMarkdown( markdown = documentToMarkdown(
document, document,
lineBreak: '\n\n', lineBreak: lineBreak,
customParsers: [ customParsers: [
const MathEquationNodeParser(), const MathEquationNodeParser(),
const CalloutNodeParser(), const CalloutNodeParser(),

View file

@ -375,7 +375,7 @@ void main() {
await blocResponseFuture(); await blocResponseFuture();
bloc.runResponseAction(SuggestionAction.accept); bloc.runResponseAction(SuggestionAction.accept);
await blocResponseFuture(); await blocResponseFuture();
expect(editorState.document.root.children.length, 1); expect(editorState.document.root.children.length, 2);
expect( expect(
editorState.getNodeAtPath([0])!.delta!.toPlainText(), editorState.getNodeAtPath([0])!.delta!.toPlainText(),
'Hello World', 'Hello World',