Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync/editor/string/test/sync.1.test.ts
1451 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
8
9
DEVELOPMENT:
10
11
pnpm test sync.1.test.ts
12
13
*/
14
15
import { Client } from "./client-test";
16
import { SyncString } from "../sync";
17
import { once } from "@cocalc/util/async-utils";
18
import { a_txt } from "./data";
19
20
describe("create syncstring and test doing some edits", () => {
21
const { client_id, project_id, path, init_queries } = a_txt();
22
const client = new Client(init_queries, client_id);
23
let syncstring: SyncString;
24
const v = [
25
"SageMathCloud",
26
"SageMathCloud -- Collaborative Calculation",
27
"CoCalc -- Collaborative Calculation",
28
];
29
30
it("creates the syncstring and wait until ready", async () => {
31
syncstring = new SyncString({ project_id, path, client, cursors: true });
32
expect(syncstring.get_state()).toBe("init");
33
await once(syncstring, "ready");
34
});
35
36
it("sets the value and commit change", async () => {
37
for (let i = 0; i < v.length; i++) {
38
syncstring.from_str(v[i]);
39
expect(syncstring.to_str()).toBe(v[i]);
40
expect(syncstring.count()).toBe(v[i].length);
41
expect(syncstring.versions().length).toBe(i);
42
await syncstring.commit();
43
await syncstring.save();
44
expect(syncstring.versions().length).toBe(i + 1);
45
}
46
});
47
48
it("gets the value at each past version", () => {
49
const vers = syncstring.versions();
50
for (let i = 0; i < v.length; i++) {
51
expect(syncstring.version(vers[i]).to_str()).toEqual(v[i]);
52
}
53
});
54
55
it("undo it all", () => {
56
for (let i = 0; i < v.length; i++) {
57
syncstring.undo();
58
let e = v[v.length - i - 2];
59
if (e === undefined) {
60
e = "";
61
}
62
expect(syncstring.to_str()).toEqual(e);
63
expect(syncstring.in_undo_mode()).toBe(true);
64
}
65
// we made lots of changes with undo/redo, and these
66
// automatically result in new commits.
67
expect(syncstring.versions().length).toBe(2 * v.length);
68
});
69
70
it("redo it all (undo mode state continues from above)", () => {
71
expect(syncstring.in_undo_mode()).toBe(true);
72
for (let i = 0; i < v.length; i++) {
73
syncstring.redo();
74
expect(syncstring.to_str()).toEqual(v[i]);
75
}
76
syncstring.exit_undo_mode();
77
expect(syncstring.in_undo_mode()).toBe(false);
78
expect(syncstring.versions().length).toBe(3 * v.length);
79
});
80
81
it("revert to each past point in time", () => {
82
const vers = syncstring.versions();
83
for (let i = 0; i < v.length; i++) {
84
syncstring.revert(vers[i]);
85
expect(syncstring.to_str()).toEqual(v[i]);
86
}
87
// correct, since no commits.
88
expect(syncstring.versions().length).toBe(3 * v.length);
89
});
90
91
it("last_changed is the time of the last version", () => {
92
const vers = syncstring.versions();
93
expect(syncstring.last_changed()).toEqual(vers[vers.length - 1]);
94
});
95
96
it("is not read only", () => {
97
expect(syncstring.is_read_only()).toBe(false);
98
});
99
100
it("save to disk", async () => {
101
expect(syncstring.has_unsaved_changes()).toBe(true);
102
const promise = syncstring.save_to_disk();
103
// Mock: we set save to done in the syncstring
104
// table, otherwise the promise will never resolve.
105
(syncstring as any).set_save({
106
state: "done",
107
error: "",
108
hash: syncstring.hash_of_live_version(),
109
});
110
(syncstring as any).syncstring_table.emit("change-no-throttle");
111
await promise;
112
expect(syncstring.has_unsaved_changes()).toBe(false);
113
});
114
115
it("close and clean up", async () => {
116
await syncstring.close();
117
expect(syncstring.get_state()).toBe("closed");
118
});
119
});
120
121