CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync/editor/string/test/sync.1.test.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Client } from "./client-test";
7
import { SyncString } from "../sync";
8
import { once } from "@cocalc/util/async-utils";
9
import { a_txt } from "./data";
10
11
describe("create syncstring and test doing some edits", () => {
12
const { client_id, project_id, path, init_queries } = a_txt();
13
const client = new Client(init_queries, client_id);
14
let syncstring: SyncString;
15
const v = [
16
"SageMathCloud",
17
"SageMathCloud -- Collaborative Calculation",
18
"CoCalc -- Collaborative Calculation",
19
];
20
21
it("creates the syncstring and wait until ready", async () => {
22
syncstring = new SyncString({ project_id, path, client, cursors: true });
23
expect(syncstring.get_state()).toBe("init");
24
await once(syncstring, "ready");
25
});
26
27
it("sets the value and commit change", async () => {
28
for (let i = 0; i < v.length; i++) {
29
syncstring.from_str(v[i]);
30
expect(syncstring.to_str()).toBe(v[i]);
31
expect(syncstring.count()).toBe(v[i].length);
32
expect(syncstring.versions().length).toBe(i);
33
await syncstring.save();
34
expect(syncstring.versions().length).toBe(i + 1);
35
}
36
});
37
38
it("gets the value at each past version", () => {
39
const vers = syncstring.versions();
40
for (let i = 0; i < v.length; i++) {
41
expect(syncstring.version(vers[i]).to_str()).toEqual(v[i]);
42
}
43
});
44
45
it("undo it all", () => {
46
for (let i = 0; i < v.length; i++) {
47
syncstring.undo();
48
let e = v[v.length - i - 2];
49
if (e === undefined) {
50
e = "";
51
}
52
expect(syncstring.to_str()).toEqual(e);
53
expect(syncstring.in_undo_mode()).toBe(true);
54
}
55
// we made lots of changes with undo/redo, and these
56
// automatically result in new commits.
57
expect(syncstring.versions().length).toBe(2 * v.length);
58
});
59
60
it("redo it all (undo mode state continues from above)", () => {
61
expect(syncstring.in_undo_mode()).toBe(true);
62
for (let i = 0; i < v.length; i++) {
63
syncstring.redo();
64
expect(syncstring.to_str()).toEqual(v[i]);
65
}
66
syncstring.exit_undo_mode();
67
expect(syncstring.in_undo_mode()).toBe(false);
68
expect(syncstring.versions().length).toBe(3 * v.length);
69
});
70
71
it("revert to each past point in time", () => {
72
const vers = syncstring.versions();
73
for (let i = 0; i < v.length; i++) {
74
syncstring.revert(vers[i]);
75
expect(syncstring.to_str()).toEqual(v[i]);
76
}
77
// correct, since no commits.
78
expect(syncstring.versions().length).toBe(3 * v.length);
79
});
80
81
it("gets info about patch at a given point in time", () => {
82
const t = syncstring.versions()[1];
83
expect(syncstring.account_id(t)).toEqual(client_id);
84
85
// time sent is not set since patch wasn't made offline.
86
expect(syncstring.time_sent(t)).toBe(undefined);
87
88
expect(syncstring.user_id(t)).toEqual(1);
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