forked from microsoft/FluidFramework
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sequenceHandler.spec.ts
146 lines (110 loc) · 4.37 KB
/
sequenceHandler.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import { strict as assert } from "assert";
import { SharedString } from "@fluidframework/sequence/internal";
import {
MockContainerRuntimeFactory,
MockFluidDataStoreRuntime,
MockStorage,
} from "@fluidframework/test-runtime-utils/internal";
import { SharedSegmentSequenceUndoRedoHandler } from "../sequenceHandler.js";
import { UndoRedoStackManager } from "../undoRedoStackManager.js";
const text =
"The SharedSegmentSequenceRevertible does the heavy lifting of tracking and reverting changes on the underlying SharedSegmentSequence. This is accomplished via TrackingGroup objects.";
function insertTextAsChunks(sharedString: SharedString, targetLength = text.length) {
let chunks = 0;
while (sharedString.getLength() < targetLength && sharedString.getLength() < text.length) {
const len = (sharedString.getLength() % 13) + 1;
sharedString.insertText(
sharedString.getLength(),
text.substr(sharedString.getLength(), len),
);
chunks++;
}
return chunks;
}
function deleteTextByChunk(sharedString: SharedString, targetLength = 0) {
let chunks = 0;
while (sharedString.getLength() > targetLength && sharedString.getLength() > 0) {
const len = (sharedString.getLength() % 17) + 1;
sharedString.removeText(
Math.max(sharedString.getLength() - len, 0),
sharedString.getLength(),
);
chunks++;
}
return chunks;
}
describe("SharedSegmentSequenceUndoRedoHandler", () => {
const documentId = "fakeId";
let containerRuntimeFactory: MockContainerRuntimeFactory;
let sharedString: SharedString;
let undoRedoStack: UndoRedoStackManager;
beforeEach(() => {
const dataStoreRuntime = new MockFluidDataStoreRuntime({
registry: [SharedString.getFactory()],
});
containerRuntimeFactory = new MockContainerRuntimeFactory();
containerRuntimeFactory.createContainerRuntime(dataStoreRuntime);
const services = {
deltaConnection: dataStoreRuntime.createDeltaConnection(),
objectStorage: new MockStorage(undefined),
};
sharedString = SharedString.create(dataStoreRuntime, documentId);
sharedString.initializeLocal();
sharedString.bindToContext();
sharedString.connect(services);
undoRedoStack = new UndoRedoStackManager();
});
it("Undo and Redo Delete", () => {
insertTextAsChunks(sharedString);
const handler = new SharedSegmentSequenceUndoRedoHandler(undoRedoStack);
handler.attachSequence(sharedString);
deleteTextByChunk(sharedString);
assert.equal(sharedString.getText(), "");
while (undoRedoStack.undoOperation()) {}
assert.equal(sharedString.getText(), text);
while (undoRedoStack.redoOperation()) {}
assert.equal(sharedString.getText(), "");
});
it("Undo and Redo Insert", () => {
const handler = new SharedSegmentSequenceUndoRedoHandler(undoRedoStack);
handler.attachSequence(sharedString);
insertTextAsChunks(sharedString);
assert.equal(sharedString.getText(), text);
while (undoRedoStack.undoOperation()) {}
assert.equal(sharedString.getText(), "");
while (undoRedoStack.redoOperation()) {}
assert.equal(sharedString.getText(), text);
});
it("Undo and Redo Insert & Delete", () => {
const handler = new SharedSegmentSequenceUndoRedoHandler(undoRedoStack);
handler.attachSequence(sharedString);
for (let i = 1; i < text.length; i *= 2) {
insertTextAsChunks(sharedString, text.length - i);
deleteTextByChunk(sharedString, i);
}
const finalText = sharedString.getText();
assert.equal(sharedString.getText(), finalText);
while (undoRedoStack.undoOperation()) {}
assert.equal(sharedString.getText(), "");
while (undoRedoStack.redoOperation()) {}
assert.equal(sharedString.getText(), finalText, sharedString.getText());
}).timeout(4000); // double the default timeout. This test is a bit slow.
it("Undo and redo insert of split segment", () => {
const handler = new SharedSegmentSequenceUndoRedoHandler(undoRedoStack);
handler.attachSequence(sharedString);
// insert all text as a single segment
sharedString.insertText(0, text);
containerRuntimeFactory.processAllMessages();
// this will split that into three segment
sharedString.walkSegments(() => true, 20, 30, undefined, true);
assert.equal(sharedString.getText(), text);
// undo and redo split insert
undoRedoStack.undoOperation();
undoRedoStack.redoOperation();
assert.equal(sharedString.getText(), text);
});
});