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.0.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
// This mostly tests the trivial minimal edge cases.
12
describe("create a blank minimal string SyncDoc and call public methods on it", () => {
13
const { client_id, project_id, path, string_id, init_queries } = a_txt();
14
const client = new Client(init_queries, client_id);
15
let syncstring: SyncString;
16
17
it("creates the syncstring and wait for it to be ready", async () => {
18
syncstring = new SyncString({ project_id, path, client });
19
expect(syncstring.get_state()).toBe("init");
20
await once(syncstring, "ready");
21
expect(syncstring.get_state()).toBe("ready");
22
});
23
24
it("call set_cursor_locs, an error since cursors aren't enabled", () => {
25
expect(async () => {
26
await syncstring.set_cursor_locs([]);
27
}).rejects.toThrow("cursors are not enabled");
28
});
29
30
it("calls each public get method", () => {
31
expect(syncstring.get_state()).toBe("ready");
32
expect(syncstring.get_project_id()).toBe(project_id);
33
expect(syncstring.get_path()).toBe(path);
34
expect(syncstring.get_string_id()).toBe(string_id);
35
expect(syncstring.get_my_user_id()).toBe(1);
36
});
37
38
it("the db-style get methods all fail on a string", () => {
39
expect(() => syncstring.get()).toThrow(
40
"queries on strings don't have meaning",
41
);
42
expect(() => syncstring.get_one()).toThrow(
43
"queries on strings don't have meaning",
44
);
45
expect(() => syncstring.delete()).toThrow(
46
"delete on strings doesn't have meaning",
47
);
48
});
49
50
it("get the underlying doc", () => {
51
// via Document
52
expect(syncstring.get_doc().to_str()).toBe("");
53
// directly
54
expect(syncstring.to_str()).toBe("");
55
});
56
57
it("get the size via count", () => {
58
expect(syncstring.count()).toBe(0);
59
});
60
61
it("get current version", () => {
62
expect(syncstring.version().to_str()).toBe("");
63
});
64
65
it("get version without (removing nothing though)", () => {
66
expect(syncstring.version_without([]).to_str()).toBe("");
67
expect(syncstring.version_without([new Date()]).to_str()).toBe("");
68
});
69
70
it("revert to version now (does nothing - no error)", () => {
71
syncstring.revert(new Date());
72
});
73
74
it("undo/redo -- nothing to undo yet...", () => {
75
expect(syncstring.in_undo_mode()).toBe(false);
76
syncstring.undo();
77
expect(syncstring.in_undo_mode()).toBe(true);
78
syncstring.exit_undo_mode();
79
expect(syncstring.in_undo_mode()).toBe(false);
80
syncstring.redo(); // no error
81
});
82
83
it("account_id of change at given point in time gives error", () => {
84
expect(() => syncstring.account_id(new Date())).toThrow("no patch at");
85
});
86
87
it("time sent of change at given point in time gives error", () => {
88
expect(() => syncstring.time_sent(new Date())).toThrow("no patch at");
89
});
90
91
it("user_id of change at given point in time gives error", () => {
92
expect(() => syncstring.user_id(new Date())).toThrow("no patch at");
93
});
94
95
it("get list of versions (should be empty)", () => {
96
expect(syncstring.versions()).toEqual([]);
97
});
98
99
it("get all known versions (also empty)", () => {
100
expect(syncstring.all_versions()).toEqual([]);
101
});
102
103
it("last changed when time began", () => {
104
expect(syncstring.last_changed()).toEqual(new Date(0));
105
});
106
107
it("check ready state", async () => {
108
syncstring.assert_is_ready("check ready state");
109
await syncstring.wait_until_ready(); // trivial since already ready
110
});
111
112
it("wait for an already true condition", async () => {
113
await syncstring.wait(() => true);
114
});
115
116
it("get cursors (error, since cursors not enabled)", async () => {
117
expect(() => syncstring.get_cursors()).toThrow("cursors are not enabled");
118
});
119
120
it("set, then get, something from the settings field", async () => {
121
await syncstring.set_settings({ foo: { bar: "none" } });
122
expect(syncstring.get_settings().get("foo").toJS()).toEqual({
123
bar: "none",
124
});
125
});
126
127
it("verifies it has the full history already", () => {
128
expect(syncstring.has_full_history()).toBe(true);
129
});
130
131
it("loads full history (which does basically nothing)", async () => {
132
await syncstring.load_full_history();
133
});
134
135
it("do a save (no-op, since haven't done anything yet)", async () => {
136
await syncstring.save();
137
});
138
139
it("change the snapshot interval", async () => {
140
await syncstring.set_snapshot_interval(17);
141
expect((syncstring as any).snapshot_interval).toBe(17);
142
});
143
144
it("read only checks", async () => {
145
await syncstring.wait_until_read_only_known(); // no-op
146
expect(syncstring.is_read_only()).toBe(false);
147
});
148
149
it("hashes of versions", () => {
150
expect(syncstring.hash_of_saved_version()).toBe(0);
151
expect(syncstring.hash_of_live_version()).toBe(0);
152
expect(syncstring.has_uncommitted_changes()).toBe(false);
153
});
154
155
it("saves to disk (no-op, since nothing changed)", async () => {
156
await syncstring.save_to_disk();
157
});
158
159
it("close and clean up", async () => {
160
expect(syncstring.get_state()).toBe("ready");
161
await syncstring.close();
162
expect(syncstring.get_state()).toBe("closed");
163
});
164
});
165
166